org.el 548 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770
  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.14
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.14"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-modules'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global IDs for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  155. (const :tag " mouse: Additional mouse support" org-mouse)
  156. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  157. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  158. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  159. (const :tag "C browser-url: Store link, directly from Browser" org-browser-url)
  160. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  161. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  162. (const :tag "C eval: Include command output as text" org-eval)
  163. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  164. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  165. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  166. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  167. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  168. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  169. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  170. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  171. (const :tag "C registry: A registry for Org links" org-registry)
  172. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  173. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  174. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  175. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  176. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  177. (defgroup org-startup nil
  178. "Options concerning startup of Org-mode."
  179. :tag "Org Startup"
  180. :group 'org)
  181. (defcustom org-startup-folded t
  182. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  183. This can also be configured on a per-file basis by adding one of
  184. the following lines anywhere in the buffer:
  185. #+STARTUP: fold
  186. #+STARTUP: nofold
  187. #+STARTUP: content"
  188. :group 'org-startup
  189. :type '(choice
  190. (const :tag "nofold: show all" nil)
  191. (const :tag "fold: overview" t)
  192. (const :tag "content: all headlines" content)))
  193. (defcustom org-startup-truncated t
  194. "Non-nil means, entering Org-mode will set `truncate-lines'.
  195. This is useful since some lines containing links can be very long and
  196. uninteresting. Also tables look terrible when wrapped."
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-startup-align-all-tables nil
  200. "Non-nil means, align all tables when visiting a file.
  201. This is useful when the column width in tables is forced with <N> cookies
  202. in table fields. Such tables will look correct only after the first re-align.
  203. This can also be configured on a per-file basis by adding one of
  204. the following lines anywhere in the buffer:
  205. #+STARTUP: align
  206. #+STARTUP: noalign"
  207. :group 'org-startup
  208. :type 'boolean)
  209. (defcustom org-insert-mode-line-in-empty-file nil
  210. "Non-nil means insert the first line setting Org-mode in empty files.
  211. When the function `org-mode' is called interactively in an empty file, this
  212. normally means that the file name does not automatically trigger Org-mode.
  213. To ensure that the file will always be in Org-mode in the future, a
  214. line enforcing Org-mode will be inserted into the buffer, if this option
  215. has been set."
  216. :group 'org-startup
  217. :type 'boolean)
  218. (defcustom org-replace-disputed-keys nil
  219. "Non-nil means use alternative key bindings for some keys.
  220. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  221. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  222. If you want to use Org-mode together with one of these other modes,
  223. or more generally if you would like to move some Org-mode commands to
  224. other keys, set this variable and configure the keys with the variable
  225. `org-disputed-keys'.
  226. This option is only relevant at load-time of Org-mode, and must be set
  227. *before* org.el is loaded. Changing it requires a restart of Emacs to
  228. become effective."
  229. :group 'org-startup
  230. :type 'boolean)
  231. (defcustom org-use-extra-keys nil
  232. "Non-nil means use extra key sequence definitions for certain
  233. commands. This happens automatically if you run XEmacs or if
  234. window-system is nil. This variable lets you do the same
  235. manually. You must set it before loading org.
  236. Example: on Carbon Emacs 22 running graphically, with an external
  237. keyboard on a Powerbook, the default way of setting M-left might
  238. not work for either Alt or ESC. Setting this variable will make
  239. it work for ESC."
  240. :group 'org-startup
  241. :type 'boolean)
  242. (if (fboundp 'defvaralias)
  243. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  244. (defcustom org-disputed-keys
  245. '(([(shift up)] . [(meta p)])
  246. ([(shift down)] . [(meta n)])
  247. ([(shift left)] . [(meta -)])
  248. ([(shift right)] . [(meta +)])
  249. ([(control shift right)] . [(meta shift +)])
  250. ([(control shift left)] . [(meta shift -)]))
  251. "Keys for which Org-mode and other modes compete.
  252. This is an alist, cars are the default keys, second element specifies
  253. the alternative to use when `org-replace-disputed-keys' is t.
  254. Keys can be specified in any syntax supported by `define-key'.
  255. The value of this option takes effect only at Org-mode's startup,
  256. therefore you'll have to restart Emacs to apply it after changing."
  257. :group 'org-startup
  258. :type 'alist)
  259. (defun org-key (key)
  260. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  261. Or return the original if not disputed."
  262. (if org-replace-disputed-keys
  263. (let* ((nkey (key-description key))
  264. (x (org-find-if (lambda (x)
  265. (equal (key-description (car x)) nkey))
  266. org-disputed-keys)))
  267. (if x (cdr x) key))
  268. key))
  269. (defun org-find-if (predicate seq)
  270. (catch 'exit
  271. (while seq
  272. (if (funcall predicate (car seq))
  273. (throw 'exit (car seq))
  274. (pop seq)))))
  275. (defun org-defkey (keymap key def)
  276. "Define a key, possibly translated, as returned by `org-key'."
  277. (define-key keymap (org-key key) def))
  278. (defcustom org-ellipsis nil
  279. "The ellipsis to use in the Org-mode outline.
  280. When nil, just use the standard three dots. When a string, use that instead,
  281. When a face, use the standart 3 dots, but with the specified face.
  282. The change affects only Org-mode (which will then use its own display table).
  283. Changing this requires executing `M-x org-mode' in a buffer to become
  284. effective."
  285. :group 'org-startup
  286. :type '(choice (const :tag "Default" nil)
  287. (face :tag "Face" :value org-warning)
  288. (string :tag "String" :value "...#")))
  289. (defvar org-display-table nil
  290. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  291. (defgroup org-keywords nil
  292. "Keywords in Org-mode."
  293. :tag "Org Keywords"
  294. :group 'org)
  295. (defcustom org-deadline-string "DEADLINE:"
  296. "String to mark deadline entries.
  297. A deadline is this string, followed by a time stamp. Should be a word,
  298. terminated by a colon. You can insert a schedule keyword and
  299. a timestamp with \\[org-deadline].
  300. Changes become only effective after restarting Emacs."
  301. :group 'org-keywords
  302. :type 'string)
  303. (defcustom org-scheduled-string "SCHEDULED:"
  304. "String to mark scheduled TODO entries.
  305. A schedule is this string, followed by a time stamp. Should be a word,
  306. terminated by a colon. You can insert a schedule keyword and
  307. a timestamp with \\[org-schedule].
  308. Changes become only effective after restarting Emacs."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-closed-string "CLOSED:"
  312. "String used as the prefix for timestamps logging closing a TODO entry."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defcustom org-clock-string "CLOCK:"
  316. "String used as prefix for timestamps clocking work hours on an item."
  317. :group 'org-keywords
  318. :type 'string)
  319. (defcustom org-comment-string "COMMENT"
  320. "Entries starting with this keyword will never be exported.
  321. An entry can be toggled between COMMENT and normal with
  322. \\[org-toggle-comment].
  323. Changes become only effective after restarting Emacs."
  324. :group 'org-keywords
  325. :type 'string)
  326. (defcustom org-quote-string "QUOTE"
  327. "Entries starting with this keyword will be exported in fixed-width font.
  328. Quoting applies only to the text in the entry following the headline, and does
  329. not extend beyond the next headline, even if that is lower level.
  330. An entry can be toggled between QUOTE and normal with
  331. \\[org-toggle-fixed-width-section]."
  332. :group 'org-keywords
  333. :type 'string)
  334. (defconst org-repeat-re
  335. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  336. "Regular expression for specifying repeated events.
  337. After a match, group 1 contains the repeat expression.")
  338. (defgroup org-structure nil
  339. "Options concerning the general structure of Org-mode files."
  340. :tag "Org Structure"
  341. :group 'org)
  342. (defgroup org-reveal-location nil
  343. "Options about how to make context of a location visible."
  344. :tag "Org Reveal Location"
  345. :group 'org-structure)
  346. (defconst org-context-choice
  347. '(choice
  348. (const :tag "Always" t)
  349. (const :tag "Never" nil)
  350. (repeat :greedy t :tag "Individual contexts"
  351. (cons
  352. (choice :tag "Context"
  353. (const agenda)
  354. (const org-goto)
  355. (const occur-tree)
  356. (const tags-tree)
  357. (const link-search)
  358. (const mark-goto)
  359. (const bookmark-jump)
  360. (const isearch)
  361. (const default))
  362. (boolean))))
  363. "Contexts for the reveal options.")
  364. (defcustom org-show-hierarchy-above '((default . t))
  365. "Non-nil means, show full hierarchy when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the hierarchy of headings
  368. above the exposed location is shown.
  369. Turning this off for example for sparse trees makes them very compact.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. Valid contexts are
  372. agenda when exposing an entry from the agenda
  373. org-goto when using the command `org-goto' on key C-c C-j
  374. occur-tree when using the command `org-occur' on key C-c /
  375. tags-tree when constructing a sparse tree based on tags matches
  376. link-search when exposing search matches associated with a link
  377. mark-goto when exposing the jump goal of a mark
  378. bookmark-jump when exposing a bookmark location
  379. isearch when exiting from an incremental search
  380. default default for all contexts not set explicitly"
  381. :group 'org-reveal-location
  382. :type org-context-choice)
  383. (defcustom org-show-following-heading '((default . nil))
  384. "Non-nil means, show following heading when revealing a location.
  385. Org-mode often shows locations in an org-mode file which might have
  386. been invisible before. When this is set, the heading following the
  387. match is shown.
  388. Turning this off for example for sparse trees makes them very compact,
  389. but makes it harder to edit the location of the match. In such a case,
  390. use the command \\[org-reveal] to show more context.
  391. Instead of t, this can also be an alist specifying this option for different
  392. contexts. See `org-show-hierarchy-above' for valid contexts."
  393. :group 'org-reveal-location
  394. :type org-context-choice)
  395. (defcustom org-show-siblings '((default . nil) (isearch t))
  396. "Non-nil means, show all sibling heading when revealing a location.
  397. Org-mode often shows locations in an org-mode file which might have
  398. been invisible before. When this is set, the sibling of the current entry
  399. heading are all made visible. If `org-show-hierarchy-above' is t,
  400. the same happens on each level of the hierarchy above the current entry.
  401. By default this is on for the isearch context, off for all other contexts.
  402. Turning this off for example for sparse trees makes them very compact,
  403. but makes it harder to edit the location of the match. In such a case,
  404. use the command \\[org-reveal] to show more context.
  405. Instead of t, this can also be an alist specifying this option for different
  406. contexts. See `org-show-hierarchy-above' for valid contexts."
  407. :group 'org-reveal-location
  408. :type org-context-choice)
  409. (defcustom org-show-entry-below '((default . nil))
  410. "Non-nil means, show the entry below a headline when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the text below the headline that is
  413. exposed is also shown.
  414. By default this is off for all contexts.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. See `org-show-hierarchy-above' for valid contexts."
  417. :group 'org-reveal-location
  418. :type org-context-choice)
  419. (defcustom org-indirect-buffer-display 'other-window
  420. "How should indirect tree buffers be displayed?
  421. This applies to indirect buffers created with the commands
  422. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  423. Valid values are:
  424. current-window Display in the current window
  425. other-window Just display in another window.
  426. dedicated-frame Create one new frame, and re-use it each time.
  427. new-frame Make a new frame each time. Note that in this case
  428. previously-made indirect buffers are kept, and you need to
  429. kill these buffers yourself."
  430. :group 'org-structure
  431. :group 'org-agenda-windows
  432. :type '(choice
  433. (const :tag "In current window" current-window)
  434. (const :tag "In current frame, other window" other-window)
  435. (const :tag "Each time a new frame" new-frame)
  436. (const :tag "One dedicated frame" dedicated-frame)))
  437. (defgroup org-cycle nil
  438. "Options concerning visibility cycling in Org-mode."
  439. :tag "Org Cycle"
  440. :group 'org-structure)
  441. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  442. "Names of drawers. Drawers are not opened by cycling on the headline above.
  443. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  444. this:
  445. :DRAWERNAME:
  446. .....
  447. :END:
  448. The drawer \"PROPERTIES\" is special for capturing properties through
  449. the property API.
  450. Drawers can be defined on the per-file basis with a line like:
  451. #+DRAWERS: HIDDEN STATE PROPERTIES"
  452. :group 'org-structure
  453. :type '(repeat (string :tag "Drawer Name")))
  454. (defcustom org-cycle-global-at-bob nil
  455. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  456. This makes it possible to do global cycling without having to use S-TAB or
  457. C-u TAB. For this special case to work, the first line of the buffer
  458. must not be a headline - it may be empty ot some other text. When used in
  459. this way, `org-cycle-hook' is disables temporarily, to make sure the
  460. cursor stays at the beginning of the buffer.
  461. When this option is nil, don't do anything special at the beginning
  462. of the buffer."
  463. :group 'org-cycle
  464. :type 'boolean)
  465. (defcustom org-cycle-emulate-tab t
  466. "Where should `org-cycle' emulate TAB.
  467. nil Never
  468. white Only in completely white lines
  469. whitestart Only at the beginning of lines, before the first non-white char
  470. t Everywhere except in headlines
  471. exc-hl-bol Everywhere except at the start of a headline
  472. If TAB is used in a place where it does not emulate TAB, the current subtree
  473. visibility is cycled."
  474. :group 'org-cycle
  475. :type '(choice (const :tag "Never" nil)
  476. (const :tag "Only in completely white lines" white)
  477. (const :tag "Before first char in a line" whitestart)
  478. (const :tag "Everywhere except in headlines" t)
  479. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  480. ))
  481. (defcustom org-cycle-separator-lines 2
  482. "Number of empty lines needed to keep an empty line between collapsed trees.
  483. If you leave an empty line between the end of a subtree and the following
  484. headline, this empty line is hidden when the subtree is folded.
  485. Org-mode will leave (exactly) one empty line visible if the number of
  486. empty lines is equal or larger to the number given in this variable.
  487. So the default 2 means, at least 2 empty lines after the end of a subtree
  488. are needed to produce free space between a collapsed subtree and the
  489. following headline.
  490. Special case: when 0, never leave empty lines in collapsed view."
  491. :group 'org-cycle
  492. :type 'integer)
  493. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  494. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  495. org-cycle-hide-drawers
  496. org-cycle-show-empty-lines
  497. org-optimize-window-after-visibility-change)
  498. "Hook that is run after `org-cycle' has changed the buffer visibility.
  499. The function(s) in this hook must accept a single argument which indicates
  500. the new state that was set by the most recent `org-cycle' command. The
  501. argument is a symbol. After a global state change, it can have the values
  502. `overview', `content', or `all'. After a local state change, it can have
  503. the values `folded', `children', or `subtree'."
  504. :group 'org-cycle
  505. :type 'hook)
  506. (defgroup org-edit-structure nil
  507. "Options concerning structure editing in Org-mode."
  508. :tag "Org Edit Structure"
  509. :group 'org-structure)
  510. (defcustom org-odd-levels-only nil
  511. "Non-nil means, skip even levels and only use odd levels for the outline.
  512. This has the effect that two stars are being added/taken away in
  513. promotion/demotion commands. It also influences how levels are
  514. handled by the exporters.
  515. Changing it requires restart of `font-lock-mode' to become effective
  516. for fontification also in regions already fontified.
  517. You may also set this on a per-file basis by adding one of the following
  518. lines to the buffer:
  519. #+STARTUP: odd
  520. #+STARTUP: oddeven"
  521. :group 'org-edit-structure
  522. :group 'org-font-lock
  523. :type 'boolean)
  524. (defcustom org-adapt-indentation t
  525. "Non-nil means, adapt indentation when promoting and demoting.
  526. When this is set and the *entire* text in an entry is indented, the
  527. indentation is increased by one space in a demotion command, and
  528. decreased by one in a promotion command. If any line in the entry
  529. body starts at column 0, indentation is not changed at all."
  530. :group 'org-edit-structure
  531. :type 'boolean)
  532. (defcustom org-special-ctrl-a/e nil
  533. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  534. When t, `C-a' will bring back the cursor to the beginning of the
  535. headline text, i.e. after the stars and after a possible TODO keyword.
  536. In an item, this will be the position after the bullet.
  537. When the cursor is already at that position, another `C-a' will bring
  538. it to the beginning of the line.
  539. `C-e' will jump to the end of the headline, ignoring the presence of tags
  540. in the headline. A second `C-e' will then jump to the true end of the
  541. line, after any tags.
  542. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  543. and only a directly following, identical keypress will bring the cursor
  544. to the special positions."
  545. :group 'org-edit-structure
  546. :type '(choice
  547. (const :tag "off" nil)
  548. (const :tag "after bullet first" t)
  549. (const :tag "border first" reversed)))
  550. (if (fboundp 'defvaralias)
  551. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  552. (defcustom org-special-ctrl-k nil
  553. "Non-nil means `C-k' will behave specially in headlines.
  554. When nil, `C-k' will call the default `kill-line' command.
  555. When t, the following will happen while the cursor is in the headline:
  556. - When the cursor is at the beginning of a headline, kill the entire
  557. line and possible the folded subtree below the line.
  558. - When in the middle of the headline text, kill the headline up to the tags.
  559. - When after the headline text, kill the tags."
  560. :group 'org-edit-structure
  561. :type 'boolean)
  562. (defcustom org-yank-folded-subtrees t
  563. "Non-nil means, when yanking subtrees, fold them.
  564. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  565. it starts with a heading and all other headings in it are either children
  566. or siblings, then fold all the subtrees. However, do this only if no
  567. text after the yank would be swallowed into a folded tree by this action."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-yank-adjusted-subtrees t
  571. "Non-nil means, when yanking subtrees, adjust the level.
  572. With this setting, `org-paste-subtree' is used to insert the subtree, see
  573. this function for details."
  574. :group 'org-edit-structure
  575. :type 'boolean)
  576. (defcustom org-M-RET-may-split-line '((default . t))
  577. "Non-nil means, M-RET will split the line at the cursor position.
  578. When nil, it will go to the end of the line before making a
  579. new line.
  580. You may also set this option in a different way for different
  581. contexts. Valid contexts are:
  582. headline when creating a new headline
  583. item when creating a new item
  584. table in a table field
  585. default the value to be used for all contexts not explicitly
  586. customized"
  587. :group 'org-structure
  588. :group 'org-table
  589. :type '(choice
  590. (const :tag "Always" t)
  591. (const :tag "Never" nil)
  592. (repeat :greedy t :tag "Individual contexts"
  593. (cons
  594. (choice :tag "Context"
  595. (const headline)
  596. (const item)
  597. (const table)
  598. (const default))
  599. (boolean)))))
  600. (defcustom org-insert-heading-respect-content nil
  601. "Non-nil means, insert new headings after the current subtree.
  602. When nil, the new heading is created directly after the current line.
  603. The commands \\[org-insert-heading-respect-content] and
  604. \\[org-insert-todo-heading-respect-content] turn this variable on
  605. for the duration of the command."
  606. :group 'org-structure
  607. :type 'boolean)
  608. (defcustom org-blank-before-new-entry '((heading . nil)
  609. (plain-list-item . nil))
  610. "Should `org-insert-heading' leave a blank line before new heading/item?
  611. The value is an alist, with `heading' and `plain-list-item' as car,
  612. and a boolean flag as cdr."
  613. :group 'org-edit-structure
  614. :type '(list
  615. (cons (const heading) (boolean))
  616. (cons (const plain-list-item) (boolean))))
  617. (defcustom org-insert-heading-hook nil
  618. "Hook being run after inserting a new heading."
  619. :group 'org-edit-structure
  620. :type 'hook)
  621. (defcustom org-enable-fixed-width-editor t
  622. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  623. This currently only means, they are never auto-wrapped.
  624. When nil, such lines will be treated like ordinary lines.
  625. See also the QUOTE keyword."
  626. :group 'org-edit-structure
  627. :type 'boolean)
  628. (defcustom org-edit-src-region-extra nil
  629. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  630. For examples see the function `org-edit-src-find-region-and-lang'.
  631. The regular expression identifying the begin marker should end with a newline,
  632. and the regexp marking the end line should start with a newline, to make sure
  633. there are kept outside the narrowed region."
  634. :group 'org-edit-structure
  635. :type '(repeat
  636. (list
  637. (regexp :tag "begin regexp")
  638. (regexp :tag "end regexp")
  639. (choice :tag "language"
  640. (string :tag "specify")
  641. (integer :tag "from match group")
  642. (const :tag "from `lang' element")
  643. (const :tag "from `style' element")))))
  644. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  645. "The mode that should be used to edit fixed-width regions.
  646. These are the regions where each line starts with a colon."
  647. :group 'org-edit-structure
  648. :type '(choice
  649. (const artist-mode)
  650. (const picture-mode)
  651. (const fundamental-mode)
  652. (function :tag "Other (specify)")))
  653. (defcustom org-goto-auto-isearch t
  654. "Non-nil means, typing characters in org-goto starts incremental search."
  655. :group 'org-edit-structure
  656. :type 'boolean)
  657. (defgroup org-sparse-trees nil
  658. "Options concerning sparse trees in Org-mode."
  659. :tag "Org Sparse Trees"
  660. :group 'org-structure)
  661. (defcustom org-highlight-sparse-tree-matches t
  662. "Non-nil means, highlight all matches that define a sparse tree.
  663. The highlights will automatically disappear the next time the buffer is
  664. changed by an edit command."
  665. :group 'org-sparse-trees
  666. :type 'boolean)
  667. (defcustom org-remove-highlights-with-change t
  668. "Non-nil means, any change to the buffer will remove temporary highlights.
  669. Such highlights are created by `org-occur' and `org-clock-display'.
  670. When nil, `C-c C-c needs to be used to get rid of the highlights.
  671. The highlights created by `org-preview-latex-fragment' always need
  672. `C-c C-c' to be removed."
  673. :group 'org-sparse-trees
  674. :group 'org-time
  675. :type 'boolean)
  676. (defcustom org-occur-hook '(org-first-headline-recenter)
  677. "Hook that is run after `org-occur' has constructed a sparse tree.
  678. This can be used to recenter the window to show as much of the structure
  679. as possible."
  680. :group 'org-sparse-trees
  681. :type 'hook)
  682. (defgroup org-imenu-and-speedbar nil
  683. "Options concerning imenu and speedbar in Org-mode."
  684. :tag "Org Imenu and Speedbar"
  685. :group 'org-structure)
  686. (defcustom org-imenu-depth 2
  687. "The maximum level for Imenu access to Org-mode headlines.
  688. This also applied for speedbar access."
  689. :group 'org-imenu-and-speedbar
  690. :type 'number)
  691. (defgroup org-table nil
  692. "Options concerning tables in Org-mode."
  693. :tag "Org Table"
  694. :group 'org)
  695. (defcustom org-enable-table-editor 'optimized
  696. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  697. When nil, such lines will be treated like ordinary lines.
  698. When equal to the symbol `optimized', the table editor will be optimized to
  699. do the following:
  700. - Automatic overwrite mode in front of whitespace in table fields.
  701. This makes the structure of the table stay in tact as long as the edited
  702. field does not exceed the column width.
  703. - Minimize the number of realigns. Normally, the table is aligned each time
  704. TAB or RET are pressed to move to another field. With optimization this
  705. happens only if changes to a field might have changed the column width.
  706. Optimization requires replacing the functions `self-insert-command',
  707. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  708. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  709. very good at guessing when a re-align will be necessary, but you can always
  710. force one with \\[org-ctrl-c-ctrl-c].
  711. If you would like to use the optimized version in Org-mode, but the
  712. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  713. This variable can be used to turn on and off the table editor during a session,
  714. but in order to toggle optimization, a restart is required.
  715. See also the variable `org-table-auto-blank-field'."
  716. :group 'org-table
  717. :type '(choice
  718. (const :tag "off" nil)
  719. (const :tag "on" t)
  720. (const :tag "on, optimized" optimized)))
  721. (defcustom org-table-tab-recognizes-table.el t
  722. "Non-nil means, TAB will automatically notice a table.el table.
  723. When it sees such a table, it moves point into it and - if necessary -
  724. calls `table-recognize-table'."
  725. :group 'org-table-editing
  726. :type 'boolean)
  727. (defgroup org-link nil
  728. "Options concerning links in Org-mode."
  729. :tag "Org Link"
  730. :group 'org)
  731. (defvar org-link-abbrev-alist-local nil
  732. "Buffer-local version of `org-link-abbrev-alist', which see.
  733. The value of this is taken from the #+LINK lines.")
  734. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  735. (defcustom org-link-abbrev-alist nil
  736. "Alist of link abbreviations.
  737. The car of each element is a string, to be replaced at the start of a link.
  738. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  739. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  740. [[linkkey:tag][description]]
  741. If REPLACE is a string, the tag will simply be appended to create the link.
  742. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  743. the placeholder \"%h\" will cause a url-encoded version of the tag to
  744. be inserted at that point (see the function `url-hexify-string').
  745. REPLACE may also be a function that will be called with the tag as the
  746. only argument to create the link, which should be returned as a string.
  747. See the manual for examples."
  748. :group 'org-link
  749. :type '(repeat
  750. (cons
  751. (string :tag "Protocol")
  752. (choice
  753. (string :tag "Format")
  754. (function)))))
  755. (defcustom org-descriptive-links t
  756. "Non-nil means, hide link part and only show description of bracket links.
  757. Bracket links are like [[link][descritpion]]. This variable sets the initial
  758. state in new org-mode buffers. The setting can then be toggled on a
  759. per-buffer basis from the Org->Hyperlinks menu."
  760. :group 'org-link
  761. :type 'boolean)
  762. (defcustom org-link-file-path-type 'adaptive
  763. "How the path name in file links should be stored.
  764. Valid values are:
  765. relative Relative to the current directory, i.e. the directory of the file
  766. into which the link is being inserted.
  767. absolute Absolute path, if possible with ~ for home directory.
  768. noabbrev Absolute path, no abbreviation of home directory.
  769. adaptive Use relative path for files in the current directory and sub-
  770. directories of it. For other files, use an absolute path."
  771. :group 'org-link
  772. :type '(choice
  773. (const relative)
  774. (const absolute)
  775. (const noabbrev)
  776. (const adaptive)))
  777. (defcustom org-activate-links '(bracket angle plain radio tag date)
  778. "Types of links that should be activated in Org-mode files.
  779. This is a list of symbols, each leading to the activation of a certain link
  780. type. In principle, it does not hurt to turn on most link types - there may
  781. be a small gain when turning off unused link types. The types are:
  782. bracket The recommended [[link][description]] or [[link]] links with hiding.
  783. angular Links in angular brackes that may contain whitespace like
  784. <bbdb:Carsten Dominik>.
  785. plain Plain links in normal text, no whitespace, like http://google.com.
  786. radio Text that is matched by a radio target, see manual for details.
  787. tag Tag settings in a headline (link to tag search).
  788. date Time stamps (link to calendar).
  789. Changing this variable requires a restart of Emacs to become effective."
  790. :group 'org-link
  791. :type '(set (const :tag "Double bracket links (new style)" bracket)
  792. (const :tag "Angular bracket links (old style)" angular)
  793. (const :tag "Plain text links" plain)
  794. (const :tag "Radio target matches" radio)
  795. (const :tag "Tags" tag)
  796. (const :tag "Timestamps" date)))
  797. (defcustom org-make-link-description-function nil
  798. "Function to use to generate link descriptions from links. If
  799. nil the link location will be used. This function must take two
  800. parameters; the first is the link and the second the description
  801. org-insert-link has generated, and should return the description
  802. to use."
  803. :group 'org-link
  804. :type 'function)
  805. (defgroup org-link-store nil
  806. "Options concerning storing links in Org-mode."
  807. :tag "Org Store Link"
  808. :group 'org-link)
  809. (defcustom org-email-link-description-format "Email %c: %.30s"
  810. "Format of the description part of a link to an email or usenet message.
  811. The following %-excapes will be replaced by corresponding information:
  812. %F full \"From\" field
  813. %f name, taken from \"From\" field, address if no name
  814. %T full \"To\" field
  815. %t first name in \"To\" field, address if no name
  816. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  817. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  818. %s subject
  819. %m message-id.
  820. You may use normal field width specification between the % and the letter.
  821. This is for example useful to limit the length of the subject.
  822. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  823. :group 'org-link-store
  824. :type 'string)
  825. (defcustom org-from-is-user-regexp
  826. (let (r1 r2)
  827. (when (and user-mail-address (not (string= user-mail-address "")))
  828. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  829. (when (and user-full-name (not (string= user-full-name "")))
  830. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  831. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  832. "Regexp mached against the \"From:\" header of an email or usenet message.
  833. It should match if the message is from the user him/herself."
  834. :group 'org-link-store
  835. :type 'regexp)
  836. (defcustom org-link-to-org-use-id 'create-if-interactive
  837. "Non-nil means, storing a link to an Org file will use entry IDs.
  838. Note that before this variable is even considered, org-id must be loaded,
  839. to please customize `org-modules' and turn it on.
  840. The variable can have the following values:
  841. t Create an ID if needed to make a link to the current entry.
  842. create-if-interactive
  843. If `org-store-link' is called directly (interactively, as a user
  844. command), do create an ID to support the link. But when doing the
  845. job for remember, only use the ID if it already exists. The
  846. purpose of this setting is to avoid proliferation of unwanted
  847. IDs, just because you happen to be in an Org file when you
  848. call `org-remember' that automatically and preemptively
  849. creates a link. If you do want to get an ID link in a remember
  850. template to an entry not having an ID, create it first by
  851. explicitly creating a link to it, using `C-c C-l' first.
  852. use-existing
  853. Use existing ID, do not create one.
  854. nil Never use an ID to make a link, instead link using a text search for
  855. the headline text."
  856. :group 'org-link-store
  857. :type '(choice
  858. (const :tag "Create ID to make link" t)
  859. (const :tag "Create if string link interactively"
  860. 'create-if-interactive)
  861. (const :tag "Only use existing" 'use-existing)
  862. (const :tag "Do not use ID to create link" nil)))
  863. (defcustom org-context-in-file-links t
  864. "Non-nil means, file links from `org-store-link' contain context.
  865. A search string will be added to the file name with :: as separator and
  866. used to find the context when the link is activated by the command
  867. `org-open-at-point'.
  868. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  869. negates this setting for the duration of the command."
  870. :group 'org-link-store
  871. :type 'boolean)
  872. (defcustom org-keep-stored-link-after-insertion nil
  873. "Non-nil means, keep link in list for entire session.
  874. The command `org-store-link' adds a link pointing to the current
  875. location to an internal list. These links accumulate during a session.
  876. The command `org-insert-link' can be used to insert links into any
  877. Org-mode file (offering completion for all stored links). When this
  878. option is nil, every link which has been inserted once using \\[org-insert-link]
  879. will be removed from the list, to make completing the unused links
  880. more efficient."
  881. :group 'org-link-store
  882. :type 'boolean)
  883. (defgroup org-link-follow nil
  884. "Options concerning following links in Org-mode."
  885. :tag "Org Follow Link"
  886. :group 'org-link)
  887. (defcustom org-link-translation-function nil
  888. "Function to translate links with different syntax to Org syntax.
  889. This can be used to translate links created for example by the Planner
  890. or emacs-wiki packages to Org syntax.
  891. The function must accept two parameters, a TYPE containing the link
  892. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  893. which is everything after the link protocol. It should return a cons
  894. with possibly modifed values of type and path.
  895. Org contains a function for this, so if you set this variable to
  896. `org-translate-link-from-planner', you should be able follow many
  897. links created by planner."
  898. :group 'org-link-follow
  899. :type 'function)
  900. (defcustom org-follow-link-hook nil
  901. "Hook that is run after a link has been followed."
  902. :group 'org-link-follow
  903. :type 'hook)
  904. (defcustom org-tab-follows-link nil
  905. "Non-nil means, on links TAB will follow the link.
  906. Needs to be set before org.el is loaded."
  907. :group 'org-link-follow
  908. :type 'boolean)
  909. (defcustom org-return-follows-link nil
  910. "Non-nil means, on links RET will follow the link.
  911. Needs to be set before org.el is loaded."
  912. :group 'org-link-follow
  913. :type 'boolean)
  914. (defcustom org-mouse-1-follows-link
  915. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  916. "Non-nil means, mouse-1 on a link will follow the link.
  917. A longer mouse click will still set point. Does not work on XEmacs.
  918. Needs to be set before org.el is loaded."
  919. :group 'org-link-follow
  920. :type 'boolean)
  921. (defcustom org-mark-ring-length 4
  922. "Number of different positions to be recorded in the ring
  923. Changing this requires a restart of Emacs to work correctly."
  924. :group 'org-link-follow
  925. :type 'interger)
  926. (defcustom org-link-frame-setup
  927. '((vm . vm-visit-folder-other-frame)
  928. (gnus . gnus-other-frame)
  929. (file . find-file-other-window))
  930. "Setup the frame configuration for following links.
  931. When following a link with Emacs, it may often be useful to display
  932. this link in another window or frame. This variable can be used to
  933. set this up for the different types of links.
  934. For VM, use any of
  935. `vm-visit-folder'
  936. `vm-visit-folder-other-frame'
  937. For Gnus, use any of
  938. `gnus'
  939. `gnus-other-frame'
  940. `org-gnus-no-new-news'
  941. For FILE, use any of
  942. `find-file'
  943. `find-file-other-window'
  944. `find-file-other-frame'
  945. For the calendar, use the variable `calendar-setup'.
  946. For BBDB, it is currently only possible to display the matches in
  947. another window."
  948. :group 'org-link-follow
  949. :type '(list
  950. (cons (const vm)
  951. (choice
  952. (const vm-visit-folder)
  953. (const vm-visit-folder-other-window)
  954. (const vm-visit-folder-other-frame)))
  955. (cons (const gnus)
  956. (choice
  957. (const gnus)
  958. (const gnus-other-frame)
  959. (const org-gnus-no-new-news)))
  960. (cons (const file)
  961. (choice
  962. (const find-file)
  963. (const find-file-other-window)
  964. (const find-file-other-frame)))))
  965. (defcustom org-display-internal-link-with-indirect-buffer nil
  966. "Non-nil means, use indirect buffer to display infile links.
  967. Activating internal links (from one location in a file to another location
  968. in the same file) normally just jumps to the location. When the link is
  969. activated with a C-u prefix (or with mouse-3), the link is displayed in
  970. another window. When this option is set, the other window actually displays
  971. an indirect buffer clone of the current buffer, to avoid any visibility
  972. changes to the current buffer."
  973. :group 'org-link-follow
  974. :type 'boolean)
  975. (defcustom org-open-non-existing-files nil
  976. "Non-nil means, `org-open-file' will open non-existing files.
  977. When nil, an error will be generated."
  978. :group 'org-link-follow
  979. :type 'boolean)
  980. (defcustom org-open-directory-means-index-dot-org nil
  981. "Non-nil means, a link to a directory really means to index.org.
  982. When nil, following a directory link will run dired or open a finder/explorer
  983. window on that directory."
  984. :group 'org-link-follow
  985. :type 'boolean)
  986. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  987. "Function and arguments to call for following mailto links.
  988. This is a list with the first element being a lisp function, and the
  989. remaining elements being arguments to the function. In string arguments,
  990. %a will be replaced by the address, and %s will be replaced by the subject
  991. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  992. :group 'org-link-follow
  993. :type '(choice
  994. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  995. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  996. (const :tag "message-mail" (message-mail "%a" "%s"))
  997. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  998. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  999. "Non-nil means, ask for confirmation before executing shell links.
  1000. Shell links can be dangerous: just think about a link
  1001. [[shell:rm -rf ~/*][Google Search]]
  1002. This link would show up in your Org-mode document as \"Google Search\",
  1003. but really it would remove your entire home directory.
  1004. Therefore we advise against setting this variable to nil.
  1005. Just change it to `y-or-n-p' of you want to confirm with a
  1006. single keystroke rather than having to type \"yes\"."
  1007. :group 'org-link-follow
  1008. :type '(choice
  1009. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1010. (const :tag "with y-or-n (faster)" y-or-n-p)
  1011. (const :tag "no confirmation (dangerous)" nil)))
  1012. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  1013. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  1014. Elisp links can be dangerous: just think about a link
  1015. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  1016. This link would show up in your Org-mode document as \"Google Search\",
  1017. but really it would remove your entire home directory.
  1018. Therefore we advise against setting this variable to nil.
  1019. Just change it to `y-or-n-p' of you want to confirm with a
  1020. single keystroke rather than having to type \"yes\"."
  1021. :group 'org-link-follow
  1022. :type '(choice
  1023. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  1024. (const :tag "with y-or-n (faster)" y-or-n-p)
  1025. (const :tag "no confirmation (dangerous)" nil)))
  1026. (defconst org-file-apps-defaults-gnu
  1027. '((remote . emacs)
  1028. (system . mailcap)
  1029. (t . mailcap))
  1030. "Default file applications on a UNIX or GNU/Linux system.
  1031. See `org-file-apps'.")
  1032. (defconst org-file-apps-defaults-macosx
  1033. '((remote . emacs)
  1034. (t . "open %s")
  1035. (system . "open %s")
  1036. ("ps.gz" . "gv %s")
  1037. ("eps.gz" . "gv %s")
  1038. ("dvi" . "xdvi %s")
  1039. ("fig" . "xfig %s"))
  1040. "Default file applications on a MacOS X system.
  1041. The system \"open\" is known as a default, but we use X11 applications
  1042. for some files for which the OS does not have a good default.
  1043. See `org-file-apps'.")
  1044. (defconst org-file-apps-defaults-windowsnt
  1045. (list
  1046. '(remote . emacs)
  1047. (cons t
  1048. (list (if (featurep 'xemacs)
  1049. 'mswindows-shell-execute
  1050. 'w32-shell-execute)
  1051. "open" 'file))
  1052. (cons 'system
  1053. (list (if (featurep 'xemacs)
  1054. 'mswindows-shell-execute
  1055. 'w32-shell-execute)
  1056. "open" 'file)))
  1057. "Default file applications on a Windows NT system.
  1058. The system \"open\" is used for most files.
  1059. See `org-file-apps'.")
  1060. (defcustom org-file-apps
  1061. '(
  1062. (auto-mode . emacs)
  1063. ("\\.x?html?\\'" . default)
  1064. ("\\.pdf\\'" . default)
  1065. )
  1066. "External applications for opening `file:path' items in a document.
  1067. Org-mode uses system defaults for different file types, but
  1068. you can use this variable to set the application for a given file
  1069. extension. The entries in this list are cons cells where the car identifies
  1070. files and the cdr the corresponding command. Possible values for the
  1071. file identifier are
  1072. \"regex\" Regular expression matched against the file name. For backward
  1073. compatibility, this can also be a string with only alphanumeric
  1074. characters, which is then interpreted as an extension.
  1075. `directory' Matches a directory
  1076. `remote' Matches a remote file, accessible through tramp or efs.
  1077. Remote files most likely should be visited through Emacs
  1078. because external applications cannot handle such paths.
  1079. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1080. so all files Emacs knows how to handle. Using this with
  1081. command `emacs' will open most files in Emacs. Beware that this
  1082. will also open html files insite Emacs, unless you add
  1083. (\"html\" . default) to the list as well.
  1084. t Default for files not matched by any of the other options.
  1085. `system' The system command to open files, like `open' on Windows
  1086. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1087. that will be selected if you call `C-c C-o' with a double
  1088. `C-u C-u' prefix.
  1089. Possible values for the command are:
  1090. `emacs' The file will be visited by the current Emacs process.
  1091. `default' Use the default application for this file type, which is the
  1092. association for t in the list, most likely in the system-specific
  1093. part.
  1094. This can be used to overrule an unwanted seting in the
  1095. system-specific variable.
  1096. `system' Use the system command for opening files, like \"open\".
  1097. This command is specified by the entry whose car is `system'.
  1098. Most likely, the system-specific version of this variable
  1099. does define this command, but you can overrule/replace it
  1100. here.
  1101. string A command to be executed by a shell; %s will be replaced
  1102. by the path to the file.
  1103. sexp A Lisp form which will be evaluated. The file path will
  1104. be available in the Lisp variable `file'.
  1105. For more examples, see the system specific constants
  1106. `org-file-apps-defaults-macosx'
  1107. `org-file-apps-defaults-windowsnt'
  1108. `org-file-apps-defaults-gnu'."
  1109. :group 'org-link-follow
  1110. :type '(repeat
  1111. (cons (choice :value ""
  1112. (string :tag "Extension")
  1113. (const :tag "System command to open files" system)
  1114. (const :tag "Default for unrecognized files" t)
  1115. (const :tag "Remote file" remote)
  1116. (const :tag "Links to a directory" directory)
  1117. (const :tag "Any files that have Emacs modes"
  1118. auto-mode))
  1119. (choice :value ""
  1120. (const :tag "Visit with Emacs" emacs)
  1121. (const :tag "Use default" default)
  1122. (const :tag "Use the system command" system)
  1123. (string :tag "Command")
  1124. (sexp :tag "Lisp form")))))
  1125. (defgroup org-refile nil
  1126. "Options concerning refiling entries in Org-mode."
  1127. :tag "Org Refile"
  1128. :group 'org)
  1129. (defcustom org-directory "~/org"
  1130. "Directory with org files.
  1131. This directory will be used as default to prompt for org files.
  1132. Used by the hooks for remember.el."
  1133. :group 'org-refile
  1134. :group 'org-remember
  1135. :type 'directory)
  1136. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1137. "Default target for storing notes.
  1138. Used by the hooks for remember.el. This can be a string, or nil to mean
  1139. the value of `remember-data-file'.
  1140. You can set this on a per-template basis with the variable
  1141. `org-remember-templates'."
  1142. :group 'org-refile
  1143. :group 'org-remember
  1144. :type '(choice
  1145. (const :tag "Default from remember-data-file" nil)
  1146. file))
  1147. (defcustom org-goto-interface 'outline
  1148. "The default interface to be used for `org-goto'.
  1149. Allowed vaues are:
  1150. outline The interface shows an outline of the relevant file
  1151. and the correct heading is found by moving through
  1152. the outline or by searching with incremental search.
  1153. outline-path-completion Headlines in the current buffer are offered via
  1154. completion. This is the interface also used by
  1155. the refile command."
  1156. :group 'org-refile
  1157. :type '(choice
  1158. (const :tag "Outline" outline)
  1159. (const :tag "Outline-path-completion" outline-path-completion)))
  1160. (defcustom org-reverse-note-order nil
  1161. "Non-nil means, store new notes at the beginning of a file or entry.
  1162. When nil, new notes will be filed to the end of a file or entry.
  1163. This can also be a list with cons cells of regular expressions that
  1164. are matched against file names, and values."
  1165. :group 'org-remember
  1166. :group 'org-refile
  1167. :type '(choice
  1168. (const :tag "Reverse always" t)
  1169. (const :tag "Reverse never" nil)
  1170. (repeat :tag "By file name regexp"
  1171. (cons regexp boolean))))
  1172. (defcustom org-refile-targets nil
  1173. "Targets for refiling entries with \\[org-refile].
  1174. This is list of cons cells. Each cell contains:
  1175. - a specification of the files to be considered, either a list of files,
  1176. or a symbol whose function or variable value will be used to retrieve
  1177. a file name or a list of file names. Nil means, refile to a different
  1178. heading in the current buffer.
  1179. - A specification of how to find candidate refile targets. This may be
  1180. any of
  1181. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1182. This tag has to be present in all target headlines, inheritance will
  1183. not be considered.
  1184. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1185. todo keyword.
  1186. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1187. headlines that are refiling targets.
  1188. - a cons cell (:level . N). Any headline of level N is considered a target.
  1189. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1190. When this variable is nil, all top-level headlines in the current buffer
  1191. are used, equivalent to the value `((nil . (:level . 1))'."
  1192. :group 'org-refile
  1193. :type '(repeat
  1194. (cons
  1195. (choice :value org-agenda-files
  1196. (const :tag "All agenda files" org-agenda-files)
  1197. (const :tag "Current buffer" nil)
  1198. (function) (variable) (file))
  1199. (choice :tag "Identify target headline by"
  1200. (cons :tag "Specific tag" (const :value :tag) (string))
  1201. (cons :tag "TODO keyword" (const :value :todo) (string))
  1202. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1203. (cons :tag "Level number" (const :value :level) (integer))
  1204. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1205. (defcustom org-refile-use-outline-path nil
  1206. "Non-nil means, provide refile targets as paths.
  1207. So a level 3 headline will be available as level1/level2/level3.
  1208. When the value is `file', also include the file name (without directory)
  1209. into the path. When `full-file-path', include the full file path."
  1210. :group 'org-refile
  1211. :type '(choice
  1212. (const :tag "Not" nil)
  1213. (const :tag "Yes" t)
  1214. (const :tag "Start with file name" file)
  1215. (const :tag "Start with full file path" full-file-path)))
  1216. (defcustom org-outline-path-complete-in-steps t
  1217. "Non-nil means, complete the outline path in hierarchical steps.
  1218. When Org-mode uses the refile interface to select an outline path
  1219. \(see variable `org-refile-use-outline-path'), the completion of
  1220. the path can be done is a single go, or if can be done in steps down
  1221. the headline hierarchy. Going in steps is probably the best if you
  1222. do not use a special completion package like `ido' or `icicles'.
  1223. However, when using these packages, going in one step can be very
  1224. fast, while still showing the whole path to the entry."
  1225. :group 'org-refile
  1226. :type 'boolean)
  1227. (defgroup org-todo nil
  1228. "Options concerning TODO items in Org-mode."
  1229. :tag "Org TODO"
  1230. :group 'org)
  1231. (defgroup org-progress nil
  1232. "Options concerning Progress logging in Org-mode."
  1233. :tag "Org Progress"
  1234. :group 'org-time)
  1235. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1236. "List of TODO entry keyword sequences and their interpretation.
  1237. \\<org-mode-map>This is a list of sequences.
  1238. Each sequence starts with a symbol, either `sequence' or `type',
  1239. indicating if the keywords should be interpreted as a sequence of
  1240. action steps, or as different types of TODO items. The first
  1241. keywords are states requiring action - these states will select a headline
  1242. for inclusion into the global TODO list Org-mode produces. If one of
  1243. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1244. signify that no further action is necessary. If \"|\" is not found,
  1245. the last keyword is treated as the only DONE state of the sequence.
  1246. The command \\[org-todo] cycles an entry through these states, and one
  1247. additional state where no keyword is present. For details about this
  1248. cycling, see the manual.
  1249. TODO keywords and interpretation can also be set on a per-file basis with
  1250. the special #+SEQ_TODO and #+TYP_TODO lines.
  1251. Each keyword can optionally specify a character for fast state selection
  1252. \(in combination with the variable `org-use-fast-todo-selection')
  1253. and specifiers for state change logging, using the same syntax
  1254. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1255. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1256. indicates to record a time stamp each time this state is selected.
  1257. Each keyword may also specify if a timestamp or a note should be
  1258. recorded when entering or leaving the state, by adding additional
  1259. characters in the parenthesis after the keyword. This looks like this:
  1260. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1261. record only the time of the state change. With X and Y being either
  1262. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1263. Y when leaving the state if and only if the *target* state does not
  1264. define X. You may omit any of the fast-selection key or X or /Y,
  1265. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1266. For backward compatibility, this variable may also be just a list
  1267. of keywords - in this case the interptetation (sequence or type) will be
  1268. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1269. :group 'org-todo
  1270. :group 'org-keywords
  1271. :type '(choice
  1272. (repeat :tag "Old syntax, just keywords"
  1273. (string :tag "Keyword"))
  1274. (repeat :tag "New syntax"
  1275. (cons
  1276. (choice
  1277. :tag "Interpretation"
  1278. (const :tag "Sequence (cycling hits every state)" sequence)
  1279. (const :tag "Type (cycling directly to DONE)" type))
  1280. (repeat
  1281. (string :tag "Keyword"))))))
  1282. (defvar org-todo-keywords-1 nil
  1283. "All TODO and DONE keywords active in a buffer.")
  1284. (make-variable-buffer-local 'org-todo-keywords-1)
  1285. (defvar org-todo-keywords-for-agenda nil)
  1286. (defvar org-done-keywords-for-agenda nil)
  1287. (defvar org-todo-keyword-alist-for-agenda nil)
  1288. (defvar org-tag-alist-for-agenda nil)
  1289. (defvar org-agenda-contributing-files nil)
  1290. (defvar org-not-done-keywords nil)
  1291. (make-variable-buffer-local 'org-not-done-keywords)
  1292. (defvar org-done-keywords nil)
  1293. (make-variable-buffer-local 'org-done-keywords)
  1294. (defvar org-todo-heads nil)
  1295. (make-variable-buffer-local 'org-todo-heads)
  1296. (defvar org-todo-sets nil)
  1297. (make-variable-buffer-local 'org-todo-sets)
  1298. (defvar org-todo-log-states nil)
  1299. (make-variable-buffer-local 'org-todo-log-states)
  1300. (defvar org-todo-kwd-alist nil)
  1301. (make-variable-buffer-local 'org-todo-kwd-alist)
  1302. (defvar org-todo-key-alist nil)
  1303. (make-variable-buffer-local 'org-todo-key-alist)
  1304. (defvar org-todo-key-trigger nil)
  1305. (make-variable-buffer-local 'org-todo-key-trigger)
  1306. (defcustom org-todo-interpretation 'sequence
  1307. "Controls how TODO keywords are interpreted.
  1308. This variable is in principle obsolete and is only used for
  1309. backward compatibility, if the interpretation of todo keywords is
  1310. not given already in `org-todo-keywords'. See that variable for
  1311. more information."
  1312. :group 'org-todo
  1313. :group 'org-keywords
  1314. :type '(choice (const sequence)
  1315. (const type)))
  1316. (defcustom org-use-fast-todo-selection 'prefix
  1317. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1318. This variable describes if and under what circumstances the cycling
  1319. mechanism for TODO keywords will be replaced by a single-key, direct
  1320. selection scheme.
  1321. When nil, fast selection is never used.
  1322. When the symbol `prefix', it will be used when `org-todo' is called with
  1323. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1324. in an agenda buffer.
  1325. When t, fast selection is used by default. In this case, the prefix
  1326. argument forces cycling instead.
  1327. In all cases, the special interface is only used if access keys have actually
  1328. been assigned by the user, i.e. if keywords in the configuration are followed
  1329. by a letter in parenthesis, like TODO(t)."
  1330. :group 'org-todo
  1331. :type '(choice
  1332. (const :tag "Never" nil)
  1333. (const :tag "By default" t)
  1334. (const :tag "Only with C-u C-c C-t" prefix)))
  1335. (defcustom org-provide-todo-statistics t
  1336. "Non-nil means, update todo statistics after insert and toggle.
  1337. When this is set, todo statistics is updated in the parent of the current
  1338. entry each time a todo state is changed."
  1339. :group 'org-todo
  1340. :type 'boolean)
  1341. (defcustom org-after-todo-state-change-hook nil
  1342. "Hook which is run after the state of a TODO item was changed.
  1343. The new state (a string with a TODO keyword, or nil) is available in the
  1344. Lisp variable `state'."
  1345. :group 'org-todo
  1346. :type 'hook)
  1347. (defcustom org-todo-state-tags-triggers nil
  1348. "Tag changes that should be triggered by TODO state changes.
  1349. This is a list. Each entry is
  1350. (state-change (tag . flag) .......)
  1351. State-change can be a string with a state, and empty string to indicate the
  1352. state that has no TODO keyword, or it can be one of the symbols `todo'
  1353. or `done', meaning any not-done or done state, respectively."
  1354. :group 'org-todo
  1355. :group 'org-tags
  1356. :type '(repeat
  1357. (cons (choice :tag "When changing to"
  1358. (const :tag "Not-done state" todo)
  1359. (const :tag "Done state" done)
  1360. (string :tag "State"))
  1361. (repeat
  1362. (cons :tag "Tag action"
  1363. (string :tag "Tag")
  1364. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1365. (defcustom org-log-done nil
  1366. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1367. When equal to the list (done), also prompt for a closing note.
  1368. This can also be configured on a per-file basis by adding one of
  1369. the following lines anywhere in the buffer:
  1370. #+STARTUP: logdone
  1371. #+STARTUP: lognotedone
  1372. #+STARTUP: nologdone"
  1373. :group 'org-todo
  1374. :group 'org-progress
  1375. :type '(choice
  1376. (const :tag "No logging" nil)
  1377. (const :tag "Record CLOSED timestamp" time)
  1378. (const :tag "Record CLOSED timestamp with closing note." note)))
  1379. ;; Normalize old uses of org-log-done.
  1380. (cond
  1381. ((eq org-log-done t) (setq org-log-done 'time))
  1382. ((and (listp org-log-done) (memq 'done org-log-done))
  1383. (setq org-log-done 'note)))
  1384. (defcustom org-log-note-clock-out nil
  1385. "Non-nil means, record a note when clocking out of an item.
  1386. This can also be configured on a per-file basis by adding one of
  1387. the following lines anywhere in the buffer:
  1388. #+STARTUP: lognoteclock-out
  1389. #+STARTUP: nolognoteclock-out"
  1390. :group 'org-todo
  1391. :group 'org-progress
  1392. :type 'boolean)
  1393. (defcustom org-log-done-with-time t
  1394. "Non-nil means, the CLOSED time stamp will contain date and time.
  1395. When nil, only the date will be recorded."
  1396. :group 'org-progress
  1397. :type 'boolean)
  1398. (defcustom org-log-note-headings
  1399. '((done . "CLOSING NOTE %t")
  1400. (state . "State %-12s %t")
  1401. (note . "Note taken on %t")
  1402. (clock-out . ""))
  1403. "Headings for notes added to entries.
  1404. The value is an alist, with the car being a symbol indicating the note
  1405. context, and the cdr is the heading to be used. The heading may also be the
  1406. empty string.
  1407. %t in the heading will be replaced by a time stamp.
  1408. %s will be replaced by the new TODO state, in double quotes.
  1409. %u will be replaced by the user name.
  1410. %U will be replaced by the full user name."
  1411. :group 'org-todo
  1412. :group 'org-progress
  1413. :type '(list :greedy t
  1414. (cons (const :tag "Heading when closing an item" done) string)
  1415. (cons (const :tag
  1416. "Heading when changing todo state (todo sequence only)"
  1417. state) string)
  1418. (cons (const :tag "Heading when just taking a note" note) string)
  1419. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1420. (unless (assq 'note org-log-note-headings)
  1421. (push '(note . "%t") org-log-note-headings))
  1422. (defcustom org-log-state-notes-insert-after-drawers nil
  1423. "Non-nil means, insert state change notes after any drawers in entry.
  1424. Only the drawers that *immediately* follow the headline and the
  1425. deadline/scheduled line are skipped.
  1426. When nil, insert notes right after the heading and perhaps the line
  1427. with deadline/scheduling if present."
  1428. :group 'org-todo
  1429. :group 'org-progress
  1430. :type 'boolean)
  1431. (defcustom org-log-states-order-reversed t
  1432. "Non-nil means, the latest state change note will be directly after heading.
  1433. When nil, the notes will be orderer according to time."
  1434. :group 'org-todo
  1435. :group 'org-progress
  1436. :type 'boolean)
  1437. (defcustom org-log-repeat 'time
  1438. "Non-nil means, record moving through the DONE state when triggering repeat.
  1439. An auto-repeating tasks is immediately switched back to TODO when marked
  1440. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1441. the TODO keyword definition, or recording a closing note by setting
  1442. `org-log-done', there will be no record of the task moving through DONE.
  1443. This variable forces taking a note anyway. Possible values are:
  1444. nil Don't force a record
  1445. time Record a time stamp
  1446. note Record a note
  1447. This option can also be set with on a per-file-basis with
  1448. #+STARTUP: logrepeat
  1449. #+STARTUP: lognoterepeat
  1450. #+STARTUP: nologrepeat
  1451. You can have local logging settings for a subtree by setting the LOGGING
  1452. property to one or more of these keywords."
  1453. :group 'org-todo
  1454. :group 'org-progress
  1455. :type '(choice
  1456. (const :tag "Don't force a record" nil)
  1457. (const :tag "Force recording the DONE state" time)
  1458. (const :tag "Force recording a note with the DONE state" note)))
  1459. (defgroup org-priorities nil
  1460. "Priorities in Org-mode."
  1461. :tag "Org Priorities"
  1462. :group 'org-todo)
  1463. (defcustom org-highest-priority ?A
  1464. "The highest priority of TODO items. A character like ?A, ?B etc.
  1465. Must have a smaller ASCII number than `org-lowest-priority'."
  1466. :group 'org-priorities
  1467. :type 'character)
  1468. (defcustom org-lowest-priority ?C
  1469. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1470. Must have a larger ASCII number than `org-highest-priority'."
  1471. :group 'org-priorities
  1472. :type 'character)
  1473. (defcustom org-default-priority ?B
  1474. "The default priority of TODO items.
  1475. This is the priority an item get if no explicit priority is given."
  1476. :group 'org-priorities
  1477. :type 'character)
  1478. (defcustom org-priority-start-cycle-with-default t
  1479. "Non-nil means, start with default priority when starting to cycle.
  1480. When this is nil, the first step in the cycle will be (depending on the
  1481. command used) one higher or lower that the default priority."
  1482. :group 'org-priorities
  1483. :type 'boolean)
  1484. (defgroup org-time nil
  1485. "Options concerning time stamps and deadlines in Org-mode."
  1486. :tag "Org Time"
  1487. :group 'org)
  1488. (defcustom org-insert-labeled-timestamps-at-point nil
  1489. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1490. When nil, these labeled time stamps are forces into the second line of an
  1491. entry, just after the headline. When scheduling from the global TODO list,
  1492. the time stamp will always be forced into the second line."
  1493. :group 'org-time
  1494. :type 'boolean)
  1495. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1496. "Formats for `format-time-string' which are used for time stamps.
  1497. It is not recommended to change this constant.")
  1498. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1499. "Number of minutes to round time stamps to.
  1500. These are two values, the first applies when first creating a time stamp.
  1501. The second applies when changing it with the commands `S-up' and `S-down'.
  1502. When changing the time stamp, this means that it will change in steps
  1503. of N minutes, as given by the second value.
  1504. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1505. numbers should be factors of 60, so for example 5, 10, 15.
  1506. When this is larger than 1, you can still force an exact time-stamp by using
  1507. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1508. and by using a prefix arg to `S-up/down' to specify the exact number
  1509. of minutes to shift."
  1510. :group 'org-time
  1511. :get '(lambda (var) ; Make sure all entries have 5 elements
  1512. (if (integerp (default-value var))
  1513. (list (default-value var) 5)
  1514. (default-value var)))
  1515. :type '(list
  1516. (integer :tag "when inserting times")
  1517. (integer :tag "when modifying times")))
  1518. ;; Normalize old customizations of this variable.
  1519. (when (integerp org-time-stamp-rounding-minutes)
  1520. (setq org-time-stamp-rounding-minutes
  1521. (list org-time-stamp-rounding-minutes
  1522. org-time-stamp-rounding-minutes)))
  1523. (defcustom org-display-custom-times nil
  1524. "Non-nil means, overlay custom formats over all time stamps.
  1525. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1526. To turn this on on a per-file basis, insert anywhere in the file:
  1527. #+STARTUP: customtime"
  1528. :group 'org-time
  1529. :set 'set-default
  1530. :type 'sexp)
  1531. (make-variable-buffer-local 'org-display-custom-times)
  1532. (defcustom org-time-stamp-custom-formats
  1533. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1534. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1535. These are overlayed over the default ISO format if the variable
  1536. `org-display-custom-times' is set. Time like %H:%M should be at the
  1537. end of the second format."
  1538. :group 'org-time
  1539. :type 'sexp)
  1540. (defun org-time-stamp-format (&optional long inactive)
  1541. "Get the right format for a time string."
  1542. (let ((f (if long (cdr org-time-stamp-formats)
  1543. (car org-time-stamp-formats))))
  1544. (if inactive
  1545. (concat "[" (substring f 1 -1) "]")
  1546. f)))
  1547. (defcustom org-time-clocksum-format "%d:%02d"
  1548. "The format string used when creating CLOCKSUM lines, or when
  1549. org-mode generates a time duration."
  1550. :group 'org-time
  1551. :type 'string)
  1552. (defcustom org-deadline-warning-days 14
  1553. "No. of days before expiration during which a deadline becomes active.
  1554. This variable governs the display in sparse trees and in the agenda.
  1555. When 0 or negative, it means use this number (the absolute value of it)
  1556. even if a deadline has a different individual lead time specified."
  1557. :group 'org-time
  1558. :group 'org-agenda-daily/weekly
  1559. :type 'number)
  1560. (defcustom org-read-date-prefer-future t
  1561. "Non-nil means, assume future for incomplete date input from user.
  1562. This affects the following situations:
  1563. 1. The user gives a day, but no month.
  1564. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1565. read this as the third of *next* month. However, if you enter \"17\",
  1566. it will be considered as *this* month.
  1567. 2. The user gives a month but not a year.
  1568. For example, if it is april and you enter \"feb 2\", this will be read
  1569. as feb 2, *next* year. \"May 5\", however, will be this year.
  1570. Currently this does not work for ISO week specifications.
  1571. When this option is nil, the current month and year will always be used
  1572. as defaults."
  1573. :group 'org-time
  1574. :type 'boolean)
  1575. (defcustom org-read-date-display-live t
  1576. "Non-nil means, display current interpretation of date prompt live.
  1577. This display will be in an overlay, in the minibuffer."
  1578. :group 'org-time
  1579. :type 'boolean)
  1580. (defcustom org-read-date-popup-calendar t
  1581. "Non-nil means, pop up a calendar when prompting for a date.
  1582. In the calendar, the date can be selected with mouse-1. However, the
  1583. minibuffer will also be active, and you can simply enter the date as well.
  1584. When nil, only the minibuffer will be available."
  1585. :group 'org-time
  1586. :type 'boolean)
  1587. (if (fboundp 'defvaralias)
  1588. (defvaralias 'org-popup-calendar-for-date-prompt
  1589. 'org-read-date-popup-calendar))
  1590. (defcustom org-extend-today-until 0
  1591. "The hour when your day really ends. Must be an integer.
  1592. This has influence for the following applications:
  1593. - When switching the agenda to \"today\". It it is still earlier than
  1594. the time given here, the day recognized as TODAY is actually yesterday.
  1595. - When a date is read from the user and it is still before the time given
  1596. here, the current date and time will be assumed to be yesterday, 23:59.
  1597. Also, timestamps inserted in remember templates follow this rule.
  1598. IMPORTANT: This is a feature whose implementation is and likely will
  1599. remain incomplete. Really, it is only here because past midnight seems to
  1600. be the favorite working time of John Wiegley :-)"
  1601. :group 'org-time
  1602. :type 'number)
  1603. (defcustom org-edit-timestamp-down-means-later nil
  1604. "Non-nil means, S-down will increase the time in a time stamp.
  1605. When nil, S-up will increase."
  1606. :group 'org-time
  1607. :type 'boolean)
  1608. (defcustom org-calendar-follow-timestamp-change t
  1609. "Non-nil means, make the calendar window follow timestamp changes.
  1610. When a timestamp is modified and the calendar window is visible, it will be
  1611. moved to the new date."
  1612. :group 'org-time
  1613. :type 'boolean)
  1614. (defgroup org-tags nil
  1615. "Options concerning tags in Org-mode."
  1616. :tag "Org Tags"
  1617. :group 'org)
  1618. (defcustom org-tag-alist nil
  1619. "List of tags allowed in Org-mode files.
  1620. When this list is nil, Org-mode will base TAG input on what is already in the
  1621. buffer.
  1622. The value of this variable is an alist, the car of each entry must be a
  1623. keyword as a string, the cdr may be a character that is used to select
  1624. that tag through the fast-tag-selection interface.
  1625. See the manual for details."
  1626. :group 'org-tags
  1627. :type '(repeat
  1628. (choice
  1629. (cons (string :tag "Tag name")
  1630. (character :tag "Access char"))
  1631. (const :tag "Start radio group" (:startgroup))
  1632. (const :tag "End radio group" (:endgroup)))))
  1633. (defvar org-file-tags nil
  1634. "List of tags that can be inherited by all entries in the file.
  1635. The tags will be inherited if the variable `org-use-tag-inheritance'
  1636. says they should be.
  1637. This variable is populated from #+TAG lines.")
  1638. (defcustom org-use-fast-tag-selection 'auto
  1639. "Non-nil means, use fast tag selection scheme.
  1640. This is a special interface to select and deselect tags with single keys.
  1641. When nil, fast selection is never used.
  1642. When the symbol `auto', fast selection is used if and only if selection
  1643. characters for tags have been configured, either through the variable
  1644. `org-tag-alist' or through a #+TAGS line in the buffer.
  1645. When t, fast selection is always used and selection keys are assigned
  1646. automatically if necessary."
  1647. :group 'org-tags
  1648. :type '(choice
  1649. (const :tag "Always" t)
  1650. (const :tag "Never" nil)
  1651. (const :tag "When selection characters are configured" 'auto)))
  1652. (defcustom org-fast-tag-selection-single-key nil
  1653. "Non-nil means, fast tag selection exits after first change.
  1654. When nil, you have to press RET to exit it.
  1655. During fast tag selection, you can toggle this flag with `C-c'.
  1656. This variable can also have the value `expert'. In this case, the window
  1657. displaying the tags menu is not even shown, until you press C-c again."
  1658. :group 'org-tags
  1659. :type '(choice
  1660. (const :tag "No" nil)
  1661. (const :tag "Yes" t)
  1662. (const :tag "Expert" expert)))
  1663. (defvar org-fast-tag-selection-include-todo nil
  1664. "Non-nil means, fast tags selection interface will also offer TODO states.
  1665. This is an undocumented feature, you should not rely on it.")
  1666. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1667. "The column to which tags should be indented in a headline.
  1668. If this number is positive, it specifies the column. If it is negative,
  1669. it means that the tags should be flushright to that column. For example,
  1670. -80 works well for a normal 80 character screen."
  1671. :group 'org-tags
  1672. :type 'integer)
  1673. (defcustom org-auto-align-tags t
  1674. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1675. These operations change the length of a headline and therefore shift
  1676. the tags around. With this options turned on, after each such operation
  1677. the tags are again aligned to `org-tags-column'."
  1678. :group 'org-tags
  1679. :type 'boolean)
  1680. (defcustom org-use-tag-inheritance t
  1681. "Non-nil means, tags in levels apply also for sublevels.
  1682. When nil, only the tags directly given in a specific line apply there.
  1683. This may also be a list of tags that should be inherited, or a regexp that
  1684. matches tags that should be inherited. Additional control is possible
  1685. with the variable `org-tags-exclude-from-inheritance' which gives an
  1686. explicit list of tags to be excluded from inheritance., even if the value of
  1687. `org-use-tag-inheritance' would select it for inheritance.
  1688. If this option is t, a match early-on in a tree can lead to a large
  1689. number of matches in the subtree when constructing the agenda or creating
  1690. a sparse tree. If you only want to see the first match in a tree during
  1691. a search, check out the variable `org-tags-match-list-sublevels'."
  1692. :group 'org-tags
  1693. :type '(choice
  1694. (const :tag "Not" nil)
  1695. (const :tag "Always" t)
  1696. (repeat :tag "Specific tags" (string :tag "Tag"))
  1697. (regexp :tag "Tags matched by regexp")))
  1698. (defcustom org-tags-exclude-from-inheritance nil
  1699. "List of tags that should never be inherited.
  1700. This is a way to exclude a few tags from inheritance. For way to do
  1701. the opposite, to actively allow inheritance for selected tags,
  1702. see the variable `org-use-tag-inheritance'."
  1703. :group 'org-tags
  1704. :type '(repeat (string :tag "Tag")))
  1705. (defun org-tag-inherit-p (tag)
  1706. "Check if TAG is one that should be inherited."
  1707. (cond
  1708. ((member tag org-tags-exclude-from-inheritance) nil)
  1709. ((eq org-use-tag-inheritance t) t)
  1710. ((not org-use-tag-inheritance) nil)
  1711. ((stringp org-use-tag-inheritance)
  1712. (string-match org-use-tag-inheritance tag))
  1713. ((listp org-use-tag-inheritance)
  1714. (member tag org-use-tag-inheritance))
  1715. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1716. (defcustom org-tags-match-list-sublevels t
  1717. "Non-nil means list also sublevels of headlines matching tag search.
  1718. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1719. the sublevels of a headline matching a tag search often also match
  1720. the same search. Listing all of them can create very long lists.
  1721. Setting this variable to nil causes subtrees of a match to be skipped.
  1722. This option is off by default, because inheritance in on. If you turn
  1723. inheritance off, you very likely want to turn this option on.
  1724. As a special case, if the tag search is restricted to TODO items, the
  1725. value of this variable is ignored and sublevels are always checked, to
  1726. make sure all corresponding TODO items find their way into the list.
  1727. This variable is semi-obsolete and probably should always be true. It
  1728. is better to limit inheritance to certain tags using the variables
  1729. `org-use-tag-inheritanc'e and `org-tags-exclude-from-inheritance'."
  1730. :group 'org-tags
  1731. :type 'boolean)
  1732. (defvar org-tags-history nil
  1733. "History of minibuffer reads for tags.")
  1734. (defvar org-last-tags-completion-table nil
  1735. "The last used completion table for tags.")
  1736. (defvar org-after-tags-change-hook nil
  1737. "Hook that is run after the tags in a line have changed.")
  1738. (defgroup org-properties nil
  1739. "Options concerning properties in Org-mode."
  1740. :tag "Org Properties"
  1741. :group 'org)
  1742. (defcustom org-property-format "%-10s %s"
  1743. "How property key/value pairs should be formatted by `indent-line'.
  1744. When `indent-line' hits a property definition, it will format the line
  1745. according to this format, mainly to make sure that the values are
  1746. lined-up with respect to each other."
  1747. :group 'org-properties
  1748. :type 'string)
  1749. (defcustom org-use-property-inheritance nil
  1750. "Non-nil means, properties apply also for sublevels.
  1751. This setting is chiefly used during property searches. Turning it on can
  1752. cause significant overhead when doing a search, which is why it is not
  1753. on by default.
  1754. When nil, only the properties directly given in the current entry count.
  1755. When t, every property is inherited. The value may also be a list of
  1756. properties that should have inheritance, or a regular expression matching
  1757. properties that should be inherited.
  1758. However, note that some special properties use inheritance under special
  1759. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1760. and the properties ending in \"_ALL\" when they are used as descriptor
  1761. for valid values of a property.
  1762. Note for programmers:
  1763. When querying an entry with `org-entry-get', you can control if inheritance
  1764. should be used. By default, `org-entry-get' looks only at the local
  1765. properties. You can request inheritance by setting the inherit argument
  1766. to t (to force inheritance) or to `selective' (to respect the setting
  1767. in this variable)."
  1768. :group 'org-properties
  1769. :type '(choice
  1770. (const :tag "Not" nil)
  1771. (const :tag "Always" t)
  1772. (repeat :tag "Specific properties" (string :tag "Property"))
  1773. (regexp :tag "Properties matched by regexp")))
  1774. (defun org-property-inherit-p (property)
  1775. "Check if PROPERTY is one that should be inherited."
  1776. (cond
  1777. ((eq org-use-property-inheritance t) t)
  1778. ((not org-use-property-inheritance) nil)
  1779. ((stringp org-use-property-inheritance)
  1780. (string-match org-use-property-inheritance property))
  1781. ((listp org-use-property-inheritance)
  1782. (member property org-use-property-inheritance))
  1783. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1784. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1785. "The default column format, if no other format has been defined.
  1786. This variable can be set on the per-file basis by inserting a line
  1787. #+COLUMNS: %25ITEM ....."
  1788. :group 'org-properties
  1789. :type 'string)
  1790. (defcustom org-columns-ellipses ".."
  1791. "The ellipses to be used when a field in column view is truncated.
  1792. When this is the empty string, as many characters as possible are shown,
  1793. but then there will be no visual indication that the field has been truncated.
  1794. When this is a string of length N, the last N characters of a truncated
  1795. field are replaced by this string. If the column is narrower than the
  1796. ellipses string, only part of the ellipses string will be shown."
  1797. :group 'org-properties
  1798. :type 'string)
  1799. (defcustom org-columns-modify-value-for-display-function nil
  1800. "Function that modifies values for display in column view.
  1801. For example, it can be used to cut out a certain part from a time stamp.
  1802. The function must take 2 arguments:
  1803. column-title The tite of the column (*not* the property name)
  1804. value The value that should be modified.
  1805. The function should return the value that should be displayed,
  1806. or nil if the normal value should be used."
  1807. :group 'org-properties
  1808. :type 'function)
  1809. (defcustom org-effort-property "Effort"
  1810. "The property that is being used to keep track of effort estimates.
  1811. Effort estimates given in this property need to have the format H:MM."
  1812. :group 'org-properties
  1813. :group 'org-progress
  1814. :type '(string :tag "Property"))
  1815. (defconst org-global-properties-fixed
  1816. '(("VISIBILITY_ALL" . "folded children content all"))
  1817. "List of property/value pairs that can be inherited by any entry.
  1818. These are fixed values, for the preset properties.")
  1819. (defcustom org-global-properties nil
  1820. "List of property/value pairs that can be inherited by any entry.
  1821. You can set buffer-local values for the same purpose in the variable
  1822. `org-file-properties' this by adding lines like
  1823. #+PROPERTY: NAME VALUE"
  1824. :group 'org-properties
  1825. :type '(repeat
  1826. (cons (string :tag "Property")
  1827. (string :tag "Value"))))
  1828. (defvar org-file-properties nil
  1829. "List of property/value pairs that can be inherited by any entry.
  1830. Valid for the current buffer.
  1831. This variable is populated from #+PROPERTY lines.")
  1832. (make-variable-buffer-local 'org-file-properties)
  1833. (defgroup org-agenda nil
  1834. "Options concerning agenda views in Org-mode."
  1835. :tag "Org Agenda"
  1836. :group 'org)
  1837. (defvar org-category nil
  1838. "Variable used by org files to set a category for agenda display.
  1839. Such files should use a file variable to set it, for example
  1840. # -*- mode: org; org-category: \"ELisp\"
  1841. or contain a special line
  1842. #+CATEGORY: ELisp
  1843. If the file does not specify a category, then file's base name
  1844. is used instead.")
  1845. (make-variable-buffer-local 'org-category)
  1846. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1847. (defcustom org-agenda-files nil
  1848. "The files to be used for agenda display.
  1849. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1850. \\[org-remove-file]. You can also use customize to edit the list.
  1851. If an entry is a directory, all files in that directory that are matched by
  1852. `org-agenda-file-regexp' will be part of the file list.
  1853. If the value of the variable is not a list but a single file name, then
  1854. the list of agenda files is actually stored and maintained in that file, one
  1855. agenda file per line."
  1856. :group 'org-agenda
  1857. :type '(choice
  1858. (repeat :tag "List of files and directories" file)
  1859. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1860. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1861. "Regular expression to match files for `org-agenda-files'.
  1862. If any element in the list in that variable contains a directory instead
  1863. of a normal file, all files in that directory that are matched by this
  1864. regular expression will be included."
  1865. :group 'org-agenda
  1866. :type 'regexp)
  1867. (defcustom org-agenda-text-search-extra-files nil
  1868. "List of extra files to be searched by text search commands.
  1869. These files will be search in addition to the agenda files by the
  1870. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1871. Note that these files will only be searched for text search commands,
  1872. not for the other agenda views like todo lists, tag searches or the weekly
  1873. agenda. This variable is intended to list notes and possibly archive files
  1874. that should also be searched by these two commands.
  1875. In fact, if the first element in the list is the symbol `agenda-archives',
  1876. than all archive files of all agenda files will be added to the search
  1877. scope."
  1878. :group 'org-agenda
  1879. :type '(set :greedy t
  1880. (const :tag "Agenda Archives" agenda-archives)
  1881. (repeat :inline t (file))))
  1882. (if (fboundp 'defvaralias)
  1883. (defvaralias 'org-agenda-multi-occur-extra-files
  1884. 'org-agenda-text-search-extra-files))
  1885. (defcustom org-agenda-skip-unavailable-files nil
  1886. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1887. A nil value means to remove them, after a query, from the list."
  1888. :group 'org-agenda
  1889. :type 'boolean)
  1890. (defcustom org-calendar-to-agenda-key [?c]
  1891. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1892. The command `org-calendar-goto-agenda' will be bound to this key. The
  1893. default is the character `c' because then `c' can be used to switch back and
  1894. forth between agenda and calendar."
  1895. :group 'org-agenda
  1896. :type 'sexp)
  1897. (defcustom org-calendar-agenda-action-key [?k]
  1898. "The key to be installed in `calendar-mode-map' for agenda-action.
  1899. The command `org-agenda-action' will be bound to this key. The
  1900. default is the character `k' because we use the same key in the agenda."
  1901. :group 'org-agenda
  1902. :type 'sexp)
  1903. (eval-after-load "calendar"
  1904. '(progn
  1905. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1906. 'org-calendar-goto-agenda)
  1907. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1908. 'org-agenda-action)))
  1909. (defgroup org-latex nil
  1910. "Options for embedding LaTeX code into Org-mode."
  1911. :tag "Org LaTeX"
  1912. :group 'org)
  1913. (defcustom org-format-latex-options
  1914. '(:foreground default :background default :scale 1.0
  1915. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1916. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1917. "Options for creating images from LaTeX fragments.
  1918. This is a property list with the following properties:
  1919. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1920. `default' means use the foreground of the default face.
  1921. :background the background color, or \"Transparent\".
  1922. `default' means use the background of the default face.
  1923. :scale a scaling factor for the size of the images.
  1924. :html-foreground, :html-background, :html-scale
  1925. the same numbers for HTML export.
  1926. :matchers a list indicating which matchers should be used to
  1927. find LaTeX fragments. Valid members of this list are:
  1928. \"begin\" find environments
  1929. \"$\" find math expressions surrounded by $...$
  1930. \"$$\" find math expressions surrounded by $$....$$
  1931. \"\\(\" find math expressions surrounded by \\(...\\)
  1932. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1933. :group 'org-latex
  1934. :type 'plist)
  1935. (defcustom org-format-latex-header "\\documentclass{article}
  1936. \\usepackage{fullpage} % do not remove
  1937. \\usepackage{amssymb}
  1938. \\usepackage[usenames]{color}
  1939. \\usepackage{amsmath}
  1940. \\usepackage{latexsym}
  1941. \\usepackage[mathscr]{eucal}
  1942. \\pagestyle{empty} % do not remove"
  1943. "The document header used for processing LaTeX fragments."
  1944. :group 'org-latex
  1945. :type 'string)
  1946. (defgroup org-font-lock nil
  1947. "Font-lock settings for highlighting in Org-mode."
  1948. :tag "Org Font Lock"
  1949. :group 'org)
  1950. (defcustom org-level-color-stars-only nil
  1951. "Non-nil means fontify only the stars in each headline.
  1952. When nil, the entire headline is fontified.
  1953. Changing it requires restart of `font-lock-mode' to become effective
  1954. also in regions already fontified."
  1955. :group 'org-font-lock
  1956. :type 'boolean)
  1957. (defcustom org-hide-leading-stars nil
  1958. "Non-nil means, hide the first N-1 stars in a headline.
  1959. This works by using the face `org-hide' for these stars. This
  1960. face is white for a light background, and black for a dark
  1961. background. You may have to customize the face `org-hide' to
  1962. make this work.
  1963. Changing it requires restart of `font-lock-mode' to become effective
  1964. also in regions already fontified.
  1965. You may also set this on a per-file basis by adding one of the following
  1966. lines to the buffer:
  1967. #+STARTUP: hidestars
  1968. #+STARTUP: showstars"
  1969. :group 'org-font-lock
  1970. :type 'boolean)
  1971. (defcustom org-fontify-done-headline nil
  1972. "Non-nil means, change the face of a headline if it is marked DONE.
  1973. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1974. When this is non-nil, the headline after the keyword is set to the
  1975. `org-headline-done' as an additional indication."
  1976. :group 'org-font-lock
  1977. :type 'boolean)
  1978. (defcustom org-fontify-emphasized-text t
  1979. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1980. Changing this variable requires a restart of Emacs to take effect."
  1981. :group 'org-font-lock
  1982. :type 'boolean)
  1983. (defcustom org-highlight-latex-fragments-and-specials nil
  1984. "Non-nil means, fontify what is treated specially by the exporters."
  1985. :group 'org-font-lock
  1986. :type 'boolean)
  1987. (defcustom org-hide-emphasis-markers nil
  1988. "Non-nil mean font-lock should hide the emphasis marker characters."
  1989. :group 'org-font-lock
  1990. :type 'boolean)
  1991. (defvar org-emph-re nil
  1992. "Regular expression for matching emphasis.")
  1993. (defvar org-verbatim-re nil
  1994. "Regular expression for matching verbatim text.")
  1995. (defvar org-emphasis-regexp-components) ; defined just below
  1996. (defvar org-emphasis-alist) ; defined just below
  1997. (defun org-set-emph-re (var val)
  1998. "Set variable and compute the emphasis regular expression."
  1999. (set var val)
  2000. (when (and (boundp 'org-emphasis-alist)
  2001. (boundp 'org-emphasis-regexp-components)
  2002. org-emphasis-alist org-emphasis-regexp-components)
  2003. (let* ((e org-emphasis-regexp-components)
  2004. (pre (car e))
  2005. (post (nth 1 e))
  2006. (border (nth 2 e))
  2007. (body (nth 3 e))
  2008. (nl (nth 4 e))
  2009. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  2010. (body1 (concat body "*?"))
  2011. (markers (mapconcat 'car org-emphasis-alist ""))
  2012. (vmarkers (mapconcat
  2013. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  2014. org-emphasis-alist "")))
  2015. ;; make sure special characters appear at the right position in the class
  2016. (if (string-match "\\^" markers)
  2017. (setq markers (concat (replace-match "" t t markers) "^")))
  2018. (if (string-match "-" markers)
  2019. (setq markers (concat (replace-match "" t t markers) "-")))
  2020. (if (string-match "\\^" vmarkers)
  2021. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  2022. (if (string-match "-" vmarkers)
  2023. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  2024. (if (> nl 0)
  2025. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  2026. (int-to-string nl) "\\}")))
  2027. ;; Make the regexp
  2028. (setq org-emph-re
  2029. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  2030. "\\("
  2031. "\\([" markers "]\\)"
  2032. "\\("
  2033. "[^" border "]\\|"
  2034. "[^" border (if (and nil stacked) markers) "]"
  2035. body1
  2036. "[^" border (if (and nil stacked) markers) "]"
  2037. "\\)"
  2038. "\\3\\)"
  2039. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  2040. (setq org-verbatim-re
  2041. (concat "\\([" pre "]\\|^\\)"
  2042. "\\("
  2043. "\\([" vmarkers "]\\)"
  2044. "\\("
  2045. "[^" border "]\\|"
  2046. "[^" border "]"
  2047. body1
  2048. "[^" border "]"
  2049. "\\)"
  2050. "\\3\\)"
  2051. "\\([" post "]\\|$\\)")))))
  2052. (defcustom org-emphasis-regexp-components
  2053. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2054. "Components used to build the regular expression for emphasis.
  2055. This is a list with 6 entries. Terminology: In an emphasis string
  2056. like \" *strong word* \", we call the initial space PREMATCH, the final
  2057. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2058. and \"trong wor\" is the body. The different components in this variable
  2059. specify what is allowed/forbidden in each part:
  2060. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2061. post Chars allowed as postmatch. End of line will be allowed too.
  2062. border The chars *forbidden* as border characters.
  2063. body-regexp A regexp like \".\" to match a body character. Don't use
  2064. non-shy groups here, and don't allow newline here.
  2065. newline The maximum number of newlines allowed in an emphasis exp.
  2066. Use customize to modify this, or restart Emacs after changing it."
  2067. :group 'org-font-lock
  2068. :set 'org-set-emph-re
  2069. :type '(list
  2070. (sexp :tag "Allowed chars in pre ")
  2071. (sexp :tag "Allowed chars in post ")
  2072. (sexp :tag "Forbidden chars in border ")
  2073. (sexp :tag "Regexp for body ")
  2074. (integer :tag "number of newlines allowed")
  2075. (option (boolean :tag "Please ignore this button"))))
  2076. (defcustom org-emphasis-alist
  2077. `(("*" bold "<b>" "</b>")
  2078. ("/" italic "<i>" "</i>")
  2079. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2080. ("=" org-code "<code>" "</code>" verbatim)
  2081. ("~" org-verbatim "<code>" "</code>" verbatim)
  2082. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2083. "<del>" "</del>")
  2084. )
  2085. "Special syntax for emphasized text.
  2086. Text starting and ending with a special character will be emphasized, for
  2087. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2088. characters, the face to be used by font-lock for highlighting in Org-mode
  2089. Emacs buffers, and the HTML tags to be used for this.
  2090. Use customize to modify this, or restart Emacs after changing it."
  2091. :group 'org-font-lock
  2092. :set 'org-set-emph-re
  2093. :type '(repeat
  2094. (list
  2095. (string :tag "Marker character")
  2096. (choice
  2097. (face :tag "Font-lock-face")
  2098. (plist :tag "Face property list"))
  2099. (string :tag "HTML start tag")
  2100. (string :tag "HTML end tag")
  2101. (option (const verbatim)))))
  2102. ;;; Miscellaneous options
  2103. (defgroup org-completion nil
  2104. "Completion in Org-mode."
  2105. :tag "Org Completion"
  2106. :group 'org)
  2107. (defcustom org-completion-use-ido nil
  2108. "Non-nil means, use ido completion wherever possible."
  2109. :group 'org-completion
  2110. :type 'boolean)
  2111. (defcustom org-completion-fallback-command 'hippie-expand
  2112. "The expansion command called by \\[org-complete] in normal context.
  2113. Normal means, no org-mode-specific context."
  2114. :group 'org-completion
  2115. :type 'function)
  2116. ;;; Functions and variables from ther packages
  2117. ;; Declared here to avoid compiler warnings
  2118. ;; XEmacs only
  2119. (defvar outline-mode-menu-heading)
  2120. (defvar outline-mode-menu-show)
  2121. (defvar outline-mode-menu-hide)
  2122. (defvar zmacs-regions) ; XEmacs regions
  2123. ;; Emacs only
  2124. (defvar mark-active)
  2125. ;; Various packages
  2126. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2127. (declare-function calendar-forward-day "cal-move" (arg))
  2128. (declare-function calendar-goto-date "cal-move" (date))
  2129. (declare-function calendar-goto-today "cal-move" ())
  2130. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2131. (defvar calc-embedded-close-formula)
  2132. (defvar calc-embedded-open-formula)
  2133. (declare-function cdlatex-tab "ext:cdlatex" ())
  2134. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2135. (defvar font-lock-unfontify-region-function)
  2136. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2137. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2138. (defvar iswitchb-temp-buflist)
  2139. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2140. (declare-function org-agenda-skip "org-agenda" ())
  2141. (declare-function org-format-agenda-item "org-agenda"
  2142. (extra txt &optional category tags dotime noprefix remove-re))
  2143. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2144. (declare-function org-agenda-change-all-lines "org-agenda"
  2145. (newhead hdmarker &optional fixface just-this))
  2146. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2147. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2148. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2149. (beg end))
  2150. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2151. (declare-function parse-time-string "parse-time" (string))
  2152. (declare-function remember "remember" (&optional initial))
  2153. (declare-function remember-buffer-desc "remember" ())
  2154. (declare-function remember-finalize "remember" ())
  2155. (defvar remember-save-after-remembering)
  2156. (defvar remember-data-file)
  2157. (defvar remember-register)
  2158. (defvar remember-buffer)
  2159. (defvar remember-handler-functions)
  2160. (defvar remember-annotation-functions)
  2161. (defvar texmathp-why)
  2162. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2163. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2164. (defvar w3m-current-url)
  2165. (defvar w3m-current-title)
  2166. (defvar org-latex-regexps)
  2167. ;;; Autoload and prepare some org modules
  2168. ;; Some table stuff that needs to be defined here, because it is used
  2169. ;; by the functions setting up org-mode or checking for table context.
  2170. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2171. "Detects an org-type or table-type table.")
  2172. (defconst org-table-line-regexp "^[ \t]*|"
  2173. "Detects an org-type table line.")
  2174. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2175. "Detects an org-type table line.")
  2176. (defconst org-table-hline-regexp "^[ \t]*|-"
  2177. "Detects an org-type table hline.")
  2178. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2179. "Detects a table-type table hline.")
  2180. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2181. "Searching from within a table (any type) this finds the first line
  2182. outside the table.")
  2183. ;; Autoload the functions in org-table.el that are needed by functions here.
  2184. (eval-and-compile
  2185. (org-autoload "org-table"
  2186. '(org-table-align org-table-begin org-table-blank-field
  2187. org-table-convert org-table-convert-region org-table-copy-down
  2188. org-table-copy-region org-table-create
  2189. org-table-create-or-convert-from-region
  2190. org-table-create-with-table.el org-table-current-dline
  2191. org-table-cut-region org-table-delete-column org-table-edit-field
  2192. org-table-edit-formulas org-table-end org-table-eval-formula
  2193. org-table-export org-table-field-info
  2194. org-table-get-stored-formulas org-table-goto-column
  2195. org-table-hline-and-move org-table-import org-table-insert-column
  2196. org-table-insert-hline org-table-insert-row org-table-iterate
  2197. org-table-justify-field-maybe org-table-kill-row
  2198. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2199. org-table-move-column org-table-move-column-left
  2200. org-table-move-column-right org-table-move-row
  2201. org-table-move-row-down org-table-move-row-up
  2202. org-table-next-field org-table-next-row org-table-paste-rectangle
  2203. org-table-previous-field org-table-recalculate
  2204. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2205. org-table-toggle-coordinate-overlays
  2206. org-table-toggle-formula-debugger org-table-wrap-region
  2207. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2208. (defun org-at-table-p (&optional table-type)
  2209. "Return t if the cursor is inside an org-type table.
  2210. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2211. (if org-enable-table-editor
  2212. (save-excursion
  2213. (beginning-of-line 1)
  2214. (looking-at (if table-type org-table-any-line-regexp
  2215. org-table-line-regexp)))
  2216. nil))
  2217. (defsubst org-table-p () (org-at-table-p))
  2218. (defun org-at-table.el-p ()
  2219. "Return t if and only if we are at a table.el table."
  2220. (and (org-at-table-p 'any)
  2221. (save-excursion
  2222. (goto-char (org-table-begin 'any))
  2223. (looking-at org-table1-hline-regexp))))
  2224. (defun org-table-recognize-table.el ()
  2225. "If there is a table.el table nearby, recognize it and move into it."
  2226. (if org-table-tab-recognizes-table.el
  2227. (if (org-at-table.el-p)
  2228. (progn
  2229. (beginning-of-line 1)
  2230. (if (looking-at org-table-dataline-regexp)
  2231. nil
  2232. (if (looking-at org-table1-hline-regexp)
  2233. (progn
  2234. (beginning-of-line 2)
  2235. (if (looking-at org-table-any-border-regexp)
  2236. (beginning-of-line -1)))))
  2237. (if (re-search-forward "|" (org-table-end t) t)
  2238. (progn
  2239. (require 'table)
  2240. (if (table--at-cell-p (point))
  2241. t
  2242. (message "recognizing table.el table...")
  2243. (table-recognize-table)
  2244. (message "recognizing table.el table...done")))
  2245. (error "This should not happen..."))
  2246. t)
  2247. nil)
  2248. nil))
  2249. (defun org-at-table-hline-p ()
  2250. "Return t if the cursor is inside a hline in a table."
  2251. (if org-enable-table-editor
  2252. (save-excursion
  2253. (beginning-of-line 1)
  2254. (looking-at org-table-hline-regexp))
  2255. nil))
  2256. (defvar org-table-clean-did-remove-column nil)
  2257. (defun org-table-map-tables (function)
  2258. "Apply FUNCTION to the start of all tables in the buffer."
  2259. (save-excursion
  2260. (save-restriction
  2261. (widen)
  2262. (goto-char (point-min))
  2263. (while (re-search-forward org-table-any-line-regexp nil t)
  2264. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2265. (beginning-of-line 1)
  2266. (if (looking-at org-table-line-regexp)
  2267. (save-excursion (funcall function)))
  2268. (re-search-forward org-table-any-border-regexp nil 1))))
  2269. (message "Mapping tables: done"))
  2270. ;; Declare and autoload functions from org-exp.el
  2271. (declare-function org-default-export-plist "org-exp")
  2272. (declare-function org-infile-export-plist "org-exp")
  2273. (declare-function org-get-current-options "org-exp")
  2274. (eval-and-compile
  2275. (org-autoload "org-exp"
  2276. '(org-export org-export-as-ascii org-export-visible
  2277. org-insert-export-options-template org-export-as-html-and-open
  2278. org-export-as-html-batch org-export-as-html-to-buffer
  2279. org-replace-region-by-html org-export-region-as-html
  2280. org-export-as-html org-export-icalendar-this-file
  2281. org-export-icalendar-all-agenda-files
  2282. org-table-clean-before-export
  2283. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2284. ;; Declare and autoload functions from org-agenda.el
  2285. (eval-and-compile
  2286. (org-autoload "org-agenda"
  2287. '(org-agenda org-agenda-list org-search-view
  2288. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2289. org-diary org-agenda-to-appt)))
  2290. ;; Autoload org-remember
  2291. (eval-and-compile
  2292. (org-autoload "org-remember"
  2293. '(org-remember-insinuate org-remember-annotation
  2294. org-remember-apply-template org-remember org-remember-handler)))
  2295. ;; Autoload org-clock.el
  2296. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2297. (beg end))
  2298. (declare-function org-update-mode-line "org-clock" ())
  2299. (defvar org-clock-start-time)
  2300. (defvar org-clock-marker (make-marker)
  2301. "Marker recording the last clock-in.")
  2302. (eval-and-compile
  2303. (org-autoload
  2304. "org-clock"
  2305. '(org-clock-in org-clock-out org-clock-cancel
  2306. org-clock-goto org-clock-sum org-clock-display
  2307. org-remove-clock-overlays org-clock-report
  2308. org-clocktable-shift org-dblock-write:clocktable
  2309. org-get-clocktable)))
  2310. (defun org-clock-update-time-maybe ()
  2311. "If this is a CLOCK line, update it and return t.
  2312. Otherwise, return nil."
  2313. (interactive)
  2314. (save-excursion
  2315. (beginning-of-line 1)
  2316. (skip-chars-forward " \t")
  2317. (when (looking-at org-clock-string)
  2318. (let ((re (concat "[ \t]*" org-clock-string
  2319. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2320. "\\([ \t]*=>.*\\)?\\)?"))
  2321. ts te h m s neg)
  2322. (cond
  2323. ((not (looking-at re))
  2324. nil)
  2325. ((not (match-end 2))
  2326. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2327. (> org-clock-marker (point))
  2328. (<= org-clock-marker (point-at-eol)))
  2329. ;; The clock is running here
  2330. (setq org-clock-start-time
  2331. (apply 'encode-time
  2332. (org-parse-time-string (match-string 1))))
  2333. (org-update-mode-line)))
  2334. (t
  2335. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2336. (end-of-line 1)
  2337. (setq ts (match-string 1)
  2338. te (match-string 3))
  2339. (setq s (- (time-to-seconds
  2340. (apply 'encode-time (org-parse-time-string te)))
  2341. (time-to-seconds
  2342. (apply 'encode-time (org-parse-time-string ts))))
  2343. neg (< s 0)
  2344. s (abs s)
  2345. h (floor (/ s 3600))
  2346. s (- s (* 3600 h))
  2347. m (floor (/ s 60))
  2348. s (- s (* 60 s)))
  2349. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2350. t))))))
  2351. (defun org-check-running-clock ()
  2352. "Check if the current buffer contains the running clock.
  2353. If yes, offer to stop it and to save the buffer with the changes."
  2354. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2355. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2356. (buffer-name))))
  2357. (org-clock-out)
  2358. (when (y-or-n-p "Save changed buffer?")
  2359. (save-buffer))))
  2360. (defun org-clocktable-try-shift (dir n)
  2361. "Check if this line starts a clock table, if yes, shift the time block."
  2362. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2363. (org-clocktable-shift dir n)))
  2364. ;; Autoload org-timer.el
  2365. ;(declare-function org-timer "org-timer")
  2366. (eval-and-compile
  2367. (org-autoload
  2368. "org-timer"
  2369. '(org-timer-start org-timer org-timer-item
  2370. org-timer-change-times-in-region)))
  2371. ;; Autoload archiving code
  2372. ;; The stuff that is needed for cycling and tags has to be defined here.
  2373. (defgroup org-archive nil
  2374. "Options concerning archiving in Org-mode."
  2375. :tag "Org Archive"
  2376. :group 'org-structure)
  2377. (defcustom org-archive-location "%s_archive::"
  2378. "The location where subtrees should be archived.
  2379. The value of this variable is a string, consisting of two parts,
  2380. separated by a double-colon. The first part is a filename and
  2381. the second part is a headline.
  2382. When the filename is omitted, archiving happens in the same file.
  2383. %s in the filename will be replaced by the current file
  2384. name (without the directory part). Archiving to a different file
  2385. is useful to keep archived entries from contributing to the
  2386. Org-mode Agenda.
  2387. The archived entries will be filed as subtrees of the specified
  2388. headline. When the headline is omitted, the subtrees are simply
  2389. filed away at the end of the file, as top-level entries.
  2390. Here are a few examples:
  2391. \"%s_archive::\"
  2392. If the current file is Projects.org, archive in file
  2393. Projects.org_archive, as top-level trees. This is the default.
  2394. \"::* Archived Tasks\"
  2395. Archive in the current file, under the top-level headline
  2396. \"* Archived Tasks\".
  2397. \"~/org/archive.org::\"
  2398. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2399. \"basement::** Finished Tasks\"
  2400. Archive in file ./basement (relative path), as level 3 trees
  2401. below the level 2 heading \"** Finished Tasks\".
  2402. You may set this option on a per-file basis by adding to the buffer a
  2403. line like
  2404. #+ARCHIVE: basement::** Finished Tasks
  2405. You may also define it locally for a subtree by setting an ARCHIVE property
  2406. in the entry. If such a property is found in an entry, or anywhere up
  2407. the hierarchy, it will be used."
  2408. :group 'org-archive
  2409. :type 'string)
  2410. (defcustom org-archive-tag "ARCHIVE"
  2411. "The tag that marks a subtree as archived.
  2412. An archived subtree does not open during visibility cycling, and does
  2413. not contribute to the agenda listings.
  2414. After changing this, font-lock must be restarted in the relevant buffers to
  2415. get the proper fontification."
  2416. :group 'org-archive
  2417. :group 'org-keywords
  2418. :type 'string)
  2419. (defcustom org-agenda-skip-archived-trees t
  2420. "Non-nil means, the agenda will skip any items located in archived trees.
  2421. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2422. variable is no longer recommended, you should leave it at the value t.
  2423. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2424. :group 'org-archive
  2425. :group 'org-agenda-skip
  2426. :type 'boolean)
  2427. (defcustom org-cycle-open-archived-trees nil
  2428. "Non-nil means, `org-cycle' will open archived trees.
  2429. An archived tree is a tree marked with the tag ARCHIVE.
  2430. When nil, archived trees will stay folded. You can still open them with
  2431. normal outline commands like `show-all', but not with the cycling commands."
  2432. :group 'org-archive
  2433. :group 'org-cycle
  2434. :type 'boolean)
  2435. (defcustom org-sparse-tree-open-archived-trees nil
  2436. "Non-nil means sparse tree construction shows matches in archived trees.
  2437. When nil, matches in these trees are highlighted, but the trees are kept in
  2438. collapsed state."
  2439. :group 'org-archive
  2440. :group 'org-sparse-trees
  2441. :type 'boolean)
  2442. (defun org-cycle-hide-archived-subtrees (state)
  2443. "Re-hide all archived subtrees after a visibility state change."
  2444. (when (and (not org-cycle-open-archived-trees)
  2445. (not (memq state '(overview folded))))
  2446. (save-excursion
  2447. (let* ((globalp (memq state '(contents all)))
  2448. (beg (if globalp (point-min) (point)))
  2449. (end (if globalp (point-max) (org-end-of-subtree t))))
  2450. (org-hide-archived-subtrees beg end)
  2451. (goto-char beg)
  2452. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2453. (message "%s" (substitute-command-keys
  2454. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2455. (defun org-force-cycle-archived ()
  2456. "Cycle subtree even if it is archived."
  2457. (interactive)
  2458. (setq this-command 'org-cycle)
  2459. (let ((org-cycle-open-archived-trees t))
  2460. (call-interactively 'org-cycle)))
  2461. (defun org-hide-archived-subtrees (beg end)
  2462. "Re-hide all archived subtrees after a visibility state change."
  2463. (save-excursion
  2464. (let* ((re (concat ":" org-archive-tag ":")))
  2465. (goto-char beg)
  2466. (while (re-search-forward re end t)
  2467. (and (org-on-heading-p) (hide-subtree))
  2468. (org-end-of-subtree t)))))
  2469. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2470. (eval-and-compile
  2471. (org-autoload "org-archive"
  2472. '(org-add-archive-files org-archive-subtree
  2473. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2474. ;; Autoload Column View Code
  2475. (declare-function org-columns-number-to-string "org-colview")
  2476. (declare-function org-columns-get-format-and-top-level "org-colview")
  2477. (declare-function org-columns-compute "org-colview")
  2478. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2479. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2480. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2481. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2482. ;; Autoload ID code
  2483. (declare-function org-id-store-link "org-id")
  2484. (org-autoload "org-id"
  2485. '(org-id-get-create org-id-new org-id-copy org-id-get
  2486. org-id-get-with-outline-path-completion
  2487. org-id-get-with-outline-drilling
  2488. org-id-goto org-id-find org-id-store-link))
  2489. ;;; Variables for pre-computed regular expressions, all buffer local
  2490. (defvar org-drawer-regexp nil
  2491. "Matches first line of a hidden block.")
  2492. (make-variable-buffer-local 'org-drawer-regexp)
  2493. (defvar org-todo-regexp nil
  2494. "Matches any of the TODO state keywords.")
  2495. (make-variable-buffer-local 'org-todo-regexp)
  2496. (defvar org-not-done-regexp nil
  2497. "Matches any of the TODO state keywords except the last one.")
  2498. (make-variable-buffer-local 'org-not-done-regexp)
  2499. (defvar org-todo-line-regexp nil
  2500. "Matches a headline and puts TODO state into group 2 if present.")
  2501. (make-variable-buffer-local 'org-todo-line-regexp)
  2502. (defvar org-complex-heading-regexp nil
  2503. "Matches a headline and puts everything into groups:
  2504. group 1: the stars
  2505. group 2: The todo keyword, maybe
  2506. group 3: Priority cookie
  2507. group 4: True headline
  2508. group 5: Tags")
  2509. (make-variable-buffer-local 'org-complex-heading-regexp)
  2510. (defvar org-todo-line-tags-regexp nil
  2511. "Matches a headline and puts TODO state into group 2 if present.
  2512. Also put tags into group 4 if tags are present.")
  2513. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2514. (defvar org-nl-done-regexp nil
  2515. "Matches newline followed by a headline with the DONE keyword.")
  2516. (make-variable-buffer-local 'org-nl-done-regexp)
  2517. (defvar org-looking-at-done-regexp nil
  2518. "Matches the DONE keyword a point.")
  2519. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2520. (defvar org-ds-keyword-length 12
  2521. "Maximum length of the Deadline and SCHEDULED keywords.")
  2522. (make-variable-buffer-local 'org-ds-keyword-length)
  2523. (defvar org-deadline-regexp nil
  2524. "Matches the DEADLINE keyword.")
  2525. (make-variable-buffer-local 'org-deadline-regexp)
  2526. (defvar org-deadline-time-regexp nil
  2527. "Matches the DEADLINE keyword together with a time stamp.")
  2528. (make-variable-buffer-local 'org-deadline-time-regexp)
  2529. (defvar org-deadline-line-regexp nil
  2530. "Matches the DEADLINE keyword and the rest of the line.")
  2531. (make-variable-buffer-local 'org-deadline-line-regexp)
  2532. (defvar org-scheduled-regexp nil
  2533. "Matches the SCHEDULED keyword.")
  2534. (make-variable-buffer-local 'org-scheduled-regexp)
  2535. (defvar org-scheduled-time-regexp nil
  2536. "Matches the SCHEDULED keyword together with a time stamp.")
  2537. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2538. (defvar org-closed-time-regexp nil
  2539. "Matches the CLOSED keyword together with a time stamp.")
  2540. (make-variable-buffer-local 'org-closed-time-regexp)
  2541. (defvar org-keyword-time-regexp nil
  2542. "Matches any of the 4 keywords, together with the time stamp.")
  2543. (make-variable-buffer-local 'org-keyword-time-regexp)
  2544. (defvar org-keyword-time-not-clock-regexp nil
  2545. "Matches any of the 3 keywords, together with the time stamp.")
  2546. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2547. (defvar org-maybe-keyword-time-regexp nil
  2548. "Matches a timestamp, possibly preceeded by a keyword.")
  2549. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2550. (defvar org-planning-or-clock-line-re nil
  2551. "Matches a line with planning or clock info.")
  2552. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2553. (defconst org-plain-time-of-day-regexp
  2554. (concat
  2555. "\\(\\<[012]?[0-9]"
  2556. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2557. "\\(--?"
  2558. "\\(\\<[012]?[0-9]"
  2559. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2560. "\\)?")
  2561. "Regular expression to match a plain time or time range.
  2562. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2563. groups carry important information:
  2564. 0 the full match
  2565. 1 the first time, range or not
  2566. 8 the second time, if it is a range.")
  2567. (defconst org-plain-time-extension-regexp
  2568. (concat
  2569. "\\(\\<[012]?[0-9]"
  2570. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2571. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2572. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2573. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2574. groups carry important information:
  2575. 0 the full match
  2576. 7 hours of duration
  2577. 9 minutes of duration")
  2578. (defconst org-stamp-time-of-day-regexp
  2579. (concat
  2580. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2581. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2582. "\\(--?"
  2583. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2584. "Regular expression to match a timestamp time or time range.
  2585. After a match, the following groups carry important information:
  2586. 0 the full match
  2587. 1 date plus weekday, for backreferencing to make sure both times on same day
  2588. 2 the first time, range or not
  2589. 4 the second time, if it is a range.")
  2590. (defconst org-startup-options
  2591. '(("fold" org-startup-folded t)
  2592. ("overview" org-startup-folded t)
  2593. ("nofold" org-startup-folded nil)
  2594. ("showall" org-startup-folded nil)
  2595. ("content" org-startup-folded content)
  2596. ("hidestars" org-hide-leading-stars t)
  2597. ("showstars" org-hide-leading-stars nil)
  2598. ("odd" org-odd-levels-only t)
  2599. ("oddeven" org-odd-levels-only nil)
  2600. ("align" org-startup-align-all-tables t)
  2601. ("noalign" org-startup-align-all-tables nil)
  2602. ("customtime" org-display-custom-times t)
  2603. ("logdone" org-log-done time)
  2604. ("lognotedone" org-log-done note)
  2605. ("nologdone" org-log-done nil)
  2606. ("lognoteclock-out" org-log-note-clock-out t)
  2607. ("nolognoteclock-out" org-log-note-clock-out nil)
  2608. ("logrepeat" org-log-repeat state)
  2609. ("lognoterepeat" org-log-repeat note)
  2610. ("nologrepeat" org-log-repeat nil)
  2611. ("constcgs" constants-unit-system cgs)
  2612. ("constSI" constants-unit-system SI))
  2613. "Variable associated with STARTUP options for org-mode.
  2614. Each element is a list of three items: The startup options as written
  2615. in the #+STARTUP line, the corresponding variable, and the value to
  2616. set this variable to if the option is found. An optional forth element PUSH
  2617. means to push this value onto the list in the variable.")
  2618. (defun org-set-regexps-and-options ()
  2619. "Precompute regular expressions for current buffer."
  2620. (when (org-mode-p)
  2621. (org-set-local 'org-todo-kwd-alist nil)
  2622. (org-set-local 'org-todo-key-alist nil)
  2623. (org-set-local 'org-todo-key-trigger nil)
  2624. (org-set-local 'org-todo-keywords-1 nil)
  2625. (org-set-local 'org-done-keywords nil)
  2626. (org-set-local 'org-todo-heads nil)
  2627. (org-set-local 'org-todo-sets nil)
  2628. (org-set-local 'org-todo-log-states nil)
  2629. (org-set-local 'org-file-properties nil)
  2630. (org-set-local 'org-file-tags nil)
  2631. (let ((re (org-make-options-regexp
  2632. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2633. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2634. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2635. (splitre "[ \t]+")
  2636. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2637. tail sep kws1 prio props ftags drawers
  2638. ext-setup-or-nil setup-contents (start 0))
  2639. (save-excursion
  2640. (save-restriction
  2641. (widen)
  2642. (goto-char (point-min))
  2643. (while (or (and ext-setup-or-nil
  2644. (string-match re ext-setup-or-nil start)
  2645. (setq start (match-end 0)))
  2646. (and (setq ext-setup-or-nil nil start 0)
  2647. (re-search-forward re nil t)))
  2648. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2649. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2650. (cond
  2651. ((equal key "CATEGORY")
  2652. (if (string-match "[ \t]+$" value)
  2653. (setq value (replace-match "" t t value)))
  2654. (setq cat value))
  2655. ((member key '("SEQ_TODO" "TODO"))
  2656. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2657. ((equal key "TYP_TODO")
  2658. (push (cons 'type (org-split-string value splitre)) kwds))
  2659. ((equal key "TAGS")
  2660. (setq tags (append tags (org-split-string value splitre))))
  2661. ((equal key "COLUMNS")
  2662. (org-set-local 'org-columns-default-format value))
  2663. ((equal key "LINK")
  2664. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2665. (push (cons (match-string 1 value)
  2666. (org-trim (match-string 2 value)))
  2667. links)))
  2668. ((equal key "PRIORITIES")
  2669. (setq prio (org-split-string value " +")))
  2670. ((equal key "PROPERTY")
  2671. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2672. (push (cons (match-string 1 value) (match-string 2 value))
  2673. props)))
  2674. ((equal key "FILETAGS")
  2675. (when (string-match "\\S-" value)
  2676. (setq ftags
  2677. (append
  2678. ftags
  2679. (apply 'append
  2680. (mapcar (lambda (x) (org-split-string x ":"))
  2681. (org-split-string value)))))))
  2682. ((equal key "DRAWERS")
  2683. (setq drawers (org-split-string value splitre)))
  2684. ((equal key "CONSTANTS")
  2685. (setq const (append const (org-split-string value splitre))))
  2686. ((equal key "STARTUP")
  2687. (let ((opts (org-split-string value splitre))
  2688. l var val)
  2689. (while (setq l (pop opts))
  2690. (when (setq l (assoc l org-startup-options))
  2691. (setq var (nth 1 l) val (nth 2 l))
  2692. (if (not (nth 3 l))
  2693. (set (make-local-variable var) val)
  2694. (if (not (listp (symbol-value var)))
  2695. (set (make-local-variable var) nil))
  2696. (set (make-local-variable var) (symbol-value var))
  2697. (add-to-list var val))))))
  2698. ((equal key "ARCHIVE")
  2699. (string-match " *$" value)
  2700. (setq arch (replace-match "" t t value))
  2701. (remove-text-properties 0 (length arch)
  2702. '(face t fontified t) arch))
  2703. ((equal key "SETUPFILE")
  2704. (setq setup-contents (org-file-contents
  2705. (expand-file-name
  2706. (org-remove-double-quotes value))
  2707. 'noerror))
  2708. (if (not ext-setup-or-nil)
  2709. (setq ext-setup-or-nil setup-contents start 0)
  2710. (setq ext-setup-or-nil
  2711. (concat (substring ext-setup-or-nil 0 start)
  2712. "\n" setup-contents "\n"
  2713. (substring ext-setup-or-nil start)))))
  2714. ))))
  2715. (when cat
  2716. (org-set-local 'org-category (intern cat))
  2717. (push (cons "CATEGORY" cat) props))
  2718. (when prio
  2719. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2720. (setq prio (mapcar 'string-to-char prio))
  2721. (org-set-local 'org-highest-priority (nth 0 prio))
  2722. (org-set-local 'org-lowest-priority (nth 1 prio))
  2723. (org-set-local 'org-default-priority (nth 2 prio)))
  2724. (and props (org-set-local 'org-file-properties (nreverse props)))
  2725. (and ftags (org-set-local 'org-file-tags ftags))
  2726. (and drawers (org-set-local 'org-drawers drawers))
  2727. (and arch (org-set-local 'org-archive-location arch))
  2728. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2729. ;; Process the TODO keywords
  2730. (unless kwds
  2731. ;; Use the global values as if they had been given locally.
  2732. (setq kwds (default-value 'org-todo-keywords))
  2733. (if (stringp (car kwds))
  2734. (setq kwds (list (cons org-todo-interpretation
  2735. (default-value 'org-todo-keywords)))))
  2736. (setq kwds (reverse kwds)))
  2737. (setq kwds (nreverse kwds))
  2738. (let (inter kws kw)
  2739. (while (setq kws (pop kwds))
  2740. (setq inter (pop kws) sep (member "|" kws)
  2741. kws0 (delete "|" (copy-sequence kws))
  2742. kwsa nil
  2743. kws1 (mapcar
  2744. (lambda (x)
  2745. ;; 1 2
  2746. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2747. (progn
  2748. (setq kw (match-string 1 x)
  2749. key (and (match-end 2) (match-string 2 x))
  2750. log (org-extract-log-state-settings x))
  2751. (push (cons kw (and key (string-to-char key))) kwsa)
  2752. (and log (push log org-todo-log-states))
  2753. kw)
  2754. (error "Invalid TODO keyword %s" x)))
  2755. kws0)
  2756. kwsa (if kwsa (append '((:startgroup))
  2757. (nreverse kwsa)
  2758. '((:endgroup))))
  2759. hw (car kws1)
  2760. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2761. tail (list inter hw (car dws) (org-last dws)))
  2762. (add-to-list 'org-todo-heads hw 'append)
  2763. (push kws1 org-todo-sets)
  2764. (setq org-done-keywords (append org-done-keywords dws nil))
  2765. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2766. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2767. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2768. (setq org-todo-sets (nreverse org-todo-sets)
  2769. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2770. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2771. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2772. ;; Process the constants
  2773. (when const
  2774. (let (e cst)
  2775. (while (setq e (pop const))
  2776. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2777. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2778. (setq org-table-formula-constants-local cst)))
  2779. ;; Process the tags.
  2780. (when tags
  2781. (let (e tgs)
  2782. (while (setq e (pop tags))
  2783. (cond
  2784. ((equal e "{") (push '(:startgroup) tgs))
  2785. ((equal e "}") (push '(:endgroup) tgs))
  2786. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2787. (push (cons (match-string 1 e)
  2788. (string-to-char (match-string 2 e)))
  2789. tgs))
  2790. (t (push (list e) tgs))))
  2791. (org-set-local 'org-tag-alist nil)
  2792. (while (setq e (pop tgs))
  2793. (or (and (stringp (car e))
  2794. (assoc (car e) org-tag-alist))
  2795. (push e org-tag-alist)))))
  2796. ;; Compute the regular expressions and other local variables
  2797. (if (not org-done-keywords)
  2798. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2799. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2800. (length org-scheduled-string)
  2801. (length org-clock-string)
  2802. (length org-closed-string)))
  2803. org-drawer-regexp
  2804. (concat "^[ \t]*:\\("
  2805. (mapconcat 'regexp-quote org-drawers "\\|")
  2806. "\\):[ \t]*$")
  2807. org-not-done-keywords
  2808. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2809. org-todo-regexp
  2810. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2811. "\\|") "\\)\\>")
  2812. org-not-done-regexp
  2813. (concat "\\<\\("
  2814. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2815. "\\)\\>")
  2816. org-todo-line-regexp
  2817. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2818. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2819. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2820. org-complex-heading-regexp
  2821. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2822. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2823. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2824. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2825. org-nl-done-regexp
  2826. (concat "\n\\*+[ \t]+"
  2827. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2828. "\\)" "\\>")
  2829. org-todo-line-tags-regexp
  2830. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2831. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2832. (org-re
  2833. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2834. org-looking-at-done-regexp
  2835. (concat "^" "\\(?:"
  2836. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2837. "\\>")
  2838. org-deadline-regexp (concat "\\<" org-deadline-string)
  2839. org-deadline-time-regexp
  2840. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2841. org-deadline-line-regexp
  2842. (concat "\\<\\(" org-deadline-string "\\).*")
  2843. org-scheduled-regexp
  2844. (concat "\\<" org-scheduled-string)
  2845. org-scheduled-time-regexp
  2846. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2847. org-closed-time-regexp
  2848. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2849. org-keyword-time-regexp
  2850. (concat "\\<\\(" org-scheduled-string
  2851. "\\|" org-deadline-string
  2852. "\\|" org-closed-string
  2853. "\\|" org-clock-string "\\)"
  2854. " *[[<]\\([^]>]+\\)[]>]")
  2855. org-keyword-time-not-clock-regexp
  2856. (concat "\\<\\(" org-scheduled-string
  2857. "\\|" org-deadline-string
  2858. "\\|" org-closed-string
  2859. "\\)"
  2860. " *[[<]\\([^]>]+\\)[]>]")
  2861. org-maybe-keyword-time-regexp
  2862. (concat "\\(\\<\\(" org-scheduled-string
  2863. "\\|" org-deadline-string
  2864. "\\|" org-closed-string
  2865. "\\|" org-clock-string "\\)\\)?"
  2866. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2867. org-planning-or-clock-line-re
  2868. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2869. "\\|" org-deadline-string
  2870. "\\|" org-closed-string "\\|" org-clock-string
  2871. "\\)\\>\\)")
  2872. )
  2873. (org-compute-latex-and-specials-regexp)
  2874. (org-set-font-lock-defaults))))
  2875. (defun org-file-contents (file &optional noerror)
  2876. "Return the contents of FILE, as a string."
  2877. (if (or (not file)
  2878. (not (file-readable-p file)))
  2879. (if noerror
  2880. (progn
  2881. (message "Cannot read file %s" file)
  2882. (ding) (sit-for 2)
  2883. "")
  2884. (error "Cannot read file %s" file))
  2885. (with-temp-buffer
  2886. (insert-file-contents file)
  2887. (buffer-string))))
  2888. (defun org-extract-log-state-settings (x)
  2889. "Extract the log state setting from a TODO keyword string.
  2890. This will extract info from a string like \"WAIT(w@/!)\"."
  2891. (let (kw key log1 log2)
  2892. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2893. (setq kw (match-string 1 x)
  2894. key (and (match-end 2) (match-string 2 x))
  2895. log1 (and (match-end 3) (match-string 3 x))
  2896. log2 (and (match-end 4) (match-string 4 x)))
  2897. (and (or log1 log2)
  2898. (list kw
  2899. (and log1 (if (equal log1 "!") 'time 'note))
  2900. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2901. (defun org-remove-keyword-keys (list)
  2902. "Remove a pair of parenthesis at the end of each string in LIST."
  2903. (mapcar (lambda (x)
  2904. (if (string-match "(.*)$" x)
  2905. (substring x 0 (match-beginning 0))
  2906. x))
  2907. list))
  2908. ;; FIXME: this could be done much better, using second characters etc.
  2909. (defun org-assign-fast-keys (alist)
  2910. "Assign fast keys to a keyword-key alist.
  2911. Respect keys that are already there."
  2912. (let (new e k c c1 c2 (char ?a))
  2913. (while (setq e (pop alist))
  2914. (cond
  2915. ((equal e '(:startgroup)) (push e new))
  2916. ((equal e '(:endgroup)) (push e new))
  2917. (t
  2918. (setq k (car e) c2 nil)
  2919. (if (cdr e)
  2920. (setq c (cdr e))
  2921. ;; automatically assign a character.
  2922. (setq c1 (string-to-char
  2923. (downcase (substring
  2924. k (if (= (string-to-char k) ?@) 1 0)))))
  2925. (if (or (rassoc c1 new) (rassoc c1 alist))
  2926. (while (or (rassoc char new) (rassoc char alist))
  2927. (setq char (1+ char)))
  2928. (setq c2 c1))
  2929. (setq c (or c2 char)))
  2930. (push (cons k c) new))))
  2931. (nreverse new)))
  2932. ;;; Some variables used in various places
  2933. (defvar org-window-configuration nil
  2934. "Used in various places to store a window configuration.")
  2935. (defvar org-finish-function nil
  2936. "Function to be called when `C-c C-c' is used.
  2937. This is for getting out of special buffers like remember.")
  2938. ;; FIXME: Occasionally check by commenting these, to make sure
  2939. ;; no other functions uses these, forgetting to let-bind them.
  2940. (defvar entry)
  2941. (defvar state)
  2942. (defvar last-state)
  2943. (defvar date)
  2944. (defvar description)
  2945. ;; Defined somewhere in this file, but used before definition.
  2946. (defvar org-html-entities)
  2947. (defvar org-struct-menu)
  2948. (defvar org-org-menu)
  2949. (defvar org-tbl-menu)
  2950. (defvar org-agenda-keymap)
  2951. ;;;; Define the Org-mode
  2952. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2953. (error "Conflict with outdated version of allout.el. Load org.el before allout.el, or ugrade to newer allout, for example by switching to Emacs 22."))
  2954. ;; We use a before-change function to check if a table might need
  2955. ;; an update.
  2956. (defvar org-table-may-need-update t
  2957. "Indicates that a table might need an update.
  2958. This variable is set by `org-before-change-function'.
  2959. `org-table-align' sets it back to nil.")
  2960. (defun org-before-change-function (beg end)
  2961. "Every change indicates that a table might need an update."
  2962. (setq org-table-may-need-update t))
  2963. (defvar org-mode-map)
  2964. (defvar org-mode-hook nil)
  2965. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2966. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2967. (defvar org-table-buffer-is-an nil)
  2968. (defconst org-outline-regexp "\\*+ ")
  2969. ;;;###autoload
  2970. (define-derived-mode org-mode outline-mode "Org"
  2971. "Outline-based notes management and organizer, alias
  2972. \"Carsten's outline-mode for keeping track of everything.\"
  2973. Org-mode develops organizational tasks around a NOTES file which
  2974. contains information about projects as plain text. Org-mode is
  2975. implemented on top of outline-mode, which is ideal to keep the content
  2976. of large files well structured. It supports ToDo items, deadlines and
  2977. time stamps, which magically appear in the diary listing of the Emacs
  2978. calendar. Tables are easily created with a built-in table editor.
  2979. Plain text URL-like links connect to websites, emails (VM), Usenet
  2980. messages (Gnus), BBDB entries, and any files related to the project.
  2981. For printing and sharing of notes, an Org-mode file (or a part of it)
  2982. can be exported as a structured ASCII or HTML file.
  2983. The following commands are available:
  2984. \\{org-mode-map}"
  2985. ;; Get rid of Outline menus, they are not needed
  2986. ;; Need to do this here because define-derived-mode sets up
  2987. ;; the keymap so late. Still, it is a waste to call this each time
  2988. ;; we switch another buffer into org-mode.
  2989. (if (featurep 'xemacs)
  2990. (when (boundp 'outline-mode-menu-heading)
  2991. ;; Assume this is Greg's port, it used easymenu
  2992. (easy-menu-remove outline-mode-menu-heading)
  2993. (easy-menu-remove outline-mode-menu-show)
  2994. (easy-menu-remove outline-mode-menu-hide))
  2995. (define-key org-mode-map [menu-bar headings] 'undefined)
  2996. (define-key org-mode-map [menu-bar hide] 'undefined)
  2997. (define-key org-mode-map [menu-bar show] 'undefined))
  2998. (org-load-modules-maybe)
  2999. (easy-menu-add org-org-menu)
  3000. (easy-menu-add org-tbl-menu)
  3001. (org-install-agenda-files-menu)
  3002. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  3003. (org-add-to-invisibility-spec '(org-cwidth))
  3004. (when (featurep 'xemacs)
  3005. (org-set-local 'line-move-ignore-invisible t))
  3006. (org-set-local 'outline-regexp org-outline-regexp)
  3007. (org-set-local 'outline-level 'org-outline-level)
  3008. (when (and org-ellipsis
  3009. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  3010. (fboundp 'make-glyph-code))
  3011. (unless org-display-table
  3012. (setq org-display-table (make-display-table)))
  3013. (set-display-table-slot
  3014. org-display-table 4
  3015. (vconcat (mapcar
  3016. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  3017. org-ellipsis)))
  3018. (if (stringp org-ellipsis) org-ellipsis "..."))))
  3019. (setq buffer-display-table org-display-table))
  3020. (org-set-regexps-and-options)
  3021. ;; Calc embedded
  3022. (org-set-local 'calc-embedded-open-mode "# ")
  3023. (modify-syntax-entry ?# "<")
  3024. (modify-syntax-entry ?@ "w")
  3025. (if org-startup-truncated (setq truncate-lines t))
  3026. (org-set-local 'font-lock-unfontify-region-function
  3027. 'org-unfontify-region)
  3028. ;; Activate before-change-function
  3029. (org-set-local 'org-table-may-need-update t)
  3030. (org-add-hook 'before-change-functions 'org-before-change-function nil
  3031. 'local)
  3032. ;; Check for running clock before killing a buffer
  3033. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  3034. ;; Paragraphs and auto-filling
  3035. (org-set-autofill-regexps)
  3036. (setq indent-line-function 'org-indent-line-function)
  3037. (org-update-radio-target-regexp)
  3038. ;; Comment characters
  3039. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  3040. (org-set-local 'comment-padding " ")
  3041. ;; Align options lines
  3042. (org-set-local
  3043. 'align-mode-rules-list
  3044. '((org-in-buffer-settings
  3045. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3046. (modes . '(org-mode)))))
  3047. ;; Imenu
  3048. (org-set-local 'imenu-create-index-function
  3049. 'org-imenu-get-tree)
  3050. ;; Make isearch reveal context
  3051. (if (or (featurep 'xemacs)
  3052. (not (boundp 'outline-isearch-open-invisible-function)))
  3053. ;; Emacs 21 and XEmacs make use of the hook
  3054. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3055. ;; Emacs 22 deals with this through a special variable
  3056. (org-set-local 'outline-isearch-open-invisible-function
  3057. (lambda (&rest ignore) (org-show-context 'isearch))))
  3058. ;; If empty file that did not turn on org-mode automatically, make it to.
  3059. (if (and org-insert-mode-line-in-empty-file
  3060. (interactive-p)
  3061. (= (point-min) (point-max)))
  3062. (insert "# -*- mode: org -*-\n\n"))
  3063. (unless org-inhibit-startup
  3064. (when org-startup-align-all-tables
  3065. (let ((bmp (buffer-modified-p)))
  3066. (org-table-map-tables 'org-table-align)
  3067. (set-buffer-modified-p bmp)))
  3068. (org-set-startup-visibility)))
  3069. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3070. (defun org-current-time ()
  3071. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3072. (if (> (car org-time-stamp-rounding-minutes) 1)
  3073. (let ((r (car org-time-stamp-rounding-minutes))
  3074. (time (decode-time)))
  3075. (apply 'encode-time
  3076. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3077. (nthcdr 2 time))))
  3078. (current-time)))
  3079. ;;;; Font-Lock stuff, including the activators
  3080. (defvar org-mouse-map (make-sparse-keymap))
  3081. (org-defkey org-mouse-map
  3082. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3083. (org-defkey org-mouse-map
  3084. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3085. (when org-mouse-1-follows-link
  3086. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3087. (when org-tab-follows-link
  3088. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3089. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3090. (when org-return-follows-link
  3091. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3092. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3093. (require 'font-lock)
  3094. (defconst org-non-link-chars "]\t\n\r<>")
  3095. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3096. "shell" "elisp"))
  3097. (defvar org-link-types-re nil
  3098. "Matches a link that has a url-like prefix like \"http:\"")
  3099. (defvar org-link-re-with-space nil
  3100. "Matches a link with spaces, optional angular brackets around it.")
  3101. (defvar org-link-re-with-space2 nil
  3102. "Matches a link with spaces, optional angular brackets around it.")
  3103. (defvar org-link-re-with-space3 nil
  3104. "Matches a link with spaces, only for internal part in bracket links.")
  3105. (defvar org-angle-link-re nil
  3106. "Matches link with angular brackets, spaces are allowed.")
  3107. (defvar org-plain-link-re nil
  3108. "Matches plain link, without spaces.")
  3109. (defvar org-bracket-link-regexp nil
  3110. "Matches a link in double brackets.")
  3111. (defvar org-bracket-link-analytic-regexp nil
  3112. "Regular expression used to analyze links.
  3113. Here is what the match groups contain after a match:
  3114. 1: http:
  3115. 2: http
  3116. 3: path
  3117. 4: [desc]
  3118. 5: desc")
  3119. (defvar org-any-link-re nil
  3120. "Regular expression matching any link.")
  3121. (defun org-make-link-regexps ()
  3122. "Update the link regular expressions.
  3123. This should be called after the variable `org-link-types' has changed."
  3124. (setq org-link-types-re
  3125. (concat
  3126. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3127. org-link-re-with-space
  3128. (concat
  3129. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3130. "\\([^" org-non-link-chars " ]"
  3131. "[^" org-non-link-chars "]*"
  3132. "[^" org-non-link-chars " ]\\)>?")
  3133. org-link-re-with-space2
  3134. (concat
  3135. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3136. "\\([^" org-non-link-chars " ]"
  3137. "[^\t\n\r]*"
  3138. "[^" org-non-link-chars " ]\\)>?")
  3139. org-link-re-with-space3
  3140. (concat
  3141. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3142. "\\([^" org-non-link-chars " ]"
  3143. "[^\t\n\r]*\\)")
  3144. org-angle-link-re
  3145. (concat
  3146. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3147. "\\([^" org-non-link-chars " ]"
  3148. "[^" org-non-link-chars "]*"
  3149. "\\)>")
  3150. org-plain-link-re
  3151. (concat
  3152. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3153. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3154. org-bracket-link-regexp
  3155. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3156. org-bracket-link-analytic-regexp
  3157. (concat
  3158. "\\[\\["
  3159. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3160. "\\([^]]+\\)"
  3161. "\\]"
  3162. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3163. "\\]")
  3164. org-any-link-re
  3165. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3166. org-angle-link-re "\\)\\|\\("
  3167. org-plain-link-re "\\)")))
  3168. (org-make-link-regexps)
  3169. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3170. "Regular expression for fast time stamp matching.")
  3171. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3172. "Regular expression for fast time stamp matching.")
  3173. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3174. "Regular expression matching time strings for analysis.
  3175. This one does not require the space after the date, so it can be used
  3176. on a string that terminates immediately after the date.")
  3177. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3178. "Regular expression matching time strings for analysis.")
  3179. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3180. "Regular expression matching time stamps, with groups.")
  3181. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3182. "Regular expression matching time stamps (also [..]), with groups.")
  3183. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3184. "Regular expression matching a time stamp range.")
  3185. (defconst org-tr-regexp-both
  3186. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3187. "Regular expression matching a time stamp range.")
  3188. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3189. org-ts-regexp "\\)?")
  3190. "Regular expression matching a time stamp or time stamp range.")
  3191. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3192. org-ts-regexp-both "\\)?")
  3193. "Regular expression matching a time stamp or time stamp range.
  3194. The time stamps may be either active or inactive.")
  3195. (defvar org-emph-face nil)
  3196. (defun org-do-emphasis-faces (limit)
  3197. "Run through the buffer and add overlays to links."
  3198. (let (rtn)
  3199. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3200. (if (not (= (char-after (match-beginning 3))
  3201. (char-after (match-beginning 4))))
  3202. (progn
  3203. (setq rtn t)
  3204. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3205. 'face
  3206. (nth 1 (assoc (match-string 3)
  3207. org-emphasis-alist)))
  3208. (add-text-properties (match-beginning 2) (match-end 2)
  3209. '(font-lock-multiline t))
  3210. (when org-hide-emphasis-markers
  3211. (add-text-properties (match-end 4) (match-beginning 5)
  3212. '(invisible org-link))
  3213. (add-text-properties (match-beginning 3) (match-end 3)
  3214. '(invisible org-link)))))
  3215. (backward-char 1))
  3216. rtn))
  3217. (defun org-emphasize (&optional char)
  3218. "Insert or change an emphasis, i.e. a font like bold or italic.
  3219. If there is an active region, change that region to a new emphasis.
  3220. If there is no region, just insert the marker characters and position
  3221. the cursor between them.
  3222. CHAR should be either the marker character, or the first character of the
  3223. HTML tag associated with that emphasis. If CHAR is a space, the means
  3224. to remove the emphasis of the selected region.
  3225. If char is not given (for example in an interactive call) it
  3226. will be prompted for."
  3227. (interactive)
  3228. (let ((eal org-emphasis-alist) e det
  3229. (erc org-emphasis-regexp-components)
  3230. (prompt "")
  3231. (string "") beg end move tag c s)
  3232. (if (org-region-active-p)
  3233. (setq beg (region-beginning) end (region-end)
  3234. string (buffer-substring beg end))
  3235. (setq move t))
  3236. (while (setq e (pop eal))
  3237. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3238. c (aref tag 0))
  3239. (push (cons c (string-to-char (car e))) det)
  3240. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3241. (substring tag 1)))))
  3242. (setq det (nreverse det))
  3243. (unless char
  3244. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3245. (setq char (read-char-exclusive)))
  3246. (setq char (or (cdr (assoc char det)) char))
  3247. (if (equal char ?\ )
  3248. (setq s "" move nil)
  3249. (unless (assoc (char-to-string char) org-emphasis-alist)
  3250. (error "No such emphasis marker: \"%c\"" char))
  3251. (setq s (char-to-string char)))
  3252. (while (and (> (length string) 1)
  3253. (equal (substring string 0 1) (substring string -1))
  3254. (assoc (substring string 0 1) org-emphasis-alist))
  3255. (setq string (substring string 1 -1)))
  3256. (setq string (concat s string s))
  3257. (if beg (delete-region beg end))
  3258. (unless (or (bolp)
  3259. (string-match (concat "[" (nth 0 erc) "\n]")
  3260. (char-to-string (char-before (point)))))
  3261. (insert " "))
  3262. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3263. (char-to-string (char-after (point))))
  3264. (insert " ") (backward-char 1))
  3265. (insert string)
  3266. (and move (backward-char 1))))
  3267. (defconst org-nonsticky-props
  3268. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3269. (defun org-activate-plain-links (limit)
  3270. "Run through the buffer and add overlays to links."
  3271. (catch 'exit
  3272. (let (f)
  3273. (while (re-search-forward org-plain-link-re limit t)
  3274. (setq f (get-text-property (match-beginning 0) 'face))
  3275. (if (or (eq f 'org-tag)
  3276. (and (listp f) (memq 'org-tag f)))
  3277. nil
  3278. (add-text-properties (match-beginning 0) (match-end 0)
  3279. (list 'mouse-face 'highlight
  3280. 'rear-nonsticky org-nonsticky-props
  3281. 'keymap org-mouse-map
  3282. ))
  3283. (throw 'exit t))))))
  3284. (defun org-activate-code (limit)
  3285. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3286. (progn
  3287. (remove-text-properties (match-beginning 0) (match-end 0)
  3288. '(display t invisible t intangible t))
  3289. t)))
  3290. (defun org-activate-angle-links (limit)
  3291. "Run through the buffer and add overlays to links."
  3292. (if (re-search-forward org-angle-link-re limit t)
  3293. (progn
  3294. (add-text-properties (match-beginning 0) (match-end 0)
  3295. (list 'mouse-face 'highlight
  3296. 'rear-nonsticky org-nonsticky-props
  3297. 'keymap org-mouse-map
  3298. ))
  3299. t)))
  3300. (defun org-activate-bracket-links (limit)
  3301. "Run through the buffer and add overlays to bracketed links."
  3302. (if (re-search-forward org-bracket-link-regexp limit t)
  3303. (let* ((help (concat "LINK: "
  3304. (org-match-string-no-properties 1)))
  3305. ;; FIXME: above we should remove the escapes.
  3306. ;; but that requires another match, protecting match data,
  3307. ;; a lot of overhead for font-lock.
  3308. (ip (org-maybe-intangible
  3309. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3310. 'keymap org-mouse-map 'mouse-face 'highlight
  3311. 'font-lock-multiline t 'help-echo help)))
  3312. (vp (list 'rear-nonsticky org-nonsticky-props
  3313. 'keymap org-mouse-map 'mouse-face 'highlight
  3314. ' font-lock-multiline t 'help-echo help)))
  3315. ;; We need to remove the invisible property here. Table narrowing
  3316. ;; may have made some of this invisible.
  3317. (remove-text-properties (match-beginning 0) (match-end 0)
  3318. '(invisible nil))
  3319. (if (match-end 3)
  3320. (progn
  3321. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3322. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3323. (add-text-properties (match-end 3) (match-end 0) ip))
  3324. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3325. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3326. (add-text-properties (match-end 1) (match-end 0) ip))
  3327. t)))
  3328. (defun org-activate-dates (limit)
  3329. "Run through the buffer and add overlays to dates."
  3330. (if (re-search-forward org-tsr-regexp-both limit t)
  3331. (progn
  3332. (add-text-properties (match-beginning 0) (match-end 0)
  3333. (list 'mouse-face 'highlight
  3334. 'rear-nonsticky org-nonsticky-props
  3335. 'keymap org-mouse-map))
  3336. (when org-display-custom-times
  3337. (if (match-end 3)
  3338. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3339. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3340. t)))
  3341. (defvar org-target-link-regexp nil
  3342. "Regular expression matching radio targets in plain text.")
  3343. (make-variable-buffer-local 'org-target-link-regexp)
  3344. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3345. "Regular expression matching a link target.")
  3346. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3347. "Regular expression matching a radio target.")
  3348. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3349. "Regular expression matching any target.")
  3350. (defun org-activate-target-links (limit)
  3351. "Run through the buffer and add overlays to target matches."
  3352. (when org-target-link-regexp
  3353. (let ((case-fold-search t))
  3354. (if (re-search-forward org-target-link-regexp limit t)
  3355. (progn
  3356. (add-text-properties (match-beginning 0) (match-end 0)
  3357. (list 'mouse-face 'highlight
  3358. 'rear-nonsticky org-nonsticky-props
  3359. 'keymap org-mouse-map
  3360. 'help-echo "Radio target link"
  3361. 'org-linked-text t))
  3362. t)))))
  3363. (defun org-update-radio-target-regexp ()
  3364. "Find all radio targets in this file and update the regular expression."
  3365. (interactive)
  3366. (when (memq 'radio org-activate-links)
  3367. (setq org-target-link-regexp
  3368. (org-make-target-link-regexp (org-all-targets 'radio)))
  3369. (org-restart-font-lock)))
  3370. (defun org-hide-wide-columns (limit)
  3371. (let (s e)
  3372. (setq s (text-property-any (point) (or limit (point-max))
  3373. 'org-cwidth t))
  3374. (when s
  3375. (setq e (next-single-property-change s 'org-cwidth))
  3376. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3377. (goto-char e)
  3378. t)))
  3379. (defvar org-latex-and-specials-regexp nil
  3380. "Regular expression for highlighting export special stuff.")
  3381. (defvar org-match-substring-regexp)
  3382. (defvar org-match-substring-with-braces-regexp)
  3383. (defvar org-export-html-special-string-regexps)
  3384. (defun org-compute-latex-and-specials-regexp ()
  3385. "Compute regular expression for stuff treated specially by exporters."
  3386. (if (not org-highlight-latex-fragments-and-specials)
  3387. (org-set-local 'org-latex-and-specials-regexp nil)
  3388. (require 'org-exp)
  3389. (let*
  3390. ((matchers (plist-get org-format-latex-options :matchers))
  3391. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3392. org-latex-regexps)))
  3393. (options (org-combine-plists (org-default-export-plist)
  3394. (org-infile-export-plist)))
  3395. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3396. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3397. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3398. (org-export-html-expand (plist-get options :expand-quoted-html))
  3399. (org-export-with-special-strings (plist-get options :special-strings))
  3400. (re-sub
  3401. (cond
  3402. ((equal org-export-with-sub-superscripts '{})
  3403. (list org-match-substring-with-braces-regexp))
  3404. (org-export-with-sub-superscripts
  3405. (list org-match-substring-regexp))
  3406. (t nil)))
  3407. (re-latex
  3408. (if org-export-with-LaTeX-fragments
  3409. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3410. (re-macros
  3411. (if org-export-with-TeX-macros
  3412. (list (concat "\\\\"
  3413. (regexp-opt
  3414. (append (mapcar 'car org-html-entities)
  3415. (if (boundp 'org-latex-entities)
  3416. org-latex-entities nil))
  3417. 'words))) ; FIXME
  3418. ))
  3419. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3420. (re-special (if org-export-with-special-strings
  3421. (mapcar (lambda (x) (car x))
  3422. org-export-html-special-string-regexps)))
  3423. (re-rest
  3424. (delq nil
  3425. (list
  3426. (if org-export-html-expand "@<[^>\n]+>")
  3427. ))))
  3428. (org-set-local
  3429. 'org-latex-and-specials-regexp
  3430. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3431. re-rest) "\\|")))))
  3432. (defun org-do-latex-and-special-faces (limit)
  3433. "Run through the buffer and add overlays to links."
  3434. (when org-latex-and-specials-regexp
  3435. (let (rtn d)
  3436. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3437. limit t))
  3438. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3439. 'face))
  3440. '(org-code org-verbatim underline)))
  3441. (progn
  3442. (setq rtn t
  3443. d (cond ((member (char-after (1+ (match-beginning 0)))
  3444. '(?_ ?^)) 1)
  3445. (t 0)))
  3446. (font-lock-prepend-text-property
  3447. (+ d (match-beginning 0)) (match-end 0)
  3448. 'face 'org-latex-and-export-specials)
  3449. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3450. '(font-lock-multiline t)))))
  3451. rtn)))
  3452. (defun org-restart-font-lock ()
  3453. "Restart font-lock-mode, to force refontification."
  3454. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3455. (font-lock-mode -1)
  3456. (font-lock-mode 1)))
  3457. (defun org-all-targets (&optional radio)
  3458. "Return a list of all targets in this file.
  3459. With optional argument RADIO, only find radio targets."
  3460. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3461. rtn)
  3462. (save-excursion
  3463. (goto-char (point-min))
  3464. (while (re-search-forward re nil t)
  3465. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3466. rtn)))
  3467. (defun org-make-target-link-regexp (targets)
  3468. "Make regular expression matching all strings in TARGETS.
  3469. The regular expression finds the targets also if there is a line break
  3470. between words."
  3471. (and targets
  3472. (concat
  3473. "\\<\\("
  3474. (mapconcat
  3475. (lambda (x)
  3476. (while (string-match " +" x)
  3477. (setq x (replace-match "\\s-+" t t x)))
  3478. x)
  3479. targets
  3480. "\\|")
  3481. "\\)\\>")))
  3482. (defun org-activate-tags (limit)
  3483. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3484. (progn
  3485. (add-text-properties (match-beginning 1) (match-end 1)
  3486. (list 'mouse-face 'highlight
  3487. 'rear-nonsticky org-nonsticky-props
  3488. 'keymap org-mouse-map))
  3489. t)))
  3490. (defun org-outline-level ()
  3491. (save-excursion
  3492. (looking-at outline-regexp)
  3493. (if (match-beginning 1)
  3494. (+ (org-get-string-indentation (match-string 1)) 1000)
  3495. (1- (- (match-end 0) (match-beginning 0))))))
  3496. (defvar org-font-lock-keywords nil)
  3497. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3498. "Regular expression matching a property line.")
  3499. (defvar org-font-lock-hook nil
  3500. "Functions to be called for special font lock stuff.")
  3501. (defun org-font-lock-hook (limit)
  3502. (run-hook-with-args 'org-font-lock-hook limit))
  3503. (defun org-set-font-lock-defaults ()
  3504. (let* ((em org-fontify-emphasized-text)
  3505. (lk org-activate-links)
  3506. (org-font-lock-extra-keywords
  3507. (list
  3508. ;; Call the hook
  3509. '(org-font-lock-hook)
  3510. ;; Headlines
  3511. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3512. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3513. ;; Table lines
  3514. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3515. (1 'org-table t))
  3516. ;; Table internals
  3517. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3518. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3519. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3520. ;; Drawers
  3521. (list org-drawer-regexp '(0 'org-special-keyword t))
  3522. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3523. ;; Properties
  3524. (list org-property-re
  3525. '(1 'org-special-keyword t)
  3526. '(3 'org-property-value t))
  3527. (if org-format-transports-properties-p
  3528. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3529. ;; Links
  3530. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3531. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3532. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3533. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3534. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3535. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3536. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3537. '(org-hide-wide-columns (0 nil append))
  3538. ;; TODO lines
  3539. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3540. '(1 (org-get-todo-face 1) t))
  3541. ;; DONE
  3542. (if org-fontify-done-headline
  3543. (list (concat "^[*]+ +\\<\\("
  3544. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3545. "\\)\\(.*\\)")
  3546. '(2 'org-headline-done t))
  3547. nil)
  3548. ;; Priorities
  3549. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3550. ;; Tags
  3551. '(org-font-lock-add-tag-faces)
  3552. ;; Special keywords
  3553. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3554. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3555. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3556. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3557. ;; Emphasis
  3558. (if em
  3559. (if (featurep 'xemacs)
  3560. '(org-do-emphasis-faces (0 nil append))
  3561. '(org-do-emphasis-faces)))
  3562. ;; Checkboxes
  3563. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3564. 2 'bold prepend)
  3565. (if org-provide-checkbox-statistics
  3566. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3567. (0 (org-get-checkbox-statistics-face) t)))
  3568. ;; Description list items
  3569. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3570. 2 'bold prepend)
  3571. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3572. '(1 'org-archived prepend))
  3573. ;; Specials
  3574. '(org-do-latex-and-special-faces)
  3575. ;; Code
  3576. '(org-activate-code (1 'org-code t))
  3577. ;; COMMENT
  3578. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3579. "\\|" org-quote-string "\\)\\>")
  3580. '(1 'org-special-keyword t))
  3581. '("^#.*" (0 'font-lock-comment-face t))
  3582. )))
  3583. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3584. ;; Now set the full font-lock-keywords
  3585. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3586. (org-set-local 'font-lock-defaults
  3587. '(org-font-lock-keywords t nil nil backward-paragraph))
  3588. (kill-local-variable 'font-lock-keywords) nil))
  3589. (defvar org-m nil)
  3590. (defvar org-l nil)
  3591. (defvar org-f nil)
  3592. (defun org-get-level-face (n)
  3593. "Get the right face for match N in font-lock matching of healdines."
  3594. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3595. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3596. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3597. (cond
  3598. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3599. ((eq n 2) org-f)
  3600. (t (if org-level-color-stars-only nil org-f))))
  3601. (defun org-get-todo-face (kwd)
  3602. "Get the right face for a TODO keyword KWD.
  3603. If KWD is a number, get the corresponding match group."
  3604. (if (numberp kwd) (setq kwd (match-string kwd)))
  3605. (or (cdr (assoc kwd org-todo-keyword-faces))
  3606. (and (member kwd org-done-keywords) 'org-done)
  3607. 'org-todo))
  3608. (defun org-font-lock-add-tag-faces (limit)
  3609. "Add the special tag faces."
  3610. (when (and org-tag-faces org-tags-special-faces-re)
  3611. (while (re-search-forward org-tags-special-faces-re limit t)
  3612. (add-text-properties (match-beginning 1) (match-end 1)
  3613. (list 'face (org-get-tag-face 1)
  3614. 'font-lock-fontified t))
  3615. (backward-char 1))))
  3616. (defun org-get-tag-face (kwd)
  3617. "Get the right face for a TODO keyword KWD.
  3618. If KWD is a number, get the corresponding match group."
  3619. (if (numberp kwd) (setq kwd (match-string kwd)))
  3620. (or (cdr (assoc kwd org-tag-faces))
  3621. 'org-tag))
  3622. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3623. "Remove fontification and activation overlays from links."
  3624. (font-lock-default-unfontify-region beg end)
  3625. (let* ((buffer-undo-list t)
  3626. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3627. (inhibit-modification-hooks t)
  3628. deactivate-mark buffer-file-name buffer-file-truename)
  3629. (remove-text-properties beg end
  3630. '(mouse-face t keymap t org-linked-text t
  3631. invisible t intangible t))))
  3632. ;;;; Visibility cycling, including org-goto and indirect buffer
  3633. ;;; Cycling
  3634. (defvar org-cycle-global-status nil)
  3635. (make-variable-buffer-local 'org-cycle-global-status)
  3636. (defvar org-cycle-subtree-status nil)
  3637. (make-variable-buffer-local 'org-cycle-subtree-status)
  3638. ;;;###autoload
  3639. (defun org-cycle (&optional arg)
  3640. "Visibility cycling for Org-mode.
  3641. - When this function is called with a prefix argument, rotate the entire
  3642. buffer through 3 states (global cycling)
  3643. 1. OVERVIEW: Show only top-level headlines.
  3644. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3645. 3. SHOW ALL: Show everything.
  3646. When called with two C-u C-u prefixes, switch to the startup visibility,
  3647. determined by the variable `org-startup-folded', and by any VISIBILITY
  3648. properties in the buffer.
  3649. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3650. including drawers.
  3651. - When point is at the beginning of a headline, rotate the subtree started
  3652. by this line through 3 different states (local cycling)
  3653. 1. FOLDED: Only the main headline is shown.
  3654. 2. CHILDREN: The main headline and the direct children are shown.
  3655. From this state, you can move to one of the children
  3656. and zoom in further.
  3657. 3. SUBTREE: Show the entire subtree, including body text.
  3658. - When there is a numeric prefix, go up to a heading with level ARG, do
  3659. a `show-subtree' and return to the previous cursor position. If ARG
  3660. is negative, go up that many levels.
  3661. - When point is not at the beginning of a headline, execute the global
  3662. binding for TAB, which is re-indenting the line. See the option
  3663. `org-cycle-emulate-tab' for details.
  3664. - Special case: if point is at the beginning of the buffer and there is
  3665. no headline in line 1, this function will act as if called with prefix arg.
  3666. But only if also the variable `org-cycle-global-at-bob' is t."
  3667. (interactive "P")
  3668. (org-load-modules-maybe)
  3669. (let* ((outline-regexp
  3670. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3671. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3672. outline-regexp))
  3673. (bob-special (and org-cycle-global-at-bob (bobp)
  3674. (not (looking-at outline-regexp))))
  3675. (org-cycle-hook
  3676. (if bob-special
  3677. (delq 'org-optimize-window-after-visibility-change
  3678. (copy-sequence org-cycle-hook))
  3679. org-cycle-hook))
  3680. (pos (point)))
  3681. (if (or bob-special (equal arg '(4)))
  3682. ;; special case: use global cycling
  3683. (setq arg t))
  3684. (cond
  3685. ((equal arg '(16))
  3686. (org-set-startup-visibility)
  3687. (message "Startup visibility, plus VISIBILITY properties"))
  3688. ((equal arg '(64))
  3689. (show-all)
  3690. (message "Entire buffer visible, including drawers"))
  3691. ((org-at-table-p 'any)
  3692. ;; Enter the table or move to the next field in the table
  3693. (or (org-table-recognize-table.el)
  3694. (progn
  3695. (if arg (org-table-edit-field t)
  3696. (org-table-justify-field-maybe)
  3697. (call-interactively 'org-table-next-field)))))
  3698. ((eq arg t) ;; Global cycling
  3699. (cond
  3700. ((and (eq last-command this-command)
  3701. (eq org-cycle-global-status 'overview))
  3702. ;; We just created the overview - now do table of contents
  3703. ;; This can be slow in very large buffers, so indicate action
  3704. (message "CONTENTS...")
  3705. (org-content)
  3706. (message "CONTENTS...done")
  3707. (setq org-cycle-global-status 'contents)
  3708. (run-hook-with-args 'org-cycle-hook 'contents))
  3709. ((and (eq last-command this-command)
  3710. (eq org-cycle-global-status 'contents))
  3711. ;; We just showed the table of contents - now show everything
  3712. (show-all)
  3713. (message "SHOW ALL")
  3714. (setq org-cycle-global-status 'all)
  3715. (run-hook-with-args 'org-cycle-hook 'all))
  3716. (t
  3717. ;; Default action: go to overview
  3718. (org-overview)
  3719. (message "OVERVIEW")
  3720. (setq org-cycle-global-status 'overview)
  3721. (run-hook-with-args 'org-cycle-hook 'overview))))
  3722. ((and org-drawers org-drawer-regexp
  3723. (save-excursion
  3724. (beginning-of-line 1)
  3725. (looking-at org-drawer-regexp)))
  3726. ;; Toggle block visibility
  3727. (org-flag-drawer
  3728. (not (get-char-property (match-end 0) 'invisible))))
  3729. ((integerp arg)
  3730. ;; Show-subtree, ARG levels up from here.
  3731. (save-excursion
  3732. (org-back-to-heading)
  3733. (outline-up-heading (if (< arg 0) (- arg)
  3734. (- (funcall outline-level) arg)))
  3735. (org-show-subtree)))
  3736. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3737. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3738. ;; At a heading: rotate between three different views
  3739. (org-back-to-heading)
  3740. (let ((goal-column 0) eoh eol eos)
  3741. ;; First, some boundaries
  3742. (save-excursion
  3743. (org-back-to-heading)
  3744. (save-excursion
  3745. (beginning-of-line 2)
  3746. (while (and (not (eobp)) ;; this is like `next-line'
  3747. (get-char-property (1- (point)) 'invisible))
  3748. (beginning-of-line 2)) (setq eol (point)))
  3749. (outline-end-of-heading) (setq eoh (point))
  3750. (org-end-of-subtree t)
  3751. (unless (eobp)
  3752. (skip-chars-forward " \t\n")
  3753. (beginning-of-line 1) ; in case this is an item
  3754. )
  3755. (setq eos (1- (point))))
  3756. ;; Find out what to do next and set `this-command'
  3757. (cond
  3758. ((= eos eoh)
  3759. ;; Nothing is hidden behind this heading
  3760. (message "EMPTY ENTRY")
  3761. (setq org-cycle-subtree-status nil)
  3762. (save-excursion
  3763. (goto-char eos)
  3764. (outline-next-heading)
  3765. (if (org-invisible-p) (org-flag-heading nil))))
  3766. ((or (>= eol eos)
  3767. (not (string-match "\\S-" (buffer-substring eol eos))))
  3768. ;; Entire subtree is hidden in one line: open it
  3769. (org-show-entry)
  3770. (show-children)
  3771. (message "CHILDREN")
  3772. (save-excursion
  3773. (goto-char eos)
  3774. (outline-next-heading)
  3775. (if (org-invisible-p) (org-flag-heading nil)))
  3776. (setq org-cycle-subtree-status 'children)
  3777. (run-hook-with-args 'org-cycle-hook 'children))
  3778. ((and (eq last-command this-command)
  3779. (eq org-cycle-subtree-status 'children))
  3780. ;; We just showed the children, now show everything.
  3781. (org-show-subtree)
  3782. (message "SUBTREE")
  3783. (setq org-cycle-subtree-status 'subtree)
  3784. (run-hook-with-args 'org-cycle-hook 'subtree))
  3785. (t
  3786. ;; Default action: hide the subtree.
  3787. (hide-subtree)
  3788. (message "FOLDED")
  3789. (setq org-cycle-subtree-status 'folded)
  3790. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3791. ;; TAB emulation and template completion
  3792. (buffer-read-only (org-back-to-heading))
  3793. ((org-try-structure-completion))
  3794. ((org-try-cdlatex-tab))
  3795. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3796. (or (not (bolp))
  3797. (not (looking-at outline-regexp))))
  3798. (call-interactively (global-key-binding "\t")))
  3799. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3800. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3801. (or (and (eq org-cycle-emulate-tab 'white)
  3802. (= (match-end 0) (point-at-eol)))
  3803. (and (eq org-cycle-emulate-tab 'whitestart)
  3804. (>= (match-end 0) pos))))
  3805. t
  3806. (eq org-cycle-emulate-tab t))
  3807. (call-interactively (global-key-binding "\t")))
  3808. (t (save-excursion
  3809. (org-back-to-heading)
  3810. (org-cycle))))))
  3811. ;;;###autoload
  3812. (defun org-global-cycle (&optional arg)
  3813. "Cycle the global visibility. For details see `org-cycle'.
  3814. With C-u prefix arg, switch to startup visibility.
  3815. With a numeric prefix, show all headlines up to that level."
  3816. (interactive "P")
  3817. (let ((org-cycle-include-plain-lists
  3818. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3819. (cond
  3820. ((integerp arg)
  3821. (show-all)
  3822. (hide-sublevels arg)
  3823. (setq org-cycle-global-status 'contents))
  3824. ((equal arg '(4))
  3825. (org-set-startup-visibility)
  3826. (message "Startup visibility, plus VISIBILITY properties."))
  3827. (t
  3828. (org-cycle '(4))))))
  3829. (defun org-set-startup-visibility ()
  3830. "Set the visibility required by startup options and properties."
  3831. (cond
  3832. ((eq org-startup-folded t)
  3833. (org-cycle '(4)))
  3834. ((eq org-startup-folded 'content)
  3835. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3836. (org-cycle '(4)) (org-cycle '(4)))))
  3837. (org-set-visibility-according-to-property 'no-cleanup)
  3838. (org-cycle-hide-archived-subtrees 'all)
  3839. (org-cycle-hide-drawers 'all)
  3840. (org-cycle-show-empty-lines 'all))
  3841. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3842. "Switch subtree visibilities according to :VISIBILITY: property."
  3843. (interactive)
  3844. (let (state)
  3845. (save-excursion
  3846. (goto-char (point-min))
  3847. (while (re-search-forward
  3848. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3849. nil t)
  3850. (setq state (match-string 1))
  3851. (save-excursion
  3852. (org-back-to-heading t)
  3853. (hide-subtree)
  3854. (org-reveal)
  3855. (cond
  3856. ((equal state '("fold" "folded"))
  3857. (hide-subtree))
  3858. ((equal state "children")
  3859. (org-show-hidden-entry)
  3860. (show-children))
  3861. ((equal state "content")
  3862. (save-excursion
  3863. (save-restriction
  3864. (org-narrow-to-subtree)
  3865. (org-content))))
  3866. ((member state '("all" "showall"))
  3867. (show-subtree)))))
  3868. (unless no-cleanup
  3869. (org-cycle-hide-archived-subtrees 'all)
  3870. (org-cycle-hide-drawers 'all)
  3871. (org-cycle-show-empty-lines 'all)))))
  3872. (defun org-overview ()
  3873. "Switch to overview mode, shoing only top-level headlines.
  3874. Really, this shows all headlines with level equal or greater than the level
  3875. of the first headline in the buffer. This is important, because if the
  3876. first headline is not level one, then (hide-sublevels 1) gives confusing
  3877. results."
  3878. (interactive)
  3879. (let ((level (save-excursion
  3880. (goto-char (point-min))
  3881. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3882. (progn
  3883. (goto-char (match-beginning 0))
  3884. (funcall outline-level))))))
  3885. (and level (hide-sublevels level))))
  3886. (defun org-content (&optional arg)
  3887. "Show all headlines in the buffer, like a table of contents.
  3888. With numerical argument N, show content up to level N."
  3889. (interactive "P")
  3890. (save-excursion
  3891. ;; Visit all headings and show their offspring
  3892. (and (integerp arg) (org-overview))
  3893. (goto-char (point-max))
  3894. (catch 'exit
  3895. (while (and (progn (condition-case nil
  3896. (outline-previous-visible-heading 1)
  3897. (error (goto-char (point-min))))
  3898. t)
  3899. (looking-at outline-regexp))
  3900. (if (integerp arg)
  3901. (show-children (1- arg))
  3902. (show-branches))
  3903. (if (bobp) (throw 'exit nil))))))
  3904. (defun org-optimize-window-after-visibility-change (state)
  3905. "Adjust the window after a change in outline visibility.
  3906. This function is the default value of the hook `org-cycle-hook'."
  3907. (when (get-buffer-window (current-buffer))
  3908. (cond
  3909. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3910. ; ((eq state 'overview) (org-beginning-of-line))
  3911. ((eq state 'content) nil)
  3912. ((eq state 'all) nil)
  3913. ((eq state 'folded) nil)
  3914. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3915. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3916. (defun org-compact-display-after-subtree-move ()
  3917. (let (beg end)
  3918. (save-excursion
  3919. (if (org-up-heading-safe)
  3920. (progn
  3921. (hide-subtree)
  3922. (show-entry)
  3923. (show-children)
  3924. (org-cycle-show-empty-lines 'children)
  3925. (org-cycle-hide-drawers 'children))
  3926. (org-overview)))))
  3927. (defun org-cycle-show-empty-lines (state)
  3928. "Show empty lines above all visible headlines.
  3929. The region to be covered depends on STATE when called through
  3930. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3931. entire buffer covered. Note that an empty line is only shown if there
  3932. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3933. (when (> org-cycle-separator-lines 0)
  3934. (save-excursion
  3935. (let* ((n org-cycle-separator-lines)
  3936. (re (cond
  3937. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3938. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3939. (t (let ((ns (number-to-string (- n 2))))
  3940. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3941. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3942. beg end)
  3943. (cond
  3944. ((memq state '(overview contents t))
  3945. (setq beg (point-min) end (point-max)))
  3946. ((memq state '(children folded))
  3947. (setq beg (point) end (progn (org-end-of-subtree t t)
  3948. (beginning-of-line 2)
  3949. (point)))))
  3950. (when beg
  3951. (goto-char beg)
  3952. (while (re-search-forward re end t)
  3953. (if (not (get-char-property (match-end 1) 'invisible))
  3954. (outline-flag-region
  3955. (match-beginning 1) (match-end 1) nil)))))))
  3956. ;; Never hide empty lines at the end of the file.
  3957. (save-excursion
  3958. (goto-char (point-max))
  3959. (outline-previous-heading)
  3960. (outline-end-of-heading)
  3961. (if (and (looking-at "[ \t\n]+")
  3962. (= (match-end 0) (point-max)))
  3963. (outline-flag-region (point) (match-end 0) nil))))
  3964. (defun org-show-empty-lines-in-parent ()
  3965. "Move to the parent and re-show empty lines before visible headlines."
  3966. (save-excursion
  3967. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3968. (org-cycle-show-empty-lines context))))
  3969. (defun org-cycle-hide-drawers (state)
  3970. "Re-hide all drawers after a visibility state change."
  3971. (when (and (org-mode-p)
  3972. (not (memq state '(overview folded))))
  3973. (save-excursion
  3974. (let* ((globalp (memq state '(contents all)))
  3975. (beg (if globalp (point-min) (point)))
  3976. (end (if globalp (point-max) (org-end-of-subtree t))))
  3977. (goto-char beg)
  3978. (while (re-search-forward org-drawer-regexp end t)
  3979. (org-flag-drawer t))))))
  3980. (defun org-flag-drawer (flag)
  3981. (save-excursion
  3982. (beginning-of-line 1)
  3983. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3984. (let ((b (match-end 0))
  3985. (outline-regexp org-outline-regexp))
  3986. (if (re-search-forward
  3987. "^[ \t]*:END:"
  3988. (save-excursion (outline-next-heading) (point)) t)
  3989. (outline-flag-region b (point-at-eol) flag)
  3990. (error ":END: line missing"))))))
  3991. (defun org-subtree-end-visible-p ()
  3992. "Is the end of the current subtree visible?"
  3993. (pos-visible-in-window-p
  3994. (save-excursion (org-end-of-subtree t) (point))))
  3995. (defun org-first-headline-recenter (&optional N)
  3996. "Move cursor to the first headline and recenter the headline.
  3997. Optional argument N means, put the headline into the Nth line of the window."
  3998. (goto-char (point-min))
  3999. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  4000. (beginning-of-line)
  4001. (recenter (prefix-numeric-value N))))
  4002. ;;; Org-goto
  4003. (defvar org-goto-window-configuration nil)
  4004. (defvar org-goto-marker nil)
  4005. (defvar org-goto-map
  4006. (let ((map (make-sparse-keymap)))
  4007. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  4008. (while (setq cmd (pop cmds))
  4009. (substitute-key-definition cmd cmd map global-map)))
  4010. (suppress-keymap map)
  4011. (org-defkey map "\C-m" 'org-goto-ret)
  4012. (org-defkey map [(return)] 'org-goto-ret)
  4013. (org-defkey map [(left)] 'org-goto-left)
  4014. (org-defkey map [(right)] 'org-goto-right)
  4015. (org-defkey map [(control ?g)] 'org-goto-quit)
  4016. (org-defkey map "\C-i" 'org-cycle)
  4017. (org-defkey map [(tab)] 'org-cycle)
  4018. (org-defkey map [(down)] 'outline-next-visible-heading)
  4019. (org-defkey map [(up)] 'outline-previous-visible-heading)
  4020. (if org-goto-auto-isearch
  4021. (if (fboundp 'define-key-after)
  4022. (define-key-after map [t] 'org-goto-local-auto-isearch)
  4023. nil)
  4024. (org-defkey map "q" 'org-goto-quit)
  4025. (org-defkey map "n" 'outline-next-visible-heading)
  4026. (org-defkey map "p" 'outline-previous-visible-heading)
  4027. (org-defkey map "f" 'outline-forward-same-level)
  4028. (org-defkey map "b" 'outline-backward-same-level)
  4029. (org-defkey map "u" 'outline-up-heading))
  4030. (org-defkey map "/" 'org-occur)
  4031. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  4032. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  4033. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  4034. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  4035. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  4036. map))
  4037. (defconst org-goto-help
  4038. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  4039. RET=jump to location [Q]uit and return to previous location
  4040. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  4041. (defvar org-goto-start-pos) ; dynamically scoped parameter
  4042. ;; FIXME: Docstring doe not mention both interfaces
  4043. (defun org-goto (&optional alternative-interface)
  4044. "Look up a different location in the current file, keeping current visibility.
  4045. When you want look-up or go to a different location in a document, the
  4046. fastest way is often to fold the entire buffer and then dive into the tree.
  4047. This method has the disadvantage, that the previous location will be folded,
  4048. which may not be what you want.
  4049. This command works around this by showing a copy of the current buffer
  4050. in an indirect buffer, in overview mode. You can dive into the tree in
  4051. that copy, use org-occur and incremental search to find a location.
  4052. When pressing RET or `Q', the command returns to the original buffer in
  4053. which the visibility is still unchanged. After RET is will also jump to
  4054. the location selected in the indirect buffer and expose the
  4055. the headline hierarchy above."
  4056. (interactive "P")
  4057. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  4058. (org-refile-use-outline-path t)
  4059. (interface
  4060. (if (not alternative-interface)
  4061. org-goto-interface
  4062. (if (eq org-goto-interface 'outline)
  4063. 'outline-path-completion
  4064. 'outline)))
  4065. (org-goto-start-pos (point))
  4066. (selected-point
  4067. (if (eq interface 'outline)
  4068. (car (org-get-location (current-buffer) org-goto-help))
  4069. (nth 3 (org-refile-get-location "Goto: ")))))
  4070. (if selected-point
  4071. (progn
  4072. (org-mark-ring-push org-goto-start-pos)
  4073. (goto-char selected-point)
  4074. (if (or (org-invisible-p) (org-invisible-p2))
  4075. (org-show-context 'org-goto)))
  4076. (message "Quit"))))
  4077. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4078. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4079. (defvar org-goto-local-auto-isearch-map) ; defined below
  4080. (defun org-get-location (buf help)
  4081. "Let the user select a location in the Org-mode buffer BUF.
  4082. This function uses a recursive edit. It returns the selected position
  4083. or nil."
  4084. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4085. (isearch-hide-immediately nil)
  4086. (isearch-search-fun-function
  4087. (lambda () 'org-goto-local-search-headings))
  4088. (org-goto-selected-point org-goto-exit-command))
  4089. (save-excursion
  4090. (save-window-excursion
  4091. (delete-other-windows)
  4092. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4093. (switch-to-buffer
  4094. (condition-case nil
  4095. (make-indirect-buffer (current-buffer) "*org-goto*")
  4096. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4097. (with-output-to-temp-buffer "*Help*"
  4098. (princ help))
  4099. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4100. (setq buffer-read-only nil)
  4101. (let ((org-startup-truncated t)
  4102. (org-startup-folded nil)
  4103. (org-startup-align-all-tables nil))
  4104. (org-mode)
  4105. (org-overview))
  4106. (setq buffer-read-only t)
  4107. (if (and (boundp 'org-goto-start-pos)
  4108. (integer-or-marker-p org-goto-start-pos))
  4109. (let ((org-show-hierarchy-above t)
  4110. (org-show-siblings t)
  4111. (org-show-following-heading t))
  4112. (goto-char org-goto-start-pos)
  4113. (and (org-invisible-p) (org-show-context)))
  4114. (goto-char (point-min)))
  4115. (org-beginning-of-line)
  4116. (message "Select location and press RET")
  4117. (use-local-map org-goto-map)
  4118. (recursive-edit)
  4119. ))
  4120. (kill-buffer "*org-goto*")
  4121. (cons org-goto-selected-point org-goto-exit-command)))
  4122. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4123. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4124. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4125. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4126. (defun org-goto-local-search-headings (string bound noerror)
  4127. "Search and make sure that any matches are in headlines."
  4128. (catch 'return
  4129. (while (if isearch-forward
  4130. (search-forward string bound noerror)
  4131. (search-backward string bound noerror))
  4132. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4133. (and (member :headline context)
  4134. (not (member :tags context))))
  4135. (throw 'return (point))))))
  4136. (defun org-goto-local-auto-isearch ()
  4137. "Start isearch."
  4138. (interactive)
  4139. (goto-char (point-min))
  4140. (let ((keys (this-command-keys)))
  4141. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4142. (isearch-mode t)
  4143. (isearch-process-search-char (string-to-char keys)))))
  4144. (defun org-goto-ret (&optional arg)
  4145. "Finish `org-goto' by going to the new location."
  4146. (interactive "P")
  4147. (setq org-goto-selected-point (point)
  4148. org-goto-exit-command 'return)
  4149. (throw 'exit nil))
  4150. (defun org-goto-left ()
  4151. "Finish `org-goto' by going to the new location."
  4152. (interactive)
  4153. (if (org-on-heading-p)
  4154. (progn
  4155. (beginning-of-line 1)
  4156. (setq org-goto-selected-point (point)
  4157. org-goto-exit-command 'left)
  4158. (throw 'exit nil))
  4159. (error "Not on a heading")))
  4160. (defun org-goto-right ()
  4161. "Finish `org-goto' by going to the new location."
  4162. (interactive)
  4163. (if (org-on-heading-p)
  4164. (progn
  4165. (setq org-goto-selected-point (point)
  4166. org-goto-exit-command 'right)
  4167. (throw 'exit nil))
  4168. (error "Not on a heading")))
  4169. (defun org-goto-quit ()
  4170. "Finish `org-goto' without cursor motion."
  4171. (interactive)
  4172. (setq org-goto-selected-point nil)
  4173. (setq org-goto-exit-command 'quit)
  4174. (throw 'exit nil))
  4175. ;;; Indirect buffer display of subtrees
  4176. (defvar org-indirect-dedicated-frame nil
  4177. "This is the frame being used for indirect tree display.")
  4178. (defvar org-last-indirect-buffer nil)
  4179. (defun org-tree-to-indirect-buffer (&optional arg)
  4180. "Create indirect buffer and narrow it to current subtree.
  4181. With numerical prefix ARG, go up to this level and then take that tree.
  4182. If ARG is negative, go up that many levels.
  4183. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4184. indirect buffer previously made with this command, to avoid proliferation of
  4185. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4186. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4187. is kept so that you can work with several indirect buffers at the same time.
  4188. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4189. requests that a new frame be made for the new buffer, so that the dedicated
  4190. frame is not changed."
  4191. (interactive "P")
  4192. (let ((cbuf (current-buffer))
  4193. (cwin (selected-window))
  4194. (pos (point))
  4195. beg end level heading ibuf)
  4196. (save-excursion
  4197. (org-back-to-heading t)
  4198. (when (numberp arg)
  4199. (setq level (org-outline-level))
  4200. (if (< arg 0) (setq arg (+ level arg)))
  4201. (while (> (setq level (org-outline-level)) arg)
  4202. (outline-up-heading 1 t)))
  4203. (setq beg (point)
  4204. heading (org-get-heading))
  4205. (org-end-of-subtree t) (setq end (point)))
  4206. (if (and (buffer-live-p org-last-indirect-buffer)
  4207. (not (eq org-indirect-buffer-display 'new-frame))
  4208. (not arg))
  4209. (kill-buffer org-last-indirect-buffer))
  4210. (setq ibuf (org-get-indirect-buffer cbuf)
  4211. org-last-indirect-buffer ibuf)
  4212. (cond
  4213. ((or (eq org-indirect-buffer-display 'new-frame)
  4214. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4215. (select-frame (make-frame))
  4216. (delete-other-windows)
  4217. (switch-to-buffer ibuf)
  4218. (org-set-frame-title heading))
  4219. ((eq org-indirect-buffer-display 'dedicated-frame)
  4220. (raise-frame
  4221. (select-frame (or (and org-indirect-dedicated-frame
  4222. (frame-live-p org-indirect-dedicated-frame)
  4223. org-indirect-dedicated-frame)
  4224. (setq org-indirect-dedicated-frame (make-frame)))))
  4225. (delete-other-windows)
  4226. (switch-to-buffer ibuf)
  4227. (org-set-frame-title (concat "Indirect: " heading)))
  4228. ((eq org-indirect-buffer-display 'current-window)
  4229. (switch-to-buffer ibuf))
  4230. ((eq org-indirect-buffer-display 'other-window)
  4231. (pop-to-buffer ibuf))
  4232. (t (error "Invalid value.")))
  4233. (if (featurep 'xemacs)
  4234. (save-excursion (org-mode) (turn-on-font-lock)))
  4235. (narrow-to-region beg end)
  4236. (show-all)
  4237. (goto-char pos)
  4238. (and (window-live-p cwin) (select-window cwin))))
  4239. (defun org-get-indirect-buffer (&optional buffer)
  4240. (setq buffer (or buffer (current-buffer)))
  4241. (let ((n 1) (base (buffer-name buffer)) bname)
  4242. (while (buffer-live-p
  4243. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4244. (setq n (1+ n)))
  4245. (condition-case nil
  4246. (make-indirect-buffer buffer bname 'clone)
  4247. (error (make-indirect-buffer buffer bname)))))
  4248. (defun org-set-frame-title (title)
  4249. "Set the title of the current frame to the string TITLE."
  4250. ;; FIXME: how to name a single frame in XEmacs???
  4251. (unless (featurep 'xemacs)
  4252. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4253. ;;;; Structure editing
  4254. ;;; Inserting headlines
  4255. (defun org-insert-heading (&optional force-heading)
  4256. "Insert a new heading or item with same depth at point.
  4257. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4258. If point is at the beginning of a headline, insert a sibling before the
  4259. current headline. If point is not at the beginning, do not split the line,
  4260. but create the new headline after the current line."
  4261. (interactive "P")
  4262. (if (= (buffer-size) 0)
  4263. (insert "\n* ")
  4264. (when (or force-heading (not (org-insert-item)))
  4265. (let* ((head (save-excursion
  4266. (condition-case nil
  4267. (progn
  4268. (org-back-to-heading)
  4269. (match-string 0))
  4270. (error "*"))))
  4271. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4272. pos hide-previous previous-pos)
  4273. (cond
  4274. ((and (org-on-heading-p) (bolp)
  4275. (or (bobp)
  4276. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4277. ;; insert before the current line
  4278. (open-line (if blank 2 1)))
  4279. ((and (bolp)
  4280. (or (bobp)
  4281. (save-excursion
  4282. (backward-char 1) (not (org-invisible-p)))))
  4283. ;; insert right here
  4284. nil)
  4285. (t
  4286. ;; somewhere in the line
  4287. (save-excursion
  4288. (setq previous-pos (point-at-bol))
  4289. (end-of-line)
  4290. (setq hide-previous (org-invisible-p)))
  4291. (and org-insert-heading-respect-content (org-show-subtree))
  4292. (let ((split
  4293. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4294. (save-excursion
  4295. (let ((p (point)))
  4296. (goto-char (point-at-bol))
  4297. (and (looking-at org-complex-heading-regexp)
  4298. (> p (match-beginning 4)))))))
  4299. tags pos)
  4300. (cond
  4301. (org-insert-heading-respect-content
  4302. (org-end-of-subtree nil t)
  4303. (or (bolp) (newline))
  4304. (open-line 1))
  4305. ((org-on-heading-p)
  4306. (when hide-previous
  4307. (show-children)
  4308. (org-show-entry))
  4309. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4310. (setq tags (and (match-end 2) (match-string 2)))
  4311. (and (match-end 1)
  4312. (delete-region (match-beginning 1) (match-end 1)))
  4313. (setq pos (point-at-bol))
  4314. (or split (end-of-line 1))
  4315. (delete-horizontal-space)
  4316. (newline (if blank 2 1))
  4317. (when tags
  4318. (save-excursion
  4319. (goto-char pos)
  4320. (end-of-line 1)
  4321. (insert " " tags)
  4322. (org-set-tags nil 'align))))
  4323. (t
  4324. (or split (end-of-line 1))
  4325. (newline (if blank 2 1)))))))
  4326. (insert head) (just-one-space)
  4327. (setq pos (point))
  4328. (end-of-line 1)
  4329. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4330. (when (and org-insert-heading-respect-content hide-previous)
  4331. (save-excursion
  4332. (goto-char previous-pos)
  4333. (hide-subtree)))
  4334. (run-hooks 'org-insert-heading-hook)))))
  4335. (defun org-get-heading (&optional no-tags)
  4336. "Return the heading of the current entry, without the stars."
  4337. (save-excursion
  4338. (org-back-to-heading t)
  4339. (if (looking-at
  4340. (if no-tags
  4341. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4342. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4343. (match-string 1) "")))
  4344. (defun org-insert-heading-after-current ()
  4345. "Insert a new heading with same level as current, after current subtree."
  4346. (interactive)
  4347. (org-back-to-heading)
  4348. (org-insert-heading)
  4349. (org-move-subtree-down)
  4350. (end-of-line 1))
  4351. (defun org-insert-heading-respect-content ()
  4352. (interactive)
  4353. (let ((org-insert-heading-respect-content t))
  4354. (org-insert-heading t)))
  4355. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4356. (interactive "P")
  4357. (let ((org-insert-heading-respect-content t))
  4358. (org-insert-todo-heading force-state t)))
  4359. (defun org-insert-todo-heading (arg &optional force-heading)
  4360. "Insert a new heading with the same level and TODO state as current heading.
  4361. If the heading has no TODO state, or if the state is DONE, use the first
  4362. state (TODO by default). Also with prefix arg, force first state."
  4363. (interactive "P")
  4364. (when (or force-heading (not (org-insert-item 'checkbox)))
  4365. (org-insert-heading force-heading)
  4366. (save-excursion
  4367. (org-back-to-heading)
  4368. (outline-previous-heading)
  4369. (looking-at org-todo-line-regexp))
  4370. (if (or arg
  4371. (not (match-beginning 2))
  4372. (member (match-string 2) org-done-keywords))
  4373. (insert (car org-todo-keywords-1) " ")
  4374. (insert (match-string 2) " "))
  4375. (when org-provide-todo-statistics
  4376. (org-update-parent-todo-statistics))))
  4377. (defun org-insert-subheading (arg)
  4378. "Insert a new subheading and demote it.
  4379. Works for outline headings and for plain lists alike."
  4380. (interactive "P")
  4381. (org-insert-heading arg)
  4382. (cond
  4383. ((org-on-heading-p) (org-do-demote))
  4384. ((org-at-item-p) (org-indent-item 1))))
  4385. (defun org-insert-todo-subheading (arg)
  4386. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4387. Works for outline headings and for plain lists alike."
  4388. (interactive "P")
  4389. (org-insert-todo-heading arg)
  4390. (cond
  4391. ((org-on-heading-p) (org-do-demote))
  4392. ((org-at-item-p) (org-indent-item 1))))
  4393. ;;; Promotion and Demotion
  4394. (defun org-promote-subtree ()
  4395. "Promote the entire subtree.
  4396. See also `org-promote'."
  4397. (interactive)
  4398. (save-excursion
  4399. (org-map-tree 'org-promote))
  4400. (org-fix-position-after-promote))
  4401. (defun org-demote-subtree ()
  4402. "Demote the entire subtree. See `org-demote'.
  4403. See also `org-promote'."
  4404. (interactive)
  4405. (save-excursion
  4406. (org-map-tree 'org-demote))
  4407. (org-fix-position-after-promote))
  4408. (defun org-do-promote ()
  4409. "Promote the current heading higher up the tree.
  4410. If the region is active in `transient-mark-mode', promote all headings
  4411. in the region."
  4412. (interactive)
  4413. (save-excursion
  4414. (if (org-region-active-p)
  4415. (org-map-region 'org-promote (region-beginning) (region-end))
  4416. (org-promote)))
  4417. (org-fix-position-after-promote))
  4418. (defun org-do-demote ()
  4419. "Demote the current heading lower down the tree.
  4420. If the region is active in `transient-mark-mode', demote all headings
  4421. in the region."
  4422. (interactive)
  4423. (save-excursion
  4424. (if (org-region-active-p)
  4425. (org-map-region 'org-demote (region-beginning) (region-end))
  4426. (org-demote)))
  4427. (org-fix-position-after-promote))
  4428. (defun org-fix-position-after-promote ()
  4429. "Make sure that after pro/demotion cursor position is right."
  4430. (let ((pos (point)))
  4431. (when (save-excursion
  4432. (beginning-of-line 1)
  4433. (looking-at org-todo-line-regexp)
  4434. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4435. (cond ((eobp) (insert " "))
  4436. ((eolp) (insert " "))
  4437. ((equal (char-after) ?\ ) (forward-char 1))))))
  4438. (defun org-reduced-level (l)
  4439. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4440. (defun org-get-valid-level (level &optional change)
  4441. "Rectify a level change under the influence of `org-odd-levels-only'
  4442. LEVEL is a current level, CHANGE is by how much the level should be
  4443. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4444. even level numbers will become the next higher odd number."
  4445. (if org-odd-levels-only
  4446. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4447. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4448. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4449. (max 1 (+ level change))))
  4450. (if (boundp 'define-obsolete-function-alias)
  4451. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4452. (define-obsolete-function-alias 'org-get-legal-level
  4453. 'org-get-valid-level)
  4454. (define-obsolete-function-alias 'org-get-legal-level
  4455. 'org-get-valid-level "23.1")))
  4456. (defun org-promote ()
  4457. "Promote the current heading higher up the tree.
  4458. If the region is active in `transient-mark-mode', promote all headings
  4459. in the region."
  4460. (org-back-to-heading t)
  4461. (let* ((level (save-match-data (funcall outline-level)))
  4462. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4463. (diff (abs (- level (length up-head) -1))))
  4464. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4465. (replace-match up-head nil t)
  4466. ;; Fixup tag positioning
  4467. (and org-auto-align-tags (org-set-tags nil t))
  4468. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4469. (defun org-demote ()
  4470. "Demote the current heading lower down the tree.
  4471. If the region is active in `transient-mark-mode', demote all headings
  4472. in the region."
  4473. (org-back-to-heading t)
  4474. (let* ((level (save-match-data (funcall outline-level)))
  4475. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4476. (diff (abs (- level (length down-head) -1))))
  4477. (replace-match down-head nil t)
  4478. ;; Fixup tag positioning
  4479. (and org-auto-align-tags (org-set-tags nil t))
  4480. (if org-adapt-indentation (org-fixup-indentation diff))))
  4481. (defun org-map-tree (fun)
  4482. "Call FUN for every heading underneath the current one."
  4483. (org-back-to-heading)
  4484. (let ((level (funcall outline-level)))
  4485. (save-excursion
  4486. (funcall fun)
  4487. (while (and (progn
  4488. (outline-next-heading)
  4489. (> (funcall outline-level) level))
  4490. (not (eobp)))
  4491. (funcall fun)))))
  4492. (defun org-map-region (fun beg end)
  4493. "Call FUN for every heading between BEG and END."
  4494. (let ((org-ignore-region t))
  4495. (save-excursion
  4496. (setq end (copy-marker end))
  4497. (goto-char beg)
  4498. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4499. (< (point) end))
  4500. (funcall fun))
  4501. (while (and (progn
  4502. (outline-next-heading)
  4503. (< (point) end))
  4504. (not (eobp)))
  4505. (funcall fun)))))
  4506. (defun org-fixup-indentation (diff)
  4507. "Change the indentation in the current entry by DIFF
  4508. However, if any line in the current entry has no indentation, or if it
  4509. would end up with no indentation after the change, nothing at all is done."
  4510. (save-excursion
  4511. (let ((end (save-excursion (outline-next-heading)
  4512. (point-marker)))
  4513. (prohibit (if (> diff 0)
  4514. "^\\S-"
  4515. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4516. col)
  4517. (unless (save-excursion (end-of-line 1)
  4518. (re-search-forward prohibit end t))
  4519. (while (and (< (point) end)
  4520. (re-search-forward "^[ \t]+" end t))
  4521. (goto-char (match-end 0))
  4522. (setq col (current-column))
  4523. (if (< diff 0) (replace-match ""))
  4524. (org-indent-to-column (+ diff col))))
  4525. (move-marker end nil))))
  4526. (defun org-convert-to-odd-levels ()
  4527. "Convert an org-mode file with all levels allowed to one with odd levels.
  4528. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4529. level 5 etc."
  4530. (interactive)
  4531. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4532. (let ((org-odd-levels-only nil) n)
  4533. (save-excursion
  4534. (goto-char (point-min))
  4535. (while (re-search-forward "^\\*\\*+ " nil t)
  4536. (setq n (- (length (match-string 0)) 2))
  4537. (while (>= (setq n (1- n)) 0)
  4538. (org-demote))
  4539. (end-of-line 1))))))
  4540. (defun org-convert-to-oddeven-levels ()
  4541. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4542. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4543. section with an even level, conversion would destroy the structure of the file. An error
  4544. is signaled in this case."
  4545. (interactive)
  4546. (goto-char (point-min))
  4547. ;; First check if there are no even levels
  4548. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4549. (org-show-context t)
  4550. (error "Not all levels are odd in this file. Conversion not possible."))
  4551. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4552. (let ((org-odd-levels-only nil) n)
  4553. (save-excursion
  4554. (goto-char (point-min))
  4555. (while (re-search-forward "^\\*\\*+ " nil t)
  4556. (setq n (/ (1- (length (match-string 0))) 2))
  4557. (while (>= (setq n (1- n)) 0)
  4558. (org-promote))
  4559. (end-of-line 1))))))
  4560. (defun org-tr-level (n)
  4561. "Make N odd if required."
  4562. (if org-odd-levels-only (1+ (/ n 2)) n))
  4563. ;;; Vertical tree motion, cutting and pasting of subtrees
  4564. (defun org-move-subtree-up (&optional arg)
  4565. "Move the current subtree up past ARG headlines of the same level."
  4566. (interactive "p")
  4567. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4568. (defun org-move-subtree-down (&optional arg)
  4569. "Move the current subtree down past ARG headlines of the same level."
  4570. (interactive "p")
  4571. (setq arg (prefix-numeric-value arg))
  4572. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4573. 'outline-get-last-sibling))
  4574. (ins-point (make-marker))
  4575. (cnt (abs arg))
  4576. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4577. ;; Select the tree
  4578. (org-back-to-heading)
  4579. (setq beg0 (point))
  4580. (save-excursion
  4581. (setq ne-beg (org-back-over-empty-lines))
  4582. (setq beg (point)))
  4583. (save-match-data
  4584. (save-excursion (outline-end-of-heading)
  4585. (setq folded (org-invisible-p)))
  4586. (outline-end-of-subtree))
  4587. (outline-next-heading)
  4588. (setq ne-end (org-back-over-empty-lines))
  4589. (setq end (point))
  4590. (goto-char beg0)
  4591. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4592. ;; include less whitespace
  4593. (save-excursion
  4594. (goto-char beg)
  4595. (forward-line (- ne-beg ne-end))
  4596. (setq beg (point))))
  4597. ;; Find insertion point, with error handling
  4598. (while (> cnt 0)
  4599. (or (and (funcall movfunc) (looking-at outline-regexp))
  4600. (progn (goto-char beg0)
  4601. (error "Cannot move past superior level or buffer limit")))
  4602. (setq cnt (1- cnt)))
  4603. (if (> arg 0)
  4604. ;; Moving forward - still need to move over subtree
  4605. (progn (org-end-of-subtree t t)
  4606. (save-excursion
  4607. (org-back-over-empty-lines)
  4608. (or (bolp) (newline)))))
  4609. (setq ne-ins (org-back-over-empty-lines))
  4610. (move-marker ins-point (point))
  4611. (setq txt (buffer-substring beg end))
  4612. (org-save-markers-in-region beg end)
  4613. (delete-region beg end)
  4614. (or (= beg (point-min)) (outline-flag-region (1- beg) beg nil))
  4615. (or (bobp) (outline-flag-region (1- (point)) (point) nil))
  4616. (let ((bbb (point)))
  4617. (insert-before-markers txt)
  4618. (org-reinstall-markers-in-region bbb)
  4619. (move-marker ins-point bbb))
  4620. (or (bolp) (insert "\n"))
  4621. (setq ins-end (point))
  4622. (goto-char ins-point)
  4623. (org-skip-whitespace)
  4624. (when (and (< arg 0)
  4625. (org-first-sibling-p)
  4626. (> ne-ins ne-beg))
  4627. ;; Move whitespace back to beginning
  4628. (save-excursion
  4629. (goto-char ins-end)
  4630. (let ((kill-whole-line t))
  4631. (kill-line (- ne-ins ne-beg)) (point)))
  4632. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4633. (move-marker ins-point nil)
  4634. (org-compact-display-after-subtree-move)
  4635. (org-show-empty-lines-in-parent)
  4636. (unless folded
  4637. (org-show-entry)
  4638. (show-children)
  4639. (org-cycle-hide-drawers 'children))))
  4640. (defvar org-subtree-clip ""
  4641. "Clipboard for cut and paste of subtrees.
  4642. This is actually only a copy of the kill, because we use the normal kill
  4643. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4644. (defvar org-subtree-clip-folded nil
  4645. "Was the last copied subtree folded?
  4646. This is used to fold the tree back after pasting.")
  4647. (defun org-cut-subtree (&optional n)
  4648. "Cut the current subtree into the clipboard.
  4649. With prefix arg N, cut this many sequential subtrees.
  4650. This is a short-hand for marking the subtree and then cutting it."
  4651. (interactive "p")
  4652. (org-copy-subtree n 'cut))
  4653. (defun org-copy-subtree (&optional n cut force-store-markers)
  4654. "Cut the current subtree into the clipboard.
  4655. With prefix arg N, cut this many sequential subtrees.
  4656. This is a short-hand for marking the subtree and then copying it.
  4657. If CUT is non-nil, actually cut the subtree.
  4658. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4659. of some markers in the region, even if CUT is non-nil. This is
  4660. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4661. (interactive "p")
  4662. (let (beg end folded (beg0 (point)))
  4663. (if (interactive-p)
  4664. (org-back-to-heading nil) ; take what looks like a subtree
  4665. (org-back-to-heading t)) ; take what is really there
  4666. (org-back-over-empty-lines)
  4667. (setq beg (point))
  4668. (skip-chars-forward " \t\r\n")
  4669. (save-match-data
  4670. (save-excursion (outline-end-of-heading)
  4671. (setq folded (org-invisible-p)))
  4672. (condition-case nil
  4673. (outline-forward-same-level (1- n))
  4674. (error nil))
  4675. (org-end-of-subtree t t))
  4676. (org-back-over-empty-lines)
  4677. (setq end (point))
  4678. (goto-char beg0)
  4679. (when (> end beg)
  4680. (setq org-subtree-clip-folded folded)
  4681. (when (or cut force-store-markers)
  4682. (org-save-markers-in-region beg end))
  4683. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4684. (setq org-subtree-clip (current-kill 0))
  4685. (message "%s: Subtree(s) with %d characters"
  4686. (if cut "Cut" "Copied")
  4687. (length org-subtree-clip)))))
  4688. (defun org-paste-subtree (&optional level tree for-yank)
  4689. "Paste the clipboard as a subtree, with modification of headline level.
  4690. The entire subtree is promoted or demoted in order to match a new headline
  4691. level.
  4692. If the cursor is at the beginning of a headline, the same level as
  4693. that headline is used to paste the tree
  4694. If not, the new level is derived from the *visible* headings
  4695. before and after the insertion point, and taken to be the inferior headline
  4696. level of the two. So if the previous visible heading is level 3 and the
  4697. next is level 4 (or vice versa), level 4 will be used for insertion.
  4698. This makes sure that the subtree remains an independent subtree and does
  4699. not swallow low level entries.
  4700. You can also force a different level, either by using a numeric prefix
  4701. argument, or by inserting the heading marker by hand. For example, if the
  4702. cursor is after \"*****\", then the tree will be shifted to level 5.
  4703. If optional TREE is given, use this text instead of the kill ring.
  4704. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4705. move back over whitespace before inserting, and move point to the end of
  4706. the inserted text when done."
  4707. (interactive "P")
  4708. (unless (org-kill-is-subtree-p tree)
  4709. (error "%s"
  4710. (substitute-command-keys
  4711. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4712. (let* ((visp (not (org-invisible-p)))
  4713. (txt (or tree (and kill-ring (current-kill 0))))
  4714. (^re (concat "^\\(" outline-regexp "\\)"))
  4715. (re (concat "\\(" outline-regexp "\\)"))
  4716. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4717. (old-level (if (string-match ^re txt)
  4718. (- (match-end 0) (match-beginning 0) 1)
  4719. -1))
  4720. (force-level (cond (level (prefix-numeric-value level))
  4721. ((and (looking-at "[ \t]*$")
  4722. (string-match
  4723. ^re_ (buffer-substring
  4724. (point-at-bol) (point))))
  4725. (- (match-end 1) (match-beginning 1)))
  4726. ((and (bolp)
  4727. (looking-at org-outline-regexp))
  4728. (- (match-end 0) (point) 1))
  4729. (t nil)))
  4730. (previous-level (save-excursion
  4731. (condition-case nil
  4732. (progn
  4733. (outline-previous-visible-heading 1)
  4734. (if (looking-at re)
  4735. (- (match-end 0) (match-beginning 0) 1)
  4736. 1))
  4737. (error 1))))
  4738. (next-level (save-excursion
  4739. (condition-case nil
  4740. (progn
  4741. (or (looking-at outline-regexp)
  4742. (outline-next-visible-heading 1))
  4743. (if (looking-at re)
  4744. (- (match-end 0) (match-beginning 0) 1)
  4745. 1))
  4746. (error 1))))
  4747. (new-level (or force-level (max previous-level next-level)))
  4748. (shift (if (or (= old-level -1)
  4749. (= new-level -1)
  4750. (= old-level new-level))
  4751. 0
  4752. (- new-level old-level)))
  4753. (delta (if (> shift 0) -1 1))
  4754. (func (if (> shift 0) 'org-demote 'org-promote))
  4755. (org-odd-levels-only nil)
  4756. beg end newend)
  4757. ;; Remove the forced level indicator
  4758. (if force-level
  4759. (delete-region (point-at-bol) (point)))
  4760. ;; Paste
  4761. (beginning-of-line 1)
  4762. (unless for-yank (org-back-over-empty-lines))
  4763. (setq beg (point))
  4764. (and (fboundp 'org-id-paste-tracker) (org-id-paste-tracker txt))
  4765. (insert-before-markers txt)
  4766. (unless (string-match "\n\\'" txt) (insert "\n"))
  4767. (setq newend (point))
  4768. (org-reinstall-markers-in-region beg)
  4769. (setq end (point))
  4770. (goto-char beg)
  4771. (skip-chars-forward " \t\n\r")
  4772. (setq beg (point))
  4773. (if (and (org-invisible-p) visp)
  4774. (save-excursion (outline-show-heading)))
  4775. ;; Shift if necessary
  4776. (unless (= shift 0)
  4777. (save-restriction
  4778. (narrow-to-region beg end)
  4779. (while (not (= shift 0))
  4780. (org-map-region func (point-min) (point-max))
  4781. (setq shift (+ delta shift)))
  4782. (goto-char (point-min))
  4783. (setq newend (point-max))))
  4784. (when (or (interactive-p) for-yank)
  4785. (message "Clipboard pasted as level %d subtree" new-level))
  4786. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4787. kill-ring
  4788. (eq org-subtree-clip (current-kill 0))
  4789. org-subtree-clip-folded)
  4790. ;; The tree was folded before it was killed/copied
  4791. (hide-subtree))
  4792. (and for-yank (goto-char newend))))
  4793. (defun org-kill-is-subtree-p (&optional txt)
  4794. "Check if the current kill is an outline subtree, or a set of trees.
  4795. Returns nil if kill does not start with a headline, or if the first
  4796. headline level is not the largest headline level in the tree.
  4797. So this will actually accept several entries of equal levels as well,
  4798. which is OK for `org-paste-subtree'.
  4799. If optional TXT is given, check this string instead of the current kill."
  4800. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4801. (start-level (and kill
  4802. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4803. org-outline-regexp "\\)")
  4804. kill)
  4805. (- (match-end 2) (match-beginning 2) 1)))
  4806. (re (concat "^" org-outline-regexp))
  4807. (start (1+ (or (match-beginning 2) -1))))
  4808. (if (not start-level)
  4809. (progn
  4810. nil) ;; does not even start with a heading
  4811. (catch 'exit
  4812. (while (setq start (string-match re kill (1+ start)))
  4813. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4814. (throw 'exit nil)))
  4815. t))))
  4816. (defvar org-markers-to-move nil
  4817. "Markers that should be moved with a cut-and-paste operation.
  4818. Those markers are stored together with their positions relative to
  4819. the start of the region.")
  4820. (defun org-save-markers-in-region (beg end)
  4821. "Check markers in region.
  4822. If these markers are between BEG and END, record their position relative
  4823. to BEG, so that after moving the block of text, we can put the markers back
  4824. into place.
  4825. This function gets called just before an entry or tree gets cut from the
  4826. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4827. called immediately, to move the markers with the entries."
  4828. (setq org-markers-to-move nil)
  4829. (when (featurep 'org-clock)
  4830. (org-clock-save-markers-for-cut-and-paste beg end))
  4831. (when (featurep 'org-agenda)
  4832. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4833. (defun org-check-and-save-marker (marker beg end)
  4834. "Check if MARKER is between BEG and END.
  4835. If yes, remember the marker and the distance to BEG."
  4836. (when (and (marker-buffer marker)
  4837. (equal (marker-buffer marker) (current-buffer)))
  4838. (if (and (>= marker beg) (< marker end))
  4839. (push (cons marker (- marker beg)) org-markers-to-move))))
  4840. (defun org-reinstall-markers-in-region (beg)
  4841. "Move all remembered markers to their position relative to BEG."
  4842. (mapc (lambda (x)
  4843. (move-marker (car x) (+ beg (cdr x))))
  4844. org-markers-to-move)
  4845. (setq org-markers-to-move nil))
  4846. (defun org-narrow-to-subtree ()
  4847. "Narrow buffer to the current subtree."
  4848. (interactive)
  4849. (save-excursion
  4850. (save-match-data
  4851. (narrow-to-region
  4852. (progn (org-back-to-heading) (point))
  4853. (progn (org-end-of-subtree t) (point))))))
  4854. ;;; Outline Sorting
  4855. (defun org-sort (with-case)
  4856. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4857. Optional argument WITH-CASE means sort case-sensitively."
  4858. (interactive "P")
  4859. (if (org-at-table-p)
  4860. (org-call-with-arg 'org-table-sort-lines with-case)
  4861. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4862. (defun org-sort-remove-invisible (s)
  4863. (remove-text-properties 0 (length s) org-rm-props s)
  4864. (while (string-match org-bracket-link-regexp s)
  4865. (setq s (replace-match (if (match-end 2)
  4866. (match-string 3 s)
  4867. (match-string 1 s)) t t s)))
  4868. s)
  4869. (defvar org-priority-regexp) ; defined later in the file
  4870. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4871. "Sort entries on a certain level of an outline tree.
  4872. If there is an active region, the entries in the region are sorted.
  4873. Else, if the cursor is before the first entry, sort the top-level items.
  4874. Else, the children of the entry at point are sorted.
  4875. Sorting can be alphabetically, numerically, and by date/time as given by
  4876. the first time stamp in the entry. The command prompts for the sorting
  4877. type unless it has been given to the function through the SORTING-TYPE
  4878. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4879. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4880. called with point at the beginning of the record. It must return either
  4881. a string or a number that should serve as the sorting key for that record.
  4882. Comparing entries ignores case by default. However, with an optional argument
  4883. WITH-CASE, the sorting considers case as well."
  4884. (interactive "P")
  4885. (let ((case-func (if with-case 'identity 'downcase))
  4886. start beg end stars re re2
  4887. txt what tmp plain-list-p)
  4888. ;; Find beginning and end of region to sort
  4889. (cond
  4890. ((org-region-active-p)
  4891. ;; we will sort the region
  4892. (setq end (region-end)
  4893. what "region")
  4894. (goto-char (region-beginning))
  4895. (if (not (org-on-heading-p)) (outline-next-heading))
  4896. (setq start (point)))
  4897. ((org-at-item-p)
  4898. ;; we will sort this plain list
  4899. (org-beginning-of-item-list) (setq start (point))
  4900. (org-end-of-item-list) (setq end (point))
  4901. (goto-char start)
  4902. (setq plain-list-p t
  4903. what "plain list"))
  4904. ((or (org-on-heading-p)
  4905. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4906. ;; we will sort the children of the current headline
  4907. (org-back-to-heading)
  4908. (setq start (point)
  4909. end (progn (org-end-of-subtree t t)
  4910. (org-back-over-empty-lines)
  4911. (point))
  4912. what "children")
  4913. (goto-char start)
  4914. (show-subtree)
  4915. (outline-next-heading))
  4916. (t
  4917. ;; we will sort the top-level entries in this file
  4918. (goto-char (point-min))
  4919. (or (org-on-heading-p) (outline-next-heading))
  4920. (setq start (point) end (point-max) what "top-level")
  4921. (goto-char start)
  4922. (show-all)))
  4923. (setq beg (point))
  4924. (if (>= beg end) (error "Nothing to sort"))
  4925. (unless plain-list-p
  4926. (looking-at "\\(\\*+\\)")
  4927. (setq stars (match-string 1)
  4928. re (concat "^" (regexp-quote stars) " +")
  4929. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4930. txt (buffer-substring beg end))
  4931. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4932. (if (and (not (equal stars "*")) (string-match re2 txt))
  4933. (error "Region to sort contains a level above the first entry")))
  4934. (unless sorting-type
  4935. (message
  4936. (if plain-list-p
  4937. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4938. "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:")
  4939. what)
  4940. (setq sorting-type (read-char-exclusive))
  4941. (and (= (downcase sorting-type) ?f)
  4942. (setq getkey-func
  4943. (org-ido-completing-read "Sort using function: "
  4944. obarray 'fboundp t nil nil))
  4945. (setq getkey-func (intern getkey-func)))
  4946. (and (= (downcase sorting-type) ?r)
  4947. (setq property
  4948. (org-ido-completing-read "Property: "
  4949. (mapcar 'list (org-buffer-property-keys t))
  4950. nil t))))
  4951. (message "Sorting entries...")
  4952. (save-restriction
  4953. (narrow-to-region start end)
  4954. (let ((dcst (downcase sorting-type))
  4955. (now (current-time)))
  4956. (sort-subr
  4957. (/= dcst sorting-type)
  4958. ;; This function moves to the beginning character of the "record" to
  4959. ;; be sorted.
  4960. (if plain-list-p
  4961. (lambda nil
  4962. (if (org-at-item-p) t (goto-char (point-max))))
  4963. (lambda nil
  4964. (if (re-search-forward re nil t)
  4965. (goto-char (match-beginning 0))
  4966. (goto-char (point-max)))))
  4967. ;; This function moves to the last character of the "record" being
  4968. ;; sorted.
  4969. (if plain-list-p
  4970. 'org-end-of-item
  4971. (lambda nil
  4972. (save-match-data
  4973. (condition-case nil
  4974. (outline-forward-same-level 1)
  4975. (error
  4976. (goto-char (point-max)))))))
  4977. ;; This function returns the value that gets sorted against.
  4978. (if plain-list-p
  4979. (lambda nil
  4980. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4981. (cond
  4982. ((= dcst ?n)
  4983. (string-to-number (buffer-substring (match-end 0)
  4984. (point-at-eol))))
  4985. ((= dcst ?a)
  4986. (buffer-substring (match-end 0) (point-at-eol)))
  4987. ((= dcst ?t)
  4988. (if (re-search-forward org-ts-regexp
  4989. (point-at-eol) t)
  4990. (org-time-string-to-time (match-string 0))
  4991. now))
  4992. ((= dcst ?f)
  4993. (if getkey-func
  4994. (progn
  4995. (setq tmp (funcall getkey-func))
  4996. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4997. tmp)
  4998. (error "Invalid key function `%s'" getkey-func)))
  4999. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5000. (lambda nil
  5001. (cond
  5002. ((= dcst ?n)
  5003. (if (looking-at org-complex-heading-regexp)
  5004. (string-to-number (match-string 4))
  5005. nil))
  5006. ((= dcst ?a)
  5007. (if (looking-at org-complex-heading-regexp)
  5008. (funcall case-func (match-string 4))
  5009. nil))
  5010. ((= dcst ?t)
  5011. (if (re-search-forward org-ts-regexp
  5012. (save-excursion
  5013. (forward-line 2)
  5014. (point)) t)
  5015. (org-time-string-to-time (match-string 0))
  5016. now))
  5017. ((= dcst ?p)
  5018. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  5019. (string-to-char (match-string 2))
  5020. org-default-priority))
  5021. ((= dcst ?r)
  5022. (or (org-entry-get nil property) ""))
  5023. ((= dcst ?o)
  5024. (if (looking-at org-complex-heading-regexp)
  5025. (- 9999 (length (member (match-string 2)
  5026. org-todo-keywords-1)))))
  5027. ((= dcst ?f)
  5028. (if getkey-func
  5029. (progn
  5030. (setq tmp (funcall getkey-func))
  5031. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  5032. tmp)
  5033. (error "Invalid key function `%s'" getkey-func)))
  5034. (t (error "Invalid sorting type `%c'" sorting-type)))))
  5035. nil
  5036. (cond
  5037. ((= dcst ?a) 'string<)
  5038. ((= dcst ?t) 'time-less-p)
  5039. (t nil)))))
  5040. (message "Sorting entries...done")))
  5041. (defun org-do-sort (table what &optional with-case sorting-type)
  5042. "Sort TABLE of WHAT according to SORTING-TYPE.
  5043. The user will be prompted for the SORTING-TYPE if the call to this
  5044. function does not specify it. WHAT is only for the prompt, to indicate
  5045. what is being sorted. The sorting key will be extracted from
  5046. the car of the elements of the table.
  5047. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  5048. (unless sorting-type
  5049. (message
  5050. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  5051. what)
  5052. (setq sorting-type (read-char-exclusive)))
  5053. (let ((dcst (downcase sorting-type))
  5054. extractfun comparefun)
  5055. ;; Define the appropriate functions
  5056. (cond
  5057. ((= dcst ?n)
  5058. (setq extractfun 'string-to-number
  5059. comparefun (if (= dcst sorting-type) '< '>)))
  5060. ((= dcst ?a)
  5061. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5062. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5063. comparefun (if (= dcst sorting-type)
  5064. 'string<
  5065. (lambda (a b) (and (not (string< a b))
  5066. (not (string= a b)))))))
  5067. ((= dcst ?t)
  5068. (setq extractfun
  5069. (lambda (x)
  5070. (if (string-match org-ts-regexp x)
  5071. (time-to-seconds
  5072. (org-time-string-to-time (match-string 0 x)))
  5073. 0))
  5074. comparefun (if (= dcst sorting-type) '< '>)))
  5075. (t (error "Invalid sorting type `%c'" sorting-type)))
  5076. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5077. table)
  5078. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5079. ;;; Editing source examples
  5080. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5081. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5082. (defvar org-edit-src-force-single-line nil)
  5083. (defvar org-edit-src-from-org-mode nil)
  5084. (defvar org-edit-src-picture nil)
  5085. (define-minor-mode org-exit-edit-mode
  5086. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5087. (defun org-edit-src-code ()
  5088. "Edit the source code example at point.
  5089. An indirect buffer is created, and that buffer is then narrowed to the
  5090. example at point and switched to the correct language mode. When done,
  5091. exit by killing the buffer with \\[org-edit-src-exit]."
  5092. (interactive)
  5093. (let ((line (org-current-line))
  5094. (case-fold-search t)
  5095. (msg (substitute-command-keys
  5096. "Edit, then exit with C-c ' (C-c and single quote)"))
  5097. (info (org-edit-src-find-region-and-lang))
  5098. (org-mode-p (eq major-mode 'org-mode))
  5099. beg end lang lang-f single)
  5100. (if (not info)
  5101. nil
  5102. (setq beg (nth 0 info)
  5103. end (nth 1 info)
  5104. lang (nth 2 info)
  5105. single (nth 3 info)
  5106. lang-f (intern (concat lang "-mode")))
  5107. (unless (functionp lang-f)
  5108. (error "No such language mode: %s" lang-f))
  5109. (goto-line line)
  5110. (if (get-buffer "*Org Edit Src Example*")
  5111. (kill-buffer "*Org Edit Src Example*"))
  5112. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5113. "*Org Edit Src Example*"))
  5114. (narrow-to-region beg end)
  5115. (remove-text-properties beg end '(display nil invisible nil
  5116. intangible nil))
  5117. (let ((org-inhibit-startup t))
  5118. (funcall lang-f))
  5119. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5120. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5121. (when org-mode-p
  5122. (goto-char (point-min))
  5123. (while (re-search-forward "^," nil t)
  5124. (replace-match "")))
  5125. (goto-line line)
  5126. (org-exit-edit-mode)
  5127. (org-set-local 'header-line-format msg)
  5128. (message "%s" msg)
  5129. t)))
  5130. (defun org-edit-fixed-width-region ()
  5131. "Edit the fixed-width ascii drawing at point.
  5132. This must be a region where each line starts with ca colon followed by
  5133. a space character.
  5134. An indirect buffer is created, and that buffer is then narrowed to the
  5135. example at point and switched to artist-mode. When done,
  5136. exit by killing the buffer with \\[org-edit-src-exit]."
  5137. (interactive)
  5138. (let ((line (org-current-line))
  5139. (case-fold-search t)
  5140. (msg (substitute-command-keys
  5141. "Edit, then exit with C-c ' (C-c and single quote)"))
  5142. (org-mode-p (eq major-mode 'org-mode))
  5143. beg end lang lang-f)
  5144. (beginning-of-line 1)
  5145. (if (looking-at "[ \t]*[^:\n \t]")
  5146. nil
  5147. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5148. (setq beg (point) end beg)
  5149. (save-excursion
  5150. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5151. (setq beg (point-at-bol 2))
  5152. (setq beg (point))))
  5153. (save-excursion
  5154. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5155. (setq end (1- (match-beginning 0)))
  5156. (setq end (point))))
  5157. (goto-line line))
  5158. (if (get-buffer "*Org Edit Picture*")
  5159. (kill-buffer "*Org Edit Picture*"))
  5160. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5161. "*Org Edit Picture*"))
  5162. (narrow-to-region beg end)
  5163. (remove-text-properties beg end '(display nil invisible nil
  5164. intangible nil))
  5165. (when (fboundp 'font-lock-unfontify-region)
  5166. (font-lock-unfontify-region (point-min) (point-max)))
  5167. (cond
  5168. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5169. (fundamental-mode)
  5170. (artist-mode 1))
  5171. (t (funcall org-edit-fixed-width-region-mode)))
  5172. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5173. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5174. (set (make-local-variable 'org-edit-src-picture) t)
  5175. (goto-char (point-min))
  5176. (while (re-search-forward "^[ \t]*: ?" nil t)
  5177. (replace-match ""))
  5178. (goto-line line)
  5179. (org-exit-edit-mode)
  5180. (org-set-local 'header-line-format msg)
  5181. (message "%s" msg)
  5182. t)))
  5183. (defun org-edit-src-find-region-and-lang ()
  5184. "Find the region and language for a local edit.
  5185. Return a list with beginning and end of the region, a string representing
  5186. the language, a switch telling of the content should be in a single line."
  5187. (let ((re-list
  5188. (append
  5189. org-edit-src-region-extra
  5190. '(
  5191. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5192. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5193. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5194. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5195. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5196. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5197. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5198. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5199. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5200. ("^#\\+html:" "\n" "html" single-line)
  5201. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5202. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5203. ("^#\\+latex:" "\n" "latex" single-line)
  5204. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5205. ("^#\\+ascii:" "\n" "ascii" single-line)
  5206. )))
  5207. (pos (point))
  5208. re re1 re2 single beg end lang)
  5209. (catch 'exit
  5210. (while (setq entry (pop re-list))
  5211. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5212. single (nth 3 entry))
  5213. (save-excursion
  5214. (if (or (looking-at re1)
  5215. (re-search-backward re1 nil t))
  5216. (progn
  5217. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5218. (if (and (re-search-forward re2 nil t)
  5219. (>= (match-end 0) pos))
  5220. (throw 'exit (list beg (match-beginning 0) lang single))))
  5221. (if (or (looking-at re2)
  5222. (re-search-forward re2 nil t))
  5223. (progn
  5224. (setq end (match-beginning 0))
  5225. (if (and (re-search-backward re1 nil t)
  5226. (<= (match-beginning 0) pos))
  5227. (throw 'exit
  5228. (list (match-end 0) end
  5229. (org-edit-src-get-lang lang) single)))))))))))
  5230. (defun org-edit-src-get-lang (lang)
  5231. "Extract the src language."
  5232. (let ((m (match-string 0)))
  5233. (cond
  5234. ((stringp lang) lang)
  5235. ((integerp lang) (match-string lang))
  5236. ((and (eq lang 'lang)
  5237. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5238. (match-string 1 m))
  5239. ((and (eq lang 'style)
  5240. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5241. (match-string 1 m))
  5242. (t "fundamental"))))
  5243. (defun org-edit-src-exit ()
  5244. "Exit special edit and protect problematic lines."
  5245. (interactive)
  5246. (unless (buffer-base-buffer (current-buffer))
  5247. (error "This is not an indirect buffer, something is wrong..."))
  5248. (unless (> (point-min) 1)
  5249. (error "This buffer is not narrowed, something is wrong..."))
  5250. (goto-char (point-min))
  5251. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5252. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5253. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5254. (goto-char (point-min))
  5255. (while (re-search-forward "\n" nil t)
  5256. (replace-match " "))
  5257. (goto-char (point-min))
  5258. (if (looking-at "\\s-*") (replace-match " "))
  5259. (if (re-search-forward "\\s-+\\'" nil t)
  5260. (replace-match "")))
  5261. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5262. (goto-char (point-min))
  5263. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5264. (replace-match ",\\1"))
  5265. (when font-lock-mode
  5266. (font-lock-unfontify-region (point-min) (point-max)))
  5267. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5268. (when (org-bound-and-true-p org-edit-src-picture)
  5269. (untabify (point-min) (point-max))
  5270. (goto-char (point-min))
  5271. (while (re-search-forward "^" nil t)
  5272. (replace-match ": "))
  5273. (when font-lock-mode
  5274. (font-lock-unfontify-region (point-min) (point-max)))
  5275. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5276. (kill-buffer (current-buffer))
  5277. (and (org-mode-p) (org-restart-font-lock)))
  5278. ;;; The orgstruct minor mode
  5279. ;; Define a minor mode which can be used in other modes in order to
  5280. ;; integrate the org-mode structure editing commands.
  5281. ;; This is really a hack, because the org-mode structure commands use
  5282. ;; keys which normally belong to the major mode. Here is how it
  5283. ;; works: The minor mode defines all the keys necessary to operate the
  5284. ;; structure commands, but wraps the commands into a function which
  5285. ;; tests if the cursor is currently at a headline or a plain list
  5286. ;; item. If that is the case, the structure command is used,
  5287. ;; temporarily setting many Org-mode variables like regular
  5288. ;; expressions for filling etc. However, when any of those keys is
  5289. ;; used at a different location, function uses `key-binding' to look
  5290. ;; up if the key has an associated command in another currently active
  5291. ;; keymap (minor modes, major mode, global), and executes that
  5292. ;; command. There might be problems if any of the keys is otherwise
  5293. ;; used as a prefix key.
  5294. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5295. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5296. ;; addresses this by checking explicitly for both bindings.
  5297. (defvar orgstruct-mode-map (make-sparse-keymap)
  5298. "Keymap for the minor `orgstruct-mode'.")
  5299. (defvar org-local-vars nil
  5300. "List of local variables, for use by `orgstruct-mode'")
  5301. ;;;###autoload
  5302. (define-minor-mode orgstruct-mode
  5303. "Toggle the minor more `orgstruct-mode'.
  5304. This mode is for using Org-mode structure commands in other modes.
  5305. The following key behave as if Org-mode was active, if the cursor
  5306. is on a headline, or on a plain list item (both in the definition
  5307. of Org-mode).
  5308. M-up Move entry/item up
  5309. M-down Move entry/item down
  5310. M-left Promote
  5311. M-right Demote
  5312. M-S-up Move entry/item up
  5313. M-S-down Move entry/item down
  5314. M-S-left Promote subtree
  5315. M-S-right Demote subtree
  5316. M-q Fill paragraph and items like in Org-mode
  5317. C-c ^ Sort entries
  5318. C-c - Cycle list bullet
  5319. TAB Cycle item visibility
  5320. M-RET Insert new heading/item
  5321. S-M-RET Insert new TODO heading / Chekbox item
  5322. C-c C-c Set tags / toggle checkbox"
  5323. nil " OrgStruct" nil
  5324. (org-load-modules-maybe)
  5325. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5326. ;;;###autoload
  5327. (defun turn-on-orgstruct ()
  5328. "Unconditionally turn on `orgstruct-mode'."
  5329. (orgstruct-mode 1))
  5330. ;;;###autoload
  5331. (defun turn-on-orgstruct++ ()
  5332. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5333. In addition to setting orgstruct-mode, this also exports all indentation and
  5334. autofilling variables from org-mode into the buffer. Note that turning
  5335. off orgstruct-mode will *not* remove these additional settings."
  5336. (orgstruct-mode 1)
  5337. (let (var val)
  5338. (mapc
  5339. (lambda (x)
  5340. (when (string-match
  5341. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5342. (symbol-name (car x)))
  5343. (setq var (car x) val (nth 1 x))
  5344. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5345. org-local-vars)))
  5346. (defun orgstruct-error ()
  5347. "Error when there is no default binding for a structure key."
  5348. (interactive)
  5349. (error "This key has no function outside structure elements"))
  5350. (defun orgstruct-setup ()
  5351. "Setup orgstruct keymaps."
  5352. (let ((nfunc 0)
  5353. (bindings
  5354. (list
  5355. '([(meta up)] org-metaup)
  5356. '([(meta down)] org-metadown)
  5357. '([(meta left)] org-metaleft)
  5358. '([(meta right)] org-metaright)
  5359. '([(meta shift up)] org-shiftmetaup)
  5360. '([(meta shift down)] org-shiftmetadown)
  5361. '([(meta shift left)] org-shiftmetaleft)
  5362. '([(meta shift right)] org-shiftmetaright)
  5363. '([(shift up)] org-shiftup)
  5364. '([(shift down)] org-shiftdown)
  5365. '([(shift left)] org-shiftleft)
  5366. '([(shift right)] org-shiftright)
  5367. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5368. '("\M-q" fill-paragraph)
  5369. '("\C-c^" org-sort)
  5370. '("\C-c-" org-cycle-list-bullet)))
  5371. elt key fun cmd)
  5372. (while (setq elt (pop bindings))
  5373. (setq nfunc (1+ nfunc))
  5374. (setq key (org-key (car elt))
  5375. fun (nth 1 elt)
  5376. cmd (orgstruct-make-binding fun nfunc key))
  5377. (org-defkey orgstruct-mode-map key cmd))
  5378. ;; Special treatment needed for TAB and RET
  5379. (org-defkey orgstruct-mode-map [(tab)]
  5380. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5381. (org-defkey orgstruct-mode-map "\C-i"
  5382. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5383. (org-defkey orgstruct-mode-map "\M-\C-m"
  5384. (orgstruct-make-binding 'org-insert-heading 105
  5385. "\M-\C-m" [(meta return)]))
  5386. (org-defkey orgstruct-mode-map [(meta return)]
  5387. (orgstruct-make-binding 'org-insert-heading 106
  5388. [(meta return)] "\M-\C-m"))
  5389. (org-defkey orgstruct-mode-map [(shift meta return)]
  5390. (orgstruct-make-binding 'org-insert-todo-heading 107
  5391. [(meta return)] "\M-\C-m"))
  5392. (unless org-local-vars
  5393. (setq org-local-vars (org-get-local-variables)))
  5394. t))
  5395. (defun orgstruct-make-binding (fun n &rest keys)
  5396. "Create a function for binding in the structure minor mode.
  5397. FUN is the command to call inside a table. N is used to create a unique
  5398. command name. KEYS are keys that should be checked in for a command
  5399. to execute outside of tables."
  5400. (eval
  5401. (list 'defun
  5402. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5403. '(arg)
  5404. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5405. "Outside of structure, run the binding of `"
  5406. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5407. "'.")
  5408. '(interactive "p")
  5409. (list 'if
  5410. '(org-context-p 'headline 'item)
  5411. (list 'org-run-like-in-org-mode (list 'quote fun))
  5412. (list 'let '(orgstruct-mode)
  5413. (list 'call-interactively
  5414. (append '(or)
  5415. (mapcar (lambda (k)
  5416. (list 'key-binding k))
  5417. keys)
  5418. '('orgstruct-error))))))))
  5419. (defun org-context-p (&rest contexts)
  5420. "Check if local context is any of CONTEXTS.
  5421. Possible values in the list of contexts are `table', `headline', and `item'."
  5422. (let ((pos (point)))
  5423. (goto-char (point-at-bol))
  5424. (prog1 (or (and (memq 'table contexts)
  5425. (looking-at "[ \t]*|"))
  5426. (and (memq 'headline contexts)
  5427. ;;????????? (looking-at "\\*+"))
  5428. (looking-at outline-regexp))
  5429. (and (memq 'item contexts)
  5430. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5431. (goto-char pos))))
  5432. (defun org-get-local-variables ()
  5433. "Return a list of all local variables in an org-mode buffer."
  5434. (let (varlist)
  5435. (with-current-buffer (get-buffer-create "*Org tmp*")
  5436. (erase-buffer)
  5437. (org-mode)
  5438. (setq varlist (buffer-local-variables)))
  5439. (kill-buffer "*Org tmp*")
  5440. (delq nil
  5441. (mapcar
  5442. (lambda (x)
  5443. (setq x
  5444. (if (symbolp x)
  5445. (list x)
  5446. (list (car x) (list 'quote (cdr x)))))
  5447. (if (string-match
  5448. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5449. (symbol-name (car x)))
  5450. x nil))
  5451. varlist))))
  5452. ;;;###autoload
  5453. (defun org-run-like-in-org-mode (cmd)
  5454. (org-load-modules-maybe)
  5455. (unless org-local-vars
  5456. (setq org-local-vars (org-get-local-variables)))
  5457. (eval (list 'let org-local-vars
  5458. (list 'call-interactively (list 'quote cmd)))))
  5459. ;;;; Archiving
  5460. (defun org-get-category (&optional pos)
  5461. "Get the category applying to position POS."
  5462. (get-text-property (or pos (point)) 'org-category))
  5463. (defun org-refresh-category-properties ()
  5464. "Refresh category text properties in the buffer."
  5465. (let ((def-cat (cond
  5466. ((null org-category)
  5467. (if buffer-file-name
  5468. (file-name-sans-extension
  5469. (file-name-nondirectory buffer-file-name))
  5470. "???"))
  5471. ((symbolp org-category) (symbol-name org-category))
  5472. (t org-category)))
  5473. beg end cat pos optionp)
  5474. (org-unmodified
  5475. (save-excursion
  5476. (save-restriction
  5477. (widen)
  5478. (goto-char (point-min))
  5479. (put-text-property (point) (point-max) 'org-category def-cat)
  5480. (while (re-search-forward
  5481. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5482. (setq pos (match-end 0)
  5483. optionp (equal (char-after (match-beginning 0)) ?#)
  5484. cat (org-trim (match-string 2)))
  5485. (if optionp
  5486. (setq beg (point-at-bol) end (point-max))
  5487. (org-back-to-heading t)
  5488. (setq beg (point) end (org-end-of-subtree t t)))
  5489. (put-text-property beg end 'org-category cat)
  5490. (goto-char pos)))))))
  5491. ;;;; Link Stuff
  5492. ;;; Link abbreviations
  5493. (defun org-link-expand-abbrev (link)
  5494. "Apply replacements as defined in `org-link-abbrev-alist."
  5495. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5496. (let* ((key (match-string 1 link))
  5497. (as (or (assoc key org-link-abbrev-alist-local)
  5498. (assoc key org-link-abbrev-alist)))
  5499. (tag (and (match-end 2) (match-string 3 link)))
  5500. rpl)
  5501. (if (not as)
  5502. link
  5503. (setq rpl (cdr as))
  5504. (cond
  5505. ((symbolp rpl) (funcall rpl tag))
  5506. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5507. ((string-match "%h" rpl)
  5508. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5509. (t (concat rpl tag)))))
  5510. link))
  5511. ;;; Storing and inserting links
  5512. (defvar org-insert-link-history nil
  5513. "Minibuffer history for links inserted with `org-insert-link'.")
  5514. (defvar org-stored-links nil
  5515. "Contains the links stored with `org-store-link'.")
  5516. (defvar org-store-link-plist nil
  5517. "Plist with info about the most recently link created with `org-store-link'.")
  5518. (defvar org-link-protocols nil
  5519. "Link protocols added to Org-mode using `org-add-link-type'.")
  5520. (defvar org-store-link-functions nil
  5521. "List of functions that are called to create and store a link.
  5522. Each function will be called in turn until one returns a non-nil
  5523. value. Each function should check if it is responsible for creating
  5524. this link (for example by looking at the major mode).
  5525. If not, it must exit and return nil.
  5526. If yes, it should return a non-nil value after a calling
  5527. `org-store-link-props' with a list of properties and values.
  5528. Special properties are:
  5529. :type The link prefix. like \"http\". This must be given.
  5530. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5531. This is obligatory as well.
  5532. :description Optional default description for the second pair
  5533. of brackets in an Org-mode link. The user can still change
  5534. this when inserting this link into an Org-mode buffer.
  5535. In addition to these, any additional properties can be specified
  5536. and then used in remember templates.")
  5537. (defun org-add-link-type (type &optional follow export)
  5538. "Add TYPE to the list of `org-link-types'.
  5539. Re-compute all regular expressions depending on `org-link-types'
  5540. FOLLOW and EXPORT are two functions.
  5541. FOLLOW should take the link path as the single argument and do whatever
  5542. is necessary to follow the link, for example find a file or display
  5543. a mail message.
  5544. EXPORT should format the link path for export to one of the export formats.
  5545. It should be a function accepting three arguments:
  5546. path the path of the link, the text after the prefix (like \"http:\")
  5547. desc the description of the link, if any, nil if there was no descripton
  5548. format the export format, a symbol like `html' or `latex'.
  5549. The function may use the FORMAT information to return different values
  5550. depending on the format. The return value will be put literally into
  5551. the exported file.
  5552. Org-mode has a built-in default for exporting links. If you are happy with
  5553. this default, there is no need to define an export function for the link
  5554. type. For a simple example of an export function, see `org-bbdb.el'."
  5555. (add-to-list 'org-link-types type t)
  5556. (org-make-link-regexps)
  5557. (if (assoc type org-link-protocols)
  5558. (setcdr (assoc type org-link-protocols) (list follow export))
  5559. (push (list type follow export) org-link-protocols)))
  5560. ;;;###autoload
  5561. (defun org-store-link (arg)
  5562. "\\<org-mode-map>Store an org-link to the current location.
  5563. This link is added to `org-stored-links' and can later be inserted
  5564. into an org-buffer with \\[org-insert-link].
  5565. For some link types, a prefix arg is interpreted:
  5566. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5567. For file links, arg negates `org-context-in-file-links'."
  5568. (interactive "P")
  5569. (org-load-modules-maybe)
  5570. (setq org-store-link-plist nil) ; reset
  5571. (let (link cpltxt desc description search txt)
  5572. (cond
  5573. ((run-hook-with-args-until-success 'org-store-link-functions)
  5574. (setq link (plist-get org-store-link-plist :link)
  5575. desc (or (plist-get org-store-link-plist :description) link)))
  5576. ((eq major-mode 'calendar-mode)
  5577. (let ((cd (calendar-cursor-to-date)))
  5578. (setq link
  5579. (format-time-string
  5580. (car org-time-stamp-formats)
  5581. (apply 'encode-time
  5582. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5583. nil nil nil))))
  5584. (org-store-link-props :type "calendar" :date cd)))
  5585. ((eq major-mode 'w3-mode)
  5586. (setq cpltxt (url-view-url t)
  5587. link (org-make-link cpltxt))
  5588. (org-store-link-props :type "w3" :url (url-view-url t)))
  5589. ((eq major-mode 'w3m-mode)
  5590. (setq cpltxt (or w3m-current-title w3m-current-url)
  5591. link (org-make-link w3m-current-url))
  5592. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5593. ((setq search (run-hook-with-args-until-success
  5594. 'org-create-file-search-functions))
  5595. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5596. "::" search))
  5597. (setq cpltxt (or description link)))
  5598. ((eq major-mode 'image-mode)
  5599. (setq cpltxt (concat "file:"
  5600. (abbreviate-file-name buffer-file-name))
  5601. link (org-make-link cpltxt))
  5602. (org-store-link-props :type "image" :file buffer-file-name))
  5603. ((eq major-mode 'dired-mode)
  5604. ;; link to the file in the current line
  5605. (setq cpltxt (concat "file:"
  5606. (abbreviate-file-name
  5607. (expand-file-name
  5608. (dired-get-filename nil t))))
  5609. link (org-make-link cpltxt)))
  5610. ((and buffer-file-name (org-mode-p))
  5611. (cond
  5612. ((org-in-regexp "<<\\(.*?\\)>>")
  5613. (setq cpltxt
  5614. (concat "file:"
  5615. (abbreviate-file-name buffer-file-name)
  5616. "::" (match-string 1))
  5617. link (org-make-link cpltxt)))
  5618. ((and (featurep 'org-id)
  5619. (or (eq org-link-to-org-use-id t)
  5620. (and (eq org-link-to-org-use-id 'create-if-interactive)
  5621. (interactive-p))
  5622. (and org-link-to-org-use-id
  5623. (condition-case nil
  5624. (org-entry-get nil "ID")
  5625. (error nil)))))
  5626. ;; We can make a link using the ID.
  5627. (setq link (condition-case nil
  5628. (org-id-store-link)
  5629. (error
  5630. ;; probably before first headling, link to file only
  5631. (concat "file:"
  5632. (abbreviate-file-name buffer-file-name))))))
  5633. (t
  5634. ;; Just link to current headline
  5635. (setq cpltxt (concat "file:"
  5636. (abbreviate-file-name buffer-file-name)))
  5637. ;; Add a context search string
  5638. (when (org-xor org-context-in-file-links arg)
  5639. (setq txt (cond
  5640. ((org-on-heading-p) nil)
  5641. ((org-region-active-p)
  5642. (buffer-substring (region-beginning) (region-end)))
  5643. (t nil)))
  5644. (when (or (null txt) (string-match "\\S-" txt))
  5645. (setq cpltxt
  5646. (concat cpltxt "::"
  5647. (condition-case nil
  5648. (org-make-org-heading-search-string txt)
  5649. (error "")))
  5650. desc "NONE")))
  5651. (if (string-match "::\\'" cpltxt)
  5652. (setq cpltxt (substring cpltxt 0 -2)))
  5653. (setq link (org-make-link cpltxt)))))
  5654. ((buffer-file-name (buffer-base-buffer))
  5655. ;; Just link to this file here.
  5656. (setq cpltxt (concat "file:"
  5657. (abbreviate-file-name
  5658. (buffer-file-name (buffer-base-buffer)))))
  5659. ;; Add a context string
  5660. (when (org-xor org-context-in-file-links arg)
  5661. (setq txt (if (org-region-active-p)
  5662. (buffer-substring (region-beginning) (region-end))
  5663. (buffer-substring (point-at-bol) (point-at-eol))))
  5664. ;; Only use search option if there is some text.
  5665. (when (string-match "\\S-" txt)
  5666. (setq cpltxt
  5667. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5668. desc "NONE")))
  5669. (setq link (org-make-link cpltxt)))
  5670. ((interactive-p)
  5671. (error "Cannot link to a buffer which is not visiting a file"))
  5672. (t (setq link nil)))
  5673. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5674. (setq link (or link cpltxt)
  5675. desc (or desc cpltxt))
  5676. (if (equal desc "NONE") (setq desc nil))
  5677. (if (and (interactive-p) link)
  5678. (progn
  5679. (setq org-stored-links
  5680. (cons (list link desc) org-stored-links))
  5681. (message "Stored: %s" (or desc link)))
  5682. (and link (org-make-link-string link desc)))))
  5683. (defun org-store-link-props (&rest plist)
  5684. "Store link properties, extract names and addresses."
  5685. (let (x adr)
  5686. (when (setq x (plist-get plist :from))
  5687. (setq adr (mail-extract-address-components x))
  5688. (setq plist (plist-put plist :fromname (car adr)))
  5689. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5690. (when (setq x (plist-get plist :to))
  5691. (setq adr (mail-extract-address-components x))
  5692. (setq plist (plist-put plist :toname (car adr)))
  5693. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5694. (let ((from (plist-get plist :from))
  5695. (to (plist-get plist :to)))
  5696. (when (and from to org-from-is-user-regexp)
  5697. (setq plist
  5698. (plist-put plist :fromto
  5699. (if (string-match org-from-is-user-regexp from)
  5700. (concat "to %t")
  5701. (concat "from %f"))))))
  5702. (setq org-store-link-plist plist))
  5703. (defun org-add-link-props (&rest plist)
  5704. "Add these properties to the link property list."
  5705. (let (key value)
  5706. (while plist
  5707. (setq key (pop plist) value (pop plist))
  5708. (setq org-store-link-plist
  5709. (plist-put org-store-link-plist key value)))))
  5710. (defun org-email-link-description (&optional fmt)
  5711. "Return the description part of an email link.
  5712. This takes information from `org-store-link-plist' and formats it
  5713. according to FMT (default from `org-email-link-description-format')."
  5714. (setq fmt (or fmt org-email-link-description-format))
  5715. (let* ((p org-store-link-plist)
  5716. (to (plist-get p :toaddress))
  5717. (from (plist-get p :fromaddress))
  5718. (table
  5719. (list
  5720. (cons "%c" (plist-get p :fromto))
  5721. (cons "%F" (plist-get p :from))
  5722. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5723. (cons "%T" (plist-get p :to))
  5724. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5725. (cons "%s" (plist-get p :subject))
  5726. (cons "%m" (plist-get p :message-id)))))
  5727. (when (string-match "%c" fmt)
  5728. ;; Check if the user wrote this message
  5729. (if (and org-from-is-user-regexp from to
  5730. (save-match-data (string-match org-from-is-user-regexp from)))
  5731. (setq fmt (replace-match "to %t" t t fmt))
  5732. (setq fmt (replace-match "from %f" t t fmt))))
  5733. (org-replace-escapes fmt table)))
  5734. (defun org-make-org-heading-search-string (&optional string heading)
  5735. "Make search string for STRING or current headline."
  5736. (interactive)
  5737. (let ((s (or string (org-get-heading))))
  5738. (unless (and string (not heading))
  5739. ;; We are using a headline, clean up garbage in there.
  5740. (if (string-match org-todo-regexp s)
  5741. (setq s (replace-match "" t t s)))
  5742. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5743. (setq s (replace-match "" t t s)))
  5744. (setq s (org-trim s))
  5745. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5746. org-comment-string "\\)") s)
  5747. (setq s (replace-match "" t t s)))
  5748. (while (string-match org-ts-regexp s)
  5749. (setq s (replace-match "" t t s))))
  5750. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5751. (setq s (replace-match " " t t s)))
  5752. (or string (setq s (concat "*" s))) ; Add * for headlines
  5753. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5754. (defun org-make-link (&rest strings)
  5755. "Concatenate STRINGS."
  5756. (apply 'concat strings))
  5757. (defun org-make-link-string (link &optional description)
  5758. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5759. (unless (string-match "\\S-" link)
  5760. (error "Empty link"))
  5761. (when (stringp description)
  5762. ;; Remove brackets from the description, they are fatal.
  5763. (while (string-match "\\[" description)
  5764. (setq description (replace-match "{" t t description)))
  5765. (while (string-match "\\]" description)
  5766. (setq description (replace-match "}" t t description))))
  5767. (when (equal (org-link-escape link) description)
  5768. ;; No description needed, it is identical
  5769. (setq description nil))
  5770. (when (and (not description)
  5771. (not (equal link (org-link-escape link))))
  5772. (setq description (org-extract-attributes link)))
  5773. (concat "[[" (org-link-escape link) "]"
  5774. (if description (concat "[" description "]") "")
  5775. "]"))
  5776. (defconst org-link-escape-chars
  5777. '((?\ . "%20")
  5778. (?\[ . "%5B")
  5779. (?\] . "%5D")
  5780. (?\340 . "%E0") ; `a
  5781. (?\342 . "%E2") ; ^a
  5782. (?\347 . "%E7") ; ,c
  5783. (?\350 . "%E8") ; `e
  5784. (?\351 . "%E9") ; 'e
  5785. (?\352 . "%EA") ; ^e
  5786. (?\356 . "%EE") ; ^i
  5787. (?\364 . "%F4") ; ^o
  5788. (?\371 . "%F9") ; `u
  5789. (?\373 . "%FB") ; ^u
  5790. (?\; . "%3B")
  5791. (?? . "%3F")
  5792. (?= . "%3D")
  5793. (?+ . "%2B")
  5794. )
  5795. "Association list of escapes for some characters problematic in links.
  5796. This is the list that is used for internal purposes.")
  5797. (defconst org-link-escape-chars-browser
  5798. '((?\ . "%20")) ; 32 for the SPC char
  5799. "Association list of escapes for some characters problematic in links.
  5800. This is the list that is used before handing over to the browser.")
  5801. (defun org-link-escape (text &optional table)
  5802. "Escape characters in TEXT that are problematic for links."
  5803. (setq table (or table org-link-escape-chars))
  5804. (when text
  5805. (let ((re (mapconcat (lambda (x) (regexp-quote
  5806. (char-to-string (car x))))
  5807. table "\\|")))
  5808. (while (string-match re text)
  5809. (setq text
  5810. (replace-match
  5811. (cdr (assoc (string-to-char (match-string 0 text))
  5812. table))
  5813. t t text)))
  5814. text)))
  5815. (defun org-link-unescape (text &optional table)
  5816. "Reverse the action of `org-link-escape'."
  5817. (setq table (or table org-link-escape-chars))
  5818. (when text
  5819. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5820. table "\\|")))
  5821. (while (string-match re text)
  5822. (setq text
  5823. (replace-match
  5824. (char-to-string (car (rassoc (match-string 0 text) table)))
  5825. t t text)))
  5826. text)))
  5827. (defun org-xor (a b)
  5828. "Exclusive or."
  5829. (if a (not b) b))
  5830. (defun org-get-header (header)
  5831. "Find a header field in the current buffer."
  5832. (save-excursion
  5833. (goto-char (point-min))
  5834. (let ((case-fold-search t) s)
  5835. (cond
  5836. ((eq header 'from)
  5837. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5838. (setq s (match-string 1)))
  5839. (while (string-match "\"" s)
  5840. (setq s (replace-match "" t t s)))
  5841. (if (string-match "[<(].*" s)
  5842. (setq s (replace-match "" t t s))))
  5843. ((eq header 'message-id)
  5844. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5845. (setq s (match-string 1))))
  5846. ((eq header 'subject)
  5847. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5848. (setq s (match-string 1)))))
  5849. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5850. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5851. s)))
  5852. (defun org-fixup-message-id-for-http (s)
  5853. "Replace special characters in a message id, so it can be used in an http query."
  5854. (while (string-match "<" s)
  5855. (setq s (replace-match "%3C" t t s)))
  5856. (while (string-match ">" s)
  5857. (setq s (replace-match "%3E" t t s)))
  5858. (while (string-match "@" s)
  5859. (setq s (replace-match "%40" t t s)))
  5860. s)
  5861. ;;;###autoload
  5862. (defun org-insert-link-global ()
  5863. "Insert a link like Org-mode does.
  5864. This command can be called in any mode to insert a link in Org-mode syntax."
  5865. (interactive)
  5866. (org-load-modules-maybe)
  5867. (org-run-like-in-org-mode 'org-insert-link))
  5868. (defun org-insert-link (&optional complete-file link-location)
  5869. "Insert a link. At the prompt, enter the link.
  5870. Completion can be used to insert any of the link protocol prefixes like
  5871. http or ftp in use.
  5872. The history can be used to select a link previously stored with
  5873. `org-store-link'. When the empty string is entered (i.e. if you just
  5874. press RET at the prompt), the link defaults to the most recently
  5875. stored link. As SPC triggers completion in the minibuffer, you need to
  5876. use M-SPC or C-q SPC to force the insertion of a space character.
  5877. You will also be prompted for a description, and if one is given, it will
  5878. be displayed in the buffer instead of the link.
  5879. If there is already a link at point, this command will allow you to edit link
  5880. and description parts.
  5881. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5882. be selected using completion. The path to the file will be relative to the
  5883. current directory if the file is in the current directory or a subdirectory.
  5884. Otherwise, the link will be the absolute path as completed in the minibuffer
  5885. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5886. option `org-link-file-path-type'.
  5887. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5888. the current directory or below.
  5889. With three \\[universal-argument] prefixes, negate the meaning of
  5890. `org-keep-stored-link-after-insertion'.
  5891. If `org-make-link-description-function' is non-nil, this function will be
  5892. called with the link target, and the result will be the default
  5893. link description.
  5894. If the LINK-LOCATION parameter is non-nil, this value will be
  5895. used as the link location instead of reading one interactively."
  5896. (interactive "P")
  5897. (let* ((wcf (current-window-configuration))
  5898. (region (if (org-region-active-p)
  5899. (buffer-substring (region-beginning) (region-end))))
  5900. (remove (and region (list (region-beginning) (region-end))))
  5901. (desc region)
  5902. tmphist ; byte-compile incorrectly complains about this
  5903. (link link-location)
  5904. entry file)
  5905. (cond
  5906. (link-location) ; specified by arg, just use it.
  5907. ((org-in-regexp org-bracket-link-regexp 1)
  5908. ;; We do have a link at point, and we are going to edit it.
  5909. (setq remove (list (match-beginning 0) (match-end 0)))
  5910. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5911. (setq link (read-string "Link: "
  5912. (org-link-unescape
  5913. (org-match-string-no-properties 1)))))
  5914. ((or (org-in-regexp org-angle-link-re)
  5915. (org-in-regexp org-plain-link-re))
  5916. ;; Convert to bracket link
  5917. (setq remove (list (match-beginning 0) (match-end 0))
  5918. link (read-string "Link: "
  5919. (org-remove-angle-brackets (match-string 0)))))
  5920. ((member complete-file '((4) (16)))
  5921. ;; Completing read for file names.
  5922. (setq file (read-file-name "File: "))
  5923. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5924. (pwd1 (file-name-as-directory (abbreviate-file-name
  5925. (expand-file-name ".")))))
  5926. (cond
  5927. ((equal complete-file '(16))
  5928. (setq link (org-make-link
  5929. "file:"
  5930. (abbreviate-file-name (expand-file-name file)))))
  5931. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5932. (setq link (org-make-link "file:" (match-string 1 file))))
  5933. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5934. (expand-file-name file))
  5935. (setq link (org-make-link
  5936. "file:" (match-string 1 (expand-file-name file)))))
  5937. (t (setq link (org-make-link "file:" file))))))
  5938. (t
  5939. ;; Read link, with completion for stored links.
  5940. (with-output-to-temp-buffer "*Org Links*"
  5941. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5942. (when org-stored-links
  5943. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5944. (princ (mapconcat
  5945. (lambda (x)
  5946. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5947. (reverse org-stored-links) "\n"))))
  5948. (let ((cw (selected-window)))
  5949. (select-window (get-buffer-window "*Org Links*"))
  5950. (org-fit-window-to-buffer)
  5951. (setq truncate-lines t)
  5952. (select-window cw))
  5953. ;; Fake a link history, containing the stored links.
  5954. (setq tmphist (append (mapcar 'car org-stored-links)
  5955. org-insert-link-history))
  5956. (unwind-protect
  5957. (setq link (org-completing-read
  5958. "Link: "
  5959. (append
  5960. (mapcar (lambda (x) (list (concat (car x) ":")))
  5961. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5962. (mapcar (lambda (x) (list (concat x ":")))
  5963. org-link-types))
  5964. nil nil nil
  5965. 'tmphist
  5966. (or (car (car org-stored-links)))))
  5967. (set-window-configuration wcf)
  5968. (kill-buffer "*Org Links*"))
  5969. (setq entry (assoc link org-stored-links))
  5970. (or entry (push link org-insert-link-history))
  5971. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5972. (not org-keep-stored-link-after-insertion))
  5973. (setq org-stored-links (delq (assoc link org-stored-links)
  5974. org-stored-links)))
  5975. (setq desc (or desc (nth 1 entry)))))
  5976. (if (string-match org-plain-link-re link)
  5977. ;; URL-like link, normalize the use of angular brackets.
  5978. (setq link (org-make-link (org-remove-angle-brackets link))))
  5979. ;; Check if we are linking to the current file with a search option
  5980. ;; If yes, simplify the link by using only the search option.
  5981. (when (and buffer-file-name
  5982. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  5983. (let* ((path (match-string 1 link))
  5984. (case-fold-search nil)
  5985. (search (match-string 2 link)))
  5986. (save-match-data
  5987. (if (equal (file-truename buffer-file-name) (file-truename path))
  5988. ;; We are linking to this same file, with a search option
  5989. (setq link search)))))
  5990. ;; Check if we can/should use a relative path. If yes, simplify the link
  5991. (when (string-match "^file:\\(.*\\)" link)
  5992. (let* ((path (match-string 1 link))
  5993. (origpath path)
  5994. (case-fold-search nil))
  5995. (cond
  5996. ((or (eq org-link-file-path-type 'absolute)
  5997. (equal complete-file '(16)))
  5998. (setq path (abbreviate-file-name (expand-file-name path))))
  5999. ((eq org-link-file-path-type 'noabbrev)
  6000. (setq path (expand-file-name path)))
  6001. ((eq org-link-file-path-type 'relative)
  6002. (setq path (file-relative-name path)))
  6003. (t
  6004. (save-match-data
  6005. (if (string-match (concat "^" (regexp-quote
  6006. (file-name-as-directory
  6007. (expand-file-name "."))))
  6008. (expand-file-name path))
  6009. ;; We are linking a file with relative path name.
  6010. (setq path (substring (expand-file-name path)
  6011. (match-end 0)))
  6012. (setq path (abbreviate-file-name (expand-file-name path)))))))
  6013. (setq link (concat "file:" path))
  6014. (if (equal desc origpath)
  6015. (setq desc path))))
  6016. (if org-make-link-description-function
  6017. (setq desc (funcall org-make-link-description-function link desc)))
  6018. (setq desc (read-string "Description: " desc))
  6019. (unless (string-match "\\S-" desc) (setq desc nil))
  6020. (if remove (apply 'delete-region remove))
  6021. (insert (org-make-link-string link desc))))
  6022. (defun org-completing-read (&rest args)
  6023. "Completing-read with SPACE being a normal character."
  6024. (let ((minibuffer-local-completion-map
  6025. (copy-keymap minibuffer-local-completion-map)))
  6026. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6027. (apply 'org-ido-completing-read args)))
  6028. (defun org-ido-completing-read (&rest args)
  6029. "Completing-read using `ido-mode' speedups if available"
  6030. (if (and org-completion-use-ido
  6031. (fboundp 'ido-completing-read)
  6032. (boundp 'ido-mode) ido-mode
  6033. (listp (second args)))
  6034. (apply 'ido-completing-read (concat (car args)) (cdr args))
  6035. (apply 'completing-read args)))
  6036. (defun org-extract-attributes (s)
  6037. "Extract the attributes cookie from a string and set as text property."
  6038. (let (a attr (start 0) key value)
  6039. (save-match-data
  6040. (when (string-match "{{\\([^}]+\\)}}$" s)
  6041. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  6042. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  6043. (setq key (match-string 1 a) value (match-string 2 a)
  6044. start (match-end 0)
  6045. attr (plist-put attr (intern key) value))))
  6046. (org-add-props s nil 'org-attr attr))
  6047. s))
  6048. (defun org-attributes-to-string (plist)
  6049. "Format a property list into an HTML attribute list."
  6050. (let ((s "") key value)
  6051. (while plist
  6052. (setq key (pop plist) value (pop plist))
  6053. (and value
  6054. (setq s (concat s " " (symbol-name key) "=\"" value "\""))))
  6055. s))
  6056. ;;; Opening/following a link
  6057. (defvar org-link-search-failed nil)
  6058. (defun org-next-link ()
  6059. "Move forward to the next link.
  6060. If the link is in hidden text, expose it."
  6061. (interactive)
  6062. (when (and org-link-search-failed (eq this-command last-command))
  6063. (goto-char (point-min))
  6064. (message "Link search wrapped back to beginning of buffer"))
  6065. (setq org-link-search-failed nil)
  6066. (let* ((pos (point))
  6067. (ct (org-context))
  6068. (a (assoc :link ct)))
  6069. (if a (goto-char (nth 2 a)))
  6070. (if (re-search-forward org-any-link-re nil t)
  6071. (progn
  6072. (goto-char (match-beginning 0))
  6073. (if (org-invisible-p) (org-show-context)))
  6074. (goto-char pos)
  6075. (setq org-link-search-failed t)
  6076. (error "No further link found"))))
  6077. (defun org-previous-link ()
  6078. "Move backward to the previous link.
  6079. If the link is in hidden text, expose it."
  6080. (interactive)
  6081. (when (and org-link-search-failed (eq this-command last-command))
  6082. (goto-char (point-max))
  6083. (message "Link search wrapped back to end of buffer"))
  6084. (setq org-link-search-failed nil)
  6085. (let* ((pos (point))
  6086. (ct (org-context))
  6087. (a (assoc :link ct)))
  6088. (if a (goto-char (nth 1 a)))
  6089. (if (re-search-backward org-any-link-re nil t)
  6090. (progn
  6091. (goto-char (match-beginning 0))
  6092. (if (org-invisible-p) (org-show-context)))
  6093. (goto-char pos)
  6094. (setq org-link-search-failed t)
  6095. (error "No further link found"))))
  6096. (defun org-translate-link (s)
  6097. "Translate a link string if a translation function has been defined."
  6098. (if (and org-link-translation-function
  6099. (fboundp org-link-translation-function)
  6100. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6101. (progn
  6102. (setq s (funcall org-link-translation-function
  6103. (match-string 1) (match-string 2)))
  6104. (concat (car s) ":" (cdr s)))
  6105. s))
  6106. (defun org-translate-link-from-planner (type path)
  6107. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6108. This is still an experimental function, your mileage may vary."
  6109. (cond
  6110. ((member type '("http" "https" "news" "ftp"))
  6111. ;; standard Internet links are the same.
  6112. nil)
  6113. ((and (equal type "irc") (string-match "^//" path))
  6114. ;; Planner has two / at the beginning of an irc link, we have 1.
  6115. ;; We should have zero, actually....
  6116. (setq path (substring path 1)))
  6117. ((and (equal type "lisp") (string-match "^/" path))
  6118. ;; Planner has a slash, we do not.
  6119. (setq type "elisp" path (substring path 1)))
  6120. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6121. ;; A typical message link. Planner has the id after the fina slash,
  6122. ;; we separate it with a hash mark
  6123. (setq path (concat (match-string 1 path) "#"
  6124. (org-remove-angle-brackets (match-string 2 path)))))
  6125. )
  6126. (cons type path))
  6127. (defun org-find-file-at-mouse (ev)
  6128. "Open file link or URL at mouse."
  6129. (interactive "e")
  6130. (mouse-set-point ev)
  6131. (org-open-at-point 'in-emacs))
  6132. (defun org-open-at-mouse (ev)
  6133. "Open file link or URL at mouse."
  6134. (interactive "e")
  6135. (mouse-set-point ev)
  6136. (if (eq major-mode 'org-agenda-mode)
  6137. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6138. (org-open-at-point))
  6139. (defvar org-window-config-before-follow-link nil
  6140. "The window configuration before following a link.
  6141. This is saved in case the need arises to restore it.")
  6142. (defvar org-open-link-marker (make-marker)
  6143. "Marker pointing to the location where `org-open-at-point; was called.")
  6144. ;;;###autoload
  6145. (defun org-open-at-point-global ()
  6146. "Follow a link like Org-mode does.
  6147. This command can be called in any mode to follow a link that has
  6148. Org-mode syntax."
  6149. (interactive)
  6150. (org-run-like-in-org-mode 'org-open-at-point))
  6151. ;;;###autoload
  6152. (defun org-open-link-from-string (s &optional arg)
  6153. "Open a link in the string S, as if it was in Org-mode."
  6154. (interactive "sLink: \nP")
  6155. (with-temp-buffer
  6156. (let ((org-inhibit-startup t))
  6157. (org-mode)
  6158. (insert s)
  6159. (goto-char (point-min))
  6160. (org-open-at-point arg))))
  6161. (defun org-open-at-point (&optional in-emacs)
  6162. "Open link at or after point.
  6163. If there is no link at point, this function will search forward up to
  6164. the end of the current subtree.
  6165. Normally, files will be opened by an appropriate application. If the
  6166. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6167. With a double prefix argument, try to open outside of Emacs, in the
  6168. application the system uses for this file type."
  6169. (interactive "P")
  6170. (org-load-modules-maybe)
  6171. (move-marker org-open-link-marker (point))
  6172. (setq org-window-config-before-follow-link (current-window-configuration))
  6173. (org-remove-occur-highlights nil nil t)
  6174. (if (org-at-timestamp-p t)
  6175. (org-follow-timestamp-link)
  6176. (let (type path link line search (pos (point)))
  6177. (catch 'match
  6178. (save-excursion
  6179. (skip-chars-forward "^]\n\r")
  6180. (when (org-in-regexp org-bracket-link-regexp)
  6181. (setq link (org-extract-attributes
  6182. (org-link-unescape (org-match-string-no-properties 1))))
  6183. (while (string-match " *\n *" link)
  6184. (setq link (replace-match " " t t link)))
  6185. (setq link (org-link-expand-abbrev link))
  6186. (cond
  6187. ((or (file-name-absolute-p link)
  6188. (string-match "^\\.\\.?/" link))
  6189. (setq type "file" path link))
  6190. ((string-match org-link-re-with-space3 link)
  6191. (setq type (match-string 1 link) path (match-string 2 link)))
  6192. (t (setq type "thisfile" path link)))
  6193. (throw 'match t)))
  6194. (when (get-text-property (point) 'org-linked-text)
  6195. (setq type "thisfile"
  6196. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6197. (1+ (point)) (point))
  6198. path (buffer-substring
  6199. (previous-single-property-change pos 'org-linked-text)
  6200. (next-single-property-change pos 'org-linked-text)))
  6201. (throw 'match t))
  6202. (save-excursion
  6203. (when (or (org-in-regexp org-angle-link-re)
  6204. (org-in-regexp org-plain-link-re))
  6205. (setq type (match-string 1) path (match-string 2))
  6206. (throw 'match t)))
  6207. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6208. (setq type "tree-match"
  6209. path (match-string 1))
  6210. (throw 'match t))
  6211. (save-excursion
  6212. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6213. (setq type "tags"
  6214. path (match-string 1))
  6215. (while (string-match ":" path)
  6216. (setq path (replace-match "+" t t path)))
  6217. (throw 'match t))))
  6218. (unless path
  6219. (error "No link found"))
  6220. ;; Remove any trailing spaces in path
  6221. (if (string-match " +\\'" path)
  6222. (setq path (replace-match "" t t path)))
  6223. (if (and org-link-translation-function
  6224. (fboundp org-link-translation-function))
  6225. ;; Check if we need to translate the link
  6226. (let ((tmp (funcall org-link-translation-function type path)))
  6227. (setq type (car tmp) path (cdr tmp))))
  6228. (cond
  6229. ((assoc type org-link-protocols)
  6230. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6231. ((equal type "mailto")
  6232. (let ((cmd (car org-link-mailto-program))
  6233. (args (cdr org-link-mailto-program)) args1
  6234. (address path) (subject "") a)
  6235. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6236. (setq address (match-string 1 path)
  6237. subject (org-link-escape (match-string 2 path))))
  6238. (while args
  6239. (cond
  6240. ((not (stringp (car args))) (push (pop args) args1))
  6241. (t (setq a (pop args))
  6242. (if (string-match "%a" a)
  6243. (setq a (replace-match address t t a)))
  6244. (if (string-match "%s" a)
  6245. (setq a (replace-match subject t t a)))
  6246. (push a args1))))
  6247. (apply cmd (nreverse args1))))
  6248. ((member type '("http" "https" "ftp" "news"))
  6249. (browse-url (concat type ":" (org-link-escape
  6250. path org-link-escape-chars-browser))))
  6251. ((member type '("message"))
  6252. (browse-url (concat type ":" path)))
  6253. ((string= type "tags")
  6254. (org-tags-view in-emacs path))
  6255. ((string= type "thisfile")
  6256. (if in-emacs
  6257. (switch-to-buffer-other-window
  6258. (org-get-buffer-for-internal-link (current-buffer)))
  6259. (org-mark-ring-push))
  6260. (let ((cmd `(org-link-search
  6261. ,path
  6262. ,(cond ((equal in-emacs '(4)) 'occur)
  6263. ((equal in-emacs '(16)) 'org-occur)
  6264. (t nil))
  6265. ,pos)))
  6266. (condition-case nil (eval cmd)
  6267. (error (progn (widen) (eval cmd))))))
  6268. ((string= type "tree-match")
  6269. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6270. ((string= type "file")
  6271. (if (string-match "::\\([0-9]+\\)\\'" path)
  6272. (setq line (string-to-number (match-string 1 path))
  6273. path (substring path 0 (match-beginning 0)))
  6274. (if (string-match "::\\(.+\\)\\'" path)
  6275. (setq search (match-string 1 path)
  6276. path (substring path 0 (match-beginning 0)))))
  6277. (if (string-match "[*?{]" (file-name-nondirectory path))
  6278. (dired path)
  6279. (org-open-file path in-emacs line search)))
  6280. ((string= type "news")
  6281. (require 'org-gnus)
  6282. (org-gnus-follow-link path))
  6283. ((string= type "shell")
  6284. (let ((cmd path))
  6285. (if (or (not org-confirm-shell-link-function)
  6286. (funcall org-confirm-shell-link-function
  6287. (format "Execute \"%s\" in shell? "
  6288. (org-add-props cmd nil
  6289. 'face 'org-warning))))
  6290. (progn
  6291. (message "Executing %s" cmd)
  6292. (shell-command cmd))
  6293. (error "Abort"))))
  6294. ((string= type "elisp")
  6295. (let ((cmd path))
  6296. (if (or (not org-confirm-elisp-link-function)
  6297. (funcall org-confirm-elisp-link-function
  6298. (format "Execute \"%s\" as elisp? "
  6299. (org-add-props cmd nil
  6300. 'face 'org-warning))))
  6301. (message "%s => %s" cmd
  6302. (if (equal (string-to-char cmd) ?\()
  6303. (eval (read cmd))
  6304. (call-interactively (read cmd))))
  6305. (error "Abort"))))
  6306. (t
  6307. (browse-url-at-point)))))
  6308. (move-marker org-open-link-marker nil)
  6309. (run-hook-with-args 'org-follow-link-hook))
  6310. ;;;; Time estimates
  6311. (defun org-get-effort (&optional pom)
  6312. "Get the effort estimate for the current entry."
  6313. (org-entry-get pom org-effort-property))
  6314. ;;; File search
  6315. (defvar org-create-file-search-functions nil
  6316. "List of functions to construct the right search string for a file link.
  6317. These functions are called in turn with point at the location to
  6318. which the link should point.
  6319. A function in the hook should first test if it would like to
  6320. handle this file type, for example by checking the major-mode or
  6321. the file extension. If it decides not to handle this file, it
  6322. should just return nil to give other functions a chance. If it
  6323. does handle the file, it must return the search string to be used
  6324. when following the link. The search string will be part of the
  6325. file link, given after a double colon, and `org-open-at-point'
  6326. will automatically search for it. If special measures must be
  6327. taken to make the search successful, another function should be
  6328. added to the companion hook `org-execute-file-search-functions',
  6329. which see.
  6330. A function in this hook may also use `setq' to set the variable
  6331. `description' to provide a suggestion for the descriptive text to
  6332. be used for this link when it gets inserted into an Org-mode
  6333. buffer with \\[org-insert-link].")
  6334. (defvar org-execute-file-search-functions nil
  6335. "List of functions to execute a file search triggered by a link.
  6336. Functions added to this hook must accept a single argument, the
  6337. search string that was part of the file link, the part after the
  6338. double colon. The function must first check if it would like to
  6339. handle this search, for example by checking the major-mode or the
  6340. file extension. If it decides not to handle this search, it
  6341. should just return nil to give other functions a chance. If it
  6342. does handle the search, it must return a non-nil value to keep
  6343. other functions from trying.
  6344. Each function can access the current prefix argument through the
  6345. variable `current-prefix-argument'. Note that a single prefix is
  6346. used to force opening a link in Emacs, so it may be good to only
  6347. use a numeric or double prefix to guide the search function.
  6348. In case this is needed, a function in this hook can also restore
  6349. the window configuration before `org-open-at-point' was called using:
  6350. (set-window-configuration org-window-config-before-follow-link)")
  6351. (defun org-link-search (s &optional type avoid-pos)
  6352. "Search for a link search option.
  6353. If S is surrounded by forward slashes, it is interpreted as a
  6354. regular expression. In org-mode files, this will create an `org-occur'
  6355. sparse tree. In ordinary files, `occur' will be used to list matches.
  6356. If the current buffer is in `dired-mode', grep will be used to search
  6357. in all files. If AVOID-POS is given, ignore matches near that position."
  6358. (let ((case-fold-search t)
  6359. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6360. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6361. (append '(("") (" ") ("\t") ("\n"))
  6362. org-emphasis-alist)
  6363. "\\|") "\\)"))
  6364. (pos (point))
  6365. (pre nil) (post nil)
  6366. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6367. (cond
  6368. ;; First check if there are any special
  6369. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6370. ;; Now try the builtin stuff
  6371. ((save-excursion
  6372. (goto-char (point-min))
  6373. (and
  6374. (re-search-forward
  6375. (concat "<<" (regexp-quote s0) ">>") nil t)
  6376. (setq type 'dedicated
  6377. pos (match-beginning 0))))
  6378. ;; There is an exact target for this
  6379. (goto-char pos))
  6380. ((string-match "^/\\(.*\\)/$" s)
  6381. ;; A regular expression
  6382. (cond
  6383. ((org-mode-p)
  6384. (org-occur (match-string 1 s)))
  6385. ;;((eq major-mode 'dired-mode)
  6386. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6387. (t (org-do-occur (match-string 1 s)))))
  6388. (t
  6389. ;; A normal search strings
  6390. (when (equal (string-to-char s) ?*)
  6391. ;; Anchor on headlines, post may include tags.
  6392. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6393. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6394. s (substring s 1)))
  6395. (remove-text-properties
  6396. 0 (length s)
  6397. '(face nil mouse-face nil keymap nil fontified nil) s)
  6398. ;; Make a series of regular expressions to find a match
  6399. (setq words (org-split-string s "[ \n\r\t]+")
  6400. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6401. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6402. "\\)" markers)
  6403. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6404. re2a (concat "[ \t\r\n]" re2a_)
  6405. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6406. re4 (concat "[^a-zA-Z_]" re4_)
  6407. re1 (concat pre re2 post)
  6408. re3 (concat pre (if pre re4_ re4) post)
  6409. re5 (concat pre ".*" re4)
  6410. re2 (concat pre re2)
  6411. re2a (concat pre (if pre re2a_ re2a))
  6412. re4 (concat pre (if pre re4_ re4))
  6413. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6414. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6415. re5 "\\)"
  6416. ))
  6417. (cond
  6418. ((eq type 'org-occur) (org-occur reall))
  6419. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6420. (t (goto-char (point-min))
  6421. (setq type 'fuzzy)
  6422. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6423. (org-search-not-self 1 re1 nil t)
  6424. (org-search-not-self 1 re2 nil t)
  6425. (org-search-not-self 1 re2a nil t)
  6426. (org-search-not-self 1 re3 nil t)
  6427. (org-search-not-self 1 re4 nil t)
  6428. (org-search-not-self 1 re5 nil t)
  6429. )
  6430. (goto-char (match-beginning 1))
  6431. (goto-char pos)
  6432. (error "No match")))))
  6433. (t
  6434. ;; Normal string-search
  6435. (goto-char (point-min))
  6436. (if (search-forward s nil t)
  6437. (goto-char (match-beginning 0))
  6438. (error "No match"))))
  6439. (and (org-mode-p) (org-show-context 'link-search))
  6440. type))
  6441. (defun org-search-not-self (group &rest args)
  6442. "Execute `re-search-forward', but only accept matches that do not
  6443. enclose the position of `org-open-link-marker'."
  6444. (let ((m org-open-link-marker))
  6445. (catch 'exit
  6446. (while (apply 're-search-forward args)
  6447. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6448. (goto-char (match-end group))
  6449. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6450. (> (match-beginning 0) (marker-position m))
  6451. (< (match-end 0) (marker-position m)))
  6452. (save-match-data
  6453. (or (not (org-in-regexp
  6454. org-bracket-link-analytic-regexp 1))
  6455. (not (match-end 4)) ; no description
  6456. (and (<= (match-beginning 4) (point))
  6457. (>= (match-end 4) (point))))))
  6458. (throw 'exit (point))))))))
  6459. (defun org-get-buffer-for-internal-link (buffer)
  6460. "Return a buffer to be used for displaying the link target of internal links."
  6461. (cond
  6462. ((not org-display-internal-link-with-indirect-buffer)
  6463. buffer)
  6464. ((string-match "(Clone)$" (buffer-name buffer))
  6465. (message "Buffer is already a clone, not making another one")
  6466. ;; we also do not modify visibility in this case
  6467. buffer)
  6468. (t ; make a new indirect buffer for displaying the link
  6469. (let* ((bn (buffer-name buffer))
  6470. (ibn (concat bn "(Clone)"))
  6471. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6472. (with-current-buffer ib (org-overview))
  6473. ib))))
  6474. (defun org-do-occur (regexp &optional cleanup)
  6475. "Call the Emacs command `occur'.
  6476. If CLEANUP is non-nil, remove the printout of the regular expression
  6477. in the *Occur* buffer. This is useful if the regex is long and not useful
  6478. to read."
  6479. (occur regexp)
  6480. (when cleanup
  6481. (let ((cwin (selected-window)) win beg end)
  6482. (when (setq win (get-buffer-window "*Occur*"))
  6483. (select-window win))
  6484. (goto-char (point-min))
  6485. (when (re-search-forward "match[a-z]+" nil t)
  6486. (setq beg (match-end 0))
  6487. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6488. (setq end (1- (match-beginning 0)))))
  6489. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6490. (goto-char (point-min))
  6491. (select-window cwin))))
  6492. ;;; The mark ring for links jumps
  6493. (defvar org-mark-ring nil
  6494. "Mark ring for positions before jumps in Org-mode.")
  6495. (defvar org-mark-ring-last-goto nil
  6496. "Last position in the mark ring used to go back.")
  6497. ;; Fill and close the ring
  6498. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6499. (loop for i from 1 to org-mark-ring-length do
  6500. (push (make-marker) org-mark-ring))
  6501. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6502. org-mark-ring)
  6503. (defun org-mark-ring-push (&optional pos buffer)
  6504. "Put the current position or POS into the mark ring and rotate it."
  6505. (interactive)
  6506. (setq pos (or pos (point)))
  6507. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6508. (move-marker (car org-mark-ring)
  6509. (or pos (point))
  6510. (or buffer (current-buffer)))
  6511. (message "%s"
  6512. (substitute-command-keys
  6513. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6514. (defun org-mark-ring-goto (&optional n)
  6515. "Jump to the previous position in the mark ring.
  6516. With prefix arg N, jump back that many stored positions. When
  6517. called several times in succession, walk through the entire ring.
  6518. Org-mode commands jumping to a different position in the current file,
  6519. or to another Org-mode file, automatically push the old position
  6520. onto the ring."
  6521. (interactive "p")
  6522. (let (p m)
  6523. (if (eq last-command this-command)
  6524. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6525. (setq p org-mark-ring))
  6526. (setq org-mark-ring-last-goto p)
  6527. (setq m (car p))
  6528. (switch-to-buffer (marker-buffer m))
  6529. (goto-char m)
  6530. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6531. (defun org-remove-angle-brackets (s)
  6532. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6533. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6534. s)
  6535. (defun org-add-angle-brackets (s)
  6536. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6537. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6538. s)
  6539. (defun org-remove-double-quotes (s)
  6540. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6541. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6542. s)
  6543. ;;; Following specific links
  6544. (defun org-follow-timestamp-link ()
  6545. (cond
  6546. ((org-at-date-range-p t)
  6547. (let ((org-agenda-start-on-weekday)
  6548. (t1 (match-string 1))
  6549. (t2 (match-string 2)))
  6550. (setq t1 (time-to-days (org-time-string-to-time t1))
  6551. t2 (time-to-days (org-time-string-to-time t2)))
  6552. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6553. ((org-at-timestamp-p t)
  6554. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6555. (substring (match-string 1) 0 10)))
  6556. 1))
  6557. (t (error "This should not happen"))))
  6558. ;;; Following file links
  6559. (defvar org-wait nil)
  6560. (defun org-open-file (path &optional in-emacs line search)
  6561. "Open the file at PATH.
  6562. First, this expands any special file name abbreviations. Then the
  6563. configuration variable `org-file-apps' is checked if it contains an
  6564. entry for this file type, and if yes, the corresponding command is launched.
  6565. If no application is found, Emacs simply visits the file.
  6566. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6567. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6568. and o use an external application to visit the file.
  6569. Optional LINE specifies a line to go to, optional SEARCH a string to
  6570. search for. If LINE or SEARCH is given, the file will always be
  6571. opened in Emacs.
  6572. If the file does not exist, an error is thrown."
  6573. (setq in-emacs (or in-emacs line search))
  6574. (let* ((file (if (equal path "")
  6575. buffer-file-name
  6576. (substitute-in-file-name (expand-file-name path))))
  6577. (apps (append org-file-apps (org-default-apps)))
  6578. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6579. (dirp (if remp nil (file-directory-p file)))
  6580. (file (if (and dirp org-open-directory-means-index-dot-org)
  6581. (concat (file-name-as-directory file) "index.org")
  6582. file))
  6583. (a-m-a-p (assq 'auto-mode apps))
  6584. (dfile (downcase file))
  6585. (old-buffer (current-buffer))
  6586. (old-pos (point))
  6587. (old-mode major-mode)
  6588. ext cmd)
  6589. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6590. (setq ext (match-string 1 dfile))
  6591. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6592. (setq ext (match-string 1 dfile))))
  6593. (cond
  6594. ((equal in-emacs '(16))
  6595. (setq cmd (cdr (assoc 'system apps))))
  6596. (in-emacs (setq cmd 'emacs))
  6597. (t
  6598. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6599. (and dirp (cdr (assoc 'directory apps)))
  6600. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6601. 'string-match)
  6602. (cdr (assoc ext apps))
  6603. (cdr (assoc t apps))))))
  6604. (when (eq cmd 'system)
  6605. (setq cmd (cdr (assoc 'system apps))))
  6606. (when (eq cmd 'default)
  6607. (setq cmd (cdr (assoc t apps))))
  6608. (when (eq cmd 'mailcap)
  6609. (require 'mailcap)
  6610. (mailcap-parse-mailcaps)
  6611. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6612. (command (mailcap-mime-info mime-type)))
  6613. (if (stringp command)
  6614. (setq cmd command)
  6615. (setq cmd 'emacs))))
  6616. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6617. (not (file-exists-p file))
  6618. (not org-open-non-existing-files))
  6619. (error "No such file: %s" file))
  6620. (cond
  6621. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6622. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6623. (while (string-match "['\"]%s['\"]" cmd)
  6624. (setq cmd (replace-match "%s" t t cmd)))
  6625. (while (string-match "%s" cmd)
  6626. (setq cmd (replace-match
  6627. (save-match-data
  6628. (shell-quote-argument
  6629. (convert-standard-filename file)))
  6630. t t cmd)))
  6631. (save-window-excursion
  6632. (start-process-shell-command cmd nil cmd)
  6633. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6634. ))
  6635. ((or (stringp cmd)
  6636. (eq cmd 'emacs))
  6637. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6638. (widen)
  6639. (if line (goto-line line)
  6640. (if search (org-link-search search))))
  6641. ((consp cmd)
  6642. (let ((file (convert-standard-filename file)))
  6643. (eval cmd)))
  6644. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6645. (and (org-mode-p) (eq old-mode 'org-mode)
  6646. (or (not (equal old-buffer (current-buffer)))
  6647. (not (equal old-pos (point))))
  6648. (org-mark-ring-push old-pos old-buffer))))
  6649. (defun org-default-apps ()
  6650. "Return the default applications for this operating system."
  6651. (cond
  6652. ((eq system-type 'darwin)
  6653. org-file-apps-defaults-macosx)
  6654. ((eq system-type 'windows-nt)
  6655. org-file-apps-defaults-windowsnt)
  6656. (t org-file-apps-defaults-gnu)))
  6657. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6658. "Convert extensions to regular expressions in the cars of LIST.
  6659. Also, weed out any non-string entries, because the return value is used
  6660. only for regexp matching.
  6661. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6662. point to the symbol `emacs', indicating that the file should
  6663. be opened in Emacs."
  6664. (append
  6665. (delq nil
  6666. (mapcar (lambda (x)
  6667. (if (not (stringp (car x)))
  6668. nil
  6669. (if (string-match "\\W" (car x))
  6670. x
  6671. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6672. list))
  6673. (if add-auto-mode
  6674. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6675. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6676. (defun org-file-remote-p (file)
  6677. "Test whether FILE specifies a location on a remote system.
  6678. Return non-nil if the location is indeed remote.
  6679. For example, the filename \"/user@host:/foo\" specifies a location
  6680. on the system \"/user@host:\"."
  6681. (cond ((fboundp 'file-remote-p)
  6682. (file-remote-p file))
  6683. ((fboundp 'tramp-handle-file-remote-p)
  6684. (tramp-handle-file-remote-p file))
  6685. ((and (boundp 'ange-ftp-name-format)
  6686. (string-match (car ange-ftp-name-format) file))
  6687. t)
  6688. (t nil)))
  6689. ;;;; Refiling
  6690. (defun org-get-org-file ()
  6691. "Read a filename, with default directory `org-directory'."
  6692. (let ((default (or org-default-notes-file remember-data-file)))
  6693. (read-file-name (format "File name [%s]: " default)
  6694. (file-name-as-directory org-directory)
  6695. default)))
  6696. (defun org-notes-order-reversed-p ()
  6697. "Check if the current file should receive notes in reversed order."
  6698. (cond
  6699. ((not org-reverse-note-order) nil)
  6700. ((eq t org-reverse-note-order) t)
  6701. ((not (listp org-reverse-note-order)) nil)
  6702. (t (catch 'exit
  6703. (let ((all org-reverse-note-order)
  6704. entry)
  6705. (while (setq entry (pop all))
  6706. (if (string-match (car entry) buffer-file-name)
  6707. (throw 'exit (cdr entry))))
  6708. nil)))))
  6709. (defvar org-refile-target-table nil
  6710. "The list of refile targets, created by `org-refile'.")
  6711. (defvar org-agenda-new-buffers nil
  6712. "Buffers created to visit agenda files.")
  6713. (defun org-get-refile-targets (&optional default-buffer)
  6714. "Produce a table with refile targets."
  6715. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6716. targets txt re files f desc descre)
  6717. (with-current-buffer (or default-buffer (current-buffer))
  6718. (while (setq entry (pop entries))
  6719. (setq files (car entry) desc (cdr entry))
  6720. (cond
  6721. ((null files) (setq files (list (current-buffer))))
  6722. ((eq files 'org-agenda-files)
  6723. (setq files (org-agenda-files 'unrestricted)))
  6724. ((and (symbolp files) (fboundp files))
  6725. (setq files (funcall files)))
  6726. ((and (symbolp files) (boundp files))
  6727. (setq files (symbol-value files))))
  6728. (if (stringp files) (setq files (list files)))
  6729. (cond
  6730. ((eq (car desc) :tag)
  6731. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6732. ((eq (car desc) :todo)
  6733. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6734. ((eq (car desc) :regexp)
  6735. (setq descre (cdr desc)))
  6736. ((eq (car desc) :level)
  6737. (setq descre (concat "^\\*\\{" (number-to-string
  6738. (if org-odd-levels-only
  6739. (1- (* 2 (cdr desc)))
  6740. (cdr desc)))
  6741. "\\}[ \t]")))
  6742. ((eq (car desc) :maxlevel)
  6743. (setq descre (concat "^\\*\\{1," (number-to-string
  6744. (if org-odd-levels-only
  6745. (1- (* 2 (cdr desc)))
  6746. (cdr desc)))
  6747. "\\}[ \t]")))
  6748. (t (error "Bad refiling target description %s" desc)))
  6749. (while (setq f (pop files))
  6750. (save-excursion
  6751. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6752. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6753. (save-excursion
  6754. (save-restriction
  6755. (widen)
  6756. (goto-char (point-min))
  6757. (while (re-search-forward descre nil t)
  6758. (goto-char (point-at-bol))
  6759. (when (looking-at org-complex-heading-regexp)
  6760. (setq txt (org-link-display-format (match-string 4))
  6761. re (concat "^" (regexp-quote
  6762. (buffer-substring (match-beginning 1)
  6763. (match-end 4)))))
  6764. (if (match-end 5) (setq re (concat re "[ \t]+"
  6765. (regexp-quote
  6766. (match-string 5)))))
  6767. (setq re (concat re "[ \t]*$"))
  6768. (when org-refile-use-outline-path
  6769. (setq txt (mapconcat 'org-protect-slash
  6770. (append
  6771. (if (eq org-refile-use-outline-path 'file)
  6772. (list (file-name-nondirectory
  6773. (buffer-file-name (buffer-base-buffer))))
  6774. (if (eq org-refile-use-outline-path 'full-file-path)
  6775. (list (buffer-file-name (buffer-base-buffer)))))
  6776. (org-get-outline-path)
  6777. (list txt))
  6778. "/")))
  6779. (push (list txt f re (point)) targets))
  6780. (goto-char (point-at-eol))))))))
  6781. (nreverse targets))))
  6782. (defun org-protect-slash (s)
  6783. (while (string-match "/" s)
  6784. (setq s (replace-match "\\" t t s)))
  6785. s)
  6786. (defun org-get-outline-path ()
  6787. "Return the outline path to the current entry, as a list."
  6788. (let (rtn)
  6789. (save-excursion
  6790. (while (org-up-heading-safe)
  6791. (when (looking-at org-complex-heading-regexp)
  6792. (push (org-match-string-no-properties 4) rtn)))
  6793. rtn)))
  6794. (defvar org-refile-history nil
  6795. "History for refiling operations.")
  6796. (defun org-refile (&optional goto default-buffer)
  6797. "Move the entry at point to another heading.
  6798. The list of target headings is compiled using the information in
  6799. `org-refile-targets', which see. This list is created before each use
  6800. and will therefore always be up-to-date.
  6801. At the target location, the entry is filed as a subitem of the target heading.
  6802. Depending on `org-reverse-note-order', the new subitem will either be the
  6803. first or the last subitem.
  6804. If there is an active region, all entries in that region will be moved.
  6805. However, the region must fulfil the requirement that the first heading
  6806. is the first one sets the top-level of the moved text - at most siblings
  6807. below it are allowed.
  6808. With prefix arg GOTO, the command will only visit the target location,
  6809. not actually move anything.
  6810. With a double prefix `C-u C-u', go to the location where the last refiling
  6811. operation has put the subtree."
  6812. (interactive "P")
  6813. (let* ((cbuf (current-buffer))
  6814. (regionp (org-region-active-p))
  6815. (region-start (and regionp (region-beginning)))
  6816. (region-end (and regionp (region-end)))
  6817. (region-length (and regionp (- region-end region-start)))
  6818. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6819. pos it nbuf file re level reversed)
  6820. (when regionp (goto-char region-start)
  6821. (unless (org-kill-is-subtree-p
  6822. (buffer-substring region-start region-end))
  6823. (error "The region is not a (sequence of) subtree(s)")))
  6824. (if (equal goto '(16))
  6825. (org-refile-goto-last-stored)
  6826. (when (setq it (org-refile-get-location
  6827. (if goto "Goto: " "Refile to: ") default-buffer))
  6828. (setq file (nth 1 it)
  6829. re (nth 2 it)
  6830. pos (nth 3 it))
  6831. (setq nbuf (or (find-buffer-visiting file)
  6832. (find-file-noselect file)))
  6833. (if goto
  6834. (progn
  6835. (switch-to-buffer nbuf)
  6836. (goto-char pos)
  6837. (org-show-context 'org-goto))
  6838. (if regionp
  6839. (progn
  6840. (kill-new (buffer-substring region-start region-end))
  6841. (org-save-markers-in-region region-start region-end))
  6842. (org-copy-subtree 1 nil t))
  6843. (save-excursion
  6844. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6845. (find-file-noselect file))))
  6846. (setq reversed (org-notes-order-reversed-p))
  6847. (save-excursion
  6848. (save-restriction
  6849. (widen)
  6850. (goto-char pos)
  6851. (looking-at outline-regexp)
  6852. (setq level (org-get-valid-level (funcall outline-level) 1))
  6853. (goto-char
  6854. (if reversed
  6855. (or (outline-next-heading) (point-max))
  6856. (or (save-excursion (outline-get-next-sibling))
  6857. (org-end-of-subtree t t)
  6858. (point-max))))
  6859. (if (not (bolp)) (newline))
  6860. (bookmark-set "org-refile-last-stored")
  6861. (org-paste-subtree level))))
  6862. (if regionp
  6863. (delete-region (point) (+ (point) region-length))
  6864. (org-cut-subtree))
  6865. (setq org-markers-to-move nil)
  6866. (message "Refiled to \"%s\"" (car it)))))))
  6867. (defun org-refile-goto-last-stored ()
  6868. "Go to the location where the last refile was stored."
  6869. (interactive)
  6870. (bookmark-jump "org-refile-last-stored")
  6871. (message "This is the location of the last refile"))
  6872. (defun org-refile-get-location (&optional prompt default-buffer)
  6873. "Prompt the user for a refile location, using PROMPT."
  6874. (let ((org-refile-targets org-refile-targets)
  6875. (org-refile-use-outline-path org-refile-use-outline-path))
  6876. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6877. (unless org-refile-target-table
  6878. (error "No refile targets"))
  6879. (let* ((cbuf (current-buffer))
  6880. (cfunc (if (and org-refile-use-outline-path
  6881. org-outline-path-complete-in-steps)
  6882. 'org-olpath-completing-read
  6883. 'org-ido-completing-read))
  6884. (extra (if org-refile-use-outline-path "/" ""))
  6885. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6886. (fname (and filename (file-truename filename)))
  6887. (tbl (mapcar
  6888. (lambda (x)
  6889. (if (not (equal fname (file-truename (nth 1 x))))
  6890. (cons (concat (car x) extra " ("
  6891. (file-name-nondirectory (nth 1 x)) ")")
  6892. (cdr x))
  6893. (cons (concat (car x) extra) (cdr x))))
  6894. org-refile-target-table))
  6895. (completion-ignore-case t))
  6896. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6897. tbl)))
  6898. (defun org-olpath-completing-read (prompt collection &rest args)
  6899. "Read an outline path like a file name."
  6900. (let ((thetable collection))
  6901. (apply
  6902. 'org-ido-completing-read prompt
  6903. (lambda (string predicate &optional flag)
  6904. (let (rtn r s f (l (length string)))
  6905. (cond
  6906. ((eq flag nil)
  6907. ;; try completion
  6908. (try-completion string thetable))
  6909. ((eq flag t)
  6910. ;; all-completions
  6911. (setq rtn (all-completions string thetable predicate))
  6912. (mapcar
  6913. (lambda (x)
  6914. (setq r (substring x l))
  6915. (if (string-match " ([^)]*)$" x)
  6916. (setq f (match-string 0 x))
  6917. (setq f ""))
  6918. (if (string-match "/" r)
  6919. (concat string (substring r 0 (match-end 0)) f)
  6920. x))
  6921. rtn))
  6922. ((eq flag 'lambda)
  6923. ;; exact match?
  6924. (assoc string thetable)))
  6925. ))
  6926. args)))
  6927. ;;;; Dynamic blocks
  6928. (defun org-find-dblock (name)
  6929. "Find the first dynamic block with name NAME in the buffer.
  6930. If not found, stay at current position and return nil."
  6931. (let (pos)
  6932. (save-excursion
  6933. (goto-char (point-min))
  6934. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6935. nil t)
  6936. (match-beginning 0))))
  6937. (if pos (goto-char pos))
  6938. pos))
  6939. (defconst org-dblock-start-re
  6940. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6941. "Matches the startline of a dynamic block, with parameters.")
  6942. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6943. "Matches the end of a dyhamic block.")
  6944. (defun org-create-dblock (plist)
  6945. "Create a dynamic block section, with parameters taken from PLIST.
  6946. PLIST must containe a :name entry which is used as name of the block."
  6947. (unless (bolp) (newline))
  6948. (let ((name (plist-get plist :name)))
  6949. (insert "#+BEGIN: " name)
  6950. (while plist
  6951. (if (eq (car plist) :name)
  6952. (setq plist (cddr plist))
  6953. (insert " " (prin1-to-string (pop plist)))))
  6954. (insert "\n\n#+END:\n")
  6955. (beginning-of-line -2)))
  6956. (defun org-prepare-dblock ()
  6957. "Prepare dynamic block for refresh.
  6958. This empties the block, puts the cursor at the insert position and returns
  6959. the property list including an extra property :name with the block name."
  6960. (unless (looking-at org-dblock-start-re)
  6961. (error "Not at a dynamic block"))
  6962. (let* ((begdel (1+ (match-end 0)))
  6963. (name (org-no-properties (match-string 1)))
  6964. (params (append (list :name name)
  6965. (read (concat "(" (match-string 3) ")")))))
  6966. (unless (re-search-forward org-dblock-end-re nil t)
  6967. (error "Dynamic block not terminated"))
  6968. (setq params
  6969. (append params
  6970. (list :content (buffer-substring
  6971. begdel (match-beginning 0)))))
  6972. (delete-region begdel (match-beginning 0))
  6973. (goto-char begdel)
  6974. (open-line 1)
  6975. params))
  6976. (defun org-map-dblocks (&optional command)
  6977. "Apply COMMAND to all dynamic blocks in the current buffer.
  6978. If COMMAND is not given, use `org-update-dblock'."
  6979. (let ((cmd (or command 'org-update-dblock))
  6980. pos)
  6981. (save-excursion
  6982. (goto-char (point-min))
  6983. (while (re-search-forward org-dblock-start-re nil t)
  6984. (goto-char (setq pos (match-beginning 0)))
  6985. (condition-case nil
  6986. (funcall cmd)
  6987. (error (message "Error during update of dynamic block")))
  6988. (goto-char pos)
  6989. (unless (re-search-forward org-dblock-end-re nil t)
  6990. (error "Dynamic block not terminated"))))))
  6991. (defun org-dblock-update (&optional arg)
  6992. "User command for updating dynamic blocks.
  6993. Update the dynamic block at point. With prefix ARG, update all dynamic
  6994. blocks in the buffer."
  6995. (interactive "P")
  6996. (if arg
  6997. (org-update-all-dblocks)
  6998. (or (looking-at org-dblock-start-re)
  6999. (org-beginning-of-dblock))
  7000. (org-update-dblock)))
  7001. (defun org-update-dblock ()
  7002. "Update the dynamic block at point
  7003. This means to empty the block, parse for parameters and then call
  7004. the correct writing function."
  7005. (save-window-excursion
  7006. (let* ((pos (point))
  7007. (line (org-current-line))
  7008. (params (org-prepare-dblock))
  7009. (name (plist-get params :name))
  7010. (cmd (intern (concat "org-dblock-write:" name))))
  7011. (message "Updating dynamic block `%s' at line %d..." name line)
  7012. (funcall cmd params)
  7013. (message "Updating dynamic block `%s' at line %d...done" name line)
  7014. (goto-char pos))))
  7015. (defun org-beginning-of-dblock ()
  7016. "Find the beginning of the dynamic block at point.
  7017. Error if there is no scuh block at point."
  7018. (let ((pos (point))
  7019. beg)
  7020. (end-of-line 1)
  7021. (if (and (re-search-backward org-dblock-start-re nil t)
  7022. (setq beg (match-beginning 0))
  7023. (re-search-forward org-dblock-end-re nil t)
  7024. (> (match-end 0) pos))
  7025. (goto-char beg)
  7026. (goto-char pos)
  7027. (error "Not in a dynamic block"))))
  7028. (defun org-update-all-dblocks ()
  7029. "Update all dynamic blocks in the buffer.
  7030. This function can be used in a hook."
  7031. (when (org-mode-p)
  7032. (org-map-dblocks 'org-update-dblock)))
  7033. ;;;; Completion
  7034. (defconst org-additional-option-like-keywords
  7035. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7036. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7037. "BEGIN_EXAMPLE" "END_EXAMPLE"
  7038. "BEGIN_QUOTE" "END_QUOTE"
  7039. "BEGIN_VERSE" "END_VERSE"
  7040. "BEGIN_SRC" "END_SRC"
  7041. "CAPTION" "LABEL" "ATTR_HTML" "ATTR_LaTeX"))
  7042. (defcustom org-structure-template-alist
  7043. '(
  7044. ("s" "#+begin_src ?\n\n#+end_src"
  7045. "<src lang=\"?\">\n\n</src>")
  7046. ("e" "#+begin_example\n?\n#+end_example"
  7047. "<example>\n?\n</example>")
  7048. ("q" "#+begin_quote\n?\n#+end_quote"
  7049. "<quote>\n?\n</quote>")
  7050. ("v" "#+begin_verse\n?\n#+end_verse"
  7051. "<verse>\n?\n/verse>")
  7052. ("l" "#+begin_latex\n?\n#+end_latex"
  7053. "<literal style=\"latex\">\n?\n</literal>")
  7054. ("L" "#+latex: "
  7055. "<literal style=\"latex\">?</literal>")
  7056. ("h" "#+begin_html\n?\n#+end_html"
  7057. "<literal style=\"html\">\n?\n</literal>")
  7058. ("H" "#+html: "
  7059. "<literal style=\"html\">?</literal>")
  7060. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7061. ("A" "#+ascii: ")
  7062. ("i" "#+include %file ?"
  7063. "<include file=%file markup=\"?\">")
  7064. )
  7065. "Structure completion elements.
  7066. This is a list of abbreviation keys and values. The value gets inserted
  7067. it you type @samp{.} followed by the key and then the completion key,
  7068. usually `M-TAB'. %file will be replaced by a file name after prompting
  7069. for the file uning completion.
  7070. There are two templates for each key, the first uses the original Org syntax,
  7071. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7072. the default when the /org-mtags.el/ module has been loaded. See also the
  7073. variable `org-mtags-prefer-muse-templates'.
  7074. This is an experimental feature, it is undecided if it is going to stay in."
  7075. :group 'org-completion
  7076. :type '(repeat
  7077. (string :tag "Key")
  7078. (string :tag "Template")
  7079. (string :tag "Muse Template")))
  7080. (defun org-try-structure-completion ()
  7081. "Try to complete a structure template before point.
  7082. This looks for strings like \"<e\" on an otherwise empty line and
  7083. expands them."
  7084. (let ((l (buffer-substring (point-at-bol) (point)))
  7085. a)
  7086. (when (and (looking-at "[ \t]*$")
  7087. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7088. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7089. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7090. (match-beginning 1)) a)
  7091. t)))
  7092. (defun org-complete-expand-structure-template (start cell)
  7093. "Expand a structure template."
  7094. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7095. (rpl (nth (if musep 2 1) cell)))
  7096. (delete-region start (point))
  7097. (when (string-match "\\`#\\+" rpl)
  7098. (cond
  7099. ((bolp))
  7100. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7101. (delete-region (point-at-bol) (point)))
  7102. (t (newline))))
  7103. (setq start (point))
  7104. (if (string-match "%file" rpl)
  7105. (setq rpl (replace-match
  7106. (concat
  7107. "\""
  7108. (save-match-data
  7109. (abbreviate-file-name (read-file-name "Include file: ")))
  7110. "\"")
  7111. t t rpl)))
  7112. (insert rpl)
  7113. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7114. (defun org-complete (&optional arg)
  7115. "Perform completion on word at point.
  7116. At the beginning of a headline, this completes TODO keywords as given in
  7117. `org-todo-keywords'.
  7118. If the current word is preceded by a backslash, completes the TeX symbols
  7119. that are supported for HTML support.
  7120. If the current word is preceded by \"#+\", completes special words for
  7121. setting file options.
  7122. In the line after \"#+STARTUP:, complete valid keywords.\"
  7123. At all other locations, this simply calls the value of
  7124. `org-completion-fallback-command'."
  7125. (interactive "P")
  7126. (org-without-partial-completion
  7127. (catch 'exit
  7128. (let* ((a nil)
  7129. (end (point))
  7130. (beg1 (save-excursion
  7131. (skip-chars-backward (org-re "[:alnum:]_@"))
  7132. (point)))
  7133. (beg (save-excursion
  7134. (skip-chars-backward "a-zA-Z0-9_:$")
  7135. (point)))
  7136. (confirm (lambda (x) (stringp (car x))))
  7137. (searchhead (equal (char-before beg) ?*))
  7138. (struct
  7139. (when (and (member (char-before beg1) '(?. ?<))
  7140. (setq a (assoc (buffer-substring beg1 (point))
  7141. org-structure-template-alist)))
  7142. (org-complete-expand-structure-template (1- beg1) a)
  7143. (throw 'exit t)))
  7144. (tag (and (equal (char-before beg1) ?:)
  7145. (equal (char-after (point-at-bol)) ?*)))
  7146. (prop (and (equal (char-before beg1) ?:)
  7147. (not (equal (char-after (point-at-bol)) ?*))))
  7148. (texp (equal (char-before beg) ?\\))
  7149. (link (equal (char-before beg) ?\[))
  7150. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7151. beg)
  7152. "#+"))
  7153. (startup (string-match "^#\\+STARTUP:.*"
  7154. (buffer-substring (point-at-bol) (point))))
  7155. (completion-ignore-case opt)
  7156. (type nil)
  7157. (tbl nil)
  7158. (table (cond
  7159. (opt
  7160. (setq type :opt)
  7161. (require 'org-exp)
  7162. (append
  7163. (mapcar
  7164. (lambda (x)
  7165. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7166. (cons (match-string 2 x) (match-string 1 x)))
  7167. (org-split-string (org-get-current-options) "\n"))
  7168. (mapcar 'list org-additional-option-like-keywords)))
  7169. (startup
  7170. (setq type :startup)
  7171. org-startup-options)
  7172. (link (append org-link-abbrev-alist-local
  7173. org-link-abbrev-alist))
  7174. (texp
  7175. (setq type :tex)
  7176. org-html-entities)
  7177. ((string-match "\\`\\*+[ \t]+\\'"
  7178. (buffer-substring (point-at-bol) beg))
  7179. (setq type :todo)
  7180. (mapcar 'list org-todo-keywords-1))
  7181. (searchhead
  7182. (setq type :searchhead)
  7183. (save-excursion
  7184. (goto-char (point-min))
  7185. (while (re-search-forward org-todo-line-regexp nil t)
  7186. (push (list
  7187. (org-make-org-heading-search-string
  7188. (match-string 3) t))
  7189. tbl)))
  7190. tbl)
  7191. (tag (setq type :tag beg beg1)
  7192. (or org-tag-alist (org-get-buffer-tags)))
  7193. (prop (setq type :prop beg beg1)
  7194. (mapcar 'list (org-buffer-property-keys nil t t)))
  7195. (t (progn
  7196. (call-interactively org-completion-fallback-command)
  7197. (throw 'exit nil)))))
  7198. (pattern (buffer-substring-no-properties beg end))
  7199. (completion (try-completion pattern table confirm)))
  7200. (cond ((eq completion t)
  7201. (if (not (assoc (upcase pattern) table))
  7202. (message "Already complete")
  7203. (if (and (equal type :opt)
  7204. (not (member (car (assoc (upcase pattern) table))
  7205. org-additional-option-like-keywords)))
  7206. (insert (substring (cdr (assoc (upcase pattern) table))
  7207. (length pattern)))
  7208. (if (memq type '(:tag :prop)) (insert ":")))))
  7209. ((null completion)
  7210. (message "Can't find completion for \"%s\"" pattern)
  7211. (ding))
  7212. ((not (string= pattern completion))
  7213. (delete-region beg end)
  7214. (if (string-match " +$" completion)
  7215. (setq completion (replace-match "" t t completion)))
  7216. (insert completion)
  7217. (if (get-buffer-window "*Completions*")
  7218. (delete-window (get-buffer-window "*Completions*")))
  7219. (if (assoc completion table)
  7220. (if (eq type :todo) (insert " ")
  7221. (if (memq type '(:tag :prop)) (insert ":"))))
  7222. (if (and (equal type :opt) (assoc completion table))
  7223. (message "%s" (substitute-command-keys
  7224. "Press \\[org-complete] again to insert example settings"))))
  7225. (t
  7226. (message "Making completion list...")
  7227. (let ((list (sort (all-completions pattern table confirm)
  7228. 'string<)))
  7229. (with-output-to-temp-buffer "*Completions*"
  7230. (condition-case nil
  7231. ;; Protection needed for XEmacs and emacs 21
  7232. (display-completion-list list pattern)
  7233. (error (display-completion-list list)))))
  7234. (message "Making completion list...%s" "done")))))))
  7235. ;;;; TODO, DEADLINE, Comments
  7236. (defun org-toggle-comment ()
  7237. "Change the COMMENT state of an entry."
  7238. (interactive)
  7239. (save-excursion
  7240. (org-back-to-heading)
  7241. (let (case-fold-search)
  7242. (if (looking-at (concat outline-regexp
  7243. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7244. (replace-match "" t t nil 1)
  7245. (if (looking-at outline-regexp)
  7246. (progn
  7247. (goto-char (match-end 0))
  7248. (insert org-comment-string " ")))))))
  7249. (defvar org-last-todo-state-is-todo nil
  7250. "This is non-nil when the last TODO state change led to a TODO state.
  7251. If the last change removed the TODO tag or switched to DONE, then
  7252. this is nil.")
  7253. (defvar org-setting-tags nil) ; dynamically skiped
  7254. (defun org-parse-local-options (string var)
  7255. "Parse STRING for startup setting relevant for variable VAR."
  7256. (let ((rtn (symbol-value var))
  7257. e opts)
  7258. (save-match-data
  7259. (if (or (not string) (not (string-match "\\S-" string)))
  7260. rtn
  7261. (setq opts (delq nil (mapcar (lambda (x)
  7262. (setq e (assoc x org-startup-options))
  7263. (if (eq (nth 1 e) var) e nil))
  7264. (org-split-string string "[ \t]+"))))
  7265. (if (not opts)
  7266. rtn
  7267. (setq rtn nil)
  7268. (while (setq e (pop opts))
  7269. (if (not (nth 3 e))
  7270. (setq rtn (nth 2 e))
  7271. (if (not (listp rtn)) (setq rtn nil))
  7272. (push (nth 2 e) rtn)))
  7273. rtn)))))
  7274. (defvar org-blocker-hook nil
  7275. "Hook for functions that are allowed to block a state change.
  7276. Each function gets as its single argument a property list, see
  7277. `org-trigger-hook' for more information about this list.
  7278. If any of the functions in this hook returns nil, the state change
  7279. is blocked.")
  7280. (defvar org-trigger-hook nil
  7281. "Hook for functions that are triggered by a state change.
  7282. Each function gets as its single argument a property list with at least
  7283. the following elements:
  7284. (:type type-of-change :position pos-at-entry-start
  7285. :from old-state :to new-state)
  7286. Depending on the type, more properties may be present.
  7287. This mechanism is currently implemented for:
  7288. TODO state changes
  7289. ------------------
  7290. :type todo-state-change
  7291. :from previous state (keyword as a string), or nil
  7292. :to new state (keyword as a string), or nil")
  7293. (defvar org-agenda-headline-snapshot-before-repeat)
  7294. (defun org-todo (&optional arg)
  7295. "Change the TODO state of an item.
  7296. The state of an item is given by a keyword at the start of the heading,
  7297. like
  7298. *** TODO Write paper
  7299. *** DONE Call mom
  7300. The different keywords are specified in the variable `org-todo-keywords'.
  7301. By default the available states are \"TODO\" and \"DONE\".
  7302. So for this example: when the item starts with TODO, it is changed to DONE.
  7303. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7304. DONE are present, add TODO at the beginning of the heading.
  7305. With C-u prefix arg, use completion to determine the new state.
  7306. With numeric prefix arg, switch to that state.
  7307. For calling through lisp, arg is also interpreted in the following way:
  7308. 'none -> empty state
  7309. \"\"(empty string) -> switch to empty state
  7310. 'done -> switch to DONE
  7311. 'nextset -> switch to the next set of keywords
  7312. 'previousset -> switch to the previous set of keywords
  7313. \"WAITING\" -> switch to the specified keyword, but only if it
  7314. really is a member of `org-todo-keywords'."
  7315. (interactive "P")
  7316. (save-excursion
  7317. (catch 'exit
  7318. (org-back-to-heading)
  7319. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7320. (or (looking-at (concat " +" org-todo-regexp " *"))
  7321. (looking-at " *"))
  7322. (let* ((match-data (match-data))
  7323. (startpos (point-at-bol))
  7324. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7325. (org-log-done org-log-done)
  7326. (org-log-repeat org-log-repeat)
  7327. (org-todo-log-states org-todo-log-states)
  7328. (this (match-string 1))
  7329. (hl-pos (match-beginning 0))
  7330. (head (org-get-todo-sequence-head this))
  7331. (ass (assoc head org-todo-kwd-alist))
  7332. (interpret (nth 1 ass))
  7333. (done-word (nth 3 ass))
  7334. (final-done-word (nth 4 ass))
  7335. (last-state (or this ""))
  7336. (completion-ignore-case t)
  7337. (member (member this org-todo-keywords-1))
  7338. (tail (cdr member))
  7339. (state (cond
  7340. ((and org-todo-key-trigger
  7341. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7342. (and (not arg) org-use-fast-todo-selection
  7343. (not (eq org-use-fast-todo-selection 'prefix)))))
  7344. ;; Use fast selection
  7345. (org-fast-todo-selection))
  7346. ((and (equal arg '(4))
  7347. (or (not org-use-fast-todo-selection)
  7348. (not org-todo-key-trigger)))
  7349. ;; Read a state with completion
  7350. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7351. org-todo-keywords-1)
  7352. nil t))
  7353. ((eq arg 'right)
  7354. (if this
  7355. (if tail (car tail) nil)
  7356. (car org-todo-keywords-1)))
  7357. ((eq arg 'left)
  7358. (if (equal member org-todo-keywords-1)
  7359. nil
  7360. (if this
  7361. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7362. org-todo-keywords-1)
  7363. (org-last org-todo-keywords-1))))
  7364. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7365. (setq arg nil))) ; hack to fall back to cycling
  7366. (arg
  7367. ;; user or caller requests a specific state
  7368. (cond
  7369. ((equal arg "") nil)
  7370. ((eq arg 'none) nil)
  7371. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7372. ((eq arg 'nextset)
  7373. (or (car (cdr (member head org-todo-heads)))
  7374. (car org-todo-heads)))
  7375. ((eq arg 'previousset)
  7376. (let ((org-todo-heads (reverse org-todo-heads)))
  7377. (or (car (cdr (member head org-todo-heads)))
  7378. (car org-todo-heads))))
  7379. ((car (member arg org-todo-keywords-1)))
  7380. ((nth (1- (prefix-numeric-value arg))
  7381. org-todo-keywords-1))))
  7382. ((null member) (or head (car org-todo-keywords-1)))
  7383. ((equal this final-done-word) nil) ;; -> make empty
  7384. ((null tail) nil) ;; -> first entry
  7385. ((eq interpret 'sequence)
  7386. (car tail))
  7387. ((memq interpret '(type priority))
  7388. (if (eq this-command last-command)
  7389. (car tail)
  7390. (if (> (length tail) 0)
  7391. (or done-word (car org-done-keywords))
  7392. nil)))
  7393. (t nil)))
  7394. (next (if state (concat " " state " ") " "))
  7395. (change-plist (list :type 'todo-state-change :from this :to state
  7396. :position startpos))
  7397. dolog now-done-p)
  7398. (when org-blocker-hook
  7399. (unless (save-excursion
  7400. (save-match-data
  7401. (run-hook-with-args-until-failure
  7402. 'org-blocker-hook change-plist)))
  7403. (if (interactive-p)
  7404. (error "TODO state change from %s to %s blocked" this state)
  7405. ;; fail silently
  7406. (message "TODO state change from %s to %s blocked" this state)
  7407. (throw 'exit nil))))
  7408. (store-match-data match-data)
  7409. (replace-match next t t)
  7410. (unless (pos-visible-in-window-p hl-pos)
  7411. (message "TODO state changed to %s" (org-trim next)))
  7412. (unless head
  7413. (setq head (org-get-todo-sequence-head state)
  7414. ass (assoc head org-todo-kwd-alist)
  7415. interpret (nth 1 ass)
  7416. done-word (nth 3 ass)
  7417. final-done-word (nth 4 ass)))
  7418. (when (memq arg '(nextset previousset))
  7419. (message "Keyword-Set %d/%d: %s"
  7420. (- (length org-todo-sets) -1
  7421. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7422. (length org-todo-sets)
  7423. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7424. (setq org-last-todo-state-is-todo
  7425. (not (member state org-done-keywords)))
  7426. (setq now-done-p (and (member state org-done-keywords)
  7427. (not (member this org-done-keywords))))
  7428. (and logging (org-local-logging logging))
  7429. (when (and (or org-todo-log-states org-log-done)
  7430. (not (memq arg '(nextset previousset))))
  7431. ;; we need to look at recording a time and note
  7432. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7433. (nth 2 (assoc this org-todo-log-states))))
  7434. (when (and state
  7435. (member state org-not-done-keywords)
  7436. (not (member this org-not-done-keywords)))
  7437. ;; This is now a todo state and was not one before
  7438. ;; If there was a CLOSED time stamp, get rid of it.
  7439. (org-add-planning-info nil nil 'closed))
  7440. (when (and now-done-p org-log-done)
  7441. ;; It is now done, and it was not done before
  7442. (org-add-planning-info 'closed (org-current-time))
  7443. (if (and (not dolog) (eq 'note org-log-done))
  7444. (org-add-log-setup 'done state 'findpos 'note)))
  7445. (when (and state dolog)
  7446. ;; This is a non-nil state, and we need to log it
  7447. (org-add-log-setup 'state state 'findpos dolog)))
  7448. ;; Fixup tag positioning
  7449. (org-todo-trigger-tag-changes state)
  7450. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7451. (when org-provide-todo-statistics
  7452. (org-update-parent-todo-statistics))
  7453. (run-hooks 'org-after-todo-state-change-hook)
  7454. (if (and arg (not (member state org-done-keywords)))
  7455. (setq head (org-get-todo-sequence-head state)))
  7456. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7457. ;; Do we need to trigger a repeat?
  7458. (when now-done-p
  7459. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7460. ;; This is for the agenda, take a snapshot of the headline.
  7461. (save-match-data
  7462. (setq org-agenda-headline-snapshot-before-repeat
  7463. (org-get-heading))))
  7464. (org-auto-repeat-maybe state))
  7465. ;; Fixup cursor location if close to the keyword
  7466. (if (and (outline-on-heading-p)
  7467. (not (bolp))
  7468. (save-excursion (beginning-of-line 1)
  7469. (looking-at org-todo-line-regexp))
  7470. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7471. (progn
  7472. (goto-char (or (match-end 2) (match-end 1)))
  7473. (just-one-space)))
  7474. (when org-trigger-hook
  7475. (save-excursion
  7476. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7477. (defun org-update-parent-todo-statistics ()
  7478. "Update any statistics cookie in the parent of the current headline."
  7479. (interactive)
  7480. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7481. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7482. (catch 'exit
  7483. (save-excursion
  7484. (setq level (org-up-heading-safe))
  7485. (unless (and level
  7486. (re-search-forward box-re (point-at-eol) t))
  7487. (throw 'exit nil))
  7488. (setq is-percent (match-end 2))
  7489. (save-match-data
  7490. (unless (outline-next-heading) (throw 'exit nil))
  7491. (while (looking-at org-todo-line-regexp)
  7492. (setq kwd (match-string 2))
  7493. (and kwd (setq cnt-all (1+ cnt-all)))
  7494. (and (member kwd org-done-keywords)
  7495. (setq cnt-done (1+ cnt-done)))
  7496. (condition-case nil
  7497. (org-forward-same-level 1)
  7498. (error (end-of-line 1)))))
  7499. (replace-match
  7500. (if is-percent
  7501. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7502. (format "[%d/%d]" cnt-done cnt-all)))
  7503. (run-hook-with-args 'org-after-todo-statistics-hook
  7504. cnt-done (- cnt-all cnt-done))))))
  7505. (defvar org-after-todo-statistics-hook nil
  7506. "Hook that is called after a TODO statistics cookie has been updated.
  7507. Each function is called with two arguments: the number of not-done entries
  7508. and the number of done entries.
  7509. For example, the following function, when added to this hook, will switch
  7510. an entry to DONE when all children are done, and back to TODO when new
  7511. entries are set to a TODO status. Note that this hook is only called
  7512. when there is a statistics cookie in the headline!
  7513. (defun org-summary-todo (n-done n-not-done)
  7514. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7515. (let (org-log-done org-log-states) ; turn off logging
  7516. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7517. ")
  7518. (defun org-todo-trigger-tag-changes (state)
  7519. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7520. (let ((l org-todo-state-tags-triggers)
  7521. changes)
  7522. (when (or (not state) (equal state ""))
  7523. (setq changes (append changes (cdr (assoc "" l)))))
  7524. (when (and (stringp state) (> (length state) 0))
  7525. (setq changes (append changes (cdr (assoc state l)))))
  7526. (when (member state org-not-done-keywords)
  7527. (setq changes (append changes (cdr (assoc 'todo l)))))
  7528. (when (member state org-done-keywords)
  7529. (setq changes (append changes (cdr (assoc 'done l)))))
  7530. (dolist (c changes)
  7531. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7532. (defun org-local-logging (value)
  7533. "Get logging settings from a property VALUE."
  7534. (let* (words w a)
  7535. ;; directly set the variables, they are already local.
  7536. (setq org-log-done nil
  7537. org-log-repeat nil
  7538. org-todo-log-states nil)
  7539. (setq words (org-split-string value))
  7540. (while (setq w (pop words))
  7541. (cond
  7542. ((setq a (assoc w org-startup-options))
  7543. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7544. (set (nth 1 a) (nth 2 a))))
  7545. ((setq a (org-extract-log-state-settings w))
  7546. (and (member (car a) org-todo-keywords-1)
  7547. (push a org-todo-log-states)))))))
  7548. (defun org-get-todo-sequence-head (kwd)
  7549. "Return the head of the TODO sequence to which KWD belongs.
  7550. If KWD is not set, check if there is a text property remembering the
  7551. right sequence."
  7552. (let (p)
  7553. (cond
  7554. ((not kwd)
  7555. (or (get-text-property (point-at-bol) 'org-todo-head)
  7556. (progn
  7557. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7558. nil (point-at-eol)))
  7559. (get-text-property p 'org-todo-head))))
  7560. ((not (member kwd org-todo-keywords-1))
  7561. (car org-todo-keywords-1))
  7562. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7563. (defun org-fast-todo-selection ()
  7564. "Fast TODO keyword selection with single keys.
  7565. Returns the new TODO keyword, or nil if no state change should occur."
  7566. (let* ((fulltable org-todo-key-alist)
  7567. (done-keywords org-done-keywords) ;; needed for the faces.
  7568. (maxlen (apply 'max (mapcar
  7569. (lambda (x)
  7570. (if (stringp (car x)) (string-width (car x)) 0))
  7571. fulltable)))
  7572. (expert nil)
  7573. (fwidth (+ maxlen 3 1 3))
  7574. (ncol (/ (- (window-width) 4) fwidth))
  7575. tg cnt e c tbl
  7576. groups ingroup)
  7577. (save-window-excursion
  7578. (if expert
  7579. (set-buffer (get-buffer-create " *Org todo*"))
  7580. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7581. (erase-buffer)
  7582. (org-set-local 'org-done-keywords done-keywords)
  7583. (setq tbl fulltable cnt 0)
  7584. (while (setq e (pop tbl))
  7585. (cond
  7586. ((equal e '(:startgroup))
  7587. (push '() groups) (setq ingroup t)
  7588. (when (not (= cnt 0))
  7589. (setq cnt 0)
  7590. (insert "\n"))
  7591. (insert "{ "))
  7592. ((equal e '(:endgroup))
  7593. (setq ingroup nil cnt 0)
  7594. (insert "}\n"))
  7595. (t
  7596. (setq tg (car e) c (cdr e))
  7597. (if ingroup (push tg (car groups)))
  7598. (setq tg (org-add-props tg nil 'face
  7599. (org-get-todo-face tg)))
  7600. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7601. (insert "[" c "] " tg (make-string
  7602. (- fwidth 4 (length tg)) ?\ ))
  7603. (when (= (setq cnt (1+ cnt)) ncol)
  7604. (insert "\n")
  7605. (if ingroup (insert " "))
  7606. (setq cnt 0)))))
  7607. (insert "\n")
  7608. (goto-char (point-min))
  7609. (if (not expert) (org-fit-window-to-buffer))
  7610. (message "[a-z..]:Set [SPC]:clear")
  7611. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7612. (cond
  7613. ((or (= c ?\C-g)
  7614. (and (= c ?q) (not (rassoc c fulltable))))
  7615. (setq quit-flag t))
  7616. ((= c ?\ ) nil)
  7617. ((setq e (rassoc c fulltable) tg (car e))
  7618. tg)
  7619. (t (setq quit-flag t))))))
  7620. (defun org-entry-is-todo-p ()
  7621. (member (org-get-todo-state) org-not-done-keywords))
  7622. (defun org-entry-is-done-p ()
  7623. (member (org-get-todo-state) org-done-keywords))
  7624. (defun org-get-todo-state ()
  7625. (save-excursion
  7626. (org-back-to-heading t)
  7627. (and (looking-at org-todo-line-regexp)
  7628. (match-end 2)
  7629. (match-string 2))))
  7630. (defun org-at-date-range-p (&optional inactive-ok)
  7631. "Is the cursor inside a date range?"
  7632. (interactive)
  7633. (save-excursion
  7634. (catch 'exit
  7635. (let ((pos (point)))
  7636. (skip-chars-backward "^[<\r\n")
  7637. (skip-chars-backward "<[")
  7638. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7639. (>= (match-end 0) pos)
  7640. (throw 'exit t))
  7641. (skip-chars-backward "^<[\r\n")
  7642. (skip-chars-backward "<[")
  7643. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7644. (>= (match-end 0) pos)
  7645. (throw 'exit t)))
  7646. nil)))
  7647. (defun org-get-repeat ()
  7648. "Check if there is a deadline/schedule with repeater in this entry."
  7649. (save-match-data
  7650. (save-excursion
  7651. (org-back-to-heading t)
  7652. (if (re-search-forward
  7653. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7654. (match-string 1)))))
  7655. (defvar org-last-changed-timestamp)
  7656. (defvar org-last-inserted-timestamp)
  7657. (defvar org-log-post-message)
  7658. (defvar org-log-note-purpose)
  7659. (defvar org-log-note-how)
  7660. (defvar org-log-note-extra)
  7661. (defun org-auto-repeat-maybe (done-word)
  7662. "Check if the current headline contains a repeated deadline/schedule.
  7663. If yes, set TODO state back to what it was and change the base date
  7664. of repeating deadline/scheduled time stamps to new date.
  7665. This function is run automatically after each state change to a DONE state."
  7666. ;; last-state is dynamically scoped into this function
  7667. (let* ((repeat (org-get-repeat))
  7668. (aa (assoc last-state org-todo-kwd-alist))
  7669. (interpret (nth 1 aa))
  7670. (head (nth 2 aa))
  7671. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7672. (msg "Entry repeats: ")
  7673. (org-log-done nil)
  7674. (org-todo-log-states nil)
  7675. (nshiftmax 10) (nshift 0)
  7676. re type n what ts mb0 time)
  7677. (when repeat
  7678. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7679. (org-todo (if (eq interpret 'type) last-state head))
  7680. (when org-log-repeat
  7681. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7682. (memq 'org-add-log-note post-command-hook))
  7683. ;; OK, we are already setup for some record
  7684. (if (eq org-log-repeat 'note)
  7685. ;; make sure we take a note, not only a time stamp
  7686. (setq org-log-note-how 'note))
  7687. ;; Set up for taking a record
  7688. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7689. 'findpos org-log-repeat)))
  7690. (org-back-to-heading t)
  7691. (org-add-planning-info nil nil 'closed)
  7692. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7693. org-deadline-time-regexp "\\)\\|\\("
  7694. org-ts-regexp "\\)"))
  7695. (while (re-search-forward
  7696. re (save-excursion (outline-next-heading) (point)) t)
  7697. (setq type (if (match-end 1) org-scheduled-string
  7698. (if (match-end 3) org-deadline-string "Plain:"))
  7699. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7700. mb0 (match-beginning 0))
  7701. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7702. (setq n (string-to-number (match-string 2 ts))
  7703. what (match-string 3 ts))
  7704. (if (equal what "w") (setq n (* n 7) what "d"))
  7705. ;; Preparation, see if we need to modify the start date for the change
  7706. (when (match-end 1)
  7707. (setq time (save-match-data (org-time-string-to-time ts)))
  7708. (cond
  7709. ((equal (match-string 1 ts) ".")
  7710. ;; Shift starting date to today
  7711. (org-timestamp-change
  7712. (- (time-to-days (current-time)) (time-to-days time))
  7713. 'day))
  7714. ((equal (match-string 1 ts) "+")
  7715. (while (or (= nshift 0)
  7716. (<= (time-to-days time) (time-to-days (current-time))))
  7717. (when (= (incf nshift) nshiftmax)
  7718. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7719. (error "Abort")))
  7720. (org-timestamp-change n (cdr (assoc what whata)))
  7721. (org-at-timestamp-p t)
  7722. (setq ts (match-string 1))
  7723. (setq time (save-match-data (org-time-string-to-time ts))))
  7724. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7725. ;; rematch, so that we have everything in place for the real shift
  7726. (org-at-timestamp-p t)
  7727. (setq ts (match-string 1))
  7728. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7729. (org-timestamp-change n (cdr (assoc what whata)))
  7730. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7731. (setq org-log-post-message msg)
  7732. (message "%s" msg))))
  7733. (defun org-show-todo-tree (arg)
  7734. "Make a compact tree which shows all headlines marked with TODO.
  7735. The tree will show the lines where the regexp matches, and all higher
  7736. headlines above the match.
  7737. With a \\[universal-argument] prefix, also show the DONE entries.
  7738. With a numeric prefix N, construct a sparse tree for the Nth element
  7739. of `org-todo-keywords-1'."
  7740. (interactive "P")
  7741. (let ((case-fold-search nil)
  7742. (kwd-re
  7743. (cond ((null arg) org-not-done-regexp)
  7744. ((equal arg '(4))
  7745. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7746. (mapcar 'list org-todo-keywords-1))))
  7747. (concat "\\("
  7748. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7749. "\\)\\>")))
  7750. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7751. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7752. org-todo-keywords-1)))
  7753. (t (error "Invalid prefix argument: %s" arg)))))
  7754. (message "%d TODO entries found"
  7755. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7756. (defun org-deadline (&optional remove time)
  7757. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7758. With argument REMOVE, remove any deadline from the item.
  7759. When TIME is set, it should be an internal time specification, and the
  7760. scheduling will use the corresponding date."
  7761. (interactive "P")
  7762. (if remove
  7763. (progn
  7764. (org-remove-timestamp-with-keyword org-deadline-string)
  7765. (message "Item no longer has a deadline."))
  7766. (if (org-get-repeat)
  7767. (error "Cannot change deadline on task with repeater, please do that by hand")
  7768. (org-add-planning-info 'deadline time 'closed)
  7769. (message "Deadline on %s" org-last-inserted-timestamp))))
  7770. (defun org-schedule (&optional remove time)
  7771. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7772. With argument REMOVE, remove any scheduling date from the item.
  7773. When TIME is set, it should be an internal time specification, and the
  7774. scheduling will use the corresponding date."
  7775. (interactive "P")
  7776. (if remove
  7777. (progn
  7778. (org-remove-timestamp-with-keyword org-scheduled-string)
  7779. (message "Item is no longer scheduled."))
  7780. (if (org-get-repeat)
  7781. (error "Cannot reschedule task with repeater, please do that by hand")
  7782. (org-add-planning-info 'scheduled time 'closed)
  7783. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7784. (defun org-remove-timestamp-with-keyword (keyword)
  7785. "Remove all time stamps with KEYWORD in the current entry."
  7786. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7787. beg)
  7788. (save-excursion
  7789. (org-back-to-heading t)
  7790. (setq beg (point))
  7791. (org-end-of-subtree t t)
  7792. (while (re-search-backward re beg t)
  7793. (replace-match "")
  7794. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7795. (equal (char-before) ?\ ))
  7796. (backward-delete-char 1)
  7797. (if (string-match "^[ \t]*$" (buffer-substring
  7798. (point-at-bol) (point-at-eol)))
  7799. (delete-region (point-at-bol)
  7800. (min (point-max) (1+ (point-at-eol))))))))))
  7801. (defun org-add-planning-info (what &optional time &rest remove)
  7802. "Insert new timestamp with keyword in the line directly after the headline.
  7803. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7804. If non is given, the user is prompted for a date.
  7805. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7806. be removed."
  7807. (interactive)
  7808. (let (org-time-was-given org-end-time-was-given ts
  7809. end default-time default-input)
  7810. (when (and (not time) (memq what '(scheduled deadline)))
  7811. ;; Try to get a default date/time from existing timestamp
  7812. (save-excursion
  7813. (org-back-to-heading t)
  7814. (setq end (save-excursion (outline-next-heading) (point)))
  7815. (when (re-search-forward (if (eq what 'scheduled)
  7816. org-scheduled-time-regexp
  7817. org-deadline-time-regexp)
  7818. end t)
  7819. (setq ts (match-string 1)
  7820. default-time
  7821. (apply 'encode-time (org-parse-time-string ts))
  7822. default-input (and ts (org-get-compact-tod ts))))))
  7823. (when what
  7824. ;; If necessary, get the time from the user
  7825. (setq time (or time (org-read-date nil 'to-time nil nil
  7826. default-time default-input))))
  7827. (when (and org-insert-labeled-timestamps-at-point
  7828. (member what '(scheduled deadline)))
  7829. (insert
  7830. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7831. (org-insert-time-stamp time org-time-was-given
  7832. nil nil nil (list org-end-time-was-given))
  7833. (setq what nil))
  7834. (save-excursion
  7835. (save-restriction
  7836. (let (col list elt ts buffer-invisibility-spec)
  7837. (org-back-to-heading t)
  7838. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7839. (goto-char (match-end 1))
  7840. (setq col (current-column))
  7841. (goto-char (match-end 0))
  7842. (if (eobp) (insert "\n") (forward-char 1))
  7843. (if (and (not (looking-at outline-regexp))
  7844. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7845. "[^\r\n]*"))
  7846. (not (equal (match-string 1) org-clock-string)))
  7847. (narrow-to-region (match-beginning 0) (match-end 0))
  7848. (insert-before-markers "\n")
  7849. (backward-char 1)
  7850. (narrow-to-region (point) (point))
  7851. (and org-adapt-indentation (org-indent-to-column col)))
  7852. ;; Check if we have to remove something.
  7853. (setq list (cons what remove))
  7854. (while list
  7855. (setq elt (pop list))
  7856. (goto-char (point-min))
  7857. (when (or (and (eq elt 'scheduled)
  7858. (re-search-forward org-scheduled-time-regexp nil t))
  7859. (and (eq elt 'deadline)
  7860. (re-search-forward org-deadline-time-regexp nil t))
  7861. (and (eq elt 'closed)
  7862. (re-search-forward org-closed-time-regexp nil t)))
  7863. (replace-match "")
  7864. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7865. (if (looking-at " +") (replace-match ""))))
  7866. (goto-char (point-max))
  7867. (when what
  7868. (insert
  7869. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7870. (cond ((eq what 'scheduled) org-scheduled-string)
  7871. ((eq what 'deadline) org-deadline-string)
  7872. ((eq what 'closed) org-closed-string))
  7873. " ")
  7874. (setq ts (org-insert-time-stamp
  7875. time
  7876. (or org-time-was-given
  7877. (and (eq what 'closed) org-log-done-with-time))
  7878. (eq what 'closed)
  7879. nil nil (list org-end-time-was-given)))
  7880. (end-of-line 1))
  7881. (goto-char (point-min))
  7882. (widen)
  7883. (if (and (looking-at "[ \t]+\n")
  7884. (equal (char-before) ?\n))
  7885. (delete-region (1- (point)) (point-at-eol)))
  7886. ts)))))
  7887. (defvar org-log-note-marker (make-marker))
  7888. (defvar org-log-note-purpose nil)
  7889. (defvar org-log-note-state nil)
  7890. (defvar org-log-note-how nil)
  7891. (defvar org-log-note-extra nil)
  7892. (defvar org-log-note-window-configuration nil)
  7893. (defvar org-log-note-return-to (make-marker))
  7894. (defvar org-log-post-message nil
  7895. "Message to be displayed after a log note has been stored.
  7896. The auto-repeater uses this.")
  7897. (defun org-add-note ()
  7898. "Add a note to the current entry.
  7899. This is done in the same way as adding a state change note."
  7900. (interactive)
  7901. (org-add-log-setup 'note nil 'findpos nil))
  7902. (defvar org-property-end-re)
  7903. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7904. "Set up the post command hook to take a note.
  7905. If this is about to TODO state change, the new state is expected in STATE.
  7906. When FINDPOS is non-nil, find the correct position for the note in
  7907. the current entry. If not, assume that it can be inserted at point.
  7908. HOW is an indicator what kind of note should be created.
  7909. EXTRA is additional text that will be inserted into the notes buffer."
  7910. (save-restriction
  7911. (save-excursion
  7912. (when findpos
  7913. (org-back-to-heading t)
  7914. (narrow-to-region (point) (save-excursion
  7915. (outline-next-heading) (point)))
  7916. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7917. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7918. "[^\r\n]*\\)?"))
  7919. (goto-char (match-end 0))
  7920. (when (and org-log-state-notes-insert-after-drawers
  7921. (save-excursion
  7922. (forward-line) (looking-at org-drawer-regexp)))
  7923. (progn (forward-line)
  7924. (while (looking-at org-drawer-regexp)
  7925. (goto-char (match-end 0))
  7926. (re-search-forward org-property-end-re (point-max) t)
  7927. (forward-line))
  7928. (forward-line -1)))
  7929. (unless org-log-states-order-reversed
  7930. (and (= (char-after) ?\n) (forward-char 1))
  7931. (org-skip-over-state-notes)
  7932. (skip-chars-backward " \t\n\r")))
  7933. (move-marker org-log-note-marker (point))
  7934. (setq org-log-note-purpose purpose
  7935. org-log-note-state state
  7936. org-log-note-how how
  7937. org-log-note-extra extra)
  7938. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7939. (defun org-skip-over-state-notes ()
  7940. "Skip past the list of State notes in an entry."
  7941. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7942. (while (looking-at "[ \t]*- State")
  7943. (condition-case nil
  7944. (org-next-item)
  7945. (error (org-end-of-item)))))
  7946. (defun org-add-log-note (&optional purpose)
  7947. "Pop up a window for taking a note, and add this note later at point."
  7948. (remove-hook 'post-command-hook 'org-add-log-note)
  7949. (setq org-log-note-window-configuration (current-window-configuration))
  7950. (delete-other-windows)
  7951. (move-marker org-log-note-return-to (point))
  7952. (switch-to-buffer (marker-buffer org-log-note-marker))
  7953. (goto-char org-log-note-marker)
  7954. (org-switch-to-buffer-other-window "*Org Note*")
  7955. (erase-buffer)
  7956. (if (memq org-log-note-how '(time state))
  7957. (let (current-prefix-arg) (org-store-log-note))
  7958. (let ((org-inhibit-startup t)) (org-mode))
  7959. (insert (format "# Insert note for %s.
  7960. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7961. (cond
  7962. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7963. ((eq org-log-note-purpose 'done) "closed todo item")
  7964. ((eq org-log-note-purpose 'state)
  7965. (format "state change to \"%s\"" org-log-note-state))
  7966. ((eq org-log-note-purpose 'note)
  7967. "this entry")
  7968. (t (error "This should not happen")))))
  7969. (if org-log-note-extra (insert org-log-note-extra))
  7970. (org-set-local 'org-finish-function 'org-store-log-note)))
  7971. (defvar org-note-abort nil) ; dynamically scoped
  7972. (defun org-store-log-note ()
  7973. "Finish taking a log note, and insert it to where it belongs."
  7974. (let ((txt (buffer-string))
  7975. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7976. lines ind)
  7977. (kill-buffer (current-buffer))
  7978. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7979. (setq txt (replace-match "" t t txt)))
  7980. (if (string-match "\\s-+\\'" txt)
  7981. (setq txt (replace-match "" t t txt)))
  7982. (setq lines (org-split-string txt "\n"))
  7983. (when (and note (string-match "\\S-" note))
  7984. (setq note
  7985. (org-replace-escapes
  7986. note
  7987. (list (cons "%u" (user-login-name))
  7988. (cons "%U" user-full-name)
  7989. (cons "%t" (format-time-string
  7990. (org-time-stamp-format 'long 'inactive)
  7991. (current-time)))
  7992. (cons "%s" (if org-log-note-state
  7993. (concat "\"" org-log-note-state "\"")
  7994. "")))))
  7995. (if lines (setq note (concat note " \\\\")))
  7996. (push note lines))
  7997. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7998. (when lines
  7999. (save-excursion
  8000. (set-buffer (marker-buffer org-log-note-marker))
  8001. (save-excursion
  8002. (goto-char org-log-note-marker)
  8003. (move-marker org-log-note-marker nil)
  8004. (end-of-line 1)
  8005. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8006. (indent-relative nil)
  8007. (insert "- " (pop lines))
  8008. (org-indent-line-function)
  8009. (beginning-of-line 1)
  8010. (looking-at "[ \t]*")
  8011. (setq ind (concat (match-string 0) " "))
  8012. (end-of-line 1)
  8013. (while lines (insert "\n" ind (pop lines)))))))
  8014. (set-window-configuration org-log-note-window-configuration)
  8015. (with-current-buffer (marker-buffer org-log-note-return-to)
  8016. (goto-char org-log-note-return-to))
  8017. (move-marker org-log-note-return-to nil)
  8018. (and org-log-post-message (message "%s" org-log-post-message)))
  8019. (defun org-sparse-tree (&optional arg)
  8020. "Create a sparse tree, prompt for the details.
  8021. This command can create sparse trees. You first need to select the type
  8022. of match used to create the tree:
  8023. t Show entries with a specific TODO keyword.
  8024. T Show entries selected by a tags match.
  8025. p Enter a property name and its value (both with completion on existing
  8026. names/values) and show entries with that property.
  8027. r Show entries matching a regular expression
  8028. d Show deadlines due within `org-deadline-warning-days'."
  8029. (interactive "P")
  8030. (let (ans kwd value)
  8031. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8032. (setq ans (read-char-exclusive))
  8033. (cond
  8034. ((equal ans ?d)
  8035. (call-interactively 'org-check-deadlines))
  8036. ((equal ans ?b)
  8037. (call-interactively 'org-check-before-date))
  8038. ((equal ans ?t)
  8039. (org-show-todo-tree '(4)))
  8040. ((equal ans ?T)
  8041. (call-interactively 'org-tags-sparse-tree))
  8042. ((member ans '(?p ?P))
  8043. (setq kwd (org-ido-completing-read "Property: "
  8044. (mapcar 'list (org-buffer-property-keys))))
  8045. (setq value (org-ido-completing-read "Value: "
  8046. (mapcar 'list (org-property-values kwd))))
  8047. (unless (string-match "\\`{.*}\\'" value)
  8048. (setq value (concat "\"" value "\"")))
  8049. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8050. ((member ans '(?r ?R ?/))
  8051. (call-interactively 'org-occur))
  8052. (t (error "No such sparse tree command \"%c\"" ans)))))
  8053. (defvar org-occur-highlights nil
  8054. "List of overlays used for occur matches.")
  8055. (make-variable-buffer-local 'org-occur-highlights)
  8056. (defvar org-occur-parameters nil
  8057. "Parameters of the active org-occur calls.
  8058. This is a list, each call to org-occur pushes as cons cell,
  8059. containing the regular expression and the callback, onto the list.
  8060. The list can contain several entries if `org-occur' has been called
  8061. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8062. will only contain one set of parameters. When the highlights are
  8063. removed (for example with `C-c C-c', or with the next edit (depending
  8064. on `org-remove-highlights-with-change'), this variable is emptied
  8065. as well.")
  8066. (make-variable-buffer-local 'org-occur-parameters)
  8067. (defun org-occur (regexp &optional keep-previous callback)
  8068. "Make a compact tree which shows all matches of REGEXP.
  8069. The tree will show the lines where the regexp matches, and all higher
  8070. headlines above the match. It will also show the heading after the match,
  8071. to make sure editing the matching entry is easy.
  8072. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8073. call to `org-occur' will be kept, to allow stacking of calls to this
  8074. command.
  8075. If CALLBACK is non-nil, it is a function which is called to confirm
  8076. that the match should indeed be shown."
  8077. (interactive "sRegexp: \nP")
  8078. (unless keep-previous
  8079. (org-remove-occur-highlights nil nil t))
  8080. (push (cons regexp callback) org-occur-parameters)
  8081. (let ((cnt 0))
  8082. (save-excursion
  8083. (goto-char (point-min))
  8084. (if (or (not keep-previous) ; do not want to keep
  8085. (not org-occur-highlights)) ; no previous matches
  8086. ;; hide everything
  8087. (org-overview))
  8088. (while (re-search-forward regexp nil t)
  8089. (when (or (not callback)
  8090. (save-match-data (funcall callback)))
  8091. (setq cnt (1+ cnt))
  8092. (when org-highlight-sparse-tree-matches
  8093. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8094. (org-show-context 'occur-tree))))
  8095. (when org-remove-highlights-with-change
  8096. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8097. nil 'local))
  8098. (unless org-sparse-tree-open-archived-trees
  8099. (org-hide-archived-subtrees (point-min) (point-max)))
  8100. (run-hooks 'org-occur-hook)
  8101. (if (interactive-p)
  8102. (message "%d match(es) for regexp %s" cnt regexp))
  8103. cnt))
  8104. (defun org-show-context (&optional key)
  8105. "Make sure point and context and visible.
  8106. How much context is shown depends upon the variables
  8107. `org-show-hierarchy-above', `org-show-following-heading'. and
  8108. `org-show-siblings'."
  8109. (let ((heading-p (org-on-heading-p t))
  8110. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8111. (following-p (org-get-alist-option org-show-following-heading key))
  8112. (entry-p (org-get-alist-option org-show-entry-below key))
  8113. (siblings-p (org-get-alist-option org-show-siblings key)))
  8114. (catch 'exit
  8115. ;; Show heading or entry text
  8116. (if (and heading-p (not entry-p))
  8117. (org-flag-heading nil) ; only show the heading
  8118. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8119. (org-show-hidden-entry))) ; show entire entry
  8120. (when following-p
  8121. ;; Show next sibling, or heading below text
  8122. (save-excursion
  8123. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8124. (org-flag-heading nil))))
  8125. (when siblings-p (org-show-siblings))
  8126. (when hierarchy-p
  8127. ;; show all higher headings, possibly with siblings
  8128. (save-excursion
  8129. (while (and (condition-case nil
  8130. (progn (org-up-heading-all 1) t)
  8131. (error nil))
  8132. (not (bobp)))
  8133. (org-flag-heading nil)
  8134. (when siblings-p (org-show-siblings))))))))
  8135. (defun org-reveal (&optional siblings)
  8136. "Show current entry, hierarchy above it, and the following headline.
  8137. This can be used to show a consistent set of context around locations
  8138. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8139. not t for the search context.
  8140. With optional argument SIBLINGS, on each level of the hierarchy all
  8141. siblings are shown. This repairs the tree structure to what it would
  8142. look like when opened with hierarchical calls to `org-cycle'."
  8143. (interactive "P")
  8144. (let ((org-show-hierarchy-above t)
  8145. (org-show-following-heading t)
  8146. (org-show-siblings (if siblings t org-show-siblings)))
  8147. (org-show-context nil)))
  8148. (defun org-highlight-new-match (beg end)
  8149. "Highlight from BEG to END and mark the highlight is an occur headline."
  8150. (let ((ov (org-make-overlay beg end)))
  8151. (org-overlay-put ov 'face 'secondary-selection)
  8152. (push ov org-occur-highlights)))
  8153. (defun org-remove-occur-highlights (&optional beg end noremove)
  8154. "Remove the occur highlights from the buffer.
  8155. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8156. from the `before-change-functions' in the current buffer."
  8157. (interactive)
  8158. (unless org-inhibit-highlight-removal
  8159. (mapc 'org-delete-overlay org-occur-highlights)
  8160. (setq org-occur-highlights nil)
  8161. (setq org-occur-parameters nil)
  8162. (unless noremove
  8163. (remove-hook 'before-change-functions
  8164. 'org-remove-occur-highlights 'local))))
  8165. ;;;; Priorities
  8166. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8167. "Regular expression matching the priority indicator.")
  8168. (defvar org-remove-priority-next-time nil)
  8169. (defun org-priority-up ()
  8170. "Increase the priority of the current item."
  8171. (interactive)
  8172. (org-priority 'up))
  8173. (defun org-priority-down ()
  8174. "Decrease the priority of the current item."
  8175. (interactive)
  8176. (org-priority 'down))
  8177. (defun org-priority (&optional action)
  8178. "Change the priority of an item by ARG.
  8179. ACTION can be `set', `up', `down', or a character."
  8180. (interactive)
  8181. (setq action (or action 'set))
  8182. (let (current new news have remove)
  8183. (save-excursion
  8184. (org-back-to-heading)
  8185. (if (looking-at org-priority-regexp)
  8186. (setq current (string-to-char (match-string 2))
  8187. have t)
  8188. (setq current org-default-priority))
  8189. (cond
  8190. ((or (eq action 'set)
  8191. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8192. (if (not (eq action 'set))
  8193. (setq new action)
  8194. (message "Priority %c-%c, SPC to remove: "
  8195. org-highest-priority org-lowest-priority)
  8196. (setq new (read-char-exclusive)))
  8197. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8198. (= (upcase org-lowest-priority) org-lowest-priority))
  8199. (setq new (upcase new)))
  8200. (cond ((equal new ?\ ) (setq remove t))
  8201. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8202. (error "Priority must be between `%c' and `%c'"
  8203. org-highest-priority org-lowest-priority))))
  8204. ((eq action 'up)
  8205. (if (and (not have) (eq last-command this-command))
  8206. (setq new org-lowest-priority)
  8207. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8208. org-default-priority (1- current)))))
  8209. ((eq action 'down)
  8210. (if (and (not have) (eq last-command this-command))
  8211. (setq new org-highest-priority)
  8212. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8213. org-default-priority (1+ current)))))
  8214. (t (error "Invalid action")))
  8215. (if (or (< (upcase new) org-highest-priority)
  8216. (> (upcase new) org-lowest-priority))
  8217. (setq remove t))
  8218. (setq news (format "%c" new))
  8219. (if have
  8220. (if remove
  8221. (replace-match "" t t nil 1)
  8222. (replace-match news t t nil 2))
  8223. (if remove
  8224. (error "No priority cookie found in line")
  8225. (looking-at org-todo-line-regexp)
  8226. (if (match-end 2)
  8227. (progn
  8228. (goto-char (match-end 2))
  8229. (insert " [#" news "]"))
  8230. (goto-char (match-beginning 3))
  8231. (insert "[#" news "] ")))))
  8232. (org-preserve-lc (org-set-tags nil 'align))
  8233. (if remove
  8234. (message "Priority removed")
  8235. (message "Priority of current item set to %s" news))))
  8236. (defun org-get-priority (s)
  8237. "Find priority cookie and return priority."
  8238. (save-match-data
  8239. (if (not (string-match org-priority-regexp s))
  8240. (* 1000 (- org-lowest-priority org-default-priority))
  8241. (* 1000 (- org-lowest-priority
  8242. (string-to-char (match-string 2 s)))))))
  8243. ;;;; Tags
  8244. (defvar org-agenda-archives-mode)
  8245. (defun org-scan-tags (action matcher &optional todo-only)
  8246. "Scan headline tags with inheritance and produce output ACTION.
  8247. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8248. or `agenda' to produce an entry list for an agenda view. It can also be
  8249. a Lisp form or a function that should be called at each matched headline, in
  8250. this case the return value is a list of all return values from these calls.
  8251. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8252. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8253. only lines with a TODO keyword are included in the output."
  8254. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8255. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8256. (org-re
  8257. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8258. (props (list 'face 'default
  8259. 'done-face 'org-done
  8260. 'undone-face 'default
  8261. 'mouse-face 'highlight
  8262. 'org-not-done-regexp org-not-done-regexp
  8263. 'org-todo-regexp org-todo-regexp
  8264. 'keymap org-agenda-keymap
  8265. 'help-echo
  8266. (format "mouse-2 or RET jump to org file %s"
  8267. (abbreviate-file-name
  8268. (or (buffer-file-name (buffer-base-buffer))
  8269. (buffer-name (buffer-base-buffer)))))))
  8270. (case-fold-search nil)
  8271. lspos tags tags-list
  8272. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8273. (llast 0) rtn rtn1 level category i txt
  8274. todo marker entry priority)
  8275. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8276. (setq action (list 'lambda nil action)))
  8277. (save-excursion
  8278. (goto-char (point-min))
  8279. (when (eq action 'sparse-tree)
  8280. (org-overview)
  8281. (org-remove-occur-highlights))
  8282. (while (re-search-forward re nil t)
  8283. (catch :skip
  8284. (setq todo (if (match-end 1) (match-string 2))
  8285. tags (if (match-end 4) (match-string 4)))
  8286. (goto-char (setq lspos (1+ (match-beginning 0))))
  8287. (setq level (org-reduced-level (funcall outline-level))
  8288. category (org-get-category))
  8289. (setq i llast llast level)
  8290. ;; remove tag lists from same and sublevels
  8291. (while (>= i level)
  8292. (when (setq entry (assoc i tags-alist))
  8293. (setq tags-alist (delete entry tags-alist)))
  8294. (setq i (1- i)))
  8295. ;; add the next tags
  8296. (when tags
  8297. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8298. tags-alist
  8299. (cons (cons level tags) tags-alist)))
  8300. ;; compile tags for current headline
  8301. (setq tags-list
  8302. (if org-use-tag-inheritance
  8303. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8304. tags))
  8305. (when org-use-tag-inheritance
  8306. (setcdr (car tags-alist)
  8307. (mapcar (lambda (x)
  8308. (setq x (copy-sequence x))
  8309. (org-add-prop-inherited x))
  8310. (cdar tags-alist))))
  8311. (when (and tags org-use-tag-inheritance
  8312. (not (eq t org-use-tag-inheritance)))
  8313. ;; selective inheritance, remove uninherited ones
  8314. (setcdr (car tags-alist)
  8315. (org-remove-uniherited-tags (cdar tags-alist))))
  8316. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8317. (let ((case-fold-search t)) (eval matcher))
  8318. (or
  8319. (not (member org-archive-tag tags-list))
  8320. ;; we have an archive tag, should we use this anyway?
  8321. (or (not org-agenda-skip-archived-trees)
  8322. (and (eq action 'agenda) org-agenda-archives-mode))))
  8323. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8324. ;; select this headline
  8325. (cond
  8326. ((eq action 'sparse-tree)
  8327. (and org-highlight-sparse-tree-matches
  8328. (org-get-heading) (match-end 0)
  8329. (org-highlight-new-match
  8330. (match-beginning 0) (match-beginning 1)))
  8331. (org-show-context 'tags-tree))
  8332. ((eq action 'agenda)
  8333. (setq txt (org-format-agenda-item
  8334. ""
  8335. (concat
  8336. (if org-tags-match-list-sublevels
  8337. (make-string (1- level) ?.) "")
  8338. (org-get-heading))
  8339. category tags-list)
  8340. priority (org-get-priority txt))
  8341. (goto-char lspos)
  8342. (setq marker (org-agenda-new-marker))
  8343. (org-add-props txt props
  8344. 'org-marker marker 'org-hd-marker marker 'org-category category
  8345. 'priority priority 'type "tagsmatch")
  8346. (push txt rtn))
  8347. ((functionp action)
  8348. (save-excursion
  8349. (setq rtn1 (funcall action))
  8350. (push rtn1 rtn))
  8351. (goto-char (point-at-eol)))
  8352. (t (error "Invalid action")))
  8353. ;; if we are to skip sublevels, jump to end of subtree
  8354. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8355. (when (and (eq action 'sparse-tree)
  8356. (not org-sparse-tree-open-archived-trees))
  8357. (org-hide-archived-subtrees (point-min) (point-max)))
  8358. (nreverse rtn)))
  8359. (defun org-remove-uniherited-tags (tags)
  8360. "Remove all tags that are not inherited from the list TAGS."
  8361. (cond
  8362. ((eq org-use-tag-inheritance t)
  8363. (if org-tags-exclude-from-inheritance
  8364. (org-delete-all org-tags-exclude-from-inheritance tags)
  8365. tags))
  8366. ((not org-use-tag-inheritance) nil)
  8367. ((stringp org-use-tag-inheritance)
  8368. (delq nil (mapcar
  8369. (lambda (x)
  8370. (if (and (string-match org-use-tag-inheritance x)
  8371. (not (member x org-tags-exclude-from-inheritance)))
  8372. x nil))
  8373. tags)))
  8374. ((listp org-use-tag-inheritance)
  8375. (delq nil (mapcar
  8376. (lambda (x)
  8377. (if (member x org-use-tag-inheritance) x nil))
  8378. tags)))))
  8379. (defvar todo-only) ;; dynamically scoped
  8380. (defun org-tags-sparse-tree (&optional todo-only match)
  8381. "Create a sparse tree according to tags string MATCH.
  8382. MATCH can contain positive and negative selection of tags, like
  8383. \"+WORK+URGENT-WITHBOSS\".
  8384. If optional argument TODO-ONLY is non-nil, only select lines that are
  8385. also TODO lines."
  8386. (interactive "P")
  8387. (org-prepare-agenda-buffers (list (current-buffer)))
  8388. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8389. (defvar org-cached-props nil)
  8390. (defun org-cached-entry-get (pom property)
  8391. (if (or (eq t org-use-property-inheritance)
  8392. (and (stringp org-use-property-inheritance)
  8393. (string-match org-use-property-inheritance property))
  8394. (and (listp org-use-property-inheritance)
  8395. (member property org-use-property-inheritance)))
  8396. ;; Caching is not possible, check it directly
  8397. (org-entry-get pom property 'inherit)
  8398. ;; Get all properties, so that we can do complicated checks easily
  8399. (cdr (assoc property (or org-cached-props
  8400. (setq org-cached-props
  8401. (org-entry-properties pom)))))))
  8402. (defun org-global-tags-completion-table (&optional files)
  8403. "Return the list of all tags in all agenda buffer/files."
  8404. (save-excursion
  8405. (org-uniquify
  8406. (delq nil
  8407. (apply 'append
  8408. (mapcar
  8409. (lambda (file)
  8410. (set-buffer (find-file-noselect file))
  8411. (append (org-get-buffer-tags)
  8412. (mapcar (lambda (x) (if (stringp (car-safe x))
  8413. (list (car-safe x)) nil))
  8414. org-tag-alist)))
  8415. (if (and files (car files))
  8416. files
  8417. (org-agenda-files))))))))
  8418. (defun org-make-tags-matcher (match)
  8419. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8420. ;; todo-only is scoped dynamically into this function, and the function
  8421. ;; may change it it the matcher asksk for it.
  8422. (unless match
  8423. ;; Get a new match request, with completion
  8424. (let ((org-last-tags-completion-table
  8425. (org-global-tags-completion-table)))
  8426. (setq match (org-ido-completing-read
  8427. "Match: " 'org-tags-completion-function nil nil nil
  8428. 'org-tags-history))))
  8429. ;; Parse the string and create a lisp form
  8430. (let ((match0 match)
  8431. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8432. minus tag mm
  8433. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8434. orterms term orlist re-p str-p level-p level-op time-p
  8435. prop-p pn pv po cat-p gv rest)
  8436. (if (string-match "/+" match)
  8437. ;; match contains also a todo-matching request
  8438. (progn
  8439. (setq tagsmatch (substring match 0 (match-beginning 0))
  8440. todomatch (substring match (match-end 0)))
  8441. (if (string-match "^!" todomatch)
  8442. (setq todo-only t todomatch (substring todomatch 1)))
  8443. (if (string-match "^\\s-*$" todomatch)
  8444. (setq todomatch nil)))
  8445. ;; only matching tags
  8446. (setq tagsmatch match todomatch nil))
  8447. ;; Make the tags matcher
  8448. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8449. (setq tagsmatcher t)
  8450. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8451. (while (setq term (pop orterms))
  8452. (while (and (equal (substring term -1) "\\") orterms)
  8453. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8454. (while (string-match re term)
  8455. (setq rest (substring term (match-end 0))
  8456. minus (and (match-end 1)
  8457. (equal (match-string 1 term) "-"))
  8458. tag (match-string 2 term)
  8459. re-p (equal (string-to-char tag) ?{)
  8460. level-p (match-end 4)
  8461. prop-p (match-end 5)
  8462. mm (cond
  8463. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8464. (level-p
  8465. (setq level-op (org-op-to-function (match-string 3 term)))
  8466. `(,level-op level ,(string-to-number
  8467. (match-string 4 term))))
  8468. (prop-p
  8469. (setq pn (match-string 5 term)
  8470. po (match-string 6 term)
  8471. pv (match-string 7 term)
  8472. cat-p (equal pn "CATEGORY")
  8473. re-p (equal (string-to-char pv) ?{)
  8474. str-p (equal (string-to-char pv) ?\")
  8475. time-p (save-match-data
  8476. (string-match "^\"[[<].*[]>]\"$" pv))
  8477. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8478. (if time-p (setq pv (org-matcher-time pv)))
  8479. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8480. (cond
  8481. ((equal pn "CATEGORY")
  8482. (setq gv '(get-text-property (point) 'org-category)))
  8483. ((equal pn "TODO")
  8484. (setq gv 'todo))
  8485. (t
  8486. (setq gv `(org-cached-entry-get nil ,pn))))
  8487. (if re-p
  8488. (if (eq po 'org<>)
  8489. `(not (string-match ,pv (or ,gv "")))
  8490. `(string-match ,pv (or ,gv "")))
  8491. (if str-p
  8492. `(,po (or ,gv "") ,pv)
  8493. `(,po (string-to-number (or ,gv ""))
  8494. ,(string-to-number pv) ))))
  8495. (t `(member ,(downcase tag) tags-list)))
  8496. mm (if minus (list 'not mm) mm)
  8497. term rest)
  8498. (push mm tagsmatcher))
  8499. (push (if (> (length tagsmatcher) 1)
  8500. (cons 'and tagsmatcher)
  8501. (car tagsmatcher))
  8502. orlist)
  8503. (setq tagsmatcher nil))
  8504. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8505. (setq tagsmatcher
  8506. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8507. ;; Make the todo matcher
  8508. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8509. (setq todomatcher t)
  8510. (setq orterms (org-split-string todomatch "|") orlist nil)
  8511. (while (setq term (pop orterms))
  8512. (while (string-match re term)
  8513. (setq minus (and (match-end 1)
  8514. (equal (match-string 1 term) "-"))
  8515. kwd (match-string 2 term)
  8516. re-p (equal (string-to-char kwd) ?{)
  8517. term (substring term (match-end 0))
  8518. mm (if re-p
  8519. `(string-match ,(substring kwd 1 -1) todo)
  8520. (list 'equal 'todo kwd))
  8521. mm (if minus (list 'not mm) mm))
  8522. (push mm todomatcher))
  8523. (push (if (> (length todomatcher) 1)
  8524. (cons 'and todomatcher)
  8525. (car todomatcher))
  8526. orlist)
  8527. (setq todomatcher nil))
  8528. (setq todomatcher (if (> (length orlist) 1)
  8529. (cons 'or orlist) (car orlist))))
  8530. ;; Return the string and lisp forms of the matcher
  8531. (setq matcher (if todomatcher
  8532. (list 'and tagsmatcher todomatcher)
  8533. tagsmatcher))
  8534. (cons match0 matcher)))
  8535. (defun org-op-to-function (op &optional stringp)
  8536. "Turn an operator into the appropriate function."
  8537. (setq op
  8538. (cond
  8539. ((equal op "<" ) '(< string< org-time<))
  8540. ((equal op ">" ) '(> org-string> org-time>))
  8541. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8542. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8543. ((member op '("=" "==")) '(= string= org-time=))
  8544. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8545. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8546. (defun org<> (a b) (not (= a b)))
  8547. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8548. (defun org-string>= (a b) (not (string< a b)))
  8549. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8550. (defun org-string<> (a b) (not (string= a b)))
  8551. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8552. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8553. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8554. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8555. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8556. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8557. (defun org-2ft (s)
  8558. "Convert S to a floating point time.
  8559. If S is already a number, just return it. If it is a string, parse
  8560. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8561. (cond
  8562. ((numberp s) s)
  8563. ((stringp s)
  8564. (condition-case nil
  8565. (float-time (apply 'encode-time (org-parse-time-string s)))
  8566. (error 0.)))
  8567. (t 0.)))
  8568. (defun org-time-today ()
  8569. "Time in seconds today at 0:00.
  8570. Returns the float number of seconds since the beginning of the
  8571. epoch to the beginning of today (00:00)."
  8572. (float-time (apply 'encode-time
  8573. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8574. (defun org-matcher-time (s)
  8575. "Interprete a time comparison value."
  8576. (save-match-data
  8577. (cond
  8578. ((string= s "<now>") (float-time))
  8579. ((string= s "<today>") (org-time-today))
  8580. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8581. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8582. ((string-match "^<\\([-+][0-9]+\\)\\([dwmy]\\)>$" s)
  8583. (+ (org-time-today)
  8584. (* (string-to-number (match-string 1 s))
  8585. (cdr (assoc (match-string 2 s)
  8586. '(("d" . 86400.0) ("w" . 604800.0)
  8587. ("m" . 2678400.0) ("y" . 31557600.0)))))))
  8588. (t (org-2ft s)))))
  8589. (defun org-match-any-p (re list)
  8590. "Does re match any element of list?"
  8591. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8592. (delq nil list))
  8593. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8594. (defvar org-tags-overlay (org-make-overlay 1 1))
  8595. (org-detach-overlay org-tags-overlay)
  8596. (defun org-get-local-tags-at (&optional pos)
  8597. "Get a list of tags defined in the current headline."
  8598. (org-get-tags-at pos 'local))
  8599. (defun org-get-local-tags ()
  8600. "Get a list of tags defined in the current headline."
  8601. (org-get-tags-at nil 'local))
  8602. (defun org-get-tags-at (&optional pos local)
  8603. "Get a list of all headline tags applicable at POS.
  8604. POS defaults to point. If tags are inherited, the list contains
  8605. the targets in the same sequence as the headlines appear, i.e.
  8606. the tags of the current headline come last.
  8607. When LOCAL is non-nil, only return tags from the current headline,
  8608. ignore inherited ones."
  8609. (interactive)
  8610. (let (tags ltags lastpos parent)
  8611. (save-excursion
  8612. (save-restriction
  8613. (widen)
  8614. (goto-char (or pos (point)))
  8615. (save-match-data
  8616. (catch 'done
  8617. (condition-case nil
  8618. (progn
  8619. (org-back-to-heading t)
  8620. (while (not (equal lastpos (point)))
  8621. (setq lastpos (point))
  8622. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8623. (setq ltags (org-split-string
  8624. (org-match-string-no-properties 1) ":"))
  8625. (when parent
  8626. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8627. (setq tags (append
  8628. (if parent
  8629. (org-remove-uniherited-tags ltags)
  8630. ltags)
  8631. tags)))
  8632. (or org-use-tag-inheritance (throw 'done t))
  8633. (if local (throw 'done t))
  8634. (org-up-heading-all 1)
  8635. (setq parent t)))
  8636. (error nil)))))
  8637. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8638. (defun org-add-prop-inherited (s)
  8639. (add-text-properties 0 (length s) '(inherited t) s)
  8640. s)
  8641. (defun org-toggle-tag (tag &optional onoff)
  8642. "Toggle the tag TAG for the current line.
  8643. If ONOFF is `on' or `off', don't toggle but set to this state."
  8644. (unless (org-on-heading-p t) (error "Not on headling"))
  8645. (let (res current)
  8646. (save-excursion
  8647. (beginning-of-line)
  8648. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8649. (point-at-eol) t)
  8650. (progn
  8651. (setq current (match-string 1))
  8652. (replace-match ""))
  8653. (setq current ""))
  8654. (setq current (nreverse (org-split-string current ":")))
  8655. (cond
  8656. ((eq onoff 'on)
  8657. (setq res t)
  8658. (or (member tag current) (push tag current)))
  8659. ((eq onoff 'off)
  8660. (or (not (member tag current)) (setq current (delete tag current))))
  8661. (t (if (member tag current)
  8662. (setq current (delete tag current))
  8663. (setq res t)
  8664. (push tag current))))
  8665. (end-of-line 1)
  8666. (if current
  8667. (progn
  8668. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8669. (org-set-tags nil t))
  8670. (delete-horizontal-space))
  8671. (run-hooks 'org-after-tags-change-hook))
  8672. res))
  8673. (defun org-align-tags-here (to-col)
  8674. ;; Assumes that this is a headline
  8675. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8676. (beginning-of-line 1)
  8677. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8678. (< pos (match-beginning 2)))
  8679. (progn
  8680. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8681. (goto-char (match-beginning 1))
  8682. (insert " ")
  8683. (delete-region (point) (1+ (match-beginning 2)))
  8684. (setq ncol (max (1+ (current-column))
  8685. (1+ col)
  8686. (if (> to-col 0)
  8687. to-col
  8688. (- (abs to-col) tags-l))))
  8689. (setq p (point))
  8690. (insert (make-string (- ncol (current-column)) ?\ ))
  8691. (setq ncol (current-column))
  8692. (when indent-tabs-mode (tabify p (point-at-eol)))
  8693. (org-move-to-column (min ncol col) t))
  8694. (goto-char pos))))
  8695. (defun org-set-tags-command (&optional arg just-align)
  8696. "Call the set-tags command for the current entry."
  8697. (interactive "P")
  8698. (if (org-on-heading-p)
  8699. (org-set-tags arg just-align)
  8700. (save-excursion
  8701. (org-back-to-heading t)
  8702. (org-set-tags arg just-align))))
  8703. (defun org-set-tags (&optional arg just-align)
  8704. "Set the tags for the current headline.
  8705. With prefix ARG, realign all tags in headings in the current buffer."
  8706. (interactive "P")
  8707. (let* ((re (concat "^" outline-regexp))
  8708. (current (org-get-tags-string))
  8709. (col (current-column))
  8710. (org-setting-tags t)
  8711. table current-tags inherited-tags ; computed below when needed
  8712. tags p0 c0 c1 rpl)
  8713. (if arg
  8714. (save-excursion
  8715. (goto-char (point-min))
  8716. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8717. (while (re-search-forward re nil t)
  8718. (org-set-tags nil t)
  8719. (end-of-line 1)))
  8720. (message "All tags realigned to column %d" org-tags-column))
  8721. (if just-align
  8722. (setq tags current)
  8723. ;; Get a new set of tags from the user
  8724. (save-excursion
  8725. (setq table (or org-tag-alist (org-get-buffer-tags))
  8726. org-last-tags-completion-table table
  8727. current-tags (org-split-string current ":")
  8728. inherited-tags (nreverse
  8729. (nthcdr (length current-tags)
  8730. (nreverse (org-get-tags-at))))
  8731. tags
  8732. (if (or (eq t org-use-fast-tag-selection)
  8733. (and org-use-fast-tag-selection
  8734. (delq nil (mapcar 'cdr table))))
  8735. (org-fast-tag-selection
  8736. current-tags inherited-tags table
  8737. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8738. (let ((org-add-colon-after-tag-completion t))
  8739. (org-trim
  8740. (org-without-partial-completion
  8741. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8742. nil nil current 'org-tags-history)))))))
  8743. (while (string-match "[-+&]+" tags)
  8744. ;; No boolean logic, just a list
  8745. (setq tags (replace-match ":" t t tags))))
  8746. (if (string-match "\\`[\t ]*\\'" tags)
  8747. (setq tags "")
  8748. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8749. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8750. ;; Insert new tags at the correct column
  8751. (beginning-of-line 1)
  8752. (cond
  8753. ((and (equal current "") (equal tags "")))
  8754. ((re-search-forward
  8755. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8756. (point-at-eol) t)
  8757. (if (equal tags "")
  8758. (setq rpl "")
  8759. (goto-char (match-beginning 0))
  8760. (setq c0 (current-column) p0 (point)
  8761. c1 (max (1+ c0) (if (> org-tags-column 0)
  8762. org-tags-column
  8763. (- (- org-tags-column) (length tags))))
  8764. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8765. (replace-match rpl t t)
  8766. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8767. tags)
  8768. (t (error "Tags alignment failed")))
  8769. (org-move-to-column col)
  8770. (unless just-align
  8771. (run-hooks 'org-after-tags-change-hook)))))
  8772. (defun org-change-tag-in-region (beg end tag off)
  8773. "Add or remove TAG for each entry in the region.
  8774. This works in the agenda, and also in an org-mode buffer."
  8775. (interactive
  8776. (list (region-beginning) (region-end)
  8777. (let ((org-last-tags-completion-table
  8778. (if (org-mode-p)
  8779. (org-get-buffer-tags)
  8780. (org-global-tags-completion-table))))
  8781. (org-ido-completing-read
  8782. "Tag: " 'org-tags-completion-function nil nil nil
  8783. 'org-tags-history))
  8784. (progn
  8785. (message "[s]et or [r]emove? ")
  8786. (equal (read-char-exclusive) ?r))))
  8787. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8788. (let ((agendap (equal major-mode 'org-agenda-mode))
  8789. l1 l2 m buf pos newhead (cnt 0))
  8790. (goto-char end)
  8791. (setq l2 (1- (org-current-line)))
  8792. (goto-char beg)
  8793. (setq l1 (org-current-line))
  8794. (loop for l from l1 to l2 do
  8795. (goto-line l)
  8796. (setq m (get-text-property (point) 'org-hd-marker))
  8797. (when (or (and (org-mode-p) (org-on-heading-p))
  8798. (and agendap m))
  8799. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8800. pos (if agendap m (point)))
  8801. (with-current-buffer buf
  8802. (save-excursion
  8803. (save-restriction
  8804. (goto-char pos)
  8805. (setq cnt (1+ cnt))
  8806. (org-toggle-tag tag (if off 'off 'on))
  8807. (setq newhead (org-get-heading)))))
  8808. (and agendap (org-agenda-change-all-lines newhead m))))
  8809. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8810. (defun org-tags-completion-function (string predicate &optional flag)
  8811. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8812. (confirm (lambda (x) (stringp (car x)))))
  8813. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8814. (setq s1 (match-string 1 string)
  8815. s2 (match-string 2 string))
  8816. (setq s1 "" s2 string))
  8817. (cond
  8818. ((eq flag nil)
  8819. ;; try completion
  8820. (setq rtn (try-completion s2 ctable confirm))
  8821. (if (stringp rtn)
  8822. (setq rtn
  8823. (concat s1 s2 (substring rtn (length s2))
  8824. (if (and org-add-colon-after-tag-completion
  8825. (assoc rtn ctable))
  8826. ":" ""))))
  8827. rtn)
  8828. ((eq flag t)
  8829. ;; all-completions
  8830. (all-completions s2 ctable confirm)
  8831. )
  8832. ((eq flag 'lambda)
  8833. ;; exact match?
  8834. (assoc s2 ctable)))
  8835. ))
  8836. (defun org-fast-tag-insert (kwd tags face &optional end)
  8837. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8838. (insert (format "%-12s" (concat kwd ":"))
  8839. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8840. (or end "")))
  8841. (defun org-fast-tag-show-exit (flag)
  8842. (save-excursion
  8843. (goto-line 3)
  8844. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8845. (replace-match ""))
  8846. (when flag
  8847. (end-of-line 1)
  8848. (org-move-to-column (- (window-width) 19) t)
  8849. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8850. (defun org-set-current-tags-overlay (current prefix)
  8851. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8852. (if (featurep 'xemacs)
  8853. (org-overlay-display org-tags-overlay (concat prefix s)
  8854. 'secondary-selection)
  8855. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8856. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8857. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8858. "Fast tag selection with single keys.
  8859. CURRENT is the current list of tags in the headline, INHERITED is the
  8860. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8861. possibly with grouping information. TODO-TABLE is a similar table with
  8862. TODO keywords, should these have keys assigned to them.
  8863. If the keys are nil, a-z are automatically assigned.
  8864. Returns the new tags string, or nil to not change the current settings."
  8865. (let* ((fulltable (append table todo-table))
  8866. (maxlen (apply 'max (mapcar
  8867. (lambda (x)
  8868. (if (stringp (car x)) (string-width (car x)) 0))
  8869. fulltable)))
  8870. (buf (current-buffer))
  8871. (expert (eq org-fast-tag-selection-single-key 'expert))
  8872. (buffer-tags nil)
  8873. (fwidth (+ maxlen 3 1 3))
  8874. (ncol (/ (- (window-width) 4) fwidth))
  8875. (i-face 'org-done)
  8876. (c-face 'org-todo)
  8877. tg cnt e c char c1 c2 ntable tbl rtn
  8878. ov-start ov-end ov-prefix
  8879. (exit-after-next org-fast-tag-selection-single-key)
  8880. (done-keywords org-done-keywords)
  8881. groups ingroup)
  8882. (save-excursion
  8883. (beginning-of-line 1)
  8884. (if (looking-at
  8885. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8886. (setq ov-start (match-beginning 1)
  8887. ov-end (match-end 1)
  8888. ov-prefix "")
  8889. (setq ov-start (1- (point-at-eol))
  8890. ov-end (1+ ov-start))
  8891. (skip-chars-forward "^\n\r")
  8892. (setq ov-prefix
  8893. (concat
  8894. (buffer-substring (1- (point)) (point))
  8895. (if (> (current-column) org-tags-column)
  8896. " "
  8897. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8898. (org-move-overlay org-tags-overlay ov-start ov-end)
  8899. (save-window-excursion
  8900. (if expert
  8901. (set-buffer (get-buffer-create " *Org tags*"))
  8902. (delete-other-windows)
  8903. (split-window-vertically)
  8904. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8905. (erase-buffer)
  8906. (org-set-local 'org-done-keywords done-keywords)
  8907. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8908. (org-fast-tag-insert "Current" current c-face "\n\n")
  8909. (org-fast-tag-show-exit exit-after-next)
  8910. (org-set-current-tags-overlay current ov-prefix)
  8911. (setq tbl fulltable char ?a cnt 0)
  8912. (while (setq e (pop tbl))
  8913. (cond
  8914. ((equal e '(:startgroup))
  8915. (push '() groups) (setq ingroup t)
  8916. (when (not (= cnt 0))
  8917. (setq cnt 0)
  8918. (insert "\n"))
  8919. (insert "{ "))
  8920. ((equal e '(:endgroup))
  8921. (setq ingroup nil cnt 0)
  8922. (insert "}\n"))
  8923. (t
  8924. (setq tg (car e) c2 nil)
  8925. (if (cdr e)
  8926. (setq c (cdr e))
  8927. ;; automatically assign a character.
  8928. (setq c1 (string-to-char
  8929. (downcase (substring
  8930. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8931. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8932. (while (or (rassoc char ntable) (rassoc char table))
  8933. (setq char (1+ char)))
  8934. (setq c2 c1))
  8935. (setq c (or c2 char)))
  8936. (if ingroup (push tg (car groups)))
  8937. (setq tg (org-add-props tg nil 'face
  8938. (cond
  8939. ((not (assoc tg table))
  8940. (org-get-todo-face tg))
  8941. ((member tg current) c-face)
  8942. ((member tg inherited) i-face)
  8943. (t nil))))
  8944. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8945. (insert "[" c "] " tg (make-string
  8946. (- fwidth 4 (length tg)) ?\ ))
  8947. (push (cons tg c) ntable)
  8948. (when (= (setq cnt (1+ cnt)) ncol)
  8949. (insert "\n")
  8950. (if ingroup (insert " "))
  8951. (setq cnt 0)))))
  8952. (setq ntable (nreverse ntable))
  8953. (insert "\n")
  8954. (goto-char (point-min))
  8955. (if (not expert) (org-fit-window-to-buffer))
  8956. (setq rtn
  8957. (catch 'exit
  8958. (while t
  8959. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8960. (if groups " [!] no groups" " [!]groups")
  8961. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8962. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8963. (cond
  8964. ((= c ?\r) (throw 'exit t))
  8965. ((= c ?!)
  8966. (setq groups (not groups))
  8967. (goto-char (point-min))
  8968. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8969. ((= c ?\C-c)
  8970. (if (not expert)
  8971. (org-fast-tag-show-exit
  8972. (setq exit-after-next (not exit-after-next)))
  8973. (setq expert nil)
  8974. (delete-other-windows)
  8975. (split-window-vertically)
  8976. (org-switch-to-buffer-other-window " *Org tags*")
  8977. (org-fit-window-to-buffer)))
  8978. ((or (= c ?\C-g)
  8979. (and (= c ?q) (not (rassoc c ntable))))
  8980. (org-detach-overlay org-tags-overlay)
  8981. (setq quit-flag t))
  8982. ((= c ?\ )
  8983. (setq current nil)
  8984. (if exit-after-next (setq exit-after-next 'now)))
  8985. ((= c ?\t)
  8986. (condition-case nil
  8987. (setq tg (org-ido-completing-read
  8988. "Tag: "
  8989. (or buffer-tags
  8990. (with-current-buffer buf
  8991. (org-get-buffer-tags)))))
  8992. (quit (setq tg "")))
  8993. (when (string-match "\\S-" tg)
  8994. (add-to-list 'buffer-tags (list tg))
  8995. (if (member tg current)
  8996. (setq current (delete tg current))
  8997. (push tg current)))
  8998. (if exit-after-next (setq exit-after-next 'now)))
  8999. ((setq e (rassoc c todo-table) tg (car e))
  9000. (with-current-buffer buf
  9001. (save-excursion (org-todo tg)))
  9002. (if exit-after-next (setq exit-after-next 'now)))
  9003. ((setq e (rassoc c ntable) tg (car e))
  9004. (if (member tg current)
  9005. (setq current (delete tg current))
  9006. (loop for g in groups do
  9007. (if (member tg g)
  9008. (mapc (lambda (x)
  9009. (setq current (delete x current)))
  9010. g)))
  9011. (push tg current))
  9012. (if exit-after-next (setq exit-after-next 'now))))
  9013. ;; Create a sorted list
  9014. (setq current
  9015. (sort current
  9016. (lambda (a b)
  9017. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9018. (if (eq exit-after-next 'now) (throw 'exit t))
  9019. (goto-char (point-min))
  9020. (beginning-of-line 2)
  9021. (delete-region (point) (point-at-eol))
  9022. (org-fast-tag-insert "Current" current c-face)
  9023. (org-set-current-tags-overlay current ov-prefix)
  9024. (while (re-search-forward
  9025. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9026. (setq tg (match-string 1))
  9027. (add-text-properties
  9028. (match-beginning 1) (match-end 1)
  9029. (list 'face
  9030. (cond
  9031. ((member tg current) c-face)
  9032. ((member tg inherited) i-face)
  9033. (t (get-text-property (match-beginning 1) 'face))))))
  9034. (goto-char (point-min)))))
  9035. (org-detach-overlay org-tags-overlay)
  9036. (if rtn
  9037. (mapconcat 'identity current ":")
  9038. nil))))
  9039. (defun org-get-tags-string ()
  9040. "Get the TAGS string in the current headline."
  9041. (unless (org-on-heading-p t)
  9042. (error "Not on a heading"))
  9043. (save-excursion
  9044. (beginning-of-line 1)
  9045. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9046. (org-match-string-no-properties 1)
  9047. "")))
  9048. (defun org-get-tags ()
  9049. "Get the list of tags specified in the current headline."
  9050. (org-split-string (org-get-tags-string) ":"))
  9051. (defun org-get-buffer-tags ()
  9052. "Get a table of all tags used in the buffer, for completion."
  9053. (let (tags)
  9054. (save-excursion
  9055. (goto-char (point-min))
  9056. (while (re-search-forward
  9057. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9058. (when (equal (char-after (point-at-bol 0)) ?*)
  9059. (mapc (lambda (x) (add-to-list 'tags x))
  9060. (org-split-string (org-match-string-no-properties 1) ":")))))
  9061. (mapcar 'list tags)))
  9062. ;;;; The mapping API
  9063. ;;;###autoload
  9064. (defun org-map-entries (func &optional match scope &rest skip)
  9065. "Call FUNC at each headline selected by MATCH in SCOPE.
  9066. FUNC is a function or a lisp form. The function will be called without
  9067. arguments, with the cursor positioned at the beginning of the headline.
  9068. The return values of all calls to the function will be collected and
  9069. returned as a list.
  9070. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9071. Only headlines that are matched by this query will be considered during
  9072. the iteration. When MATCH is nil or t, all headlines will be
  9073. visited by the iteration.
  9074. SCOPE determines the scope of this command. It can be any of:
  9075. nil The current buffer, respecting the restriction if any
  9076. tree The subtree started with the entry at point
  9077. file The current buffer, without restriction
  9078. file-with-archives
  9079. The current buffer, and any archives associated with it
  9080. agenda All agenda files
  9081. agenda-with-archives
  9082. All agenda files with any archive files associated with them
  9083. \(file1 file2 ...)
  9084. If this is a list, all files in the list will be scanned
  9085. The remaining args are treated as settings for the skipping facilities of
  9086. the scanner. The following items can be given here:
  9087. archive skip trees with the archive tag.
  9088. comment skip trees with the COMMENT keyword
  9089. function or Emacs Lisp form:
  9090. will be used as value for `org-agenda-skip-function', so whenever
  9091. the the function returns t, FUNC will not be called for that
  9092. entry and search will continue from the point where the
  9093. function leaves it."
  9094. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9095. (org-agenda-skip-archived-trees (memq 'archive skip))
  9096. (org-agenda-skip-comment-trees (memq 'comment skip))
  9097. (org-agenda-skip-function
  9098. (car (org-delete-all '(comment archive) skip)))
  9099. (org-tags-match-list-sublevels t)
  9100. matcher pos file res
  9101. org-todo-keywords-for-agenda
  9102. org-done-keywords-for-agenda
  9103. org-todo-keyword-alist-for-agenda
  9104. org-tag-alist-for-agenda)
  9105. (cond
  9106. ((eq match t) (setq matcher t))
  9107. ((eq match nil) (setq matcher t))
  9108. (t (setq matcher (if match (cdr (org-make-tags-matcher match)) t))))
  9109. (when (eq scope 'tree)
  9110. (org-back-to-heading t)
  9111. (org-narrow-to-subtree)
  9112. (setq scope nil))
  9113. (if (not scope)
  9114. (progn
  9115. (org-prepare-agenda-buffers
  9116. (list (buffer-file-name (current-buffer))))
  9117. (org-scan-tags func matcher))
  9118. ;; Get the right scope
  9119. (setq pos (point))
  9120. (cond
  9121. ((and scope (listp scope) (symbolp (car scope)))
  9122. (setq scope (eval scope)))
  9123. ((eq scope 'agenda)
  9124. (setq scope (org-agenda-files t)))
  9125. ((eq scope 'agenda-with-archives)
  9126. (setq scope (org-agenda-files t))
  9127. (setq scope (org-add-archive-files scope)))
  9128. ((eq scope 'file)
  9129. (setq scope (list (buffer-file-name))))
  9130. ((eq scope 'file-with-archives)
  9131. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9132. (org-prepare-agenda-buffers scope)
  9133. (while (setq file (pop scope))
  9134. (with-current-buffer (org-find-base-buffer-visiting file)
  9135. (save-excursion
  9136. (save-restriction
  9137. (widen)
  9138. (goto-char (point-min))
  9139. (setq res (append res (org-scan-tags func matcher)))))))
  9140. res)))
  9141. ;;;; Properties
  9142. ;;; Setting and retrieving properties
  9143. (defconst org-special-properties
  9144. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9145. "TIMESTAMP" "TIMESTAMP_IA")
  9146. "The special properties valid in Org-mode.
  9147. These are properties that are not defined in the property drawer,
  9148. but in some other way.")
  9149. (defconst org-default-properties
  9150. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9151. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9152. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9153. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9154. "Some properties that are used by Org-mode for various purposes.
  9155. Being in this list makes sure that they are offered for completion.")
  9156. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9157. "Regular expression matching the first line of a property drawer.")
  9158. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9159. "Regular expression matching the first line of a property drawer.")
  9160. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9161. "Regular expression matching the first line of a property drawer.")
  9162. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9163. "Regular expression matching the first line of a property drawer.")
  9164. (defconst org-property-drawer-re
  9165. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9166. org-property-end-re "\\)\n?")
  9167. "Matches an entire property drawer.")
  9168. (defconst org-clock-drawer-re
  9169. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9170. org-property-end-re "\\)\n?")
  9171. "Matches an entire clock drawer.")
  9172. (defun org-property-action ()
  9173. "Do an action on properties."
  9174. (interactive)
  9175. (let (c)
  9176. (org-at-property-p)
  9177. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9178. (setq c (read-char-exclusive))
  9179. (cond
  9180. ((equal c ?s)
  9181. (call-interactively 'org-set-property))
  9182. ((equal c ?d)
  9183. (call-interactively 'org-delete-property))
  9184. ((equal c ?D)
  9185. (call-interactively 'org-delete-property-globally))
  9186. ((equal c ?c)
  9187. (call-interactively 'org-compute-property-at-point))
  9188. (t (error "No such property action %c" c)))))
  9189. (defun org-at-property-p ()
  9190. "Is the cursor in a property line?"
  9191. ;; FIXME: Does not check if we are actually in the drawer.
  9192. ;; FIXME: also returns true on any drawers.....
  9193. ;; This is used by C-c C-c for property action.
  9194. (save-excursion
  9195. (beginning-of-line 1)
  9196. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9197. (defun org-get-property-block (&optional beg end force)
  9198. "Return the (beg . end) range of the body of the property drawer.
  9199. BEG and END can be beginning and end of subtree, if not given
  9200. they will be found.
  9201. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9202. (catch 'exit
  9203. (save-excursion
  9204. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9205. (end (or end (progn (outline-next-heading) (point)))))
  9206. (goto-char beg)
  9207. (if (re-search-forward org-property-start-re end t)
  9208. (setq beg (1+ (match-end 0)))
  9209. (if force
  9210. (save-excursion
  9211. (org-insert-property-drawer)
  9212. (setq end (progn (outline-next-heading) (point))))
  9213. (throw 'exit nil))
  9214. (goto-char beg)
  9215. (if (re-search-forward org-property-start-re end t)
  9216. (setq beg (1+ (match-end 0)))))
  9217. (if (re-search-forward org-property-end-re end t)
  9218. (setq end (match-beginning 0))
  9219. (or force (throw 'exit nil))
  9220. (goto-char beg)
  9221. (setq end beg)
  9222. (org-indent-line-function)
  9223. (insert ":END:\n"))
  9224. (cons beg end)))))
  9225. (defun org-entry-properties (&optional pom which)
  9226. "Get all properties of the entry at point-or-marker POM.
  9227. This includes the TODO keyword, the tags, time strings for deadline,
  9228. scheduled, and clocking, and any additional properties defined in the
  9229. entry. The return value is an alist, keys may occur multiple times
  9230. if the property key was used several times.
  9231. POM may also be nil, in which case the current entry is used.
  9232. If WHICH is nil or `all', get all properties. If WHICH is
  9233. `special' or `standard', only get that subclass."
  9234. (setq which (or which 'all))
  9235. (org-with-point-at pom
  9236. (let ((clockstr (substring org-clock-string 0 -1))
  9237. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9238. beg end range props sum-props key value string clocksum)
  9239. (save-excursion
  9240. (when (condition-case nil (org-back-to-heading t) (error nil))
  9241. (setq beg (point))
  9242. (setq sum-props (get-text-property (point) 'org-summaries))
  9243. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9244. (outline-next-heading)
  9245. (setq end (point))
  9246. (when (memq which '(all special))
  9247. ;; Get the special properties, like TODO and tags
  9248. (goto-char beg)
  9249. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9250. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9251. (when (looking-at org-priority-regexp)
  9252. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9253. (when (and (setq value (org-get-tags-string))
  9254. (string-match "\\S-" value))
  9255. (push (cons "TAGS" value) props))
  9256. (when (setq value (org-get-tags-at))
  9257. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9258. props))
  9259. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9260. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9261. string (if (equal key clockstr)
  9262. (org-no-properties
  9263. (org-trim
  9264. (buffer-substring
  9265. (match-beginning 3) (goto-char (point-at-eol)))))
  9266. (substring (org-match-string-no-properties 3) 1 -1)))
  9267. (unless key
  9268. (if (= (char-after (match-beginning 3)) ?\[)
  9269. (setq key "TIMESTAMP_IA")
  9270. (setq key "TIMESTAMP")))
  9271. (when (or (equal key clockstr) (not (assoc key props)))
  9272. (push (cons key string) props)))
  9273. )
  9274. (when (memq which '(all standard))
  9275. ;; Get the standard properties, like :PORP: ...
  9276. (setq range (org-get-property-block beg end))
  9277. (when range
  9278. (goto-char (car range))
  9279. (while (re-search-forward
  9280. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9281. (cdr range) t)
  9282. (setq key (org-match-string-no-properties 1)
  9283. value (org-trim (or (org-match-string-no-properties 2) "")))
  9284. (unless (member key excluded)
  9285. (push (cons key (or value "")) props)))))
  9286. (if clocksum
  9287. (push (cons "CLOCKSUM"
  9288. (org-columns-number-to-string (/ (float clocksum) 60.)
  9289. 'add_times))
  9290. props))
  9291. (unless (assoc "CATEGORY" props)
  9292. (setq value (or (org-get-category)
  9293. (progn (org-refresh-category-properties)
  9294. (org-get-category))))
  9295. (push (cons "CATEGORY" value) props))
  9296. (append sum-props (nreverse props)))))))
  9297. (defun org-entry-get (pom property &optional inherit)
  9298. "Get value of PROPERTY for entry at point-or-marker POM.
  9299. If INHERIT is non-nil and the entry does not have the property,
  9300. then also check higher levels of the hierarchy.
  9301. If INHERIT is the symbol `selective', use inheritance only if the setting
  9302. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9303. If the property is present but empty, the return value is the empty string.
  9304. If the property is not present at all, nil is returned."
  9305. (org-with-point-at pom
  9306. (if (and inherit (if (eq inherit 'selective)
  9307. (org-property-inherit-p property)
  9308. t))
  9309. (org-entry-get-with-inheritance property)
  9310. (if (member property org-special-properties)
  9311. ;; We need a special property. Use brute force, get all properties.
  9312. (cdr (assoc property (org-entry-properties nil 'special)))
  9313. (let ((range (org-get-property-block)))
  9314. (if (and range
  9315. (goto-char (car range))
  9316. (re-search-forward
  9317. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9318. (cdr range) t))
  9319. ;; Found the property, return it.
  9320. (if (match-end 1)
  9321. (org-match-string-no-properties 1)
  9322. "")))))))
  9323. (defun org-property-or-variable-value (var &optional inherit)
  9324. "Check if there is a property fixing the value of VAR.
  9325. If yes, return this value. If not, return the current value of the variable."
  9326. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9327. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9328. (read prop)
  9329. (symbol-value var))))
  9330. (defun org-entry-delete (pom property)
  9331. "Delete the property PROPERTY from entry at point-or-marker POM."
  9332. (org-with-point-at pom
  9333. (if (member property org-special-properties)
  9334. nil ; cannot delete these properties.
  9335. (let ((range (org-get-property-block)))
  9336. (if (and range
  9337. (goto-char (car range))
  9338. (re-search-forward
  9339. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9340. (cdr range) t))
  9341. (progn
  9342. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9343. t)
  9344. nil)))))
  9345. ;; Multi-values properties are properties that contain multiple values
  9346. ;; These values are assumed to be single words, separated by whitespace.
  9347. (defun org-entry-add-to-multivalued-property (pom property value)
  9348. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9349. (let* ((old (org-entry-get pom property))
  9350. (values (and old (org-split-string old "[ \t]"))))
  9351. (setq value (org-entry-protect-space value))
  9352. (unless (member value values)
  9353. (setq values (cons value values))
  9354. (org-entry-put pom property
  9355. (mapconcat 'identity values " ")))))
  9356. (defun org-entry-remove-from-multivalued-property (pom property value)
  9357. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9358. (let* ((old (org-entry-get pom property))
  9359. (values (and old (org-split-string old "[ \t]"))))
  9360. (setq value (org-entry-protect-space value))
  9361. (when (member value values)
  9362. (setq values (delete value values))
  9363. (org-entry-put pom property
  9364. (mapconcat 'identity values " ")))))
  9365. (defun org-entry-member-in-multivalued-property (pom property value)
  9366. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9367. (let* ((old (org-entry-get pom property))
  9368. (values (and old (org-split-string old "[ \t]"))))
  9369. (setq value (org-entry-protect-space value))
  9370. (member value values)))
  9371. (defun org-entry-get-multivalued-property (pom property)
  9372. "Return a list of values in a multivalued property."
  9373. (let* ((value (org-entry-get pom property))
  9374. (values (and value (org-split-string value "[ \t]"))))
  9375. (mapcar 'org-entry-restore-space values)))
  9376. (defun org-entry-put-multivalued-property (pom property &rest values)
  9377. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9378. VALUES should be a list of strings. Spaces will be protected."
  9379. (org-entry-put pom property
  9380. (mapconcat 'org-entry-protect-space values " "))
  9381. (let* ((value (org-entry-get pom property))
  9382. (values (and value (org-split-string value "[ \t]"))))
  9383. (mapcar 'org-entry-restore-space values)))
  9384. (defun org-entry-protect-space (s)
  9385. "Protect spaces and newline in string S."
  9386. (while (string-match " " s)
  9387. (setq s (replace-match "%20" t t s)))
  9388. (while (string-match "\n" s)
  9389. (setq s (replace-match "%0A" t t s)))
  9390. s)
  9391. (defun org-entry-restore-space (s)
  9392. "Restore spaces and newline in string S."
  9393. (while (string-match "%20" s)
  9394. (setq s (replace-match " " t t s)))
  9395. (while (string-match "%0A" s)
  9396. (setq s (replace-match "\n" t t s)))
  9397. s)
  9398. (defvar org-entry-property-inherited-from (make-marker)
  9399. "Marker pointing to the entry from where a proerty was inherited.
  9400. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9401. location of the entry where the inheriance search matched. If there was
  9402. no match, the marker will point nowhere.
  9403. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9404. is set.")
  9405. (defun org-entry-get-with-inheritance (property)
  9406. "Get entry property, and search higher levels if not present."
  9407. (move-marker org-entry-property-inherited-from nil)
  9408. (let (tmp)
  9409. (save-excursion
  9410. (save-restriction
  9411. (widen)
  9412. (catch 'ex
  9413. (while t
  9414. (when (setq tmp (org-entry-get nil property))
  9415. (org-back-to-heading t)
  9416. (move-marker org-entry-property-inherited-from (point))
  9417. (throw 'ex tmp))
  9418. (or (org-up-heading-safe) (throw 'ex nil)))))
  9419. (or tmp
  9420. (cdr (assoc property org-file-properties))
  9421. (cdr (assoc property org-global-properties))
  9422. (cdr (assoc property org-global-properties-fixed))))))
  9423. (defun org-entry-put (pom property value)
  9424. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9425. (org-with-point-at pom
  9426. (org-back-to-heading t)
  9427. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9428. range)
  9429. (cond
  9430. ((equal property "TODO")
  9431. (when (and (stringp value) (string-match "\\S-" value)
  9432. (not (member value org-todo-keywords-1)))
  9433. (error "\"%s\" is not a valid TODO state" value))
  9434. (if (or (not value)
  9435. (not (string-match "\\S-" value)))
  9436. (setq value 'none))
  9437. (org-todo value)
  9438. (org-set-tags nil 'align))
  9439. ((equal property "PRIORITY")
  9440. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9441. (string-to-char value) ?\ ))
  9442. (org-set-tags nil 'align))
  9443. ((equal property "SCHEDULED")
  9444. (if (re-search-forward org-scheduled-time-regexp end t)
  9445. (cond
  9446. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9447. ((eq value 'later) (org-timestamp-change 1 'day))
  9448. (t (call-interactively 'org-schedule)))
  9449. (call-interactively 'org-schedule)))
  9450. ((equal property "DEADLINE")
  9451. (if (re-search-forward org-deadline-time-regexp end t)
  9452. (cond
  9453. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9454. ((eq value 'later) (org-timestamp-change 1 'day))
  9455. (t (call-interactively 'org-deadline)))
  9456. (call-interactively 'org-deadline)))
  9457. ((member property org-special-properties)
  9458. (error "The %s property can not yet be set with `org-entry-put'"
  9459. property))
  9460. (t ; a non-special property
  9461. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9462. (setq range (org-get-property-block beg end 'force))
  9463. (goto-char (car range))
  9464. (if (re-search-forward
  9465. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9466. (progn
  9467. (delete-region (match-beginning 1) (match-end 1))
  9468. (goto-char (match-beginning 1)))
  9469. (goto-char (cdr range))
  9470. (insert "\n")
  9471. (backward-char 1)
  9472. (org-indent-line-function)
  9473. (insert ":" property ":"))
  9474. (and value (insert " " value))
  9475. (org-indent-line-function)))))))
  9476. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9477. "Get all property keys in the current buffer.
  9478. With INCLUDE-SPECIALS, also list the special properties that relect things
  9479. like tags and TODO state.
  9480. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9481. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9482. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9483. formats in the current buffer."
  9484. (let (rtn range cfmt cols s p)
  9485. (save-excursion
  9486. (save-restriction
  9487. (widen)
  9488. (goto-char (point-min))
  9489. (while (re-search-forward org-property-start-re nil t)
  9490. (setq range (org-get-property-block))
  9491. (goto-char (car range))
  9492. (while (re-search-forward
  9493. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9494. (cdr range) t)
  9495. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9496. (outline-next-heading))))
  9497. (when include-specials
  9498. (setq rtn (append org-special-properties rtn)))
  9499. (when include-defaults
  9500. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9501. (when include-columns
  9502. (save-excursion
  9503. (save-restriction
  9504. (widen)
  9505. (goto-char (point-min))
  9506. (while (re-search-forward
  9507. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9508. nil t)
  9509. (setq cfmt (match-string 2) s 0)
  9510. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9511. cfmt s)
  9512. (setq s (match-end 0)
  9513. p (match-string 1 cfmt))
  9514. (unless (or (equal p "ITEM")
  9515. (member p org-special-properties))
  9516. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9517. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9518. (defun org-property-values (key)
  9519. "Return a list of all values of property KEY."
  9520. (save-excursion
  9521. (save-restriction
  9522. (widen)
  9523. (goto-char (point-min))
  9524. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9525. values)
  9526. (while (re-search-forward re nil t)
  9527. (add-to-list 'values (org-trim (match-string 1))))
  9528. (delete "" values)))))
  9529. (defun org-insert-property-drawer ()
  9530. "Insert a property drawer into the current entry."
  9531. (interactive)
  9532. (org-back-to-heading t)
  9533. (looking-at outline-regexp)
  9534. (let ((indent (- (match-end 0)(match-beginning 0)))
  9535. (beg (point))
  9536. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9537. end hiddenp)
  9538. (outline-next-heading)
  9539. (setq end (point))
  9540. (goto-char beg)
  9541. (while (re-search-forward re end t))
  9542. (setq hiddenp (org-invisible-p))
  9543. (end-of-line 1)
  9544. (and (equal (char-after) ?\n) (forward-char 1))
  9545. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9546. (beginning-of-line 2))
  9547. (org-skip-over-state-notes)
  9548. (skip-chars-backward " \t\n\r")
  9549. (if (eq (char-before) ?*) (forward-char 1))
  9550. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9551. (beginning-of-line 0)
  9552. (org-indent-to-column indent)
  9553. (beginning-of-line 2)
  9554. (org-indent-to-column indent)
  9555. (beginning-of-line 0)
  9556. (if hiddenp
  9557. (save-excursion
  9558. (org-back-to-heading t)
  9559. (hide-entry))
  9560. (org-flag-drawer t))))
  9561. (defun org-set-property (property value)
  9562. "In the current entry, set PROPERTY to VALUE.
  9563. When called interactively, this will prompt for a property name, offering
  9564. completion on existing and default properties. And then it will prompt
  9565. for a value, offering competion either on allowed values (via an inherited
  9566. xxx_ALL property) or on existing values in other instances of this property
  9567. in the current file."
  9568. (interactive
  9569. (let* ((completion-ignore-case t)
  9570. (keys (org-buffer-property-keys nil t t))
  9571. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9572. (prop (if (member prop0 keys)
  9573. prop0
  9574. (or (cdr (assoc (downcase prop0)
  9575. (mapcar (lambda (x) (cons (downcase x) x))
  9576. keys)))
  9577. prop0)))
  9578. (cur (org-entry-get nil prop))
  9579. (allowed (org-property-get-allowed-values nil prop 'table))
  9580. (existing (mapcar 'list (org-property-values prop)))
  9581. (val (if allowed
  9582. (org-completing-read "Value: " allowed nil 'req-match)
  9583. (org-completing-read
  9584. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9585. (concat "[" cur "]") "")
  9586. ": ")
  9587. existing nil nil "" nil cur))))
  9588. (list prop (if (equal val "") cur val))))
  9589. (unless (equal (org-entry-get nil property) value)
  9590. (org-entry-put nil property value)))
  9591. (defun org-delete-property (property)
  9592. "In the current entry, delete PROPERTY."
  9593. (interactive
  9594. (let* ((completion-ignore-case t)
  9595. (prop (org-ido-completing-read
  9596. "Property: " (org-entry-properties nil 'standard))))
  9597. (list prop)))
  9598. (message "Property %s %s" property
  9599. (if (org-entry-delete nil property)
  9600. "deleted"
  9601. "was not present in the entry")))
  9602. (defun org-delete-property-globally (property)
  9603. "Remove PROPERTY globally, from all entries."
  9604. (interactive
  9605. (let* ((completion-ignore-case t)
  9606. (prop (org-ido-completing-read
  9607. "Globally remove property: "
  9608. (mapcar 'list (org-buffer-property-keys)))))
  9609. (list prop)))
  9610. (save-excursion
  9611. (save-restriction
  9612. (widen)
  9613. (goto-char (point-min))
  9614. (let ((cnt 0))
  9615. (while (re-search-forward
  9616. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9617. nil t)
  9618. (setq cnt (1+ cnt))
  9619. (replace-match ""))
  9620. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9621. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9622. (defun org-compute-property-at-point ()
  9623. "Compute the property at point.
  9624. This looks for an enclosing column format, extracts the operator and
  9625. then applies it to the proerty in the column format's scope."
  9626. (interactive)
  9627. (unless (org-at-property-p)
  9628. (error "Not at a property"))
  9629. (let ((prop (org-match-string-no-properties 2)))
  9630. (org-columns-get-format-and-top-level)
  9631. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9632. (error "No operator defined for property %s" prop))
  9633. (org-columns-compute prop)))
  9634. (defun org-property-get-allowed-values (pom property &optional table)
  9635. "Get allowed values for the property PROPERTY.
  9636. When TABLE is non-nil, return an alist that can directly be used for
  9637. completion."
  9638. (let (vals)
  9639. (cond
  9640. ((equal property "TODO")
  9641. (setq vals (org-with-point-at pom
  9642. (append org-todo-keywords-1 '("")))))
  9643. ((equal property "PRIORITY")
  9644. (let ((n org-lowest-priority))
  9645. (while (>= n org-highest-priority)
  9646. (push (char-to-string n) vals)
  9647. (setq n (1- n)))))
  9648. ((member property org-special-properties))
  9649. (t
  9650. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9651. (when (and vals (string-match "\\S-" vals))
  9652. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9653. (setq vals (mapcar (lambda (x)
  9654. (cond ((stringp x) x)
  9655. ((numberp x) (number-to-string x))
  9656. ((symbolp x) (symbol-name x))
  9657. (t "???")))
  9658. vals)))))
  9659. (if table (mapcar 'list vals) vals)))
  9660. (defun org-property-previous-allowed-value (&optional previous)
  9661. "Switch to the next allowed value for this property."
  9662. (interactive)
  9663. (org-property-next-allowed-value t))
  9664. (defun org-property-next-allowed-value (&optional previous)
  9665. "Switch to the next allowed value for this property."
  9666. (interactive)
  9667. (unless (org-at-property-p)
  9668. (error "Not at a property"))
  9669. (let* ((key (match-string 2))
  9670. (value (match-string 3))
  9671. (allowed (or (org-property-get-allowed-values (point) key)
  9672. (and (member value '("[ ]" "[-]" "[X]"))
  9673. '("[ ]" "[X]"))))
  9674. nval)
  9675. (unless allowed
  9676. (error "Allowed values for this property have not been defined"))
  9677. (if previous (setq allowed (reverse allowed)))
  9678. (if (member value allowed)
  9679. (setq nval (car (cdr (member value allowed)))))
  9680. (setq nval (or nval (car allowed)))
  9681. (if (equal nval value)
  9682. (error "Only one allowed value for this property"))
  9683. (org-at-property-p)
  9684. (replace-match (concat " :" key ": " nval) t t)
  9685. (org-indent-line-function)
  9686. (beginning-of-line 1)
  9687. (skip-chars-forward " \t")))
  9688. (defun org-find-entry-with-id (ident)
  9689. "Locate the entry that contains the ID property with exact value IDENT.
  9690. IDENT can be a string, a symbol or a number, this function will search for
  9691. the string representation of it.
  9692. Return the position where this entry starts, or nil if there is no such entry."
  9693. (interactive "sID: ")
  9694. (let ((id (cond
  9695. ((stringp ident) ident)
  9696. ((symbol-name ident) (symbol-name ident))
  9697. ((numberp ident) (number-to-string ident))
  9698. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9699. (case-fold-search nil))
  9700. (save-excursion
  9701. (save-restriction
  9702. (widen)
  9703. (goto-char (point-min))
  9704. (when (re-search-forward
  9705. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9706. nil t)
  9707. (org-back-to-heading)
  9708. (point))))))
  9709. ;;;; Timestamps
  9710. (defvar org-last-changed-timestamp nil)
  9711. (defvar org-last-inserted-timestamp nil
  9712. "The last time stamp inserted with `org-insert-time-stamp'.")
  9713. (defvar org-time-was-given) ; dynamically scoped parameter
  9714. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9715. (defvar org-ts-what) ; dynamically scoped parameter
  9716. (defun org-time-stamp (arg &optional inactive)
  9717. "Prompt for a date/time and insert a time stamp.
  9718. If the user specifies a time like HH:MM, or if this command is called
  9719. with a prefix argument, the time stamp will contain date and time.
  9720. Otherwise, only the date will be included. All parts of a date not
  9721. specified by the user will be filled in from the current date/time.
  9722. So if you press just return without typing anything, the time stamp
  9723. will represent the current date/time. If there is already a timestamp
  9724. at the cursor, it will be modified."
  9725. (interactive "P")
  9726. (let* ((ts nil)
  9727. (default-time
  9728. ;; Default time is either today, or, when entering a range,
  9729. ;; the range start.
  9730. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9731. (save-excursion
  9732. (re-search-backward
  9733. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9734. (- (point) 20) t)))
  9735. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9736. (current-time)))
  9737. (default-input (and ts (org-get-compact-tod ts)))
  9738. org-time-was-given org-end-time-was-given time)
  9739. (cond
  9740. ((and (org-at-timestamp-p t)
  9741. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9742. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9743. (insert "--")
  9744. (setq time (let ((this-command this-command))
  9745. (org-read-date arg 'totime nil nil
  9746. default-time default-input)))
  9747. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9748. ((org-at-timestamp-p t)
  9749. (setq time (let ((this-command this-command))
  9750. (org-read-date arg 'totime nil nil default-time default-input)))
  9751. (when (org-at-timestamp-p t) ; just to get the match data
  9752. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9753. (replace-match "")
  9754. (setq org-last-changed-timestamp
  9755. (org-insert-time-stamp
  9756. time (or org-time-was-given arg)
  9757. inactive nil nil (list org-end-time-was-given))))
  9758. (message "Timestamp updated"))
  9759. (t
  9760. (setq time (let ((this-command this-command))
  9761. (org-read-date arg 'totime nil nil default-time default-input)))
  9762. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9763. nil nil (list org-end-time-was-given))))))
  9764. ;; FIXME: can we use this for something else, like computing time differences?
  9765. (defun org-get-compact-tod (s)
  9766. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9767. (let* ((t1 (match-string 1 s))
  9768. (h1 (string-to-number (match-string 2 s)))
  9769. (m1 (string-to-number (match-string 3 s)))
  9770. (t2 (and (match-end 4) (match-string 5 s)))
  9771. (h2 (and t2 (string-to-number (match-string 6 s))))
  9772. (m2 (and t2 (string-to-number (match-string 7 s))))
  9773. dh dm)
  9774. (if (not t2)
  9775. t1
  9776. (setq dh (- h2 h1) dm (- m2 m1))
  9777. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9778. (concat t1 "+" (number-to-string dh)
  9779. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9780. (defun org-time-stamp-inactive (&optional arg)
  9781. "Insert an inactive time stamp.
  9782. An inactive time stamp is enclosed in square brackets instead of angle
  9783. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9784. does not link to the calendar and cannot be changed with the S-cursor keys.
  9785. So these are more for recording a certain time/date."
  9786. (interactive "P")
  9787. (org-time-stamp arg 'inactive))
  9788. (defvar org-date-ovl (org-make-overlay 1 1))
  9789. (org-overlay-put org-date-ovl 'face 'org-warning)
  9790. (org-detach-overlay org-date-ovl)
  9791. (defvar org-ans1) ; dynamically scoped parameter
  9792. (defvar org-ans2) ; dynamically scoped parameter
  9793. (defvar org-plain-time-of-day-regexp) ; defined below
  9794. (defvar org-overriding-default-time nil) ; dynamically scoped
  9795. (defvar org-read-date-overlay nil)
  9796. (defvar org-dcst nil) ; dynamically scoped
  9797. (defun org-read-date (&optional with-time to-time from-string prompt
  9798. default-time default-input)
  9799. "Read a date, possibly a time, and make things smooth for the user.
  9800. The prompt will suggest to enter an ISO date, but you can also enter anything
  9801. which will at least partially be understood by `parse-time-string'.
  9802. Unrecognized parts of the date will default to the current day, month, year,
  9803. hour and minute. If this command is called to replace a timestamp at point,
  9804. of to enter the second timestamp of a range, the default time is taken from the
  9805. existing stamp. For example,
  9806. 3-2-5 --> 2003-02-05
  9807. feb 15 --> currentyear-02-15
  9808. sep 12 9 --> 2009-09-12
  9809. 12:45 --> today 12:45
  9810. 22 sept 0:34 --> currentyear-09-22 0:34
  9811. 12 --> currentyear-currentmonth-12
  9812. Fri --> nearest Friday (today or later)
  9813. etc.
  9814. Furthermore you can specify a relative date by giving, as the *first* thing
  9815. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9816. change in days weeks, months, years.
  9817. With a single plus or minus, the date is relative to today. With a double
  9818. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9819. +4d --> four days from today
  9820. +4 --> same as above
  9821. +2w --> two weeks from today
  9822. ++5 --> five days from default date
  9823. The function understands only English month and weekday abbreviations,
  9824. but this can be configured with the variables `parse-time-months' and
  9825. `parse-time-weekdays'.
  9826. While prompting, a calendar is popped up - you can also select the
  9827. date with the mouse (button 1). The calendar shows a period of three
  9828. months. To scroll it to other months, use the keys `>' and `<'.
  9829. If you don't like the calendar, turn it off with
  9830. \(setq org-read-date-popup-calendar nil)
  9831. With optional argument TO-TIME, the date will immediately be converted
  9832. to an internal time.
  9833. With an optional argument WITH-TIME, the prompt will suggest to also
  9834. insert a time. Note that when WITH-TIME is not set, you can still
  9835. enter a time, and this function will inform the calling routine about
  9836. this change. The calling routine may then choose to change the format
  9837. used to insert the time stamp into the buffer to include the time.
  9838. With optional argument FROM-STRING, read from this string instead from
  9839. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9840. the time/date that is used for everything that is not specified by the
  9841. user."
  9842. (require 'parse-time)
  9843. (let* ((org-time-stamp-rounding-minutes
  9844. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9845. (org-dcst org-display-custom-times)
  9846. (ct (org-current-time))
  9847. (def (or org-overriding-default-time default-time ct))
  9848. (defdecode (decode-time def))
  9849. (dummy (progn
  9850. (when (< (nth 2 defdecode) org-extend-today-until)
  9851. (setcar (nthcdr 2 defdecode) -1)
  9852. (setcar (nthcdr 1 defdecode) 59)
  9853. (setq def (apply 'encode-time defdecode)
  9854. defdecode (decode-time def)))))
  9855. (calendar-move-hook nil)
  9856. (calendar-view-diary-initially-flag nil)
  9857. (view-diary-entries-initially nil)
  9858. (calendar-view-holidays-initially-flag nil)
  9859. (view-calendar-holidays-initially nil)
  9860. (timestr (format-time-string
  9861. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9862. (prompt (concat (if prompt (concat prompt " ") "")
  9863. (format "Date+time [%s]: " timestr)))
  9864. ans (org-ans0 "") org-ans1 org-ans2 final)
  9865. (cond
  9866. (from-string (setq ans from-string))
  9867. (org-read-date-popup-calendar
  9868. (save-excursion
  9869. (save-window-excursion
  9870. (calendar)
  9871. (calendar-forward-day (- (time-to-days def)
  9872. (calendar-absolute-from-gregorian
  9873. (calendar-current-date))))
  9874. (org-eval-in-calendar nil t)
  9875. (let* ((old-map (current-local-map))
  9876. (map (copy-keymap calendar-mode-map))
  9877. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9878. (org-defkey map (kbd "RET") 'org-calendar-select)
  9879. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9880. 'org-calendar-select-mouse)
  9881. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9882. 'org-calendar-select-mouse)
  9883. (org-defkey minibuffer-local-map [(meta shift left)]
  9884. (lambda () (interactive)
  9885. (org-eval-in-calendar '(calendar-backward-month 1))))
  9886. (org-defkey minibuffer-local-map [(meta shift right)]
  9887. (lambda () (interactive)
  9888. (org-eval-in-calendar '(calendar-forward-month 1))))
  9889. (org-defkey minibuffer-local-map [(meta shift up)]
  9890. (lambda () (interactive)
  9891. (org-eval-in-calendar '(calendar-backward-year 1))))
  9892. (org-defkey minibuffer-local-map [(meta shift down)]
  9893. (lambda () (interactive)
  9894. (org-eval-in-calendar '(calendar-forward-year 1))))
  9895. (org-defkey minibuffer-local-map [(shift up)]
  9896. (lambda () (interactive)
  9897. (org-eval-in-calendar '(calendar-backward-week 1))))
  9898. (org-defkey minibuffer-local-map [(shift down)]
  9899. (lambda () (interactive)
  9900. (org-eval-in-calendar '(calendar-forward-week 1))))
  9901. (org-defkey minibuffer-local-map [(shift left)]
  9902. (lambda () (interactive)
  9903. (org-eval-in-calendar '(calendar-backward-day 1))))
  9904. (org-defkey minibuffer-local-map [(shift right)]
  9905. (lambda () (interactive)
  9906. (org-eval-in-calendar '(calendar-forward-day 1))))
  9907. (org-defkey minibuffer-local-map ">"
  9908. (lambda () (interactive)
  9909. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9910. (org-defkey minibuffer-local-map "<"
  9911. (lambda () (interactive)
  9912. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9913. (unwind-protect
  9914. (progn
  9915. (use-local-map map)
  9916. (add-hook 'post-command-hook 'org-read-date-display)
  9917. (setq org-ans0 (read-string prompt default-input nil nil))
  9918. ;; org-ans0: from prompt
  9919. ;; org-ans1: from mouse click
  9920. ;; org-ans2: from calendar motion
  9921. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9922. (remove-hook 'post-command-hook 'org-read-date-display)
  9923. (use-local-map old-map)
  9924. (when org-read-date-overlay
  9925. (org-delete-overlay org-read-date-overlay)
  9926. (setq org-read-date-overlay nil)))))))
  9927. (t ; Naked prompt only
  9928. (unwind-protect
  9929. (setq ans (read-string prompt default-input nil timestr))
  9930. (when org-read-date-overlay
  9931. (org-delete-overlay org-read-date-overlay)
  9932. (setq org-read-date-overlay nil)))))
  9933. (setq final (org-read-date-analyze ans def defdecode))
  9934. (if to-time
  9935. (apply 'encode-time final)
  9936. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9937. (format "%04d-%02d-%02d %02d:%02d"
  9938. (nth 5 final) (nth 4 final) (nth 3 final)
  9939. (nth 2 final) (nth 1 final))
  9940. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9941. (defvar def)
  9942. (defvar defdecode)
  9943. (defvar with-time)
  9944. (defun org-read-date-display ()
  9945. "Display the currrent date prompt interpretation in the minibuffer."
  9946. (when org-read-date-display-live
  9947. (when org-read-date-overlay
  9948. (org-delete-overlay org-read-date-overlay))
  9949. (let ((p (point)))
  9950. (end-of-line 1)
  9951. (while (not (equal (buffer-substring
  9952. (max (point-min) (- (point) 4)) (point))
  9953. " "))
  9954. (insert " "))
  9955. (goto-char p))
  9956. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9957. " " (or org-ans1 org-ans2)))
  9958. (org-end-time-was-given nil)
  9959. (f (org-read-date-analyze ans def defdecode))
  9960. (fmts (if org-dcst
  9961. org-time-stamp-custom-formats
  9962. org-time-stamp-formats))
  9963. (fmt (if (or with-time
  9964. (and (boundp 'org-time-was-given) org-time-was-given))
  9965. (cdr fmts)
  9966. (car fmts)))
  9967. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9968. (when (and org-end-time-was-given
  9969. (string-match org-plain-time-of-day-regexp txt))
  9970. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9971. org-end-time-was-given
  9972. (substring txt (match-end 0)))))
  9973. (setq org-read-date-overlay
  9974. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9975. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9976. (defun org-read-date-analyze (ans def defdecode)
  9977. "Analyze the combined answer of the date prompt."
  9978. ;; FIXME: cleanup and comment
  9979. (let (delta deltan deltaw deltadef year month day
  9980. hour minute second wday pm h2 m2 tl wday1
  9981. iso-year iso-weekday iso-week iso-year iso-date)
  9982. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9983. (setq ans "+0"))
  9984. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9985. (setq ans (replace-match "" t t ans)
  9986. deltan (car delta)
  9987. deltaw (nth 1 delta)
  9988. deltadef (nth 2 delta)))
  9989. ;; Check if there is an iso week date in there
  9990. ;; If yes, sore the info and ostpone interpreting it until the rest
  9991. ;; of the parsing is done
  9992. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9993. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9994. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9995. iso-week (string-to-number (match-string 2 ans)))
  9996. (setq ans (replace-match "" t t ans)))
  9997. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9998. (when (string-match
  9999. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10000. (setq year (if (match-end 2)
  10001. (string-to-number (match-string 2 ans))
  10002. (string-to-number (format-time-string "%Y")))
  10003. month (string-to-number (match-string 3 ans))
  10004. day (string-to-number (match-string 4 ans)))
  10005. (if (< year 100) (setq year (+ 2000 year)))
  10006. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10007. t nil ans)))
  10008. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10009. ;; If there is a time with am/pm, and *no* time without it, we convert
  10010. ;; so that matching will be successful.
  10011. (loop for i from 1 to 2 do ; twice, for end time as well
  10012. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10013. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10014. (setq hour (string-to-number (match-string 1 ans))
  10015. minute (if (match-end 3)
  10016. (string-to-number (match-string 3 ans))
  10017. 0)
  10018. pm (equal ?p
  10019. (string-to-char (downcase (match-string 4 ans)))))
  10020. (if (and (= hour 12) (not pm))
  10021. (setq hour 0)
  10022. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10023. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10024. t t ans))))
  10025. ;; Check if a time range is given as a duration
  10026. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10027. (setq hour (string-to-number (match-string 1 ans))
  10028. h2 (+ hour (string-to-number (match-string 3 ans)))
  10029. minute (string-to-number (match-string 2 ans))
  10030. m2 (+ minute (if (match-end 5) (string-to-number
  10031. (match-string 5 ans))0)))
  10032. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10033. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10034. t t ans)))
  10035. ;; Check if there is a time range
  10036. (when (boundp 'org-end-time-was-given)
  10037. (setq org-time-was-given nil)
  10038. (when (and (string-match org-plain-time-of-day-regexp ans)
  10039. (match-end 8))
  10040. (setq org-end-time-was-given (match-string 8 ans))
  10041. (setq ans (concat (substring ans 0 (match-beginning 7))
  10042. (substring ans (match-end 7))))))
  10043. (setq tl (parse-time-string ans)
  10044. day (or (nth 3 tl) (nth 3 defdecode))
  10045. month (or (nth 4 tl)
  10046. (if (and org-read-date-prefer-future
  10047. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10048. (1+ (nth 4 defdecode))
  10049. (nth 4 defdecode)))
  10050. year (or (nth 5 tl)
  10051. (if (and org-read-date-prefer-future
  10052. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10053. (1+ (nth 5 defdecode))
  10054. (nth 5 defdecode)))
  10055. hour (or (nth 2 tl) (nth 2 defdecode))
  10056. minute (or (nth 1 tl) (nth 1 defdecode))
  10057. second (or (nth 0 tl) 0)
  10058. wday (nth 6 tl))
  10059. ;; Special date definitions below
  10060. (cond
  10061. (iso-week
  10062. ;; There was an iso week
  10063. (setq year (or iso-year year)
  10064. day (or iso-weekday wday 1)
  10065. wday nil ; to make sure that the trigger below does not match
  10066. iso-date (calendar-gregorian-from-absolute
  10067. (calendar-absolute-from-iso
  10068. (list iso-week day year))))
  10069. ; FIXME: Should we also push ISO weeks into the future?
  10070. ; (when (and org-read-date-prefer-future
  10071. ; (not iso-year)
  10072. ; (< (calendar-absolute-from-gregorian iso-date)
  10073. ; (time-to-days (current-time))))
  10074. ; (setq year (1+ year)
  10075. ; iso-date (calendar-gregorian-from-absolute
  10076. ; (calendar-absolute-from-iso
  10077. ; (list iso-week day year)))))
  10078. (setq month (car iso-date)
  10079. year (nth 2 iso-date)
  10080. day (nth 1 iso-date)))
  10081. (deltan
  10082. (unless deltadef
  10083. (let ((now (decode-time (current-time))))
  10084. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10085. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10086. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10087. ((equal deltaw "m") (setq month (+ month deltan)))
  10088. ((equal deltaw "y") (setq year (+ year deltan)))))
  10089. ((and wday (not (nth 3 tl)))
  10090. ;; Weekday was given, but no day, so pick that day in the week
  10091. ;; on or after the derived date.
  10092. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10093. (unless (equal wday wday1)
  10094. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10095. (if (and (boundp 'org-time-was-given)
  10096. (nth 2 tl))
  10097. (setq org-time-was-given t))
  10098. (if (< year 100) (setq year (+ 2000 year)))
  10099. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10100. (list second minute hour day month year)))
  10101. (defvar parse-time-weekdays)
  10102. (defun org-read-date-get-relative (s today default)
  10103. "Check string S for special relative date string.
  10104. TODAY and DEFAULT are internal times, for today and for a default.
  10105. Return shift list (N what def-flag)
  10106. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10107. N is the number of WHATs to shift.
  10108. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10109. the DEFAULT date rather than TODAY."
  10110. (when (and
  10111. (string-match
  10112. (concat
  10113. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10114. "\\([0-9]+\\)?"
  10115. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10116. "\\([ \t]\\|$\\)") s)
  10117. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10118. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10119. (string-to-char (substring (match-string 1 s) -1))
  10120. ?+))
  10121. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10122. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10123. (what (if (match-end 3) (match-string 3 s) "d"))
  10124. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10125. (date (if rel default today))
  10126. (wday (nth 6 (decode-time date)))
  10127. delta)
  10128. (if wday1
  10129. (progn
  10130. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10131. (if (= dir ?-) (setq delta (- delta 7)))
  10132. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10133. (list delta "d" rel))
  10134. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10135. (defun org-eval-in-calendar (form &optional keepdate)
  10136. "Eval FORM in the calendar window and return to current window.
  10137. Also, store the cursor date in variable org-ans2."
  10138. (let ((sw (selected-window)))
  10139. (select-window (get-buffer-window "*Calendar*"))
  10140. (eval form)
  10141. (when (and (not keepdate) (calendar-cursor-to-date))
  10142. (let* ((date (calendar-cursor-to-date))
  10143. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10144. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10145. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10146. (select-window sw)))
  10147. (defun org-calendar-select ()
  10148. "Return to `org-read-date' with the date currently selected.
  10149. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10150. (interactive)
  10151. (when (calendar-cursor-to-date)
  10152. (let* ((date (calendar-cursor-to-date))
  10153. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10154. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10155. (if (active-minibuffer-window) (exit-minibuffer))))
  10156. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10157. "Insert a date stamp for the date given by the internal TIME.
  10158. WITH-HM means, use the stamp format that includes the time of the day.
  10159. INACTIVE means use square brackets instead of angular ones, so that the
  10160. stamp will not contribute to the agenda.
  10161. PRE and POST are optional strings to be inserted before and after the
  10162. stamp.
  10163. The command returns the inserted time stamp."
  10164. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10165. stamp)
  10166. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10167. (insert-before-markers (or pre ""))
  10168. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10169. (when (listp extra)
  10170. (setq extra (car extra))
  10171. (if (and (stringp extra)
  10172. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10173. (setq extra (format "-%02d:%02d"
  10174. (string-to-number (match-string 1 extra))
  10175. (string-to-number (match-string 2 extra))))
  10176. (setq extra nil)))
  10177. (when extra
  10178. (backward-char 1)
  10179. (insert-before-markers extra)
  10180. (forward-char 1))
  10181. (insert-before-markers (or post ""))
  10182. (setq org-last-inserted-timestamp stamp)))
  10183. (defun org-toggle-time-stamp-overlays ()
  10184. "Toggle the use of custom time stamp formats."
  10185. (interactive)
  10186. (setq org-display-custom-times (not org-display-custom-times))
  10187. (unless org-display-custom-times
  10188. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10189. (while (setq p (next-single-property-change p 'display))
  10190. (if (and (get-text-property p 'display)
  10191. (eq (get-text-property p 'face) 'org-date))
  10192. (remove-text-properties
  10193. p (setq p (next-single-property-change p 'display))
  10194. '(display t))))
  10195. (set-buffer-modified-p bmp)))
  10196. (if (featurep 'xemacs)
  10197. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10198. (org-restart-font-lock)
  10199. (setq org-table-may-need-update t)
  10200. (if org-display-custom-times
  10201. (message "Time stamps are overlayed with custom format")
  10202. (message "Time stamp overlays removed")))
  10203. (defun org-display-custom-time (beg end)
  10204. "Overlay modified time stamp format over timestamp between BEG and END."
  10205. (let* ((ts (buffer-substring beg end))
  10206. t1 w1 with-hm tf time str w2 (off 0))
  10207. (save-match-data
  10208. (setq t1 (org-parse-time-string ts t))
  10209. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10210. (setq off (- (match-end 0) (match-beginning 0)))))
  10211. (setq end (- end off))
  10212. (setq w1 (- end beg)
  10213. with-hm (and (nth 1 t1) (nth 2 t1))
  10214. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10215. time (org-fix-decoded-time t1)
  10216. str (org-add-props
  10217. (format-time-string
  10218. (substring tf 1 -1) (apply 'encode-time time))
  10219. nil 'mouse-face 'highlight)
  10220. w2 (length str))
  10221. (if (not (= w2 w1))
  10222. (add-text-properties (1+ beg) (+ 2 beg)
  10223. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10224. (if (featurep 'xemacs)
  10225. (progn
  10226. (put-text-property beg end 'invisible t)
  10227. (put-text-property beg end 'end-glyph (make-glyph str)))
  10228. (put-text-property beg end 'display str))))
  10229. (defun org-translate-time (string)
  10230. "Translate all timestamps in STRING to custom format.
  10231. But do this only if the variable `org-display-custom-times' is set."
  10232. (when org-display-custom-times
  10233. (save-match-data
  10234. (let* ((start 0)
  10235. (re org-ts-regexp-both)
  10236. t1 with-hm inactive tf time str beg end)
  10237. (while (setq start (string-match re string start))
  10238. (setq beg (match-beginning 0)
  10239. end (match-end 0)
  10240. t1 (save-match-data
  10241. (org-parse-time-string (substring string beg end) t))
  10242. with-hm (and (nth 1 t1) (nth 2 t1))
  10243. inactive (equal (substring string beg (1+ beg)) "[")
  10244. tf (funcall (if with-hm 'cdr 'car)
  10245. org-time-stamp-custom-formats)
  10246. time (org-fix-decoded-time t1)
  10247. str (format-time-string
  10248. (concat
  10249. (if inactive "[" "<") (substring tf 1 -1)
  10250. (if inactive "]" ">"))
  10251. (apply 'encode-time time))
  10252. string (replace-match str t t string)
  10253. start (+ start (length str)))))))
  10254. string)
  10255. (defun org-fix-decoded-time (time)
  10256. "Set 0 instead of nil for the first 6 elements of time.
  10257. Don't touch the rest."
  10258. (let ((n 0))
  10259. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10260. (defun org-days-to-time (timestamp-string)
  10261. "Difference between TIMESTAMP-STRING and now in days."
  10262. (- (time-to-days (org-time-string-to-time timestamp-string))
  10263. (time-to-days (current-time))))
  10264. (defun org-deadline-close (timestamp-string &optional ndays)
  10265. "Is the time in TIMESTAMP-STRING close to the current date?"
  10266. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10267. (and (< (org-days-to-time timestamp-string) ndays)
  10268. (not (org-entry-is-done-p))))
  10269. (defun org-get-wdays (ts)
  10270. "Get the deadline lead time appropriate for timestring TS."
  10271. (cond
  10272. ((<= org-deadline-warning-days 0)
  10273. ;; 0 or negative, enforce this value no matter what
  10274. (- org-deadline-warning-days))
  10275. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10276. ;; lead time is specified.
  10277. (floor (* (string-to-number (match-string 1 ts))
  10278. (cdr (assoc (match-string 2 ts)
  10279. '(("d" . 1) ("w" . 7)
  10280. ("m" . 30.4) ("y" . 365.25)))))))
  10281. ;; go for the default.
  10282. (t org-deadline-warning-days)))
  10283. (defun org-calendar-select-mouse (ev)
  10284. "Return to `org-read-date' with the date currently selected.
  10285. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10286. (interactive "e")
  10287. (mouse-set-point ev)
  10288. (when (calendar-cursor-to-date)
  10289. (let* ((date (calendar-cursor-to-date))
  10290. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10291. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10292. (if (active-minibuffer-window) (exit-minibuffer))))
  10293. (defun org-check-deadlines (ndays)
  10294. "Check if there are any deadlines due or past due.
  10295. A deadline is considered due if it happens within `org-deadline-warning-days'
  10296. days from today's date. If the deadline appears in an entry marked DONE,
  10297. it is not shown. The prefix arg NDAYS can be used to test that many
  10298. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10299. (interactive "P")
  10300. (let* ((org-warn-days
  10301. (cond
  10302. ((equal ndays '(4)) 100000)
  10303. (ndays (prefix-numeric-value ndays))
  10304. (t (abs org-deadline-warning-days))))
  10305. (case-fold-search nil)
  10306. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10307. (callback
  10308. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10309. (message "%d deadlines past-due or due within %d days"
  10310. (org-occur regexp nil callback)
  10311. org-warn-days)))
  10312. (defun org-check-before-date (date)
  10313. "Check if there are deadlines or scheduled entries before DATE."
  10314. (interactive (list (org-read-date)))
  10315. (let ((case-fold-search nil)
  10316. (regexp (concat "\\<\\(" org-deadline-string
  10317. "\\|" org-scheduled-string
  10318. "\\) *<\\([^>]+\\)>"))
  10319. (callback
  10320. (lambda () (time-less-p
  10321. (org-time-string-to-time (match-string 2))
  10322. (org-time-string-to-time date)))))
  10323. (message "%d entries before %s"
  10324. (org-occur regexp nil callback) date)))
  10325. (defun org-evaluate-time-range (&optional to-buffer)
  10326. "Evaluate a time range by computing the difference between start and end.
  10327. Normally the result is just printed in the echo area, but with prefix arg
  10328. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10329. If the time range is actually in a table, the result is inserted into the
  10330. next column.
  10331. For time difference computation, a year is assumed to be exactly 365
  10332. days in order to avoid rounding problems."
  10333. (interactive "P")
  10334. (or
  10335. (org-clock-update-time-maybe)
  10336. (save-excursion
  10337. (unless (org-at-date-range-p t)
  10338. (goto-char (point-at-bol))
  10339. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10340. (if (not (org-at-date-range-p t))
  10341. (error "Not at a time-stamp range, and none found in current line")))
  10342. (let* ((ts1 (match-string 1))
  10343. (ts2 (match-string 2))
  10344. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10345. (match-end (match-end 0))
  10346. (time1 (org-time-string-to-time ts1))
  10347. (time2 (org-time-string-to-time ts2))
  10348. (t1 (time-to-seconds time1))
  10349. (t2 (time-to-seconds time2))
  10350. (diff (abs (- t2 t1)))
  10351. (negative (< (- t2 t1) 0))
  10352. ;; (ys (floor (* 365 24 60 60)))
  10353. (ds (* 24 60 60))
  10354. (hs (* 60 60))
  10355. (fy "%dy %dd %02d:%02d")
  10356. (fy1 "%dy %dd")
  10357. (fd "%dd %02d:%02d")
  10358. (fd1 "%dd")
  10359. (fh "%02d:%02d")
  10360. y d h m align)
  10361. (if havetime
  10362. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10363. y 0
  10364. d (floor (/ diff ds)) diff (mod diff ds)
  10365. h (floor (/ diff hs)) diff (mod diff hs)
  10366. m (floor (/ diff 60)))
  10367. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10368. y 0
  10369. d (floor (+ (/ diff ds) 0.5))
  10370. h 0 m 0))
  10371. (if (not to-buffer)
  10372. (message "%s" (org-make-tdiff-string y d h m))
  10373. (if (org-at-table-p)
  10374. (progn
  10375. (goto-char match-end)
  10376. (setq align t)
  10377. (and (looking-at " *|") (goto-char (match-end 0))))
  10378. (goto-char match-end))
  10379. (if (looking-at
  10380. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10381. (replace-match ""))
  10382. (if negative (insert " -"))
  10383. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10384. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10385. (insert " " (format fh h m))))
  10386. (if align (org-table-align))
  10387. (message "Time difference inserted")))))
  10388. (defun org-make-tdiff-string (y d h m)
  10389. (let ((fmt "")
  10390. (l nil))
  10391. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10392. l (push y l)))
  10393. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10394. l (push d l)))
  10395. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10396. l (push h l)))
  10397. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10398. l (push m l)))
  10399. (apply 'format fmt (nreverse l))))
  10400. (defun org-time-string-to-time (s)
  10401. (apply 'encode-time (org-parse-time-string s)))
  10402. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10403. "Convert a time stamp to an absolute day number.
  10404. If there is a specifyer for a cyclic time stamp, get the closest date to
  10405. DAYNR.
  10406. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10407. (cond
  10408. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10409. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10410. daynr
  10411. (+ daynr 1000)))
  10412. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10413. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10414. (time-to-days (current-time))) (match-string 0 s)
  10415. prefer show-all))
  10416. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10417. (defun org-days-to-iso-week (days)
  10418. "Return the iso week number."
  10419. (require 'cal-iso)
  10420. (car (calendar-iso-from-absolute days)))
  10421. (defun org-small-year-to-year (year)
  10422. "Convert 2-digit years into 4-digit years.
  10423. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10424. The year 2000 cannot be abbreviated. Any year larger than 99
  10425. is returned unchanged."
  10426. (if (< year 38)
  10427. (setq year (+ 2000 year))
  10428. (if (< year 100)
  10429. (setq year (+ 1900 year))))
  10430. year)
  10431. (defun org-time-from-absolute (d)
  10432. "Return the time corresponding to date D.
  10433. D may be an absolute day number, or a calendar-type list (month day year)."
  10434. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10435. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10436. (defun org-calendar-holiday ()
  10437. "List of holidays, for Diary display in Org-mode."
  10438. (require 'holidays)
  10439. (let ((hl (funcall
  10440. (if (fboundp 'calendar-check-holidays)
  10441. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10442. (if hl (mapconcat 'identity hl "; "))))
  10443. (defun org-diary-sexp-entry (sexp entry date)
  10444. "Process a SEXP diary ENTRY for DATE."
  10445. (require 'diary-lib)
  10446. (let ((result (if calendar-debug-sexp
  10447. (let ((stack-trace-on-error t))
  10448. (eval (car (read-from-string sexp))))
  10449. (condition-case nil
  10450. (eval (car (read-from-string sexp)))
  10451. (error
  10452. (beep)
  10453. (message "Bad sexp at line %d in %s: %s"
  10454. (org-current-line)
  10455. (buffer-file-name) sexp)
  10456. (sleep-for 2))))))
  10457. (cond ((stringp result) result)
  10458. ((and (consp result)
  10459. (stringp (cdr result))) (cdr result))
  10460. (result entry)
  10461. (t nil))))
  10462. (defun org-diary-to-ical-string (frombuf)
  10463. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10464. This uses the icalendar.el library."
  10465. (let* ((tmpdir (if (featurep 'xemacs)
  10466. (temp-directory)
  10467. temporary-file-directory))
  10468. (tmpfile (make-temp-name
  10469. (expand-file-name "orgics" tmpdir)))
  10470. buf rtn b e)
  10471. (save-excursion
  10472. (set-buffer frombuf)
  10473. (icalendar-export-region (point-min) (point-max) tmpfile)
  10474. (setq buf (find-buffer-visiting tmpfile))
  10475. (set-buffer buf)
  10476. (goto-char (point-min))
  10477. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10478. (setq b (match-beginning 0)))
  10479. (goto-char (point-max))
  10480. (if (re-search-backward "^END:VEVENT" nil t)
  10481. (setq e (match-end 0)))
  10482. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10483. (kill-buffer buf)
  10484. (delete-file tmpfile)
  10485. rtn))
  10486. (defun org-closest-date (start current change prefer show-all)
  10487. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10488. When PREFER is `past' return a date that is either CURRENT or past.
  10489. When PREFER is `future', return a date that is either CURRENT or future.
  10490. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10491. ;; Make the proper lists from the dates
  10492. (catch 'exit
  10493. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10494. dn dw sday cday n1 n2
  10495. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10496. (setq start (org-date-to-gregorian start)
  10497. current (org-date-to-gregorian
  10498. (if show-all
  10499. current
  10500. (time-to-days (current-time))))
  10501. sday (calendar-absolute-from-gregorian start)
  10502. cday (calendar-absolute-from-gregorian current))
  10503. (if (<= cday sday) (throw 'exit sday))
  10504. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10505. (setq dn (string-to-number (match-string 1 change))
  10506. dw (cdr (assoc (match-string 2 change) a1)))
  10507. (error "Invalid change specifyer: %s" change))
  10508. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10509. (cond
  10510. ((eq dw 'day)
  10511. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10512. n2 (+ n1 dn)))
  10513. ((eq dw 'year)
  10514. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10515. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10516. (setq date1 (list m d y1)
  10517. n1 (calendar-absolute-from-gregorian date1)
  10518. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10519. n2 (calendar-absolute-from-gregorian date2)))
  10520. ((eq dw 'month)
  10521. ;; approx number of month between the two dates
  10522. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10523. ;; How often does dn fit in there?
  10524. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10525. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10526. m (+ m nm)
  10527. ny (floor (/ m 12))
  10528. y (+ y ny)
  10529. m (- m (* ny 12)))
  10530. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10531. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10532. (setq m2 (+ m dn) y2 y)
  10533. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10534. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10535. (while (<= n2 cday)
  10536. (setq n1 n2 m m2 y y2)
  10537. (setq m2 (+ m dn) y2 y)
  10538. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10539. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10540. (if show-all
  10541. (cond
  10542. ((eq prefer 'past) n1)
  10543. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10544. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10545. (cond
  10546. ((eq prefer 'past) n1)
  10547. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10548. (t (if (= cday n1) n1 n2)))))))
  10549. (defun org-date-to-gregorian (date)
  10550. "Turn any specification of DATE into a gregorian date for the calendar."
  10551. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10552. ((and (listp date) (= (length date) 3)) date)
  10553. ((stringp date)
  10554. (setq date (org-parse-time-string date))
  10555. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10556. ((listp date)
  10557. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10558. (defun org-parse-time-string (s &optional nodefault)
  10559. "Parse the standard Org-mode time string.
  10560. This should be a lot faster than the normal `parse-time-string'.
  10561. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10562. hour and minute fields will be nil if not given."
  10563. (if (string-match org-ts-regexp0 s)
  10564. (list 0
  10565. (if (or (match-beginning 8) (not nodefault))
  10566. (string-to-number (or (match-string 8 s) "0")))
  10567. (if (or (match-beginning 7) (not nodefault))
  10568. (string-to-number (or (match-string 7 s) "0")))
  10569. (string-to-number (match-string 4 s))
  10570. (string-to-number (match-string 3 s))
  10571. (string-to-number (match-string 2 s))
  10572. nil nil nil)
  10573. (make-list 9 0)))
  10574. (defun org-timestamp-up (&optional arg)
  10575. "Increase the date item at the cursor by one.
  10576. If the cursor is on the year, change the year. If it is on the month or
  10577. the day, change that.
  10578. With prefix ARG, change by that many units."
  10579. (interactive "p")
  10580. (org-timestamp-change (prefix-numeric-value arg)))
  10581. (defun org-timestamp-down (&optional arg)
  10582. "Decrease the date item at the cursor by one.
  10583. If the cursor is on the year, change the year. If it is on the month or
  10584. the day, change that.
  10585. With prefix ARG, change by that many units."
  10586. (interactive "p")
  10587. (org-timestamp-change (- (prefix-numeric-value arg))))
  10588. (defun org-timestamp-up-day (&optional arg)
  10589. "Increase the date in the time stamp by one day.
  10590. With prefix ARG, change that many days."
  10591. (interactive "p")
  10592. (if (and (not (org-at-timestamp-p t))
  10593. (org-on-heading-p))
  10594. (org-todo 'up)
  10595. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10596. (defun org-timestamp-down-day (&optional arg)
  10597. "Decrease the date in the time stamp by one day.
  10598. With prefix ARG, change that many days."
  10599. (interactive "p")
  10600. (if (and (not (org-at-timestamp-p t))
  10601. (org-on-heading-p))
  10602. (org-todo 'down)
  10603. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10604. (defun org-at-timestamp-p (&optional inactive-ok)
  10605. "Determine if the cursor is in or at a timestamp."
  10606. (interactive)
  10607. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10608. (pos (point))
  10609. (ans (or (looking-at tsr)
  10610. (save-excursion
  10611. (skip-chars-backward "^[<\n\r\t")
  10612. (if (> (point) (point-min)) (backward-char 1))
  10613. (and (looking-at tsr)
  10614. (> (- (match-end 0) pos) -1))))))
  10615. (and ans
  10616. (boundp 'org-ts-what)
  10617. (setq org-ts-what
  10618. (cond
  10619. ((= pos (match-beginning 0)) 'bracket)
  10620. ((= pos (1- (match-end 0))) 'bracket)
  10621. ((org-pos-in-match-range pos 2) 'year)
  10622. ((org-pos-in-match-range pos 3) 'month)
  10623. ((org-pos-in-match-range pos 7) 'hour)
  10624. ((org-pos-in-match-range pos 8) 'minute)
  10625. ((or (org-pos-in-match-range pos 4)
  10626. (org-pos-in-match-range pos 5)) 'day)
  10627. ((and (> pos (or (match-end 8) (match-end 5)))
  10628. (< pos (match-end 0)))
  10629. (- pos (or (match-end 8) (match-end 5))))
  10630. (t 'day))))
  10631. ans))
  10632. (defun org-toggle-timestamp-type ()
  10633. "Toggle the type (<active> or [inactive]) of a time stamp."
  10634. (interactive)
  10635. (when (org-at-timestamp-p t)
  10636. (let ((beg (match-beginning 0)) (end (match-end 0))
  10637. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10638. (save-excursion
  10639. (goto-char beg)
  10640. (while (re-search-forward "[][<>]" end t)
  10641. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10642. t t)))
  10643. (message "Timestamp is now %sactive"
  10644. (if (equal (char-after beg) ?<) "" "in")))))
  10645. (defun org-timestamp-change (n &optional what)
  10646. "Change the date in the time stamp at point.
  10647. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10648. `year', `minute', `second'. If WHAT is not given, the cursor position
  10649. in the timestamp determines what will be changed."
  10650. (let ((pos (point))
  10651. with-hm inactive
  10652. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10653. org-ts-what
  10654. extra rem
  10655. ts time time0)
  10656. (if (not (org-at-timestamp-p t))
  10657. (error "Not at a timestamp"))
  10658. (if (and (not what) (eq org-ts-what 'bracket))
  10659. (org-toggle-timestamp-type)
  10660. (if (and (not what) (not (eq org-ts-what 'day))
  10661. org-display-custom-times
  10662. (get-text-property (point) 'display)
  10663. (not (get-text-property (1- (point)) 'display)))
  10664. (setq org-ts-what 'day))
  10665. (setq org-ts-what (or what org-ts-what)
  10666. inactive (= (char-after (match-beginning 0)) ?\[)
  10667. ts (match-string 0))
  10668. (replace-match "")
  10669. (if (string-match
  10670. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10671. ts)
  10672. (setq extra (match-string 1 ts)))
  10673. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10674. (setq with-hm t))
  10675. (setq time0 (org-parse-time-string ts))
  10676. (when (and (eq org-ts-what 'minute)
  10677. (eq current-prefix-arg nil))
  10678. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10679. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10680. (setcar (cdr time0) (+ (nth 1 time0)
  10681. (if (> n 0) (- rem) (- dm rem))))))
  10682. (setq time
  10683. (encode-time (or (car time0) 0)
  10684. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10685. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10686. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10687. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10688. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10689. (nthcdr 6 time0)))
  10690. (when (integerp org-ts-what)
  10691. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10692. (if (eq what 'calendar)
  10693. (let ((cal-date (org-get-date-from-calendar)))
  10694. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10695. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10696. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10697. (setcar time0 (or (car time0) 0))
  10698. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10699. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10700. (setq time (apply 'encode-time time0))))
  10701. (setq org-last-changed-timestamp
  10702. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10703. (org-clock-update-time-maybe)
  10704. (goto-char pos)
  10705. ;; Try to recenter the calendar window, if any
  10706. (if (and org-calendar-follow-timestamp-change
  10707. (get-buffer-window "*Calendar*" t)
  10708. (memq org-ts-what '(day month year)))
  10709. (org-recenter-calendar (time-to-days time))))))
  10710. (defun org-modify-ts-extra (s pos n dm)
  10711. "Change the different parts of the lead-time and repeat fields in timestamp."
  10712. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10713. ng h m new rem)
  10714. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10715. (cond
  10716. ((or (org-pos-in-match-range pos 2)
  10717. (org-pos-in-match-range pos 3))
  10718. (setq m (string-to-number (match-string 3 s))
  10719. h (string-to-number (match-string 2 s)))
  10720. (if (org-pos-in-match-range pos 2)
  10721. (setq h (+ h n))
  10722. (setq n (* dm (org-no-warnings (signum n))))
  10723. (when (not (= 0 (setq rem (% m dm))))
  10724. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10725. (setq m (+ m n)))
  10726. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10727. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10728. (setq h (min 24 (max 0 h)))
  10729. (setq ng 1 new (format "-%02d:%02d" h m)))
  10730. ((org-pos-in-match-range pos 6)
  10731. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10732. ((org-pos-in-match-range pos 5)
  10733. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10734. ((org-pos-in-match-range pos 9)
  10735. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10736. ((org-pos-in-match-range pos 8)
  10737. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10738. (when ng
  10739. (setq s (concat
  10740. (substring s 0 (match-beginning ng))
  10741. new
  10742. (substring s (match-end ng))))))
  10743. s))
  10744. (defun org-recenter-calendar (date)
  10745. "If the calendar is visible, recenter it to DATE."
  10746. (let* ((win (selected-window))
  10747. (cwin (get-buffer-window "*Calendar*" t))
  10748. (calendar-move-hook nil))
  10749. (when cwin
  10750. (select-window cwin)
  10751. (calendar-goto-date (if (listp date) date
  10752. (calendar-gregorian-from-absolute date)))
  10753. (select-window win))))
  10754. (defun org-goto-calendar (&optional arg)
  10755. "Go to the Emacs calendar at the current date.
  10756. If there is a time stamp in the current line, go to that date.
  10757. A prefix ARG can be used to force the current date."
  10758. (interactive "P")
  10759. (let ((tsr org-ts-regexp) diff
  10760. (calendar-move-hook nil)
  10761. (calendar-view-holidays-initially-flag nil)
  10762. (view-calendar-holidays-initially nil)
  10763. (calendar-view-diary-initially-flag nil)
  10764. (view-diary-entries-initially nil))
  10765. (if (or (org-at-timestamp-p)
  10766. (save-excursion
  10767. (beginning-of-line 1)
  10768. (looking-at (concat ".*" tsr))))
  10769. (let ((d1 (time-to-days (current-time)))
  10770. (d2 (time-to-days
  10771. (org-time-string-to-time (match-string 1)))))
  10772. (setq diff (- d2 d1))))
  10773. (calendar)
  10774. (calendar-goto-today)
  10775. (if (and diff (not arg)) (calendar-forward-day diff))))
  10776. (defun org-get-date-from-calendar ()
  10777. "Return a list (month day year) of date at point in calendar."
  10778. (with-current-buffer "*Calendar*"
  10779. (save-match-data
  10780. (calendar-cursor-to-date))))
  10781. (defun org-date-from-calendar ()
  10782. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10783. If there is already a time stamp at the cursor position, update it."
  10784. (interactive)
  10785. (if (org-at-timestamp-p t)
  10786. (org-timestamp-change 0 'calendar)
  10787. (let ((cal-date (org-get-date-from-calendar)))
  10788. (org-insert-time-stamp
  10789. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10790. (defun org-minutes-to-hh:mm-string (m)
  10791. "Compute H:MM from a number of minutes."
  10792. (let ((h (/ m 60)))
  10793. (setq m (- m (* 60 h)))
  10794. (format org-time-clocksum-format h m)))
  10795. (defun org-hh:mm-string-to-minutes (s)
  10796. "Convert a string H:MM to a number of minutes."
  10797. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10798. (+ (* (string-to-number (match-string 1 s)) 60)
  10799. (string-to-number (match-string 2 s)))
  10800. 0))
  10801. ;;;; Agenda files
  10802. ;;;###autoload
  10803. (defun org-iswitchb (&optional arg)
  10804. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10805. With a prefix argument, restrict available to files.
  10806. With two prefix arguments, restrict available buffers to agenda files.
  10807. Due to some yet unresolved reason, the global function
  10808. `iswitchb-mode' needs to be active for this function to work."
  10809. (interactive "P")
  10810. (require 'iswitchb)
  10811. (let ((enabled iswitchb-mode) blist)
  10812. (or enabled (iswitchb-mode 1))
  10813. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10814. ((equal arg '(16)) (org-buffer-list 'agenda))
  10815. (t (org-buffer-list))))
  10816. (unwind-protect
  10817. (let ((iswitchb-make-buflist-hook
  10818. (lambda ()
  10819. (setq iswitchb-temp-buflist
  10820. (mapcar 'buffer-name blist)))))
  10821. (switch-to-buffer
  10822. (iswitchb-read-buffer
  10823. "Switch-to: " nil t))
  10824. (or enabled (iswitchb-mode -1))))))
  10825. (defun org-buffer-list (&optional predicate exclude-tmp)
  10826. "Return a list of Org buffers.
  10827. PREDICATE can be `export', `files' or `agenda'.
  10828. export restrict the list to Export buffers.
  10829. files restrict the list to buffers visiting Org files.
  10830. agenda restrict the list to buffers visiting agenda files.
  10831. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10832. (let* ((bfn nil)
  10833. (agenda-files (and (eq predicate 'agenda)
  10834. (mapcar 'file-truename (org-agenda-files t))))
  10835. (filter
  10836. (cond
  10837. ((eq predicate 'files)
  10838. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10839. ((eq predicate 'export)
  10840. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10841. ((eq predicate 'agenda)
  10842. (lambda (b)
  10843. (with-current-buffer b
  10844. (and (eq major-mode 'org-mode)
  10845. (setq bfn (buffer-file-name b))
  10846. (member (file-truename bfn) agenda-files)))))
  10847. (t (lambda (b) (with-current-buffer b
  10848. (or (eq major-mode 'org-mode)
  10849. (string-match "\*Org .*Export"
  10850. (buffer-name b)))))))))
  10851. (delq nil
  10852. (mapcar
  10853. (lambda(b)
  10854. (if (and (funcall filter b)
  10855. (or (not exclude-tmp)
  10856. (not (string-match "tmp" (buffer-name b)))))
  10857. b
  10858. nil))
  10859. (buffer-list)))))
  10860. (defun org-agenda-files (&optional unrestricted archives)
  10861. "Get the list of agenda files.
  10862. Optional UNRESTRICTED means return the full list even if a restriction
  10863. is currently in place.
  10864. When ARCHIVES is t, include all archive files hat are really being
  10865. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10866. `org-agenda-archives-mode' is t."
  10867. (let ((files
  10868. (cond
  10869. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10870. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10871. ((listp org-agenda-files) org-agenda-files)
  10872. (t (error "Invalid value of `org-agenda-files'")))))
  10873. (setq files (apply 'append
  10874. (mapcar (lambda (f)
  10875. (if (file-directory-p f)
  10876. (directory-files
  10877. f t org-agenda-file-regexp)
  10878. (list f)))
  10879. files)))
  10880. (when org-agenda-skip-unavailable-files
  10881. (setq files (delq nil
  10882. (mapcar (function
  10883. (lambda (file)
  10884. (and (file-readable-p file) file)))
  10885. files))))
  10886. (when (or (eq archives t)
  10887. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10888. (setq files (org-add-archive-files files)))
  10889. files))
  10890. (defun org-edit-agenda-file-list ()
  10891. "Edit the list of agenda files.
  10892. Depending on setup, this either uses customize to edit the variable
  10893. `org-agenda-files', or it visits the file that is holding the list. In the
  10894. latter case, the buffer is set up in a way that saving it automatically kills
  10895. the buffer and restores the previous window configuration."
  10896. (interactive)
  10897. (if (stringp org-agenda-files)
  10898. (let ((cw (current-window-configuration)))
  10899. (find-file org-agenda-files)
  10900. (org-set-local 'org-window-configuration cw)
  10901. (org-add-hook 'after-save-hook
  10902. (lambda ()
  10903. (set-window-configuration
  10904. (prog1 org-window-configuration
  10905. (kill-buffer (current-buffer))))
  10906. (org-install-agenda-files-menu)
  10907. (message "New agenda file list installed"))
  10908. nil 'local)
  10909. (message "%s" (substitute-command-keys
  10910. "Edit list and finish with \\[save-buffer]")))
  10911. (customize-variable 'org-agenda-files)))
  10912. (defun org-store-new-agenda-file-list (list)
  10913. "Set new value for the agenda file list and save it correcly."
  10914. (if (stringp org-agenda-files)
  10915. (let ((f org-agenda-files) b)
  10916. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10917. (with-temp-file f
  10918. (insert (mapconcat 'identity list "\n") "\n")))
  10919. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10920. (setq org-agenda-files list)
  10921. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10922. (defun org-read-agenda-file-list ()
  10923. "Read the list of agenda files from a file."
  10924. (when (file-directory-p org-agenda-files)
  10925. (error "`org-agenda-files' cannot be a single directory"))
  10926. (when (stringp org-agenda-files)
  10927. (with-temp-buffer
  10928. (insert-file-contents org-agenda-files)
  10929. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10930. ;;;###autoload
  10931. (defun org-cycle-agenda-files ()
  10932. "Cycle through the files in `org-agenda-files'.
  10933. If the current buffer visits an agenda file, find the next one in the list.
  10934. If the current buffer does not, find the first agenda file."
  10935. (interactive)
  10936. (let* ((fs (org-agenda-files t))
  10937. (files (append fs (list (car fs))))
  10938. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10939. file)
  10940. (unless files (error "No agenda files"))
  10941. (catch 'exit
  10942. (while (setq file (pop files))
  10943. (if (equal (file-truename file) tcf)
  10944. (when (car files)
  10945. (find-file (car files))
  10946. (throw 'exit t))))
  10947. (find-file (car fs)))
  10948. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10949. (defun org-agenda-file-to-front (&optional to-end)
  10950. "Move/add the current file to the top of the agenda file list.
  10951. If the file is not present in the list, it is added to the front. If it is
  10952. present, it is moved there. With optional argument TO-END, add/move to the
  10953. end of the list."
  10954. (interactive "P")
  10955. (let ((org-agenda-skip-unavailable-files nil)
  10956. (file-alist (mapcar (lambda (x)
  10957. (cons (file-truename x) x))
  10958. (org-agenda-files t)))
  10959. (ctf (file-truename buffer-file-name))
  10960. x had)
  10961. (setq x (assoc ctf file-alist) had x)
  10962. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10963. (if to-end
  10964. (setq file-alist (append (delq x file-alist) (list x)))
  10965. (setq file-alist (cons x (delq x file-alist))))
  10966. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10967. (org-install-agenda-files-menu)
  10968. (message "File %s to %s of agenda file list"
  10969. (if had "moved" "added") (if to-end "end" "front"))))
  10970. (defun org-remove-file (&optional file)
  10971. "Remove current file from the list of files in variable `org-agenda-files'.
  10972. These are the files which are being checked for agenda entries.
  10973. Optional argument FILE means, use this file instead of the current."
  10974. (interactive)
  10975. (let* ((org-agenda-skip-unavailable-files nil)
  10976. (file (or file buffer-file-name))
  10977. (true-file (file-truename file))
  10978. (afile (abbreviate-file-name file))
  10979. (files (delq nil (mapcar
  10980. (lambda (x)
  10981. (if (equal true-file
  10982. (file-truename x))
  10983. nil x))
  10984. (org-agenda-files t)))))
  10985. (if (not (= (length files) (length (org-agenda-files t))))
  10986. (progn
  10987. (org-store-new-agenda-file-list files)
  10988. (org-install-agenda-files-menu)
  10989. (message "Removed file: %s" afile))
  10990. (message "File was not in list: %s (not removed)" afile))))
  10991. (defun org-file-menu-entry (file)
  10992. (vector file (list 'find-file file) t))
  10993. (defun org-check-agenda-file (file)
  10994. "Make sure FILE exists. If not, ask user what to do."
  10995. (when (not (file-exists-p file))
  10996. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10997. (abbreviate-file-name file))
  10998. (let ((r (downcase (read-char-exclusive))))
  10999. (cond
  11000. ((equal r ?r)
  11001. (org-remove-file file)
  11002. (throw 'nextfile t))
  11003. (t (error "Abort"))))))
  11004. (defun org-get-agenda-file-buffer (file)
  11005. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11006. it to the list of buffers which might be released later."
  11007. (let ((buf (org-find-base-buffer-visiting file)))
  11008. (if buf
  11009. buf ; just return it
  11010. ;; Make a new buffer and remember it
  11011. (setq buf (find-file-noselect file))
  11012. (if buf (push buf org-agenda-new-buffers))
  11013. buf)))
  11014. (defun org-release-buffers (blist)
  11015. "Release all buffers in list, asking the user for confirmation when needed.
  11016. When a buffer is unmodified, it is just killed. When modified, it is saved
  11017. \(if the user agrees) and then killed."
  11018. (let (buf file)
  11019. (while (setq buf (pop blist))
  11020. (setq file (buffer-file-name buf))
  11021. (when (and (buffer-modified-p buf)
  11022. file
  11023. (y-or-n-p (format "Save file %s? " file)))
  11024. (with-current-buffer buf (save-buffer)))
  11025. (kill-buffer buf))))
  11026. (defun org-prepare-agenda-buffers (files)
  11027. "Create buffers for all agenda files, protect archived trees and comments."
  11028. (interactive)
  11029. (let ((pa '(:org-archived t))
  11030. (pc '(:org-comment t))
  11031. (pall '(:org-archived t :org-comment t))
  11032. (inhibit-read-only t)
  11033. (rea (concat ":" org-archive-tag ":"))
  11034. bmp file re)
  11035. (save-excursion
  11036. (save-restriction
  11037. (while (setq file (pop files))
  11038. (if (bufferp file)
  11039. (set-buffer file)
  11040. (org-check-agenda-file file)
  11041. (set-buffer (org-get-agenda-file-buffer file)))
  11042. (widen)
  11043. (setq bmp (buffer-modified-p))
  11044. (org-refresh-category-properties)
  11045. (setq org-todo-keywords-for-agenda
  11046. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11047. (setq org-done-keywords-for-agenda
  11048. (append org-done-keywords-for-agenda org-done-keywords))
  11049. (setq org-todo-keyword-alist-for-agenda
  11050. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  11051. (setq org-tag-alist-for-agenda
  11052. (append org-tag-alist-for-agenda org-tag-alist))
  11053. (save-excursion
  11054. (remove-text-properties (point-min) (point-max) pall)
  11055. (when org-agenda-skip-archived-trees
  11056. (goto-char (point-min))
  11057. (while (re-search-forward rea nil t)
  11058. (if (org-on-heading-p t)
  11059. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11060. (goto-char (point-min))
  11061. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11062. (while (re-search-forward re nil t)
  11063. (add-text-properties
  11064. (match-beginning 0) (org-end-of-subtree t) pc)))
  11065. (set-buffer-modified-p bmp))))
  11066. (setq org-todo-keyword-alist-for-agenda
  11067. (org-uniquify org-todo-keyword-alist-for-agenda)
  11068. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  11069. ;;;; Embedded LaTeX
  11070. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11071. "Keymap for the minor `org-cdlatex-mode'.")
  11072. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11073. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11074. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11075. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11076. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11077. (defvar org-cdlatex-texmathp-advice-is-done nil
  11078. "Flag remembering if we have applied the advice to texmathp already.")
  11079. (define-minor-mode org-cdlatex-mode
  11080. "Toggle the minor `org-cdlatex-mode'.
  11081. This mode supports entering LaTeX environment and math in LaTeX fragments
  11082. in Org-mode.
  11083. \\{org-cdlatex-mode-map}"
  11084. nil " OCDL" nil
  11085. (when org-cdlatex-mode (require 'cdlatex))
  11086. (unless org-cdlatex-texmathp-advice-is-done
  11087. (setq org-cdlatex-texmathp-advice-is-done t)
  11088. (defadvice texmathp (around org-math-always-on activate)
  11089. "Always return t in org-mode buffers.
  11090. This is because we want to insert math symbols without dollars even outside
  11091. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11092. en embedded LaTeX fragement, let texmathp do its job.
  11093. \\[org-cdlatex-mode-map]"
  11094. (interactive)
  11095. (let (p)
  11096. (cond
  11097. ((not (org-mode-p)) ad-do-it)
  11098. ((eq this-command 'cdlatex-math-symbol)
  11099. (setq ad-return-value t
  11100. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11101. (t
  11102. (let ((p (org-inside-LaTeX-fragment-p)))
  11103. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11104. (setq ad-return-value t
  11105. texmathp-why '("Org-mode embedded math" . 0))
  11106. (if p ad-do-it)))))))))
  11107. (defun turn-on-org-cdlatex ()
  11108. "Unconditionally turn on `org-cdlatex-mode'."
  11109. (org-cdlatex-mode 1))
  11110. (defun org-inside-LaTeX-fragment-p ()
  11111. "Test if point is inside a LaTeX fragment.
  11112. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11113. sequence appearing also before point.
  11114. Even though the matchers for math are configurable, this function assumes
  11115. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11116. delimiters are skipped when they have been removed by customization.
  11117. The return value is nil, or a cons cell with the delimiter and
  11118. and the position of this delimiter.
  11119. This function does a reasonably good job, but can locally be fooled by
  11120. for example currency specifications. For example it will assume being in
  11121. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11122. fragments that are properly closed, but during editing, we have to live
  11123. with the uncertainty caused by missing closing delimiters. This function
  11124. looks only before point, not after."
  11125. (catch 'exit
  11126. (let ((pos (point))
  11127. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11128. (lim (progn
  11129. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11130. (point)))
  11131. dd-on str (start 0) m re)
  11132. (goto-char pos)
  11133. (when dodollar
  11134. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11135. re (nth 1 (assoc "$" org-latex-regexps)))
  11136. (while (string-match re str start)
  11137. (cond
  11138. ((= (match-end 0) (length str))
  11139. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11140. ((= (match-end 0) (- (length str) 5))
  11141. (throw 'exit nil))
  11142. (t (setq start (match-end 0))))))
  11143. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11144. (goto-char pos)
  11145. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11146. (and (match-beginning 2) (throw 'exit nil))
  11147. ;; count $$
  11148. (while (re-search-backward "\\$\\$" lim t)
  11149. (setq dd-on (not dd-on)))
  11150. (goto-char pos)
  11151. (if dd-on (cons "$$" m))))))
  11152. (defun org-try-cdlatex-tab ()
  11153. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11154. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11155. - inside a LaTeX fragment, or
  11156. - after the first word in a line, where an abbreviation expansion could
  11157. insert a LaTeX environment."
  11158. (when org-cdlatex-mode
  11159. (cond
  11160. ((save-excursion
  11161. (skip-chars-backward "a-zA-Z0-9*")
  11162. (skip-chars-backward " \t")
  11163. (bolp))
  11164. (cdlatex-tab) t)
  11165. ((org-inside-LaTeX-fragment-p)
  11166. (cdlatex-tab) t)
  11167. (t nil))))
  11168. (defun org-cdlatex-underscore-caret (&optional arg)
  11169. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11170. Revert to the normal definition outside of these fragments."
  11171. (interactive "P")
  11172. (if (org-inside-LaTeX-fragment-p)
  11173. (call-interactively 'cdlatex-sub-superscript)
  11174. (let (org-cdlatex-mode)
  11175. (call-interactively (key-binding (vector last-input-event))))))
  11176. (defun org-cdlatex-math-modify (&optional arg)
  11177. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11178. Revert to the normal definition outside of these fragments."
  11179. (interactive "P")
  11180. (if (org-inside-LaTeX-fragment-p)
  11181. (call-interactively 'cdlatex-math-modify)
  11182. (let (org-cdlatex-mode)
  11183. (call-interactively (key-binding (vector last-input-event))))))
  11184. (defvar org-latex-fragment-image-overlays nil
  11185. "List of overlays carrying the images of latex fragments.")
  11186. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11187. (defun org-remove-latex-fragment-image-overlays ()
  11188. "Remove all overlays with LaTeX fragment images in current buffer."
  11189. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11190. (setq org-latex-fragment-image-overlays nil))
  11191. (defun org-preview-latex-fragment (&optional subtree)
  11192. "Preview the LaTeX fragment at point, or all locally or globally.
  11193. If the cursor is in a LaTeX fragment, create the image and overlay
  11194. it over the source code. If there is no fragment at point, display
  11195. all fragments in the current text, from one headline to the next. With
  11196. prefix SUBTREE, display all fragments in the current subtree. With a
  11197. double prefix `C-u C-u', or when the cursor is before the first headline,
  11198. display all fragments in the buffer.
  11199. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11200. (interactive "P")
  11201. (org-remove-latex-fragment-image-overlays)
  11202. (save-excursion
  11203. (save-restriction
  11204. (let (beg end at msg)
  11205. (cond
  11206. ((or (equal subtree '(16))
  11207. (not (save-excursion
  11208. (re-search-backward (concat "^" outline-regexp) nil t))))
  11209. (setq beg (point-min) end (point-max)
  11210. msg "Creating images for buffer...%s"))
  11211. ((equal subtree '(4))
  11212. (org-back-to-heading)
  11213. (setq beg (point) end (org-end-of-subtree t)
  11214. msg "Creating images for subtree...%s"))
  11215. (t
  11216. (if (setq at (org-inside-LaTeX-fragment-p))
  11217. (goto-char (max (point-min) (- (cdr at) 2)))
  11218. (org-back-to-heading))
  11219. (setq beg (point) end (progn (outline-next-heading) (point))
  11220. msg (if at "Creating image...%s"
  11221. "Creating images for entry...%s"))))
  11222. (message msg "")
  11223. (narrow-to-region beg end)
  11224. (goto-char beg)
  11225. (org-format-latex
  11226. (concat "ltxpng/" (file-name-sans-extension
  11227. (file-name-nondirectory
  11228. buffer-file-name)))
  11229. default-directory 'overlays msg at 'forbuffer)
  11230. (message msg "done. Use `C-c C-c' to remove images.")))))
  11231. (defvar org-latex-regexps
  11232. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11233. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11234. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11235. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11236. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11237. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11238. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11239. "Regular expressions for matching embedded LaTeX.")
  11240. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11241. "Replace LaTeX fragments with links to an image, and produce images."
  11242. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11243. (let* ((prefixnodir (file-name-nondirectory prefix))
  11244. (absprefix (expand-file-name prefix dir))
  11245. (todir (file-name-directory absprefix))
  11246. (opt org-format-latex-options)
  11247. (matchers (plist-get opt :matchers))
  11248. (re-list org-latex-regexps)
  11249. (cnt 0) txt link beg end re e checkdir
  11250. m n block linkfile movefile ov)
  11251. ;; Check if there are old images files with this prefix, and remove them
  11252. (when (file-directory-p todir)
  11253. (mapc 'delete-file
  11254. (directory-files
  11255. todir 'full
  11256. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11257. ;; Check the different regular expressions
  11258. (while (setq e (pop re-list))
  11259. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11260. block (if (nth 3 e) "\n\n" ""))
  11261. (when (member m matchers)
  11262. (goto-char (point-min))
  11263. (while (re-search-forward re nil t)
  11264. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11265. (setq txt (match-string n)
  11266. beg (match-beginning n) end (match-end n)
  11267. cnt (1+ cnt)
  11268. linkfile (format "%s_%04d.png" prefix cnt)
  11269. movefile (format "%s_%04d.png" absprefix cnt)
  11270. link (concat block "[[file:" linkfile "]]" block))
  11271. (if msg (message msg cnt))
  11272. (goto-char beg)
  11273. (unless checkdir ; make sure the directory exists
  11274. (setq checkdir t)
  11275. (or (file-directory-p todir) (make-directory todir)))
  11276. (org-create-formula-image
  11277. txt movefile opt forbuffer)
  11278. (if overlays
  11279. (progn
  11280. (setq ov (org-make-overlay beg end))
  11281. (if (featurep 'xemacs)
  11282. (progn
  11283. (org-overlay-put ov 'invisible t)
  11284. (org-overlay-put
  11285. ov 'end-glyph
  11286. (make-glyph (vector 'png :file movefile))))
  11287. (org-overlay-put
  11288. ov 'display
  11289. (list 'image :type 'png :file movefile :ascent 'center)))
  11290. (push ov org-latex-fragment-image-overlays)
  11291. (goto-char end))
  11292. (delete-region beg end)
  11293. (insert link))))))))
  11294. ;; This function borrows from Ganesh Swami's latex2png.el
  11295. (defun org-create-formula-image (string tofile options buffer)
  11296. (let* ((tmpdir (if (featurep 'xemacs)
  11297. (temp-directory)
  11298. temporary-file-directory))
  11299. (texfilebase (make-temp-name
  11300. (expand-file-name "orgtex" tmpdir)))
  11301. (texfile (concat texfilebase ".tex"))
  11302. (dvifile (concat texfilebase ".dvi"))
  11303. (pngfile (concat texfilebase ".png"))
  11304. (fnh (if (featurep 'xemacs)
  11305. (font-height (get-face-font 'default))
  11306. (face-attribute 'default :height nil)))
  11307. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11308. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11309. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11310. "Black"))
  11311. (bg (or (plist-get options (if buffer :background :html-background))
  11312. "Transparent")))
  11313. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11314. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11315. (with-temp-file texfile
  11316. (insert org-format-latex-header
  11317. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11318. (let ((dir default-directory))
  11319. (condition-case nil
  11320. (progn
  11321. (cd tmpdir)
  11322. (call-process "latex" nil nil nil texfile))
  11323. (error nil))
  11324. (cd dir))
  11325. (if (not (file-exists-p dvifile))
  11326. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11327. (condition-case nil
  11328. (call-process "dvipng" nil nil nil
  11329. "-E" "-fg" fg "-bg" bg
  11330. "-D" dpi
  11331. ;;"-x" scale "-y" scale
  11332. "-T" "tight"
  11333. "-o" pngfile
  11334. dvifile)
  11335. (error nil))
  11336. (if (not (file-exists-p pngfile))
  11337. (progn (message "Failed to create png file from %s" texfile) nil)
  11338. ;; Use the requested file name and clean up
  11339. (copy-file pngfile tofile 'replace)
  11340. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11341. (delete-file (concat texfilebase e)))
  11342. pngfile))))
  11343. (defun org-dvipng-color (attr)
  11344. "Return an rgb color specification for dvipng."
  11345. (apply 'format "rgb %s %s %s"
  11346. (mapcar 'org-normalize-color
  11347. (color-values (face-attribute 'default attr nil)))))
  11348. (defun org-normalize-color (value)
  11349. "Return string to be used as color value for an RGB component."
  11350. (format "%g" (/ value 65535.0)))
  11351. ;;;; Key bindings
  11352. ;; Make `C-c C-x' a prefix key
  11353. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11354. ;; TAB key with modifiers
  11355. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11356. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11357. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11358. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11359. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11360. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11361. ;; The following line is necessary under Suse GNU/Linux
  11362. (unless (featurep 'xemacs)
  11363. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11364. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11365. (define-key org-mode-map [backtab] 'org-shifttab)
  11366. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11367. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11368. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11369. ;; Cursor keys with modifiers
  11370. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11371. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11372. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11373. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11374. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11375. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11376. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11377. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11378. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11379. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11380. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11381. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11382. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11383. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11384. ;;; Extra keys for tty access.
  11385. ;; We only set them when really needed because otherwise the
  11386. ;; menus don't show the simple keys
  11387. (when (or org-use-extra-keys
  11388. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11389. (not window-system))
  11390. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11391. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11392. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11393. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11394. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11395. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11396. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11397. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11398. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11399. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11400. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11401. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11402. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11403. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11404. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11405. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11406. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11407. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11408. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11409. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11410. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11411. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11412. ;; All the other keys
  11413. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11414. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11415. (if (boundp 'narrow-map)
  11416. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11417. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11418. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11419. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11420. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11421. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11422. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11423. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11424. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11425. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11426. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11427. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11428. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11429. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11430. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11431. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11432. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11433. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11434. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11435. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11436. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11437. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11438. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11439. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11440. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11441. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11442. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11443. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11444. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11445. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11446. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11447. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11448. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11449. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11450. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11451. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11452. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11453. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11454. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11455. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11456. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11457. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11458. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11459. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11460. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11461. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11462. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11463. (org-defkey org-mode-map "\C-m" 'org-return)
  11464. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11465. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11466. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11467. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11468. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11469. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11470. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11471. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11472. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11473. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11474. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11475. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11476. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11477. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11478. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11479. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11480. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11481. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11482. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11483. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11484. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11485. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11486. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11487. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11488. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11489. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11490. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11491. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11492. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11493. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11494. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11495. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11496. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11497. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11498. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11499. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11500. (when (featurep 'xemacs)
  11501. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11502. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11503. (defun org-self-insert-command (N)
  11504. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11505. If the cursor is in a table looking at whitespace, the whitespace is
  11506. overwritten, and the table is not marked as requiring realignment."
  11507. (interactive "p")
  11508. (if (and (org-table-p)
  11509. (progn
  11510. ;; check if we blank the field, and if that triggers align
  11511. (and (featurep 'org-table) org-table-auto-blank-field
  11512. (member last-command
  11513. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11514. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11515. ;; got extra space, this field does not determine column width
  11516. (let (org-table-may-need-update) (org-table-blank-field))
  11517. ;; no extra space, this field may determine column width
  11518. (org-table-blank-field)))
  11519. t)
  11520. (eq N 1)
  11521. (looking-at "[^|\n]* |"))
  11522. (let (org-table-may-need-update)
  11523. (goto-char (1- (match-end 0)))
  11524. (delete-backward-char 1)
  11525. (goto-char (match-beginning 0))
  11526. (self-insert-command N))
  11527. (setq org-table-may-need-update t)
  11528. (self-insert-command N)
  11529. (org-fix-tags-on-the-fly)))
  11530. (defun org-fix-tags-on-the-fly ()
  11531. (when (and (equal (char-after (point-at-bol)) ?*)
  11532. (org-on-heading-p))
  11533. (org-align-tags-here org-tags-column)))
  11534. (defun org-delete-backward-char (N)
  11535. "Like `delete-backward-char', insert whitespace at field end in tables.
  11536. When deleting backwards, in tables this function will insert whitespace in
  11537. front of the next \"|\" separator, to keep the table aligned. The table will
  11538. still be marked for re-alignment if the field did fill the entire column,
  11539. because, in this case the deletion might narrow the column."
  11540. (interactive "p")
  11541. (if (and (org-table-p)
  11542. (eq N 1)
  11543. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11544. (looking-at ".*?|"))
  11545. (let ((pos (point))
  11546. (noalign (looking-at "[^|\n\r]* |"))
  11547. (c org-table-may-need-update))
  11548. (backward-delete-char N)
  11549. (skip-chars-forward "^|")
  11550. (insert " ")
  11551. (goto-char (1- pos))
  11552. ;; noalign: if there were two spaces at the end, this field
  11553. ;; does not determine the width of the column.
  11554. (if noalign (setq org-table-may-need-update c)))
  11555. (backward-delete-char N)
  11556. (org-fix-tags-on-the-fly)))
  11557. (defun org-delete-char (N)
  11558. "Like `delete-char', but insert whitespace at field end in tables.
  11559. When deleting characters, in tables this function will insert whitespace in
  11560. front of the next \"|\" separator, to keep the table aligned. The table will
  11561. still be marked for re-alignment if the field did fill the entire column,
  11562. because, in this case the deletion might narrow the column."
  11563. (interactive "p")
  11564. (if (and (org-table-p)
  11565. (not (bolp))
  11566. (not (= (char-after) ?|))
  11567. (eq N 1))
  11568. (if (looking-at ".*?|")
  11569. (let ((pos (point))
  11570. (noalign (looking-at "[^|\n\r]* |"))
  11571. (c org-table-may-need-update))
  11572. (replace-match (concat
  11573. (substring (match-string 0) 1 -1)
  11574. " |"))
  11575. (goto-char pos)
  11576. ;; noalign: if there were two spaces at the end, this field
  11577. ;; does not determine the width of the column.
  11578. (if noalign (setq org-table-may-need-update c)))
  11579. (delete-char N))
  11580. (delete-char N)
  11581. (org-fix-tags-on-the-fly)))
  11582. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11583. (put 'org-self-insert-command 'delete-selection t)
  11584. (put 'orgtbl-self-insert-command 'delete-selection t)
  11585. (put 'org-delete-char 'delete-selection 'supersede)
  11586. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11587. ;; Make `flyspell-mode' delay after some commands
  11588. (put 'org-self-insert-command 'flyspell-delayed t)
  11589. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11590. (put 'org-delete-char 'flyspell-delayed t)
  11591. (put 'org-delete-backward-char 'flyspell-delayed t)
  11592. ;; Make pabbrev-mode expand after org-mode commands
  11593. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11594. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11595. ;; How to do this: Measure non-white length of current string
  11596. ;; If equal to column width, we should realign.
  11597. (defun org-remap (map &rest commands)
  11598. "In MAP, remap the functions given in COMMANDS.
  11599. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11600. (let (new old)
  11601. (while commands
  11602. (setq old (pop commands) new (pop commands))
  11603. (if (fboundp 'command-remapping)
  11604. (org-defkey map (vector 'remap old) new)
  11605. (substitute-key-definition old new map global-map)))))
  11606. (when (eq org-enable-table-editor 'optimized)
  11607. ;; If the user wants maximum table support, we need to hijack
  11608. ;; some standard editing functions
  11609. (org-remap org-mode-map
  11610. 'self-insert-command 'org-self-insert-command
  11611. 'delete-char 'org-delete-char
  11612. 'delete-backward-char 'org-delete-backward-char)
  11613. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11614. (defun org-shiftcursor-error ()
  11615. "Throw an error because Shift-Cursor command was applied in wrong context."
  11616. (error "This command is active in special context like tables, headlines or timestamps"))
  11617. (defun org-shifttab (&optional arg)
  11618. "Global visibility cycling or move to previous table field.
  11619. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11620. on context.
  11621. See the individual commands for more information."
  11622. (interactive "P")
  11623. (cond
  11624. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11625. ((integerp arg)
  11626. (message "Content view to level: %d" arg)
  11627. (org-content (prefix-numeric-value arg))
  11628. (setq org-cycle-global-status 'overview))
  11629. (t (call-interactively 'org-global-cycle))))
  11630. (defun org-shiftmetaleft ()
  11631. "Promote subtree or delete table column.
  11632. Calls `org-promote-subtree', `org-outdent-item',
  11633. or `org-table-delete-column', depending on context.
  11634. See the individual commands for more information."
  11635. (interactive)
  11636. (cond
  11637. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11638. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11639. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11640. (t (org-shiftcursor-error))))
  11641. (defun org-shiftmetaright ()
  11642. "Demote subtree or insert table column.
  11643. Calls `org-demote-subtree', `org-indent-item',
  11644. or `org-table-insert-column', depending on context.
  11645. See the individual commands for more information."
  11646. (interactive)
  11647. (cond
  11648. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11649. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11650. ((org-at-item-p) (call-interactively 'org-indent-item))
  11651. (t (org-shiftcursor-error))))
  11652. (defun org-shiftmetaup (&optional arg)
  11653. "Move subtree up or kill table row.
  11654. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11655. `org-move-item-up' depending on context. See the individual commands
  11656. for more information."
  11657. (interactive "P")
  11658. (cond
  11659. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11660. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11661. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11662. (t (org-shiftcursor-error))))
  11663. (defun org-shiftmetadown (&optional arg)
  11664. "Move subtree down or insert table row.
  11665. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11666. `org-move-item-down', depending on context. See the individual
  11667. commands for more information."
  11668. (interactive "P")
  11669. (cond
  11670. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11671. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11672. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11673. (t (org-shiftcursor-error))))
  11674. (defun org-metaleft (&optional arg)
  11675. "Promote heading or move table column to left.
  11676. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11677. With no specific context, calls the Emacs default `backward-word'.
  11678. See the individual commands for more information."
  11679. (interactive "P")
  11680. (cond
  11681. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11682. ((or (org-on-heading-p) (org-region-active-p))
  11683. (call-interactively 'org-do-promote))
  11684. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11685. (t (call-interactively 'backward-word))))
  11686. (defun org-metaright (&optional arg)
  11687. "Demote subtree or move table column to right.
  11688. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11689. With no specific context, calls the Emacs default `forward-word'.
  11690. See the individual commands for more information."
  11691. (interactive "P")
  11692. (cond
  11693. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11694. ((or (org-on-heading-p) (org-region-active-p))
  11695. (call-interactively 'org-do-demote))
  11696. ((org-at-item-p) (call-interactively 'org-indent-item))
  11697. (t (call-interactively 'forward-word))))
  11698. (defun org-metaup (&optional arg)
  11699. "Move subtree up or move table row up.
  11700. Calls `org-move-subtree-up' or `org-table-move-row' or
  11701. `org-move-item-up', depending on context. See the individual commands
  11702. for more information."
  11703. (interactive "P")
  11704. (cond
  11705. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11706. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11707. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11708. (t (transpose-lines 1) (beginning-of-line -1))))
  11709. (defun org-metadown (&optional arg)
  11710. "Move subtree down or move table row down.
  11711. Calls `org-move-subtree-down' or `org-table-move-row' or
  11712. `org-move-item-down', depending on context. See the individual
  11713. commands for more information."
  11714. (interactive "P")
  11715. (cond
  11716. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11717. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11718. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11719. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11720. (defun org-shiftup (&optional arg)
  11721. "Increase item in timestamp or increase priority of current headline.
  11722. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11723. depending on context. See the individual commands for more information."
  11724. (interactive "P")
  11725. (cond
  11726. ((org-at-timestamp-p t)
  11727. (call-interactively (if org-edit-timestamp-down-means-later
  11728. 'org-timestamp-down 'org-timestamp-up)))
  11729. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11730. ((org-at-item-p) (call-interactively 'org-previous-item))
  11731. ((org-clocktable-try-shift 'up arg))
  11732. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11733. (defun org-shiftdown (&optional arg)
  11734. "Decrease item in timestamp or decrease priority of current headline.
  11735. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11736. depending on context. See the individual commands for more information."
  11737. (interactive "P")
  11738. (cond
  11739. ((org-at-timestamp-p t)
  11740. (call-interactively (if org-edit-timestamp-down-means-later
  11741. 'org-timestamp-up 'org-timestamp-down)))
  11742. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11743. ((org-clocktable-try-shift 'down arg))
  11744. (t (call-interactively 'org-next-item))))
  11745. (defun org-shiftright (&optional arg)
  11746. "Cycle the thing at point or in the current line, depending on context.
  11747. Depending on context, this does one of the following:
  11748. - switch a timestamp at point one day into the future
  11749. - on a headline, switch to the next TODO keyword.
  11750. - on an item, switch entire list to the next bullet type
  11751. - on a property line, switch to the next allowed value
  11752. - on a clocktable definition line, move time block into the future"
  11753. (interactive "P")
  11754. (cond
  11755. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11756. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11757. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11758. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11759. ((org-clocktable-try-shift 'right arg))
  11760. (t (org-shiftcursor-error))))
  11761. (defun org-shiftleft (&optional arg)
  11762. "Cycle the thing at point or in the current line, depending on context.
  11763. Depending on context, this does one of the following:
  11764. - switch a timestamp at point one day into the past
  11765. - on a headline, switch to the previous TODO keyword.
  11766. - on an item, switch entire list to the previous bullet type
  11767. - on a property line, switch to the previous allowed value
  11768. - on a clocktable definition line, move time block into the past"
  11769. (interactive "P")
  11770. (cond
  11771. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11772. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11773. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11774. ((org-at-property-p)
  11775. (call-interactively 'org-property-previous-allowed-value))
  11776. ((org-clocktable-try-shift 'left arg))
  11777. (t (org-shiftcursor-error))))
  11778. (defun org-shiftcontrolright ()
  11779. "Switch to next TODO set."
  11780. (interactive)
  11781. (cond
  11782. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11783. (t (org-shiftcursor-error))))
  11784. (defun org-shiftcontrolleft ()
  11785. "Switch to previous TODO set."
  11786. (interactive)
  11787. (cond
  11788. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11789. (t (org-shiftcursor-error))))
  11790. (defun org-ctrl-c-ret ()
  11791. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11792. (interactive)
  11793. (cond
  11794. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11795. (t (call-interactively 'org-insert-heading))))
  11796. (defun org-copy-special ()
  11797. "Copy region in table or copy current subtree.
  11798. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11799. See the individual commands for more information."
  11800. (interactive)
  11801. (call-interactively
  11802. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11803. (defun org-cut-special ()
  11804. "Cut region in table or cut current subtree.
  11805. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11806. See the individual commands for more information."
  11807. (interactive)
  11808. (call-interactively
  11809. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11810. (defun org-paste-special (arg)
  11811. "Paste rectangular region into table, or past subtree relative to level.
  11812. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11813. See the individual commands for more information."
  11814. (interactive "P")
  11815. (if (org-at-table-p)
  11816. (org-table-paste-rectangle)
  11817. (org-paste-subtree arg)))
  11818. (defun org-edit-special ()
  11819. "Call a special editor for the stuff at point.
  11820. When at a table, call the formula editor with `org-table-edit-formulas'.
  11821. When at the first line of an src example, call `org-edit-src-code'.
  11822. When in an #+include line, visit the include file. Otherwise call
  11823. `ffap' to visit the file at point."
  11824. (interactive)
  11825. (cond
  11826. ((org-at-table-p)
  11827. (call-interactively 'org-table-edit-formulas))
  11828. ((save-excursion
  11829. (beginning-of-line 1)
  11830. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11831. (find-file (org-trim (match-string 1))))
  11832. ((org-edit-src-code))
  11833. ((org-edit-fixed-width-region))
  11834. (t (call-interactively 'ffap))))
  11835. (defun org-ctrl-c-ctrl-c (&optional arg)
  11836. "Set tags in headline, or update according to changed information at point.
  11837. This command does many different things, depending on context:
  11838. - If the cursor is in a headline, prompt for tags and insert them
  11839. into the current line, aligned to `org-tags-column'. When called
  11840. with prefix arg, realign all tags in the current buffer.
  11841. - If the cursor is in one of the special #+KEYWORD lines, this
  11842. triggers scanning the buffer for these lines and updating the
  11843. information.
  11844. - If the cursor is inside a table, realign the table. This command
  11845. works even if the automatic table editor has been turned off.
  11846. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11847. the entire table.
  11848. - If the cursor is a the beginning of a dynamic block, update it.
  11849. - If the cursor is inside a table created by the table.el package,
  11850. activate that table.
  11851. - If the current buffer is a remember buffer, close note and file
  11852. it. A prefix argument of 1 files to the default location
  11853. without further interaction. A prefix argument of 2 files to
  11854. the currently clocking task.
  11855. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11856. links in this buffer.
  11857. - If the cursor is on a numbered item in a plain list, renumber the
  11858. ordered list.
  11859. - If the cursor is on a checkbox, toggle it."
  11860. (interactive "P")
  11861. (let ((org-enable-table-editor t))
  11862. (cond
  11863. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11864. org-occur-highlights
  11865. org-latex-fragment-image-overlays)
  11866. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11867. (org-remove-occur-highlights)
  11868. (org-remove-latex-fragment-image-overlays)
  11869. (message "Temporary highlights/overlays removed from current buffer"))
  11870. ((and (local-variable-p 'org-finish-function (current-buffer))
  11871. (fboundp org-finish-function))
  11872. (funcall org-finish-function))
  11873. ((org-at-property-p)
  11874. (call-interactively 'org-property-action))
  11875. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11876. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11877. ((org-at-table.el-p)
  11878. (require 'table)
  11879. (beginning-of-line 1)
  11880. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11881. (call-interactively 'table-recognize-table))
  11882. ((org-at-table-p)
  11883. (org-table-maybe-eval-formula)
  11884. (if arg
  11885. (call-interactively 'org-table-recalculate)
  11886. (org-table-maybe-recalculate-line))
  11887. (call-interactively 'org-table-align))
  11888. ((org-at-item-checkbox-p)
  11889. (call-interactively 'org-toggle-checkbox))
  11890. ((org-at-item-p)
  11891. (call-interactively 'org-maybe-renumber-ordered-list))
  11892. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11893. ;; Dynamic block
  11894. (beginning-of-line 1)
  11895. (save-excursion (org-update-dblock)))
  11896. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11897. (cond
  11898. ((equal (match-string 1) "TBLFM")
  11899. ;; Recalculate the table before this line
  11900. (save-excursion
  11901. (beginning-of-line 1)
  11902. (skip-chars-backward " \r\n\t")
  11903. (if (org-at-table-p)
  11904. (org-call-with-arg 'org-table-recalculate t))))
  11905. (t
  11906. ; (org-set-regexps-and-options)
  11907. ; (org-restart-font-lock)
  11908. (let ((org-inhibit-startup t)) (org-mode-restart))
  11909. (message "Local setup has been refreshed"))))
  11910. (t (error "C-c C-c can do nothing useful at this location.")))))
  11911. (defun org-mode-restart ()
  11912. "Restart Org-mode, to scan again for special lines.
  11913. Also updates the keyword regular expressions."
  11914. (interactive)
  11915. (org-mode)
  11916. (message "Org-mode restarted"))
  11917. (defun org-kill-note-or-show-branches ()
  11918. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11919. (interactive)
  11920. (if (not org-finish-function)
  11921. (call-interactively 'show-branches)
  11922. (let ((org-note-abort t))
  11923. (funcall org-finish-function))))
  11924. (defun org-return (&optional indent)
  11925. "Goto next table row or insert a newline.
  11926. Calls `org-table-next-row' or `newline', depending on context.
  11927. See the individual commands for more information."
  11928. (interactive)
  11929. (cond
  11930. ((bobp) (if indent (newline-and-indent) (newline)))
  11931. ((and (org-at-heading-p)
  11932. (looking-at
  11933. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11934. (org-show-entry)
  11935. (end-of-line 1)
  11936. (newline))
  11937. ((org-at-table-p)
  11938. (org-table-justify-field-maybe)
  11939. (call-interactively 'org-table-next-row))
  11940. (t (if indent (newline-and-indent) (newline)))))
  11941. (defun org-return-indent ()
  11942. "Goto next table row or insert a newline and indent.
  11943. Calls `org-table-next-row' or `newline-and-indent', depending on
  11944. context. See the individual commands for more information."
  11945. (interactive)
  11946. (org-return t))
  11947. (defun org-ctrl-c-star ()
  11948. "Compute table, or change heading status of lines.
  11949. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11950. depending on context. This will also turn a plain list item or a normal
  11951. line into a subheading."
  11952. (interactive)
  11953. (cond
  11954. ((org-at-table-p)
  11955. (call-interactively 'org-table-recalculate))
  11956. ((org-region-active-p)
  11957. ;; Convert all lines in region to list items
  11958. (call-interactively 'org-toggle-region-headings))
  11959. ((org-on-heading-p)
  11960. (org-toggle-region-headings (point-at-bol)
  11961. (min (1+ (point-at-eol)) (point-max))))
  11962. ((org-at-item-p)
  11963. ;; Convert to heading
  11964. (let ((level (save-match-data
  11965. (save-excursion
  11966. (condition-case nil
  11967. (progn
  11968. (org-back-to-heading t)
  11969. (funcall outline-level))
  11970. (error 0))))))
  11971. (replace-match
  11972. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11973. (t (org-toggle-region-headings (point-at-bol)
  11974. (min (1+ (point-at-eol)) (point-max))))))
  11975. (defun org-ctrl-c-minus ()
  11976. "Insert separator line in table or modify bullet status of line.
  11977. Also turns a plain line or a region of lines into list items.
  11978. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11979. `org-cycle-list-bullet', depending on context."
  11980. (interactive)
  11981. (cond
  11982. ((org-at-table-p)
  11983. (call-interactively 'org-table-insert-hline))
  11984. ((org-on-heading-p)
  11985. ;; Convert to item
  11986. (save-excursion
  11987. (beginning-of-line 1)
  11988. (if (looking-at "\\*+ ")
  11989. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11990. ((org-region-active-p)
  11991. ;; Convert all lines in region to list items
  11992. (call-interactively 'org-toggle-region-items))
  11993. ((org-in-item-p)
  11994. (call-interactively 'org-cycle-list-bullet))
  11995. (t (org-toggle-region-items (point-at-bol)
  11996. (min (1+ (point-at-eol)) (point-max))))))
  11997. (defun org-toggle-region-items (beg end)
  11998. "Convert all lines in region to list items.
  11999. If the first line is already an item, convert all list items in the region
  12000. to normal lines."
  12001. (interactive "r")
  12002. (let (l2 l)
  12003. (save-excursion
  12004. (goto-char end)
  12005. (setq l2 (org-current-line))
  12006. (goto-char beg)
  12007. (beginning-of-line 1)
  12008. (setq l (1- (org-current-line)))
  12009. (if (org-at-item-p)
  12010. ;; We already have items, de-itemize
  12011. (while (< (setq l (1+ l)) l2)
  12012. (when (org-at-item-p)
  12013. (goto-char (match-beginning 2))
  12014. (delete-region (match-beginning 2) (match-end 2))
  12015. (and (looking-at "[ \t]+") (replace-match "")))
  12016. (beginning-of-line 2))
  12017. (while (< (setq l (1+ l)) l2)
  12018. (unless (org-at-item-p)
  12019. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12020. (replace-match "\\1- \\2")))
  12021. (beginning-of-line 2))))))
  12022. (defun org-toggle-region-headings (beg end)
  12023. "Convert all lines in region to list items.
  12024. If the first line is already an item, convert all list items in the region
  12025. to normal lines."
  12026. (interactive "r")
  12027. (let (l2 l)
  12028. (save-excursion
  12029. (goto-char end)
  12030. (setq l2 (org-current-line))
  12031. (goto-char beg)
  12032. (beginning-of-line 1)
  12033. (setq l (1- (org-current-line)))
  12034. (if (org-on-heading-p)
  12035. ;; We already have headlines, de-star them
  12036. (while (< (setq l (1+ l)) l2)
  12037. (when (org-on-heading-p t)
  12038. (and (looking-at outline-regexp) (replace-match "")))
  12039. (beginning-of-line 2))
  12040. (let* ((stars (save-excursion
  12041. (re-search-backward org-complex-heading-regexp nil t)
  12042. (or (match-string 1) "*")))
  12043. (add-stars (if org-odd-levels-only "**" "*"))
  12044. (rpl (concat stars add-stars " \\2")))
  12045. (while (< (setq l (1+ l)) l2)
  12046. (unless (org-on-heading-p)
  12047. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12048. (replace-match rpl)))
  12049. (beginning-of-line 2)))))))
  12050. (defun org-meta-return (&optional arg)
  12051. "Insert a new heading or wrap a region in a table.
  12052. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12053. See the individual commands for more information."
  12054. (interactive "P")
  12055. (cond
  12056. ((org-at-table-p)
  12057. (call-interactively 'org-table-wrap-region))
  12058. (t (call-interactively 'org-insert-heading))))
  12059. ;;; Menu entries
  12060. ;; Define the Org-mode menus
  12061. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12062. '("Tbl"
  12063. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12064. ["Next Field" org-cycle (org-at-table-p)]
  12065. ["Previous Field" org-shifttab (org-at-table-p)]
  12066. ["Next Row" org-return (org-at-table-p)]
  12067. "--"
  12068. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12069. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12070. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12071. "--"
  12072. ("Column"
  12073. ["Move Column Left" org-metaleft (org-at-table-p)]
  12074. ["Move Column Right" org-metaright (org-at-table-p)]
  12075. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12076. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12077. ("Row"
  12078. ["Move Row Up" org-metaup (org-at-table-p)]
  12079. ["Move Row Down" org-metadown (org-at-table-p)]
  12080. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12081. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12082. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12083. "--"
  12084. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12085. ("Rectangle"
  12086. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12087. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12088. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12089. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12090. "--"
  12091. ("Calculate"
  12092. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12093. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12094. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12095. "--"
  12096. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12097. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12098. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12099. "--"
  12100. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12101. "--"
  12102. ["Sum Column/Rectangle" org-table-sum
  12103. (or (org-at-table-p) (org-region-active-p))]
  12104. ["Which Column?" org-table-current-column (org-at-table-p)])
  12105. ["Debug Formulas"
  12106. org-table-toggle-formula-debugger
  12107. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12108. ["Show Col/Row Numbers"
  12109. org-table-toggle-coordinate-overlays
  12110. :style toggle
  12111. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12112. "--"
  12113. ["Create" org-table-create (and (not (org-at-table-p))
  12114. org-enable-table-editor)]
  12115. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12116. ["Import from File" org-table-import (not (org-at-table-p))]
  12117. ["Export to File" org-table-export (org-at-table-p)]
  12118. "--"
  12119. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12120. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12121. '("Org"
  12122. ("Show/Hide"
  12123. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12124. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12125. ["Sparse Tree..." org-sparse-tree t]
  12126. ["Reveal Context" org-reveal t]
  12127. ["Show All" show-all t]
  12128. "--"
  12129. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12130. "--"
  12131. ["New Heading" org-insert-heading t]
  12132. ("Navigate Headings"
  12133. ["Up" outline-up-heading t]
  12134. ["Next" outline-next-visible-heading t]
  12135. ["Previous" outline-previous-visible-heading t]
  12136. ["Next Same Level" outline-forward-same-level t]
  12137. ["Previous Same Level" outline-backward-same-level t]
  12138. "--"
  12139. ["Jump" org-goto t])
  12140. ("Edit Structure"
  12141. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12142. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12143. "--"
  12144. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12145. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12146. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12147. "--"
  12148. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12149. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12150. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12151. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12152. "--"
  12153. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12154. "--"
  12155. ["Convert to odd levels" org-convert-to-odd-levels t]
  12156. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12157. ("Editing"
  12158. ["Emphasis..." org-emphasize t]
  12159. ["Edit Source Example" org-edit-special t])
  12160. ("Archive"
  12161. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12162. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12163. ; :active t :keys "C-u C-c C-x C-a"]
  12164. ["Sparse trees open ARCHIVE trees"
  12165. (setq org-sparse-tree-open-archived-trees
  12166. (not org-sparse-tree-open-archived-trees))
  12167. :style toggle :selected org-sparse-tree-open-archived-trees]
  12168. ["Cycling opens ARCHIVE trees"
  12169. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12170. :style toggle :selected org-cycle-open-archived-trees]
  12171. "--"
  12172. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12173. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12174. ; ["Check and Move Children" (org-archive-subtree '(4))
  12175. ; :active t :keys "C-u C-c C-x C-s"]
  12176. )
  12177. "--"
  12178. ("TODO Lists"
  12179. ["TODO/DONE/-" org-todo t]
  12180. ("Select keyword"
  12181. ["Next keyword" org-shiftright (org-on-heading-p)]
  12182. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12183. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12184. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12185. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12186. ["Show TODO Tree" org-show-todo-tree t]
  12187. ["Global TODO list" org-todo-list t]
  12188. "--"
  12189. ["Set Priority" org-priority t]
  12190. ["Priority Up" org-shiftup t]
  12191. ["Priority Down" org-shiftdown t])
  12192. ("TAGS and Properties"
  12193. ["Set Tags" 'org-set-tags-command t]
  12194. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12195. "--"
  12196. ["Set property" 'org-set-property t]
  12197. ["Column view of properties" org-columns t]
  12198. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12199. ("Dates and Scheduling"
  12200. ["Timestamp" org-time-stamp t]
  12201. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12202. ("Change Date"
  12203. ["1 Day Later" org-shiftright t]
  12204. ["1 Day Earlier" org-shiftleft t]
  12205. ["1 ... Later" org-shiftup t]
  12206. ["1 ... Earlier" org-shiftdown t])
  12207. ["Compute Time Range" org-evaluate-time-range t]
  12208. ["Schedule Item" org-schedule t]
  12209. ["Deadline" org-deadline t]
  12210. "--"
  12211. ["Custom time format" org-toggle-time-stamp-overlays
  12212. :style radio :selected org-display-custom-times]
  12213. "--"
  12214. ["Goto Calendar" org-goto-calendar t]
  12215. ["Date from Calendar" org-date-from-calendar t]
  12216. "--"
  12217. ["Start/restart timer" org-timer-start t]
  12218. ["Insert timer string" org-timer t]
  12219. ["Insert timer item" org-timer-item t])
  12220. ("Logging work"
  12221. ["Clock in" org-clock-in t]
  12222. ["Clock out" org-clock-out t]
  12223. ["Clock cancel" org-clock-cancel t]
  12224. ["Goto running clock" org-clock-goto t]
  12225. ["Display times" org-clock-display t]
  12226. ["Create clock table" org-clock-report t]
  12227. "--"
  12228. ["Record DONE time"
  12229. (progn (setq org-log-done (not org-log-done))
  12230. (message "Switching to %s will %s record a timestamp"
  12231. (car org-done-keywords)
  12232. (if org-log-done "automatically" "not")))
  12233. :style toggle :selected org-log-done])
  12234. "--"
  12235. ["Agenda Command..." org-agenda t]
  12236. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12237. ("File List for Agenda")
  12238. ("Special views current file"
  12239. ["TODO Tree" org-show-todo-tree t]
  12240. ["Check Deadlines" org-check-deadlines t]
  12241. ["Timeline" org-timeline t]
  12242. ["Tags Tree" org-tags-sparse-tree t])
  12243. "--"
  12244. ("Hyperlinks"
  12245. ["Store Link (Global)" org-store-link t]
  12246. ["Insert Link" org-insert-link t]
  12247. ["Follow Link" org-open-at-point t]
  12248. "--"
  12249. ["Next link" org-next-link t]
  12250. ["Previous link" org-previous-link t]
  12251. "--"
  12252. ["Descriptive Links"
  12253. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12254. :style radio
  12255. :selected (member '(org-link) buffer-invisibility-spec)]
  12256. ["Literal Links"
  12257. (progn
  12258. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12259. :style radio
  12260. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12261. "--"
  12262. ["Export/Publish..." org-export t]
  12263. ("LaTeX"
  12264. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12265. :selected org-cdlatex-mode]
  12266. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12267. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12268. ["Modify math symbol" org-cdlatex-math-modify
  12269. (org-inside-LaTeX-fragment-p)]
  12270. ["Export LaTeX fragments as images"
  12271. (if (featurep 'org-exp)
  12272. (setq org-export-with-LaTeX-fragments
  12273. (not org-export-with-LaTeX-fragments))
  12274. (require 'org-exp))
  12275. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12276. org-export-with-LaTeX-fragments)])
  12277. "--"
  12278. ("Documentation"
  12279. ["Show Version" org-version t]
  12280. ["Info Documentation" org-info t])
  12281. ("Customize"
  12282. ["Browse Org Group" org-customize t]
  12283. "--"
  12284. ["Expand This Menu" org-create-customize-menu
  12285. (fboundp 'customize-menu-create)])
  12286. "--"
  12287. ["Refresh setup" org-mode-restart t]
  12288. ))
  12289. (defun org-info (&optional node)
  12290. "Read documentation for Org-mode in the info system.
  12291. With optional NODE, go directly to that node."
  12292. (interactive)
  12293. (info (format "(org)%s" (or node ""))))
  12294. (defun org-install-agenda-files-menu ()
  12295. (let ((bl (buffer-list)))
  12296. (save-excursion
  12297. (while bl
  12298. (set-buffer (pop bl))
  12299. (if (org-mode-p) (setq bl nil)))
  12300. (when (org-mode-p)
  12301. (easy-menu-change
  12302. '("Org") "File List for Agenda"
  12303. (append
  12304. (list
  12305. ["Edit File List" (org-edit-agenda-file-list) t]
  12306. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12307. ["Remove Current File from List" org-remove-file t]
  12308. ["Cycle through agenda files" org-cycle-agenda-files t]
  12309. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12310. "--")
  12311. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12312. ;;;; Documentation
  12313. ;;;###autoload
  12314. (defun org-require-autoloaded-modules ()
  12315. (interactive)
  12316. (mapc 'require
  12317. '(org-agenda org-archive org-clock org-colview
  12318. org-exp org-id org-export-latex org-publish
  12319. org-remember org-table)))
  12320. ;;;###autoload
  12321. (defun org-customize ()
  12322. "Call the customize function with org as argument."
  12323. (interactive)
  12324. (org-load-modules-maybe)
  12325. (org-require-autoloaded-modules)
  12326. (customize-browse 'org))
  12327. (defun org-create-customize-menu ()
  12328. "Create a full customization menu for Org-mode, insert it into the menu."
  12329. (interactive)
  12330. (org-load-modules-maybe)
  12331. (org-require-autoloaded-modules)
  12332. (if (fboundp 'customize-menu-create)
  12333. (progn
  12334. (easy-menu-change
  12335. '("Org") "Customize"
  12336. `(["Browse Org group" org-customize t]
  12337. "--"
  12338. ,(customize-menu-create 'org)
  12339. ["Set" Custom-set t]
  12340. ["Save" Custom-save t]
  12341. ["Reset to Current" Custom-reset-current t]
  12342. ["Reset to Saved" Custom-reset-saved t]
  12343. ["Reset to Standard Settings" Custom-reset-standard t]))
  12344. (message "\"Org\"-menu now contains full customization menu"))
  12345. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12346. ;;;; Miscellaneous stuff
  12347. ;;; Generally useful functions
  12348. (defun org-find-text-property-in-string (prop s)
  12349. "Return the first non-nil value of property PROP in string S."
  12350. (or (get-text-property 0 prop s)
  12351. (get-text-property (or (next-single-property-change 0 prop s) 0)
  12352. prop s)))
  12353. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12354. "Display the given MESSAGE as a warning."
  12355. (if (fboundp 'display-warning)
  12356. (display-warning 'org message
  12357. (if (featurep 'xemacs)
  12358. 'warning
  12359. :warning))
  12360. (let ((buf (get-buffer-create "*Org warnings*")))
  12361. (with-current-buffer buf
  12362. (goto-char (point-max))
  12363. (insert "Warning (Org): " message)
  12364. (unless (bolp)
  12365. (newline)))
  12366. (display-buffer buf)
  12367. (sit-for 0))))
  12368. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12369. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12370. (if (and marker (marker-buffer marker)
  12371. (buffer-live-p (marker-buffer marker)))
  12372. (progn
  12373. (switch-to-buffer (marker-buffer marker))
  12374. (if (or (> marker (point-max)) (< marker (point-min)))
  12375. (widen))
  12376. (goto-char marker))
  12377. (if bookmark
  12378. (bookmark-jump bookmark)
  12379. (error "Cannot find location"))))
  12380. (defun org-quote-csv-field (s)
  12381. "Quote field for inclusion in CSV material."
  12382. (if (string-match "[\",]" s)
  12383. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12384. s))
  12385. (defun org-plist-delete (plist property)
  12386. "Delete PROPERTY from PLIST.
  12387. This is in contrast to merely setting it to 0."
  12388. (let (p)
  12389. (while plist
  12390. (if (not (eq property (car plist)))
  12391. (setq p (plist-put p (car plist) (nth 1 plist))))
  12392. (setq plist (cddr plist)))
  12393. p))
  12394. (defun org-force-self-insert (N)
  12395. "Needed to enforce self-insert under remapping."
  12396. (interactive "p")
  12397. (self-insert-command N))
  12398. (defun org-string-width (s)
  12399. "Compute width of string, ignoring invisible characters.
  12400. This ignores character with invisibility property `org-link', and also
  12401. characters with property `org-cwidth', because these will become invisible
  12402. upon the next fontification round."
  12403. (let (b l)
  12404. (when (or (eq t buffer-invisibility-spec)
  12405. (assq 'org-link buffer-invisibility-spec))
  12406. (while (setq b (text-property-any 0 (length s)
  12407. 'invisible 'org-link s))
  12408. (setq s (concat (substring s 0 b)
  12409. (substring s (or (next-single-property-change
  12410. b 'invisible s) (length s)))))))
  12411. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12412. (setq s (concat (substring s 0 b)
  12413. (substring s (or (next-single-property-change
  12414. b 'org-cwidth s) (length s))))))
  12415. (setq l (string-width s) b -1)
  12416. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12417. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12418. l))
  12419. (defun org-get-indentation (&optional line)
  12420. "Get the indentation of the current line, interpreting tabs.
  12421. When LINE is given, assume it represents a line and compute its indentation."
  12422. (if line
  12423. (if (string-match "^ *" (org-remove-tabs line))
  12424. (match-end 0))
  12425. (save-excursion
  12426. (beginning-of-line 1)
  12427. (skip-chars-forward " \t")
  12428. (current-column))))
  12429. (defun org-remove-tabs (s &optional width)
  12430. "Replace tabulators in S with spaces.
  12431. Assumes that s is a single line, starting in column 0."
  12432. (setq width (or width tab-width))
  12433. (while (string-match "\t" s)
  12434. (setq s (replace-match
  12435. (make-string
  12436. (- (* width (/ (+ (match-beginning 0) width) width))
  12437. (match-beginning 0)) ?\ )
  12438. t t s)))
  12439. s)
  12440. (defun org-fix-indentation (line ind)
  12441. "Fix indentation in LINE.
  12442. IND is a cons cell with target and minimum indentation.
  12443. If the current indenation in LINE is smaller than the minimum,
  12444. leave it alone. If it is larger than ind, set it to the target."
  12445. (let* ((l (org-remove-tabs line))
  12446. (i (org-get-indentation l))
  12447. (i1 (car ind)) (i2 (cdr ind)))
  12448. (if (>= i i2) (setq l (substring line i2)))
  12449. (if (> i1 0)
  12450. (concat (make-string i1 ?\ ) l)
  12451. l)))
  12452. (defun org-base-buffer (buffer)
  12453. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12454. (if (not buffer)
  12455. buffer
  12456. (or (buffer-base-buffer buffer)
  12457. buffer)))
  12458. (defun org-trim (s)
  12459. "Remove whitespace at beginning and end of string."
  12460. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12461. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12462. s)
  12463. (defun org-wrap (string &optional width lines)
  12464. "Wrap string to either a number of lines, or a width in characters.
  12465. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12466. that costs. If there is a word longer than WIDTH, the text is actually
  12467. wrapped to the length of that word.
  12468. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12469. many lines, whatever width that takes.
  12470. The return value is a list of lines, without newlines at the end."
  12471. (let* ((words (org-split-string string "[ \t\n]+"))
  12472. (maxword (apply 'max (mapcar 'org-string-width words)))
  12473. w ll)
  12474. (cond (width
  12475. (org-do-wrap words (max maxword width)))
  12476. (lines
  12477. (setq w maxword)
  12478. (setq ll (org-do-wrap words maxword))
  12479. (if (<= (length ll) lines)
  12480. ll
  12481. (setq ll words)
  12482. (while (> (length ll) lines)
  12483. (setq w (1+ w))
  12484. (setq ll (org-do-wrap words w)))
  12485. ll))
  12486. (t (error "Cannot wrap this")))))
  12487. (defun org-do-wrap (words width)
  12488. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12489. (let (lines line)
  12490. (while words
  12491. (setq line (pop words))
  12492. (while (and words (< (+ (length line) (length (car words))) width))
  12493. (setq line (concat line " " (pop words))))
  12494. (setq lines (push line lines)))
  12495. (nreverse lines)))
  12496. (defun org-split-string (string &optional separators)
  12497. "Splits STRING into substrings at SEPARATORS.
  12498. No empty strings are returned if there are matches at the beginning
  12499. and end of string."
  12500. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12501. (start 0)
  12502. notfirst
  12503. (list nil))
  12504. (while (and (string-match rexp string
  12505. (if (and notfirst
  12506. (= start (match-beginning 0))
  12507. (< start (length string)))
  12508. (1+ start) start))
  12509. (< (match-beginning 0) (length string)))
  12510. (setq notfirst t)
  12511. (or (eq (match-beginning 0) 0)
  12512. (and (eq (match-beginning 0) (match-end 0))
  12513. (eq (match-beginning 0) start))
  12514. (setq list
  12515. (cons (substring string start (match-beginning 0))
  12516. list)))
  12517. (setq start (match-end 0)))
  12518. (or (eq start (length string))
  12519. (setq list
  12520. (cons (substring string start)
  12521. list)))
  12522. (nreverse list)))
  12523. (defun org-context ()
  12524. "Return a list of contexts of the current cursor position.
  12525. If several contexts apply, all are returned.
  12526. Each context entry is a list with a symbol naming the context, and
  12527. two positions indicating start and end of the context. Possible
  12528. contexts are:
  12529. :headline anywhere in a headline
  12530. :headline-stars on the leading stars in a headline
  12531. :todo-keyword on a TODO keyword (including DONE) in a headline
  12532. :tags on the TAGS in a headline
  12533. :priority on the priority cookie in a headline
  12534. :item on the first line of a plain list item
  12535. :item-bullet on the bullet/number of a plain list item
  12536. :checkbox on the checkbox in a plain list item
  12537. :table in an org-mode table
  12538. :table-special on a special filed in a table
  12539. :table-table in a table.el table
  12540. :link on a hyperlink
  12541. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12542. :target on a <<target>>
  12543. :radio-target on a <<<radio-target>>>
  12544. :latex-fragment on a LaTeX fragment
  12545. :latex-preview on a LaTeX fragment with overlayed preview image
  12546. This function expects the position to be visible because it uses font-lock
  12547. faces as a help to recognize the following contexts: :table-special, :link,
  12548. and :keyword."
  12549. (let* ((f (get-text-property (point) 'face))
  12550. (faces (if (listp f) f (list f)))
  12551. (p (point)) clist o)
  12552. ;; First the large context
  12553. (cond
  12554. ((org-on-heading-p t)
  12555. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12556. (when (progn
  12557. (beginning-of-line 1)
  12558. (looking-at org-todo-line-tags-regexp))
  12559. (push (org-point-in-group p 1 :headline-stars) clist)
  12560. (push (org-point-in-group p 2 :todo-keyword) clist)
  12561. (push (org-point-in-group p 4 :tags) clist))
  12562. (goto-char p)
  12563. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12564. (if (looking-at "\\[#[A-Z0-9]\\]")
  12565. (push (org-point-in-group p 0 :priority) clist)))
  12566. ((org-at-item-p)
  12567. (push (org-point-in-group p 2 :item-bullet) clist)
  12568. (push (list :item (point-at-bol)
  12569. (save-excursion (org-end-of-item) (point)))
  12570. clist)
  12571. (and (org-at-item-checkbox-p)
  12572. (push (org-point-in-group p 0 :checkbox) clist)))
  12573. ((org-at-table-p)
  12574. (push (list :table (org-table-begin) (org-table-end)) clist)
  12575. (if (memq 'org-formula faces)
  12576. (push (list :table-special
  12577. (previous-single-property-change p 'face)
  12578. (next-single-property-change p 'face)) clist)))
  12579. ((org-at-table-p 'any)
  12580. (push (list :table-table) clist)))
  12581. (goto-char p)
  12582. ;; Now the small context
  12583. (cond
  12584. ((org-at-timestamp-p)
  12585. (push (org-point-in-group p 0 :timestamp) clist))
  12586. ((memq 'org-link faces)
  12587. (push (list :link
  12588. (previous-single-property-change p 'face)
  12589. (next-single-property-change p 'face)) clist))
  12590. ((memq 'org-special-keyword faces)
  12591. (push (list :keyword
  12592. (previous-single-property-change p 'face)
  12593. (next-single-property-change p 'face)) clist))
  12594. ((org-on-target-p)
  12595. (push (org-point-in-group p 0 :target) clist)
  12596. (goto-char (1- (match-beginning 0)))
  12597. (if (looking-at org-radio-target-regexp)
  12598. (push (org-point-in-group p 0 :radio-target) clist))
  12599. (goto-char p))
  12600. ((setq o (car (delq nil
  12601. (mapcar
  12602. (lambda (x)
  12603. (if (memq x org-latex-fragment-image-overlays) x))
  12604. (org-overlays-at (point))))))
  12605. (push (list :latex-fragment
  12606. (org-overlay-start o) (org-overlay-end o)) clist)
  12607. (push (list :latex-preview
  12608. (org-overlay-start o) (org-overlay-end o)) clist))
  12609. ((org-inside-LaTeX-fragment-p)
  12610. ;; FIXME: positions wrong.
  12611. (push (list :latex-fragment (point) (point)) clist)))
  12612. (setq clist (nreverse (delq nil clist)))
  12613. clist))
  12614. ;; FIXME: Compare with at-regexp-p Do we need both?
  12615. (defun org-in-regexp (re &optional nlines visually)
  12616. "Check if point is inside a match of regexp.
  12617. Normally only the current line is checked, but you can include NLINES extra
  12618. lines both before and after point into the search.
  12619. If VISUALLY is set, require that the cursor is not after the match but
  12620. really on, so that the block visually is on the match."
  12621. (catch 'exit
  12622. (let ((pos (point))
  12623. (eol (point-at-eol (+ 1 (or nlines 0))))
  12624. (inc (if visually 1 0)))
  12625. (save-excursion
  12626. (beginning-of-line (- 1 (or nlines 0)))
  12627. (while (re-search-forward re eol t)
  12628. (if (and (<= (match-beginning 0) pos)
  12629. (>= (+ inc (match-end 0)) pos))
  12630. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12631. (defun org-at-regexp-p (regexp)
  12632. "Is point inside a match of REGEXP in the current line?"
  12633. (catch 'exit
  12634. (save-excursion
  12635. (let ((pos (point)) (end (point-at-eol)))
  12636. (beginning-of-line 1)
  12637. (while (re-search-forward regexp end t)
  12638. (if (and (<= (match-beginning 0) pos)
  12639. (>= (match-end 0) pos))
  12640. (throw 'exit t)))
  12641. nil))))
  12642. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12643. "Call `multi-occur' with buffers for all agenda files."
  12644. (interactive "sOrg-files matching: \np")
  12645. (let* ((files (org-agenda-files))
  12646. (tnames (mapcar 'file-truename files))
  12647. (extra org-agenda-text-search-extra-files)
  12648. f)
  12649. (when (eq (car extra) 'agenda-archives)
  12650. (setq extra (cdr extra))
  12651. (setq files (org-add-archive-files files)))
  12652. (while (setq f (pop extra))
  12653. (unless (member (file-truename f) tnames)
  12654. (add-to-list 'files f 'append)
  12655. (add-to-list 'tnames (file-truename f) 'append)))
  12656. (multi-occur
  12657. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12658. regexp)))
  12659. (if (boundp 'occur-mode-find-occurrence-hook)
  12660. ;; Emacs 23
  12661. (add-hook 'occur-mode-find-occurrence-hook
  12662. (lambda ()
  12663. (when (org-mode-p)
  12664. (org-reveal))))
  12665. ;; Emacs 22
  12666. (defadvice occur-mode-goto-occurrence
  12667. (after org-occur-reveal activate)
  12668. (and (org-mode-p) (org-reveal)))
  12669. (defadvice occur-mode-goto-occurrence-other-window
  12670. (after org-occur-reveal activate)
  12671. (and (org-mode-p) (org-reveal)))
  12672. (defadvice occur-mode-display-occurrence
  12673. (after org-occur-reveal activate)
  12674. (when (org-mode-p)
  12675. (let ((pos (occur-mode-find-occurrence)))
  12676. (with-current-buffer (marker-buffer pos)
  12677. (save-excursion
  12678. (goto-char pos)
  12679. (org-reveal)))))))
  12680. (defun org-uniquify (list)
  12681. "Remove duplicate elements from LIST."
  12682. (let (res)
  12683. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12684. res))
  12685. (defun org-delete-all (elts list)
  12686. "Remove all elements in ELTS from LIST."
  12687. (while elts
  12688. (setq list (delete (pop elts) list)))
  12689. list)
  12690. (defun org-back-over-empty-lines ()
  12691. "Move backwards over witespace, to the beginning of the first empty line.
  12692. Returns the number of empty lines passed."
  12693. (let ((pos (point)))
  12694. (skip-chars-backward " \t\n\r")
  12695. (beginning-of-line 2)
  12696. (goto-char (min (point) pos))
  12697. (count-lines (point) pos)))
  12698. (defun org-skip-whitespace ()
  12699. (skip-chars-forward " \t\n\r"))
  12700. (defun org-point-in-group (point group &optional context)
  12701. "Check if POINT is in match-group GROUP.
  12702. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12703. match. If the match group does ot exist or point is not inside it,
  12704. return nil."
  12705. (and (match-beginning group)
  12706. (>= point (match-beginning group))
  12707. (<= point (match-end group))
  12708. (if context
  12709. (list context (match-beginning group) (match-end group))
  12710. t)))
  12711. (defun org-switch-to-buffer-other-window (&rest args)
  12712. "Switch to buffer in a second window on the current frame.
  12713. In particular, do not allow pop-up frames."
  12714. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12715. special-display-function)
  12716. (apply 'switch-to-buffer-other-window args)))
  12717. (defun org-combine-plists (&rest plists)
  12718. "Create a single property list from all plists in PLISTS.
  12719. The process starts by copying the first list, and then setting properties
  12720. from the other lists. Settings in the last list are the most significant
  12721. ones and overrule settings in the other lists."
  12722. (let ((rtn (copy-sequence (pop plists)))
  12723. p v ls)
  12724. (while plists
  12725. (setq ls (pop plists))
  12726. (while ls
  12727. (setq p (pop ls) v (pop ls))
  12728. (setq rtn (plist-put rtn p v))))
  12729. rtn))
  12730. (defun org-move-line-down (arg)
  12731. "Move the current line down. With prefix argument, move it past ARG lines."
  12732. (interactive "p")
  12733. (let ((col (current-column))
  12734. beg end pos)
  12735. (beginning-of-line 1) (setq beg (point))
  12736. (beginning-of-line 2) (setq end (point))
  12737. (beginning-of-line (+ 1 arg))
  12738. (setq pos (move-marker (make-marker) (point)))
  12739. (insert (delete-and-extract-region beg end))
  12740. (goto-char pos)
  12741. (org-move-to-column col)))
  12742. (defun org-move-line-up (arg)
  12743. "Move the current line up. With prefix argument, move it past ARG lines."
  12744. (interactive "p")
  12745. (let ((col (current-column))
  12746. beg end pos)
  12747. (beginning-of-line 1) (setq beg (point))
  12748. (beginning-of-line 2) (setq end (point))
  12749. (beginning-of-line (- arg))
  12750. (setq pos (move-marker (make-marker) (point)))
  12751. (insert (delete-and-extract-region beg end))
  12752. (goto-char pos)
  12753. (org-move-to-column col)))
  12754. (defun org-replace-escapes (string table)
  12755. "Replace %-escapes in STRING with values in TABLE.
  12756. TABLE is an association list with keys like \"%a\" and string values.
  12757. The sequences in STRING may contain normal field width and padding information,
  12758. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12759. so values can contain further %-escapes if they are define later in TABLE."
  12760. (let ((case-fold-search nil)
  12761. e re rpl)
  12762. (while (setq e (pop table))
  12763. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12764. (while (string-match re string)
  12765. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12766. (cdr e)))
  12767. (setq string (replace-match rpl t t string))))
  12768. string))
  12769. (defun org-sublist (list start end)
  12770. "Return a section of LIST, from START to END.
  12771. Counting starts at 1."
  12772. (let (rtn (c start))
  12773. (setq list (nthcdr (1- start) list))
  12774. (while (and list (<= c end))
  12775. (push (pop list) rtn)
  12776. (setq c (1+ c)))
  12777. (nreverse rtn)))
  12778. (defun org-find-base-buffer-visiting (file)
  12779. "Like `find-buffer-visiting' but alway return the base buffer and
  12780. not an indirect buffer."
  12781. (let ((buf (find-buffer-visiting file)))
  12782. (if buf
  12783. (or (buffer-base-buffer buf) buf)
  12784. nil)))
  12785. (defun org-image-file-name-regexp ()
  12786. "Return regexp matching the file names of images."
  12787. (if (fboundp 'image-file-name-regexp)
  12788. (image-file-name-regexp)
  12789. (let ((image-file-name-extensions
  12790. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12791. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12792. (concat "\\."
  12793. (regexp-opt (nconc (mapcar 'upcase
  12794. image-file-name-extensions)
  12795. image-file-name-extensions)
  12796. t)
  12797. "\\'"))))
  12798. (defun org-file-image-p (file)
  12799. "Return non-nil if FILE is an image."
  12800. (save-match-data
  12801. (string-match (org-image-file-name-regexp) file)))
  12802. (defun org-get-cursor-date ()
  12803. "Return the date at cursor in as a time.
  12804. This works in the calendar and in the agenda, anywhere else it just
  12805. returns the current time."
  12806. (let (date day defd)
  12807. (cond
  12808. ((eq major-mode 'calendar-mode)
  12809. (setq date (calendar-cursor-to-date)
  12810. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12811. ((eq major-mode 'org-agenda-mode)
  12812. (setq day (get-text-property (point) 'day))
  12813. (if day
  12814. (setq date (calendar-gregorian-from-absolute day)
  12815. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12816. (nth 2 date))))))
  12817. (or defd (current-time))))
  12818. (defvar org-agenda-action-marker (make-marker)
  12819. "Marker pointing to the entry for the next agenda action.")
  12820. (defun org-mark-entry-for-agenda-action ()
  12821. "Mark the current entry as target of an agenda action.
  12822. Agenda actions are actions executed from the agenda with the key `k',
  12823. which make use of the date at the cursor."
  12824. (interactive)
  12825. (move-marker org-agenda-action-marker
  12826. (save-excursion (org-back-to-heading t) (point))
  12827. (current-buffer))
  12828. (message
  12829. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12830. ;;; Paragraph filling stuff.
  12831. ;; We want this to be just right, so use the full arsenal.
  12832. (defun org-indent-line-function ()
  12833. "Indent line like previous, but further if previous was headline or item."
  12834. (interactive)
  12835. (let* ((pos (point))
  12836. (itemp (org-at-item-p))
  12837. column bpos bcol tpos tcol bullet btype bullet-type)
  12838. ;; Find the previous relevant line
  12839. (beginning-of-line 1)
  12840. (cond
  12841. ((looking-at "#") (setq column 0))
  12842. ((looking-at "\\*+ ") (setq column 0))
  12843. (t
  12844. (beginning-of-line 0)
  12845. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12846. (beginning-of-line 0))
  12847. (cond
  12848. ((looking-at "\\*+[ \t]+")
  12849. (if (not org-adapt-indentation)
  12850. (setq column 0)
  12851. (goto-char (match-end 0))
  12852. (setq column (current-column))))
  12853. ((org-in-item-p)
  12854. (org-beginning-of-item)
  12855. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12856. (setq bpos (match-beginning 1) tpos (match-end 0)
  12857. bcol (progn (goto-char bpos) (current-column))
  12858. tcol (progn (goto-char tpos) (current-column))
  12859. bullet (match-string 1)
  12860. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12861. (if (> tcol (+ bcol org-description-max-indent))
  12862. (setq tcol (+ bcol 5)))
  12863. (if (not itemp)
  12864. (setq column tcol)
  12865. (goto-char pos)
  12866. (beginning-of-line 1)
  12867. (if (looking-at "\\S-")
  12868. (progn
  12869. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12870. (setq bullet (match-string 1)
  12871. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12872. (setq column (if (equal btype bullet-type) bcol tcol)))
  12873. (setq column (org-get-indentation)))))
  12874. (t (setq column (org-get-indentation))))))
  12875. (goto-char pos)
  12876. (if (<= (current-column) (current-indentation))
  12877. (org-indent-line-to column)
  12878. (save-excursion (org-indent-line-to column)))
  12879. (setq column (current-column))
  12880. (beginning-of-line 1)
  12881. (if (looking-at
  12882. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12883. (replace-match (concat "\\1" (format org-property-format
  12884. (match-string 2) (match-string 3)))
  12885. t nil))
  12886. (org-move-to-column column)))
  12887. (defun org-set-autofill-regexps ()
  12888. (interactive)
  12889. ;; In the paragraph separator we include headlines, because filling
  12890. ;; text in a line directly attached to a headline would otherwise
  12891. ;; fill the headline as well.
  12892. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12893. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12894. ;; The paragraph starter includes hand-formatted lists.
  12895. (org-set-local 'paragraph-start
  12896. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12897. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12898. ;; But only if the user has not turned off tables or fixed-width regions
  12899. (org-set-local
  12900. 'auto-fill-inhibit-regexp
  12901. (concat "\\*+ \\|#\\+"
  12902. "\\|[ \t]*" org-keyword-time-regexp
  12903. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12904. (concat
  12905. "\\|[ \t]*["
  12906. (if org-enable-table-editor "|" "")
  12907. (if org-enable-fixed-width-editor ":" "")
  12908. "]"))))
  12909. ;; We use our own fill-paragraph function, to make sure that tables
  12910. ;; and fixed-width regions are not wrapped. That function will pass
  12911. ;; through to `fill-paragraph' when appropriate.
  12912. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12913. ; Adaptive filling: To get full control, first make sure that
  12914. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12915. (org-set-local 'adaptive-fill-regexp "\000")
  12916. (org-set-local 'adaptive-fill-function
  12917. 'org-adaptive-fill-function)
  12918. (org-set-local
  12919. 'align-mode-rules-list
  12920. '((org-in-buffer-settings
  12921. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12922. (modes . '(org-mode))))))
  12923. (defun org-fill-paragraph (&optional justify)
  12924. "Re-align a table, pass through to fill-paragraph if no table."
  12925. (let ((table-p (org-at-table-p))
  12926. (table.el-p (org-at-table.el-p)))
  12927. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12928. (save-excursion (goto-char (point-at-bol))
  12929. (looking-at outline-regexp)))
  12930. t) ; skip headlines
  12931. (table.el-p t) ; skip table.el tables
  12932. (table-p (org-table-align) t) ; align org-mode tables
  12933. (t nil)))) ; call paragraph-fill
  12934. ;; For reference, this is the default value of adaptive-fill-regexp
  12935. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12936. (defun org-adaptive-fill-function ()
  12937. "Return a fill prefix for org-mode files.
  12938. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12939. work correctly."
  12940. (cond ((looking-at "#[ \t]+")
  12941. (match-string 0))
  12942. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12943. (save-excursion
  12944. (if (> (match-end 1) (+ (match-beginning 1)
  12945. org-description-max-indent))
  12946. (goto-char (+ (match-beginning 1) 5))
  12947. (goto-char (match-end 0)))
  12948. (make-string (current-column) ?\ )))
  12949. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  12950. (save-excursion
  12951. (goto-char (match-end 0))
  12952. (make-string (current-column) ?\ )))
  12953. (t nil)))
  12954. ;;; Other stuff.
  12955. (defun org-toggle-fixed-width-section (arg)
  12956. "Toggle the fixed-width export.
  12957. If there is no active region, the QUOTE keyword at the current headline is
  12958. inserted or removed. When present, it causes the text between this headline
  12959. and the next to be exported as fixed-width text, and unmodified.
  12960. If there is an active region, this command adds or removes a colon as the
  12961. first character of this line. If the first character of a line is a colon,
  12962. this line is also exported in fixed-width font."
  12963. (interactive "P")
  12964. (let* ((cc 0)
  12965. (regionp (org-region-active-p))
  12966. (beg (if regionp (region-beginning) (point)))
  12967. (end (if regionp (region-end)))
  12968. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12969. (case-fold-search nil)
  12970. (re "[ \t]*\\(:\\)")
  12971. off)
  12972. (if regionp
  12973. (save-excursion
  12974. (goto-char beg)
  12975. (setq cc (current-column))
  12976. (beginning-of-line 1)
  12977. (setq off (looking-at re))
  12978. (while (> nlines 0)
  12979. (setq nlines (1- nlines))
  12980. (beginning-of-line 1)
  12981. (cond
  12982. (arg
  12983. (org-move-to-column cc t)
  12984. (insert ":\n")
  12985. (forward-line -1))
  12986. ((and off (looking-at re))
  12987. (replace-match "" t t nil 1))
  12988. ((not off) (org-move-to-column cc t) (insert ":")))
  12989. (forward-line 1)))
  12990. (save-excursion
  12991. (org-back-to-heading)
  12992. (if (looking-at (concat outline-regexp
  12993. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12994. (replace-match "" t t nil 1)
  12995. (if (looking-at outline-regexp)
  12996. (progn
  12997. (goto-char (match-end 0))
  12998. (insert org-quote-string " "))))))))
  12999. ;;;; Functions extending outline functionality
  13000. (defun org-beginning-of-line (&optional arg)
  13001. "Go to the beginning of the current line. If that is invisible, continue
  13002. to a visible line beginning. This makes the function of C-a more intuitive.
  13003. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13004. first attempt, and only move to after the tags when the cursor is already
  13005. beyond the end of the headline."
  13006. (interactive "P")
  13007. (let ((pos (point)) refpos)
  13008. (beginning-of-line 1)
  13009. (if (bobp)
  13010. nil
  13011. (backward-char 1)
  13012. (if (org-invisible-p)
  13013. (while (and (not (bobp)) (org-invisible-p))
  13014. (backward-char 1)
  13015. (beginning-of-line 1))
  13016. (forward-char 1)))
  13017. (when org-special-ctrl-a/e
  13018. (cond
  13019. ((and (looking-at org-complex-heading-regexp)
  13020. (= (char-after (match-end 1)) ?\ ))
  13021. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  13022. (point-at-eol)))
  13023. (goto-char
  13024. (if (eq org-special-ctrl-a/e t)
  13025. (cond ((> pos refpos) refpos)
  13026. ((= pos (point)) refpos)
  13027. (t (point)))
  13028. (cond ((> pos (point)) (point))
  13029. ((not (eq last-command this-command)) (point))
  13030. (t refpos)))))
  13031. ((org-at-item-p)
  13032. (goto-char
  13033. (if (eq org-special-ctrl-a/e t)
  13034. (cond ((> pos (match-end 4)) (match-end 4))
  13035. ((= pos (point)) (match-end 4))
  13036. (t (point)))
  13037. (cond ((> pos (point)) (point))
  13038. ((not (eq last-command this-command)) (point))
  13039. (t (match-end 4))))))))
  13040. (org-no-warnings
  13041. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  13042. (defun org-end-of-line (&optional arg)
  13043. "Go to the end of the line.
  13044. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  13045. first attempt, and only move to after the tags when the cursor is already
  13046. beyond the end of the headline."
  13047. (interactive "P")
  13048. (if (or (not org-special-ctrl-a/e)
  13049. (not (org-on-heading-p)))
  13050. (end-of-line arg)
  13051. (let ((pos (point)))
  13052. (beginning-of-line 1)
  13053. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  13054. (if (eq org-special-ctrl-a/e t)
  13055. (if (or (< pos (match-beginning 1))
  13056. (= pos (match-end 0)))
  13057. (goto-char (match-beginning 1))
  13058. (goto-char (match-end 0)))
  13059. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13060. (goto-char (match-end 0))
  13061. (goto-char (match-beginning 1))))
  13062. (end-of-line arg))))
  13063. (org-no-warnings
  13064. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13065. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13066. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13067. (defun org-kill-line (&optional arg)
  13068. "Kill line, to tags or end of line."
  13069. (interactive "P")
  13070. (cond
  13071. ((or (not org-special-ctrl-k)
  13072. (bolp)
  13073. (not (org-on-heading-p)))
  13074. (call-interactively 'kill-line))
  13075. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13076. (kill-region (point) (match-beginning 1))
  13077. (org-set-tags nil t))
  13078. (t (kill-region (point) (point-at-eol)))))
  13079. (define-key org-mode-map "\C-k" 'org-kill-line)
  13080. (defun org-yank (&optional arg)
  13081. "Yank. If the kill is a subtree, treat it specially.
  13082. This command will look at the current kill and check if is a single
  13083. subtree, or a series of subtrees[1]. If it passes the test, and if the
  13084. cursor is at the beginning of a line or after the stars of a currently
  13085. empty headline, then the yank is handeled specially. How exactly depends
  13086. on the value of the following variables, both set by default.
  13087. org-yank-folded-subtrees
  13088. When set, the subree(s) will be folded after insertion, but only
  13089. if doing so would now swallow text after the yanked text.
  13090. org-yank-adjusted-subtrees
  13091. When set, the subtree will be promoted or demoted in order to
  13092. fit into the local outline tree structure, which means that the level
  13093. will be adjusted so that it becomes the smaller one of the two
  13094. *visible* surrounding headings.
  13095. Any prefix to this command will cause `yank' to be called directly with
  13096. no special treatment. In particular, a simple `C-u' prefix will just
  13097. plainly yank the text as it is.
  13098. \[1] Basically, the test checks if the first non-white line is a heading
  13099. and if there are no other headings with fewer stars."
  13100. (interactive "P")
  13101. (setq this-command 'yank)
  13102. (if arg
  13103. (call-interactively 'yank)
  13104. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13105. (and (org-kill-is-subtree-p)
  13106. (or (bolp)
  13107. (and (looking-at "[ \t]*$")
  13108. (string-match
  13109. "\\`\\*+\\'"
  13110. (buffer-substring (point-at-bol) (point)))))))
  13111. swallowp)
  13112. (cond
  13113. ((and subtreep org-yank-folded-subtrees)
  13114. (let ((beg (point))
  13115. end)
  13116. (if (and subtreep org-yank-adjusted-subtrees)
  13117. (org-paste-subtree nil nil 'for-yank)
  13118. (call-interactively 'yank))
  13119. (setq end (point))
  13120. (goto-char beg)
  13121. (when (and (bolp) subtreep
  13122. (not (setq swallowp
  13123. (org-yank-folding-would-swallow-text beg end))))
  13124. (or (looking-at outline-regexp)
  13125. (re-search-forward (concat "^" outline-regexp) end t))
  13126. (while (and (< (point) end) (looking-at outline-regexp))
  13127. (hide-subtree)
  13128. (org-cycle-show-empty-lines 'folded)
  13129. (condition-case nil
  13130. (outline-forward-same-level 1)
  13131. (error (goto-char end)))))
  13132. (when swallowp
  13133. (message
  13134. "Yanked text not folded because that would swallow text"))
  13135. (goto-char end)
  13136. (skip-chars-forward " \t\n\r")
  13137. (beginning-of-line 1)
  13138. (push-mark beg 'nomsg)))
  13139. ((and subtreep org-yank-adjusted-subtrees)
  13140. (let ((beg (point-at-bol)))
  13141. (org-paste-subtree nil nil 'for-yank)
  13142. (push-mark beg 'nomsg)))
  13143. (t
  13144. (call-interactively 'yank))))))
  13145. (defun org-yank-folding-would-swallow-text (beg end)
  13146. "Would hide-subtree at BEG swallow any text after END?"
  13147. (let (level)
  13148. (save-excursion
  13149. (goto-char beg)
  13150. (when (or (looking-at outline-regexp)
  13151. (re-search-forward (concat "^" outline-regexp) end t))
  13152. (setq level (org-outline-level)))
  13153. (goto-char end)
  13154. (skip-chars-forward " \t\r\n\v\f")
  13155. (if (or (eobp)
  13156. (and (bolp) (looking-at org-outline-regexp)
  13157. (<= (org-outline-level) level)))
  13158. nil ; Nothing would be swallowed
  13159. t)))) ; something would swallow
  13160. (define-key org-mode-map "\C-y" 'org-yank)
  13161. (defun org-invisible-p ()
  13162. "Check if point is at a character currently not visible."
  13163. ;; Early versions of noutline don't have `outline-invisible-p'.
  13164. (if (fboundp 'outline-invisible-p)
  13165. (outline-invisible-p)
  13166. (get-char-property (point) 'invisible)))
  13167. (defun org-invisible-p2 ()
  13168. "Check if point is at a character currently not visible."
  13169. (save-excursion
  13170. (if (and (eolp) (not (bobp))) (backward-char 1))
  13171. ;; Early versions of noutline don't have `outline-invisible-p'.
  13172. (if (fboundp 'outline-invisible-p)
  13173. (outline-invisible-p)
  13174. (get-char-property (point) 'invisible))))
  13175. (defun org-back-to-heading (&optional invisible-ok)
  13176. "Call `outline-back-to-heading', but provide a better error message."
  13177. (condition-case nil
  13178. (outline-back-to-heading invisible-ok)
  13179. (error (error "Before first headline at position %d in buffer %s"
  13180. (point) (current-buffer)))))
  13181. (defun org-before-first-heading-p ()
  13182. "Before first heading?"
  13183. (save-excursion
  13184. (null (re-search-backward "^\\*+ " nil t))))
  13185. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13186. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13187. (defun org-at-heading-or-item-p ()
  13188. (or (org-on-heading-p) (org-at-item-p)))
  13189. (defun org-on-target-p ()
  13190. (or (org-in-regexp org-radio-target-regexp)
  13191. (org-in-regexp org-target-regexp)))
  13192. (defun org-up-heading-all (arg)
  13193. "Move to the heading line of which the present line is a subheading.
  13194. This function considers both visible and invisible heading lines.
  13195. With argument, move up ARG levels."
  13196. (if (fboundp 'outline-up-heading-all)
  13197. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13198. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13199. (defun org-up-heading-safe ()
  13200. "Move to the heading line of which the present line is a subheading.
  13201. This version will not throw an error. It will return the level of the
  13202. headline found, or nil if no higher level is found."
  13203. (let ((pos (point)) start-level level
  13204. (re (concat "^" outline-regexp)))
  13205. (catch 'exit
  13206. (org-back-to-heading t)
  13207. (setq start-level (funcall outline-level))
  13208. (if (equal start-level 1) (throw 'exit nil))
  13209. (while (re-search-backward re nil t)
  13210. (setq level (funcall outline-level))
  13211. (if (< level start-level) (throw 'exit level)))
  13212. nil)))
  13213. (defun org-first-sibling-p ()
  13214. "Is this heading the first child of its parents?"
  13215. (interactive)
  13216. (let ((re (concat "^" outline-regexp))
  13217. level l)
  13218. (unless (org-at-heading-p t)
  13219. (error "Not at a heading"))
  13220. (setq level (funcall outline-level))
  13221. (save-excursion
  13222. (if (not (re-search-backward re nil t))
  13223. t
  13224. (setq l (funcall outline-level))
  13225. (< l level)))))
  13226. (defun org-goto-sibling (&optional previous)
  13227. "Goto the next sibling, even if it is invisible.
  13228. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13229. when a sibling was found. When none is found, return nil and don't
  13230. move point."
  13231. (let ((fun (if previous 're-search-backward 're-search-forward))
  13232. (pos (point))
  13233. (re (concat "^" outline-regexp))
  13234. level l)
  13235. (when (condition-case nil (org-back-to-heading t) (error nil))
  13236. (setq level (funcall outline-level))
  13237. (catch 'exit
  13238. (or previous (forward-char 1))
  13239. (while (funcall fun re nil t)
  13240. (setq l (funcall outline-level))
  13241. (when (< l level) (goto-char pos) (throw 'exit nil))
  13242. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13243. (goto-char pos)
  13244. nil))))
  13245. (defun org-show-siblings ()
  13246. "Show all siblings of the current headline."
  13247. (save-excursion
  13248. (while (org-goto-sibling) (org-flag-heading nil)))
  13249. (save-excursion
  13250. (while (org-goto-sibling 'previous)
  13251. (org-flag-heading nil))))
  13252. (defun org-show-hidden-entry ()
  13253. "Show an entry where even the heading is hidden."
  13254. (save-excursion
  13255. (org-show-entry)))
  13256. (defun org-flag-heading (flag &optional entry)
  13257. "Flag the current heading. FLAG non-nil means make invisible.
  13258. When ENTRY is non-nil, show the entire entry."
  13259. (save-excursion
  13260. (org-back-to-heading t)
  13261. ;; Check if we should show the entire entry
  13262. (if entry
  13263. (progn
  13264. (org-show-entry)
  13265. (save-excursion
  13266. (and (outline-next-heading)
  13267. (org-flag-heading nil))))
  13268. (outline-flag-region (max (point-min) (1- (point)))
  13269. (save-excursion (outline-end-of-heading) (point))
  13270. flag))))
  13271. (defun org-forward-same-level (arg)
  13272. "Move forward to the ARG'th subheading at same level as this one.
  13273. Stop at the first and last subheadings of a superior heading.
  13274. This is like outline-forward-same-level, but invisible headings are ok."
  13275. (interactive "p")
  13276. (org-back-to-heading t)
  13277. (while (> arg 0)
  13278. (let ((point-to-move-to (save-excursion
  13279. (org-get-next-sibling))))
  13280. (if point-to-move-to
  13281. (progn
  13282. (goto-char point-to-move-to)
  13283. (setq arg (1- arg)))
  13284. (progn
  13285. (setq arg 0)
  13286. (error "No following same-level heading"))))))
  13287. (defun org-get-next-sibling ()
  13288. "Move to next heading of the same level, and return point.
  13289. If there is no such heading, return nil.
  13290. This is like outline-next-sibling, but invisible headings are ok."
  13291. (let ((level (funcall outline-level)))
  13292. (outline-next-heading)
  13293. (while (and (not (eobp)) (> (funcall outline-level) level))
  13294. (outline-next-heading))
  13295. (if (or (eobp) (< (funcall outline-level) level))
  13296. nil
  13297. (point))))
  13298. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13299. ;; This is an exact copy of the original function, but it uses
  13300. ;; `org-back-to-heading', to make it work also in invisible
  13301. ;; trees. And is uses an invisible-OK argument.
  13302. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13303. (org-back-to-heading invisible-OK)
  13304. (let ((first t)
  13305. (level (funcall outline-level)))
  13306. (while (and (not (eobp))
  13307. (or first (> (funcall outline-level) level)))
  13308. (setq first nil)
  13309. (outline-next-heading))
  13310. (unless to-heading
  13311. (if (memq (preceding-char) '(?\n ?\^M))
  13312. (progn
  13313. ;; Go to end of line before heading
  13314. (forward-char -1)
  13315. (if (memq (preceding-char) '(?\n ?\^M))
  13316. ;; leave blank line before heading
  13317. (forward-char -1))))))
  13318. (point))
  13319. (defun org-show-subtree ()
  13320. "Show everything after this heading at deeper levels."
  13321. (outline-flag-region
  13322. (point)
  13323. (save-excursion
  13324. (outline-end-of-subtree) (outline-next-heading) (point))
  13325. nil))
  13326. (defun org-show-entry ()
  13327. "Show the body directly following this heading.
  13328. Show the heading too, if it is currently invisible."
  13329. (interactive)
  13330. (save-excursion
  13331. (condition-case nil
  13332. (progn
  13333. (org-back-to-heading t)
  13334. (outline-flag-region
  13335. (max (point-min) (1- (point)))
  13336. (save-excursion
  13337. (re-search-forward
  13338. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13339. (or (match-beginning 1) (point-max)))
  13340. nil))
  13341. (error nil))))
  13342. (defun org-make-options-regexp (kwds)
  13343. "Make a regular expression for keyword lines."
  13344. (concat
  13345. "^"
  13346. "#?[ \t]*\\+\\("
  13347. (mapconcat 'regexp-quote kwds "\\|")
  13348. "\\):[ \t]*"
  13349. "\\(.+\\)"))
  13350. ;; Make isearch reveal the necessary context
  13351. (defun org-isearch-end ()
  13352. "Reveal context after isearch exits."
  13353. (when isearch-success ; only if search was successful
  13354. (if (featurep 'xemacs)
  13355. ;; Under XEmacs, the hook is run in the correct place,
  13356. ;; we directly show the context.
  13357. (org-show-context 'isearch)
  13358. ;; In Emacs the hook runs *before* restoring the overlays.
  13359. ;; So we have to use a one-time post-command-hook to do this.
  13360. ;; (Emacs 22 has a special variable, see function `org-mode')
  13361. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13362. isearch-mode-end-hook-quit)
  13363. ;; Only when the isearch was not quitted.
  13364. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13365. 'append 'local)))))
  13366. (defun org-isearch-post-command ()
  13367. "Remove self from hook, and show context."
  13368. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13369. (org-show-context 'isearch))
  13370. ;;;; Integration with and fixes for other packages
  13371. ;;; Imenu support
  13372. (defvar org-imenu-markers nil
  13373. "All markers currently used by Imenu.")
  13374. (make-variable-buffer-local 'org-imenu-markers)
  13375. (defun org-imenu-new-marker (&optional pos)
  13376. "Return a new marker for use by Imenu, and remember the marker."
  13377. (let ((m (make-marker)))
  13378. (move-marker m (or pos (point)))
  13379. (push m org-imenu-markers)
  13380. m))
  13381. (defun org-imenu-get-tree ()
  13382. "Produce the index for Imenu."
  13383. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13384. (setq org-imenu-markers nil)
  13385. (let* ((n org-imenu-depth)
  13386. (re (concat "^" outline-regexp))
  13387. (subs (make-vector (1+ n) nil))
  13388. (last-level 0)
  13389. m tree level head)
  13390. (save-excursion
  13391. (save-restriction
  13392. (widen)
  13393. (goto-char (point-max))
  13394. (while (re-search-backward re nil t)
  13395. (setq level (org-reduced-level (funcall outline-level)))
  13396. (when (<= level n)
  13397. (looking-at org-complex-heading-regexp)
  13398. (setq head (org-link-display-format
  13399. (org-match-string-no-properties 4))
  13400. m (org-imenu-new-marker))
  13401. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13402. (if (>= level last-level)
  13403. (push (cons head m) (aref subs level))
  13404. (push (cons head (aref subs (1+ level))) (aref subs level))
  13405. (loop for i from (1+ level) to n do (aset subs i nil)))
  13406. (setq last-level level)))))
  13407. (aref subs 1)))
  13408. (eval-after-load "imenu"
  13409. '(progn
  13410. (add-hook 'imenu-after-jump-hook
  13411. (lambda ()
  13412. (if (eq major-mode 'org-mode)
  13413. (org-show-context 'org-goto))))))
  13414. (defun org-link-display-format (link)
  13415. "Replace a link with either the description, or the link target
  13416. if no description is present"
  13417. (save-match-data
  13418. (if (string-match org-bracket-link-analytic-regexp link)
  13419. (replace-match (or (match-string 5 link)
  13420. (concat (match-string 1 link)
  13421. (match-string 3 link)))
  13422. nil nil link)
  13423. link)))
  13424. ;; Speedbar support
  13425. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13426. "Overlay marking the agenda restriction line in speedbar.")
  13427. (org-overlay-put org-speedbar-restriction-lock-overlay
  13428. 'face 'org-agenda-restriction-lock)
  13429. (org-overlay-put org-speedbar-restriction-lock-overlay
  13430. 'help-echo "Agendas are currently limited to this item.")
  13431. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13432. (defun org-speedbar-set-agenda-restriction ()
  13433. "Restrict future agenda commands to the location at point in speedbar.
  13434. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13435. (interactive)
  13436. (require 'org-agenda)
  13437. (let (p m tp np dir txt w)
  13438. (cond
  13439. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13440. 'org-imenu t))
  13441. (setq m (get-text-property p 'org-imenu-marker))
  13442. (save-excursion
  13443. (save-restriction
  13444. (set-buffer (marker-buffer m))
  13445. (goto-char m)
  13446. (org-agenda-set-restriction-lock 'subtree))))
  13447. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13448. 'speedbar-function 'speedbar-find-file))
  13449. (setq tp (previous-single-property-change
  13450. (1+ p) 'speedbar-function)
  13451. np (next-single-property-change
  13452. tp 'speedbar-function)
  13453. dir (speedbar-line-directory)
  13454. txt (buffer-substring-no-properties (or tp (point-min))
  13455. (or np (point-max))))
  13456. (save-excursion
  13457. (save-restriction
  13458. (set-buffer (find-file-noselect
  13459. (let ((default-directory dir))
  13460. (expand-file-name txt))))
  13461. (unless (org-mode-p)
  13462. (error "Cannot restrict to non-Org-mode file"))
  13463. (org-agenda-set-restriction-lock 'file))))
  13464. (t (error "Don't know how to restrict Org-mode's agenda")))
  13465. (org-move-overlay org-speedbar-restriction-lock-overlay
  13466. (point-at-bol) (point-at-eol))
  13467. (setq current-prefix-arg nil)
  13468. (org-agenda-maybe-redo)))
  13469. (eval-after-load "speedbar"
  13470. '(progn
  13471. (speedbar-add-supported-extension ".org")
  13472. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13473. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13474. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13475. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13476. (add-hook 'speedbar-visiting-tag-hook
  13477. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13478. ;;; Fixes and Hacks for problems with other packages
  13479. ;; Make flyspell not check words in links, to not mess up our keymap
  13480. (defun org-mode-flyspell-verify ()
  13481. "Don't let flyspell put overlays at active buttons."
  13482. (not (get-text-property (point) 'keymap)))
  13483. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13484. (eval-after-load "bookmark"
  13485. '(if (boundp 'bookmark-after-jump-hook)
  13486. ;; We can use the hook
  13487. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13488. ;; Hook not available, use advice
  13489. (defadvice bookmark-jump (after org-make-visible activate)
  13490. "Make the position visible."
  13491. (org-bookmark-jump-unhide))))
  13492. ;; Make sure saveplace show the location if it was hidden
  13493. (eval-after-load "saveplace"
  13494. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13495. "Make the position visible."
  13496. (org-bookmark-jump-unhide)))
  13497. (defun org-bookmark-jump-unhide ()
  13498. "Unhide the current position, to show the bookmark location."
  13499. (and (org-mode-p)
  13500. (or (org-invisible-p)
  13501. (save-excursion (goto-char (max (point-min) (1- (point))))
  13502. (org-invisible-p)))
  13503. (org-show-context 'bookmark-jump)))
  13504. ;; Make session.el ignore our circular variable
  13505. (eval-after-load "session"
  13506. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13507. ;;;; Experimental code
  13508. (defun org-closed-in-range ()
  13509. "Sparse tree of items closed in a certain time range.
  13510. Still experimental, may disappear in the future."
  13511. (interactive)
  13512. ;; Get the time interval from the user.
  13513. (let* ((time1 (time-to-seconds
  13514. (org-read-date nil 'to-time nil "Starting date: ")))
  13515. (time2 (time-to-seconds
  13516. (org-read-date nil 'to-time nil "End date:")))
  13517. ;; callback function
  13518. (callback (lambda ()
  13519. (let ((time
  13520. (time-to-seconds
  13521. (apply 'encode-time
  13522. (org-parse-time-string
  13523. (match-string 1))))))
  13524. ;; check if time in interval
  13525. (and (>= time time1) (<= time time2))))))
  13526. ;; make tree, check each match with the callback
  13527. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13528. ;;;; Finish up
  13529. (provide 'org)
  13530. (run-hooks 'org-load-hook)
  13531. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13532. ;;; org.el ends here