org.el 553 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914
  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.17
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. (require 'org-footnote)
  85. ;;;; Customization variables
  86. ;;; Version
  87. (defconst org-version "6.17"
  88. "The version number of the file org.el.")
  89. (defun org-version (&optional here)
  90. "Show the org-mode version in the echo area.
  91. With prefix arg HERE, insert it at point."
  92. (interactive "P")
  93. (let ((version (format "Org-mode version %s" org-version)))
  94. (message version)
  95. (if here
  96. (insert version))))
  97. ;;; Compatibility constants
  98. ;;; The custom variables
  99. (defgroup org nil
  100. "Outline-based notes management and organizer."
  101. :tag "Org"
  102. :group 'outlines
  103. :group 'hypermedia
  104. :group 'calendar)
  105. (defcustom org-load-hook nil
  106. "Hook that is run after org.el has been loaded."
  107. :group 'org
  108. :type 'hook)
  109. (defvar org-modules) ; defined below
  110. (defvar org-modules-loaded nil
  111. "Have the modules been loaded already?")
  112. (defun org-load-modules-maybe (&optional force)
  113. "Load all extensions listed in `org-modules'."
  114. (when (or force (not org-modules-loaded))
  115. (mapc (lambda (ext)
  116. (condition-case nil (require ext)
  117. (error (message "Problems while trying to load feature `%s'" ext))))
  118. org-modules)
  119. (setq org-modules-loaded t)))
  120. (defun org-set-modules (var value)
  121. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  122. (set var value)
  123. (when (featurep 'org)
  124. (org-load-modules-maybe 'force)))
  125. (when (org-bound-and-true-p org-modules)
  126. (let ((a (member 'org-infojs org-modules)))
  127. (and a (setcar a 'org-jsinfo))))
  128. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  129. "Modules that should always be loaded together with org.el.
  130. If a description starts with <C>, the file is not part of Emacs
  131. and loading it will require that you have downloaded and properly installed
  132. the org-mode distribution.
  133. You can also use this system to load external packages (i.e. neither Org
  134. core modules, not modules from the CONTRIB directory). Just add symbols
  135. to the end of the list. If the package is called org-xyz.el, then you need
  136. to add the symbol `xyz', and the package must have a call to
  137. (provide 'org-xyz)"
  138. :group 'org
  139. :set 'org-set-modules
  140. :type
  141. '(set :greedy t
  142. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  143. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  144. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  145. (const :tag " id: Global IDs for identifying entries" org-id)
  146. (const :tag " info: Links to Info nodes" org-info)
  147. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  148. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  149. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  150. (const :tag " mew Links to Mew folders/messages" org-mew)
  151. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  152. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  153. (const :tag " vm: Links to VM folders/messages" org-vm)
  154. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  155. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  156. (const :tag " mouse: Additional mouse support" org-mouse)
  157. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  158. (const :tag "C annotation-helper: Call Remember directly from Browser" org-annotation-helper)
  159. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  160. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  161. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  162. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  163. (const :tag "C eval: Include command output as text" org-eval)
  164. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  165. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  166. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  167. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  168. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  169. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  170. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  171. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  172. (const :tag "C registry: A registry for Org links" org-registry)
  173. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  174. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  175. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  176. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  177. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  178. (defgroup org-startup nil
  179. "Options concerning startup of Org-mode."
  180. :tag "Org Startup"
  181. :group 'org)
  182. (defcustom org-startup-folded t
  183. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  184. This can also be configured on a per-file basis by adding one of
  185. the following lines anywhere in the buffer:
  186. #+STARTUP: fold
  187. #+STARTUP: nofold
  188. #+STARTUP: content"
  189. :group 'org-startup
  190. :type '(choice
  191. (const :tag "nofold: show all" nil)
  192. (const :tag "fold: overview" t)
  193. (const :tag "content: all headlines" content)))
  194. (defcustom org-startup-truncated t
  195. "Non-nil means, entering Org-mode will set `truncate-lines'.
  196. This is useful since some lines containing links can be very long and
  197. uninteresting. Also tables look terrible when wrapped."
  198. :group 'org-startup
  199. :type 'boolean)
  200. (defcustom org-startup-align-all-tables nil
  201. "Non-nil means, align all tables when visiting a file.
  202. This is useful when the column width in tables is forced with <N> cookies
  203. in table fields. Such tables will look correct only after the first re-align.
  204. This can also be configured on a per-file basis by adding one of
  205. the following lines anywhere in the buffer:
  206. #+STARTUP: align
  207. #+STARTUP: noalign"
  208. :group 'org-startup
  209. :type 'boolean)
  210. (defcustom org-insert-mode-line-in-empty-file nil
  211. "Non-nil means insert the first line setting Org-mode in empty files.
  212. When the function `org-mode' is called interactively in an empty file, this
  213. normally means that the file name does not automatically trigger Org-mode.
  214. To ensure that the file will always be in Org-mode in the future, a
  215. line enforcing Org-mode will be inserted into the buffer, if this option
  216. has been set."
  217. :group 'org-startup
  218. :type 'boolean)
  219. (defcustom org-replace-disputed-keys nil
  220. "Non-nil means use alternative key bindings for some keys.
  221. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  222. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  223. If you want to use Org-mode together with one of these other modes,
  224. or more generally if you would like to move some Org-mode commands to
  225. other keys, set this variable and configure the keys with the variable
  226. `org-disputed-keys'.
  227. This option is only relevant at load-time of Org-mode, and must be set
  228. *before* org.el is loaded. Changing it requires a restart of Emacs to
  229. become effective."
  230. :group 'org-startup
  231. :type 'boolean)
  232. (defcustom org-use-extra-keys nil
  233. "Non-nil means use extra key sequence definitions for certain
  234. commands. This happens automatically if you run XEmacs or if
  235. window-system is nil. This variable lets you do the same
  236. manually. You must set it before loading org.
  237. Example: on Carbon Emacs 22 running graphically, with an external
  238. keyboard on a Powerbook, the default way of setting M-left might
  239. not work for either Alt or ESC. Setting this variable will make
  240. it work for ESC."
  241. :group 'org-startup
  242. :type 'boolean)
  243. (if (fboundp 'defvaralias)
  244. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  245. (defcustom org-disputed-keys
  246. '(([(shift up)] . [(meta p)])
  247. ([(shift down)] . [(meta n)])
  248. ([(shift left)] . [(meta -)])
  249. ([(shift right)] . [(meta +)])
  250. ([(control shift right)] . [(meta shift +)])
  251. ([(control shift left)] . [(meta shift -)]))
  252. "Keys for which Org-mode and other modes compete.
  253. This is an alist, cars are the default keys, second element specifies
  254. the alternative to use when `org-replace-disputed-keys' is t.
  255. Keys can be specified in any syntax supported by `define-key'.
  256. The value of this option takes effect only at Org-mode's startup,
  257. therefore you'll have to restart Emacs to apply it after changing."
  258. :group 'org-startup
  259. :type 'alist)
  260. (defun org-key (key)
  261. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  262. Or return the original if not disputed."
  263. (if org-replace-disputed-keys
  264. (let* ((nkey (key-description key))
  265. (x (org-find-if (lambda (x)
  266. (equal (key-description (car x)) nkey))
  267. org-disputed-keys)))
  268. (if x (cdr x) key))
  269. key))
  270. (defun org-find-if (predicate seq)
  271. (catch 'exit
  272. (while seq
  273. (if (funcall predicate (car seq))
  274. (throw 'exit (car seq))
  275. (pop seq)))))
  276. (defun org-defkey (keymap key def)
  277. "Define a key, possibly translated, as returned by `org-key'."
  278. (define-key keymap (org-key key) def))
  279. (defcustom org-ellipsis nil
  280. "The ellipsis to use in the Org-mode outline.
  281. When nil, just use the standard three dots. When a string, use that instead,
  282. When a face, use the standard 3 dots, but with the specified face.
  283. The change affects only Org-mode (which will then use its own display table).
  284. Changing this requires executing `M-x org-mode' in a buffer to become
  285. effective."
  286. :group 'org-startup
  287. :type '(choice (const :tag "Default" nil)
  288. (face :tag "Face" :value org-warning)
  289. (string :tag "String" :value "...#")))
  290. (defvar org-display-table nil
  291. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  292. (defgroup org-keywords nil
  293. "Keywords in Org-mode."
  294. :tag "Org Keywords"
  295. :group 'org)
  296. (defcustom org-deadline-string "DEADLINE:"
  297. "String to mark deadline entries.
  298. A deadline is this string, followed by a time stamp. Should be a word,
  299. terminated by a colon. You can insert a schedule keyword and
  300. a timestamp with \\[org-deadline].
  301. Changes become only effective after restarting Emacs."
  302. :group 'org-keywords
  303. :type 'string)
  304. (defcustom org-scheduled-string "SCHEDULED:"
  305. "String to mark scheduled TODO entries.
  306. A schedule is this string, followed by a time stamp. Should be a word,
  307. terminated by a colon. You can insert a schedule keyword and
  308. a timestamp with \\[org-schedule].
  309. Changes become only effective after restarting Emacs."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-closed-string "CLOSED:"
  313. "String used as the prefix for timestamps logging closing a TODO entry."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-clock-string "CLOCK:"
  317. "String used as prefix for timestamps clocking work hours on an item."
  318. :group 'org-keywords
  319. :type 'string)
  320. (defcustom org-comment-string "COMMENT"
  321. "Entries starting with this keyword will never be exported.
  322. An entry can be toggled between COMMENT and normal with
  323. \\[org-toggle-comment].
  324. Changes become only effective after restarting Emacs."
  325. :group 'org-keywords
  326. :type 'string)
  327. (defcustom org-quote-string "QUOTE"
  328. "Entries starting with this keyword will be exported in fixed-width font.
  329. Quoting applies only to the text in the entry following the headline, and does
  330. not extend beyond the next headline, even if that is lower level.
  331. An entry can be toggled between QUOTE and normal with
  332. \\[org-toggle-fixed-width-section]."
  333. :group 'org-keywords
  334. :type 'string)
  335. (defconst org-repeat-re
  336. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  337. "Regular expression for specifying repeated events.
  338. After a match, group 1 contains the repeat expression.")
  339. (defgroup org-structure nil
  340. "Options concerning the general structure of Org-mode files."
  341. :tag "Org Structure"
  342. :group 'org)
  343. (defgroup org-reveal-location nil
  344. "Options about how to make context of a location visible."
  345. :tag "Org Reveal Location"
  346. :group 'org-structure)
  347. (defconst org-context-choice
  348. '(choice
  349. (const :tag "Always" t)
  350. (const :tag "Never" nil)
  351. (repeat :greedy t :tag "Individual contexts"
  352. (cons
  353. (choice :tag "Context"
  354. (const agenda)
  355. (const org-goto)
  356. (const occur-tree)
  357. (const tags-tree)
  358. (const link-search)
  359. (const mark-goto)
  360. (const bookmark-jump)
  361. (const isearch)
  362. (const default))
  363. (boolean))))
  364. "Contexts for the reveal options.")
  365. (defcustom org-show-hierarchy-above '((default . t))
  366. "Non-nil means, show full hierarchy when revealing a location.
  367. Org-mode often shows locations in an org-mode file which might have
  368. been invisible before. When this is set, the hierarchy of headings
  369. above the exposed location is shown.
  370. Turning this off for example for sparse trees makes them very compact.
  371. Instead of t, this can also be an alist specifying this option for different
  372. contexts. Valid contexts are
  373. agenda when exposing an entry from the agenda
  374. org-goto when using the command `org-goto' on key C-c C-j
  375. occur-tree when using the command `org-occur' on key C-c /
  376. tags-tree when constructing a sparse tree based on tags matches
  377. link-search when exposing search matches associated with a link
  378. mark-goto when exposing the jump goal of a mark
  379. bookmark-jump when exposing a bookmark location
  380. isearch when exiting from an incremental search
  381. default default for all contexts not set explicitly"
  382. :group 'org-reveal-location
  383. :type org-context-choice)
  384. (defcustom org-show-following-heading '((default . nil))
  385. "Non-nil means, show following heading when revealing a location.
  386. Org-mode often shows locations in an org-mode file which might have
  387. been invisible before. When this is set, the heading following the
  388. match is shown.
  389. Turning this off for example for sparse trees makes them very compact,
  390. but makes it harder to edit the location of the match. In such a case,
  391. use the command \\[org-reveal] to show more context.
  392. Instead of t, this can also be an alist specifying this option for different
  393. contexts. See `org-show-hierarchy-above' for valid contexts."
  394. :group 'org-reveal-location
  395. :type org-context-choice)
  396. (defcustom org-show-siblings '((default . nil) (isearch t))
  397. "Non-nil means, show all sibling heading when revealing a location.
  398. Org-mode often shows locations in an org-mode file which might have
  399. been invisible before. When this is set, the sibling of the current entry
  400. heading are all made visible. If `org-show-hierarchy-above' is t,
  401. the same happens on each level of the hierarchy above the current entry.
  402. By default this is on for the isearch context, off for all other contexts.
  403. Turning this off for example for sparse trees makes them very compact,
  404. but makes it harder to edit the location of the match. In such a case,
  405. use the command \\[org-reveal] to show more context.
  406. Instead of t, this can also be an alist specifying this option for different
  407. contexts. See `org-show-hierarchy-above' for valid contexts."
  408. :group 'org-reveal-location
  409. :type org-context-choice)
  410. (defcustom org-show-entry-below '((default . nil))
  411. "Non-nil means, show the entry below a headline when revealing a location.
  412. Org-mode often shows locations in an org-mode file which might have
  413. been invisible before. When this is set, the text below the headline that is
  414. exposed is also shown.
  415. By default this is off for all contexts.
  416. Instead of t, this can also be an alist specifying this option for different
  417. contexts. See `org-show-hierarchy-above' for valid contexts."
  418. :group 'org-reveal-location
  419. :type org-context-choice)
  420. (defcustom org-indirect-buffer-display 'other-window
  421. "How should indirect tree buffers be displayed?
  422. This applies to indirect buffers created with the commands
  423. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  424. Valid values are:
  425. current-window Display in the current window
  426. other-window Just display in another window.
  427. dedicated-frame Create one new frame, and re-use it each time.
  428. new-frame Make a new frame each time. Note that in this case
  429. previously-made indirect buffers are kept, and you need to
  430. kill these buffers yourself."
  431. :group 'org-structure
  432. :group 'org-agenda-windows
  433. :type '(choice
  434. (const :tag "In current window" current-window)
  435. (const :tag "In current frame, other window" other-window)
  436. (const :tag "Each time a new frame" new-frame)
  437. (const :tag "One dedicated frame" dedicated-frame)))
  438. (defgroup org-cycle nil
  439. "Options concerning visibility cycling in Org-mode."
  440. :tag "Org Cycle"
  441. :group 'org-structure)
  442. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  443. "Names of drawers. Drawers are not opened by cycling on the headline above.
  444. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  445. this:
  446. :DRAWERNAME:
  447. .....
  448. :END:
  449. The drawer \"PROPERTIES\" is special for capturing properties through
  450. the property API.
  451. Drawers can be defined on the per-file basis with a line like:
  452. #+DRAWERS: HIDDEN STATE PROPERTIES"
  453. :group 'org-structure
  454. :type '(repeat (string :tag "Drawer Name")))
  455. (defcustom org-cycle-global-at-bob nil
  456. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  457. This makes it possible to do global cycling without having to use S-TAB or
  458. C-u TAB. For this special case to work, the first line of the buffer
  459. must not be a headline - it may be empty ot some other text. When used in
  460. this way, `org-cycle-hook' is disables temporarily, to make sure the
  461. cursor stays at the beginning of the buffer.
  462. When this option is nil, don't do anything special at the beginning
  463. of the buffer."
  464. :group 'org-cycle
  465. :type 'boolean)
  466. (defcustom org-cycle-emulate-tab t
  467. "Where should `org-cycle' emulate TAB.
  468. nil Never
  469. white Only in completely white lines
  470. whitestart Only at the beginning of lines, before the first non-white char
  471. t Everywhere except in headlines
  472. exc-hl-bol Everywhere except at the start of a headline
  473. If TAB is used in a place where it does not emulate TAB, the current subtree
  474. visibility is cycled."
  475. :group 'org-cycle
  476. :type '(choice (const :tag "Never" nil)
  477. (const :tag "Only in completely white lines" white)
  478. (const :tag "Before first char in a line" whitestart)
  479. (const :tag "Everywhere except in headlines" t)
  480. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  481. ))
  482. (defcustom org-cycle-separator-lines 2
  483. "Number of empty lines needed to keep an empty line between collapsed trees.
  484. If you leave an empty line between the end of a subtree and the following
  485. headline, this empty line is hidden when the subtree is folded.
  486. Org-mode will leave (exactly) one empty line visible if the number of
  487. empty lines is equal or larger to the number given in this variable.
  488. So the default 2 means, at least 2 empty lines after the end of a subtree
  489. are needed to produce free space between a collapsed subtree and the
  490. following headline.
  491. Special case: when 0, never leave empty lines in collapsed view."
  492. :group 'org-cycle
  493. :type 'integer)
  494. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  495. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  496. org-cycle-hide-drawers
  497. org-cycle-show-empty-lines
  498. org-optimize-window-after-visibility-change)
  499. "Hook that is run after `org-cycle' has changed the buffer visibility.
  500. The function(s) in this hook must accept a single argument which indicates
  501. the new state that was set by the most recent `org-cycle' command. The
  502. argument is a symbol. After a global state change, it can have the values
  503. `overview', `content', or `all'. After a local state change, it can have
  504. the values `folded', `children', or `subtree'."
  505. :group 'org-cycle
  506. :type 'hook)
  507. (defgroup org-edit-structure nil
  508. "Options concerning structure editing in Org-mode."
  509. :tag "Org Edit Structure"
  510. :group 'org-structure)
  511. (defcustom org-odd-levels-only nil
  512. "Non-nil means, skip even levels and only use odd levels for the outline.
  513. This has the effect that two stars are being added/taken away in
  514. promotion/demotion commands. It also influences how levels are
  515. handled by the exporters.
  516. Changing it requires restart of `font-lock-mode' to become effective
  517. for fontification also in regions already fontified.
  518. You may also set this on a per-file basis by adding one of the following
  519. lines to the buffer:
  520. #+STARTUP: odd
  521. #+STARTUP: oddeven"
  522. :group 'org-edit-structure
  523. :group 'org-font-lock
  524. :type 'boolean)
  525. (defcustom org-adapt-indentation t
  526. "Non-nil means, adapt indentation when promoting and demoting.
  527. When this is set and the *entire* text in an entry is indented, the
  528. indentation is increased by one space in a demotion command, and
  529. decreased by one in a promotion command. If any line in the entry
  530. body starts at column 0, indentation is not changed at all."
  531. :group 'org-edit-structure
  532. :type 'boolean)
  533. (defcustom org-special-ctrl-a/e nil
  534. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  535. When t, `C-a' will bring back the cursor to the beginning of the
  536. headline text, i.e. after the stars and after a possible TODO keyword.
  537. In an item, this will be the position after the bullet.
  538. When the cursor is already at that position, another `C-a' will bring
  539. it to the beginning of the line.
  540. `C-e' will jump to the end of the headline, ignoring the presence of tags
  541. in the headline. A second `C-e' will then jump to the true end of the
  542. line, after any tags.
  543. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  544. and only a directly following, identical keypress will bring the cursor
  545. to the special positions."
  546. :group 'org-edit-structure
  547. :type '(choice
  548. (const :tag "off" nil)
  549. (const :tag "after bullet first" t)
  550. (const :tag "border first" reversed)))
  551. (if (fboundp 'defvaralias)
  552. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  553. (defcustom org-special-ctrl-k nil
  554. "Non-nil means `C-k' will behave specially in headlines.
  555. When nil, `C-k' will call the default `kill-line' command.
  556. When t, the following will happen while the cursor is in the headline:
  557. - When the cursor is at the beginning of a headline, kill the entire
  558. line and possible the folded subtree below the line.
  559. - When in the middle of the headline text, kill the headline up to the tags.
  560. - When after the headline text, kill the tags."
  561. :group 'org-edit-structure
  562. :type 'boolean)
  563. (defcustom org-yank-folded-subtrees t
  564. "Non-nil means, when yanking subtrees, fold them.
  565. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  566. it starts with a heading and all other headings in it are either children
  567. or siblings, then fold all the subtrees. However, do this only if no
  568. text after the yank would be swallowed into a folded tree by this action."
  569. :group 'org-edit-structure
  570. :type 'boolean)
  571. (defcustom org-yank-adjusted-subtrees t
  572. "Non-nil means, when yanking subtrees, adjust the level.
  573. With this setting, `org-paste-subtree' is used to insert the subtree, see
  574. this function for details."
  575. :group 'org-edit-structure
  576. :type 'boolean)
  577. (defcustom org-M-RET-may-split-line '((default . t))
  578. "Non-nil means, M-RET will split the line at the cursor position.
  579. When nil, it will go to the end of the line before making a
  580. new line.
  581. You may also set this option in a different way for different
  582. contexts. Valid contexts are:
  583. headline when creating a new headline
  584. item when creating a new item
  585. table in a table field
  586. default the value to be used for all contexts not explicitly
  587. customized"
  588. :group 'org-structure
  589. :group 'org-table
  590. :type '(choice
  591. (const :tag "Always" t)
  592. (const :tag "Never" nil)
  593. (repeat :greedy t :tag "Individual contexts"
  594. (cons
  595. (choice :tag "Context"
  596. (const headline)
  597. (const item)
  598. (const table)
  599. (const default))
  600. (boolean)))))
  601. (defcustom org-insert-heading-respect-content nil
  602. "Non-nil means, insert new headings after the current subtree.
  603. When nil, the new heading is created directly after the current line.
  604. The commands \\[org-insert-heading-respect-content] and
  605. \\[org-insert-todo-heading-respect-content] turn this variable on
  606. for the duration of the command."
  607. :group 'org-structure
  608. :type 'boolean)
  609. (defcustom org-blank-before-new-entry '((heading . nil)
  610. (plain-list-item . nil))
  611. "Should `org-insert-heading' leave a blank line before new heading/item?
  612. The value is an alist, with `heading' and `plain-list-item' as car,
  613. and a boolean flag as cdr."
  614. :group 'org-edit-structure
  615. :type '(list
  616. (cons (const heading) (boolean))
  617. (cons (const plain-list-item) (boolean))))
  618. (defcustom org-insert-heading-hook nil
  619. "Hook being run after inserting a new heading."
  620. :group 'org-edit-structure
  621. :type 'hook)
  622. (defcustom org-enable-fixed-width-editor t
  623. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  624. This currently only means, they are never auto-wrapped.
  625. When nil, such lines will be treated like ordinary lines.
  626. See also the QUOTE keyword."
  627. :group 'org-edit-structure
  628. :type 'boolean)
  629. (defcustom org-edit-src-region-extra nil
  630. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  631. For examples see the function `org-edit-src-find-region-and-lang'.
  632. The regular expression identifying the begin marker should end with a newline,
  633. and the regexp marking the end line should start with a newline, to make sure
  634. there are kept outside the narrowed region."
  635. :group 'org-edit-structure
  636. :type '(repeat
  637. (list
  638. (regexp :tag "begin regexp")
  639. (regexp :tag "end regexp")
  640. (choice :tag "language"
  641. (string :tag "specify")
  642. (integer :tag "from match group")
  643. (const :tag "from `lang' element")
  644. (const :tag "from `style' element")))))
  645. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  646. "The mode that should be used to edit fixed-width regions.
  647. These are the regions where each line starts with a colon."
  648. :group 'org-edit-structure
  649. :type '(choice
  650. (const artist-mode)
  651. (const picture-mode)
  652. (const fundamental-mode)
  653. (function :tag "Other (specify)")))
  654. (defcustom org-goto-auto-isearch t
  655. "Non-nil means, typing characters in org-goto starts incremental search."
  656. :group 'org-edit-structure
  657. :type 'boolean)
  658. (defgroup org-sparse-trees nil
  659. "Options concerning sparse trees in Org-mode."
  660. :tag "Org Sparse Trees"
  661. :group 'org-structure)
  662. (defcustom org-highlight-sparse-tree-matches t
  663. "Non-nil means, highlight all matches that define a sparse tree.
  664. The highlights will automatically disappear the next time the buffer is
  665. changed by an edit command."
  666. :group 'org-sparse-trees
  667. :type 'boolean)
  668. (defcustom org-remove-highlights-with-change t
  669. "Non-nil means, any change to the buffer will remove temporary highlights.
  670. Such highlights are created by `org-occur' and `org-clock-display'.
  671. When nil, `C-c C-c needs to be used to get rid of the highlights.
  672. The highlights created by `org-preview-latex-fragment' always need
  673. `C-c C-c' to be removed."
  674. :group 'org-sparse-trees
  675. :group 'org-time
  676. :type 'boolean)
  677. (defcustom org-occur-hook '(org-first-headline-recenter)
  678. "Hook that is run after `org-occur' has constructed a sparse tree.
  679. This can be used to recenter the window to show as much of the structure
  680. as possible."
  681. :group 'org-sparse-trees
  682. :type 'hook)
  683. (defgroup org-imenu-and-speedbar nil
  684. "Options concerning imenu and speedbar in Org-mode."
  685. :tag "Org Imenu and Speedbar"
  686. :group 'org-structure)
  687. (defcustom org-imenu-depth 2
  688. "The maximum level for Imenu access to Org-mode headlines.
  689. This also applied for speedbar access."
  690. :group 'org-imenu-and-speedbar
  691. :type 'number)
  692. (defgroup org-table nil
  693. "Options concerning tables in Org-mode."
  694. :tag "Org Table"
  695. :group 'org)
  696. (defcustom org-enable-table-editor 'optimized
  697. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  698. When nil, such lines will be treated like ordinary lines.
  699. When equal to the symbol `optimized', the table editor will be optimized to
  700. do the following:
  701. - Automatic overwrite mode in front of whitespace in table fields.
  702. This makes the structure of the table stay in tact as long as the edited
  703. field does not exceed the column width.
  704. - Minimize the number of realigns. Normally, the table is aligned each time
  705. TAB or RET are pressed to move to another field. With optimization this
  706. happens only if changes to a field might have changed the column width.
  707. Optimization requires replacing the functions `self-insert-command',
  708. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  709. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  710. very good at guessing when a re-align will be necessary, but you can always
  711. force one with \\[org-ctrl-c-ctrl-c].
  712. If you would like to use the optimized version in Org-mode, but the
  713. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  714. This variable can be used to turn on and off the table editor during a session,
  715. but in order to toggle optimization, a restart is required.
  716. See also the variable `org-table-auto-blank-field'."
  717. :group 'org-table
  718. :type '(choice
  719. (const :tag "off" nil)
  720. (const :tag "on" t)
  721. (const :tag "on, optimized" optimized)))
  722. (defcustom org-table-tab-recognizes-table.el t
  723. "Non-nil means, TAB will automatically notice a table.el table.
  724. When it sees such a table, it moves point into it and - if necessary -
  725. calls `table-recognize-table'."
  726. :group 'org-table-editing
  727. :type 'boolean)
  728. (defgroup org-link nil
  729. "Options concerning links in Org-mode."
  730. :tag "Org Link"
  731. :group 'org)
  732. (defvar org-link-abbrev-alist-local nil
  733. "Buffer-local version of `org-link-abbrev-alist', which see.
  734. The value of this is taken from the #+LINK lines.")
  735. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  736. (defcustom org-link-abbrev-alist nil
  737. "Alist of link abbreviations.
  738. The car of each element is a string, to be replaced at the start of a link.
  739. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  740. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  741. [[linkkey:tag][description]]
  742. If REPLACE is a string, the tag will simply be appended to create the link.
  743. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  744. the placeholder \"%h\" will cause a url-encoded version of the tag to
  745. be inserted at that point (see the function `url-hexify-string').
  746. REPLACE may also be a function that will be called with the tag as the
  747. only argument to create the link, which should be returned as a string.
  748. See the manual for examples."
  749. :group 'org-link
  750. :type '(repeat
  751. (cons
  752. (string :tag "Protocol")
  753. (choice
  754. (string :tag "Format")
  755. (function)))))
  756. (defcustom org-descriptive-links t
  757. "Non-nil means, hide link part and only show description of bracket links.
  758. Bracket links are like [[link][description]]. This variable sets the initial
  759. state in new org-mode buffers. The setting can then be toggled on a
  760. per-buffer basis from the Org->Hyperlinks menu."
  761. :group 'org-link
  762. :type 'boolean)
  763. (defcustom org-link-file-path-type 'adaptive
  764. "How the path name in file links should be stored.
  765. Valid values are:
  766. relative Relative to the current directory, i.e. the directory of the file
  767. into which the link is being inserted.
  768. absolute Absolute path, if possible with ~ for home directory.
  769. noabbrev Absolute path, no abbreviation of home directory.
  770. adaptive Use relative path for files in the current directory and sub-
  771. directories of it. For other files, use an absolute path."
  772. :group 'org-link
  773. :type '(choice
  774. (const relative)
  775. (const absolute)
  776. (const noabbrev)
  777. (const adaptive)))
  778. (defcustom org-activate-links '(bracket angle plain radio tag date footnote)
  779. "Types of links that should be activated in Org-mode files.
  780. This is a list of symbols, each leading to the activation of a certain link
  781. type. In principle, it does not hurt to turn on most link types - there may
  782. be a small gain when turning off unused link types. The types are:
  783. bracket The recommended [[link][description]] or [[link]] links with hiding.
  784. angular Links in angular brackets that may contain whitespace like
  785. <bbdb:Carsten Dominik>.
  786. plain Plain links in normal text, no whitespace, like http://google.com.
  787. radio Text that is matched by a radio target, see manual for details.
  788. tag Tag settings in a headline (link to tag search).
  789. date Time stamps (link to calendar).
  790. footnote Footnote labels.
  791. Changing this variable requires a restart of Emacs to become effective."
  792. :group 'org-link
  793. :type '(set :greedy t
  794. (const :tag "Double bracket links (new style)" bracket)
  795. (const :tag "Angular bracket links (old style)" angular)
  796. (const :tag "Plain text links" plain)
  797. (const :tag "Radio target matches" radio)
  798. (const :tag "Tags" tag)
  799. (const :tag "Timestamps" date)
  800. (const :tag "Footnotes" footnote)))
  801. (defcustom org-make-link-description-function nil
  802. "Function to use to generate link descriptions from links. If
  803. nil the link location will be used. This function must take two
  804. parameters; the first is the link and the second the description
  805. org-insert-link has generated, and should return the description
  806. to use."
  807. :group 'org-link
  808. :type 'function)
  809. (defgroup org-link-store nil
  810. "Options concerning storing links in Org-mode."
  811. :tag "Org Store Link"
  812. :group 'org-link)
  813. (defcustom org-email-link-description-format "Email %c: %.30s"
  814. "Format of the description part of a link to an email or usenet message.
  815. The following %-escapes will be replaced by corresponding information:
  816. %F full \"From\" field
  817. %f name, taken from \"From\" field, address if no name
  818. %T full \"To\" field
  819. %t first name in \"To\" field, address if no name
  820. %c correspondent. Usually \"from NAME\", but if you sent it yourself, it
  821. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  822. %s subject
  823. %m message-id.
  824. You may use normal field width specification between the % and the letter.
  825. This is for example useful to limit the length of the subject.
  826. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  827. :group 'org-link-store
  828. :type 'string)
  829. (defcustom org-from-is-user-regexp
  830. (let (r1 r2)
  831. (when (and user-mail-address (not (string= user-mail-address "")))
  832. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  833. (when (and user-full-name (not (string= user-full-name "")))
  834. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  835. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  836. "Regexp matched against the \"From:\" header of an email or usenet message.
  837. It should match if the message is from the user him/herself."
  838. :group 'org-link-store
  839. :type 'regexp)
  840. (defcustom org-link-to-org-use-id 'create-if-interactive
  841. "Non-nil means, storing a link to an Org file will use entry IDs.
  842. Note that before this variable is even considered, org-id must be loaded,
  843. to please customize `org-modules' and turn it on.
  844. The variable can have the following values:
  845. t Create an ID if needed to make a link to the current entry.
  846. create-if-interactive
  847. If `org-store-link' is called directly (interactively, as a user
  848. command), do create an ID to support the link. But when doing the
  849. job for remember, only use the ID if it already exists. The
  850. purpose of this setting is to avoid proliferation of unwanted
  851. IDs, just because you happen to be in an Org file when you
  852. call `org-remember' that automatically and preemptively
  853. creates a link. If you do want to get an ID link in a remember
  854. template to an entry not having an ID, create it first by
  855. explicitly creating a link to it, using `C-c C-l' first.
  856. use-existing
  857. Use existing ID, do not create one.
  858. nil Never use an ID to make a link, instead link using a text search for
  859. the headline text."
  860. :group 'org-link-store
  861. :type '(choice
  862. (const :tag "Create ID to make link" t)
  863. (const :tag "Create if string link interactively"
  864. 'create-if-interactive)
  865. (const :tag "Only use existing" 'use-existing)
  866. (const :tag "Do not use ID to create link" nil)))
  867. (defcustom org-context-in-file-links t
  868. "Non-nil means, file links from `org-store-link' contain context.
  869. A search string will be added to the file name with :: as separator and
  870. used to find the context when the link is activated by the command
  871. `org-open-at-point'.
  872. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  873. negates this setting for the duration of the command."
  874. :group 'org-link-store
  875. :type 'boolean)
  876. (defcustom org-keep-stored-link-after-insertion nil
  877. "Non-nil means, keep link in list for entire session.
  878. The command `org-store-link' adds a link pointing to the current
  879. location to an internal list. These links accumulate during a session.
  880. The command `org-insert-link' can be used to insert links into any
  881. Org-mode file (offering completion for all stored links). When this
  882. option is nil, every link which has been inserted once using \\[org-insert-link]
  883. will be removed from the list, to make completing the unused links
  884. more efficient."
  885. :group 'org-link-store
  886. :type 'boolean)
  887. (defgroup org-link-follow nil
  888. "Options concerning following links in Org-mode."
  889. :tag "Org Follow Link"
  890. :group 'org-link)
  891. (defcustom org-link-translation-function nil
  892. "Function to translate links with different syntax to Org syntax.
  893. This can be used to translate links created for example by the Planner
  894. or emacs-wiki packages to Org syntax.
  895. The function must accept two parameters, a TYPE containing the link
  896. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  897. which is everything after the link protocol. It should return a cons
  898. with possibly modified values of type and path.
  899. Org contains a function for this, so if you set this variable to
  900. `org-translate-link-from-planner', you should be able follow many
  901. links created by planner."
  902. :group 'org-link-follow
  903. :type 'function)
  904. (defcustom org-follow-link-hook nil
  905. "Hook that is run after a link has been followed."
  906. :group 'org-link-follow
  907. :type 'hook)
  908. (defcustom org-tab-follows-link nil
  909. "Non-nil means, on links TAB will follow the link.
  910. Needs to be set before org.el is loaded."
  911. :group 'org-link-follow
  912. :type 'boolean)
  913. (defcustom org-return-follows-link nil
  914. "Non-nil means, on links RET will follow the link.
  915. Needs to be set before org.el is loaded."
  916. :group 'org-link-follow
  917. :type 'boolean)
  918. (defcustom org-mouse-1-follows-link
  919. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  920. "Non-nil means, mouse-1 on a link will follow the link.
  921. A longer mouse click will still set point. Does not work on XEmacs.
  922. Needs to be set before org.el is loaded."
  923. :group 'org-link-follow
  924. :type 'boolean)
  925. (defcustom org-mark-ring-length 4
  926. "Number of different positions to be recorded in the ring
  927. Changing this requires a restart of Emacs to work correctly."
  928. :group 'org-link-follow
  929. :type 'integer)
  930. (defcustom org-link-frame-setup
  931. '((vm . vm-visit-folder-other-frame)
  932. (gnus . gnus-other-frame)
  933. (file . find-file-other-window))
  934. "Setup the frame configuration for following links.
  935. When following a link with Emacs, it may often be useful to display
  936. this link in another window or frame. This variable can be used to
  937. set this up for the different types of links.
  938. For VM, use any of
  939. `vm-visit-folder'
  940. `vm-visit-folder-other-frame'
  941. For Gnus, use any of
  942. `gnus'
  943. `gnus-other-frame'
  944. `org-gnus-no-new-news'
  945. For FILE, use any of
  946. `find-file'
  947. `find-file-other-window'
  948. `find-file-other-frame'
  949. For the calendar, use the variable `calendar-setup'.
  950. For BBDB, it is currently only possible to display the matches in
  951. another window."
  952. :group 'org-link-follow
  953. :type '(list
  954. (cons (const vm)
  955. (choice
  956. (const vm-visit-folder)
  957. (const vm-visit-folder-other-window)
  958. (const vm-visit-folder-other-frame)))
  959. (cons (const gnus)
  960. (choice
  961. (const gnus)
  962. (const gnus-other-frame)
  963. (const org-gnus-no-new-news)))
  964. (cons (const file)
  965. (choice
  966. (const find-file)
  967. (const find-file-other-window)
  968. (const find-file-other-frame)))))
  969. (defcustom org-display-internal-link-with-indirect-buffer nil
  970. "Non-nil means, use indirect buffer to display infile links.
  971. Activating internal links (from one location in a file to another location
  972. in the same file) normally just jumps to the location. When the link is
  973. activated with a C-u prefix (or with mouse-3), the link is displayed in
  974. another window. When this option is set, the other window actually displays
  975. an indirect buffer clone of the current buffer, to avoid any visibility
  976. changes to the current buffer."
  977. :group 'org-link-follow
  978. :type 'boolean)
  979. (defcustom org-open-non-existing-files nil
  980. "Non-nil means, `org-open-file' will open non-existing files.
  981. When nil, an error will be generated."
  982. :group 'org-link-follow
  983. :type 'boolean)
  984. (defcustom org-open-directory-means-index-dot-org nil
  985. "Non-nil means, a link to a directory really means to index.org.
  986. When nil, following a directory link will run dired or open a finder/explorer
  987. window on that directory."
  988. :group 'org-link-follow
  989. :type 'boolean)
  990. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  991. "Function and arguments to call for following mailto links.
  992. This is a list with the first element being a lisp function, and the
  993. remaining elements being arguments to the function. In string arguments,
  994. %a will be replaced by the address, and %s will be replaced by the subject
  995. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  996. :group 'org-link-follow
  997. :type '(choice
  998. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  999. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  1000. (const :tag "message-mail" (message-mail "%a" "%s"))
  1001. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  1002. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  1003. "Non-nil means, ask for confirmation before executing shell links.
  1004. Shell links can be dangerous: just think about a link
  1005. [[shell:rm -rf ~/*][Google Search]]
  1006. This link would show up in your Org-mode document as \"Google Search\",
  1007. but really it would remove your entire home directory.
  1008. Therefore we advise against setting this variable to nil.
  1009. Just change it to `y-or-n-p' of you want to confirm with a
  1010. single keystroke rather than having to type \"yes\"."
  1011. :group 'org-link-follow
  1012. :type '(choice
  1013. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1014. (const :tag "with y-or-n (faster)" y-or-n-p)
  1015. (const :tag "no confirmation (dangerous)" nil)))
  1016. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1017. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1018. Elisp links can be dangerous: just think about a link
  1019. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1020. This link would show up in your Org-mode document as \"Google Search\",
  1021. but really it would remove your entire home directory.
  1022. Therefore we advise against setting this variable to nil.
  1023. Just change it to `y-or-n-p' of you want to confirm with a
  1024. single keystroke rather than having to type \"yes\"."
  1025. :group 'org-link-follow
  1026. :type '(choice
  1027. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1028. (const :tag "with y-or-n (faster)" y-or-n-p)
  1029. (const :tag "no confirmation (dangerous)" nil)))
  1030. (defconst org-file-apps-defaults-gnu
  1031. '((remote . emacs)
  1032. (system . mailcap)
  1033. (t . mailcap))
  1034. "Default file applications on a UNIX or GNU/Linux system.
  1035. See `org-file-apps'.")
  1036. (defconst org-file-apps-defaults-macosx
  1037. '((remote . emacs)
  1038. (t . "open %s")
  1039. (system . "open %s")
  1040. ("ps.gz" . "gv %s")
  1041. ("eps.gz" . "gv %s")
  1042. ("dvi" . "xdvi %s")
  1043. ("fig" . "xfig %s"))
  1044. "Default file applications on a MacOS X system.
  1045. The system \"open\" is known as a default, but we use X11 applications
  1046. for some files for which the OS does not have a good default.
  1047. See `org-file-apps'.")
  1048. (defconst org-file-apps-defaults-windowsnt
  1049. (list
  1050. '(remote . emacs)
  1051. (cons t
  1052. (list (if (featurep 'xemacs)
  1053. 'mswindows-shell-execute
  1054. 'w32-shell-execute)
  1055. "open" 'file))
  1056. (cons 'system
  1057. (list (if (featurep 'xemacs)
  1058. 'mswindows-shell-execute
  1059. 'w32-shell-execute)
  1060. "open" 'file)))
  1061. "Default file applications on a Windows NT system.
  1062. The system \"open\" is used for most files.
  1063. See `org-file-apps'.")
  1064. (defcustom org-file-apps
  1065. '(
  1066. (auto-mode . emacs)
  1067. ("\\.x?html?\\'" . default)
  1068. ("\\.pdf\\'" . default)
  1069. )
  1070. "External applications for opening `file:path' items in a document.
  1071. Org-mode uses system defaults for different file types, but
  1072. you can use this variable to set the application for a given file
  1073. extension. The entries in this list are cons cells where the car identifies
  1074. files and the cdr the corresponding command. Possible values for the
  1075. file identifier are
  1076. \"regex\" Regular expression matched against the file name. For backward
  1077. compatibility, this can also be a string with only alphanumeric
  1078. characters, which is then interpreted as an extension.
  1079. `directory' Matches a directory
  1080. `remote' Matches a remote file, accessible through tramp or efs.
  1081. Remote files most likely should be visited through Emacs
  1082. because external applications cannot handle such paths.
  1083. `auto-mode' Matches files that are matched by any entry in `auto-mode-alist',
  1084. so all files Emacs knows how to handle. Using this with
  1085. command `emacs' will open most files in Emacs. Beware that this
  1086. will also open html files inside Emacs, unless you add
  1087. (\"html\" . default) to the list as well.
  1088. t Default for files not matched by any of the other options.
  1089. `system' The system command to open files, like `open' on Windows
  1090. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1091. that will be selected if you call `C-c C-o' with a double
  1092. `C-u C-u' prefix.
  1093. Possible values for the command are:
  1094. `emacs' The file will be visited by the current Emacs process.
  1095. `default' Use the default application for this file type, which is the
  1096. association for t in the list, most likely in the system-specific
  1097. part.
  1098. This can be used to overrule an unwanted setting in the
  1099. system-specific variable.
  1100. `system' Use the system command for opening files, like \"open\".
  1101. This command is specified by the entry whose car is `system'.
  1102. Most likely, the system-specific version of this variable
  1103. does define this command, but you can overrule/replace it
  1104. here.
  1105. string A command to be executed by a shell; %s will be replaced
  1106. by the path to the file.
  1107. sexp A Lisp form which will be evaluated. The file path will
  1108. be available in the Lisp variable `file'.
  1109. For more examples, see the system specific constants
  1110. `org-file-apps-defaults-macosx'
  1111. `org-file-apps-defaults-windowsnt'
  1112. `org-file-apps-defaults-gnu'."
  1113. :group 'org-link-follow
  1114. :type '(repeat
  1115. (cons (choice :value ""
  1116. (string :tag "Extension")
  1117. (const :tag "System command to open files" system)
  1118. (const :tag "Default for unrecognized files" t)
  1119. (const :tag "Remote file" remote)
  1120. (const :tag "Links to a directory" directory)
  1121. (const :tag "Any files that have Emacs modes"
  1122. auto-mode))
  1123. (choice :value ""
  1124. (const :tag "Visit with Emacs" emacs)
  1125. (const :tag "Use default" default)
  1126. (const :tag "Use the system command" system)
  1127. (string :tag "Command")
  1128. (sexp :tag "Lisp form")))))
  1129. (defgroup org-refile nil
  1130. "Options concerning refiling entries in Org-mode."
  1131. :tag "Org Refile"
  1132. :group 'org)
  1133. (defcustom org-directory "~/org"
  1134. "Directory with org files.
  1135. This directory will be used as default to prompt for org files.
  1136. Used by the hooks for remember.el."
  1137. :group 'org-refile
  1138. :group 'org-remember
  1139. :type 'directory)
  1140. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1141. "Default target for storing notes.
  1142. Used by the hooks for remember.el. This can be a string, or nil to mean
  1143. the value of `remember-data-file'.
  1144. You can set this on a per-template basis with the variable
  1145. `org-remember-templates'."
  1146. :group 'org-refile
  1147. :group 'org-remember
  1148. :type '(choice
  1149. (const :tag "Default from remember-data-file" nil)
  1150. file))
  1151. (defcustom org-goto-interface 'outline
  1152. "The default interface to be used for `org-goto'.
  1153. Allowed values are:
  1154. outline The interface shows an outline of the relevant file
  1155. and the correct heading is found by moving through
  1156. the outline or by searching with incremental search.
  1157. outline-path-completion Headlines in the current buffer are offered via
  1158. completion. This is the interface also used by
  1159. the refile command."
  1160. :group 'org-refile
  1161. :type '(choice
  1162. (const :tag "Outline" outline)
  1163. (const :tag "Outline-path-completion" outline-path-completion)))
  1164. (defcustom org-goto-max-level 5
  1165. "Maximum level to be considered when running org-goto with refile interface."
  1166. :group 'org-refile
  1167. :type 'number)
  1168. (defcustom org-reverse-note-order nil
  1169. "Non-nil means, store new notes at the beginning of a file or entry.
  1170. When nil, new notes will be filed to the end of a file or entry.
  1171. This can also be a list with cons cells of regular expressions that
  1172. are matched against file names, and values."
  1173. :group 'org-remember
  1174. :group 'org-refile
  1175. :type '(choice
  1176. (const :tag "Reverse always" t)
  1177. (const :tag "Reverse never" nil)
  1178. (repeat :tag "By file name regexp"
  1179. (cons regexp boolean))))
  1180. (defcustom org-refile-targets nil
  1181. "Targets for refiling entries with \\[org-refile].
  1182. This is list of cons cells. Each cell contains:
  1183. - a specification of the files to be considered, either a list of files,
  1184. or a symbol whose function or variable value will be used to retrieve
  1185. a file name or a list of file names. If you use `org-agenda-files' for
  1186. that, all agenda files will be scanned for targets. Nil means, consider
  1187. headings in the current buffer.
  1188. - A specification of how to select find candidate refile targets. This
  1189. may be any of
  1190. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1191. This tag has to be present in all target headlines, inheritance will
  1192. not be considered.
  1193. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1194. todo keyword.
  1195. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1196. headlines that are refiling targets.
  1197. - a cons cell (:level . N). Any headline of level N is considered a target.
  1198. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1199. When this variable is nil, all top-level headlines in the current buffer
  1200. are used, equivalent to the value `((nil . (:level . 1))'."
  1201. :group 'org-refile
  1202. :type '(repeat
  1203. (cons
  1204. (choice :value org-agenda-files
  1205. (const :tag "All agenda files" org-agenda-files)
  1206. (const :tag "Current buffer" nil)
  1207. (function) (variable) (file))
  1208. (choice :tag "Identify target headline by"
  1209. (cons :tag "Specific tag" (const :value :tag) (string))
  1210. (cons :tag "TODO keyword" (const :value :todo) (string))
  1211. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1212. (cons :tag "Level number" (const :value :level) (integer))
  1213. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1214. (defcustom org-refile-use-outline-path nil
  1215. "Non-nil means, provide refile targets as paths.
  1216. So a level 3 headline will be available as level1/level2/level3.
  1217. When the value is `file', also include the file name (without directory)
  1218. into the path. When `full-file-path', include the full file path."
  1219. :group 'org-refile
  1220. :type '(choice
  1221. (const :tag "Not" nil)
  1222. (const :tag "Yes" t)
  1223. (const :tag "Start with file name" file)
  1224. (const :tag "Start with full file path" full-file-path)))
  1225. (defcustom org-outline-path-complete-in-steps t
  1226. "Non-nil means, complete the outline path in hierarchical steps.
  1227. When Org-mode uses the refile interface to select an outline path
  1228. \(see variable `org-refile-use-outline-path'), the completion of
  1229. the path can be done is a single go, or if can be done in steps down
  1230. the headline hierarchy. Going in steps is probably the best if you
  1231. do not use a special completion package like `ido' or `icicles'.
  1232. However, when using these packages, going in one step can be very
  1233. fast, while still showing the whole path to the entry."
  1234. :group 'org-refile
  1235. :type 'boolean)
  1236. (defgroup org-todo nil
  1237. "Options concerning TODO items in Org-mode."
  1238. :tag "Org TODO"
  1239. :group 'org)
  1240. (defgroup org-progress nil
  1241. "Options concerning Progress logging in Org-mode."
  1242. :tag "Org Progress"
  1243. :group 'org-time)
  1244. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1245. "List of TODO entry keyword sequences and their interpretation.
  1246. \\<org-mode-map>This is a list of sequences.
  1247. Each sequence starts with a symbol, either `sequence' or `type',
  1248. indicating if the keywords should be interpreted as a sequence of
  1249. action steps, or as different types of TODO items. The first
  1250. keywords are states requiring action - these states will select a headline
  1251. for inclusion into the global TODO list Org-mode produces. If one of
  1252. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1253. signify that no further action is necessary. If \"|\" is not found,
  1254. the last keyword is treated as the only DONE state of the sequence.
  1255. The command \\[org-todo] cycles an entry through these states, and one
  1256. additional state where no keyword is present. For details about this
  1257. cycling, see the manual.
  1258. TODO keywords and interpretation can also be set on a per-file basis with
  1259. the special #+SEQ_TODO and #+TYP_TODO lines.
  1260. Each keyword can optionally specify a character for fast state selection
  1261. \(in combination with the variable `org-use-fast-todo-selection')
  1262. and specifiers for state change logging, using the same syntax
  1263. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1264. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1265. indicates to record a time stamp each time this state is selected.
  1266. Each keyword may also specify if a timestamp or a note should be
  1267. recorded when entering or leaving the state, by adding additional
  1268. characters in the parenthesis after the keyword. This looks like this:
  1269. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1270. record only the time of the state change. With X and Y being either
  1271. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1272. Y when leaving the state if and only if the *target* state does not
  1273. define X. You may omit any of the fast-selection key or X or /Y,
  1274. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1275. For backward compatibility, this variable may also be just a list
  1276. of keywords - in this case the interpretation (sequence or type) will be
  1277. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1278. :group 'org-todo
  1279. :group 'org-keywords
  1280. :type '(choice
  1281. (repeat :tag "Old syntax, just keywords"
  1282. (string :tag "Keyword"))
  1283. (repeat :tag "New syntax"
  1284. (cons
  1285. (choice
  1286. :tag "Interpretation"
  1287. (const :tag "Sequence (cycling hits every state)" sequence)
  1288. (const :tag "Type (cycling directly to DONE)" type))
  1289. (repeat
  1290. (string :tag "Keyword"))))))
  1291. (defvar org-todo-keywords-1 nil
  1292. "All TODO and DONE keywords active in a buffer.")
  1293. (make-variable-buffer-local 'org-todo-keywords-1)
  1294. (defvar org-todo-keywords-for-agenda nil)
  1295. (defvar org-done-keywords-for-agenda nil)
  1296. (defvar org-todo-keyword-alist-for-agenda nil)
  1297. (defvar org-tag-alist-for-agenda nil)
  1298. (defvar org-agenda-contributing-files nil)
  1299. (defvar org-not-done-keywords nil)
  1300. (make-variable-buffer-local 'org-not-done-keywords)
  1301. (defvar org-done-keywords nil)
  1302. (make-variable-buffer-local 'org-done-keywords)
  1303. (defvar org-todo-heads nil)
  1304. (make-variable-buffer-local 'org-todo-heads)
  1305. (defvar org-todo-sets nil)
  1306. (make-variable-buffer-local 'org-todo-sets)
  1307. (defvar org-todo-log-states nil)
  1308. (make-variable-buffer-local 'org-todo-log-states)
  1309. (defvar org-todo-kwd-alist nil)
  1310. (make-variable-buffer-local 'org-todo-kwd-alist)
  1311. (defvar org-todo-key-alist nil)
  1312. (make-variable-buffer-local 'org-todo-key-alist)
  1313. (defvar org-todo-key-trigger nil)
  1314. (make-variable-buffer-local 'org-todo-key-trigger)
  1315. (defcustom org-todo-interpretation 'sequence
  1316. "Controls how TODO keywords are interpreted.
  1317. This variable is in principle obsolete and is only used for
  1318. backward compatibility, if the interpretation of todo keywords is
  1319. not given already in `org-todo-keywords'. See that variable for
  1320. more information."
  1321. :group 'org-todo
  1322. :group 'org-keywords
  1323. :type '(choice (const sequence)
  1324. (const type)))
  1325. (defcustom org-use-fast-todo-selection 'prefix
  1326. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1327. This variable describes if and under what circumstances the cycling
  1328. mechanism for TODO keywords will be replaced by a single-key, direct
  1329. selection scheme.
  1330. When nil, fast selection is never used.
  1331. When the symbol `prefix', it will be used when `org-todo' is called with
  1332. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1333. in an agenda buffer.
  1334. When t, fast selection is used by default. In this case, the prefix
  1335. argument forces cycling instead.
  1336. In all cases, the special interface is only used if access keys have actually
  1337. been assigned by the user, i.e. if keywords in the configuration are followed
  1338. by a letter in parenthesis, like TODO(t)."
  1339. :group 'org-todo
  1340. :type '(choice
  1341. (const :tag "Never" nil)
  1342. (const :tag "By default" t)
  1343. (const :tag "Only with C-u C-c C-t" prefix)))
  1344. (defcustom org-provide-todo-statistics t
  1345. "Non-nil means, update todo statistics after insert and toggle.
  1346. When this is set, todo statistics is updated in the parent of the current
  1347. entry each time a todo state is changed."
  1348. :group 'org-todo
  1349. :type 'boolean)
  1350. (defcustom org-after-todo-state-change-hook nil
  1351. "Hook which is run after the state of a TODO item was changed.
  1352. The new state (a string with a TODO keyword, or nil) is available in the
  1353. Lisp variable `state'."
  1354. :group 'org-todo
  1355. :type 'hook)
  1356. (defcustom org-todo-state-tags-triggers nil
  1357. "Tag changes that should be triggered by TODO state changes.
  1358. This is a list. Each entry is
  1359. (state-change (tag . flag) .......)
  1360. State-change can be a string with a state, and empty string to indicate the
  1361. state that has no TODO keyword, or it can be one of the symbols `todo'
  1362. or `done', meaning any not-done or done state, respectively."
  1363. :group 'org-todo
  1364. :group 'org-tags
  1365. :type '(repeat
  1366. (cons (choice :tag "When changing to"
  1367. (const :tag "Not-done state" todo)
  1368. (const :tag "Done state" done)
  1369. (string :tag "State"))
  1370. (repeat
  1371. (cons :tag "Tag action"
  1372. (string :tag "Tag")
  1373. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1374. (defcustom org-log-done nil
  1375. "Information to record when a task moves to the DONE state.
  1376. Possible values are:
  1377. nil Don't add anything, just change the keyword
  1378. time Add a time stamp to the task
  1379. note Prompt a closing note and add it with template `org-log-note-headings'
  1380. This option can also be set with on a per-file-basis with
  1381. #+STARTUP: nologdone
  1382. #+STARTUP: logdone
  1383. #+STARTUP: lognotedone
  1384. You can have local logging settings for a subtree by setting the LOGGING
  1385. property to one or more of these keywords."
  1386. :group 'org-todo
  1387. :group 'org-progress
  1388. :type '(choice
  1389. (const :tag "No logging" nil)
  1390. (const :tag "Record CLOSED timestamp" time)
  1391. (const :tag "Record CLOSED timestamp with closing note." note)))
  1392. ;; Normalize old uses of org-log-done.
  1393. (cond
  1394. ((eq org-log-done t) (setq org-log-done 'time))
  1395. ((and (listp org-log-done) (memq 'done org-log-done))
  1396. (setq org-log-done 'note)))
  1397. (defcustom org-log-note-clock-out nil
  1398. "Non-nil means, record a note when clocking out of an item.
  1399. This can also be configured on a per-file basis by adding one of
  1400. the following lines anywhere in the buffer:
  1401. #+STARTUP: lognoteclock-out
  1402. #+STARTUP: nolognoteclock-out"
  1403. :group 'org-todo
  1404. :group 'org-progress
  1405. :type 'boolean)
  1406. (defcustom org-log-done-with-time t
  1407. "Non-nil means, the CLOSED time stamp will contain date and time.
  1408. When nil, only the date will be recorded."
  1409. :group 'org-progress
  1410. :type 'boolean)
  1411. (defcustom org-log-note-headings
  1412. '((done . "CLOSING NOTE %t")
  1413. (state . "State %-12s %t")
  1414. (note . "Note taken on %t")
  1415. (clock-out . ""))
  1416. "Headings for notes added to entries.
  1417. The value is an alist, with the car being a symbol indicating the note
  1418. context, and the cdr is the heading to be used. The heading may also be the
  1419. empty string.
  1420. %t in the heading will be replaced by a time stamp.
  1421. %s will be replaced by the new TODO state, in double quotes.
  1422. %u will be replaced by the user name.
  1423. %U will be replaced by the full user name."
  1424. :group 'org-todo
  1425. :group 'org-progress
  1426. :type '(list :greedy t
  1427. (cons (const :tag "Heading when closing an item" done) string)
  1428. (cons (const :tag
  1429. "Heading when changing todo state (todo sequence only)"
  1430. state) string)
  1431. (cons (const :tag "Heading when just taking a note" note) string)
  1432. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1433. (unless (assq 'note org-log-note-headings)
  1434. (push '(note . "%t") org-log-note-headings))
  1435. (defcustom org-log-state-notes-insert-after-drawers nil
  1436. "Non-nil means, insert state change notes after any drawers in entry.
  1437. Only the drawers that *immediately* follow the headline and the
  1438. deadline/scheduled line are skipped.
  1439. When nil, insert notes right after the heading and perhaps the line
  1440. with deadline/scheduling if present."
  1441. :group 'org-todo
  1442. :group 'org-progress
  1443. :type 'boolean)
  1444. (defcustom org-log-states-order-reversed t
  1445. "Non-nil means, the latest state change note will be directly after heading.
  1446. When nil, the notes will be orderer according to time."
  1447. :group 'org-todo
  1448. :group 'org-progress
  1449. :type 'boolean)
  1450. (defcustom org-log-repeat 'time
  1451. "Non-nil means, record moving through the DONE state when triggering repeat.
  1452. An auto-repeating tasks is immediately switched back to TODO when marked
  1453. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1454. the TODO keyword definition, or recording a closing note by setting
  1455. `org-log-done', there will be no record of the task moving through DONE.
  1456. This variable forces taking a note anyway. Possible values are:
  1457. nil Don't force a record
  1458. time Record a time stamp
  1459. note Record a note
  1460. This option can also be set with on a per-file-basis with
  1461. #+STARTUP: logrepeat
  1462. #+STARTUP: lognoterepeat
  1463. #+STARTUP: nologrepeat
  1464. You can have local logging settings for a subtree by setting the LOGGING
  1465. property to one or more of these keywords."
  1466. :group 'org-todo
  1467. :group 'org-progress
  1468. :type '(choice
  1469. (const :tag "Don't force a record" nil)
  1470. (const :tag "Force recording the DONE state" time)
  1471. (const :tag "Force recording a note with the DONE state" note)))
  1472. (defgroup org-priorities nil
  1473. "Priorities in Org-mode."
  1474. :tag "Org Priorities"
  1475. :group 'org-todo)
  1476. (defcustom org-highest-priority ?A
  1477. "The highest priority of TODO items. A character like ?A, ?B etc.
  1478. Must have a smaller ASCII number than `org-lowest-priority'."
  1479. :group 'org-priorities
  1480. :type 'character)
  1481. (defcustom org-lowest-priority ?C
  1482. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1483. Must have a larger ASCII number than `org-highest-priority'."
  1484. :group 'org-priorities
  1485. :type 'character)
  1486. (defcustom org-default-priority ?B
  1487. "The default priority of TODO items.
  1488. This is the priority an item get if no explicit priority is given."
  1489. :group 'org-priorities
  1490. :type 'character)
  1491. (defcustom org-priority-start-cycle-with-default t
  1492. "Non-nil means, start with default priority when starting to cycle.
  1493. When this is nil, the first step in the cycle will be (depending on the
  1494. command used) one higher or lower that the default priority."
  1495. :group 'org-priorities
  1496. :type 'boolean)
  1497. (defgroup org-time nil
  1498. "Options concerning time stamps and deadlines in Org-mode."
  1499. :tag "Org Time"
  1500. :group 'org)
  1501. (defcustom org-insert-labeled-timestamps-at-point nil
  1502. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1503. When nil, these labeled time stamps are forces into the second line of an
  1504. entry, just after the headline. When scheduling from the global TODO list,
  1505. the time stamp will always be forced into the second line."
  1506. :group 'org-time
  1507. :type 'boolean)
  1508. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1509. "Formats for `format-time-string' which are used for time stamps.
  1510. It is not recommended to change this constant.")
  1511. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1512. "Number of minutes to round time stamps to.
  1513. These are two values, the first applies when first creating a time stamp.
  1514. The second applies when changing it with the commands `S-up' and `S-down'.
  1515. When changing the time stamp, this means that it will change in steps
  1516. of N minutes, as given by the second value.
  1517. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1518. numbers should be factors of 60, so for example 5, 10, 15.
  1519. When this is larger than 1, you can still force an exact time-stamp by using
  1520. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1521. and by using a prefix arg to `S-up/down' to specify the exact number
  1522. of minutes to shift."
  1523. :group 'org-time
  1524. :get '(lambda (var) ; Make sure all entries have 5 elements
  1525. (if (integerp (default-value var))
  1526. (list (default-value var) 5)
  1527. (default-value var)))
  1528. :type '(list
  1529. (integer :tag "when inserting times")
  1530. (integer :tag "when modifying times")))
  1531. ;; Normalize old customizations of this variable.
  1532. (when (integerp org-time-stamp-rounding-minutes)
  1533. (setq org-time-stamp-rounding-minutes
  1534. (list org-time-stamp-rounding-minutes
  1535. org-time-stamp-rounding-minutes)))
  1536. (defcustom org-display-custom-times nil
  1537. "Non-nil means, overlay custom formats over all time stamps.
  1538. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1539. To turn this on on a per-file basis, insert anywhere in the file:
  1540. #+STARTUP: customtime"
  1541. :group 'org-time
  1542. :set 'set-default
  1543. :type 'sexp)
  1544. (make-variable-buffer-local 'org-display-custom-times)
  1545. (defcustom org-time-stamp-custom-formats
  1546. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1547. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1548. These are overlayed over the default ISO format if the variable
  1549. `org-display-custom-times' is set. Time like %H:%M should be at the
  1550. end of the second format."
  1551. :group 'org-time
  1552. :type 'sexp)
  1553. (defun org-time-stamp-format (&optional long inactive)
  1554. "Get the right format for a time string."
  1555. (let ((f (if long (cdr org-time-stamp-formats)
  1556. (car org-time-stamp-formats))))
  1557. (if inactive
  1558. (concat "[" (substring f 1 -1) "]")
  1559. f)))
  1560. (defcustom org-time-clocksum-format "%d:%02d"
  1561. "The format string used when creating CLOCKSUM lines, or when
  1562. org-mode generates a time duration."
  1563. :group 'org-time
  1564. :type 'string)
  1565. (defcustom org-deadline-warning-days 14
  1566. "No. of days before expiration during which a deadline becomes active.
  1567. This variable governs the display in sparse trees and in the agenda.
  1568. When 0 or negative, it means use this number (the absolute value of it)
  1569. even if a deadline has a different individual lead time specified."
  1570. :group 'org-time
  1571. :group 'org-agenda-daily/weekly
  1572. :type 'number)
  1573. (defcustom org-read-date-prefer-future t
  1574. "Non-nil means, assume future for incomplete date input from user.
  1575. This affects the following situations:
  1576. 1. The user gives a day, but no month.
  1577. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1578. read this as the third of *next* month. However, if you enter \"17\",
  1579. it will be considered as *this* month.
  1580. 2. The user gives a month but not a year.
  1581. For example, if it is april and you enter \"feb 2\", this will be read
  1582. as feb 2, *next* year. \"May 5\", however, will be this year.
  1583. Currently this does not work for ISO week specifications.
  1584. When this option is nil, the current month and year will always be used
  1585. as defaults."
  1586. :group 'org-time
  1587. :type 'boolean)
  1588. (defcustom org-read-date-display-live t
  1589. "Non-nil means, display current interpretation of date prompt live.
  1590. This display will be in an overlay, in the minibuffer."
  1591. :group 'org-time
  1592. :type 'boolean)
  1593. (defcustom org-read-date-popup-calendar t
  1594. "Non-nil means, pop up a calendar when prompting for a date.
  1595. In the calendar, the date can be selected with mouse-1. However, the
  1596. minibuffer will also be active, and you can simply enter the date as well.
  1597. When nil, only the minibuffer will be available."
  1598. :group 'org-time
  1599. :type 'boolean)
  1600. (if (fboundp 'defvaralias)
  1601. (defvaralias 'org-popup-calendar-for-date-prompt
  1602. 'org-read-date-popup-calendar))
  1603. (defcustom org-extend-today-until 0
  1604. "The hour when your day really ends. Must be an integer.
  1605. This has influence for the following applications:
  1606. - When switching the agenda to \"today\". It it is still earlier than
  1607. the time given here, the day recognized as TODAY is actually yesterday.
  1608. - When a date is read from the user and it is still before the time given
  1609. here, the current date and time will be assumed to be yesterday, 23:59.
  1610. Also, timestamps inserted in remember templates follow this rule.
  1611. IMPORTANT: This is a feature whose implementation is and likely will
  1612. remain incomplete. Really, it is only here because past midnight seems to
  1613. be the favorite working time of John Wiegley :-)"
  1614. :group 'org-time
  1615. :type 'number)
  1616. (defcustom org-edit-timestamp-down-means-later nil
  1617. "Non-nil means, S-down will increase the time in a time stamp.
  1618. When nil, S-up will increase."
  1619. :group 'org-time
  1620. :type 'boolean)
  1621. (defcustom org-calendar-follow-timestamp-change t
  1622. "Non-nil means, make the calendar window follow timestamp changes.
  1623. When a timestamp is modified and the calendar window is visible, it will be
  1624. moved to the new date."
  1625. :group 'org-time
  1626. :type 'boolean)
  1627. (defgroup org-tags nil
  1628. "Options concerning tags in Org-mode."
  1629. :tag "Org Tags"
  1630. :group 'org)
  1631. (defcustom org-tag-alist nil
  1632. "List of tags allowed in Org-mode files.
  1633. When this list is nil, Org-mode will base TAG input on what is already in the
  1634. buffer.
  1635. The value of this variable is an alist, the car of each entry must be a
  1636. keyword as a string, the cdr may be a character that is used to select
  1637. that tag through the fast-tag-selection interface.
  1638. See the manual for details."
  1639. :group 'org-tags
  1640. :type '(repeat
  1641. (choice
  1642. (cons (string :tag "Tag name")
  1643. (character :tag "Access char"))
  1644. (const :tag "Start radio group" (:startgroup))
  1645. (const :tag "End radio group" (:endgroup)))))
  1646. (defvar org-file-tags nil
  1647. "List of tags that can be inherited by all entries in the file.
  1648. The tags will be inherited if the variable `org-use-tag-inheritance'
  1649. says they should be.
  1650. This variable is populated from #+TAG lines.")
  1651. (defcustom org-use-fast-tag-selection 'auto
  1652. "Non-nil means, use fast tag selection scheme.
  1653. This is a special interface to select and deselect tags with single keys.
  1654. When nil, fast selection is never used.
  1655. When the symbol `auto', fast selection is used if and only if selection
  1656. characters for tags have been configured, either through the variable
  1657. `org-tag-alist' or through a #+TAGS line in the buffer.
  1658. When t, fast selection is always used and selection keys are assigned
  1659. automatically if necessary."
  1660. :group 'org-tags
  1661. :type '(choice
  1662. (const :tag "Always" t)
  1663. (const :tag "Never" nil)
  1664. (const :tag "When selection characters are configured" 'auto)))
  1665. (defcustom org-fast-tag-selection-single-key nil
  1666. "Non-nil means, fast tag selection exits after first change.
  1667. When nil, you have to press RET to exit it.
  1668. During fast tag selection, you can toggle this flag with `C-c'.
  1669. This variable can also have the value `expert'. In this case, the window
  1670. displaying the tags menu is not even shown, until you press C-c again."
  1671. :group 'org-tags
  1672. :type '(choice
  1673. (const :tag "No" nil)
  1674. (const :tag "Yes" t)
  1675. (const :tag "Expert" expert)))
  1676. (defvar org-fast-tag-selection-include-todo nil
  1677. "Non-nil means, fast tags selection interface will also offer TODO states.
  1678. This is an undocumented feature, you should not rely on it.")
  1679. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1680. "The column to which tags should be indented in a headline.
  1681. If this number is positive, it specifies the column. If it is negative,
  1682. it means that the tags should be flushright to that column. For example,
  1683. -80 works well for a normal 80 character screen."
  1684. :group 'org-tags
  1685. :type 'integer)
  1686. (defcustom org-auto-align-tags t
  1687. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1688. These operations change the length of a headline and therefore shift
  1689. the tags around. With this options turned on, after each such operation
  1690. the tags are again aligned to `org-tags-column'."
  1691. :group 'org-tags
  1692. :type 'boolean)
  1693. (defcustom org-use-tag-inheritance t
  1694. "Non-nil means, tags in levels apply also for sublevels.
  1695. When nil, only the tags directly given in a specific line apply there.
  1696. This may also be a list of tags that should be inherited, or a regexp that
  1697. matches tags that should be inherited. Additional control is possible
  1698. with the variable `org-tags-exclude-from-inheritance' which gives an
  1699. explicit list of tags to be excluded from inheritance., even if the value of
  1700. `org-use-tag-inheritance' would select it for inheritance.
  1701. If this option is t, a match early-on in a tree can lead to a large
  1702. number of matches in the subtree when constructing the agenda or creating
  1703. a sparse tree. If you only want to see the first match in a tree during
  1704. a search, check out the variable `org-tags-match-list-sublevels'."
  1705. :group 'org-tags
  1706. :type '(choice
  1707. (const :tag "Not" nil)
  1708. (const :tag "Always" t)
  1709. (repeat :tag "Specific tags" (string :tag "Tag"))
  1710. (regexp :tag "Tags matched by regexp")))
  1711. (defcustom org-tags-exclude-from-inheritance nil
  1712. "List of tags that should never be inherited.
  1713. This is a way to exclude a few tags from inheritance. For way to do
  1714. the opposite, to actively allow inheritance for selected tags,
  1715. see the variable `org-use-tag-inheritance'."
  1716. :group 'org-tags
  1717. :type '(repeat (string :tag "Tag")))
  1718. (defun org-tag-inherit-p (tag)
  1719. "Check if TAG is one that should be inherited."
  1720. (cond
  1721. ((member tag org-tags-exclude-from-inheritance) nil)
  1722. ((eq org-use-tag-inheritance t) t)
  1723. ((not org-use-tag-inheritance) nil)
  1724. ((stringp org-use-tag-inheritance)
  1725. (string-match org-use-tag-inheritance tag))
  1726. ((listp org-use-tag-inheritance)
  1727. (member tag org-use-tag-inheritance))
  1728. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1729. (defcustom org-tags-match-list-sublevels t
  1730. "Non-nil means list also sublevels of headlines matching tag search.
  1731. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1732. the sublevels of a headline matching a tag search often also match
  1733. the same search. Listing all of them can create very long lists.
  1734. Setting this variable to nil causes subtrees of a match to be skipped.
  1735. This option is off by default, because inheritance in on. If you turn
  1736. inheritance off, you very likely want to turn this option on.
  1737. As a special case, if the tag search is restricted to TODO items, the
  1738. value of this variable is ignored and sublevels are always checked, to
  1739. make sure all corresponding TODO items find their way into the list.
  1740. This variable is semi-obsolete and probably should always be true. It
  1741. is better to limit inheritance to certain tags using the variables
  1742. `org-use-tag-inheritance' and `org-tags-exclude-from-inheritance'."
  1743. :group 'org-tags
  1744. :type 'boolean)
  1745. (defvar org-tags-history nil
  1746. "History of minibuffer reads for tags.")
  1747. (defvar org-last-tags-completion-table nil
  1748. "The last used completion table for tags.")
  1749. (defvar org-after-tags-change-hook nil
  1750. "Hook that is run after the tags in a line have changed.")
  1751. (defgroup org-properties nil
  1752. "Options concerning properties in Org-mode."
  1753. :tag "Org Properties"
  1754. :group 'org)
  1755. (defcustom org-property-format "%-10s %s"
  1756. "How property key/value pairs should be formatted by `indent-line'.
  1757. When `indent-line' hits a property definition, it will format the line
  1758. according to this format, mainly to make sure that the values are
  1759. lined-up with respect to each other."
  1760. :group 'org-properties
  1761. :type 'string)
  1762. (defcustom org-use-property-inheritance nil
  1763. "Non-nil means, properties apply also for sublevels.
  1764. This setting is chiefly used during property searches. Turning it on can
  1765. cause significant overhead when doing a search, which is why it is not
  1766. on by default.
  1767. When nil, only the properties directly given in the current entry count.
  1768. When t, every property is inherited. The value may also be a list of
  1769. properties that should have inheritance, or a regular expression matching
  1770. properties that should be inherited.
  1771. However, note that some special properties use inheritance under special
  1772. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1773. and the properties ending in \"_ALL\" when they are used as descriptor
  1774. for valid values of a property.
  1775. Note for programmers:
  1776. When querying an entry with `org-entry-get', you can control if inheritance
  1777. should be used. By default, `org-entry-get' looks only at the local
  1778. properties. You can request inheritance by setting the inherit argument
  1779. to t (to force inheritance) or to `selective' (to respect the setting
  1780. in this variable)."
  1781. :group 'org-properties
  1782. :type '(choice
  1783. (const :tag "Not" nil)
  1784. (const :tag "Always" t)
  1785. (repeat :tag "Specific properties" (string :tag "Property"))
  1786. (regexp :tag "Properties matched by regexp")))
  1787. (defun org-property-inherit-p (property)
  1788. "Check if PROPERTY is one that should be inherited."
  1789. (cond
  1790. ((eq org-use-property-inheritance t) t)
  1791. ((not org-use-property-inheritance) nil)
  1792. ((stringp org-use-property-inheritance)
  1793. (string-match org-use-property-inheritance property))
  1794. ((listp org-use-property-inheritance)
  1795. (member property org-use-property-inheritance))
  1796. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1797. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1798. "The default column format, if no other format has been defined.
  1799. This variable can be set on the per-file basis by inserting a line
  1800. #+COLUMNS: %25ITEM ....."
  1801. :group 'org-properties
  1802. :type 'string)
  1803. (defcustom org-columns-ellipses ".."
  1804. "The ellipses to be used when a field in column view is truncated.
  1805. When this is the empty string, as many characters as possible are shown,
  1806. but then there will be no visual indication that the field has been truncated.
  1807. When this is a string of length N, the last N characters of a truncated
  1808. field are replaced by this string. If the column is narrower than the
  1809. ellipses string, only part of the ellipses string will be shown."
  1810. :group 'org-properties
  1811. :type 'string)
  1812. (defcustom org-columns-modify-value-for-display-function nil
  1813. "Function that modifies values for display in column view.
  1814. For example, it can be used to cut out a certain part from a time stamp.
  1815. The function must take 2 arguments:
  1816. column-title The title of the column (*not* the property name)
  1817. value The value that should be modified.
  1818. The function should return the value that should be displayed,
  1819. or nil if the normal value should be used."
  1820. :group 'org-properties
  1821. :type 'function)
  1822. (defcustom org-effort-property "Effort"
  1823. "The property that is being used to keep track of effort estimates.
  1824. Effort estimates given in this property need to have the format H:MM."
  1825. :group 'org-properties
  1826. :group 'org-progress
  1827. :type '(string :tag "Property"))
  1828. (defconst org-global-properties-fixed
  1829. '(("VISIBILITY_ALL" . "folded children content all"))
  1830. "List of property/value pairs that can be inherited by any entry.
  1831. These are fixed values, for the preset properties.")
  1832. (defcustom org-global-properties nil
  1833. "List of property/value pairs that can be inherited by any entry.
  1834. You can set buffer-local values for the same purpose in the variable
  1835. `org-file-properties' this by adding lines like
  1836. #+PROPERTY: NAME VALUE"
  1837. :group 'org-properties
  1838. :type '(repeat
  1839. (cons (string :tag "Property")
  1840. (string :tag "Value"))))
  1841. (defvar org-file-properties nil
  1842. "List of property/value pairs that can be inherited by any entry.
  1843. Valid for the current buffer.
  1844. This variable is populated from #+PROPERTY lines.")
  1845. (make-variable-buffer-local 'org-file-properties)
  1846. (defgroup org-agenda nil
  1847. "Options concerning agenda views in Org-mode."
  1848. :tag "Org Agenda"
  1849. :group 'org)
  1850. (defvar org-category nil
  1851. "Variable used by org files to set a category for agenda display.
  1852. Such files should use a file variable to set it, for example
  1853. # -*- mode: org; org-category: \"ELisp\"
  1854. or contain a special line
  1855. #+CATEGORY: ELisp
  1856. If the file does not specify a category, then file's base name
  1857. is used instead.")
  1858. (make-variable-buffer-local 'org-category)
  1859. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1860. (defcustom org-agenda-files nil
  1861. "The files to be used for agenda display.
  1862. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1863. \\[org-remove-file]. You can also use customize to edit the list.
  1864. If an entry is a directory, all files in that directory that are matched by
  1865. `org-agenda-file-regexp' will be part of the file list.
  1866. If the value of the variable is not a list but a single file name, then
  1867. the list of agenda files is actually stored and maintained in that file, one
  1868. agenda file per line."
  1869. :group 'org-agenda
  1870. :type '(choice
  1871. (repeat :tag "List of files and directories" file)
  1872. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1873. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1874. "Regular expression to match files for `org-agenda-files'.
  1875. If any element in the list in that variable contains a directory instead
  1876. of a normal file, all files in that directory that are matched by this
  1877. regular expression will be included."
  1878. :group 'org-agenda
  1879. :type 'regexp)
  1880. (defcustom org-agenda-text-search-extra-files nil
  1881. "List of extra files to be searched by text search commands.
  1882. These files will be search in addition to the agenda files by the
  1883. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1884. Note that these files will only be searched for text search commands,
  1885. not for the other agenda views like todo lists, tag searches or the weekly
  1886. agenda. This variable is intended to list notes and possibly archive files
  1887. that should also be searched by these two commands.
  1888. In fact, if the first element in the list is the symbol `agenda-archives',
  1889. than all archive files of all agenda files will be added to the search
  1890. scope."
  1891. :group 'org-agenda
  1892. :type '(set :greedy t
  1893. (const :tag "Agenda Archives" agenda-archives)
  1894. (repeat :inline t (file))))
  1895. (if (fboundp 'defvaralias)
  1896. (defvaralias 'org-agenda-multi-occur-extra-files
  1897. 'org-agenda-text-search-extra-files))
  1898. (defcustom org-agenda-skip-unavailable-files nil
  1899. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1900. A nil value means to remove them, after a query, from the list."
  1901. :group 'org-agenda
  1902. :type 'boolean)
  1903. (defcustom org-calendar-to-agenda-key [?c]
  1904. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1905. The command `org-calendar-goto-agenda' will be bound to this key. The
  1906. default is the character `c' because then `c' can be used to switch back and
  1907. forth between agenda and calendar."
  1908. :group 'org-agenda
  1909. :type 'sexp)
  1910. (defcustom org-calendar-agenda-action-key [?k]
  1911. "The key to be installed in `calendar-mode-map' for agenda-action.
  1912. The command `org-agenda-action' will be bound to this key. The
  1913. default is the character `k' because we use the same key in the agenda."
  1914. :group 'org-agenda
  1915. :type 'sexp)
  1916. (eval-after-load "calendar"
  1917. '(progn
  1918. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1919. 'org-calendar-goto-agenda)
  1920. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1921. 'org-agenda-action)))
  1922. (defgroup org-latex nil
  1923. "Options for embedding LaTeX code into Org-mode."
  1924. :tag "Org LaTeX"
  1925. :group 'org)
  1926. (defcustom org-format-latex-options
  1927. '(:foreground default :background default :scale 1.0
  1928. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1929. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1930. "Options for creating images from LaTeX fragments.
  1931. This is a property list with the following properties:
  1932. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1933. `default' means use the foreground of the default face.
  1934. :background the background color, or \"Transparent\".
  1935. `default' means use the background of the default face.
  1936. :scale a scaling factor for the size of the images.
  1937. :html-foreground, :html-background, :html-scale
  1938. the same numbers for HTML export.
  1939. :matchers a list indicating which matchers should be used to
  1940. find LaTeX fragments. Valid members of this list are:
  1941. \"begin\" find environments
  1942. \"$\" find math expressions surrounded by $...$
  1943. \"$$\" find math expressions surrounded by $$....$$
  1944. \"\\(\" find math expressions surrounded by \\(...\\)
  1945. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1946. :group 'org-latex
  1947. :type 'plist)
  1948. (defcustom org-format-latex-header "\\documentclass{article}
  1949. \\usepackage{fullpage} % do not remove
  1950. \\usepackage{amssymb}
  1951. \\usepackage[usenames]{color}
  1952. \\usepackage{amsmath}
  1953. \\usepackage{latexsym}
  1954. \\usepackage[mathscr]{eucal}
  1955. \\pagestyle{empty} % do not remove"
  1956. "The document header used for processing LaTeX fragments."
  1957. :group 'org-latex
  1958. :type 'string)
  1959. (defgroup org-font-lock nil
  1960. "Font-lock settings for highlighting in Org-mode."
  1961. :tag "Org Font Lock"
  1962. :group 'org)
  1963. (defcustom org-level-color-stars-only nil
  1964. "Non-nil means fontify only the stars in each headline.
  1965. When nil, the entire headline is fontified.
  1966. Changing it requires restart of `font-lock-mode' to become effective
  1967. also in regions already fontified."
  1968. :group 'org-font-lock
  1969. :type 'boolean)
  1970. (defcustom org-hide-leading-stars nil
  1971. "Non-nil means, hide the first N-1 stars in a headline.
  1972. This works by using the face `org-hide' for these stars. This
  1973. face is white for a light background, and black for a dark
  1974. background. You may have to customize the face `org-hide' to
  1975. make this work.
  1976. Changing it requires restart of `font-lock-mode' to become effective
  1977. also in regions already fontified.
  1978. You may also set this on a per-file basis by adding one of the following
  1979. lines to the buffer:
  1980. #+STARTUP: hidestars
  1981. #+STARTUP: showstars"
  1982. :group 'org-font-lock
  1983. :type 'boolean)
  1984. (defcustom org-fontify-done-headline nil
  1985. "Non-nil means, change the face of a headline if it is marked DONE.
  1986. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1987. When this is non-nil, the headline after the keyword is set to the
  1988. `org-headline-done' as an additional indication."
  1989. :group 'org-font-lock
  1990. :type 'boolean)
  1991. (defcustom org-fontify-emphasized-text t
  1992. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1993. Changing this variable requires a restart of Emacs to take effect."
  1994. :group 'org-font-lock
  1995. :type 'boolean)
  1996. (defcustom org-highlight-latex-fragments-and-specials nil
  1997. "Non-nil means, fontify what is treated specially by the exporters."
  1998. :group 'org-font-lock
  1999. :type 'boolean)
  2000. (defcustom org-hide-emphasis-markers nil
  2001. "Non-nil mean font-lock should hide the emphasis marker characters."
  2002. :group 'org-font-lock
  2003. :type 'boolean)
  2004. (defvar org-emph-re nil
  2005. "Regular expression for matching emphasis.")
  2006. (defvar org-verbatim-re nil
  2007. "Regular expression for matching verbatim text.")
  2008. (defvar org-emphasis-regexp-components) ; defined just below
  2009. (defvar org-emphasis-alist) ; defined just below
  2010. (defun org-set-emph-re (var val)
  2011. "Set variable and compute the emphasis regular expression."
  2012. (set var val)
  2013. (when (and (boundp 'org-emphasis-alist)
  2014. (boundp 'org-emphasis-regexp-components)
  2015. org-emphasis-alist org-emphasis-regexp-components)
  2016. (let* ((e org-emphasis-regexp-components)
  2017. (pre (car e))
  2018. (post (nth 1 e))
  2019. (border (nth 2 e))
  2020. (body (nth 3 e))
  2021. (nl (nth 4 e))
  2022. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  2023. (body1 (concat body "*?"))
  2024. (markers (mapconcat 'car org-emphasis-alist ""))
  2025. (vmarkers (mapconcat
  2026. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2027. org-emphasis-alist "")))
  2028. ;; make sure special characters appear at the right position in the class
  2029. (if (string-match "\\^" markers)
  2030. (setq markers (concat (replace-match "" t t markers) "^")))
  2031. (if (string-match "-" markers)
  2032. (setq markers (concat (replace-match "" t t markers) "-")))
  2033. (if (string-match "\\^" vmarkers)
  2034. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2035. (if (string-match "-" vmarkers)
  2036. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2037. (if (> nl 0)
  2038. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2039. (int-to-string nl) "\\}")))
  2040. ;; Make the regexp
  2041. (setq org-emph-re
  2042. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  2043. "\\("
  2044. "\\([" markers "]\\)"
  2045. "\\("
  2046. "[^" border "]\\|"
  2047. "[^" border (if (and nil stacked) markers) "]"
  2048. body1
  2049. "[^" border (if (and nil stacked) markers) "]"
  2050. "\\)"
  2051. "\\3\\)"
  2052. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  2053. (setq org-verbatim-re
  2054. (concat "\\([" pre "]\\|^\\)"
  2055. "\\("
  2056. "\\([" vmarkers "]\\)"
  2057. "\\("
  2058. "[^" border "]\\|"
  2059. "[^" border "]"
  2060. body1
  2061. "[^" border "]"
  2062. "\\)"
  2063. "\\3\\)"
  2064. "\\([" post "]\\|$\\)")))))
  2065. (defcustom org-emphasis-regexp-components
  2066. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2067. "Components used to build the regular expression for emphasis.
  2068. This is a list with 6 entries. Terminology: In an emphasis string
  2069. like \" *strong word* \", we call the initial space PREMATCH, the final
  2070. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2071. and \"trong wor\" is the body. The different components in this variable
  2072. specify what is allowed/forbidden in each part:
  2073. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2074. post Chars allowed as postmatch. End of line will be allowed too.
  2075. border The chars *forbidden* as border characters.
  2076. body-regexp A regexp like \".\" to match a body character. Don't use
  2077. non-shy groups here, and don't allow newline here.
  2078. newline The maximum number of newlines allowed in an emphasis exp.
  2079. Use customize to modify this, or restart Emacs after changing it."
  2080. :group 'org-font-lock
  2081. :set 'org-set-emph-re
  2082. :type '(list
  2083. (sexp :tag "Allowed chars in pre ")
  2084. (sexp :tag "Allowed chars in post ")
  2085. (sexp :tag "Forbidden chars in border ")
  2086. (sexp :tag "Regexp for body ")
  2087. (integer :tag "number of newlines allowed")
  2088. (option (boolean :tag "Please ignore this button"))))
  2089. (defcustom org-emphasis-alist
  2090. `(("*" bold "<b>" "</b>")
  2091. ("/" italic "<i>" "</i>")
  2092. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2093. ("=" org-code "<code>" "</code>" verbatim)
  2094. ("~" org-verbatim "<code>" "</code>" verbatim)
  2095. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2096. "<del>" "</del>")
  2097. )
  2098. "Special syntax for emphasized text.
  2099. Text starting and ending with a special character will be emphasized, for
  2100. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2101. characters, the face to be used by font-lock for highlighting in Org-mode
  2102. Emacs buffers, and the HTML tags to be used for this.
  2103. Use customize to modify this, or restart Emacs after changing it."
  2104. :group 'org-font-lock
  2105. :set 'org-set-emph-re
  2106. :type '(repeat
  2107. (list
  2108. (string :tag "Marker character")
  2109. (choice
  2110. (face :tag "Font-lock-face")
  2111. (plist :tag "Face property list"))
  2112. (string :tag "HTML start tag")
  2113. (string :tag "HTML end tag")
  2114. (option (const verbatim)))))
  2115. ;;; Miscellaneous options
  2116. (defgroup org-completion nil
  2117. "Completion in Org-mode."
  2118. :tag "Org Completion"
  2119. :group 'org)
  2120. (defcustom org-completion-use-ido nil
  2121. "Non-nil means, use ido completion wherever possible.
  2122. Note that `ido-mode' must be active for this variable to be relevant.
  2123. If you decide to turn this variable on, you might well want to turn off
  2124. `org-outline-path-complete-in-steps'."
  2125. :group 'org-completion
  2126. :type 'boolean)
  2127. (defcustom org-completion-fallback-command 'hippie-expand
  2128. "The expansion command called by \\[org-complete] in normal context.
  2129. Normal means, no org-mode-specific context."
  2130. :group 'org-completion
  2131. :type 'function)
  2132. ;;; Functions and variables from ther packages
  2133. ;; Declared here to avoid compiler warnings
  2134. ;; XEmacs only
  2135. (defvar outline-mode-menu-heading)
  2136. (defvar outline-mode-menu-show)
  2137. (defvar outline-mode-menu-hide)
  2138. (defvar zmacs-regions) ; XEmacs regions
  2139. ;; Emacs only
  2140. (defvar mark-active)
  2141. ;; Various packages
  2142. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2143. (declare-function calendar-forward-day "cal-move" (arg))
  2144. (declare-function calendar-goto-date "cal-move" (date))
  2145. (declare-function calendar-goto-today "cal-move" ())
  2146. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2147. (defvar calc-embedded-close-formula)
  2148. (defvar calc-embedded-open-formula)
  2149. (declare-function cdlatex-tab "ext:cdlatex" ())
  2150. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2151. (defvar font-lock-unfontify-region-function)
  2152. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2153. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2154. (defvar iswitchb-temp-buflist)
  2155. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2156. (declare-function org-agenda-skip "org-agenda" ())
  2157. (declare-function org-format-agenda-item "org-agenda"
  2158. (extra txt &optional category tags dotime noprefix remove-re))
  2159. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2160. (declare-function org-agenda-change-all-lines "org-agenda"
  2161. (newhead hdmarker &optional fixface just-this))
  2162. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2163. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2164. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2165. (beg end))
  2166. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2167. (declare-function parse-time-string "parse-time" (string))
  2168. (declare-function remember "remember" (&optional initial))
  2169. (declare-function remember-buffer-desc "remember" ())
  2170. (declare-function remember-finalize "remember" ())
  2171. (defvar remember-save-after-remembering)
  2172. (defvar remember-data-file)
  2173. (defvar remember-register)
  2174. (defvar remember-buffer)
  2175. (defvar remember-handler-functions)
  2176. (defvar remember-annotation-functions)
  2177. (defvar texmathp-why)
  2178. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2179. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2180. (defvar w3m-current-url)
  2181. (defvar w3m-current-title)
  2182. (defvar org-latex-regexps)
  2183. ;;; Autoload and prepare some org modules
  2184. ;; Some table stuff that needs to be defined here, because it is used
  2185. ;; by the functions setting up org-mode or checking for table context.
  2186. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2187. "Detects an org-type or table-type table.")
  2188. (defconst org-table-line-regexp "^[ \t]*|"
  2189. "Detects an org-type table line.")
  2190. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2191. "Detects an org-type table line.")
  2192. (defconst org-table-hline-regexp "^[ \t]*|-"
  2193. "Detects an org-type table hline.")
  2194. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2195. "Detects a table-type table hline.")
  2196. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2197. "Searching from within a table (any type) this finds the first line
  2198. outside the table.")
  2199. ;; Autoload the functions in org-table.el that are needed by functions here.
  2200. (eval-and-compile
  2201. (org-autoload "org-table"
  2202. '(org-table-align org-table-begin org-table-blank-field
  2203. org-table-convert org-table-convert-region org-table-copy-down
  2204. org-table-copy-region org-table-create
  2205. org-table-create-or-convert-from-region
  2206. org-table-create-with-table.el org-table-current-dline
  2207. org-table-cut-region org-table-delete-column org-table-edit-field
  2208. org-table-edit-formulas org-table-end org-table-eval-formula
  2209. org-table-export org-table-field-info
  2210. org-table-get-stored-formulas org-table-goto-column
  2211. org-table-hline-and-move org-table-import org-table-insert-column
  2212. org-table-insert-hline org-table-insert-row org-table-iterate
  2213. org-table-justify-field-maybe org-table-kill-row
  2214. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2215. org-table-move-column org-table-move-column-left
  2216. org-table-move-column-right org-table-move-row
  2217. org-table-move-row-down org-table-move-row-up
  2218. org-table-next-field org-table-next-row org-table-paste-rectangle
  2219. org-table-previous-field org-table-recalculate
  2220. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2221. org-table-toggle-coordinate-overlays
  2222. org-table-toggle-formula-debugger org-table-wrap-region
  2223. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2224. (defun org-at-table-p (&optional table-type)
  2225. "Return t if the cursor is inside an org-type table.
  2226. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2227. (if org-enable-table-editor
  2228. (save-excursion
  2229. (beginning-of-line 1)
  2230. (looking-at (if table-type org-table-any-line-regexp
  2231. org-table-line-regexp)))
  2232. nil))
  2233. (defsubst org-table-p () (org-at-table-p))
  2234. (defun org-at-table.el-p ()
  2235. "Return t if and only if we are at a table.el table."
  2236. (and (org-at-table-p 'any)
  2237. (save-excursion
  2238. (goto-char (org-table-begin 'any))
  2239. (looking-at org-table1-hline-regexp))))
  2240. (defun org-table-recognize-table.el ()
  2241. "If there is a table.el table nearby, recognize it and move into it."
  2242. (if org-table-tab-recognizes-table.el
  2243. (if (org-at-table.el-p)
  2244. (progn
  2245. (beginning-of-line 1)
  2246. (if (looking-at org-table-dataline-regexp)
  2247. nil
  2248. (if (looking-at org-table1-hline-regexp)
  2249. (progn
  2250. (beginning-of-line 2)
  2251. (if (looking-at org-table-any-border-regexp)
  2252. (beginning-of-line -1)))))
  2253. (if (re-search-forward "|" (org-table-end t) t)
  2254. (progn
  2255. (require 'table)
  2256. (if (table--at-cell-p (point))
  2257. t
  2258. (message "recognizing table.el table...")
  2259. (table-recognize-table)
  2260. (message "recognizing table.el table...done")))
  2261. (error "This should not happen..."))
  2262. t)
  2263. nil)
  2264. nil))
  2265. (defun org-at-table-hline-p ()
  2266. "Return t if the cursor is inside a hline in a table."
  2267. (if org-enable-table-editor
  2268. (save-excursion
  2269. (beginning-of-line 1)
  2270. (looking-at org-table-hline-regexp))
  2271. nil))
  2272. (defvar org-table-clean-did-remove-column nil)
  2273. (defun org-table-map-tables (function)
  2274. "Apply FUNCTION to the start of all tables in the buffer."
  2275. (save-excursion
  2276. (save-restriction
  2277. (widen)
  2278. (goto-char (point-min))
  2279. (while (re-search-forward org-table-any-line-regexp nil t)
  2280. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2281. (beginning-of-line 1)
  2282. (if (looking-at org-table-line-regexp)
  2283. (save-excursion (funcall function)))
  2284. (re-search-forward org-table-any-border-regexp nil 1))))
  2285. (message "Mapping tables: done"))
  2286. ;; Declare and autoload functions from org-exp.el
  2287. (declare-function org-default-export-plist "org-exp")
  2288. (declare-function org-infile-export-plist "org-exp")
  2289. (declare-function org-get-current-options "org-exp")
  2290. (eval-and-compile
  2291. (org-autoload "org-exp"
  2292. '(org-export org-export-as-ascii org-export-visible
  2293. org-insert-export-options-template org-export-as-html-and-open
  2294. org-export-as-html-batch org-export-as-html-to-buffer
  2295. org-replace-region-by-html org-export-region-as-html
  2296. org-export-as-html org-export-icalendar-this-file
  2297. org-export-icalendar-all-agenda-files
  2298. org-table-clean-before-export
  2299. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2300. ;; Declare and autoload functions from org-agenda.el
  2301. (eval-and-compile
  2302. (org-autoload "org-agenda"
  2303. '(org-agenda org-agenda-list org-search-view
  2304. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2305. org-diary org-agenda-to-appt)))
  2306. ;; Autoload org-remember
  2307. (eval-and-compile
  2308. (org-autoload "org-remember"
  2309. '(org-remember-insinuate org-remember-annotation
  2310. org-remember-apply-template org-remember org-remember-handler)))
  2311. ;; Autoload org-clock.el
  2312. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2313. (beg end))
  2314. (declare-function org-update-mode-line "org-clock" ())
  2315. (defvar org-clock-start-time)
  2316. (defvar org-clock-marker (make-marker)
  2317. "Marker recording the last clock-in.")
  2318. (eval-and-compile
  2319. (org-autoload
  2320. "org-clock"
  2321. '(org-clock-in org-clock-out org-clock-cancel
  2322. org-clock-goto org-clock-sum org-clock-display
  2323. org-remove-clock-overlays org-clock-report
  2324. org-clocktable-shift org-dblock-write:clocktable
  2325. org-get-clocktable)))
  2326. (defun org-clock-update-time-maybe ()
  2327. "If this is a CLOCK line, update it and return t.
  2328. Otherwise, return nil."
  2329. (interactive)
  2330. (save-excursion
  2331. (beginning-of-line 1)
  2332. (skip-chars-forward " \t")
  2333. (when (looking-at org-clock-string)
  2334. (let ((re (concat "[ \t]*" org-clock-string
  2335. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2336. "\\([ \t]*=>.*\\)?\\)?"))
  2337. ts te h m s neg)
  2338. (cond
  2339. ((not (looking-at re))
  2340. nil)
  2341. ((not (match-end 2))
  2342. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2343. (> org-clock-marker (point))
  2344. (<= org-clock-marker (point-at-eol)))
  2345. ;; The clock is running here
  2346. (setq org-clock-start-time
  2347. (apply 'encode-time
  2348. (org-parse-time-string (match-string 1))))
  2349. (org-update-mode-line)))
  2350. (t
  2351. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2352. (end-of-line 1)
  2353. (setq ts (match-string 1)
  2354. te (match-string 3))
  2355. (setq s (- (time-to-seconds
  2356. (apply 'encode-time (org-parse-time-string te)))
  2357. (time-to-seconds
  2358. (apply 'encode-time (org-parse-time-string ts))))
  2359. neg (< s 0)
  2360. s (abs s)
  2361. h (floor (/ s 3600))
  2362. s (- s (* 3600 h))
  2363. m (floor (/ s 60))
  2364. s (- s (* 60 s)))
  2365. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2366. t))))))
  2367. (defun org-check-running-clock ()
  2368. "Check if the current buffer contains the running clock.
  2369. If yes, offer to stop it and to save the buffer with the changes."
  2370. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2371. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2372. (buffer-name))))
  2373. (org-clock-out)
  2374. (when (y-or-n-p "Save changed buffer?")
  2375. (save-buffer))))
  2376. (defun org-clocktable-try-shift (dir n)
  2377. "Check if this line starts a clock table, if yes, shift the time block."
  2378. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2379. (org-clocktable-shift dir n)))
  2380. ;; Autoload org-timer.el
  2381. ;(declare-function org-timer "org-timer")
  2382. (eval-and-compile
  2383. (org-autoload
  2384. "org-timer"
  2385. '(org-timer-start org-timer org-timer-item
  2386. org-timer-change-times-in-region)))
  2387. ;; Autoload archiving code
  2388. ;; The stuff that is needed for cycling and tags has to be defined here.
  2389. (defgroup org-archive nil
  2390. "Options concerning archiving in Org-mode."
  2391. :tag "Org Archive"
  2392. :group 'org-structure)
  2393. (defcustom org-archive-location "%s_archive::"
  2394. "The location where subtrees should be archived.
  2395. The value of this variable is a string, consisting of two parts,
  2396. separated by a double-colon. The first part is a filename and
  2397. the second part is a headline.
  2398. When the filename is omitted, archiving happens in the same file.
  2399. %s in the filename will be replaced by the current file
  2400. name (without the directory part). Archiving to a different file
  2401. is useful to keep archived entries from contributing to the
  2402. Org-mode Agenda.
  2403. The archived entries will be filed as subtrees of the specified
  2404. headline. When the headline is omitted, the subtrees are simply
  2405. filed away at the end of the file, as top-level entries.
  2406. Here are a few examples:
  2407. \"%s_archive::\"
  2408. If the current file is Projects.org, archive in file
  2409. Projects.org_archive, as top-level trees. This is the default.
  2410. \"::* Archived Tasks\"
  2411. Archive in the current file, under the top-level headline
  2412. \"* Archived Tasks\".
  2413. \"~/org/archive.org::\"
  2414. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2415. \"basement::** Finished Tasks\"
  2416. Archive in file ./basement (relative path), as level 3 trees
  2417. below the level 2 heading \"** Finished Tasks\".
  2418. You may set this option on a per-file basis by adding to the buffer a
  2419. line like
  2420. #+ARCHIVE: basement::** Finished Tasks
  2421. You may also define it locally for a subtree by setting an ARCHIVE property
  2422. in the entry. If such a property is found in an entry, or anywhere up
  2423. the hierarchy, it will be used."
  2424. :group 'org-archive
  2425. :type 'string)
  2426. (defcustom org-archive-tag "ARCHIVE"
  2427. "The tag that marks a subtree as archived.
  2428. An archived subtree does not open during visibility cycling, and does
  2429. not contribute to the agenda listings.
  2430. After changing this, font-lock must be restarted in the relevant buffers to
  2431. get the proper fontification."
  2432. :group 'org-archive
  2433. :group 'org-keywords
  2434. :type 'string)
  2435. (defcustom org-agenda-skip-archived-trees t
  2436. "Non-nil means, the agenda will skip any items located in archived trees.
  2437. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2438. variable is no longer recommended, you should leave it at the value t.
  2439. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2440. :group 'org-archive
  2441. :group 'org-agenda-skip
  2442. :type 'boolean)
  2443. (defcustom org-cycle-open-archived-trees nil
  2444. "Non-nil means, `org-cycle' will open archived trees.
  2445. An archived tree is a tree marked with the tag ARCHIVE.
  2446. When nil, archived trees will stay folded. You can still open them with
  2447. normal outline commands like `show-all', but not with the cycling commands."
  2448. :group 'org-archive
  2449. :group 'org-cycle
  2450. :type 'boolean)
  2451. (defcustom org-sparse-tree-open-archived-trees nil
  2452. "Non-nil means sparse tree construction shows matches in archived trees.
  2453. When nil, matches in these trees are highlighted, but the trees are kept in
  2454. collapsed state."
  2455. :group 'org-archive
  2456. :group 'org-sparse-trees
  2457. :type 'boolean)
  2458. (defun org-cycle-hide-archived-subtrees (state)
  2459. "Re-hide all archived subtrees after a visibility state change."
  2460. (when (and (not org-cycle-open-archived-trees)
  2461. (not (memq state '(overview folded))))
  2462. (save-excursion
  2463. (let* ((globalp (memq state '(contents all)))
  2464. (beg (if globalp (point-min) (point)))
  2465. (end (if globalp (point-max) (org-end-of-subtree t))))
  2466. (org-hide-archived-subtrees beg end)
  2467. (goto-char beg)
  2468. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2469. (message "%s" (substitute-command-keys
  2470. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2471. (defun org-force-cycle-archived ()
  2472. "Cycle subtree even if it is archived."
  2473. (interactive)
  2474. (setq this-command 'org-cycle)
  2475. (let ((org-cycle-open-archived-trees t))
  2476. (call-interactively 'org-cycle)))
  2477. (defun org-hide-archived-subtrees (beg end)
  2478. "Re-hide all archived subtrees after a visibility state change."
  2479. (save-excursion
  2480. (let* ((re (concat ":" org-archive-tag ":")))
  2481. (goto-char beg)
  2482. (while (re-search-forward re end t)
  2483. (and (org-on-heading-p) (hide-subtree))
  2484. (org-end-of-subtree t)))))
  2485. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2486. (eval-and-compile
  2487. (org-autoload "org-archive"
  2488. '(org-add-archive-files org-archive-subtree
  2489. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2490. ;; Autoload Column View Code
  2491. (declare-function org-columns-number-to-string "org-colview")
  2492. (declare-function org-columns-get-format-and-top-level "org-colview")
  2493. (declare-function org-columns-compute "org-colview")
  2494. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2495. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2496. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2497. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2498. ;; Autoload ID code
  2499. (declare-function org-id-store-link "org-id")
  2500. (org-autoload "org-id"
  2501. '(org-id-get-create org-id-new org-id-copy org-id-get
  2502. org-id-get-with-outline-path-completion
  2503. org-id-get-with-outline-drilling
  2504. org-id-goto org-id-find org-id-store-link))
  2505. ;;; Variables for pre-computed regular expressions, all buffer local
  2506. (defvar org-drawer-regexp nil
  2507. "Matches first line of a hidden block.")
  2508. (make-variable-buffer-local 'org-drawer-regexp)
  2509. (defvar org-todo-regexp nil
  2510. "Matches any of the TODO state keywords.")
  2511. (make-variable-buffer-local 'org-todo-regexp)
  2512. (defvar org-not-done-regexp nil
  2513. "Matches any of the TODO state keywords except the last one.")
  2514. (make-variable-buffer-local 'org-not-done-regexp)
  2515. (defvar org-todo-line-regexp nil
  2516. "Matches a headline and puts TODO state into group 2 if present.")
  2517. (make-variable-buffer-local 'org-todo-line-regexp)
  2518. (defvar org-complex-heading-regexp nil
  2519. "Matches a headline and puts everything into groups:
  2520. group 1: the stars
  2521. group 2: The todo keyword, maybe
  2522. group 3: Priority cookie
  2523. group 4: True headline
  2524. group 5: Tags")
  2525. (make-variable-buffer-local 'org-complex-heading-regexp)
  2526. (defvar org-todo-line-tags-regexp nil
  2527. "Matches a headline and puts TODO state into group 2 if present.
  2528. Also put tags into group 4 if tags are present.")
  2529. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2530. (defvar org-nl-done-regexp nil
  2531. "Matches newline followed by a headline with the DONE keyword.")
  2532. (make-variable-buffer-local 'org-nl-done-regexp)
  2533. (defvar org-looking-at-done-regexp nil
  2534. "Matches the DONE keyword a point.")
  2535. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2536. (defvar org-ds-keyword-length 12
  2537. "Maximum length of the Deadline and SCHEDULED keywords.")
  2538. (make-variable-buffer-local 'org-ds-keyword-length)
  2539. (defvar org-deadline-regexp nil
  2540. "Matches the DEADLINE keyword.")
  2541. (make-variable-buffer-local 'org-deadline-regexp)
  2542. (defvar org-deadline-time-regexp nil
  2543. "Matches the DEADLINE keyword together with a time stamp.")
  2544. (make-variable-buffer-local 'org-deadline-time-regexp)
  2545. (defvar org-deadline-line-regexp nil
  2546. "Matches the DEADLINE keyword and the rest of the line.")
  2547. (make-variable-buffer-local 'org-deadline-line-regexp)
  2548. (defvar org-scheduled-regexp nil
  2549. "Matches the SCHEDULED keyword.")
  2550. (make-variable-buffer-local 'org-scheduled-regexp)
  2551. (defvar org-scheduled-time-regexp nil
  2552. "Matches the SCHEDULED keyword together with a time stamp.")
  2553. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2554. (defvar org-closed-time-regexp nil
  2555. "Matches the CLOSED keyword together with a time stamp.")
  2556. (make-variable-buffer-local 'org-closed-time-regexp)
  2557. (defvar org-keyword-time-regexp nil
  2558. "Matches any of the 4 keywords, together with the time stamp.")
  2559. (make-variable-buffer-local 'org-keyword-time-regexp)
  2560. (defvar org-keyword-time-not-clock-regexp nil
  2561. "Matches any of the 3 keywords, together with the time stamp.")
  2562. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2563. (defvar org-maybe-keyword-time-regexp nil
  2564. "Matches a timestamp, possibly preceeded by a keyword.")
  2565. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2566. (defvar org-planning-or-clock-line-re nil
  2567. "Matches a line with planning or clock info.")
  2568. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2569. (defconst org-plain-time-of-day-regexp
  2570. (concat
  2571. "\\(\\<[012]?[0-9]"
  2572. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2573. "\\(--?"
  2574. "\\(\\<[012]?[0-9]"
  2575. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2576. "\\)?")
  2577. "Regular expression to match a plain time or time range.
  2578. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2579. groups carry important information:
  2580. 0 the full match
  2581. 1 the first time, range or not
  2582. 8 the second time, if it is a range.")
  2583. (defconst org-plain-time-extension-regexp
  2584. (concat
  2585. "\\(\\<[012]?[0-9]"
  2586. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2587. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2588. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2589. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2590. groups carry important information:
  2591. 0 the full match
  2592. 7 hours of duration
  2593. 9 minutes of duration")
  2594. (defconst org-stamp-time-of-day-regexp
  2595. (concat
  2596. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2597. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2598. "\\(--?"
  2599. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2600. "Regular expression to match a timestamp time or time range.
  2601. After a match, the following groups carry important information:
  2602. 0 the full match
  2603. 1 date plus weekday, for backreferencing to make sure both times on same day
  2604. 2 the first time, range or not
  2605. 4 the second time, if it is a range.")
  2606. (defconst org-startup-options
  2607. '(("fold" org-startup-folded t)
  2608. ("overview" org-startup-folded t)
  2609. ("nofold" org-startup-folded nil)
  2610. ("showall" org-startup-folded nil)
  2611. ("content" org-startup-folded content)
  2612. ("hidestars" org-hide-leading-stars t)
  2613. ("showstars" org-hide-leading-stars nil)
  2614. ("odd" org-odd-levels-only t)
  2615. ("oddeven" org-odd-levels-only nil)
  2616. ("align" org-startup-align-all-tables t)
  2617. ("noalign" org-startup-align-all-tables nil)
  2618. ("customtime" org-display-custom-times t)
  2619. ("logdone" org-log-done time)
  2620. ("lognotedone" org-log-done note)
  2621. ("nologdone" org-log-done nil)
  2622. ("lognoteclock-out" org-log-note-clock-out t)
  2623. ("nolognoteclock-out" org-log-note-clock-out nil)
  2624. ("logrepeat" org-log-repeat state)
  2625. ("lognoterepeat" org-log-repeat note)
  2626. ("nologrepeat" org-log-repeat nil)
  2627. ("fninline" org-footnote-define-inline t)
  2628. ("nofninline" org-footnote-define-inline nil)
  2629. ("fnlocal" org-footnote-section nil)
  2630. ("fnauto" org-footnote-auto-label t)
  2631. ("fnprompt" org-footnote-auto-label nil)
  2632. ("fnconfirm" org-footnote-auto-label confirm)
  2633. ("fnplain" org-footnote-auto-label plain)
  2634. ("constcgs" constants-unit-system cgs)
  2635. ("constSI" constants-unit-system SI))
  2636. "Variable associated with STARTUP options for org-mode.
  2637. Each element is a list of three items: The startup options as written
  2638. in the #+STARTUP line, the corresponding variable, and the value to
  2639. set this variable to if the option is found. An optional forth element PUSH
  2640. means to push this value onto the list in the variable.")
  2641. (defun org-set-regexps-and-options ()
  2642. "Precompute regular expressions for current buffer."
  2643. (when (org-mode-p)
  2644. (org-set-local 'org-todo-kwd-alist nil)
  2645. (org-set-local 'org-todo-key-alist nil)
  2646. (org-set-local 'org-todo-key-trigger nil)
  2647. (org-set-local 'org-todo-keywords-1 nil)
  2648. (org-set-local 'org-done-keywords nil)
  2649. (org-set-local 'org-todo-heads nil)
  2650. (org-set-local 'org-todo-sets nil)
  2651. (org-set-local 'org-todo-log-states nil)
  2652. (org-set-local 'org-file-properties nil)
  2653. (org-set-local 'org-file-tags nil)
  2654. (let ((re (org-make-options-regexp
  2655. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2656. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2657. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2658. (splitre "[ \t]+")
  2659. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2660. tail sep kws1 prio props ftags drawers
  2661. ext-setup-or-nil setup-contents (start 0))
  2662. (save-excursion
  2663. (save-restriction
  2664. (widen)
  2665. (goto-char (point-min))
  2666. (while (or (and ext-setup-or-nil
  2667. (string-match re ext-setup-or-nil start)
  2668. (setq start (match-end 0)))
  2669. (and (setq ext-setup-or-nil nil start 0)
  2670. (re-search-forward re nil t)))
  2671. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2672. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2673. (cond
  2674. ((equal key "CATEGORY")
  2675. (if (string-match "[ \t]+$" value)
  2676. (setq value (replace-match "" t t value)))
  2677. (setq cat value))
  2678. ((member key '("SEQ_TODO" "TODO"))
  2679. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2680. ((equal key "TYP_TODO")
  2681. (push (cons 'type (org-split-string value splitre)) kwds))
  2682. ((equal key "TAGS")
  2683. (setq tags (append tags (org-split-string value splitre))))
  2684. ((equal key "COLUMNS")
  2685. (org-set-local 'org-columns-default-format value))
  2686. ((equal key "LINK")
  2687. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2688. (push (cons (match-string 1 value)
  2689. (org-trim (match-string 2 value)))
  2690. links)))
  2691. ((equal key "PRIORITIES")
  2692. (setq prio (org-split-string value " +")))
  2693. ((equal key "PROPERTY")
  2694. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2695. (push (cons (match-string 1 value) (match-string 2 value))
  2696. props)))
  2697. ((equal key "FILETAGS")
  2698. (when (string-match "\\S-" value)
  2699. (setq ftags
  2700. (append
  2701. ftags
  2702. (apply 'append
  2703. (mapcar (lambda (x) (org-split-string x ":"))
  2704. (org-split-string value)))))))
  2705. ((equal key "DRAWERS")
  2706. (setq drawers (org-split-string value splitre)))
  2707. ((equal key "CONSTANTS")
  2708. (setq const (append const (org-split-string value splitre))))
  2709. ((equal key "STARTUP")
  2710. (let ((opts (org-split-string value splitre))
  2711. l var val)
  2712. (while (setq l (pop opts))
  2713. (when (setq l (assoc l org-startup-options))
  2714. (setq var (nth 1 l) val (nth 2 l))
  2715. (if (not (nth 3 l))
  2716. (set (make-local-variable var) val)
  2717. (if (not (listp (symbol-value var)))
  2718. (set (make-local-variable var) nil))
  2719. (set (make-local-variable var) (symbol-value var))
  2720. (add-to-list var val))))))
  2721. ((equal key "ARCHIVE")
  2722. (string-match " *$" value)
  2723. (setq arch (replace-match "" t t value))
  2724. (remove-text-properties 0 (length arch)
  2725. '(face t fontified t) arch))
  2726. ((equal key "SETUPFILE")
  2727. (setq setup-contents (org-file-contents
  2728. (expand-file-name
  2729. (org-remove-double-quotes value))
  2730. 'noerror))
  2731. (if (not ext-setup-or-nil)
  2732. (setq ext-setup-or-nil setup-contents start 0)
  2733. (setq ext-setup-or-nil
  2734. (concat (substring ext-setup-or-nil 0 start)
  2735. "\n" setup-contents "\n"
  2736. (substring ext-setup-or-nil start)))))
  2737. ))))
  2738. (when cat
  2739. (org-set-local 'org-category (intern cat))
  2740. (push (cons "CATEGORY" cat) props))
  2741. (when prio
  2742. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2743. (setq prio (mapcar 'string-to-char prio))
  2744. (org-set-local 'org-highest-priority (nth 0 prio))
  2745. (org-set-local 'org-lowest-priority (nth 1 prio))
  2746. (org-set-local 'org-default-priority (nth 2 prio)))
  2747. (and props (org-set-local 'org-file-properties (nreverse props)))
  2748. (and ftags (org-set-local 'org-file-tags ftags))
  2749. (and drawers (org-set-local 'org-drawers drawers))
  2750. (and arch (org-set-local 'org-archive-location arch))
  2751. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2752. ;; Process the TODO keywords
  2753. (unless kwds
  2754. ;; Use the global values as if they had been given locally.
  2755. (setq kwds (default-value 'org-todo-keywords))
  2756. (if (stringp (car kwds))
  2757. (setq kwds (list (cons org-todo-interpretation
  2758. (default-value 'org-todo-keywords)))))
  2759. (setq kwds (reverse kwds)))
  2760. (setq kwds (nreverse kwds))
  2761. (let (inter kws kw)
  2762. (while (setq kws (pop kwds))
  2763. (setq inter (pop kws) sep (member "|" kws)
  2764. kws0 (delete "|" (copy-sequence kws))
  2765. kwsa nil
  2766. kws1 (mapcar
  2767. (lambda (x)
  2768. ;; 1 2
  2769. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2770. (progn
  2771. (setq kw (match-string 1 x)
  2772. key (and (match-end 2) (match-string 2 x))
  2773. log (org-extract-log-state-settings x))
  2774. (push (cons kw (and key (string-to-char key))) kwsa)
  2775. (and log (push log org-todo-log-states))
  2776. kw)
  2777. (error "Invalid TODO keyword %s" x)))
  2778. kws0)
  2779. kwsa (if kwsa (append '((:startgroup))
  2780. (nreverse kwsa)
  2781. '((:endgroup))))
  2782. hw (car kws1)
  2783. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2784. tail (list inter hw (car dws) (org-last dws)))
  2785. (add-to-list 'org-todo-heads hw 'append)
  2786. (push kws1 org-todo-sets)
  2787. (setq org-done-keywords (append org-done-keywords dws nil))
  2788. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2789. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2790. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2791. (setq org-todo-sets (nreverse org-todo-sets)
  2792. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2793. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2794. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2795. ;; Process the constants
  2796. (when const
  2797. (let (e cst)
  2798. (while (setq e (pop const))
  2799. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2800. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2801. (setq org-table-formula-constants-local cst)))
  2802. ;; Process the tags.
  2803. (when tags
  2804. (let (e tgs)
  2805. (while (setq e (pop tags))
  2806. (cond
  2807. ((equal e "{") (push '(:startgroup) tgs))
  2808. ((equal e "}") (push '(:endgroup) tgs))
  2809. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2810. (push (cons (match-string 1 e)
  2811. (string-to-char (match-string 2 e)))
  2812. tgs))
  2813. (t (push (list e) tgs))))
  2814. (org-set-local 'org-tag-alist nil)
  2815. (while (setq e (pop tgs))
  2816. (or (and (stringp (car e))
  2817. (assoc (car e) org-tag-alist))
  2818. (push e org-tag-alist)))))
  2819. ;; Compute the regular expressions and other local variables
  2820. (if (not org-done-keywords)
  2821. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2822. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2823. (length org-scheduled-string)
  2824. (length org-clock-string)
  2825. (length org-closed-string)))
  2826. org-drawer-regexp
  2827. (concat "^[ \t]*:\\("
  2828. (mapconcat 'regexp-quote org-drawers "\\|")
  2829. "\\):[ \t]*$")
  2830. org-not-done-keywords
  2831. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2832. org-todo-regexp
  2833. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2834. "\\|") "\\)\\>")
  2835. org-not-done-regexp
  2836. (concat "\\<\\("
  2837. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2838. "\\)\\>")
  2839. org-todo-line-regexp
  2840. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2841. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2842. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2843. org-complex-heading-regexp
  2844. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2845. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2846. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2847. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2848. org-nl-done-regexp
  2849. (concat "\n\\*+[ \t]+"
  2850. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2851. "\\)" "\\>")
  2852. org-todo-line-tags-regexp
  2853. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2854. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2855. (org-re
  2856. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2857. org-looking-at-done-regexp
  2858. (concat "^" "\\(?:"
  2859. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2860. "\\>")
  2861. org-deadline-regexp (concat "\\<" org-deadline-string)
  2862. org-deadline-time-regexp
  2863. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2864. org-deadline-line-regexp
  2865. (concat "\\<\\(" org-deadline-string "\\).*")
  2866. org-scheduled-regexp
  2867. (concat "\\<" org-scheduled-string)
  2868. org-scheduled-time-regexp
  2869. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2870. org-closed-time-regexp
  2871. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2872. org-keyword-time-regexp
  2873. (concat "\\<\\(" org-scheduled-string
  2874. "\\|" org-deadline-string
  2875. "\\|" org-closed-string
  2876. "\\|" org-clock-string "\\)"
  2877. " *[[<]\\([^]>]+\\)[]>]")
  2878. org-keyword-time-not-clock-regexp
  2879. (concat "\\<\\(" org-scheduled-string
  2880. "\\|" org-deadline-string
  2881. "\\|" org-closed-string
  2882. "\\)"
  2883. " *[[<]\\([^]>]+\\)[]>]")
  2884. org-maybe-keyword-time-regexp
  2885. (concat "\\(\\<\\(" org-scheduled-string
  2886. "\\|" org-deadline-string
  2887. "\\|" org-closed-string
  2888. "\\|" org-clock-string "\\)\\)?"
  2889. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2890. org-planning-or-clock-line-re
  2891. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2892. "\\|" org-deadline-string
  2893. "\\|" org-closed-string "\\|" org-clock-string
  2894. "\\)\\>\\)")
  2895. )
  2896. (org-compute-latex-and-specials-regexp)
  2897. (org-set-font-lock-defaults))))
  2898. (defun org-file-contents (file &optional noerror)
  2899. "Return the contents of FILE, as a string."
  2900. (if (or (not file)
  2901. (not (file-readable-p file)))
  2902. (if noerror
  2903. (progn
  2904. (message "Cannot read file %s" file)
  2905. (ding) (sit-for 2)
  2906. "")
  2907. (error "Cannot read file %s" file))
  2908. (with-temp-buffer
  2909. (insert-file-contents file)
  2910. (buffer-string))))
  2911. (defun org-extract-log-state-settings (x)
  2912. "Extract the log state setting from a TODO keyword string.
  2913. This will extract info from a string like \"WAIT(w@/!)\"."
  2914. (let (kw key log1 log2)
  2915. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2916. (setq kw (match-string 1 x)
  2917. key (and (match-end 2) (match-string 2 x))
  2918. log1 (and (match-end 3) (match-string 3 x))
  2919. log2 (and (match-end 4) (match-string 4 x)))
  2920. (and (or log1 log2)
  2921. (list kw
  2922. (and log1 (if (equal log1 "!") 'time 'note))
  2923. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2924. (defun org-remove-keyword-keys (list)
  2925. "Remove a pair of parenthesis at the end of each string in LIST."
  2926. (mapcar (lambda (x)
  2927. (if (string-match "(.*)$" x)
  2928. (substring x 0 (match-beginning 0))
  2929. x))
  2930. list))
  2931. ;; FIXME: this could be done much better, using second characters etc.
  2932. (defun org-assign-fast-keys (alist)
  2933. "Assign fast keys to a keyword-key alist.
  2934. Respect keys that are already there."
  2935. (let (new e k c c1 c2 (char ?a))
  2936. (while (setq e (pop alist))
  2937. (cond
  2938. ((equal e '(:startgroup)) (push e new))
  2939. ((equal e '(:endgroup)) (push e new))
  2940. (t
  2941. (setq k (car e) c2 nil)
  2942. (if (cdr e)
  2943. (setq c (cdr e))
  2944. ;; automatically assign a character.
  2945. (setq c1 (string-to-char
  2946. (downcase (substring
  2947. k (if (= (string-to-char k) ?@) 1 0)))))
  2948. (if (or (rassoc c1 new) (rassoc c1 alist))
  2949. (while (or (rassoc char new) (rassoc char alist))
  2950. (setq char (1+ char)))
  2951. (setq c2 c1))
  2952. (setq c (or c2 char)))
  2953. (push (cons k c) new))))
  2954. (nreverse new)))
  2955. ;;; Some variables used in various places
  2956. (defvar org-window-configuration nil
  2957. "Used in various places to store a window configuration.")
  2958. (defvar org-finish-function nil
  2959. "Function to be called when `C-c C-c' is used.
  2960. This is for getting out of special buffers like remember.")
  2961. ;; FIXME: Occasionally check by commenting these, to make sure
  2962. ;; no other functions uses these, forgetting to let-bind them.
  2963. (defvar entry)
  2964. (defvar state)
  2965. (defvar last-state)
  2966. (defvar date)
  2967. (defvar description)
  2968. ;; Defined somewhere in this file, but used before definition.
  2969. (defvar org-html-entities)
  2970. (defvar org-struct-menu)
  2971. (defvar org-org-menu)
  2972. (defvar org-tbl-menu)
  2973. (defvar org-agenda-keymap)
  2974. ;;;; Define the Org-mode
  2975. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2976. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or upgrade to newer allout, for example by switching to Emacs 22."))
  2977. ;; We use a before-change function to check if a table might need
  2978. ;; an update.
  2979. (defvar org-table-may-need-update t
  2980. "Indicates that a table might need an update.
  2981. This variable is set by `org-before-change-function'.
  2982. `org-table-align' sets it back to nil.")
  2983. (defun org-before-change-function (beg end)
  2984. "Every change indicates that a table might need an update."
  2985. (setq org-table-may-need-update t))
  2986. (defvar org-mode-map)
  2987. (defvar org-mode-hook nil
  2988. "Mode hook for Org-mode, run after the mode was turned on.")
  2989. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2990. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2991. (defvar org-table-buffer-is-an nil)
  2992. (defconst org-outline-regexp "\\*+ ")
  2993. ;;;###autoload
  2994. (define-derived-mode org-mode outline-mode "Org"
  2995. "Outline-based notes management and organizer, alias
  2996. \"Carsten's outline-mode for keeping track of everything.\"
  2997. Org-mode develops organizational tasks around a NOTES file which
  2998. contains information about projects as plain text. Org-mode is
  2999. implemented on top of outline-mode, which is ideal to keep the content
  3000. of large files well structured. It supports ToDo items, deadlines and
  3001. time stamps, which magically appear in the diary listing of the Emacs
  3002. calendar. Tables are easily created with a built-in table editor.
  3003. Plain text URL-like links connect to websites, emails (VM), Usenet
  3004. messages (Gnus), BBDB entries, and any files related to the project.
  3005. For printing and sharing of notes, an Org-mode file (or a part of it)
  3006. can be exported as a structured ASCII or HTML file.
  3007. The following commands are available:
  3008. \\{org-mode-map}"
  3009. ;; Get rid of Outline menus, they are not needed
  3010. ;; Need to do this here because define-derived-mode sets up
  3011. ;; the keymap so late. Still, it is a waste to call this each time
  3012. ;; we switch another buffer into org-mode.
  3013. (if (featurep 'xemacs)
  3014. (when (boundp 'outline-mode-menu-heading)
  3015. ;; Assume this is Greg's port, it used easymenu
  3016. (easy-menu-remove outline-mode-menu-heading)
  3017. (easy-menu-remove outline-mode-menu-show)
  3018. (easy-menu-remove outline-mode-menu-hide))
  3019. (define-key org-mode-map [menu-bar headings] 'undefined)
  3020. (define-key org-mode-map [menu-bar hide] 'undefined)
  3021. (define-key org-mode-map [menu-bar show] 'undefined))
  3022. (org-load-modules-maybe)
  3023. (easy-menu-add org-org-menu)
  3024. (easy-menu-add org-tbl-menu)
  3025. (org-install-agenda-files-menu)
  3026. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3027. (org-add-to-invisibility-spec '(org-cwidth))
  3028. (when (featurep 'xemacs)
  3029. (org-set-local 'line-move-ignore-invisible t))
  3030. (org-set-local 'outline-regexp org-outline-regexp)
  3031. (org-set-local 'outline-level 'org-outline-level)
  3032. (when (and org-ellipsis
  3033. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3034. (fboundp 'make-glyph-code))
  3035. (unless org-display-table
  3036. (setq org-display-table (make-display-table)))
  3037. (set-display-table-slot
  3038. org-display-table 4
  3039. (vconcat (mapcar
  3040. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3041. org-ellipsis)))
  3042. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3043. (setq buffer-display-table org-display-table))
  3044. (org-set-regexps-and-options)
  3045. (when (and org-tag-faces (not org-tags-special-faces-re))
  3046. ;; tag faces set outside customize.... force initialization.
  3047. (org-set-tag-faces 'org-tag-faces org-tag-faces))
  3048. ;; Calc embedded
  3049. (org-set-local 'calc-embedded-open-mode "# ")
  3050. (modify-syntax-entry ?# "<")
  3051. (modify-syntax-entry ?@ "w")
  3052. (if org-startup-truncated (setq truncate-lines t))
  3053. (org-set-local 'font-lock-unfontify-region-function
  3054. 'org-unfontify-region)
  3055. ;; Activate before-change-function
  3056. (org-set-local 'org-table-may-need-update t)
  3057. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3058. 'local)
  3059. ;; Check for running clock before killing a buffer
  3060. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3061. ;; Paragraphs and auto-filling
  3062. (org-set-autofill-regexps)
  3063. (setq indent-line-function 'org-indent-line-function)
  3064. (org-update-radio-target-regexp)
  3065. ;; Comment characters
  3066. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3067. (org-set-local 'comment-padding " ")
  3068. ;; Align options lines
  3069. (org-set-local
  3070. 'align-mode-rules-list
  3071. '((org-in-buffer-settings
  3072. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3073. (modes . '(org-mode)))))
  3074. ;; Imenu
  3075. (org-set-local 'imenu-create-index-function
  3076. 'org-imenu-get-tree)
  3077. ;; Make isearch reveal context
  3078. (if (or (featurep 'xemacs)
  3079. (not (boundp 'outline-isearch-open-invisible-function)))
  3080. ;; Emacs 21 and XEmacs make use of the hook
  3081. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3082. ;; Emacs 22 deals with this through a special variable
  3083. (org-set-local 'outline-isearch-open-invisible-function
  3084. (lambda (&rest ignore) (org-show-context 'isearch))))
  3085. ;; If empty file that did not turn on org-mode automatically, make it to.
  3086. (if (and org-insert-mode-line-in-empty-file
  3087. (interactive-p)
  3088. (= (point-min) (point-max)))
  3089. (insert "# -*- mode: org -*-\n\n"))
  3090. (unless org-inhibit-startup
  3091. (when org-startup-align-all-tables
  3092. (let ((bmp (buffer-modified-p)))
  3093. (org-table-map-tables 'org-table-align)
  3094. (set-buffer-modified-p bmp)))
  3095. (org-set-startup-visibility)))
  3096. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3097. (defun org-current-time ()
  3098. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3099. (if (> (car org-time-stamp-rounding-minutes) 1)
  3100. (let ((r (car org-time-stamp-rounding-minutes))
  3101. (time (decode-time)))
  3102. (apply 'encode-time
  3103. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3104. (nthcdr 2 time))))
  3105. (current-time)))
  3106. ;;;; Font-Lock stuff, including the activators
  3107. (defvar org-mouse-map (make-sparse-keymap))
  3108. (org-defkey org-mouse-map
  3109. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3110. (org-defkey org-mouse-map
  3111. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3112. (when org-mouse-1-follows-link
  3113. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3114. (when org-tab-follows-link
  3115. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3116. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3117. (when org-return-follows-link
  3118. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3119. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3120. (require 'font-lock)
  3121. (defconst org-non-link-chars "]\t\n\r<>")
  3122. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3123. "shell" "elisp"))
  3124. (defvar org-link-types-re nil
  3125. "Matches a link that has a url-like prefix like \"http:\"")
  3126. (defvar org-link-re-with-space nil
  3127. "Matches a link with spaces, optional angular brackets around it.")
  3128. (defvar org-link-re-with-space2 nil
  3129. "Matches a link with spaces, optional angular brackets around it.")
  3130. (defvar org-link-re-with-space3 nil
  3131. "Matches a link with spaces, only for internal part in bracket links.")
  3132. (defvar org-angle-link-re nil
  3133. "Matches link with angular brackets, spaces are allowed.")
  3134. (defvar org-plain-link-re nil
  3135. "Matches plain link, without spaces.")
  3136. (defvar org-bracket-link-regexp nil
  3137. "Matches a link in double brackets.")
  3138. (defvar org-bracket-link-analytic-regexp nil
  3139. "Regular expression used to analyze links.
  3140. Here is what the match groups contain after a match:
  3141. 1: http:
  3142. 2: http
  3143. 3: path
  3144. 4: [desc]
  3145. 5: desc")
  3146. (defvar org-bracket-link-analytic-regexp++ nil
  3147. "Like org-bracket-link-analytic-regexp, but include coderef internal type.")
  3148. (defvar org-any-link-re nil
  3149. "Regular expression matching any link.")
  3150. (defun org-make-link-regexps ()
  3151. "Update the link regular expressions.
  3152. This should be called after the variable `org-link-types' has changed."
  3153. (setq org-link-types-re
  3154. (concat
  3155. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3156. org-link-re-with-space
  3157. (concat
  3158. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3159. "\\([^" org-non-link-chars " ]"
  3160. "[^" org-non-link-chars "]*"
  3161. "[^" org-non-link-chars " ]\\)>?")
  3162. org-link-re-with-space2
  3163. (concat
  3164. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3165. "\\([^" org-non-link-chars " ]"
  3166. "[^\t\n\r]*"
  3167. "[^" org-non-link-chars " ]\\)>?")
  3168. org-link-re-with-space3
  3169. (concat
  3170. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3171. "\\([^" org-non-link-chars " ]"
  3172. "[^\t\n\r]*\\)")
  3173. org-angle-link-re
  3174. (concat
  3175. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3176. "\\([^" org-non-link-chars " ]"
  3177. "[^" org-non-link-chars "]*"
  3178. "\\)>")
  3179. org-plain-link-re
  3180. (concat
  3181. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3182. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3183. org-bracket-link-regexp
  3184. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3185. org-bracket-link-analytic-regexp
  3186. (concat
  3187. "\\[\\["
  3188. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3189. "\\([^]]+\\)"
  3190. "\\]"
  3191. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3192. "\\]")
  3193. org-bracket-link-analytic-regexp++
  3194. (concat
  3195. "\\[\\["
  3196. "\\(\\(" (mapconcat 'identity (cons "coderef" org-link-types) "\\|") "\\):\\)?"
  3197. "\\([^]]+\\)"
  3198. "\\]"
  3199. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3200. "\\]")
  3201. org-any-link-re
  3202. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3203. org-angle-link-re "\\)\\|\\("
  3204. org-plain-link-re "\\)")))
  3205. (org-make-link-regexps)
  3206. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3207. "Regular expression for fast time stamp matching.")
  3208. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3209. "Regular expression for fast time stamp matching.")
  3210. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3211. "Regular expression matching time strings for analysis.
  3212. This one does not require the space after the date, so it can be used
  3213. on a string that terminates immediately after the date.")
  3214. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3215. "Regular expression matching time strings for analysis.")
  3216. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3217. "Regular expression matching time stamps, with groups.")
  3218. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3219. "Regular expression matching time stamps (also [..]), with groups.")
  3220. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3221. "Regular expression matching a time stamp range.")
  3222. (defconst org-tr-regexp-both
  3223. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3224. "Regular expression matching a time stamp range.")
  3225. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3226. org-ts-regexp "\\)?")
  3227. "Regular expression matching a time stamp or time stamp range.")
  3228. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3229. org-ts-regexp-both "\\)?")
  3230. "Regular expression matching a time stamp or time stamp range.
  3231. The time stamps may be either active or inactive.")
  3232. (defvar org-emph-face nil)
  3233. (defun org-do-emphasis-faces (limit)
  3234. "Run through the buffer and add overlays to links."
  3235. (let (rtn)
  3236. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3237. (if (not (= (char-after (match-beginning 3))
  3238. (char-after (match-beginning 4))))
  3239. (progn
  3240. (setq rtn t)
  3241. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3242. 'face
  3243. (nth 1 (assoc (match-string 3)
  3244. org-emphasis-alist)))
  3245. (add-text-properties (match-beginning 2) (match-end 2)
  3246. '(font-lock-multiline t))
  3247. (when org-hide-emphasis-markers
  3248. (add-text-properties (match-end 4) (match-beginning 5)
  3249. '(invisible org-link))
  3250. (add-text-properties (match-beginning 3) (match-end 3)
  3251. '(invisible org-link)))))
  3252. (backward-char 1))
  3253. rtn))
  3254. (defun org-emphasize (&optional char)
  3255. "Insert or change an emphasis, i.e. a font like bold or italic.
  3256. If there is an active region, change that region to a new emphasis.
  3257. If there is no region, just insert the marker characters and position
  3258. the cursor between them.
  3259. CHAR should be either the marker character, or the first character of the
  3260. HTML tag associated with that emphasis. If CHAR is a space, the means
  3261. to remove the emphasis of the selected region.
  3262. If char is not given (for example in an interactive call) it
  3263. will be prompted for."
  3264. (interactive)
  3265. (let ((eal org-emphasis-alist) e det
  3266. (erc org-emphasis-regexp-components)
  3267. (prompt "")
  3268. (string "") beg end move tag c s)
  3269. (if (org-region-active-p)
  3270. (setq beg (region-beginning) end (region-end)
  3271. string (buffer-substring beg end))
  3272. (setq move t))
  3273. (while (setq e (pop eal))
  3274. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3275. c (aref tag 0))
  3276. (push (cons c (string-to-char (car e))) det)
  3277. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3278. (substring tag 1)))))
  3279. (setq det (nreverse det))
  3280. (unless char
  3281. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3282. (setq char (read-char-exclusive)))
  3283. (setq char (or (cdr (assoc char det)) char))
  3284. (if (equal char ?\ )
  3285. (setq s "" move nil)
  3286. (unless (assoc (char-to-string char) org-emphasis-alist)
  3287. (error "No such emphasis marker: \"%c\"" char))
  3288. (setq s (char-to-string char)))
  3289. (while (and (> (length string) 1)
  3290. (equal (substring string 0 1) (substring string -1))
  3291. (assoc (substring string 0 1) org-emphasis-alist))
  3292. (setq string (substring string 1 -1)))
  3293. (setq string (concat s string s))
  3294. (if beg (delete-region beg end))
  3295. (unless (or (bolp)
  3296. (string-match (concat "[" (nth 0 erc) "\n]")
  3297. (char-to-string (char-before (point)))))
  3298. (insert " "))
  3299. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3300. (char-to-string (char-after (point))))
  3301. (insert " ") (backward-char 1))
  3302. (insert string)
  3303. (and move (backward-char 1))))
  3304. (defconst org-nonsticky-props
  3305. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3306. (defun org-activate-plain-links (limit)
  3307. "Run through the buffer and add overlays to links."
  3308. (catch 'exit
  3309. (let (f)
  3310. (while (re-search-forward org-plain-link-re limit t)
  3311. (setq f (get-text-property (match-beginning 0) 'face))
  3312. (if (or (eq f 'org-tag)
  3313. (and (listp f) (memq 'org-tag f)))
  3314. nil
  3315. (add-text-properties (match-beginning 0) (match-end 0)
  3316. (list 'mouse-face 'highlight
  3317. 'rear-nonsticky org-nonsticky-props
  3318. 'keymap org-mouse-map
  3319. ))
  3320. (throw 'exit t))))))
  3321. (defun org-activate-code (limit)
  3322. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3323. (progn
  3324. (remove-text-properties (match-beginning 0) (match-end 0)
  3325. '(display t invisible t intangible t))
  3326. t)))
  3327. (defun org-activate-angle-links (limit)
  3328. "Run through the buffer and add overlays to links."
  3329. (if (re-search-forward org-angle-link-re limit t)
  3330. (progn
  3331. (add-text-properties (match-beginning 0) (match-end 0)
  3332. (list 'mouse-face 'highlight
  3333. 'rear-nonsticky org-nonsticky-props
  3334. 'keymap org-mouse-map
  3335. ))
  3336. t)))
  3337. (defun org-activate-footnote-links (limit)
  3338. "Run through the buffer and add overlays to links."
  3339. (if (re-search-forward "\\[\\([0-9]+\\]\\|fn:[^ \t\r\n:]+?[]:]\\)"
  3340. limit t)
  3341. (progn
  3342. (add-text-properties (match-beginning 0) (match-end 0)
  3343. (list 'mouse-face 'highlight
  3344. 'rear-nonsticky org-nonsticky-props
  3345. 'keymap org-mouse-map
  3346. 'help-echo
  3347. (if (= (point-at-bol) (match-beginning 0))
  3348. "Footnote definition"
  3349. "Footnote reference")
  3350. ))
  3351. t)))
  3352. (defun org-activate-bracket-links (limit)
  3353. "Run through the buffer and add overlays to bracketed links."
  3354. (if (re-search-forward org-bracket-link-regexp limit t)
  3355. (let* ((help (concat "LINK: "
  3356. (org-match-string-no-properties 1)))
  3357. ;; FIXME: above we should remove the escapes.
  3358. ;; but that requires another match, protecting match data,
  3359. ;; a lot of overhead for font-lock.
  3360. (ip (org-maybe-intangible
  3361. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3362. 'keymap org-mouse-map 'mouse-face 'highlight
  3363. 'font-lock-multiline t 'help-echo help)))
  3364. (vp (list 'rear-nonsticky org-nonsticky-props
  3365. 'keymap org-mouse-map 'mouse-face 'highlight
  3366. ' font-lock-multiline t 'help-echo help)))
  3367. ;; We need to remove the invisible property here. Table narrowing
  3368. ;; may have made some of this invisible.
  3369. (remove-text-properties (match-beginning 0) (match-end 0)
  3370. '(invisible nil))
  3371. (if (match-end 3)
  3372. (progn
  3373. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3374. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3375. (add-text-properties (match-end 3) (match-end 0) ip))
  3376. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3377. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3378. (add-text-properties (match-end 1) (match-end 0) ip))
  3379. t)))
  3380. (defun org-activate-dates (limit)
  3381. "Run through the buffer and add overlays to dates."
  3382. (if (re-search-forward org-tsr-regexp-both limit t)
  3383. (progn
  3384. (add-text-properties (match-beginning 0) (match-end 0)
  3385. (list 'mouse-face 'highlight
  3386. 'rear-nonsticky org-nonsticky-props
  3387. 'keymap org-mouse-map))
  3388. (when org-display-custom-times
  3389. (if (match-end 3)
  3390. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3391. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3392. t)))
  3393. (defvar org-target-link-regexp nil
  3394. "Regular expression matching radio targets in plain text.")
  3395. (make-variable-buffer-local 'org-target-link-regexp)
  3396. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3397. "Regular expression matching a link target.")
  3398. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3399. "Regular expression matching a radio target.")
  3400. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3401. "Regular expression matching any target.")
  3402. (defun org-activate-target-links (limit)
  3403. "Run through the buffer and add overlays to target matches."
  3404. (when org-target-link-regexp
  3405. (let ((case-fold-search t))
  3406. (if (re-search-forward org-target-link-regexp limit t)
  3407. (progn
  3408. (add-text-properties (match-beginning 0) (match-end 0)
  3409. (list 'mouse-face 'highlight
  3410. 'rear-nonsticky org-nonsticky-props
  3411. 'keymap org-mouse-map
  3412. 'help-echo "Radio target link"
  3413. 'org-linked-text t))
  3414. t)))))
  3415. (defun org-update-radio-target-regexp ()
  3416. "Find all radio targets in this file and update the regular expression."
  3417. (interactive)
  3418. (when (memq 'radio org-activate-links)
  3419. (setq org-target-link-regexp
  3420. (org-make-target-link-regexp (org-all-targets 'radio)))
  3421. (org-restart-font-lock)))
  3422. (defun org-hide-wide-columns (limit)
  3423. (let (s e)
  3424. (setq s (text-property-any (point) (or limit (point-max))
  3425. 'org-cwidth t))
  3426. (when s
  3427. (setq e (next-single-property-change s 'org-cwidth))
  3428. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3429. (goto-char e)
  3430. t)))
  3431. (defvar org-latex-and-specials-regexp nil
  3432. "Regular expression for highlighting export special stuff.")
  3433. (defvar org-match-substring-regexp)
  3434. (defvar org-match-substring-with-braces-regexp)
  3435. (defvar org-export-html-special-string-regexps)
  3436. (defun org-compute-latex-and-specials-regexp ()
  3437. "Compute regular expression for stuff treated specially by exporters."
  3438. (if (not org-highlight-latex-fragments-and-specials)
  3439. (org-set-local 'org-latex-and-specials-regexp nil)
  3440. (require 'org-exp)
  3441. (let*
  3442. ((matchers (plist-get org-format-latex-options :matchers))
  3443. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3444. org-latex-regexps)))
  3445. (options (org-combine-plists (org-default-export-plist)
  3446. (org-infile-export-plist)))
  3447. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3448. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3449. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3450. (org-export-html-expand (plist-get options :expand-quoted-html))
  3451. (org-export-with-special-strings (plist-get options :special-strings))
  3452. (re-sub
  3453. (cond
  3454. ((equal org-export-with-sub-superscripts '{})
  3455. (list org-match-substring-with-braces-regexp))
  3456. (org-export-with-sub-superscripts
  3457. (list org-match-substring-regexp))
  3458. (t nil)))
  3459. (re-latex
  3460. (if org-export-with-LaTeX-fragments
  3461. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3462. (re-macros
  3463. (if org-export-with-TeX-macros
  3464. (list (concat "\\\\"
  3465. (regexp-opt
  3466. (append (mapcar 'car org-html-entities)
  3467. (if (boundp 'org-latex-entities)
  3468. org-latex-entities nil))
  3469. 'words))) ; FIXME
  3470. ))
  3471. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3472. (re-special (if org-export-with-special-strings
  3473. (mapcar (lambda (x) (car x))
  3474. org-export-html-special-string-regexps)))
  3475. (re-rest
  3476. (delq nil
  3477. (list
  3478. (if org-export-html-expand "@<[^>\n]+>")
  3479. ))))
  3480. (org-set-local
  3481. 'org-latex-and-specials-regexp
  3482. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3483. re-rest) "\\|")))))
  3484. (defun org-do-latex-and-special-faces (limit)
  3485. "Run through the buffer and add overlays to links."
  3486. (when org-latex-and-specials-regexp
  3487. (let (rtn d)
  3488. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3489. limit t))
  3490. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3491. 'face))
  3492. '(org-code org-verbatim underline)))
  3493. (progn
  3494. (setq rtn t
  3495. d (cond ((member (char-after (1+ (match-beginning 0)))
  3496. '(?_ ?^)) 1)
  3497. (t 0)))
  3498. (font-lock-prepend-text-property
  3499. (+ d (match-beginning 0)) (match-end 0)
  3500. 'face 'org-latex-and-export-specials)
  3501. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3502. '(font-lock-multiline t)))))
  3503. rtn)))
  3504. (defun org-restart-font-lock ()
  3505. "Restart font-lock-mode, to force refontification."
  3506. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3507. (font-lock-mode -1)
  3508. (font-lock-mode 1)))
  3509. (defun org-all-targets (&optional radio)
  3510. "Return a list of all targets in this file.
  3511. With optional argument RADIO, only find radio targets."
  3512. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3513. rtn)
  3514. (save-excursion
  3515. (goto-char (point-min))
  3516. (while (re-search-forward re nil t)
  3517. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3518. rtn)))
  3519. (defun org-make-target-link-regexp (targets)
  3520. "Make regular expression matching all strings in TARGETS.
  3521. The regular expression finds the targets also if there is a line break
  3522. between words."
  3523. (and targets
  3524. (concat
  3525. "\\<\\("
  3526. (mapconcat
  3527. (lambda (x)
  3528. (while (string-match " +" x)
  3529. (setq x (replace-match "\\s-+" t t x)))
  3530. x)
  3531. targets
  3532. "\\|")
  3533. "\\)\\>")))
  3534. (defun org-activate-tags (limit)
  3535. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3536. (progn
  3537. (add-text-properties (match-beginning 1) (match-end 1)
  3538. (list 'mouse-face 'highlight
  3539. 'rear-nonsticky org-nonsticky-props
  3540. 'keymap org-mouse-map))
  3541. t)))
  3542. (defun org-outline-level ()
  3543. (save-excursion
  3544. (looking-at outline-regexp)
  3545. (if (match-beginning 1)
  3546. (+ (org-get-string-indentation (match-string 1)) 1000)
  3547. (1- (- (match-end 0) (match-beginning 0))))))
  3548. (defvar org-font-lock-keywords nil)
  3549. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3550. "Regular expression matching a property line.")
  3551. (defvar org-font-lock-hook nil
  3552. "Functions to be called for special font lock stuff.")
  3553. (defun org-font-lock-hook (limit)
  3554. (run-hook-with-args 'org-font-lock-hook limit))
  3555. (defun org-set-font-lock-defaults ()
  3556. (let* ((em org-fontify-emphasized-text)
  3557. (lk org-activate-links)
  3558. (org-font-lock-extra-keywords
  3559. (list
  3560. ;; Call the hook
  3561. '(org-font-lock-hook)
  3562. ;; Headlines
  3563. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3564. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3565. ;; Table lines
  3566. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3567. (1 'org-table t))
  3568. ;; Table internals
  3569. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3570. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3571. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3572. ;; Drawers
  3573. (list org-drawer-regexp '(0 'org-special-keyword t))
  3574. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3575. ;; Properties
  3576. (list org-property-re
  3577. '(1 'org-special-keyword t)
  3578. '(3 'org-property-value t))
  3579. (if org-format-transports-properties-p
  3580. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3581. ;; Links
  3582. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3583. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3584. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3585. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3586. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3587. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3588. (if (memq 'footnote lk) '(org-activate-footnote-links
  3589. (0 'org-footnote t)))
  3590. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3591. '(org-hide-wide-columns (0 nil append))
  3592. ;; TODO lines
  3593. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3594. '(1 (org-get-todo-face 1) t))
  3595. ;; DONE
  3596. (if org-fontify-done-headline
  3597. (list (concat "^[*]+ +\\<\\("
  3598. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3599. "\\)\\(.*\\)")
  3600. '(2 'org-headline-done t))
  3601. nil)
  3602. ;; Priorities
  3603. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3604. ;; Tags
  3605. '(org-font-lock-add-tag-faces)
  3606. ;; Special keywords
  3607. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3608. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3609. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3610. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3611. ;; Emphasis
  3612. (if em
  3613. (if (featurep 'xemacs)
  3614. '(org-do-emphasis-faces (0 nil append))
  3615. '(org-do-emphasis-faces)))
  3616. ;; Checkboxes
  3617. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3618. 2 'bold prepend)
  3619. (if org-provide-checkbox-statistics
  3620. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3621. (0 (org-get-checkbox-statistics-face) t)))
  3622. ;; Description list items
  3623. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3624. 2 'bold prepend)
  3625. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3626. '(1 'org-archived prepend))
  3627. ;; Specials
  3628. '(org-do-latex-and-special-faces)
  3629. ;; Code
  3630. '(org-activate-code (1 'org-code t))
  3631. ;; COMMENT
  3632. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3633. "\\|" org-quote-string "\\)\\>")
  3634. '(1 'org-special-keyword t))
  3635. '("^#.*" (0 'font-lock-comment-face t))
  3636. )))
  3637. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3638. ;; Now set the full font-lock-keywords
  3639. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3640. (org-set-local 'font-lock-defaults
  3641. '(org-font-lock-keywords t nil nil backward-paragraph))
  3642. (kill-local-variable 'font-lock-keywords) nil))
  3643. (defvar org-m nil)
  3644. (defvar org-l nil)
  3645. (defvar org-f nil)
  3646. (defun org-get-level-face (n)
  3647. "Get the right face for match N in font-lock matching of headlines."
  3648. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3649. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3650. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3651. (cond
  3652. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3653. ((eq n 2) org-f)
  3654. (t (if org-level-color-stars-only nil org-f))))
  3655. (defun org-get-todo-face (kwd)
  3656. "Get the right face for a TODO keyword KWD.
  3657. If KWD is a number, get the corresponding match group."
  3658. (if (numberp kwd) (setq kwd (match-string kwd)))
  3659. (or (cdr (assoc kwd org-todo-keyword-faces))
  3660. (and (member kwd org-done-keywords) 'org-done)
  3661. 'org-todo))
  3662. (defun org-font-lock-add-tag-faces (limit)
  3663. "Add the special tag faces."
  3664. (when (and org-tag-faces org-tags-special-faces-re)
  3665. (while (re-search-forward org-tags-special-faces-re limit t)
  3666. (add-text-properties (match-beginning 1) (match-end 1)
  3667. (list 'face (org-get-tag-face 1)
  3668. 'font-lock-fontified t))
  3669. (backward-char 1))))
  3670. (defun org-get-tag-face (kwd)
  3671. "Get the right face for a TODO keyword KWD.
  3672. If KWD is a number, get the corresponding match group."
  3673. (if (numberp kwd) (setq kwd (match-string kwd)))
  3674. (or (cdr (assoc kwd org-tag-faces))
  3675. 'org-tag))
  3676. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3677. "Remove fontification and activation overlays from links."
  3678. (font-lock-default-unfontify-region beg end)
  3679. (let* ((buffer-undo-list t)
  3680. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3681. (inhibit-modification-hooks t)
  3682. deactivate-mark buffer-file-name buffer-file-truename)
  3683. (remove-text-properties beg end
  3684. '(mouse-face t keymap t org-linked-text t
  3685. invisible t intangible t))))
  3686. ;;;; Visibility cycling, including org-goto and indirect buffer
  3687. ;;; Cycling
  3688. (defvar org-cycle-global-status nil)
  3689. (make-variable-buffer-local 'org-cycle-global-status)
  3690. (defvar org-cycle-subtree-status nil)
  3691. (make-variable-buffer-local 'org-cycle-subtree-status)
  3692. ;;;###autoload
  3693. (defun org-cycle (&optional arg)
  3694. "Visibility cycling for Org-mode.
  3695. - When this function is called with a prefix argument, rotate the entire
  3696. buffer through 3 states (global cycling)
  3697. 1. OVERVIEW: Show only top-level headlines.
  3698. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3699. 3. SHOW ALL: Show everything.
  3700. When called with two C-u C-u prefixes, switch to the startup visibility,
  3701. determined by the variable `org-startup-folded', and by any VISIBILITY
  3702. properties in the buffer.
  3703. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3704. including drawers.
  3705. - When point is at the beginning of a headline, rotate the subtree started
  3706. by this line through 3 different states (local cycling)
  3707. 1. FOLDED: Only the main headline is shown.
  3708. 2. CHILDREN: The main headline and the direct children are shown.
  3709. From this state, you can move to one of the children
  3710. and zoom in further.
  3711. 3. SUBTREE: Show the entire subtree, including body text.
  3712. - When there is a numeric prefix, go up to a heading with level ARG, do
  3713. a `show-subtree' and return to the previous cursor position. If ARG
  3714. is negative, go up that many levels.
  3715. - When point is not at the beginning of a headline, execute the global
  3716. binding for TAB, which is re-indenting the line. See the option
  3717. `org-cycle-emulate-tab' for details.
  3718. - Special case: if point is at the beginning of the buffer and there is
  3719. no headline in line 1, this function will act as if called with prefix arg.
  3720. But only if also the variable `org-cycle-global-at-bob' is t."
  3721. (interactive "P")
  3722. (org-load-modules-maybe)
  3723. (let* ((outline-regexp
  3724. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3725. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3726. outline-regexp))
  3727. (bob-special (and org-cycle-global-at-bob (bobp)
  3728. (not (looking-at outline-regexp))))
  3729. (org-cycle-hook
  3730. (if bob-special
  3731. (delq 'org-optimize-window-after-visibility-change
  3732. (copy-sequence org-cycle-hook))
  3733. org-cycle-hook))
  3734. (pos (point)))
  3735. (if (or bob-special (equal arg '(4)))
  3736. ;; special case: use global cycling
  3737. (setq arg t))
  3738. (cond
  3739. ((equal arg '(16))
  3740. (org-set-startup-visibility)
  3741. (message "Startup visibility, plus VISIBILITY properties"))
  3742. ((equal arg '(64))
  3743. (show-all)
  3744. (message "Entire buffer visible, including drawers"))
  3745. ((org-at-table-p 'any)
  3746. ;; Enter the table or move to the next field in the table
  3747. (or (org-table-recognize-table.el)
  3748. (progn
  3749. (if arg (org-table-edit-field t)
  3750. (org-table-justify-field-maybe)
  3751. (call-interactively 'org-table-next-field)))))
  3752. ((eq arg t) ;; Global cycling
  3753. (cond
  3754. ((and (eq last-command this-command)
  3755. (eq org-cycle-global-status 'overview))
  3756. ;; We just created the overview - now do table of contents
  3757. ;; This can be slow in very large buffers, so indicate action
  3758. (message "CONTENTS...")
  3759. (org-content)
  3760. (message "CONTENTS...done")
  3761. (setq org-cycle-global-status 'contents)
  3762. (run-hook-with-args 'org-cycle-hook 'contents))
  3763. ((and (eq last-command this-command)
  3764. (eq org-cycle-global-status 'contents))
  3765. ;; We just showed the table of contents - now show everything
  3766. (show-all)
  3767. (message "SHOW ALL")
  3768. (setq org-cycle-global-status 'all)
  3769. (run-hook-with-args 'org-cycle-hook 'all))
  3770. (t
  3771. ;; Default action: go to overview
  3772. (org-overview)
  3773. (message "OVERVIEW")
  3774. (setq org-cycle-global-status 'overview)
  3775. (run-hook-with-args 'org-cycle-hook 'overview))))
  3776. ((and org-drawers org-drawer-regexp
  3777. (save-excursion
  3778. (beginning-of-line 1)
  3779. (looking-at org-drawer-regexp)))
  3780. ;; Toggle block visibility
  3781. (org-flag-drawer
  3782. (not (get-char-property (match-end 0) 'invisible))))
  3783. ((integerp arg)
  3784. ;; Show-subtree, ARG levels up from here.
  3785. (save-excursion
  3786. (org-back-to-heading)
  3787. (outline-up-heading (if (< arg 0) (- arg)
  3788. (- (funcall outline-level) arg)))
  3789. (org-show-subtree)))
  3790. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3791. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3792. ;; At a heading: rotate between three different views
  3793. (org-back-to-heading)
  3794. (let ((goal-column 0) eoh eol eos)
  3795. ;; First, some boundaries
  3796. (save-excursion
  3797. (org-back-to-heading)
  3798. (save-excursion
  3799. (beginning-of-line 2)
  3800. (while (and (not (eobp)) ;; this is like `next-line'
  3801. (get-char-property (1- (point)) 'invisible))
  3802. (beginning-of-line 2)) (setq eol (point)))
  3803. (outline-end-of-heading) (setq eoh (point))
  3804. (org-end-of-subtree t)
  3805. (unless (eobp)
  3806. (skip-chars-forward " \t\n")
  3807. (beginning-of-line 1) ; in case this is an item
  3808. )
  3809. (setq eos (1- (point))))
  3810. ;; Find out what to do next and set `this-command'
  3811. (cond
  3812. ((= eos eoh)
  3813. ;; Nothing is hidden behind this heading
  3814. (message "EMPTY ENTRY")
  3815. (setq org-cycle-subtree-status nil)
  3816. (save-excursion
  3817. (goto-char eos)
  3818. (outline-next-heading)
  3819. (if (org-invisible-p) (org-flag-heading nil))))
  3820. ((or (>= eol eos)
  3821. (not (string-match "\\S-" (buffer-substring eol eos))))
  3822. ;; Entire subtree is hidden in one line: open it
  3823. (org-show-entry)
  3824. (show-children)
  3825. (message "CHILDREN")
  3826. (save-excursion
  3827. (goto-char eos)
  3828. (outline-next-heading)
  3829. (if (org-invisible-p) (org-flag-heading nil)))
  3830. (setq org-cycle-subtree-status 'children)
  3831. (run-hook-with-args 'org-cycle-hook 'children))
  3832. ((and (eq last-command this-command)
  3833. (eq org-cycle-subtree-status 'children))
  3834. ;; We just showed the children, now show everything.
  3835. (org-show-subtree)
  3836. (message "SUBTREE")
  3837. (setq org-cycle-subtree-status 'subtree)
  3838. (run-hook-with-args 'org-cycle-hook 'subtree))
  3839. (t
  3840. ;; Default action: hide the subtree.
  3841. (hide-subtree)
  3842. (message "FOLDED")
  3843. (setq org-cycle-subtree-status 'folded)
  3844. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3845. ;; TAB emulation and template completion
  3846. (buffer-read-only (org-back-to-heading))
  3847. ((org-try-structure-completion))
  3848. ((org-try-cdlatex-tab))
  3849. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3850. (or (not (bolp))
  3851. (not (looking-at outline-regexp))))
  3852. (call-interactively (global-key-binding "\t")))
  3853. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3854. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3855. (or (and (eq org-cycle-emulate-tab 'white)
  3856. (= (match-end 0) (point-at-eol)))
  3857. (and (eq org-cycle-emulate-tab 'whitestart)
  3858. (>= (match-end 0) pos))))
  3859. t
  3860. (eq org-cycle-emulate-tab t))
  3861. (call-interactively (global-key-binding "\t")))
  3862. (t (save-excursion
  3863. (org-back-to-heading)
  3864. (org-cycle))))))
  3865. ;;;###autoload
  3866. (defun org-global-cycle (&optional arg)
  3867. "Cycle the global visibility. For details see `org-cycle'.
  3868. With C-u prefix arg, switch to startup visibility.
  3869. With a numeric prefix, show all headlines up to that level."
  3870. (interactive "P")
  3871. (let ((org-cycle-include-plain-lists
  3872. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3873. (cond
  3874. ((integerp arg)
  3875. (show-all)
  3876. (hide-sublevels arg)
  3877. (setq org-cycle-global-status 'contents))
  3878. ((equal arg '(4))
  3879. (org-set-startup-visibility)
  3880. (message "Startup visibility, plus VISIBILITY properties."))
  3881. (t
  3882. (org-cycle '(4))))))
  3883. (defun org-set-startup-visibility ()
  3884. "Set the visibility required by startup options and properties."
  3885. (cond
  3886. ((eq org-startup-folded t)
  3887. (org-cycle '(4)))
  3888. ((eq org-startup-folded 'content)
  3889. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3890. (org-cycle '(4)) (org-cycle '(4)))))
  3891. (org-set-visibility-according-to-property 'no-cleanup)
  3892. (org-cycle-hide-archived-subtrees 'all)
  3893. (org-cycle-hide-drawers 'all)
  3894. (org-cycle-show-empty-lines 'all))
  3895. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3896. "Switch subtree visibilities according to :VISIBILITY: property."
  3897. (interactive)
  3898. (let (state)
  3899. (save-excursion
  3900. (goto-char (point-min))
  3901. (while (re-search-forward
  3902. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3903. nil t)
  3904. (setq state (match-string 1))
  3905. (save-excursion
  3906. (org-back-to-heading t)
  3907. (hide-subtree)
  3908. (org-reveal)
  3909. (cond
  3910. ((equal state '("fold" "folded"))
  3911. (hide-subtree))
  3912. ((equal state "children")
  3913. (org-show-hidden-entry)
  3914. (show-children))
  3915. ((equal state "content")
  3916. (save-excursion
  3917. (save-restriction
  3918. (org-narrow-to-subtree)
  3919. (org-content))))
  3920. ((member state '("all" "showall"))
  3921. (show-subtree)))))
  3922. (unless no-cleanup
  3923. (org-cycle-hide-archived-subtrees 'all)
  3924. (org-cycle-hide-drawers 'all)
  3925. (org-cycle-show-empty-lines 'all)))))
  3926. (defun org-overview ()
  3927. "Switch to overview mode, showing only top-level headlines.
  3928. Really, this shows all headlines with level equal or greater than the level
  3929. of the first headline in the buffer. This is important, because if the
  3930. first headline is not level one, then (hide-sublevels 1) gives confusing
  3931. results."
  3932. (interactive)
  3933. (let ((level (save-excursion
  3934. (goto-char (point-min))
  3935. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3936. (progn
  3937. (goto-char (match-beginning 0))
  3938. (funcall outline-level))))))
  3939. (and level (hide-sublevels level))))
  3940. (defun org-content (&optional arg)
  3941. "Show all headlines in the buffer, like a table of contents.
  3942. With numerical argument N, show content up to level N."
  3943. (interactive "P")
  3944. (save-excursion
  3945. ;; Visit all headings and show their offspring
  3946. (and (integerp arg) (org-overview))
  3947. (goto-char (point-max))
  3948. (catch 'exit
  3949. (while (and (progn (condition-case nil
  3950. (outline-previous-visible-heading 1)
  3951. (error (goto-char (point-min))))
  3952. t)
  3953. (looking-at outline-regexp))
  3954. (if (integerp arg)
  3955. (show-children (1- arg))
  3956. (show-branches))
  3957. (if (bobp) (throw 'exit nil))))))
  3958. (defun org-optimize-window-after-visibility-change (state)
  3959. "Adjust the window after a change in outline visibility.
  3960. This function is the default value of the hook `org-cycle-hook'."
  3961. (when (get-buffer-window (current-buffer))
  3962. (cond
  3963. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3964. ; ((eq state 'overview) (org-beginning-of-line))
  3965. ((eq state 'content) nil)
  3966. ((eq state 'all) nil)
  3967. ((eq state 'folded) nil)
  3968. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3969. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3970. (defun org-compact-display-after-subtree-move ()
  3971. (let (beg end)
  3972. (save-excursion
  3973. (if (org-up-heading-safe)
  3974. (progn
  3975. (hide-subtree)
  3976. (show-entry)
  3977. (show-children)
  3978. (org-cycle-show-empty-lines 'children)
  3979. (org-cycle-hide-drawers 'children))
  3980. (org-overview)))))
  3981. (defun org-cycle-show-empty-lines (state)
  3982. "Show empty lines above all visible headlines.
  3983. The region to be covered depends on STATE when called through
  3984. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3985. entire buffer covered. Note that an empty line is only shown if there
  3986. are at least `org-cycle-separator-lines' empty lines before the headline."
  3987. (when (> org-cycle-separator-lines 0)
  3988. (save-excursion
  3989. (let* ((n org-cycle-separator-lines)
  3990. (re (cond
  3991. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3992. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3993. (t (let ((ns (number-to-string (- n 2))))
  3994. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3995. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3996. beg end)
  3997. (cond
  3998. ((memq state '(overview contents t))
  3999. (setq beg (point-min) end (point-max)))
  4000. ((memq state '(children folded))
  4001. (setq beg (point) end (progn (org-end-of-subtree t t)
  4002. (beginning-of-line 2)
  4003. (point)))))
  4004. (when beg
  4005. (goto-char beg)
  4006. (while (re-search-forward re end t)
  4007. (if (not (get-char-property (match-end 1) 'invisible))
  4008. (outline-flag-region
  4009. (match-beginning 1) (match-end 1) nil)))))))
  4010. ;; Never hide empty lines at the end of the file.
  4011. (save-excursion
  4012. (goto-char (point-max))
  4013. (outline-previous-heading)
  4014. (outline-end-of-heading)
  4015. (if (and (looking-at "[ \t\n]+")
  4016. (= (match-end 0) (point-max)))
  4017. (outline-flag-region (point) (match-end 0) nil))))
  4018. (defun org-show-empty-lines-in-parent ()
  4019. "Move to the parent and re-show empty lines before visible headlines."
  4020. (save-excursion
  4021. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  4022. (org-cycle-show-empty-lines context))))
  4023. (defun org-cycle-hide-drawers (state)
  4024. "Re-hide all drawers after a visibility state change."
  4025. (when (and (org-mode-p)
  4026. (not (memq state '(overview folded))))
  4027. (save-excursion
  4028. (let* ((globalp (memq state '(contents all)))
  4029. (beg (if globalp (point-min) (point)))
  4030. (end (if globalp (point-max) (org-end-of-subtree t))))
  4031. (goto-char beg)
  4032. (while (re-search-forward org-drawer-regexp end t)
  4033. (org-flag-drawer t))))))
  4034. (defun org-flag-drawer (flag)
  4035. (save-excursion
  4036. (beginning-of-line 1)
  4037. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  4038. (let ((b (match-end 0))
  4039. (outline-regexp org-outline-regexp))
  4040. (if (re-search-forward
  4041. "^[ \t]*:END:"
  4042. (save-excursion (outline-next-heading) (point)) t)
  4043. (outline-flag-region b (point-at-eol) flag)
  4044. (error ":END: line missing"))))))
  4045. (defun org-subtree-end-visible-p ()
  4046. "Is the end of the current subtree visible?"
  4047. (pos-visible-in-window-p
  4048. (save-excursion (org-end-of-subtree t) (point))))
  4049. (defun org-first-headline-recenter (&optional N)
  4050. "Move cursor to the first headline and recenter the headline.
  4051. Optional argument N means, put the headline into the Nth line of the window."
  4052. (goto-char (point-min))
  4053. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4054. (beginning-of-line)
  4055. (recenter (prefix-numeric-value N))))
  4056. ;;; Org-goto
  4057. (defvar org-goto-window-configuration nil)
  4058. (defvar org-goto-marker nil)
  4059. (defvar org-goto-map
  4060. (let ((map (make-sparse-keymap)))
  4061. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4062. (while (setq cmd (pop cmds))
  4063. (substitute-key-definition cmd cmd map global-map)))
  4064. (suppress-keymap map)
  4065. (org-defkey map "\C-m" 'org-goto-ret)
  4066. (org-defkey map [(return)] 'org-goto-ret)
  4067. (org-defkey map [(left)] 'org-goto-left)
  4068. (org-defkey map [(right)] 'org-goto-right)
  4069. (org-defkey map [(control ?g)] 'org-goto-quit)
  4070. (org-defkey map "\C-i" 'org-cycle)
  4071. (org-defkey map [(tab)] 'org-cycle)
  4072. (org-defkey map [(down)] 'outline-next-visible-heading)
  4073. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4074. (if org-goto-auto-isearch
  4075. (if (fboundp 'define-key-after)
  4076. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4077. nil)
  4078. (org-defkey map "q" 'org-goto-quit)
  4079. (org-defkey map "n" 'outline-next-visible-heading)
  4080. (org-defkey map "p" 'outline-previous-visible-heading)
  4081. (org-defkey map "f" 'outline-forward-same-level)
  4082. (org-defkey map "b" 'outline-backward-same-level)
  4083. (org-defkey map "u" 'outline-up-heading))
  4084. (org-defkey map "/" 'org-occur)
  4085. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4086. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4087. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4088. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4089. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4090. map))
  4091. (defconst org-goto-help
  4092. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4093. RET=jump to location [Q]uit and return to previous location
  4094. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4095. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4096. ;; FIXME: Docstring doe not mention both interfaces
  4097. (defun org-goto (&optional alternative-interface)
  4098. "Look up a different location in the current file, keeping current visibility.
  4099. When you want look-up or go to a different location in a document, the
  4100. fastest way is often to fold the entire buffer and then dive into the tree.
  4101. This method has the disadvantage, that the previous location will be folded,
  4102. which may not be what you want.
  4103. This command works around this by showing a copy of the current buffer
  4104. in an indirect buffer, in overview mode. You can dive into the tree in
  4105. that copy, use org-occur and incremental search to find a location.
  4106. When pressing RET or `Q', the command returns to the original buffer in
  4107. which the visibility is still unchanged. After RET is will also jump to
  4108. the location selected in the indirect buffer and expose the
  4109. the headline hierarchy above."
  4110. (interactive "P")
  4111. (let* ((org-refile-targets `((nil . (:maxlevel . ,org-goto-max-level))))
  4112. (org-refile-use-outline-path t)
  4113. (interface
  4114. (if (not alternative-interface)
  4115. org-goto-interface
  4116. (if (eq org-goto-interface 'outline)
  4117. 'outline-path-completion
  4118. 'outline)))
  4119. (org-goto-start-pos (point))
  4120. (selected-point
  4121. (if (eq interface 'outline)
  4122. (car (org-get-location (current-buffer) org-goto-help))
  4123. (nth 3 (org-refile-get-location "Goto: ")))))
  4124. (if selected-point
  4125. (progn
  4126. (org-mark-ring-push org-goto-start-pos)
  4127. (goto-char selected-point)
  4128. (if (or (org-invisible-p) (org-invisible-p2))
  4129. (org-show-context 'org-goto)))
  4130. (message "Quit"))))
  4131. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4132. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4133. (defvar org-goto-local-auto-isearch-map) ; defined below
  4134. (defun org-get-location (buf help)
  4135. "Let the user select a location in the Org-mode buffer BUF.
  4136. This function uses a recursive edit. It returns the selected position
  4137. or nil."
  4138. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4139. (isearch-hide-immediately nil)
  4140. (isearch-search-fun-function
  4141. (lambda () 'org-goto-local-search-headings))
  4142. (org-goto-selected-point org-goto-exit-command))
  4143. (save-excursion
  4144. (save-window-excursion
  4145. (delete-other-windows)
  4146. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4147. (switch-to-buffer
  4148. (condition-case nil
  4149. (make-indirect-buffer (current-buffer) "*org-goto*")
  4150. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4151. (with-output-to-temp-buffer "*Help*"
  4152. (princ help))
  4153. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4154. (setq buffer-read-only nil)
  4155. (let ((org-startup-truncated t)
  4156. (org-startup-folded nil)
  4157. (org-startup-align-all-tables nil))
  4158. (org-mode)
  4159. (org-overview))
  4160. (setq buffer-read-only t)
  4161. (if (and (boundp 'org-goto-start-pos)
  4162. (integer-or-marker-p org-goto-start-pos))
  4163. (let ((org-show-hierarchy-above t)
  4164. (org-show-siblings t)
  4165. (org-show-following-heading t))
  4166. (goto-char org-goto-start-pos)
  4167. (and (org-invisible-p) (org-show-context)))
  4168. (goto-char (point-min)))
  4169. (org-beginning-of-line)
  4170. (message "Select location and press RET")
  4171. (use-local-map org-goto-map)
  4172. (recursive-edit)
  4173. ))
  4174. (kill-buffer "*org-goto*")
  4175. (cons org-goto-selected-point org-goto-exit-command)))
  4176. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4177. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4178. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4179. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4180. (defun org-goto-local-search-headings (string bound noerror)
  4181. "Search and make sure that any matches are in headlines."
  4182. (catch 'return
  4183. (while (if isearch-forward
  4184. (search-forward string bound noerror)
  4185. (search-backward string bound noerror))
  4186. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4187. (and (member :headline context)
  4188. (not (member :tags context))))
  4189. (throw 'return (point))))))
  4190. (defun org-goto-local-auto-isearch ()
  4191. "Start isearch."
  4192. (interactive)
  4193. (goto-char (point-min))
  4194. (let ((keys (this-command-keys)))
  4195. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4196. (isearch-mode t)
  4197. (isearch-process-search-char (string-to-char keys)))))
  4198. (defun org-goto-ret (&optional arg)
  4199. "Finish `org-goto' by going to the new location."
  4200. (interactive "P")
  4201. (setq org-goto-selected-point (point)
  4202. org-goto-exit-command 'return)
  4203. (throw 'exit nil))
  4204. (defun org-goto-left ()
  4205. "Finish `org-goto' by going to the new location."
  4206. (interactive)
  4207. (if (org-on-heading-p)
  4208. (progn
  4209. (beginning-of-line 1)
  4210. (setq org-goto-selected-point (point)
  4211. org-goto-exit-command 'left)
  4212. (throw 'exit nil))
  4213. (error "Not on a heading")))
  4214. (defun org-goto-right ()
  4215. "Finish `org-goto' by going to the new location."
  4216. (interactive)
  4217. (if (org-on-heading-p)
  4218. (progn
  4219. (setq org-goto-selected-point (point)
  4220. org-goto-exit-command 'right)
  4221. (throw 'exit nil))
  4222. (error "Not on a heading")))
  4223. (defun org-goto-quit ()
  4224. "Finish `org-goto' without cursor motion."
  4225. (interactive)
  4226. (setq org-goto-selected-point nil)
  4227. (setq org-goto-exit-command 'quit)
  4228. (throw 'exit nil))
  4229. ;;; Indirect buffer display of subtrees
  4230. (defvar org-indirect-dedicated-frame nil
  4231. "This is the frame being used for indirect tree display.")
  4232. (defvar org-last-indirect-buffer nil)
  4233. (defun org-tree-to-indirect-buffer (&optional arg)
  4234. "Create indirect buffer and narrow it to current subtree.
  4235. With numerical prefix ARG, go up to this level and then take that tree.
  4236. If ARG is negative, go up that many levels.
  4237. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4238. indirect buffer previously made with this command, to avoid proliferation of
  4239. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4240. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4241. is kept so that you can work with several indirect buffers at the same time.
  4242. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4243. requests that a new frame be made for the new buffer, so that the dedicated
  4244. frame is not changed."
  4245. (interactive "P")
  4246. (let ((cbuf (current-buffer))
  4247. (cwin (selected-window))
  4248. (pos (point))
  4249. beg end level heading ibuf)
  4250. (save-excursion
  4251. (org-back-to-heading t)
  4252. (when (numberp arg)
  4253. (setq level (org-outline-level))
  4254. (if (< arg 0) (setq arg (+ level arg)))
  4255. (while (> (setq level (org-outline-level)) arg)
  4256. (outline-up-heading 1 t)))
  4257. (setq beg (point)
  4258. heading (org-get-heading))
  4259. (org-end-of-subtree t) (setq end (point)))
  4260. (if (and (buffer-live-p org-last-indirect-buffer)
  4261. (not (eq org-indirect-buffer-display 'new-frame))
  4262. (not arg))
  4263. (kill-buffer org-last-indirect-buffer))
  4264. (setq ibuf (org-get-indirect-buffer cbuf)
  4265. org-last-indirect-buffer ibuf)
  4266. (cond
  4267. ((or (eq org-indirect-buffer-display 'new-frame)
  4268. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4269. (select-frame (make-frame))
  4270. (delete-other-windows)
  4271. (switch-to-buffer ibuf)
  4272. (org-set-frame-title heading))
  4273. ((eq org-indirect-buffer-display 'dedicated-frame)
  4274. (raise-frame
  4275. (select-frame (or (and org-indirect-dedicated-frame
  4276. (frame-live-p org-indirect-dedicated-frame)
  4277. org-indirect-dedicated-frame)
  4278. (setq org-indirect-dedicated-frame (make-frame)))))
  4279. (delete-other-windows)
  4280. (switch-to-buffer ibuf)
  4281. (org-set-frame-title (concat "Indirect: " heading)))
  4282. ((eq org-indirect-buffer-display 'current-window)
  4283. (switch-to-buffer ibuf))
  4284. ((eq org-indirect-buffer-display 'other-window)
  4285. (pop-to-buffer ibuf))
  4286. (t (error "Invalid value.")))
  4287. (if (featurep 'xemacs)
  4288. (save-excursion (org-mode) (turn-on-font-lock)))
  4289. (narrow-to-region beg end)
  4290. (show-all)
  4291. (goto-char pos)
  4292. (and (window-live-p cwin) (select-window cwin))))
  4293. (defun org-get-indirect-buffer (&optional buffer)
  4294. (setq buffer (or buffer (current-buffer)))
  4295. (let ((n 1) (base (buffer-name buffer)) bname)
  4296. (while (buffer-live-p
  4297. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4298. (setq n (1+ n)))
  4299. (condition-case nil
  4300. (make-indirect-buffer buffer bname 'clone)
  4301. (error (make-indirect-buffer buffer bname)))))
  4302. (defun org-set-frame-title (title)
  4303. "Set the title of the current frame to the string TITLE."
  4304. ;; FIXME: how to name a single frame in XEmacs???
  4305. (unless (featurep 'xemacs)
  4306. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4307. ;;;; Structure editing
  4308. ;;; Inserting headlines
  4309. (defun org-insert-heading (&optional force-heading)
  4310. "Insert a new heading or item with same depth at point.
  4311. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4312. If point is at the beginning of a headline, insert a sibling before the
  4313. current headline. If point is not at the beginning, do not split the line,
  4314. but create the new headline after the current line."
  4315. (interactive "P")
  4316. (if (= (buffer-size) 0)
  4317. (insert "\n* ")
  4318. (when (or force-heading (not (org-insert-item)))
  4319. (let* ((head (save-excursion
  4320. (condition-case nil
  4321. (progn
  4322. (org-back-to-heading)
  4323. (match-string 0))
  4324. (error "*"))))
  4325. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4326. pos hide-previous previous-pos)
  4327. (cond
  4328. ((and (org-on-heading-p) (bolp)
  4329. (or (bobp)
  4330. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4331. ;; insert before the current line
  4332. (open-line (if blank 2 1)))
  4333. ((and (bolp)
  4334. (or (bobp)
  4335. (save-excursion
  4336. (backward-char 1) (not (org-invisible-p)))))
  4337. ;; insert right here
  4338. nil)
  4339. (t
  4340. ;; somewhere in the line
  4341. (save-excursion
  4342. (setq previous-pos (point-at-bol))
  4343. (end-of-line)
  4344. (setq hide-previous (org-invisible-p)))
  4345. (and org-insert-heading-respect-content (org-show-subtree))
  4346. (let ((split
  4347. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4348. (save-excursion
  4349. (let ((p (point)))
  4350. (goto-char (point-at-bol))
  4351. (and (looking-at org-complex-heading-regexp)
  4352. (> p (match-beginning 4)))))))
  4353. tags pos)
  4354. (cond
  4355. (org-insert-heading-respect-content
  4356. (org-end-of-subtree nil t)
  4357. (or (bolp) (newline))
  4358. (open-line 1))
  4359. ((org-on-heading-p)
  4360. (when hide-previous
  4361. (show-children)
  4362. (org-show-entry))
  4363. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4364. (setq tags (and (match-end 2) (match-string 2)))
  4365. (and (match-end 1)
  4366. (delete-region (match-beginning 1) (match-end 1)))
  4367. (setq pos (point-at-bol))
  4368. (or split (end-of-line 1))
  4369. (delete-horizontal-space)
  4370. (newline (if blank 2 1))
  4371. (when tags
  4372. (save-excursion
  4373. (goto-char pos)
  4374. (end-of-line 1)
  4375. (insert " " tags)
  4376. (org-set-tags nil 'align))))
  4377. (t
  4378. (or split (end-of-line 1))
  4379. (newline (if blank 2 1)))))))
  4380. (insert head) (just-one-space)
  4381. (setq pos (point))
  4382. (end-of-line 1)
  4383. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4384. (when (and org-insert-heading-respect-content hide-previous)
  4385. (save-excursion
  4386. (goto-char previous-pos)
  4387. (hide-subtree)))
  4388. (run-hooks 'org-insert-heading-hook)))))
  4389. (defun org-get-heading (&optional no-tags)
  4390. "Return the heading of the current entry, without the stars."
  4391. (save-excursion
  4392. (org-back-to-heading t)
  4393. (if (looking-at
  4394. (if no-tags
  4395. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4396. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4397. (match-string 1) "")))
  4398. (defun org-insert-heading-after-current ()
  4399. "Insert a new heading with same level as current, after current subtree."
  4400. (interactive)
  4401. (org-back-to-heading)
  4402. (org-insert-heading)
  4403. (org-move-subtree-down)
  4404. (end-of-line 1))
  4405. (defun org-insert-heading-respect-content ()
  4406. (interactive)
  4407. (let ((org-insert-heading-respect-content t))
  4408. (org-insert-heading t)))
  4409. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4410. (interactive "P")
  4411. (let ((org-insert-heading-respect-content t))
  4412. (org-insert-todo-heading force-state t)))
  4413. (defun org-insert-todo-heading (arg &optional force-heading)
  4414. "Insert a new heading with the same level and TODO state as current heading.
  4415. If the heading has no TODO state, or if the state is DONE, use the first
  4416. state (TODO by default). Also with prefix arg, force first state."
  4417. (interactive "P")
  4418. (when (or force-heading (not (org-insert-item 'checkbox)))
  4419. (org-insert-heading force-heading)
  4420. (save-excursion
  4421. (org-back-to-heading)
  4422. (outline-previous-heading)
  4423. (looking-at org-todo-line-regexp))
  4424. (if (or arg
  4425. (not (match-beginning 2))
  4426. (member (match-string 2) org-done-keywords))
  4427. (insert (car org-todo-keywords-1) " ")
  4428. (insert (match-string 2) " "))
  4429. (when org-provide-todo-statistics
  4430. (org-update-parent-todo-statistics))))
  4431. (defun org-insert-subheading (arg)
  4432. "Insert a new subheading and demote it.
  4433. Works for outline headings and for plain lists alike."
  4434. (interactive "P")
  4435. (org-insert-heading arg)
  4436. (cond
  4437. ((org-on-heading-p) (org-do-demote))
  4438. ((org-at-item-p) (org-indent-item 1))))
  4439. (defun org-insert-todo-subheading (arg)
  4440. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4441. Works for outline headings and for plain lists alike."
  4442. (interactive "P")
  4443. (org-insert-todo-heading arg)
  4444. (cond
  4445. ((org-on-heading-p) (org-do-demote))
  4446. ((org-at-item-p) (org-indent-item 1))))
  4447. ;;; Promotion and Demotion
  4448. (defun org-promote-subtree ()
  4449. "Promote the entire subtree.
  4450. See also `org-promote'."
  4451. (interactive)
  4452. (save-excursion
  4453. (org-map-tree 'org-promote))
  4454. (org-fix-position-after-promote))
  4455. (defun org-demote-subtree ()
  4456. "Demote the entire subtree. See `org-demote'.
  4457. See also `org-promote'."
  4458. (interactive)
  4459. (save-excursion
  4460. (org-map-tree 'org-demote))
  4461. (org-fix-position-after-promote))
  4462. (defun org-do-promote ()
  4463. "Promote the current heading higher up the tree.
  4464. If the region is active in `transient-mark-mode', promote all headings
  4465. in the region."
  4466. (interactive)
  4467. (save-excursion
  4468. (if (org-region-active-p)
  4469. (org-map-region 'org-promote (region-beginning) (region-end))
  4470. (org-promote)))
  4471. (org-fix-position-after-promote))
  4472. (defun org-do-demote ()
  4473. "Demote the current heading lower down the tree.
  4474. If the region is active in `transient-mark-mode', demote all headings
  4475. in the region."
  4476. (interactive)
  4477. (save-excursion
  4478. (if (org-region-active-p)
  4479. (org-map-region 'org-demote (region-beginning) (region-end))
  4480. (org-demote)))
  4481. (org-fix-position-after-promote))
  4482. (defun org-fix-position-after-promote ()
  4483. "Make sure that after pro/demotion cursor position is right."
  4484. (let ((pos (point)))
  4485. (when (save-excursion
  4486. (beginning-of-line 1)
  4487. (looking-at org-todo-line-regexp)
  4488. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4489. (cond ((eobp) (insert " "))
  4490. ((eolp) (insert " "))
  4491. ((equal (char-after) ?\ ) (forward-char 1))))))
  4492. (defun org-reduced-level (l)
  4493. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4494. (defun org-get-valid-level (level &optional change)
  4495. "Rectify a level change under the influence of `org-odd-levels-only'
  4496. LEVEL is a current level, CHANGE is by how much the level should be
  4497. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4498. even level numbers will become the next higher odd number."
  4499. (if org-odd-levels-only
  4500. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4501. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4502. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4503. (max 1 (+ level change))))
  4504. (if (boundp 'define-obsolete-function-alias)
  4505. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4506. (define-obsolete-function-alias 'org-get-legal-level
  4507. 'org-get-valid-level)
  4508. (define-obsolete-function-alias 'org-get-legal-level
  4509. 'org-get-valid-level "23.1")))
  4510. (defun org-promote ()
  4511. "Promote the current heading higher up the tree.
  4512. If the region is active in `transient-mark-mode', promote all headings
  4513. in the region."
  4514. (org-back-to-heading t)
  4515. (let* ((level (save-match-data (funcall outline-level)))
  4516. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4517. (diff (abs (- level (length up-head) -1))))
  4518. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4519. (replace-match up-head nil t)
  4520. ;; Fixup tag positioning
  4521. (and org-auto-align-tags (org-set-tags nil t))
  4522. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4523. (defun org-demote ()
  4524. "Demote the current heading lower down the tree.
  4525. If the region is active in `transient-mark-mode', demote all headings
  4526. in the region."
  4527. (org-back-to-heading t)
  4528. (let* ((level (save-match-data (funcall outline-level)))
  4529. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4530. (diff (abs (- level (length down-head) -1))))
  4531. (replace-match down-head nil t)
  4532. ;; Fixup tag positioning
  4533. (and org-auto-align-tags (org-set-tags nil t))
  4534. (if org-adapt-indentation (org-fixup-indentation diff))))
  4535. (defun org-map-tree (fun)
  4536. "Call FUN for every heading underneath the current one."
  4537. (org-back-to-heading)
  4538. (let ((level (funcall outline-level)))
  4539. (save-excursion
  4540. (funcall fun)
  4541. (while (and (progn
  4542. (outline-next-heading)
  4543. (> (funcall outline-level) level))
  4544. (not (eobp)))
  4545. (funcall fun)))))
  4546. (defun org-map-region (fun beg end)
  4547. "Call FUN for every heading between BEG and END."
  4548. (let ((org-ignore-region t))
  4549. (save-excursion
  4550. (setq end (copy-marker end))
  4551. (goto-char beg)
  4552. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4553. (< (point) end))
  4554. (funcall fun))
  4555. (while (and (progn
  4556. (outline-next-heading)
  4557. (< (point) end))
  4558. (not (eobp)))
  4559. (funcall fun)))))
  4560. (defun org-fixup-indentation (diff)
  4561. "Change the indentation in the current entry by DIFF
  4562. However, if any line in the current entry has no indentation, or if it
  4563. would end up with no indentation after the change, nothing at all is done."
  4564. (save-excursion
  4565. (let ((end (save-excursion (outline-next-heading)
  4566. (point-marker)))
  4567. (prohibit (if (> diff 0)
  4568. "^\\S-"
  4569. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4570. col)
  4571. (unless (save-excursion (end-of-line 1)
  4572. (re-search-forward prohibit end t))
  4573. (while (and (< (point) end)
  4574. (re-search-forward "^[ \t]+" end t))
  4575. (goto-char (match-end 0))
  4576. (setq col (current-column))
  4577. (if (< diff 0) (replace-match ""))
  4578. (org-indent-to-column (+ diff col))))
  4579. (move-marker end nil))))
  4580. (defun org-convert-to-odd-levels ()
  4581. "Convert an org-mode file with all levels allowed to one with odd levels.
  4582. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4583. level 5 etc."
  4584. (interactive)
  4585. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4586. (let ((org-odd-levels-only nil) n)
  4587. (save-excursion
  4588. (goto-char (point-min))
  4589. (while (re-search-forward "^\\*\\*+ " nil t)
  4590. (setq n (- (length (match-string 0)) 2))
  4591. (while (>= (setq n (1- n)) 0)
  4592. (org-demote))
  4593. (end-of-line 1))))))
  4594. (defun org-convert-to-oddeven-levels ()
  4595. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4596. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4597. section with an even level, conversion would destroy the structure of the file. An error
  4598. is signaled in this case."
  4599. (interactive)
  4600. (goto-char (point-min))
  4601. ;; First check if there are no even levels
  4602. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4603. (org-show-context t)
  4604. (error "Not all levels are odd in this file. Conversion not possible."))
  4605. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4606. (let ((org-odd-levels-only nil) n)
  4607. (save-excursion
  4608. (goto-char (point-min))
  4609. (while (re-search-forward "^\\*\\*+ " nil t)
  4610. (setq n (/ (1- (length (match-string 0))) 2))
  4611. (while (>= (setq n (1- n)) 0)
  4612. (org-promote))
  4613. (end-of-line 1))))))
  4614. (defun org-tr-level (n)
  4615. "Make N odd if required."
  4616. (if org-odd-levels-only (1+ (/ n 2)) n))
  4617. ;;; Vertical tree motion, cutting and pasting of subtrees
  4618. (defun org-move-subtree-up (&optional arg)
  4619. "Move the current subtree up past ARG headlines of the same level."
  4620. (interactive "p")
  4621. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4622. (defun org-move-subtree-down (&optional arg)
  4623. "Move the current subtree down past ARG headlines of the same level."
  4624. (interactive "p")
  4625. (setq arg (prefix-numeric-value arg))
  4626. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4627. 'outline-get-last-sibling))
  4628. (ins-point (make-marker))
  4629. (cnt (abs arg))
  4630. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4631. ;; Select the tree
  4632. (org-back-to-heading)
  4633. (setq beg0 (point))
  4634. (save-excursion
  4635. (setq ne-beg (org-back-over-empty-lines))
  4636. (setq beg (point)))
  4637. (save-match-data
  4638. (save-excursion (outline-end-of-heading)
  4639. (setq folded (org-invisible-p)))
  4640. (outline-end-of-subtree))
  4641. (outline-next-heading)
  4642. (setq ne-end (org-back-over-empty-lines))
  4643. (setq end (point))
  4644. (goto-char beg0)
  4645. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4646. ;; include less whitespace
  4647. (save-excursion
  4648. (goto-char beg)
  4649. (forward-line (- ne-beg ne-end))
  4650. (setq beg (point))))
  4651. ;; Find insertion point, with error handling
  4652. (while (> cnt 0)
  4653. (or (and (funcall movfunc) (looking-at outline-regexp))
  4654. (progn (goto-char beg0)
  4655. (error "Cannot move past superior level or buffer limit")))
  4656. (setq cnt (1- cnt)))
  4657. (if (> arg 0)
  4658. ;; Moving forward - still need to move over subtree
  4659. (progn (org-end-of-subtree t t)
  4660. (save-excursion
  4661. (org-back-over-empty-lines)
  4662. (or (bolp) (newline)))))
  4663. (setq ne-ins (org-back-over-empty-lines))
  4664. (move-marker ins-point (point))
  4665. (setq txt (buffer-substring beg end))
  4666. (org-save-markers-in-region beg end)
  4667. (delete-region beg end)
  4668. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4669. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4670. (let ((bbb (point)))
  4671. (insert-before-markers txt)
  4672. (org-reinstall-markers-in-region bbb)
  4673. (move-marker ins-point bbb))
  4674. (or (bolp) (insert "\n"))
  4675. (setq ins-end (point))
  4676. (goto-char ins-point)
  4677. (org-skip-whitespace)
  4678. (when (and (< arg 0)
  4679. (org-first-sibling-p)
  4680. (> ne-ins ne-beg))
  4681. ;; Move whitespace back to beginning
  4682. (save-excursion
  4683. (goto-char ins-end)
  4684. (let ((kill-whole-line t))
  4685. (kill-line (- ne-ins ne-beg)) (point)))
  4686. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4687. (move-marker ins-point nil)
  4688. (org-compact-display-after-subtree-move)
  4689. (org-show-empty-lines-in-parent)
  4690. (unless folded
  4691. (org-show-entry)
  4692. (show-children)
  4693. (org-cycle-hide-drawers 'children))))
  4694. (defvar org-subtree-clip ""
  4695. "Clipboard for cut and paste of subtrees.
  4696. This is actually only a copy of the kill, because we use the normal kill
  4697. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4698. (defvar org-subtree-clip-folded nil
  4699. "Was the last copied subtree folded?
  4700. This is used to fold the tree back after pasting.")
  4701. (defun org-cut-subtree (&optional n)
  4702. "Cut the current subtree into the clipboard.
  4703. With prefix arg N, cut this many sequential subtrees.
  4704. This is a short-hand for marking the subtree and then cutting it."
  4705. (interactive "p")
  4706. (org-copy-subtree n 'cut))
  4707. (defun org-copy-subtree (&optional n cut force-store-markers)
  4708. "Cut the current subtree into the clipboard.
  4709. With prefix arg N, cut this many sequential subtrees.
  4710. This is a short-hand for marking the subtree and then copying it.
  4711. If CUT is non-nil, actually cut the subtree.
  4712. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4713. of some markers in the region, even if CUT is non-nil. This is
  4714. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4715. (interactive "p")
  4716. (let (beg end folded (beg0 (point)))
  4717. (if (interactive-p)
  4718. (org-back-to-heading nil) ; take what looks like a subtree
  4719. (org-back-to-heading t)) ; take what is really there
  4720. (org-back-over-empty-lines)
  4721. (setq beg (point))
  4722. (skip-chars-forward " \t\r\n")
  4723. (save-match-data
  4724. (save-excursion (outline-end-of-heading)
  4725. (setq folded (org-invisible-p)))
  4726. (condition-case nil
  4727. (outline-forward-same-level (1- n))
  4728. (error nil))
  4729. (org-end-of-subtree t t))
  4730. (org-back-over-empty-lines)
  4731. (setq end (point))
  4732. (goto-char beg0)
  4733. (when (> end beg)
  4734. (setq org-subtree-clip-folded folded)
  4735. (when (or cut force-store-markers)
  4736. (org-save-markers-in-region beg end))
  4737. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4738. (setq org-subtree-clip (current-kill 0))
  4739. (message "%s: Subtree(s) with %d characters"
  4740. (if cut "Cut" "Copied")
  4741. (length org-subtree-clip)))))
  4742. (defun org-paste-subtree (&optional level tree for-yank)
  4743. "Paste the clipboard as a subtree, with modification of headline level.
  4744. The entire subtree is promoted or demoted in order to match a new headline
  4745. level.
  4746. If the cursor is at the beginning of a headline, the same level as
  4747. that headline is used to paste the tree
  4748. If not, the new level is derived from the *visible* headings
  4749. before and after the insertion point, and taken to be the inferior headline
  4750. level of the two. So if the previous visible heading is level 3 and the
  4751. next is level 4 (or vice versa), level 4 will be used for insertion.
  4752. This makes sure that the subtree remains an independent subtree and does
  4753. not swallow low level entries.
  4754. You can also force a different level, either by using a numeric prefix
  4755. argument, or by inserting the heading marker by hand. For example, if the
  4756. cursor is after \"*****\", then the tree will be shifted to level 5.
  4757. If optional TREE is given, use this text instead of the kill ring.
  4758. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4759. move back over whitespace before inserting, and move point to the end of
  4760. the inserted text when done."
  4761. (interactive "P")
  4762. (unless (org-kill-is-subtree-p tree)
  4763. (error "%s"
  4764. (substitute-command-keys
  4765. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4766. (let* ((visp (not (org-invisible-p)))
  4767. (txt (or tree (and kill-ring (current-kill 0))))
  4768. (^re (concat "^\\(" outline-regexp "\\)"))
  4769. (re (concat "\\(" outline-regexp "\\)"))
  4770. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4771. (old-level (if (string-match ^re txt)
  4772. (- (match-end 0) (match-beginning 0) 1)
  4773. -1))
  4774. (force-level (cond (level (prefix-numeric-value level))
  4775. ((and (looking-at "[ \t]*$")
  4776. (string-match
  4777. ^re_ (buffer-substring
  4778. (point-at-bol) (point))))
  4779. (- (match-end 1) (match-beginning 1)))
  4780. ((and (bolp)
  4781. (looking-at org-outline-regexp))
  4782. (- (match-end 0) (point) 1))
  4783. (t nil)))
  4784. (previous-level (save-excursion
  4785. (condition-case nil
  4786. (progn
  4787. (outline-previous-visible-heading 1)
  4788. (if (looking-at re)
  4789. (- (match-end 0) (match-beginning 0) 1)
  4790. 1))
  4791. (error 1))))
  4792. (next-level (save-excursion
  4793. (condition-case nil
  4794. (progn
  4795. (or (looking-at outline-regexp)
  4796. (outline-next-visible-heading 1))
  4797. (if (looking-at re)
  4798. (- (match-end 0) (match-beginning 0) 1)
  4799. 1))
  4800. (error 1))))
  4801. (new-level (or force-level (max previous-level next-level)))
  4802. (shift (if (or (= old-level -1)
  4803. (= new-level -1)
  4804. (= old-level new-level))
  4805. 0
  4806. (- new-level old-level)))
  4807. (delta (if (> shift 0) -1 1))
  4808. (func (if (> shift 0) 'org-demote 'org-promote))
  4809. (org-odd-levels-only nil)
  4810. beg end newend)
  4811. ;; Remove the forced level indicator
  4812. (if force-level
  4813. (delete-region (point-at-bol) (point)))
  4814. ;; Paste
  4815. (beginning-of-line 1)
  4816. (unless for-yank (org-back-over-empty-lines))
  4817. (setq beg (point))
  4818. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4819. (insert-before-markers txt)
  4820. (unless (string-match "\n\\'" txt) (insert "\n"))
  4821. (setq newend (point))
  4822. (org-reinstall-markers-in-region beg)
  4823. (setq end (point))
  4824. (goto-char beg)
  4825. (skip-chars-forward " \t\n\r")
  4826. (setq beg (point))
  4827. (if (and (org-invisible-p) visp)
  4828. (save-excursion (outline-show-heading)))
  4829. ;; Shift if necessary
  4830. (unless (= shift 0)
  4831. (save-restriction
  4832. (narrow-to-region beg end)
  4833. (while (not (= shift 0))
  4834. (org-map-region func (point-min) (point-max))
  4835. (setq shift (+ delta shift)))
  4836. (goto-char (point-min))
  4837. (setq newend (point-max))))
  4838. (when (or (interactive-p) for-yank)
  4839. (message "Clipboard pasted as level %d subtree" new-level))
  4840. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4841. kill-ring
  4842. (eq org-subtree-clip (current-kill 0))
  4843. org-subtree-clip-folded)
  4844. ;; The tree was folded before it was killed/copied
  4845. (hide-subtree))
  4846. (and for-yank (goto-char newend))))
  4847. (defun org-kill-is-subtree-p (&optional txt)
  4848. "Check if the current kill is an outline subtree, or a set of trees.
  4849. Returns nil if kill does not start with a headline, or if the first
  4850. headline level is not the largest headline level in the tree.
  4851. So this will actually accept several entries of equal levels as well,
  4852. which is OK for `org-paste-subtree'.
  4853. If optional TXT is given, check this string instead of the current kill."
  4854. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4855. (start-level (and kill
  4856. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4857. org-outline-regexp "\\)")
  4858. kill)
  4859. (- (match-end 2) (match-beginning 2) 1)))
  4860. (re (concat "^" org-outline-regexp))
  4861. (start (1+ (or (match-beginning 2) -1))))
  4862. (if (not start-level)
  4863. (progn
  4864. nil) ;; does not even start with a heading
  4865. (catch 'exit
  4866. (while (setq start (string-match re kill (1+ start)))
  4867. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4868. (throw 'exit nil)))
  4869. t))))
  4870. (defvar org-markers-to-move nil
  4871. "Markers that should be moved with a cut-and-paste operation.
  4872. Those markers are stored together with their positions relative to
  4873. the start of the region.")
  4874. (defun org-save-markers-in-region (beg end)
  4875. "Check markers in region.
  4876. If these markers are between BEG and END, record their position relative
  4877. to BEG, so that after moving the block of text, we can put the markers back
  4878. into place.
  4879. This function gets called just before an entry or tree gets cut from the
  4880. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4881. called immediately, to move the markers with the entries."
  4882. (setq org-markers-to-move nil)
  4883. (when (featurep 'org-clock)
  4884. (org-clock-save-markers-for-cut-and-paste beg end))
  4885. (when (featurep 'org-agenda)
  4886. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4887. (defun org-check-and-save-marker (marker beg end)
  4888. "Check if MARKER is between BEG and END.
  4889. If yes, remember the marker and the distance to BEG."
  4890. (when (and (marker-buffer marker)
  4891. (equal (marker-buffer marker) (current-buffer)))
  4892. (if (and (>= marker beg) (< marker end))
  4893. (push (cons marker (- marker beg)) org-markers-to-move))))
  4894. (defun org-reinstall-markers-in-region (beg)
  4895. "Move all remembered markers to their position relative to BEG."
  4896. (mapc (lambda (x)
  4897. (move-marker (car x) (+ beg (cdr x))))
  4898. org-markers-to-move)
  4899. (setq org-markers-to-move nil))
  4900. (defun org-narrow-to-subtree ()
  4901. "Narrow buffer to the current subtree."
  4902. (interactive)
  4903. (save-excursion
  4904. (save-match-data
  4905. (narrow-to-region
  4906. (progn (org-back-to-heading) (point))
  4907. (progn (org-end-of-subtree t) (point))))))
  4908. ;;; Outline Sorting
  4909. (defun org-sort (with-case)
  4910. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4911. Optional argument WITH-CASE means sort case-sensitively."
  4912. (interactive "P")
  4913. (if (org-at-table-p)
  4914. (org-call-with-arg 'org-table-sort-lines with-case)
  4915. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4916. (defun org-sort-remove-invisible (s)
  4917. (remove-text-properties 0 (length s) org-rm-props s)
  4918. (while (string-match org-bracket-link-regexp s)
  4919. (setq s (replace-match (if (match-end 2)
  4920. (match-string 3 s)
  4921. (match-string 1 s)) t t s)))
  4922. s)
  4923. (defvar org-priority-regexp) ; defined later in the file
  4924. (defun org-sort-entries-or-items
  4925. (&optional with-case sorting-type getkey-func compare-func property)
  4926. "Sort entries on a certain level of an outline tree.
  4927. If there is an active region, the entries in the region are sorted.
  4928. Else, if the cursor is before the first entry, sort the top-level items.
  4929. Else, the children of the entry at point are sorted.
  4930. Sorting can be alphabetically, numerically, and by date/time as given by
  4931. the first time stamp in the entry. The command prompts for the sorting
  4932. type unless it has been given to the function through the SORTING-TYPE
  4933. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4934. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4935. called with point at the beginning of the record. It must return either
  4936. a string or a number that should serve as the sorting key for that record.
  4937. Comparing entries ignores case by default. However, with an optional argument
  4938. WITH-CASE, the sorting considers case as well."
  4939. (interactive "P")
  4940. (let ((case-func (if with-case 'identity 'downcase))
  4941. start beg end stars re re2
  4942. txt what tmp plain-list-p)
  4943. ;; Find beginning and end of region to sort
  4944. (cond
  4945. ((org-region-active-p)
  4946. ;; we will sort the region
  4947. (setq end (region-end)
  4948. what "region")
  4949. (goto-char (region-beginning))
  4950. (if (not (org-on-heading-p)) (outline-next-heading))
  4951. (setq start (point)))
  4952. ((org-at-item-p)
  4953. ;; we will sort this plain list
  4954. (org-beginning-of-item-list) (setq start (point))
  4955. (org-end-of-item-list) (setq end (point))
  4956. (goto-char start)
  4957. (setq plain-list-p t
  4958. what "plain list"))
  4959. ((or (org-on-heading-p)
  4960. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4961. ;; we will sort the children of the current headline
  4962. (org-back-to-heading)
  4963. (setq start (point)
  4964. end (progn (org-end-of-subtree t t)
  4965. (org-back-over-empty-lines)
  4966. (point))
  4967. what "children")
  4968. (goto-char start)
  4969. (show-subtree)
  4970. (outline-next-heading))
  4971. (t
  4972. ;; we will sort the top-level entries in this file
  4973. (goto-char (point-min))
  4974. (or (org-on-heading-p) (outline-next-heading))
  4975. (setq start (point) end (point-max) what "top-level")
  4976. (goto-char start)
  4977. (show-all)))
  4978. (setq beg (point))
  4979. (if (>= beg end) (error "Nothing to sort"))
  4980. (unless plain-list-p
  4981. (looking-at "\\(\\*+\\)")
  4982. (setq stars (match-string 1)
  4983. re (concat "^" (regexp-quote stars) " +")
  4984. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4985. txt (buffer-substring beg end))
  4986. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4987. (if (and (not (equal stars "*")) (string-match re2 txt))
  4988. (error "Region to sort contains a level above the first entry")))
  4989. (unless sorting-type
  4990. (message
  4991. (if plain-list-p
  4992. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4993. "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:")
  4994. what)
  4995. (setq sorting-type (read-char-exclusive))
  4996. (and (= (downcase sorting-type) ?f)
  4997. (setq getkey-func
  4998. (org-ido-completing-read "Sort using function: "
  4999. obarray 'fboundp t nil nil))
  5000. (setq getkey-func (intern getkey-func)))
  5001. (and (= (downcase sorting-type) ?r)
  5002. (setq property
  5003. (org-ido-completing-read "Property: "
  5004. (mapcar 'list (org-buffer-property-keys t))
  5005. nil t))))
  5006. (message "Sorting entries...")
  5007. (save-restriction
  5008. (narrow-to-region start end)
  5009. (let ((dcst (downcase sorting-type))
  5010. (now (current-time)))
  5011. (sort-subr
  5012. (/= dcst sorting-type)
  5013. ;; This function moves to the beginning character of the "record" to
  5014. ;; be sorted.
  5015. (if plain-list-p
  5016. (lambda nil
  5017. (if (org-at-item-p) t (goto-char (point-max))))
  5018. (lambda nil
  5019. (if (re-search-forward re nil t)
  5020. (goto-char (match-beginning 0))
  5021. (goto-char (point-max)))))
  5022. ;; This function moves to the last character of the "record" being
  5023. ;; sorted.
  5024. (if plain-list-p
  5025. 'org-end-of-item
  5026. (lambda nil
  5027. (save-match-data
  5028. (condition-case nil
  5029. (outline-forward-same-level 1)
  5030. (error
  5031. (goto-char (point-max)))))))
  5032. ;; This function returns the value that gets sorted against.
  5033. (if plain-list-p
  5034. (lambda nil
  5035. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  5036. (cond
  5037. ((= dcst ?n)
  5038. (string-to-number (buffer-substring (match-end 0)
  5039. (point-at-eol))))
  5040. ((= dcst ?a)
  5041. (buffer-substring (match-end 0) (point-at-eol)))
  5042. ((= dcst ?t)
  5043. (if (re-search-forward org-ts-regexp
  5044. (point-at-eol) t)
  5045. (org-time-string-to-time (match-string 0))
  5046. now))
  5047. ((= dcst ?f)
  5048. (if getkey-func
  5049. (progn
  5050. (setq tmp (funcall getkey-func))
  5051. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5052. tmp)
  5053. (error "Invalid key function `%s'" getkey-func)))
  5054. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5055. (lambda nil
  5056. (cond
  5057. ((= dcst ?n)
  5058. (if (looking-at org-complex-heading-regexp)
  5059. (string-to-number (match-string 4))
  5060. nil))
  5061. ((= dcst ?a)
  5062. (if (looking-at org-complex-heading-regexp)
  5063. (funcall case-func (match-string 4))
  5064. nil))
  5065. ((= dcst ?t)
  5066. (if (re-search-forward org-ts-regexp
  5067. (save-excursion
  5068. (forward-line 2)
  5069. (point)) t)
  5070. (org-time-string-to-time (match-string 0))
  5071. now))
  5072. ((= dcst ?p)
  5073. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5074. (string-to-char (match-string 2))
  5075. org-default-priority))
  5076. ((= dcst ?r)
  5077. (or (org-entry-get nil property) ""))
  5078. ((= dcst ?o)
  5079. (if (looking-at org-complex-heading-regexp)
  5080. (- 9999 (length (member (match-string 2)
  5081. org-todo-keywords-1)))))
  5082. ((= dcst ?f)
  5083. (if getkey-func
  5084. (progn
  5085. (setq tmp (funcall getkey-func))
  5086. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5087. tmp)
  5088. (error "Invalid key function `%s'" getkey-func)))
  5089. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5090. nil
  5091. (cond
  5092. ((= dcst ?a) 'string<)
  5093. ((= dcst ?t) 'time-less-p)
  5094. ((= dcst ?f) compare-func)
  5095. (t nil)))))
  5096. (message "Sorting entries...done")))
  5097. (defun org-do-sort (table what &optional with-case sorting-type)
  5098. "Sort TABLE of WHAT according to SORTING-TYPE.
  5099. The user will be prompted for the SORTING-TYPE if the call to this
  5100. function does not specify it. WHAT is only for the prompt, to indicate
  5101. what is being sorted. The sorting key will be extracted from
  5102. the car of the elements of the table.
  5103. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5104. (unless sorting-type
  5105. (message
  5106. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5107. what)
  5108. (setq sorting-type (read-char-exclusive)))
  5109. (let ((dcst (downcase sorting-type))
  5110. extractfun comparefun)
  5111. ;; Define the appropriate functions
  5112. (cond
  5113. ((= dcst ?n)
  5114. (setq extractfun 'string-to-number
  5115. comparefun (if (= dcst sorting-type) '< '>)))
  5116. ((= dcst ?a)
  5117. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5118. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5119. comparefun (if (= dcst sorting-type)
  5120. 'string<
  5121. (lambda (a b) (and (not (string< a b))
  5122. (not (string= a b)))))))
  5123. ((= dcst ?t)
  5124. (setq extractfun
  5125. (lambda (x)
  5126. (if (string-match org-ts-regexp x)
  5127. (time-to-seconds
  5128. (org-time-string-to-time (match-string 0 x)))
  5129. 0))
  5130. comparefun (if (= dcst sorting-type) '< '>)))
  5131. (t (error "Invalid sorting type `%c'" sorting-type)))
  5132. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5133. table)
  5134. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5135. ;;; Editing source examples
  5136. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5137. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5138. (defvar org-edit-src-force-single-line nil)
  5139. (defvar org-edit-src-from-org-mode nil)
  5140. (defvar org-edit-src-picture nil)
  5141. (define-minor-mode org-exit-edit-mode
  5142. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5143. (defun org-edit-src-code ()
  5144. "Edit the source code example at point.
  5145. An indirect buffer is created, and that buffer is then narrowed to the
  5146. example at point and switched to the correct language mode. When done,
  5147. exit by killing the buffer with \\[org-edit-src-exit]."
  5148. (interactive)
  5149. (let ((line (org-current-line))
  5150. (case-fold-search t)
  5151. (msg (substitute-command-keys
  5152. "Edit, then exit with C-c ' (C-c and single quote)"))
  5153. (info (org-edit-src-find-region-and-lang))
  5154. (org-mode-p (eq major-mode 'org-mode))
  5155. beg end lang lang-f single)
  5156. (if (not info)
  5157. nil
  5158. (setq beg (nth 0 info)
  5159. end (nth 1 info)
  5160. lang (nth 2 info)
  5161. single (nth 3 info)
  5162. lang-f (intern (concat lang "-mode")))
  5163. (unless (functionp lang-f)
  5164. (error "No such language mode: %s" lang-f))
  5165. (goto-line line)
  5166. (if (get-buffer "*Org Edit Src Example*")
  5167. (kill-buffer "*Org Edit Src Example*"))
  5168. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5169. "*Org Edit Src Example*"))
  5170. (narrow-to-region beg end)
  5171. (remove-text-properties beg end '(display nil invisible nil
  5172. intangible nil))
  5173. (let ((org-inhibit-startup t))
  5174. (funcall lang-f))
  5175. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5176. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5177. (when org-mode-p
  5178. (goto-char (point-min))
  5179. (while (re-search-forward "^," nil t)
  5180. (replace-match "")))
  5181. (goto-line line)
  5182. (org-exit-edit-mode)
  5183. (org-set-local 'header-line-format msg)
  5184. (message "%s" msg)
  5185. t)))
  5186. (defun org-edit-fixed-width-region ()
  5187. "Edit the fixed-width ascii drawing at point.
  5188. This must be a region where each line starts with a colon followed by
  5189. a space character.
  5190. An indirect buffer is created, and that buffer is then narrowed to the
  5191. example at point and switched to artist-mode. When done,
  5192. exit by killing the buffer with \\[org-edit-src-exit]."
  5193. (interactive)
  5194. (let ((line (org-current-line))
  5195. (case-fold-search t)
  5196. (msg (substitute-command-keys
  5197. "Edit, then exit with C-c ' (C-c and single quote)"))
  5198. (org-mode-p (eq major-mode 'org-mode))
  5199. beg end lang lang-f)
  5200. (beginning-of-line 1)
  5201. (if (looking-at "[ \t]*[^:\n \t]")
  5202. nil
  5203. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5204. (setq beg (point) end beg)
  5205. (save-excursion
  5206. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5207. (setq beg (point-at-bol 2))
  5208. (setq beg (point))))
  5209. (save-excursion
  5210. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5211. (setq end (1- (match-beginning 0)))
  5212. (setq end (point))))
  5213. (goto-line line))
  5214. (if (get-buffer "*Org Edit Picture*")
  5215. (kill-buffer "*Org Edit Picture*"))
  5216. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5217. "*Org Edit Picture*"))
  5218. (narrow-to-region beg end)
  5219. (remove-text-properties beg end '(display nil invisible nil
  5220. intangible nil))
  5221. (when (fboundp 'font-lock-unfontify-region)
  5222. (font-lock-unfontify-region (point-min) (point-max)))
  5223. (cond
  5224. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5225. (fundamental-mode)
  5226. (artist-mode 1))
  5227. (t (funcall org-edit-fixed-width-region-mode)))
  5228. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5229. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5230. (set (make-local-variable 'org-edit-src-picture) t)
  5231. (goto-char (point-min))
  5232. (while (re-search-forward "^[ \t]*: ?" nil t)
  5233. (replace-match ""))
  5234. (goto-line line)
  5235. (org-exit-edit-mode)
  5236. (org-set-local 'header-line-format msg)
  5237. (message "%s" msg)
  5238. t)))
  5239. (defun org-edit-src-find-region-and-lang ()
  5240. "Find the region and language for a local edit.
  5241. Return a list with beginning and end of the region, a string representing
  5242. the language, a switch telling of the content should be in a single line."
  5243. (let ((re-list
  5244. (append
  5245. org-edit-src-region-extra
  5246. '(
  5247. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5248. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5249. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5250. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5251. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5252. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5253. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5254. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5255. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5256. ("^#\\+html:" "\n" "html" single-line)
  5257. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5258. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5259. ("^#\\+latex:" "\n" "latex" single-line)
  5260. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5261. ("^#\\+ascii:" "\n" "ascii" single-line)
  5262. )))
  5263. (pos (point))
  5264. re re1 re2 single beg end lang)
  5265. (catch 'exit
  5266. (while (setq entry (pop re-list))
  5267. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5268. single (nth 3 entry))
  5269. (save-excursion
  5270. (if (or (looking-at re1)
  5271. (re-search-backward re1 nil t))
  5272. (progn
  5273. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5274. (if (and (re-search-forward re2 nil t)
  5275. (>= (match-end 0) pos))
  5276. (throw 'exit (list beg (match-beginning 0) lang single))))
  5277. (if (or (looking-at re2)
  5278. (re-search-forward re2 nil t))
  5279. (progn
  5280. (setq end (match-beginning 0))
  5281. (if (and (re-search-backward re1 nil t)
  5282. (<= (match-beginning 0) pos))
  5283. (throw 'exit
  5284. (list (match-end 0) end
  5285. (org-edit-src-get-lang lang) single)))))))))))
  5286. (defun org-edit-src-get-lang (lang)
  5287. "Extract the src language."
  5288. (let ((m (match-string 0)))
  5289. (cond
  5290. ((stringp lang) lang)
  5291. ((integerp lang) (match-string lang))
  5292. ((and (eq lang 'lang)
  5293. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5294. (match-string 1 m))
  5295. ((and (eq lang 'style)
  5296. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5297. (match-string 1 m))
  5298. (t "fundamental"))))
  5299. (defun org-edit-src-exit ()
  5300. "Exit special edit and protect problematic lines."
  5301. (interactive)
  5302. (unless (buffer-base-buffer (current-buffer))
  5303. (error "This is not an indirect buffer, something is wrong..."))
  5304. (unless (> (point-min) 1)
  5305. (error "This buffer is not narrowed, something is wrong..."))
  5306. (goto-char (point-min))
  5307. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5308. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5309. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5310. (goto-char (point-min))
  5311. (while (re-search-forward "\n" nil t)
  5312. (replace-match " "))
  5313. (goto-char (point-min))
  5314. (if (looking-at "\\s-*") (replace-match " "))
  5315. (if (re-search-forward "\\s-+\\'" nil t)
  5316. (replace-match "")))
  5317. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5318. (goto-char (point-min))
  5319. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5320. (replace-match ",\\1"))
  5321. (when font-lock-mode
  5322. (font-lock-unfontify-region (point-min) (point-max)))
  5323. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5324. (when (org-bound-and-true-p org-edit-src-picture)
  5325. (untabify (point-min) (point-max))
  5326. (goto-char (point-min))
  5327. (while (re-search-forward "^" nil t)
  5328. (replace-match ": "))
  5329. (when font-lock-mode
  5330. (font-lock-unfontify-region (point-min) (point-max)))
  5331. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5332. (kill-buffer (current-buffer))
  5333. (and (org-mode-p) (org-restart-font-lock)))
  5334. ;;; The orgstruct minor mode
  5335. ;; Define a minor mode which can be used in other modes in order to
  5336. ;; integrate the org-mode structure editing commands.
  5337. ;; This is really a hack, because the org-mode structure commands use
  5338. ;; keys which normally belong to the major mode. Here is how it
  5339. ;; works: The minor mode defines all the keys necessary to operate the
  5340. ;; structure commands, but wraps the commands into a function which
  5341. ;; tests if the cursor is currently at a headline or a plain list
  5342. ;; item. If that is the case, the structure command is used,
  5343. ;; temporarily setting many Org-mode variables like regular
  5344. ;; expressions for filling etc. However, when any of those keys is
  5345. ;; used at a different location, function uses `key-binding' to look
  5346. ;; up if the key has an associated command in another currently active
  5347. ;; keymap (minor modes, major mode, global), and executes that
  5348. ;; command. There might be problems if any of the keys is otherwise
  5349. ;; used as a prefix key.
  5350. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5351. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5352. ;; addresses this by checking explicitly for both bindings.
  5353. (defvar orgstruct-mode-map (make-sparse-keymap)
  5354. "Keymap for the minor `orgstruct-mode'.")
  5355. (defvar org-local-vars nil
  5356. "List of local variables, for use by `orgstruct-mode'")
  5357. ;;;###autoload
  5358. (define-minor-mode orgstruct-mode
  5359. "Toggle the minor more `orgstruct-mode'.
  5360. This mode is for using Org-mode structure commands in other modes.
  5361. The following key behave as if Org-mode was active, if the cursor
  5362. is on a headline, or on a plain list item (both in the definition
  5363. of Org-mode).
  5364. M-up Move entry/item up
  5365. M-down Move entry/item down
  5366. M-left Promote
  5367. M-right Demote
  5368. M-S-up Move entry/item up
  5369. M-S-down Move entry/item down
  5370. M-S-left Promote subtree
  5371. M-S-right Demote subtree
  5372. M-q Fill paragraph and items like in Org-mode
  5373. C-c ^ Sort entries
  5374. C-c - Cycle list bullet
  5375. TAB Cycle item visibility
  5376. M-RET Insert new heading/item
  5377. S-M-RET Insert new TODO heading / Checkbox item
  5378. C-c C-c Set tags / toggle checkbox"
  5379. nil " OrgStruct" nil
  5380. (org-load-modules-maybe)
  5381. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5382. ;;;###autoload
  5383. (defun turn-on-orgstruct ()
  5384. "Unconditionally turn on `orgstruct-mode'."
  5385. (orgstruct-mode 1))
  5386. ;;;###autoload
  5387. (defun turn-on-orgstruct++ ()
  5388. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5389. In addition to setting orgstruct-mode, this also exports all indentation and
  5390. autofilling variables from org-mode into the buffer. Note that turning
  5391. off orgstruct-mode will *not* remove these additional settings."
  5392. (orgstruct-mode 1)
  5393. (let (var val)
  5394. (mapc
  5395. (lambda (x)
  5396. (when (string-match
  5397. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5398. (symbol-name (car x)))
  5399. (setq var (car x) val (nth 1 x))
  5400. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5401. org-local-vars)))
  5402. (defun orgstruct-error ()
  5403. "Error when there is no default binding for a structure key."
  5404. (interactive)
  5405. (error "This key has no function outside structure elements"))
  5406. (defun orgstruct-setup ()
  5407. "Setup orgstruct keymaps."
  5408. (let ((nfunc 0)
  5409. (bindings
  5410. (list
  5411. '([(meta up)] org-metaup)
  5412. '([(meta down)] org-metadown)
  5413. '([(meta left)] org-metaleft)
  5414. '([(meta right)] org-metaright)
  5415. '([(meta shift up)] org-shiftmetaup)
  5416. '([(meta shift down)] org-shiftmetadown)
  5417. '([(meta shift left)] org-shiftmetaleft)
  5418. '([(meta shift right)] org-shiftmetaright)
  5419. '([(shift up)] org-shiftup)
  5420. '([(shift down)] org-shiftdown)
  5421. '([(shift left)] org-shiftleft)
  5422. '([(shift right)] org-shiftright)
  5423. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5424. '("\M-q" fill-paragraph)
  5425. '("\C-c^" org-sort)
  5426. '("\C-c-" org-cycle-list-bullet)))
  5427. elt key fun cmd)
  5428. (while (setq elt (pop bindings))
  5429. (setq nfunc (1+ nfunc))
  5430. (setq key (org-key (car elt))
  5431. fun (nth 1 elt)
  5432. cmd (orgstruct-make-binding fun nfunc key))
  5433. (org-defkey orgstruct-mode-map key cmd))
  5434. ;; Special treatment needed for TAB and RET
  5435. (org-defkey orgstruct-mode-map [(tab)]
  5436. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5437. (org-defkey orgstruct-mode-map "\C-i"
  5438. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5439. (org-defkey orgstruct-mode-map "\M-\C-m"
  5440. (orgstruct-make-binding 'org-insert-heading 105
  5441. "\M-\C-m" [(meta return)]))
  5442. (org-defkey orgstruct-mode-map [(meta return)]
  5443. (orgstruct-make-binding 'org-insert-heading 106
  5444. [(meta return)] "\M-\C-m"))
  5445. (org-defkey orgstruct-mode-map [(shift meta return)]
  5446. (orgstruct-make-binding 'org-insert-todo-heading 107
  5447. [(meta return)] "\M-\C-m"))
  5448. (unless org-local-vars
  5449. (setq org-local-vars (org-get-local-variables)))
  5450. t))
  5451. (defun orgstruct-make-binding (fun n &rest keys)
  5452. "Create a function for binding in the structure minor mode.
  5453. FUN is the command to call inside a table. N is used to create a unique
  5454. command name. KEYS are keys that should be checked in for a command
  5455. to execute outside of tables."
  5456. (eval
  5457. (list 'defun
  5458. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5459. '(arg)
  5460. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5461. "Outside of structure, run the binding of `"
  5462. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5463. "'.")
  5464. '(interactive "p")
  5465. (list 'if
  5466. '(org-context-p 'headline 'item)
  5467. (list 'org-run-like-in-org-mode (list 'quote fun))
  5468. (list 'let '(orgstruct-mode)
  5469. (list 'call-interactively
  5470. (append '(or)
  5471. (mapcar (lambda (k)
  5472. (list 'key-binding k))
  5473. keys)
  5474. '('orgstruct-error))))))))
  5475. (defun org-context-p (&rest contexts)
  5476. "Check if local context is any of CONTEXTS.
  5477. Possible values in the list of contexts are `table', `headline', and `item'."
  5478. (let ((pos (point)))
  5479. (goto-char (point-at-bol))
  5480. (prog1 (or (and (memq 'table contexts)
  5481. (looking-at "[ \t]*|"))
  5482. (and (memq 'headline contexts)
  5483. ;;????????? (looking-at "\\*+"))
  5484. (looking-at outline-regexp))
  5485. (and (memq 'item contexts)
  5486. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5487. (goto-char pos))))
  5488. (defun org-get-local-variables ()
  5489. "Return a list of all local variables in an org-mode buffer."
  5490. (let (varlist)
  5491. (with-current-buffer (get-buffer-create "*Org tmp*")
  5492. (erase-buffer)
  5493. (org-mode)
  5494. (setq varlist (buffer-local-variables)))
  5495. (kill-buffer "*Org tmp*")
  5496. (delq nil
  5497. (mapcar
  5498. (lambda (x)
  5499. (setq x
  5500. (if (symbolp x)
  5501. (list x)
  5502. (list (car x) (list 'quote (cdr x)))))
  5503. (if (string-match
  5504. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5505. (symbol-name (car x)))
  5506. x nil))
  5507. varlist))))
  5508. ;;;###autoload
  5509. (defun org-run-like-in-org-mode (cmd)
  5510. (org-load-modules-maybe)
  5511. (unless org-local-vars
  5512. (setq org-local-vars (org-get-local-variables)))
  5513. (eval (list 'let org-local-vars
  5514. (list 'call-interactively (list 'quote cmd)))))
  5515. ;;;; Archiving
  5516. (defun org-get-category (&optional pos)
  5517. "Get the category applying to position POS."
  5518. (get-text-property (or pos (point)) 'org-category))
  5519. (defun org-refresh-category-properties ()
  5520. "Refresh category text properties in the buffer."
  5521. (let ((def-cat (cond
  5522. ((null org-category)
  5523. (if buffer-file-name
  5524. (file-name-sans-extension
  5525. (file-name-nondirectory buffer-file-name))
  5526. "???"))
  5527. ((symbolp org-category) (symbol-name org-category))
  5528. (t org-category)))
  5529. beg end cat pos optionp)
  5530. (org-unmodified
  5531. (save-excursion
  5532. (save-restriction
  5533. (widen)
  5534. (goto-char (point-min))
  5535. (put-text-property (point) (point-max) 'org-category def-cat)
  5536. (while (re-search-forward
  5537. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5538. (setq pos (match-end 0)
  5539. optionp (equal (char-after (match-beginning 0)) ?#)
  5540. cat (org-trim (match-string 2)))
  5541. (if optionp
  5542. (setq beg (point-at-bol) end (point-max))
  5543. (org-back-to-heading t)
  5544. (setq beg (point) end (org-end-of-subtree t t)))
  5545. (put-text-property beg end 'org-category cat)
  5546. (goto-char pos)))))))
  5547. ;;;; Link Stuff
  5548. ;;; Link abbreviations
  5549. (defun org-link-expand-abbrev (link)
  5550. "Apply replacements as defined in `org-link-abbrev-alist."
  5551. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5552. (let* ((key (match-string 1 link))
  5553. (as (or (assoc key org-link-abbrev-alist-local)
  5554. (assoc key org-link-abbrev-alist)))
  5555. (tag (and (match-end 2) (match-string 3 link)))
  5556. rpl)
  5557. (if (not as)
  5558. link
  5559. (setq rpl (cdr as))
  5560. (cond
  5561. ((symbolp rpl) (funcall rpl tag))
  5562. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5563. ((string-match "%h" rpl)
  5564. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5565. (t (concat rpl tag)))))
  5566. link))
  5567. ;;; Storing and inserting links
  5568. (defvar org-insert-link-history nil
  5569. "Minibuffer history for links inserted with `org-insert-link'.")
  5570. (defvar org-stored-links nil
  5571. "Contains the links stored with `org-store-link'.")
  5572. (defvar org-store-link-plist nil
  5573. "Plist with info about the most recently link created with `org-store-link'.")
  5574. (defvar org-link-protocols nil
  5575. "Link protocols added to Org-mode using `org-add-link-type'.")
  5576. (defvar org-store-link-functions nil
  5577. "List of functions that are called to create and store a link.
  5578. Each function will be called in turn until one returns a non-nil
  5579. value. Each function should check if it is responsible for creating
  5580. this link (for example by looking at the major mode).
  5581. If not, it must exit and return nil.
  5582. If yes, it should return a non-nil value after a calling
  5583. `org-store-link-props' with a list of properties and values.
  5584. Special properties are:
  5585. :type The link prefix. like \"http\". This must be given.
  5586. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5587. This is obligatory as well.
  5588. :description Optional default description for the second pair
  5589. of brackets in an Org-mode link. The user can still change
  5590. this when inserting this link into an Org-mode buffer.
  5591. In addition to these, any additional properties can be specified
  5592. and then used in remember templates.")
  5593. (defun org-add-link-type (type &optional follow export)
  5594. "Add TYPE to the list of `org-link-types'.
  5595. Re-compute all regular expressions depending on `org-link-types'
  5596. FOLLOW and EXPORT are two functions.
  5597. FOLLOW should take the link path as the single argument and do whatever
  5598. is necessary to follow the link, for example find a file or display
  5599. a mail message.
  5600. EXPORT should format the link path for export to one of the export formats.
  5601. It should be a function accepting three arguments:
  5602. path the path of the link, the text after the prefix (like \"http:\")
  5603. desc the description of the link, if any, nil if there was no description
  5604. format the export format, a symbol like `html' or `latex'.
  5605. The function may use the FORMAT information to return different values
  5606. depending on the format. The return value will be put literally into
  5607. the exported file.
  5608. Org-mode has a built-in default for exporting links. If you are happy with
  5609. this default, there is no need to define an export function for the link
  5610. type. For a simple example of an export function, see `org-bbdb.el'."
  5611. (add-to-list 'org-link-types type t)
  5612. (org-make-link-regexps)
  5613. (if (assoc type org-link-protocols)
  5614. (setcdr (assoc type org-link-protocols) (list follow export))
  5615. (push (list type follow export) org-link-protocols)))
  5616. ;;;###autoload
  5617. (defun org-store-link (arg)
  5618. "\\<org-mode-map>Store an org-link to the current location.
  5619. This link is added to `org-stored-links' and can later be inserted
  5620. into an org-buffer with \\[org-insert-link].
  5621. For some link types, a prefix arg is interpreted:
  5622. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5623. For file links, arg negates `org-context-in-file-links'."
  5624. (interactive "P")
  5625. (org-load-modules-maybe)
  5626. (setq org-store-link-plist nil) ; reset
  5627. (let (link cpltxt desc description search txt)
  5628. (cond
  5629. ((run-hook-with-args-until-success 'org-store-link-functions)
  5630. (setq link (plist-get org-store-link-plist :link)
  5631. desc (or (plist-get org-store-link-plist :description) link)))
  5632. ((equal (buffer-name) "*Org Edit Src Example*")
  5633. (let (label gc)
  5634. (while (or (not label)
  5635. (save-excursion
  5636. (save-restriction
  5637. (widen)
  5638. (goto-char (point-min))
  5639. (re-search-forward (concat "((" label "))") nil t))))
  5640. (when label (message "Label exists already") (sit-for 2))
  5641. (setq label (read-string "Code line label: " label)))
  5642. (end-of-line 1)
  5643. (setq link (format "((%s))" label))
  5644. (setq gc (- 79 (length link)))
  5645. (if (< (current-column) gc) (org-move-to-column gc t) (insert " "))
  5646. (insert link)
  5647. (setq desc nil)))
  5648. ((eq major-mode 'calendar-mode)
  5649. (let ((cd (calendar-cursor-to-date)))
  5650. (setq link
  5651. (format-time-string
  5652. (car org-time-stamp-formats)
  5653. (apply 'encode-time
  5654. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5655. nil nil nil))))
  5656. (org-store-link-props :type "calendar" :date cd)))
  5657. ((eq major-mode 'w3-mode)
  5658. (setq cpltxt (url-view-url t)
  5659. link (org-make-link cpltxt))
  5660. (org-store-link-props :type "w3" :url (url-view-url t)))
  5661. ((eq major-mode 'w3m-mode)
  5662. (setq cpltxt (or w3m-current-title w3m-current-url)
  5663. link (org-make-link w3m-current-url))
  5664. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5665. ((setq search (run-hook-with-args-until-success
  5666. 'org-create-file-search-functions))
  5667. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5668. "::" search))
  5669. (setq cpltxt (or description link)))
  5670. ((eq major-mode 'image-mode)
  5671. (setq cpltxt (concat "file:"
  5672. (abbreviate-file-name buffer-file-name))
  5673. link (org-make-link cpltxt))
  5674. (org-store-link-props :type "image" :file buffer-file-name))
  5675. ((eq major-mode 'dired-mode)
  5676. ;; link to the file in the current line
  5677. (setq cpltxt (concat "file:"
  5678. (abbreviate-file-name
  5679. (expand-file-name
  5680. (dired-get-filename nil t))))
  5681. link (org-make-link cpltxt)))
  5682. ((and buffer-file-name (org-mode-p))
  5683. (cond
  5684. ((org-in-regexp "<<\\(.*?\\)>>")
  5685. (setq cpltxt
  5686. (concat "file:"
  5687. (abbreviate-file-name buffer-file-name)
  5688. "::" (match-string 1))
  5689. link (org-make-link cpltxt)))
  5690. ((and (featurep 'org-id)
  5691. (or (eq org-link-to-org-use-id t)
  5692. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5693. (interactive-p))
  5694. (and org-link-to-org-use-id
  5695. (condition-case nil
  5696. (org-entry-get nil "ID")
  5697. (error nil)))))
  5698. ;; We can make a link using the ID.
  5699. (setq link (condition-case nil
  5700. (prog1 (org-id-store-link)
  5701. (setq desc (plist-get org-store-link-plist
  5702. :description)))
  5703. (error
  5704. ;; probably before first headline, link to file only
  5705. (concat "file:"
  5706. (abbreviate-file-name buffer-file-name))))))
  5707. (t
  5708. ;; Just link to current headline
  5709. (setq cpltxt (concat "file:"
  5710. (abbreviate-file-name buffer-file-name)))
  5711. ;; Add a context search string
  5712. (when (org-xor org-context-in-file-links arg)
  5713. (setq txt (cond
  5714. ((org-on-heading-p) nil)
  5715. ((org-region-active-p)
  5716. (buffer-substring (region-beginning) (region-end)))
  5717. (t nil)))
  5718. (when (or (null txt) (string-match "\\S-" txt))
  5719. (setq cpltxt
  5720. (concat cpltxt "::"
  5721. (condition-case nil
  5722. (org-make-org-heading-search-string txt)
  5723. (error "")))
  5724. desc "NONE")))
  5725. (if (string-match "::\\'" cpltxt)
  5726. (setq cpltxt (substring cpltxt 0 -2)))
  5727. (setq link (org-make-link cpltxt)))))
  5728. ((buffer-file-name (buffer-base-buffer))
  5729. ;; Just link to this file here.
  5730. (setq cpltxt (concat "file:"
  5731. (abbreviate-file-name
  5732. (buffer-file-name (buffer-base-buffer)))))
  5733. ;; Add a context string
  5734. (when (org-xor org-context-in-file-links arg)
  5735. (setq txt (if (org-region-active-p)
  5736. (buffer-substring (region-beginning) (region-end))
  5737. (buffer-substring (point-at-bol) (point-at-eol))))
  5738. ;; Only use search option if there is some text.
  5739. (when (string-match "\\S-" txt)
  5740. (setq cpltxt
  5741. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5742. desc "NONE")))
  5743. (setq link (org-make-link cpltxt)))
  5744. ((interactive-p)
  5745. (error "Cannot link to a buffer which is not visiting a file"))
  5746. (t (setq link nil)))
  5747. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5748. (setq link (or link cpltxt)
  5749. desc (or desc cpltxt))
  5750. (if (equal desc "NONE") (setq desc nil))
  5751. (if (and (interactive-p) link)
  5752. (progn
  5753. (setq org-stored-links
  5754. (cons (list link desc) org-stored-links))
  5755. (message "Stored: %s" (or desc link)))
  5756. (and link (org-make-link-string link desc)))))
  5757. (defun org-store-link-props (&rest plist)
  5758. "Store link properties, extract names and addresses."
  5759. (let (x adr)
  5760. (when (setq x (plist-get plist :from))
  5761. (setq adr (mail-extract-address-components x))
  5762. (setq plist (plist-put plist :fromname (car adr)))
  5763. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5764. (when (setq x (plist-get plist :to))
  5765. (setq adr (mail-extract-address-components x))
  5766. (setq plist (plist-put plist :toname (car adr)))
  5767. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5768. (let ((from (plist-get plist :from))
  5769. (to (plist-get plist :to)))
  5770. (when (and from to org-from-is-user-regexp)
  5771. (setq plist
  5772. (plist-put plist :fromto
  5773. (if (string-match org-from-is-user-regexp from)
  5774. (concat "to %t")
  5775. (concat "from %f"))))))
  5776. (setq org-store-link-plist plist))
  5777. (defun org-add-link-props (&rest plist)
  5778. "Add these properties to the link property list."
  5779. (let (key value)
  5780. (while plist
  5781. (setq key (pop plist) value (pop plist))
  5782. (setq org-store-link-plist
  5783. (plist-put org-store-link-plist key value)))))
  5784. (defun org-email-link-description (&optional fmt)
  5785. "Return the description part of an email link.
  5786. This takes information from `org-store-link-plist' and formats it
  5787. according to FMT (default from `org-email-link-description-format')."
  5788. (setq fmt (or fmt org-email-link-description-format))
  5789. (let* ((p org-store-link-plist)
  5790. (to (plist-get p :toaddress))
  5791. (from (plist-get p :fromaddress))
  5792. (table
  5793. (list
  5794. (cons "%c" (plist-get p :fromto))
  5795. (cons "%F" (plist-get p :from))
  5796. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5797. (cons "%T" (plist-get p :to))
  5798. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5799. (cons "%s" (plist-get p :subject))
  5800. (cons "%m" (plist-get p :message-id)))))
  5801. (when (string-match "%c" fmt)
  5802. ;; Check if the user wrote this message
  5803. (if (and org-from-is-user-regexp from to
  5804. (save-match-data (string-match org-from-is-user-regexp from)))
  5805. (setq fmt (replace-match "to %t" t t fmt))
  5806. (setq fmt (replace-match "from %f" t t fmt))))
  5807. (org-replace-escapes fmt table)))
  5808. (defun org-make-org-heading-search-string (&optional string heading)
  5809. "Make search string for STRING or current headline."
  5810. (interactive)
  5811. (let ((s (or string (org-get-heading))))
  5812. (unless (and string (not heading))
  5813. ;; We are using a headline, clean up garbage in there.
  5814. (if (string-match org-todo-regexp s)
  5815. (setq s (replace-match "" t t s)))
  5816. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5817. (setq s (replace-match "" t t s)))
  5818. (setq s (org-trim s))
  5819. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5820. org-comment-string "\\)") s)
  5821. (setq s (replace-match "" t t s)))
  5822. (while (string-match org-ts-regexp s)
  5823. (setq s (replace-match "" t t s))))
  5824. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5825. (setq s (replace-match " " t t s)))
  5826. (or string (setq s (concat "*" s))) ; Add * for headlines
  5827. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5828. (defun org-make-link (&rest strings)
  5829. "Concatenate STRINGS."
  5830. (apply 'concat strings))
  5831. (defun org-make-link-string (link &optional description)
  5832. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5833. (unless (string-match "\\S-" link)
  5834. (error "Empty link"))
  5835. (when (stringp description)
  5836. ;; Remove brackets from the description, they are fatal.
  5837. (while (string-match "\\[" description)
  5838. (setq description (replace-match "{" t t description)))
  5839. (while (string-match "\\]" description)
  5840. (setq description (replace-match "}" t t description))))
  5841. (when (equal (org-link-escape link) description)
  5842. ;; No description needed, it is identical
  5843. (setq description nil))
  5844. (when (and (not description)
  5845. (not (equal link (org-link-escape link))))
  5846. (setq description (org-extract-attributes link)))
  5847. (concat "[[" (org-link-escape link) "]"
  5848. (if description (concat "[" description "]") "")
  5849. "]"))
  5850. (defconst org-link-escape-chars
  5851. '((?\ . "%20")
  5852. (?\[ . "%5B")
  5853. (?\] . "%5D")
  5854. (?\340 . "%E0") ; `a
  5855. (?\342 . "%E2") ; ^a
  5856. (?\347 . "%E7") ; ,c
  5857. (?\350 . "%E8") ; `e
  5858. (?\351 . "%E9") ; 'e
  5859. (?\352 . "%EA") ; ^e
  5860. (?\356 . "%EE") ; ^i
  5861. (?\364 . "%F4") ; ^o
  5862. (?\371 . "%F9") ; `u
  5863. (?\373 . "%FB") ; ^u
  5864. (?\; . "%3B")
  5865. (?? . "%3F")
  5866. (?= . "%3D")
  5867. (?+ . "%2B")
  5868. )
  5869. "Association list of escapes for some characters problematic in links.
  5870. This is the list that is used for internal purposes.")
  5871. (defconst org-link-escape-chars-browser
  5872. '((?\ . "%20")) ; 32 for the SPC char
  5873. "Association list of escapes for some characters problematic in links.
  5874. This is the list that is used before handing over to the browser.")
  5875. (defun org-link-escape (text &optional table)
  5876. "Escape characters in TEXT that are problematic for links."
  5877. (setq table (or table org-link-escape-chars))
  5878. (when text
  5879. (let ((re (mapconcat (lambda (x) (regexp-quote
  5880. (char-to-string (car x))))
  5881. table "\\|")))
  5882. (while (string-match re text)
  5883. (setq text
  5884. (replace-match
  5885. (cdr (assoc (string-to-char (match-string 0 text))
  5886. table))
  5887. t t text)))
  5888. text)))
  5889. (defun org-link-unescape (text &optional table)
  5890. "Reverse the action of `org-link-escape'."
  5891. (setq table (or table org-link-escape-chars))
  5892. (when text
  5893. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5894. table "\\|")))
  5895. (while (string-match re text)
  5896. (setq text
  5897. (replace-match
  5898. (char-to-string (car (rassoc (match-string 0 text) table)))
  5899. t t text)))
  5900. text)))
  5901. (defun org-xor (a b)
  5902. "Exclusive or."
  5903. (if a (not b) b))
  5904. (defun org-get-header (header)
  5905. "Find a header field in the current buffer."
  5906. (save-excursion
  5907. (goto-char (point-min))
  5908. (let ((case-fold-search t) s)
  5909. (cond
  5910. ((eq header 'from)
  5911. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5912. (setq s (match-string 1)))
  5913. (while (string-match "\"" s)
  5914. (setq s (replace-match "" t t s)))
  5915. (if (string-match "[<(].*" s)
  5916. (setq s (replace-match "" t t s))))
  5917. ((eq header 'message-id)
  5918. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5919. (setq s (match-string 1))))
  5920. ((eq header 'subject)
  5921. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5922. (setq s (match-string 1)))))
  5923. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5924. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5925. s)))
  5926. (defun org-fixup-message-id-for-http (s)
  5927. "Replace special characters in a message id, so it can be used in an http query."
  5928. (while (string-match "<" s)
  5929. (setq s (replace-match "%3C" t t s)))
  5930. (while (string-match ">" s)
  5931. (setq s (replace-match "%3E" t t s)))
  5932. (while (string-match "@" s)
  5933. (setq s (replace-match "%40" t t s)))
  5934. s)
  5935. ;;;###autoload
  5936. (defun org-insert-link-global ()
  5937. "Insert a link like Org-mode does.
  5938. This command can be called in any mode to insert a link in Org-mode syntax."
  5939. (interactive)
  5940. (org-load-modules-maybe)
  5941. (org-run-like-in-org-mode 'org-insert-link))
  5942. (defun org-insert-link (&optional complete-file link-location)
  5943. "Insert a link. At the prompt, enter the link.
  5944. Completion can be used to insert any of the link protocol prefixes like
  5945. http or ftp in use.
  5946. The history can be used to select a link previously stored with
  5947. `org-store-link'. When the empty string is entered (i.e. if you just
  5948. press RET at the prompt), the link defaults to the most recently
  5949. stored link. As SPC triggers completion in the minibuffer, you need to
  5950. use M-SPC or C-q SPC to force the insertion of a space character.
  5951. You will also be prompted for a description, and if one is given, it will
  5952. be displayed in the buffer instead of the link.
  5953. If there is already a link at point, this command will allow you to edit link
  5954. and description parts.
  5955. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5956. be selected using completion. The path to the file will be relative to the
  5957. current directory if the file is in the current directory or a subdirectory.
  5958. Otherwise, the link will be the absolute path as completed in the minibuffer
  5959. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5960. option `org-link-file-path-type'.
  5961. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5962. the current directory or below.
  5963. With three \\[universal-argument] prefixes, negate the meaning of
  5964. `org-keep-stored-link-after-insertion'.
  5965. If `org-make-link-description-function' is non-nil, this function will be
  5966. called with the link target, and the result will be the default
  5967. link description.
  5968. If the LINK-LOCATION parameter is non-nil, this value will be
  5969. used as the link location instead of reading one interactively."
  5970. (interactive "P")
  5971. (let* ((wcf (current-window-configuration))
  5972. (region (if (org-region-active-p)
  5973. (buffer-substring (region-beginning) (region-end))))
  5974. (remove (and region (list (region-beginning) (region-end))))
  5975. (desc region)
  5976. tmphist ; byte-compile incorrectly complains about this
  5977. (link link-location)
  5978. entry file)
  5979. (cond
  5980. (link-location) ; specified by arg, just use it.
  5981. ((org-in-regexp org-bracket-link-regexp 1)
  5982. ;; We do have a link at point, and we are going to edit it.
  5983. (setq remove (list (match-beginning 0) (match-end 0)))
  5984. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5985. (setq link (read-string "Link: "
  5986. (org-link-unescape
  5987. (org-match-string-no-properties 1)))))
  5988. ((or (org-in-regexp org-angle-link-re)
  5989. (org-in-regexp org-plain-link-re))
  5990. ;; Convert to bracket link
  5991. (setq remove (list (match-beginning 0) (match-end 0))
  5992. link (read-string "Link: "
  5993. (org-remove-angle-brackets (match-string 0)))))
  5994. ((member complete-file '((4) (16)))
  5995. ;; Completing read for file names.
  5996. (setq file (read-file-name "File: "))
  5997. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5998. (pwd1 (file-name-as-directory (abbreviate-file-name
  5999. (expand-file-name ".")))))
  6000. (cond
  6001. ((equal complete-file '(16))
  6002. (setq link (org-make-link
  6003. "file:"
  6004. (abbreviate-file-name (expand-file-name file)))))
  6005. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6006. (setq link (org-make-link "file:" (match-string 1 file))))
  6007. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6008. (expand-file-name file))
  6009. (setq link (org-make-link
  6010. "file:" (match-string 1 (expand-file-name file)))))
  6011. (t (setq link (org-make-link "file:" file))))))
  6012. (t
  6013. ;; Read link, with completion for stored links.
  6014. (with-output-to-temp-buffer "*Org Links*"
  6015. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6016. (when org-stored-links
  6017. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6018. (princ (mapconcat
  6019. (lambda (x)
  6020. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6021. (reverse org-stored-links) "\n"))))
  6022. (let ((cw (selected-window)))
  6023. (select-window (get-buffer-window "*Org Links*"))
  6024. (setq truncate-lines t)
  6025. (org-fit-window-to-buffer)
  6026. (select-window cw))
  6027. ;; Fake a link history, containing the stored links.
  6028. (setq tmphist (append (mapcar 'car org-stored-links)
  6029. org-insert-link-history))
  6030. (unwind-protect
  6031. (setq link
  6032. (let ((org-completion-use-ido nil))
  6033. (org-completing-read
  6034. "Link: "
  6035. (append
  6036. (mapcar (lambda (x) (list (concat (car x) ":")))
  6037. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6038. (mapcar (lambda (x) (list (concat x ":")))
  6039. org-link-types))
  6040. nil nil nil
  6041. 'tmphist
  6042. (or (car (car org-stored-links))))))
  6043. (set-window-configuration wcf)
  6044. (kill-buffer "*Org Links*"))
  6045. (setq entry (assoc link org-stored-links))
  6046. (or entry (push link org-insert-link-history))
  6047. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6048. (not org-keep-stored-link-after-insertion))
  6049. (setq org-stored-links (delq (assoc link org-stored-links)
  6050. org-stored-links)))
  6051. (setq desc (or desc (nth 1 entry)))))
  6052. (if (string-match org-plain-link-re link)
  6053. ;; URL-like link, normalize the use of angular brackets.
  6054. (setq link (org-make-link (org-remove-angle-brackets link))))
  6055. ;; Check if we are linking to the current file with a search option
  6056. ;; If yes, simplify the link by using only the search option.
  6057. (when (and buffer-file-name
  6058. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  6059. (let* ((path (match-string 1 link))
  6060. (case-fold-search nil)
  6061. (search (match-string 2 link)))
  6062. (save-match-data
  6063. (if (equal (file-truename buffer-file-name) (file-truename path))
  6064. ;; We are linking to this same file, with a search option
  6065. (setq link search)))))
  6066. ;; Check if we can/should use a relative path. If yes, simplify the link
  6067. (when (string-match "^file:\\(.*\\)" link)
  6068. (let* ((path (match-string 1 link))
  6069. (origpath path)
  6070. (case-fold-search nil))
  6071. (cond
  6072. ((or (eq org-link-file-path-type 'absolute)
  6073. (equal complete-file '(16)))
  6074. (setq path (abbreviate-file-name (expand-file-name path))))
  6075. ((eq org-link-file-path-type 'noabbrev)
  6076. (setq path (expand-file-name path)))
  6077. ((eq org-link-file-path-type 'relative)
  6078. (setq path (file-relative-name path)))
  6079. (t
  6080. (save-match-data
  6081. (if (string-match (concat "^" (regexp-quote
  6082. (file-name-as-directory
  6083. (expand-file-name "."))))
  6084. (expand-file-name path))
  6085. ;; We are linking a file with relative path name.
  6086. (setq path (substring (expand-file-name path)
  6087. (match-end 0)))
  6088. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6089. (setq link (concat "file:" path))
  6090. (if (equal desc origpath)
  6091. (setq desc path))))
  6092. (if org-make-link-description-function
  6093. (setq desc (funcall org-make-link-description-function link desc)))
  6094. (setq desc (read-string "Description: " desc))
  6095. (unless (string-match "\\S-" desc) (setq desc nil))
  6096. (if remove (apply 'delete-region remove))
  6097. (insert (org-make-link-string link desc))))
  6098. (defun org-completing-read (&rest args)
  6099. "Completing-read with SPACE being a normal character."
  6100. (let ((minibuffer-local-completion-map
  6101. (copy-keymap minibuffer-local-completion-map)))
  6102. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6103. (apply 'org-ido-completing-read args)))
  6104. (defun org-ido-completing-read (&rest args)
  6105. "Completing-read using `ido-mode' speedups if available"
  6106. (if (and org-completion-use-ido
  6107. (fboundp 'ido-completing-read)
  6108. (boundp 'ido-mode) ido-mode
  6109. (listp (second args)))
  6110. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6111. (apply 'completing-read args)))
  6112. (defun org-extract-attributes (s)
  6113. "Extract the attributes cookie from a string and set as text property."
  6114. (let (a attr (start 0) key value)
  6115. (save-match-data
  6116. (when (string-match "{{\\([^}]+\\)}}$" s)
  6117. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6118. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6119. (setq key (match-string 1 a) value (match-string 2 a)
  6120. start (match-end 0)
  6121. attr (plist-put attr (intern key) value))))
  6122. (org-add-props s nil 'org-attr attr))
  6123. s))
  6124. (defun org-attributes-to-string (plist)
  6125. "Format a property list into an HTML attribute list."
  6126. (let ((s "") key value)
  6127. (while plist
  6128. (setq key (pop plist) value (pop plist))
  6129. (and value
  6130. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6131. s))
  6132. ;;; Opening/following a link
  6133. (defvar org-link-search-failed nil)
  6134. (defun org-next-link ()
  6135. "Move forward to the next link.
  6136. If the link is in hidden text, expose it."
  6137. (interactive)
  6138. (when (and org-link-search-failed (eq this-command last-command))
  6139. (goto-char (point-min))
  6140. (message "Link search wrapped back to beginning of buffer"))
  6141. (setq org-link-search-failed nil)
  6142. (let* ((pos (point))
  6143. (ct (org-context))
  6144. (a (assoc :link ct)))
  6145. (if a (goto-char (nth 2 a)))
  6146. (if (re-search-forward org-any-link-re nil t)
  6147. (progn
  6148. (goto-char (match-beginning 0))
  6149. (if (org-invisible-p) (org-show-context)))
  6150. (goto-char pos)
  6151. (setq org-link-search-failed t)
  6152. (error "No further link found"))))
  6153. (defun org-previous-link ()
  6154. "Move backward to the previous link.
  6155. If the link is in hidden text, expose it."
  6156. (interactive)
  6157. (when (and org-link-search-failed (eq this-command last-command))
  6158. (goto-char (point-max))
  6159. (message "Link search wrapped back to end of buffer"))
  6160. (setq org-link-search-failed nil)
  6161. (let* ((pos (point))
  6162. (ct (org-context))
  6163. (a (assoc :link ct)))
  6164. (if a (goto-char (nth 1 a)))
  6165. (if (re-search-backward org-any-link-re nil t)
  6166. (progn
  6167. (goto-char (match-beginning 0))
  6168. (if (org-invisible-p) (org-show-context)))
  6169. (goto-char pos)
  6170. (setq org-link-search-failed t)
  6171. (error "No further link found"))))
  6172. (defun org-translate-link (s)
  6173. "Translate a link string if a translation function has been defined."
  6174. (if (and org-link-translation-function
  6175. (fboundp org-link-translation-function)
  6176. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6177. (progn
  6178. (setq s (funcall org-link-translation-function
  6179. (match-string 1) (match-string 2)))
  6180. (concat (car s) ":" (cdr s)))
  6181. s))
  6182. (defun org-translate-link-from-planner (type path)
  6183. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6184. This is still an experimental function, your mileage may vary."
  6185. (cond
  6186. ((member type '("http" "https" "news" "ftp"))
  6187. ;; standard Internet links are the same.
  6188. nil)
  6189. ((and (equal type "irc") (string-match "^//" path))
  6190. ;; Planner has two / at the beginning of an irc link, we have 1.
  6191. ;; We should have zero, actually....
  6192. (setq path (substring path 1)))
  6193. ((and (equal type "lisp") (string-match "^/" path))
  6194. ;; Planner has a slash, we do not.
  6195. (setq type "elisp" path (substring path 1)))
  6196. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6197. ;; A typical message link. Planner has the id after the fina slash,
  6198. ;; we separate it with a hash mark
  6199. (setq path (concat (match-string 1 path) "#"
  6200. (org-remove-angle-brackets (match-string 2 path)))))
  6201. )
  6202. (cons type path))
  6203. (defun org-find-file-at-mouse (ev)
  6204. "Open file link or URL at mouse."
  6205. (interactive "e")
  6206. (mouse-set-point ev)
  6207. (org-open-at-point 'in-emacs))
  6208. (defun org-open-at-mouse (ev)
  6209. "Open file link or URL at mouse."
  6210. (interactive "e")
  6211. (mouse-set-point ev)
  6212. (if (eq major-mode 'org-agenda-mode)
  6213. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6214. (org-open-at-point))
  6215. (defvar org-window-config-before-follow-link nil
  6216. "The window configuration before following a link.
  6217. This is saved in case the need arises to restore it.")
  6218. (defvar org-open-link-marker (make-marker)
  6219. "Marker pointing to the location where `org-open-at-point; was called.")
  6220. ;;;###autoload
  6221. (defun org-open-at-point-global ()
  6222. "Follow a link like Org-mode does.
  6223. This command can be called in any mode to follow a link that has
  6224. Org-mode syntax."
  6225. (interactive)
  6226. (org-run-like-in-org-mode 'org-open-at-point))
  6227. ;;;###autoload
  6228. (defun org-open-link-from-string (s &optional arg)
  6229. "Open a link in the string S, as if it was in Org-mode."
  6230. (interactive "sLink: \nP")
  6231. (with-temp-buffer
  6232. (let ((org-inhibit-startup t))
  6233. (org-mode)
  6234. (insert s)
  6235. (goto-char (point-min))
  6236. (org-open-at-point arg))))
  6237. (defun org-open-at-point (&optional in-emacs)
  6238. "Open link at or after point.
  6239. If there is no link at point, this function will search forward up to
  6240. the end of the current subtree.
  6241. Normally, files will be opened by an appropriate application. If the
  6242. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6243. With a double prefix argument, try to open outside of Emacs, in the
  6244. application the system uses for this file type."
  6245. (interactive "P")
  6246. (org-load-modules-maybe)
  6247. (move-marker org-open-link-marker (point))
  6248. (setq org-window-config-before-follow-link (current-window-configuration))
  6249. (org-remove-occur-highlights nil nil t)
  6250. (cond
  6251. ((org-at-timestamp-p t) (org-follow-timestamp-link))
  6252. ((or (org-footnote-at-reference-p) (org-footnote-at-definition-p))
  6253. (org-footnote-action))
  6254. (t
  6255. (let (type path link line search (pos (point)))
  6256. (catch 'match
  6257. (save-excursion
  6258. (skip-chars-forward "^]\n\r")
  6259. (when (org-in-regexp org-bracket-link-regexp)
  6260. (setq link (org-extract-attributes
  6261. (org-link-unescape (org-match-string-no-properties 1))))
  6262. (while (string-match " *\n *" link)
  6263. (setq link (replace-match " " t t link)))
  6264. (setq link (org-link-expand-abbrev link))
  6265. (cond
  6266. ((or (file-name-absolute-p link)
  6267. (string-match "^\\.\\.?/" link))
  6268. (setq type "file" path link))
  6269. ((string-match org-link-re-with-space3 link)
  6270. (setq type (match-string 1 link) path (match-string 2 link)))
  6271. (t (setq type "thisfile" path link)))
  6272. (throw 'match t)))
  6273. (when (get-text-property (point) 'org-linked-text)
  6274. (setq type "thisfile"
  6275. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6276. (1+ (point)) (point))
  6277. path (buffer-substring
  6278. (previous-single-property-change pos 'org-linked-text)
  6279. (next-single-property-change pos 'org-linked-text)))
  6280. (throw 'match t))
  6281. (save-excursion
  6282. (when (or (org-in-regexp org-angle-link-re)
  6283. (org-in-regexp org-plain-link-re))
  6284. (setq type (match-string 1) path (match-string 2))
  6285. (throw 'match t)))
  6286. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6287. (setq type "tree-match"
  6288. path (match-string 1))
  6289. (throw 'match t))
  6290. (save-excursion
  6291. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6292. (setq type "tags"
  6293. path (match-string 1))
  6294. (while (string-match ":" path)
  6295. (setq path (replace-match "+" t t path)))
  6296. (throw 'match t))))
  6297. (unless path
  6298. (error "No link found"))
  6299. ;; Remove any trailing spaces in path
  6300. (if (string-match " +\\'" path)
  6301. (setq path (replace-match "" t t path)))
  6302. (if (and org-link-translation-function
  6303. (fboundp org-link-translation-function))
  6304. ;; Check if we need to translate the link
  6305. (let ((tmp (funcall org-link-translation-function type path)))
  6306. (setq type (car tmp) path (cdr tmp))))
  6307. (cond
  6308. ((assoc type org-link-protocols)
  6309. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6310. ((equal type "mailto")
  6311. (let ((cmd (car org-link-mailto-program))
  6312. (args (cdr org-link-mailto-program)) args1
  6313. (address path) (subject "") a)
  6314. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6315. (setq address (match-string 1 path)
  6316. subject (org-link-escape (match-string 2 path))))
  6317. (while args
  6318. (cond
  6319. ((not (stringp (car args))) (push (pop args) args1))
  6320. (t (setq a (pop args))
  6321. (if (string-match "%a" a)
  6322. (setq a (replace-match address t t a)))
  6323. (if (string-match "%s" a)
  6324. (setq a (replace-match subject t t a)))
  6325. (push a args1))))
  6326. (apply cmd (nreverse args1))))
  6327. ((member type '("http" "https" "ftp" "news"))
  6328. (browse-url (concat type ":" (org-link-escape
  6329. path org-link-escape-chars-browser))))
  6330. ((member type '("message"))
  6331. (browse-url (concat type ":" path)))
  6332. ((string= type "tags")
  6333. (org-tags-view in-emacs path))
  6334. ((string= type "thisfile")
  6335. (if in-emacs
  6336. (switch-to-buffer-other-window
  6337. (org-get-buffer-for-internal-link (current-buffer)))
  6338. (org-mark-ring-push))
  6339. (let ((cmd `(org-link-search
  6340. ,path
  6341. ,(cond ((equal in-emacs '(4)) 'occur)
  6342. ((equal in-emacs '(16)) 'org-occur)
  6343. (t nil))
  6344. ,pos)))
  6345. (condition-case nil (eval cmd)
  6346. (error (progn (widen) (eval cmd))))))
  6347. ((string= type "tree-match")
  6348. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6349. ((string= type "file")
  6350. (if (string-match "::\\([0-9]+\\)\\'" path)
  6351. (setq line (string-to-number (match-string 1 path))
  6352. path (substring path 0 (match-beginning 0)))
  6353. (if (string-match "::\\(.+\\)\\'" path)
  6354. (setq search (match-string 1 path)
  6355. path (substring path 0 (match-beginning 0)))))
  6356. (if (string-match "[*?{]" (file-name-nondirectory path))
  6357. (dired path)
  6358. (org-open-file path in-emacs line search)))
  6359. ((string= type "news")
  6360. (require 'org-gnus)
  6361. (org-gnus-follow-link path))
  6362. ((string= type "shell")
  6363. (let ((cmd path))
  6364. (if (or (not org-confirm-shell-link-function)
  6365. (funcall org-confirm-shell-link-function
  6366. (format "Execute \"%s\" in shell? "
  6367. (org-add-props cmd nil
  6368. 'face 'org-warning))))
  6369. (progn
  6370. (message "Executing %s" cmd)
  6371. (shell-command cmd))
  6372. (error "Abort"))))
  6373. ((string= type "elisp")
  6374. (let ((cmd path))
  6375. (if (or (not org-confirm-elisp-link-function)
  6376. (funcall org-confirm-elisp-link-function
  6377. (format "Execute \"%s\" as elisp? "
  6378. (org-add-props cmd nil
  6379. 'face 'org-warning))))
  6380. (message "%s => %s" cmd
  6381. (if (equal (string-to-char cmd) ?\()
  6382. (eval (read cmd))
  6383. (call-interactively (read cmd))))
  6384. (error "Abort"))))
  6385. (t
  6386. (browse-url-at-point))))))
  6387. (move-marker org-open-link-marker nil)
  6388. (run-hook-with-args 'org-follow-link-hook))
  6389. ;;;; Time estimates
  6390. (defun org-get-effort (&optional pom)
  6391. "Get the effort estimate for the current entry."
  6392. (org-entry-get pom org-effort-property))
  6393. ;;; File search
  6394. (defvar org-create-file-search-functions nil
  6395. "List of functions to construct the right search string for a file link.
  6396. These functions are called in turn with point at the location to
  6397. which the link should point.
  6398. A function in the hook should first test if it would like to
  6399. handle this file type, for example by checking the major-mode or
  6400. the file extension. If it decides not to handle this file, it
  6401. should just return nil to give other functions a chance. If it
  6402. does handle the file, it must return the search string to be used
  6403. when following the link. The search string will be part of the
  6404. file link, given after a double colon, and `org-open-at-point'
  6405. will automatically search for it. If special measures must be
  6406. taken to make the search successful, another function should be
  6407. added to the companion hook `org-execute-file-search-functions',
  6408. which see.
  6409. A function in this hook may also use `setq' to set the variable
  6410. `description' to provide a suggestion for the descriptive text to
  6411. be used for this link when it gets inserted into an Org-mode
  6412. buffer with \\[org-insert-link].")
  6413. (defvar org-execute-file-search-functions nil
  6414. "List of functions to execute a file search triggered by a link.
  6415. Functions added to this hook must accept a single argument, the
  6416. search string that was part of the file link, the part after the
  6417. double colon. The function must first check if it would like to
  6418. handle this search, for example by checking the major-mode or the
  6419. file extension. If it decides not to handle this search, it
  6420. should just return nil to give other functions a chance. If it
  6421. does handle the search, it must return a non-nil value to keep
  6422. other functions from trying.
  6423. Each function can access the current prefix argument through the
  6424. variable `current-prefix-argument'. Note that a single prefix is
  6425. used to force opening a link in Emacs, so it may be good to only
  6426. use a numeric or double prefix to guide the search function.
  6427. In case this is needed, a function in this hook can also restore
  6428. the window configuration before `org-open-at-point' was called using:
  6429. (set-window-configuration org-window-config-before-follow-link)")
  6430. (defun org-link-search (s &optional type avoid-pos)
  6431. "Search for a link search option.
  6432. If S is surrounded by forward slashes, it is interpreted as a
  6433. regular expression. In org-mode files, this will create an `org-occur'
  6434. sparse tree. In ordinary files, `occur' will be used to list matches.
  6435. If the current buffer is in `dired-mode', grep will be used to search
  6436. in all files. If AVOID-POS is given, ignore matches near that position."
  6437. (let ((case-fold-search t)
  6438. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6439. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6440. (append '(("") (" ") ("\t") ("\n"))
  6441. org-emphasis-alist)
  6442. "\\|") "\\)"))
  6443. (pos (point))
  6444. (pre nil) (post nil)
  6445. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6446. (cond
  6447. ;; First check if there are any special
  6448. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6449. ;; Now try the builtin stuff
  6450. ((save-excursion
  6451. (goto-char (point-min))
  6452. (and
  6453. (re-search-forward
  6454. (concat "<<" (regexp-quote s0) ">>") nil t)
  6455. (setq type 'dedicated
  6456. pos (match-beginning 0))))
  6457. ;; There is an exact target for this
  6458. (goto-char pos))
  6459. ((and (string-match "^((.*))$" s0)
  6460. (save-excursion
  6461. (goto-char (point-min))
  6462. (and
  6463. (re-search-forward (concat "[^[]" (regexp-quote s0)) nil t)
  6464. (setq type 'dedicated
  6465. pos (1+ (match-beginning 0))))))
  6466. ;; There is a coderef target for this
  6467. (goto-char pos))
  6468. ((string-match "^/\\(.*\\)/$" s)
  6469. ;; A regular expression
  6470. (cond
  6471. ((org-mode-p)
  6472. (org-occur (match-string 1 s)))
  6473. ;;((eq major-mode 'dired-mode)
  6474. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6475. (t (org-do-occur (match-string 1 s)))))
  6476. (t
  6477. ;; A normal search strings
  6478. (when (equal (string-to-char s) ?*)
  6479. ;; Anchor on headlines, post may include tags.
  6480. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6481. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6482. s (substring s 1)))
  6483. (remove-text-properties
  6484. 0 (length s)
  6485. '(face nil mouse-face nil keymap nil fontified nil) s)
  6486. ;; Make a series of regular expressions to find a match
  6487. (setq words (org-split-string s "[ \n\r\t]+")
  6488. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6489. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6490. "\\)" markers)
  6491. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6492. re2a (concat "[ \t\r\n]" re2a_)
  6493. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6494. re4 (concat "[^a-zA-Z_]" re4_)
  6495. re1 (concat pre re2 post)
  6496. re3 (concat pre (if pre re4_ re4) post)
  6497. re5 (concat pre ".*" re4)
  6498. re2 (concat pre re2)
  6499. re2a (concat pre (if pre re2a_ re2a))
  6500. re4 (concat pre (if pre re4_ re4))
  6501. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6502. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6503. re5 "\\)"
  6504. ))
  6505. (cond
  6506. ((eq type 'org-occur) (org-occur reall))
  6507. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6508. (t (goto-char (point-min))
  6509. (setq type 'fuzzy)
  6510. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6511. (org-search-not-self 1 re1 nil t)
  6512. (org-search-not-self 1 re2 nil t)
  6513. (org-search-not-self 1 re2a nil t)
  6514. (org-search-not-self 1 re3 nil t)
  6515. (org-search-not-self 1 re4 nil t)
  6516. (org-search-not-self 1 re5 nil t)
  6517. )
  6518. (goto-char (match-beginning 1))
  6519. (goto-char pos)
  6520. (error "No match")))))
  6521. (t
  6522. ;; Normal string-search
  6523. (goto-char (point-min))
  6524. (if (search-forward s nil t)
  6525. (goto-char (match-beginning 0))
  6526. (error "No match"))))
  6527. (and (org-mode-p) (org-show-context 'link-search))
  6528. type))
  6529. (defun org-search-not-self (group &rest args)
  6530. "Execute `re-search-forward', but only accept matches that do not
  6531. enclose the position of `org-open-link-marker'."
  6532. (let ((m org-open-link-marker))
  6533. (catch 'exit
  6534. (while (apply 're-search-forward args)
  6535. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6536. (goto-char (match-end group))
  6537. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6538. (> (match-beginning 0) (marker-position m))
  6539. (< (match-end 0) (marker-position m)))
  6540. (save-match-data
  6541. (or (not (org-in-regexp
  6542. org-bracket-link-analytic-regexp 1))
  6543. (not (match-end 4)) ; no description
  6544. (and (<= (match-beginning 4) (point))
  6545. (>= (match-end 4) (point))))))
  6546. (throw 'exit (point))))))))
  6547. (defun org-get-buffer-for-internal-link (buffer)
  6548. "Return a buffer to be used for displaying the link target of internal links."
  6549. (cond
  6550. ((not org-display-internal-link-with-indirect-buffer)
  6551. buffer)
  6552. ((string-match "(Clone)$" (buffer-name buffer))
  6553. (message "Buffer is already a clone, not making another one")
  6554. ;; we also do not modify visibility in this case
  6555. buffer)
  6556. (t ; make a new indirect buffer for displaying the link
  6557. (let* ((bn (buffer-name buffer))
  6558. (ibn (concat bn "(Clone)"))
  6559. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6560. (with-current-buffer ib (org-overview))
  6561. ib))))
  6562. (defun org-do-occur (regexp &optional cleanup)
  6563. "Call the Emacs command `occur'.
  6564. If CLEANUP is non-nil, remove the printout of the regular expression
  6565. in the *Occur* buffer. This is useful if the regex is long and not useful
  6566. to read."
  6567. (occur regexp)
  6568. (when cleanup
  6569. (let ((cwin (selected-window)) win beg end)
  6570. (when (setq win (get-buffer-window "*Occur*"))
  6571. (select-window win))
  6572. (goto-char (point-min))
  6573. (when (re-search-forward "match[a-z]+" nil t)
  6574. (setq beg (match-end 0))
  6575. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6576. (setq end (1- (match-beginning 0)))))
  6577. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6578. (goto-char (point-min))
  6579. (select-window cwin))))
  6580. ;;; The mark ring for links jumps
  6581. (defvar org-mark-ring nil
  6582. "Mark ring for positions before jumps in Org-mode.")
  6583. (defvar org-mark-ring-last-goto nil
  6584. "Last position in the mark ring used to go back.")
  6585. ;; Fill and close the ring
  6586. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6587. (loop for i from 1 to org-mark-ring-length do
  6588. (push (make-marker) org-mark-ring))
  6589. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6590. org-mark-ring)
  6591. (defun org-mark-ring-push (&optional pos buffer)
  6592. "Put the current position or POS into the mark ring and rotate it."
  6593. (interactive)
  6594. (setq pos (or pos (point)))
  6595. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6596. (move-marker (car org-mark-ring)
  6597. (or pos (point))
  6598. (or buffer (current-buffer)))
  6599. (message "%s"
  6600. (substitute-command-keys
  6601. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6602. (defun org-mark-ring-goto (&optional n)
  6603. "Jump to the previous position in the mark ring.
  6604. With prefix arg N, jump back that many stored positions. When
  6605. called several times in succession, walk through the entire ring.
  6606. Org-mode commands jumping to a different position in the current file,
  6607. or to another Org-mode file, automatically push the old position
  6608. onto the ring."
  6609. (interactive "p")
  6610. (let (p m)
  6611. (if (eq last-command this-command)
  6612. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6613. (setq p org-mark-ring))
  6614. (setq org-mark-ring-last-goto p)
  6615. (setq m (car p))
  6616. (switch-to-buffer (marker-buffer m))
  6617. (goto-char m)
  6618. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6619. (defun org-remove-angle-brackets (s)
  6620. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6621. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6622. s)
  6623. (defun org-add-angle-brackets (s)
  6624. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6625. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6626. s)
  6627. (defun org-remove-double-quotes (s)
  6628. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6629. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6630. s)
  6631. ;;; Following specific links
  6632. (defun org-follow-timestamp-link ()
  6633. (cond
  6634. ((org-at-date-range-p t)
  6635. (let ((org-agenda-start-on-weekday)
  6636. (t1 (match-string 1))
  6637. (t2 (match-string 2)))
  6638. (setq t1 (time-to-days (org-time-string-to-time t1))
  6639. t2 (time-to-days (org-time-string-to-time t2)))
  6640. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6641. ((org-at-timestamp-p t)
  6642. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6643. (substring (match-string 1) 0 10)))
  6644. 1))
  6645. (t (error "This should not happen"))))
  6646. ;;; Following file links
  6647. (defvar org-wait nil)
  6648. (defun org-open-file (path &optional in-emacs line search)
  6649. "Open the file at PATH.
  6650. First, this expands any special file name abbreviations. Then the
  6651. configuration variable `org-file-apps' is checked if it contains an
  6652. entry for this file type, and if yes, the corresponding command is launched.
  6653. If no application is found, Emacs simply visits the file.
  6654. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6655. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6656. and o use an external application to visit the file.
  6657. Optional LINE specifies a line to go to, optional SEARCH a string to
  6658. search for. If LINE or SEARCH is given, the file will always be
  6659. opened in Emacs.
  6660. If the file does not exist, an error is thrown."
  6661. (setq in-emacs (or in-emacs line search))
  6662. (let* ((file (if (equal path "")
  6663. buffer-file-name
  6664. (substitute-in-file-name (expand-file-name path))))
  6665. (apps (append org-file-apps (org-default-apps)))
  6666. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6667. (dirp (if remp nil (file-directory-p file)))
  6668. (file (if (and dirp org-open-directory-means-index-dot-org)
  6669. (concat (file-name-as-directory file) "index.org")
  6670. file))
  6671. (a-m-a-p (assq 'auto-mode apps))
  6672. (dfile (downcase file))
  6673. (old-buffer (current-buffer))
  6674. (old-pos (point))
  6675. (old-mode major-mode)
  6676. ext cmd)
  6677. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6678. (setq ext (match-string 1 dfile))
  6679. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6680. (setq ext (match-string 1 dfile))))
  6681. (cond
  6682. ((equal in-emacs '(16))
  6683. (setq cmd (cdr (assoc 'system apps))))
  6684. (in-emacs (setq cmd 'emacs))
  6685. (t
  6686. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6687. (and dirp (cdr (assoc 'directory apps)))
  6688. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6689. 'string-match)
  6690. (cdr (assoc ext apps))
  6691. (cdr (assoc t apps))))))
  6692. (when (eq cmd 'system)
  6693. (setq cmd (cdr (assoc 'system apps))))
  6694. (when (eq cmd 'default)
  6695. (setq cmd (cdr (assoc t apps))))
  6696. (when (eq cmd 'mailcap)
  6697. (require 'mailcap)
  6698. (mailcap-parse-mailcaps)
  6699. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6700. (command (mailcap-mime-info mime-type)))
  6701. (if (stringp command)
  6702. (setq cmd command)
  6703. (setq cmd 'emacs))))
  6704. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6705. (not (file-exists-p file))
  6706. (not org-open-non-existing-files))
  6707. (error "No such file: %s" file))
  6708. (cond
  6709. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6710. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6711. (while (string-match "['\"]%s['\"]" cmd)
  6712. (setq cmd (replace-match "%s" t t cmd)))
  6713. (while (string-match "%s" cmd)
  6714. (setq cmd (replace-match
  6715. (save-match-data
  6716. (shell-quote-argument
  6717. (convert-standard-filename file)))
  6718. t t cmd)))
  6719. (save-window-excursion
  6720. (start-process-shell-command cmd nil cmd)
  6721. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6722. ))
  6723. ((or (stringp cmd)
  6724. (eq cmd 'emacs))
  6725. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6726. (widen)
  6727. (if line (goto-line line)
  6728. (if search (org-link-search search))))
  6729. ((consp cmd)
  6730. (let ((file (convert-standard-filename file)))
  6731. (eval cmd)))
  6732. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6733. (and (org-mode-p) (eq old-mode 'org-mode)
  6734. (or (not (equal old-buffer (current-buffer)))
  6735. (not (equal old-pos (point))))
  6736. (org-mark-ring-push old-pos old-buffer))))
  6737. (defun org-default-apps ()
  6738. "Return the default applications for this operating system."
  6739. (cond
  6740. ((eq system-type 'darwin)
  6741. org-file-apps-defaults-macosx)
  6742. ((eq system-type 'windows-nt)
  6743. org-file-apps-defaults-windowsnt)
  6744. (t org-file-apps-defaults-gnu)))
  6745. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6746. "Convert extensions to regular expressions in the cars of LIST.
  6747. Also, weed out any non-string entries, because the return value is used
  6748. only for regexp matching.
  6749. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6750. point to the symbol `emacs', indicating that the file should
  6751. be opened in Emacs."
  6752. (append
  6753. (delq nil
  6754. (mapcar (lambda (x)
  6755. (if (not (stringp (car x)))
  6756. nil
  6757. (if (string-match "\\W" (car x))
  6758. x
  6759. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6760. list))
  6761. (if add-auto-mode
  6762. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6763. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6764. (defun org-file-remote-p (file)
  6765. "Test whether FILE specifies a location on a remote system.
  6766. Return non-nil if the location is indeed remote.
  6767. For example, the filename \"/user@host:/foo\" specifies a location
  6768. on the system \"/user@host:\"."
  6769. (cond ((fboundp 'file-remote-p)
  6770. (file-remote-p file))
  6771. ((fboundp 'tramp-handle-file-remote-p)
  6772. (tramp-handle-file-remote-p file))
  6773. ((and (boundp 'ange-ftp-name-format)
  6774. (string-match (car ange-ftp-name-format) file))
  6775. t)
  6776. (t nil)))
  6777. ;;;; Refiling
  6778. (defun org-get-org-file ()
  6779. "Read a filename, with default directory `org-directory'."
  6780. (let ((default (or org-default-notes-file remember-data-file)))
  6781. (read-file-name (format "File name [%s]: " default)
  6782. (file-name-as-directory org-directory)
  6783. default)))
  6784. (defun org-notes-order-reversed-p ()
  6785. "Check if the current file should receive notes in reversed order."
  6786. (cond
  6787. ((not org-reverse-note-order) nil)
  6788. ((eq t org-reverse-note-order) t)
  6789. ((not (listp org-reverse-note-order)) nil)
  6790. (t (catch 'exit
  6791. (let ((all org-reverse-note-order)
  6792. entry)
  6793. (while (setq entry (pop all))
  6794. (if (string-match (car entry) buffer-file-name)
  6795. (throw 'exit (cdr entry))))
  6796. nil)))))
  6797. (defvar org-refile-target-table nil
  6798. "The list of refile targets, created by `org-refile'.")
  6799. (defvar org-agenda-new-buffers nil
  6800. "Buffers created to visit agenda files.")
  6801. (defun org-get-refile-targets (&optional default-buffer)
  6802. "Produce a table with refile targets."
  6803. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6804. targets txt re files f desc descre fast-path-p level)
  6805. (message "Getting targets...")
  6806. (with-current-buffer (or default-buffer (current-buffer))
  6807. (while (setq entry (pop entries))
  6808. (setq files (car entry) desc (cdr entry))
  6809. (setq fast-path-p nil)
  6810. (cond
  6811. ((null files) (setq files (list (current-buffer))))
  6812. ((eq files 'org-agenda-files)
  6813. (setq files (org-agenda-files 'unrestricted)))
  6814. ((and (symbolp files) (fboundp files))
  6815. (setq files (funcall files)))
  6816. ((and (symbolp files) (boundp files))
  6817. (setq files (symbol-value files))))
  6818. (if (stringp files) (setq files (list files)))
  6819. (cond
  6820. ((eq (car desc) :tag)
  6821. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6822. ((eq (car desc) :todo)
  6823. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6824. ((eq (car desc) :regexp)
  6825. (setq descre (cdr desc)))
  6826. ((eq (car desc) :level)
  6827. (setq descre (concat "^\\*\\{" (number-to-string
  6828. (if org-odd-levels-only
  6829. (1- (* 2 (cdr desc)))
  6830. (cdr desc)))
  6831. "\\}[ \t]")))
  6832. ((eq (car desc) :maxlevel)
  6833. (setq fast-path-p t)
  6834. (setq descre (concat "^\\*\\{1," (number-to-string
  6835. (if org-odd-levels-only
  6836. (1- (* 2 (cdr desc)))
  6837. (cdr desc)))
  6838. "\\}[ \t]")))
  6839. (t (error "Bad refiling target description %s" desc)))
  6840. (while (setq f (pop files))
  6841. (save-excursion
  6842. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6843. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6844. (setq f (expand-file-name f))
  6845. (save-excursion
  6846. (save-restriction
  6847. (widen)
  6848. (goto-char (point-min))
  6849. (while (re-search-forward descre nil t)
  6850. (goto-char (point-at-bol))
  6851. (when (looking-at org-complex-heading-regexp)
  6852. (setq level (org-reduced-level (- (match-end 1) (match-beginning 1)))
  6853. txt (org-link-display-format (match-string 4))
  6854. re (concat "^" (regexp-quote
  6855. (buffer-substring (match-beginning 1)
  6856. (match-end 4)))))
  6857. (if (match-end 5) (setq re (concat re "[ \t]+"
  6858. (regexp-quote
  6859. (match-string 5)))))
  6860. (setq re (concat re "[ \t]*$"))
  6861. (when org-refile-use-outline-path
  6862. (setq txt (mapconcat 'org-protect-slash
  6863. (append
  6864. (if (eq org-refile-use-outline-path 'file)
  6865. (list (file-name-nondirectory
  6866. (buffer-file-name (buffer-base-buffer))))
  6867. (if (eq org-refile-use-outline-path 'full-file-path)
  6868. (list (buffer-file-name (buffer-base-buffer)))))
  6869. (org-get-outline-path fast-path-p level txt)
  6870. (list txt))
  6871. "/")))
  6872. (push (list txt f re (point)) targets))
  6873. (goto-char (point-at-eol))))))))
  6874. (message "Getting targets...done")
  6875. (nreverse targets))))
  6876. (defun org-protect-slash (s)
  6877. (while (string-match "/" s)
  6878. (setq s (replace-match "\\" t t s)))
  6879. s)
  6880. (defvar org-olpa (make-vector 20 nil))
  6881. (defun org-get-outline-path (&optional fastp level heading)
  6882. "Return the outline path to the current entry, as a list."
  6883. (if fastp
  6884. (progn
  6885. (if (> level 19)
  6886. (error "Outline path failure, more than 19 levels."))
  6887. (loop for i from level upto 19 do
  6888. (aset org-olpa i nil))
  6889. (prog1
  6890. (delq nil (append org-olpa nil))
  6891. (aset org-olpa level heading)))
  6892. (let (rtn)
  6893. (save-excursion
  6894. (while (org-up-heading-safe)
  6895. (when (looking-at org-complex-heading-regexp)
  6896. (push (org-match-string-no-properties 4) rtn)))
  6897. rtn))))
  6898. (defvar org-refile-history nil
  6899. "History for refiling operations.")
  6900. (defun org-refile (&optional goto default-buffer)
  6901. "Move the entry at point to another heading.
  6902. The list of target headings is compiled using the information in
  6903. `org-refile-targets', which see. This list is created before each use
  6904. and will therefore always be up-to-date.
  6905. At the target location, the entry is filed as a subitem of the target heading.
  6906. Depending on `org-reverse-note-order', the new subitem will either be the
  6907. first or the last subitem.
  6908. If there is an active region, all entries in that region will be moved.
  6909. However, the region must fulfil the requirement that the first heading
  6910. is the first one sets the top-level of the moved text - at most siblings
  6911. below it are allowed.
  6912. With prefix arg GOTO, the command will only visit the target location,
  6913. not actually move anything.
  6914. With a double prefix `C-u C-u', go to the location where the last refiling
  6915. operation has put the subtree."
  6916. (interactive "P")
  6917. (let* ((cbuf (current-buffer))
  6918. (regionp (org-region-active-p))
  6919. (region-start (and regionp (region-beginning)))
  6920. (region-end (and regionp (region-end)))
  6921. (region-length (and regionp (- region-end region-start)))
  6922. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6923. pos it nbuf file re level reversed)
  6924. (when regionp (goto-char region-start)
  6925. (unless (org-kill-is-subtree-p
  6926. (buffer-substring region-start region-end))
  6927. (error "The region is not a (sequence of) subtree(s)")))
  6928. (if (equal goto '(16))
  6929. (org-refile-goto-last-stored)
  6930. (when (setq it (org-refile-get-location
  6931. (if goto "Goto: " "Refile to: ") default-buffer))
  6932. (setq file (nth 1 it)
  6933. re (nth 2 it)
  6934. pos (nth 3 it))
  6935. (if (and (equal (buffer-file-name) file)
  6936. (if regionp
  6937. (and (>= pos region-start)
  6938. (<= pos region-end))
  6939. (and (>= pos (point))
  6940. (< pos (save-excursion
  6941. (org-end-of-subtree t t))))))
  6942. (error "Cannot refile to position inside the tree or region"))
  6943. (setq nbuf (or (find-buffer-visiting file)
  6944. (find-file-noselect file)))
  6945. (if goto
  6946. (progn
  6947. (switch-to-buffer nbuf)
  6948. (goto-char pos)
  6949. (org-show-context 'org-goto))
  6950. (if regionp
  6951. (progn
  6952. (kill-new (buffer-substring region-start region-end))
  6953. (org-save-markers-in-region region-start region-end))
  6954. (org-copy-subtree 1 nil t))
  6955. (save-excursion
  6956. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6957. (find-file-noselect file))))
  6958. (setq reversed (org-notes-order-reversed-p))
  6959. (save-excursion
  6960. (save-restriction
  6961. (widen)
  6962. (goto-char pos)
  6963. (looking-at outline-regexp)
  6964. (setq level (org-get-valid-level (funcall outline-level) 1))
  6965. (goto-char
  6966. (if reversed
  6967. (or (outline-next-heading) (point-max))
  6968. (or (save-excursion (outline-get-next-sibling))
  6969. (org-end-of-subtree t t)
  6970. (point-max))))
  6971. (if (not (bolp)) (newline))
  6972. (bookmark-set "org-refile-last-stored")
  6973. (org-paste-subtree level))))
  6974. (if regionp
  6975. (delete-region (point) (+ (point) region-length))
  6976. (org-cut-subtree))
  6977. (setq org-markers-to-move nil)
  6978. (message "Refiled to \"%s\"" (car it)))))))
  6979. (defun org-refile-goto-last-stored ()
  6980. "Go to the location where the last refile was stored."
  6981. (interactive)
  6982. (bookmark-jump "org-refile-last-stored")
  6983. (message "This is the location of the last refile"))
  6984. (defun org-refile-get-location (&optional prompt default-buffer)
  6985. "Prompt the user for a refile location, using PROMPT."
  6986. (let ((org-refile-targets org-refile-targets)
  6987. (org-refile-use-outline-path org-refile-use-outline-path))
  6988. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6989. (unless org-refile-target-table
  6990. (error "No refile targets"))
  6991. (let* ((cbuf (current-buffer))
  6992. (cfn (buffer-file-name (buffer-base-buffer cbuf)))
  6993. (cfunc (if (and org-refile-use-outline-path
  6994. org-outline-path-complete-in-steps)
  6995. 'org-olpath-completing-read
  6996. 'org-ido-completing-read))
  6997. (extra (if org-refile-use-outline-path "/" ""))
  6998. (filename (and cfn (expand-file-name cfn)))
  6999. (tbl (mapcar
  7000. (lambda (x)
  7001. (if (not (equal filename (nth 1 x)))
  7002. (cons (concat (car x) extra " ("
  7003. (file-name-nondirectory (nth 1 x)) ")")
  7004. (cdr x))
  7005. (cons (concat (car x) extra) (cdr x))))
  7006. org-refile-target-table))
  7007. (completion-ignore-case t))
  7008. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7009. tbl)))
  7010. (defun org-olpath-completing-read (prompt collection &rest args)
  7011. "Read an outline path like a file name."
  7012. (let ((thetable collection))
  7013. (apply
  7014. 'org-ido-completing-read prompt
  7015. (lambda (string predicate &optional flag)
  7016. (let (rtn r s f (l (length string)))
  7017. (cond
  7018. ((eq flag nil)
  7019. ;; try completion
  7020. (try-completion string thetable))
  7021. ((eq flag t)
  7022. ;; all-completions
  7023. (setq rtn (all-completions string thetable predicate))
  7024. (mapcar
  7025. (lambda (x)
  7026. (setq r (substring x l))
  7027. (if (string-match " ([^)]*)$" x)
  7028. (setq f (match-string 0 x))
  7029. (setq f ""))
  7030. (if (string-match "/" r)
  7031. (concat string (substring r 0 (match-end 0)) f)
  7032. x))
  7033. rtn))
  7034. ((eq flag 'lambda)
  7035. ;; exact match?
  7036. (assoc string thetable)))
  7037. ))
  7038. args)))
  7039. ;;;; Dynamic blocks
  7040. (defun org-find-dblock (name)
  7041. "Find the first dynamic block with name NAME in the buffer.
  7042. If not found, stay at current position and return nil."
  7043. (let (pos)
  7044. (save-excursion
  7045. (goto-char (point-min))
  7046. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7047. nil t)
  7048. (match-beginning 0))))
  7049. (if pos (goto-char pos))
  7050. pos))
  7051. (defconst org-dblock-start-re
  7052. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7053. "Matches the startline of a dynamic block, with parameters.")
  7054. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7055. "Matches the end of a dynamic block.")
  7056. (defun org-create-dblock (plist)
  7057. "Create a dynamic block section, with parameters taken from PLIST.
  7058. PLIST must contain a :name entry which is used as name of the block."
  7059. (unless (bolp) (newline))
  7060. (let ((name (plist-get plist :name)))
  7061. (insert "#+BEGIN: " name)
  7062. (while plist
  7063. (if (eq (car plist) :name)
  7064. (setq plist (cddr plist))
  7065. (insert " " (prin1-to-string (pop plist)))))
  7066. (insert "\n\n#+END:\n")
  7067. (beginning-of-line -2)))
  7068. (defun org-prepare-dblock ()
  7069. "Prepare dynamic block for refresh.
  7070. This empties the block, puts the cursor at the insert position and returns
  7071. the property list including an extra property :name with the block name."
  7072. (unless (looking-at org-dblock-start-re)
  7073. (error "Not at a dynamic block"))
  7074. (let* ((begdel (1+ (match-end 0)))
  7075. (name (org-no-properties (match-string 1)))
  7076. (params (append (list :name name)
  7077. (read (concat "(" (match-string 3) ")")))))
  7078. (unless (re-search-forward org-dblock-end-re nil t)
  7079. (error "Dynamic block not terminated"))
  7080. (setq params
  7081. (append params
  7082. (list :content (buffer-substring
  7083. begdel (match-beginning 0)))))
  7084. (delete-region begdel (match-beginning 0))
  7085. (goto-char begdel)
  7086. (open-line 1)
  7087. params))
  7088. (defun org-map-dblocks (&optional command)
  7089. "Apply COMMAND to all dynamic blocks in the current buffer.
  7090. If COMMAND is not given, use `org-update-dblock'."
  7091. (let ((cmd (or command 'org-update-dblock))
  7092. pos)
  7093. (save-excursion
  7094. (goto-char (point-min))
  7095. (while (re-search-forward org-dblock-start-re nil t)
  7096. (goto-char (setq pos (match-beginning 0)))
  7097. (condition-case nil
  7098. (funcall cmd)
  7099. (error (message "Error during update of dynamic block")))
  7100. (goto-char pos)
  7101. (unless (re-search-forward org-dblock-end-re nil t)
  7102. (error "Dynamic block not terminated"))))))
  7103. (defun org-dblock-update (&optional arg)
  7104. "User command for updating dynamic blocks.
  7105. Update the dynamic block at point. With prefix ARG, update all dynamic
  7106. blocks in the buffer."
  7107. (interactive "P")
  7108. (if arg
  7109. (org-update-all-dblocks)
  7110. (or (looking-at org-dblock-start-re)
  7111. (org-beginning-of-dblock))
  7112. (org-update-dblock)))
  7113. (defun org-update-dblock ()
  7114. "Update the dynamic block at point
  7115. This means to empty the block, parse for parameters and then call
  7116. the correct writing function."
  7117. (save-window-excursion
  7118. (let* ((pos (point))
  7119. (line (org-current-line))
  7120. (params (org-prepare-dblock))
  7121. (name (plist-get params :name))
  7122. (cmd (intern (concat "org-dblock-write:" name))))
  7123. (message "Updating dynamic block `%s' at line %d..." name line)
  7124. (funcall cmd params)
  7125. (message "Updating dynamic block `%s' at line %d...done" name line)
  7126. (goto-char pos))))
  7127. (defun org-beginning-of-dblock ()
  7128. "Find the beginning of the dynamic block at point.
  7129. Error if there is no such block at point."
  7130. (let ((pos (point))
  7131. beg)
  7132. (end-of-line 1)
  7133. (if (and (re-search-backward org-dblock-start-re nil t)
  7134. (setq beg (match-beginning 0))
  7135. (re-search-forward org-dblock-end-re nil t)
  7136. (> (match-end 0) pos))
  7137. (goto-char beg)
  7138. (goto-char pos)
  7139. (error "Not in a dynamic block"))))
  7140. (defun org-update-all-dblocks ()
  7141. "Update all dynamic blocks in the buffer.
  7142. This function can be used in a hook."
  7143. (when (org-mode-p)
  7144. (org-map-dblocks 'org-update-dblock)))
  7145. ;;;; Completion
  7146. (defconst org-additional-option-like-keywords
  7147. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7148. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7149. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7150. "BEGIN_QUOTE" "END_QUOTE"
  7151. "BEGIN_VERSE" "END_VERSE"
  7152. "BEGIN_SRC" "END_SRC"
  7153. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7154. (defcustom org-structure-template-alist
  7155. '(
  7156. ("s" "#+begin_src ?\n\n#+end_src"
  7157. "<src lang=\"?\">\n\n</src>")
  7158. ("e" "#+begin_example\n?\n#+end_example"
  7159. "<example>\n?\n</example>")
  7160. ("q" "#+begin_quote\n?\n#+end_quote"
  7161. "<quote>\n?\n</quote>")
  7162. ("v" "#+begin_verse\n?\n#+end_verse"
  7163. "<verse>\n?\n/verse>")
  7164. ("l" "#+begin_latex\n?\n#+end_latex"
  7165. "<literal style=\"latex\">\n?\n</literal>")
  7166. ("L" "#+latex: "
  7167. "<literal style=\"latex\">?</literal>")
  7168. ("h" "#+begin_html\n?\n#+end_html"
  7169. "<literal style=\"html\">\n?\n</literal>")
  7170. ("H" "#+html: "
  7171. "<literal style=\"html\">?</literal>")
  7172. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7173. ("A" "#+ascii: ")
  7174. ("i" "#+include %file ?"
  7175. "<include file=%file markup=\"?\">")
  7176. )
  7177. "Structure completion elements.
  7178. This is a list of abbreviation keys and values. The value gets inserted
  7179. it you type @samp{.} followed by the key and then the completion key,
  7180. usually `M-TAB'. %file will be replaced by a file name after prompting
  7181. for the file using completion.
  7182. There are two templates for each key, the first uses the original Org syntax,
  7183. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7184. the default when the /org-mtags.el/ module has been loaded. See also the
  7185. variable `org-mtags-prefer-muse-templates'.
  7186. This is an experimental feature, it is undecided if it is going to stay in."
  7187. :group 'org-completion
  7188. :type '(repeat
  7189. (string :tag "Key")
  7190. (string :tag "Template")
  7191. (string :tag "Muse Template")))
  7192. (defun org-try-structure-completion ()
  7193. "Try to complete a structure template before point.
  7194. This looks for strings like \"<e\" on an otherwise empty line and
  7195. expands them."
  7196. (let ((l (buffer-substring (point-at-bol) (point)))
  7197. a)
  7198. (when (and (looking-at "[ \t]*$")
  7199. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7200. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7201. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7202. (match-beginning 1)) a)
  7203. t)))
  7204. (defun org-complete-expand-structure-template (start cell)
  7205. "Expand a structure template."
  7206. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7207. (rpl (nth (if musep 2 1) cell)))
  7208. (delete-region start (point))
  7209. (when (string-match "\\`#\\+" rpl)
  7210. (cond
  7211. ((bolp))
  7212. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7213. (delete-region (point-at-bol) (point)))
  7214. (t (newline))))
  7215. (setq start (point))
  7216. (if (string-match "%file" rpl)
  7217. (setq rpl (replace-match
  7218. (concat
  7219. "\""
  7220. (save-match-data
  7221. (abbreviate-file-name (read-file-name "Include file: ")))
  7222. "\"")
  7223. t t rpl)))
  7224. (insert rpl)
  7225. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7226. (defun org-complete (&optional arg)
  7227. "Perform completion on word at point.
  7228. At the beginning of a headline, this completes TODO keywords as given in
  7229. `org-todo-keywords'.
  7230. If the current word is preceded by a backslash, completes the TeX symbols
  7231. that are supported for HTML support.
  7232. If the current word is preceded by \"#+\", completes special words for
  7233. setting file options.
  7234. In the line after \"#+STARTUP:, complete valid keywords.\"
  7235. At all other locations, this simply calls the value of
  7236. `org-completion-fallback-command'."
  7237. (interactive "P")
  7238. (org-without-partial-completion
  7239. (catch 'exit
  7240. (let* ((a nil)
  7241. (end (point))
  7242. (beg1 (save-excursion
  7243. (skip-chars-backward (org-re "[:alnum:]_@"))
  7244. (point)))
  7245. (beg (save-excursion
  7246. (skip-chars-backward "a-zA-Z0-9_:$")
  7247. (point)))
  7248. (confirm (lambda (x) (stringp (car x))))
  7249. (searchhead (equal (char-before beg) ?*))
  7250. (struct
  7251. (when (and (member (char-before beg1) '(?. ?<))
  7252. (setq a (assoc (buffer-substring beg1 (point))
  7253. org-structure-template-alist)))
  7254. (org-complete-expand-structure-template (1- beg1) a)
  7255. (throw 'exit t)))
  7256. (tag (and (equal (char-before beg1) ?:)
  7257. (equal (char-after (point-at-bol)) ?*)))
  7258. (prop (and (equal (char-before beg1) ?:)
  7259. (not (equal (char-after (point-at-bol)) ?*))))
  7260. (texp (equal (char-before beg) ?\\))
  7261. (link (equal (char-before beg) ?\[))
  7262. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7263. beg)
  7264. "#+"))
  7265. (startup (string-match "^#\\+STARTUP:.*"
  7266. (buffer-substring (point-at-bol) (point))))
  7267. (completion-ignore-case opt)
  7268. (type nil)
  7269. (tbl nil)
  7270. (table (cond
  7271. (opt
  7272. (setq type :opt)
  7273. (require 'org-exp)
  7274. (append
  7275. (mapcar
  7276. (lambda (x)
  7277. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7278. (cons (match-string 2 x) (match-string 1 x)))
  7279. (org-split-string (org-get-current-options) "\n"))
  7280. (mapcar 'list org-additional-option-like-keywords)))
  7281. (startup
  7282. (setq type :startup)
  7283. org-startup-options)
  7284. (link (append org-link-abbrev-alist-local
  7285. org-link-abbrev-alist))
  7286. (texp
  7287. (setq type :tex)
  7288. org-html-entities)
  7289. ((string-match "\\`\\*+[ \t]+\\'"
  7290. (buffer-substring (point-at-bol) beg))
  7291. (setq type :todo)
  7292. (mapcar 'list org-todo-keywords-1))
  7293. (searchhead
  7294. (setq type :searchhead)
  7295. (save-excursion
  7296. (goto-char (point-min))
  7297. (while (re-search-forward org-todo-line-regexp nil t)
  7298. (push (list
  7299. (org-make-org-heading-search-string
  7300. (match-string 3) t))
  7301. tbl)))
  7302. tbl)
  7303. (tag (setq type :tag beg beg1)
  7304. (or org-tag-alist (org-get-buffer-tags)))
  7305. (prop (setq type :prop beg beg1)
  7306. (mapcar 'list (org-buffer-property-keys nil t t)))
  7307. (t (progn
  7308. (call-interactively org-completion-fallback-command)
  7309. (throw 'exit nil)))))
  7310. (pattern (buffer-substring-no-properties beg end))
  7311. (completion (try-completion pattern table confirm)))
  7312. (cond ((eq completion t)
  7313. (if (not (assoc (upcase pattern) table))
  7314. (message "Already complete")
  7315. (if (and (equal type :opt)
  7316. (not (member (car (assoc (upcase pattern) table))
  7317. org-additional-option-like-keywords)))
  7318. (insert (substring (cdr (assoc (upcase pattern) table))
  7319. (length pattern)))
  7320. (if (memq type '(:tag :prop)) (insert ":")))))
  7321. ((null completion)
  7322. (message "Can't find completion for \"%s\"" pattern)
  7323. (ding))
  7324. ((not (string= pattern completion))
  7325. (delete-region beg end)
  7326. (if (string-match " +$" completion)
  7327. (setq completion (replace-match "" t t completion)))
  7328. (insert completion)
  7329. (if (get-buffer-window "*Completions*")
  7330. (delete-window (get-buffer-window "*Completions*")))
  7331. (if (assoc completion table)
  7332. (if (eq type :todo) (insert " ")
  7333. (if (memq type '(:tag :prop)) (insert ":"))))
  7334. (if (and (equal type :opt) (assoc completion table))
  7335. (message "%s" (substitute-command-keys
  7336. "Press \\[org-complete] again to insert example settings"))))
  7337. (t
  7338. (message "Making completion list...")
  7339. (let ((list (sort (all-completions pattern table confirm)
  7340. 'string<)))
  7341. (with-output-to-temp-buffer "*Completions*"
  7342. (condition-case nil
  7343. ;; Protection needed for XEmacs and emacs 21
  7344. (display-completion-list list pattern)
  7345. (error (display-completion-list list)))))
  7346. (message "Making completion list...%s" "done")))))))
  7347. ;;;; TODO, DEADLINE, Comments
  7348. (defun org-toggle-comment ()
  7349. "Change the COMMENT state of an entry."
  7350. (interactive)
  7351. (save-excursion
  7352. (org-back-to-heading)
  7353. (let (case-fold-search)
  7354. (if (looking-at (concat outline-regexp
  7355. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7356. (replace-match "" t t nil 1)
  7357. (if (looking-at outline-regexp)
  7358. (progn
  7359. (goto-char (match-end 0))
  7360. (insert org-comment-string " ")))))))
  7361. (defvar org-last-todo-state-is-todo nil
  7362. "This is non-nil when the last TODO state change led to a TODO state.
  7363. If the last change removed the TODO tag or switched to DONE, then
  7364. this is nil.")
  7365. (defvar org-setting-tags nil) ; dynamically skipped
  7366. (defun org-parse-local-options (string var)
  7367. "Parse STRING for startup setting relevant for variable VAR."
  7368. (let ((rtn (symbol-value var))
  7369. e opts)
  7370. (save-match-data
  7371. (if (or (not string) (not (string-match "\\S-" string)))
  7372. rtn
  7373. (setq opts (delq nil (mapcar (lambda (x)
  7374. (setq e (assoc x org-startup-options))
  7375. (if (eq (nth 1 e) var) e nil))
  7376. (org-split-string string "[ \t]+"))))
  7377. (if (not opts)
  7378. rtn
  7379. (setq rtn nil)
  7380. (while (setq e (pop opts))
  7381. (if (not (nth 3 e))
  7382. (setq rtn (nth 2 e))
  7383. (if (not (listp rtn)) (setq rtn nil))
  7384. (push (nth 2 e) rtn)))
  7385. rtn)))))
  7386. (defvar org-blocker-hook nil
  7387. "Hook for functions that are allowed to block a state change.
  7388. Each function gets as its single argument a property list, see
  7389. `org-trigger-hook' for more information about this list.
  7390. If any of the functions in this hook returns nil, the state change
  7391. is blocked.")
  7392. (defvar org-trigger-hook nil
  7393. "Hook for functions that are triggered by a state change.
  7394. Each function gets as its single argument a property list with at least
  7395. the following elements:
  7396. (:type type-of-change :position pos-at-entry-start
  7397. :from old-state :to new-state)
  7398. Depending on the type, more properties may be present.
  7399. This mechanism is currently implemented for:
  7400. TODO state changes
  7401. ------------------
  7402. :type todo-state-change
  7403. :from previous state (keyword as a string), or nil
  7404. :to new state (keyword as a string), or nil")
  7405. (defvar org-agenda-headline-snapshot-before-repeat)
  7406. (defun org-todo (&optional arg)
  7407. "Change the TODO state of an item.
  7408. The state of an item is given by a keyword at the start of the heading,
  7409. like
  7410. *** TODO Write paper
  7411. *** DONE Call mom
  7412. The different keywords are specified in the variable `org-todo-keywords'.
  7413. By default the available states are \"TODO\" and \"DONE\".
  7414. So for this example: when the item starts with TODO, it is changed to DONE.
  7415. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7416. DONE are present, add TODO at the beginning of the heading.
  7417. With C-u prefix arg, use completion to determine the new state.
  7418. With numeric prefix arg, switch to that state.
  7419. For calling through lisp, arg is also interpreted in the following way:
  7420. 'none -> empty state
  7421. \"\"(empty string) -> switch to empty state
  7422. 'done -> switch to DONE
  7423. 'nextset -> switch to the next set of keywords
  7424. 'previousset -> switch to the previous set of keywords
  7425. \"WAITING\" -> switch to the specified keyword, but only if it
  7426. really is a member of `org-todo-keywords'."
  7427. (interactive "P")
  7428. (save-excursion
  7429. (catch 'exit
  7430. (org-back-to-heading)
  7431. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7432. (or (looking-at (concat " +" org-todo-regexp " *"))
  7433. (looking-at " *"))
  7434. (let* ((match-data (match-data))
  7435. (startpos (point-at-bol))
  7436. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7437. (org-log-done org-log-done)
  7438. (org-log-repeat org-log-repeat)
  7439. (org-todo-log-states org-todo-log-states)
  7440. (this (match-string 1))
  7441. (hl-pos (match-beginning 0))
  7442. (head (org-get-todo-sequence-head this))
  7443. (ass (assoc head org-todo-kwd-alist))
  7444. (interpret (nth 1 ass))
  7445. (done-word (nth 3 ass))
  7446. (final-done-word (nth 4 ass))
  7447. (last-state (or this ""))
  7448. (completion-ignore-case t)
  7449. (member (member this org-todo-keywords-1))
  7450. (tail (cdr member))
  7451. (state (cond
  7452. ((and org-todo-key-trigger
  7453. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7454. (and (not arg) org-use-fast-todo-selection
  7455. (not (eq org-use-fast-todo-selection 'prefix)))))
  7456. ;; Use fast selection
  7457. (org-fast-todo-selection))
  7458. ((and (equal arg '(4))
  7459. (or (not org-use-fast-todo-selection)
  7460. (not org-todo-key-trigger)))
  7461. ;; Read a state with completion
  7462. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7463. org-todo-keywords-1)
  7464. nil t))
  7465. ((eq arg 'right)
  7466. (if this
  7467. (if tail (car tail) nil)
  7468. (car org-todo-keywords-1)))
  7469. ((eq arg 'left)
  7470. (if (equal member org-todo-keywords-1)
  7471. nil
  7472. (if this
  7473. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7474. org-todo-keywords-1)
  7475. (org-last org-todo-keywords-1))))
  7476. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7477. (setq arg nil))) ; hack to fall back to cycling
  7478. (arg
  7479. ;; user or caller requests a specific state
  7480. (cond
  7481. ((equal arg "") nil)
  7482. ((eq arg 'none) nil)
  7483. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7484. ((eq arg 'nextset)
  7485. (or (car (cdr (member head org-todo-heads)))
  7486. (car org-todo-heads)))
  7487. ((eq arg 'previousset)
  7488. (let ((org-todo-heads (reverse org-todo-heads)))
  7489. (or (car (cdr (member head org-todo-heads)))
  7490. (car org-todo-heads))))
  7491. ((car (member arg org-todo-keywords-1)))
  7492. ((nth (1- (prefix-numeric-value arg))
  7493. org-todo-keywords-1))))
  7494. ((null member) (or head (car org-todo-keywords-1)))
  7495. ((equal this final-done-word) nil) ;; -> make empty
  7496. ((null tail) nil) ;; -> first entry
  7497. ((eq interpret 'sequence)
  7498. (car tail))
  7499. ((memq interpret '(type priority))
  7500. (if (eq this-command last-command)
  7501. (car tail)
  7502. (if (> (length tail) 0)
  7503. (or done-word (car org-done-keywords))
  7504. nil)))
  7505. (t nil)))
  7506. (next (if state (concat " " state " ") " "))
  7507. (change-plist (list :type 'todo-state-change :from this :to state
  7508. :position startpos))
  7509. dolog now-done-p)
  7510. (when org-blocker-hook
  7511. (unless (save-excursion
  7512. (save-match-data
  7513. (run-hook-with-args-until-failure
  7514. 'org-blocker-hook change-plist)))
  7515. (if (interactive-p)
  7516. (error "TODO state change from %s to %s blocked" this state)
  7517. ;; fail silently
  7518. (message "TODO state change from %s to %s blocked" this state)
  7519. (throw 'exit nil))))
  7520. (store-match-data match-data)
  7521. (replace-match next t t)
  7522. (unless (pos-visible-in-window-p hl-pos)
  7523. (message "TODO state changed to %s" (org-trim next)))
  7524. (unless head
  7525. (setq head (org-get-todo-sequence-head state)
  7526. ass (assoc head org-todo-kwd-alist)
  7527. interpret (nth 1 ass)
  7528. done-word (nth 3 ass)
  7529. final-done-word (nth 4 ass)))
  7530. (when (memq arg '(nextset previousset))
  7531. (message "Keyword-Set %d/%d: %s"
  7532. (- (length org-todo-sets) -1
  7533. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7534. (length org-todo-sets)
  7535. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7536. (setq org-last-todo-state-is-todo
  7537. (not (member state org-done-keywords)))
  7538. (setq now-done-p (and (member state org-done-keywords)
  7539. (not (member this org-done-keywords))))
  7540. (and logging (org-local-logging logging))
  7541. (when (and (or org-todo-log-states org-log-done)
  7542. (not (memq arg '(nextset previousset))))
  7543. ;; we need to look at recording a time and note
  7544. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7545. (nth 2 (assoc this org-todo-log-states))))
  7546. (when (and state
  7547. (member state org-not-done-keywords)
  7548. (not (member this org-not-done-keywords)))
  7549. ;; This is now a todo state and was not one before
  7550. ;; If there was a CLOSED time stamp, get rid of it.
  7551. (org-add-planning-info nil nil 'closed))
  7552. (when (and now-done-p org-log-done)
  7553. ;; It is now done, and it was not done before
  7554. (org-add-planning-info 'closed (org-current-time))
  7555. (if (and (not dolog) (eq 'note org-log-done))
  7556. (org-add-log-setup 'done state 'findpos 'note)))
  7557. (when (and state dolog)
  7558. ;; This is a non-nil state, and we need to log it
  7559. (org-add-log-setup 'state state 'findpos dolog)))
  7560. ;; Fixup tag positioning
  7561. (org-todo-trigger-tag-changes state)
  7562. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7563. (when org-provide-todo-statistics
  7564. (org-update-parent-todo-statistics))
  7565. (run-hooks 'org-after-todo-state-change-hook)
  7566. (if (and arg (not (member state org-done-keywords)))
  7567. (setq head (org-get-todo-sequence-head state)))
  7568. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7569. ;; Do we need to trigger a repeat?
  7570. (when now-done-p
  7571. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7572. ;; This is for the agenda, take a snapshot of the headline.
  7573. (save-match-data
  7574. (setq org-agenda-headline-snapshot-before-repeat
  7575. (org-get-heading))))
  7576. (org-auto-repeat-maybe state))
  7577. ;; Fixup cursor location if close to the keyword
  7578. (if (and (outline-on-heading-p)
  7579. (not (bolp))
  7580. (save-excursion (beginning-of-line 1)
  7581. (looking-at org-todo-line-regexp))
  7582. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7583. (progn
  7584. (goto-char (or (match-end 2) (match-end 1)))
  7585. (just-one-space)))
  7586. (when org-trigger-hook
  7587. (save-excursion
  7588. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7589. (defun org-update-parent-todo-statistics ()
  7590. "Update any statistics cookie in the parent of the current headline."
  7591. (interactive)
  7592. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7593. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7594. (catch 'exit
  7595. (save-excursion
  7596. (setq level (org-up-heading-safe))
  7597. (unless (and level
  7598. (re-search-forward box-re (point-at-eol) t))
  7599. (throw 'exit nil))
  7600. (setq is-percent (match-end 2))
  7601. (save-match-data
  7602. (unless (outline-next-heading) (throw 'exit nil))
  7603. (while (looking-at org-todo-line-regexp)
  7604. (setq kwd (match-string 2))
  7605. (and kwd (setq cnt-all (1+ cnt-all)))
  7606. (and (member kwd org-done-keywords)
  7607. (setq cnt-done (1+ cnt-done)))
  7608. (condition-case nil
  7609. (org-forward-same-level 1)
  7610. (error (end-of-line 1)))))
  7611. (replace-match
  7612. (if is-percent
  7613. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7614. (format "[%d/%d]" cnt-done cnt-all)))
  7615. (run-hook-with-args 'org-after-todo-statistics-hook
  7616. cnt-done (- cnt-all cnt-done))))))
  7617. (defvar org-after-todo-statistics-hook nil
  7618. "Hook that is called after a TODO statistics cookie has been updated.
  7619. Each function is called with two arguments: the number of not-done entries
  7620. and the number of done entries.
  7621. For example, the following function, when added to this hook, will switch
  7622. an entry to DONE when all children are done, and back to TODO when new
  7623. entries are set to a TODO status. Note that this hook is only called
  7624. when there is a statistics cookie in the headline!
  7625. (defun org-summary-todo (n-done n-not-done)
  7626. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7627. (let (org-log-done org-log-states) ; turn off logging
  7628. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7629. ")
  7630. (defun org-todo-trigger-tag-changes (state)
  7631. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7632. (let ((l org-todo-state-tags-triggers)
  7633. changes)
  7634. (when (or (not state) (equal state ""))
  7635. (setq changes (append changes (cdr (assoc "" l)))))
  7636. (when (and (stringp state) (> (length state) 0))
  7637. (setq changes (append changes (cdr (assoc state l)))))
  7638. (when (member state org-not-done-keywords)
  7639. (setq changes (append changes (cdr (assoc 'todo l)))))
  7640. (when (member state org-done-keywords)
  7641. (setq changes (append changes (cdr (assoc 'done l)))))
  7642. (dolist (c changes)
  7643. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7644. (defun org-local-logging (value)
  7645. "Get logging settings from a property VALUE."
  7646. (let* (words w a)
  7647. ;; directly set the variables, they are already local.
  7648. (setq org-log-done nil
  7649. org-log-repeat nil
  7650. org-todo-log-states nil)
  7651. (setq words (org-split-string value))
  7652. (while (setq w (pop words))
  7653. (cond
  7654. ((setq a (assoc w org-startup-options))
  7655. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7656. (set (nth 1 a) (nth 2 a))))
  7657. ((setq a (org-extract-log-state-settings w))
  7658. (and (member (car a) org-todo-keywords-1)
  7659. (push a org-todo-log-states)))))))
  7660. (defun org-get-todo-sequence-head (kwd)
  7661. "Return the head of the TODO sequence to which KWD belongs.
  7662. If KWD is not set, check if there is a text property remembering the
  7663. right sequence."
  7664. (let (p)
  7665. (cond
  7666. ((not kwd)
  7667. (or (get-text-property (point-at-bol) 'org-todo-head)
  7668. (progn
  7669. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7670. nil (point-at-eol)))
  7671. (get-text-property p 'org-todo-head))))
  7672. ((not (member kwd org-todo-keywords-1))
  7673. (car org-todo-keywords-1))
  7674. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7675. (defun org-fast-todo-selection ()
  7676. "Fast TODO keyword selection with single keys.
  7677. Returns the new TODO keyword, or nil if no state change should occur."
  7678. (let* ((fulltable org-todo-key-alist)
  7679. (done-keywords org-done-keywords) ;; needed for the faces.
  7680. (maxlen (apply 'max (mapcar
  7681. (lambda (x)
  7682. (if (stringp (car x)) (string-width (car x)) 0))
  7683. fulltable)))
  7684. (expert nil)
  7685. (fwidth (+ maxlen 3 1 3))
  7686. (ncol (/ (- (window-width) 4) fwidth))
  7687. tg cnt e c tbl
  7688. groups ingroup)
  7689. (save-window-excursion
  7690. (if expert
  7691. (set-buffer (get-buffer-create " *Org todo*"))
  7692. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7693. (erase-buffer)
  7694. (org-set-local 'org-done-keywords done-keywords)
  7695. (setq tbl fulltable cnt 0)
  7696. (while (setq e (pop tbl))
  7697. (cond
  7698. ((equal e '(:startgroup))
  7699. (push '() groups) (setq ingroup t)
  7700. (when (not (= cnt 0))
  7701. (setq cnt 0)
  7702. (insert "\n"))
  7703. (insert "{ "))
  7704. ((equal e '(:endgroup))
  7705. (setq ingroup nil cnt 0)
  7706. (insert "}\n"))
  7707. (t
  7708. (setq tg (car e) c (cdr e))
  7709. (if ingroup (push tg (car groups)))
  7710. (setq tg (org-add-props tg nil 'face
  7711. (org-get-todo-face tg)))
  7712. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7713. (insert "[" c "] " tg (make-string
  7714. (- fwidth 4 (length tg)) ?\ ))
  7715. (when (= (setq cnt (1+ cnt)) ncol)
  7716. (insert "\n")
  7717. (if ingroup (insert " "))
  7718. (setq cnt 0)))))
  7719. (insert "\n")
  7720. (goto-char (point-min))
  7721. (if (not expert) (org-fit-window-to-buffer))
  7722. (message "[a-z..]:Set [SPC]:clear")
  7723. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7724. (cond
  7725. ((or (= c ?\C-g)
  7726. (and (= c ?q) (not (rassoc c fulltable))))
  7727. (setq quit-flag t))
  7728. ((= c ?\ ) nil)
  7729. ((setq e (rassoc c fulltable) tg (car e))
  7730. tg)
  7731. (t (setq quit-flag t))))))
  7732. (defun org-entry-is-todo-p ()
  7733. (member (org-get-todo-state) org-not-done-keywords))
  7734. (defun org-entry-is-done-p ()
  7735. (member (org-get-todo-state) org-done-keywords))
  7736. (defun org-get-todo-state ()
  7737. (save-excursion
  7738. (org-back-to-heading t)
  7739. (and (looking-at org-todo-line-regexp)
  7740. (match-end 2)
  7741. (match-string 2))))
  7742. (defun org-at-date-range-p (&optional inactive-ok)
  7743. "Is the cursor inside a date range?"
  7744. (interactive)
  7745. (save-excursion
  7746. (catch 'exit
  7747. (let ((pos (point)))
  7748. (skip-chars-backward "^[<\r\n")
  7749. (skip-chars-backward "<[")
  7750. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7751. (>= (match-end 0) pos)
  7752. (throw 'exit t))
  7753. (skip-chars-backward "^<[\r\n")
  7754. (skip-chars-backward "<[")
  7755. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7756. (>= (match-end 0) pos)
  7757. (throw 'exit t)))
  7758. nil)))
  7759. (defun org-get-repeat ()
  7760. "Check if there is a deadline/schedule with repeater in this entry."
  7761. (save-match-data
  7762. (save-excursion
  7763. (org-back-to-heading t)
  7764. (if (re-search-forward
  7765. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7766. (match-string 1)))))
  7767. (defvar org-last-changed-timestamp)
  7768. (defvar org-last-inserted-timestamp)
  7769. (defvar org-log-post-message)
  7770. (defvar org-log-note-purpose)
  7771. (defvar org-log-note-how)
  7772. (defvar org-log-note-extra)
  7773. (defun org-auto-repeat-maybe (done-word)
  7774. "Check if the current headline contains a repeated deadline/schedule.
  7775. If yes, set TODO state back to what it was and change the base date
  7776. of repeating deadline/scheduled time stamps to new date.
  7777. This function is run automatically after each state change to a DONE state."
  7778. ;; last-state is dynamically scoped into this function
  7779. (let* ((repeat (org-get-repeat))
  7780. (aa (assoc last-state org-todo-kwd-alist))
  7781. (interpret (nth 1 aa))
  7782. (head (nth 2 aa))
  7783. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7784. (msg "Entry repeats: ")
  7785. (org-log-done nil)
  7786. (org-todo-log-states nil)
  7787. (nshiftmax 10) (nshift 0)
  7788. re type n what ts mb0 time)
  7789. (when repeat
  7790. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7791. (org-todo (if (eq interpret 'type) last-state head))
  7792. (when org-log-repeat
  7793. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7794. (memq 'org-add-log-note post-command-hook))
  7795. ;; OK, we are already setup for some record
  7796. (if (eq org-log-repeat 'note)
  7797. ;; make sure we take a note, not only a time stamp
  7798. (setq org-log-note-how 'note))
  7799. ;; Set up for taking a record
  7800. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7801. 'findpos org-log-repeat)))
  7802. (org-back-to-heading t)
  7803. (org-add-planning-info nil nil 'closed)
  7804. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7805. org-deadline-time-regexp "\\)\\|\\("
  7806. org-ts-regexp "\\)"))
  7807. (while (re-search-forward
  7808. re (save-excursion (outline-next-heading) (point)) t)
  7809. (setq type (if (match-end 1) org-scheduled-string
  7810. (if (match-end 3) org-deadline-string "Plain:"))
  7811. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7812. mb0 (match-beginning 0))
  7813. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7814. (setq n (string-to-number (match-string 2 ts))
  7815. what (match-string 3 ts))
  7816. (if (equal what "w") (setq n (* n 7) what "d"))
  7817. ;; Preparation, see if we need to modify the start date for the change
  7818. (when (match-end 1)
  7819. (setq time (save-match-data (org-time-string-to-time ts)))
  7820. (cond
  7821. ((equal (match-string 1 ts) ".")
  7822. ;; Shift starting date to today
  7823. (org-timestamp-change
  7824. (- (time-to-days (current-time)) (time-to-days time))
  7825. 'day))
  7826. ((equal (match-string 1 ts) "+")
  7827. (while (or (= nshift 0)
  7828. (<= (time-to-days time) (time-to-days (current-time))))
  7829. (when (= (incf nshift) nshiftmax)
  7830. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7831. (error "Abort")))
  7832. (org-timestamp-change n (cdr (assoc what whata)))
  7833. (org-at-timestamp-p t)
  7834. (setq ts (match-string 1))
  7835. (setq time (save-match-data (org-time-string-to-time ts))))
  7836. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7837. ;; rematch, so that we have everything in place for the real shift
  7838. (org-at-timestamp-p t)
  7839. (setq ts (match-string 1))
  7840. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7841. (org-timestamp-change n (cdr (assoc what whata)))
  7842. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7843. (setq org-log-post-message msg)
  7844. (message "%s" msg))))
  7845. (defun org-show-todo-tree (arg)
  7846. "Make a compact tree which shows all headlines marked with TODO.
  7847. The tree will show the lines where the regexp matches, and all higher
  7848. headlines above the match.
  7849. With a \\[universal-argument] prefix, also show the DONE entries.
  7850. With a numeric prefix N, construct a sparse tree for the Nth element
  7851. of `org-todo-keywords-1'."
  7852. (interactive "P")
  7853. (let ((case-fold-search nil)
  7854. (kwd-re
  7855. (cond ((null arg) org-not-done-regexp)
  7856. ((equal arg '(4))
  7857. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7858. (mapcar 'list org-todo-keywords-1))))
  7859. (concat "\\("
  7860. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7861. "\\)\\>")))
  7862. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7863. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7864. org-todo-keywords-1)))
  7865. (t (error "Invalid prefix argument: %s" arg)))))
  7866. (message "%d TODO entries found"
  7867. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7868. (defun org-deadline (&optional remove time)
  7869. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7870. With argument REMOVE, remove any deadline from the item.
  7871. When TIME is set, it should be an internal time specification, and the
  7872. scheduling will use the corresponding date."
  7873. (interactive "P")
  7874. (if remove
  7875. (progn
  7876. (org-remove-timestamp-with-keyword org-deadline-string)
  7877. (message "Item no longer has a deadline."))
  7878. (if (org-get-repeat)
  7879. (error "Cannot change deadline on task with repeater, please do that by hand")
  7880. (org-add-planning-info 'deadline time 'closed)
  7881. (message "Deadline on %s" org-last-inserted-timestamp))))
  7882. (defun org-schedule (&optional remove time)
  7883. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7884. With argument REMOVE, remove any scheduling date from the item.
  7885. When TIME is set, it should be an internal time specification, and the
  7886. scheduling will use the corresponding date."
  7887. (interactive "P")
  7888. (if remove
  7889. (progn
  7890. (org-remove-timestamp-with-keyword org-scheduled-string)
  7891. (message "Item is no longer scheduled."))
  7892. (if (org-get-repeat)
  7893. (error "Cannot reschedule task with repeater, please do that by hand")
  7894. (org-add-planning-info 'scheduled time 'closed)
  7895. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7896. (defun org-remove-timestamp-with-keyword (keyword)
  7897. "Remove all time stamps with KEYWORD in the current entry."
  7898. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7899. beg)
  7900. (save-excursion
  7901. (org-back-to-heading t)
  7902. (setq beg (point))
  7903. (org-end-of-subtree t t)
  7904. (while (re-search-backward re beg t)
  7905. (replace-match "")
  7906. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7907. (equal (char-before) ?\ ))
  7908. (backward-delete-char 1)
  7909. (if (string-match "^[ \t]*$" (buffer-substring
  7910. (point-at-bol) (point-at-eol)))
  7911. (delete-region (point-at-bol)
  7912. (min (point-max) (1+ (point-at-eol))))))))))
  7913. (defun org-add-planning-info (what &optional time &rest remove)
  7914. "Insert new timestamp with keyword in the line directly after the headline.
  7915. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7916. If non is given, the user is prompted for a date.
  7917. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7918. be removed."
  7919. (interactive)
  7920. (let (org-time-was-given org-end-time-was-given ts
  7921. end default-time default-input)
  7922. (when (and (not time) (memq what '(scheduled deadline)))
  7923. ;; Try to get a default date/time from existing timestamp
  7924. (save-excursion
  7925. (org-back-to-heading t)
  7926. (setq end (save-excursion (outline-next-heading) (point)))
  7927. (when (re-search-forward (if (eq what 'scheduled)
  7928. org-scheduled-time-regexp
  7929. org-deadline-time-regexp)
  7930. end t)
  7931. (setq ts (match-string 1)
  7932. default-time
  7933. (apply 'encode-time (org-parse-time-string ts))
  7934. default-input (and ts (org-get-compact-tod ts))))))
  7935. (when what
  7936. ;; If necessary, get the time from the user
  7937. (setq time (or time (org-read-date nil 'to-time nil nil
  7938. default-time default-input))))
  7939. (when (and org-insert-labeled-timestamps-at-point
  7940. (member what '(scheduled deadline)))
  7941. (insert
  7942. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7943. (org-insert-time-stamp time org-time-was-given
  7944. nil nil nil (list org-end-time-was-given))
  7945. (setq what nil))
  7946. (save-excursion
  7947. (save-restriction
  7948. (let (col list elt ts buffer-invisibility-spec)
  7949. (org-back-to-heading t)
  7950. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7951. (goto-char (match-end 1))
  7952. (setq col (current-column))
  7953. (goto-char (match-end 0))
  7954. (if (eobp) (insert "\n") (forward-char 1))
  7955. (if (and (not (looking-at outline-regexp))
  7956. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7957. "[^\r\n]*"))
  7958. (not (equal (match-string 1) org-clock-string)))
  7959. (narrow-to-region (match-beginning 0) (match-end 0))
  7960. (insert-before-markers "\n")
  7961. (backward-char 1)
  7962. (narrow-to-region (point) (point))
  7963. (and org-adapt-indentation (org-indent-to-column col)))
  7964. ;; Check if we have to remove something.
  7965. (setq list (cons what remove))
  7966. (while list
  7967. (setq elt (pop list))
  7968. (goto-char (point-min))
  7969. (when (or (and (eq elt 'scheduled)
  7970. (re-search-forward org-scheduled-time-regexp nil t))
  7971. (and (eq elt 'deadline)
  7972. (re-search-forward org-deadline-time-regexp nil t))
  7973. (and (eq elt 'closed)
  7974. (re-search-forward org-closed-time-regexp nil t)))
  7975. (replace-match "")
  7976. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7977. (if (looking-at " +") (replace-match ""))))
  7978. (goto-char (point-max))
  7979. (when what
  7980. (insert
  7981. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7982. (cond ((eq what 'scheduled) org-scheduled-string)
  7983. ((eq what 'deadline) org-deadline-string)
  7984. ((eq what 'closed) org-closed-string))
  7985. " ")
  7986. (setq ts (org-insert-time-stamp
  7987. time
  7988. (or org-time-was-given
  7989. (and (eq what 'closed) org-log-done-with-time))
  7990. (eq what 'closed)
  7991. nil nil (list org-end-time-was-given)))
  7992. (end-of-line 1))
  7993. (goto-char (point-min))
  7994. (widen)
  7995. (if (and (looking-at "[ \t]+\n")
  7996. (equal (char-before) ?\n))
  7997. (delete-region (1- (point)) (point-at-eol)))
  7998. ts)))))
  7999. (defvar org-log-note-marker (make-marker))
  8000. (defvar org-log-note-purpose nil)
  8001. (defvar org-log-note-state nil)
  8002. (defvar org-log-note-how nil)
  8003. (defvar org-log-note-extra nil)
  8004. (defvar org-log-note-window-configuration nil)
  8005. (defvar org-log-note-return-to (make-marker))
  8006. (defvar org-log-post-message nil
  8007. "Message to be displayed after a log note has been stored.
  8008. The auto-repeater uses this.")
  8009. (defun org-add-note ()
  8010. "Add a note to the current entry.
  8011. This is done in the same way as adding a state change note."
  8012. (interactive)
  8013. (org-add-log-setup 'note nil 'findpos nil))
  8014. (defvar org-property-end-re)
  8015. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  8016. "Set up the post command hook to take a note.
  8017. If this is about to TODO state change, the new state is expected in STATE.
  8018. When FINDPOS is non-nil, find the correct position for the note in
  8019. the current entry. If not, assume that it can be inserted at point.
  8020. HOW is an indicator what kind of note should be created.
  8021. EXTRA is additional text that will be inserted into the notes buffer."
  8022. (save-restriction
  8023. (save-excursion
  8024. (when findpos
  8025. (org-back-to-heading t)
  8026. (narrow-to-region (point) (save-excursion
  8027. (outline-next-heading) (point)))
  8028. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8029. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8030. "[^\r\n]*\\)?"))
  8031. (goto-char (match-end 0))
  8032. (when (and org-log-state-notes-insert-after-drawers
  8033. (save-excursion
  8034. (forward-line) (looking-at org-drawer-regexp)))
  8035. (progn (forward-line)
  8036. (while (looking-at org-drawer-regexp)
  8037. (goto-char (match-end 0))
  8038. (re-search-forward org-property-end-re (point-max) t)
  8039. (forward-line))
  8040. (forward-line -1)))
  8041. (unless org-log-states-order-reversed
  8042. (and (= (char-after) ?\n) (forward-char 1))
  8043. (org-skip-over-state-notes)
  8044. (skip-chars-backward " \t\n\r")))
  8045. (move-marker org-log-note-marker (point))
  8046. (setq org-log-note-purpose purpose
  8047. org-log-note-state state
  8048. org-log-note-how how
  8049. org-log-note-extra extra)
  8050. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  8051. (defun org-skip-over-state-notes ()
  8052. "Skip past the list of State notes in an entry."
  8053. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8054. (while (looking-at "[ \t]*- State")
  8055. (condition-case nil
  8056. (org-next-item)
  8057. (error (org-end-of-item)))))
  8058. (defun org-add-log-note (&optional purpose)
  8059. "Pop up a window for taking a note, and add this note later at point."
  8060. (remove-hook 'post-command-hook 'org-add-log-note)
  8061. (setq org-log-note-window-configuration (current-window-configuration))
  8062. (delete-other-windows)
  8063. (move-marker org-log-note-return-to (point))
  8064. (switch-to-buffer (marker-buffer org-log-note-marker))
  8065. (goto-char org-log-note-marker)
  8066. (org-switch-to-buffer-other-window "*Org Note*")
  8067. (erase-buffer)
  8068. (if (memq org-log-note-how '(time state))
  8069. (let (current-prefix-arg) (org-store-log-note))
  8070. (let ((org-inhibit-startup t)) (org-mode))
  8071. (insert (format "# Insert note for %s.
  8072. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8073. (cond
  8074. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8075. ((eq org-log-note-purpose 'done) "closed todo item")
  8076. ((eq org-log-note-purpose 'state)
  8077. (format "state change to \"%s\"" org-log-note-state))
  8078. ((eq org-log-note-purpose 'note)
  8079. "this entry")
  8080. (t (error "This should not happen")))))
  8081. (if org-log-note-extra (insert org-log-note-extra))
  8082. (org-set-local 'org-finish-function 'org-store-log-note)))
  8083. (defvar org-note-abort nil) ; dynamically scoped
  8084. (defun org-store-log-note ()
  8085. "Finish taking a log note, and insert it to where it belongs."
  8086. (let ((txt (buffer-string))
  8087. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8088. lines ind)
  8089. (kill-buffer (current-buffer))
  8090. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8091. (setq txt (replace-match "" t t txt)))
  8092. (if (string-match "\\s-+\\'" txt)
  8093. (setq txt (replace-match "" t t txt)))
  8094. (setq lines (org-split-string txt "\n"))
  8095. (when (and note (string-match "\\S-" note))
  8096. (setq note
  8097. (org-replace-escapes
  8098. note
  8099. (list (cons "%u" (user-login-name))
  8100. (cons "%U" user-full-name)
  8101. (cons "%t" (format-time-string
  8102. (org-time-stamp-format 'long 'inactive)
  8103. (current-time)))
  8104. (cons "%s" (if org-log-note-state
  8105. (concat "\"" org-log-note-state "\"")
  8106. "")))))
  8107. (if lines (setq note (concat note " \\\\")))
  8108. (push note lines))
  8109. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8110. (when lines
  8111. (save-excursion
  8112. (set-buffer (marker-buffer org-log-note-marker))
  8113. (save-excursion
  8114. (goto-char org-log-note-marker)
  8115. (move-marker org-log-note-marker nil)
  8116. (end-of-line 1)
  8117. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8118. (indent-relative nil)
  8119. (insert "- " (pop lines))
  8120. (org-indent-line-function)
  8121. (beginning-of-line 1)
  8122. (looking-at "[ \t]*")
  8123. (setq ind (concat (match-string 0) " "))
  8124. (end-of-line 1)
  8125. (while lines (insert "\n" ind (pop lines)))))))
  8126. (set-window-configuration org-log-note-window-configuration)
  8127. (with-current-buffer (marker-buffer org-log-note-return-to)
  8128. (goto-char org-log-note-return-to))
  8129. (move-marker org-log-note-return-to nil)
  8130. (and org-log-post-message (message "%s" org-log-post-message)))
  8131. (defun org-sparse-tree (&optional arg)
  8132. "Create a sparse tree, prompt for the details.
  8133. This command can create sparse trees. You first need to select the type
  8134. of match used to create the tree:
  8135. t Show entries with a specific TODO keyword.
  8136. T Show entries selected by a tags match.
  8137. p Enter a property name and its value (both with completion on existing
  8138. names/values) and show entries with that property.
  8139. r Show entries matching a regular expression
  8140. d Show deadlines due within `org-deadline-warning-days'."
  8141. (interactive "P")
  8142. (let (ans kwd value)
  8143. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8144. (setq ans (read-char-exclusive))
  8145. (cond
  8146. ((equal ans ?d)
  8147. (call-interactively 'org-check-deadlines))
  8148. ((equal ans ?b)
  8149. (call-interactively 'org-check-before-date))
  8150. ((equal ans ?t)
  8151. (org-show-todo-tree '(4)))
  8152. ((equal ans ?T)
  8153. (call-interactively 'org-tags-sparse-tree))
  8154. ((member ans '(?p ?P))
  8155. (setq kwd (org-ido-completing-read "Property: "
  8156. (mapcar 'list (org-buffer-property-keys))))
  8157. (setq value (org-ido-completing-read "Value: "
  8158. (mapcar 'list (org-property-values kwd))))
  8159. (unless (string-match "\\`{.*}\\'" value)
  8160. (setq value (concat "\"" value "\"")))
  8161. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8162. ((member ans '(?r ?R ?/))
  8163. (call-interactively 'org-occur))
  8164. (t (error "No such sparse tree command \"%c\"" ans)))))
  8165. (defvar org-occur-highlights nil
  8166. "List of overlays used for occur matches.")
  8167. (make-variable-buffer-local 'org-occur-highlights)
  8168. (defvar org-occur-parameters nil
  8169. "Parameters of the active org-occur calls.
  8170. This is a list, each call to org-occur pushes as cons cell,
  8171. containing the regular expression and the callback, onto the list.
  8172. The list can contain several entries if `org-occur' has been called
  8173. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8174. will only contain one set of parameters. When the highlights are
  8175. removed (for example with `C-c C-c', or with the next edit (depending
  8176. on `org-remove-highlights-with-change'), this variable is emptied
  8177. as well.")
  8178. (make-variable-buffer-local 'org-occur-parameters)
  8179. (defun org-occur (regexp &optional keep-previous callback)
  8180. "Make a compact tree which shows all matches of REGEXP.
  8181. The tree will show the lines where the regexp matches, and all higher
  8182. headlines above the match. It will also show the heading after the match,
  8183. to make sure editing the matching entry is easy.
  8184. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8185. call to `org-occur' will be kept, to allow stacking of calls to this
  8186. command.
  8187. If CALLBACK is non-nil, it is a function which is called to confirm
  8188. that the match should indeed be shown."
  8189. (interactive "sRegexp: \nP")
  8190. (unless keep-previous
  8191. (org-remove-occur-highlights nil nil t))
  8192. (push (cons regexp callback) org-occur-parameters)
  8193. (let ((cnt 0))
  8194. (save-excursion
  8195. (goto-char (point-min))
  8196. (if (or (not keep-previous) ; do not want to keep
  8197. (not org-occur-highlights)) ; no previous matches
  8198. ;; hide everything
  8199. (org-overview))
  8200. (while (re-search-forward regexp nil t)
  8201. (when (or (not callback)
  8202. (save-match-data (funcall callback)))
  8203. (setq cnt (1+ cnt))
  8204. (when org-highlight-sparse-tree-matches
  8205. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8206. (org-show-context 'occur-tree))))
  8207. (when org-remove-highlights-with-change
  8208. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8209. nil 'local))
  8210. (unless org-sparse-tree-open-archived-trees
  8211. (org-hide-archived-subtrees (point-min) (point-max)))
  8212. (run-hooks 'org-occur-hook)
  8213. (if (interactive-p)
  8214. (message "%d match(es) for regexp %s" cnt regexp))
  8215. cnt))
  8216. (defun org-show-context (&optional key)
  8217. "Make sure point and context and visible.
  8218. How much context is shown depends upon the variables
  8219. `org-show-hierarchy-above', `org-show-following-heading'. and
  8220. `org-show-siblings'."
  8221. (let ((heading-p (org-on-heading-p t))
  8222. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8223. (following-p (org-get-alist-option org-show-following-heading key))
  8224. (entry-p (org-get-alist-option org-show-entry-below key))
  8225. (siblings-p (org-get-alist-option org-show-siblings key)))
  8226. (catch 'exit
  8227. ;; Show heading or entry text
  8228. (if (and heading-p (not entry-p))
  8229. (org-flag-heading nil) ; only show the heading
  8230. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8231. (org-show-hidden-entry))) ; show entire entry
  8232. (when following-p
  8233. ;; Show next sibling, or heading below text
  8234. (save-excursion
  8235. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8236. (org-flag-heading nil))))
  8237. (when siblings-p (org-show-siblings))
  8238. (when hierarchy-p
  8239. ;; show all higher headings, possibly with siblings
  8240. (save-excursion
  8241. (while (and (condition-case nil
  8242. (progn (org-up-heading-all 1) t)
  8243. (error nil))
  8244. (not (bobp)))
  8245. (org-flag-heading nil)
  8246. (when siblings-p (org-show-siblings))))))))
  8247. (defun org-reveal (&optional siblings)
  8248. "Show current entry, hierarchy above it, and the following headline.
  8249. This can be used to show a consistent set of context around locations
  8250. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8251. not t for the search context.
  8252. With optional argument SIBLINGS, on each level of the hierarchy all
  8253. siblings are shown. This repairs the tree structure to what it would
  8254. look like when opened with hierarchical calls to `org-cycle'."
  8255. (interactive "P")
  8256. (let ((org-show-hierarchy-above t)
  8257. (org-show-following-heading t)
  8258. (org-show-siblings (if siblings t org-show-siblings)))
  8259. (org-show-context nil)))
  8260. (defun org-highlight-new-match (beg end)
  8261. "Highlight from BEG to END and mark the highlight is an occur headline."
  8262. (let ((ov (org-make-overlay beg end)))
  8263. (org-overlay-put ov 'face 'secondary-selection)
  8264. (push ov org-occur-highlights)))
  8265. (defun org-remove-occur-highlights (&optional beg end noremove)
  8266. "Remove the occur highlights from the buffer.
  8267. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8268. from the `before-change-functions' in the current buffer."
  8269. (interactive)
  8270. (unless org-inhibit-highlight-removal
  8271. (mapc 'org-delete-overlay org-occur-highlights)
  8272. (setq org-occur-highlights nil)
  8273. (setq org-occur-parameters nil)
  8274. (unless noremove
  8275. (remove-hook 'before-change-functions
  8276. 'org-remove-occur-highlights 'local))))
  8277. ;;;; Priorities
  8278. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8279. "Regular expression matching the priority indicator.")
  8280. (defvar org-remove-priority-next-time nil)
  8281. (defun org-priority-up ()
  8282. "Increase the priority of the current item."
  8283. (interactive)
  8284. (org-priority 'up))
  8285. (defun org-priority-down ()
  8286. "Decrease the priority of the current item."
  8287. (interactive)
  8288. (org-priority 'down))
  8289. (defun org-priority (&optional action)
  8290. "Change the priority of an item by ARG.
  8291. ACTION can be `set', `up', `down', or a character."
  8292. (interactive)
  8293. (setq action (or action 'set))
  8294. (let (current new news have remove)
  8295. (save-excursion
  8296. (org-back-to-heading)
  8297. (if (looking-at org-priority-regexp)
  8298. (setq current (string-to-char (match-string 2))
  8299. have t)
  8300. (setq current org-default-priority))
  8301. (cond
  8302. ((or (eq action 'set)
  8303. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8304. (if (not (eq action 'set))
  8305. (setq new action)
  8306. (message "Priority %c-%c, SPC to remove: "
  8307. org-highest-priority org-lowest-priority)
  8308. (setq new (read-char-exclusive)))
  8309. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8310. (= (upcase org-lowest-priority) org-lowest-priority))
  8311. (setq new (upcase new)))
  8312. (cond ((equal new ?\ ) (setq remove t))
  8313. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8314. (error "Priority must be between `%c' and `%c'"
  8315. org-highest-priority org-lowest-priority))))
  8316. ((eq action 'up)
  8317. (if (and (not have) (eq last-command this-command))
  8318. (setq new org-lowest-priority)
  8319. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8320. org-default-priority (1- current)))))
  8321. ((eq action 'down)
  8322. (if (and (not have) (eq last-command this-command))
  8323. (setq new org-highest-priority)
  8324. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8325. org-default-priority (1+ current)))))
  8326. (t (error "Invalid action")))
  8327. (if (or (< (upcase new) org-highest-priority)
  8328. (> (upcase new) org-lowest-priority))
  8329. (setq remove t))
  8330. (setq news (format "%c" new))
  8331. (if have
  8332. (if remove
  8333. (replace-match "" t t nil 1)
  8334. (replace-match news t t nil 2))
  8335. (if remove
  8336. (error "No priority cookie found in line")
  8337. (looking-at org-todo-line-regexp)
  8338. (if (match-end 2)
  8339. (progn
  8340. (goto-char (match-end 2))
  8341. (insert " [#" news "]"))
  8342. (goto-char (match-beginning 3))
  8343. (insert "[#" news "] ")))))
  8344. (org-preserve-lc (org-set-tags nil 'align))
  8345. (if remove
  8346. (message "Priority removed")
  8347. (message "Priority of current item set to %s" news))))
  8348. (defun org-get-priority (s)
  8349. "Find priority cookie and return priority."
  8350. (save-match-data
  8351. (if (not (string-match org-priority-regexp s))
  8352. (* 1000 (- org-lowest-priority org-default-priority))
  8353. (* 1000 (- org-lowest-priority
  8354. (string-to-char (match-string 2 s)))))))
  8355. ;;;; Tags
  8356. (defvar org-agenda-archives-mode)
  8357. (defun org-scan-tags (action matcher &optional todo-only)
  8358. "Scan headline tags with inheritance and produce output ACTION.
  8359. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8360. or `agenda' to produce an entry list for an agenda view. It can also be
  8361. a Lisp form or a function that should be called at each matched headline, in
  8362. this case the return value is a list of all return values from these calls.
  8363. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8364. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8365. only lines with a TODO keyword are included in the output."
  8366. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8367. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8368. (org-re
  8369. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8370. (props (list 'face 'default
  8371. 'done-face 'org-done
  8372. 'undone-face 'default
  8373. 'mouse-face 'highlight
  8374. 'org-not-done-regexp org-not-done-regexp
  8375. 'org-todo-regexp org-todo-regexp
  8376. 'keymap org-agenda-keymap
  8377. 'help-echo
  8378. (format "mouse-2 or RET jump to org file %s"
  8379. (abbreviate-file-name
  8380. (or (buffer-file-name (buffer-base-buffer))
  8381. (buffer-name (buffer-base-buffer)))))))
  8382. (case-fold-search nil)
  8383. lspos tags tags-list
  8384. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8385. (llast 0) rtn rtn1 level category i txt
  8386. todo marker entry priority)
  8387. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8388. (setq action (list 'lambda nil action)))
  8389. (save-excursion
  8390. (goto-char (point-min))
  8391. (when (eq action 'sparse-tree)
  8392. (org-overview)
  8393. (org-remove-occur-highlights))
  8394. (while (re-search-forward re nil t)
  8395. (catch :skip
  8396. (setq todo (if (match-end 1) (match-string 2))
  8397. tags (if (match-end 4) (match-string 4)))
  8398. (goto-char (setq lspos (1+ (match-beginning 0))))
  8399. (setq level (org-reduced-level (funcall outline-level))
  8400. category (org-get-category))
  8401. (setq i llast llast level)
  8402. ;; remove tag lists from same and sublevels
  8403. (while (>= i level)
  8404. (when (setq entry (assoc i tags-alist))
  8405. (setq tags-alist (delete entry tags-alist)))
  8406. (setq i (1- i)))
  8407. ;; add the next tags
  8408. (when tags
  8409. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8410. tags-alist
  8411. (cons (cons level tags) tags-alist)))
  8412. ;; compile tags for current headline
  8413. (setq tags-list
  8414. (if org-use-tag-inheritance
  8415. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8416. tags))
  8417. (when org-use-tag-inheritance
  8418. (setcdr (car tags-alist)
  8419. (mapcar (lambda (x)
  8420. (setq x (copy-sequence x))
  8421. (org-add-prop-inherited x))
  8422. (cdar tags-alist))))
  8423. (when (and tags org-use-tag-inheritance
  8424. (not (eq t org-use-tag-inheritance)))
  8425. ;; selective inheritance, remove uninherited ones
  8426. (setcdr (car tags-alist)
  8427. (org-remove-uniherited-tags (cdar tags-alist))))
  8428. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8429. (let ((case-fold-search t)) (eval matcher))
  8430. (or
  8431. (not (member org-archive-tag tags-list))
  8432. ;; we have an archive tag, should we use this anyway?
  8433. (or (not org-agenda-skip-archived-trees)
  8434. (and (eq action 'agenda) org-agenda-archives-mode))))
  8435. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8436. ;; select this headline
  8437. (cond
  8438. ((eq action 'sparse-tree)
  8439. (and org-highlight-sparse-tree-matches
  8440. (org-get-heading) (match-end 0)
  8441. (org-highlight-new-match
  8442. (match-beginning 0) (match-beginning 1)))
  8443. (org-show-context 'tags-tree))
  8444. ((eq action 'agenda)
  8445. (setq txt (org-format-agenda-item
  8446. ""
  8447. (concat
  8448. (if org-tags-match-list-sublevels
  8449. (make-string (1- level) ?.) "")
  8450. (org-get-heading))
  8451. category (org-get-tags-at))
  8452. priority (org-get-priority txt))
  8453. (goto-char lspos)
  8454. (setq marker (org-agenda-new-marker))
  8455. (org-add-props txt props
  8456. 'org-marker marker 'org-hd-marker marker 'org-category category
  8457. 'priority priority 'type "tagsmatch")
  8458. (push txt rtn))
  8459. ((functionp action)
  8460. (save-excursion
  8461. (setq rtn1 (funcall action))
  8462. (push rtn1 rtn))
  8463. (goto-char (point-at-eol)))
  8464. (t (error "Invalid action")))
  8465. ;; if we are to skip sublevels, jump to end of subtree
  8466. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8467. (when (and (eq action 'sparse-tree)
  8468. (not org-sparse-tree-open-archived-trees))
  8469. (org-hide-archived-subtrees (point-min) (point-max)))
  8470. (nreverse rtn)))
  8471. (defun org-remove-uniherited-tags (tags)
  8472. "Remove all tags that are not inherited from the list TAGS."
  8473. (cond
  8474. ((eq org-use-tag-inheritance t)
  8475. (if org-tags-exclude-from-inheritance
  8476. (org-delete-all org-tags-exclude-from-inheritance tags)
  8477. tags))
  8478. ((not org-use-tag-inheritance) nil)
  8479. ((stringp org-use-tag-inheritance)
  8480. (delq nil (mapcar
  8481. (lambda (x)
  8482. (if (and (string-match org-use-tag-inheritance x)
  8483. (not (member x org-tags-exclude-from-inheritance)))
  8484. x nil))
  8485. tags)))
  8486. ((listp org-use-tag-inheritance)
  8487. (delq nil (mapcar
  8488. (lambda (x)
  8489. (if (member x org-use-tag-inheritance) x nil))
  8490. tags)))))
  8491. (defvar todo-only) ;; dynamically scoped
  8492. (defun org-tags-sparse-tree (&optional todo-only match)
  8493. "Create a sparse tree according to tags string MATCH.
  8494. MATCH can contain positive and negative selection of tags, like
  8495. \"+WORK+URGENT-WITHBOSS\".
  8496. If optional argument TODO-ONLY is non-nil, only select lines that are
  8497. also TODO lines."
  8498. (interactive "P")
  8499. (org-prepare-agenda-buffers (list (current-buffer)))
  8500. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8501. (defvar org-cached-props nil)
  8502. (defun org-cached-entry-get (pom property)
  8503. (if (or (eq t org-use-property-inheritance)
  8504. (and (stringp org-use-property-inheritance)
  8505. (string-match org-use-property-inheritance property))
  8506. (and (listp org-use-property-inheritance)
  8507. (member property org-use-property-inheritance)))
  8508. ;; Caching is not possible, check it directly
  8509. (org-entry-get pom property 'inherit)
  8510. ;; Get all properties, so that we can do complicated checks easily
  8511. (cdr (assoc property (or org-cached-props
  8512. (setq org-cached-props
  8513. (org-entry-properties pom)))))))
  8514. (defun org-global-tags-completion-table (&optional files)
  8515. "Return the list of all tags in all agenda buffer/files."
  8516. (save-excursion
  8517. (org-uniquify
  8518. (delq nil
  8519. (apply 'append
  8520. (mapcar
  8521. (lambda (file)
  8522. (set-buffer (find-file-noselect file))
  8523. (append (org-get-buffer-tags)
  8524. (mapcar (lambda (x) (if (stringp (car-safe x))
  8525. (list (car-safe x)) nil))
  8526. org-tag-alist)))
  8527. (if (and files (car files))
  8528. files
  8529. (org-agenda-files))))))))
  8530. (defun org-make-tags-matcher (match)
  8531. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8532. ;; todo-only is scoped dynamically into this function, and the function
  8533. ;; may change it if the matcher asks for it.
  8534. (unless match
  8535. ;; Get a new match request, with completion
  8536. (let ((org-last-tags-completion-table
  8537. (org-global-tags-completion-table)))
  8538. (setq match (org-ido-completing-read
  8539. "Match: " 'org-tags-completion-function nil nil nil
  8540. 'org-tags-history))))
  8541. ;; Parse the string and create a lisp form
  8542. (let ((match0 match)
  8543. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8544. minus tag mm
  8545. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8546. orterms term orlist re-p str-p level-p level-op time-p
  8547. prop-p pn pv po cat-p gv rest)
  8548. (if (string-match "/+" match)
  8549. ;; match contains also a todo-matching request
  8550. (progn
  8551. (setq tagsmatch (substring match 0 (match-beginning 0))
  8552. todomatch (substring match (match-end 0)))
  8553. (if (string-match "^!" todomatch)
  8554. (setq todo-only t todomatch (substring todomatch 1)))
  8555. (if (string-match "^\\s-*$" todomatch)
  8556. (setq todomatch nil)))
  8557. ;; only matching tags
  8558. (setq tagsmatch match todomatch nil))
  8559. ;; Make the tags matcher
  8560. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8561. (setq tagsmatcher t)
  8562. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8563. (while (setq term (pop orterms))
  8564. (while (and (equal (substring term -1) "\\") orterms)
  8565. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8566. (while (string-match re term)
  8567. (setq rest (substring term (match-end 0))
  8568. minus (and (match-end 1)
  8569. (equal (match-string 1 term) "-"))
  8570. tag (match-string 2 term)
  8571. re-p (equal (string-to-char tag) ?{)
  8572. level-p (match-end 4)
  8573. prop-p (match-end 5)
  8574. mm (cond
  8575. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8576. (level-p
  8577. (setq level-op (org-op-to-function (match-string 3 term)))
  8578. `(,level-op level ,(string-to-number
  8579. (match-string 4 term))))
  8580. (prop-p
  8581. (setq pn (match-string 5 term)
  8582. po (match-string 6 term)
  8583. pv (match-string 7 term)
  8584. cat-p (equal pn "CATEGORY")
  8585. re-p (equal (string-to-char pv) ?{)
  8586. str-p (equal (string-to-char pv) ?\")
  8587. time-p (save-match-data
  8588. (string-match "^\"[[<].*[]>]\"$" pv))
  8589. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8590. (if time-p (setq pv (org-matcher-time pv)))
  8591. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8592. (cond
  8593. ((equal pn "CATEGORY")
  8594. (setq gv '(get-text-property (point) 'org-category)))
  8595. ((equal pn "TODO")
  8596. (setq gv 'todo))
  8597. (t
  8598. (setq gv `(org-cached-entry-get nil ,pn))))
  8599. (if re-p
  8600. (if (eq po 'org<>)
  8601. `(not (string-match ,pv (or ,gv "")))
  8602. `(string-match ,pv (or ,gv "")))
  8603. (if str-p
  8604. `(,po (or ,gv "") ,pv)
  8605. `(,po (string-to-number (or ,gv ""))
  8606. ,(string-to-number pv) ))))
  8607. (t `(member ,(downcase tag) tags-list)))
  8608. mm (if minus (list 'not mm) mm)
  8609. term rest)
  8610. (push mm tagsmatcher))
  8611. (push (if (> (length tagsmatcher) 1)
  8612. (cons 'and tagsmatcher)
  8613. (car tagsmatcher))
  8614. orlist)
  8615. (setq tagsmatcher nil))
  8616. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8617. (setq tagsmatcher
  8618. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8619. ;; Make the todo matcher
  8620. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8621. (setq todomatcher t)
  8622. (setq orterms (org-split-string todomatch "|") orlist nil)
  8623. (while (setq term (pop orterms))
  8624. (while (string-match re term)
  8625. (setq minus (and (match-end 1)
  8626. (equal (match-string 1 term) "-"))
  8627. kwd (match-string 2 term)
  8628. re-p (equal (string-to-char kwd) ?{)
  8629. term (substring term (match-end 0))
  8630. mm (if re-p
  8631. `(string-match ,(substring kwd 1 -1) todo)
  8632. (list 'equal 'todo kwd))
  8633. mm (if minus (list 'not mm) mm))
  8634. (push mm todomatcher))
  8635. (push (if (> (length todomatcher) 1)
  8636. (cons 'and todomatcher)
  8637. (car todomatcher))
  8638. orlist)
  8639. (setq todomatcher nil))
  8640. (setq todomatcher (if (> (length orlist) 1)
  8641. (cons 'or orlist) (car orlist))))
  8642. ;; Return the string and lisp forms of the matcher
  8643. (setq matcher (if todomatcher
  8644. (list 'and tagsmatcher todomatcher)
  8645. tagsmatcher))
  8646. (cons match0 matcher)))
  8647. (defun org-op-to-function (op &optional stringp)
  8648. "Turn an operator into the appropriate function."
  8649. (setq op
  8650. (cond
  8651. ((equal op "<" ) '(< string< org-time<))
  8652. ((equal op ">" ) '(> org-string> org-time>))
  8653. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8654. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8655. ((member op '("=" "==")) '(= string= org-time=))
  8656. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8657. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8658. (defun org<> (a b) (not (= a b)))
  8659. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8660. (defun org-string>= (a b) (not (string< a b)))
  8661. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8662. (defun org-string<> (a b) (not (string= a b)))
  8663. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8664. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8665. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8666. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8667. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8668. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8669. (defun org-2ft (s)
  8670. "Convert S to a floating point time.
  8671. If S is already a number, just return it. If it is a string, parse
  8672. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8673. (cond
  8674. ((numberp s) s)
  8675. ((stringp s)
  8676. (condition-case nil
  8677. (float-time (apply 'encode-time (org-parse-time-string s)))
  8678. (error 0.)))
  8679. (t 0.)))
  8680. (defun org-time-today ()
  8681. "Time in seconds today at 0:00.
  8682. Returns the float number of seconds since the beginning of the
  8683. epoch to the beginning of today (00:00)."
  8684. (float-time (apply 'encode-time
  8685. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8686. (defun org-matcher-time (s)
  8687. "Interpret a time comparison value."
  8688. (save-match-data
  8689. (cond
  8690. ((string= s "<now>") (float-time))
  8691. ((string= s "<today>") (org-time-today))
  8692. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8693. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8694. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8695. (+ (org-time-today)
  8696. (* (string-to-number (match-string 1 s))
  8697. (cdr (assoc (match-string 2 s)
  8698. '(("d" . 86400.0) ("w" . 604800.0)
  8699. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8700. (t (org-2ft s)))))
  8701. (defun org-match-any-p (re list)
  8702. "Does re match any element of list?"
  8703. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8704. (delq nil list))
  8705. (defvar org-add-colon-after-tag-completion nil) ;; dynamically scoped param
  8706. (defvar org-tags-overlay (org-make-overlay 1 1))
  8707. (org-detach-overlay org-tags-overlay)
  8708. (defun org-get-local-tags-at (&optional pos)
  8709. "Get a list of tags defined in the current headline."
  8710. (org-get-tags-at pos 'local))
  8711. (defun org-get-local-tags ()
  8712. "Get a list of tags defined in the current headline."
  8713. (org-get-tags-at nil 'local))
  8714. (defun org-get-tags-at (&optional pos local)
  8715. "Get a list of all headline tags applicable at POS.
  8716. POS defaults to point. If tags are inherited, the list contains
  8717. the targets in the same sequence as the headlines appear, i.e.
  8718. the tags of the current headline come last.
  8719. When LOCAL is non-nil, only return tags from the current headline,
  8720. ignore inherited ones."
  8721. (interactive)
  8722. (let (tags ltags lastpos parent)
  8723. (save-excursion
  8724. (save-restriction
  8725. (widen)
  8726. (goto-char (or pos (point)))
  8727. (save-match-data
  8728. (catch 'done
  8729. (condition-case nil
  8730. (progn
  8731. (org-back-to-heading t)
  8732. (while (not (equal lastpos (point)))
  8733. (setq lastpos (point))
  8734. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8735. (setq ltags (org-split-string
  8736. (org-match-string-no-properties 1) ":"))
  8737. (when parent
  8738. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8739. (setq tags (append
  8740. (if parent
  8741. (org-remove-uniherited-tags ltags)
  8742. ltags)
  8743. tags)))
  8744. (or org-use-tag-inheritance (throw 'done t))
  8745. (if local (throw 'done t))
  8746. (org-up-heading-all 1)
  8747. (setq parent t)))
  8748. (error nil)))))
  8749. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8750. (defun org-add-prop-inherited (s)
  8751. (add-text-properties 0 (length s) '(inherited t) s)
  8752. s)
  8753. (defun org-toggle-tag (tag &optional onoff)
  8754. "Toggle the tag TAG for the current line.
  8755. If ONOFF is `on' or `off', don't toggle but set to this state."
  8756. (let (res current)
  8757. (save-excursion
  8758. (org-back-to-heading t)
  8759. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8760. (point-at-eol) t)
  8761. (progn
  8762. (setq current (match-string 1))
  8763. (replace-match ""))
  8764. (setq current ""))
  8765. (setq current (nreverse (org-split-string current ":")))
  8766. (cond
  8767. ((eq onoff 'on)
  8768. (setq res t)
  8769. (or (member tag current) (push tag current)))
  8770. ((eq onoff 'off)
  8771. (or (not (member tag current)) (setq current (delete tag current))))
  8772. (t (if (member tag current)
  8773. (setq current (delete tag current))
  8774. (setq res t)
  8775. (push tag current))))
  8776. (end-of-line 1)
  8777. (if current
  8778. (progn
  8779. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8780. (org-set-tags nil t))
  8781. (delete-horizontal-space))
  8782. (run-hooks 'org-after-tags-change-hook))
  8783. res))
  8784. (defun org-align-tags-here (to-col)
  8785. ;; Assumes that this is a headline
  8786. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8787. (beginning-of-line 1)
  8788. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8789. (< pos (match-beginning 2)))
  8790. (progn
  8791. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8792. (goto-char (match-beginning 1))
  8793. (insert " ")
  8794. (delete-region (point) (1+ (match-beginning 2)))
  8795. (setq ncol (max (1+ (current-column))
  8796. (1+ col)
  8797. (if (> to-col 0)
  8798. to-col
  8799. (- (abs to-col) tags-l))))
  8800. (setq p (point))
  8801. (insert (make-string (- ncol (current-column)) ?\ ))
  8802. (setq ncol (current-column))
  8803. (when indent-tabs-mode (tabify p (point-at-eol)))
  8804. (org-move-to-column (min ncol col) t))
  8805. (goto-char pos))))
  8806. (defun org-set-tags-command (&optional arg just-align)
  8807. "Call the set-tags command for the current entry."
  8808. (interactive "P")
  8809. (if (org-on-heading-p)
  8810. (org-set-tags arg just-align)
  8811. (save-excursion
  8812. (org-back-to-heading t)
  8813. (org-set-tags arg just-align))))
  8814. (defun org-set-tags (&optional arg just-align)
  8815. "Set the tags for the current headline.
  8816. With prefix ARG, realign all tags in headings in the current buffer."
  8817. (interactive "P")
  8818. (let* ((re (concat "^" outline-regexp))
  8819. (current (org-get-tags-string))
  8820. (col (current-column))
  8821. (org-setting-tags t)
  8822. table current-tags inherited-tags ; computed below when needed
  8823. tags p0 c0 c1 rpl)
  8824. (if arg
  8825. (save-excursion
  8826. (goto-char (point-min))
  8827. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8828. (while (re-search-forward re nil t)
  8829. (org-set-tags nil t)
  8830. (end-of-line 1)))
  8831. (message "All tags realigned to column %d" org-tags-column))
  8832. (if just-align
  8833. (setq tags current)
  8834. ;; Get a new set of tags from the user
  8835. (save-excursion
  8836. (setq table (or org-tag-alist (org-get-buffer-tags))
  8837. org-last-tags-completion-table table
  8838. current-tags (org-split-string current ":")
  8839. inherited-tags (nreverse
  8840. (nthcdr (length current-tags)
  8841. (nreverse (org-get-tags-at))))
  8842. tags
  8843. (if (or (eq t org-use-fast-tag-selection)
  8844. (and org-use-fast-tag-selection
  8845. (delq nil (mapcar 'cdr table))))
  8846. (org-fast-tag-selection
  8847. current-tags inherited-tags table
  8848. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8849. (let ((org-add-colon-after-tag-completion t))
  8850. (org-trim
  8851. (org-without-partial-completion
  8852. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8853. nil nil current 'org-tags-history)))))))
  8854. (while (string-match "[-+&]+" tags)
  8855. ;; No boolean logic, just a list
  8856. (setq tags (replace-match ":" t t tags))))
  8857. (if (string-match "\\`[\t ]*\\'" tags)
  8858. (setq tags "")
  8859. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8860. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8861. ;; Insert new tags at the correct column
  8862. (beginning-of-line 1)
  8863. (cond
  8864. ((and (equal current "") (equal tags "")))
  8865. ((re-search-forward
  8866. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8867. (point-at-eol) t)
  8868. (if (equal tags "")
  8869. (setq rpl "")
  8870. (goto-char (match-beginning 0))
  8871. (setq c0 (current-column) p0 (point)
  8872. c1 (max (1+ c0) (if (> org-tags-column 0)
  8873. org-tags-column
  8874. (- (- org-tags-column) (length tags))))
  8875. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8876. (replace-match rpl t t)
  8877. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8878. tags)
  8879. (t (error "Tags alignment failed")))
  8880. (org-move-to-column col)
  8881. (unless just-align
  8882. (run-hooks 'org-after-tags-change-hook)))))
  8883. (defun org-change-tag-in-region (beg end tag off)
  8884. "Add or remove TAG for each entry in the region.
  8885. This works in the agenda, and also in an org-mode buffer."
  8886. (interactive
  8887. (list (region-beginning) (region-end)
  8888. (let ((org-last-tags-completion-table
  8889. (if (org-mode-p)
  8890. (org-get-buffer-tags)
  8891. (org-global-tags-completion-table))))
  8892. (org-ido-completing-read
  8893. "Tag: " 'org-tags-completion-function nil nil nil
  8894. 'org-tags-history))
  8895. (progn
  8896. (message "[s]et or [r]emove? ")
  8897. (equal (read-char-exclusive) ?r))))
  8898. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8899. (let ((agendap (equal major-mode 'org-agenda-mode))
  8900. l1 l2 m buf pos newhead (cnt 0))
  8901. (goto-char end)
  8902. (setq l2 (1- (org-current-line)))
  8903. (goto-char beg)
  8904. (setq l1 (org-current-line))
  8905. (loop for l from l1 to l2 do
  8906. (goto-line l)
  8907. (setq m (get-text-property (point) 'org-hd-marker))
  8908. (when (or (and (org-mode-p) (org-on-heading-p))
  8909. (and agendap m))
  8910. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8911. pos (if agendap m (point)))
  8912. (with-current-buffer buf
  8913. (save-excursion
  8914. (save-restriction
  8915. (goto-char pos)
  8916. (setq cnt (1+ cnt))
  8917. (org-toggle-tag tag (if off 'off 'on))
  8918. (setq newhead (org-get-heading)))))
  8919. (and agendap (org-agenda-change-all-lines newhead m))))
  8920. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8921. (defun org-tags-completion-function (string predicate &optional flag)
  8922. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8923. (confirm (lambda (x) (stringp (car x)))))
  8924. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8925. (setq s1 (match-string 1 string)
  8926. s2 (match-string 2 string))
  8927. (setq s1 "" s2 string))
  8928. (cond
  8929. ((eq flag nil)
  8930. ;; try completion
  8931. (setq rtn (try-completion s2 ctable confirm))
  8932. (if (stringp rtn)
  8933. (setq rtn
  8934. (concat s1 s2 (substring rtn (length s2))
  8935. (if (and org-add-colon-after-tag-completion
  8936. (assoc rtn ctable))
  8937. ":" ""))))
  8938. rtn)
  8939. ((eq flag t)
  8940. ;; all-completions
  8941. (all-completions s2 ctable confirm)
  8942. )
  8943. ((eq flag 'lambda)
  8944. ;; exact match?
  8945. (assoc s2 ctable)))
  8946. ))
  8947. (defun org-fast-tag-insert (kwd tags face &optional end)
  8948. "Insert KDW, and the TAGS, the latter with face FACE. Also insert END."
  8949. (insert (format "%-12s" (concat kwd ":"))
  8950. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8951. (or end "")))
  8952. (defun org-fast-tag-show-exit (flag)
  8953. (save-excursion
  8954. (goto-line 3)
  8955. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8956. (replace-match ""))
  8957. (when flag
  8958. (end-of-line 1)
  8959. (org-move-to-column (- (window-width) 19) t)
  8960. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8961. (defun org-set-current-tags-overlay (current prefix)
  8962. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8963. (if (featurep 'xemacs)
  8964. (org-overlay-display org-tags-overlay (concat prefix s)
  8965. 'secondary-selection)
  8966. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8967. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8968. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8969. "Fast tag selection with single keys.
  8970. CURRENT is the current list of tags in the headline, INHERITED is the
  8971. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8972. possibly with grouping information. TODO-TABLE is a similar table with
  8973. TODO keywords, should these have keys assigned to them.
  8974. If the keys are nil, a-z are automatically assigned.
  8975. Returns the new tags string, or nil to not change the current settings."
  8976. (let* ((fulltable (append table todo-table))
  8977. (maxlen (apply 'max (mapcar
  8978. (lambda (x)
  8979. (if (stringp (car x)) (string-width (car x)) 0))
  8980. fulltable)))
  8981. (buf (current-buffer))
  8982. (expert (eq org-fast-tag-selection-single-key 'expert))
  8983. (buffer-tags nil)
  8984. (fwidth (+ maxlen 3 1 3))
  8985. (ncol (/ (- (window-width) 4) fwidth))
  8986. (i-face 'org-done)
  8987. (c-face 'org-todo)
  8988. tg cnt e c char c1 c2 ntable tbl rtn
  8989. ov-start ov-end ov-prefix
  8990. (exit-after-next org-fast-tag-selection-single-key)
  8991. (done-keywords org-done-keywords)
  8992. groups ingroup)
  8993. (save-excursion
  8994. (beginning-of-line 1)
  8995. (if (looking-at
  8996. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8997. (setq ov-start (match-beginning 1)
  8998. ov-end (match-end 1)
  8999. ov-prefix "")
  9000. (setq ov-start (1- (point-at-eol))
  9001. ov-end (1+ ov-start))
  9002. (skip-chars-forward "^\n\r")
  9003. (setq ov-prefix
  9004. (concat
  9005. (buffer-substring (1- (point)) (point))
  9006. (if (> (current-column) org-tags-column)
  9007. " "
  9008. (make-string (- org-tags-column (current-column)) ?\ ))))))
  9009. (org-move-overlay org-tags-overlay ov-start ov-end)
  9010. (save-window-excursion
  9011. (if expert
  9012. (set-buffer (get-buffer-create " *Org tags*"))
  9013. (delete-other-windows)
  9014. (split-window-vertically)
  9015. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  9016. (erase-buffer)
  9017. (org-set-local 'org-done-keywords done-keywords)
  9018. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  9019. (org-fast-tag-insert "Current" current c-face "\n\n")
  9020. (org-fast-tag-show-exit exit-after-next)
  9021. (org-set-current-tags-overlay current ov-prefix)
  9022. (setq tbl fulltable char ?a cnt 0)
  9023. (while (setq e (pop tbl))
  9024. (cond
  9025. ((equal e '(:startgroup))
  9026. (push '() groups) (setq ingroup t)
  9027. (when (not (= cnt 0))
  9028. (setq cnt 0)
  9029. (insert "\n"))
  9030. (insert "{ "))
  9031. ((equal e '(:endgroup))
  9032. (setq ingroup nil cnt 0)
  9033. (insert "}\n"))
  9034. (t
  9035. (setq tg (car e) c2 nil)
  9036. (if (cdr e)
  9037. (setq c (cdr e))
  9038. ;; automatically assign a character.
  9039. (setq c1 (string-to-char
  9040. (downcase (substring
  9041. tg (if (= (string-to-char tg) ?@) 1 0)))))
  9042. (if (or (rassoc c1 ntable) (rassoc c1 table))
  9043. (while (or (rassoc char ntable) (rassoc char table))
  9044. (setq char (1+ char)))
  9045. (setq c2 c1))
  9046. (setq c (or c2 char)))
  9047. (if ingroup (push tg (car groups)))
  9048. (setq tg (org-add-props tg nil 'face
  9049. (cond
  9050. ((not (assoc tg table))
  9051. (org-get-todo-face tg))
  9052. ((member tg current) c-face)
  9053. ((member tg inherited) i-face)
  9054. (t nil))))
  9055. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9056. (insert "[" c "] " tg (make-string
  9057. (- fwidth 4 (length tg)) ?\ ))
  9058. (push (cons tg c) ntable)
  9059. (when (= (setq cnt (1+ cnt)) ncol)
  9060. (insert "\n")
  9061. (if ingroup (insert " "))
  9062. (setq cnt 0)))))
  9063. (setq ntable (nreverse ntable))
  9064. (insert "\n")
  9065. (goto-char (point-min))
  9066. (if (not expert) (org-fit-window-to-buffer))
  9067. (setq rtn
  9068. (catch 'exit
  9069. (while t
  9070. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9071. (if groups " [!] no groups" " [!]groups")
  9072. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9073. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9074. (cond
  9075. ((= c ?\r) (throw 'exit t))
  9076. ((= c ?!)
  9077. (setq groups (not groups))
  9078. (goto-char (point-min))
  9079. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9080. ((= c ?\C-c)
  9081. (if (not expert)
  9082. (org-fast-tag-show-exit
  9083. (setq exit-after-next (not exit-after-next)))
  9084. (setq expert nil)
  9085. (delete-other-windows)
  9086. (split-window-vertically)
  9087. (org-switch-to-buffer-other-window " *Org tags*")
  9088. (org-fit-window-to-buffer)))
  9089. ((or (= c ?\C-g)
  9090. (and (= c ?q) (not (rassoc c ntable))))
  9091. (org-detach-overlay org-tags-overlay)
  9092. (setq quit-flag t))
  9093. ((= c ?\ )
  9094. (setq current nil)
  9095. (if exit-after-next (setq exit-after-next 'now)))
  9096. ((= c ?\t)
  9097. (condition-case nil
  9098. (setq tg (org-ido-completing-read
  9099. "Tag: "
  9100. (or buffer-tags
  9101. (with-current-buffer buf
  9102. (org-get-buffer-tags)))))
  9103. (quit (setq tg "")))
  9104. (when (string-match "\\S-" tg)
  9105. (add-to-list 'buffer-tags (list tg))
  9106. (if (member tg current)
  9107. (setq current (delete tg current))
  9108. (push tg current)))
  9109. (if exit-after-next (setq exit-after-next 'now)))
  9110. ((setq e (rassoc c todo-table) tg (car e))
  9111. (with-current-buffer buf
  9112. (save-excursion (org-todo tg)))
  9113. (if exit-after-next (setq exit-after-next 'now)))
  9114. ((setq e (rassoc c ntable) tg (car e))
  9115. (if (member tg current)
  9116. (setq current (delete tg current))
  9117. (loop for g in groups do
  9118. (if (member tg g)
  9119. (mapc (lambda (x)
  9120. (setq current (delete x current)))
  9121. g)))
  9122. (push tg current))
  9123. (if exit-after-next (setq exit-after-next 'now))))
  9124. ;; Create a sorted list
  9125. (setq current
  9126. (sort current
  9127. (lambda (a b)
  9128. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9129. (if (eq exit-after-next 'now) (throw 'exit t))
  9130. (goto-char (point-min))
  9131. (beginning-of-line 2)
  9132. (delete-region (point) (point-at-eol))
  9133. (org-fast-tag-insert "Current" current c-face)
  9134. (org-set-current-tags-overlay current ov-prefix)
  9135. (while (re-search-forward
  9136. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9137. (setq tg (match-string 1))
  9138. (add-text-properties
  9139. (match-beginning 1) (match-end 1)
  9140. (list 'face
  9141. (cond
  9142. ((member tg current) c-face)
  9143. ((member tg inherited) i-face)
  9144. (t (get-text-property (match-beginning 1) 'face))))))
  9145. (goto-char (point-min)))))
  9146. (org-detach-overlay org-tags-overlay)
  9147. (if rtn
  9148. (mapconcat 'identity current ":")
  9149. nil))))
  9150. (defun org-get-tags-string ()
  9151. "Get the TAGS string in the current headline."
  9152. (unless (org-on-heading-p t)
  9153. (error "Not on a heading"))
  9154. (save-excursion
  9155. (beginning-of-line 1)
  9156. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9157. (org-match-string-no-properties 1)
  9158. "")))
  9159. (defun org-get-tags ()
  9160. "Get the list of tags specified in the current headline."
  9161. (org-split-string (org-get-tags-string) ":"))
  9162. (defun org-get-buffer-tags ()
  9163. "Get a table of all tags used in the buffer, for completion."
  9164. (let (tags)
  9165. (save-excursion
  9166. (goto-char (point-min))
  9167. (while (re-search-forward
  9168. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9169. (when (equal (char-after (point-at-bol 0)) ?*)
  9170. (mapc (lambda (x) (add-to-list 'tags x))
  9171. (org-split-string (org-match-string-no-properties 1) ":")))))
  9172. (mapcar 'list tags)))
  9173. ;;;; The mapping API
  9174. ;;;###autoload
  9175. (defun org-map-entries (func &optional match scope &rest skip)
  9176. "Call FUNC at each headline selected by MATCH in SCOPE.
  9177. FUNC is a function or a lisp form. The function will be called without
  9178. arguments, with the cursor positioned at the beginning of the headline.
  9179. The return values of all calls to the function will be collected and
  9180. returned as a list.
  9181. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9182. Only headlines that are matched by this query will be considered during
  9183. the iteration. When MATCH is nil or t, all headlines will be
  9184. visited by the iteration.
  9185. SCOPE determines the scope of this command. It can be any of:
  9186. nil The current buffer, respecting the restriction if any
  9187. tree The subtree started with the entry at point
  9188. file The current buffer, without restriction
  9189. file-with-archives
  9190. The current buffer, and any archives associated with it
  9191. agenda All agenda files
  9192. agenda-with-archives
  9193. All agenda files with any archive files associated with them
  9194. \(file1 file2 ...)
  9195. If this is a list, all files in the list will be scanned
  9196. The remaining args are treated as settings for the skipping facilities of
  9197. the scanner. The following items can be given here:
  9198. archive skip trees with the archive tag.
  9199. comment skip trees with the COMMENT keyword
  9200. function or Emacs Lisp form:
  9201. will be used as value for `org-agenda-skip-function', so whenever
  9202. the the function returns t, FUNC will not be called for that
  9203. entry and search will continue from the point where the
  9204. function leaves it."
  9205. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9206. (org-agenda-skip-archived-trees (memq 'archive skip))
  9207. (org-agenda-skip-comment-trees (memq 'comment skip))
  9208. (org-agenda-skip-function
  9209. (car (org-delete-all '(comment archive) skip)))
  9210. (org-tags-match-list-sublevels t)
  9211. matcher pos file res
  9212. org-todo-keywords-for-agenda
  9213. org-done-keywords-for-agenda
  9214. org-todo-keyword-alist-for-agenda
  9215. org-tag-alist-for-agenda)
  9216. (cond
  9217. ((eq match t) (setq matcher t))
  9218. ((eq match nil) (setq matcher t))
  9219. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9220. (when (eq scope 'tree)
  9221. (org-back-to-heading t)
  9222. (org-narrow-to-subtree)
  9223. (setq scope nil))
  9224. (if (not scope)
  9225. (progn
  9226. (org-prepare-agenda-buffers
  9227. (list (buffer-file-name (current-buffer))))
  9228. (org-scan-tags func matcher))
  9229. ;; Get the right scope
  9230. (setq pos (point))
  9231. (cond
  9232. ((and scope (listp scope) (symbolp (car scope)))
  9233. (setq scope (eval scope)))
  9234. ((eq scope 'agenda)
  9235. (setq scope (org-agenda-files t)))
  9236. ((eq scope 'agenda-with-archives)
  9237. (setq scope (org-agenda-files t))
  9238. (setq scope (org-add-archive-files scope)))
  9239. ((eq scope 'file)
  9240. (setq scope (list (buffer-file-name))))
  9241. ((eq scope 'file-with-archives)
  9242. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9243. (org-prepare-agenda-buffers scope)
  9244. (while (setq file (pop scope))
  9245. (with-current-buffer (org-find-base-buffer-visiting file)
  9246. (save-excursion
  9247. (save-restriction
  9248. (widen)
  9249. (goto-char (point-min))
  9250. (setq res (append res (org-scan-tags func matcher)))))))
  9251. res)))
  9252. ;;;; Properties
  9253. ;;; Setting and retrieving properties
  9254. (defconst org-special-properties
  9255. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9256. "TIMESTAMP" "TIMESTAMP_IA")
  9257. "The special properties valid in Org-mode.
  9258. These are properties that are not defined in the property drawer,
  9259. but in some other way.")
  9260. (defconst org-default-properties
  9261. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9262. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9263. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9264. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9265. "Some properties that are used by Org-mode for various purposes.
  9266. Being in this list makes sure that they are offered for completion.")
  9267. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9268. "Regular expression matching the first line of a property drawer.")
  9269. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9270. "Regular expression matching the first line of a property drawer.")
  9271. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9272. "Regular expression matching the first line of a property drawer.")
  9273. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9274. "Regular expression matching the first line of a property drawer.")
  9275. (defconst org-property-drawer-re
  9276. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9277. org-property-end-re "\\)\n?")
  9278. "Matches an entire property drawer.")
  9279. (defconst org-clock-drawer-re
  9280. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9281. org-property-end-re "\\)\n?")
  9282. "Matches an entire clock drawer.")
  9283. (defun org-property-action ()
  9284. "Do an action on properties."
  9285. (interactive)
  9286. (let (c)
  9287. (org-at-property-p)
  9288. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9289. (setq c (read-char-exclusive))
  9290. (cond
  9291. ((equal c ?s)
  9292. (call-interactively 'org-set-property))
  9293. ((equal c ?d)
  9294. (call-interactively 'org-delete-property))
  9295. ((equal c ?D)
  9296. (call-interactively 'org-delete-property-globally))
  9297. ((equal c ?c)
  9298. (call-interactively 'org-compute-property-at-point))
  9299. (t (error "No such property action %c" c)))))
  9300. (defun org-at-property-p ()
  9301. "Is the cursor in a property line?"
  9302. ;; FIXME: Does not check if we are actually in the drawer.
  9303. ;; FIXME: also returns true on any drawers.....
  9304. ;; This is used by C-c C-c for property action.
  9305. (save-excursion
  9306. (beginning-of-line 1)
  9307. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9308. (defun org-get-property-block (&optional beg end force)
  9309. "Return the (beg . end) range of the body of the property drawer.
  9310. BEG and END can be beginning and end of subtree, if not given
  9311. they will be found.
  9312. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9313. (catch 'exit
  9314. (save-excursion
  9315. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9316. (end (or end (progn (outline-next-heading) (point)))))
  9317. (goto-char beg)
  9318. (if (re-search-forward org-property-start-re end t)
  9319. (setq beg (1+ (match-end 0)))
  9320. (if force
  9321. (save-excursion
  9322. (org-insert-property-drawer)
  9323. (setq end (progn (outline-next-heading) (point))))
  9324. (throw 'exit nil))
  9325. (goto-char beg)
  9326. (if (re-search-forward org-property-start-re end t)
  9327. (setq beg (1+ (match-end 0)))))
  9328. (if (re-search-forward org-property-end-re end t)
  9329. (setq end (match-beginning 0))
  9330. (or force (throw 'exit nil))
  9331. (goto-char beg)
  9332. (setq end beg)
  9333. (org-indent-line-function)
  9334. (insert ":END:\n"))
  9335. (cons beg end)))))
  9336. (defun org-entry-properties (&optional pom which)
  9337. "Get all properties of the entry at point-or-marker POM.
  9338. This includes the TODO keyword, the tags, time strings for deadline,
  9339. scheduled, and clocking, and any additional properties defined in the
  9340. entry. The return value is an alist, keys may occur multiple times
  9341. if the property key was used several times.
  9342. POM may also be nil, in which case the current entry is used.
  9343. If WHICH is nil or `all', get all properties. If WHICH is
  9344. `special' or `standard', only get that subclass."
  9345. (setq which (or which 'all))
  9346. (org-with-point-at pom
  9347. (let ((clockstr (substring org-clock-string 0 -1))
  9348. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9349. beg end range props sum-props key value string clocksum)
  9350. (save-excursion
  9351. (when (condition-case nil (org-back-to-heading t) (error nil))
  9352. (setq beg (point))
  9353. (setq sum-props (get-text-property (point) 'org-summaries))
  9354. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9355. (outline-next-heading)
  9356. (setq end (point))
  9357. (when (memq which '(all special))
  9358. ;; Get the special properties, like TODO and tags
  9359. (goto-char beg)
  9360. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9361. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9362. (when (looking-at org-priority-regexp)
  9363. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9364. (when (and (setq value (org-get-tags-string))
  9365. (string-match "\\S-" value))
  9366. (push (cons "TAGS" value) props))
  9367. (when (setq value (org-get-tags-at))
  9368. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9369. props))
  9370. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9371. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9372. string (if (equal key clockstr)
  9373. (org-no-properties
  9374. (org-trim
  9375. (buffer-substring
  9376. (match-beginning 3) (goto-char (point-at-eol)))))
  9377. (substring (org-match-string-no-properties 3) 1 -1)))
  9378. (unless key
  9379. (if (= (char-after (match-beginning 3)) ?\[)
  9380. (setq key "TIMESTAMP_IA")
  9381. (setq key "TIMESTAMP")))
  9382. (when (or (equal key clockstr) (not (assoc key props)))
  9383. (push (cons key string) props)))
  9384. )
  9385. (when (memq which '(all standard))
  9386. ;; Get the standard properties, like :PORP: ...
  9387. (setq range (org-get-property-block beg end))
  9388. (when range
  9389. (goto-char (car range))
  9390. (while (re-search-forward
  9391. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9392. (cdr range) t)
  9393. (setq key (org-match-string-no-properties 1)
  9394. value (org-trim (or (org-match-string-no-properties 2) "")))
  9395. (unless (member key excluded)
  9396. (push (cons key (or value "")) props)))))
  9397. (if clocksum
  9398. (push (cons "CLOCKSUM"
  9399. (org-columns-number-to-string (/ (float clocksum) 60.)
  9400. 'add_times))
  9401. props))
  9402. (unless (assoc "CATEGORY" props)
  9403. (setq value (or (org-get-category)
  9404. (progn (org-refresh-category-properties)
  9405. (org-get-category))))
  9406. (push (cons "CATEGORY" value) props))
  9407. (append sum-props (nreverse props)))))))
  9408. (defun org-entry-get (pom property &optional inherit)
  9409. "Get value of PROPERTY for entry at point-or-marker POM.
  9410. If INHERIT is non-nil and the entry does not have the property,
  9411. then also check higher levels of the hierarchy.
  9412. If INHERIT is the symbol `selective', use inheritance only if the setting
  9413. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9414. If the property is present but empty, the return value is the empty string.
  9415. If the property is not present at all, nil is returned."
  9416. (org-with-point-at pom
  9417. (if (and inherit (if (eq inherit 'selective)
  9418. (org-property-inherit-p property)
  9419. t))
  9420. (org-entry-get-with-inheritance property)
  9421. (if (member property org-special-properties)
  9422. ;; We need a special property. Use brute force, get all properties.
  9423. (cdr (assoc property (org-entry-properties nil 'special)))
  9424. (let ((range (org-get-property-block)))
  9425. (if (and range
  9426. (goto-char (car range))
  9427. (re-search-forward
  9428. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9429. (cdr range) t))
  9430. ;; Found the property, return it.
  9431. (if (match-end 1)
  9432. (org-match-string-no-properties 1)
  9433. "")))))))
  9434. (defun org-property-or-variable-value (var &optional inherit)
  9435. "Check if there is a property fixing the value of VAR.
  9436. If yes, return this value. If not, return the current value of the variable."
  9437. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9438. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9439. (read prop)
  9440. (symbol-value var))))
  9441. (defun org-entry-delete (pom property)
  9442. "Delete the property PROPERTY from entry at point-or-marker POM."
  9443. (org-with-point-at pom
  9444. (if (member property org-special-properties)
  9445. nil ; cannot delete these properties.
  9446. (let ((range (org-get-property-block)))
  9447. (if (and range
  9448. (goto-char (car range))
  9449. (re-search-forward
  9450. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9451. (cdr range) t))
  9452. (progn
  9453. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9454. t)
  9455. nil)))))
  9456. ;; Multi-values properties are properties that contain multiple values
  9457. ;; These values are assumed to be single words, separated by whitespace.
  9458. (defun org-entry-add-to-multivalued-property (pom property value)
  9459. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9460. (let* ((old (org-entry-get pom property))
  9461. (values (and old (org-split-string old "[ \t]"))))
  9462. (setq value (org-entry-protect-space value))
  9463. (unless (member value values)
  9464. (setq values (cons value values))
  9465. (org-entry-put pom property
  9466. (mapconcat 'identity values " ")))))
  9467. (defun org-entry-remove-from-multivalued-property (pom property value)
  9468. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9469. (let* ((old (org-entry-get pom property))
  9470. (values (and old (org-split-string old "[ \t]"))))
  9471. (setq value (org-entry-protect-space value))
  9472. (when (member value values)
  9473. (setq values (delete value values))
  9474. (org-entry-put pom property
  9475. (mapconcat 'identity values " ")))))
  9476. (defun org-entry-member-in-multivalued-property (pom property value)
  9477. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9478. (let* ((old (org-entry-get pom property))
  9479. (values (and old (org-split-string old "[ \t]"))))
  9480. (setq value (org-entry-protect-space value))
  9481. (member value values)))
  9482. (defun org-entry-get-multivalued-property (pom property)
  9483. "Return a list of values in a multivalued property."
  9484. (let* ((value (org-entry-get pom property))
  9485. (values (and value (org-split-string value "[ \t]"))))
  9486. (mapcar 'org-entry-restore-space values)))
  9487. (defun org-entry-put-multivalued-property (pom property &rest values)
  9488. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9489. VALUES should be a list of strings. Spaces will be protected."
  9490. (org-entry-put pom property
  9491. (mapconcat 'org-entry-protect-space values " "))
  9492. (let* ((value (org-entry-get pom property))
  9493. (values (and value (org-split-string value "[ \t]"))))
  9494. (mapcar 'org-entry-restore-space values)))
  9495. (defun org-entry-protect-space (s)
  9496. "Protect spaces and newline in string S."
  9497. (while (string-match " " s)
  9498. (setq s (replace-match "%20" t t s)))
  9499. (while (string-match "\n" s)
  9500. (setq s (replace-match "%0A" t t s)))
  9501. s)
  9502. (defun org-entry-restore-space (s)
  9503. "Restore spaces and newline in string S."
  9504. (while (string-match "%20" s)
  9505. (setq s (replace-match " " t t s)))
  9506. (while (string-match "%0A" s)
  9507. (setq s (replace-match "\n" t t s)))
  9508. s)
  9509. (defvar org-entry-property-inherited-from (make-marker)
  9510. "Marker pointing to the entry from where a property was inherited.
  9511. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9512. location of the entry where the inheritance search matched. If there was
  9513. no match, the marker will point nowhere.
  9514. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9515. is set.")
  9516. (defun org-entry-get-with-inheritance (property)
  9517. "Get entry property, and search higher levels if not present."
  9518. (move-marker org-entry-property-inherited-from nil)
  9519. (let (tmp)
  9520. (save-excursion
  9521. (save-restriction
  9522. (widen)
  9523. (catch 'ex
  9524. (while t
  9525. (when (setq tmp (org-entry-get nil property))
  9526. (org-back-to-heading t)
  9527. (move-marker org-entry-property-inherited-from (point))
  9528. (throw 'ex tmp))
  9529. (or (org-up-heading-safe) (throw 'ex nil)))))
  9530. (or tmp
  9531. (cdr (assoc property org-file-properties))
  9532. (cdr (assoc property org-global-properties))
  9533. (cdr (assoc property org-global-properties-fixed))))))
  9534. (defun org-entry-put (pom property value)
  9535. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9536. (org-with-point-at pom
  9537. (org-back-to-heading t)
  9538. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9539. range)
  9540. (cond
  9541. ((equal property "TODO")
  9542. (when (and (stringp value) (string-match "\\S-" value)
  9543. (not (member value org-todo-keywords-1)))
  9544. (error "\"%s\" is not a valid TODO state" value))
  9545. (if (or (not value)
  9546. (not (string-match "\\S-" value)))
  9547. (setq value 'none))
  9548. (org-todo value)
  9549. (org-set-tags nil 'align))
  9550. ((equal property "PRIORITY")
  9551. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9552. (string-to-char value) ?\ ))
  9553. (org-set-tags nil 'align))
  9554. ((equal property "SCHEDULED")
  9555. (if (re-search-forward org-scheduled-time-regexp end t)
  9556. (cond
  9557. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9558. ((eq value 'later) (org-timestamp-change 1 'day))
  9559. (t (call-interactively 'org-schedule)))
  9560. (call-interactively 'org-schedule)))
  9561. ((equal property "DEADLINE")
  9562. (if (re-search-forward org-deadline-time-regexp end t)
  9563. (cond
  9564. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9565. ((eq value 'later) (org-timestamp-change 1 'day))
  9566. (t (call-interactively 'org-deadline)))
  9567. (call-interactively 'org-deadline)))
  9568. ((member property org-special-properties)
  9569. (error "The %s property can not yet be set with `org-entry-put'"
  9570. property))
  9571. (t ; a non-special property
  9572. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9573. (setq range (org-get-property-block beg end 'force))
  9574. (goto-char (car range))
  9575. (if (re-search-forward
  9576. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9577. (progn
  9578. (delete-region (match-beginning 1) (match-end 1))
  9579. (goto-char (match-beginning 1)))
  9580. (goto-char (cdr range))
  9581. (insert "\n")
  9582. (backward-char 1)
  9583. (org-indent-line-function)
  9584. (insert ":" property ":"))
  9585. (and value (insert " " value))
  9586. (org-indent-line-function)))))))
  9587. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9588. "Get all property keys in the current buffer.
  9589. With INCLUDE-SPECIALS, also list the special properties that reflect things
  9590. like tags and TODO state.
  9591. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9592. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9593. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9594. formats in the current buffer."
  9595. (let (rtn range cfmt cols s p)
  9596. (save-excursion
  9597. (save-restriction
  9598. (widen)
  9599. (goto-char (point-min))
  9600. (while (re-search-forward org-property-start-re nil t)
  9601. (setq range (org-get-property-block))
  9602. (goto-char (car range))
  9603. (while (re-search-forward
  9604. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9605. (cdr range) t)
  9606. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9607. (outline-next-heading))))
  9608. (when include-specials
  9609. (setq rtn (append org-special-properties rtn)))
  9610. (when include-defaults
  9611. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9612. (when include-columns
  9613. (save-excursion
  9614. (save-restriction
  9615. (widen)
  9616. (goto-char (point-min))
  9617. (while (re-search-forward
  9618. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9619. nil t)
  9620. (setq cfmt (match-string 2) s 0)
  9621. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9622. cfmt s)
  9623. (setq s (match-end 0)
  9624. p (match-string 1 cfmt))
  9625. (unless (or (equal p "ITEM")
  9626. (member p org-special-properties))
  9627. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9628. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9629. (defun org-property-values (key)
  9630. "Return a list of all values of property KEY."
  9631. (save-excursion
  9632. (save-restriction
  9633. (widen)
  9634. (goto-char (point-min))
  9635. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9636. values)
  9637. (while (re-search-forward re nil t)
  9638. (add-to-list 'values (org-trim (match-string 1))))
  9639. (delete "" values)))))
  9640. (defun org-insert-property-drawer ()
  9641. "Insert a property drawer into the current entry."
  9642. (interactive)
  9643. (org-back-to-heading t)
  9644. (looking-at outline-regexp)
  9645. (let ((indent (- (match-end 0)(match-beginning 0)))
  9646. (beg (point))
  9647. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9648. end hiddenp)
  9649. (outline-next-heading)
  9650. (setq end (point))
  9651. (goto-char beg)
  9652. (while (re-search-forward re end t))
  9653. (setq hiddenp (org-invisible-p))
  9654. (end-of-line 1)
  9655. (and (equal (char-after) ?\n) (forward-char 1))
  9656. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9657. (beginning-of-line 2))
  9658. (org-skip-over-state-notes)
  9659. (skip-chars-backward " \t\n\r")
  9660. (if (eq (char-before) ?*) (forward-char 1))
  9661. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9662. (beginning-of-line 0)
  9663. (org-indent-to-column indent)
  9664. (beginning-of-line 2)
  9665. (org-indent-to-column indent)
  9666. (beginning-of-line 0)
  9667. (if hiddenp
  9668. (save-excursion
  9669. (org-back-to-heading t)
  9670. (hide-entry))
  9671. (org-flag-drawer t))))
  9672. (defun org-set-property (property value)
  9673. "In the current entry, set PROPERTY to VALUE.
  9674. When called interactively, this will prompt for a property name, offering
  9675. completion on existing and default properties. And then it will prompt
  9676. for a value, offering completion either on allowed values (via an inherited
  9677. xxx_ALL property) or on existing values in other instances of this property
  9678. in the current file."
  9679. (interactive
  9680. (let* ((completion-ignore-case t)
  9681. (keys (org-buffer-property-keys nil t t))
  9682. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9683. (prop (if (member prop0 keys)
  9684. prop0
  9685. (or (cdr (assoc (downcase prop0)
  9686. (mapcar (lambda (x) (cons (downcase x) x))
  9687. keys)))
  9688. prop0)))
  9689. (cur (org-entry-get nil prop))
  9690. (allowed (org-property-get-allowed-values nil prop 'table))
  9691. (existing (mapcar 'list (org-property-values prop)))
  9692. (val (if allowed
  9693. (org-completing-read "Value: " allowed nil 'req-match)
  9694. (org-completing-read
  9695. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9696. (concat "[" cur "]") "")
  9697. ": ")
  9698. existing nil nil "" nil cur))))
  9699. (list prop (if (equal val "") cur val))))
  9700. (unless (equal (org-entry-get nil property) value)
  9701. (org-entry-put nil property value)))
  9702. (defun org-delete-property (property)
  9703. "In the current entry, delete PROPERTY."
  9704. (interactive
  9705. (let* ((completion-ignore-case t)
  9706. (prop (org-ido-completing-read
  9707. "Property: " (org-entry-properties nil 'standard))))
  9708. (list prop)))
  9709. (message "Property %s %s" property
  9710. (if (org-entry-delete nil property)
  9711. "deleted"
  9712. "was not present in the entry")))
  9713. (defun org-delete-property-globally (property)
  9714. "Remove PROPERTY globally, from all entries."
  9715. (interactive
  9716. (let* ((completion-ignore-case t)
  9717. (prop (org-ido-completing-read
  9718. "Globally remove property: "
  9719. (mapcar 'list (org-buffer-property-keys)))))
  9720. (list prop)))
  9721. (save-excursion
  9722. (save-restriction
  9723. (widen)
  9724. (goto-char (point-min))
  9725. (let ((cnt 0))
  9726. (while (re-search-forward
  9727. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9728. nil t)
  9729. (setq cnt (1+ cnt))
  9730. (replace-match ""))
  9731. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9732. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9733. (defun org-compute-property-at-point ()
  9734. "Compute the property at point.
  9735. This looks for an enclosing column format, extracts the operator and
  9736. then applies it to the property in the column format's scope."
  9737. (interactive)
  9738. (unless (org-at-property-p)
  9739. (error "Not at a property"))
  9740. (let ((prop (org-match-string-no-properties 2)))
  9741. (org-columns-get-format-and-top-level)
  9742. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9743. (error "No operator defined for property %s" prop))
  9744. (org-columns-compute prop)))
  9745. (defun org-property-get-allowed-values (pom property &optional table)
  9746. "Get allowed values for the property PROPERTY.
  9747. When TABLE is non-nil, return an alist that can directly be used for
  9748. completion."
  9749. (let (vals)
  9750. (cond
  9751. ((equal property "TODO")
  9752. (setq vals (org-with-point-at pom
  9753. (append org-todo-keywords-1 '("")))))
  9754. ((equal property "PRIORITY")
  9755. (let ((n org-lowest-priority))
  9756. (while (>= n org-highest-priority)
  9757. (push (char-to-string n) vals)
  9758. (setq n (1- n)))))
  9759. ((member property org-special-properties))
  9760. (t
  9761. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9762. (when (and vals (string-match "\\S-" vals))
  9763. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9764. (setq vals (mapcar (lambda (x)
  9765. (cond ((stringp x) x)
  9766. ((numberp x) (number-to-string x))
  9767. ((symbolp x) (symbol-name x))
  9768. (t "???")))
  9769. vals)))))
  9770. (if table (mapcar 'list vals) vals)))
  9771. (defun org-property-previous-allowed-value (&optional previous)
  9772. "Switch to the next allowed value for this property."
  9773. (interactive)
  9774. (org-property-next-allowed-value t))
  9775. (defun org-property-next-allowed-value (&optional previous)
  9776. "Switch to the next allowed value for this property."
  9777. (interactive)
  9778. (unless (org-at-property-p)
  9779. (error "Not at a property"))
  9780. (let* ((key (match-string 2))
  9781. (value (match-string 3))
  9782. (allowed (or (org-property-get-allowed-values (point) key)
  9783. (and (member value '("[ ]" "[-]" "[X]"))
  9784. '("[ ]" "[X]"))))
  9785. nval)
  9786. (unless allowed
  9787. (error "Allowed values for this property have not been defined"))
  9788. (if previous (setq allowed (reverse allowed)))
  9789. (if (member value allowed)
  9790. (setq nval (car (cdr (member value allowed)))))
  9791. (setq nval (or nval (car allowed)))
  9792. (if (equal nval value)
  9793. (error "Only one allowed value for this property"))
  9794. (org-at-property-p)
  9795. (replace-match (concat " :" key ": " nval) t t)
  9796. (org-indent-line-function)
  9797. (beginning-of-line 1)
  9798. (skip-chars-forward " \t")))
  9799. (defun org-find-entry-with-id (ident)
  9800. "Locate the entry that contains the ID property with exact value IDENT.
  9801. IDENT can be a string, a symbol or a number, this function will search for
  9802. the string representation of it.
  9803. Return the position where this entry starts, or nil if there is no such entry."
  9804. (interactive "sID: ")
  9805. (let ((id (cond
  9806. ((stringp ident) ident)
  9807. ((symbol-name ident) (symbol-name ident))
  9808. ((numberp ident) (number-to-string ident))
  9809. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9810. (case-fold-search nil))
  9811. (save-excursion
  9812. (save-restriction
  9813. (widen)
  9814. (goto-char (point-min))
  9815. (when (re-search-forward
  9816. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9817. nil t)
  9818. (org-back-to-heading)
  9819. (point))))))
  9820. ;;;; Timestamps
  9821. (defvar org-last-changed-timestamp nil)
  9822. (defvar org-last-inserted-timestamp nil
  9823. "The last time stamp inserted with `org-insert-time-stamp'.")
  9824. (defvar org-time-was-given) ; dynamically scoped parameter
  9825. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9826. (defvar org-ts-what) ; dynamically scoped parameter
  9827. (defun org-time-stamp (arg &optional inactive)
  9828. "Prompt for a date/time and insert a time stamp.
  9829. If the user specifies a time like HH:MM, or if this command is called
  9830. with a prefix argument, the time stamp will contain date and time.
  9831. Otherwise, only the date will be included. All parts of a date not
  9832. specified by the user will be filled in from the current date/time.
  9833. So if you press just return without typing anything, the time stamp
  9834. will represent the current date/time. If there is already a timestamp
  9835. at the cursor, it will be modified."
  9836. (interactive "P")
  9837. (let* ((ts nil)
  9838. (default-time
  9839. ;; Default time is either today, or, when entering a range,
  9840. ;; the range start.
  9841. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9842. (save-excursion
  9843. (re-search-backward
  9844. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9845. (- (point) 20) t)))
  9846. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9847. (current-time)))
  9848. (default-input (and ts (org-get-compact-tod ts)))
  9849. org-time-was-given org-end-time-was-given time)
  9850. (cond
  9851. ((and (org-at-timestamp-p t)
  9852. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9853. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9854. (insert "--")
  9855. (setq time (let ((this-command this-command))
  9856. (org-read-date arg 'totime nil nil
  9857. default-time default-input)))
  9858. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9859. ((org-at-timestamp-p t)
  9860. (setq time (let ((this-command this-command))
  9861. (org-read-date arg 'totime nil nil default-time default-input)))
  9862. (when (org-at-timestamp-p t) ; just to get the match data
  9863. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9864. (replace-match "")
  9865. (setq org-last-changed-timestamp
  9866. (org-insert-time-stamp
  9867. time (or org-time-was-given arg)
  9868. inactive nil nil (list org-end-time-was-given))))
  9869. (message "Timestamp updated"))
  9870. (t
  9871. (setq time (let ((this-command this-command))
  9872. (org-read-date arg 'totime nil nil default-time default-input)))
  9873. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9874. nil nil (list org-end-time-was-given))))))
  9875. ;; FIXME: can we use this for something else, like computing time differences?
  9876. (defun org-get-compact-tod (s)
  9877. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9878. (let* ((t1 (match-string 1 s))
  9879. (h1 (string-to-number (match-string 2 s)))
  9880. (m1 (string-to-number (match-string 3 s)))
  9881. (t2 (and (match-end 4) (match-string 5 s)))
  9882. (h2 (and t2 (string-to-number (match-string 6 s))))
  9883. (m2 (and t2 (string-to-number (match-string 7 s))))
  9884. dh dm)
  9885. (if (not t2)
  9886. t1
  9887. (setq dh (- h2 h1) dm (- m2 m1))
  9888. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9889. (concat t1 "+" (number-to-string dh)
  9890. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9891. (defun org-time-stamp-inactive (&optional arg)
  9892. "Insert an inactive time stamp.
  9893. An inactive time stamp is enclosed in square brackets instead of angle
  9894. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9895. does not link to the calendar and cannot be changed with the S-cursor keys.
  9896. So these are more for recording a certain time/date."
  9897. (interactive "P")
  9898. (org-time-stamp arg 'inactive))
  9899. (defvar org-date-ovl (org-make-overlay 1 1))
  9900. (org-overlay-put org-date-ovl 'face 'org-warning)
  9901. (org-detach-overlay org-date-ovl)
  9902. (defvar org-ans1) ; dynamically scoped parameter
  9903. (defvar org-ans2) ; dynamically scoped parameter
  9904. (defvar org-plain-time-of-day-regexp) ; defined below
  9905. (defvar org-overriding-default-time nil) ; dynamically scoped
  9906. (defvar org-read-date-overlay nil)
  9907. (defvar org-dcst nil) ; dynamically scoped
  9908. (defun org-read-date (&optional with-time to-time from-string prompt
  9909. default-time default-input)
  9910. "Read a date, possibly a time, and make things smooth for the user.
  9911. The prompt will suggest to enter an ISO date, but you can also enter anything
  9912. which will at least partially be understood by `parse-time-string'.
  9913. Unrecognized parts of the date will default to the current day, month, year,
  9914. hour and minute. If this command is called to replace a timestamp at point,
  9915. of to enter the second timestamp of a range, the default time is taken from the
  9916. existing stamp. For example,
  9917. 3-2-5 --> 2003-02-05
  9918. feb 15 --> currentyear-02-15
  9919. sep 12 9 --> 2009-09-12
  9920. 12:45 --> today 12:45
  9921. 22 sept 0:34 --> currentyear-09-22 0:34
  9922. 12 --> currentyear-currentmonth-12
  9923. Fri --> nearest Friday (today or later)
  9924. etc.
  9925. Furthermore you can specify a relative date by giving, as the *first* thing
  9926. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9927. change in days weeks, months, years.
  9928. With a single plus or minus, the date is relative to today. With a double
  9929. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9930. +4d --> four days from today
  9931. +4 --> same as above
  9932. +2w --> two weeks from today
  9933. ++5 --> five days from default date
  9934. The function understands only English month and weekday abbreviations,
  9935. but this can be configured with the variables `parse-time-months' and
  9936. `parse-time-weekdays'.
  9937. While prompting, a calendar is popped up - you can also select the
  9938. date with the mouse (button 1). The calendar shows a period of three
  9939. months. To scroll it to other months, use the keys `>' and `<'.
  9940. If you don't like the calendar, turn it off with
  9941. \(setq org-read-date-popup-calendar nil)
  9942. With optional argument TO-TIME, the date will immediately be converted
  9943. to an internal time.
  9944. With an optional argument WITH-TIME, the prompt will suggest to also
  9945. insert a time. Note that when WITH-TIME is not set, you can still
  9946. enter a time, and this function will inform the calling routine about
  9947. this change. The calling routine may then choose to change the format
  9948. used to insert the time stamp into the buffer to include the time.
  9949. With optional argument FROM-STRING, read from this string instead from
  9950. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9951. the time/date that is used for everything that is not specified by the
  9952. user."
  9953. (require 'parse-time)
  9954. (let* ((org-time-stamp-rounding-minutes
  9955. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9956. (org-dcst org-display-custom-times)
  9957. (ct (org-current-time))
  9958. (def (or org-overriding-default-time default-time ct))
  9959. (defdecode (decode-time def))
  9960. (dummy (progn
  9961. (when (< (nth 2 defdecode) org-extend-today-until)
  9962. (setcar (nthcdr 2 defdecode) -1)
  9963. (setcar (nthcdr 1 defdecode) 59)
  9964. (setq def (apply 'encode-time defdecode)
  9965. defdecode (decode-time def)))))
  9966. (calendar-move-hook nil)
  9967. (calendar-view-diary-initially-flag nil)
  9968. (view-diary-entries-initially nil)
  9969. (calendar-view-holidays-initially-flag nil)
  9970. (view-calendar-holidays-initially nil)
  9971. (timestr (format-time-string
  9972. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9973. (prompt (concat (if prompt (concat prompt " ") "")
  9974. (format "Date+time [%s]: " timestr)))
  9975. ans (org-ans0 "") org-ans1 org-ans2 final)
  9976. (cond
  9977. (from-string (setq ans from-string))
  9978. (org-read-date-popup-calendar
  9979. (save-excursion
  9980. (save-window-excursion
  9981. (calendar)
  9982. (calendar-forward-day (- (time-to-days def)
  9983. (calendar-absolute-from-gregorian
  9984. (calendar-current-date))))
  9985. (org-eval-in-calendar nil t)
  9986. (let* ((old-map (current-local-map))
  9987. (map (copy-keymap calendar-mode-map))
  9988. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9989. (org-defkey map (kbd "RET") 'org-calendar-select)
  9990. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9991. 'org-calendar-select-mouse)
  9992. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9993. 'org-calendar-select-mouse)
  9994. (org-defkey minibuffer-local-map [(meta shift left)]
  9995. (lambda () (interactive)
  9996. (org-eval-in-calendar '(calendar-backward-month 1))))
  9997. (org-defkey minibuffer-local-map [(meta shift right)]
  9998. (lambda () (interactive)
  9999. (org-eval-in-calendar '(calendar-forward-month 1))))
  10000. (org-defkey minibuffer-local-map [(meta shift up)]
  10001. (lambda () (interactive)
  10002. (org-eval-in-calendar '(calendar-backward-year 1))))
  10003. (org-defkey minibuffer-local-map [(meta shift down)]
  10004. (lambda () (interactive)
  10005. (org-eval-in-calendar '(calendar-forward-year 1))))
  10006. (org-defkey minibuffer-local-map [(shift up)]
  10007. (lambda () (interactive)
  10008. (org-eval-in-calendar '(calendar-backward-week 1))))
  10009. (org-defkey minibuffer-local-map [(shift down)]
  10010. (lambda () (interactive)
  10011. (org-eval-in-calendar '(calendar-forward-week 1))))
  10012. (org-defkey minibuffer-local-map [(shift left)]
  10013. (lambda () (interactive)
  10014. (org-eval-in-calendar '(calendar-backward-day 1))))
  10015. (org-defkey minibuffer-local-map [(shift right)]
  10016. (lambda () (interactive)
  10017. (org-eval-in-calendar '(calendar-forward-day 1))))
  10018. (org-defkey minibuffer-local-map ">"
  10019. (lambda () (interactive)
  10020. (org-eval-in-calendar '(scroll-calendar-left 1))))
  10021. (org-defkey minibuffer-local-map "<"
  10022. (lambda () (interactive)
  10023. (org-eval-in-calendar '(scroll-calendar-right 1))))
  10024. (unwind-protect
  10025. (progn
  10026. (use-local-map map)
  10027. (add-hook 'post-command-hook 'org-read-date-display)
  10028. (setq org-ans0 (read-string prompt default-input nil nil))
  10029. ;; org-ans0: from prompt
  10030. ;; org-ans1: from mouse click
  10031. ;; org-ans2: from calendar motion
  10032. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  10033. (remove-hook 'post-command-hook 'org-read-date-display)
  10034. (use-local-map old-map)
  10035. (when org-read-date-overlay
  10036. (org-delete-overlay org-read-date-overlay)
  10037. (setq org-read-date-overlay nil)))))))
  10038. (t ; Naked prompt only
  10039. (unwind-protect
  10040. (setq ans (read-string prompt default-input nil timestr))
  10041. (when org-read-date-overlay
  10042. (org-delete-overlay org-read-date-overlay)
  10043. (setq org-read-date-overlay nil)))))
  10044. (setq final (org-read-date-analyze ans def defdecode))
  10045. (if to-time
  10046. (apply 'encode-time final)
  10047. (if (and (boundp 'org-time-was-given) org-time-was-given)
  10048. (format "%04d-%02d-%02d %02d:%02d"
  10049. (nth 5 final) (nth 4 final) (nth 3 final)
  10050. (nth 2 final) (nth 1 final))
  10051. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  10052. (defvar def)
  10053. (defvar defdecode)
  10054. (defvar with-time)
  10055. (defun org-read-date-display ()
  10056. "Display the current date prompt interpretation in the minibuffer."
  10057. (when org-read-date-display-live
  10058. (when org-read-date-overlay
  10059. (org-delete-overlay org-read-date-overlay))
  10060. (let ((p (point)))
  10061. (end-of-line 1)
  10062. (while (not (equal (buffer-substring
  10063. (max (point-min) (- (point) 4)) (point))
  10064. " "))
  10065. (insert " "))
  10066. (goto-char p))
  10067. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  10068. " " (or org-ans1 org-ans2)))
  10069. (org-end-time-was-given nil)
  10070. (f (org-read-date-analyze ans def defdecode))
  10071. (fmts (if org-dcst
  10072. org-time-stamp-custom-formats
  10073. org-time-stamp-formats))
  10074. (fmt (if (or with-time
  10075. (and (boundp 'org-time-was-given) org-time-was-given))
  10076. (cdr fmts)
  10077. (car fmts)))
  10078. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  10079. (when (and org-end-time-was-given
  10080. (string-match org-plain-time-of-day-regexp txt))
  10081. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  10082. org-end-time-was-given
  10083. (substring txt (match-end 0)))))
  10084. (setq org-read-date-overlay
  10085. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  10086. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  10087. (defun org-read-date-analyze (ans def defdecode)
  10088. "Analyse the combined answer of the date prompt."
  10089. ;; FIXME: cleanup and comment
  10090. (let (delta deltan deltaw deltadef year month day
  10091. hour minute second wday pm h2 m2 tl wday1
  10092. iso-year iso-weekday iso-week iso-year iso-date)
  10093. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  10094. (setq ans "+0"))
  10095. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10096. (setq ans (replace-match "" t t ans)
  10097. deltan (car delta)
  10098. deltaw (nth 1 delta)
  10099. deltadef (nth 2 delta)))
  10100. ;; Check if there is an iso week date in there
  10101. ;; If yes, sore the info and postpone interpreting it until the rest
  10102. ;; of the parsing is done
  10103. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10104. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10105. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10106. iso-week (string-to-number (match-string 2 ans)))
  10107. (setq ans (replace-match "" t t ans)))
  10108. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10109. (when (string-match
  10110. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10111. (setq year (if (match-end 2)
  10112. (string-to-number (match-string 2 ans))
  10113. (string-to-number (format-time-string "%Y")))
  10114. month (string-to-number (match-string 3 ans))
  10115. day (string-to-number (match-string 4 ans)))
  10116. (if (< year 100) (setq year (+ 2000 year)))
  10117. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10118. t nil ans)))
  10119. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10120. ;; If there is a time with am/pm, and *no* time without it, we convert
  10121. ;; so that matching will be successful.
  10122. (loop for i from 1 to 2 do ; twice, for end time as well
  10123. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10124. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10125. (setq hour (string-to-number (match-string 1 ans))
  10126. minute (if (match-end 3)
  10127. (string-to-number (match-string 3 ans))
  10128. 0)
  10129. pm (equal ?p
  10130. (string-to-char (downcase (match-string 4 ans)))))
  10131. (if (and (= hour 12) (not pm))
  10132. (setq hour 0)
  10133. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10134. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10135. t t ans))))
  10136. ;; Check if a time range is given as a duration
  10137. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10138. (setq hour (string-to-number (match-string 1 ans))
  10139. h2 (+ hour (string-to-number (match-string 3 ans)))
  10140. minute (string-to-number (match-string 2 ans))
  10141. m2 (+ minute (if (match-end 5) (string-to-number
  10142. (match-string 5 ans))0)))
  10143. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10144. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10145. t t ans)))
  10146. ;; Check if there is a time range
  10147. (when (boundp 'org-end-time-was-given)
  10148. (setq org-time-was-given nil)
  10149. (when (and (string-match org-plain-time-of-day-regexp ans)
  10150. (match-end 8))
  10151. (setq org-end-time-was-given (match-string 8 ans))
  10152. (setq ans (concat (substring ans 0 (match-beginning 7))
  10153. (substring ans (match-end 7))))))
  10154. (setq tl (parse-time-string ans)
  10155. day (or (nth 3 tl) (nth 3 defdecode))
  10156. month (or (nth 4 tl)
  10157. (if (and org-read-date-prefer-future
  10158. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10159. (1+ (nth 4 defdecode))
  10160. (nth 4 defdecode)))
  10161. year (or (nth 5 tl)
  10162. (if (and org-read-date-prefer-future
  10163. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10164. (1+ (nth 5 defdecode))
  10165. (nth 5 defdecode)))
  10166. hour (or (nth 2 tl) (nth 2 defdecode))
  10167. minute (or (nth 1 tl) (nth 1 defdecode))
  10168. second (or (nth 0 tl) 0)
  10169. wday (nth 6 tl))
  10170. ;; Special date definitions below
  10171. (cond
  10172. (iso-week
  10173. ;; There was an iso week
  10174. (setq year (or iso-year year)
  10175. day (or iso-weekday wday 1)
  10176. wday nil ; to make sure that the trigger below does not match
  10177. iso-date (calendar-gregorian-from-absolute
  10178. (calendar-absolute-from-iso
  10179. (list iso-week day year))))
  10180. ; FIXME: Should we also push ISO weeks into the future?
  10181. ; (when (and org-read-date-prefer-future
  10182. ; (not iso-year)
  10183. ; (< (calendar-absolute-from-gregorian iso-date)
  10184. ; (time-to-days (current-time))))
  10185. ; (setq year (1+ year)
  10186. ; iso-date (calendar-gregorian-from-absolute
  10187. ; (calendar-absolute-from-iso
  10188. ; (list iso-week day year)))))
  10189. (setq month (car iso-date)
  10190. year (nth 2 iso-date)
  10191. day (nth 1 iso-date)))
  10192. (deltan
  10193. (unless deltadef
  10194. (let ((now (decode-time (current-time))))
  10195. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10196. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10197. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10198. ((equal deltaw "m") (setq month (+ month deltan)))
  10199. ((equal deltaw "y") (setq year (+ year deltan)))))
  10200. ((and wday (not (nth 3 tl)))
  10201. ;; Weekday was given, but no day, so pick that day in the week
  10202. ;; on or after the derived date.
  10203. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10204. (unless (equal wday wday1)
  10205. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10206. (if (and (boundp 'org-time-was-given)
  10207. (nth 2 tl))
  10208. (setq org-time-was-given t))
  10209. (if (< year 100) (setq year (+ 2000 year)))
  10210. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10211. (list second minute hour day month year)))
  10212. (defvar parse-time-weekdays)
  10213. (defun org-read-date-get-relative (s today default)
  10214. "Check string S for special relative date string.
  10215. TODAY and DEFAULT are internal times, for today and for a default.
  10216. Return shift list (N what def-flag)
  10217. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10218. N is the number of WHATs to shift.
  10219. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10220. the DEFAULT date rather than TODAY."
  10221. (when (and
  10222. (string-match
  10223. (concat
  10224. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10225. "\\([0-9]+\\)?"
  10226. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10227. "\\([ \t]\\|$\\)") s)
  10228. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10229. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10230. (string-to-char (substring (match-string 1 s) -1))
  10231. ?+))
  10232. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10233. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10234. (what (if (match-end 3) (match-string 3 s) "d"))
  10235. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10236. (date (if rel default today))
  10237. (wday (nth 6 (decode-time date)))
  10238. delta)
  10239. (if wday1
  10240. (progn
  10241. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10242. (if (= dir ?-) (setq delta (- delta 7)))
  10243. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10244. (list delta "d" rel))
  10245. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10246. (defun org-eval-in-calendar (form &optional keepdate)
  10247. "Eval FORM in the calendar window and return to current window.
  10248. Also, store the cursor date in variable org-ans2."
  10249. (let ((sw (selected-window)))
  10250. (select-window (get-buffer-window "*Calendar*"))
  10251. (eval form)
  10252. (when (and (not keepdate) (calendar-cursor-to-date))
  10253. (let* ((date (calendar-cursor-to-date))
  10254. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10255. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10256. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10257. (select-window sw)))
  10258. (defun org-calendar-select ()
  10259. "Return to `org-read-date' with the date currently selected.
  10260. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10261. (interactive)
  10262. (when (calendar-cursor-to-date)
  10263. (let* ((date (calendar-cursor-to-date))
  10264. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10265. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10266. (if (active-minibuffer-window) (exit-minibuffer))))
  10267. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10268. "Insert a date stamp for the date given by the internal TIME.
  10269. WITH-HM means, use the stamp format that includes the time of the day.
  10270. INACTIVE means use square brackets instead of angular ones, so that the
  10271. stamp will not contribute to the agenda.
  10272. PRE and POST are optional strings to be inserted before and after the
  10273. stamp.
  10274. The command returns the inserted time stamp."
  10275. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10276. stamp)
  10277. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10278. (insert-before-markers (or pre ""))
  10279. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10280. (when (listp extra)
  10281. (setq extra (car extra))
  10282. (if (and (stringp extra)
  10283. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10284. (setq extra (format "-%02d:%02d"
  10285. (string-to-number (match-string 1 extra))
  10286. (string-to-number (match-string 2 extra))))
  10287. (setq extra nil)))
  10288. (when extra
  10289. (backward-char 1)
  10290. (insert-before-markers extra)
  10291. (forward-char 1))
  10292. (insert-before-markers (or post ""))
  10293. (setq org-last-inserted-timestamp stamp)))
  10294. (defun org-toggle-time-stamp-overlays ()
  10295. "Toggle the use of custom time stamp formats."
  10296. (interactive)
  10297. (setq org-display-custom-times (not org-display-custom-times))
  10298. (unless org-display-custom-times
  10299. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10300. (while (setq p (next-single-property-change p 'display))
  10301. (if (and (get-text-property p 'display)
  10302. (eq (get-text-property p 'face) 'org-date))
  10303. (remove-text-properties
  10304. p (setq p (next-single-property-change p 'display))
  10305. '(display t))))
  10306. (set-buffer-modified-p bmp)))
  10307. (if (featurep 'xemacs)
  10308. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10309. (org-restart-font-lock)
  10310. (setq org-table-may-need-update t)
  10311. (if org-display-custom-times
  10312. (message "Time stamps are overlayed with custom format")
  10313. (message "Time stamp overlays removed")))
  10314. (defun org-display-custom-time (beg end)
  10315. "Overlay modified time stamp format over timestamp between BEG and END."
  10316. (let* ((ts (buffer-substring beg end))
  10317. t1 w1 with-hm tf time str w2 (off 0))
  10318. (save-match-data
  10319. (setq t1 (org-parse-time-string ts t))
  10320. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10321. (setq off (- (match-end 0) (match-beginning 0)))))
  10322. (setq end (- end off))
  10323. (setq w1 (- end beg)
  10324. with-hm (and (nth 1 t1) (nth 2 t1))
  10325. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10326. time (org-fix-decoded-time t1)
  10327. str (org-add-props
  10328. (format-time-string
  10329. (substring tf 1 -1) (apply 'encode-time time))
  10330. nil 'mouse-face 'highlight)
  10331. w2 (length str))
  10332. (if (not (= w2 w1))
  10333. (add-text-properties (1+ beg) (+ 2 beg)
  10334. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10335. (if (featurep 'xemacs)
  10336. (progn
  10337. (put-text-property beg end 'invisible t)
  10338. (put-text-property beg end 'end-glyph (make-glyph str)))
  10339. (put-text-property beg end 'display str))))
  10340. (defun org-translate-time (string)
  10341. "Translate all timestamps in STRING to custom format.
  10342. But do this only if the variable `org-display-custom-times' is set."
  10343. (when org-display-custom-times
  10344. (save-match-data
  10345. (let* ((start 0)
  10346. (re org-ts-regexp-both)
  10347. t1 with-hm inactive tf time str beg end)
  10348. (while (setq start (string-match re string start))
  10349. (setq beg (match-beginning 0)
  10350. end (match-end 0)
  10351. t1 (save-match-data
  10352. (org-parse-time-string (substring string beg end) t))
  10353. with-hm (and (nth 1 t1) (nth 2 t1))
  10354. inactive (equal (substring string beg (1+ beg)) "[")
  10355. tf (funcall (if with-hm 'cdr 'car)
  10356. org-time-stamp-custom-formats)
  10357. time (org-fix-decoded-time t1)
  10358. str (format-time-string
  10359. (concat
  10360. (if inactive "[" "<") (substring tf 1 -1)
  10361. (if inactive "]" ">"))
  10362. (apply 'encode-time time))
  10363. string (replace-match str t t string)
  10364. start (+ start (length str)))))))
  10365. string)
  10366. (defun org-fix-decoded-time (time)
  10367. "Set 0 instead of nil for the first 6 elements of time.
  10368. Don't touch the rest."
  10369. (let ((n 0))
  10370. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10371. (defun org-days-to-time (timestamp-string)
  10372. "Difference between TIMESTAMP-STRING and now in days."
  10373. (- (time-to-days (org-time-string-to-time timestamp-string))
  10374. (time-to-days (current-time))))
  10375. (defun org-deadline-close (timestamp-string &optional ndays)
  10376. "Is the time in TIMESTAMP-STRING close to the current date?"
  10377. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10378. (and (< (org-days-to-time timestamp-string) ndays)
  10379. (not (org-entry-is-done-p))))
  10380. (defun org-get-wdays (ts)
  10381. "Get the deadline lead time appropriate for timestring TS."
  10382. (cond
  10383. ((<= org-deadline-warning-days 0)
  10384. ;; 0 or negative, enforce this value no matter what
  10385. (- org-deadline-warning-days))
  10386. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10387. ;; lead time is specified.
  10388. (floor (* (string-to-number (match-string 1 ts))
  10389. (cdr (assoc (match-string 2 ts)
  10390. '(("d" . 1) ("w" . 7)
  10391. ("m" . 30.4) ("y" . 365.25)))))))
  10392. ;; go for the default.
  10393. (t org-deadline-warning-days)))
  10394. (defun org-calendar-select-mouse (ev)
  10395. "Return to `org-read-date' with the date currently selected.
  10396. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10397. (interactive "e")
  10398. (mouse-set-point ev)
  10399. (when (calendar-cursor-to-date)
  10400. (let* ((date (calendar-cursor-to-date))
  10401. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10402. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10403. (if (active-minibuffer-window) (exit-minibuffer))))
  10404. (defun org-check-deadlines (ndays)
  10405. "Check if there are any deadlines due or past due.
  10406. A deadline is considered due if it happens within `org-deadline-warning-days'
  10407. days from today's date. If the deadline appears in an entry marked DONE,
  10408. it is not shown. The prefix arg NDAYS can be used to test that many
  10409. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10410. (interactive "P")
  10411. (let* ((org-warn-days
  10412. (cond
  10413. ((equal ndays '(4)) 100000)
  10414. (ndays (prefix-numeric-value ndays))
  10415. (t (abs org-deadline-warning-days))))
  10416. (case-fold-search nil)
  10417. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10418. (callback
  10419. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10420. (message "%d deadlines past-due or due within %d days"
  10421. (org-occur regexp nil callback)
  10422. org-warn-days)))
  10423. (defun org-check-before-date (date)
  10424. "Check if there are deadlines or scheduled entries before DATE."
  10425. (interactive (list (org-read-date)))
  10426. (let ((case-fold-search nil)
  10427. (regexp (concat "\\<\\(" org-deadline-string
  10428. "\\|" org-scheduled-string
  10429. "\\) *<\\([^>]+\\)>"))
  10430. (callback
  10431. (lambda () (time-less-p
  10432. (org-time-string-to-time (match-string 2))
  10433. (org-time-string-to-time date)))))
  10434. (message "%d entries before %s"
  10435. (org-occur regexp nil callback) date)))
  10436. (defun org-evaluate-time-range (&optional to-buffer)
  10437. "Evaluate a time range by computing the difference between start and end.
  10438. Normally the result is just printed in the echo area, but with prefix arg
  10439. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10440. If the time range is actually in a table, the result is inserted into the
  10441. next column.
  10442. For time difference computation, a year is assumed to be exactly 365
  10443. days in order to avoid rounding problems."
  10444. (interactive "P")
  10445. (or
  10446. (org-clock-update-time-maybe)
  10447. (save-excursion
  10448. (unless (org-at-date-range-p t)
  10449. (goto-char (point-at-bol))
  10450. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10451. (if (not (org-at-date-range-p t))
  10452. (error "Not at a time-stamp range, and none found in current line")))
  10453. (let* ((ts1 (match-string 1))
  10454. (ts2 (match-string 2))
  10455. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10456. (match-end (match-end 0))
  10457. (time1 (org-time-string-to-time ts1))
  10458. (time2 (org-time-string-to-time ts2))
  10459. (t1 (time-to-seconds time1))
  10460. (t2 (time-to-seconds time2))
  10461. (diff (abs (- t2 t1)))
  10462. (negative (< (- t2 t1) 0))
  10463. ;; (ys (floor (* 365 24 60 60)))
  10464. (ds (* 24 60 60))
  10465. (hs (* 60 60))
  10466. (fy "%dy %dd %02d:%02d")
  10467. (fy1 "%dy %dd")
  10468. (fd "%dd %02d:%02d")
  10469. (fd1 "%dd")
  10470. (fh "%02d:%02d")
  10471. y d h m align)
  10472. (if havetime
  10473. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10474. y 0
  10475. d (floor (/ diff ds)) diff (mod diff ds)
  10476. h (floor (/ diff hs)) diff (mod diff hs)
  10477. m (floor (/ diff 60)))
  10478. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10479. y 0
  10480. d (floor (+ (/ diff ds) 0.5))
  10481. h 0 m 0))
  10482. (if (not to-buffer)
  10483. (message "%s" (org-make-tdiff-string y d h m))
  10484. (if (org-at-table-p)
  10485. (progn
  10486. (goto-char match-end)
  10487. (setq align t)
  10488. (and (looking-at " *|") (goto-char (match-end 0))))
  10489. (goto-char match-end))
  10490. (if (looking-at
  10491. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10492. (replace-match ""))
  10493. (if negative (insert " -"))
  10494. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10495. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10496. (insert " " (format fh h m))))
  10497. (if align (org-table-align))
  10498. (message "Time difference inserted")))))
  10499. (defun org-make-tdiff-string (y d h m)
  10500. (let ((fmt "")
  10501. (l nil))
  10502. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10503. l (push y l)))
  10504. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10505. l (push d l)))
  10506. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10507. l (push h l)))
  10508. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10509. l (push m l)))
  10510. (apply 'format fmt (nreverse l))))
  10511. (defun org-time-string-to-time (s)
  10512. (apply 'encode-time (org-parse-time-string s)))
  10513. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10514. "Convert a time stamp to an absolute day number.
  10515. If there is a specifyer for a cyclic time stamp, get the closest date to
  10516. DAYNR.
  10517. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10518. (cond
  10519. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10520. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10521. daynr
  10522. (+ daynr 1000)))
  10523. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10524. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10525. (time-to-days (current-time))) (match-string 0 s)
  10526. prefer show-all))
  10527. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10528. (defun org-days-to-iso-week (days)
  10529. "Return the iso week number."
  10530. (require 'cal-iso)
  10531. (car (calendar-iso-from-absolute days)))
  10532. (defun org-small-year-to-year (year)
  10533. "Convert 2-digit years into 4-digit years.
  10534. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10535. The year 2000 cannot be abbreviated. Any year larger than 99
  10536. is returned unchanged."
  10537. (if (< year 38)
  10538. (setq year (+ 2000 year))
  10539. (if (< year 100)
  10540. (setq year (+ 1900 year))))
  10541. year)
  10542. (defun org-time-from-absolute (d)
  10543. "Return the time corresponding to date D.
  10544. D may be an absolute day number, or a calendar-type list (month day year)."
  10545. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10546. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10547. (defun org-calendar-holiday ()
  10548. "List of holidays, for Diary display in Org-mode."
  10549. (require 'holidays)
  10550. (let ((hl (funcall
  10551. (if (fboundp 'calendar-check-holidays)
  10552. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10553. (if hl (mapconcat 'identity hl "; "))))
  10554. (defun org-diary-sexp-entry (sexp entry date)
  10555. "Process a SEXP diary ENTRY for DATE."
  10556. (require 'diary-lib)
  10557. (let ((result (if calendar-debug-sexp
  10558. (let ((stack-trace-on-error t))
  10559. (eval (car (read-from-string sexp))))
  10560. (condition-case nil
  10561. (eval (car (read-from-string sexp)))
  10562. (error
  10563. (beep)
  10564. (message "Bad sexp at line %d in %s: %s"
  10565. (org-current-line)
  10566. (buffer-file-name) sexp)
  10567. (sleep-for 2))))))
  10568. (cond ((stringp result) result)
  10569. ((and (consp result)
  10570. (stringp (cdr result))) (cdr result))
  10571. (result entry)
  10572. (t nil))))
  10573. (defun org-diary-to-ical-string (frombuf)
  10574. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10575. This uses the icalendar.el library."
  10576. (let* ((tmpdir (if (featurep 'xemacs)
  10577. (temp-directory)
  10578. temporary-file-directory))
  10579. (tmpfile (make-temp-name
  10580. (expand-file-name "orgics" tmpdir)))
  10581. buf rtn b e)
  10582. (save-excursion
  10583. (set-buffer frombuf)
  10584. (icalendar-export-region (point-min) (point-max) tmpfile)
  10585. (setq buf (find-buffer-visiting tmpfile))
  10586. (set-buffer buf)
  10587. (goto-char (point-min))
  10588. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10589. (setq b (match-beginning 0)))
  10590. (goto-char (point-max))
  10591. (if (re-search-backward "^END:VEVENT" nil t)
  10592. (setq e (match-end 0)))
  10593. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10594. (kill-buffer buf)
  10595. (delete-file tmpfile)
  10596. rtn))
  10597. (defun org-closest-date (start current change prefer show-all)
  10598. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10599. When PREFER is `past' return a date that is either CURRENT or past.
  10600. When PREFER is `future', return a date that is either CURRENT or future.
  10601. When SHOW-ALL is nil, only return the current occurrence of a time stamp."
  10602. ;; Make the proper lists from the dates
  10603. (catch 'exit
  10604. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10605. dn dw sday cday n1 n2
  10606. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10607. (setq start (org-date-to-gregorian start)
  10608. current (org-date-to-gregorian
  10609. (if show-all
  10610. current
  10611. (time-to-days (current-time))))
  10612. sday (calendar-absolute-from-gregorian start)
  10613. cday (calendar-absolute-from-gregorian current))
  10614. (if (<= cday sday) (throw 'exit sday))
  10615. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10616. (setq dn (string-to-number (match-string 1 change))
  10617. dw (cdr (assoc (match-string 2 change) a1)))
  10618. (error "Invalid change specifyer: %s" change))
  10619. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10620. (cond
  10621. ((eq dw 'day)
  10622. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10623. n2 (+ n1 dn)))
  10624. ((eq dw 'year)
  10625. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10626. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10627. (setq date1 (list m d y1)
  10628. n1 (calendar-absolute-from-gregorian date1)
  10629. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10630. n2 (calendar-absolute-from-gregorian date2)))
  10631. ((eq dw 'month)
  10632. ;; approx number of month between the two dates
  10633. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10634. ;; How often does dn fit in there?
  10635. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10636. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10637. m (+ m nm)
  10638. ny (floor (/ m 12))
  10639. y (+ y ny)
  10640. m (- m (* ny 12)))
  10641. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10642. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10643. (setq m2 (+ m dn) y2 y)
  10644. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10645. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10646. (while (<= n2 cday)
  10647. (setq n1 n2 m m2 y y2)
  10648. (setq m2 (+ m dn) y2 y)
  10649. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10650. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10651. (if show-all
  10652. (cond
  10653. ((eq prefer 'past) n1)
  10654. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10655. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10656. (cond
  10657. ((eq prefer 'past) n1)
  10658. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10659. (t (if (= cday n1) n1 n2)))))))
  10660. (defun org-date-to-gregorian (date)
  10661. "Turn any specification of DATE into a gregorian date for the calendar."
  10662. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10663. ((and (listp date) (= (length date) 3)) date)
  10664. ((stringp date)
  10665. (setq date (org-parse-time-string date))
  10666. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10667. ((listp date)
  10668. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10669. (defun org-parse-time-string (s &optional nodefault)
  10670. "Parse the standard Org-mode time string.
  10671. This should be a lot faster than the normal `parse-time-string'.
  10672. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10673. hour and minute fields will be nil if not given."
  10674. (if (string-match org-ts-regexp0 s)
  10675. (list 0
  10676. (if (or (match-beginning 8) (not nodefault))
  10677. (string-to-number (or (match-string 8 s) "0")))
  10678. (if (or (match-beginning 7) (not nodefault))
  10679. (string-to-number (or (match-string 7 s) "0")))
  10680. (string-to-number (match-string 4 s))
  10681. (string-to-number (match-string 3 s))
  10682. (string-to-number (match-string 2 s))
  10683. nil nil nil)
  10684. (make-list 9 0)))
  10685. (defun org-timestamp-up (&optional arg)
  10686. "Increase the date item at the cursor by one.
  10687. If the cursor is on the year, change the year. If it is on the month or
  10688. the day, change that.
  10689. With prefix ARG, change by that many units."
  10690. (interactive "p")
  10691. (org-timestamp-change (prefix-numeric-value arg)))
  10692. (defun org-timestamp-down (&optional arg)
  10693. "Decrease the date item at the cursor by one.
  10694. If the cursor is on the year, change the year. If it is on the month or
  10695. the day, change that.
  10696. With prefix ARG, change by that many units."
  10697. (interactive "p")
  10698. (org-timestamp-change (- (prefix-numeric-value arg))))
  10699. (defun org-timestamp-up-day (&optional arg)
  10700. "Increase the date in the time stamp by one day.
  10701. With prefix ARG, change that many days."
  10702. (interactive "p")
  10703. (if (and (not (org-at-timestamp-p t))
  10704. (org-on-heading-p))
  10705. (org-todo 'up)
  10706. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10707. (defun org-timestamp-down-day (&optional arg)
  10708. "Decrease the date in the time stamp by one day.
  10709. With prefix ARG, change that many days."
  10710. (interactive "p")
  10711. (if (and (not (org-at-timestamp-p t))
  10712. (org-on-heading-p))
  10713. (org-todo 'down)
  10714. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10715. (defun org-at-timestamp-p (&optional inactive-ok)
  10716. "Determine if the cursor is in or at a timestamp."
  10717. (interactive)
  10718. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10719. (pos (point))
  10720. (ans (or (looking-at tsr)
  10721. (save-excursion
  10722. (skip-chars-backward "^[<\n\r\t")
  10723. (if (> (point) (point-min)) (backward-char 1))
  10724. (and (looking-at tsr)
  10725. (> (- (match-end 0) pos) -1))))))
  10726. (and ans
  10727. (boundp 'org-ts-what)
  10728. (setq org-ts-what
  10729. (cond
  10730. ((= pos (match-beginning 0)) 'bracket)
  10731. ((= pos (1- (match-end 0))) 'bracket)
  10732. ((org-pos-in-match-range pos 2) 'year)
  10733. ((org-pos-in-match-range pos 3) 'month)
  10734. ((org-pos-in-match-range pos 7) 'hour)
  10735. ((org-pos-in-match-range pos 8) 'minute)
  10736. ((or (org-pos-in-match-range pos 4)
  10737. (org-pos-in-match-range pos 5)) 'day)
  10738. ((and (> pos (or (match-end 8) (match-end 5)))
  10739. (< pos (match-end 0)))
  10740. (- pos (or (match-end 8) (match-end 5))))
  10741. (t 'day))))
  10742. ans))
  10743. (defun org-toggle-timestamp-type ()
  10744. "Toggle the type (<active> or [inactive]) of a time stamp."
  10745. (interactive)
  10746. (when (org-at-timestamp-p t)
  10747. (let ((beg (match-beginning 0)) (end (match-end 0))
  10748. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10749. (save-excursion
  10750. (goto-char beg)
  10751. (while (re-search-forward "[][<>]" end t)
  10752. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10753. t t)))
  10754. (message "Timestamp is now %sactive"
  10755. (if (equal (char-after beg) ?<) "" "in")))))
  10756. (defun org-timestamp-change (n &optional what)
  10757. "Change the date in the time stamp at point.
  10758. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10759. `year', `minute', `second'. If WHAT is not given, the cursor position
  10760. in the timestamp determines what will be changed."
  10761. (let ((pos (point))
  10762. with-hm inactive
  10763. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10764. org-ts-what
  10765. extra rem
  10766. ts time time0)
  10767. (if (not (org-at-timestamp-p t))
  10768. (error "Not at a timestamp"))
  10769. (if (and (not what) (eq org-ts-what 'bracket))
  10770. (org-toggle-timestamp-type)
  10771. (if (and (not what) (not (eq org-ts-what 'day))
  10772. org-display-custom-times
  10773. (get-text-property (point) 'display)
  10774. (not (get-text-property (1- (point)) 'display)))
  10775. (setq org-ts-what 'day))
  10776. (setq org-ts-what (or what org-ts-what)
  10777. inactive (= (char-after (match-beginning 0)) ?\[)
  10778. ts (match-string 0))
  10779. (replace-match "")
  10780. (if (string-match
  10781. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10782. ts)
  10783. (setq extra (match-string 1 ts)))
  10784. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10785. (setq with-hm t))
  10786. (setq time0 (org-parse-time-string ts))
  10787. (when (and (eq org-ts-what 'minute)
  10788. (eq current-prefix-arg nil))
  10789. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10790. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10791. (setcar (cdr time0) (+ (nth 1 time0)
  10792. (if (> n 0) (- rem) (- dm rem))))))
  10793. (setq time
  10794. (encode-time (or (car time0) 0)
  10795. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10796. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10797. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10798. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10799. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10800. (nthcdr 6 time0)))
  10801. (when (integerp org-ts-what)
  10802. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10803. (if (eq what 'calendar)
  10804. (let ((cal-date (org-get-date-from-calendar)))
  10805. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10806. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10807. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10808. (setcar time0 (or (car time0) 0))
  10809. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10810. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10811. (setq time (apply 'encode-time time0))))
  10812. (setq org-last-changed-timestamp
  10813. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10814. (org-clock-update-time-maybe)
  10815. (goto-char pos)
  10816. ;; Try to recenter the calendar window, if any
  10817. (if (and org-calendar-follow-timestamp-change
  10818. (get-buffer-window "*Calendar*" t)
  10819. (memq org-ts-what '(day month year)))
  10820. (org-recenter-calendar (time-to-days time))))))
  10821. (defun org-modify-ts-extra (s pos n dm)
  10822. "Change the different parts of the lead-time and repeat fields in timestamp."
  10823. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10824. ng h m new rem)
  10825. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10826. (cond
  10827. ((or (org-pos-in-match-range pos 2)
  10828. (org-pos-in-match-range pos 3))
  10829. (setq m (string-to-number (match-string 3 s))
  10830. h (string-to-number (match-string 2 s)))
  10831. (if (org-pos-in-match-range pos 2)
  10832. (setq h (+ h n))
  10833. (setq n (* dm (org-no-warnings (signum n))))
  10834. (when (not (= 0 (setq rem (% m dm))))
  10835. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10836. (setq m (+ m n)))
  10837. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10838. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10839. (setq h (min 24 (max 0 h)))
  10840. (setq ng 1 new (format "-%02d:%02d" h m)))
  10841. ((org-pos-in-match-range pos 6)
  10842. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10843. ((org-pos-in-match-range pos 5)
  10844. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10845. ((org-pos-in-match-range pos 9)
  10846. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10847. ((org-pos-in-match-range pos 8)
  10848. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10849. (when ng
  10850. (setq s (concat
  10851. (substring s 0 (match-beginning ng))
  10852. new
  10853. (substring s (match-end ng))))))
  10854. s))
  10855. (defun org-recenter-calendar (date)
  10856. "If the calendar is visible, recenter it to DATE."
  10857. (let* ((win (selected-window))
  10858. (cwin (get-buffer-window "*Calendar*" t))
  10859. (calendar-move-hook nil))
  10860. (when cwin
  10861. (select-window cwin)
  10862. (calendar-goto-date (if (listp date) date
  10863. (calendar-gregorian-from-absolute date)))
  10864. (select-window win))))
  10865. (defun org-goto-calendar (&optional arg)
  10866. "Go to the Emacs calendar at the current date.
  10867. If there is a time stamp in the current line, go to that date.
  10868. A prefix ARG can be used to force the current date."
  10869. (interactive "P")
  10870. (let ((tsr org-ts-regexp) diff
  10871. (calendar-move-hook nil)
  10872. (calendar-view-holidays-initially-flag nil)
  10873. (view-calendar-holidays-initially nil)
  10874. (calendar-view-diary-initially-flag nil)
  10875. (view-diary-entries-initially nil))
  10876. (if (or (org-at-timestamp-p)
  10877. (save-excursion
  10878. (beginning-of-line 1)
  10879. (looking-at (concat ".*" tsr))))
  10880. (let ((d1 (time-to-days (current-time)))
  10881. (d2 (time-to-days
  10882. (org-time-string-to-time (match-string 1)))))
  10883. (setq diff (- d2 d1))))
  10884. (calendar)
  10885. (calendar-goto-today)
  10886. (if (and diff (not arg)) (calendar-forward-day diff))))
  10887. (defun org-get-date-from-calendar ()
  10888. "Return a list (month day year) of date at point in calendar."
  10889. (with-current-buffer "*Calendar*"
  10890. (save-match-data
  10891. (calendar-cursor-to-date))))
  10892. (defun org-date-from-calendar ()
  10893. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10894. If there is already a time stamp at the cursor position, update it."
  10895. (interactive)
  10896. (if (org-at-timestamp-p t)
  10897. (org-timestamp-change 0 'calendar)
  10898. (let ((cal-date (org-get-date-from-calendar)))
  10899. (org-insert-time-stamp
  10900. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10901. (defun org-minutes-to-hh:mm-string (m)
  10902. "Compute H:MM from a number of minutes."
  10903. (let ((h (/ m 60)))
  10904. (setq m (- m (* 60 h)))
  10905. (format org-time-clocksum-format h m)))
  10906. (defun org-hh:mm-string-to-minutes (s)
  10907. "Convert a string H:MM to a number of minutes."
  10908. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10909. (+ (* (string-to-number (match-string 1 s)) 60)
  10910. (string-to-number (match-string 2 s)))
  10911. 0))
  10912. ;;;; Agenda files
  10913. ;;;###autoload
  10914. (defun org-iswitchb (&optional arg)
  10915. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10916. With a prefix argument, restrict available to files.
  10917. With two prefix arguments, restrict available buffers to agenda files.
  10918. Due to some yet unresolved reason, the global function
  10919. `iswitchb-mode' needs to be active for this function to work."
  10920. (interactive "P")
  10921. (require 'iswitchb)
  10922. (let ((enabled iswitchb-mode) blist)
  10923. (or enabled (iswitchb-mode 1))
  10924. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10925. ((equal arg '(16)) (org-buffer-list 'agenda))
  10926. (t (org-buffer-list))))
  10927. (unwind-protect
  10928. (let ((iswitchb-make-buflist-hook
  10929. (lambda ()
  10930. (setq iswitchb-temp-buflist
  10931. (mapcar 'buffer-name blist)))))
  10932. (switch-to-buffer
  10933. (iswitchb-read-buffer
  10934. "Switch-to: " nil t))
  10935. (or enabled (iswitchb-mode -1))))))
  10936. ;;;###autoload
  10937. (defun org-ido-switchb (&optional arg)
  10938. "Use `org-ido-completing-read' to prompt for an Org buffer to switch to.
  10939. With a prefix argument, restrict available to files.
  10940. With two prefix arguments, restrict available buffers to agenda files."
  10941. (interactive "P")
  10942. (let ((blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10943. ((equal arg '(16)) (org-buffer-list 'agenda))
  10944. (t (org-buffer-list)))))
  10945. (switch-to-buffer
  10946. (org-ido-completing-read "Org buffer: "
  10947. (mapcar 'buffer-name blist)
  10948. nil t))))
  10949. (defun org-buffer-list (&optional predicate exclude-tmp)
  10950. "Return a list of Org buffers.
  10951. PREDICATE can be `export', `files' or `agenda'.
  10952. export restrict the list to Export buffers.
  10953. files restrict the list to buffers visiting Org files.
  10954. agenda restrict the list to buffers visiting agenda files.
  10955. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10956. (let* ((bfn nil)
  10957. (agenda-files (and (eq predicate 'agenda)
  10958. (mapcar 'file-truename (org-agenda-files t))))
  10959. (filter
  10960. (cond
  10961. ((eq predicate 'files)
  10962. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10963. ((eq predicate 'export)
  10964. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10965. ((eq predicate 'agenda)
  10966. (lambda (b)
  10967. (with-current-buffer b
  10968. (and (eq major-mode 'org-mode)
  10969. (setq bfn (buffer-file-name b))
  10970. (member (file-truename bfn) agenda-files)))))
  10971. (t (lambda (b) (with-current-buffer b
  10972. (or (eq major-mode 'org-mode)
  10973. (string-match "\*Org .*Export"
  10974. (buffer-name b)))))))))
  10975. (delq nil
  10976. (mapcar
  10977. (lambda(b)
  10978. (if (and (funcall filter b)
  10979. (or (not exclude-tmp)
  10980. (not (string-match "tmp" (buffer-name b)))))
  10981. b
  10982. nil))
  10983. (buffer-list)))))
  10984. (defun org-agenda-files (&optional unrestricted archives)
  10985. "Get the list of agenda files.
  10986. Optional UNRESTRICTED means return the full list even if a restriction
  10987. is currently in place.
  10988. When ARCHIVES is t, include all archive files hat are really being
  10989. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10990. `org-agenda-archives-mode' is t."
  10991. (let ((files
  10992. (cond
  10993. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10994. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10995. ((listp org-agenda-files) org-agenda-files)
  10996. (t (error "Invalid value of `org-agenda-files'")))))
  10997. (setq files (apply 'append
  10998. (mapcar (lambda (f)
  10999. (if (file-directory-p f)
  11000. (directory-files
  11001. f t org-agenda-file-regexp)
  11002. (list f)))
  11003. files)))
  11004. (when org-agenda-skip-unavailable-files
  11005. (setq files (delq nil
  11006. (mapcar (function
  11007. (lambda (file)
  11008. (and (file-readable-p file) file)))
  11009. files))))
  11010. (when (or (eq archives t)
  11011. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  11012. (setq files (org-add-archive-files files)))
  11013. files))
  11014. (defun org-edit-agenda-file-list ()
  11015. "Edit the list of agenda files.
  11016. Depending on setup, this either uses customize to edit the variable
  11017. `org-agenda-files', or it visits the file that is holding the list. In the
  11018. latter case, the buffer is set up in a way that saving it automatically kills
  11019. the buffer and restores the previous window configuration."
  11020. (interactive)
  11021. (if (stringp org-agenda-files)
  11022. (let ((cw (current-window-configuration)))
  11023. (find-file org-agenda-files)
  11024. (org-set-local 'org-window-configuration cw)
  11025. (org-add-hook 'after-save-hook
  11026. (lambda ()
  11027. (set-window-configuration
  11028. (prog1 org-window-configuration
  11029. (kill-buffer (current-buffer))))
  11030. (org-install-agenda-files-menu)
  11031. (message "New agenda file list installed"))
  11032. nil 'local)
  11033. (message "%s" (substitute-command-keys
  11034. "Edit list and finish with \\[save-buffer]")))
  11035. (customize-variable 'org-agenda-files)))
  11036. (defun org-store-new-agenda-file-list (list)
  11037. "Set new value for the agenda file list and save it correctly."
  11038. (if (stringp org-agenda-files)
  11039. (let ((f org-agenda-files) b)
  11040. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  11041. (with-temp-file f
  11042. (insert (mapconcat 'identity list "\n") "\n")))
  11043. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  11044. (setq org-agenda-files list)
  11045. (customize-save-variable 'org-agenda-files org-agenda-files))))
  11046. (defun org-read-agenda-file-list ()
  11047. "Read the list of agenda files from a file."
  11048. (when (file-directory-p org-agenda-files)
  11049. (error "`org-agenda-files' cannot be a single directory"))
  11050. (when (stringp org-agenda-files)
  11051. (with-temp-buffer
  11052. (insert-file-contents org-agenda-files)
  11053. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  11054. ;;;###autoload
  11055. (defun org-cycle-agenda-files ()
  11056. "Cycle through the files in `org-agenda-files'.
  11057. If the current buffer visits an agenda file, find the next one in the list.
  11058. If the current buffer does not, find the first agenda file."
  11059. (interactive)
  11060. (let* ((fs (org-agenda-files t))
  11061. (files (append fs (list (car fs))))
  11062. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  11063. file)
  11064. (unless files (error "No agenda files"))
  11065. (catch 'exit
  11066. (while (setq file (pop files))
  11067. (if (equal (file-truename file) tcf)
  11068. (when (car files)
  11069. (find-file (car files))
  11070. (throw 'exit t))))
  11071. (find-file (car fs)))
  11072. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  11073. (defun org-agenda-file-to-front (&optional to-end)
  11074. "Move/add the current file to the top of the agenda file list.
  11075. If the file is not present in the list, it is added to the front. If it is
  11076. present, it is moved there. With optional argument TO-END, add/move to the
  11077. end of the list."
  11078. (interactive "P")
  11079. (let ((org-agenda-skip-unavailable-files nil)
  11080. (file-alist (mapcar (lambda (x)
  11081. (cons (file-truename x) x))
  11082. (org-agenda-files t)))
  11083. (ctf (file-truename buffer-file-name))
  11084. x had)
  11085. (setq x (assoc ctf file-alist) had x)
  11086. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  11087. (if to-end
  11088. (setq file-alist (append (delq x file-alist) (list x)))
  11089. (setq file-alist (cons x (delq x file-alist))))
  11090. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  11091. (org-install-agenda-files-menu)
  11092. (message "File %s to %s of agenda file list"
  11093. (if had "moved" "added") (if to-end "end" "front"))))
  11094. (defun org-remove-file (&optional file)
  11095. "Remove current file from the list of files in variable `org-agenda-files'.
  11096. These are the files which are being checked for agenda entries.
  11097. Optional argument FILE means, use this file instead of the current."
  11098. (interactive)
  11099. (let* ((org-agenda-skip-unavailable-files nil)
  11100. (file (or file buffer-file-name))
  11101. (true-file (file-truename file))
  11102. (afile (abbreviate-file-name file))
  11103. (files (delq nil (mapcar
  11104. (lambda (x)
  11105. (if (equal true-file
  11106. (file-truename x))
  11107. nil x))
  11108. (org-agenda-files t)))))
  11109. (if (not (= (length files) (length (org-agenda-files t))))
  11110. (progn
  11111. (org-store-new-agenda-file-list files)
  11112. (org-install-agenda-files-menu)
  11113. (message "Removed file: %s" afile))
  11114. (message "File was not in list: %s (not removed)" afile))))
  11115. (defun org-file-menu-entry (file)
  11116. (vector file (list 'find-file file) t))
  11117. (defun org-check-agenda-file (file)
  11118. "Make sure FILE exists. If not, ask user what to do."
  11119. (when (not (file-exists-p file))
  11120. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11121. (abbreviate-file-name file))
  11122. (let ((r (downcase (read-char-exclusive))))
  11123. (cond
  11124. ((equal r ?r)
  11125. (org-remove-file file)
  11126. (throw 'nextfile t))
  11127. (t (error "Abort"))))))
  11128. (defun org-get-agenda-file-buffer (file)
  11129. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11130. it to the list of buffers which might be released later."
  11131. (let ((buf (org-find-base-buffer-visiting file)))
  11132. (if buf
  11133. buf ; just return it
  11134. ;; Make a new buffer and remember it
  11135. (setq buf (find-file-noselect file))
  11136. (if buf (push buf org-agenda-new-buffers))
  11137. buf)))
  11138. (defun org-release-buffers (blist)
  11139. "Release all buffers in list, asking the user for confirmation when needed.
  11140. When a buffer is unmodified, it is just killed. When modified, it is saved
  11141. \(if the user agrees) and then killed."
  11142. (let (buf file)
  11143. (while (setq buf (pop blist))
  11144. (setq file (buffer-file-name buf))
  11145. (when (and (buffer-modified-p buf)
  11146. file
  11147. (y-or-n-p (format "Save file %s? " file)))
  11148. (with-current-buffer buf (save-buffer)))
  11149. (kill-buffer buf))))
  11150. (defun org-prepare-agenda-buffers (files)
  11151. "Create buffers for all agenda files, protect archived trees and comments."
  11152. (interactive)
  11153. (let ((pa '(:org-archived t))
  11154. (pc '(:org-comment t))
  11155. (pall '(:org-archived t :org-comment t))
  11156. (inhibit-read-only t)
  11157. (rea (concat ":" org-archive-tag ":"))
  11158. bmp file re)
  11159. (save-excursion
  11160. (save-restriction
  11161. (while (setq file (pop files))
  11162. (if (bufferp file)
  11163. (set-buffer file)
  11164. (org-check-agenda-file file)
  11165. (set-buffer (org-get-agenda-file-buffer file)))
  11166. (widen)
  11167. (setq bmp (buffer-modified-p))
  11168. (org-refresh-category-properties)
  11169. (setq org-todo-keywords-for-agenda
  11170. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11171. (setq org-done-keywords-for-agenda
  11172. (append org-done-keywords-for-agenda org-done-keywords))
  11173. (setq org-todo-keyword-alist-for-agenda
  11174. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11175. (setq org-tag-alist-for-agenda
  11176. (append org-tag-alist-for-agenda org-tag-alist))
  11177. (save-excursion
  11178. (remove-text-properties (point-min) (point-max) pall)
  11179. (when org-agenda-skip-archived-trees
  11180. (goto-char (point-min))
  11181. (while (re-search-forward rea nil t)
  11182. (if (org-on-heading-p t)
  11183. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11184. (goto-char (point-min))
  11185. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11186. (while (re-search-forward re nil t)
  11187. (add-text-properties
  11188. (match-beginning 0) (org-end-of-subtree t) pc)))
  11189. (set-buffer-modified-p bmp))))
  11190. (setq org-todo-keyword-alist-for-agenda
  11191. (org-uniquify org-todo-keyword-alist-for-agenda)
  11192. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11193. ;;;; Embedded LaTeX
  11194. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11195. "Keymap for the minor `org-cdlatex-mode'.")
  11196. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11197. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11198. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11199. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11200. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11201. (defvar org-cdlatex-texmathp-advice-is-done nil
  11202. "Flag remembering if we have applied the advice to texmathp already.")
  11203. (define-minor-mode org-cdlatex-mode
  11204. "Toggle the minor `org-cdlatex-mode'.
  11205. This mode supports entering LaTeX environment and math in LaTeX fragments
  11206. in Org-mode.
  11207. \\{org-cdlatex-mode-map}"
  11208. nil " OCDL" nil
  11209. (when org-cdlatex-mode (require 'cdlatex))
  11210. (unless org-cdlatex-texmathp-advice-is-done
  11211. (setq org-cdlatex-texmathp-advice-is-done t)
  11212. (defadvice texmathp (around org-math-always-on activate)
  11213. "Always return t in org-mode buffers.
  11214. This is because we want to insert math symbols without dollars even outside
  11215. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11216. an embedded LaTeX fragment, let texmathp do its job.
  11217. \\[org-cdlatex-mode-map]"
  11218. (interactive)
  11219. (let (p)
  11220. (cond
  11221. ((not (org-mode-p)) ad-do-it)
  11222. ((eq this-command 'cdlatex-math-symbol)
  11223. (setq ad-return-value t
  11224. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11225. (t
  11226. (let ((p (org-inside-LaTeX-fragment-p)))
  11227. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11228. (setq ad-return-value t
  11229. texmathp-why '("Org-mode embedded math" . 0))
  11230. (if p ad-do-it)))))))))
  11231. (defun turn-on-org-cdlatex ()
  11232. "Unconditionally turn on `org-cdlatex-mode'."
  11233. (org-cdlatex-mode 1))
  11234. (defun org-inside-LaTeX-fragment-p ()
  11235. "Test if point is inside a LaTeX fragment.
  11236. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11237. sequence appearing also before point.
  11238. Even though the matchers for math are configurable, this function assumes
  11239. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11240. delimiters are skipped when they have been removed by customization.
  11241. The return value is nil, or a cons cell with the delimiter and
  11242. and the position of this delimiter.
  11243. This function does a reasonably good job, but can locally be fooled by
  11244. for example currency specifications. For example it will assume being in
  11245. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11246. fragments that are properly closed, but during editing, we have to live
  11247. with the uncertainty caused by missing closing delimiters. This function
  11248. looks only before point, not after."
  11249. (catch 'exit
  11250. (let ((pos (point))
  11251. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11252. (lim (progn
  11253. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11254. (point)))
  11255. dd-on str (start 0) m re)
  11256. (goto-char pos)
  11257. (when dodollar
  11258. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11259. re (nth 1 (assoc "$" org-latex-regexps)))
  11260. (while (string-match re str start)
  11261. (cond
  11262. ((= (match-end 0) (length str))
  11263. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11264. ((= (match-end 0) (- (length str) 5))
  11265. (throw 'exit nil))
  11266. (t (setq start (match-end 0))))))
  11267. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11268. (goto-char pos)
  11269. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11270. (and (match-beginning 2) (throw 'exit nil))
  11271. ;; count $$
  11272. (while (re-search-backward "\\$\\$" lim t)
  11273. (setq dd-on (not dd-on)))
  11274. (goto-char pos)
  11275. (if dd-on (cons "$$" m))))))
  11276. (defun org-try-cdlatex-tab ()
  11277. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11278. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11279. - inside a LaTeX fragment, or
  11280. - after the first word in a line, where an abbreviation expansion could
  11281. insert a LaTeX environment."
  11282. (when org-cdlatex-mode
  11283. (cond
  11284. ((save-excursion
  11285. (skip-chars-backward "a-zA-Z0-9*")
  11286. (skip-chars-backward " \t")
  11287. (bolp))
  11288. (cdlatex-tab) t)
  11289. ((org-inside-LaTeX-fragment-p)
  11290. (cdlatex-tab) t)
  11291. (t nil))))
  11292. (defun org-cdlatex-underscore-caret (&optional arg)
  11293. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11294. Revert to the normal definition outside of these fragments."
  11295. (interactive "P")
  11296. (if (org-inside-LaTeX-fragment-p)
  11297. (call-interactively 'cdlatex-sub-superscript)
  11298. (let (org-cdlatex-mode)
  11299. (call-interactively (key-binding (vector last-input-event))))))
  11300. (defun org-cdlatex-math-modify (&optional arg)
  11301. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11302. Revert to the normal definition outside of these fragments."
  11303. (interactive "P")
  11304. (if (org-inside-LaTeX-fragment-p)
  11305. (call-interactively 'cdlatex-math-modify)
  11306. (let (org-cdlatex-mode)
  11307. (call-interactively (key-binding (vector last-input-event))))))
  11308. (defvar org-latex-fragment-image-overlays nil
  11309. "List of overlays carrying the images of latex fragments.")
  11310. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11311. (defun org-remove-latex-fragment-image-overlays ()
  11312. "Remove all overlays with LaTeX fragment images in current buffer."
  11313. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11314. (setq org-latex-fragment-image-overlays nil))
  11315. (defun org-preview-latex-fragment (&optional subtree)
  11316. "Preview the LaTeX fragment at point, or all locally or globally.
  11317. If the cursor is in a LaTeX fragment, create the image and overlay
  11318. it over the source code. If there is no fragment at point, display
  11319. all fragments in the current text, from one headline to the next. With
  11320. prefix SUBTREE, display all fragments in the current subtree. With a
  11321. double prefix `C-u C-u', or when the cursor is before the first headline,
  11322. display all fragments in the buffer.
  11323. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11324. (interactive "P")
  11325. (org-remove-latex-fragment-image-overlays)
  11326. (save-excursion
  11327. (save-restriction
  11328. (let (beg end at msg)
  11329. (cond
  11330. ((or (equal subtree '(16))
  11331. (not (save-excursion
  11332. (re-search-backward (concat "^" outline-regexp) nil t))))
  11333. (setq beg (point-min) end (point-max)
  11334. msg "Creating images for buffer...%s"))
  11335. ((equal subtree '(4))
  11336. (org-back-to-heading)
  11337. (setq beg (point) end (org-end-of-subtree t)
  11338. msg "Creating images for subtree...%s"))
  11339. (t
  11340. (if (setq at (org-inside-LaTeX-fragment-p))
  11341. (goto-char (max (point-min) (- (cdr at) 2)))
  11342. (org-back-to-heading))
  11343. (setq beg (point) end (progn (outline-next-heading) (point))
  11344. msg (if at "Creating image...%s"
  11345. "Creating images for entry...%s"))))
  11346. (message msg "")
  11347. (narrow-to-region beg end)
  11348. (goto-char beg)
  11349. (org-format-latex
  11350. (concat "ltxpng/" (file-name-sans-extension
  11351. (file-name-nondirectory
  11352. buffer-file-name)))
  11353. default-directory 'overlays msg at 'forbuffer)
  11354. (message msg "done. Use `C-c C-c' to remove images.")))))
  11355. (defvar org-latex-regexps
  11356. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11357. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11358. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11359. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11360. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11361. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11362. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11363. "Regular expressions for matching embedded LaTeX.")
  11364. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11365. "Replace LaTeX fragments with links to an image, and produce images."
  11366. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11367. (let* ((prefixnodir (file-name-nondirectory prefix))
  11368. (absprefix (expand-file-name prefix dir))
  11369. (todir (file-name-directory absprefix))
  11370. (opt org-format-latex-options)
  11371. (matchers (plist-get opt :matchers))
  11372. (re-list org-latex-regexps)
  11373. (cnt 0) txt link beg end re e checkdir
  11374. m n block linkfile movefile ov)
  11375. ;; Check if there are old images files with this prefix, and remove them
  11376. (when (file-directory-p todir)
  11377. (mapc 'delete-file
  11378. (directory-files
  11379. todir 'full
  11380. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11381. ;; Check the different regular expressions
  11382. (while (setq e (pop re-list))
  11383. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11384. block (if (nth 3 e) "\n\n" ""))
  11385. (when (member m matchers)
  11386. (goto-char (point-min))
  11387. (while (re-search-forward re nil t)
  11388. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11389. (setq txt (match-string n)
  11390. beg (match-beginning n) end (match-end n)
  11391. cnt (1+ cnt)
  11392. linkfile (format "%s_%04d.png" prefix cnt)
  11393. movefile (format "%s_%04d.png" absprefix cnt)
  11394. link (concat block "[[file:" linkfile "]]" block))
  11395. (if msg (message msg cnt))
  11396. (goto-char beg)
  11397. (unless checkdir ; make sure the directory exists
  11398. (setq checkdir t)
  11399. (or (file-directory-p todir) (make-directory todir)))
  11400. (org-create-formula-image
  11401. txt movefile opt forbuffer)
  11402. (if overlays
  11403. (progn
  11404. (setq ov (org-make-overlay beg end))
  11405. (if (featurep 'xemacs)
  11406. (progn
  11407. (org-overlay-put ov 'invisible t)
  11408. (org-overlay-put
  11409. ov 'end-glyph
  11410. (make-glyph (vector 'png :file movefile))))
  11411. (org-overlay-put
  11412. ov 'display
  11413. (list 'image :type 'png :file movefile :ascent 'center)))
  11414. (push ov org-latex-fragment-image-overlays)
  11415. (goto-char end))
  11416. (delete-region beg end)
  11417. (insert link))))))))
  11418. ;; This function borrows from Ganesh Swami's latex2png.el
  11419. (defun org-create-formula-image (string tofile options buffer)
  11420. (let* ((tmpdir (if (featurep 'xemacs)
  11421. (temp-directory)
  11422. temporary-file-directory))
  11423. (texfilebase (make-temp-name
  11424. (expand-file-name "orgtex" tmpdir)))
  11425. (texfile (concat texfilebase ".tex"))
  11426. (dvifile (concat texfilebase ".dvi"))
  11427. (pngfile (concat texfilebase ".png"))
  11428. (fnh (if (featurep 'xemacs)
  11429. (font-height (get-face-font 'default))
  11430. (face-attribute 'default :height nil)))
  11431. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11432. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11433. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11434. "Black"))
  11435. (bg (or (plist-get options (if buffer :background :html-background))
  11436. "Transparent")))
  11437. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11438. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11439. (with-temp-file texfile
  11440. (insert org-format-latex-header
  11441. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11442. (let ((dir default-directory))
  11443. (condition-case nil
  11444. (progn
  11445. (cd tmpdir)
  11446. (call-process "latex" nil nil nil texfile))
  11447. (error nil))
  11448. (cd dir))
  11449. (if (not (file-exists-p dvifile))
  11450. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11451. (condition-case nil
  11452. (call-process "dvipng" nil nil nil
  11453. "-E" "-fg" fg "-bg" bg
  11454. "-D" dpi
  11455. ;;"-x" scale "-y" scale
  11456. "-T" "tight"
  11457. "-o" pngfile
  11458. dvifile)
  11459. (error nil))
  11460. (if (not (file-exists-p pngfile))
  11461. (progn (message "Failed to create png file from %s" texfile) nil)
  11462. ;; Use the requested file name and clean up
  11463. (copy-file pngfile tofile 'replace)
  11464. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11465. (delete-file (concat texfilebase e)))
  11466. pngfile))))
  11467. (defun org-dvipng-color (attr)
  11468. "Return an rgb color specification for dvipng."
  11469. (apply 'format "rgb %s %s %s"
  11470. (mapcar 'org-normalize-color
  11471. (color-values (face-attribute 'default attr nil)))))
  11472. (defun org-normalize-color (value)
  11473. "Return string to be used as color value for an RGB component."
  11474. (format "%g" (/ value 65535.0)))
  11475. ;;;; Key bindings
  11476. ;; Make `C-c C-x' a prefix key
  11477. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11478. ;; TAB key with modifiers
  11479. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11480. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11481. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11482. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11483. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11484. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11485. ;; The following line is necessary under Suse GNU/Linux
  11486. (unless (featurep 'xemacs)
  11487. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11488. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11489. (define-key org-mode-map [backtab] 'org-shifttab)
  11490. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11491. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11492. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11493. ;; Cursor keys with modifiers
  11494. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11495. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11496. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11497. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11498. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11499. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11500. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11501. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11502. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11503. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11504. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11505. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11506. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11507. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11508. ;;; Extra keys for tty access.
  11509. ;; We only set them when really needed because otherwise the
  11510. ;; menus don't show the simple keys
  11511. (when (or org-use-extra-keys
  11512. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11513. (not window-system))
  11514. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11515. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11516. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11517. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11518. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11519. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11520. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11521. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11522. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11523. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11524. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11525. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11526. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11527. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11528. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11529. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11530. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11531. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11532. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11533. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11534. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11535. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11536. ;; All the other keys
  11537. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11538. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11539. (if (boundp 'narrow-map)
  11540. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11541. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11542. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11543. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11544. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11545. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11546. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11547. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11548. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11549. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11550. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11551. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11552. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11553. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11554. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11555. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11556. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11557. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11558. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11559. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11560. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11561. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11562. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11563. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11564. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11565. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11566. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11567. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11568. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11569. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11570. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11571. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11572. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11573. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11574. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11575. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11576. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11577. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11578. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11579. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11580. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11581. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11582. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11583. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11584. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11585. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11586. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11587. (org-defkey org-mode-map "\C-m" 'org-return)
  11588. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11589. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11590. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11591. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11592. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11593. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11594. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11595. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11596. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11597. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11598. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11599. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11600. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11601. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11602. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11603. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11604. (org-defkey org-mode-map "\C-c\C-xf" 'org-footnote-action)
  11605. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11606. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11607. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11608. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11609. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11610. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11611. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11612. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11613. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11614. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11615. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11616. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11617. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11618. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11619. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11620. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11621. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11622. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11623. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11624. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11625. (when (featurep 'xemacs)
  11626. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11627. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11628. (defun org-self-insert-command (N)
  11629. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11630. If the cursor is in a table looking at whitespace, the whitespace is
  11631. overwritten, and the table is not marked as requiring realignment."
  11632. (interactive "p")
  11633. (if (and (org-table-p)
  11634. (progn
  11635. ;; check if we blank the field, and if that triggers align
  11636. (and (featurep 'org-table) org-table-auto-blank-field
  11637. (member last-command
  11638. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11639. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11640. ;; got extra space, this field does not determine column width
  11641. (let (org-table-may-need-update) (org-table-blank-field))
  11642. ;; no extra space, this field may determine column width
  11643. (org-table-blank-field)))
  11644. t)
  11645. (eq N 1)
  11646. (looking-at "[^|\n]* |"))
  11647. (let (org-table-may-need-update)
  11648. (goto-char (1- (match-end 0)))
  11649. (delete-backward-char 1)
  11650. (goto-char (match-beginning 0))
  11651. (self-insert-command N))
  11652. (setq org-table-may-need-update t)
  11653. (self-insert-command N)
  11654. (org-fix-tags-on-the-fly)))
  11655. (defun org-fix-tags-on-the-fly ()
  11656. (when (and (equal (char-after (point-at-bol)) ?*)
  11657. (org-on-heading-p))
  11658. (org-align-tags-here org-tags-column)))
  11659. (defun org-delete-backward-char (N)
  11660. "Like `delete-backward-char', insert whitespace at field end in tables.
  11661. When deleting backwards, in tables this function will insert whitespace in
  11662. front of the next \"|\" separator, to keep the table aligned. The table will
  11663. still be marked for re-alignment if the field did fill the entire column,
  11664. because, in this case the deletion might narrow the column."
  11665. (interactive "p")
  11666. (if (and (org-table-p)
  11667. (eq N 1)
  11668. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11669. (looking-at ".*?|"))
  11670. (let ((pos (point))
  11671. (noalign (looking-at "[^|\n\r]* |"))
  11672. (c org-table-may-need-update))
  11673. (backward-delete-char N)
  11674. (skip-chars-forward "^|")
  11675. (insert " ")
  11676. (goto-char (1- pos))
  11677. ;; noalign: if there were two spaces at the end, this field
  11678. ;; does not determine the width of the column.
  11679. (if noalign (setq org-table-may-need-update c)))
  11680. (backward-delete-char N)
  11681. (org-fix-tags-on-the-fly)))
  11682. (defun org-delete-char (N)
  11683. "Like `delete-char', but insert whitespace at field end in tables.
  11684. When deleting characters, in tables this function will insert whitespace in
  11685. front of the next \"|\" separator, to keep the table aligned. The table will
  11686. still be marked for re-alignment if the field did fill the entire column,
  11687. because, in this case the deletion might narrow the column."
  11688. (interactive "p")
  11689. (if (and (org-table-p)
  11690. (not (bolp))
  11691. (not (= (char-after) ?|))
  11692. (eq N 1))
  11693. (if (looking-at ".*?|")
  11694. (let ((pos (point))
  11695. (noalign (looking-at "[^|\n\r]* |"))
  11696. (c org-table-may-need-update))
  11697. (replace-match (concat
  11698. (substring (match-string 0) 1 -1)
  11699. " |"))
  11700. (goto-char pos)
  11701. ;; noalign: if there were two spaces at the end, this field
  11702. ;; does not determine the width of the column.
  11703. (if noalign (setq org-table-may-need-update c)))
  11704. (delete-char N))
  11705. (delete-char N)
  11706. (org-fix-tags-on-the-fly)))
  11707. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11708. (put 'org-self-insert-command 'delete-selection t)
  11709. (put 'orgtbl-self-insert-command 'delete-selection t)
  11710. (put 'org-delete-char 'delete-selection 'supersede)
  11711. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11712. ;; Make `flyspell-mode' delay after some commands
  11713. (put 'org-self-insert-command 'flyspell-delayed t)
  11714. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11715. (put 'org-delete-char 'flyspell-delayed t)
  11716. (put 'org-delete-backward-char 'flyspell-delayed t)
  11717. ;; Make pabbrev-mode expand after org-mode commands
  11718. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11719. (put 'orgtbl-self-insert-command 'pabbrev-expand-after-command t)
  11720. ;; How to do this: Measure non-white length of current string
  11721. ;; If equal to column width, we should realign.
  11722. (defun org-remap (map &rest commands)
  11723. "In MAP, remap the functions given in COMMANDS.
  11724. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11725. (let (new old)
  11726. (while commands
  11727. (setq old (pop commands) new (pop commands))
  11728. (if (fboundp 'command-remapping)
  11729. (org-defkey map (vector 'remap old) new)
  11730. (substitute-key-definition old new map global-map)))))
  11731. (when (eq org-enable-table-editor 'optimized)
  11732. ;; If the user wants maximum table support, we need to hijack
  11733. ;; some standard editing functions
  11734. (org-remap org-mode-map
  11735. 'self-insert-command 'org-self-insert-command
  11736. 'delete-char 'org-delete-char
  11737. 'delete-backward-char 'org-delete-backward-char)
  11738. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11739. (defun org-shiftcursor-error ()
  11740. "Throw an error because Shift-Cursor command was applied in wrong context."
  11741. (error "This command is active in special context like tables, headlines or timestamps"))
  11742. (defun org-shifttab (&optional arg)
  11743. "Global visibility cycling or move to previous table field.
  11744. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11745. on context.
  11746. See the individual commands for more information."
  11747. (interactive "P")
  11748. (cond
  11749. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11750. ((integerp arg)
  11751. (message "Content view to level: %d" arg)
  11752. (org-content (prefix-numeric-value arg))
  11753. (setq org-cycle-global-status 'overview))
  11754. (t (call-interactively 'org-global-cycle))))
  11755. (defun org-shiftmetaleft ()
  11756. "Promote subtree or delete table column.
  11757. Calls `org-promote-subtree', `org-outdent-item',
  11758. or `org-table-delete-column', depending on context.
  11759. See the individual commands for more information."
  11760. (interactive)
  11761. (cond
  11762. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11763. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11764. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11765. (t (org-shiftcursor-error))))
  11766. (defun org-shiftmetaright ()
  11767. "Demote subtree or insert table column.
  11768. Calls `org-demote-subtree', `org-indent-item',
  11769. or `org-table-insert-column', depending on context.
  11770. See the individual commands for more information."
  11771. (interactive)
  11772. (cond
  11773. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11774. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11775. ((org-at-item-p) (call-interactively 'org-indent-item))
  11776. (t (org-shiftcursor-error))))
  11777. (defun org-shiftmetaup (&optional arg)
  11778. "Move subtree up or kill table row.
  11779. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11780. `org-move-item-up' depending on context. See the individual commands
  11781. for more information."
  11782. (interactive "P")
  11783. (cond
  11784. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11785. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11786. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11787. (t (org-shiftcursor-error))))
  11788. (defun org-shiftmetadown (&optional arg)
  11789. "Move subtree down or insert table row.
  11790. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11791. `org-move-item-down', depending on context. See the individual
  11792. commands for more information."
  11793. (interactive "P")
  11794. (cond
  11795. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11796. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11797. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11798. (t (org-shiftcursor-error))))
  11799. (defun org-metaleft (&optional arg)
  11800. "Promote heading or move table column to left.
  11801. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11802. With no specific context, calls the Emacs default `backward-word'.
  11803. See the individual commands for more information."
  11804. (interactive "P")
  11805. (cond
  11806. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11807. ((or (org-on-heading-p) (org-region-active-p))
  11808. (call-interactively 'org-do-promote))
  11809. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11810. (t (call-interactively 'backward-word))))
  11811. (defun org-metaright (&optional arg)
  11812. "Demote subtree or move table column to right.
  11813. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11814. With no specific context, calls the Emacs default `forward-word'.
  11815. See the individual commands for more information."
  11816. (interactive "P")
  11817. (cond
  11818. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11819. ((or (org-on-heading-p) (org-region-active-p))
  11820. (call-interactively 'org-do-demote))
  11821. ((org-at-item-p) (call-interactively 'org-indent-item))
  11822. (t (call-interactively 'forward-word))))
  11823. (defun org-metaup (&optional arg)
  11824. "Move subtree up or move table row up.
  11825. Calls `org-move-subtree-up' or `org-table-move-row' or
  11826. `org-move-item-up', depending on context. See the individual commands
  11827. for more information."
  11828. (interactive "P")
  11829. (cond
  11830. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11831. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11832. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11833. (t (transpose-lines 1) (beginning-of-line -1))))
  11834. (defun org-metadown (&optional arg)
  11835. "Move subtree down or move table row down.
  11836. Calls `org-move-subtree-down' or `org-table-move-row' or
  11837. `org-move-item-down', depending on context. See the individual
  11838. commands for more information."
  11839. (interactive "P")
  11840. (cond
  11841. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11842. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11843. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11844. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11845. (defun org-shiftup (&optional arg)
  11846. "Increase item in timestamp or increase priority of current headline.
  11847. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11848. depending on context. See the individual commands for more information."
  11849. (interactive "P")
  11850. (cond
  11851. ((org-at-timestamp-p t)
  11852. (call-interactively (if org-edit-timestamp-down-means-later
  11853. 'org-timestamp-down 'org-timestamp-up)))
  11854. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11855. ((org-at-item-p) (call-interactively 'org-previous-item))
  11856. ((org-clocktable-try-shift 'up arg))
  11857. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11858. (defun org-shiftdown (&optional arg)
  11859. "Decrease item in timestamp or decrease priority of current headline.
  11860. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11861. depending on context. See the individual commands for more information."
  11862. (interactive "P")
  11863. (cond
  11864. ((org-at-timestamp-p t)
  11865. (call-interactively (if org-edit-timestamp-down-means-later
  11866. 'org-timestamp-up 'org-timestamp-down)))
  11867. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11868. ((org-clocktable-try-shift 'down arg))
  11869. (t (call-interactively 'org-next-item))))
  11870. (defun org-shiftright (&optional arg)
  11871. "Cycle the thing at point or in the current line, depending on context.
  11872. Depending on context, this does one of the following:
  11873. - switch a timestamp at point one day into the future
  11874. - on a headline, switch to the next TODO keyword.
  11875. - on an item, switch entire list to the next bullet type
  11876. - on a property line, switch to the next allowed value
  11877. - on a clocktable definition line, move time block into the future"
  11878. (interactive "P")
  11879. (cond
  11880. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11881. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11882. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11883. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11884. ((org-clocktable-try-shift 'right arg))
  11885. (t (org-shiftcursor-error))))
  11886. (defun org-shiftleft (&optional arg)
  11887. "Cycle the thing at point or in the current line, depending on context.
  11888. Depending on context, this does one of the following:
  11889. - switch a timestamp at point one day into the past
  11890. - on a headline, switch to the previous TODO keyword.
  11891. - on an item, switch entire list to the previous bullet type
  11892. - on a property line, switch to the previous allowed value
  11893. - on a clocktable definition line, move time block into the past"
  11894. (interactive "P")
  11895. (cond
  11896. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11897. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11898. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11899. ((org-at-property-p)
  11900. (call-interactively 'org-property-previous-allowed-value))
  11901. ((org-clocktable-try-shift 'left arg))
  11902. (t (org-shiftcursor-error))))
  11903. (defun org-shiftcontrolright ()
  11904. "Switch to next TODO set."
  11905. (interactive)
  11906. (cond
  11907. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11908. (t (org-shiftcursor-error))))
  11909. (defun org-shiftcontrolleft ()
  11910. "Switch to previous TODO set."
  11911. (interactive)
  11912. (cond
  11913. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11914. (t (org-shiftcursor-error))))
  11915. (defun org-ctrl-c-ret ()
  11916. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11917. (interactive)
  11918. (cond
  11919. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11920. (t (call-interactively 'org-insert-heading))))
  11921. (defun org-copy-special ()
  11922. "Copy region in table or copy current subtree.
  11923. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11924. See the individual commands for more information."
  11925. (interactive)
  11926. (call-interactively
  11927. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11928. (defun org-cut-special ()
  11929. "Cut region in table or cut current subtree.
  11930. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11931. See the individual commands for more information."
  11932. (interactive)
  11933. (call-interactively
  11934. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11935. (defun org-paste-special (arg)
  11936. "Paste rectangular region into table, or past subtree relative to level.
  11937. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11938. See the individual commands for more information."
  11939. (interactive "P")
  11940. (if (org-at-table-p)
  11941. (org-table-paste-rectangle)
  11942. (org-paste-subtree arg)))
  11943. (defun org-edit-special ()
  11944. "Call a special editor for the stuff at point.
  11945. When at a table, call the formula editor with `org-table-edit-formulas'.
  11946. When at the first line of an src example, call `org-edit-src-code'.
  11947. When in an #+include line, visit the include file. Otherwise call
  11948. `ffap' to visit the file at point."
  11949. (interactive)
  11950. (cond
  11951. ((org-at-table-p)
  11952. (call-interactively 'org-table-edit-formulas))
  11953. ((save-excursion
  11954. (beginning-of-line 1)
  11955. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11956. (find-file (org-trim (match-string 1))))
  11957. ((org-edit-src-code))
  11958. ((org-edit-fixed-width-region))
  11959. (t (call-interactively 'ffap))))
  11960. (defun org-ctrl-c-ctrl-c (&optional arg)
  11961. "Set tags in headline, or update according to changed information at point.
  11962. This command does many different things, depending on context:
  11963. - If the cursor is in a headline, prompt for tags and insert them
  11964. into the current line, aligned to `org-tags-column'. When called
  11965. with prefix arg, realign all tags in the current buffer.
  11966. - If the cursor is in one of the special #+KEYWORD lines, this
  11967. triggers scanning the buffer for these lines and updating the
  11968. information.
  11969. - If the cursor is inside a table, realign the table. This command
  11970. works even if the automatic table editor has been turned off.
  11971. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11972. the entire table.
  11973. - If the cursor is at a footnote reference or definition, jump to
  11974. the corresponding definition or references, respectively.
  11975. - If the cursor is a the beginning of a dynamic block, update it.
  11976. - If the cursor is inside a table created by the table.el package,
  11977. activate that table.
  11978. - If the current buffer is a remember buffer, close note and file
  11979. it. A prefix argument of 1 files to the default location
  11980. without further interaction. A prefix argument of 2 files to
  11981. the currently clocking task.
  11982. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11983. links in this buffer.
  11984. - If the cursor is on a numbered item in a plain list, renumber the
  11985. ordered list.
  11986. - If the cursor is on a checkbox, toggle it."
  11987. (interactive "P")
  11988. (let ((org-enable-table-editor t))
  11989. (cond
  11990. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11991. org-occur-highlights
  11992. org-latex-fragment-image-overlays)
  11993. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11994. (org-remove-occur-highlights)
  11995. (org-remove-latex-fragment-image-overlays)
  11996. (message "Temporary highlights/overlays removed from current buffer"))
  11997. ((and (local-variable-p 'org-finish-function (current-buffer))
  11998. (fboundp org-finish-function))
  11999. (funcall org-finish-function))
  12000. ((org-at-property-p)
  12001. (call-interactively 'org-property-action))
  12002. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  12003. ((org-on-heading-p) (call-interactively 'org-set-tags))
  12004. ((org-at-table.el-p)
  12005. (require 'table)
  12006. (beginning-of-line 1)
  12007. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  12008. (call-interactively 'table-recognize-table))
  12009. ((org-at-table-p)
  12010. (org-table-maybe-eval-formula)
  12011. (if arg
  12012. (call-interactively 'org-table-recalculate)
  12013. (org-table-maybe-recalculate-line))
  12014. (call-interactively 'org-table-align))
  12015. ((or (org-footnote-at-reference-p)
  12016. (org-footnote-at-definition-p))
  12017. (call-interactively 'org-footnote-action))
  12018. ((org-at-item-checkbox-p)
  12019. (call-interactively 'org-toggle-checkbox))
  12020. ((org-at-item-p)
  12021. (call-interactively 'org-maybe-renumber-ordered-list))
  12022. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  12023. ;; Dynamic block
  12024. (beginning-of-line 1)
  12025. (save-excursion (org-update-dblock)))
  12026. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  12027. (cond
  12028. ((equal (match-string 1) "TBLFM")
  12029. ;; Recalculate the table before this line
  12030. (save-excursion
  12031. (beginning-of-line 1)
  12032. (skip-chars-backward " \r\n\t")
  12033. (if (org-at-table-p)
  12034. (org-call-with-arg 'org-table-recalculate t))))
  12035. (t
  12036. ; (org-set-regexps-and-options)
  12037. ; (org-restart-font-lock)
  12038. (let ((org-inhibit-startup t)) (org-mode-restart))
  12039. (message "Local setup has been refreshed"))))
  12040. (t (error "C-c C-c can do nothing useful at this location.")))))
  12041. (defun org-mode-restart ()
  12042. "Restart Org-mode, to scan again for special lines.
  12043. Also updates the keyword regular expressions."
  12044. (interactive)
  12045. (org-mode)
  12046. (message "Org-mode restarted"))
  12047. (defun org-kill-note-or-show-branches ()
  12048. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  12049. (interactive)
  12050. (if (not org-finish-function)
  12051. (call-interactively 'show-branches)
  12052. (let ((org-note-abort t))
  12053. (funcall org-finish-function))))
  12054. (defun org-return (&optional indent)
  12055. "Goto next table row or insert a newline.
  12056. Calls `org-table-next-row' or `newline', depending on context.
  12057. See the individual commands for more information."
  12058. (interactive)
  12059. (cond
  12060. ((bobp) (if indent (newline-and-indent) (newline)))
  12061. ((and (org-at-heading-p)
  12062. (looking-at
  12063. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  12064. (org-show-entry)
  12065. (end-of-line 1)
  12066. (newline))
  12067. ((org-at-table-p)
  12068. (org-table-justify-field-maybe)
  12069. (call-interactively 'org-table-next-row))
  12070. (t (if indent (newline-and-indent) (newline)))))
  12071. (defun org-return-indent ()
  12072. "Goto next table row or insert a newline and indent.
  12073. Calls `org-table-next-row' or `newline-and-indent', depending on
  12074. context. See the individual commands for more information."
  12075. (interactive)
  12076. (org-return t))
  12077. (defun org-ctrl-c-star ()
  12078. "Compute table, or change heading status of lines.
  12079. Calls `org-table-recalculate' or `org-toggle-region-headings',
  12080. depending on context. This will also turn a plain list item or a normal
  12081. line into a subheading."
  12082. (interactive)
  12083. (cond
  12084. ((org-at-table-p)
  12085. (call-interactively 'org-table-recalculate))
  12086. ((org-region-active-p)
  12087. ;; Convert all lines in region to list items
  12088. (call-interactively 'org-toggle-region-headings))
  12089. ((org-on-heading-p)
  12090. (org-toggle-region-headings (point-at-bol)
  12091. (min (1+ (point-at-eol)) (point-max))))
  12092. ((org-at-item-p)
  12093. ;; Convert to heading
  12094. (let ((level (save-match-data
  12095. (save-excursion
  12096. (condition-case nil
  12097. (progn
  12098. (org-back-to-heading t)
  12099. (funcall outline-level))
  12100. (error 0))))))
  12101. (replace-match
  12102. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  12103. (t (org-toggle-region-headings (point-at-bol)
  12104. (min (1+ (point-at-eol)) (point-max))))))
  12105. (defun org-ctrl-c-minus ()
  12106. "Insert separator line in table or modify bullet status of line.
  12107. Also turns a plain line or a region of lines into list items.
  12108. Calls `org-table-insert-hline', `org-toggle-region-items', or
  12109. `org-cycle-list-bullet', depending on context."
  12110. (interactive)
  12111. (cond
  12112. ((org-at-table-p)
  12113. (call-interactively 'org-table-insert-hline))
  12114. ((org-on-heading-p)
  12115. ;; Convert to item
  12116. (save-excursion
  12117. (beginning-of-line 1)
  12118. (if (looking-at "\\*+ ")
  12119. (replace-match
  12120. (concat (make-string
  12121. (- (match-end 0) (point) (if org-odd-levels-only 2 1)) ?\ )
  12122. "- ")))))
  12123. ((org-region-active-p)
  12124. ;; Convert all lines in region to list items
  12125. (call-interactively 'org-toggle-region-items))
  12126. ((org-in-item-p)
  12127. (call-interactively 'org-cycle-list-bullet))
  12128. (t (org-toggle-region-items (point-at-bol)
  12129. (min (1+ (point-at-eol)) (point-max))))))
  12130. (defun org-toggle-region-items (beg end)
  12131. "Convert all lines in region to list items.
  12132. If the first line is already an item, convert all list items in the region
  12133. to normal lines."
  12134. (interactive "r")
  12135. (let (l2 l)
  12136. (save-excursion
  12137. (goto-char end)
  12138. (setq l2 (org-current-line))
  12139. (goto-char beg)
  12140. (beginning-of-line 1)
  12141. (setq l (1- (org-current-line)))
  12142. (if (org-at-item-p)
  12143. ;; We already have items, de-itemize
  12144. (while (< (setq l (1+ l)) l2)
  12145. (when (org-at-item-p)
  12146. (goto-char (match-beginning 2))
  12147. (delete-region (match-beginning 2) (match-end 2))
  12148. (and (looking-at "[ \t]+") (replace-match "")))
  12149. (beginning-of-line 2))
  12150. (while (< (setq l (1+ l)) l2)
  12151. (unless (org-at-item-p)
  12152. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12153. (replace-match "\\1- \\2")))
  12154. (beginning-of-line 2))))))
  12155. (defun org-toggle-region-headings (beg end)
  12156. "Convert all lines in region to list items.
  12157. If the first line is already an item, convert all list items in the region
  12158. to normal lines."
  12159. (interactive "r")
  12160. (let (l2 l)
  12161. (save-excursion
  12162. (goto-char end)
  12163. (setq l2 (org-current-line))
  12164. (goto-char beg)
  12165. (beginning-of-line 1)
  12166. (setq l (1- (org-current-line)))
  12167. (if (org-on-heading-p)
  12168. ;; We already have headlines, de-star them
  12169. (while (< (setq l (1+ l)) l2)
  12170. (when (org-on-heading-p t)
  12171. (and (looking-at outline-regexp) (replace-match "")))
  12172. (beginning-of-line 2))
  12173. (let* ((stars (save-excursion
  12174. (re-search-backward org-complex-heading-regexp nil t)
  12175. (or (match-string 1) "*")))
  12176. (add-stars (if org-odd-levels-only "**" "*"))
  12177. (rpl (concat stars add-stars " \\2")))
  12178. (while (< (setq l (1+ l)) l2)
  12179. (unless (org-on-heading-p)
  12180. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12181. (replace-match rpl)))
  12182. (beginning-of-line 2)))))))
  12183. (defun org-meta-return (&optional arg)
  12184. "Insert a new heading or wrap a region in a table.
  12185. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12186. See the individual commands for more information."
  12187. (interactive "P")
  12188. (cond
  12189. ((org-at-table-p)
  12190. (call-interactively 'org-table-wrap-region))
  12191. (t (call-interactively 'org-insert-heading))))
  12192. ;;; Menu entries
  12193. ;; Define the Org-mode menus
  12194. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12195. '("Tbl"
  12196. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12197. ["Next Field" org-cycle (org-at-table-p)]
  12198. ["Previous Field" org-shifttab (org-at-table-p)]
  12199. ["Next Row" org-return (org-at-table-p)]
  12200. "--"
  12201. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12202. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12203. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12204. "--"
  12205. ("Column"
  12206. ["Move Column Left" org-metaleft (org-at-table-p)]
  12207. ["Move Column Right" org-metaright (org-at-table-p)]
  12208. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12209. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12210. ("Row"
  12211. ["Move Row Up" org-metaup (org-at-table-p)]
  12212. ["Move Row Down" org-metadown (org-at-table-p)]
  12213. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12214. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12215. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12216. "--"
  12217. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12218. ("Rectangle"
  12219. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12220. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12221. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12222. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12223. "--"
  12224. ("Calculate"
  12225. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12226. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12227. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12228. "--"
  12229. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12230. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12231. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12232. "--"
  12233. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12234. "--"
  12235. ["Sum Column/Rectangle" org-table-sum
  12236. (or (org-at-table-p) (org-region-active-p))]
  12237. ["Which Column?" org-table-current-column (org-at-table-p)])
  12238. ["Debug Formulas"
  12239. org-table-toggle-formula-debugger
  12240. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12241. ["Show Col/Row Numbers"
  12242. org-table-toggle-coordinate-overlays
  12243. :style toggle
  12244. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12245. "--"
  12246. ["Create" org-table-create (and (not (org-at-table-p))
  12247. org-enable-table-editor)]
  12248. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12249. ["Import from File" org-table-import (not (org-at-table-p))]
  12250. ["Export to File" org-table-export (org-at-table-p)]
  12251. "--"
  12252. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12253. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12254. '("Org"
  12255. ("Show/Hide"
  12256. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12257. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12258. ["Sparse Tree..." org-sparse-tree t]
  12259. ["Reveal Context" org-reveal t]
  12260. ["Show All" show-all t]
  12261. "--"
  12262. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12263. "--"
  12264. ["New Heading" org-insert-heading t]
  12265. ("Navigate Headings"
  12266. ["Up" outline-up-heading t]
  12267. ["Next" outline-next-visible-heading t]
  12268. ["Previous" outline-previous-visible-heading t]
  12269. ["Next Same Level" outline-forward-same-level t]
  12270. ["Previous Same Level" outline-backward-same-level t]
  12271. "--"
  12272. ["Jump" org-goto t])
  12273. ("Edit Structure"
  12274. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12275. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12276. "--"
  12277. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12278. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12279. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12280. "--"
  12281. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12282. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12283. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12284. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12285. "--"
  12286. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12287. "--"
  12288. ["Convert to odd levels" org-convert-to-odd-levels t]
  12289. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12290. ("Editing"
  12291. ["Emphasis..." org-emphasize t]
  12292. ["Edit Source Example" org-edit-special t]
  12293. "--"
  12294. ["Footnote new/jump" org-footnote-action t]
  12295. ["Footnote extra" (org-footnote-action t) :active t :keys "C-u C-c C-x f"])
  12296. ("Archive"
  12297. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12298. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12299. ; :active t :keys "C-u C-c C-x C-a"]
  12300. ["Sparse trees open ARCHIVE trees"
  12301. (setq org-sparse-tree-open-archived-trees
  12302. (not org-sparse-tree-open-archived-trees))
  12303. :style toggle :selected org-sparse-tree-open-archived-trees]
  12304. ["Cycling opens ARCHIVE trees"
  12305. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12306. :style toggle :selected org-cycle-open-archived-trees]
  12307. "--"
  12308. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12309. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12310. ; ["Check and Move Children" (org-archive-subtree '(4))
  12311. ; :active t :keys "C-u C-c C-x C-s"]
  12312. )
  12313. "--"
  12314. ("TODO Lists"
  12315. ["TODO/DONE/-" org-todo t]
  12316. ("Select keyword"
  12317. ["Next keyword" org-shiftright (org-on-heading-p)]
  12318. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12319. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12320. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12321. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12322. ["Show TODO Tree" org-show-todo-tree t]
  12323. ["Global TODO list" org-todo-list t]
  12324. "--"
  12325. ["Set Priority" org-priority t]
  12326. ["Priority Up" org-shiftup t]
  12327. ["Priority Down" org-shiftdown t])
  12328. ("TAGS and Properties"
  12329. ["Set Tags" org-set-tags-command t]
  12330. ["Change tag in region" org-change-tag-in-region (org-region-active-p)]
  12331. "--"
  12332. ["Set property" org-set-property t]
  12333. ["Column view of properties" org-columns t]
  12334. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12335. ("Dates and Scheduling"
  12336. ["Timestamp" org-time-stamp t]
  12337. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12338. ("Change Date"
  12339. ["1 Day Later" org-shiftright t]
  12340. ["1 Day Earlier" org-shiftleft t]
  12341. ["1 ... Later" org-shiftup t]
  12342. ["1 ... Earlier" org-shiftdown t])
  12343. ["Compute Time Range" org-evaluate-time-range t]
  12344. ["Schedule Item" org-schedule t]
  12345. ["Deadline" org-deadline t]
  12346. "--"
  12347. ["Custom time format" org-toggle-time-stamp-overlays
  12348. :style radio :selected org-display-custom-times]
  12349. "--"
  12350. ["Goto Calendar" org-goto-calendar t]
  12351. ["Date from Calendar" org-date-from-calendar t]
  12352. "--"
  12353. ["Start/restart timer" org-timer-start t]
  12354. ["Insert timer string" org-timer t]
  12355. ["Insert timer item" org-timer-item t])
  12356. ("Logging work"
  12357. ["Clock in" org-clock-in t]
  12358. ["Clock out" org-clock-out t]
  12359. ["Clock cancel" org-clock-cancel t]
  12360. ["Goto running clock" org-clock-goto t]
  12361. ["Display times" org-clock-display t]
  12362. ["Create clock table" org-clock-report t]
  12363. "--"
  12364. ["Record DONE time"
  12365. (progn (setq org-log-done (not org-log-done))
  12366. (message "Switching to %s will %s record a timestamp"
  12367. (car org-done-keywords)
  12368. (if org-log-done "automatically" "not")))
  12369. :style toggle :selected org-log-done])
  12370. "--"
  12371. ["Agenda Command..." org-agenda t]
  12372. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12373. ("File List for Agenda")
  12374. ("Special views current file"
  12375. ["TODO Tree" org-show-todo-tree t]
  12376. ["Check Deadlines" org-check-deadlines t]
  12377. ["Timeline" org-timeline t]
  12378. ["Tags Tree" org-tags-sparse-tree t])
  12379. "--"
  12380. ("Hyperlinks"
  12381. ["Store Link (Global)" org-store-link t]
  12382. ["Insert Link" org-insert-link t]
  12383. ["Follow Link" org-open-at-point t]
  12384. "--"
  12385. ["Next link" org-next-link t]
  12386. ["Previous link" org-previous-link t]
  12387. "--"
  12388. ["Descriptive Links"
  12389. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12390. :style radio
  12391. :selected (member '(org-link) buffer-invisibility-spec)]
  12392. ["Literal Links"
  12393. (progn
  12394. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12395. :style radio
  12396. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12397. "--"
  12398. ["Export/Publish..." org-export t]
  12399. ("LaTeX"
  12400. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12401. :selected org-cdlatex-mode]
  12402. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12403. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12404. ["Modify math symbol" org-cdlatex-math-modify
  12405. (org-inside-LaTeX-fragment-p)]
  12406. ["Export LaTeX fragments as images"
  12407. (if (featurep 'org-exp)
  12408. (setq org-export-with-LaTeX-fragments
  12409. (not org-export-with-LaTeX-fragments))
  12410. (require 'org-exp))
  12411. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12412. org-export-with-LaTeX-fragments)])
  12413. "--"
  12414. ("Documentation"
  12415. ["Show Version" org-version t]
  12416. ["Info Documentation" org-info t])
  12417. ("Customize"
  12418. ["Browse Org Group" org-customize t]
  12419. "--"
  12420. ["Expand This Menu" org-create-customize-menu
  12421. (fboundp 'customize-menu-create)])
  12422. "--"
  12423. ["Refresh setup" org-mode-restart t]
  12424. ))
  12425. (defun org-info (&optional node)
  12426. "Read documentation for Org-mode in the info system.
  12427. With optional NODE, go directly to that node."
  12428. (interactive)
  12429. (info (format "(org)%s" (or node ""))))
  12430. (defun org-install-agenda-files-menu ()
  12431. (let ((bl (buffer-list)))
  12432. (save-excursion
  12433. (while bl
  12434. (set-buffer (pop bl))
  12435. (if (org-mode-p) (setq bl nil)))
  12436. (when (org-mode-p)
  12437. (easy-menu-change
  12438. '("Org") "File List for Agenda"
  12439. (append
  12440. (list
  12441. ["Edit File List" (org-edit-agenda-file-list) t]
  12442. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12443. ["Remove Current File from List" org-remove-file t]
  12444. ["Cycle through agenda files" org-cycle-agenda-files t]
  12445. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12446. "--")
  12447. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12448. ;;;; Documentation
  12449. ;;;###autoload
  12450. (defun org-require-autoloaded-modules ()
  12451. (interactive)
  12452. (mapc 'require
  12453. '(org-agenda org-archive org-clock org-colview
  12454. org-exp org-id org-export-latex org-publish
  12455. org-remember org-table)))
  12456. ;;;###autoload
  12457. (defun org-customize ()
  12458. "Call the customize function with org as argument."
  12459. (interactive)
  12460. (org-load-modules-maybe)
  12461. (org-require-autoloaded-modules)
  12462. (customize-browse 'org))
  12463. (defun org-create-customize-menu ()
  12464. "Create a full customization menu for Org-mode, insert it into the menu."
  12465. (interactive)
  12466. (org-load-modules-maybe)
  12467. (org-require-autoloaded-modules)
  12468. (if (fboundp 'customize-menu-create)
  12469. (progn
  12470. (easy-menu-change
  12471. '("Org") "Customize"
  12472. `(["Browse Org group" org-customize t]
  12473. "--"
  12474. ,(customize-menu-create 'org)
  12475. ["Set" Custom-set t]
  12476. ["Save" Custom-save t]
  12477. ["Reset to Current" Custom-reset-current t]
  12478. ["Reset to Saved" Custom-reset-saved t]
  12479. ["Reset to Standard Settings" Custom-reset-standard t]))
  12480. (message "\"Org\"-menu now contains full customization menu"))
  12481. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12482. ;;;; Miscellaneous stuff
  12483. ;;; Generally useful functions
  12484. (defun org-find-text-property-in-string (prop s)
  12485. "Return the first non-nil value of property PROP in string S."
  12486. (or (get-text-property 0 prop s)
  12487. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12488. prop s)))
  12489. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12490. "Display the given MESSAGE as a warning."
  12491. (if (fboundp 'display-warning)
  12492. (display-warning 'org message
  12493. (if (featurep 'xemacs)
  12494. 'warning
  12495. :warning))
  12496. (let ((buf (get-buffer-create "*Org warnings*")))
  12497. (with-current-buffer buf
  12498. (goto-char (point-max))
  12499. (insert "Warning (Org): " message)
  12500. (unless (bolp)
  12501. (newline)))
  12502. (display-buffer buf)
  12503. (sit-for 0))))
  12504. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12505. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12506. (if (and marker (marker-buffer marker)
  12507. (buffer-live-p (marker-buffer marker)))
  12508. (progn
  12509. (switch-to-buffer (marker-buffer marker))
  12510. (if (or (> marker (point-max)) (< marker (point-min)))
  12511. (widen))
  12512. (goto-char marker))
  12513. (if bookmark
  12514. (bookmark-jump bookmark)
  12515. (error "Cannot find location"))))
  12516. (defun org-quote-csv-field (s)
  12517. "Quote field for inclusion in CSV material."
  12518. (if (string-match "[\",]" s)
  12519. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12520. s))
  12521. (defun org-plist-delete (plist property)
  12522. "Delete PROPERTY from PLIST.
  12523. This is in contrast to merely setting it to 0."
  12524. (let (p)
  12525. (while plist
  12526. (if (not (eq property (car plist)))
  12527. (setq p (plist-put p (car plist) (nth 1 plist))))
  12528. (setq plist (cddr plist)))
  12529. p))
  12530. (defun org-force-self-insert (N)
  12531. "Needed to enforce self-insert under remapping."
  12532. (interactive "p")
  12533. (self-insert-command N))
  12534. (defun org-string-width (s)
  12535. "Compute width of string, ignoring invisible characters.
  12536. This ignores character with invisibility property `org-link', and also
  12537. characters with property `org-cwidth', because these will become invisible
  12538. upon the next fontification round."
  12539. (let (b l)
  12540. (when (or (eq t buffer-invisibility-spec)
  12541. (assq 'org-link buffer-invisibility-spec))
  12542. (while (setq b (text-property-any 0 (length s)
  12543. 'invisible 'org-link s))
  12544. (setq s (concat (substring s 0 b)
  12545. (substring s (or (next-single-property-change
  12546. b 'invisible s) (length s)))))))
  12547. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12548. (setq s (concat (substring s 0 b)
  12549. (substring s (or (next-single-property-change
  12550. b 'org-cwidth s) (length s))))))
  12551. (setq l (string-width s) b -1)
  12552. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12553. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12554. l))
  12555. (defun org-get-indentation (&optional line)
  12556. "Get the indentation of the current line, interpreting tabs.
  12557. When LINE is given, assume it represents a line and compute its indentation."
  12558. (if line
  12559. (if (string-match "^ *" (org-remove-tabs line))
  12560. (match-end 0))
  12561. (save-excursion
  12562. (beginning-of-line 1)
  12563. (skip-chars-forward " \t")
  12564. (current-column))))
  12565. (defun org-remove-tabs (s &optional width)
  12566. "Replace tabulators in S with spaces.
  12567. Assumes that s is a single line, starting in column 0."
  12568. (setq width (or width tab-width))
  12569. (while (string-match "\t" s)
  12570. (setq s (replace-match
  12571. (make-string
  12572. (- (* width (/ (+ (match-beginning 0) width) width))
  12573. (match-beginning 0)) ?\ )
  12574. t t s)))
  12575. s)
  12576. (defun org-fix-indentation (line ind)
  12577. "Fix indentation in LINE.
  12578. IND is a cons cell with target and minimum indentation.
  12579. If the current indentation in LINE is smaller than the minimum,
  12580. leave it alone. If it is larger than ind, set it to the target."
  12581. (let* ((l (org-remove-tabs line))
  12582. (i (org-get-indentation l))
  12583. (i1 (car ind)) (i2 (cdr ind)))
  12584. (if (>= i i2) (setq l (substring line i2)))
  12585. (if (> i1 0)
  12586. (concat (make-string i1 ?\ ) l)
  12587. l)))
  12588. (defun org-base-buffer (buffer)
  12589. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12590. (if (not buffer)
  12591. buffer
  12592. (or (buffer-base-buffer buffer)
  12593. buffer)))
  12594. (defun org-trim (s)
  12595. "Remove whitespace at beginning and end of string."
  12596. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12597. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12598. s)
  12599. (defun org-wrap (string &optional width lines)
  12600. "Wrap string to either a number of lines, or a width in characters.
  12601. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12602. that costs. If there is a word longer than WIDTH, the text is actually
  12603. wrapped to the length of that word.
  12604. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12605. many lines, whatever width that takes.
  12606. The return value is a list of lines, without newlines at the end."
  12607. (let* ((words (org-split-string string "[ \t\n]+"))
  12608. (maxword (apply 'max (mapcar 'org-string-width words)))
  12609. w ll)
  12610. (cond (width
  12611. (org-do-wrap words (max maxword width)))
  12612. (lines
  12613. (setq w maxword)
  12614. (setq ll (org-do-wrap words maxword))
  12615. (if (<= (length ll) lines)
  12616. ll
  12617. (setq ll words)
  12618. (while (> (length ll) lines)
  12619. (setq w (1+ w))
  12620. (setq ll (org-do-wrap words w)))
  12621. ll))
  12622. (t (error "Cannot wrap this")))))
  12623. (defun org-do-wrap (words width)
  12624. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12625. (let (lines line)
  12626. (while words
  12627. (setq line (pop words))
  12628. (while (and words (< (+ (length line) (length (car words))) width))
  12629. (setq line (concat line " " (pop words))))
  12630. (setq lines (push line lines)))
  12631. (nreverse lines)))
  12632. (defun org-split-string (string &optional separators)
  12633. "Splits STRING into substrings at SEPARATORS.
  12634. No empty strings are returned if there are matches at the beginning
  12635. and end of string."
  12636. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12637. (start 0)
  12638. notfirst
  12639. (list nil))
  12640. (while (and (string-match rexp string
  12641. (if (and notfirst
  12642. (= start (match-beginning 0))
  12643. (< start (length string)))
  12644. (1+ start) start))
  12645. (< (match-beginning 0) (length string)))
  12646. (setq notfirst t)
  12647. (or (eq (match-beginning 0) 0)
  12648. (and (eq (match-beginning 0) (match-end 0))
  12649. (eq (match-beginning 0) start))
  12650. (setq list
  12651. (cons (substring string start (match-beginning 0))
  12652. list)))
  12653. (setq start (match-end 0)))
  12654. (or (eq start (length string))
  12655. (setq list
  12656. (cons (substring string start)
  12657. list)))
  12658. (nreverse list)))
  12659. (defun org-context ()
  12660. "Return a list of contexts of the current cursor position.
  12661. If several contexts apply, all are returned.
  12662. Each context entry is a list with a symbol naming the context, and
  12663. two positions indicating start and end of the context. Possible
  12664. contexts are:
  12665. :headline anywhere in a headline
  12666. :headline-stars on the leading stars in a headline
  12667. :todo-keyword on a TODO keyword (including DONE) in a headline
  12668. :tags on the TAGS in a headline
  12669. :priority on the priority cookie in a headline
  12670. :item on the first line of a plain list item
  12671. :item-bullet on the bullet/number of a plain list item
  12672. :checkbox on the checkbox in a plain list item
  12673. :table in an org-mode table
  12674. :table-special on a special filed in a table
  12675. :table-table in a table.el table
  12676. :link on a hyperlink
  12677. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12678. :target on a <<target>>
  12679. :radio-target on a <<<radio-target>>>
  12680. :latex-fragment on a LaTeX fragment
  12681. :latex-preview on a LaTeX fragment with overlayed preview image
  12682. This function expects the position to be visible because it uses font-lock
  12683. faces as a help to recognize the following contexts: :table-special, :link,
  12684. and :keyword."
  12685. (let* ((f (get-text-property (point) 'face))
  12686. (faces (if (listp f) f (list f)))
  12687. (p (point)) clist o)
  12688. ;; First the large context
  12689. (cond
  12690. ((org-on-heading-p t)
  12691. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12692. (when (progn
  12693. (beginning-of-line 1)
  12694. (looking-at org-todo-line-tags-regexp))
  12695. (push (org-point-in-group p 1 :headline-stars) clist)
  12696. (push (org-point-in-group p 2 :todo-keyword) clist)
  12697. (push (org-point-in-group p 4 :tags) clist))
  12698. (goto-char p)
  12699. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12700. (if (looking-at "\\[#[A-Z0-9]\\]")
  12701. (push (org-point-in-group p 0 :priority) clist)))
  12702. ((org-at-item-p)
  12703. (push (org-point-in-group p 2 :item-bullet) clist)
  12704. (push (list :item (point-at-bol)
  12705. (save-excursion (org-end-of-item) (point)))
  12706. clist)
  12707. (and (org-at-item-checkbox-p)
  12708. (push (org-point-in-group p 0 :checkbox) clist)))
  12709. ((org-at-table-p)
  12710. (push (list :table (org-table-begin) (org-table-end)) clist)
  12711. (if (memq 'org-formula faces)
  12712. (push (list :table-special
  12713. (previous-single-property-change p 'face)
  12714. (next-single-property-change p 'face)) clist)))
  12715. ((org-at-table-p 'any)
  12716. (push (list :table-table) clist)))
  12717. (goto-char p)
  12718. ;; Now the small context
  12719. (cond
  12720. ((org-at-timestamp-p)
  12721. (push (org-point-in-group p 0 :timestamp) clist))
  12722. ((memq 'org-link faces)
  12723. (push (list :link
  12724. (previous-single-property-change p 'face)
  12725. (next-single-property-change p 'face)) clist))
  12726. ((memq 'org-special-keyword faces)
  12727. (push (list :keyword
  12728. (previous-single-property-change p 'face)
  12729. (next-single-property-change p 'face)) clist))
  12730. ((org-on-target-p)
  12731. (push (org-point-in-group p 0 :target) clist)
  12732. (goto-char (1- (match-beginning 0)))
  12733. (if (looking-at org-radio-target-regexp)
  12734. (push (org-point-in-group p 0 :radio-target) clist))
  12735. (goto-char p))
  12736. ((setq o (car (delq nil
  12737. (mapcar
  12738. (lambda (x)
  12739. (if (memq x org-latex-fragment-image-overlays) x))
  12740. (org-overlays-at (point))))))
  12741. (push (list :latex-fragment
  12742. (org-overlay-start o) (org-overlay-end o)) clist)
  12743. (push (list :latex-preview
  12744. (org-overlay-start o) (org-overlay-end o)) clist))
  12745. ((org-inside-LaTeX-fragment-p)
  12746. ;; FIXME: positions wrong.
  12747. (push (list :latex-fragment (point) (point)) clist)))
  12748. (setq clist (nreverse (delq nil clist)))
  12749. clist))
  12750. ;; FIXME: Compare with at-regexp-p Do we need both?
  12751. (defun org-in-regexp (re &optional nlines visually)
  12752. "Check if point is inside a match of regexp.
  12753. Normally only the current line is checked, but you can include NLINES extra
  12754. lines both before and after point into the search.
  12755. If VISUALLY is set, require that the cursor is not after the match but
  12756. really on, so that the block visually is on the match."
  12757. (catch 'exit
  12758. (let ((pos (point))
  12759. (eol (point-at-eol (+ 1 (or nlines 0))))
  12760. (inc (if visually 1 0)))
  12761. (save-excursion
  12762. (beginning-of-line (- 1 (or nlines 0)))
  12763. (while (re-search-forward re eol t)
  12764. (if (and (<= (match-beginning 0) pos)
  12765. (>= (+ inc (match-end 0)) pos))
  12766. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12767. (defun org-at-regexp-p (regexp)
  12768. "Is point inside a match of REGEXP in the current line?"
  12769. (catch 'exit
  12770. (save-excursion
  12771. (let ((pos (point)) (end (point-at-eol)))
  12772. (beginning-of-line 1)
  12773. (while (re-search-forward regexp end t)
  12774. (if (and (<= (match-beginning 0) pos)
  12775. (>= (match-end 0) pos))
  12776. (throw 'exit t)))
  12777. nil))))
  12778. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12779. "Call `multi-occur' with buffers for all agenda files."
  12780. (interactive "sOrg-files matching: \np")
  12781. (let* ((files (org-agenda-files))
  12782. (tnames (mapcar 'file-truename files))
  12783. (extra org-agenda-text-search-extra-files)
  12784. f)
  12785. (when (eq (car extra) 'agenda-archives)
  12786. (setq extra (cdr extra))
  12787. (setq files (org-add-archive-files files)))
  12788. (while (setq f (pop extra))
  12789. (unless (member (file-truename f) tnames)
  12790. (add-to-list 'files f 'append)
  12791. (add-to-list 'tnames (file-truename f) 'append)))
  12792. (multi-occur
  12793. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12794. regexp)))
  12795. (if (boundp 'occur-mode-find-occurrence-hook)
  12796. ;; Emacs 23
  12797. (add-hook 'occur-mode-find-occurrence-hook
  12798. (lambda ()
  12799. (when (org-mode-p)
  12800. (org-reveal))))
  12801. ;; Emacs 22
  12802. (defadvice occur-mode-goto-occurrence
  12803. (after org-occur-reveal activate)
  12804. (and (org-mode-p) (org-reveal)))
  12805. (defadvice occur-mode-goto-occurrence-other-window
  12806. (after org-occur-reveal activate)
  12807. (and (org-mode-p) (org-reveal)))
  12808. (defadvice occur-mode-display-occurrence
  12809. (after org-occur-reveal activate)
  12810. (when (org-mode-p)
  12811. (let ((pos (occur-mode-find-occurrence)))
  12812. (with-current-buffer (marker-buffer pos)
  12813. (save-excursion
  12814. (goto-char pos)
  12815. (org-reveal)))))))
  12816. (defun org-uniquify (list)
  12817. "Remove duplicate elements from LIST."
  12818. (let (res)
  12819. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12820. res))
  12821. (defun org-delete-all (elts list)
  12822. "Remove all elements in ELTS from LIST."
  12823. (while elts
  12824. (setq list (delete (pop elts) list)))
  12825. list)
  12826. (defun org-back-over-empty-lines ()
  12827. "Move backwards over whitespace, to the beginning of the first empty line.
  12828. Returns the number of empty lines passed."
  12829. (let ((pos (point)))
  12830. (skip-chars-backward " \t\n\r")
  12831. (beginning-of-line 2)
  12832. (goto-char (min (point) pos))
  12833. (count-lines (point) pos)))
  12834. (defun org-skip-whitespace ()
  12835. (skip-chars-forward " \t\n\r"))
  12836. (defun org-point-in-group (point group &optional context)
  12837. "Check if POINT is in match-group GROUP.
  12838. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12839. match. If the match group does ot exist or point is not inside it,
  12840. return nil."
  12841. (and (match-beginning group)
  12842. (>= point (match-beginning group))
  12843. (<= point (match-end group))
  12844. (if context
  12845. (list context (match-beginning group) (match-end group))
  12846. t)))
  12847. (defun org-switch-to-buffer-other-window (&rest args)
  12848. "Switch to buffer in a second window on the current frame.
  12849. In particular, do not allow pop-up frames."
  12850. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12851. special-display-function)
  12852. (apply 'switch-to-buffer-other-window args)))
  12853. (defun org-combine-plists (&rest plists)
  12854. "Create a single property list from all plists in PLISTS.
  12855. The process starts by copying the first list, and then setting properties
  12856. from the other lists. Settings in the last list are the most significant
  12857. ones and overrule settings in the other lists."
  12858. (let ((rtn (copy-sequence (pop plists)))
  12859. p v ls)
  12860. (while plists
  12861. (setq ls (pop plists))
  12862. (while ls
  12863. (setq p (pop ls) v (pop ls))
  12864. (setq rtn (plist-put rtn p v))))
  12865. rtn))
  12866. (defun org-move-line-down (arg)
  12867. "Move the current line down. With prefix argument, move it past ARG lines."
  12868. (interactive "p")
  12869. (let ((col (current-column))
  12870. beg end pos)
  12871. (beginning-of-line 1) (setq beg (point))
  12872. (beginning-of-line 2) (setq end (point))
  12873. (beginning-of-line (+ 1 arg))
  12874. (setq pos (move-marker (make-marker) (point)))
  12875. (insert (delete-and-extract-region beg end))
  12876. (goto-char pos)
  12877. (org-move-to-column col)))
  12878. (defun org-move-line-up (arg)
  12879. "Move the current line up. With prefix argument, move it past ARG lines."
  12880. (interactive "p")
  12881. (let ((col (current-column))
  12882. beg end pos)
  12883. (beginning-of-line 1) (setq beg (point))
  12884. (beginning-of-line 2) (setq end (point))
  12885. (beginning-of-line (- arg))
  12886. (setq pos (move-marker (make-marker) (point)))
  12887. (insert (delete-and-extract-region beg end))
  12888. (goto-char pos)
  12889. (org-move-to-column col)))
  12890. (defun org-replace-escapes (string table)
  12891. "Replace %-escapes in STRING with values in TABLE.
  12892. TABLE is an association list with keys like \"%a\" and string values.
  12893. The sequences in STRING may contain normal field width and padding information,
  12894. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12895. so values can contain further %-escapes if they are define later in TABLE."
  12896. (let ((case-fold-search nil)
  12897. e re rpl)
  12898. (while (setq e (pop table))
  12899. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12900. (while (string-match re string)
  12901. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12902. (cdr e)))
  12903. (setq string (replace-match rpl t t string))))
  12904. string))
  12905. (defun org-sublist (list start end)
  12906. "Return a section of LIST, from START to END.
  12907. Counting starts at 1."
  12908. (let (rtn (c start))
  12909. (setq list (nthcdr (1- start) list))
  12910. (while (and list (<= c end))
  12911. (push (pop list) rtn)
  12912. (setq c (1+ c)))
  12913. (nreverse rtn)))
  12914. (defun org-find-base-buffer-visiting (file)
  12915. "Like `find-buffer-visiting' but alway return the base buffer and
  12916. not an indirect buffer."
  12917. (let ((buf (find-buffer-visiting file)))
  12918. (if buf
  12919. (or (buffer-base-buffer buf) buf)
  12920. nil)))
  12921. (defun org-image-file-name-regexp ()
  12922. "Return regexp matching the file names of images."
  12923. (if (fboundp 'image-file-name-regexp)
  12924. (image-file-name-regexp)
  12925. (let ((image-file-name-extensions
  12926. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12927. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12928. (concat "\\."
  12929. (regexp-opt (nconc (mapcar 'upcase
  12930. image-file-name-extensions)
  12931. image-file-name-extensions)
  12932. t)
  12933. "\\'"))))
  12934. (defun org-file-image-p (file)
  12935. "Return non-nil if FILE is an image."
  12936. (save-match-data
  12937. (string-match (org-image-file-name-regexp) file)))
  12938. (defun org-get-cursor-date ()
  12939. "Return the date at cursor in as a time.
  12940. This works in the calendar and in the agenda, anywhere else it just
  12941. returns the current time."
  12942. (let (date day defd)
  12943. (cond
  12944. ((eq major-mode 'calendar-mode)
  12945. (setq date (calendar-cursor-to-date)
  12946. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12947. ((eq major-mode 'org-agenda-mode)
  12948. (setq day (get-text-property (point) 'day))
  12949. (if day
  12950. (setq date (calendar-gregorian-from-absolute day)
  12951. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12952. (nth 2 date))))))
  12953. (or defd (current-time))))
  12954. (defvar org-agenda-action-marker (make-marker)
  12955. "Marker pointing to the entry for the next agenda action.")
  12956. (defun org-mark-entry-for-agenda-action ()
  12957. "Mark the current entry as target of an agenda action.
  12958. Agenda actions are actions executed from the agenda with the key `k',
  12959. which make use of the date at the cursor."
  12960. (interactive)
  12961. (move-marker org-agenda-action-marker
  12962. (save-excursion (org-back-to-heading t) (point))
  12963. (current-buffer))
  12964. (message
  12965. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12966. ;;; Paragraph filling stuff.
  12967. ;; We want this to be just right, so use the full arsenal.
  12968. (defun org-indent-line-function ()
  12969. "Indent line like previous, but further if previous was headline or item."
  12970. (interactive)
  12971. (let* ((pos (point))
  12972. (itemp (org-at-item-p))
  12973. column bpos bcol tpos tcol bullet btype bullet-type)
  12974. ;; Find the previous relevant line
  12975. (beginning-of-line 1)
  12976. (cond
  12977. ((looking-at "#") (setq column 0))
  12978. ((looking-at "\\*+ ") (setq column 0))
  12979. (t
  12980. (beginning-of-line 0)
  12981. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12982. (beginning-of-line 0))
  12983. (cond
  12984. ((looking-at "\\*+[ \t]+")
  12985. (if (not org-adapt-indentation)
  12986. (setq column 0)
  12987. (goto-char (match-end 0))
  12988. (setq column (current-column))))
  12989. ((org-in-item-p)
  12990. (org-beginning-of-item)
  12991. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12992. (setq bpos (match-beginning 1) tpos (match-end 0)
  12993. bcol (progn (goto-char bpos) (current-column))
  12994. tcol (progn (goto-char tpos) (current-column))
  12995. bullet (match-string 1)
  12996. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12997. (if (> tcol (+ bcol org-description-max-indent))
  12998. (setq tcol (+ bcol 5)))
  12999. (if (not itemp)
  13000. (setq column tcol)
  13001. (goto-char pos)
  13002. (beginning-of-line 1)
  13003. (if (looking-at "\\S-")
  13004. (progn
  13005. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  13006. (setq bullet (match-string 1)
  13007. btype (if (string-match "[0-9]" bullet) "n" bullet))
  13008. (setq column (if (equal btype bullet-type) bcol tcol)))
  13009. (setq column (org-get-indentation)))))
  13010. (t (setq column (org-get-indentation))))))
  13011. (goto-char pos)
  13012. (if (<= (current-column) (current-indentation))
  13013. (org-indent-line-to column)
  13014. (save-excursion (org-indent-line-to column)))
  13015. (setq column (current-column))
  13016. (beginning-of-line 1)
  13017. (if (looking-at
  13018. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  13019. (replace-match (concat "\\1" (format org-property-format
  13020. (match-string 2) (match-string 3)))
  13021. t nil))
  13022. (org-move-to-column column)))
  13023. (defun org-set-autofill-regexps ()
  13024. (interactive)
  13025. ;; In the paragraph separator we include headlines, because filling
  13026. ;; text in a line directly attached to a headline would otherwise
  13027. ;; fill the headline as well.
  13028. (org-set-local 'comment-start-skip "^#+[ \t]*")
  13029. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  13030. ;; The paragraph starter includes hand-formatted lists.
  13031. (org-set-local 'paragraph-start
  13032. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13033. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13034. ;; But only if the user has not turned off tables or fixed-width regions
  13035. (org-set-local
  13036. 'auto-fill-inhibit-regexp
  13037. (concat "\\*+ \\|#\\+"
  13038. "\\|[ \t]*" org-keyword-time-regexp
  13039. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13040. (concat
  13041. "\\|[ \t]*["
  13042. (if org-enable-table-editor "|" "")
  13043. (if org-enable-fixed-width-editor ":" "")
  13044. "]"))))
  13045. ;; We use our own fill-paragraph function, to make sure that tables
  13046. ;; and fixed-width regions are not wrapped. That function will pass
  13047. ;; through to `fill-paragraph' when appropriate.
  13048. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  13049. ; Adaptive filling: To get full control, first make sure that
  13050. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13051. (org-set-local 'adaptive-fill-regexp "\000")
  13052. (org-set-local 'adaptive-fill-function
  13053. 'org-adaptive-fill-function)
  13054. (org-set-local
  13055. 'align-mode-rules-list
  13056. '((org-in-buffer-settings
  13057. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  13058. (modes . '(org-mode))))))
  13059. (defun org-fill-paragraph (&optional justify)
  13060. "Re-align a table, pass through to fill-paragraph if no table."
  13061. (let ((table-p (org-at-table-p))
  13062. (table.el-p (org-at-table.el-p)))
  13063. (cond ((and (equal (char-after (point-at-bol)) ?*)
  13064. (save-excursion (goto-char (point-at-bol))
  13065. (looking-at outline-regexp)))
  13066. t) ; skip headlines
  13067. (table.el-p t) ; skip table.el tables
  13068. (table-p (org-table-align) t) ; align org-mode tables
  13069. (t nil)))) ; call paragraph-fill
  13070. ;; For reference, this is the default value of adaptive-fill-regexp
  13071. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13072. (defun org-adaptive-fill-function ()
  13073. "Return a fill prefix for org-mode files.
  13074. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13075. work correctly."
  13076. (cond ((looking-at "#[ \t]+")
  13077. (match-string 0))
  13078. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  13079. (save-excursion
  13080. (if (> (match-end 1) (+ (match-beginning 1)
  13081. org-description-max-indent))
  13082. (goto-char (+ (match-beginning 1) 5))
  13083. (goto-char (match-end 0)))
  13084. (make-string (current-column) ?\ )))
  13085. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  13086. (save-excursion
  13087. (goto-char (match-end 0))
  13088. (make-string (current-column) ?\ )))
  13089. (t nil)))
  13090. ;;; Other stuff.
  13091. (defun org-toggle-fixed-width-section (arg)
  13092. "Toggle the fixed-width export.
  13093. If there is no active region, the QUOTE keyword at the current headline is
  13094. inserted or removed. When present, it causes the text between this headline
  13095. and the next to be exported as fixed-width text, and unmodified.
  13096. If there is an active region, this command adds or removes a colon as the
  13097. first character of this line. If the first character of a line is a colon,
  13098. this line is also exported in fixed-width font."
  13099. (interactive "P")
  13100. (let* ((cc 0)
  13101. (regionp (org-region-active-p))
  13102. (beg (if regionp (region-beginning) (point)))
  13103. (end (if regionp (region-end)))
  13104. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  13105. (case-fold-search nil)
  13106. (re "[ \t]*\\(:\\)")
  13107. off)
  13108. (if regionp
  13109. (save-excursion
  13110. (goto-char beg)
  13111. (setq cc (current-column))
  13112. (beginning-of-line 1)
  13113. (setq off (looking-at re))
  13114. (while (> nlines 0)
  13115. (setq nlines (1- nlines))
  13116. (beginning-of-line 1)
  13117. (cond
  13118. (arg
  13119. (org-move-to-column cc t)
  13120. (insert ":\n")
  13121. (forward-line -1))
  13122. ((and off (looking-at re))
  13123. (replace-match "" t t nil 1))
  13124. ((not off) (org-move-to-column cc t) (insert ":")))
  13125. (forward-line 1)))
  13126. (save-excursion
  13127. (org-back-to-heading)
  13128. (if (looking-at (concat outline-regexp
  13129. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  13130. (replace-match "" t t nil 1)
  13131. (if (looking-at outline-regexp)
  13132. (progn
  13133. (goto-char (match-end 0))
  13134. (insert org-quote-string " "))))))))
  13135. ;;;; Functions extending outline functionality
  13136. (defun org-beginning-of-line (&optional arg)
  13137. "Go to the beginning of the current line. If that is invisible, continue
  13138. to a visible line beginning. This makes the function of C-a more intuitive.
  13139. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13140. first attempt, and only move to after the tags when the cursor is already
  13141. beyond the end of the headline."
  13142. (interactive "P")
  13143. (let ((pos (point)) refpos)
  13144. (beginning-of-line 1)
  13145. (if (bobp)
  13146. nil
  13147. (backward-char 1)
  13148. (if (org-invisible-p)
  13149. (while (and (not (bobp)) (org-invisible-p))
  13150. (backward-char 1)
  13151. (beginning-of-line 1))
  13152. (forward-char 1)))
  13153. (when org-special-ctrl-a/e
  13154. (cond
  13155. ((and (looking-at org-complex-heading-regexp)
  13156. (= (char-after (match-end 1)) ?\ ))
  13157. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13158. (point-at-eol)))
  13159. (goto-char
  13160. (if (eq org-special-ctrl-a/e t)
  13161. (cond ((> pos refpos) refpos)
  13162. ((= pos (point)) refpos)
  13163. (t (point)))
  13164. (cond ((> pos (point)) (point))
  13165. ((not (eq last-command this-command)) (point))
  13166. (t refpos)))))
  13167. ((org-at-item-p)
  13168. (goto-char
  13169. (if (eq org-special-ctrl-a/e t)
  13170. (cond ((> pos (match-end 4)) (match-end 4))
  13171. ((= pos (point)) (match-end 4))
  13172. (t (point)))
  13173. (cond ((> pos (point)) (point))
  13174. ((not (eq last-command this-command)) (point))
  13175. (t (match-end 4))))))))
  13176. (org-no-warnings
  13177. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13178. (defun org-end-of-line (&optional arg)
  13179. "Go to the end of the line.
  13180. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13181. first attempt, and only move to after the tags when the cursor is already
  13182. beyond the end of the headline."
  13183. (interactive "P")
  13184. (if (or (not org-special-ctrl-a/e)
  13185. (not (org-on-heading-p)))
  13186. (end-of-line arg)
  13187. (let ((pos (point)))
  13188. (beginning-of-line 1)
  13189. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13190. (if (eq org-special-ctrl-a/e t)
  13191. (if (or (< pos (match-beginning 1))
  13192. (= pos (match-end 0)))
  13193. (goto-char (match-beginning 1))
  13194. (goto-char (match-end 0)))
  13195. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13196. (goto-char (match-end 0))
  13197. (goto-char (match-beginning 1))))
  13198. (end-of-line arg))))
  13199. (org-no-warnings
  13200. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13201. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13202. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13203. (defun org-kill-line (&optional arg)
  13204. "Kill line, to tags or end of line."
  13205. (interactive "P")
  13206. (cond
  13207. ((or (not org-special-ctrl-k)
  13208. (bolp)
  13209. (not (org-on-heading-p)))
  13210. (call-interactively 'kill-line))
  13211. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13212. (kill-region (point) (match-beginning 1))
  13213. (org-set-tags nil t))
  13214. (t (kill-region (point) (point-at-eol)))))
  13215. (define-key org-mode-map "\C-k" 'org-kill-line)
  13216. (defun org-yank (&optional arg)
  13217. "Yank. If the kill is a subtree, treat it specially.
  13218. This command will look at the current kill and check if is a single
  13219. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13220. cursor is at the beginning of a line or after the stars of a currently
  13221. empty headline, then the yank is handled specially. How exactly depends
  13222. on the value of the following variables, both set by default.
  13223. org-yank-folded-subtrees
  13224. When set, the subtree(s) will be folded after insertion, but only
  13225. if doing so would now swallow text after the yanked text.
  13226. org-yank-adjusted-subtrees
  13227. When set, the subtree will be promoted or demoted in order to
  13228. fit into the local outline tree structure, which means that the level
  13229. will be adjusted so that it becomes the smaller one of the two
  13230. *visible* surrounding headings.
  13231. Any prefix to this command will cause `yank' to be called directly with
  13232. no special treatment. In particular, a simple `C-u' prefix will just
  13233. plainly yank the text as it is.
  13234. \[1] Basically, the test checks if the first non-white line is a heading
  13235. and if there are no other headings with fewer stars."
  13236. (interactive "P")
  13237. (setq this-command 'yank)
  13238. (if arg
  13239. (call-interactively 'yank)
  13240. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13241. (and (org-kill-is-subtree-p)
  13242. (or (bolp)
  13243. (and (looking-at "[ \t]*$")
  13244. (string-match
  13245. "\\`\\*+\\'"
  13246. (buffer-substring (point-at-bol) (point)))))))
  13247. swallowp)
  13248. (cond
  13249. ((and subtreep org-yank-folded-subtrees)
  13250. (let ((beg (point))
  13251. end)
  13252. (if (and subtreep org-yank-adjusted-subtrees)
  13253. (org-paste-subtree nil nil 'for-yank)
  13254. (call-interactively 'yank))
  13255. (setq end (point))
  13256. (goto-char beg)
  13257. (when (and (bolp) subtreep
  13258. (not (setq swallowp
  13259. (org-yank-folding-would-swallow-text beg end))))
  13260. (or (looking-at outline-regexp)
  13261. (re-search-forward (concat "^" outline-regexp) end t))
  13262. (while (and (< (point) end) (looking-at outline-regexp))
  13263. (hide-subtree)
  13264. (org-cycle-show-empty-lines 'folded)
  13265. (condition-case nil
  13266. (outline-forward-same-level 1)
  13267. (error (goto-char end)))))
  13268. (when swallowp
  13269. (message
  13270. "Yanked text not folded because that would swallow text"))
  13271. (goto-char end)
  13272. (skip-chars-forward " \t\n\r")
  13273. (beginning-of-line 1)
  13274. (push-mark beg 'nomsg)))
  13275. ((and subtreep org-yank-adjusted-subtrees)
  13276. (let ((beg (point-at-bol)))
  13277. (org-paste-subtree nil nil 'for-yank)
  13278. (push-mark beg 'nomsg)))
  13279. (t
  13280. (call-interactively 'yank))))))
  13281. (defun org-yank-folding-would-swallow-text (beg end)
  13282. "Would hide-subtree at BEG swallow any text after END?"
  13283. (let (level)
  13284. (save-excursion
  13285. (goto-char beg)
  13286. (when (or (looking-at outline-regexp)
  13287. (re-search-forward (concat "^" outline-regexp) end t))
  13288. (setq level (org-outline-level)))
  13289. (goto-char end)
  13290. (skip-chars-forward " \t\r\n\v\f")
  13291. (if (or (eobp)
  13292. (and (bolp) (looking-at org-outline-regexp)
  13293. (<= (org-outline-level) level)))
  13294. nil ; Nothing would be swallowed
  13295. t)))) ; something would swallow
  13296. (define-key org-mode-map "\C-y" 'org-yank)
  13297. (defun org-invisible-p ()
  13298. "Check if point is at a character currently not visible."
  13299. ;; Early versions of noutline don't have `outline-invisible-p'.
  13300. (if (fboundp 'outline-invisible-p)
  13301. (outline-invisible-p)
  13302. (get-char-property (point) 'invisible)))
  13303. (defun org-invisible-p2 ()
  13304. "Check if point is at a character currently not visible."
  13305. (save-excursion
  13306. (if (and (eolp) (not (bobp))) (backward-char 1))
  13307. ;; Early versions of noutline don't have `outline-invisible-p'.
  13308. (if (fboundp 'outline-invisible-p)
  13309. (outline-invisible-p)
  13310. (get-char-property (point) 'invisible))))
  13311. (defun org-back-to-heading (&optional invisible-ok)
  13312. "Call `outline-back-to-heading', but provide a better error message."
  13313. (condition-case nil
  13314. (outline-back-to-heading invisible-ok)
  13315. (error (error "Before first headline at position %d in buffer %s"
  13316. (point) (current-buffer)))))
  13317. (defun org-before-first-heading-p ()
  13318. "Before first heading?"
  13319. (save-excursion
  13320. (null (re-search-backward "^\\*+ " nil t))))
  13321. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13322. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13323. (defun org-at-heading-or-item-p ()
  13324. (or (org-on-heading-p) (org-at-item-p)))
  13325. (defun org-on-target-p ()
  13326. (or (org-in-regexp org-radio-target-regexp)
  13327. (org-in-regexp org-target-regexp)))
  13328. (defun org-up-heading-all (arg)
  13329. "Move to the heading line of which the present line is a subheading.
  13330. This function considers both visible and invisible heading lines.
  13331. With argument, move up ARG levels."
  13332. (if (fboundp 'outline-up-heading-all)
  13333. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13334. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13335. (defun org-up-heading-safe ()
  13336. "Move to the heading line of which the present line is a subheading.
  13337. This version will not throw an error. It will return the level of the
  13338. headline found, or nil if no higher level is found."
  13339. (let (start-level re)
  13340. (org-back-to-heading t)
  13341. (setq start-level (funcall outline-level))
  13342. (if (equal start-level 1)
  13343. nil
  13344. (setq re (concat "^\\*\\{1," (number-to-string (1- start-level)) "\\} "))
  13345. (if (re-search-backward re nil t)
  13346. (funcall outline-level)))))
  13347. (defun org-first-sibling-p ()
  13348. "Is this heading the first child of its parents?"
  13349. (interactive)
  13350. (let ((re (concat "^" outline-regexp))
  13351. level l)
  13352. (unless (org-at-heading-p t)
  13353. (error "Not at a heading"))
  13354. (setq level (funcall outline-level))
  13355. (save-excursion
  13356. (if (not (re-search-backward re nil t))
  13357. t
  13358. (setq l (funcall outline-level))
  13359. (< l level)))))
  13360. (defun org-goto-sibling (&optional previous)
  13361. "Goto the next sibling, even if it is invisible.
  13362. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13363. when a sibling was found. When none is found, return nil and don't
  13364. move point."
  13365. (let ((fun (if previous 're-search-backward 're-search-forward))
  13366. (pos (point))
  13367. (re (concat "^" outline-regexp))
  13368. level l)
  13369. (when (condition-case nil (org-back-to-heading t) (error nil))
  13370. (setq level (funcall outline-level))
  13371. (catch 'exit
  13372. (or previous (forward-char 1))
  13373. (while (funcall fun re nil t)
  13374. (setq l (funcall outline-level))
  13375. (when (< l level) (goto-char pos) (throw 'exit nil))
  13376. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13377. (goto-char pos)
  13378. nil))))
  13379. (defun org-show-siblings ()
  13380. "Show all siblings of the current headline."
  13381. (save-excursion
  13382. (while (org-goto-sibling) (org-flag-heading nil)))
  13383. (save-excursion
  13384. (while (org-goto-sibling 'previous)
  13385. (org-flag-heading nil))))
  13386. (defun org-show-hidden-entry ()
  13387. "Show an entry where even the heading is hidden."
  13388. (save-excursion
  13389. (org-show-entry)))
  13390. (defun org-flag-heading (flag &optional entry)
  13391. "Flag the current heading. FLAG non-nil means make invisible.
  13392. When ENTRY is non-nil, show the entire entry."
  13393. (save-excursion
  13394. (org-back-to-heading t)
  13395. ;; Check if we should show the entire entry
  13396. (if entry
  13397. (progn
  13398. (org-show-entry)
  13399. (save-excursion
  13400. (and (outline-next-heading)
  13401. (org-flag-heading nil))))
  13402. (outline-flag-region (max (point-min) (1- (point)))
  13403. (save-excursion (outline-end-of-heading) (point))
  13404. flag))))
  13405. (defun org-forward-same-level (arg)
  13406. "Move forward to the ARG'th subheading at same level as this one.
  13407. Stop at the first and last subheadings of a superior heading.
  13408. This is like outline-forward-same-level, but invisible headings are ok."
  13409. (interactive "p")
  13410. (org-back-to-heading t)
  13411. (while (> arg 0)
  13412. (let ((point-to-move-to (save-excursion
  13413. (org-get-next-sibling))))
  13414. (if point-to-move-to
  13415. (progn
  13416. (goto-char point-to-move-to)
  13417. (setq arg (1- arg)))
  13418. (progn
  13419. (setq arg 0)
  13420. (error "No following same-level heading"))))))
  13421. (defun org-get-next-sibling ()
  13422. "Move to next heading of the same level, and return point.
  13423. If there is no such heading, return nil.
  13424. This is like outline-next-sibling, but invisible headings are ok."
  13425. (let ((level (funcall outline-level)))
  13426. (outline-next-heading)
  13427. (while (and (not (eobp)) (> (funcall outline-level) level))
  13428. (outline-next-heading))
  13429. (if (or (eobp) (< (funcall outline-level) level))
  13430. nil
  13431. (point))))
  13432. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13433. ;; This is an exact copy of the original function, but it uses
  13434. ;; `org-back-to-heading', to make it work also in invisible
  13435. ;; trees. And is uses an invisible-OK argument.
  13436. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13437. (org-back-to-heading invisible-OK)
  13438. (let ((first t)
  13439. (level (funcall outline-level)))
  13440. (while (and (not (eobp))
  13441. (or first (> (funcall outline-level) level)))
  13442. (setq first nil)
  13443. (outline-next-heading))
  13444. (unless to-heading
  13445. (if (memq (preceding-char) '(?\n ?\^M))
  13446. (progn
  13447. ;; Go to end of line before heading
  13448. (forward-char -1)
  13449. (if (memq (preceding-char) '(?\n ?\^M))
  13450. ;; leave blank line before heading
  13451. (forward-char -1))))))
  13452. (point))
  13453. (defun org-show-subtree ()
  13454. "Show everything after this heading at deeper levels."
  13455. (outline-flag-region
  13456. (point)
  13457. (save-excursion
  13458. (outline-end-of-subtree) (outline-next-heading) (point))
  13459. nil))
  13460. (defun org-show-entry ()
  13461. "Show the body directly following this heading.
  13462. Show the heading too, if it is currently invisible."
  13463. (interactive)
  13464. (save-excursion
  13465. (condition-case nil
  13466. (progn
  13467. (org-back-to-heading t)
  13468. (outline-flag-region
  13469. (max (point-min) (1- (point)))
  13470. (save-excursion
  13471. (re-search-forward
  13472. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13473. (or (match-beginning 1) (point-max)))
  13474. nil))
  13475. (error nil))))
  13476. (defun org-make-options-regexp (kwds)
  13477. "Make a regular expression for keyword lines."
  13478. (concat
  13479. "^"
  13480. "#?[ \t]*\\+\\("
  13481. (mapconcat 'regexp-quote kwds "\\|")
  13482. "\\):[ \t]*"
  13483. "\\(.+\\)"))
  13484. ;; Make isearch reveal the necessary context
  13485. (defun org-isearch-end ()
  13486. "Reveal context after isearch exits."
  13487. (when isearch-success ; only if search was successful
  13488. (if (featurep 'xemacs)
  13489. ;; Under XEmacs, the hook is run in the correct place,
  13490. ;; we directly show the context.
  13491. (org-show-context 'isearch)
  13492. ;; In Emacs the hook runs *before* restoring the overlays.
  13493. ;; So we have to use a one-time post-command-hook to do this.
  13494. ;; (Emacs 22 has a special variable, see function `org-mode')
  13495. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13496. isearch-mode-end-hook-quit)
  13497. ;; Only when the isearch was not quitted.
  13498. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13499. 'append 'local)))))
  13500. (defun org-isearch-post-command ()
  13501. "Remove self from hook, and show context."
  13502. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13503. (org-show-context 'isearch))
  13504. ;;;; Integration with and fixes for other packages
  13505. ;;; Imenu support
  13506. (defvar org-imenu-markers nil
  13507. "All markers currently used by Imenu.")
  13508. (make-variable-buffer-local 'org-imenu-markers)
  13509. (defun org-imenu-new-marker (&optional pos)
  13510. "Return a new marker for use by Imenu, and remember the marker."
  13511. (let ((m (make-marker)))
  13512. (move-marker m (or pos (point)))
  13513. (push m org-imenu-markers)
  13514. m))
  13515. (defun org-imenu-get-tree ()
  13516. "Produce the index for Imenu."
  13517. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13518. (setq org-imenu-markers nil)
  13519. (let* ((n org-imenu-depth)
  13520. (re (concat "^" outline-regexp))
  13521. (subs (make-vector (1+ n) nil))
  13522. (last-level 0)
  13523. m tree level head)
  13524. (save-excursion
  13525. (save-restriction
  13526. (widen)
  13527. (goto-char (point-max))
  13528. (while (re-search-backward re nil t)
  13529. (setq level (org-reduced-level (funcall outline-level)))
  13530. (when (<= level n)
  13531. (looking-at org-complex-heading-regexp)
  13532. (setq head (org-link-display-format
  13533. (org-match-string-no-properties 4))
  13534. m (org-imenu-new-marker))
  13535. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13536. (if (>= level last-level)
  13537. (push (cons head m) (aref subs level))
  13538. (push (cons head (aref subs (1+ level))) (aref subs level))
  13539. (loop for i from (1+ level) to n do (aset subs i nil)))
  13540. (setq last-level level)))))
  13541. (aref subs 1)))
  13542. (eval-after-load "imenu"
  13543. '(progn
  13544. (add-hook 'imenu-after-jump-hook
  13545. (lambda ()
  13546. (if (eq major-mode 'org-mode)
  13547. (org-show-context 'org-goto))))))
  13548. (defun org-link-display-format (link)
  13549. "Replace a link with either the description, or the link target
  13550. if no description is present"
  13551. (save-match-data
  13552. (if (string-match org-bracket-link-analytic-regexp link)
  13553. (replace-match (or (match-string 5 link)
  13554. (concat (match-string 1 link)
  13555. (match-string 3 link)))
  13556. nil nil link)
  13557. link)))
  13558. ;; Speedbar support
  13559. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13560. "Overlay marking the agenda restriction line in speedbar.")
  13561. (org-overlay-put org-speedbar-restriction-lock-overlay
  13562. 'face 'org-agenda-restriction-lock)
  13563. (org-overlay-put org-speedbar-restriction-lock-overlay
  13564. 'help-echo "Agendas are currently limited to this item.")
  13565. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13566. (defun org-speedbar-set-agenda-restriction ()
  13567. "Restrict future agenda commands to the location at point in speedbar.
  13568. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13569. (interactive)
  13570. (require 'org-agenda)
  13571. (let (p m tp np dir txt w)
  13572. (cond
  13573. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13574. 'org-imenu t))
  13575. (setq m (get-text-property p 'org-imenu-marker))
  13576. (save-excursion
  13577. (save-restriction
  13578. (set-buffer (marker-buffer m))
  13579. (goto-char m)
  13580. (org-agenda-set-restriction-lock 'subtree))))
  13581. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13582. 'speedbar-function 'speedbar-find-file))
  13583. (setq tp (previous-single-property-change
  13584. (1+ p) 'speedbar-function)
  13585. np (next-single-property-change
  13586. tp 'speedbar-function)
  13587. dir (speedbar-line-directory)
  13588. txt (buffer-substring-no-properties (or tp (point-min))
  13589. (or np (point-max))))
  13590. (save-excursion
  13591. (save-restriction
  13592. (set-buffer (find-file-noselect
  13593. (let ((default-directory dir))
  13594. (expand-file-name txt))))
  13595. (unless (org-mode-p)
  13596. (error "Cannot restrict to non-Org-mode file"))
  13597. (org-agenda-set-restriction-lock 'file))))
  13598. (t (error "Don't know how to restrict Org-mode's agenda")))
  13599. (org-move-overlay org-speedbar-restriction-lock-overlay
  13600. (point-at-bol) (point-at-eol))
  13601. (setq current-prefix-arg nil)
  13602. (org-agenda-maybe-redo)))
  13603. (eval-after-load "speedbar"
  13604. '(progn
  13605. (speedbar-add-supported-extension ".org")
  13606. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13607. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13608. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13609. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13610. (add-hook 'speedbar-visiting-tag-hook
  13611. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13612. ;;; Fixes and Hacks for problems with other packages
  13613. ;; Make flyspell not check words in links, to not mess up our keymap
  13614. (defun org-mode-flyspell-verify ()
  13615. "Don't let flyspell put overlays at active buttons."
  13616. (not (get-text-property (point) 'keymap)))
  13617. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13618. (eval-after-load "bookmark"
  13619. '(if (boundp 'bookmark-after-jump-hook)
  13620. ;; We can use the hook
  13621. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13622. ;; Hook not available, use advice
  13623. (defadvice bookmark-jump (after org-make-visible activate)
  13624. "Make the position visible."
  13625. (org-bookmark-jump-unhide))))
  13626. ;; Make sure saveplace show the location if it was hidden
  13627. (eval-after-load "saveplace"
  13628. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13629. "Make the position visible."
  13630. (org-bookmark-jump-unhide)))
  13631. (defun org-bookmark-jump-unhide ()
  13632. "Unhide the current position, to show the bookmark location."
  13633. (and (org-mode-p)
  13634. (or (org-invisible-p)
  13635. (save-excursion (goto-char (max (point-min) (1- (point))))
  13636. (org-invisible-p)))
  13637. (org-show-context 'bookmark-jump)))
  13638. ;; Make session.el ignore our circular variable
  13639. (eval-after-load "session"
  13640. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13641. ;;;; Experimental code
  13642. (defun org-closed-in-range ()
  13643. "Sparse tree of items closed in a certain time range.
  13644. Still experimental, may disappear in the future."
  13645. (interactive)
  13646. ;; Get the time interval from the user.
  13647. (let* ((time1 (time-to-seconds
  13648. (org-read-date nil 'to-time nil "Starting date: ")))
  13649. (time2 (time-to-seconds
  13650. (org-read-date nil 'to-time nil "End date:")))
  13651. ;; callback function
  13652. (callback (lambda ()
  13653. (let ((time
  13654. (time-to-seconds
  13655. (apply 'encode-time
  13656. (org-parse-time-string
  13657. (match-string 1))))))
  13658. ;; check if time in interval
  13659. (and (>= time time1) (<= time time2))))))
  13660. ;; make tree, check each match with the callback
  13661. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13662. ;;;; Finish up
  13663. (provide 'org)
  13664. (run-hooks 'org-load-hook)
  13665. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13666. ;;; org.el ends here