org.el 534 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542
  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.05b
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.05b"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (when (org-bound-and-true-p org-modules)
  124. (let ((a (member 'org-infojs org-modules)))
  125. (and a (setcar a 'org-jsinfo))))
  126. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  127. "Modules that should always be loaded together with org.el.
  128. If a description starts with <C>, the file is not part of Emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution.
  131. You can also use this system to load external packages (i.e. neither Org
  132. core modules, not modules from the CONTRIB directory). Just add symbols
  133. to the end of the list. If the package is called org-xyz.el, then you need
  134. to add the symbol `xyz', and the package must have a call to
  135. (provide 'org-xyz)"
  136. :group 'org
  137. :set 'org-set-modules
  138. :type
  139. '(set :greedy t
  140. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  141. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  142. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  143. (const :tag " id: Global id's for identifying entries" org-id)
  144. (const :tag " info: Links to Info nodes" org-info)
  145. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  146. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  147. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  148. (const :tag " mew Links to Mew folders/messages" org-mew)
  149. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  150. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  151. (const :tag " vm: Links to VM folders/messages" org-vm)
  152. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  153. (const :tag " mouse: Additional mouse support" org-mouse)
  154. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  155. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  156. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  157. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  158. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  159. (const :tag "C eval: Include command output as text" org-eval)
  160. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  161. (const :tag "C id: Global id's for identifying entries" org-id)
  162. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  163. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  164. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  165. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  166. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  167. (const :tag "C registry: A registry for Org links" org-registry)
  168. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  169. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  170. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  171. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  172. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  173. (defgroup org-startup nil
  174. "Options concerning startup of Org-mode."
  175. :tag "Org Startup"
  176. :group 'org)
  177. (defcustom org-startup-folded t
  178. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  179. This can also be configured on a per-file basis by adding one of
  180. the following lines anywhere in the buffer:
  181. #+STARTUP: fold
  182. #+STARTUP: nofold
  183. #+STARTUP: content"
  184. :group 'org-startup
  185. :type '(choice
  186. (const :tag "nofold: show all" nil)
  187. (const :tag "fold: overview" t)
  188. (const :tag "content: all headlines" content)))
  189. (defcustom org-startup-truncated t
  190. "Non-nil means, entering Org-mode will set `truncate-lines'.
  191. This is useful since some lines containing links can be very long and
  192. uninteresting. Also tables look terrible when wrapped."
  193. :group 'org-startup
  194. :type 'boolean)
  195. (defcustom org-startup-indented nil
  196. "Non-nil means, turn on `org-indent-mode' on startup.
  197. This can also be configured on a per-file basis by adding one of
  198. the following lines anywhere in the buffer:
  199. #+STARTUP: localindent
  200. #+STARTUP: indent
  201. #+STARTUP: noindent"
  202. :group 'org-structure
  203. :type '(choice
  204. (const :tag "Not" nil)
  205. (const :tag "Locally" local)
  206. (const :tag "Globally (slow on startup in large files)" t)))
  207. (defcustom org-startup-align-all-tables nil
  208. "Non-nil means, align all tables when visiting a file.
  209. This is useful when the column width in tables is forced with <N> cookies
  210. in table fields. Such tables will look correct only after the first re-align.
  211. This can also be configured on a per-file basis by adding one of
  212. the following lines anywhere in the buffer:
  213. #+STARTUP: align
  214. #+STARTUP: noalign"
  215. :group 'org-startup
  216. :type 'boolean)
  217. (defcustom org-insert-mode-line-in-empty-file nil
  218. "Non-nil means insert the first line setting Org-mode in empty files.
  219. When the function `org-mode' is called interactively in an empty file, this
  220. normally means that the file name does not automatically trigger Org-mode.
  221. To ensure that the file will always be in Org-mode in the future, a
  222. line enforcing Org-mode will be inserted into the buffer, if this option
  223. has been set."
  224. :group 'org-startup
  225. :type 'boolean)
  226. (defcustom org-replace-disputed-keys nil
  227. "Non-nil means use alternative key bindings for some keys.
  228. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  229. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  230. If you want to use Org-mode together with one of these other modes,
  231. or more generally if you would like to move some Org-mode commands to
  232. other keys, set this variable and configure the keys with the variable
  233. `org-disputed-keys'.
  234. This option is only relevant at load-time of Org-mode, and must be set
  235. *before* org.el is loaded. Changing it requires a restart of Emacs to
  236. become effective."
  237. :group 'org-startup
  238. :type 'boolean)
  239. (if (fboundp 'defvaralias)
  240. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  241. (defcustom org-disputed-keys
  242. '(([(shift up)] . [(meta p)])
  243. ([(shift down)] . [(meta n)])
  244. ([(shift left)] . [(meta -)])
  245. ([(shift right)] . [(meta +)])
  246. ([(control shift right)] . [(meta shift +)])
  247. ([(control shift left)] . [(meta shift -)]))
  248. "Keys for which Org-mode and other modes compete.
  249. This is an alist, cars are the default keys, second element specifies
  250. the alternative to use when `org-replace-disputed-keys' is t.
  251. Keys can be specified in any syntax supported by `define-key'.
  252. The value of this option takes effect only at Org-mode's startup,
  253. therefore you'll have to restart Emacs to apply it after changing."
  254. :group 'org-startup
  255. :type 'alist)
  256. (defun org-key (key)
  257. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  258. Or return the original if not disputed."
  259. (if org-replace-disputed-keys
  260. (let* ((nkey (key-description key))
  261. (x (org-find-if (lambda (x)
  262. (equal (key-description (car x)) nkey))
  263. org-disputed-keys)))
  264. (if x (cdr x) key))
  265. key))
  266. (defun org-find-if (predicate seq)
  267. (catch 'exit
  268. (while seq
  269. (if (funcall predicate (car seq))
  270. (throw 'exit (car seq))
  271. (pop seq)))))
  272. (defun org-defkey (keymap key def)
  273. "Define a key, possibly translated, as returned by `org-key'."
  274. (define-key keymap (org-key key) def))
  275. (defcustom org-ellipsis nil
  276. "The ellipsis to use in the Org-mode outline.
  277. When nil, just use the standard three dots. When a string, use that instead,
  278. When a face, use the standart 3 dots, but with the specified face.
  279. The change affects only Org-mode (which will then use its own display table).
  280. Changing this requires executing `M-x org-mode' in a buffer to become
  281. effective."
  282. :group 'org-startup
  283. :type '(choice (const :tag "Default" nil)
  284. (face :tag "Face" :value org-warning)
  285. (string :tag "String" :value "...#")))
  286. (defvar org-display-table nil
  287. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  288. (defgroup org-keywords nil
  289. "Keywords in Org-mode."
  290. :tag "Org Keywords"
  291. :group 'org)
  292. (defcustom org-deadline-string "DEADLINE:"
  293. "String to mark deadline entries.
  294. A deadline is this string, followed by a time stamp. Should be a word,
  295. terminated by a colon. You can insert a schedule keyword and
  296. a timestamp with \\[org-deadline].
  297. Changes become only effective after restarting Emacs."
  298. :group 'org-keywords
  299. :type 'string)
  300. (defcustom org-scheduled-string "SCHEDULED:"
  301. "String to mark scheduled TODO entries.
  302. A schedule is this string, followed by a time stamp. Should be a word,
  303. terminated by a colon. You can insert a schedule keyword and
  304. a timestamp with \\[org-schedule].
  305. Changes become only effective after restarting Emacs."
  306. :group 'org-keywords
  307. :type 'string)
  308. (defcustom org-closed-string "CLOSED:"
  309. "String used as the prefix for timestamps logging closing a TODO entry."
  310. :group 'org-keywords
  311. :type 'string)
  312. (defcustom org-clock-string "CLOCK:"
  313. "String used as prefix for timestamps clocking work hours on an item."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-comment-string "COMMENT"
  317. "Entries starting with this keyword will never be exported.
  318. An entry can be toggled between COMMENT and normal with
  319. \\[org-toggle-comment].
  320. Changes become only effective after restarting Emacs."
  321. :group 'org-keywords
  322. :type 'string)
  323. (defcustom org-quote-string "QUOTE"
  324. "Entries starting with this keyword will be exported in fixed-width font.
  325. Quoting applies only to the text in the entry following the headline, and does
  326. not extend beyond the next headline, even if that is lower level.
  327. An entry can be toggled between QUOTE and normal with
  328. \\[org-toggle-fixed-width-section]."
  329. :group 'org-keywords
  330. :type 'string)
  331. (defconst org-repeat-re
  332. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  333. "Regular expression for specifying repeated events.
  334. After a match, group 1 contains the repeat expression.")
  335. (defgroup org-structure nil
  336. "Options concerning the general structure of Org-mode files."
  337. :tag "Org Structure"
  338. :group 'org)
  339. (defgroup org-reveal-location nil
  340. "Options about how to make context of a location visible."
  341. :tag "Org Reveal Location"
  342. :group 'org-structure)
  343. (defconst org-context-choice
  344. '(choice
  345. (const :tag "Always" t)
  346. (const :tag "Never" nil)
  347. (repeat :greedy t :tag "Individual contexts"
  348. (cons
  349. (choice :tag "Context"
  350. (const agenda)
  351. (const org-goto)
  352. (const occur-tree)
  353. (const tags-tree)
  354. (const link-search)
  355. (const mark-goto)
  356. (const bookmark-jump)
  357. (const isearch)
  358. (const default))
  359. (boolean))))
  360. "Contexts for the reveal options.")
  361. (defcustom org-show-hierarchy-above '((default . t))
  362. "Non-nil means, show full hierarchy when revealing a location.
  363. Org-mode often shows locations in an org-mode file which might have
  364. been invisible before. When this is set, the hierarchy of headings
  365. above the exposed location is shown.
  366. Turning this off for example for sparse trees makes them very compact.
  367. Instead of t, this can also be an alist specifying this option for different
  368. contexts. Valid contexts are
  369. agenda when exposing an entry from the agenda
  370. org-goto when using the command `org-goto' on key C-c C-j
  371. occur-tree when using the command `org-occur' on key C-c /
  372. tags-tree when constructing a sparse tree based on tags matches
  373. link-search when exposing search matches associated with a link
  374. mark-goto when exposing the jump goal of a mark
  375. bookmark-jump when exposing a bookmark location
  376. isearch when exiting from an incremental search
  377. default default for all contexts not set explicitly"
  378. :group 'org-reveal-location
  379. :type org-context-choice)
  380. (defcustom org-show-following-heading '((default . nil))
  381. "Non-nil means, show following heading when revealing a location.
  382. Org-mode often shows locations in an org-mode file which might have
  383. been invisible before. When this is set, the heading following the
  384. match is shown.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-siblings '((default . nil) (isearch t))
  393. "Non-nil means, show all sibling heading when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the sibling of the current entry
  396. heading are all made visible. If `org-show-hierarchy-above' is t,
  397. the same happens on each level of the hierarchy above the current entry.
  398. By default this is on for the isearch context, off for all other contexts.
  399. Turning this off for example for sparse trees makes them very compact,
  400. but makes it harder to edit the location of the match. In such a case,
  401. use the command \\[org-reveal] to show more context.
  402. Instead of t, this can also be an alist specifying this option for different
  403. contexts. See `org-show-hierarchy-above' for valid contexts."
  404. :group 'org-reveal-location
  405. :type org-context-choice)
  406. (defcustom org-show-entry-below '((default . nil))
  407. "Non-nil means, show the entry below a headline when revealing a location.
  408. Org-mode often shows locations in an org-mode file which might have
  409. been invisible before. When this is set, the text below the headline that is
  410. exposed is also shown.
  411. By default this is off for all contexts.
  412. Instead of t, this can also be an alist specifying this option for different
  413. contexts. See `org-show-hierarchy-above' for valid contexts."
  414. :group 'org-reveal-location
  415. :type org-context-choice)
  416. (defcustom org-indirect-buffer-display 'other-window
  417. "How should indirect tree buffers be displayed?
  418. This applies to indirect buffers created with the commands
  419. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  420. Valid values are:
  421. current-window Display in the current window
  422. other-window Just display in another window.
  423. dedicated-frame Create one new frame, and re-use it each time.
  424. new-frame Make a new frame each time. Note that in this case
  425. previously-made indirect buffers are kept, and you need to
  426. kill these buffers yourself."
  427. :group 'org-structure
  428. :group 'org-agenda-windows
  429. :type '(choice
  430. (const :tag "In current window" current-window)
  431. (const :tag "In current frame, other window" other-window)
  432. (const :tag "Each time a new frame" new-frame)
  433. (const :tag "One dedicated frame" dedicated-frame)))
  434. (defgroup org-cycle nil
  435. "Options concerning visibility cycling in Org-mode."
  436. :tag "Org Cycle"
  437. :group 'org-structure)
  438. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  439. "Names of drawers. Drawers are not opened by cycling on the headline above.
  440. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  441. this:
  442. :DRAWERNAME:
  443. .....
  444. :END:
  445. The drawer \"PROPERTIES\" is special for capturing properties through
  446. the property API.
  447. Drawers can be defined on the per-file basis with a line like:
  448. #+DRAWERS: HIDDEN STATE PROPERTIES"
  449. :group 'org-structure
  450. :type '(repeat (string :tag "Drawer Name")))
  451. (defcustom org-cycle-global-at-bob nil
  452. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  453. This makes it possible to do global cycling without having to use S-TAB or
  454. C-u TAB. For this special case to work, the first line of the buffer
  455. must not be a headline - it may be empty ot some other text. When used in
  456. this way, `org-cycle-hook' is disables temporarily, to make sure the
  457. cursor stays at the beginning of the buffer.
  458. When this option is nil, don't do anything special at the beginning
  459. of the buffer."
  460. :group 'org-cycle
  461. :type 'boolean)
  462. (defcustom org-cycle-emulate-tab t
  463. "Where should `org-cycle' emulate TAB.
  464. nil Never
  465. white Only in completely white lines
  466. whitestart Only at the beginning of lines, before the first non-white char
  467. t Everywhere except in headlines
  468. exc-hl-bol Everywhere except at the start of a headline
  469. If TAB is used in a place where it does not emulate TAB, the current subtree
  470. visibility is cycled."
  471. :group 'org-cycle
  472. :type '(choice (const :tag "Never" nil)
  473. (const :tag "Only in completely white lines" white)
  474. (const :tag "Before first char in a line" whitestart)
  475. (const :tag "Everywhere except in headlines" t)
  476. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  477. ))
  478. (defcustom org-cycle-separator-lines 2
  479. "Number of empty lines needed to keep an empty line between collapsed trees.
  480. If you leave an empty line between the end of a subtree and the following
  481. headline, this empty line is hidden when the subtree is folded.
  482. Org-mode will leave (exactly) one empty line visible if the number of
  483. empty lines is equal or larger to the number given in this variable.
  484. So the default 2 means, at least 2 empty lines after the end of a subtree
  485. are needed to produce free space between a collapsed subtree and the
  486. following headline.
  487. Special case: when 0, never leave empty lines in collapsed view."
  488. :group 'org-cycle
  489. :type 'integer)
  490. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  491. org-cycle-hide-drawers
  492. org-cycle-show-empty-lines
  493. org-optimize-window-after-visibility-change)
  494. "Hook that is run after `org-cycle' has changed the buffer visibility.
  495. The function(s) in this hook must accept a single argument which indicates
  496. the new state that was set by the most recent `org-cycle' command. The
  497. argument is a symbol. After a global state change, it can have the values
  498. `overview', `content', or `all'. After a local state change, it can have
  499. the values `folded', `children', or `subtree'."
  500. :group 'org-cycle
  501. :type 'hook)
  502. (defgroup org-edit-structure nil
  503. "Options concerning structure editing in Org-mode."
  504. :tag "Org Edit Structure"
  505. :group 'org-structure)
  506. (defcustom org-odd-levels-only nil
  507. "Non-nil means, skip even levels and only use odd levels for the outline.
  508. This has the effect that two stars are being added/taken away in
  509. promotion/demotion commands. It also influences how levels are
  510. handled by the exporters.
  511. Changing it requires restart of `font-lock-mode' to become effective
  512. for fontification also in regions already fontified.
  513. You may also set this on a per-file basis by adding one of the following
  514. lines to the buffer:
  515. #+STARTUP: odd
  516. #+STARTUP: oddeven"
  517. :group 'org-edit-structure
  518. :group 'org-font-lock
  519. :type 'boolean)
  520. (defcustom org-adapt-indentation t
  521. "Non-nil means, adapt indentation when promoting and demoting.
  522. When this is set and the *entire* text in an entry is indented, the
  523. indentation is increased by one space in a demotion command, and
  524. decreased by one in a promotion command. If any line in the entry
  525. body starts at column 0, indentation is not changed at all."
  526. :group 'org-edit-structure
  527. :type 'boolean)
  528. (defcustom org-special-ctrl-a/e nil
  529. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  530. When t, `C-a' will bring back the cursor to the beginning of the
  531. headline text, i.e. after the stars and after a possible TODO keyword.
  532. In an item, this will be the position after the bullet.
  533. When the cursor is already at that position, another `C-a' will bring
  534. it to the beginning of the line.
  535. `C-e' will jump to the end of the headline, ignoring the presence of tags
  536. in the headline. A second `C-e' will then jump to the true end of the
  537. line, after any tags.
  538. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  539. and only a directly following, identical keypress will bring the cursor
  540. to the special positions."
  541. :group 'org-edit-structure
  542. :type '(choice
  543. (const :tag "off" nil)
  544. (const :tag "after bullet first" t)
  545. (const :tag "border first" reversed)))
  546. (if (fboundp 'defvaralias)
  547. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  548. (defcustom org-special-ctrl-k nil
  549. "Non-nil means `C-k' will behave specially in headlines.
  550. When nil, `C-k' will call the default `kill-line' command.
  551. When t, the following will happen while the cursor is in the headline:
  552. - When the cursor is at the beginning of a headline, kill the entire
  553. line and possible the folded subtree below the line.
  554. - When in the middle of the headline text, kill the headline up to the tags.
  555. - When after the headline text, kill the tags."
  556. :group 'org-edit-structure
  557. :type 'boolean)
  558. (defcustom org-M-RET-may-split-line '((default . t))
  559. "Non-nil means, M-RET will split the line at the cursor position.
  560. When nil, it will go to the end of the line before making a
  561. new line.
  562. You may also set this option in a different way for different
  563. contexts. Valid contexts are:
  564. headline when creating a new headline
  565. item when creating a new item
  566. table in a table field
  567. default the value to be used for all contexts not explicitly
  568. customized"
  569. :group 'org-structure
  570. :group 'org-table
  571. :type '(choice
  572. (const :tag "Always" t)
  573. (const :tag "Never" nil)
  574. (repeat :greedy t :tag "Individual contexts"
  575. (cons
  576. (choice :tag "Context"
  577. (const headline)
  578. (const item)
  579. (const table)
  580. (const default))
  581. (boolean)))))
  582. (defcustom org-blank-before-new-entry '((heading . nil)
  583. (plain-list-item . nil))
  584. "Should `org-insert-heading' leave a blank line before new heading/item?
  585. The value is an alist, with `heading' and `plain-list-item' as car,
  586. and a boolean flag as cdr."
  587. :group 'org-edit-structure
  588. :type '(list
  589. (cons (const heading) (boolean))
  590. (cons (const plain-list-item) (boolean))))
  591. (defcustom org-insert-heading-hook nil
  592. "Hook being run after inserting a new heading."
  593. :group 'org-edit-structure
  594. :type 'hook)
  595. (defcustom org-enable-fixed-width-editor t
  596. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  597. This currently only means, they are never auto-wrapped.
  598. When nil, such lines will be treated like ordinary lines.
  599. See also the QUOTE keyword."
  600. :group 'org-edit-structure
  601. :type 'boolean)
  602. (defcustom org-goto-auto-isearch t
  603. "Non-nil means, typing characters in org-goto starts incremental search."
  604. :group 'org-edit-structure
  605. :type 'boolean)
  606. (defgroup org-sparse-trees nil
  607. "Options concerning sparse trees in Org-mode."
  608. :tag "Org Sparse Trees"
  609. :group 'org-structure)
  610. (defcustom org-highlight-sparse-tree-matches t
  611. "Non-nil means, highlight all matches that define a sparse tree.
  612. The highlights will automatically disappear the next time the buffer is
  613. changed by an edit command."
  614. :group 'org-sparse-trees
  615. :type 'boolean)
  616. (defcustom org-remove-highlights-with-change t
  617. "Non-nil means, any change to the buffer will remove temporary highlights.
  618. Such highlights are created by `org-occur' and `org-clock-display'.
  619. When nil, `C-c C-c needs to be used to get rid of the highlights.
  620. The highlights created by `org-preview-latex-fragment' always need
  621. `C-c C-c' to be removed."
  622. :group 'org-sparse-trees
  623. :group 'org-time
  624. :type 'boolean)
  625. (defcustom org-occur-hook '(org-first-headline-recenter)
  626. "Hook that is run after `org-occur' has constructed a sparse tree.
  627. This can be used to recenter the window to show as much of the structure
  628. as possible."
  629. :group 'org-sparse-trees
  630. :type 'hook)
  631. (defgroup org-plain-lists nil
  632. "Options concerning plain lists in Org-mode."
  633. :tag "Org Plain lists"
  634. :group 'org-structure)
  635. (defcustom org-cycle-include-plain-lists nil
  636. "Non-nil means, include plain lists into visibility cycling.
  637. This means that during cycling, plain list items will *temporarily* be
  638. interpreted as outline headlines with a level given by 1000+i where i is the
  639. indentation of the bullet. In all other operations, plain list items are
  640. not seen as headlines. For example, you cannot assign a TODO keyword to
  641. such an item."
  642. :group 'org-plain-lists
  643. :type 'boolean)
  644. (defcustom org-plain-list-ordered-item-terminator t
  645. "The character that makes a line with leading number an ordered list item.
  646. Valid values are ?. and ?\). To get both terminators, use t. While
  647. ?. may look nicer, it creates the danger that a line with leading
  648. number may be incorrectly interpreted as an item. ?\) therefore is
  649. the safe choice."
  650. :group 'org-plain-lists
  651. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  652. (const :tag "paren like in \"2)\"" ?\))
  653. (const :tab "both" t)))
  654. (defcustom org-empty-line-terminates-plain-lists nil
  655. "Non-nil means, an empty line ends all plain list levels.
  656. When nil, empty lines are part of the preceeding item."
  657. :group 'org-plain-lists
  658. :type 'boolean)
  659. (defcustom org-auto-renumber-ordered-lists t
  660. "Non-nil means, automatically renumber ordered plain lists.
  661. Renumbering happens when the sequence have been changed with
  662. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  663. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  664. :group 'org-plain-lists
  665. :type 'boolean)
  666. (defcustom org-provide-checkbox-statistics t
  667. "Non-nil means, update checkbox statistics after insert and toggle.
  668. When this is set, checkbox statistics is updated each time you either insert
  669. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  670. with \\[org-ctrl-c-ctrl-c\\]."
  671. :group 'org-plain-lists
  672. :type 'boolean)
  673. (defcustom org-description-max-indent 20
  674. "Maximum indentation for the second line of a description list.
  675. When the indentation would be larger than this, it will become
  676. 5 characters instead."
  677. :group 'org-plain-lists
  678. :type 'integer)
  679. (defgroup org-imenu-and-speedbar nil
  680. "Options concerning imenu and speedbar in Org-mode."
  681. :tag "Org Imenu and Speedbar"
  682. :group 'org-structure)
  683. (defcustom org-imenu-depth 2
  684. "The maximum level for Imenu access to Org-mode headlines.
  685. This also applied for speedbar access."
  686. :group 'org-imenu-and-speedbar
  687. :type 'number)
  688. (defgroup org-table nil
  689. "Options concerning tables in Org-mode."
  690. :tag "Org Table"
  691. :group 'org)
  692. (defcustom org-enable-table-editor 'optimized
  693. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  694. When nil, such lines will be treated like ordinary lines.
  695. When equal to the symbol `optimized', the table editor will be optimized to
  696. do the following:
  697. - Automatic overwrite mode in front of whitespace in table fields.
  698. This makes the structure of the table stay in tact as long as the edited
  699. field does not exceed the column width.
  700. - Minimize the number of realigns. Normally, the table is aligned each time
  701. TAB or RET are pressed to move to another field. With optimization this
  702. happens only if changes to a field might have changed the column width.
  703. Optimization requires replacing the functions `self-insert-command',
  704. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  705. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  706. very good at guessing when a re-align will be necessary, but you can always
  707. force one with \\[org-ctrl-c-ctrl-c].
  708. If you would like to use the optimized version in Org-mode, but the
  709. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  710. This variable can be used to turn on and off the table editor during a session,
  711. but in order to toggle optimization, a restart is required.
  712. See also the variable `org-table-auto-blank-field'."
  713. :group 'org-table
  714. :type '(choice
  715. (const :tag "off" nil)
  716. (const :tag "on" t)
  717. (const :tag "on, optimized" optimized)))
  718. (defcustom org-table-tab-recognizes-table.el t
  719. "Non-nil means, TAB will automatically notice a table.el table.
  720. When it sees such a table, it moves point into it and - if necessary -
  721. calls `table-recognize-table'."
  722. :group 'org-table-editing
  723. :type 'boolean)
  724. (defgroup org-link nil
  725. "Options concerning links in Org-mode."
  726. :tag "Org Link"
  727. :group 'org)
  728. (defvar org-link-abbrev-alist-local nil
  729. "Buffer-local version of `org-link-abbrev-alist', which see.
  730. The value of this is taken from the #+LINK lines.")
  731. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  732. (defcustom org-link-abbrev-alist nil
  733. "Alist of link abbreviations.
  734. The car of each element is a string, to be replaced at the start of a link.
  735. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  736. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  737. [[linkkey:tag][description]]
  738. If REPLACE is a string, the tag will simply be appended to create the link.
  739. If the string contains \"%s\", the tag will be inserted there.
  740. REPLACE may also be a function that will be called with the tag as the
  741. only argument to create the link, which should be returned as a string.
  742. See the manual for examples."
  743. :group 'org-link
  744. :type 'alist)
  745. (defcustom org-descriptive-links t
  746. "Non-nil means, hide link part and only show description of bracket links.
  747. Bracket links are like [[link][descritpion]]. This variable sets the initial
  748. state in new org-mode buffers. The setting can then be toggled on a
  749. per-buffer basis from the Org->Hyperlinks menu."
  750. :group 'org-link
  751. :type 'boolean)
  752. (defcustom org-link-file-path-type 'adaptive
  753. "How the path name in file links should be stored.
  754. Valid values are:
  755. relative Relative to the current directory, i.e. the directory of the file
  756. into which the link is being inserted.
  757. absolute Absolute path, if possible with ~ for home directory.
  758. noabbrev Absolute path, no abbreviation of home directory.
  759. adaptive Use relative path for files in the current directory and sub-
  760. directories of it. For other files, use an absolute path."
  761. :group 'org-link
  762. :type '(choice
  763. (const relative)
  764. (const absolute)
  765. (const noabbrev)
  766. (const adaptive)))
  767. (defcustom org-activate-links '(bracket angle plain radio tag date)
  768. "Types of links that should be activated in Org-mode files.
  769. This is a list of symbols, each leading to the activation of a certain link
  770. type. In principle, it does not hurt to turn on most link types - there may
  771. be a small gain when turning off unused link types. The types are:
  772. bracket The recommended [[link][description]] or [[link]] links with hiding.
  773. angular Links in angular brackes that may contain whitespace like
  774. <bbdb:Carsten Dominik>.
  775. plain Plain links in normal text, no whitespace, like http://google.com.
  776. radio Text that is matched by a radio target, see manual for details.
  777. tag Tag settings in a headline (link to tag search).
  778. date Time stamps (link to calendar).
  779. Changing this variable requires a restart of Emacs to become effective."
  780. :group 'org-link
  781. :type '(set (const :tag "Double bracket links (new style)" bracket)
  782. (const :tag "Angular bracket links (old style)" angular)
  783. (const :tag "Plain text links" plain)
  784. (const :tag "Radio target matches" radio)
  785. (const :tag "Tags" tag)
  786. (const :tag "Timestamps" date)))
  787. (defcustom org-make-link-description-function nil
  788. "Function to use to generate link descriptions from links. If
  789. nil the link location will be used. This function must take two
  790. parameters; the first is the link and the second the description
  791. org-insert-link has generated, and should return the description
  792. to use."
  793. :group 'org-link
  794. :type 'function)
  795. (defgroup org-link-store nil
  796. "Options concerning storing links in Org-mode."
  797. :tag "Org Store Link"
  798. :group 'org-link)
  799. (defcustom org-email-link-description-format "Email %c: %.30s"
  800. "Format of the description part of a link to an email or usenet message.
  801. The following %-excapes will be replaced by corresponding information:
  802. %F full \"From\" field
  803. %f name, taken from \"From\" field, address if no name
  804. %T full \"To\" field
  805. %t first name in \"To\" field, address if no name
  806. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  807. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  808. %s subject
  809. %m message-id.
  810. You may use normal field width specification between the % and the letter.
  811. This is for example useful to limit the length of the subject.
  812. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  813. :group 'org-link-store
  814. :type 'string)
  815. (defcustom org-from-is-user-regexp
  816. (let (r1 r2)
  817. (when (and user-mail-address (not (string= user-mail-address "")))
  818. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  819. (when (and user-full-name (not (string= user-full-name "")))
  820. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  821. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  822. "Regexp mached against the \"From:\" header of an email or usenet message.
  823. It should match if the message is from the user him/herself."
  824. :group 'org-link-store
  825. :type 'regexp)
  826. (defcustom org-context-in-file-links t
  827. "Non-nil means, file links from `org-store-link' contain context.
  828. A search string will be added to the file name with :: as separator and
  829. used to find the context when the link is activated by the command
  830. `org-open-at-point'.
  831. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  832. negates this setting for the duration of the command."
  833. :group 'org-link-store
  834. :type 'boolean)
  835. (defcustom org-keep-stored-link-after-insertion nil
  836. "Non-nil means, keep link in list for entire session.
  837. The command `org-store-link' adds a link pointing to the current
  838. location to an internal list. These links accumulate during a session.
  839. The command `org-insert-link' can be used to insert links into any
  840. Org-mode file (offering completion for all stored links). When this
  841. option is nil, every link which has been inserted once using \\[org-insert-link]
  842. will be removed from the list, to make completing the unused links
  843. more efficient."
  844. :group 'org-link-store
  845. :type 'boolean)
  846. (defgroup org-link-follow nil
  847. "Options concerning following links in Org-mode."
  848. :tag "Org Follow Link"
  849. :group 'org-link)
  850. (defcustom org-follow-link-hook nil
  851. "Hook that is run after a link has been followed."
  852. :group 'org-link-follow
  853. :type 'hook)
  854. (defcustom org-tab-follows-link nil
  855. "Non-nil means, on links TAB will follow the link.
  856. Needs to be set before org.el is loaded."
  857. :group 'org-link-follow
  858. :type 'boolean)
  859. (defcustom org-return-follows-link nil
  860. "Non-nil means, on links RET will follow the link.
  861. Needs to be set before org.el is loaded."
  862. :group 'org-link-follow
  863. :type 'boolean)
  864. (defcustom org-mouse-1-follows-link
  865. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  866. "Non-nil means, mouse-1 on a link will follow the link.
  867. A longer mouse click will still set point. Does not work on XEmacs.
  868. Needs to be set before org.el is loaded."
  869. :group 'org-link-follow
  870. :type 'boolean)
  871. (defcustom org-mark-ring-length 4
  872. "Number of different positions to be recorded in the ring
  873. Changing this requires a restart of Emacs to work correctly."
  874. :group 'org-link-follow
  875. :type 'interger)
  876. (defcustom org-link-frame-setup
  877. '((vm . vm-visit-folder-other-frame)
  878. (gnus . gnus-other-frame)
  879. (file . find-file-other-window))
  880. "Setup the frame configuration for following links.
  881. When following a link with Emacs, it may often be useful to display
  882. this link in another window or frame. This variable can be used to
  883. set this up for the different types of links.
  884. For VM, use any of
  885. `vm-visit-folder'
  886. `vm-visit-folder-other-frame'
  887. For Gnus, use any of
  888. `gnus'
  889. `gnus-other-frame'
  890. For FILE, use any of
  891. `find-file'
  892. `find-file-other-window'
  893. `find-file-other-frame'
  894. For the calendar, use the variable `calendar-setup'.
  895. For BBDB, it is currently only possible to display the matches in
  896. another window."
  897. :group 'org-link-follow
  898. :type '(list
  899. (cons (const vm)
  900. (choice
  901. (const vm-visit-folder)
  902. (const vm-visit-folder-other-window)
  903. (const vm-visit-folder-other-frame)))
  904. (cons (const gnus)
  905. (choice
  906. (const gnus)
  907. (const gnus-other-frame)))
  908. (cons (const file)
  909. (choice
  910. (const find-file)
  911. (const find-file-other-window)
  912. (const find-file-other-frame)))))
  913. (defcustom org-display-internal-link-with-indirect-buffer nil
  914. "Non-nil means, use indirect buffer to display infile links.
  915. Activating internal links (from one location in a file to another location
  916. in the same file) normally just jumps to the location. When the link is
  917. activated with a C-u prefix (or with mouse-3), the link is displayed in
  918. another window. When this option is set, the other window actually displays
  919. an indirect buffer clone of the current buffer, to avoid any visibility
  920. changes to the current buffer."
  921. :group 'org-link-follow
  922. :type 'boolean)
  923. (defcustom org-open-non-existing-files nil
  924. "Non-nil means, `org-open-file' will open non-existing files.
  925. When nil, an error will be generated."
  926. :group 'org-link-follow
  927. :type 'boolean)
  928. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  929. "Function and arguments to call for following mailto links.
  930. This is a list with the first element being a lisp function, and the
  931. remaining elements being arguments to the function. In string arguments,
  932. %a will be replaced by the address, and %s will be replaced by the subject
  933. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  934. :group 'org-link-follow
  935. :type '(choice
  936. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  937. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  938. (const :tag "message-mail" (message-mail "%a" "%s"))
  939. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  940. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  941. "Non-nil means, ask for confirmation before executing shell links.
  942. Shell links can be dangerous: just think about a link
  943. [[shell:rm -rf ~/*][Google Search]]
  944. This link would show up in your Org-mode document as \"Google Search\",
  945. but really it would remove your entire home directory.
  946. Therefore we advise against setting this variable to nil.
  947. Just change it to `y-or-n-p' of you want to confirm with a
  948. single keystroke rather than having to type \"yes\"."
  949. :group 'org-link-follow
  950. :type '(choice
  951. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  952. (const :tag "with y-or-n (faster)" y-or-n-p)
  953. (const :tag "no confirmation (dangerous)" nil)))
  954. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  955. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  956. Elisp links can be dangerous: just think about a link
  957. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  958. This link would show up in your Org-mode document as \"Google Search\",
  959. but really it would remove your entire home directory.
  960. Therefore we advise against setting this variable to nil.
  961. Just change it to `y-or-n-p' of you want to confirm with a
  962. single keystroke rather than having to type \"yes\"."
  963. :group 'org-link-follow
  964. :type '(choice
  965. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  966. (const :tag "with y-or-n (faster)" y-or-n-p)
  967. (const :tag "no confirmation (dangerous)" nil)))
  968. (defconst org-file-apps-defaults-gnu
  969. '((remote . emacs)
  970. (t . mailcap))
  971. "Default file applications on a UNIX or GNU/Linux system.
  972. See `org-file-apps'.")
  973. (defconst org-file-apps-defaults-macosx
  974. '((remote . emacs)
  975. (t . "open %s")
  976. ("ps" . "gv %s")
  977. ("ps.gz" . "gv %s")
  978. ("eps" . "gv %s")
  979. ("eps.gz" . "gv %s")
  980. ("dvi" . "xdvi %s")
  981. ("fig" . "xfig %s"))
  982. "Default file applications on a MacOS X system.
  983. The system \"open\" is known as a default, but we use X11 applications
  984. for some files for which the OS does not have a good default.
  985. See `org-file-apps'.")
  986. (defconst org-file-apps-defaults-windowsnt
  987. (list
  988. '(remote . emacs)
  989. (cons t
  990. (list (if (featurep 'xemacs)
  991. 'mswindows-shell-execute
  992. 'w32-shell-execute)
  993. "open" 'file)))
  994. "Default file applications on a Windows NT system.
  995. The system \"open\" is used for most files.
  996. See `org-file-apps'.")
  997. (defcustom org-file-apps
  998. '(
  999. ("txt" . emacs)
  1000. ("tex" . emacs)
  1001. ("ltx" . emacs)
  1002. ("org" . emacs)
  1003. ("el" . emacs)
  1004. ("bib" . emacs)
  1005. )
  1006. "External applications for opening `file:path' items in a document.
  1007. Org-mode uses system defaults for different file types, but
  1008. you can use this variable to set the application for a given file
  1009. extension. The entries in this list are cons cells where the car identifies
  1010. files and the cdr the corresponding command. Possible values for the
  1011. file identifier are
  1012. \"ext\" A string identifying an extension
  1013. `directory' Matches a directory
  1014. `remote' Matches a remote file, accessible through tramp or efs.
  1015. Remote files most likely should be visited through Emacs
  1016. because external applications cannot handle such paths.
  1017. t Default for all remaining files
  1018. Possible values for the command are:
  1019. `emacs' The file will be visited by the current Emacs process.
  1020. `default' Use the default application for this file type.
  1021. string A command to be executed by a shell; %s will be replaced
  1022. by the path to the file.
  1023. sexp A Lisp form which will be evaluated. The file path will
  1024. be available in the Lisp variable `file'.
  1025. For more examples, see the system specific constants
  1026. `org-file-apps-defaults-macosx'
  1027. `org-file-apps-defaults-windowsnt'
  1028. `org-file-apps-defaults-gnu'."
  1029. :group 'org-link-follow
  1030. :type '(repeat
  1031. (cons (choice :value ""
  1032. (string :tag "Extension")
  1033. (const :tag "Default for unrecognized files" t)
  1034. (const :tag "Remote file" remote)
  1035. (const :tag "Links to a directory" directory))
  1036. (choice :value ""
  1037. (const :tag "Visit with Emacs" emacs)
  1038. (const :tag "Use system default" default)
  1039. (string :tag "Command")
  1040. (sexp :tag "Lisp form")))))
  1041. (defgroup org-refile nil
  1042. "Options concerning refiling entries in Org-mode."
  1043. :tag "Org Remember"
  1044. :group 'org)
  1045. (defcustom org-directory "~/org"
  1046. "Directory with org files.
  1047. This directory will be used as default to prompt for org files.
  1048. Used by the hooks for remember.el."
  1049. :group 'org-refile
  1050. :group 'org-remember
  1051. :type 'directory)
  1052. (defcustom org-default-notes-file "~/.notes"
  1053. "Default target for storing notes.
  1054. Used by the hooks for remember.el. This can be a string, or nil to mean
  1055. the value of `remember-data-file'.
  1056. You can set this on a per-template basis with the variable
  1057. `org-remember-templates'."
  1058. :group 'org-refile
  1059. :group 'org-remember
  1060. :type '(choice
  1061. (const :tag "Default from remember-data-file" nil)
  1062. file))
  1063. (defcustom org-goto-interface 'outline
  1064. "The default interface to be used for `org-goto'.
  1065. Allowed vaues are:
  1066. outline The interface shows an outline of the relevant file
  1067. and the correct heading is found by moving through
  1068. the outline or by searching with incremental search.
  1069. outline-path-completion Headlines in the current buffer are offered via
  1070. completion."
  1071. :group 'org-refile
  1072. :type '(choice
  1073. (const :tag "Outline" outline)
  1074. (const :tag "Outline-path-completion" outline-path-completion)))
  1075. (defcustom org-reverse-note-order nil
  1076. "Non-nil means, store new notes at the beginning of a file or entry.
  1077. When nil, new notes will be filed to the end of a file or entry.
  1078. This can also be a list with cons cells of regular expressions that
  1079. are matched against file names, and values."
  1080. :group 'org-remember
  1081. :type '(choice
  1082. (const :tag "Reverse always" t)
  1083. (const :tag "Reverse never" nil)
  1084. (repeat :tag "By file name regexp"
  1085. (cons regexp boolean))))
  1086. (defcustom org-refile-targets nil
  1087. "Targets for refiling entries with \\[org-refile].
  1088. This is list of cons cells. Each cell contains:
  1089. - a specification of the files to be considered, either a list of files,
  1090. or a symbol whose function or variable value will be used to retrieve
  1091. a file name or a list of file names. Nil means, refile to a different
  1092. heading in the current buffer.
  1093. - A specification of how to find candidate refile targets. This may be
  1094. any of
  1095. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1096. This tag has to be present in all target headlines, inheritance will
  1097. not be considered.
  1098. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1099. todo keyword.
  1100. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1101. headlines that are refiling targets.
  1102. - a cons cell (:level . N). Any headline of level N is considered a target.
  1103. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1104. :group 'org-remember
  1105. :type '(repeat
  1106. (cons
  1107. (choice :value org-agenda-files
  1108. (const :tag "All agenda files" org-agenda-files)
  1109. (const :tag "Current buffer" nil)
  1110. (function) (variable) (file))
  1111. (choice :tag "Identify target headline by"
  1112. (cons :tag "Specific tag" (const :tag) (string))
  1113. (cons :tag "TODO keyword" (const :todo) (string))
  1114. (cons :tag "Regular expression" (const :regexp) (regexp))
  1115. (cons :tag "Level number" (const :level) (integer))
  1116. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1117. (defcustom org-refile-use-outline-path nil
  1118. "Non-nil means, provide refile targets as paths.
  1119. So a level 3 headline will be available as level1/level2/level3.
  1120. When the value is `file', also include the file name (without directory)
  1121. into the path. When `full-file-path', include the full file path."
  1122. :group 'org-remember
  1123. :type '(choice
  1124. (const :tag "Not" nil)
  1125. (const :tag "Yes" t)
  1126. (const :tag "Start with file name" file)
  1127. (const :tag "Start with full file path" full-file-path)))
  1128. (defgroup org-todo nil
  1129. "Options concerning TODO items in Org-mode."
  1130. :tag "Org TODO"
  1131. :group 'org)
  1132. (defgroup org-progress nil
  1133. "Options concerning Progress logging in Org-mode."
  1134. :tag "Org Progress"
  1135. :group 'org-time)
  1136. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1137. "List of TODO entry keyword sequences and their interpretation.
  1138. \\<org-mode-map>This is a list of sequences.
  1139. Each sequence starts with a symbol, either `sequence' or `type',
  1140. indicating if the keywords should be interpreted as a sequence of
  1141. action steps, or as different types of TODO items. The first
  1142. keywords are states requiring action - these states will select a headline
  1143. for inclusion into the global TODO list Org-mode produces. If one of
  1144. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1145. signify that no further action is necessary. If \"|\" is not found,
  1146. the last keyword is treated as the only DONE state of the sequence.
  1147. The command \\[org-todo] cycles an entry through these states, and one
  1148. additional state where no keyword is present. For details about this
  1149. cycling, see the manual.
  1150. TODO keywords and interpretation can also be set on a per-file basis with
  1151. the special #+SEQ_TODO and #+TYP_TODO lines.
  1152. Each keyword can optionally specify a character for fast state selection
  1153. \(in combination with the variable `org-use-fast-todo-selection')
  1154. and specifiers for state change logging, using the same syntax
  1155. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1156. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1157. indicates to record a time stamp each time this state is selected.
  1158. Each keyword may also specify if a timestamp or a note should be
  1159. recorded when entering or leaving the state, by adding additional
  1160. characters in the parenthesis after the keyword. This looks like this:
  1161. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1162. record only the time of the state change. With X and Y being either
  1163. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1164. Y when leaving the state if and only if the *target* state does not
  1165. define X. You may omit any of the fast-selection key or X or /Y,
  1166. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1167. For backward compatibility, this variable may also be just a list
  1168. of keywords - in this case the interptetation (sequence or type) will be
  1169. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1170. :group 'org-todo
  1171. :group 'org-keywords
  1172. :type '(choice
  1173. (repeat :tag "Old syntax, just keywords"
  1174. (string :tag "Keyword"))
  1175. (repeat :tag "New syntax"
  1176. (cons
  1177. (choice
  1178. :tag "Interpretation"
  1179. (const :tag "Sequence (cycling hits every state)" sequence)
  1180. (const :tag "Type (cycling directly to DONE)" type))
  1181. (repeat
  1182. (string :tag "Keyword"))))))
  1183. (defvar org-todo-keywords-1 nil
  1184. "All TODO and DONE keywords active in a buffer.")
  1185. (make-variable-buffer-local 'org-todo-keywords-1)
  1186. (defvar org-todo-keywords-for-agenda nil)
  1187. (defvar org-done-keywords-for-agenda nil)
  1188. (defvar org-agenda-contributing-files nil)
  1189. (defvar org-not-done-keywords nil)
  1190. (make-variable-buffer-local 'org-not-done-keywords)
  1191. (defvar org-done-keywords nil)
  1192. (make-variable-buffer-local 'org-done-keywords)
  1193. (defvar org-todo-heads nil)
  1194. (make-variable-buffer-local 'org-todo-heads)
  1195. (defvar org-todo-sets nil)
  1196. (make-variable-buffer-local 'org-todo-sets)
  1197. (defvar org-todo-log-states nil)
  1198. (make-variable-buffer-local 'org-todo-log-states)
  1199. (defvar org-todo-kwd-alist nil)
  1200. (make-variable-buffer-local 'org-todo-kwd-alist)
  1201. (defvar org-todo-key-alist nil)
  1202. (make-variable-buffer-local 'org-todo-key-alist)
  1203. (defvar org-todo-key-trigger nil)
  1204. (make-variable-buffer-local 'org-todo-key-trigger)
  1205. (defcustom org-todo-interpretation 'sequence
  1206. "Controls how TODO keywords are interpreted.
  1207. This variable is in principle obsolete and is only used for
  1208. backward compatibility, if the interpretation of todo keywords is
  1209. not given already in `org-todo-keywords'. See that variable for
  1210. more information."
  1211. :group 'org-todo
  1212. :group 'org-keywords
  1213. :type '(choice (const sequence)
  1214. (const type)))
  1215. (defcustom org-use-fast-todo-selection 'prefix
  1216. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1217. This variable describes if and under what circumstances the cycling
  1218. mechanism for TODO keywords will be replaced by a single-key, direct
  1219. selection scheme.
  1220. When nil, fast selection is never used.
  1221. When the symbol `prefix', it will be used when `org-todo' is called with
  1222. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1223. in an agenda buffer.
  1224. When t, fast selection is used by default. In this case, the prefix
  1225. argument forces cycling instead.
  1226. In all cases, the special interface is only used if access keys have actually
  1227. been assigned by the user, i.e. if keywords in the configuration are followed
  1228. by a letter in parenthesis, like TODO(t)."
  1229. :group 'org-todo
  1230. :type '(choice
  1231. (const :tag "Never" nil)
  1232. (const :tag "By default" t)
  1233. (const :tag "Only with C-u C-c C-t" prefix)))
  1234. (defcustom org-provide-todo-statistics t
  1235. "Non-nil means, update todo statistics after insert and toggle.
  1236. When this is set, todo statistics is updated in the parent of the current
  1237. entry each time a todo state is changed."
  1238. :group 'org-todo
  1239. :type 'boolean)
  1240. (defcustom org-after-todo-state-change-hook nil
  1241. "Hook which is run after the state of a TODO item was changed.
  1242. The new state (a string with a TODO keyword, or nil) is available in the
  1243. Lisp variable `state'."
  1244. :group 'org-todo
  1245. :type 'hook)
  1246. (defcustom org-log-done nil
  1247. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1248. When equal to the list (done), also prompt for a closing note.
  1249. This can also be configured on a per-file basis by adding one of
  1250. the following lines anywhere in the buffer:
  1251. #+STARTUP: logdone
  1252. #+STARTUP: lognotedone
  1253. #+STARTUP: nologdone"
  1254. :group 'org-todo
  1255. :group 'org-progress
  1256. :type '(choice
  1257. (const :tag "No logging" nil)
  1258. (const :tag "Record CLOSED timestamp" time)
  1259. (const :tag "Record CLOSED timestamp with closing note." note)))
  1260. ;; Normalize old uses of org-log-done.
  1261. (cond
  1262. ((eq org-log-done t) (setq org-log-done 'time))
  1263. ((and (listp org-log-done) (memq 'done org-log-done))
  1264. (setq org-log-done 'note)))
  1265. (defcustom org-log-note-clock-out nil
  1266. "Non-nil means, recored a note when clocking out of an item.
  1267. This can also be configured on a per-file basis by adding one of
  1268. the following lines anywhere in the buffer:
  1269. #+STARTUP: lognoteclock-out
  1270. #+STARTUP: nolognoteclock-out"
  1271. :group 'org-todo
  1272. :group 'org-progress
  1273. :type 'boolean)
  1274. (defcustom org-log-done-with-time t
  1275. "Non-nil means, the CLOSED time stamp will contain date and time.
  1276. When nil, only the date will be recorded."
  1277. :group 'org-progress
  1278. :type 'boolean)
  1279. (defcustom org-log-note-headings
  1280. '((done . "CLOSING NOTE %t")
  1281. (state . "State %-12s %t")
  1282. (note . "Note taken on %t")
  1283. (clock-out . ""))
  1284. "Headings for notes added to entries.
  1285. The value is an alist, with the car being a symbol indicating the note
  1286. context, and the cdr is the heading to be used. The heading may also be the
  1287. empty string.
  1288. %t in the heading will be replaced by a time stamp.
  1289. %s will be replaced by the new TODO state, in double quotes.
  1290. %u will be replaced by the user name.
  1291. %U will be replaced by the full user name."
  1292. :group 'org-todo
  1293. :group 'org-progress
  1294. :type '(list :greedy t
  1295. (cons (const :tag "Heading when closing an item" done) string)
  1296. (cons (const :tag
  1297. "Heading when changing todo state (todo sequence only)"
  1298. state) string)
  1299. (cons (const :tag "Heading when just taking a note" note) string)
  1300. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1301. (unless (assq 'note org-log-note-headings)
  1302. (push '(note . "%t") org-log-note-headings))
  1303. (defcustom org-log-states-order-reversed t
  1304. "Non-nil means, the latest state change note will be directly after heading.
  1305. When nil, the notes will be orderer according to time."
  1306. :group 'org-todo
  1307. :group 'org-progress
  1308. :type 'boolean)
  1309. (defcustom org-log-repeat 'time
  1310. "Non-nil means, record moving through the DONE state when triggering repeat.
  1311. An auto-repeating tasks is immediately switched back to TODO when marked
  1312. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1313. the TODO keyword definition, or recording a closing note by setting
  1314. `org-log-done', there will be no record of the task moving through DONE.
  1315. This variable forces taking a note anyway. Possible values are:
  1316. nil Don't force a record
  1317. time Record a time stamp
  1318. note Record a note
  1319. This option can also be set with on a per-file-basis with
  1320. #+STARTUP: logrepeat
  1321. #+STARTUP: lognoterepeat
  1322. #+STARTUP: nologrepeat
  1323. You can have local logging settings for a subtree by setting the LOGGING
  1324. property to one or more of these keywords."
  1325. :group 'org-todo
  1326. :group 'org-progress
  1327. :type '(choice
  1328. (const :tag "Don't force a record" nil)
  1329. (const :tag "Force recording the DONE state" time)
  1330. (const :tag "Force recording a note with the DONE state" note)))
  1331. (defgroup org-priorities nil
  1332. "Priorities in Org-mode."
  1333. :tag "Org Priorities"
  1334. :group 'org-todo)
  1335. (defcustom org-highest-priority ?A
  1336. "The highest priority of TODO items. A character like ?A, ?B etc.
  1337. Must have a smaller ASCII number than `org-lowest-priority'."
  1338. :group 'org-priorities
  1339. :type 'character)
  1340. (defcustom org-lowest-priority ?C
  1341. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1342. Must have a larger ASCII number than `org-highest-priority'."
  1343. :group 'org-priorities
  1344. :type 'character)
  1345. (defcustom org-default-priority ?B
  1346. "The default priority of TODO items.
  1347. This is the priority an item get if no explicit priority is given."
  1348. :group 'org-priorities
  1349. :type 'character)
  1350. (defcustom org-priority-start-cycle-with-default t
  1351. "Non-nil means, start with default priority when starting to cycle.
  1352. When this is nil, the first step in the cycle will be (depending on the
  1353. command used) one higher or lower that the default priority."
  1354. :group 'org-priorities
  1355. :type 'boolean)
  1356. (defgroup org-time nil
  1357. "Options concerning time stamps and deadlines in Org-mode."
  1358. :tag "Org Time"
  1359. :group 'org)
  1360. (defcustom org-insert-labeled-timestamps-at-point nil
  1361. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1362. When nil, these labeled time stamps are forces into the second line of an
  1363. entry, just after the headline. When scheduling from the global TODO list,
  1364. the time stamp will always be forced into the second line."
  1365. :group 'org-time
  1366. :type 'boolean)
  1367. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1368. "Formats for `format-time-string' which are used for time stamps.
  1369. It is not recommended to change this constant.")
  1370. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1371. "Number of minutes to round time stamps to.
  1372. These are two values, the first applies when first creating a time stamp.
  1373. The second applies when changing it with the commands `S-up' and `S-down'.
  1374. When changing the time stamp, this means that it will change in steps
  1375. of N minutes, as given by the second value.
  1376. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1377. numbers should be factors of 60, so for example 5, 10, 15.
  1378. When this is larger than 1, you can still force an exact time-stamp by using
  1379. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1380. and by using a prefix arg to `S-up/down' to specify the exact number
  1381. of minutes to shift."
  1382. :group 'org-time
  1383. :get '(lambda (var) ; Make sure all entries have 5 elements
  1384. (if (integerp (default-value var))
  1385. (list (default-value var) 5)
  1386. (default-value var)))
  1387. :type '(list
  1388. (integer :tag "when inserting times")
  1389. (integer :tag "when modifying times")))
  1390. ;; Normalize old customizations of this variable.
  1391. (when (integerp org-time-stamp-rounding-minutes)
  1392. (setq org-time-stamp-rounding-minutes
  1393. (list org-time-stamp-rounding-minutes
  1394. org-time-stamp-rounding-minutes)))
  1395. (defcustom org-display-custom-times nil
  1396. "Non-nil means, overlay custom formats over all time stamps.
  1397. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1398. To turn this on on a per-file basis, insert anywhere in the file:
  1399. #+STARTUP: customtime"
  1400. :group 'org-time
  1401. :set 'set-default
  1402. :type 'sexp)
  1403. (make-variable-buffer-local 'org-display-custom-times)
  1404. (defcustom org-time-stamp-custom-formats
  1405. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1406. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1407. These are overlayed over the default ISO format if the variable
  1408. `org-display-custom-times' is set. Time like %H:%M should be at the
  1409. end of the second format."
  1410. :group 'org-time
  1411. :type 'sexp)
  1412. (defun org-time-stamp-format (&optional long inactive)
  1413. "Get the right format for a time string."
  1414. (let ((f (if long (cdr org-time-stamp-formats)
  1415. (car org-time-stamp-formats))))
  1416. (if inactive
  1417. (concat "[" (substring f 1 -1) "]")
  1418. f)))
  1419. (defcustom org-time-clocksum-format "%d:%02d"
  1420. "The format string used when creating CLOCKSUM lines, or when
  1421. org-mode generates a time duration."
  1422. :group 'org-time
  1423. :type 'string)
  1424. (defcustom org-deadline-warning-days 14
  1425. "No. of days before expiration during which a deadline becomes active.
  1426. This variable governs the display in sparse trees and in the agenda.
  1427. When 0 or negative, it means use this number (the absolute value of it)
  1428. even if a deadline has a different individual lead time specified."
  1429. :group 'org-time
  1430. :group 'org-agenda-daily/weekly
  1431. :type 'number)
  1432. (defcustom org-read-date-prefer-future t
  1433. "Non-nil means, assume future for incomplete date input from user.
  1434. This affects the following situations:
  1435. 1. The user gives a day, but no month.
  1436. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1437. read this as the third of *next* month. However, if you enter \"17\",
  1438. it will be considered as *this* month.
  1439. 2. The user gives a month but not a year.
  1440. For example, if it is april and you enter \"feb 2\", this will be read
  1441. as feb 2, *next* year. \"May 5\", however, will be this year.
  1442. Currently this does not work for ISO week specifications.
  1443. When this option is nil, the current month and year will always be used
  1444. as defaults."
  1445. :group 'org-time
  1446. :type 'boolean)
  1447. (defcustom org-read-date-display-live t
  1448. "Non-nil means, display current interpretation of date prompt live.
  1449. This display will be in an overlay, in the minibuffer."
  1450. :group 'org-time
  1451. :type 'boolean)
  1452. (defcustom org-read-date-popup-calendar t
  1453. "Non-nil means, pop up a calendar when prompting for a date.
  1454. In the calendar, the date can be selected with mouse-1. However, the
  1455. minibuffer will also be active, and you can simply enter the date as well.
  1456. When nil, only the minibuffer will be available."
  1457. :group 'org-time
  1458. :type 'boolean)
  1459. (if (fboundp 'defvaralias)
  1460. (defvaralias 'org-popup-calendar-for-date-prompt
  1461. 'org-read-date-popup-calendar))
  1462. (defcustom org-extend-today-until 0
  1463. "The hour when your day really ends.
  1464. This has influence for the following applications:
  1465. - When switching the agenda to \"today\". It it is still earlier than
  1466. the time given here, the day recognized as TODAY is actually yesterday.
  1467. - When a date is read from the user and it is still before the time given
  1468. here, the current date and time will be assumed to be yesterday, 23:59.
  1469. FIXME:
  1470. IMPORTANT: This is still a very experimental feature, it may disappear
  1471. again or it may be extended to mean more things."
  1472. :group 'org-time
  1473. :type 'number)
  1474. (defcustom org-edit-timestamp-down-means-later nil
  1475. "Non-nil means, S-down will increase the time in a time stamp.
  1476. When nil, S-up will increase."
  1477. :group 'org-time
  1478. :type 'boolean)
  1479. (defcustom org-calendar-follow-timestamp-change t
  1480. "Non-nil means, make the calendar window follow timestamp changes.
  1481. When a timestamp is modified and the calendar window is visible, it will be
  1482. moved to the new date."
  1483. :group 'org-time
  1484. :type 'boolean)
  1485. (defgroup org-tags nil
  1486. "Options concerning tags in Org-mode."
  1487. :tag "Org Tags"
  1488. :group 'org)
  1489. (defcustom org-tag-alist nil
  1490. "List of tags allowed in Org-mode files.
  1491. When this list is nil, Org-mode will base TAG input on what is already in the
  1492. buffer.
  1493. The value of this variable is an alist, the car of each entry must be a
  1494. keyword as a string, the cdr may be a character that is used to select
  1495. that tag through the fast-tag-selection interface.
  1496. See the manual for details."
  1497. :group 'org-tags
  1498. :type '(repeat
  1499. (choice
  1500. (cons (string :tag "Tag name")
  1501. (character :tag "Access char"))
  1502. (const :tag "Start radio group" (:startgroup))
  1503. (const :tag "End radio group" (:endgroup)))))
  1504. (defvar org-file-tags nil
  1505. "List of tags that can be inherited by all entries in the file.
  1506. The tags will be inherited if the variable `org-use-tag-inheritance'
  1507. says they should be.
  1508. This variable is populated from #+TAG lines.")
  1509. (defcustom org-use-fast-tag-selection 'auto
  1510. "Non-nil means, use fast tag selection scheme.
  1511. This is a special interface to select and deselect tags with single keys.
  1512. When nil, fast selection is never used.
  1513. When the symbol `auto', fast selection is used if and only if selection
  1514. characters for tags have been configured, either through the variable
  1515. `org-tag-alist' or through a #+TAGS line in the buffer.
  1516. When t, fast selection is always used and selection keys are assigned
  1517. automatically if necessary."
  1518. :group 'org-tags
  1519. :type '(choice
  1520. (const :tag "Always" t)
  1521. (const :tag "Never" nil)
  1522. (const :tag "When selection characters are configured" 'auto)))
  1523. (defcustom org-fast-tag-selection-single-key nil
  1524. "Non-nil means, fast tag selection exits after first change.
  1525. When nil, you have to press RET to exit it.
  1526. During fast tag selection, you can toggle this flag with `C-c'.
  1527. This variable can also have the value `expert'. In this case, the window
  1528. displaying the tags menu is not even shown, until you press C-c again."
  1529. :group 'org-tags
  1530. :type '(choice
  1531. (const :tag "No" nil)
  1532. (const :tag "Yes" t)
  1533. (const :tag "Expert" expert)))
  1534. (defvar org-fast-tag-selection-include-todo nil
  1535. "Non-nil means, fast tags selection interface will also offer TODO states.
  1536. This is an undocumented feature, you should not rely on it.")
  1537. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1538. "The column to which tags should be indented in a headline.
  1539. If this number is positive, it specifies the column. If it is negative,
  1540. it means that the tags should be flushright to that column. For example,
  1541. -80 works well for a normal 80 character screen."
  1542. :group 'org-tags
  1543. :type 'integer)
  1544. (defcustom org-auto-align-tags t
  1545. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1546. These operations change the length of a headline and therefore shift
  1547. the tags around. With this options turned on, after each such operation
  1548. the tags are again aligned to `org-tags-column'."
  1549. :group 'org-tags
  1550. :type 'boolean)
  1551. (defcustom org-use-tag-inheritance t
  1552. "Non-nil means, tags in levels apply also for sublevels.
  1553. When nil, only the tags directly given in a specific line apply there.
  1554. If this option is t, a match early-on in a tree can lead to a large
  1555. number of matches in the subtree. If you only want to see the first
  1556. match in a tree during a search, check out the variable
  1557. `org-tags-match-list-sublevels'.
  1558. This may also be a list of tags that should be inherited, or a regexp that
  1559. matches tags that should be inherited."
  1560. :group 'org-tags
  1561. :type '(choice
  1562. (const :tag "Not" nil)
  1563. (const :tag "Always" t)
  1564. (repeat :tag "Specific tags" (string :tag "Tag"))
  1565. (regexp :tag "Tags matched by regexp")))
  1566. (defun org-tag-inherit-p (tag)
  1567. "Check if TAG is one that should be inherited."
  1568. (cond
  1569. ((eq org-use-tag-inheritance t) t)
  1570. ((not org-use-tag-inheritance) nil)
  1571. ((stringp org-use-tag-inheritance)
  1572. (string-match org-use-tag-inheritance tag))
  1573. ((listp org-use-tag-inheritance)
  1574. (member tag org-use-tag-inheritance))
  1575. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1576. (defcustom org-tags-match-list-sublevels t
  1577. "Non-nil means list also sublevels of headlines matching tag search.
  1578. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1579. the sublevels of a headline matching a tag search often also match
  1580. the same search. Listing all of them can create very long lists.
  1581. Setting this variable to nil causes subtrees of a match to be skipped.
  1582. This option is off by default, because inheritance in on. If you turn
  1583. inheritance off, you very likely want to turn this option on.
  1584. As a special case, if the tag search is restricted to TODO items, the
  1585. value of this variable is ignored and sublevels are always checked, to
  1586. make sure all corresponding TODO items find their way into the list."
  1587. :group 'org-tags
  1588. :type 'boolean)
  1589. (defvar org-tags-history nil
  1590. "History of minibuffer reads for tags.")
  1591. (defvar org-last-tags-completion-table nil
  1592. "The last used completion table for tags.")
  1593. (defvar org-after-tags-change-hook nil
  1594. "Hook that is run after the tags in a line have changed.")
  1595. (defgroup org-properties nil
  1596. "Options concerning properties in Org-mode."
  1597. :tag "Org Properties"
  1598. :group 'org)
  1599. (defcustom org-property-format "%-10s %s"
  1600. "How property key/value pairs should be formatted by `indent-line'.
  1601. When `indent-line' hits a property definition, it will format the line
  1602. according to this format, mainly to make sure that the values are
  1603. lined-up with respect to each other."
  1604. :group 'org-properties
  1605. :type 'string)
  1606. (defcustom org-use-property-inheritance nil
  1607. "Non-nil means, properties apply also for sublevels.
  1608. This setting is chiefly used during property searches. Turning it on can
  1609. cause significant overhead when doing a search, which is why it is not
  1610. on by default.
  1611. When nil, only the properties directly given in the current entry count.
  1612. When t, every property is inherited. The value may also be a list of
  1613. properties that should have inheritance, or a regular expression matching
  1614. properties that should be inherited.
  1615. However, note that some special properties use inheritance under special
  1616. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1617. and the properties ending in \"_ALL\" when they are used as descriptor
  1618. for valid values of a property.
  1619. Note for programmers:
  1620. When querying an entry with `org-entry-get', you can control if inheritance
  1621. should be used. By default, `org-entry-get' looks only at the local
  1622. properties. You can request inheritance by setting the inherit argument
  1623. to t (to force inheritance) or to `selective' (to respect the setting
  1624. in this variable)."
  1625. :group 'org-properties
  1626. :type '(choice
  1627. (const :tag "Not" nil)
  1628. (const :tag "Always" t)
  1629. (repeat :tag "Specific properties" (string :tag "Property"))
  1630. (regexp :tag "Properties matched by regexp")))
  1631. (defun org-property-inherit-p (property)
  1632. "Check if PROPERTY is one that should be inherited."
  1633. (cond
  1634. ((eq org-use-property-inheritance t) t)
  1635. ((not org-use-property-inheritance) nil)
  1636. ((stringp org-use-property-inheritance)
  1637. (string-match org-use-property-inheritance property))
  1638. ((listp org-use-property-inheritance)
  1639. (member property org-use-property-inheritance))
  1640. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1641. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1642. "The default column format, if no other format has been defined.
  1643. This variable can be set on the per-file basis by inserting a line
  1644. #+COLUMNS: %25ITEM ....."
  1645. :group 'org-properties
  1646. :type 'string)
  1647. (defcustom org-columns-ellipses ".."
  1648. "The ellipses to be used when a field in column view is truncated.
  1649. When this is the empty string, as many characters as possible are shown,
  1650. but then there will be no visual indication that the field has been truncated.
  1651. When this is a string of length N, the last N characters of a truncated
  1652. field are replaced by this string. If the column is narrower than the
  1653. ellipses string, only part of the ellipses string will be shown."
  1654. :group 'org-properties
  1655. :type 'string)
  1656. (defcustom org-effort-property "Effort"
  1657. "The property that is being used to keep track of effort estimates.
  1658. Effort estimates given in this property need to have the format H:MM."
  1659. :group 'org-properties
  1660. :group 'org-progress
  1661. :type '(string :tag "Property"))
  1662. (defconst org-global-properties-fixed
  1663. '(("VISIBILITY_ALL" . "folded children content all"))
  1664. "List of property/value pairs that can be inherited by any entry.
  1665. These are fixed values, for the preset properties.")
  1666. (defcustom org-global-properties nil
  1667. "List of property/value pairs that can be inherited by any entry.
  1668. You can set buffer-local values for this by adding lines like
  1669. #+PROPERTY: NAME VALUE"
  1670. :group 'org-properties
  1671. :type '(repeat
  1672. (cons (string :tag "Property")
  1673. (string :tag "Value"))))
  1674. (defvar org-file-properties nil
  1675. "List of property/value pairs that can be inherited by any entry.
  1676. Valid for the current buffer.
  1677. This variable is populated from #+PROPERTY lines.")
  1678. (make-variable-buffer-local 'org-file-properties)
  1679. (defgroup org-agenda nil
  1680. "Options concerning agenda views in Org-mode."
  1681. :tag "Org Agenda"
  1682. :group 'org)
  1683. (defvar org-category nil
  1684. "Variable used by org files to set a category for agenda display.
  1685. Such files should use a file variable to set it, for example
  1686. # -*- mode: org; org-category: \"ELisp\"
  1687. or contain a special line
  1688. #+CATEGORY: ELisp
  1689. If the file does not specify a category, then file's base name
  1690. is used instead.")
  1691. (make-variable-buffer-local 'org-category)
  1692. (defcustom org-agenda-files nil
  1693. "The files to be used for agenda display.
  1694. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1695. \\[org-remove-file]. You can also use customize to edit the list.
  1696. If an entry is a directory, all files in that directory that are matched by
  1697. `org-agenda-file-regexp' will be part of the file list.
  1698. If the value of the variable is not a list but a single file name, then
  1699. the list of agenda files is actually stored and maintained in that file, one
  1700. agenda file per line."
  1701. :group 'org-agenda
  1702. :type '(choice
  1703. (repeat :tag "List of files and directories" file)
  1704. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1705. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1706. "Regular expression to match files for `org-agenda-files'.
  1707. If any element in the list in that variable contains a directory instead
  1708. of a normal file, all files in that directory that are matched by this
  1709. regular expression will be included."
  1710. :group 'org-agenda
  1711. :type 'regexp)
  1712. (defcustom org-agenda-text-search-extra-files nil
  1713. "List of extra files to be searched by text search commands.
  1714. These files will be search in addition to the agenda files by the
  1715. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1716. Note that these files will only be searched for text search commands,
  1717. not for the other agenda views like todo lists, tag searches or the weekly
  1718. agenda. This variable is intended to list notes and possibly archive files
  1719. that should also be searched by these two commands.
  1720. In fact, if the first element in the list is the symbol `agenda-archives',
  1721. than all archive files of all agenda files will be added to the search
  1722. scope."
  1723. :group 'org-agenda
  1724. :type '(set :greedy t
  1725. (const :tag "Agenda Archives" agenda-archives)
  1726. (repeat :inline t (file))))
  1727. (if (fboundp 'defvaralias)
  1728. (defvaralias 'org-agenda-multi-occur-extra-files
  1729. 'org-agenda-text-search-extra-files))
  1730. (defcustom org-agenda-skip-unavailable-files nil
  1731. "t means to just skip non-reachable files in `org-agenda-files'.
  1732. Nil means to remove them, after a query, from the list."
  1733. :group 'org-agenda
  1734. :type 'boolean)
  1735. (defcustom org-calendar-to-agenda-key [?c]
  1736. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1737. The command `org-calendar-goto-agenda' will be bound to this key. The
  1738. default is the character `c' because then `c' can be used to switch back and
  1739. forth between agenda and calendar."
  1740. :group 'org-agenda
  1741. :type 'sexp)
  1742. (defcustom org-calendar-agenda-action-key [?k]
  1743. "The key to be installed in `calendar-mode-map' for agenda-action.
  1744. The command `org-agenda-action' will be bound to this key. The
  1745. default is the character `k' because we use the same key in the agenda."
  1746. :group 'org-agenda
  1747. :type 'sexp)
  1748. (eval-after-load "calendar"
  1749. '(progn
  1750. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1751. 'org-calendar-goto-agenda)
  1752. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1753. 'org-agenda-action)))
  1754. (defgroup org-latex nil
  1755. "Options for embedding LaTeX code into Org-mode."
  1756. :tag "Org LaTeX"
  1757. :group 'org)
  1758. (defcustom org-format-latex-options
  1759. '(:foreground default :background default :scale 1.0
  1760. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1761. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1762. "Options for creating images from LaTeX fragments.
  1763. This is a property list with the following properties:
  1764. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1765. `default' means use the foreground of the default face.
  1766. :background the background color, or \"Transparent\".
  1767. `default' means use the background of the default face.
  1768. :scale a scaling factor for the size of the images.
  1769. :html-foreground, :html-background, :html-scale
  1770. the same numbers for HTML export.
  1771. :matchers a list indicating which matchers should be used to
  1772. find LaTeX fragments. Valid members of this list are:
  1773. \"begin\" find environments
  1774. \"$\" find math expressions surrounded by $...$
  1775. \"$$\" find math expressions surrounded by $$....$$
  1776. \"\\(\" find math expressions surrounded by \\(...\\)
  1777. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1778. :group 'org-latex
  1779. :type 'plist)
  1780. (defcustom org-format-latex-header "\\documentclass{article}
  1781. \\usepackage{fullpage} % do not remove
  1782. \\usepackage{amssymb}
  1783. \\usepackage[usenames]{color}
  1784. \\usepackage{amsmath}
  1785. \\usepackage{latexsym}
  1786. \\usepackage[mathscr]{eucal}
  1787. \\pagestyle{empty} % do not remove"
  1788. "The document header used for processing LaTeX fragments."
  1789. :group 'org-latex
  1790. :type 'string)
  1791. (defgroup org-font-lock nil
  1792. "Font-lock settings for highlighting in Org-mode."
  1793. :tag "Org Font Lock"
  1794. :group 'org)
  1795. (defcustom org-level-color-stars-only nil
  1796. "Non-nil means fontify only the stars in each headline.
  1797. When nil, the entire headline is fontified.
  1798. Changing it requires restart of `font-lock-mode' to become effective
  1799. also in regions already fontified."
  1800. :group 'org-font-lock
  1801. :type 'boolean)
  1802. (defcustom org-hide-leading-stars nil
  1803. "Non-nil means, hide the first N-1 stars in a headline.
  1804. This works by using the face `org-hide' for these stars. This
  1805. face is white for a light background, and black for a dark
  1806. background. You may have to customize the face `org-hide' to
  1807. make this work.
  1808. Changing it requires restart of `font-lock-mode' to become effective
  1809. also in regions already fontified.
  1810. You may also set this on a per-file basis by adding one of the following
  1811. lines to the buffer:
  1812. #+STARTUP: hidestars
  1813. #+STARTUP: showstars"
  1814. :group 'org-font-lock
  1815. :type 'boolean)
  1816. (defcustom org-fontify-done-headline nil
  1817. "Non-nil means, change the face of a headline if it is marked DONE.
  1818. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1819. When this is non-nil, the headline after the keyword is set to the
  1820. `org-headline-done' as an additional indication."
  1821. :group 'org-font-lock
  1822. :type 'boolean)
  1823. (defcustom org-fontify-emphasized-text t
  1824. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1825. Changing this variable requires a restart of Emacs to take effect."
  1826. :group 'org-font-lock
  1827. :type 'boolean)
  1828. (defcustom org-highlight-latex-fragments-and-specials nil
  1829. "Non-nil means, fontify what is treated specially by the exporters."
  1830. :group 'org-font-lock
  1831. :type 'boolean)
  1832. (defcustom org-hide-emphasis-markers nil
  1833. "Non-nil mean font-lock should hide the emphasis marker characters."
  1834. :group 'org-font-lock
  1835. :type 'boolean)
  1836. (defvar org-emph-re nil
  1837. "Regular expression for matching emphasis.")
  1838. (defvar org-verbatim-re nil
  1839. "Regular expression for matching verbatim text.")
  1840. (defvar org-emphasis-regexp-components) ; defined just below
  1841. (defvar org-emphasis-alist) ; defined just below
  1842. (defun org-set-emph-re (var val)
  1843. "Set variable and compute the emphasis regular expression."
  1844. (set var val)
  1845. (when (and (boundp 'org-emphasis-alist)
  1846. (boundp 'org-emphasis-regexp-components)
  1847. org-emphasis-alist org-emphasis-regexp-components)
  1848. (let* ((e org-emphasis-regexp-components)
  1849. (pre (car e))
  1850. (post (nth 1 e))
  1851. (border (nth 2 e))
  1852. (body (nth 3 e))
  1853. (nl (nth 4 e))
  1854. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1855. (body1 (concat body "*?"))
  1856. (markers (mapconcat 'car org-emphasis-alist ""))
  1857. (vmarkers (mapconcat
  1858. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1859. org-emphasis-alist "")))
  1860. ;; make sure special characters appear at the right position in the class
  1861. (if (string-match "\\^" markers)
  1862. (setq markers (concat (replace-match "" t t markers) "^")))
  1863. (if (string-match "-" markers)
  1864. (setq markers (concat (replace-match "" t t markers) "-")))
  1865. (if (string-match "\\^" vmarkers)
  1866. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1867. (if (string-match "-" vmarkers)
  1868. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1869. (if (> nl 0)
  1870. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1871. (int-to-string nl) "\\}")))
  1872. ;; Make the regexp
  1873. (setq org-emph-re
  1874. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1875. "\\("
  1876. "\\([" markers "]\\)"
  1877. "\\("
  1878. "[^" border "]\\|"
  1879. "[^" border (if (and nil stacked) markers) "]"
  1880. body1
  1881. "[^" border (if (and nil stacked) markers) "]"
  1882. "\\)"
  1883. "\\3\\)"
  1884. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1885. (setq org-verbatim-re
  1886. (concat "\\([" pre "]\\|^\\)"
  1887. "\\("
  1888. "\\([" vmarkers "]\\)"
  1889. "\\("
  1890. "[^" border "]\\|"
  1891. "[^" border "]"
  1892. body1
  1893. "[^" border "]"
  1894. "\\)"
  1895. "\\3\\)"
  1896. "\\([" post "]\\|$\\)")))))
  1897. (defcustom org-emphasis-regexp-components
  1898. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1899. "Components used to build the regular expression for emphasis.
  1900. This is a list with 6 entries. Terminology: In an emphasis string
  1901. like \" *strong word* \", we call the initial space PREMATCH, the final
  1902. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1903. and \"trong wor\" is the body. The different components in this variable
  1904. specify what is allowed/forbidden in each part:
  1905. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1906. post Chars allowed as postmatch. End of line will be allowed too.
  1907. border The chars *forbidden* as border characters.
  1908. body-regexp A regexp like \".\" to match a body character. Don't use
  1909. non-shy groups here, and don't allow newline here.
  1910. newline The maximum number of newlines allowed in an emphasis exp.
  1911. Use customize to modify this, or restart Emacs after changing it."
  1912. :group 'org-font-lock
  1913. :set 'org-set-emph-re
  1914. :type '(list
  1915. (sexp :tag "Allowed chars in pre ")
  1916. (sexp :tag "Allowed chars in post ")
  1917. (sexp :tag "Forbidden chars in border ")
  1918. (sexp :tag "Regexp for body ")
  1919. (integer :tag "number of newlines allowed")
  1920. (option (boolean :tag "Please ignore this button"))))
  1921. (defcustom org-emphasis-alist
  1922. `(("*" bold "<b>" "</b>")
  1923. ("/" italic "<i>" "</i>")
  1924. ("_" underline "<u>" "</u>")
  1925. ("=" org-code "<code>" "</code>" verbatim)
  1926. ("~" org-verbatim "" "" verbatim)
  1927. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1928. "<del>" "</del>")
  1929. )
  1930. "Special syntax for emphasized text.
  1931. Text starting and ending with a special character will be emphasized, for
  1932. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1933. characters, the face to be used by font-lock for highlighting in Org-mode
  1934. Emacs buffers, and the HTML tags to be used for this.
  1935. Use customize to modify this, or restart Emacs after changing it."
  1936. :group 'org-font-lock
  1937. :set 'org-set-emph-re
  1938. :type '(repeat
  1939. (list
  1940. (string :tag "Marker character")
  1941. (choice
  1942. (face :tag "Font-lock-face")
  1943. (plist :tag "Face property list"))
  1944. (string :tag "HTML start tag")
  1945. (string :tag "HTML end tag")
  1946. (option (const verbatim)))))
  1947. ;;; Miscellaneous options
  1948. (defgroup org-completion nil
  1949. "Completion in Org-mode."
  1950. :tag "Org Completion"
  1951. :group 'org)
  1952. (defcustom org-completion-fallback-command 'hippie-expand
  1953. "The expansion command called by \\[org-complete] in normal context.
  1954. Normal means, no org-mode-specific context."
  1955. :group 'org-completion
  1956. :type 'function)
  1957. ;;; Functions and variables from ther packages
  1958. ;; Declared here to avoid compiler warnings
  1959. ;; XEmacs only
  1960. (defvar outline-mode-menu-heading)
  1961. (defvar outline-mode-menu-show)
  1962. (defvar outline-mode-menu-hide)
  1963. (defvar zmacs-regions) ; XEmacs regions
  1964. ;; Emacs only
  1965. (defvar mark-active)
  1966. ;; Various packages
  1967. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1968. (declare-function calendar-forward-day "cal-move" (arg))
  1969. (declare-function calendar-goto-date "cal-move" (date))
  1970. (declare-function calendar-goto-today "cal-move" ())
  1971. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1972. (defvar calc-embedded-close-formula)
  1973. (defvar calc-embedded-open-formula)
  1974. (declare-function cdlatex-tab "ext:cdlatex" ())
  1975. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1976. (defvar font-lock-unfontify-region-function)
  1977. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1978. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1979. (defvar iswitchb-temp-buflist)
  1980. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1981. (declare-function org-agenda-skip "org-agenda" ())
  1982. (declare-function org-format-agenda-item "org-agenda"
  1983. (extra txt &optional category tags dotime noprefix remove-re))
  1984. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1985. (declare-function org-agenda-change-all-lines "org-agenda"
  1986. (newhead hdmarker &optional fixface))
  1987. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1988. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1989. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1990. (beg end))
  1991. (declare-function parse-time-string "parse-time" (string))
  1992. (declare-function remember "remember" (&optional initial))
  1993. (declare-function remember-buffer-desc "remember" ())
  1994. (declare-function remember-finalize "remember" ())
  1995. (defvar remember-save-after-remembering)
  1996. (defvar remember-data-file)
  1997. (defvar remember-register)
  1998. (defvar remember-buffer)
  1999. (defvar remember-handler-functions)
  2000. (defvar remember-annotation-functions)
  2001. (defvar texmathp-why)
  2002. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2003. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2004. (defvar w3m-current-url)
  2005. (defvar w3m-current-title)
  2006. (defvar org-latex-regexps)
  2007. ;;; Autoload and prepare some org modules
  2008. ;; Some table stuff that needs to be defined here, because it is used
  2009. ;; by the functions setting up org-mode or checking for table context.
  2010. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2011. "Detects an org-type or table-type table.")
  2012. (defconst org-table-line-regexp "^[ \t]*|"
  2013. "Detects an org-type table line.")
  2014. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2015. "Detects an org-type table line.")
  2016. (defconst org-table-hline-regexp "^[ \t]*|-"
  2017. "Detects an org-type table hline.")
  2018. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2019. "Detects a table-type table hline.")
  2020. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2021. "Searching from within a table (any type) this finds the first line
  2022. outside the table.")
  2023. ;; Autoload the functions in org-table.el that are needed by functions here.
  2024. (eval-and-compile
  2025. (org-autoload "org-table"
  2026. '(org-table-align org-table-begin org-table-blank-field
  2027. org-table-convert org-table-convert-region org-table-copy-down
  2028. org-table-copy-region org-table-create
  2029. org-table-create-or-convert-from-region
  2030. org-table-create-with-table.el org-table-current-dline
  2031. org-table-cut-region org-table-delete-column org-table-edit-field
  2032. org-table-edit-formulas org-table-end org-table-eval-formula
  2033. org-table-export org-table-field-info
  2034. org-table-get-stored-formulas org-table-goto-column
  2035. org-table-hline-and-move org-table-import org-table-insert-column
  2036. org-table-insert-hline org-table-insert-row org-table-iterate
  2037. org-table-justify-field-maybe org-table-kill-row
  2038. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2039. org-table-move-column org-table-move-column-left
  2040. org-table-move-column-right org-table-move-row
  2041. org-table-move-row-down org-table-move-row-up
  2042. org-table-next-field org-table-next-row org-table-paste-rectangle
  2043. org-table-previous-field org-table-recalculate
  2044. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2045. org-table-toggle-coordinate-overlays
  2046. org-table-toggle-formula-debugger org-table-wrap-region
  2047. orgtbl-mode turn-on-orgtbl)))
  2048. (defun org-at-table-p (&optional table-type)
  2049. "Return t if the cursor is inside an org-type table.
  2050. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2051. (if org-enable-table-editor
  2052. (save-excursion
  2053. (beginning-of-line 1)
  2054. (looking-at (if table-type org-table-any-line-regexp
  2055. org-table-line-regexp)))
  2056. nil))
  2057. (defsubst org-table-p () (org-at-table-p))
  2058. (defun org-at-table.el-p ()
  2059. "Return t if and only if we are at a table.el table."
  2060. (and (org-at-table-p 'any)
  2061. (save-excursion
  2062. (goto-char (org-table-begin 'any))
  2063. (looking-at org-table1-hline-regexp))))
  2064. (defun org-table-recognize-table.el ()
  2065. "If there is a table.el table nearby, recognize it and move into it."
  2066. (if org-table-tab-recognizes-table.el
  2067. (if (org-at-table.el-p)
  2068. (progn
  2069. (beginning-of-line 1)
  2070. (if (looking-at org-table-dataline-regexp)
  2071. nil
  2072. (if (looking-at org-table1-hline-regexp)
  2073. (progn
  2074. (beginning-of-line 2)
  2075. (if (looking-at org-table-any-border-regexp)
  2076. (beginning-of-line -1)))))
  2077. (if (re-search-forward "|" (org-table-end t) t)
  2078. (progn
  2079. (require 'table)
  2080. (if (table--at-cell-p (point))
  2081. t
  2082. (message "recognizing table.el table...")
  2083. (table-recognize-table)
  2084. (message "recognizing table.el table...done")))
  2085. (error "This should not happen..."))
  2086. t)
  2087. nil)
  2088. nil))
  2089. (defun org-at-table-hline-p ()
  2090. "Return t if the cursor is inside a hline in a table."
  2091. (if org-enable-table-editor
  2092. (save-excursion
  2093. (beginning-of-line 1)
  2094. (looking-at org-table-hline-regexp))
  2095. nil))
  2096. (defvar org-table-clean-did-remove-column nil)
  2097. (defun org-table-map-tables (function)
  2098. "Apply FUNCTION to the start of all tables in the buffer."
  2099. (save-excursion
  2100. (save-restriction
  2101. (widen)
  2102. (goto-char (point-min))
  2103. (while (re-search-forward org-table-any-line-regexp nil t)
  2104. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2105. (beginning-of-line 1)
  2106. (if (looking-at org-table-line-regexp)
  2107. (save-excursion (funcall function)))
  2108. (re-search-forward org-table-any-border-regexp nil 1))))
  2109. (message "Mapping tables: done"))
  2110. ;; Declare and autoload functions from org-exp.el
  2111. (declare-function org-default-export-plist "org-exp")
  2112. (declare-function org-infile-export-plist "org-exp")
  2113. (declare-function org-get-current-options "org-exp")
  2114. (eval-and-compile
  2115. (org-autoload "org-exp"
  2116. '(org-export org-export-as-ascii org-export-visible
  2117. org-insert-export-options-template org-export-as-html-and-open
  2118. org-export-as-html-batch org-export-as-html-to-buffer
  2119. org-replace-region-by-html org-export-region-as-html
  2120. org-export-as-html org-export-icalendar-this-file
  2121. org-export-icalendar-all-agenda-files
  2122. org-table-clean-before-export
  2123. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2124. ;; Declare and autoload functions from org-exp.el
  2125. (eval-and-compile
  2126. (org-autoload "org-exp"
  2127. '(org-agenda org-agenda-list org-search-view
  2128. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2129. org-diary org-agenda-to-appt)))
  2130. ;; Autoload org-remember
  2131. (eval-and-compile
  2132. (org-autoload "org-remember"
  2133. '(org-remember-insinuate org-remember-annotation
  2134. org-remember-apply-template org-remember org-remember-handler)))
  2135. ;; Autoload org-clock.el
  2136. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2137. (beg end))
  2138. (declare-function org-update-mode-line "org-clock" ())
  2139. (defvar org-clock-start-time)
  2140. (defvar org-clock-marker (make-marker)
  2141. "Marker recording the last clock-in.")
  2142. (eval-and-compile
  2143. (org-autoload
  2144. "org-clock"
  2145. '(org-clock-in org-clock-out org-clock-cancel
  2146. org-clock-goto org-clock-sum org-clock-display
  2147. org-remove-clock-overlays org-clock-report
  2148. org-clocktable-shift org-dblock-write:clocktable
  2149. org-get-clocktable)))
  2150. (defun org-clock-update-time-maybe ()
  2151. "If this is a CLOCK line, update it and return t.
  2152. Otherwise, return nil."
  2153. (interactive)
  2154. (save-excursion
  2155. (beginning-of-line 1)
  2156. (skip-chars-forward " \t")
  2157. (when (looking-at org-clock-string)
  2158. (let ((re (concat "[ \t]*" org-clock-string
  2159. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2160. "\\([ \t]*=>.*\\)?\\)?"))
  2161. ts te h m s)
  2162. (cond
  2163. ((not (looking-at re))
  2164. nil)
  2165. ((not (match-end 2))
  2166. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2167. (> org-clock-marker (point))
  2168. (<= org-clock-marker (point-at-eol)))
  2169. ;; The clock is running here
  2170. (setq org-clock-start-time
  2171. (apply 'encode-time
  2172. (org-parse-time-string (match-string 1))))
  2173. (org-update-mode-line)))
  2174. (t
  2175. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2176. (end-of-line 1)
  2177. (setq ts (match-string 1)
  2178. te (match-string 3))
  2179. (setq s (- (time-to-seconds
  2180. (apply 'encode-time (org-parse-time-string te)))
  2181. (time-to-seconds
  2182. (apply 'encode-time (org-parse-time-string ts))))
  2183. h (floor (/ s 3600))
  2184. s (- s (* 3600 h))
  2185. m (floor (/ s 60))
  2186. s (- s (* 60 s)))
  2187. (insert " => " (format "%2d:%02d" h m))
  2188. t))))))
  2189. (defun org-check-running-clock ()
  2190. "Check if the current buffer contains the running clock.
  2191. If yes, offer to stop it and to save the buffer with the changes."
  2192. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2193. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2194. (buffer-name))))
  2195. (org-clock-out)
  2196. (when (y-or-n-p "Save changed buffer?")
  2197. (save-buffer))))
  2198. (defun org-clocktable-try-shift (dir n)
  2199. "Check if this line starts a clock table, if yes, shift the time block."
  2200. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2201. (org-clocktable-shift dir n)))
  2202. ;; Autoload archiving code
  2203. ;; The stuff that is needed for cycling and tags has to be defined here.
  2204. (defgroup org-archive nil
  2205. "Options concerning archiving in Org-mode."
  2206. :tag "Org Archive"
  2207. :group 'org-structure)
  2208. (defcustom org-archive-location "%s_archive::"
  2209. "The location where subtrees should be archived.
  2210. Otherwise, the value of this variable is a string, consisting of two
  2211. parts, separated by a double-colon.
  2212. The first part is a file name - when omitted, archiving happens in the same
  2213. file. %s will be replaced by the current file name (without directory part).
  2214. Archiving to a different file is useful to keep archived entries from
  2215. contributing to the Org-mode Agenda.
  2216. The part after the double colon is a headline. The archived entries will be
  2217. filed under that headline. When omitted, the subtrees are simply filed away
  2218. at the end of the file, as top-level entries.
  2219. Here are a few examples:
  2220. \"%s_archive::\"
  2221. If the current file is Projects.org, archive in file
  2222. Projects.org_archive, as top-level trees. This is the default.
  2223. \"::* Archived Tasks\"
  2224. Archive in the current file, under the top-level headline
  2225. \"* Archived Tasks\".
  2226. \"~/org/archive.org::\"
  2227. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2228. \"basement::** Finished Tasks\"
  2229. Archive in file ./basement (relative path), as level 3 trees
  2230. below the level 2 heading \"** Finished Tasks\".
  2231. You may set this option on a per-file basis by adding to the buffer a
  2232. line like
  2233. #+ARCHIVE: basement::** Finished Tasks
  2234. You may also define it locally for a subtree by setting an ARCHIVE property
  2235. in the entry. If such a property is found in an entry, or anywhere up
  2236. the hierarchy, it will be used."
  2237. :group 'org-archive
  2238. :type 'string)
  2239. (defcustom org-archive-tag "ARCHIVE"
  2240. "The tag that marks a subtree as archived.
  2241. An archived subtree does not open during visibility cycling, and does
  2242. not contribute to the agenda listings.
  2243. After changing this, font-lock must be restarted in the relevant buffers to
  2244. get the proper fontification."
  2245. :group 'org-archive
  2246. :group 'org-keywords
  2247. :type 'string)
  2248. (defcustom org-agenda-skip-archived-trees t
  2249. "Non-nil means, the agenda will skip any items located in archived trees.
  2250. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2251. variable is no longer recommended, you should leave it at the value t.
  2252. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2253. :group 'org-archive
  2254. :group 'org-agenda-skip
  2255. :type 'boolean)
  2256. (defcustom org-cycle-open-archived-trees nil
  2257. "Non-nil means, `org-cycle' will open archived trees.
  2258. An archived tree is a tree marked with the tag ARCHIVE.
  2259. When nil, archived trees will stay folded. You can still open them with
  2260. normal outline commands like `show-all', but not with the cycling commands."
  2261. :group 'org-archive
  2262. :group 'org-cycle
  2263. :type 'boolean)
  2264. (defcustom org-sparse-tree-open-archived-trees nil
  2265. "Non-nil means sparse tree construction shows matches in archived trees.
  2266. When nil, matches in these trees are highlighted, but the trees are kept in
  2267. collapsed state."
  2268. :group 'org-archive
  2269. :group 'org-sparse-trees
  2270. :type 'boolean)
  2271. (defun org-cycle-hide-archived-subtrees (state)
  2272. "Re-hide all archived subtrees after a visibility state change."
  2273. (when (and (not org-cycle-open-archived-trees)
  2274. (not (memq state '(overview folded))))
  2275. (save-excursion
  2276. (let* ((globalp (memq state '(contents all)))
  2277. (beg (if globalp (point-min) (point)))
  2278. (end (if globalp (point-max) (org-end-of-subtree t))))
  2279. (org-hide-archived-subtrees beg end)
  2280. (goto-char beg)
  2281. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2282. (message "%s" (substitute-command-keys
  2283. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2284. (defun org-force-cycle-archived ()
  2285. "Cycle subtree even if it is archived."
  2286. (interactive)
  2287. (setq this-command 'org-cycle)
  2288. (let ((org-cycle-open-archived-trees t))
  2289. (call-interactively 'org-cycle)))
  2290. (defun org-hide-archived-subtrees (beg end)
  2291. "Re-hide all archived subtrees after a visibility state change."
  2292. (save-excursion
  2293. (let* ((re (concat ":" org-archive-tag ":")))
  2294. (goto-char beg)
  2295. (while (re-search-forward re end t)
  2296. (and (org-on-heading-p) (hide-subtree))
  2297. (org-end-of-subtree t)))))
  2298. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2299. (eval-and-compile
  2300. (org-autoload "org-archive"
  2301. '(org-add-archive-files org-archive-subtree
  2302. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2303. ;; Autoload Column View Code
  2304. (declare-function org-columns-number-to-string "org-colview")
  2305. (declare-function org-columns-get-format-and-top-level "org-colview")
  2306. (declare-function org-columns-compute "org-colview")
  2307. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2308. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2309. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2310. org-columns org-insert-columns-dblock))
  2311. ;; Autoload ID code
  2312. (org-autoload "org-id"
  2313. '(org-id-get-create org-id-new org-id-copy org-id-get
  2314. org-id-get-with-outline-path-completion
  2315. org-id-get-with-outline-drilling
  2316. org-id-goto org-id-find))
  2317. ;;; Variables for pre-computed regular expressions, all buffer local
  2318. (defvar org-drawer-regexp nil
  2319. "Matches first line of a hidden block.")
  2320. (make-variable-buffer-local 'org-drawer-regexp)
  2321. (defvar org-todo-regexp nil
  2322. "Matches any of the TODO state keywords.")
  2323. (make-variable-buffer-local 'org-todo-regexp)
  2324. (defvar org-not-done-regexp nil
  2325. "Matches any of the TODO state keywords except the last one.")
  2326. (make-variable-buffer-local 'org-not-done-regexp)
  2327. (defvar org-todo-line-regexp nil
  2328. "Matches a headline and puts TODO state into group 2 if present.")
  2329. (make-variable-buffer-local 'org-todo-line-regexp)
  2330. (defvar org-complex-heading-regexp nil
  2331. "Matches a headline and puts everything into groups:
  2332. group 1: the stars
  2333. group 2: The todo keyword, maybe
  2334. group 3: Priority cookie
  2335. group 4: True headline
  2336. group 5: Tags")
  2337. (make-variable-buffer-local 'org-complex-heading-regexp)
  2338. (defvar org-todo-line-tags-regexp nil
  2339. "Matches a headline and puts TODO state into group 2 if present.
  2340. Also put tags into group 4 if tags are present.")
  2341. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2342. (defvar org-nl-done-regexp nil
  2343. "Matches newline followed by a headline with the DONE keyword.")
  2344. (make-variable-buffer-local 'org-nl-done-regexp)
  2345. (defvar org-looking-at-done-regexp nil
  2346. "Matches the DONE keyword a point.")
  2347. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2348. (defvar org-ds-keyword-length 12
  2349. "Maximum length of the Deadline and SCHEDULED keywords.")
  2350. (make-variable-buffer-local 'org-ds-keyword-length)
  2351. (defvar org-deadline-regexp nil
  2352. "Matches the DEADLINE keyword.")
  2353. (make-variable-buffer-local 'org-deadline-regexp)
  2354. (defvar org-deadline-time-regexp nil
  2355. "Matches the DEADLINE keyword together with a time stamp.")
  2356. (make-variable-buffer-local 'org-deadline-time-regexp)
  2357. (defvar org-deadline-line-regexp nil
  2358. "Matches the DEADLINE keyword and the rest of the line.")
  2359. (make-variable-buffer-local 'org-deadline-line-regexp)
  2360. (defvar org-scheduled-regexp nil
  2361. "Matches the SCHEDULED keyword.")
  2362. (make-variable-buffer-local 'org-scheduled-regexp)
  2363. (defvar org-scheduled-time-regexp nil
  2364. "Matches the SCHEDULED keyword together with a time stamp.")
  2365. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2366. (defvar org-closed-time-regexp nil
  2367. "Matches the CLOSED keyword together with a time stamp.")
  2368. (make-variable-buffer-local 'org-closed-time-regexp)
  2369. (defvar org-keyword-time-regexp nil
  2370. "Matches any of the 4 keywords, together with the time stamp.")
  2371. (make-variable-buffer-local 'org-keyword-time-regexp)
  2372. (defvar org-keyword-time-not-clock-regexp nil
  2373. "Matches any of the 3 keywords, together with the time stamp.")
  2374. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2375. (defvar org-maybe-keyword-time-regexp nil
  2376. "Matches a timestamp, possibly preceeded by a keyword.")
  2377. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2378. (defvar org-planning-or-clock-line-re nil
  2379. "Matches a line with planning or clock info.")
  2380. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2381. (defconst org-plain-time-of-day-regexp
  2382. (concat
  2383. "\\(\\<[012]?[0-9]"
  2384. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2385. "\\(--?"
  2386. "\\(\\<[012]?[0-9]"
  2387. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2388. "\\)?")
  2389. "Regular expression to match a plain time or time range.
  2390. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2391. groups carry important information:
  2392. 0 the full match
  2393. 1 the first time, range or not
  2394. 8 the second time, if it is a range.")
  2395. (defconst org-plain-time-extension-regexp
  2396. (concat
  2397. "\\(\\<[012]?[0-9]"
  2398. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2399. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2400. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2401. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2402. groups carry important information:
  2403. 0 the full match
  2404. 7 hours of duration
  2405. 9 minutes of duration")
  2406. (defconst org-stamp-time-of-day-regexp
  2407. (concat
  2408. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2409. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2410. "\\(--?"
  2411. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2412. "Regular expression to match a timestamp time or time range.
  2413. After a match, the following groups carry important information:
  2414. 0 the full match
  2415. 1 date plus weekday, for backreferencing to make sure both times on same day
  2416. 2 the first time, range or not
  2417. 4 the second time, if it is a range.")
  2418. (defconst org-startup-options
  2419. '(("fold" org-startup-folded t)
  2420. ("overview" org-startup-folded t)
  2421. ("nofold" org-startup-folded nil)
  2422. ("showall" org-startup-folded nil)
  2423. ("content" org-startup-folded content)
  2424. ("hidestars" org-hide-leading-stars t)
  2425. ("showstars" org-hide-leading-stars nil)
  2426. ("odd" org-odd-levels-only t)
  2427. ("oddeven" org-odd-levels-only nil)
  2428. ("align" org-startup-align-all-tables t)
  2429. ("noalign" org-startup-align-all-tables nil)
  2430. ("customtime" org-display-custom-times t)
  2431. ("logdone" org-log-done time)
  2432. ("lognotedone" org-log-done note)
  2433. ("nologdone" org-log-done nil)
  2434. ("lognoteclock-out" org-log-note-clock-out t)
  2435. ("nolognoteclock-out" org-log-note-clock-out nil)
  2436. ("logrepeat" org-log-repeat state)
  2437. ("lognoterepeat" org-log-repeat note)
  2438. ("nologrepeat" org-log-repeat nil)
  2439. ("constcgs" constants-unit-system cgs)
  2440. ("constSI" constants-unit-system SI))
  2441. "Variable associated with STARTUP options for org-mode.
  2442. Each element is a list of three items: The startup options as written
  2443. in the #+STARTUP line, the corresponding variable, and the value to
  2444. set this variable to if the option is found. An optional forth element PUSH
  2445. means to push this value onto the list in the variable.")
  2446. (defun org-set-regexps-and-options ()
  2447. "Precompute regular expressions for current buffer."
  2448. (when (org-mode-p)
  2449. (org-set-local 'org-todo-kwd-alist nil)
  2450. (org-set-local 'org-todo-key-alist nil)
  2451. (org-set-local 'org-todo-key-trigger nil)
  2452. (org-set-local 'org-todo-keywords-1 nil)
  2453. (org-set-local 'org-done-keywords nil)
  2454. (org-set-local 'org-todo-heads nil)
  2455. (org-set-local 'org-todo-sets nil)
  2456. (org-set-local 'org-todo-log-states nil)
  2457. (org-set-local 'org-file-properties nil)
  2458. (org-set-local 'org-file-tags nil)
  2459. (let ((re (org-make-options-regexp
  2460. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2461. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2462. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2463. (splitre "[ \t]+")
  2464. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2465. tail sep kws1 prio props ftags drawers
  2466. ext-setup-or-nil setup-contents (start 0))
  2467. (save-excursion
  2468. (save-restriction
  2469. (widen)
  2470. (goto-char (point-min))
  2471. (while (or (and ext-setup-or-nil
  2472. (string-match re ext-setup-or-nil start)
  2473. (setq start (match-end 0)))
  2474. (and (setq ext-setup-or-nil nil start 0)
  2475. (re-search-forward re nil t)))
  2476. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2477. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2478. (cond
  2479. ((equal key "CATEGORY")
  2480. (if (string-match "[ \t]+$" value)
  2481. (setq value (replace-match "" t t value)))
  2482. (setq cat value))
  2483. ((member key '("SEQ_TODO" "TODO"))
  2484. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2485. ((equal key "TYP_TODO")
  2486. (push (cons 'type (org-split-string value splitre)) kwds))
  2487. ((equal key "TAGS")
  2488. (setq tags (append tags (org-split-string value splitre))))
  2489. ((equal key "COLUMNS")
  2490. (org-set-local 'org-columns-default-format value))
  2491. ((equal key "LINK")
  2492. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2493. (push (cons (match-string 1 value)
  2494. (org-trim (match-string 2 value)))
  2495. links)))
  2496. ((equal key "PRIORITIES")
  2497. (setq prio (org-split-string value " +")))
  2498. ((equal key "PROPERTY")
  2499. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2500. (push (cons (match-string 1 value) (match-string 2 value))
  2501. props)))
  2502. ((equal key "FILETAGS")
  2503. (when (string-match "\\S-" value)
  2504. (setq ftags
  2505. (append
  2506. ftags
  2507. (apply 'append
  2508. (mapcar (lambda (x) (org-split-string x ":"))
  2509. (org-split-string value)))))))
  2510. ((equal key "DRAWERS")
  2511. (setq drawers (org-split-string value splitre)))
  2512. ((equal key "CONSTANTS")
  2513. (setq const (append const (org-split-string value splitre))))
  2514. ((equal key "STARTUP")
  2515. (let ((opts (org-split-string value splitre))
  2516. l var val)
  2517. (while (setq l (pop opts))
  2518. (when (setq l (assoc l org-startup-options))
  2519. (setq var (nth 1 l) val (nth 2 l))
  2520. (if (not (nth 3 l))
  2521. (set (make-local-variable var) val)
  2522. (if (not (listp (symbol-value var)))
  2523. (set (make-local-variable var) nil))
  2524. (set (make-local-variable var) (symbol-value var))
  2525. (add-to-list var val))))))
  2526. ((equal key "ARCHIVE")
  2527. (string-match " *$" value)
  2528. (setq arch (replace-match "" t t value))
  2529. (remove-text-properties 0 (length arch)
  2530. '(face t fontified t) arch))
  2531. ((equal key "SETUPFILE")
  2532. (setq setup-contents (org-file-contents
  2533. (expand-file-name
  2534. (org-remove-double-quotes value))
  2535. 'noerror))
  2536. (if (not ext-setup-or-nil)
  2537. (setq ext-setup-or-nil setup-contents start 0)
  2538. (setq ext-setup-or-nil
  2539. (concat (substring ext-setup-or-nil 0 start)
  2540. "\n" setup-contents "\n"
  2541. (substring ext-setup-or-nil start)))))
  2542. ))))
  2543. (when cat
  2544. (org-set-local 'org-category (intern cat))
  2545. (push (cons "CATEGORY" cat) props))
  2546. (when prio
  2547. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2548. (setq prio (mapcar 'string-to-char prio))
  2549. (org-set-local 'org-highest-priority (nth 0 prio))
  2550. (org-set-local 'org-lowest-priority (nth 1 prio))
  2551. (org-set-local 'org-default-priority (nth 2 prio)))
  2552. (and props (org-set-local 'org-file-properties (nreverse props)))
  2553. (and ftags (org-set-local 'org-file-tags ftags))
  2554. (and drawers (org-set-local 'org-drawers drawers))
  2555. (and arch (org-set-local 'org-archive-location arch))
  2556. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2557. ;; Process the TODO keywords
  2558. (unless kwds
  2559. ;; Use the global values as if they had been given locally.
  2560. (setq kwds (default-value 'org-todo-keywords))
  2561. (if (stringp (car kwds))
  2562. (setq kwds (list (cons org-todo-interpretation
  2563. (default-value 'org-todo-keywords)))))
  2564. (setq kwds (reverse kwds)))
  2565. (setq kwds (nreverse kwds))
  2566. (let (inter kws kw)
  2567. (while (setq kws (pop kwds))
  2568. (setq inter (pop kws) sep (member "|" kws)
  2569. kws0 (delete "|" (copy-sequence kws))
  2570. kwsa nil
  2571. kws1 (mapcar
  2572. (lambda (x)
  2573. ;; 1 2
  2574. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2575. (progn
  2576. (setq kw (match-string 1 x)
  2577. key (and (match-end 2) (match-string 2 x))
  2578. log (org-extract-log-state-settings x))
  2579. (push (cons kw (and key (string-to-char key))) kwsa)
  2580. (and log (push log org-todo-log-states))
  2581. kw)
  2582. (error "Invalid TODO keyword %s" x)))
  2583. kws0)
  2584. kwsa (if kwsa (append '((:startgroup))
  2585. (nreverse kwsa)
  2586. '((:endgroup))))
  2587. hw (car kws1)
  2588. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2589. tail (list inter hw (car dws) (org-last dws)))
  2590. (add-to-list 'org-todo-heads hw 'append)
  2591. (push kws1 org-todo-sets)
  2592. (setq org-done-keywords (append org-done-keywords dws nil))
  2593. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2594. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2595. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2596. (setq org-todo-sets (nreverse org-todo-sets)
  2597. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2598. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2599. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2600. ;; Process the constants
  2601. (when const
  2602. (let (e cst)
  2603. (while (setq e (pop const))
  2604. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2605. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2606. (setq org-table-formula-constants-local cst)))
  2607. ;; Process the tags.
  2608. (when tags
  2609. (let (e tgs)
  2610. (while (setq e (pop tags))
  2611. (cond
  2612. ((equal e "{") (push '(:startgroup) tgs))
  2613. ((equal e "}") (push '(:endgroup) tgs))
  2614. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2615. (push (cons (match-string 1 e)
  2616. (string-to-char (match-string 2 e)))
  2617. tgs))
  2618. (t (push (list e) tgs))))
  2619. (org-set-local 'org-tag-alist nil)
  2620. (while (setq e (pop tgs))
  2621. (or (and (stringp (car e))
  2622. (assoc (car e) org-tag-alist))
  2623. (push e org-tag-alist)))))
  2624. ;; Compute the regular expressions and other local variables
  2625. (if (not org-done-keywords)
  2626. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2627. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2628. (length org-scheduled-string)
  2629. (length org-clock-string)
  2630. (length org-closed-string)))
  2631. org-drawer-regexp
  2632. (concat "^[ \t]*:\\("
  2633. (mapconcat 'regexp-quote org-drawers "\\|")
  2634. "\\):[ \t]*$")
  2635. org-not-done-keywords
  2636. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2637. org-todo-regexp
  2638. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2639. "\\|") "\\)\\>")
  2640. org-not-done-regexp
  2641. (concat "\\<\\("
  2642. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2643. "\\)\\>")
  2644. org-todo-line-regexp
  2645. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2646. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2647. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2648. org-complex-heading-regexp
  2649. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2650. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2651. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2652. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2653. org-nl-done-regexp
  2654. (concat "\n\\*+[ \t]+"
  2655. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2656. "\\)" "\\>")
  2657. org-todo-line-tags-regexp
  2658. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2659. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2660. (org-re
  2661. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2662. org-looking-at-done-regexp
  2663. (concat "^" "\\(?:"
  2664. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2665. "\\>")
  2666. org-deadline-regexp (concat "\\<" org-deadline-string)
  2667. org-deadline-time-regexp
  2668. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2669. org-deadline-line-regexp
  2670. (concat "\\<\\(" org-deadline-string "\\).*")
  2671. org-scheduled-regexp
  2672. (concat "\\<" org-scheduled-string)
  2673. org-scheduled-time-regexp
  2674. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2675. org-closed-time-regexp
  2676. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2677. org-keyword-time-regexp
  2678. (concat "\\<\\(" org-scheduled-string
  2679. "\\|" org-deadline-string
  2680. "\\|" org-closed-string
  2681. "\\|" org-clock-string "\\)"
  2682. " *[[<]\\([^]>]+\\)[]>]")
  2683. org-keyword-time-not-clock-regexp
  2684. (concat "\\<\\(" org-scheduled-string
  2685. "\\|" org-deadline-string
  2686. "\\|" org-closed-string
  2687. "\\)"
  2688. " *[[<]\\([^]>]+\\)[]>]")
  2689. org-maybe-keyword-time-regexp
  2690. (concat "\\(\\<\\(" org-scheduled-string
  2691. "\\|" org-deadline-string
  2692. "\\|" org-closed-string
  2693. "\\|" org-clock-string "\\)\\)?"
  2694. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2695. org-planning-or-clock-line-re
  2696. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2697. "\\|" org-deadline-string
  2698. "\\|" org-closed-string "\\|" org-clock-string
  2699. "\\)\\>\\)")
  2700. )
  2701. (org-compute-latex-and-specials-regexp)
  2702. (org-set-font-lock-defaults))))
  2703. (defun org-file-contents (file &optional noerror)
  2704. "Return the contents of FILE, as a string."
  2705. (if (or (not file)
  2706. (not (file-readable-p file)))
  2707. (if noerror
  2708. (progn
  2709. (message "Cannot read file %s" file)
  2710. (ding) (sit-for 2)
  2711. "")
  2712. (error "Cannot read file %s" file))
  2713. (with-temp-buffer
  2714. (insert-file-contents file)
  2715. (buffer-string))))
  2716. (defun org-extract-log-state-settings (x)
  2717. "Extract the log state setting from a TODO keyword string.
  2718. This will extract info from a string like \"WAIT(w@/!)\"."
  2719. (let (kw key log1 log2)
  2720. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2721. (setq kw (match-string 1 x)
  2722. key (and (match-end 2) (match-string 2 x))
  2723. log1 (and (match-end 3) (match-string 3 x))
  2724. log2 (and (match-end 4) (match-string 4 x)))
  2725. (and (or log1 log2)
  2726. (list kw
  2727. (and log1 (if (equal log1 "!") 'time 'note))
  2728. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2729. (defun org-remove-keyword-keys (list)
  2730. "Remove a pair of parenthesis at the end of each string in LIST."
  2731. (mapcar (lambda (x)
  2732. (if (string-match "(.*)$" x)
  2733. (substring x 0 (match-beginning 0))
  2734. x))
  2735. list))
  2736. ;; FIXME: this could be done much better, using second characters etc.
  2737. (defun org-assign-fast-keys (alist)
  2738. "Assign fast keys to a keyword-key alist.
  2739. Respect keys that are already there."
  2740. (let (new e k c c1 c2 (char ?a))
  2741. (while (setq e (pop alist))
  2742. (cond
  2743. ((equal e '(:startgroup)) (push e new))
  2744. ((equal e '(:endgroup)) (push e new))
  2745. (t
  2746. (setq k (car e) c2 nil)
  2747. (if (cdr e)
  2748. (setq c (cdr e))
  2749. ;; automatically assign a character.
  2750. (setq c1 (string-to-char
  2751. (downcase (substring
  2752. k (if (= (string-to-char k) ?@) 1 0)))))
  2753. (if (or (rassoc c1 new) (rassoc c1 alist))
  2754. (while (or (rassoc char new) (rassoc char alist))
  2755. (setq char (1+ char)))
  2756. (setq c2 c1))
  2757. (setq c (or c2 char)))
  2758. (push (cons k c) new))))
  2759. (nreverse new)))
  2760. ;;; Some variables used in various places
  2761. (defvar org-window-configuration nil
  2762. "Used in various places to store a window configuration.")
  2763. (defvar org-finish-function nil
  2764. "Function to be called when `C-c C-c' is used.
  2765. This is for getting out of special buffers like remember.")
  2766. ;; FIXME: Occasionally check by commenting these, to make sure
  2767. ;; no other functions uses these, forgetting to let-bind them.
  2768. (defvar entry)
  2769. (defvar state)
  2770. (defvar last-state)
  2771. (defvar date)
  2772. (defvar description)
  2773. ;; Defined somewhere in this file, but used before definition.
  2774. (defvar org-html-entities)
  2775. (defvar org-struct-menu)
  2776. (defvar org-org-menu)
  2777. (defvar org-tbl-menu)
  2778. (defvar org-agenda-keymap)
  2779. ;;;; Define the Org-mode
  2780. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2781. (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."))
  2782. ;; We use a before-change function to check if a table might need
  2783. ;; an update.
  2784. (defvar org-table-may-need-update t
  2785. "Indicates that a table might need an update.
  2786. This variable is set by `org-before-change-function'.
  2787. `org-table-align' sets it back to nil.")
  2788. (defun org-before-change-function (beg end)
  2789. "Every change indicates that a table might need an update."
  2790. (setq org-table-may-need-update t))
  2791. (defvar org-mode-map)
  2792. (defvar org-mode-hook nil)
  2793. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2794. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2795. (defvar org-table-buffer-is-an nil)
  2796. (defconst org-outline-regexp "\\*+ ")
  2797. ;;;###autoload
  2798. (define-derived-mode org-mode outline-mode "Org"
  2799. "Outline-based notes management and organizer, alias
  2800. \"Carsten's outline-mode for keeping track of everything.\"
  2801. Org-mode develops organizational tasks around a NOTES file which
  2802. contains information about projects as plain text. Org-mode is
  2803. implemented on top of outline-mode, which is ideal to keep the content
  2804. of large files well structured. It supports ToDo items, deadlines and
  2805. time stamps, which magically appear in the diary listing of the Emacs
  2806. calendar. Tables are easily created with a built-in table editor.
  2807. Plain text URL-like links connect to websites, emails (VM), Usenet
  2808. messages (Gnus), BBDB entries, and any files related to the project.
  2809. For printing and sharing of notes, an Org-mode file (or a part of it)
  2810. can be exported as a structured ASCII or HTML file.
  2811. The following commands are available:
  2812. \\{org-mode-map}"
  2813. ;; Get rid of Outline menus, they are not needed
  2814. ;; Need to do this here because define-derived-mode sets up
  2815. ;; the keymap so late. Still, it is a waste to call this each time
  2816. ;; we switch another buffer into org-mode.
  2817. (if (featurep 'xemacs)
  2818. (when (boundp 'outline-mode-menu-heading)
  2819. ;; Assume this is Greg's port, it used easymenu
  2820. (easy-menu-remove outline-mode-menu-heading)
  2821. (easy-menu-remove outline-mode-menu-show)
  2822. (easy-menu-remove outline-mode-menu-hide))
  2823. (define-key org-mode-map [menu-bar headings] 'undefined)
  2824. (define-key org-mode-map [menu-bar hide] 'undefined)
  2825. (define-key org-mode-map [menu-bar show] 'undefined))
  2826. (org-load-modules-maybe)
  2827. (easy-menu-add org-org-menu)
  2828. (easy-menu-add org-tbl-menu)
  2829. (org-install-agenda-files-menu)
  2830. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2831. (org-add-to-invisibility-spec '(org-cwidth))
  2832. (when (featurep 'xemacs)
  2833. (org-set-local 'line-move-ignore-invisible t))
  2834. (org-set-local 'outline-regexp org-outline-regexp)
  2835. (org-set-local 'outline-level 'org-outline-level)
  2836. (when (and org-ellipsis
  2837. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2838. (fboundp 'make-glyph-code))
  2839. (unless org-display-table
  2840. (setq org-display-table (make-display-table)))
  2841. (set-display-table-slot
  2842. org-display-table 4
  2843. (vconcat (mapcar
  2844. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2845. org-ellipsis)))
  2846. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2847. (setq buffer-display-table org-display-table))
  2848. (org-set-regexps-and-options)
  2849. ;; Calc embedded
  2850. (org-set-local 'calc-embedded-open-mode "# ")
  2851. (modify-syntax-entry ?# "<")
  2852. (modify-syntax-entry ?@ "w")
  2853. (if org-startup-truncated (setq truncate-lines t))
  2854. (org-set-local 'font-lock-unfontify-region-function
  2855. 'org-unfontify-region)
  2856. ;; Activate before-change-function
  2857. (org-set-local 'org-table-may-need-update t)
  2858. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2859. 'local)
  2860. ;; Check for running clock before killing a buffer
  2861. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2862. ;; Paragraphs and auto-filling
  2863. (org-set-autofill-regexps)
  2864. (setq indent-line-function 'org-indent-line-function)
  2865. (org-update-radio-target-regexp)
  2866. ;; Comment characters
  2867. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2868. (org-set-local 'comment-padding " ")
  2869. ;; Align options lines
  2870. (org-set-local
  2871. 'align-mode-rules-list
  2872. '((org-in-buffer-settings
  2873. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2874. (modes . '(org-mode)))))
  2875. ;; Imenu
  2876. (org-set-local 'imenu-create-index-function
  2877. 'org-imenu-get-tree)
  2878. ;; Make isearch reveal context
  2879. (if (or (featurep 'xemacs)
  2880. (not (boundp 'outline-isearch-open-invisible-function)))
  2881. ;; Emacs 21 and XEmacs make use of the hook
  2882. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2883. ;; Emacs 22 deals with this through a special variable
  2884. (org-set-local 'outline-isearch-open-invisible-function
  2885. (lambda (&rest ignore) (org-show-context 'isearch))))
  2886. ;; If empty file that did not turn on org-mode automatically, make it to.
  2887. (if (and org-insert-mode-line-in-empty-file
  2888. (interactive-p)
  2889. (= (point-min) (point-max)))
  2890. (insert "# -*- mode: org -*-\n\n"))
  2891. (unless org-inhibit-startup
  2892. (when org-startup-align-all-tables
  2893. (let ((bmp (buffer-modified-p)))
  2894. (org-table-map-tables 'org-table-align)
  2895. (set-buffer-modified-p bmp)))
  2896. (org-set-startup-visibility)))
  2897. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2898. (defun org-current-time ()
  2899. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2900. (if (> (car org-time-stamp-rounding-minutes) 1)
  2901. (let ((r (car org-time-stamp-rounding-minutes))
  2902. (time (decode-time)))
  2903. (apply 'encode-time
  2904. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2905. (nthcdr 2 time))))
  2906. (current-time)))
  2907. ;;;; Font-Lock stuff, including the activators
  2908. (defvar org-mouse-map (make-sparse-keymap))
  2909. (org-defkey org-mouse-map
  2910. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2911. (org-defkey org-mouse-map
  2912. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2913. (when org-mouse-1-follows-link
  2914. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2915. (when org-tab-follows-link
  2916. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2917. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2918. (when org-return-follows-link
  2919. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2920. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2921. (require 'font-lock)
  2922. (defconst org-non-link-chars "]\t\n\r<>")
  2923. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2924. "shell" "elisp"))
  2925. (defvar org-link-types-re nil
  2926. "Matches a link that has a url-like prefix like \"http:\"")
  2927. (defvar org-link-re-with-space nil
  2928. "Matches a link with spaces, optional angular brackets around it.")
  2929. (defvar org-link-re-with-space2 nil
  2930. "Matches a link with spaces, optional angular brackets around it.")
  2931. (defvar org-angle-link-re nil
  2932. "Matches link with angular brackets, spaces are allowed.")
  2933. (defvar org-plain-link-re nil
  2934. "Matches plain link, without spaces.")
  2935. (defvar org-bracket-link-regexp nil
  2936. "Matches a link in double brackets.")
  2937. (defvar org-bracket-link-analytic-regexp nil
  2938. "Regular expression used to analyze links.
  2939. Here is what the match groups contain after a match:
  2940. 1: http:
  2941. 2: http
  2942. 3: path
  2943. 4: [desc]
  2944. 5: desc")
  2945. (defvar org-any-link-re nil
  2946. "Regular expression matching any link.")
  2947. (defun org-make-link-regexps ()
  2948. "Update the link regular expressions.
  2949. This should be called after the variable `org-link-types' has changed."
  2950. (setq org-link-types-re
  2951. (concat
  2952. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2953. org-link-re-with-space
  2954. (concat
  2955. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2956. "\\([^" org-non-link-chars " ]"
  2957. "[^" org-non-link-chars "]*"
  2958. "[^" org-non-link-chars " ]\\)>?")
  2959. org-link-re-with-space2
  2960. (concat
  2961. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2962. "\\([^" org-non-link-chars " ]"
  2963. "[^]\t\n\r]*"
  2964. "[^" org-non-link-chars " ]\\)>?")
  2965. org-angle-link-re
  2966. (concat
  2967. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2968. "\\([^" org-non-link-chars " ]"
  2969. "[^" org-non-link-chars "]*"
  2970. "\\)>")
  2971. org-plain-link-re
  2972. (concat
  2973. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2974. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2975. org-bracket-link-regexp
  2976. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2977. org-bracket-link-analytic-regexp
  2978. (concat
  2979. "\\[\\["
  2980. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2981. "\\([^]]+\\)"
  2982. "\\]"
  2983. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2984. "\\]")
  2985. org-any-link-re
  2986. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2987. org-angle-link-re "\\)\\|\\("
  2988. org-plain-link-re "\\)")))
  2989. (org-make-link-regexps)
  2990. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2991. "Regular expression for fast time stamp matching.")
  2992. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2993. "Regular expression for fast time stamp matching.")
  2994. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2995. "Regular expression matching time strings for analysis.
  2996. This one does not require the space after the date, so it can be used
  2997. on a string that terminates immediately after the date.")
  2998. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2999. "Regular expression matching time strings for analysis.")
  3000. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3001. "Regular expression matching time stamps, with groups.")
  3002. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3003. "Regular expression matching time stamps (also [..]), with groups.")
  3004. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3005. "Regular expression matching a time stamp range.")
  3006. (defconst org-tr-regexp-both
  3007. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3008. "Regular expression matching a time stamp range.")
  3009. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3010. org-ts-regexp "\\)?")
  3011. "Regular expression matching a time stamp or time stamp range.")
  3012. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3013. org-ts-regexp-both "\\)?")
  3014. "Regular expression matching a time stamp or time stamp range.
  3015. The time stamps may be either active or inactive.")
  3016. (defvar org-emph-face nil)
  3017. (defun org-do-emphasis-faces (limit)
  3018. "Run through the buffer and add overlays to links."
  3019. (let (rtn)
  3020. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3021. (if (not (= (char-after (match-beginning 3))
  3022. (char-after (match-beginning 4))))
  3023. (progn
  3024. (setq rtn t)
  3025. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3026. 'face
  3027. (nth 1 (assoc (match-string 3)
  3028. org-emphasis-alist)))
  3029. (add-text-properties (match-beginning 2) (match-end 2)
  3030. '(font-lock-multiline t))
  3031. (when org-hide-emphasis-markers
  3032. (add-text-properties (match-end 4) (match-beginning 5)
  3033. '(invisible org-link))
  3034. (add-text-properties (match-beginning 3) (match-end 3)
  3035. '(invisible org-link)))))
  3036. (backward-char 1))
  3037. rtn))
  3038. (defun org-emphasize (&optional char)
  3039. "Insert or change an emphasis, i.e. a font like bold or italic.
  3040. If there is an active region, change that region to a new emphasis.
  3041. If there is no region, just insert the marker characters and position
  3042. the cursor between them.
  3043. CHAR should be either the marker character, or the first character of the
  3044. HTML tag associated with that emphasis. If CHAR is a space, the means
  3045. to remove the emphasis of the selected region.
  3046. If char is not given (for example in an interactive call) it
  3047. will be prompted for."
  3048. (interactive)
  3049. (let ((eal org-emphasis-alist) e det
  3050. (erc org-emphasis-regexp-components)
  3051. (prompt "")
  3052. (string "") beg end move tag c s)
  3053. (if (org-region-active-p)
  3054. (setq beg (region-beginning) end (region-end)
  3055. string (buffer-substring beg end))
  3056. (setq move t))
  3057. (while (setq e (pop eal))
  3058. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3059. c (aref tag 0))
  3060. (push (cons c (string-to-char (car e))) det)
  3061. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3062. (substring tag 1)))))
  3063. (unless char
  3064. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3065. (setq char (read-char-exclusive)))
  3066. (setq char (or (cdr (assoc char det)) char))
  3067. (if (equal char ?\ )
  3068. (setq s "" move nil)
  3069. (unless (assoc (char-to-string char) org-emphasis-alist)
  3070. (error "No such emphasis marker: \"%c\"" char))
  3071. (setq s (char-to-string char)))
  3072. (while (and (> (length string) 1)
  3073. (equal (substring string 0 1) (substring string -1))
  3074. (assoc (substring string 0 1) org-emphasis-alist))
  3075. (setq string (substring string 1 -1)))
  3076. (setq string (concat s string s))
  3077. (if beg (delete-region beg end))
  3078. (unless (or (bolp)
  3079. (string-match (concat "[" (nth 0 erc) "\n]")
  3080. (char-to-string (char-before (point)))))
  3081. (insert " "))
  3082. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3083. (char-to-string (char-after (point))))
  3084. (insert " ") (backward-char 1))
  3085. (insert string)
  3086. (and move (backward-char 1))))
  3087. (defconst org-nonsticky-props
  3088. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3089. (defun org-activate-plain-links (limit)
  3090. "Run through the buffer and add overlays to links."
  3091. (catch 'exit
  3092. (let (f)
  3093. (while (re-search-forward org-plain-link-re limit t)
  3094. (setq f (get-text-property (match-beginning 0) 'face))
  3095. (if (or (eq f 'org-tag)
  3096. (and (listp f) (memq 'org-tag f)))
  3097. nil
  3098. (add-text-properties (match-beginning 0) (match-end 0)
  3099. (list 'mouse-face 'highlight
  3100. 'rear-nonsticky org-nonsticky-props
  3101. 'keymap org-mouse-map
  3102. ))
  3103. (throw 'exit t))))))
  3104. (defun org-activate-code (limit)
  3105. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3106. (unless (get-text-property (match-beginning 1) 'face)
  3107. (remove-text-properties (match-beginning 0) (match-end 0)
  3108. '(display t invisible t intangible t))
  3109. t)))
  3110. (defun org-activate-angle-links (limit)
  3111. "Run through the buffer and add overlays to links."
  3112. (if (re-search-forward org-angle-link-re limit t)
  3113. (progn
  3114. (add-text-properties (match-beginning 0) (match-end 0)
  3115. (list 'mouse-face 'highlight
  3116. 'rear-nonsticky org-nonsticky-props
  3117. 'keymap org-mouse-map
  3118. ))
  3119. t)))
  3120. (defun org-activate-bracket-links (limit)
  3121. "Run through the buffer and add overlays to bracketed links."
  3122. (if (re-search-forward org-bracket-link-regexp limit t)
  3123. (let* ((help (concat "LINK: "
  3124. (org-match-string-no-properties 1)))
  3125. ;; FIXME: above we should remove the escapes.
  3126. ;; but that requires another match, protecting match data,
  3127. ;; a lot of overhead for font-lock.
  3128. (ip (org-maybe-intangible
  3129. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3130. 'keymap org-mouse-map 'mouse-face 'highlight
  3131. 'font-lock-multiline t 'help-echo help)))
  3132. (vp (list 'rear-nonsticky org-nonsticky-props
  3133. 'keymap org-mouse-map 'mouse-face 'highlight
  3134. ' font-lock-multiline t 'help-echo help)))
  3135. ;; We need to remove the invisible property here. Table narrowing
  3136. ;; may have made some of this invisible.
  3137. (remove-text-properties (match-beginning 0) (match-end 0)
  3138. '(invisible nil))
  3139. (if (match-end 3)
  3140. (progn
  3141. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3142. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3143. (add-text-properties (match-end 3) (match-end 0) ip))
  3144. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3145. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3146. (add-text-properties (match-end 1) (match-end 0) ip))
  3147. t)))
  3148. (defun org-activate-dates (limit)
  3149. "Run through the buffer and add overlays to dates."
  3150. (if (re-search-forward org-tsr-regexp-both limit t)
  3151. (progn
  3152. (add-text-properties (match-beginning 0) (match-end 0)
  3153. (list 'mouse-face 'highlight
  3154. 'rear-nonsticky org-nonsticky-props
  3155. 'keymap org-mouse-map))
  3156. (when org-display-custom-times
  3157. (if (match-end 3)
  3158. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3159. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3160. t)))
  3161. (defvar org-target-link-regexp nil
  3162. "Regular expression matching radio targets in plain text.")
  3163. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3164. "Regular expression matching a link target.")
  3165. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3166. "Regular expression matching a radio target.")
  3167. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3168. "Regular expression matching any target.")
  3169. (defun org-activate-target-links (limit)
  3170. "Run through the buffer and add overlays to target matches."
  3171. (when org-target-link-regexp
  3172. (let ((case-fold-search t))
  3173. (if (re-search-forward org-target-link-regexp limit t)
  3174. (progn
  3175. (add-text-properties (match-beginning 0) (match-end 0)
  3176. (list 'mouse-face 'highlight
  3177. 'rear-nonsticky org-nonsticky-props
  3178. 'keymap org-mouse-map
  3179. 'help-echo "Radio target link"
  3180. 'org-linked-text t))
  3181. t)))))
  3182. (defun org-update-radio-target-regexp ()
  3183. "Find all radio targets in this file and update the regular expression."
  3184. (interactive)
  3185. (when (memq 'radio org-activate-links)
  3186. (setq org-target-link-regexp
  3187. (org-make-target-link-regexp (org-all-targets 'radio)))
  3188. (org-restart-font-lock)))
  3189. (defun org-hide-wide-columns (limit)
  3190. (let (s e)
  3191. (setq s (text-property-any (point) (or limit (point-max))
  3192. 'org-cwidth t))
  3193. (when s
  3194. (setq e (next-single-property-change s 'org-cwidth))
  3195. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3196. (goto-char e)
  3197. t)))
  3198. (defvar org-latex-and-specials-regexp nil
  3199. "Regular expression for highlighting export special stuff.")
  3200. (defvar org-match-substring-regexp)
  3201. (defvar org-match-substring-with-braces-regexp)
  3202. (defvar org-export-html-special-string-regexps)
  3203. (defun org-compute-latex-and-specials-regexp ()
  3204. "Compute regular expression for stuff treated specially by exporters."
  3205. (if (not org-highlight-latex-fragments-and-specials)
  3206. (org-set-local 'org-latex-and-specials-regexp nil)
  3207. (require 'org-exp)
  3208. (let*
  3209. ((matchers (plist-get org-format-latex-options :matchers))
  3210. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3211. org-latex-regexps)))
  3212. (options (org-combine-plists (org-default-export-plist)
  3213. (org-infile-export-plist)))
  3214. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3215. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3216. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3217. (org-export-html-expand (plist-get options :expand-quoted-html))
  3218. (org-export-with-special-strings (plist-get options :special-strings))
  3219. (re-sub
  3220. (cond
  3221. ((equal org-export-with-sub-superscripts '{})
  3222. (list org-match-substring-with-braces-regexp))
  3223. (org-export-with-sub-superscripts
  3224. (list org-match-substring-regexp))
  3225. (t nil)))
  3226. (re-latex
  3227. (if org-export-with-LaTeX-fragments
  3228. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3229. (re-macros
  3230. (if org-export-with-TeX-macros
  3231. (list (concat "\\\\"
  3232. (regexp-opt
  3233. (append (mapcar 'car org-html-entities)
  3234. (if (boundp 'org-latex-entities)
  3235. org-latex-entities nil))
  3236. 'words))) ; FIXME
  3237. ))
  3238. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3239. (re-special (if org-export-with-special-strings
  3240. (mapcar (lambda (x) (car x))
  3241. org-export-html-special-string-regexps)))
  3242. (re-rest
  3243. (delq nil
  3244. (list
  3245. (if org-export-html-expand "@<[^>\n]+>")
  3246. ))))
  3247. (org-set-local
  3248. 'org-latex-and-specials-regexp
  3249. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3250. re-rest) "\\|")))))
  3251. (defun org-do-latex-and-special-faces (limit)
  3252. "Run through the buffer and add overlays to links."
  3253. (when org-latex-and-specials-regexp
  3254. (let (rtn d)
  3255. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3256. limit t))
  3257. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3258. 'face))
  3259. '(org-code org-verbatim underline)))
  3260. (progn
  3261. (setq rtn t
  3262. d (cond ((member (char-after (1+ (match-beginning 0)))
  3263. '(?_ ?^)) 1)
  3264. (t 0)))
  3265. (font-lock-prepend-text-property
  3266. (+ d (match-beginning 0)) (match-end 0)
  3267. 'face 'org-latex-and-export-specials)
  3268. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3269. '(font-lock-multiline t)))))
  3270. rtn)))
  3271. (defun org-restart-font-lock ()
  3272. "Restart font-lock-mode, to force refontification."
  3273. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3274. (font-lock-mode -1)
  3275. (font-lock-mode 1)))
  3276. (defun org-all-targets (&optional radio)
  3277. "Return a list of all targets in this file.
  3278. With optional argument RADIO, only find radio targets."
  3279. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3280. rtn)
  3281. (save-excursion
  3282. (goto-char (point-min))
  3283. (while (re-search-forward re nil t)
  3284. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3285. rtn)))
  3286. (defun org-make-target-link-regexp (targets)
  3287. "Make regular expression matching all strings in TARGETS.
  3288. The regular expression finds the targets also if there is a line break
  3289. between words."
  3290. (and targets
  3291. (concat
  3292. "\\<\\("
  3293. (mapconcat
  3294. (lambda (x)
  3295. (while (string-match " +" x)
  3296. (setq x (replace-match "\\s-+" t t x)))
  3297. x)
  3298. targets
  3299. "\\|")
  3300. "\\)\\>")))
  3301. (defun org-activate-tags (limit)
  3302. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3303. (progn
  3304. (add-text-properties (match-beginning 1) (match-end 1)
  3305. (list 'mouse-face 'highlight
  3306. 'rear-nonsticky org-nonsticky-props
  3307. 'keymap org-mouse-map))
  3308. t)))
  3309. (defun org-outline-level ()
  3310. (save-excursion
  3311. (looking-at outline-regexp)
  3312. (if (match-beginning 1)
  3313. (+ (org-get-string-indentation (match-string 1)) 1000)
  3314. (1- (- (match-end 0) (match-beginning 0))))))
  3315. (defvar org-font-lock-keywords nil)
  3316. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3317. "Regular expression matching a property line.")
  3318. (defvar org-font-lock-hook nil
  3319. "Functions to be called for special font lock stuff.")
  3320. (defun org-font-lock-hook (limit)
  3321. (run-hook-with-args 'org-font-lock-hook limit))
  3322. (defun org-set-font-lock-defaults ()
  3323. (let* ((em org-fontify-emphasized-text)
  3324. (lk org-activate-links)
  3325. (org-font-lock-extra-keywords
  3326. (list
  3327. ;; Call the hook
  3328. '(org-font-lock-hook)
  3329. ;; Headlines
  3330. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3331. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3332. ;; Table lines
  3333. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3334. (1 'org-table t))
  3335. ;; Table internals
  3336. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3337. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3338. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3339. ;; Drawers
  3340. (list org-drawer-regexp '(0 'org-special-keyword t))
  3341. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3342. ;; Properties
  3343. (list org-property-re
  3344. '(1 'org-special-keyword t)
  3345. '(3 'org-property-value t))
  3346. (if org-format-transports-properties-p
  3347. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3348. ;; Links
  3349. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3350. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3351. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3352. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3353. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3354. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3355. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3356. '(org-hide-wide-columns (0 nil append))
  3357. ;; TODO lines
  3358. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3359. '(1 (org-get-todo-face 1) t))
  3360. ;; DONE
  3361. (if org-fontify-done-headline
  3362. (list (concat "^[*]+ +\\<\\("
  3363. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3364. "\\)\\(.*\\)")
  3365. '(2 'org-headline-done t))
  3366. nil)
  3367. ;; Priorities
  3368. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3369. ;; Special keywords
  3370. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3371. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3372. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3373. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3374. ;; Emphasis
  3375. (if em
  3376. (if (featurep 'xemacs)
  3377. '(org-do-emphasis-faces (0 nil append))
  3378. '(org-do-emphasis-faces)))
  3379. ;; Checkboxes
  3380. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3381. 2 'bold prepend)
  3382. (if org-provide-checkbox-statistics
  3383. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3384. (0 (org-get-checkbox-statistics-face) t)))
  3385. ;; Description list items
  3386. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3387. 2 'bold prepend)
  3388. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3389. '(1 'org-archived prepend))
  3390. ;; Specials
  3391. '(org-do-latex-and-special-faces)
  3392. ;; Code
  3393. '(org-activate-code (1 'org-code t))
  3394. ;; COMMENT
  3395. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3396. "\\|" org-quote-string "\\)\\>")
  3397. '(1 'org-special-keyword t))
  3398. '("^#.*" (0 'font-lock-comment-face t))
  3399. )))
  3400. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3401. ;; Now set the full font-lock-keywords
  3402. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3403. (org-set-local 'font-lock-defaults
  3404. '(org-font-lock-keywords t nil nil backward-paragraph))
  3405. (kill-local-variable 'font-lock-keywords) nil))
  3406. (defvar org-m nil)
  3407. (defvar org-l nil)
  3408. (defvar org-f nil)
  3409. (defun org-get-level-face (n)
  3410. "Get the right face for match N in font-lock matching of healdines."
  3411. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3412. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3413. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3414. (cond
  3415. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3416. ((eq n 2) org-f)
  3417. (t (if org-level-color-stars-only nil org-f))))
  3418. (defun org-get-todo-face (kwd)
  3419. "Get the right face for a TODO keyword KWD.
  3420. If KWD is a number, get the corresponding match group."
  3421. (if (numberp kwd) (setq kwd (match-string kwd)))
  3422. (or (cdr (assoc kwd org-todo-keyword-faces))
  3423. (and (member kwd org-done-keywords) 'org-done)
  3424. 'org-todo))
  3425. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3426. "Remove fontification and activation overlays from links."
  3427. (font-lock-default-unfontify-region beg end)
  3428. (let* ((buffer-undo-list t)
  3429. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3430. (inhibit-modification-hooks t)
  3431. deactivate-mark buffer-file-name buffer-file-truename)
  3432. (remove-text-properties beg end
  3433. '(mouse-face t keymap t org-linked-text t
  3434. invisible t intangible t))))
  3435. ;;;; Visibility cycling, including org-goto and indirect buffer
  3436. ;;; Cycling
  3437. (defvar org-cycle-global-status nil)
  3438. (make-variable-buffer-local 'org-cycle-global-status)
  3439. (defvar org-cycle-subtree-status nil)
  3440. (make-variable-buffer-local 'org-cycle-subtree-status)
  3441. ;;;###autoload
  3442. (defun org-cycle (&optional arg)
  3443. "Visibility cycling for Org-mode.
  3444. - When this function is called with a prefix argument, rotate the entire
  3445. buffer through 3 states (global cycling)
  3446. 1. OVERVIEW: Show only top-level headlines.
  3447. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3448. 3. SHOW ALL: Show everything.
  3449. When called with two C-c C-u prefixes, switch to the startup visibility,
  3450. determined by the variable `org-startup-folded', and by any VISIBILITY
  3451. properties in the buffer.
  3452. - When point is at the beginning of a headline, rotate the subtree started
  3453. by this line through 3 different states (local cycling)
  3454. 1. FOLDED: Only the main headline is shown.
  3455. 2. CHILDREN: The main headline and the direct children are shown.
  3456. From this state, you can move to one of the children
  3457. and zoom in further.
  3458. 3. SUBTREE: Show the entire subtree, including body text.
  3459. - When there is a numeric prefix, go up to a heading with level ARG, do
  3460. a `show-subtree' and return to the previous cursor position. If ARG
  3461. is negative, go up that many levels.
  3462. - When point is not at the beginning of a headline, execute the global
  3463. binding for TAB, which is re-indenting the line. See the option
  3464. `org-cycle-emulate-tab' for details.
  3465. - Special case: if point is at the beginning of the buffer and there is
  3466. no headline in line 1, this function will act as if called with prefix arg.
  3467. But only if also the variable `org-cycle-global-at-bob' is t."
  3468. (interactive "P")
  3469. (org-load-modules-maybe)
  3470. (let* ((outline-regexp
  3471. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3472. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3473. outline-regexp))
  3474. (bob-special (and org-cycle-global-at-bob (bobp)
  3475. (not (looking-at outline-regexp))))
  3476. (org-cycle-hook
  3477. (if bob-special
  3478. (delq 'org-optimize-window-after-visibility-change
  3479. (copy-sequence org-cycle-hook))
  3480. org-cycle-hook))
  3481. (pos (point)))
  3482. (if (or bob-special (equal arg '(4)))
  3483. ;; special case: use global cycling
  3484. (setq arg t))
  3485. (cond
  3486. ((equal arg '(16))
  3487. (org-set-startup-visibility)
  3488. (message "Startup visibility, plus VISIBILITY properties."))
  3489. ((org-at-table-p 'any)
  3490. ;; Enter the table or move to the next field in the table
  3491. (or (org-table-recognize-table.el)
  3492. (progn
  3493. (if arg (org-table-edit-field t)
  3494. (org-table-justify-field-maybe)
  3495. (call-interactively 'org-table-next-field)))))
  3496. ((eq arg t) ;; Global cycling
  3497. (cond
  3498. ((and (eq last-command this-command)
  3499. (eq org-cycle-global-status 'overview))
  3500. ;; We just created the overview - now do table of contents
  3501. ;; This can be slow in very large buffers, so indicate action
  3502. (message "CONTENTS...")
  3503. (org-content)
  3504. (message "CONTENTS...done")
  3505. (setq org-cycle-global-status 'contents)
  3506. (run-hook-with-args 'org-cycle-hook 'contents))
  3507. ((and (eq last-command this-command)
  3508. (eq org-cycle-global-status 'contents))
  3509. ;; We just showed the table of contents - now show everything
  3510. (show-all)
  3511. (message "SHOW ALL")
  3512. (setq org-cycle-global-status 'all)
  3513. (run-hook-with-args 'org-cycle-hook 'all))
  3514. (t
  3515. ;; Default action: go to overview
  3516. (org-overview)
  3517. (message "OVERVIEW")
  3518. (setq org-cycle-global-status 'overview)
  3519. (run-hook-with-args 'org-cycle-hook 'overview))))
  3520. ((and org-drawers org-drawer-regexp
  3521. (save-excursion
  3522. (beginning-of-line 1)
  3523. (looking-at org-drawer-regexp)))
  3524. ;; Toggle block visibility
  3525. (org-flag-drawer
  3526. (not (get-char-property (match-end 0) 'invisible))))
  3527. ((integerp arg)
  3528. ;; Show-subtree, ARG levels up from here.
  3529. (save-excursion
  3530. (org-back-to-heading)
  3531. (outline-up-heading (if (< arg 0) (- arg)
  3532. (- (funcall outline-level) arg)))
  3533. (org-show-subtree)))
  3534. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3535. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3536. ;; At a heading: rotate between three different views
  3537. (org-back-to-heading)
  3538. (let ((goal-column 0) eoh eol eos)
  3539. ;; First, some boundaries
  3540. (save-excursion
  3541. (org-back-to-heading)
  3542. (save-excursion
  3543. (beginning-of-line 2)
  3544. (while (and (not (eobp)) ;; this is like `next-line'
  3545. (get-char-property (1- (point)) 'invisible))
  3546. (beginning-of-line 2)) (setq eol (point)))
  3547. (outline-end-of-heading) (setq eoh (point))
  3548. (org-end-of-subtree t)
  3549. (unless (eobp)
  3550. (skip-chars-forward " \t\n")
  3551. (beginning-of-line 1) ; in case this is an item
  3552. )
  3553. (setq eos (1- (point))))
  3554. ;; Find out what to do next and set `this-command'
  3555. (cond
  3556. ((= eos eoh)
  3557. ;; Nothing is hidden behind this heading
  3558. (message "EMPTY ENTRY")
  3559. (setq org-cycle-subtree-status nil)
  3560. (save-excursion
  3561. (goto-char eos)
  3562. (outline-next-heading)
  3563. (if (org-invisible-p) (org-flag-heading nil))))
  3564. ((or (>= eol eos)
  3565. (not (string-match "\\S-" (buffer-substring eol eos))))
  3566. ;; Entire subtree is hidden in one line: open it
  3567. (org-show-entry)
  3568. (show-children)
  3569. (message "CHILDREN")
  3570. (save-excursion
  3571. (goto-char eos)
  3572. (outline-next-heading)
  3573. (if (org-invisible-p) (org-flag-heading nil)))
  3574. (setq org-cycle-subtree-status 'children)
  3575. (run-hook-with-args 'org-cycle-hook 'children))
  3576. ((and (eq last-command this-command)
  3577. (eq org-cycle-subtree-status 'children))
  3578. ;; We just showed the children, now show everything.
  3579. (org-show-subtree)
  3580. (message "SUBTREE")
  3581. (setq org-cycle-subtree-status 'subtree)
  3582. (run-hook-with-args 'org-cycle-hook 'subtree))
  3583. (t
  3584. ;; Default action: hide the subtree.
  3585. (hide-subtree)
  3586. (message "FOLDED")
  3587. (setq org-cycle-subtree-status 'folded)
  3588. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3589. ;; TAB emulation and template completion
  3590. (buffer-read-only (org-back-to-heading))
  3591. ((org-try-structure-completion))
  3592. ((org-try-cdlatex-tab))
  3593. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3594. (or (not (bolp))
  3595. (not (looking-at outline-regexp))))
  3596. (call-interactively (global-key-binding "\t")))
  3597. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3598. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3599. (or (and (eq org-cycle-emulate-tab 'white)
  3600. (= (match-end 0) (point-at-eol)))
  3601. (and (eq org-cycle-emulate-tab 'whitestart)
  3602. (>= (match-end 0) pos))))
  3603. t
  3604. (eq org-cycle-emulate-tab t))
  3605. (call-interactively (global-key-binding "\t")))
  3606. (t (save-excursion
  3607. (org-back-to-heading)
  3608. (org-cycle))))))
  3609. ;;;###autoload
  3610. (defun org-global-cycle (&optional arg)
  3611. "Cycle the global visibility. For details see `org-cycle'.
  3612. With C-u prefix arg, switch to startup visibility.
  3613. With a numeric prefix, show all headlines up to that level."
  3614. (interactive "P")
  3615. (let ((org-cycle-include-plain-lists
  3616. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3617. (cond
  3618. ((integerp arg)
  3619. (show-all)
  3620. (hide-sublevels arg)
  3621. (setq org-cycle-global-status 'contents))
  3622. ((equal arg '(4))
  3623. (org-set-startup-visibility)
  3624. (message "Startup visibility, plus VISIBILITY properties."))
  3625. (t
  3626. (org-cycle '(4))))))
  3627. (defun org-set-startup-visibility ()
  3628. "Set the visibility required by startup options and properties."
  3629. (cond
  3630. ((eq org-startup-folded t)
  3631. (org-cycle '(4)))
  3632. ((eq org-startup-folded 'content)
  3633. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3634. (org-cycle '(4)) (org-cycle '(4)))))
  3635. (org-set-visibility-according-to-property 'no-cleanup)
  3636. (org-cycle-hide-archived-subtrees 'all)
  3637. (org-cycle-hide-drawers 'all)
  3638. (org-cycle-show-empty-lines 'all))
  3639. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3640. "Switch subtree visibilities according to :VISIBILITY: property."
  3641. (interactive)
  3642. (let (state)
  3643. (save-excursion
  3644. (goto-char (point-min))
  3645. (while (re-search-forward
  3646. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3647. nil t)
  3648. (setq state (match-string 1))
  3649. (save-excursion
  3650. (org-back-to-heading t)
  3651. (hide-subtree)
  3652. (org-reveal)
  3653. (cond
  3654. ((equal state '("fold" "folded"))
  3655. (hide-subtree))
  3656. ((equal state "children")
  3657. (org-show-hidden-entry)
  3658. (show-children))
  3659. ((equal state "content")
  3660. (save-excursion
  3661. (save-restriction
  3662. (org-narrow-to-subtree)
  3663. (org-content))))
  3664. ((member state '("all" "showall"))
  3665. (show-subtree)))))
  3666. (unless no-cleanup
  3667. (org-cycle-hide-archived-subtrees 'all)
  3668. (org-cycle-hide-drawers 'all)
  3669. (org-cycle-show-empty-lines 'all)))))
  3670. (defun org-overview ()
  3671. "Switch to overview mode, shoing only top-level headlines.
  3672. Really, this shows all headlines with level equal or greater than the level
  3673. of the first headline in the buffer. This is important, because if the
  3674. first headline is not level one, then (hide-sublevels 1) gives confusing
  3675. results."
  3676. (interactive)
  3677. (let ((level (save-excursion
  3678. (goto-char (point-min))
  3679. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3680. (progn
  3681. (goto-char (match-beginning 0))
  3682. (funcall outline-level))))))
  3683. (and level (hide-sublevels level))))
  3684. (defun org-content (&optional arg)
  3685. "Show all headlines in the buffer, like a table of contents.
  3686. With numerical argument N, show content up to level N."
  3687. (interactive "P")
  3688. (save-excursion
  3689. ;; Visit all headings and show their offspring
  3690. (and (integerp arg) (org-overview))
  3691. (goto-char (point-max))
  3692. (catch 'exit
  3693. (while (and (progn (condition-case nil
  3694. (outline-previous-visible-heading 1)
  3695. (error (goto-char (point-min))))
  3696. t)
  3697. (looking-at outline-regexp))
  3698. (if (integerp arg)
  3699. (show-children (1- arg))
  3700. (show-branches))
  3701. (if (bobp) (throw 'exit nil))))))
  3702. (defun org-optimize-window-after-visibility-change (state)
  3703. "Adjust the window after a change in outline visibility.
  3704. This function is the default value of the hook `org-cycle-hook'."
  3705. (when (get-buffer-window (current-buffer))
  3706. (cond
  3707. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3708. ; ((eq state 'overview) (org-beginning-of-line))
  3709. ((eq state 'content) nil)
  3710. ((eq state 'all) nil)
  3711. ((eq state 'folded) nil)
  3712. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3713. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3714. (defun org-compact-display-after-subtree-move ()
  3715. (let (beg end)
  3716. (save-excursion
  3717. (if (org-up-heading-safe)
  3718. (progn
  3719. (hide-subtree)
  3720. (show-entry)
  3721. (show-children)
  3722. (org-cycle-show-empty-lines 'children)
  3723. (org-cycle-hide-drawers 'children))
  3724. (org-overview)))))
  3725. (defun org-cycle-show-empty-lines (state)
  3726. "Show empty lines above all visible headlines.
  3727. The region to be covered depends on STATE when called through
  3728. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3729. entire buffer covered. Note that an empty line is only shown if there
  3730. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3731. (when (> org-cycle-separator-lines 0)
  3732. (save-excursion
  3733. (let* ((n org-cycle-separator-lines)
  3734. (re (cond
  3735. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3736. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3737. (t (let ((ns (number-to-string (- n 2))))
  3738. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3739. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3740. beg end)
  3741. (cond
  3742. ((memq state '(overview contents t))
  3743. (setq beg (point-min) end (point-max)))
  3744. ((memq state '(children folded))
  3745. (setq beg (point) end (progn (org-end-of-subtree t t)
  3746. (beginning-of-line 2)
  3747. (point)))))
  3748. (when beg
  3749. (goto-char beg)
  3750. (while (re-search-forward re end t)
  3751. (if (not (get-char-property (match-end 1) 'invisible))
  3752. (outline-flag-region
  3753. (match-beginning 1) (match-end 1) nil)))))))
  3754. ;; Never hide empty lines at the end of the file.
  3755. (save-excursion
  3756. (goto-char (point-max))
  3757. (outline-previous-heading)
  3758. (outline-end-of-heading)
  3759. (if (and (looking-at "[ \t\n]+")
  3760. (= (match-end 0) (point-max)))
  3761. (outline-flag-region (point) (match-end 0) nil))))
  3762. (defun org-show-empty-lines-in-parent ()
  3763. "Move to the parent and re-show empty lines before visible headlines."
  3764. (save-excursion
  3765. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3766. (org-cycle-show-empty-lines context))))
  3767. (defun org-cycle-hide-drawers (state)
  3768. "Re-hide all drawers after a visibility state change."
  3769. (when (and (org-mode-p)
  3770. (not (memq state '(overview folded))))
  3771. (save-excursion
  3772. (let* ((globalp (memq state '(contents all)))
  3773. (beg (if globalp (point-min) (point)))
  3774. (end (if globalp (point-max) (org-end-of-subtree t))))
  3775. (goto-char beg)
  3776. (while (re-search-forward org-drawer-regexp end t)
  3777. (org-flag-drawer t))))))
  3778. (defun org-flag-drawer (flag)
  3779. (save-excursion
  3780. (beginning-of-line 1)
  3781. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3782. (let ((b (match-end 0))
  3783. (outline-regexp org-outline-regexp))
  3784. (if (re-search-forward
  3785. "^[ \t]*:END:"
  3786. (save-excursion (outline-next-heading) (point)) t)
  3787. (outline-flag-region b (point-at-eol) flag)
  3788. (error ":END: line missing"))))))
  3789. (defun org-subtree-end-visible-p ()
  3790. "Is the end of the current subtree visible?"
  3791. (pos-visible-in-window-p
  3792. (save-excursion (org-end-of-subtree t) (point))))
  3793. (defun org-first-headline-recenter (&optional N)
  3794. "Move cursor to the first headline and recenter the headline.
  3795. Optional argument N means, put the headline into the Nth line of the window."
  3796. (goto-char (point-min))
  3797. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3798. (beginning-of-line)
  3799. (recenter (prefix-numeric-value N))))
  3800. ;;; Org-goto
  3801. (defvar org-goto-window-configuration nil)
  3802. (defvar org-goto-marker nil)
  3803. (defvar org-goto-map
  3804. (let ((map (make-sparse-keymap)))
  3805. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3806. (while (setq cmd (pop cmds))
  3807. (substitute-key-definition cmd cmd map global-map)))
  3808. (suppress-keymap map)
  3809. (org-defkey map "\C-m" 'org-goto-ret)
  3810. (org-defkey map [(return)] 'org-goto-ret)
  3811. (org-defkey map [(left)] 'org-goto-left)
  3812. (org-defkey map [(right)] 'org-goto-right)
  3813. (org-defkey map [(control ?g)] 'org-goto-quit)
  3814. (org-defkey map "\C-i" 'org-cycle)
  3815. (org-defkey map [(tab)] 'org-cycle)
  3816. (org-defkey map [(down)] 'outline-next-visible-heading)
  3817. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3818. (if org-goto-auto-isearch
  3819. (if (fboundp 'define-key-after)
  3820. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3821. nil)
  3822. (org-defkey map "q" 'org-goto-quit)
  3823. (org-defkey map "n" 'outline-next-visible-heading)
  3824. (org-defkey map "p" 'outline-previous-visible-heading)
  3825. (org-defkey map "f" 'outline-forward-same-level)
  3826. (org-defkey map "b" 'outline-backward-same-level)
  3827. (org-defkey map "u" 'outline-up-heading))
  3828. (org-defkey map "/" 'org-occur)
  3829. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3830. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3831. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3832. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3833. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3834. map))
  3835. (defconst org-goto-help
  3836. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3837. RET=jump to location [Q]uit and return to previous location
  3838. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3839. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3840. ;; FIXME: Docstring doe not mention both interfaces
  3841. (defun org-goto (&optional alternative-interface)
  3842. "Look up a different location in the current file, keeping current visibility.
  3843. When you want look-up or go to a different location in a document, the
  3844. fastest way is often to fold the entire buffer and then dive into the tree.
  3845. This method has the disadvantage, that the previous location will be folded,
  3846. which may not be what you want.
  3847. This command works around this by showing a copy of the current buffer
  3848. in an indirect buffer, in overview mode. You can dive into the tree in
  3849. that copy, use org-occur and incremental search to find a location.
  3850. When pressing RET or `Q', the command returns to the original buffer in
  3851. which the visibility is still unchanged. After RET is will also jump to
  3852. the location selected in the indirect buffer and expose the
  3853. the headline hierarchy above."
  3854. (interactive "P")
  3855. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3856. (org-refile-use-outline-path t)
  3857. (interface
  3858. (if (not alternative-interface)
  3859. org-goto-interface
  3860. (if (eq org-goto-interface 'outline)
  3861. 'outline-path-completion
  3862. 'outline)))
  3863. (org-goto-start-pos (point))
  3864. (selected-point
  3865. (if (eq interface 'outline)
  3866. (car (org-get-location (current-buffer) org-goto-help))
  3867. (nth 3 (org-refile-get-location "Goto: ")))))
  3868. (if selected-point
  3869. (progn
  3870. (org-mark-ring-push org-goto-start-pos)
  3871. (goto-char selected-point)
  3872. (if (or (org-invisible-p) (org-invisible-p2))
  3873. (org-show-context 'org-goto)))
  3874. (message "Quit"))))
  3875. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3876. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3877. (defvar org-goto-local-auto-isearch-map) ; defined below
  3878. (defun org-get-location (buf help)
  3879. "Let the user select a location in the Org-mode buffer BUF.
  3880. This function uses a recursive edit. It returns the selected position
  3881. or nil."
  3882. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3883. (isearch-hide-immediately nil)
  3884. (isearch-search-fun-function
  3885. (lambda () 'org-goto-local-search-forward-headings))
  3886. (org-goto-selected-point org-goto-exit-command))
  3887. (save-excursion
  3888. (save-window-excursion
  3889. (delete-other-windows)
  3890. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3891. (switch-to-buffer
  3892. (condition-case nil
  3893. (make-indirect-buffer (current-buffer) "*org-goto*")
  3894. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3895. (with-output-to-temp-buffer "*Help*"
  3896. (princ help))
  3897. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3898. (setq buffer-read-only nil)
  3899. (let ((org-startup-truncated t)
  3900. (org-startup-folded nil)
  3901. (org-startup-align-all-tables nil))
  3902. (org-mode)
  3903. (org-overview))
  3904. (setq buffer-read-only t)
  3905. (if (and (boundp 'org-goto-start-pos)
  3906. (integer-or-marker-p org-goto-start-pos))
  3907. (let ((org-show-hierarchy-above t)
  3908. (org-show-siblings t)
  3909. (org-show-following-heading t))
  3910. (goto-char org-goto-start-pos)
  3911. (and (org-invisible-p) (org-show-context)))
  3912. (goto-char (point-min)))
  3913. (org-beginning-of-line)
  3914. (message "Select location and press RET")
  3915. (use-local-map org-goto-map)
  3916. (recursive-edit)
  3917. ))
  3918. (kill-buffer "*org-goto*")
  3919. (cons org-goto-selected-point org-goto-exit-command)))
  3920. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3921. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3922. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3923. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3924. (defun org-goto-local-search-forward-headings (string bound noerror)
  3925. "Search and make sure that anu matches are in headlines."
  3926. (catch 'return
  3927. (while (search-forward string bound noerror)
  3928. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3929. (and (member :headline context)
  3930. (not (member :tags context))))
  3931. (throw 'return (point))))))
  3932. (defun org-goto-local-auto-isearch ()
  3933. "Start isearch."
  3934. (interactive)
  3935. (goto-char (point-min))
  3936. (let ((keys (this-command-keys)))
  3937. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3938. (isearch-mode t)
  3939. (isearch-process-search-char (string-to-char keys)))))
  3940. (defun org-goto-ret (&optional arg)
  3941. "Finish `org-goto' by going to the new location."
  3942. (interactive "P")
  3943. (setq org-goto-selected-point (point)
  3944. org-goto-exit-command 'return)
  3945. (throw 'exit nil))
  3946. (defun org-goto-left ()
  3947. "Finish `org-goto' by going to the new location."
  3948. (interactive)
  3949. (if (org-on-heading-p)
  3950. (progn
  3951. (beginning-of-line 1)
  3952. (setq org-goto-selected-point (point)
  3953. org-goto-exit-command 'left)
  3954. (throw 'exit nil))
  3955. (error "Not on a heading")))
  3956. (defun org-goto-right ()
  3957. "Finish `org-goto' by going to the new location."
  3958. (interactive)
  3959. (if (org-on-heading-p)
  3960. (progn
  3961. (setq org-goto-selected-point (point)
  3962. org-goto-exit-command 'right)
  3963. (throw 'exit nil))
  3964. (error "Not on a heading")))
  3965. (defun org-goto-quit ()
  3966. "Finish `org-goto' without cursor motion."
  3967. (interactive)
  3968. (setq org-goto-selected-point nil)
  3969. (setq org-goto-exit-command 'quit)
  3970. (throw 'exit nil))
  3971. ;;; Indirect buffer display of subtrees
  3972. (defvar org-indirect-dedicated-frame nil
  3973. "This is the frame being used for indirect tree display.")
  3974. (defvar org-last-indirect-buffer nil)
  3975. (defun org-tree-to-indirect-buffer (&optional arg)
  3976. "Create indirect buffer and narrow it to current subtree.
  3977. With numerical prefix ARG, go up to this level and then take that tree.
  3978. If ARG is negative, go up that many levels.
  3979. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3980. indirect buffer previously made with this command, to avoid proliferation of
  3981. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3982. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3983. is kept so that you can work with several indirect buffers at the same time.
  3984. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3985. requests that a new frame be made for the new buffer, so that the dedicated
  3986. frame is not changed."
  3987. (interactive "P")
  3988. (let ((cbuf (current-buffer))
  3989. (cwin (selected-window))
  3990. (pos (point))
  3991. beg end level heading ibuf)
  3992. (save-excursion
  3993. (org-back-to-heading t)
  3994. (when (numberp arg)
  3995. (setq level (org-outline-level))
  3996. (if (< arg 0) (setq arg (+ level arg)))
  3997. (while (> (setq level (org-outline-level)) arg)
  3998. (outline-up-heading 1 t)))
  3999. (setq beg (point)
  4000. heading (org-get-heading))
  4001. (org-end-of-subtree t) (setq end (point)))
  4002. (if (and (buffer-live-p org-last-indirect-buffer)
  4003. (not (eq org-indirect-buffer-display 'new-frame))
  4004. (not arg))
  4005. (kill-buffer org-last-indirect-buffer))
  4006. (setq ibuf (org-get-indirect-buffer cbuf)
  4007. org-last-indirect-buffer ibuf)
  4008. (cond
  4009. ((or (eq org-indirect-buffer-display 'new-frame)
  4010. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4011. (select-frame (make-frame))
  4012. (delete-other-windows)
  4013. (switch-to-buffer ibuf)
  4014. (org-set-frame-title heading))
  4015. ((eq org-indirect-buffer-display 'dedicated-frame)
  4016. (raise-frame
  4017. (select-frame (or (and org-indirect-dedicated-frame
  4018. (frame-live-p org-indirect-dedicated-frame)
  4019. org-indirect-dedicated-frame)
  4020. (setq org-indirect-dedicated-frame (make-frame)))))
  4021. (delete-other-windows)
  4022. (switch-to-buffer ibuf)
  4023. (org-set-frame-title (concat "Indirect: " heading)))
  4024. ((eq org-indirect-buffer-display 'current-window)
  4025. (switch-to-buffer ibuf))
  4026. ((eq org-indirect-buffer-display 'other-window)
  4027. (pop-to-buffer ibuf))
  4028. (t (error "Invalid value.")))
  4029. (if (featurep 'xemacs)
  4030. (save-excursion (org-mode) (turn-on-font-lock)))
  4031. (narrow-to-region beg end)
  4032. (show-all)
  4033. (goto-char pos)
  4034. (and (window-live-p cwin) (select-window cwin))))
  4035. (defun org-get-indirect-buffer (&optional buffer)
  4036. (setq buffer (or buffer (current-buffer)))
  4037. (let ((n 1) (base (buffer-name buffer)) bname)
  4038. (while (buffer-live-p
  4039. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4040. (setq n (1+ n)))
  4041. (condition-case nil
  4042. (make-indirect-buffer buffer bname 'clone)
  4043. (error (make-indirect-buffer buffer bname)))))
  4044. (defun org-set-frame-title (title)
  4045. "Set the title of the current frame to the string TITLE."
  4046. ;; FIXME: how to name a single frame in XEmacs???
  4047. (unless (featurep 'xemacs)
  4048. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4049. ;;;; Structure editing
  4050. ;;; Inserting headlines
  4051. (defun org-insert-heading (&optional force-heading)
  4052. "Insert a new heading or item with same depth at point.
  4053. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4054. If point is at the beginning of a headline, insert a sibling before the
  4055. current headline. If point is not at the beginning, do not split the line,
  4056. but create the new hedline after the current line."
  4057. (interactive "P")
  4058. (if (= (buffer-size) 0)
  4059. (insert "\n* ")
  4060. (when (or force-heading (not (org-insert-item)))
  4061. (let* ((head (save-excursion
  4062. (condition-case nil
  4063. (progn
  4064. (org-back-to-heading)
  4065. (match-string 0))
  4066. (error "*"))))
  4067. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4068. pos)
  4069. (cond
  4070. ((and (org-on-heading-p) (bolp)
  4071. (or (bobp)
  4072. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4073. ;; insert before the current line
  4074. (open-line (if blank 2 1)))
  4075. ((and (bolp)
  4076. (or (bobp)
  4077. (save-excursion
  4078. (backward-char 1) (not (org-invisible-p)))))
  4079. ;; insert right here
  4080. nil)
  4081. (t
  4082. ;; in the middle of the line
  4083. (org-show-entry)
  4084. (let ((split
  4085. (org-get-alist-option org-M-RET-may-split-line 'headline))
  4086. tags pos)
  4087. (if (org-on-heading-p)
  4088. (progn
  4089. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4090. (setq tags (and (match-end 2) (match-string 2)))
  4091. (and (match-end 1)
  4092. (delete-region (match-beginning 1) (match-end 1)))
  4093. (setq pos (point-at-bol))
  4094. (or split (end-of-line 1))
  4095. (delete-horizontal-space)
  4096. (newline (if blank 2 1))
  4097. (when tags
  4098. (save-excursion
  4099. (goto-char pos)
  4100. (end-of-line 1)
  4101. (insert " " tags)
  4102. (org-set-tags nil 'align))))
  4103. (or split (end-of-line 1))
  4104. (newline (if blank 2 1))))))
  4105. (insert head) (just-one-space)
  4106. (setq pos (point))
  4107. (end-of-line 1)
  4108. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4109. (run-hooks 'org-insert-heading-hook)))))
  4110. (defun org-get-heading (&optional no-tags)
  4111. "Return the heading of the current entry, without the stars."
  4112. (save-excursion
  4113. (org-back-to-heading t)
  4114. (if (looking-at
  4115. (if no-tags
  4116. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4117. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4118. (match-string 1) "")))
  4119. (defun org-insert-heading-after-current ()
  4120. "Insert a new heading with same level as current, after current subtree."
  4121. (interactive)
  4122. (org-back-to-heading)
  4123. (org-insert-heading)
  4124. (org-move-subtree-down)
  4125. (end-of-line 1))
  4126. (defun org-insert-todo-heading (arg)
  4127. "Insert a new heading with the same level and TODO state as current heading.
  4128. If the heading has no TODO state, or if the state is DONE, use the first
  4129. state (TODO by default). Also with prefix arg, force first state."
  4130. (interactive "P")
  4131. (when (not (org-insert-item 'checkbox))
  4132. (org-insert-heading)
  4133. (save-excursion
  4134. (org-back-to-heading)
  4135. (outline-previous-heading)
  4136. (looking-at org-todo-line-regexp))
  4137. (if (or arg
  4138. (not (match-beginning 2))
  4139. (member (match-string 2) org-done-keywords))
  4140. (insert (car org-todo-keywords-1) " ")
  4141. (insert (match-string 2) " "))
  4142. (when org-provide-todo-statistics
  4143. (org-update-parent-todo-statistics))))
  4144. (defun org-insert-subheading (arg)
  4145. "Insert a new subheading and demote it.
  4146. Works for outline headings and for plain lists alike."
  4147. (interactive "P")
  4148. (org-insert-heading arg)
  4149. (cond
  4150. ((org-on-heading-p) (org-do-demote))
  4151. ((org-at-item-p) (org-indent-item 1))))
  4152. (defun org-insert-todo-subheading (arg)
  4153. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4154. Works for outline headings and for plain lists alike."
  4155. (interactive "P")
  4156. (org-insert-todo-heading arg)
  4157. (cond
  4158. ((org-on-heading-p) (org-do-demote))
  4159. ((org-at-item-p) (org-indent-item 1))))
  4160. ;;; Promotion and Demotion
  4161. (defun org-promote-subtree ()
  4162. "Promote the entire subtree.
  4163. See also `org-promote'."
  4164. (interactive)
  4165. (save-excursion
  4166. (org-map-tree 'org-promote))
  4167. (org-fix-position-after-promote))
  4168. (defun org-demote-subtree ()
  4169. "Demote the entire subtree. See `org-demote'.
  4170. See also `org-promote'."
  4171. (interactive)
  4172. (save-excursion
  4173. (org-map-tree 'org-demote))
  4174. (org-fix-position-after-promote))
  4175. (defun org-do-promote ()
  4176. "Promote the current heading higher up the tree.
  4177. If the region is active in `transient-mark-mode', promote all headings
  4178. in the region."
  4179. (interactive)
  4180. (save-excursion
  4181. (if (org-region-active-p)
  4182. (org-map-region 'org-promote (region-beginning) (region-end))
  4183. (org-promote)))
  4184. (org-fix-position-after-promote))
  4185. (defun org-do-demote ()
  4186. "Demote the current heading lower down the tree.
  4187. If the region is active in `transient-mark-mode', demote all headings
  4188. in the region."
  4189. (interactive)
  4190. (save-excursion
  4191. (if (org-region-active-p)
  4192. (org-map-region 'org-demote (region-beginning) (region-end))
  4193. (org-demote)))
  4194. (org-fix-position-after-promote))
  4195. (defun org-fix-position-after-promote ()
  4196. "Make sure that after pro/demotion cursor position is right."
  4197. (let ((pos (point)))
  4198. (when (save-excursion
  4199. (beginning-of-line 1)
  4200. (looking-at org-todo-line-regexp)
  4201. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4202. (cond ((eobp) (insert " "))
  4203. ((eolp) (insert " "))
  4204. ((equal (char-after) ?\ ) (forward-char 1))))))
  4205. (defun org-reduced-level (l)
  4206. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4207. (defun org-get-valid-level (level &optional change)
  4208. "Rectify a level change under the influence of `org-odd-levels-only'
  4209. LEVEL is a current level, CHANGE is by how much the level should be
  4210. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4211. even level numbers will become the next higher odd number."
  4212. (if org-odd-levels-only
  4213. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4214. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4215. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4216. (max 1 (+ level change))))
  4217. (if (boundp 'define-obsolete-function-alias)
  4218. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4219. (define-obsolete-function-alias 'org-get-legal-level
  4220. 'org-get-valid-level)
  4221. (define-obsolete-function-alias 'org-get-legal-level
  4222. 'org-get-valid-level "23.1")))
  4223. (defun org-promote ()
  4224. "Promote the current heading higher up the tree.
  4225. If the region is active in `transient-mark-mode', promote all headings
  4226. in the region."
  4227. (org-back-to-heading t)
  4228. (let* ((level (save-match-data (funcall outline-level)))
  4229. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4230. (diff (abs (- level (length up-head) -1))))
  4231. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4232. (replace-match up-head nil t)
  4233. ;; Fixup tag positioning
  4234. (and org-auto-align-tags (org-set-tags nil t))
  4235. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4236. (defun org-demote ()
  4237. "Demote the current heading lower down the tree.
  4238. If the region is active in `transient-mark-mode', demote all headings
  4239. in the region."
  4240. (org-back-to-heading t)
  4241. (let* ((level (save-match-data (funcall outline-level)))
  4242. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4243. (diff (abs (- level (length down-head) -1))))
  4244. (replace-match down-head nil t)
  4245. ;; Fixup tag positioning
  4246. (and org-auto-align-tags (org-set-tags nil t))
  4247. (if org-adapt-indentation (org-fixup-indentation diff))))
  4248. (defun org-map-tree (fun)
  4249. "Call FUN for every heading underneath the current one."
  4250. (org-back-to-heading)
  4251. (let ((level (funcall outline-level)))
  4252. (save-excursion
  4253. (funcall fun)
  4254. (while (and (progn
  4255. (outline-next-heading)
  4256. (> (funcall outline-level) level))
  4257. (not (eobp)))
  4258. (funcall fun)))))
  4259. (defun org-map-region (fun beg end)
  4260. "Call FUN for every heading between BEG and END."
  4261. (let ((org-ignore-region t))
  4262. (save-excursion
  4263. (setq end (copy-marker end))
  4264. (goto-char beg)
  4265. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4266. (< (point) end))
  4267. (funcall fun))
  4268. (while (and (progn
  4269. (outline-next-heading)
  4270. (< (point) end))
  4271. (not (eobp)))
  4272. (funcall fun)))))
  4273. (defun org-fixup-indentation (diff)
  4274. "Change the indentation in the current entry by DIFF
  4275. However, if any line in the current entry has no indentation, or if it
  4276. would end up with no indentation after the change, nothing at all is done."
  4277. (save-excursion
  4278. (let ((end (save-excursion (outline-next-heading)
  4279. (point-marker)))
  4280. (prohibit (if (> diff 0)
  4281. "^\\S-"
  4282. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4283. col)
  4284. (unless (save-excursion (end-of-line 1)
  4285. (re-search-forward prohibit end t))
  4286. (while (and (< (point) end)
  4287. (re-search-forward "^[ \t]+" end t))
  4288. (goto-char (match-end 0))
  4289. (setq col (current-column))
  4290. (if (< diff 0) (replace-match ""))
  4291. (indent-to (+ diff col))))
  4292. (move-marker end nil))))
  4293. (defun org-convert-to-odd-levels ()
  4294. "Convert an org-mode file with all levels allowed to one with odd levels.
  4295. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4296. level 5 etc."
  4297. (interactive)
  4298. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4299. (let ((org-odd-levels-only nil) n)
  4300. (save-excursion
  4301. (goto-char (point-min))
  4302. (while (re-search-forward "^\\*\\*+ " nil t)
  4303. (setq n (- (length (match-string 0)) 2))
  4304. (while (>= (setq n (1- n)) 0)
  4305. (org-demote))
  4306. (end-of-line 1))))))
  4307. (defun org-convert-to-oddeven-levels ()
  4308. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4309. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4310. section with an even level, conversion would destroy the structure of the file. An error
  4311. is signaled in this case."
  4312. (interactive)
  4313. (goto-char (point-min))
  4314. ;; First check if there are no even levels
  4315. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4316. (org-show-context t)
  4317. (error "Not all levels are odd in this file. Conversion not possible."))
  4318. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4319. (let ((org-odd-levels-only nil) n)
  4320. (save-excursion
  4321. (goto-char (point-min))
  4322. (while (re-search-forward "^\\*\\*+ " nil t)
  4323. (setq n (/ (1- (length (match-string 0))) 2))
  4324. (while (>= (setq n (1- n)) 0)
  4325. (org-promote))
  4326. (end-of-line 1))))))
  4327. (defun org-tr-level (n)
  4328. "Make N odd if required."
  4329. (if org-odd-levels-only (1+ (/ n 2)) n))
  4330. ;;; Vertical tree motion, cutting and pasting of subtrees
  4331. (defun org-move-subtree-up (&optional arg)
  4332. "Move the current subtree up past ARG headlines of the same level."
  4333. (interactive "p")
  4334. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4335. (defun org-move-subtree-down (&optional arg)
  4336. "Move the current subtree down past ARG headlines of the same level."
  4337. (interactive "p")
  4338. (setq arg (prefix-numeric-value arg))
  4339. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4340. 'outline-get-last-sibling))
  4341. (ins-point (make-marker))
  4342. (cnt (abs arg))
  4343. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4344. ;; Select the tree
  4345. (org-back-to-heading)
  4346. (setq beg0 (point))
  4347. (save-excursion
  4348. (setq ne-beg (org-back-over-empty-lines))
  4349. (setq beg (point)))
  4350. (save-match-data
  4351. (save-excursion (outline-end-of-heading)
  4352. (setq folded (org-invisible-p)))
  4353. (outline-end-of-subtree))
  4354. (outline-next-heading)
  4355. (setq ne-end (org-back-over-empty-lines))
  4356. (setq end (point))
  4357. (goto-char beg0)
  4358. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4359. ;; include less whitespace
  4360. (save-excursion
  4361. (goto-char beg)
  4362. (forward-line (- ne-beg ne-end))
  4363. (setq beg (point))))
  4364. ;; Find insertion point, with error handling
  4365. (while (> cnt 0)
  4366. (or (and (funcall movfunc) (looking-at outline-regexp))
  4367. (progn (goto-char beg0)
  4368. (error "Cannot move past superior level or buffer limit")))
  4369. (setq cnt (1- cnt)))
  4370. (if (> arg 0)
  4371. ;; Moving forward - still need to move over subtree
  4372. (progn (org-end-of-subtree t t)
  4373. (save-excursion
  4374. (org-back-over-empty-lines)
  4375. (or (bolp) (newline)))))
  4376. (setq ne-ins (org-back-over-empty-lines))
  4377. (move-marker ins-point (point))
  4378. (setq txt (buffer-substring beg end))
  4379. (org-save-markers-in-region beg end)
  4380. (delete-region beg end)
  4381. (outline-flag-region (1- beg) beg nil)
  4382. (outline-flag-region (1- (point)) (point) nil)
  4383. (let ((bbb (point)))
  4384. (insert-before-markers txt)
  4385. (org-reinstall-markers-in-region bbb)
  4386. (move-marker ins-point bbb))
  4387. (or (bolp) (insert "\n"))
  4388. (setq ins-end (point))
  4389. (goto-char ins-point)
  4390. (org-skip-whitespace)
  4391. (when (and (< arg 0)
  4392. (org-first-sibling-p)
  4393. (> ne-ins ne-beg))
  4394. ;; Move whitespace back to beginning
  4395. (save-excursion
  4396. (goto-char ins-end)
  4397. (let ((kill-whole-line t))
  4398. (kill-line (- ne-ins ne-beg)) (point)))
  4399. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4400. (move-marker ins-point nil)
  4401. (org-compact-display-after-subtree-move)
  4402. (org-show-empty-lines-in-parent)
  4403. (unless folded
  4404. (org-show-entry)
  4405. (show-children)
  4406. (org-cycle-hide-drawers 'children))))
  4407. (defvar org-subtree-clip ""
  4408. "Clipboard for cut and paste of subtrees.
  4409. This is actually only a copy of the kill, because we use the normal kill
  4410. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4411. (defvar org-subtree-clip-folded nil
  4412. "Was the last copied subtree folded?
  4413. This is used to fold the tree back after pasting.")
  4414. (defun org-cut-subtree (&optional n)
  4415. "Cut the current subtree into the clipboard.
  4416. With prefix arg N, cut this many sequential subtrees.
  4417. This is a short-hand for marking the subtree and then cutting it."
  4418. (interactive "p")
  4419. (org-copy-subtree n 'cut))
  4420. (defun org-copy-subtree (&optional n cut force-store-markers)
  4421. "Cut the current subtree into the clipboard.
  4422. With prefix arg N, cut this many sequential subtrees.
  4423. This is a short-hand for marking the subtree and then copying it.
  4424. If CUT is non-nil, actually cut the subtree.
  4425. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4426. of some markers in the region, even if CUT is non-nil. This is
  4427. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4428. (interactive "p")
  4429. (let (beg end folded (beg0 (point)))
  4430. (if (interactive-p)
  4431. (org-back-to-heading nil) ; take what looks like a subtree
  4432. (org-back-to-heading t)) ; take what is really there
  4433. (org-back-over-empty-lines)
  4434. (setq beg (point))
  4435. (skip-chars-forward " \t\r\n")
  4436. (save-match-data
  4437. (save-excursion (outline-end-of-heading)
  4438. (setq folded (org-invisible-p)))
  4439. (condition-case nil
  4440. (outline-forward-same-level (1- n))
  4441. (error nil))
  4442. (org-end-of-subtree t t))
  4443. (org-back-over-empty-lines)
  4444. (setq end (point))
  4445. (goto-char beg0)
  4446. (when (> end beg)
  4447. (setq org-subtree-clip-folded folded)
  4448. (when (or cut force-store-markers)
  4449. (org-save-markers-in-region beg end))
  4450. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4451. (setq org-subtree-clip (current-kill 0))
  4452. (message "%s: Subtree(s) with %d characters"
  4453. (if cut "Cut" "Copied")
  4454. (length org-subtree-clip)))))
  4455. (defun org-paste-subtree (&optional level tree)
  4456. "Paste the clipboard as a subtree, with modification of headline level.
  4457. The entire subtree is promoted or demoted in order to match a new headline
  4458. level. By default, the new level is derived from the visible headings
  4459. before and after the insertion point, and taken to be the inferior headline
  4460. level of the two. So if the previous visible heading is level 3 and the
  4461. next is level 4 (or vice versa), level 4 will be used for insertion.
  4462. This makes sure that the subtree remains an independent subtree and does
  4463. not swallow low level entries.
  4464. You can also force a different level, either by using a numeric prefix
  4465. argument, or by inserting the heading marker by hand. For example, if the
  4466. cursor is after \"*****\", then the tree will be shifted to level 5.
  4467. If you want to insert the tree as is, just use \\[yank].
  4468. If optional TREE is given, use this text instead of the kill ring."
  4469. (interactive "P")
  4470. (unless (org-kill-is-subtree-p tree)
  4471. (error "%s"
  4472. (substitute-command-keys
  4473. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4474. (let* ((visp (not (org-invisible-p)))
  4475. (txt (or tree (and kill-ring (current-kill 0))))
  4476. (^re (concat "^\\(" outline-regexp "\\)"))
  4477. (re (concat "\\(" outline-regexp "\\)"))
  4478. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4479. (old-level (if (string-match ^re txt)
  4480. (- (match-end 0) (match-beginning 0) 1)
  4481. -1))
  4482. (force-level (cond (level (prefix-numeric-value level))
  4483. ((string-match
  4484. ^re_ (buffer-substring (point-at-bol) (point)))
  4485. (- (match-end 1) (match-beginning 1)))
  4486. (t nil)))
  4487. (previous-level (save-excursion
  4488. (condition-case nil
  4489. (progn
  4490. (outline-previous-visible-heading 1)
  4491. (if (looking-at re)
  4492. (- (match-end 0) (match-beginning 0) 1)
  4493. 1))
  4494. (error 1))))
  4495. (next-level (save-excursion
  4496. (condition-case nil
  4497. (progn
  4498. (or (looking-at outline-regexp)
  4499. (outline-next-visible-heading 1))
  4500. (if (looking-at re)
  4501. (- (match-end 0) (match-beginning 0) 1)
  4502. 1))
  4503. (error 1))))
  4504. (new-level (or force-level (max previous-level next-level)))
  4505. (shift (if (or (= old-level -1)
  4506. (= new-level -1)
  4507. (= old-level new-level))
  4508. 0
  4509. (- new-level old-level)))
  4510. (delta (if (> shift 0) -1 1))
  4511. (func (if (> shift 0) 'org-demote 'org-promote))
  4512. (org-odd-levels-only nil)
  4513. beg end)
  4514. ;; Remove the forced level indicator
  4515. (if force-level
  4516. (delete-region (point-at-bol) (point)))
  4517. ;; Paste
  4518. (beginning-of-line 1)
  4519. (org-back-over-empty-lines)
  4520. (setq beg (point))
  4521. (insert-before-markers txt)
  4522. (unless (string-match "\n\\'" txt) (insert "\n"))
  4523. (org-reinstall-markers-in-region beg)
  4524. (setq end (point))
  4525. (goto-char beg)
  4526. (skip-chars-forward " \t\n\r")
  4527. (setq beg (point))
  4528. (if (and (org-invisible-p) visp)
  4529. (save-excursion (outline-show-heading)))
  4530. ;; Shift if necessary
  4531. (unless (= shift 0)
  4532. (save-restriction
  4533. (narrow-to-region beg end)
  4534. (while (not (= shift 0))
  4535. (org-map-region func (point-min) (point-max))
  4536. (setq shift (+ delta shift)))
  4537. (goto-char (point-min))))
  4538. (when (interactive-p)
  4539. (message "Clipboard pasted as level %d subtree" new-level))
  4540. (if (and kill-ring
  4541. (eq org-subtree-clip (current-kill 0))
  4542. org-subtree-clip-folded)
  4543. ;; The tree was folded before it was killed/copied
  4544. (hide-subtree))))
  4545. (defun org-kill-is-subtree-p (&optional txt)
  4546. "Check if the current kill is an outline subtree, or a set of trees.
  4547. Returns nil if kill does not start with a headline, or if the first
  4548. headline level is not the largest headline level in the tree.
  4549. So this will actually accept several entries of equal levels as well,
  4550. which is OK for `org-paste-subtree'.
  4551. If optional TXT is given, check this string instead of the current kill."
  4552. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4553. (start-level (and kill
  4554. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4555. org-outline-regexp "\\)")
  4556. kill)
  4557. (- (match-end 2) (match-beginning 2) 1)))
  4558. (re (concat "^" org-outline-regexp))
  4559. (start (1+ (match-beginning 2))))
  4560. (if (not start-level)
  4561. (progn
  4562. nil) ;; does not even start with a heading
  4563. (catch 'exit
  4564. (while (setq start (string-match re kill (1+ start)))
  4565. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4566. (throw 'exit nil)))
  4567. t))))
  4568. (defvar org-markers-to-move nil
  4569. "Markers that should be moved with a cut-and-paste operation.
  4570. Those markers are stored together with their positions relative to
  4571. the start of the region.")
  4572. (defun org-save-markers-in-region (beg end)
  4573. "Check markers in region.
  4574. If these markers are between BEG and END, record their position relative
  4575. to BEG, so that after moving the block of text, we can put the markers back
  4576. into place.
  4577. This function gets called just before an entry or tree gets cut from the
  4578. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4579. called immediately, to move the markers with the entries."
  4580. (setq org-markers-to-move nil)
  4581. (when (featurep 'org-clock)
  4582. (org-clock-save-markers-for-cut-and-paste beg end))
  4583. (when (featurep 'org-agenda)
  4584. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4585. (defun org-check-and-save-marker (marker beg end)
  4586. "Check if MARKER is between BEG and END.
  4587. If yes, remember the marker and the distance to BEG."
  4588. (when (and (marker-buffer marker)
  4589. (equal (marker-buffer marker) (current-buffer)))
  4590. (if (and (>= marker beg) (< marker end))
  4591. (push (cons marker (- marker beg)) org-markers-to-move))))
  4592. (defun org-reinstall-markers-in-region (beg)
  4593. "Move all remembered markers to their position relative to BEG."
  4594. (mapc (lambda (x)
  4595. (move-marker (car x) (+ beg (cdr x))))
  4596. org-markers-to-move)
  4597. (setq org-markers-to-move nil))
  4598. (defun org-narrow-to-subtree ()
  4599. "Narrow buffer to the current subtree."
  4600. (interactive)
  4601. (save-excursion
  4602. (save-match-data
  4603. (narrow-to-region
  4604. (progn (org-back-to-heading) (point))
  4605. (progn (org-end-of-subtree t t) (point))))))
  4606. ;;; Outline Sorting
  4607. (defun org-sort (with-case)
  4608. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4609. Optional argument WITH-CASE means sort case-sensitively."
  4610. (interactive "P")
  4611. (if (org-at-table-p)
  4612. (org-call-with-arg 'org-table-sort-lines with-case)
  4613. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4614. (defun org-sort-remove-invisible (s)
  4615. (remove-text-properties 0 (length s) org-rm-props s)
  4616. (while (string-match org-bracket-link-regexp s)
  4617. (setq s (replace-match (if (match-end 2)
  4618. (match-string 3 s)
  4619. (match-string 1 s)) t t s)))
  4620. s)
  4621. (defvar org-priority-regexp) ; defined later in the file
  4622. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4623. "Sort entries on a certain level of an outline tree.
  4624. If there is an active region, the entries in the region are sorted.
  4625. Else, if the cursor is before the first entry, sort the top-level items.
  4626. Else, the children of the entry at point are sorted.
  4627. Sorting can be alphabetically, numerically, and by date/time as given by
  4628. the first time stamp in the entry. The command prompts for the sorting
  4629. type unless it has been given to the function through the SORTING-TYPE
  4630. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4631. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4632. called with point at the beginning of the record. It must return either
  4633. a string or a number that should serve as the sorting key for that record.
  4634. Comparing entries ignores case by default. However, with an optional argument
  4635. WITH-CASE, the sorting considers case as well."
  4636. (interactive "P")
  4637. (let ((case-func (if with-case 'identity 'downcase))
  4638. start beg end stars re re2
  4639. txt what tmp plain-list-p)
  4640. ;; Find beginning and end of region to sort
  4641. (cond
  4642. ((org-region-active-p)
  4643. ;; we will sort the region
  4644. (setq end (region-end)
  4645. what "region")
  4646. (goto-char (region-beginning))
  4647. (if (not (org-on-heading-p)) (outline-next-heading))
  4648. (setq start (point)))
  4649. ((org-at-item-p)
  4650. ;; we will sort this plain list
  4651. (org-beginning-of-item-list) (setq start (point))
  4652. (org-end-of-item-list) (setq end (point))
  4653. (goto-char start)
  4654. (setq plain-list-p t
  4655. what "plain list"))
  4656. ((or (org-on-heading-p)
  4657. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4658. ;; we will sort the children of the current headline
  4659. (org-back-to-heading)
  4660. (setq start (point)
  4661. end (progn (org-end-of-subtree t t)
  4662. (org-back-over-empty-lines)
  4663. (point))
  4664. what "children")
  4665. (goto-char start)
  4666. (show-subtree)
  4667. (outline-next-heading))
  4668. (t
  4669. ;; we will sort the top-level entries in this file
  4670. (goto-char (point-min))
  4671. (or (org-on-heading-p) (outline-next-heading))
  4672. (setq start (point) end (point-max) what "top-level")
  4673. (goto-char start)
  4674. (show-all)))
  4675. (setq beg (point))
  4676. (if (>= beg end) (error "Nothing to sort"))
  4677. (unless plain-list-p
  4678. (looking-at "\\(\\*+\\)")
  4679. (setq stars (match-string 1)
  4680. re (concat "^" (regexp-quote stars) " +")
  4681. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4682. txt (buffer-substring beg end))
  4683. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4684. (if (and (not (equal stars "*")) (string-match re2 txt))
  4685. (error "Region to sort contains a level above the first entry")))
  4686. (unless sorting-type
  4687. (message
  4688. (if plain-list-p
  4689. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4690. "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:")
  4691. what)
  4692. (setq sorting-type (read-char-exclusive))
  4693. (and (= (downcase sorting-type) ?f)
  4694. (setq getkey-func
  4695. (completing-read "Sort using function: "
  4696. obarray 'fboundp t nil nil))
  4697. (setq getkey-func (intern getkey-func)))
  4698. (and (= (downcase sorting-type) ?r)
  4699. (setq property
  4700. (completing-read "Property: "
  4701. (mapcar 'list (org-buffer-property-keys t))
  4702. nil t))))
  4703. (message "Sorting entries...")
  4704. (save-restriction
  4705. (narrow-to-region start end)
  4706. (let ((dcst (downcase sorting-type))
  4707. (now (current-time)))
  4708. (sort-subr
  4709. (/= dcst sorting-type)
  4710. ;; This function moves to the beginning character of the "record" to
  4711. ;; be sorted.
  4712. (if plain-list-p
  4713. (lambda nil
  4714. (if (org-at-item-p) t (goto-char (point-max))))
  4715. (lambda nil
  4716. (if (re-search-forward re nil t)
  4717. (goto-char (match-beginning 0))
  4718. (goto-char (point-max)))))
  4719. ;; This function moves to the last character of the "record" being
  4720. ;; sorted.
  4721. (if plain-list-p
  4722. 'org-end-of-item
  4723. (lambda nil
  4724. (save-match-data
  4725. (condition-case nil
  4726. (outline-forward-same-level 1)
  4727. (error
  4728. (goto-char (point-max)))))))
  4729. ;; This function returns the value that gets sorted against.
  4730. (if plain-list-p
  4731. (lambda nil
  4732. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4733. (cond
  4734. ((= dcst ?n)
  4735. (string-to-number (buffer-substring (match-end 0)
  4736. (point-at-eol))))
  4737. ((= dcst ?a)
  4738. (buffer-substring (match-end 0) (point-at-eol)))
  4739. ((= dcst ?t)
  4740. (if (re-search-forward org-ts-regexp
  4741. (point-at-eol) t)
  4742. (org-time-string-to-time (match-string 0))
  4743. now))
  4744. ((= dcst ?f)
  4745. (if getkey-func
  4746. (progn
  4747. (setq tmp (funcall getkey-func))
  4748. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4749. tmp)
  4750. (error "Invalid key function `%s'" getkey-func)))
  4751. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4752. (lambda nil
  4753. (cond
  4754. ((= dcst ?n)
  4755. (if (looking-at outline-regexp)
  4756. (string-to-number (buffer-substring (match-end 0)
  4757. (point-at-eol)))
  4758. nil))
  4759. ((= dcst ?a)
  4760. (funcall case-func (buffer-substring (point-at-bol)
  4761. (point-at-eol))))
  4762. ((= dcst ?t)
  4763. (if (re-search-forward org-ts-regexp
  4764. (save-excursion
  4765. (forward-line 2)
  4766. (point)) t)
  4767. (org-time-string-to-time (match-string 0))
  4768. now))
  4769. ((= dcst ?p)
  4770. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4771. (string-to-char (match-string 2))
  4772. org-default-priority))
  4773. ((= dcst ?r)
  4774. (or (org-entry-get nil property) ""))
  4775. ((= dcst ?o)
  4776. (if (looking-at org-complex-heading-regexp)
  4777. (- 9999 (length (member (match-string 2)
  4778. org-todo-keywords-1)))))
  4779. ((= dcst ?f)
  4780. (if getkey-func
  4781. (progn
  4782. (setq tmp (funcall getkey-func))
  4783. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4784. tmp)
  4785. (error "Invalid key function `%s'" getkey-func)))
  4786. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4787. nil
  4788. (cond
  4789. ((= dcst ?a) 'string<)
  4790. ((= dcst ?t) 'time-less-p)
  4791. (t nil)))))
  4792. (message "Sorting entries...done")))
  4793. (defun org-do-sort (table what &optional with-case sorting-type)
  4794. "Sort TABLE of WHAT according to SORTING-TYPE.
  4795. The user will be prompted for the SORTING-TYPE if the call to this
  4796. function does not specify it. WHAT is only for the prompt, to indicate
  4797. what is being sorted. The sorting key will be extracted from
  4798. the car of the elements of the table.
  4799. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4800. (unless sorting-type
  4801. (message
  4802. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4803. what)
  4804. (setq sorting-type (read-char-exclusive)))
  4805. (let ((dcst (downcase sorting-type))
  4806. extractfun comparefun)
  4807. ;; Define the appropriate functions
  4808. (cond
  4809. ((= dcst ?n)
  4810. (setq extractfun 'string-to-number
  4811. comparefun (if (= dcst sorting-type) '< '>)))
  4812. ((= dcst ?a)
  4813. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4814. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4815. comparefun (if (= dcst sorting-type)
  4816. 'string<
  4817. (lambda (a b) (and (not (string< a b))
  4818. (not (string= a b)))))))
  4819. ((= dcst ?t)
  4820. (setq extractfun
  4821. (lambda (x)
  4822. (if (string-match org-ts-regexp x)
  4823. (time-to-seconds
  4824. (org-time-string-to-time (match-string 0 x)))
  4825. 0))
  4826. comparefun (if (= dcst sorting-type) '< '>)))
  4827. (t (error "Invalid sorting type `%c'" sorting-type)))
  4828. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4829. table)
  4830. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4831. ;;; Editing source examples
  4832. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  4833. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  4834. (defvar org-edit-src-force-single-line nil)
  4835. (defvar org-edit-src-from-org-mode nil)
  4836. (define-minor-mode org-exit-edit-mode
  4837. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  4838. (defun org-edit-src-code ()
  4839. "Edit the source code example at point.
  4840. An indirect buffer is created, and that buffer is then narrowed to the
  4841. example at point and switched to the correct language mode. When done,
  4842. exit by killing the buffer with \\[org-edit-src-exit]."
  4843. (interactive)
  4844. (let ((line (org-current-line))
  4845. (case-fold-search t)
  4846. (msg (substitute-command-keys
  4847. "Edit, then exit with C-c ' (C-c and single quote)"))
  4848. (info (org-edit-src-find-region-and-lang))
  4849. (org-mode-p (eq major-mode 'org-mode))
  4850. beg end lang lang-f single)
  4851. (if (not info)
  4852. nil
  4853. (setq beg (nth 0 info)
  4854. end (nth 1 info)
  4855. lang (nth 2 info)
  4856. single (nth 3 info)
  4857. lang-f (intern (concat lang "-mode")))
  4858. (unless (functionp lang-f)
  4859. (error "No such language mode: %s" lang-f))
  4860. (goto-line line)
  4861. (if (get-buffer "*Org Edit Src Example*")
  4862. (kill-buffer "*Org Edit Src Example*"))
  4863. (switch-to-buffer (make-indirect-buffer (current-buffer)
  4864. "*Org Edit Src Example*"))
  4865. (narrow-to-region beg end)
  4866. (remove-text-properties beg end '(display nil invisible nil
  4867. intangible nil))
  4868. (let ((org-inhibit-startup t))
  4869. (funcall lang-f))
  4870. (set (make-local-variable 'org-edit-src-force-single-line) single)
  4871. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  4872. (when org-mode-p
  4873. (goto-char (point-min))
  4874. (while (re-search-forward "^," nil t)
  4875. (replace-match "")))
  4876. (goto-line line)
  4877. (org-exit-edit-mode)
  4878. (org-set-local 'header-line-format msg)
  4879. (message "%s" msg)
  4880. t)))
  4881. (defun org-edit-src-find-region-and-lang ()
  4882. "Find the region and language for a local edit.
  4883. Return a list with beginning and end of the region, a string representing
  4884. the language, a switch telling of the content should be in a single line."
  4885. (let ((re-list
  4886. '(
  4887. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  4888. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  4889. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  4890. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  4891. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  4892. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  4893. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  4894. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  4895. ("^#\\+begin_example.*\n" "^#\\+end_example" "fundamental")
  4896. ("^#\\+html:" "\n" "html" single-line)
  4897. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  4898. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  4899. ("^#\\+latex:" "\n" "latex" single-line)
  4900. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  4901. ("^#\\+ascii:" "\n" "ascii" single-line)
  4902. ))
  4903. (pos (point))
  4904. re re1 re2 single beg end lang)
  4905. (catch 'exit
  4906. (while (setq entry (pop re-list))
  4907. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  4908. single (nth 3 entry))
  4909. (save-excursion
  4910. (if (or (looking-at re1)
  4911. (re-search-backward re1 nil t))
  4912. (progn
  4913. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  4914. (if (and (re-search-forward re2 nil t)
  4915. (>= (match-end 0) pos))
  4916. (throw 'exit (list beg (match-beginning 0) lang single))))
  4917. (if (or (looking-at re2)
  4918. (re-search-forward re2 nil t))
  4919. (progn
  4920. (setq end (match-beginning 0))
  4921. (if (and (re-search-backward re1 nil t)
  4922. (<= (match-beginning 0) pos))
  4923. (throw 'exit
  4924. (list (match-end 0) end
  4925. (org-edit-src-get-lang lang) single)))))))))))
  4926. (defun org-edit-src-get-lang (lang)
  4927. "Extract the src language."
  4928. (let ((m (match-string 0)))
  4929. (cond
  4930. ((stringp lang) lang)
  4931. ((integerp lang) (match-string lang))
  4932. ((and (eq lang lang)
  4933. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  4934. (match-string 1 m))
  4935. ((and (eq lang lang)
  4936. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  4937. (match-string 1 m))
  4938. (t "fundamental"))))
  4939. (defun org-edit-src-exit ()
  4940. "Exit special edit and protect problematic lines."
  4941. (interactive)
  4942. (unless (buffer-base-buffer (current-buffer))
  4943. (error "This is not an indirect buffer, something is wrong..."))
  4944. (unless (> (point-min) 1)
  4945. (error "This buffer is not narrowed, something is wrong..."))
  4946. (goto-char (point-min))
  4947. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  4948. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  4949. (when (org-bound-and-true-p org-edit-src-force-single-line)
  4950. (goto-char (point-min))
  4951. (while (re-search-forward "\n" nil t)
  4952. (replace-match " "))
  4953. (goto-char (point-min))
  4954. (if (looking-at "\\s-*") (replace-match " "))
  4955. (if (re-search-forward "\\s-+\\'" nil t)
  4956. (replace-match "")))
  4957. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  4958. (goto-char (point-min))
  4959. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  4960. (replace-match ",\\1"))
  4961. (when font-lock-mode
  4962. (font-lock-unfontify-region (point-min) (point-max)))
  4963. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  4964. (kill-buffer (current-buffer)))
  4965. ;;;; Plain list items, including checkboxes
  4966. ;;; Plain list items
  4967. (defun org-at-item-p ()
  4968. "Is point in a line starting a hand-formatted item?"
  4969. (let ((llt org-plain-list-ordered-item-terminator))
  4970. (save-excursion
  4971. (goto-char (point-at-bol))
  4972. (looking-at
  4973. (cond
  4974. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4975. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4976. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4977. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4978. (defun org-in-item-p ()
  4979. "It the cursor inside a plain list item.
  4980. Does not have to be the first line."
  4981. (save-excursion
  4982. (condition-case nil
  4983. (progn
  4984. (org-beginning-of-item)
  4985. (org-at-item-p)
  4986. t)
  4987. (error nil))))
  4988. (defun org-insert-item (&optional checkbox)
  4989. "Insert a new item at the current level.
  4990. Return t when things worked, nil when we are not in an item."
  4991. (when (save-excursion
  4992. (condition-case nil
  4993. (progn
  4994. (org-beginning-of-item)
  4995. (org-at-item-p)
  4996. (if (org-invisible-p) (error "Invisible item"))
  4997. t)
  4998. (error nil)))
  4999. (let* ((bul (match-string 0))
  5000. (descp (save-excursion (goto-char (match-beginning 0))
  5001. (beginning-of-line 1)
  5002. (save-match-data
  5003. (looking-at "[ \t]*.*? ::"))))
  5004. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  5005. (match-end 0)))
  5006. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  5007. pos)
  5008. (if descp (setq checkbox nil))
  5009. (cond
  5010. ((and (org-at-item-p) (<= (point) eow))
  5011. ;; before the bullet
  5012. (beginning-of-line 1)
  5013. (open-line (if blank 2 1)))
  5014. ((<= (point) eow)
  5015. (beginning-of-line 1))
  5016. (t
  5017. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  5018. (end-of-line 1)
  5019. (delete-horizontal-space))
  5020. (newline (if blank 2 1))))
  5021. (insert bul
  5022. (if checkbox "[ ]" "")
  5023. (if descp (concat (if checkbox " " "")
  5024. (read-string "Term: ") " :: ") ""))
  5025. (just-one-space)
  5026. (setq pos (point))
  5027. (end-of-line 1)
  5028. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  5029. (org-maybe-renumber-ordered-list)
  5030. (and checkbox (org-update-checkbox-count-maybe))
  5031. t))
  5032. ;;; Checkboxes
  5033. (defun org-at-item-checkbox-p ()
  5034. "Is point at a line starting a plain-list item with a checklet?"
  5035. (and (org-at-item-p)
  5036. (save-excursion
  5037. (goto-char (match-end 0))
  5038. (skip-chars-forward " \t")
  5039. (looking-at "\\[[- X]\\]"))))
  5040. (defun org-toggle-checkbox (&optional arg)
  5041. "Toggle the checkbox in the current line."
  5042. (interactive "P")
  5043. (catch 'exit
  5044. (let (beg end status (firstnew 'unknown))
  5045. (cond
  5046. ((org-region-active-p)
  5047. (setq beg (region-beginning) end (region-end)))
  5048. ((org-on-heading-p)
  5049. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  5050. ((org-at-item-checkbox-p)
  5051. (let ((pos (point)))
  5052. (replace-match
  5053. (cond (arg "[-]")
  5054. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  5055. (t "[ ]"))
  5056. t t)
  5057. (goto-char pos))
  5058. (throw 'exit t))
  5059. (t (error "Not at a checkbox or heading, and no active region")))
  5060. (save-excursion
  5061. (goto-char beg)
  5062. (while (< (point) end)
  5063. (when (org-at-item-checkbox-p)
  5064. (setq status (equal (match-string 0) "[X]"))
  5065. (when (eq firstnew 'unknown)
  5066. (setq firstnew (not status)))
  5067. (replace-match
  5068. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  5069. (beginning-of-line 2)))))
  5070. (org-update-checkbox-count-maybe))
  5071. (defun org-update-checkbox-count-maybe ()
  5072. "Update checkbox statistics unless turned off by user."
  5073. (when org-provide-checkbox-statistics
  5074. (org-update-checkbox-count)))
  5075. (defun org-update-checkbox-count (&optional all)
  5076. "Update the checkbox statistics in the current section.
  5077. This will find all statistic cookies like [57%] and [6/12] and update them
  5078. with the current numbers. With optional prefix argument ALL, do this for
  5079. the whole buffer."
  5080. (interactive "P")
  5081. (save-excursion
  5082. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  5083. (beg (condition-case nil
  5084. (progn (outline-back-to-heading) (point))
  5085. (error (point-min))))
  5086. (end (move-marker (make-marker)
  5087. (progn (outline-next-heading) (point))))
  5088. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  5089. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  5090. (re-find (concat re "\\|" re-box))
  5091. beg-cookie end-cookie is-percent c-on c-off lim
  5092. eline curr-ind next-ind continue-from startsearch
  5093. (cstat 0)
  5094. )
  5095. (when all
  5096. (goto-char (point-min))
  5097. (outline-next-heading)
  5098. (setq beg (point) end (point-max)))
  5099. (goto-char end)
  5100. ;; find each statistic cookie
  5101. (while (re-search-backward re-find beg t)
  5102. (setq beg-cookie (match-beginning 1)
  5103. end-cookie (match-end 1)
  5104. cstat (+ cstat (if end-cookie 1 0))
  5105. startsearch (point-at-eol)
  5106. continue-from (point-at-bol)
  5107. is-percent (match-beginning 2)
  5108. lim (cond
  5109. ((org-on-heading-p) (outline-next-heading) (point))
  5110. ((org-at-item-p) (org-end-of-item) (point))
  5111. (t nil))
  5112. c-on 0
  5113. c-off 0)
  5114. (when lim
  5115. ;; find first checkbox for this cookie and gather
  5116. ;; statistics from all that are at this indentation level
  5117. (goto-char startsearch)
  5118. (if (re-search-forward re-box lim t)
  5119. (progn
  5120. (org-beginning-of-item)
  5121. (setq curr-ind (org-get-indentation))
  5122. (setq next-ind curr-ind)
  5123. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  5124. (save-excursion (end-of-line) (setq eline (point)))
  5125. (if (re-search-forward re-box eline t)
  5126. (if (member (match-string 2) '("[ ]" "[-]"))
  5127. (setq c-off (1+ c-off))
  5128. (setq c-on (1+ c-on))
  5129. )
  5130. )
  5131. (org-end-of-item)
  5132. (setq next-ind (org-get-indentation))
  5133. )))
  5134. (goto-char continue-from)
  5135. ;; update cookie
  5136. (when end-cookie
  5137. (delete-region beg-cookie end-cookie)
  5138. (goto-char beg-cookie)
  5139. (insert
  5140. (if is-percent
  5141. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  5142. (format "[%d/%d]" c-on (+ c-on c-off)))))
  5143. ;; update items checkbox if it has one
  5144. (when (org-at-item-p)
  5145. (org-beginning-of-item)
  5146. (when (and (> (+ c-on c-off) 0)
  5147. (re-search-forward re-box (point-at-eol) t))
  5148. (setq beg-cookie (match-beginning 2)
  5149. end-cookie (match-end 2))
  5150. (delete-region beg-cookie end-cookie)
  5151. (goto-char beg-cookie)
  5152. (cond ((= c-off 0) (insert "[X]"))
  5153. ((= c-on 0) (insert "[ ]"))
  5154. (t (insert "[-]")))
  5155. )))
  5156. (goto-char continue-from))
  5157. (when (interactive-p)
  5158. (message "Checkbox satistics updated %s (%d places)"
  5159. (if all "in entire file" "in current outline entry") cstat)))))
  5160. (defun org-get-checkbox-statistics-face ()
  5161. "Select the face for checkbox statistics.
  5162. The face will be `org-done' when all relevant boxes are checked. Otherwise
  5163. it will be `org-todo'."
  5164. (if (match-end 1)
  5165. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  5166. (if (and (> (match-end 2) (match-beginning 2))
  5167. (equal (match-string 2) (match-string 3)))
  5168. 'org-done
  5169. 'org-todo)))
  5170. (defun org-get-indentation (&optional line)
  5171. "Get the indentation of the current line, interpreting tabs.
  5172. When LINE is given, assume it represents a line and compute its indentation."
  5173. (if line
  5174. (if (string-match "^ *" (org-remove-tabs line))
  5175. (match-end 0))
  5176. (save-excursion
  5177. (beginning-of-line 1)
  5178. (skip-chars-forward " \t")
  5179. (current-column))))
  5180. (defun org-remove-tabs (s &optional width)
  5181. "Replace tabulators in S with spaces.
  5182. Assumes that s is a single line, starting in column 0."
  5183. (setq width (or width tab-width))
  5184. (while (string-match "\t" s)
  5185. (setq s (replace-match
  5186. (make-string
  5187. (- (* width (/ (+ (match-beginning 0) width) width))
  5188. (match-beginning 0)) ?\ )
  5189. t t s)))
  5190. s)
  5191. (defun org-fix-indentation (line ind)
  5192. "Fix indentation in LINE.
  5193. IND is a cons cell with target and minimum indentation.
  5194. If the current indenation in LINE is smaller than the minimum,
  5195. leave it alone. If it is larger than ind, set it to the target."
  5196. (let* ((l (org-remove-tabs line))
  5197. (i (org-get-indentation l))
  5198. (i1 (car ind)) (i2 (cdr ind)))
  5199. (if (>= i i2) (setq l (substring line i2)))
  5200. (if (> i1 0)
  5201. (concat (make-string i1 ?\ ) l)
  5202. l)))
  5203. (defun org-beginning-of-item ()
  5204. "Go to the beginning of the current hand-formatted item.
  5205. If the cursor is not in an item, throw an error."
  5206. (interactive)
  5207. (let ((pos (point))
  5208. (limit (save-excursion
  5209. (condition-case nil
  5210. (progn
  5211. (org-back-to-heading)
  5212. (beginning-of-line 2) (point))
  5213. (error (point-min)))))
  5214. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5215. ind ind1)
  5216. (if (org-at-item-p)
  5217. (beginning-of-line 1)
  5218. (beginning-of-line 1)
  5219. (skip-chars-forward " \t")
  5220. (setq ind (current-column))
  5221. (if (catch 'exit
  5222. (while t
  5223. (beginning-of-line 0)
  5224. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  5225. (if (looking-at "[ \t]*$")
  5226. (setq ind1 ind-empty)
  5227. (skip-chars-forward " \t")
  5228. (setq ind1 (current-column)))
  5229. (if (< ind1 ind)
  5230. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  5231. nil
  5232. (goto-char pos)
  5233. (error "Not in an item")))))
  5234. (defun org-end-of-item ()
  5235. "Go to the end of the current hand-formatted item.
  5236. If the cursor is not in an item, throw an error."
  5237. (interactive)
  5238. (let* ((pos (point))
  5239. ind1
  5240. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  5241. (limit (save-excursion (outline-next-heading) (point)))
  5242. (ind (save-excursion
  5243. (org-beginning-of-item)
  5244. (skip-chars-forward " \t")
  5245. (current-column)))
  5246. (end (catch 'exit
  5247. (while t
  5248. (beginning-of-line 2)
  5249. (if (eobp) (throw 'exit (point)))
  5250. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5251. (if (looking-at "[ \t]*$")
  5252. (setq ind1 ind-empty)
  5253. (skip-chars-forward " \t")
  5254. (setq ind1 (current-column)))
  5255. (if (<= ind1 ind)
  5256. (throw 'exit (point-at-bol)))))))
  5257. (if end
  5258. (goto-char end)
  5259. (goto-char pos)
  5260. (error "Not in an item"))))
  5261. (defun org-next-item ()
  5262. "Move to the beginning of the next item in the current plain list.
  5263. Error if not at a plain list, or if this is the last item in the list."
  5264. (interactive)
  5265. (let (ind ind1 (pos (point)))
  5266. (org-beginning-of-item)
  5267. (setq ind (org-get-indentation))
  5268. (org-end-of-item)
  5269. (setq ind1 (org-get-indentation))
  5270. (unless (and (org-at-item-p) (= ind ind1))
  5271. (goto-char pos)
  5272. (error "On last item"))))
  5273. (defun org-previous-item ()
  5274. "Move to the beginning of the previous item in the current plain list.
  5275. Error if not at a plain list, or if this is the first item in the list."
  5276. (interactive)
  5277. (let (beg ind ind1 (pos (point)))
  5278. (org-beginning-of-item)
  5279. (setq beg (point))
  5280. (setq ind (org-get-indentation))
  5281. (goto-char beg)
  5282. (catch 'exit
  5283. (while t
  5284. (beginning-of-line 0)
  5285. (if (looking-at "[ \t]*$")
  5286. nil
  5287. (if (<= (setq ind1 (org-get-indentation)) ind)
  5288. (throw 'exit t)))))
  5289. (condition-case nil
  5290. (if (or (not (org-at-item-p))
  5291. (< ind1 (1- ind)))
  5292. (error "")
  5293. (org-beginning-of-item))
  5294. (error (goto-char pos)
  5295. (error "On first item")))))
  5296. (defun org-first-list-item-p ()
  5297. "Is this heading the item in a plain list?"
  5298. (unless (org-at-item-p)
  5299. (error "Not at a plain list item"))
  5300. (org-beginning-of-item)
  5301. (= (point) (save-excursion (org-beginning-of-item-list))))
  5302. (defun org-move-item-down ()
  5303. "Move the plain list item at point down, i.e. swap with following item.
  5304. Subitems (items with larger indentation) are considered part of the item,
  5305. so this really moves item trees."
  5306. (interactive)
  5307. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5308. (org-beginning-of-item)
  5309. (setq beg0 (point))
  5310. (save-excursion
  5311. (setq ne-beg (org-back-over-empty-lines))
  5312. (setq beg (point)))
  5313. (goto-char beg0)
  5314. (setq ind (org-get-indentation))
  5315. (org-end-of-item)
  5316. (setq end0 (point))
  5317. (setq ind1 (org-get-indentation))
  5318. (setq ne-end (org-back-over-empty-lines))
  5319. (setq end (point))
  5320. (goto-char beg0)
  5321. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5322. ;; include less whitespace
  5323. (save-excursion
  5324. (goto-char beg)
  5325. (forward-line (- ne-beg ne-end))
  5326. (setq beg (point))))
  5327. (goto-char end0)
  5328. (if (and (org-at-item-p) (= ind ind1))
  5329. (progn
  5330. (org-end-of-item)
  5331. (org-back-over-empty-lines)
  5332. (setq txt (buffer-substring beg end))
  5333. (save-excursion
  5334. (delete-region beg end))
  5335. (setq pos (point))
  5336. (insert txt)
  5337. (goto-char pos) (org-skip-whitespace)
  5338. (org-maybe-renumber-ordered-list))
  5339. (goto-char pos)
  5340. (error "Cannot move this item further down"))))
  5341. (defun org-move-item-up (arg)
  5342. "Move the plain list item at point up, i.e. swap with previous item.
  5343. Subitems (items with larger indentation) are considered part of the item,
  5344. so this really moves item trees."
  5345. (interactive "p")
  5346. (let (beg beg0 end ind ind1 (pos (point)) txt
  5347. ne-beg ne-ins ins-end)
  5348. (org-beginning-of-item)
  5349. (setq beg0 (point))
  5350. (setq ind (org-get-indentation))
  5351. (save-excursion
  5352. (setq ne-beg (org-back-over-empty-lines))
  5353. (setq beg (point)))
  5354. (goto-char beg0)
  5355. (org-end-of-item)
  5356. (org-back-over-empty-lines)
  5357. (setq end (point))
  5358. (goto-char beg0)
  5359. (catch 'exit
  5360. (while t
  5361. (beginning-of-line 0)
  5362. (if (looking-at "[ \t]*$")
  5363. (if org-empty-line-terminates-plain-lists
  5364. (progn
  5365. (goto-char pos)
  5366. (error "Cannot move this item further up"))
  5367. nil)
  5368. (if (<= (setq ind1 (org-get-indentation)) ind)
  5369. (throw 'exit t)))))
  5370. (condition-case nil
  5371. (org-beginning-of-item)
  5372. (error (goto-char beg0)
  5373. (error "Cannot move this item further up")))
  5374. (setq ind1 (org-get-indentation))
  5375. (if (and (org-at-item-p) (= ind ind1))
  5376. (progn
  5377. (setq ne-ins (org-back-over-empty-lines))
  5378. (setq txt (buffer-substring beg end))
  5379. (save-excursion
  5380. (delete-region beg end))
  5381. (setq pos (point))
  5382. (insert txt)
  5383. (setq ins-end (point))
  5384. (goto-char pos) (org-skip-whitespace)
  5385. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5386. ;; Move whitespace back to beginning
  5387. (save-excursion
  5388. (goto-char ins-end)
  5389. (let ((kill-whole-line t))
  5390. (kill-line (- ne-ins ne-beg)) (point)))
  5391. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5392. (org-maybe-renumber-ordered-list))
  5393. (goto-char pos)
  5394. (error "Cannot move this item further up"))))
  5395. (defun org-maybe-renumber-ordered-list ()
  5396. "Renumber the ordered list at point if setup allows it.
  5397. This tests the user option `org-auto-renumber-ordered-lists' before
  5398. doing the renumbering."
  5399. (interactive)
  5400. (when (and org-auto-renumber-ordered-lists
  5401. (org-at-item-p))
  5402. (if (match-beginning 3)
  5403. (org-renumber-ordered-list 1)
  5404. (org-fix-bullet-type))))
  5405. (defun org-maybe-renumber-ordered-list-safe ()
  5406. (condition-case nil
  5407. (save-excursion
  5408. (org-maybe-renumber-ordered-list))
  5409. (error nil)))
  5410. (defun org-cycle-list-bullet (&optional which)
  5411. "Cycle through the different itemize/enumerate bullets.
  5412. This cycle the entire list level through the sequence:
  5413. `-' -> `+' -> `*' -> `1.' -> `1)'
  5414. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5415. 0 meand `-', 1 means `+' etc."
  5416. (interactive "P")
  5417. (org-preserve-lc
  5418. (org-beginning-of-item-list)
  5419. (org-at-item-p)
  5420. (beginning-of-line 1)
  5421. (let ((current (match-string 0))
  5422. (prevp (eq which 'previous))
  5423. new)
  5424. (setq new (cond
  5425. ((and (numberp which)
  5426. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5427. ((string-match "-" current) (if prevp "1)" "+"))
  5428. ((string-match "\\+" current)
  5429. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5430. ((string-match "\\*" current) (if prevp "+" "1."))
  5431. ((string-match "\\." current) (if prevp "*" "1)"))
  5432. ((string-match ")" current) (if prevp "1." "-"))
  5433. (t (error "This should not happen"))))
  5434. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5435. (org-fix-bullet-type)
  5436. (org-maybe-renumber-ordered-list))))
  5437. (defun org-get-string-indentation (s)
  5438. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5439. (let ((n -1) (i 0) (w tab-width) c)
  5440. (catch 'exit
  5441. (while (< (setq n (1+ n)) (length s))
  5442. (setq c (aref s n))
  5443. (cond ((= c ?\ ) (setq i (1+ i)))
  5444. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5445. (t (throw 'exit t)))))
  5446. i))
  5447. (defun org-renumber-ordered-list (arg)
  5448. "Renumber an ordered plain list.
  5449. Cursor needs to be in the first line of an item, the line that starts
  5450. with something like \"1.\" or \"2)\"."
  5451. (interactive "p")
  5452. (unless (and (org-at-item-p)
  5453. (match-beginning 3))
  5454. (error "This is not an ordered list"))
  5455. (let ((line (org-current-line))
  5456. (col (current-column))
  5457. (ind (org-get-string-indentation
  5458. (buffer-substring (point-at-bol) (match-beginning 3))))
  5459. ;; (term (substring (match-string 3) -1))
  5460. ind1 (n (1- arg))
  5461. fmt)
  5462. ;; find where this list begins
  5463. (org-beginning-of-item-list)
  5464. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5465. (setq fmt (concat "%d" (match-string 1)))
  5466. (beginning-of-line 0)
  5467. ;; walk forward and replace these numbers
  5468. (catch 'exit
  5469. (while t
  5470. (catch 'next
  5471. (beginning-of-line 2)
  5472. (if (eobp) (throw 'exit nil))
  5473. (if (looking-at "[ \t]*$") (throw 'next nil))
  5474. (skip-chars-forward " \t") (setq ind1 (current-column))
  5475. (if (> ind1 ind) (throw 'next t))
  5476. (if (< ind1 ind) (throw 'exit t))
  5477. (if (not (org-at-item-p)) (throw 'exit nil))
  5478. (delete-region (match-beginning 2) (match-end 2))
  5479. (goto-char (match-beginning 2))
  5480. (insert (format fmt (setq n (1+ n)))))))
  5481. (goto-line line)
  5482. (org-move-to-column col)))
  5483. (defun org-fix-bullet-type ()
  5484. "Make sure all items in this list have the same bullet as the firsst item."
  5485. (interactive)
  5486. (unless (org-at-item-p) (error "This is not a list"))
  5487. (let ((line (org-current-line))
  5488. (col (current-column))
  5489. (ind (current-indentation))
  5490. ind1 bullet)
  5491. ;; find where this list begins
  5492. (org-beginning-of-item-list)
  5493. (beginning-of-line 1)
  5494. ;; find out what the bullet type is
  5495. (looking-at "[ \t]*\\(\\S-+\\)")
  5496. (setq bullet (match-string 1))
  5497. ;; walk forward and replace these numbers
  5498. (beginning-of-line 0)
  5499. (catch 'exit
  5500. (while t
  5501. (catch 'next
  5502. (beginning-of-line 2)
  5503. (if (eobp) (throw 'exit nil))
  5504. (if (looking-at "[ \t]*$") (throw 'next nil))
  5505. (skip-chars-forward " \t") (setq ind1 (current-column))
  5506. (if (> ind1 ind) (throw 'next t))
  5507. (if (< ind1 ind) (throw 'exit t))
  5508. (if (not (org-at-item-p)) (throw 'exit nil))
  5509. (skip-chars-forward " \t")
  5510. (looking-at "\\S-+")
  5511. (replace-match bullet))))
  5512. (goto-line line)
  5513. (org-move-to-column col)
  5514. (if (string-match "[0-9]" bullet)
  5515. (org-renumber-ordered-list 1))))
  5516. (defun org-beginning-of-item-list ()
  5517. "Go to the beginning of the current item list.
  5518. I.e. to the first item in this list."
  5519. (interactive)
  5520. (org-beginning-of-item)
  5521. (let ((pos (point-at-bol))
  5522. (ind (org-get-indentation))
  5523. ind1)
  5524. ;; find where this list begins
  5525. (catch 'exit
  5526. (while t
  5527. (catch 'next
  5528. (beginning-of-line 0)
  5529. (if (looking-at "[ \t]*$")
  5530. (throw (if (bobp) 'exit 'next) t))
  5531. (skip-chars-forward " \t") (setq ind1 (current-column))
  5532. (if (or (< ind1 ind)
  5533. (and (= ind1 ind)
  5534. (not (org-at-item-p)))
  5535. (bobp))
  5536. (throw 'exit t)
  5537. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5538. (goto-char pos)))
  5539. (defun org-end-of-item-list ()
  5540. "Go to the end of the current item list.
  5541. I.e. to the text after the last item."
  5542. (interactive)
  5543. (org-beginning-of-item)
  5544. (let ((pos (point-at-bol))
  5545. (ind (org-get-indentation))
  5546. ind1)
  5547. ;; find where this list begins
  5548. (catch 'exit
  5549. (while t
  5550. (catch 'next
  5551. (beginning-of-line 2)
  5552. (if (looking-at "[ \t]*$")
  5553. (throw (if (eobp) 'exit 'next) t))
  5554. (skip-chars-forward " \t") (setq ind1 (current-column))
  5555. (if (or (< ind1 ind)
  5556. (and (= ind1 ind)
  5557. (not (org-at-item-p)))
  5558. (eobp))
  5559. (progn
  5560. (setq pos (point-at-bol))
  5561. (throw 'exit t))))))
  5562. (goto-char pos)))
  5563. (defvar org-last-indent-begin-marker (make-marker))
  5564. (defvar org-last-indent-end-marker (make-marker))
  5565. (defun org-outdent-item (arg)
  5566. "Outdent a local list item."
  5567. (interactive "p")
  5568. (org-indent-item (- arg)))
  5569. (defun org-indent-item (arg)
  5570. "Indent a local list item."
  5571. (interactive "p")
  5572. (unless (org-at-item-p)
  5573. (error "Not on an item"))
  5574. (save-excursion
  5575. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5576. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5577. (setq beg org-last-indent-begin-marker
  5578. end org-last-indent-end-marker)
  5579. (org-beginning-of-item)
  5580. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5581. (org-end-of-item)
  5582. (setq end (move-marker org-last-indent-end-marker (point))))
  5583. (goto-char beg)
  5584. (setq tmp (org-item-indent-positions)
  5585. ind (car tmp)
  5586. ind-down (nth 2 tmp)
  5587. ind-up (nth 1 tmp)
  5588. delta (if (> arg 0)
  5589. (if ind-down (- ind-down ind) 2)
  5590. (if ind-up (- ind-up ind) -2)))
  5591. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5592. (while (< (point) end)
  5593. (beginning-of-line 1)
  5594. (skip-chars-forward " \t") (setq ind1 (current-column))
  5595. (delete-region (point-at-bol) (point))
  5596. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5597. (beginning-of-line 2))))
  5598. (org-fix-bullet-type)
  5599. (org-maybe-renumber-ordered-list-safe)
  5600. (save-excursion
  5601. (beginning-of-line 0)
  5602. (condition-case nil (org-beginning-of-item) (error nil))
  5603. (org-maybe-renumber-ordered-list-safe)))
  5604. (defun org-item-indent-positions ()
  5605. "Return indentation for plain list items.
  5606. This returns a list with three values: The current indentation, the
  5607. parent indentation and the indentation a child should habe.
  5608. Assumes cursor in item line."
  5609. (let* ((bolpos (point-at-bol))
  5610. (ind (org-get-indentation))
  5611. ind-down ind-up pos)
  5612. (save-excursion
  5613. (org-beginning-of-item-list)
  5614. (skip-chars-backward "\n\r \t")
  5615. (when (org-in-item-p)
  5616. (org-beginning-of-item)
  5617. (setq ind-up (org-get-indentation))))
  5618. (setq pos (point))
  5619. (save-excursion
  5620. (cond
  5621. ((and (condition-case nil (progn (org-previous-item) t)
  5622. (error nil))
  5623. (or (forward-char 1) t)
  5624. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5625. (setq ind-down (org-get-indentation)))
  5626. ((and (goto-char pos)
  5627. (org-at-item-p))
  5628. (goto-char (match-end 0))
  5629. (skip-chars-forward " \t")
  5630. (setq ind-down (current-column)))))
  5631. (list ind ind-up ind-down)))
  5632. ;;; The orgstruct minor mode
  5633. ;; Define a minor mode which can be used in other modes in order to
  5634. ;; integrate the org-mode structure editing commands.
  5635. ;; This is really a hack, because the org-mode structure commands use
  5636. ;; keys which normally belong to the major mode. Here is how it
  5637. ;; works: The minor mode defines all the keys necessary to operate the
  5638. ;; structure commands, but wraps the commands into a function which
  5639. ;; tests if the cursor is currently at a headline or a plain list
  5640. ;; item. If that is the case, the structure command is used,
  5641. ;; temporarily setting many Org-mode variables like regular
  5642. ;; expressions for filling etc. However, when any of those keys is
  5643. ;; used at a different location, function uses `key-binding' to look
  5644. ;; up if the key has an associated command in another currently active
  5645. ;; keymap (minor modes, major mode, global), and executes that
  5646. ;; command. There might be problems if any of the keys is otherwise
  5647. ;; used as a prefix key.
  5648. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5649. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5650. ;; addresses this by checking explicitly for both bindings.
  5651. (defvar orgstruct-mode-map (make-sparse-keymap)
  5652. "Keymap for the minor `orgstruct-mode'.")
  5653. (defvar org-local-vars nil
  5654. "List of local variables, for use by `orgstruct-mode'")
  5655. ;;;###autoload
  5656. (define-minor-mode orgstruct-mode
  5657. "Toggle the minor more `orgstruct-mode'.
  5658. This mode is for using Org-mode structure commands in other modes.
  5659. The following key behave as if Org-mode was active, if the cursor
  5660. is on a headline, or on a plain list item (both in the definition
  5661. of Org-mode).
  5662. M-up Move entry/item up
  5663. M-down Move entry/item down
  5664. M-left Promote
  5665. M-right Demote
  5666. M-S-up Move entry/item up
  5667. M-S-down Move entry/item down
  5668. M-S-left Promote subtree
  5669. M-S-right Demote subtree
  5670. M-q Fill paragraph and items like in Org-mode
  5671. C-c ^ Sort entries
  5672. C-c - Cycle list bullet
  5673. TAB Cycle item visibility
  5674. M-RET Insert new heading/item
  5675. S-M-RET Insert new TODO heading / Chekbox item
  5676. C-c C-c Set tags / toggle checkbox"
  5677. nil " OrgStruct" nil
  5678. (org-load-modules-maybe)
  5679. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5680. ;;;###autoload
  5681. (defun turn-on-orgstruct ()
  5682. "Unconditionally turn on `orgstruct-mode'."
  5683. (orgstruct-mode 1))
  5684. ;;;###autoload
  5685. (defun turn-on-orgstruct++ ()
  5686. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5687. In addition to setting orgstruct-mode, this also exports all indentation and
  5688. autofilling variables from org-mode into the buffer. Note that turning
  5689. off orgstruct-mode will *not* remove these additional settings."
  5690. (orgstruct-mode 1)
  5691. (let (var val)
  5692. (mapc
  5693. (lambda (x)
  5694. (when (string-match
  5695. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5696. (symbol-name (car x)))
  5697. (setq var (car x) val (nth 1 x))
  5698. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5699. org-local-vars)))
  5700. (defun orgstruct-error ()
  5701. "Error when there is no default binding for a structure key."
  5702. (interactive)
  5703. (error "This key has no function outside structure elements"))
  5704. (defun orgstruct-setup ()
  5705. "Setup orgstruct keymaps."
  5706. (let ((nfunc 0)
  5707. (bindings
  5708. (list
  5709. '([(meta up)] org-metaup)
  5710. '([(meta down)] org-metadown)
  5711. '([(meta left)] org-metaleft)
  5712. '([(meta right)] org-metaright)
  5713. '([(meta shift up)] org-shiftmetaup)
  5714. '([(meta shift down)] org-shiftmetadown)
  5715. '([(meta shift left)] org-shiftmetaleft)
  5716. '([(meta shift right)] org-shiftmetaright)
  5717. '([(shift up)] org-shiftup)
  5718. '([(shift down)] org-shiftdown)
  5719. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5720. '("\M-q" fill-paragraph)
  5721. '("\C-c^" org-sort)
  5722. '("\C-c-" org-cycle-list-bullet)))
  5723. elt key fun cmd)
  5724. (while (setq elt (pop bindings))
  5725. (setq nfunc (1+ nfunc))
  5726. (setq key (org-key (car elt))
  5727. fun (nth 1 elt)
  5728. cmd (orgstruct-make-binding fun nfunc key))
  5729. (org-defkey orgstruct-mode-map key cmd))
  5730. ;; Special treatment needed for TAB and RET
  5731. (org-defkey orgstruct-mode-map [(tab)]
  5732. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5733. (org-defkey orgstruct-mode-map "\C-i"
  5734. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5735. (org-defkey orgstruct-mode-map "\M-\C-m"
  5736. (orgstruct-make-binding 'org-insert-heading 105
  5737. "\M-\C-m" [(meta return)]))
  5738. (org-defkey orgstruct-mode-map [(meta return)]
  5739. (orgstruct-make-binding 'org-insert-heading 106
  5740. [(meta return)] "\M-\C-m"))
  5741. (org-defkey orgstruct-mode-map [(shift meta return)]
  5742. (orgstruct-make-binding 'org-insert-todo-heading 107
  5743. [(meta return)] "\M-\C-m"))
  5744. (unless org-local-vars
  5745. (setq org-local-vars (org-get-local-variables)))
  5746. t))
  5747. (defun orgstruct-make-binding (fun n &rest keys)
  5748. "Create a function for binding in the structure minor mode.
  5749. FUN is the command to call inside a table. N is used to create a unique
  5750. command name. KEYS are keys that should be checked in for a command
  5751. to execute outside of tables."
  5752. (eval
  5753. (list 'defun
  5754. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5755. '(arg)
  5756. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5757. "Outside of structure, run the binding of `"
  5758. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5759. "'.")
  5760. '(interactive "p")
  5761. (list 'if
  5762. '(org-context-p 'headline 'item)
  5763. (list 'org-run-like-in-org-mode (list 'quote fun))
  5764. (list 'let '(orgstruct-mode)
  5765. (list 'call-interactively
  5766. (append '(or)
  5767. (mapcar (lambda (k)
  5768. (list 'key-binding k))
  5769. keys)
  5770. '('orgstruct-error))))))))
  5771. (defun org-context-p (&rest contexts)
  5772. "Check if local context is and of CONTEXTS.
  5773. Possible values in the list of contexts are `table', `headline', and `item'."
  5774. (let ((pos (point)))
  5775. (goto-char (point-at-bol))
  5776. (prog1 (or (and (memq 'table contexts)
  5777. (looking-at "[ \t]*|"))
  5778. (and (memq 'headline contexts)
  5779. (looking-at "\\*+"))
  5780. (and (memq 'item contexts)
  5781. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5782. (goto-char pos))))
  5783. (defun org-get-local-variables ()
  5784. "Return a list of all local variables in an org-mode buffer."
  5785. (let (varlist)
  5786. (with-current-buffer (get-buffer-create "*Org tmp*")
  5787. (erase-buffer)
  5788. (org-mode)
  5789. (setq varlist (buffer-local-variables)))
  5790. (kill-buffer "*Org tmp*")
  5791. (delq nil
  5792. (mapcar
  5793. (lambda (x)
  5794. (setq x
  5795. (if (symbolp x)
  5796. (list x)
  5797. (list (car x) (list 'quote (cdr x)))))
  5798. (if (string-match
  5799. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5800. (symbol-name (car x)))
  5801. x nil))
  5802. varlist))))
  5803. ;;;###autoload
  5804. (defun org-run-like-in-org-mode (cmd)
  5805. (org-load-modules-maybe)
  5806. (unless org-local-vars
  5807. (setq org-local-vars (org-get-local-variables)))
  5808. (eval (list 'let org-local-vars
  5809. (list 'call-interactively (list 'quote cmd)))))
  5810. ;;;; Archiving
  5811. (defun org-get-category (&optional pos)
  5812. "Get the category applying to position POS."
  5813. (get-text-property (or pos (point)) 'org-category))
  5814. (defun org-refresh-category-properties ()
  5815. "Refresh category text properties in the buffer."
  5816. (let ((def-cat (cond
  5817. ((null org-category)
  5818. (if buffer-file-name
  5819. (file-name-sans-extension
  5820. (file-name-nondirectory buffer-file-name))
  5821. "???"))
  5822. ((symbolp org-category) (symbol-name org-category))
  5823. (t org-category)))
  5824. beg end cat pos optionp)
  5825. (org-unmodified
  5826. (save-excursion
  5827. (save-restriction
  5828. (widen)
  5829. (goto-char (point-min))
  5830. (put-text-property (point) (point-max) 'org-category def-cat)
  5831. (while (re-search-forward
  5832. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5833. (setq pos (match-end 0)
  5834. optionp (equal (char-after (match-beginning 0)) ?#)
  5835. cat (org-trim (match-string 2)))
  5836. (if optionp
  5837. (setq beg (point-at-bol) end (point-max))
  5838. (org-back-to-heading t)
  5839. (setq beg (point) end (org-end-of-subtree t t)))
  5840. (put-text-property beg end 'org-category cat)
  5841. (goto-char pos)))))))
  5842. ;;;; Link Stuff
  5843. ;;; Link abbreviations
  5844. (defun org-link-expand-abbrev (link)
  5845. "Apply replacements as defined in `org-link-abbrev-alist."
  5846. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5847. (let* ((key (match-string 1 link))
  5848. (as (or (assoc key org-link-abbrev-alist-local)
  5849. (assoc key org-link-abbrev-alist)))
  5850. (tag (and (match-end 2) (match-string 3 link)))
  5851. rpl)
  5852. (if (not as)
  5853. link
  5854. (setq rpl (cdr as))
  5855. (cond
  5856. ((symbolp rpl) (funcall rpl tag))
  5857. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5858. (t (concat rpl tag)))))
  5859. link))
  5860. ;;; Storing and inserting links
  5861. (defvar org-insert-link-history nil
  5862. "Minibuffer history for links inserted with `org-insert-link'.")
  5863. (defvar org-stored-links nil
  5864. "Contains the links stored with `org-store-link'.")
  5865. (defvar org-store-link-plist nil
  5866. "Plist with info about the most recently link created with `org-store-link'.")
  5867. (defvar org-link-protocols nil
  5868. "Link protocols added to Org-mode using `org-add-link-type'.")
  5869. (defvar org-store-link-functions nil
  5870. "List of functions that are called to create and store a link.
  5871. Each function will be called in turn until one returns a non-nil
  5872. value. Each function should check if it is responsible for creating
  5873. this link (for example by looking at the major mode).
  5874. If not, it must exit and return nil.
  5875. If yes, it should return a non-nil value after a calling
  5876. `org-store-link-props' with a list of properties and values.
  5877. Special properties are:
  5878. :type The link prefix. like \"http\". This must be given.
  5879. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5880. This is obligatory as well.
  5881. :description Optional default description for the second pair
  5882. of brackets in an Org-mode link. The user can still change
  5883. this when inserting this link into an Org-mode buffer.
  5884. In addition to these, any additional properties can be specified
  5885. and then used in remember templates.")
  5886. (defun org-add-link-type (type &optional follow export)
  5887. "Add TYPE to the list of `org-link-types'.
  5888. Re-compute all regular expressions depending on `org-link-types'
  5889. FOLLOW and EXPORT are two functions.
  5890. FOLLOW should take the link path as the single argument and do whatever
  5891. is necessary to follow the link, for example find a file or display
  5892. a mail message.
  5893. EXPORT should format the link path for export to one of the export formats.
  5894. It should be a function accepting three arguments:
  5895. path the path of the link, the text after the prefix (like \"http:\")
  5896. desc the description of the link, if any, nil if there was no descripton
  5897. format the export format, a symbol like `html' or `latex'.
  5898. The function may use the FORMAT information to return different values
  5899. depending on the format. The return value will be put literally into
  5900. the exported file.
  5901. Org-mode has a built-in default for exporting links. If you are happy with
  5902. this default, there is no need to define an export function for the link
  5903. type. For a simple example of an export function, see `org-bbdb.el'."
  5904. (add-to-list 'org-link-types type t)
  5905. (org-make-link-regexps)
  5906. (if (assoc type org-link-protocols)
  5907. (setcdr (assoc type org-link-protocols) (list follow export))
  5908. (push (list type follow export) org-link-protocols)))
  5909. ;;;###autoload
  5910. (defun org-store-link (arg)
  5911. "\\<org-mode-map>Store an org-link to the current location.
  5912. This link is added to `org-stored-links' and can later be inserted
  5913. into an org-buffer with \\[org-insert-link].
  5914. For some link types, a prefix arg is interpreted:
  5915. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5916. For file links, arg negates `org-context-in-file-links'."
  5917. (interactive "P")
  5918. (org-load-modules-maybe)
  5919. (setq org-store-link-plist nil) ; reset
  5920. (let (link cpltxt desc description search txt)
  5921. (cond
  5922. ((run-hook-with-args-until-success 'org-store-link-functions)
  5923. (setq link (plist-get org-store-link-plist :link)
  5924. desc (or (plist-get org-store-link-plist :description) link)))
  5925. ((eq major-mode 'calendar-mode)
  5926. (let ((cd (calendar-cursor-to-date)))
  5927. (setq link
  5928. (format-time-string
  5929. (car org-time-stamp-formats)
  5930. (apply 'encode-time
  5931. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5932. nil nil nil))))
  5933. (org-store-link-props :type "calendar" :date cd)))
  5934. ((eq major-mode 'w3-mode)
  5935. (setq cpltxt (url-view-url t)
  5936. link (org-make-link cpltxt))
  5937. (org-store-link-props :type "w3" :url (url-view-url t)))
  5938. ((eq major-mode 'w3m-mode)
  5939. (setq cpltxt (or w3m-current-title w3m-current-url)
  5940. link (org-make-link w3m-current-url))
  5941. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5942. ((setq search (run-hook-with-args-until-success
  5943. 'org-create-file-search-functions))
  5944. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5945. "::" search))
  5946. (setq cpltxt (or description link)))
  5947. ((eq major-mode 'image-mode)
  5948. (setq cpltxt (concat "file:"
  5949. (abbreviate-file-name buffer-file-name))
  5950. link (org-make-link cpltxt))
  5951. (org-store-link-props :type "image" :file buffer-file-name))
  5952. ((eq major-mode 'dired-mode)
  5953. ;; link to the file in the current line
  5954. (setq cpltxt (concat "file:"
  5955. (abbreviate-file-name
  5956. (expand-file-name
  5957. (dired-get-filename nil t))))
  5958. link (org-make-link cpltxt)))
  5959. ((and buffer-file-name (org-mode-p))
  5960. ;; Just link to current headline
  5961. (setq cpltxt (concat "file:"
  5962. (abbreviate-file-name buffer-file-name)))
  5963. ;; Add a context search string
  5964. (when (org-xor org-context-in-file-links arg)
  5965. ;; Check if we are on a target
  5966. (if (org-in-regexp "<<\\(.*?\\)>>")
  5967. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5968. (setq txt (cond
  5969. ((org-on-heading-p) nil)
  5970. ((org-region-active-p)
  5971. (buffer-substring (region-beginning) (region-end)))
  5972. (t nil)))
  5973. (when (or (null txt) (string-match "\\S-" txt))
  5974. (setq cpltxt
  5975. (concat cpltxt "::"
  5976. (condition-case nil
  5977. (org-make-org-heading-search-string txt)
  5978. (error "")))
  5979. desc "NONE"))))
  5980. (if (string-match "::\\'" cpltxt)
  5981. (setq cpltxt (substring cpltxt 0 -2)))
  5982. (setq link (org-make-link cpltxt)))
  5983. ((buffer-file-name (buffer-base-buffer))
  5984. ;; Just link to this file here.
  5985. (setq cpltxt (concat "file:"
  5986. (abbreviate-file-name
  5987. (buffer-file-name (buffer-base-buffer)))))
  5988. ;; Add a context string
  5989. (when (org-xor org-context-in-file-links arg)
  5990. (setq txt (if (org-region-active-p)
  5991. (buffer-substring (region-beginning) (region-end))
  5992. (buffer-substring (point-at-bol) (point-at-eol))))
  5993. ;; Only use search option if there is some text.
  5994. (when (string-match "\\S-" txt)
  5995. (setq cpltxt
  5996. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5997. desc "NONE")))
  5998. (setq link (org-make-link cpltxt)))
  5999. ((interactive-p)
  6000. (error "Cannot link to a buffer which is not visiting a file"))
  6001. (t (setq link nil)))
  6002. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  6003. (setq link (or link cpltxt)
  6004. desc (or desc cpltxt))
  6005. (if (equal desc "NONE") (setq desc nil))
  6006. (if (and (interactive-p) link)
  6007. (progn
  6008. (setq org-stored-links
  6009. (cons (list link desc) org-stored-links))
  6010. (message "Stored: %s" (or desc link)))
  6011. (and link (org-make-link-string link desc)))))
  6012. (defun org-store-link-props (&rest plist)
  6013. "Store link properties, extract names and addresses."
  6014. (let (x adr)
  6015. (when (setq x (plist-get plist :from))
  6016. (setq adr (mail-extract-address-components x))
  6017. (plist-put plist :fromname (car adr))
  6018. (plist-put plist :fromaddress (nth 1 adr)))
  6019. (when (setq x (plist-get plist :to))
  6020. (setq adr (mail-extract-address-components x))
  6021. (plist-put plist :toname (car adr))
  6022. (plist-put plist :toaddress (nth 1 adr))))
  6023. (let ((from (plist-get plist :from))
  6024. (to (plist-get plist :to)))
  6025. (when (and from to org-from-is-user-regexp)
  6026. (plist-put plist :fromto
  6027. (if (string-match org-from-is-user-regexp from)
  6028. (concat "to %t")
  6029. (concat "from %f")))))
  6030. (setq org-store-link-plist plist))
  6031. (defun org-add-link-props (&rest plist)
  6032. "Add these properties to the link property list."
  6033. (let (key value)
  6034. (while plist
  6035. (setq key (pop plist) value (pop plist))
  6036. (setq org-store-link-plist
  6037. (plist-put org-store-link-plist key value)))))
  6038. (defun org-email-link-description (&optional fmt)
  6039. "Return the description part of an email link.
  6040. This takes information from `org-store-link-plist' and formats it
  6041. according to FMT (default from `org-email-link-description-format')."
  6042. (setq fmt (or fmt org-email-link-description-format))
  6043. (let* ((p org-store-link-plist)
  6044. (to (plist-get p :toaddress))
  6045. (from (plist-get p :fromaddress))
  6046. (table
  6047. (list
  6048. (cons "%c" (plist-get p :fromto))
  6049. (cons "%F" (plist-get p :from))
  6050. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  6051. (cons "%T" (plist-get p :to))
  6052. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  6053. (cons "%s" (plist-get p :subject))
  6054. (cons "%m" (plist-get p :message-id)))))
  6055. (when (string-match "%c" fmt)
  6056. ;; Check if the user wrote this message
  6057. (if (and org-from-is-user-regexp from to
  6058. (save-match-data (string-match org-from-is-user-regexp from)))
  6059. (setq fmt (replace-match "to %t" t t fmt))
  6060. (setq fmt (replace-match "from %f" t t fmt))))
  6061. (org-replace-escapes fmt table)))
  6062. (defun org-make-org-heading-search-string (&optional string heading)
  6063. "Make search string for STRING or current headline."
  6064. (interactive)
  6065. (let ((s (or string (org-get-heading))))
  6066. (unless (and string (not heading))
  6067. ;; We are using a headline, clean up garbage in there.
  6068. (if (string-match org-todo-regexp s)
  6069. (setq s (replace-match "" t t s)))
  6070. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  6071. (setq s (replace-match "" t t s)))
  6072. (setq s (org-trim s))
  6073. (if (string-match (concat "^\\(" org-quote-string "\\|"
  6074. org-comment-string "\\)") s)
  6075. (setq s (replace-match "" t t s)))
  6076. (while (string-match org-ts-regexp s)
  6077. (setq s (replace-match "" t t s))))
  6078. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  6079. (setq s (replace-match " " t t s)))
  6080. (or string (setq s (concat "*" s))) ; Add * for headlines
  6081. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  6082. (defun org-make-link (&rest strings)
  6083. "Concatenate STRINGS."
  6084. (apply 'concat strings))
  6085. (defun org-make-link-string (link &optional description)
  6086. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  6087. (unless (string-match "\\S-" link)
  6088. (error "Empty link"))
  6089. (when (stringp description)
  6090. ;; Remove brackets from the description, they are fatal.
  6091. (while (string-match "\\[" description)
  6092. (setq description (replace-match "{" t t description)))
  6093. (while (string-match "\\]" description)
  6094. (setq description (replace-match "}" t t description))))
  6095. (when (equal (org-link-escape link) description)
  6096. ;; No description needed, it is identical
  6097. (setq description nil))
  6098. (when (and (not description)
  6099. (not (equal link (org-link-escape link))))
  6100. (setq description link))
  6101. (concat "[[" (org-link-escape link) "]"
  6102. (if description (concat "[" description "]") "")
  6103. "]"))
  6104. (defconst org-link-escape-chars
  6105. '((?\ . "%20")
  6106. (?\[ . "%5B")
  6107. (?\] . "%5D")
  6108. (?\340 . "%E0") ; `a
  6109. (?\342 . "%E2") ; ^a
  6110. (?\347 . "%E7") ; ,c
  6111. (?\350 . "%E8") ; `e
  6112. (?\351 . "%E9") ; 'e
  6113. (?\352 . "%EA") ; ^e
  6114. (?\356 . "%EE") ; ^i
  6115. (?\364 . "%F4") ; ^o
  6116. (?\371 . "%F9") ; `u
  6117. (?\373 . "%FB") ; ^u
  6118. (?\; . "%3B")
  6119. (?? . "%3F")
  6120. (?= . "%3D")
  6121. (?+ . "%2B")
  6122. )
  6123. "Association list of escapes for some characters problematic in links.
  6124. This is the list that is used for internal purposes.")
  6125. (defconst org-link-escape-chars-browser
  6126. '((?\ . "%20")) ; 32 for the SPC char
  6127. "Association list of escapes for some characters problematic in links.
  6128. This is the list that is used before handing over to the browser.")
  6129. (defun org-link-escape (text &optional table)
  6130. "Escape charaters in TEXT that are problematic for links."
  6131. (setq table (or table org-link-escape-chars))
  6132. (when text
  6133. (let ((re (mapconcat (lambda (x) (regexp-quote
  6134. (char-to-string (car x))))
  6135. table "\\|")))
  6136. (while (string-match re text)
  6137. (setq text
  6138. (replace-match
  6139. (cdr (assoc (string-to-char (match-string 0 text))
  6140. table))
  6141. t t text)))
  6142. text)))
  6143. (defun org-link-unescape (text &optional table)
  6144. "Reverse the action of `org-link-escape'."
  6145. (setq table (or table org-link-escape-chars))
  6146. (when text
  6147. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  6148. table "\\|")))
  6149. (while (string-match re text)
  6150. (setq text
  6151. (replace-match
  6152. (char-to-string (car (rassoc (match-string 0 text) table)))
  6153. t t text)))
  6154. text)))
  6155. (defun org-xor (a b)
  6156. "Exclusive or."
  6157. (if a (not b) b))
  6158. (defun org-get-header (header)
  6159. "Find a header field in the current buffer."
  6160. (save-excursion
  6161. (goto-char (point-min))
  6162. (let ((case-fold-search t) s)
  6163. (cond
  6164. ((eq header 'from)
  6165. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  6166. (setq s (match-string 1)))
  6167. (while (string-match "\"" s)
  6168. (setq s (replace-match "" t t s)))
  6169. (if (string-match "[<(].*" s)
  6170. (setq s (replace-match "" t t s))))
  6171. ((eq header 'message-id)
  6172. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  6173. (setq s (match-string 1))))
  6174. ((eq header 'subject)
  6175. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  6176. (setq s (match-string 1)))))
  6177. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  6178. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  6179. s)))
  6180. (defun org-fixup-message-id-for-http (s)
  6181. "Replace special characters in a message id, so it can be used in an http query."
  6182. (while (string-match "<" s)
  6183. (setq s (replace-match "%3C" t t s)))
  6184. (while (string-match ">" s)
  6185. (setq s (replace-match "%3E" t t s)))
  6186. (while (string-match "@" s)
  6187. (setq s (replace-match "%40" t t s)))
  6188. s)
  6189. ;;;###autoload
  6190. (defun org-insert-link-global ()
  6191. "Insert a link like Org-mode does.
  6192. This command can be called in any mode to insert a link in Org-mode syntax."
  6193. (interactive)
  6194. (org-load-modules-maybe)
  6195. (org-run-like-in-org-mode 'org-insert-link))
  6196. (defun org-insert-link (&optional complete-file link-location)
  6197. "Insert a link. At the prompt, enter the link.
  6198. Completion can be used to select a link previously stored with
  6199. `org-store-link'. When the empty string is entered (i.e. if you just
  6200. press RET at the prompt), the link defaults to the most recently
  6201. stored link. As SPC triggers completion in the minibuffer, you need to
  6202. use M-SPC or C-q SPC to force the insertion of a space character.
  6203. You will also be prompted for a description, and if one is given, it will
  6204. be displayed in the buffer instead of the link.
  6205. If there is already a link at point, this command will allow you to edit link
  6206. and description parts.
  6207. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  6208. be selected using completion. The path to the file will be relative to the
  6209. current directory if the file is in the current directory or a subdirectory.
  6210. Otherwise, the link will be the absolute path as completed in the minibuffer
  6211. \(i.e. normally ~/path/to/file).
  6212. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  6213. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  6214. of `org-keep-stored-link-after-insertion'.
  6215. If `org-make-link-description-function' is non-nil, this function will be
  6216. called with the link target, and the result will be the default
  6217. link description.
  6218. If the LINK-LOCATION parameter is non-nil, this value will be
  6219. used as the link location instead of reading one interactively."
  6220. (interactive "P")
  6221. (let* ((wcf (current-window-configuration))
  6222. (region (if (org-region-active-p)
  6223. (buffer-substring (region-beginning) (region-end))))
  6224. (remove (and region (list (region-beginning) (region-end))))
  6225. (desc region)
  6226. tmphist ; byte-compile incorrectly complains about this
  6227. (link link-location)
  6228. entry file)
  6229. (cond
  6230. (link-location) ; specified by arg, just use it.
  6231. ((org-in-regexp org-bracket-link-regexp 1)
  6232. ;; We do have a link at point, and we are going to edit it.
  6233. (setq remove (list (match-beginning 0) (match-end 0)))
  6234. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  6235. (setq link (read-string "Link: "
  6236. (org-link-unescape
  6237. (org-match-string-no-properties 1)))))
  6238. ((or (org-in-regexp org-angle-link-re)
  6239. (org-in-regexp org-plain-link-re))
  6240. ;; Convert to bracket link
  6241. (setq remove (list (match-beginning 0) (match-end 0))
  6242. link (read-string "Link: "
  6243. (org-remove-angle-brackets (match-string 0)))))
  6244. ((equal complete-file '(4))
  6245. ;; Completing read for file names.
  6246. (setq file (read-file-name "File: "))
  6247. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6248. (pwd1 (file-name-as-directory (abbreviate-file-name
  6249. (expand-file-name ".")))))
  6250. (cond
  6251. ((equal complete-file '(16))
  6252. (setq link (org-make-link
  6253. "file:"
  6254. (abbreviate-file-name (expand-file-name file)))))
  6255. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6256. (setq link (org-make-link "file:" (match-string 1 file))))
  6257. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6258. (expand-file-name file))
  6259. (setq link (org-make-link
  6260. "file:" (match-string 1 (expand-file-name file)))))
  6261. (t (setq link (org-make-link "file:" file))))))
  6262. (t
  6263. ;; Read link, with completion for stored links.
  6264. (with-output-to-temp-buffer "*Org Links*"
  6265. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6266. (when org-stored-links
  6267. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6268. (princ (mapconcat
  6269. (lambda (x)
  6270. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6271. (reverse org-stored-links) "\n"))))
  6272. (let ((cw (selected-window)))
  6273. (select-window (get-buffer-window "*Org Links*"))
  6274. (shrink-window-if-larger-than-buffer)
  6275. (setq truncate-lines t)
  6276. (select-window cw))
  6277. ;; Fake a link history, containing the stored links.
  6278. (setq tmphist (append (mapcar 'car org-stored-links)
  6279. org-insert-link-history))
  6280. (unwind-protect
  6281. (setq link (org-completing-read
  6282. "Link: "
  6283. (append
  6284. (mapcar (lambda (x) (list (concat (car x) ":")))
  6285. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6286. (mapcar (lambda (x) (list (concat x ":")))
  6287. org-link-types))
  6288. nil nil nil
  6289. 'tmphist
  6290. (or (car (car org-stored-links)))))
  6291. (set-window-configuration wcf)
  6292. (kill-buffer "*Org Links*"))
  6293. (setq entry (assoc link org-stored-links))
  6294. (or entry (push link org-insert-link-history))
  6295. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6296. (not org-keep-stored-link-after-insertion))
  6297. (setq org-stored-links (delq (assoc link org-stored-links)
  6298. org-stored-links)))
  6299. (setq desc (or desc (nth 1 entry)))))
  6300. (if (string-match org-plain-link-re link)
  6301. ;; URL-like link, normalize the use of angular brackets.
  6302. (setq link (org-make-link (org-remove-angle-brackets link))))
  6303. ;; Check if we are linking to the current file with a search option
  6304. ;; If yes, simplify the link by using only the search option.
  6305. (when (and buffer-file-name
  6306. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6307. (let* ((path (match-string 1 link))
  6308. (case-fold-search nil)
  6309. (search (match-string 2 link)))
  6310. (save-match-data
  6311. (if (equal (file-truename buffer-file-name) (file-truename path))
  6312. ;; We are linking to this same file, with a search option
  6313. (setq link search)))))
  6314. ;; Check if we can/should use a relative path. If yes, simplify the link
  6315. (when (string-match "\\<file:\\(.*\\)" link)
  6316. (let* ((path (match-string 1 link))
  6317. (origpath path)
  6318. (case-fold-search nil))
  6319. (cond
  6320. ((eq org-link-file-path-type 'absolute)
  6321. (setq path (abbreviate-file-name (expand-file-name path))))
  6322. ((eq org-link-file-path-type 'noabbrev)
  6323. (setq path (expand-file-name path)))
  6324. ((eq org-link-file-path-type 'relative)
  6325. (setq path (file-relative-name path)))
  6326. (t
  6327. (save-match-data
  6328. (if (string-match (concat "^" (regexp-quote
  6329. (file-name-as-directory
  6330. (expand-file-name "."))))
  6331. (expand-file-name path))
  6332. ;; We are linking a file with relative path name.
  6333. (setq path (substring (expand-file-name path)
  6334. (match-end 0)))))))
  6335. (setq link (concat "file:" path))
  6336. (if (equal desc origpath)
  6337. (setq desc path))))
  6338. (if org-make-link-description-function
  6339. (setq desc (funcall org-make-link-description-function link desc)))
  6340. (setq desc (read-string "Description: " desc))
  6341. (unless (string-match "\\S-" desc) (setq desc nil))
  6342. (if remove (apply 'delete-region remove))
  6343. (insert (org-make-link-string link desc))))
  6344. (defun org-completing-read (&rest args)
  6345. (let ((minibuffer-local-completion-map
  6346. (copy-keymap minibuffer-local-completion-map)))
  6347. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6348. (apply 'completing-read args)))
  6349. ;;; Opening/following a link
  6350. (defvar org-link-search-failed nil)
  6351. (defun org-next-link ()
  6352. "Move forward to the next link.
  6353. If the link is in hidden text, expose it."
  6354. (interactive)
  6355. (when (and org-link-search-failed (eq this-command last-command))
  6356. (goto-char (point-min))
  6357. (message "Link search wrapped back to beginning of buffer"))
  6358. (setq org-link-search-failed nil)
  6359. (let* ((pos (point))
  6360. (ct (org-context))
  6361. (a (assoc :link ct)))
  6362. (if a (goto-char (nth 2 a)))
  6363. (if (re-search-forward org-any-link-re nil t)
  6364. (progn
  6365. (goto-char (match-beginning 0))
  6366. (if (org-invisible-p) (org-show-context)))
  6367. (goto-char pos)
  6368. (setq org-link-search-failed t)
  6369. (error "No further link found"))))
  6370. (defun org-previous-link ()
  6371. "Move backward to the previous link.
  6372. If the link is in hidden text, expose it."
  6373. (interactive)
  6374. (when (and org-link-search-failed (eq this-command last-command))
  6375. (goto-char (point-max))
  6376. (message "Link search wrapped back to end of buffer"))
  6377. (setq org-link-search-failed nil)
  6378. (let* ((pos (point))
  6379. (ct (org-context))
  6380. (a (assoc :link ct)))
  6381. (if a (goto-char (nth 1 a)))
  6382. (if (re-search-backward org-any-link-re nil t)
  6383. (progn
  6384. (goto-char (match-beginning 0))
  6385. (if (org-invisible-p) (org-show-context)))
  6386. (goto-char pos)
  6387. (setq org-link-search-failed t)
  6388. (error "No further link found"))))
  6389. (defun org-find-file-at-mouse (ev)
  6390. "Open file link or URL at mouse."
  6391. (interactive "e")
  6392. (mouse-set-point ev)
  6393. (org-open-at-point 'in-emacs))
  6394. (defun org-open-at-mouse (ev)
  6395. "Open file link or URL at mouse."
  6396. (interactive "e")
  6397. (mouse-set-point ev)
  6398. (org-open-at-point))
  6399. (defvar org-window-config-before-follow-link nil
  6400. "The window configuration before following a link.
  6401. This is saved in case the need arises to restore it.")
  6402. (defvar org-open-link-marker (make-marker)
  6403. "Marker pointing to the location where `org-open-at-point; was called.")
  6404. ;;;###autoload
  6405. (defun org-open-at-point-global ()
  6406. "Follow a link like Org-mode does.
  6407. This command can be called in any mode to follow a link that has
  6408. Org-mode syntax."
  6409. (interactive)
  6410. (org-run-like-in-org-mode 'org-open-at-point))
  6411. ;;;###autoload
  6412. (defun org-open-link-from-string (s &optional arg)
  6413. "Open a link in the string S, as if it was in Org-mode."
  6414. (interactive "sLink: \nP")
  6415. (with-temp-buffer
  6416. (let ((org-inhibit-startup t))
  6417. (org-mode)
  6418. (insert s)
  6419. (goto-char (point-min))
  6420. (org-open-at-point arg))))
  6421. (defun org-open-at-point (&optional in-emacs)
  6422. "Open link at or after point.
  6423. If there is no link at point, this function will search forward up to
  6424. the end of the current subtree.
  6425. Normally, files will be opened by an appropriate application. If the
  6426. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6427. (interactive "P")
  6428. (org-load-modules-maybe)
  6429. (move-marker org-open-link-marker (point))
  6430. (setq org-window-config-before-follow-link (current-window-configuration))
  6431. (org-remove-occur-highlights nil nil t)
  6432. (if (org-at-timestamp-p t)
  6433. (org-follow-timestamp-link)
  6434. (let (type path link line search (pos (point)))
  6435. (catch 'match
  6436. (save-excursion
  6437. (skip-chars-forward "^]\n\r")
  6438. (when (org-in-regexp org-bracket-link-regexp)
  6439. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6440. (while (string-match " *\n *" link)
  6441. (setq link (replace-match " " t t link)))
  6442. (setq link (org-link-expand-abbrev link))
  6443. (cond
  6444. ((or (file-name-absolute-p link)
  6445. (string-match "^\\.\\.?/" link))
  6446. (setq type "file" path link))
  6447. ((string-match org-link-re-with-space2 link)
  6448. (setq type (match-string 1 link) path (match-string 2 link)))
  6449. (t (setq type "thisfile" path link)))
  6450. (throw 'match t)))
  6451. (when (get-text-property (point) 'org-linked-text)
  6452. (setq type "thisfile"
  6453. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6454. (1+ (point)) (point))
  6455. path (buffer-substring
  6456. (previous-single-property-change pos 'org-linked-text)
  6457. (next-single-property-change pos 'org-linked-text)))
  6458. (throw 'match t))
  6459. (save-excursion
  6460. (when (or (org-in-regexp org-angle-link-re)
  6461. (org-in-regexp org-plain-link-re))
  6462. (setq type (match-string 1) path (match-string 2))
  6463. (throw 'match t)))
  6464. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6465. (setq type "tree-match"
  6466. path (match-string 1))
  6467. (throw 'match t))
  6468. (save-excursion
  6469. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6470. (setq type "tags"
  6471. path (match-string 1))
  6472. (while (string-match ":" path)
  6473. (setq path (replace-match "+" t t path)))
  6474. (throw 'match t))))
  6475. (unless path
  6476. (error "No link found"))
  6477. ;; Remove any trailing spaces in path
  6478. (if (string-match " +\\'" path)
  6479. (setq path (replace-match "" t t path)))
  6480. (cond
  6481. ((assoc type org-link-protocols)
  6482. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6483. ((equal type "mailto")
  6484. (let ((cmd (car org-link-mailto-program))
  6485. (args (cdr org-link-mailto-program)) args1
  6486. (address path) (subject "") a)
  6487. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6488. (setq address (match-string 1 path)
  6489. subject (org-link-escape (match-string 2 path))))
  6490. (while args
  6491. (cond
  6492. ((not (stringp (car args))) (push (pop args) args1))
  6493. (t (setq a (pop args))
  6494. (if (string-match "%a" a)
  6495. (setq a (replace-match address t t a)))
  6496. (if (string-match "%s" a)
  6497. (setq a (replace-match subject t t a)))
  6498. (push a args1))))
  6499. (apply cmd (nreverse args1))))
  6500. ((member type '("http" "https" "ftp" "news"))
  6501. (browse-url (concat type ":" (org-link-escape
  6502. path org-link-escape-chars-browser))))
  6503. ((member type '("message"))
  6504. (browse-url (concat type ":" path)))
  6505. ((string= type "tags")
  6506. (org-tags-view in-emacs path))
  6507. ((string= type "thisfile")
  6508. (if in-emacs
  6509. (switch-to-buffer-other-window
  6510. (org-get-buffer-for-internal-link (current-buffer)))
  6511. (org-mark-ring-push))
  6512. (let ((cmd `(org-link-search
  6513. ,path
  6514. ,(cond ((equal in-emacs '(4)) 'occur)
  6515. ((equal in-emacs '(16)) 'org-occur)
  6516. (t nil))
  6517. ,pos)))
  6518. (condition-case nil (eval cmd)
  6519. (error (progn (widen) (eval cmd))))))
  6520. ((string= type "tree-match")
  6521. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6522. ((string= type "file")
  6523. (if (string-match "::\\([0-9]+\\)\\'" path)
  6524. (setq line (string-to-number (match-string 1 path))
  6525. path (substring path 0 (match-beginning 0)))
  6526. (if (string-match "::\\(.+\\)\\'" path)
  6527. (setq search (match-string 1 path)
  6528. path (substring path 0 (match-beginning 0)))))
  6529. (if (string-match "[*?{]" (file-name-nondirectory path))
  6530. (dired path)
  6531. (org-open-file path in-emacs line search)))
  6532. ((string= type "news")
  6533. (require 'org-gnus)
  6534. (org-gnus-follow-link path))
  6535. ((string= type "shell")
  6536. (let ((cmd path))
  6537. (if (or (not org-confirm-shell-link-function)
  6538. (funcall org-confirm-shell-link-function
  6539. (format "Execute \"%s\" in shell? "
  6540. (org-add-props cmd nil
  6541. 'face 'org-warning))))
  6542. (progn
  6543. (message "Executing %s" cmd)
  6544. (shell-command cmd))
  6545. (error "Abort"))))
  6546. ((string= type "elisp")
  6547. (let ((cmd path))
  6548. (if (or (not org-confirm-elisp-link-function)
  6549. (funcall org-confirm-elisp-link-function
  6550. (format "Execute \"%s\" as elisp? "
  6551. (org-add-props cmd nil
  6552. 'face 'org-warning))))
  6553. (message "%s => %s" cmd (eval (read cmd)))
  6554. (error "Abort"))))
  6555. (t
  6556. (browse-url-at-point)))))
  6557. (move-marker org-open-link-marker nil)
  6558. (run-hook-with-args 'org-follow-link-hook))
  6559. ;;;; Time estimates
  6560. (defun org-get-effort (&optional pom)
  6561. "Get the effort estimate for the current entry."
  6562. (org-entry-get pom org-effort-property))
  6563. ;;; File search
  6564. (defvar org-create-file-search-functions nil
  6565. "List of functions to construct the right search string for a file link.
  6566. These functions are called in turn with point at the location to
  6567. which the link should point.
  6568. A function in the hook should first test if it would like to
  6569. handle this file type, for example by checking the major-mode or
  6570. the file extension. If it decides not to handle this file, it
  6571. should just return nil to give other functions a chance. If it
  6572. does handle the file, it must return the search string to be used
  6573. when following the link. The search string will be part of the
  6574. file link, given after a double colon, and `org-open-at-point'
  6575. will automatically search for it. If special measures must be
  6576. taken to make the search successful, another function should be
  6577. added to the companion hook `org-execute-file-search-functions',
  6578. which see.
  6579. A function in this hook may also use `setq' to set the variable
  6580. `description' to provide a suggestion for the descriptive text to
  6581. be used for this link when it gets inserted into an Org-mode
  6582. buffer with \\[org-insert-link].")
  6583. (defvar org-execute-file-search-functions nil
  6584. "List of functions to execute a file search triggered by a link.
  6585. Functions added to this hook must accept a single argument, the
  6586. search string that was part of the file link, the part after the
  6587. double colon. The function must first check if it would like to
  6588. handle this search, for example by checking the major-mode or the
  6589. file extension. If it decides not to handle this search, it
  6590. should just return nil to give other functions a chance. If it
  6591. does handle the search, it must return a non-nil value to keep
  6592. other functions from trying.
  6593. Each function can access the current prefix argument through the
  6594. variable `current-prefix-argument'. Note that a single prefix is
  6595. used to force opening a link in Emacs, so it may be good to only
  6596. use a numeric or double prefix to guide the search function.
  6597. In case this is needed, a function in this hook can also restore
  6598. the window configuration before `org-open-at-point' was called using:
  6599. (set-window-configuration org-window-config-before-follow-link)")
  6600. (defun org-link-search (s &optional type avoid-pos)
  6601. "Search for a link search option.
  6602. If S is surrounded by forward slashes, it is interpreted as a
  6603. regular expression. In org-mode files, this will create an `org-occur'
  6604. sparse tree. In ordinary files, `occur' will be used to list matches.
  6605. If the current buffer is in `dired-mode', grep will be used to search
  6606. in all files. If AVOID-POS is given, ignore matches near that position."
  6607. (let ((case-fold-search t)
  6608. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6609. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6610. (append '(("") (" ") ("\t") ("\n"))
  6611. org-emphasis-alist)
  6612. "\\|") "\\)"))
  6613. (pos (point))
  6614. (pre nil) (post nil)
  6615. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6616. (cond
  6617. ;; First check if there are any special
  6618. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6619. ;; Now try the builtin stuff
  6620. ((save-excursion
  6621. (goto-char (point-min))
  6622. (and
  6623. (re-search-forward
  6624. (concat "<<" (regexp-quote s0) ">>") nil t)
  6625. (setq type 'dedicated
  6626. pos (match-beginning 0))))
  6627. ;; There is an exact target for this
  6628. (goto-char pos))
  6629. ((string-match "^/\\(.*\\)/$" s)
  6630. ;; A regular expression
  6631. (cond
  6632. ((org-mode-p)
  6633. (org-occur (match-string 1 s)))
  6634. ;;((eq major-mode 'dired-mode)
  6635. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6636. (t (org-do-occur (match-string 1 s)))))
  6637. (t
  6638. ;; A normal search strings
  6639. (when (equal (string-to-char s) ?*)
  6640. ;; Anchor on headlines, post may include tags.
  6641. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6642. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6643. s (substring s 1)))
  6644. (remove-text-properties
  6645. 0 (length s)
  6646. '(face nil mouse-face nil keymap nil fontified nil) s)
  6647. ;; Make a series of regular expressions to find a match
  6648. (setq words (org-split-string s "[ \n\r\t]+")
  6649. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6650. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6651. "\\)" markers)
  6652. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6653. re2a (concat "[ \t\r\n]" re2a_)
  6654. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6655. re4 (concat "[^a-zA-Z_]" re4_)
  6656. re1 (concat pre re2 post)
  6657. re3 (concat pre (if pre re4_ re4) post)
  6658. re5 (concat pre ".*" re4)
  6659. re2 (concat pre re2)
  6660. re2a (concat pre (if pre re2a_ re2a))
  6661. re4 (concat pre (if pre re4_ re4))
  6662. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6663. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6664. re5 "\\)"
  6665. ))
  6666. (cond
  6667. ((eq type 'org-occur) (org-occur reall))
  6668. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6669. (t (goto-char (point-min))
  6670. (setq type 'fuzzy)
  6671. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6672. (org-search-not-self 1 re1 nil t)
  6673. (org-search-not-self 1 re2 nil t)
  6674. (org-search-not-self 1 re2a nil t)
  6675. (org-search-not-self 1 re3 nil t)
  6676. (org-search-not-self 1 re4 nil t)
  6677. (org-search-not-self 1 re5 nil t)
  6678. )
  6679. (goto-char (match-beginning 1))
  6680. (goto-char pos)
  6681. (error "No match")))))
  6682. (t
  6683. ;; Normal string-search
  6684. (goto-char (point-min))
  6685. (if (search-forward s nil t)
  6686. (goto-char (match-beginning 0))
  6687. (error "No match"))))
  6688. (and (org-mode-p) (org-show-context 'link-search))
  6689. type))
  6690. (defun org-search-not-self (group &rest args)
  6691. "Execute `re-search-forward', but only accept matches that do not
  6692. enclose the position of `org-open-link-marker'."
  6693. (let ((m org-open-link-marker))
  6694. (catch 'exit
  6695. (while (apply 're-search-forward args)
  6696. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6697. (goto-char (match-end group))
  6698. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6699. (> (match-beginning 0) (marker-position m))
  6700. (< (match-end 0) (marker-position m)))
  6701. (save-match-data
  6702. (or (not (org-in-regexp
  6703. org-bracket-link-analytic-regexp 1))
  6704. (not (match-end 4)) ; no description
  6705. (and (<= (match-beginning 4) (point))
  6706. (>= (match-end 4) (point))))))
  6707. (throw 'exit (point))))))))
  6708. (defun org-get-buffer-for-internal-link (buffer)
  6709. "Return a buffer to be used for displaying the link target of internal links."
  6710. (cond
  6711. ((not org-display-internal-link-with-indirect-buffer)
  6712. buffer)
  6713. ((string-match "(Clone)$" (buffer-name buffer))
  6714. (message "Buffer is already a clone, not making another one")
  6715. ;; we also do not modify visibility in this case
  6716. buffer)
  6717. (t ; make a new indirect buffer for displaying the link
  6718. (let* ((bn (buffer-name buffer))
  6719. (ibn (concat bn "(Clone)"))
  6720. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6721. (with-current-buffer ib (org-overview))
  6722. ib))))
  6723. (defun org-do-occur (regexp &optional cleanup)
  6724. "Call the Emacs command `occur'.
  6725. If CLEANUP is non-nil, remove the printout of the regular expression
  6726. in the *Occur* buffer. This is useful if the regex is long and not useful
  6727. to read."
  6728. (occur regexp)
  6729. (when cleanup
  6730. (let ((cwin (selected-window)) win beg end)
  6731. (when (setq win (get-buffer-window "*Occur*"))
  6732. (select-window win))
  6733. (goto-char (point-min))
  6734. (when (re-search-forward "match[a-z]+" nil t)
  6735. (setq beg (match-end 0))
  6736. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6737. (setq end (1- (match-beginning 0)))))
  6738. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6739. (goto-char (point-min))
  6740. (select-window cwin))))
  6741. ;;; The mark ring for links jumps
  6742. (defvar org-mark-ring nil
  6743. "Mark ring for positions before jumps in Org-mode.")
  6744. (defvar org-mark-ring-last-goto nil
  6745. "Last position in the mark ring used to go back.")
  6746. ;; Fill and close the ring
  6747. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6748. (loop for i from 1 to org-mark-ring-length do
  6749. (push (make-marker) org-mark-ring))
  6750. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6751. org-mark-ring)
  6752. (defun org-mark-ring-push (&optional pos buffer)
  6753. "Put the current position or POS into the mark ring and rotate it."
  6754. (interactive)
  6755. (setq pos (or pos (point)))
  6756. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6757. (move-marker (car org-mark-ring)
  6758. (or pos (point))
  6759. (or buffer (current-buffer)))
  6760. (message "%s"
  6761. (substitute-command-keys
  6762. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6763. (defun org-mark-ring-goto (&optional n)
  6764. "Jump to the previous position in the mark ring.
  6765. With prefix arg N, jump back that many stored positions. When
  6766. called several times in succession, walk through the entire ring.
  6767. Org-mode commands jumping to a different position in the current file,
  6768. or to another Org-mode file, automatically push the old position
  6769. onto the ring."
  6770. (interactive "p")
  6771. (let (p m)
  6772. (if (eq last-command this-command)
  6773. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6774. (setq p org-mark-ring))
  6775. (setq org-mark-ring-last-goto p)
  6776. (setq m (car p))
  6777. (switch-to-buffer (marker-buffer m))
  6778. (goto-char m)
  6779. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6780. (defun org-remove-angle-brackets (s)
  6781. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6782. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6783. s)
  6784. (defun org-add-angle-brackets (s)
  6785. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6786. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6787. s)
  6788. (defun org-remove-double-quotes (s)
  6789. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6790. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6791. s)
  6792. ;;; Following specific links
  6793. (defun org-follow-timestamp-link ()
  6794. (cond
  6795. ((org-at-date-range-p t)
  6796. (let ((org-agenda-start-on-weekday)
  6797. (t1 (match-string 1))
  6798. (t2 (match-string 2)))
  6799. (setq t1 (time-to-days (org-time-string-to-time t1))
  6800. t2 (time-to-days (org-time-string-to-time t2)))
  6801. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6802. ((org-at-timestamp-p t)
  6803. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6804. (substring (match-string 1) 0 10)))
  6805. 1))
  6806. (t (error "This should not happen"))))
  6807. ;;; Following file links
  6808. (defvar org-wait nil)
  6809. (defun org-open-file (path &optional in-emacs line search)
  6810. "Open the file at PATH.
  6811. First, this expands any special file name abbreviations. Then the
  6812. configuration variable `org-file-apps' is checked if it contains an
  6813. entry for this file type, and if yes, the corresponding command is launched.
  6814. If no application is found, Emacs simply visits the file.
  6815. With optional argument IN-EMACS, Emacs will visit the file.
  6816. Optional LINE specifies a line to go to, optional SEARCH a string to
  6817. search for. If LINE or SEARCH is given, the file will always be
  6818. opened in Emacs.
  6819. If the file does not exist, an error is thrown."
  6820. (setq in-emacs (or in-emacs line search))
  6821. (let* ((file (if (equal path "")
  6822. buffer-file-name
  6823. (substitute-in-file-name (expand-file-name path))))
  6824. (apps (append org-file-apps (org-default-apps)))
  6825. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6826. (dirp (if remp nil (file-directory-p file)))
  6827. (dfile (downcase file))
  6828. (old-buffer (current-buffer))
  6829. (old-pos (point))
  6830. (old-mode major-mode)
  6831. ext cmd)
  6832. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6833. (setq ext (match-string 1 dfile))
  6834. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6835. (setq ext (match-string 1 dfile))))
  6836. (if in-emacs
  6837. (setq cmd 'emacs)
  6838. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6839. (and dirp (cdr (assoc 'directory apps)))
  6840. (cdr (assoc ext apps))
  6841. (cdr (assoc t apps)))))
  6842. (when (eq cmd 'mailcap)
  6843. (require 'mailcap)
  6844. (mailcap-parse-mailcaps)
  6845. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6846. (command (mailcap-mime-info mime-type)))
  6847. (if (stringp command)
  6848. (setq cmd command)
  6849. (setq cmd 'emacs))))
  6850. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6851. (not (file-exists-p file))
  6852. (not org-open-non-existing-files))
  6853. (error "No such file: %s" file))
  6854. (cond
  6855. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6856. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6857. (while (string-match "['\"]%s['\"]" cmd)
  6858. (setq cmd (replace-match "%s" t t cmd)))
  6859. (while (string-match "%s" cmd)
  6860. (setq cmd (replace-match
  6861. (save-match-data
  6862. (shell-quote-argument
  6863. (convert-standard-filename file)))
  6864. t t cmd)))
  6865. (save-window-excursion
  6866. (start-process-shell-command cmd nil cmd)
  6867. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6868. ))
  6869. ((or (stringp cmd)
  6870. (eq cmd 'emacs))
  6871. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6872. (widen)
  6873. (if line (goto-line line)
  6874. (if search (org-link-search search))))
  6875. ((consp cmd)
  6876. (let ((file (convert-standard-filename file)))
  6877. (eval cmd)))
  6878. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6879. (and (org-mode-p) (eq old-mode 'org-mode)
  6880. (or (not (equal old-buffer (current-buffer)))
  6881. (not (equal old-pos (point))))
  6882. (org-mark-ring-push old-pos old-buffer))))
  6883. (defun org-default-apps ()
  6884. "Return the default applications for this operating system."
  6885. (cond
  6886. ((eq system-type 'darwin)
  6887. org-file-apps-defaults-macosx)
  6888. ((eq system-type 'windows-nt)
  6889. org-file-apps-defaults-windowsnt)
  6890. (t org-file-apps-defaults-gnu)))
  6891. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6892. (defun org-file-remote-p (file)
  6893. "Test whether FILE specifies a location on a remote system.
  6894. Return non-nil if the location is indeed remote.
  6895. For example, the filename \"/user@host:/foo\" specifies a location
  6896. on the system \"/user@host:\"."
  6897. (cond ((fboundp 'file-remote-p)
  6898. (file-remote-p file))
  6899. ((fboundp 'tramp-handle-file-remote-p)
  6900. (tramp-handle-file-remote-p file))
  6901. ((and (boundp 'ange-ftp-name-format)
  6902. (string-match (car ange-ftp-name-format) file))
  6903. t)
  6904. (t nil)))
  6905. ;;;; Refiling
  6906. (defun org-get-org-file ()
  6907. "Read a filename, with default directory `org-directory'."
  6908. (let ((default (or org-default-notes-file remember-data-file)))
  6909. (read-file-name (format "File name [%s]: " default)
  6910. (file-name-as-directory org-directory)
  6911. default)))
  6912. (defun org-notes-order-reversed-p ()
  6913. "Check if the current file should receive notes in reversed order."
  6914. (cond
  6915. ((not org-reverse-note-order) nil)
  6916. ((eq t org-reverse-note-order) t)
  6917. ((not (listp org-reverse-note-order)) nil)
  6918. (t (catch 'exit
  6919. (let ((all org-reverse-note-order)
  6920. entry)
  6921. (while (setq entry (pop all))
  6922. (if (string-match (car entry) buffer-file-name)
  6923. (throw 'exit (cdr entry))))
  6924. nil)))))
  6925. (defvar org-refile-target-table nil
  6926. "The list of refile targets, created by `org-refile'.")
  6927. (defvar org-agenda-new-buffers nil
  6928. "Buffers created to visit agenda files.")
  6929. (defun org-get-refile-targets (&optional default-buffer)
  6930. "Produce a table with refile targets."
  6931. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6932. targets txt re files f desc descre)
  6933. (with-current-buffer (or default-buffer (current-buffer))
  6934. (while (setq entry (pop entries))
  6935. (setq files (car entry) desc (cdr entry))
  6936. (cond
  6937. ((null files) (setq files (list (current-buffer))))
  6938. ((eq files 'org-agenda-files)
  6939. (setq files (org-agenda-files 'unrestricted)))
  6940. ((and (symbolp files) (fboundp files))
  6941. (setq files (funcall files)))
  6942. ((and (symbolp files) (boundp files))
  6943. (setq files (symbol-value files))))
  6944. (if (stringp files) (setq files (list files)))
  6945. (cond
  6946. ((eq (car desc) :tag)
  6947. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6948. ((eq (car desc) :todo)
  6949. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6950. ((eq (car desc) :regexp)
  6951. (setq descre (cdr desc)))
  6952. ((eq (car desc) :level)
  6953. (setq descre (concat "^\\*\\{" (number-to-string
  6954. (if org-odd-levels-only
  6955. (1- (* 2 (cdr desc)))
  6956. (cdr desc)))
  6957. "\\}[ \t]")))
  6958. ((eq (car desc) :maxlevel)
  6959. (setq descre (concat "^\\*\\{1," (number-to-string
  6960. (if org-odd-levels-only
  6961. (1- (* 2 (cdr desc)))
  6962. (cdr desc)))
  6963. "\\}[ \t]")))
  6964. (t (error "Bad refiling target description %s" desc)))
  6965. (while (setq f (pop files))
  6966. (save-excursion
  6967. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6968. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6969. (save-excursion
  6970. (save-restriction
  6971. (widen)
  6972. (goto-char (point-min))
  6973. (while (re-search-forward descre nil t)
  6974. (goto-char (point-at-bol))
  6975. (when (looking-at org-complex-heading-regexp)
  6976. (setq txt (match-string 4)
  6977. re (concat "^" (regexp-quote
  6978. (buffer-substring (match-beginning 1)
  6979. (match-end 4)))))
  6980. (if (match-end 5) (setq re (concat re "[ \t]+"
  6981. (regexp-quote
  6982. (match-string 5)))))
  6983. (setq re (concat re "[ \t]*$"))
  6984. (when org-refile-use-outline-path
  6985. (setq txt (mapconcat 'identity
  6986. (append
  6987. (if (eq org-refile-use-outline-path 'file)
  6988. (list (file-name-nondirectory
  6989. (buffer-file-name (buffer-base-buffer))))
  6990. (if (eq org-refile-use-outline-path 'full-file-path)
  6991. (list (buffer-file-name (buffer-base-buffer)))))
  6992. (org-get-outline-path)
  6993. (list txt))
  6994. "/")))
  6995. (push (list txt f re (point)) targets))
  6996. (goto-char (point-at-eol))))))))
  6997. (nreverse targets))))
  6998. (defun org-get-outline-path ()
  6999. "Return the outline path to the current entry, as a list."
  7000. (let (rtn)
  7001. (save-excursion
  7002. (while (org-up-heading-safe)
  7003. (when (looking-at org-complex-heading-regexp)
  7004. (push (org-match-string-no-properties 4) rtn)))
  7005. rtn)))
  7006. (defvar org-refile-history nil
  7007. "History for refiling operations.")
  7008. (defun org-refile (&optional goto default-buffer)
  7009. "Move the entry at point to another heading.
  7010. The list of target headings is compiled using the information in
  7011. `org-refile-targets', which see. This list is created before each use
  7012. and will therefore always be up-to-date.
  7013. At the target location, the entry is filed as a subitem of the target heading.
  7014. Depending on `org-reverse-note-order', the new subitem will either be the
  7015. first of the last subitem.
  7016. With prefix arg GOTO, the command will only visit the target location,
  7017. not actually move anything.
  7018. With a double prefix `C-c C-c', go to the location where the last refiling
  7019. operation has put the subtree."
  7020. (interactive "P")
  7021. (let* ((cbuf (current-buffer))
  7022. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7023. pos it nbuf file re level reversed)
  7024. (if (equal goto '(16))
  7025. (org-refile-goto-last-stored)
  7026. (when (setq it (org-refile-get-location
  7027. (if goto "Goto: " "Refile to: ") default-buffer))
  7028. (setq file (nth 1 it)
  7029. re (nth 2 it)
  7030. pos (nth 3 it))
  7031. (setq nbuf (or (find-buffer-visiting file)
  7032. (find-file-noselect file)))
  7033. (if goto
  7034. (progn
  7035. (switch-to-buffer nbuf)
  7036. (goto-char pos)
  7037. (org-show-context 'org-goto))
  7038. (org-copy-subtree 1 nil t)
  7039. (save-excursion
  7040. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  7041. (find-file-noselect file))))
  7042. (setq reversed (org-notes-order-reversed-p))
  7043. (save-excursion
  7044. (save-restriction
  7045. (widen)
  7046. (goto-char pos)
  7047. (looking-at outline-regexp)
  7048. (setq level (org-get-valid-level (funcall outline-level) 1))
  7049. (goto-char
  7050. (if reversed
  7051. (outline-next-heading)
  7052. (or (save-excursion (outline-get-next-sibling))
  7053. (org-end-of-subtree t t)
  7054. (point-max))))
  7055. (bookmark-set "org-refile-last-stored")
  7056. (org-paste-subtree level))))
  7057. (org-cut-subtree)
  7058. (setq org-markers-to-move nil)
  7059. (message "Entry refiled to \"%s\"" (car it)))))))
  7060. (defun org-refile-goto-last-stored ()
  7061. "Go to the location where the last refile was stored."
  7062. (interactive)
  7063. (bookmark-jump "org-refile-last-stored")
  7064. (message "This is the location of the last refile"))
  7065. (defun org-refile-get-location (&optional prompt default-buffer)
  7066. "Prompt the user for a refile location, using PROMPT."
  7067. (let ((org-refile-targets org-refile-targets)
  7068. (org-refile-use-outline-path org-refile-use-outline-path))
  7069. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  7070. (unless org-refile-target-table
  7071. (error "No refile targets"))
  7072. (let* ((cbuf (current-buffer))
  7073. (cfunc (if org-refile-use-outline-path
  7074. 'org-olpath-completing-read
  7075. 'completing-read))
  7076. (extra (if org-refile-use-outline-path "/" ""))
  7077. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  7078. (fname (and filename (file-truename filename)))
  7079. (tbl (mapcar
  7080. (lambda (x)
  7081. (if (not (equal fname (file-truename (nth 1 x))))
  7082. (cons (concat (car x) extra " ("
  7083. (file-name-nondirectory (nth 1 x)) ")")
  7084. (cdr x))
  7085. (cons (concat (car x) extra) (cdr x))))
  7086. org-refile-target-table))
  7087. (completion-ignore-case t))
  7088. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  7089. tbl)))
  7090. (defun org-olpath-completing-read (prompt collection &rest args)
  7091. "Read an outline path like a file name."
  7092. (let ((thetable collection))
  7093. (apply
  7094. 'completing-read prompt
  7095. (lambda (string predicate &optional flag)
  7096. (let (rtn r s f (l (length string)))
  7097. (cond
  7098. ((eq flag nil)
  7099. ;; try completion
  7100. (try-completion string thetable))
  7101. ((eq flag t)
  7102. ;; all-completions
  7103. (setq rtn (all-completions string thetable predicate))
  7104. (mapcar
  7105. (lambda (x)
  7106. (setq r (substring x l))
  7107. (if (string-match " ([^)]*)$" x)
  7108. (setq f (match-string 0 x))
  7109. (setq f ""))
  7110. (if (string-match "/" r)
  7111. (concat string (substring r 0 (match-end 0)) f)
  7112. x))
  7113. rtn))
  7114. ((eq flag 'lambda)
  7115. ;; exact match?
  7116. (assoc string thetable)))
  7117. ))
  7118. args)))
  7119. ;;;; Dynamic blocks
  7120. (defun org-find-dblock (name)
  7121. "Find the first dynamic block with name NAME in the buffer.
  7122. If not found, stay at current position and return nil."
  7123. (let (pos)
  7124. (save-excursion
  7125. (goto-char (point-min))
  7126. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  7127. nil t)
  7128. (match-beginning 0))))
  7129. (if pos (goto-char pos))
  7130. pos))
  7131. (defconst org-dblock-start-re
  7132. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  7133. "Matches the startline of a dynamic block, with parameters.")
  7134. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  7135. "Matches the end of a dyhamic block.")
  7136. (defun org-create-dblock (plist)
  7137. "Create a dynamic block section, with parameters taken from PLIST.
  7138. PLIST must containe a :name entry which is used as name of the block."
  7139. (unless (bolp) (newline))
  7140. (let ((name (plist-get plist :name)))
  7141. (insert "#+BEGIN: " name)
  7142. (while plist
  7143. (if (eq (car plist) :name)
  7144. (setq plist (cddr plist))
  7145. (insert " " (prin1-to-string (pop plist)))))
  7146. (insert "\n\n#+END:\n")
  7147. (beginning-of-line -2)))
  7148. (defun org-prepare-dblock ()
  7149. "Prepare dynamic block for refresh.
  7150. This empties the block, puts the cursor at the insert position and returns
  7151. the property list including an extra property :name with the block name."
  7152. (unless (looking-at org-dblock-start-re)
  7153. (error "Not at a dynamic block"))
  7154. (let* ((begdel (1+ (match-end 0)))
  7155. (name (org-no-properties (match-string 1)))
  7156. (params (append (list :name name)
  7157. (read (concat "(" (match-string 3) ")")))))
  7158. (unless (re-search-forward org-dblock-end-re nil t)
  7159. (error "Dynamic block not terminated"))
  7160. (setq params
  7161. (append params
  7162. (list :content (buffer-substring
  7163. begdel (match-beginning 0)))))
  7164. (delete-region begdel (match-beginning 0))
  7165. (goto-char begdel)
  7166. (open-line 1)
  7167. params))
  7168. (defun org-map-dblocks (&optional command)
  7169. "Apply COMMAND to all dynamic blocks in the current buffer.
  7170. If COMMAND is not given, use `org-update-dblock'."
  7171. (let ((cmd (or command 'org-update-dblock))
  7172. pos)
  7173. (save-excursion
  7174. (goto-char (point-min))
  7175. (while (re-search-forward org-dblock-start-re nil t)
  7176. (goto-char (setq pos (match-beginning 0)))
  7177. (condition-case nil
  7178. (funcall cmd)
  7179. (error (message "Error during update of dynamic block")))
  7180. (goto-char pos)
  7181. (unless (re-search-forward org-dblock-end-re nil t)
  7182. (error "Dynamic block not terminated"))))))
  7183. (defun org-dblock-update (&optional arg)
  7184. "User command for updating dynamic blocks.
  7185. Update the dynamic block at point. With prefix ARG, update all dynamic
  7186. blocks in the buffer."
  7187. (interactive "P")
  7188. (if arg
  7189. (org-update-all-dblocks)
  7190. (or (looking-at org-dblock-start-re)
  7191. (org-beginning-of-dblock))
  7192. (org-update-dblock)))
  7193. (defun org-update-dblock ()
  7194. "Update the dynamic block at point
  7195. This means to empty the block, parse for parameters and then call
  7196. the correct writing function."
  7197. (save-window-excursion
  7198. (let* ((pos (point))
  7199. (line (org-current-line))
  7200. (params (org-prepare-dblock))
  7201. (name (plist-get params :name))
  7202. (cmd (intern (concat "org-dblock-write:" name))))
  7203. (message "Updating dynamic block `%s' at line %d..." name line)
  7204. (funcall cmd params)
  7205. (message "Updating dynamic block `%s' at line %d...done" name line)
  7206. (goto-char pos))))
  7207. (defun org-beginning-of-dblock ()
  7208. "Find the beginning of the dynamic block at point.
  7209. Error if there is no scuh block at point."
  7210. (let ((pos (point))
  7211. beg)
  7212. (end-of-line 1)
  7213. (if (and (re-search-backward org-dblock-start-re nil t)
  7214. (setq beg (match-beginning 0))
  7215. (re-search-forward org-dblock-end-re nil t)
  7216. (> (match-end 0) pos))
  7217. (goto-char beg)
  7218. (goto-char pos)
  7219. (error "Not in a dynamic block"))))
  7220. (defun org-update-all-dblocks ()
  7221. "Update all dynamic blocks in the buffer.
  7222. This function can be used in a hook."
  7223. (when (org-mode-p)
  7224. (org-map-dblocks 'org-update-dblock)))
  7225. ;;;; Completion
  7226. (defconst org-additional-option-like-keywords
  7227. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  7228. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  7229. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  7230. (defcustom org-structure-template-alist
  7231. '(
  7232. ("s" "#+begin_src ?\n\n#+end_src"
  7233. "<src lang=\"?\">\n\n</src>")
  7234. ("e" "#+begin_example\n?\n#+end_example"
  7235. "<example>\n?\n</example>")
  7236. ("q" "#+begin_quote\n?\n#+end_quote"
  7237. "<quote>\n?\n</quote>")
  7238. ("v" "#+begin_verse\n?\n#+end_verse"
  7239. "<verse>\n?\n/verse>")
  7240. ("l" "#+begin_latex\n?\n#+end_latex"
  7241. "<literal style=\"latex\">\n?\n</literal>")
  7242. ("L" "#+latex: "
  7243. "<literal style=\"latex\">?</literal>")
  7244. ("h" "#+begin_html\n?\n#+end_html"
  7245. "<literal style=\"html\">\n?\n</literal>")
  7246. ("H" "#+html: "
  7247. "<literal style=\"html\">?</literal>")
  7248. ("a" "#+begin_ascii\n?\n#+end_ascii")
  7249. ("A" "#+ascii: ")
  7250. ("i" "#+include %file ?"
  7251. "<include file=%file markup=\"?\">")
  7252. )
  7253. "Structure completion elements.
  7254. This is a list of abbreviation keys and values. The value gets inserted
  7255. it you type @samp{.} followed by the key and then the completion key,
  7256. usually `M-TAB'. %file will be replaced by a file name after prompting
  7257. for the file uning completion.
  7258. There are two templates for each key, the first uses the original Org syntax,
  7259. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  7260. the default when the /org-mtags.el/ module has been loaded. See also the
  7261. variable `org-mtags-prefere-muse-templates'.
  7262. This is an experimental feature, it is undecided if it is going to stay in."
  7263. :group 'org-completion
  7264. :type '(repeat
  7265. (string :tag "Key")
  7266. (string :tag "Template")
  7267. (string :tag "Muse Template")))
  7268. (defun org-try-structure-completion ()
  7269. "Try to complete a structure template before point.
  7270. This looks for strings like \"<e\" on an otherwise empty line and
  7271. expands them."
  7272. (let ((l (buffer-substring (point-at-bol) (point)))
  7273. a)
  7274. (when (and (looking-at "[ \t]*$")
  7275. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7276. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7277. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7278. (match-beginning 1)) a)
  7279. t)))
  7280. (defun org-complete-expand-structure-template (start cell)
  7281. "Expand a structure template."
  7282. (let* ((musep (org-bound-and-true-p org-mtags-prefere-muse-templates))
  7283. (rpl (nth (if musep 2 1) cell)))
  7284. (delete-region start (point))
  7285. (when (string-match "\\`#\\+" rpl)
  7286. (cond
  7287. ((bolp))
  7288. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7289. (delete-region (point-at-bol) (point)))
  7290. (t (newline))))
  7291. (setq start (point))
  7292. (if (string-match "%file" rpl)
  7293. (setq rpl (replace-match
  7294. (concat
  7295. "\""
  7296. (save-match-data
  7297. (abbreviate-file-name (read-file-name "Include file: ")))
  7298. "\"")
  7299. t t rpl)))
  7300. (insert rpl)
  7301. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7302. (defun org-complete (&optional arg)
  7303. "Perform completion on word at point.
  7304. At the beginning of a headline, this completes TODO keywords as given in
  7305. `org-todo-keywords'.
  7306. If the current word is preceded by a backslash, completes the TeX symbols
  7307. that are supported for HTML support.
  7308. If the current word is preceded by \"#+\", completes special words for
  7309. setting file options.
  7310. In the line after \"#+STARTUP:, complete valid keywords.\"
  7311. At all other locations, this simply calls the value of
  7312. `org-completion-fallback-command'."
  7313. (interactive "P")
  7314. (org-without-partial-completion
  7315. (catch 'exit
  7316. (let* ((a nil)
  7317. (end (point))
  7318. (beg1 (save-excursion
  7319. (skip-chars-backward (org-re "[:alnum:]_@"))
  7320. (point)))
  7321. (beg (save-excursion
  7322. (skip-chars-backward "a-zA-Z0-9_:$")
  7323. (point)))
  7324. (confirm (lambda (x) (stringp (car x))))
  7325. (searchhead (equal (char-before beg) ?*))
  7326. (struct
  7327. (when (and (member (char-before beg1) '(?. ?<))
  7328. (setq a (assoc (buffer-substring beg1 (point))
  7329. org-structure-template-alist)))
  7330. (org-complete-expand-structure-template (1- beg1) a)
  7331. (throw 'exit t)))
  7332. (tag (and (equal (char-before beg1) ?:)
  7333. (equal (char-after (point-at-bol)) ?*)))
  7334. (prop (and (equal (char-before beg1) ?:)
  7335. (not (equal (char-after (point-at-bol)) ?*))))
  7336. (texp (equal (char-before beg) ?\\))
  7337. (link (equal (char-before beg) ?\[))
  7338. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7339. beg)
  7340. "#+"))
  7341. (startup (string-match "^#\\+STARTUP:.*"
  7342. (buffer-substring (point-at-bol) (point))))
  7343. (completion-ignore-case opt)
  7344. (type nil)
  7345. (tbl nil)
  7346. (table (cond
  7347. (opt
  7348. (setq type :opt)
  7349. (require 'org-exp)
  7350. (append
  7351. (mapcar
  7352. (lambda (x)
  7353. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7354. (cons (match-string 2 x) (match-string 1 x)))
  7355. (org-split-string (org-get-current-options) "\n"))
  7356. (mapcar 'list org-additional-option-like-keywords)))
  7357. (startup
  7358. (setq type :startup)
  7359. org-startup-options)
  7360. (link (append org-link-abbrev-alist-local
  7361. org-link-abbrev-alist))
  7362. (texp
  7363. (setq type :tex)
  7364. org-html-entities)
  7365. ((string-match "\\`\\*+[ \t]+\\'"
  7366. (buffer-substring (point-at-bol) beg))
  7367. (setq type :todo)
  7368. (mapcar 'list org-todo-keywords-1))
  7369. (searchhead
  7370. (setq type :searchhead)
  7371. (save-excursion
  7372. (goto-char (point-min))
  7373. (while (re-search-forward org-todo-line-regexp nil t)
  7374. (push (list
  7375. (org-make-org-heading-search-string
  7376. (match-string 3) t))
  7377. tbl)))
  7378. tbl)
  7379. (tag (setq type :tag beg beg1)
  7380. (or org-tag-alist (org-get-buffer-tags)))
  7381. (prop (setq type :prop beg beg1)
  7382. (mapcar 'list (org-buffer-property-keys nil t t)))
  7383. (t (progn
  7384. (call-interactively org-completion-fallback-command)
  7385. (throw 'exit nil)))))
  7386. (pattern (buffer-substring-no-properties beg end))
  7387. (completion (try-completion pattern table confirm)))
  7388. (cond ((eq completion t)
  7389. (if (not (assoc (upcase pattern) table))
  7390. (message "Already complete")
  7391. (if (and (equal type :opt)
  7392. (not (member (car (assoc (upcase pattern) table))
  7393. org-additional-option-like-keywords)))
  7394. (insert (substring (cdr (assoc (upcase pattern) table))
  7395. (length pattern)))
  7396. (if (memq type '(:tag :prop)) (insert ":")))))
  7397. ((null completion)
  7398. (message "Can't find completion for \"%s\"" pattern)
  7399. (ding))
  7400. ((not (string= pattern completion))
  7401. (delete-region beg end)
  7402. (if (string-match " +$" completion)
  7403. (setq completion (replace-match "" t t completion)))
  7404. (insert completion)
  7405. (if (get-buffer-window "*Completions*")
  7406. (delete-window (get-buffer-window "*Completions*")))
  7407. (if (assoc completion table)
  7408. (if (eq type :todo) (insert " ")
  7409. (if (memq type '(:tag :prop)) (insert ":"))))
  7410. (if (and (equal type :opt) (assoc completion table))
  7411. (message "%s" (substitute-command-keys
  7412. "Press \\[org-complete] again to insert example settings"))))
  7413. (t
  7414. (message "Making completion list...")
  7415. (let ((list (sort (all-completions pattern table confirm)
  7416. 'string<)))
  7417. (with-output-to-temp-buffer "*Completions*"
  7418. (condition-case nil
  7419. ;; Protection needed for XEmacs and emacs 21
  7420. (display-completion-list list pattern)
  7421. (error (display-completion-list list)))))
  7422. (message "Making completion list...%s" "done")))))))
  7423. ;;;; TODO, DEADLINE, Comments
  7424. (defun org-toggle-comment ()
  7425. "Change the COMMENT state of an entry."
  7426. (interactive)
  7427. (save-excursion
  7428. (org-back-to-heading)
  7429. (let (case-fold-search)
  7430. (if (looking-at (concat outline-regexp
  7431. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7432. (replace-match "" t t nil 1)
  7433. (if (looking-at outline-regexp)
  7434. (progn
  7435. (goto-char (match-end 0))
  7436. (insert org-comment-string " ")))))))
  7437. (defvar org-last-todo-state-is-todo nil
  7438. "This is non-nil when the last TODO state change led to a TODO state.
  7439. If the last change removed the TODO tag or switched to DONE, then
  7440. this is nil.")
  7441. (defvar org-setting-tags nil) ; dynamically skiped
  7442. (defun org-parse-local-options (string var)
  7443. "Parse STRING for startup setting relevant for variable VAR."
  7444. (let ((rtn (symbol-value var))
  7445. e opts)
  7446. (save-match-data
  7447. (if (or (not string) (not (string-match "\\S-" string)))
  7448. rtn
  7449. (setq opts (delq nil (mapcar (lambda (x)
  7450. (setq e (assoc x org-startup-options))
  7451. (if (eq (nth 1 e) var) e nil))
  7452. (org-split-string string "[ \t]+"))))
  7453. (if (not opts)
  7454. rtn
  7455. (setq rtn nil)
  7456. (while (setq e (pop opts))
  7457. (if (not (nth 3 e))
  7458. (setq rtn (nth 2 e))
  7459. (if (not (listp rtn)) (setq rtn nil))
  7460. (push (nth 2 e) rtn)))
  7461. rtn)))))
  7462. (defvar org-blocker-hook nil
  7463. "Hook for functions that are allowed to block a state change.
  7464. Each function gets as its single argument a property list, see
  7465. `org-trigger-hook' for more information about this list.
  7466. If any of the functions in this hook returns nil, the state change
  7467. is blocked.")
  7468. (defvar org-trigger-hook nil
  7469. "Hook for functions that are triggered by a state change.
  7470. Each function gets as its single argument a property list with at least
  7471. the following elements:
  7472. (:type type-of-change :position pos-at-entry-start
  7473. :from old-state :to new-state)
  7474. Depending on the type, more properties may be present.
  7475. This mechanism is currently implemented for:
  7476. TODO state changes
  7477. ------------------
  7478. :type todo-state-change
  7479. :from previous state (keyword as a string), or nil
  7480. :to new state (keyword as a string), or nil")
  7481. (defun org-todo (&optional arg)
  7482. "Change the TODO state of an item.
  7483. The state of an item is given by a keyword at the start of the heading,
  7484. like
  7485. *** TODO Write paper
  7486. *** DONE Call mom
  7487. The different keywords are specified in the variable `org-todo-keywords'.
  7488. By default the available states are \"TODO\" and \"DONE\".
  7489. So for this example: when the item starts with TODO, it is changed to DONE.
  7490. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7491. DONE are present, add TODO at the beginning of the heading.
  7492. With C-u prefix arg, use completion to determine the new state.
  7493. With numeric prefix arg, switch to that state.
  7494. For calling through lisp, arg is also interpreted in the following way:
  7495. 'none -> empty state
  7496. \"\"(empty string) -> switch to empty state
  7497. 'done -> switch to DONE
  7498. 'nextset -> switch to the next set of keywords
  7499. 'previousset -> switch to the previous set of keywords
  7500. \"WAITING\" -> switch to the specified keyword, but only if it
  7501. really is a member of `org-todo-keywords'."
  7502. (interactive "P")
  7503. (save-excursion
  7504. (catch 'exit
  7505. (org-back-to-heading)
  7506. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7507. (or (looking-at (concat " +" org-todo-regexp " *"))
  7508. (looking-at " *"))
  7509. (let* ((match-data (match-data))
  7510. (startpos (point-at-bol))
  7511. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7512. (org-log-done org-log-done)
  7513. (org-log-repeat org-log-repeat)
  7514. (org-todo-log-states org-todo-log-states)
  7515. (this (match-string 1))
  7516. (hl-pos (match-beginning 0))
  7517. (head (org-get-todo-sequence-head this))
  7518. (ass (assoc head org-todo-kwd-alist))
  7519. (interpret (nth 1 ass))
  7520. (done-word (nth 3 ass))
  7521. (final-done-word (nth 4 ass))
  7522. (last-state (or this ""))
  7523. (completion-ignore-case t)
  7524. (member (member this org-todo-keywords-1))
  7525. (tail (cdr member))
  7526. (state (cond
  7527. ((and org-todo-key-trigger
  7528. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7529. (and (not arg) org-use-fast-todo-selection
  7530. (not (eq org-use-fast-todo-selection 'prefix)))))
  7531. ;; Use fast selection
  7532. (org-fast-todo-selection))
  7533. ((and (equal arg '(4))
  7534. (or (not org-use-fast-todo-selection)
  7535. (not org-todo-key-trigger)))
  7536. ;; Read a state with completion
  7537. (completing-read "State: " (mapcar (lambda(x) (list x))
  7538. org-todo-keywords-1)
  7539. nil t))
  7540. ((eq arg 'right)
  7541. (if this
  7542. (if tail (car tail) nil)
  7543. (car org-todo-keywords-1)))
  7544. ((eq arg 'left)
  7545. (if (equal member org-todo-keywords-1)
  7546. nil
  7547. (if this
  7548. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7549. org-todo-keywords-1)
  7550. (org-last org-todo-keywords-1))))
  7551. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7552. (setq arg nil))) ; hack to fall back to cycling
  7553. (arg
  7554. ;; user or caller requests a specific state
  7555. (cond
  7556. ((equal arg "") nil)
  7557. ((eq arg 'none) nil)
  7558. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7559. ((eq arg 'nextset)
  7560. (or (car (cdr (member head org-todo-heads)))
  7561. (car org-todo-heads)))
  7562. ((eq arg 'previousset)
  7563. (let ((org-todo-heads (reverse org-todo-heads)))
  7564. (or (car (cdr (member head org-todo-heads)))
  7565. (car org-todo-heads))))
  7566. ((car (member arg org-todo-keywords-1)))
  7567. ((nth (1- (prefix-numeric-value arg))
  7568. org-todo-keywords-1))))
  7569. ((null member) (or head (car org-todo-keywords-1)))
  7570. ((equal this final-done-word) nil) ;; -> make empty
  7571. ((null tail) nil) ;; -> first entry
  7572. ((eq interpret 'sequence)
  7573. (car tail))
  7574. ((memq interpret '(type priority))
  7575. (if (eq this-command last-command)
  7576. (car tail)
  7577. (if (> (length tail) 0)
  7578. (or done-word (car org-done-keywords))
  7579. nil)))
  7580. (t nil)))
  7581. (next (if state (concat " " state " ") " "))
  7582. (change-plist (list :type 'todo-state-change :from this :to state
  7583. :position startpos))
  7584. dolog now-done-p)
  7585. (when org-blocker-hook
  7586. (unless (save-excursion
  7587. (save-match-data
  7588. (run-hook-with-args-until-failure
  7589. 'org-blocker-hook change-plist)))
  7590. (if (interactive-p)
  7591. (error "TODO state change from %s to %s blocked" this state)
  7592. ;; fail silently
  7593. (message "TODO state change from %s to %s blocked" this state)
  7594. (throw 'exit nil))))
  7595. (store-match-data match-data)
  7596. (replace-match next t t)
  7597. (unless (pos-visible-in-window-p hl-pos)
  7598. (message "TODO state changed to %s" (org-trim next)))
  7599. (unless head
  7600. (setq head (org-get-todo-sequence-head state)
  7601. ass (assoc head org-todo-kwd-alist)
  7602. interpret (nth 1 ass)
  7603. done-word (nth 3 ass)
  7604. final-done-word (nth 4 ass)))
  7605. (when (memq arg '(nextset previousset))
  7606. (message "Keyword-Set %d/%d: %s"
  7607. (- (length org-todo-sets) -1
  7608. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7609. (length org-todo-sets)
  7610. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7611. (setq org-last-todo-state-is-todo
  7612. (not (member state org-done-keywords)))
  7613. (setq now-done-p (and (member state org-done-keywords)
  7614. (not (member this org-done-keywords))))
  7615. (and logging (org-local-logging logging))
  7616. (when (and (or org-todo-log-states org-log-done)
  7617. (not (memq arg '(nextset previousset))))
  7618. ;; we need to look at recording a time and note
  7619. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7620. (nth 2 (assoc this org-todo-log-states))))
  7621. (when (and state
  7622. (member state org-not-done-keywords)
  7623. (not (member this org-not-done-keywords)))
  7624. ;; This is now a todo state and was not one before
  7625. ;; If there was a CLOSED time stamp, get rid of it.
  7626. (org-add-planning-info nil nil 'closed))
  7627. (when (and now-done-p org-log-done)
  7628. ;; It is now done, and it was not done before
  7629. (org-add-planning-info 'closed (org-current-time))
  7630. (if (and (not dolog) (eq 'note org-log-done))
  7631. (org-add-log-setup 'done state 'findpos 'note)))
  7632. (when (and state dolog)
  7633. ;; This is a non-nil state, and we need to log it
  7634. (org-add-log-setup 'state state 'findpos dolog)))
  7635. ;; Fixup tag positioning
  7636. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7637. (when org-provide-todo-statistics
  7638. (org-update-parent-todo-statistics))
  7639. (run-hooks 'org-after-todo-state-change-hook)
  7640. (if (and arg (not (member state org-done-keywords)))
  7641. (setq head (org-get-todo-sequence-head state)))
  7642. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7643. ;; Do we need to trigger a repeat?
  7644. (when now-done-p (org-auto-repeat-maybe state))
  7645. ;; Fixup cursor location if close to the keyword
  7646. (if (and (outline-on-heading-p)
  7647. (not (bolp))
  7648. (save-excursion (beginning-of-line 1)
  7649. (looking-at org-todo-line-regexp))
  7650. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7651. (progn
  7652. (goto-char (or (match-end 2) (match-end 1)))
  7653. (just-one-space)))
  7654. (when org-trigger-hook
  7655. (save-excursion
  7656. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7657. (defun org-update-parent-todo-statistics ()
  7658. "Update any statistics cookie in the parent of the current headline."
  7659. (interactive)
  7660. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7661. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7662. (catch 'exit
  7663. (save-excursion
  7664. (setq level (org-up-heading-safe))
  7665. (unless (and level
  7666. (re-search-forward box-re (point-at-eol) t))
  7667. (throw 'exit nil))
  7668. (setq is-percent (match-end 2))
  7669. (save-match-data
  7670. (unless (outline-next-heading) (throw 'exit nil))
  7671. (while (looking-at org-todo-line-regexp)
  7672. (setq kwd (match-string 2))
  7673. (and kwd (setq cnt-all (1+ cnt-all)))
  7674. (and (member kwd org-done-keywords)
  7675. (setq cnt-done (1+ cnt-done)))
  7676. (condition-case nil
  7677. (outline-forward-same-level 1)
  7678. (error (end-of-line 1)))))
  7679. (replace-match
  7680. (if is-percent
  7681. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7682. (format "[%d/%d]" cnt-done cnt-all)))
  7683. (run-hook-with-args 'org-after-todo-statistics-hook
  7684. cnt-done (- cnt-all cnt-done))))))
  7685. (defvar org-after-todo-statistics-hook nil
  7686. "Hook that is called after a TODO statistics cookie has been updated.
  7687. Each function is called with two arguments: the number of not-done entries
  7688. and the number of done entries.
  7689. For example, the following function, when added to this hook, will switch
  7690. an entry to DONE when all children are done, and back to TODO when new
  7691. entries are set to a TODO status. Note that this hook is only called
  7692. when there is a statistics cookie in the headline!
  7693. (defun org-summary-todo (n-done n-not-done)
  7694. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7695. (let (org-log-done org-log-states) ; turn off logging
  7696. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7697. ")
  7698. (defun org-local-logging (value)
  7699. "Get logging settings from a property VALUE."
  7700. (let* (words w a)
  7701. ;; directly set the variables, they are already local.
  7702. (setq org-log-done nil
  7703. org-log-repeat nil
  7704. org-todo-log-states nil)
  7705. (setq words (org-split-string value))
  7706. (while (setq w (pop words))
  7707. (cond
  7708. ((setq a (assoc w org-startup-options))
  7709. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7710. (set (nth 1 a) (nth 2 a))))
  7711. ((setq a (org-extract-log-state-settings w))
  7712. (and (member (car a) org-todo-keywords-1)
  7713. (push a org-todo-log-states)))))))
  7714. (defun org-get-todo-sequence-head (kwd)
  7715. "Return the head of the TODO sequence to which KWD belongs.
  7716. If KWD is not set, check if there is a text property remembering the
  7717. right sequence."
  7718. (let (p)
  7719. (cond
  7720. ((not kwd)
  7721. (or (get-text-property (point-at-bol) 'org-todo-head)
  7722. (progn
  7723. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7724. nil (point-at-eol)))
  7725. (get-text-property p 'org-todo-head))))
  7726. ((not (member kwd org-todo-keywords-1))
  7727. (car org-todo-keywords-1))
  7728. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7729. (defun org-fast-todo-selection ()
  7730. "Fast TODO keyword selection with single keys.
  7731. Returns the new TODO keyword, or nil if no state change should occur."
  7732. (let* ((fulltable org-todo-key-alist)
  7733. (done-keywords org-done-keywords) ;; needed for the faces.
  7734. (maxlen (apply 'max (mapcar
  7735. (lambda (x)
  7736. (if (stringp (car x)) (string-width (car x)) 0))
  7737. fulltable)))
  7738. (expert nil)
  7739. (fwidth (+ maxlen 3 1 3))
  7740. (ncol (/ (- (window-width) 4) fwidth))
  7741. tg cnt e c tbl
  7742. groups ingroup)
  7743. (save-window-excursion
  7744. (if expert
  7745. (set-buffer (get-buffer-create " *Org todo*"))
  7746. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7747. (erase-buffer)
  7748. (org-set-local 'org-done-keywords done-keywords)
  7749. (setq tbl fulltable cnt 0)
  7750. (while (setq e (pop tbl))
  7751. (cond
  7752. ((equal e '(:startgroup))
  7753. (push '() groups) (setq ingroup t)
  7754. (when (not (= cnt 0))
  7755. (setq cnt 0)
  7756. (insert "\n"))
  7757. (insert "{ "))
  7758. ((equal e '(:endgroup))
  7759. (setq ingroup nil cnt 0)
  7760. (insert "}\n"))
  7761. (t
  7762. (setq tg (car e) c (cdr e))
  7763. (if ingroup (push tg (car groups)))
  7764. (setq tg (org-add-props tg nil 'face
  7765. (org-get-todo-face tg)))
  7766. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7767. (insert "[" c "] " tg (make-string
  7768. (- fwidth 4 (length tg)) ?\ ))
  7769. (when (= (setq cnt (1+ cnt)) ncol)
  7770. (insert "\n")
  7771. (if ingroup (insert " "))
  7772. (setq cnt 0)))))
  7773. (insert "\n")
  7774. (goto-char (point-min))
  7775. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7776. (fit-window-to-buffer))
  7777. (message "[a-z..]:Set [SPC]:clear")
  7778. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7779. (cond
  7780. ((or (= c ?\C-g)
  7781. (and (= c ?q) (not (rassoc c fulltable))))
  7782. (setq quit-flag t))
  7783. ((= c ?\ ) nil)
  7784. ((setq e (rassoc c fulltable) tg (car e))
  7785. tg)
  7786. (t (setq quit-flag t))))))
  7787. (defun org-entry-is-todo-p ()
  7788. (member (org-get-todo-state) org-not-done-keywords))
  7789. (defun org-entry-is-done-p ()
  7790. (member (org-get-todo-state) org-done-keywords))
  7791. (defun org-get-todo-state ()
  7792. (save-excursion
  7793. (org-back-to-heading t)
  7794. (and (looking-at org-todo-line-regexp)
  7795. (match-end 2)
  7796. (match-string 2))))
  7797. (defun org-at-date-range-p (&optional inactive-ok)
  7798. "Is the cursor inside a date range?"
  7799. (interactive)
  7800. (save-excursion
  7801. (catch 'exit
  7802. (let ((pos (point)))
  7803. (skip-chars-backward "^[<\r\n")
  7804. (skip-chars-backward "<[")
  7805. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7806. (>= (match-end 0) pos)
  7807. (throw 'exit t))
  7808. (skip-chars-backward "^<[\r\n")
  7809. (skip-chars-backward "<[")
  7810. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7811. (>= (match-end 0) pos)
  7812. (throw 'exit t)))
  7813. nil)))
  7814. (defun org-get-repeat ()
  7815. "Check if tere is a deadline/schedule with repeater in this entry."
  7816. (save-match-data
  7817. (save-excursion
  7818. (org-back-to-heading t)
  7819. (if (re-search-forward
  7820. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7821. (match-string 1)))))
  7822. (defvar org-last-changed-timestamp)
  7823. (defvar org-last-inserted-timestamp)
  7824. (defvar org-log-post-message)
  7825. (defvar org-log-note-purpose)
  7826. (defvar org-log-note-how)
  7827. (defun org-auto-repeat-maybe (done-word)
  7828. "Check if the current headline contains a repeated deadline/schedule.
  7829. If yes, set TODO state back to what it was and change the base date
  7830. of repeating deadline/scheduled time stamps to new date.
  7831. This function is run automatically after each state change to a DONE state."
  7832. ;; last-state is dynamically scoped into this function
  7833. (let* ((repeat (org-get-repeat))
  7834. (aa (assoc last-state org-todo-kwd-alist))
  7835. (interpret (nth 1 aa))
  7836. (head (nth 2 aa))
  7837. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7838. (msg "Entry repeats: ")
  7839. (org-log-done nil)
  7840. (org-todo-log-states nil)
  7841. (nshiftmax 10) (nshift 0)
  7842. re type n what ts mb0 time)
  7843. (when repeat
  7844. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7845. (org-todo (if (eq interpret 'type) last-state head))
  7846. (when org-log-repeat
  7847. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7848. (memq 'org-add-log-note post-command-hook))
  7849. ;; OK, we are already setup for some record
  7850. (if (eq org-log-repeat 'note)
  7851. ;; make sure we take a note, not only a time stamp
  7852. (setq org-log-note-how 'note))
  7853. ;; Set up for taking a record
  7854. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7855. 'findpos org-log-repeat)))
  7856. (org-back-to-heading t)
  7857. (org-add-planning-info nil nil 'closed)
  7858. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7859. org-deadline-time-regexp "\\)\\|\\("
  7860. org-ts-regexp "\\)"))
  7861. (while (re-search-forward
  7862. re (save-excursion (outline-next-heading) (point)) t)
  7863. (setq type (if (match-end 1) org-scheduled-string
  7864. (if (match-end 3) org-deadline-string "Plain:"))
  7865. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7866. mb0 (match-beginning 0))
  7867. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7868. (setq n (string-to-number (match-string 2 ts))
  7869. what (match-string 3 ts))
  7870. (if (equal what "w") (setq n (* n 7) what "d"))
  7871. ;; Preparation, see if we need to modify the start date for the change
  7872. (when (match-end 1)
  7873. (setq time (save-match-data (org-time-string-to-time ts)))
  7874. (cond
  7875. ((equal (match-string 1 ts) ".")
  7876. ;; Shift starting date to today
  7877. (org-timestamp-change
  7878. (- (time-to-days (current-time)) (time-to-days time))
  7879. 'day))
  7880. ((equal (match-string 1 ts) "+")
  7881. (while (or (= nshift 0)
  7882. (<= (time-to-days time) (time-to-days (current-time))))
  7883. (when (= (incf nshift) nshiftmax)
  7884. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7885. (error "Abort")))
  7886. (org-timestamp-change n (cdr (assoc what whata)))
  7887. (org-at-timestamp-p t)
  7888. (setq ts (match-string 1))
  7889. (setq time (save-match-data (org-time-string-to-time ts))))
  7890. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7891. ;; rematch, so that we have everything in place for the real shift
  7892. (org-at-timestamp-p t)
  7893. (setq ts (match-string 1))
  7894. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7895. (org-timestamp-change n (cdr (assoc what whata)))
  7896. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7897. (setq org-log-post-message msg)
  7898. (message "%s" msg))))
  7899. (defun org-show-todo-tree (arg)
  7900. "Make a compact tree which shows all headlines marked with TODO.
  7901. The tree will show the lines where the regexp matches, and all higher
  7902. headlines above the match.
  7903. With a \\[universal-argument] prefix, also show the DONE entries.
  7904. With a numeric prefix N, construct a sparse tree for the Nth element
  7905. of `org-todo-keywords-1'."
  7906. (interactive "P")
  7907. (let ((case-fold-search nil)
  7908. (kwd-re
  7909. (cond ((null arg) org-not-done-regexp)
  7910. ((equal arg '(4))
  7911. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7912. (mapcar 'list org-todo-keywords-1))))
  7913. (concat "\\("
  7914. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7915. "\\)\\>")))
  7916. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7917. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7918. org-todo-keywords-1)))
  7919. (t (error "Invalid prefix argument: %s" arg)))))
  7920. (message "%d TODO entries found"
  7921. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7922. (defun org-deadline (&optional remove time)
  7923. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7924. With argument REMOVE, remove any deadline from the item.
  7925. When TIME is set, it should be an internal time specification, and the
  7926. scheduling will use the corresponding date."
  7927. (interactive "P")
  7928. (if remove
  7929. (progn
  7930. (org-remove-timestamp-with-keyword org-deadline-string)
  7931. (message "Item no longer has a deadline."))
  7932. (if (org-get-repeat)
  7933. (error "Cannot change deadline on task with repeater, please do that by hand")
  7934. (org-add-planning-info 'deadline time 'closed)
  7935. (message "Deadline on %s" org-last-inserted-timestamp))))
  7936. (defun org-schedule (&optional remove time)
  7937. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7938. With argument REMOVE, remove any scheduling date from the item.
  7939. When TIME is set, it should be an internal time specification, and the
  7940. scheduling will use the corresponding date."
  7941. (interactive "P")
  7942. (if remove
  7943. (progn
  7944. (org-remove-timestamp-with-keyword org-scheduled-string)
  7945. (message "Item is no longer scheduled."))
  7946. (if (org-get-repeat)
  7947. (error "Cannot reschedule task with repeater, please do that by hand")
  7948. (org-add-planning-info 'scheduled time 'closed)
  7949. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7950. (defun org-remove-timestamp-with-keyword (keyword)
  7951. "Remove all time stamps with KEYWORD in the current entry."
  7952. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7953. beg)
  7954. (save-excursion
  7955. (org-back-to-heading t)
  7956. (setq beg (point))
  7957. (org-end-of-subtree t t)
  7958. (while (re-search-backward re beg t)
  7959. (replace-match "")
  7960. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7961. (equal (char-before) ?\ ))
  7962. (backward-delete-char 1)
  7963. (if (string-match "^[ \t]*$" (buffer-substring
  7964. (point-at-bol) (point-at-eol)))
  7965. (delete-region (point-at-bol)
  7966. (min (point-max) (1+ (point-at-eol))))))))))
  7967. (defun org-add-planning-info (what &optional time &rest remove)
  7968. "Insert new timestamp with keyword in the line directly after the headline.
  7969. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7970. If non is given, the user is prompted for a date.
  7971. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7972. be removed."
  7973. (interactive)
  7974. (let (org-time-was-given org-end-time-was-given ts
  7975. end default-time default-input)
  7976. (when (and (not time) (memq what '(scheduled deadline)))
  7977. ;; Try to get a default date/time from existing timestamp
  7978. (save-excursion
  7979. (org-back-to-heading t)
  7980. (setq end (save-excursion (outline-next-heading) (point)))
  7981. (when (re-search-forward (if (eq what 'scheduled)
  7982. org-scheduled-time-regexp
  7983. org-deadline-time-regexp)
  7984. end t)
  7985. (setq ts (match-string 1)
  7986. default-time
  7987. (apply 'encode-time (org-parse-time-string ts))
  7988. default-input (and ts (org-get-compact-tod ts))))))
  7989. (when what
  7990. ;; If necessary, get the time from the user
  7991. (setq time (or time (org-read-date nil 'to-time nil nil
  7992. default-time default-input))))
  7993. (when (and org-insert-labeled-timestamps-at-point
  7994. (member what '(scheduled deadline)))
  7995. (insert
  7996. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7997. (org-insert-time-stamp time org-time-was-given
  7998. nil nil nil (list org-end-time-was-given))
  7999. (setq what nil))
  8000. (save-excursion
  8001. (save-restriction
  8002. (let (col list elt ts buffer-invisibility-spec)
  8003. (org-back-to-heading t)
  8004. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  8005. (goto-char (match-end 1))
  8006. (setq col (current-column))
  8007. (goto-char (match-end 0))
  8008. (if (eobp) (insert "\n") (forward-char 1))
  8009. (if (and (not (looking-at outline-regexp))
  8010. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  8011. "[^\r\n]*"))
  8012. (not (equal (match-string 1) org-clock-string)))
  8013. (narrow-to-region (match-beginning 0) (match-end 0))
  8014. (insert-before-markers "\n")
  8015. (backward-char 1)
  8016. (narrow-to-region (point) (point))
  8017. (and org-adapt-indentation (org-indent-to-column col)))
  8018. ;; Check if we have to remove something.
  8019. (setq list (cons what remove))
  8020. (while list
  8021. (setq elt (pop list))
  8022. (goto-char (point-min))
  8023. (when (or (and (eq elt 'scheduled)
  8024. (re-search-forward org-scheduled-time-regexp nil t))
  8025. (and (eq elt 'deadline)
  8026. (re-search-forward org-deadline-time-regexp nil t))
  8027. (and (eq elt 'closed)
  8028. (re-search-forward org-closed-time-regexp nil t)))
  8029. (replace-match "")
  8030. (if (looking-at "--+<[^>]+>") (replace-match ""))
  8031. (if (looking-at " +") (replace-match ""))))
  8032. (goto-char (point-max))
  8033. (when what
  8034. (insert
  8035. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  8036. (cond ((eq what 'scheduled) org-scheduled-string)
  8037. ((eq what 'deadline) org-deadline-string)
  8038. ((eq what 'closed) org-closed-string))
  8039. " ")
  8040. (setq ts (org-insert-time-stamp
  8041. time
  8042. (or org-time-was-given
  8043. (and (eq what 'closed) org-log-done-with-time))
  8044. (eq what 'closed)
  8045. nil nil (list org-end-time-was-given)))
  8046. (end-of-line 1))
  8047. (goto-char (point-min))
  8048. (widen)
  8049. (if (and (looking-at "[ \t]+\n")
  8050. (equal (char-before) ?\n))
  8051. (delete-region (1- (point)) (point-at-eol)))
  8052. ts)))))
  8053. (defvar org-log-note-marker (make-marker))
  8054. (defvar org-log-note-purpose nil)
  8055. (defvar org-log-note-state nil)
  8056. (defvar org-log-note-how nil)
  8057. (defvar org-log-note-window-configuration nil)
  8058. (defvar org-log-note-return-to (make-marker))
  8059. (defvar org-log-post-message nil
  8060. "Message to be displayed after a log note has been stored.
  8061. The auto-repeater uses this.")
  8062. (defun org-add-note ()
  8063. "Add a note to the current entry.
  8064. This is done in the same way as adding a state change note."
  8065. (interactive)
  8066. (org-add-log-setup 'note nil t nil))
  8067. (defun org-add-log-setup (&optional purpose state findpos how)
  8068. "Set up the post command hook to take a note.
  8069. If this is about to TODO state change, the new state is expected in STATE.
  8070. When FINDPOS is non-nil, find the correct position for the note in
  8071. the current entry. If not, assume that it can be inserted at point."
  8072. (save-excursion
  8073. (when findpos
  8074. (org-back-to-heading t)
  8075. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  8076. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  8077. "[^\r\n]*\\)?"))
  8078. (goto-char (match-end 0))
  8079. (unless org-log-states-order-reversed
  8080. (and (= (char-after) ?\n) (forward-char 1))
  8081. (org-skip-over-state-notes)
  8082. (skip-chars-backward " \t\n\r")))
  8083. (move-marker org-log-note-marker (point))
  8084. (setq org-log-note-purpose purpose
  8085. org-log-note-state state
  8086. org-log-note-how how)
  8087. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  8088. (defun org-skip-over-state-notes ()
  8089. "Skip past the list of State notes in an entry."
  8090. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  8091. (while (looking-at "[ \t]*- State")
  8092. (condition-case nil
  8093. (org-next-item)
  8094. (error (org-end-of-item)))))
  8095. (defun org-add-log-note (&optional purpose)
  8096. "Pop up a window for taking a note, and add this note later at point."
  8097. (remove-hook 'post-command-hook 'org-add-log-note)
  8098. (setq org-log-note-window-configuration (current-window-configuration))
  8099. (delete-other-windows)
  8100. (move-marker org-log-note-return-to (point))
  8101. (switch-to-buffer (marker-buffer org-log-note-marker))
  8102. (goto-char org-log-note-marker)
  8103. (org-switch-to-buffer-other-window "*Org Note*")
  8104. (erase-buffer)
  8105. (if (memq org-log-note-how '(time state))
  8106. (org-store-log-note)
  8107. (let ((org-inhibit-startup t)) (org-mode))
  8108. (insert (format "# Insert note for %s.
  8109. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  8110. (cond
  8111. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  8112. ((eq org-log-note-purpose 'done) "closed todo item")
  8113. ((eq org-log-note-purpose 'state)
  8114. (format "state change to \"%s\"" org-log-note-state))
  8115. ((eq org-log-note-purpose 'note)
  8116. "this entry")
  8117. (t (error "This should not happen")))))
  8118. (org-set-local 'org-finish-function 'org-store-log-note)))
  8119. (defvar org-note-abort nil) ; dynamically scoped
  8120. (defun org-store-log-note ()
  8121. "Finish taking a log note, and insert it to where it belongs."
  8122. (let ((txt (buffer-string))
  8123. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  8124. lines ind)
  8125. (kill-buffer (current-buffer))
  8126. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  8127. (setq txt (replace-match "" t t txt)))
  8128. (if (string-match "\\s-+\\'" txt)
  8129. (setq txt (replace-match "" t t txt)))
  8130. (setq lines (org-split-string txt "\n"))
  8131. (when (and note (string-match "\\S-" note))
  8132. (setq note
  8133. (org-replace-escapes
  8134. note
  8135. (list (cons "%u" (user-login-name))
  8136. (cons "%U" user-full-name)
  8137. (cons "%t" (format-time-string
  8138. (org-time-stamp-format 'long 'inactive)
  8139. (current-time)))
  8140. (cons "%s" (if org-log-note-state
  8141. (concat "\"" org-log-note-state "\"")
  8142. "")))))
  8143. (if lines (setq note (concat note " \\\\")))
  8144. (push note lines))
  8145. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  8146. (when lines
  8147. (save-excursion
  8148. (set-buffer (marker-buffer org-log-note-marker))
  8149. (save-excursion
  8150. (goto-char org-log-note-marker)
  8151. (move-marker org-log-note-marker nil)
  8152. (end-of-line 1)
  8153. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  8154. (indent-relative nil)
  8155. (insert "- " (pop lines))
  8156. (org-indent-line-function)
  8157. (beginning-of-line 1)
  8158. (looking-at "[ \t]*")
  8159. (setq ind (concat (match-string 0) " "))
  8160. (end-of-line 1)
  8161. (while lines (insert "\n" ind (pop lines)))))))
  8162. (set-window-configuration org-log-note-window-configuration)
  8163. (with-current-buffer (marker-buffer org-log-note-return-to)
  8164. (goto-char org-log-note-return-to))
  8165. (move-marker org-log-note-return-to nil)
  8166. (and org-log-post-message (message "%s" org-log-post-message)))
  8167. (defun org-sparse-tree (&optional arg)
  8168. "Create a sparse tree, prompt for the details.
  8169. This command can create sparse trees. You first need to select the type
  8170. of match used to create the tree:
  8171. t Show entries with a specific TODO keyword.
  8172. T Show entries selected by a tags match.
  8173. p Enter a property name and its value (both with completion on existing
  8174. names/values) and show entries with that property.
  8175. r Show entries matching a regular expression
  8176. d Show deadlines due within `org-deadline-warning-days'."
  8177. (interactive "P")
  8178. (let (ans kwd value)
  8179. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  8180. (setq ans (read-char-exclusive))
  8181. (cond
  8182. ((equal ans ?d)
  8183. (call-interactively 'org-check-deadlines))
  8184. ((equal ans ?b)
  8185. (call-interactively 'org-check-before-date))
  8186. ((equal ans ?t)
  8187. (org-show-todo-tree '(4)))
  8188. ((equal ans ?T)
  8189. (call-interactively 'org-tags-sparse-tree))
  8190. ((member ans '(?p ?P))
  8191. (setq kwd (completing-read "Property: "
  8192. (mapcar 'list (org-buffer-property-keys))))
  8193. (setq value (completing-read "Value: "
  8194. (mapcar 'list (org-property-values kwd))))
  8195. (unless (string-match "\\`{.*}\\'" value)
  8196. (setq value (concat "\"" value "\"")))
  8197. (org-tags-sparse-tree arg (concat kwd "=" value)))
  8198. ((member ans '(?r ?R ?/))
  8199. (call-interactively 'org-occur))
  8200. (t (error "No such sparse tree command \"%c\"" ans)))))
  8201. (defvar org-occur-highlights nil
  8202. "List of overlays used for occur matches.")
  8203. (make-variable-buffer-local 'org-occur-highlights)
  8204. (defvar org-occur-parameters nil
  8205. "Parameters of the active org-occur calls.
  8206. This is a list, each call to org-occur pushes as cons cell,
  8207. containing the regular expression and the callback, onto the list.
  8208. The list can contain several entries if `org-occur' has been called
  8209. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  8210. will only contain one set of parameters. When the highlights are
  8211. removed (for example with `C-c C-c', or with the next edit (depending
  8212. on `org-remove-highlights-with-change'), this variable is emptied
  8213. as well.")
  8214. (make-variable-buffer-local 'org-occur-parameters)
  8215. (defun org-occur (regexp &optional keep-previous callback)
  8216. "Make a compact tree which shows all matches of REGEXP.
  8217. The tree will show the lines where the regexp matches, and all higher
  8218. headlines above the match. It will also show the heading after the match,
  8219. to make sure editing the matching entry is easy.
  8220. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  8221. call to `org-occur' will be kept, to allow stacking of calls to this
  8222. command.
  8223. If CALLBACK is non-nil, it is a function which is called to confirm
  8224. that the match should indeed be shown."
  8225. (interactive "sRegexp: \nP")
  8226. (unless keep-previous
  8227. (org-remove-occur-highlights nil nil t))
  8228. (push (cons regexp callback) org-occur-parameters)
  8229. (let ((cnt 0))
  8230. (save-excursion
  8231. (goto-char (point-min))
  8232. (if (or (not keep-previous) ; do not want to keep
  8233. (not org-occur-highlights)) ; no previous matches
  8234. ;; hide everything
  8235. (org-overview))
  8236. (while (re-search-forward regexp nil t)
  8237. (when (or (not callback)
  8238. (save-match-data (funcall callback)))
  8239. (setq cnt (1+ cnt))
  8240. (when org-highlight-sparse-tree-matches
  8241. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8242. (org-show-context 'occur-tree))))
  8243. (when org-remove-highlights-with-change
  8244. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8245. nil 'local))
  8246. (unless org-sparse-tree-open-archived-trees
  8247. (org-hide-archived-subtrees (point-min) (point-max)))
  8248. (run-hooks 'org-occur-hook)
  8249. (if (interactive-p)
  8250. (message "%d match(es) for regexp %s" cnt regexp))
  8251. cnt))
  8252. (defun org-show-context (&optional key)
  8253. "Make sure point and context and visible.
  8254. How much context is shown depends upon the variables
  8255. `org-show-hierarchy-above', `org-show-following-heading'. and
  8256. `org-show-siblings'."
  8257. (let ((heading-p (org-on-heading-p t))
  8258. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8259. (following-p (org-get-alist-option org-show-following-heading key))
  8260. (entry-p (org-get-alist-option org-show-entry-below key))
  8261. (siblings-p (org-get-alist-option org-show-siblings key)))
  8262. (catch 'exit
  8263. ;; Show heading or entry text
  8264. (if (and heading-p (not entry-p))
  8265. (org-flag-heading nil) ; only show the heading
  8266. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8267. (org-show-hidden-entry))) ; show entire entry
  8268. (when following-p
  8269. ;; Show next sibling, or heading below text
  8270. (save-excursion
  8271. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8272. (org-flag-heading nil))))
  8273. (when siblings-p (org-show-siblings))
  8274. (when hierarchy-p
  8275. ;; show all higher headings, possibly with siblings
  8276. (save-excursion
  8277. (while (and (condition-case nil
  8278. (progn (org-up-heading-all 1) t)
  8279. (error nil))
  8280. (not (bobp)))
  8281. (org-flag-heading nil)
  8282. (when siblings-p (org-show-siblings))))))))
  8283. (defun org-reveal (&optional siblings)
  8284. "Show current entry, hierarchy above it, and the following headline.
  8285. This can be used to show a consistent set of context around locations
  8286. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8287. not t for the search context.
  8288. With optional argument SIBLINGS, on each level of the hierarchy all
  8289. siblings are shown. This repairs the tree structure to what it would
  8290. look like when opened with hierarchical calls to `org-cycle'."
  8291. (interactive "P")
  8292. (let ((org-show-hierarchy-above t)
  8293. (org-show-following-heading t)
  8294. (org-show-siblings (if siblings t org-show-siblings)))
  8295. (org-show-context nil)))
  8296. (defun org-highlight-new-match (beg end)
  8297. "Highlight from BEG to END and mark the highlight is an occur headline."
  8298. (let ((ov (org-make-overlay beg end)))
  8299. (org-overlay-put ov 'face 'secondary-selection)
  8300. (push ov org-occur-highlights)))
  8301. (defun org-remove-occur-highlights (&optional beg end noremove)
  8302. "Remove the occur highlights from the buffer.
  8303. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8304. from the `before-change-functions' in the current buffer."
  8305. (interactive)
  8306. (unless org-inhibit-highlight-removal
  8307. (mapc 'org-delete-overlay org-occur-highlights)
  8308. (setq org-occur-highlights nil)
  8309. (setq org-occur-parameters nil)
  8310. (unless noremove
  8311. (remove-hook 'before-change-functions
  8312. 'org-remove-occur-highlights 'local))))
  8313. ;;;; Priorities
  8314. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8315. "Regular expression matching the priority indicator.")
  8316. (defvar org-remove-priority-next-time nil)
  8317. (defun org-priority-up ()
  8318. "Increase the priority of the current item."
  8319. (interactive)
  8320. (org-priority 'up))
  8321. (defun org-priority-down ()
  8322. "Decrease the priority of the current item."
  8323. (interactive)
  8324. (org-priority 'down))
  8325. (defun org-priority (&optional action)
  8326. "Change the priority of an item by ARG.
  8327. ACTION can be `set', `up', `down', or a character."
  8328. (interactive)
  8329. (setq action (or action 'set))
  8330. (let (current new news have remove)
  8331. (save-excursion
  8332. (org-back-to-heading)
  8333. (if (looking-at org-priority-regexp)
  8334. (setq current (string-to-char (match-string 2))
  8335. have t)
  8336. (setq current org-default-priority))
  8337. (cond
  8338. ((or (eq action 'set)
  8339. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8340. (if (not (eq action 'set))
  8341. (setq new action)
  8342. (message "Priority %c-%c, SPC to remove: "
  8343. org-highest-priority org-lowest-priority)
  8344. (setq new (read-char-exclusive)))
  8345. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8346. (= (upcase org-lowest-priority) org-lowest-priority))
  8347. (setq new (upcase new)))
  8348. (cond ((equal new ?\ ) (setq remove t))
  8349. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8350. (error "Priority must be between `%c' and `%c'"
  8351. org-highest-priority org-lowest-priority))))
  8352. ((eq action 'up)
  8353. (if (and (not have) (eq last-command this-command))
  8354. (setq new org-lowest-priority)
  8355. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8356. org-default-priority (1- current)))))
  8357. ((eq action 'down)
  8358. (if (and (not have) (eq last-command this-command))
  8359. (setq new org-highest-priority)
  8360. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8361. org-default-priority (1+ current)))))
  8362. (t (error "Invalid action")))
  8363. (if (or (< (upcase new) org-highest-priority)
  8364. (> (upcase new) org-lowest-priority))
  8365. (setq remove t))
  8366. (setq news (format "%c" new))
  8367. (if have
  8368. (if remove
  8369. (replace-match "" t t nil 1)
  8370. (replace-match news t t nil 2))
  8371. (if remove
  8372. (error "No priority cookie found in line")
  8373. (looking-at org-todo-line-regexp)
  8374. (if (match-end 2)
  8375. (progn
  8376. (goto-char (match-end 2))
  8377. (insert " [#" news "]"))
  8378. (goto-char (match-beginning 3))
  8379. (insert "[#" news "] ")))))
  8380. (org-preserve-lc (org-set-tags nil 'align))
  8381. (if remove
  8382. (message "Priority removed")
  8383. (message "Priority of current item set to %s" news))))
  8384. (defun org-get-priority (s)
  8385. "Find priority cookie and return priority."
  8386. (save-match-data
  8387. (if (not (string-match org-priority-regexp s))
  8388. (* 1000 (- org-lowest-priority org-default-priority))
  8389. (* 1000 (- org-lowest-priority
  8390. (string-to-char (match-string 2 s)))))))
  8391. ;;;; Tags
  8392. (defvar org-agenda-archives-mode)
  8393. (defun org-scan-tags (action matcher &optional todo-only)
  8394. "Scan headline tags with inheritance and produce output ACTION.
  8395. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8396. or `agenda' to produce an entry list for an agenda view. It can also be
  8397. a Lisp form or a function that should be called at each matched headline, in
  8398. this case the return value is a list of all return values from these calls.
  8399. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8400. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8401. only lines with a TODO keyword are included in the output."
  8402. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8403. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8404. (org-re
  8405. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8406. (props (list 'face nil
  8407. 'done-face 'org-done
  8408. 'undone-face nil
  8409. 'mouse-face 'highlight
  8410. 'org-not-done-regexp org-not-done-regexp
  8411. 'org-todo-regexp org-todo-regexp
  8412. 'keymap org-agenda-keymap
  8413. 'help-echo
  8414. (format "mouse-2 or RET jump to org file %s"
  8415. (abbreviate-file-name
  8416. (or (buffer-file-name (buffer-base-buffer))
  8417. (buffer-name (buffer-base-buffer)))))))
  8418. (case-fold-search nil)
  8419. lspos tags tags-list
  8420. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8421. (llast 0) rtn rtn1 level category i txt
  8422. todo marker entry priority)
  8423. (when (not (member action '(agenda sparse-tree)))
  8424. (setq action (list 'lambda nil action)))
  8425. (save-excursion
  8426. (goto-char (point-min))
  8427. (when (eq action 'sparse-tree)
  8428. (org-overview)
  8429. (org-remove-occur-highlights))
  8430. (while (re-search-forward re nil t)
  8431. (catch :skip
  8432. (setq todo (if (match-end 1) (match-string 2))
  8433. tags (if (match-end 4) (match-string 4)))
  8434. (goto-char (setq lspos (1+ (match-beginning 0))))
  8435. (setq level (org-reduced-level (funcall outline-level))
  8436. category (org-get-category))
  8437. (setq i llast llast level)
  8438. ;; remove tag lists from same and sublevels
  8439. (while (>= i level)
  8440. (when (setq entry (assoc i tags-alist))
  8441. (setq tags-alist (delete entry tags-alist)))
  8442. (setq i (1- i)))
  8443. ;; add the next tags
  8444. (when tags
  8445. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8446. tags-alist
  8447. (cons (cons level tags) tags-alist)))
  8448. ;; compile tags for current headline
  8449. (setq tags-list
  8450. (if org-use-tag-inheritance
  8451. (apply 'append (mapcar 'cdr tags-alist))
  8452. tags))
  8453. (when (and tags org-use-tag-inheritance
  8454. (not (eq t org-use-tag-inheritance)))
  8455. ;; selective inheritance, remove uninherited ones
  8456. (setcdr (car tags-alist)
  8457. (org-remove-uniherited-tags (cdar tags-alist))))
  8458. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8459. (eval matcher)
  8460. (or
  8461. (not (member org-archive-tag tags-list))
  8462. ;; we have an archive tag, should we use this anyway?
  8463. (or (not org-agenda-skip-archived-trees)
  8464. (and (eq action 'agenda) org-agenda-archives-mode))))
  8465. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8466. ;; select this headline
  8467. (cond
  8468. ((eq action 'sparse-tree)
  8469. (and org-highlight-sparse-tree-matches
  8470. (org-get-heading) (match-end 0)
  8471. (org-highlight-new-match
  8472. (match-beginning 0) (match-beginning 1)))
  8473. (org-show-context 'tags-tree))
  8474. ((eq action 'agenda)
  8475. (setq txt (org-format-agenda-item
  8476. ""
  8477. (concat
  8478. (if org-tags-match-list-sublevels
  8479. (make-string (1- level) ?.) "")
  8480. (org-get-heading))
  8481. category tags-list)
  8482. priority (org-get-priority txt))
  8483. (goto-char lspos)
  8484. (setq marker (org-agenda-new-marker))
  8485. (org-add-props txt props
  8486. 'org-marker marker 'org-hd-marker marker 'org-category category
  8487. 'priority priority 'type "tagsmatch")
  8488. (push txt rtn))
  8489. ((functionp action)
  8490. (save-excursion
  8491. (setq rtn1 (funcall action))
  8492. (push rtn1 rtn))
  8493. (goto-char (point-at-eol)))
  8494. (t (error "Invalid action")))
  8495. ;; if we are to skip sublevels, jump to end of subtree
  8496. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8497. (when (and (eq action 'sparse-tree)
  8498. (not org-sparse-tree-open-archived-trees))
  8499. (org-hide-archived-subtrees (point-min) (point-max)))
  8500. (nreverse rtn)))
  8501. (defun org-remove-uniherited-tags (tags)
  8502. "Remove all tags that are not inherited from the list TAGS."
  8503. (cond
  8504. ((eq org-use-tag-inheritance t) tags)
  8505. ((not org-use-tag-inheritance) nil)
  8506. ((stringp org-use-tag-inheritance)
  8507. (delq nil (mapcar
  8508. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8509. tags)))
  8510. ((listp org-use-tag-inheritance)
  8511. (org-delete-all org-use-tag-inheritance tags))))
  8512. (defvar todo-only) ;; dynamically scoped
  8513. (defun org-tags-sparse-tree (&optional todo-only match)
  8514. "Create a sparse tree according to tags string MATCH.
  8515. MATCH can contain positive and negative selection of tags, like
  8516. \"+WORK+URGENT-WITHBOSS\".
  8517. If optional argument TODO_ONLY is non-nil, only select lines that are
  8518. also TODO lines."
  8519. (interactive "P")
  8520. (org-prepare-agenda-buffers (list (current-buffer)))
  8521. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8522. (defvar org-cached-props nil)
  8523. (defun org-cached-entry-get (pom property)
  8524. (if (or (eq t org-use-property-inheritance)
  8525. (and (stringp org-use-property-inheritance)
  8526. (string-match org-use-property-inheritance property))
  8527. (and (listp org-use-property-inheritance)
  8528. (member property org-use-property-inheritance)))
  8529. ;; Caching is not possible, check it directly
  8530. (org-entry-get pom property 'inherit)
  8531. ;; Get all properties, so that we can do complicated checks easily
  8532. (cdr (assoc property (or org-cached-props
  8533. (setq org-cached-props
  8534. (org-entry-properties pom)))))))
  8535. (defun org-global-tags-completion-table (&optional files)
  8536. "Return the list of all tags in all agenda buffer/files."
  8537. (save-excursion
  8538. (org-uniquify
  8539. (delq nil
  8540. (apply 'append
  8541. (mapcar
  8542. (lambda (file)
  8543. (set-buffer (find-file-noselect file))
  8544. (append (org-get-buffer-tags)
  8545. (mapcar (lambda (x) (if (stringp (car-safe x))
  8546. (list (car-safe x)) nil))
  8547. org-tag-alist)))
  8548. (if (and files (car files))
  8549. files
  8550. (org-agenda-files))))))))
  8551. (defun org-make-tags-matcher (match)
  8552. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8553. ;; todo-only is scoped dynamically into this function, and the function
  8554. ;; may change it it the matcher asksk for it.
  8555. (unless match
  8556. ;; Get a new match request, with completion
  8557. (let ((org-last-tags-completion-table
  8558. (org-global-tags-completion-table)))
  8559. (setq match (completing-read
  8560. "Match: " 'org-tags-completion-function nil nil nil
  8561. 'org-tags-history))))
  8562. ;; Parse the string and create a lisp form
  8563. (let ((match0 match)
  8564. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8565. minus tag mm
  8566. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8567. orterms term orlist re-p str-p level-p level-op
  8568. prop-p pn pv po cat-p gv)
  8569. (if (string-match "/+" match)
  8570. ;; match contains also a todo-matching request
  8571. (progn
  8572. (setq tagsmatch (substring match 0 (match-beginning 0))
  8573. todomatch (substring match (match-end 0)))
  8574. (if (string-match "^!" todomatch)
  8575. (setq todo-only t todomatch (substring todomatch 1)))
  8576. (if (string-match "^\\s-*$" todomatch)
  8577. (setq todomatch nil)))
  8578. ;; only matching tags
  8579. (setq tagsmatch match todomatch nil))
  8580. ;; Make the tags matcher
  8581. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8582. (setq tagsmatcher t)
  8583. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8584. (while (setq term (pop orterms))
  8585. (while (and (equal (substring term -1) "\\") orterms)
  8586. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8587. (while (string-match re term)
  8588. (setq minus (and (match-end 1)
  8589. (equal (match-string 1 term) "-"))
  8590. tag (match-string 2 term)
  8591. re-p (equal (string-to-char tag) ?{)
  8592. level-p (match-end 4)
  8593. prop-p (match-end 5)
  8594. mm (cond
  8595. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8596. (level-p
  8597. (setq level-op (org-op-to-function (match-string 3 term)))
  8598. `(,level-op level ,(string-to-number
  8599. (match-string 4 term))))
  8600. (prop-p
  8601. (setq pn (match-string 5 term)
  8602. po (match-string 6 term)
  8603. pv (match-string 7 term)
  8604. cat-p (equal pn "CATEGORY")
  8605. re-p (equal (string-to-char pv) ?{)
  8606. str-p (equal (string-to-char pv) ?\")
  8607. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8608. (setq po (org-op-to-function po str-p))
  8609. (if (equal pn "CATEGORY")
  8610. (setq gv '(get-text-property (point) 'org-category))
  8611. (setq gv `(org-cached-entry-get nil ,pn)))
  8612. (if re-p
  8613. (if (eq po 'org<>)
  8614. `(not (string-match ,pv (or ,gv "")))
  8615. `(string-match ,pv (or ,gv "")))
  8616. (if str-p
  8617. `(,po (or ,gv "") ,pv)
  8618. `(,po (string-to-number (or ,gv ""))
  8619. ,(string-to-number pv) ))))
  8620. (t `(member ,(downcase tag) tags-list)))
  8621. mm (if minus (list 'not mm) mm)
  8622. term (substring term (match-end 0)))
  8623. (push mm tagsmatcher))
  8624. (push (if (> (length tagsmatcher) 1)
  8625. (cons 'and tagsmatcher)
  8626. (car tagsmatcher))
  8627. orlist)
  8628. (setq tagsmatcher nil))
  8629. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8630. (setq tagsmatcher
  8631. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8632. ;; Make the todo matcher
  8633. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8634. (setq todomatcher t)
  8635. (setq orterms (org-split-string todomatch "|") orlist nil)
  8636. (while (setq term (pop orterms))
  8637. (while (string-match re term)
  8638. (setq minus (and (match-end 1)
  8639. (equal (match-string 1 term) "-"))
  8640. kwd (match-string 2 term)
  8641. re-p (equal (string-to-char kwd) ?{)
  8642. term (substring term (match-end 0))
  8643. mm (if re-p
  8644. `(string-match ,(substring kwd 1 -1) todo)
  8645. (list 'equal 'todo kwd))
  8646. mm (if minus (list 'not mm) mm))
  8647. (push mm todomatcher))
  8648. (push (if (> (length todomatcher) 1)
  8649. (cons 'and todomatcher)
  8650. (car todomatcher))
  8651. orlist)
  8652. (setq todomatcher nil))
  8653. (setq todomatcher (if (> (length orlist) 1)
  8654. (cons 'or orlist) (car orlist))))
  8655. ;; Return the string and lisp forms of the matcher
  8656. (setq matcher (if todomatcher
  8657. (list 'and tagsmatcher todomatcher)
  8658. tagsmatcher))
  8659. (cons match0 matcher)))
  8660. (defun org-op-to-function (op &optional stringp)
  8661. (setq op
  8662. (cond
  8663. ((equal op "<" ) '(< string< ))
  8664. ((equal op ">" ) '(> org-string> ))
  8665. ((member op '("<=" "=<")) '(<= org-string<= ))
  8666. ((member op '(">=" "=>")) '(>= org-string>= ))
  8667. ((member op '("=" "==")) '(= string= ))
  8668. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8669. (nth (if stringp 1 0) op))
  8670. (defun org<> (a b) (not (= a b)))
  8671. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8672. (defun org-string>= (a b) (not (string< a b)))
  8673. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8674. (defun org-string<> (a b) (not (string= a b)))
  8675. (defun org-match-any-p (re list)
  8676. "Does re match any element of list?"
  8677. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8678. (delq nil list))
  8679. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8680. (defvar org-tags-overlay (org-make-overlay 1 1))
  8681. (org-detach-overlay org-tags-overlay)
  8682. (defun org-get-tags-at (&optional pos)
  8683. "Get a list of all headline tags applicable at POS.
  8684. POS defaults to point. If tags are inherited, the list contains
  8685. the targets in the same sequence as the headlines appear, i.e.
  8686. the tags of the current headline come last."
  8687. (interactive)
  8688. (let (tags ltags lastpos parent)
  8689. (save-excursion
  8690. (save-restriction
  8691. (widen)
  8692. (goto-char (or pos (point)))
  8693. (save-match-data
  8694. (condition-case nil
  8695. (progn
  8696. (org-back-to-heading t)
  8697. (while (not (equal lastpos (point)))
  8698. (setq lastpos (point))
  8699. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8700. (setq ltags (org-split-string
  8701. (org-match-string-no-properties 1) ":"))
  8702. (setq tags (append (org-remove-uniherited-tags ltags)
  8703. tags)))
  8704. (or org-use-tag-inheritance (error ""))
  8705. (org-up-heading-all 1)
  8706. (setq parent t)))
  8707. (error nil))))
  8708. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8709. (defun org-toggle-tag (tag &optional onoff)
  8710. "Toggle the tag TAG for the current line.
  8711. If ONOFF is `on' or `off', don't toggle but set to this state."
  8712. (unless (org-on-heading-p t) (error "Not on headling"))
  8713. (let (res current)
  8714. (save-excursion
  8715. (beginning-of-line)
  8716. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8717. (point-at-eol) t)
  8718. (progn
  8719. (setq current (match-string 1))
  8720. (replace-match ""))
  8721. (setq current ""))
  8722. (setq current (nreverse (org-split-string current ":")))
  8723. (cond
  8724. ((eq onoff 'on)
  8725. (setq res t)
  8726. (or (member tag current) (push tag current)))
  8727. ((eq onoff 'off)
  8728. (or (not (member tag current)) (setq current (delete tag current))))
  8729. (t (if (member tag current)
  8730. (setq current (delete tag current))
  8731. (setq res t)
  8732. (push tag current))))
  8733. (end-of-line 1)
  8734. (if current
  8735. (progn
  8736. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8737. (org-set-tags nil t))
  8738. (delete-horizontal-space))
  8739. (run-hooks 'org-after-tags-change-hook))
  8740. res))
  8741. (defun org-align-tags-here (to-col)
  8742. ;; Assumes that this is a headline
  8743. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8744. (beginning-of-line 1)
  8745. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8746. (< pos (match-beginning 2)))
  8747. (progn
  8748. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8749. (goto-char (match-beginning 1))
  8750. (insert " ")
  8751. (delete-region (point) (1+ (match-beginning 2)))
  8752. (setq ncol (max (1+ (current-column))
  8753. (1+ col)
  8754. (if (> to-col 0)
  8755. to-col
  8756. (- (abs to-col) tags-l))))
  8757. (setq p (point))
  8758. (insert (make-string (- ncol (current-column)) ?\ ))
  8759. (setq ncol (current-column))
  8760. (when indent-tabs-mode (tabify p (point-at-eol)))
  8761. (org-move-to-column (min ncol col) t))
  8762. (goto-char pos))))
  8763. (defun org-set-tags (&optional arg just-align)
  8764. "Set the tags for the current headline.
  8765. With prefix ARG, realign all tags in headings in the current buffer."
  8766. (interactive "P")
  8767. (let* ((re (concat "^" outline-regexp))
  8768. (current (org-get-tags-string))
  8769. (col (current-column))
  8770. (org-setting-tags t)
  8771. table current-tags inherited-tags ; computed below when needed
  8772. tags p0 c0 c1 rpl)
  8773. (if arg
  8774. (save-excursion
  8775. (goto-char (point-min))
  8776. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8777. (while (re-search-forward re nil t)
  8778. (org-set-tags nil t)
  8779. (end-of-line 1)))
  8780. (message "All tags realigned to column %d" org-tags-column))
  8781. (if just-align
  8782. (setq tags current)
  8783. ;; Get a new set of tags from the user
  8784. (save-excursion
  8785. (setq table (or org-tag-alist (org-get-buffer-tags))
  8786. org-last-tags-completion-table table
  8787. current-tags (org-split-string current ":")
  8788. inherited-tags (nreverse
  8789. (nthcdr (length current-tags)
  8790. (nreverse (org-get-tags-at))))
  8791. tags
  8792. (if (or (eq t org-use-fast-tag-selection)
  8793. (and org-use-fast-tag-selection
  8794. (delq nil (mapcar 'cdr table))))
  8795. (org-fast-tag-selection
  8796. current-tags inherited-tags table
  8797. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8798. (let ((org-add-colon-after-tag-completion t))
  8799. (org-trim
  8800. (org-without-partial-completion
  8801. (completing-read "Tags: " 'org-tags-completion-function
  8802. nil nil current 'org-tags-history)))))))
  8803. (while (string-match "[-+&]+" tags)
  8804. ;; No boolean logic, just a list
  8805. (setq tags (replace-match ":" t t tags))))
  8806. (if (string-match "\\`[\t ]*\\'" tags)
  8807. (setq tags "")
  8808. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8809. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8810. ;; Insert new tags at the correct column
  8811. (beginning-of-line 1)
  8812. (cond
  8813. ((and (equal current "") (equal tags "")))
  8814. ((re-search-forward
  8815. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8816. (point-at-eol) t)
  8817. (if (equal tags "")
  8818. (setq rpl "")
  8819. (goto-char (match-beginning 0))
  8820. (setq c0 (current-column) p0 (point)
  8821. c1 (max (1+ c0) (if (> org-tags-column 0)
  8822. org-tags-column
  8823. (- (- org-tags-column) (length tags))))
  8824. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8825. (replace-match rpl t t)
  8826. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8827. tags)
  8828. (t (error "Tags alignment failed")))
  8829. (org-move-to-column col)
  8830. (unless just-align
  8831. (run-hooks 'org-after-tags-change-hook)))))
  8832. (defun org-change-tag-in-region (beg end tag off)
  8833. "Add or remove TAG for each entry in the region.
  8834. This works in the agenda, and also in an org-mode buffer."
  8835. (interactive
  8836. (list (region-beginning) (region-end)
  8837. (let ((org-last-tags-completion-table
  8838. (if (org-mode-p)
  8839. (org-get-buffer-tags)
  8840. (org-global-tags-completion-table))))
  8841. (completing-read
  8842. "Tag: " 'org-tags-completion-function nil nil nil
  8843. 'org-tags-history))
  8844. (progn
  8845. (message "[s]et or [r]emove? ")
  8846. (equal (read-char-exclusive) ?r))))
  8847. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8848. (let ((agendap (equal major-mode 'org-agenda-mode))
  8849. l1 l2 m buf pos newhead (cnt 0))
  8850. (goto-char end)
  8851. (setq l2 (1- (org-current-line)))
  8852. (goto-char beg)
  8853. (setq l1 (org-current-line))
  8854. (loop for l from l1 to l2 do
  8855. (goto-line l)
  8856. (setq m (get-text-property (point) 'org-hd-marker))
  8857. (when (or (and (org-mode-p) (org-on-heading-p))
  8858. (and agendap m))
  8859. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8860. pos (if agendap m (point)))
  8861. (with-current-buffer buf
  8862. (save-excursion
  8863. (save-restriction
  8864. (goto-char pos)
  8865. (setq cnt (1+ cnt))
  8866. (org-toggle-tag tag (if off 'off 'on))
  8867. (setq newhead (org-get-heading)))))
  8868. (and agendap (org-agenda-change-all-lines newhead m))))
  8869. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8870. (defun org-tags-completion-function (string predicate &optional flag)
  8871. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8872. (confirm (lambda (x) (stringp (car x)))))
  8873. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8874. (setq s1 (match-string 1 string)
  8875. s2 (match-string 2 string))
  8876. (setq s1 "" s2 string))
  8877. (cond
  8878. ((eq flag nil)
  8879. ;; try completion
  8880. (setq rtn (try-completion s2 ctable confirm))
  8881. (if (stringp rtn)
  8882. (setq rtn
  8883. (concat s1 s2 (substring rtn (length s2))
  8884. (if (and org-add-colon-after-tag-completion
  8885. (assoc rtn ctable))
  8886. ":" ""))))
  8887. rtn)
  8888. ((eq flag t)
  8889. ;; all-completions
  8890. (all-completions s2 ctable confirm)
  8891. )
  8892. ((eq flag 'lambda)
  8893. ;; exact match?
  8894. (assoc s2 ctable)))
  8895. ))
  8896. (defun org-fast-tag-insert (kwd tags face &optional end)
  8897. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8898. (insert (format "%-12s" (concat kwd ":"))
  8899. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8900. (or end "")))
  8901. (defun org-fast-tag-show-exit (flag)
  8902. (save-excursion
  8903. (goto-line 3)
  8904. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8905. (replace-match ""))
  8906. (when flag
  8907. (end-of-line 1)
  8908. (org-move-to-column (- (window-width) 19) t)
  8909. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8910. (defun org-set-current-tags-overlay (current prefix)
  8911. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8912. (if (featurep 'xemacs)
  8913. (org-overlay-display org-tags-overlay (concat prefix s)
  8914. 'secondary-selection)
  8915. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8916. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8917. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8918. "Fast tag selection with single keys.
  8919. CURRENT is the current list of tags in the headline, INHERITED is the
  8920. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8921. possibly with grouping information. TODO-TABLE is a similar table with
  8922. TODO keywords, should these have keys assigned to them.
  8923. If the keys are nil, a-z are automatically assigned.
  8924. Returns the new tags string, or nil to not change the current settings."
  8925. (let* ((fulltable (append table todo-table))
  8926. (maxlen (apply 'max (mapcar
  8927. (lambda (x)
  8928. (if (stringp (car x)) (string-width (car x)) 0))
  8929. fulltable)))
  8930. (buf (current-buffer))
  8931. (expert (eq org-fast-tag-selection-single-key 'expert))
  8932. (buffer-tags nil)
  8933. (fwidth (+ maxlen 3 1 3))
  8934. (ncol (/ (- (window-width) 4) fwidth))
  8935. (i-face 'org-done)
  8936. (c-face 'org-todo)
  8937. tg cnt e c char c1 c2 ntable tbl rtn
  8938. ov-start ov-end ov-prefix
  8939. (exit-after-next org-fast-tag-selection-single-key)
  8940. (done-keywords org-done-keywords)
  8941. groups ingroup)
  8942. (save-excursion
  8943. (beginning-of-line 1)
  8944. (if (looking-at
  8945. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8946. (setq ov-start (match-beginning 1)
  8947. ov-end (match-end 1)
  8948. ov-prefix "")
  8949. (setq ov-start (1- (point-at-eol))
  8950. ov-end (1+ ov-start))
  8951. (skip-chars-forward "^\n\r")
  8952. (setq ov-prefix
  8953. (concat
  8954. (buffer-substring (1- (point)) (point))
  8955. (if (> (current-column) org-tags-column)
  8956. " "
  8957. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8958. (org-move-overlay org-tags-overlay ov-start ov-end)
  8959. (save-window-excursion
  8960. (if expert
  8961. (set-buffer (get-buffer-create " *Org tags*"))
  8962. (delete-other-windows)
  8963. (split-window-vertically)
  8964. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8965. (erase-buffer)
  8966. (org-set-local 'org-done-keywords done-keywords)
  8967. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8968. (org-fast-tag-insert "Current" current c-face "\n\n")
  8969. (org-fast-tag-show-exit exit-after-next)
  8970. (org-set-current-tags-overlay current ov-prefix)
  8971. (setq tbl fulltable char ?a cnt 0)
  8972. (while (setq e (pop tbl))
  8973. (cond
  8974. ((equal e '(:startgroup))
  8975. (push '() groups) (setq ingroup t)
  8976. (when (not (= cnt 0))
  8977. (setq cnt 0)
  8978. (insert "\n"))
  8979. (insert "{ "))
  8980. ((equal e '(:endgroup))
  8981. (setq ingroup nil cnt 0)
  8982. (insert "}\n"))
  8983. (t
  8984. (setq tg (car e) c2 nil)
  8985. (if (cdr e)
  8986. (setq c (cdr e))
  8987. ;; automatically assign a character.
  8988. (setq c1 (string-to-char
  8989. (downcase (substring
  8990. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8991. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8992. (while (or (rassoc char ntable) (rassoc char table))
  8993. (setq char (1+ char)))
  8994. (setq c2 c1))
  8995. (setq c (or c2 char)))
  8996. (if ingroup (push tg (car groups)))
  8997. (setq tg (org-add-props tg nil 'face
  8998. (cond
  8999. ((not (assoc tg table))
  9000. (org-get-todo-face tg))
  9001. ((member tg current) c-face)
  9002. ((member tg inherited) i-face)
  9003. (t nil))))
  9004. (if (and (= cnt 0) (not ingroup)) (insert " "))
  9005. (insert "[" c "] " tg (make-string
  9006. (- fwidth 4 (length tg)) ?\ ))
  9007. (push (cons tg c) ntable)
  9008. (when (= (setq cnt (1+ cnt)) ncol)
  9009. (insert "\n")
  9010. (if ingroup (insert " "))
  9011. (setq cnt 0)))))
  9012. (setq ntable (nreverse ntable))
  9013. (insert "\n")
  9014. (goto-char (point-min))
  9015. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  9016. (fit-window-to-buffer))
  9017. (setq rtn
  9018. (catch 'exit
  9019. (while t
  9020. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  9021. (if groups " [!] no groups" " [!]groups")
  9022. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  9023. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  9024. (cond
  9025. ((= c ?\r) (throw 'exit t))
  9026. ((= c ?!)
  9027. (setq groups (not groups))
  9028. (goto-char (point-min))
  9029. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  9030. ((= c ?\C-c)
  9031. (if (not expert)
  9032. (org-fast-tag-show-exit
  9033. (setq exit-after-next (not exit-after-next)))
  9034. (setq expert nil)
  9035. (delete-other-windows)
  9036. (split-window-vertically)
  9037. (org-switch-to-buffer-other-window " *Org tags*")
  9038. (and (fboundp 'fit-window-to-buffer)
  9039. (fit-window-to-buffer))))
  9040. ((or (= c ?\C-g)
  9041. (and (= c ?q) (not (rassoc c ntable))))
  9042. (org-detach-overlay org-tags-overlay)
  9043. (setq quit-flag t))
  9044. ((= c ?\ )
  9045. (setq current nil)
  9046. (if exit-after-next (setq exit-after-next 'now)))
  9047. ((= c ?\t)
  9048. (condition-case nil
  9049. (setq tg (completing-read
  9050. "Tag: "
  9051. (or buffer-tags
  9052. (with-current-buffer buf
  9053. (org-get-buffer-tags)))))
  9054. (quit (setq tg "")))
  9055. (when (string-match "\\S-" tg)
  9056. (add-to-list 'buffer-tags (list tg))
  9057. (if (member tg current)
  9058. (setq current (delete tg current))
  9059. (push tg current)))
  9060. (if exit-after-next (setq exit-after-next 'now)))
  9061. ((setq e (rassoc c todo-table) tg (car e))
  9062. (with-current-buffer buf
  9063. (save-excursion (org-todo tg)))
  9064. (if exit-after-next (setq exit-after-next 'now)))
  9065. ((setq e (rassoc c ntable) tg (car e))
  9066. (if (member tg current)
  9067. (setq current (delete tg current))
  9068. (loop for g in groups do
  9069. (if (member tg g)
  9070. (mapc (lambda (x)
  9071. (setq current (delete x current)))
  9072. g)))
  9073. (push tg current))
  9074. (if exit-after-next (setq exit-after-next 'now))))
  9075. ;; Create a sorted list
  9076. (setq current
  9077. (sort current
  9078. (lambda (a b)
  9079. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  9080. (if (eq exit-after-next 'now) (throw 'exit t))
  9081. (goto-char (point-min))
  9082. (beginning-of-line 2)
  9083. (delete-region (point) (point-at-eol))
  9084. (org-fast-tag-insert "Current" current c-face)
  9085. (org-set-current-tags-overlay current ov-prefix)
  9086. (while (re-search-forward
  9087. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  9088. (setq tg (match-string 1))
  9089. (add-text-properties
  9090. (match-beginning 1) (match-end 1)
  9091. (list 'face
  9092. (cond
  9093. ((member tg current) c-face)
  9094. ((member tg inherited) i-face)
  9095. (t (get-text-property (match-beginning 1) 'face))))))
  9096. (goto-char (point-min)))))
  9097. (org-detach-overlay org-tags-overlay)
  9098. (if rtn
  9099. (mapconcat 'identity current ":")
  9100. nil))))
  9101. (defun org-get-tags-string ()
  9102. "Get the TAGS string in the current headline."
  9103. (unless (org-on-heading-p t)
  9104. (error "Not on a heading"))
  9105. (save-excursion
  9106. (beginning-of-line 1)
  9107. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  9108. (org-match-string-no-properties 1)
  9109. "")))
  9110. (defun org-get-tags ()
  9111. "Get the list of tags specified in the current headline."
  9112. (org-split-string (org-get-tags-string) ":"))
  9113. (defun org-get-buffer-tags ()
  9114. "Get a table of all tags used in the buffer, for completion."
  9115. (let (tags)
  9116. (save-excursion
  9117. (goto-char (point-min))
  9118. (while (re-search-forward
  9119. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  9120. (when (equal (char-after (point-at-bol 0)) ?*)
  9121. (mapc (lambda (x) (add-to-list 'tags x))
  9122. (org-split-string (org-match-string-no-properties 1) ":")))))
  9123. (mapcar 'list tags)))
  9124. ;;;; The mapping API
  9125. ;;;###autoload
  9126. (defun org-map-entries (func &optional match scope &rest skip)
  9127. "Call FUNC at each headline selected by MATCH in SCOPE.
  9128. FUNC is a function or a lisp form. The function will be called without
  9129. arguments, with the cursor positioned at the beginning of the headline.
  9130. The return values of all calls to the function will be collected and
  9131. returned as a list.
  9132. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  9133. Only headlines that are matched by this query will be considered during
  9134. the iteration. When MATCH is nil or t, all headlines will be
  9135. visited by the iteration.
  9136. SCOPE determines the scope of this command. It can be any of:
  9137. nil The current buffer, respecting the restriction if any
  9138. tree The subtree started with the entry at point
  9139. file The current buffer, without restriction
  9140. file-with-archives
  9141. The current buffer, and any archives associated with it
  9142. agenda All agenda files
  9143. agenda-with-archives
  9144. All agenda files with any archive files associated with them
  9145. \(file1 file2 ...)
  9146. If this is a list, all files in the list will be scanned
  9147. The remaining args are treated as settings for the skipping facilities of
  9148. the scanner. The following items can be given here:
  9149. archive skip trees with the archive tag.
  9150. comment skip trees with the COMMENT keyword
  9151. function or Emacs Lisp form:
  9152. will be used as value for `org-agenda-skip-function', so whenever
  9153. the the function returns t, FUNC will not be called for that
  9154. entry and search will continue from the point where the
  9155. function leaves it."
  9156. (let* ((org-agenda-archives-mode nil) ; just to make sure
  9157. (org-agenda-skip-archived-trees (memq 'archive skip))
  9158. (org-agenda-skip-comment-trees (memq 'comment skip))
  9159. (org-agenda-skip-function
  9160. (car (org-delete-all '(comment archive) skip)))
  9161. (org-tags-match-list-sublevels t)
  9162. matcher pos file)
  9163. (cond
  9164. ((eq match t) (setq matcher t))
  9165. ((eq match nil) (setq matcher t))
  9166. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  9167. (when (eq scope 'tree)
  9168. (org-back-to-heading t)
  9169. (org-narrow-to-subtree)
  9170. (setq scope nil))
  9171. (if (not scope)
  9172. (progn
  9173. (org-prepare-agenda-buffers
  9174. (list (buffer-file-name (current-buffer))))
  9175. (org-scan-tags func matcher))
  9176. ;; Get the right scope
  9177. (setq pos (point))
  9178. (cond
  9179. ((and scope (listp scope) (symbolp (car scope)))
  9180. (setq scope (eval scope)))
  9181. ((eq scope 'agenda)
  9182. (setq scope (org-agenda-files t)))
  9183. ((eq scope 'agenda-with-archives)
  9184. (setq scope (org-agenda-files t))
  9185. (setq scope (org-add-archive-files scope)))
  9186. ((eq scope 'file)
  9187. (setq scope (list (buffer-file-name))))
  9188. ((eq scope 'file-with-archives)
  9189. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9190. (org-prepare-agenda-buffers scope)
  9191. (while (setq file (pop scope))
  9192. (with-current-buffer (org-find-base-buffer-visiting file)
  9193. (save-excursion
  9194. (save-restriction
  9195. (widen)
  9196. (goto-char (point-min))
  9197. (org-scan-tags func matcher))))))))
  9198. ;;;; Properties
  9199. ;;; Setting and retrieving properties
  9200. (defconst org-special-properties
  9201. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  9202. "TIMESTAMP" "TIMESTAMP_IA")
  9203. "The special properties valid in Org-mode.
  9204. These are properties that are not defined in the property drawer,
  9205. but in some other way.")
  9206. (defconst org-default-properties
  9207. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9208. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9209. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9210. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9211. "Some properties that are used by Org-mode for various purposes.
  9212. Being in this list makes sure that they are offered for completion.")
  9213. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9214. "Regular expression matching the first line of a property drawer.")
  9215. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9216. "Regular expression matching the first line of a property drawer.")
  9217. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9218. "Regular expression matching the first line of a property drawer.")
  9219. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9220. "Regular expression matching the first line of a property drawer.")
  9221. (defconst org-property-drawer-re
  9222. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9223. org-property-end-re "\\)\n?")
  9224. "Matches an entire property drawer.")
  9225. (defconst org-clock-drawer-re
  9226. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9227. org-property-end-re "\\)\n?")
  9228. "Matches an entire clock drawer.")
  9229. (defun org-property-action ()
  9230. "Do an action on properties."
  9231. (interactive)
  9232. (let (c)
  9233. (org-at-property-p)
  9234. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9235. (setq c (read-char-exclusive))
  9236. (cond
  9237. ((equal c ?s)
  9238. (call-interactively 'org-set-property))
  9239. ((equal c ?d)
  9240. (call-interactively 'org-delete-property))
  9241. ((equal c ?D)
  9242. (call-interactively 'org-delete-property-globally))
  9243. ((equal c ?c)
  9244. (call-interactively 'org-compute-property-at-point))
  9245. (t (error "No such property action %c" c)))))
  9246. (defun org-at-property-p ()
  9247. "Is the cursor in a property line?"
  9248. ;; FIXME: Does not check if we are actually in the drawer.
  9249. ;; FIXME: also returns true on any drawers.....
  9250. ;; This is used by C-c C-c for property action.
  9251. (save-excursion
  9252. (beginning-of-line 1)
  9253. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9254. (defun org-get-property-block (&optional beg end force)
  9255. "Return the (beg . end) range of the body of the property drawer.
  9256. BEG and END can be beginning and end of subtree, if not given
  9257. they will be found.
  9258. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9259. (catch 'exit
  9260. (save-excursion
  9261. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9262. (end (or end (progn (outline-next-heading) (point)))))
  9263. (goto-char beg)
  9264. (if (re-search-forward org-property-start-re end t)
  9265. (setq beg (1+ (match-end 0)))
  9266. (if force
  9267. (save-excursion
  9268. (org-insert-property-drawer)
  9269. (setq end (progn (outline-next-heading) (point))))
  9270. (throw 'exit nil))
  9271. (goto-char beg)
  9272. (if (re-search-forward org-property-start-re end t)
  9273. (setq beg (1+ (match-end 0)))))
  9274. (if (re-search-forward org-property-end-re end t)
  9275. (setq end (match-beginning 0))
  9276. (or force (throw 'exit nil))
  9277. (goto-char beg)
  9278. (setq end beg)
  9279. (org-indent-line-function)
  9280. (insert ":END:\n"))
  9281. (cons beg end)))))
  9282. (defun org-entry-properties (&optional pom which)
  9283. "Get all properties of the entry at point-or-marker POM.
  9284. This includes the TODO keyword, the tags, time strings for deadline,
  9285. scheduled, and clocking, and any additional properties defined in the
  9286. entry. The return value is an alist, keys may occur multiple times
  9287. if the property key was used several times.
  9288. POM may also be nil, in which case the current entry is used.
  9289. If WHICH is nil or `all', get all properties. If WHICH is
  9290. `special' or `standard', only get that subclass."
  9291. (setq which (or which 'all))
  9292. (org-with-point-at pom
  9293. (let ((clockstr (substring org-clock-string 0 -1))
  9294. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9295. beg end range props sum-props key value string clocksum)
  9296. (save-excursion
  9297. (when (condition-case nil (org-back-to-heading t) (error nil))
  9298. (setq beg (point))
  9299. (setq sum-props (get-text-property (point) 'org-summaries))
  9300. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9301. (outline-next-heading)
  9302. (setq end (point))
  9303. (when (memq which '(all special))
  9304. ;; Get the special properties, like TODO and tags
  9305. (goto-char beg)
  9306. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9307. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9308. (when (looking-at org-priority-regexp)
  9309. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9310. (when (and (setq value (org-get-tags-string))
  9311. (string-match "\\S-" value))
  9312. (push (cons "TAGS" value) props))
  9313. (when (setq value (org-get-tags-at))
  9314. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9315. props))
  9316. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9317. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9318. string (if (equal key clockstr)
  9319. (org-no-properties
  9320. (org-trim
  9321. (buffer-substring
  9322. (match-beginning 3) (goto-char (point-at-eol)))))
  9323. (substring (org-match-string-no-properties 3) 1 -1)))
  9324. (unless key
  9325. (if (= (char-after (match-beginning 3)) ?\[)
  9326. (setq key "TIMESTAMP_IA")
  9327. (setq key "TIMESTAMP")))
  9328. (when (or (equal key clockstr) (not (assoc key props)))
  9329. (push (cons key string) props)))
  9330. )
  9331. (when (memq which '(all standard))
  9332. ;; Get the standard properties, like :PORP: ...
  9333. (setq range (org-get-property-block beg end))
  9334. (when range
  9335. (goto-char (car range))
  9336. (while (re-search-forward
  9337. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9338. (cdr range) t)
  9339. (setq key (org-match-string-no-properties 1)
  9340. value (org-trim (or (org-match-string-no-properties 2) "")))
  9341. (unless (member key excluded)
  9342. (push (cons key (or value "")) props)))))
  9343. (if clocksum
  9344. (push (cons "CLOCKSUM"
  9345. (org-columns-number-to-string (/ (float clocksum) 60.)
  9346. 'add_times))
  9347. props))
  9348. (append sum-props (nreverse props)))))))
  9349. (defun org-entry-get (pom property &optional inherit)
  9350. "Get value of PROPERTY for entry at point-or-marker POM.
  9351. If INHERIT is non-nil and the entry does not have the property,
  9352. then also check higher levels of the hierarchy.
  9353. If INHERIT is the symbol `selective', use inheritance only if the setting
  9354. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9355. If the property is present but empty, the return value is the empty string.
  9356. If the property is not present at all, nil is returned."
  9357. (org-with-point-at pom
  9358. (if (and inherit (if (eq inherit 'selective)
  9359. (org-property-inherit-p property)
  9360. t))
  9361. (org-entry-get-with-inheritance property)
  9362. (if (member property org-special-properties)
  9363. ;; We need a special property. Use brute force, get all properties.
  9364. (cdr (assoc property (org-entry-properties nil 'special)))
  9365. (let ((range (org-get-property-block)))
  9366. (if (and range
  9367. (goto-char (car range))
  9368. (re-search-forward
  9369. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  9370. (cdr range) t))
  9371. ;; Found the property, return it.
  9372. (if (match-end 1)
  9373. (org-match-string-no-properties 1)
  9374. "")))))))
  9375. (defun org-property-or-variable-value (var &optional inherit)
  9376. "Check if there is a property fixing the value of VAR.
  9377. If yes, return this value. If not, return the current value of the variable."
  9378. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9379. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9380. (read prop)
  9381. (symbol-value var))))
  9382. (defun org-entry-delete (pom property)
  9383. "Delete the property PROPERTY from entry at point-or-marker POM."
  9384. (org-with-point-at pom
  9385. (if (member property org-special-properties)
  9386. nil ; cannot delete these properties.
  9387. (let ((range (org-get-property-block)))
  9388. (if (and range
  9389. (goto-char (car range))
  9390. (re-search-forward
  9391. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  9392. (cdr range) t))
  9393. (progn
  9394. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9395. t)
  9396. nil)))))
  9397. ;; Multi-values properties are properties that contain multiple values
  9398. ;; These values are assumed to be single words, separated by whitespace.
  9399. (defun org-entry-add-to-multivalued-property (pom property value)
  9400. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9401. (let* ((old (org-entry-get pom property))
  9402. (values (and old (org-split-string old "[ \t]"))))
  9403. (unless (member value values)
  9404. (setq values (cons value values))
  9405. (org-entry-put pom property
  9406. (mapconcat 'identity values " ")))))
  9407. (defun org-entry-remove-from-multivalued-property (pom property value)
  9408. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9409. (let* ((old (org-entry-get pom property))
  9410. (values (and old (org-split-string old "[ \t]"))))
  9411. (when (member value values)
  9412. (setq values (delete value values))
  9413. (org-entry-put pom property
  9414. (mapconcat 'identity values " ")))))
  9415. (defun org-entry-member-in-multivalued-property (pom property value)
  9416. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9417. (let* ((old (org-entry-get pom property))
  9418. (values (and old (org-split-string old "[ \t]"))))
  9419. (member value values)))
  9420. (defvar org-entry-property-inherited-from (make-marker))
  9421. (defun org-entry-get-with-inheritance (property)
  9422. "Get entry property, and search higher levels if not present."
  9423. (let (tmp)
  9424. (save-excursion
  9425. (save-restriction
  9426. (widen)
  9427. (catch 'ex
  9428. (while t
  9429. (when (setq tmp (org-entry-get nil property))
  9430. (org-back-to-heading t)
  9431. (move-marker org-entry-property-inherited-from (point))
  9432. (throw 'ex tmp))
  9433. (or (org-up-heading-safe) (throw 'ex nil)))))
  9434. (or tmp
  9435. (cdr (assoc property org-file-properties))
  9436. (cdr (assoc property org-global-properties))
  9437. (cdr (assoc property org-global-properties-fixed))))))
  9438. (defun org-entry-put (pom property value)
  9439. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9440. (org-with-point-at pom
  9441. (org-back-to-heading t)
  9442. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9443. range)
  9444. (cond
  9445. ((equal property "TODO")
  9446. (when (and (stringp value) (string-match "\\S-" value)
  9447. (not (member value org-todo-keywords-1)))
  9448. (error "\"%s\" is not a valid TODO state" value))
  9449. (if (or (not value)
  9450. (not (string-match "\\S-" value)))
  9451. (setq value 'none))
  9452. (org-todo value)
  9453. (org-set-tags nil 'align))
  9454. ((equal property "PRIORITY")
  9455. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9456. (string-to-char value) ?\ ))
  9457. (org-set-tags nil 'align))
  9458. ((equal property "SCHEDULED")
  9459. (if (re-search-forward org-scheduled-time-regexp end t)
  9460. (cond
  9461. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9462. ((eq value 'later) (org-timestamp-change 1 'day))
  9463. (t (call-interactively 'org-schedule)))
  9464. (call-interactively 'org-schedule)))
  9465. ((equal property "DEADLINE")
  9466. (if (re-search-forward org-deadline-time-regexp end t)
  9467. (cond
  9468. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9469. ((eq value 'later) (org-timestamp-change 1 'day))
  9470. (t (call-interactively 'org-deadline)))
  9471. (call-interactively 'org-deadline)))
  9472. ((member property org-special-properties)
  9473. (error "The %s property can not yet be set with `org-entry-put'"
  9474. property))
  9475. (t ; a non-special property
  9476. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9477. (setq range (org-get-property-block beg end 'force))
  9478. (goto-char (car range))
  9479. (if (re-search-forward
  9480. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9481. (progn
  9482. (delete-region (match-beginning 1) (match-end 1))
  9483. (goto-char (match-beginning 1)))
  9484. (goto-char (cdr range))
  9485. (insert "\n")
  9486. (backward-char 1)
  9487. (org-indent-line-function)
  9488. (insert ":" property ":"))
  9489. (and value (insert " " value))
  9490. (org-indent-line-function)))))))
  9491. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9492. "Get all property keys in the current buffer.
  9493. With INCLUDE-SPECIALS, also list the special properties that relect things
  9494. like tags and TODO state.
  9495. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9496. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9497. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9498. formats in the current buffer."
  9499. (let (rtn range cfmt cols s p)
  9500. (save-excursion
  9501. (save-restriction
  9502. (widen)
  9503. (goto-char (point-min))
  9504. (while (re-search-forward org-property-start-re nil t)
  9505. (setq range (org-get-property-block))
  9506. (goto-char (car range))
  9507. (while (re-search-forward
  9508. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9509. (cdr range) t)
  9510. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9511. (outline-next-heading))))
  9512. (when include-specials
  9513. (setq rtn (append org-special-properties rtn)))
  9514. (when include-defaults
  9515. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9516. (when include-columns
  9517. (save-excursion
  9518. (save-restriction
  9519. (widen)
  9520. (goto-char (point-min))
  9521. (while (re-search-forward
  9522. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9523. nil t)
  9524. (setq cfmt (match-string 2) s 0)
  9525. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9526. cfmt s)
  9527. (setq s (match-end 0)
  9528. p (match-string 1 cfmt))
  9529. (unless (or (equal p "ITEM")
  9530. (member p org-special-properties))
  9531. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9532. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9533. (defun org-property-values (key)
  9534. "Return a list of all values of property KEY."
  9535. (save-excursion
  9536. (save-restriction
  9537. (widen)
  9538. (goto-char (point-min))
  9539. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9540. values)
  9541. (while (re-search-forward re nil t)
  9542. (add-to-list 'values (org-trim (match-string 1))))
  9543. (delete "" values)))))
  9544. (defun org-insert-property-drawer ()
  9545. "Insert a property drawer into the current entry."
  9546. (interactive)
  9547. (org-back-to-heading t)
  9548. (looking-at outline-regexp)
  9549. (let ((indent (- (match-end 0)(match-beginning 0)))
  9550. (beg (point))
  9551. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9552. end hiddenp)
  9553. (outline-next-heading)
  9554. (setq end (point))
  9555. (goto-char beg)
  9556. (while (re-search-forward re end t))
  9557. (setq hiddenp (org-invisible-p))
  9558. (end-of-line 1)
  9559. (and (equal (char-after) ?\n) (forward-char 1))
  9560. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9561. (beginning-of-line 2))
  9562. (org-skip-over-state-notes)
  9563. (skip-chars-backward " \t\n\r")
  9564. (if (eq (char-before) ?*) (forward-char 1))
  9565. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9566. (beginning-of-line 0)
  9567. (org-indent-to-column indent)
  9568. (beginning-of-line 2)
  9569. (org-indent-to-column indent)
  9570. (beginning-of-line 0)
  9571. (if hiddenp
  9572. (save-excursion
  9573. (org-back-to-heading t)
  9574. (hide-entry))
  9575. (org-flag-drawer t))))
  9576. (defun org-set-property (property value)
  9577. "In the current entry, set PROPERTY to VALUE.
  9578. When called interactively, this will prompt for a property name, offering
  9579. completion on existing and default properties. And then it will prompt
  9580. for a value, offering competion either on allowed values (via an inherited
  9581. xxx_ALL property) or on existing values in other instances of this property
  9582. in the current file."
  9583. (interactive
  9584. (let* ((completion-ignore-case t)
  9585. (keys (org-buffer-property-keys nil t t))
  9586. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9587. (prop (if (member prop0 keys)
  9588. prop0
  9589. (or (cdr (assoc (downcase prop0)
  9590. (mapcar (lambda (x) (cons (downcase x) x))
  9591. keys)))
  9592. prop0)))
  9593. (cur (org-entry-get nil prop))
  9594. (allowed (org-property-get-allowed-values nil prop 'table))
  9595. (existing (mapcar 'list (org-property-values prop)))
  9596. (val (if allowed
  9597. (org-completing-read "Value: " allowed nil 'req-match)
  9598. (org-completing-read
  9599. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9600. (concat "[" cur "]") "")
  9601. ": ")
  9602. existing nil nil "" nil cur))))
  9603. (list prop (if (equal val "") cur val))))
  9604. (unless (equal (org-entry-get nil property) value)
  9605. (org-entry-put nil property value)))
  9606. (defun org-delete-property (property)
  9607. "In the current entry, delete PROPERTY."
  9608. (interactive
  9609. (let* ((completion-ignore-case t)
  9610. (prop (completing-read
  9611. "Property: " (org-entry-properties nil 'standard))))
  9612. (list prop)))
  9613. (message "Property %s %s" property
  9614. (if (org-entry-delete nil property)
  9615. "deleted"
  9616. "was not present in the entry")))
  9617. (defun org-delete-property-globally (property)
  9618. "Remove PROPERTY globally, from all entries."
  9619. (interactive
  9620. (let* ((completion-ignore-case t)
  9621. (prop (completing-read
  9622. "Globally remove property: "
  9623. (mapcar 'list (org-buffer-property-keys)))))
  9624. (list prop)))
  9625. (save-excursion
  9626. (save-restriction
  9627. (widen)
  9628. (goto-char (point-min))
  9629. (let ((cnt 0))
  9630. (while (re-search-forward
  9631. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9632. nil t)
  9633. (setq cnt (1+ cnt))
  9634. (replace-match ""))
  9635. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9636. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9637. (defun org-compute-property-at-point ()
  9638. "Compute the property at point.
  9639. This looks for an enclosing column format, extracts the operator and
  9640. then applies it to the proerty in the column format's scope."
  9641. (interactive)
  9642. (unless (org-at-property-p)
  9643. (error "Not at a property"))
  9644. (let ((prop (org-match-string-no-properties 2)))
  9645. (org-columns-get-format-and-top-level)
  9646. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9647. (error "No operator defined for property %s" prop))
  9648. (org-columns-compute prop)))
  9649. (defun org-property-get-allowed-values (pom property &optional table)
  9650. "Get allowed values for the property PROPERTY.
  9651. When TABLE is non-nil, return an alist that can directly be used for
  9652. completion."
  9653. (let (vals)
  9654. (cond
  9655. ((equal property "TODO")
  9656. (setq vals (org-with-point-at pom
  9657. (append org-todo-keywords-1 '("")))))
  9658. ((equal property "PRIORITY")
  9659. (let ((n org-lowest-priority))
  9660. (while (>= n org-highest-priority)
  9661. (push (char-to-string n) vals)
  9662. (setq n (1- n)))))
  9663. ((member property org-special-properties))
  9664. (t
  9665. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9666. (when (and vals (string-match "\\S-" vals))
  9667. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9668. (setq vals (mapcar (lambda (x)
  9669. (cond ((stringp x) x)
  9670. ((numberp x) (number-to-string x))
  9671. ((symbolp x) (symbol-name x))
  9672. (t "???")))
  9673. vals)))))
  9674. (if table (mapcar 'list vals) vals)))
  9675. (defun org-property-previous-allowed-value (&optional previous)
  9676. "Switch to the next allowed value for this property."
  9677. (interactive)
  9678. (org-property-next-allowed-value t))
  9679. (defun org-property-next-allowed-value (&optional previous)
  9680. "Switch to the next allowed value for this property."
  9681. (interactive)
  9682. (unless (org-at-property-p)
  9683. (error "Not at a property"))
  9684. (let* ((key (match-string 2))
  9685. (value (match-string 3))
  9686. (allowed (or (org-property-get-allowed-values (point) key)
  9687. (and (member value '("[ ]" "[-]" "[X]"))
  9688. '("[ ]" "[X]"))))
  9689. nval)
  9690. (unless allowed
  9691. (error "Allowed values for this property have not been defined"))
  9692. (if previous (setq allowed (reverse allowed)))
  9693. (if (member value allowed)
  9694. (setq nval (car (cdr (member value allowed)))))
  9695. (setq nval (or nval (car allowed)))
  9696. (if (equal nval value)
  9697. (error "Only one allowed value for this property"))
  9698. (org-at-property-p)
  9699. (replace-match (concat " :" key ": " nval) t t)
  9700. (org-indent-line-function)
  9701. (beginning-of-line 1)
  9702. (skip-chars-forward " \t")))
  9703. (defun org-find-entry-with-id (ident)
  9704. "Locate the entry that contains the ID property with exact value IDENT.
  9705. IDENT can be a string, a symbol or a number, this function will search for
  9706. the string representation of it.
  9707. Return the position where this entry starts, or nil if there is no such entry."
  9708. (let ((id (cond
  9709. ((stringp ident) ident)
  9710. ((symbol-name ident) (symbol-name ident))
  9711. ((numberp ident) (number-to-string ident))
  9712. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9713. (case-fold-search nil))
  9714. (save-excursion
  9715. (save-restriction
  9716. (widen)
  9717. (goto-char (point-min))
  9718. (when (re-search-forward
  9719. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9720. nil t)
  9721. (org-back-to-heading)
  9722. (point))))))
  9723. ;;;; Timestamps
  9724. (defvar org-last-changed-timestamp nil)
  9725. (defvar org-last-inserted-timestamp nil
  9726. "The last time stamp inserted with `org-insert-time-stamp'.")
  9727. (defvar org-time-was-given) ; dynamically scoped parameter
  9728. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9729. (defvar org-ts-what) ; dynamically scoped parameter
  9730. (defun org-time-stamp (arg)
  9731. "Prompt for a date/time and insert a time stamp.
  9732. If the user specifies a time like HH:MM, or if this command is called
  9733. with a prefix argument, the time stamp will contain date and time.
  9734. Otherwise, only the date will be included. All parts of a date not
  9735. specified by the user will be filled in from the current date/time.
  9736. So if you press just return without typing anything, the time stamp
  9737. will represent the current date/time. If there is already a timestamp
  9738. at the cursor, it will be modified."
  9739. (interactive "P")
  9740. (let* ((ts nil)
  9741. (default-time
  9742. ;; Default time is either today, or, when entering a range,
  9743. ;; the range start.
  9744. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9745. (save-excursion
  9746. (re-search-backward
  9747. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9748. (- (point) 20) t)))
  9749. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9750. (current-time)))
  9751. (default-input (and ts (org-get-compact-tod ts)))
  9752. org-time-was-given org-end-time-was-given time)
  9753. (cond
  9754. ((and (org-at-timestamp-p)
  9755. (eq last-command 'org-time-stamp)
  9756. (eq this-command 'org-time-stamp))
  9757. (insert "--")
  9758. (setq time (let ((this-command this-command))
  9759. (org-read-date arg 'totime nil nil default-time default-input)))
  9760. (org-insert-time-stamp time (or org-time-was-given arg)))
  9761. ((org-at-timestamp-p)
  9762. (setq time (let ((this-command this-command))
  9763. (org-read-date arg 'totime nil nil default-time default-input)))
  9764. (when (org-at-timestamp-p) ; just to get the match data
  9765. (replace-match "")
  9766. (setq org-last-changed-timestamp
  9767. (org-insert-time-stamp
  9768. time (or org-time-was-given arg)
  9769. nil nil nil (list org-end-time-was-given))))
  9770. (message "Timestamp updated"))
  9771. (t
  9772. (setq time (let ((this-command this-command))
  9773. (org-read-date arg 'totime nil nil default-time default-input)))
  9774. (org-insert-time-stamp time (or org-time-was-given arg)
  9775. nil nil nil (list org-end-time-was-given))))))
  9776. ;; FIXME: can we use this for something else, like computing time differences?
  9777. (defun org-get-compact-tod (s)
  9778. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9779. (let* ((t1 (match-string 1 s))
  9780. (h1 (string-to-number (match-string 2 s)))
  9781. (m1 (string-to-number (match-string 3 s)))
  9782. (t2 (and (match-end 4) (match-string 5 s)))
  9783. (h2 (and t2 (string-to-number (match-string 6 s))))
  9784. (m2 (and t2 (string-to-number (match-string 7 s))))
  9785. dh dm)
  9786. (if (not t2)
  9787. t1
  9788. (setq dh (- h2 h1) dm (- m2 m1))
  9789. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9790. (concat t1 "+" (number-to-string dh)
  9791. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9792. (defun org-time-stamp-inactive (&optional arg)
  9793. "Insert an inactive time stamp.
  9794. An inactive time stamp is enclosed in square brackets instead of angle
  9795. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9796. does not link to the calendar and cannot be changed with the S-cursor keys.
  9797. So these are more for recording a certain time/date."
  9798. (interactive "P")
  9799. (let (org-time-was-given org-end-time-was-given time)
  9800. (setq time (org-read-date arg 'totime))
  9801. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9802. nil nil (list org-end-time-was-given))))
  9803. (defvar org-date-ovl (org-make-overlay 1 1))
  9804. (org-overlay-put org-date-ovl 'face 'org-warning)
  9805. (org-detach-overlay org-date-ovl)
  9806. (defvar org-ans1) ; dynamically scoped parameter
  9807. (defvar org-ans2) ; dynamically scoped parameter
  9808. (defvar org-plain-time-of-day-regexp) ; defined below
  9809. (defvar org-overriding-default-time nil) ; dynamically scoped
  9810. (defvar org-read-date-overlay nil)
  9811. (defvar org-dcst nil) ; dynamically scoped
  9812. (defun org-read-date (&optional with-time to-time from-string prompt
  9813. default-time default-input)
  9814. "Read a date, possibly a time, and make things smooth for the user.
  9815. The prompt will suggest to enter an ISO date, but you can also enter anything
  9816. which will at least partially be understood by `parse-time-string'.
  9817. Unrecognized parts of the date will default to the current day, month, year,
  9818. hour and minute. If this command is called to replace a timestamp at point,
  9819. of to enter the second timestamp of a range, the default time is taken from the
  9820. existing stamp. For example,
  9821. 3-2-5 --> 2003-02-05
  9822. feb 15 --> currentyear-02-15
  9823. sep 12 9 --> 2009-09-12
  9824. 12:45 --> today 12:45
  9825. 22 sept 0:34 --> currentyear-09-22 0:34
  9826. 12 --> currentyear-currentmonth-12
  9827. Fri --> nearest Friday (today or later)
  9828. etc.
  9829. Furthermore you can specify a relative date by giving, as the *first* thing
  9830. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9831. change in days weeks, months, years.
  9832. With a single plus or minus, the date is relative to today. With a double
  9833. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9834. +4d --> four days from today
  9835. +4 --> same as above
  9836. +2w --> two weeks from today
  9837. ++5 --> five days from default date
  9838. The function understands only English month and weekday abbreviations,
  9839. but this can be configured with the variables `parse-time-months' and
  9840. `parse-time-weekdays'.
  9841. While prompting, a calendar is popped up - you can also select the
  9842. date with the mouse (button 1). The calendar shows a period of three
  9843. months. To scroll it to other months, use the keys `>' and `<'.
  9844. If you don't like the calendar, turn it off with
  9845. \(setq org-read-date-popup-calendar nil)
  9846. With optional argument TO-TIME, the date will immediately be converted
  9847. to an internal time.
  9848. With an optional argument WITH-TIME, the prompt will suggest to also
  9849. insert a time. Note that when WITH-TIME is not set, you can still
  9850. enter a time, and this function will inform the calling routine about
  9851. this change. The calling routine may then choose to change the format
  9852. used to insert the time stamp into the buffer to include the time.
  9853. With optional argument FROM-STRING, read from this string instead from
  9854. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9855. the time/date that is used for everything that is not specified by the
  9856. user."
  9857. (require 'parse-time)
  9858. (let* ((org-time-stamp-rounding-minutes
  9859. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9860. (org-dcst org-display-custom-times)
  9861. (ct (org-current-time))
  9862. (def (or org-overriding-default-time default-time ct))
  9863. (defdecode (decode-time def))
  9864. (dummy (progn
  9865. (when (< (nth 2 defdecode) org-extend-today-until)
  9866. (setcar (nthcdr 2 defdecode) -1)
  9867. (setcar (nthcdr 1 defdecode) 59)
  9868. (setq def (apply 'encode-time defdecode)
  9869. defdecode (decode-time def)))))
  9870. (calendar-move-hook nil)
  9871. (calendar-view-diary-initially-flag nil)
  9872. (view-diary-entries-initially nil)
  9873. (calendar-view-holidays-initially-flag nil)
  9874. (view-calendar-holidays-initially nil)
  9875. (timestr (format-time-string
  9876. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9877. (prompt (concat (if prompt (concat prompt " ") "")
  9878. (format "Date+time [%s]: " timestr)))
  9879. ans (org-ans0 "") org-ans1 org-ans2 final)
  9880. (cond
  9881. (from-string (setq ans from-string))
  9882. (org-read-date-popup-calendar
  9883. (save-excursion
  9884. (save-window-excursion
  9885. (calendar)
  9886. (calendar-forward-day (- (time-to-days def)
  9887. (calendar-absolute-from-gregorian
  9888. (calendar-current-date))))
  9889. (org-eval-in-calendar nil t)
  9890. (let* ((old-map (current-local-map))
  9891. (map (copy-keymap calendar-mode-map))
  9892. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9893. (org-defkey map (kbd "RET") 'org-calendar-select)
  9894. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9895. 'org-calendar-select-mouse)
  9896. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9897. 'org-calendar-select-mouse)
  9898. (org-defkey minibuffer-local-map [(meta shift left)]
  9899. (lambda () (interactive)
  9900. (org-eval-in-calendar '(calendar-backward-month 1))))
  9901. (org-defkey minibuffer-local-map [(meta shift right)]
  9902. (lambda () (interactive)
  9903. (org-eval-in-calendar '(calendar-forward-month 1))))
  9904. (org-defkey minibuffer-local-map [(meta shift up)]
  9905. (lambda () (interactive)
  9906. (org-eval-in-calendar '(calendar-backward-year 1))))
  9907. (org-defkey minibuffer-local-map [(meta shift down)]
  9908. (lambda () (interactive)
  9909. (org-eval-in-calendar '(calendar-forward-year 1))))
  9910. (org-defkey minibuffer-local-map [(shift up)]
  9911. (lambda () (interactive)
  9912. (org-eval-in-calendar '(calendar-backward-week 1))))
  9913. (org-defkey minibuffer-local-map [(shift down)]
  9914. (lambda () (interactive)
  9915. (org-eval-in-calendar '(calendar-forward-week 1))))
  9916. (org-defkey minibuffer-local-map [(shift left)]
  9917. (lambda () (interactive)
  9918. (org-eval-in-calendar '(calendar-backward-day 1))))
  9919. (org-defkey minibuffer-local-map [(shift right)]
  9920. (lambda () (interactive)
  9921. (org-eval-in-calendar '(calendar-forward-day 1))))
  9922. (org-defkey minibuffer-local-map ">"
  9923. (lambda () (interactive)
  9924. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9925. (org-defkey minibuffer-local-map "<"
  9926. (lambda () (interactive)
  9927. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9928. (unwind-protect
  9929. (progn
  9930. (use-local-map map)
  9931. (add-hook 'post-command-hook 'org-read-date-display)
  9932. (setq org-ans0 (read-string prompt default-input nil nil))
  9933. ;; org-ans0: from prompt
  9934. ;; org-ans1: from mouse click
  9935. ;; org-ans2: from calendar motion
  9936. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9937. (remove-hook 'post-command-hook 'org-read-date-display)
  9938. (use-local-map old-map)
  9939. (when org-read-date-overlay
  9940. (org-delete-overlay org-read-date-overlay)
  9941. (setq org-read-date-overlay nil)))))))
  9942. (t ; Naked prompt only
  9943. (unwind-protect
  9944. (setq ans (read-string prompt default-input nil timestr))
  9945. (when org-read-date-overlay
  9946. (org-delete-overlay org-read-date-overlay)
  9947. (setq org-read-date-overlay nil)))))
  9948. (setq final (org-read-date-analyze ans def defdecode))
  9949. (if to-time
  9950. (apply 'encode-time final)
  9951. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9952. (format "%04d-%02d-%02d %02d:%02d"
  9953. (nth 5 final) (nth 4 final) (nth 3 final)
  9954. (nth 2 final) (nth 1 final))
  9955. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9956. (defvar def)
  9957. (defvar defdecode)
  9958. (defvar with-time)
  9959. (defun org-read-date-display ()
  9960. "Display the currrent date prompt interpretation in the minibuffer."
  9961. (when org-read-date-display-live
  9962. (when org-read-date-overlay
  9963. (org-delete-overlay org-read-date-overlay))
  9964. (let ((p (point)))
  9965. (end-of-line 1)
  9966. (while (not (equal (buffer-substring
  9967. (max (point-min) (- (point) 4)) (point))
  9968. " "))
  9969. (insert " "))
  9970. (goto-char p))
  9971. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9972. " " (or org-ans1 org-ans2)))
  9973. (org-end-time-was-given nil)
  9974. (f (org-read-date-analyze ans def defdecode))
  9975. (fmts (if org-dcst
  9976. org-time-stamp-custom-formats
  9977. org-time-stamp-formats))
  9978. (fmt (if (or with-time
  9979. (and (boundp 'org-time-was-given) org-time-was-given))
  9980. (cdr fmts)
  9981. (car fmts)))
  9982. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9983. (when (and org-end-time-was-given
  9984. (string-match org-plain-time-of-day-regexp txt))
  9985. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9986. org-end-time-was-given
  9987. (substring txt (match-end 0)))))
  9988. (setq org-read-date-overlay
  9989. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9990. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9991. (defun org-read-date-analyze (ans def defdecode)
  9992. "Analyze the combined answer of the date prompt."
  9993. ;; FIXME: cleanup and comment
  9994. (let (delta deltan deltaw deltadef year month day
  9995. hour minute second wday pm h2 m2 tl wday1
  9996. iso-year iso-weekday iso-week iso-year iso-date)
  9997. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9998. (setq ans "+0"))
  9999. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  10000. (setq ans (replace-match "" t t ans)
  10001. deltan (car delta)
  10002. deltaw (nth 1 delta)
  10003. deltadef (nth 2 delta)))
  10004. ;; Check if there is an iso week date in there
  10005. ;; If yes, sore the info and ostpone interpreting it until the rest
  10006. ;; of the parsing is done
  10007. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  10008. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  10009. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  10010. iso-week (string-to-number (match-string 2 ans)))
  10011. (setq ans (replace-match "" t t ans)))
  10012. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  10013. (when (string-match
  10014. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  10015. (setq year (if (match-end 2)
  10016. (string-to-number (match-string 2 ans))
  10017. (string-to-number (format-time-string "%Y")))
  10018. month (string-to-number (match-string 3 ans))
  10019. day (string-to-number (match-string 4 ans)))
  10020. (if (< year 100) (setq year (+ 2000 year)))
  10021. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  10022. t nil ans)))
  10023. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  10024. ;; If there is a time with am/pm, and *no* time without it, we convert
  10025. ;; so that matching will be successful.
  10026. (loop for i from 1 to 2 do ; twice, for end time as well
  10027. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  10028. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  10029. (setq hour (string-to-number (match-string 1 ans))
  10030. minute (if (match-end 3)
  10031. (string-to-number (match-string 3 ans))
  10032. 0)
  10033. pm (equal ?p
  10034. (string-to-char (downcase (match-string 4 ans)))))
  10035. (if (and (= hour 12) (not pm))
  10036. (setq hour 0)
  10037. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  10038. (setq ans (replace-match (format "%02d:%02d" hour minute)
  10039. t t ans))))
  10040. ;; Check if a time range is given as a duration
  10041. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  10042. (setq hour (string-to-number (match-string 1 ans))
  10043. h2 (+ hour (string-to-number (match-string 3 ans)))
  10044. minute (string-to-number (match-string 2 ans))
  10045. m2 (+ minute (if (match-end 5) (string-to-number
  10046. (match-string 5 ans))0)))
  10047. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  10048. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  10049. t t ans)))
  10050. ;; Check if there is a time range
  10051. (when (boundp 'org-end-time-was-given)
  10052. (setq org-time-was-given nil)
  10053. (when (and (string-match org-plain-time-of-day-regexp ans)
  10054. (match-end 8))
  10055. (setq org-end-time-was-given (match-string 8 ans))
  10056. (setq ans (concat (substring ans 0 (match-beginning 7))
  10057. (substring ans (match-end 7))))))
  10058. (setq tl (parse-time-string ans)
  10059. day (or (nth 3 tl) (nth 3 defdecode))
  10060. month (or (nth 4 tl)
  10061. (if (and org-read-date-prefer-future
  10062. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  10063. (1+ (nth 4 defdecode))
  10064. (nth 4 defdecode)))
  10065. year (or (nth 5 tl)
  10066. (if (and org-read-date-prefer-future
  10067. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  10068. (1+ (nth 5 defdecode))
  10069. (nth 5 defdecode)))
  10070. hour (or (nth 2 tl) (nth 2 defdecode))
  10071. minute (or (nth 1 tl) (nth 1 defdecode))
  10072. second (or (nth 0 tl) 0)
  10073. wday (nth 6 tl))
  10074. ;; Special date definitions below
  10075. (cond
  10076. (iso-week
  10077. ;; There was an iso week
  10078. (setq year (or iso-year year)
  10079. day (or iso-weekday wday 1)
  10080. wday nil ; to make sure that the trigger below does not match
  10081. iso-date (calendar-gregorian-from-absolute
  10082. (calendar-absolute-from-iso
  10083. (list iso-week day year))))
  10084. ; FIXME: Should we also push ISO weeks into the future?
  10085. ; (when (and org-read-date-prefer-future
  10086. ; (not iso-year)
  10087. ; (< (calendar-absolute-from-gregorian iso-date)
  10088. ; (time-to-days (current-time))))
  10089. ; (setq year (1+ year)
  10090. ; iso-date (calendar-gregorian-from-absolute
  10091. ; (calendar-absolute-from-iso
  10092. ; (list iso-week day year)))))
  10093. (setq month (car iso-date)
  10094. year (nth 2 iso-date)
  10095. day (nth 1 iso-date)))
  10096. (deltan
  10097. (unless deltadef
  10098. (let ((now (decode-time (current-time))))
  10099. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  10100. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  10101. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  10102. ((equal deltaw "m") (setq month (+ month deltan)))
  10103. ((equal deltaw "y") (setq year (+ year deltan)))))
  10104. ((and wday (not (nth 3 tl)))
  10105. ;; Weekday was given, but no day, so pick that day in the week
  10106. ;; on or after the derived date.
  10107. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  10108. (unless (equal wday wday1)
  10109. (setq day (+ day (% (- wday wday1 -7) 7))))))
  10110. (if (and (boundp 'org-time-was-given)
  10111. (nth 2 tl))
  10112. (setq org-time-was-given t))
  10113. (if (< year 100) (setq year (+ 2000 year)))
  10114. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10115. (list second minute hour day month year)))
  10116. (defvar parse-time-weekdays)
  10117. (defun org-read-date-get-relative (s today default)
  10118. "Check string S for special relative date string.
  10119. TODAY and DEFAULT are internal times, for today and for a default.
  10120. Return shift list (N what def-flag)
  10121. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10122. N is the number of WHATs to shift.
  10123. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10124. the DEFAULT date rather than TODAY."
  10125. (when (and
  10126. (string-match
  10127. (concat
  10128. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10129. "\\([0-9]+\\)?"
  10130. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10131. "\\([ \t]\\|$\\)") s)
  10132. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10133. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10134. (string-to-char (substring (match-string 1 s) -1))
  10135. ?+))
  10136. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10137. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10138. (what (if (match-end 3) (match-string 3 s) "d"))
  10139. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10140. (date (if rel default today))
  10141. (wday (nth 6 (decode-time date)))
  10142. delta)
  10143. (if wday1
  10144. (progn
  10145. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10146. (if (= dir ?-) (setq delta (- delta 7)))
  10147. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10148. (list delta "d" rel))
  10149. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10150. (defun org-eval-in-calendar (form &optional keepdate)
  10151. "Eval FORM in the calendar window and return to current window.
  10152. Also, store the cursor date in variable org-ans2."
  10153. (let ((sw (selected-window)))
  10154. (select-window (get-buffer-window "*Calendar*"))
  10155. (eval form)
  10156. (when (and (not keepdate) (calendar-cursor-to-date))
  10157. (let* ((date (calendar-cursor-to-date))
  10158. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10159. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10160. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10161. (select-window sw)))
  10162. ; ;; Update the prompt to show new default date
  10163. ; (save-excursion
  10164. ; (goto-char (point-min))
  10165. ; (when (and org-ans2
  10166. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  10167. ; (get-text-property (match-end 0) 'field))
  10168. ; (let ((inhibit-read-only t))
  10169. ; (replace-match (concat "[" org-ans2 "]") t t)
  10170. ; (add-text-properties (point-min) (1+ (match-end 0))
  10171. ; (text-properties-at (1+ (point-min)))))))))
  10172. (defun org-calendar-select ()
  10173. "Return to `org-read-date' with the date currently selected.
  10174. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10175. (interactive)
  10176. (when (calendar-cursor-to-date)
  10177. (let* ((date (calendar-cursor-to-date))
  10178. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10179. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10180. (if (active-minibuffer-window) (exit-minibuffer))))
  10181. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10182. "Insert a date stamp for the date given by the internal TIME.
  10183. WITH-HM means, use the stamp format that includes the time of the day.
  10184. INACTIVE means use square brackets instead of angular ones, so that the
  10185. stamp will not contribute to the agenda.
  10186. PRE and POST are optional strings to be inserted before and after the
  10187. stamp.
  10188. The command returns the inserted time stamp."
  10189. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10190. stamp)
  10191. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10192. (insert-before-markers (or pre ""))
  10193. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10194. (when (listp extra)
  10195. (setq extra (car extra))
  10196. (if (and (stringp extra)
  10197. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10198. (setq extra (format "-%02d:%02d"
  10199. (string-to-number (match-string 1 extra))
  10200. (string-to-number (match-string 2 extra))))
  10201. (setq extra nil)))
  10202. (when extra
  10203. (backward-char 1)
  10204. (insert-before-markers extra)
  10205. (forward-char 1))
  10206. (insert-before-markers (or post ""))
  10207. (setq org-last-inserted-timestamp stamp)))
  10208. (defun org-toggle-time-stamp-overlays ()
  10209. "Toggle the use of custom time stamp formats."
  10210. (interactive)
  10211. (setq org-display-custom-times (not org-display-custom-times))
  10212. (unless org-display-custom-times
  10213. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10214. (while (setq p (next-single-property-change p 'display))
  10215. (if (and (get-text-property p 'display)
  10216. (eq (get-text-property p 'face) 'org-date))
  10217. (remove-text-properties
  10218. p (setq p (next-single-property-change p 'display))
  10219. '(display t))))
  10220. (set-buffer-modified-p bmp)))
  10221. (if (featurep 'xemacs)
  10222. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10223. (org-restart-font-lock)
  10224. (setq org-table-may-need-update t)
  10225. (if org-display-custom-times
  10226. (message "Time stamps are overlayed with custom format")
  10227. (message "Time stamp overlays removed")))
  10228. (defun org-display-custom-time (beg end)
  10229. "Overlay modified time stamp format over timestamp between BEG and END."
  10230. (let* ((ts (buffer-substring beg end))
  10231. t1 w1 with-hm tf time str w2 (off 0))
  10232. (save-match-data
  10233. (setq t1 (org-parse-time-string ts t))
  10234. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10235. (setq off (- (match-end 0) (match-beginning 0)))))
  10236. (setq end (- end off))
  10237. (setq w1 (- end beg)
  10238. with-hm (and (nth 1 t1) (nth 2 t1))
  10239. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10240. time (org-fix-decoded-time t1)
  10241. str (org-add-props
  10242. (format-time-string
  10243. (substring tf 1 -1) (apply 'encode-time time))
  10244. nil 'mouse-face 'highlight)
  10245. w2 (length str))
  10246. (if (not (= w2 w1))
  10247. (add-text-properties (1+ beg) (+ 2 beg)
  10248. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10249. (if (featurep 'xemacs)
  10250. (progn
  10251. (put-text-property beg end 'invisible t)
  10252. (put-text-property beg end 'end-glyph (make-glyph str)))
  10253. (put-text-property beg end 'display str))))
  10254. (defun org-translate-time (string)
  10255. "Translate all timestamps in STRING to custom format.
  10256. But do this only if the variable `org-display-custom-times' is set."
  10257. (when org-display-custom-times
  10258. (save-match-data
  10259. (let* ((start 0)
  10260. (re org-ts-regexp-both)
  10261. t1 with-hm inactive tf time str beg end)
  10262. (while (setq start (string-match re string start))
  10263. (setq beg (match-beginning 0)
  10264. end (match-end 0)
  10265. t1 (save-match-data
  10266. (org-parse-time-string (substring string beg end) t))
  10267. with-hm (and (nth 1 t1) (nth 2 t1))
  10268. inactive (equal (substring string beg (1+ beg)) "[")
  10269. tf (funcall (if with-hm 'cdr 'car)
  10270. org-time-stamp-custom-formats)
  10271. time (org-fix-decoded-time t1)
  10272. str (format-time-string
  10273. (concat
  10274. (if inactive "[" "<") (substring tf 1 -1)
  10275. (if inactive "]" ">"))
  10276. (apply 'encode-time time))
  10277. string (replace-match str t t string)
  10278. start (+ start (length str)))))))
  10279. string)
  10280. (defun org-fix-decoded-time (time)
  10281. "Set 0 instead of nil for the first 6 elements of time.
  10282. Don't touch the rest."
  10283. (let ((n 0))
  10284. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10285. (defun org-days-to-time (timestamp-string)
  10286. "Difference between TIMESTAMP-STRING and now in days."
  10287. (- (time-to-days (org-time-string-to-time timestamp-string))
  10288. (time-to-days (current-time))))
  10289. (defun org-deadline-close (timestamp-string &optional ndays)
  10290. "Is the time in TIMESTAMP-STRING close to the current date?"
  10291. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10292. (and (< (org-days-to-time timestamp-string) ndays)
  10293. (not (org-entry-is-done-p))))
  10294. (defun org-get-wdays (ts)
  10295. "Get the deadline lead time appropriate for timestring TS."
  10296. (cond
  10297. ((<= org-deadline-warning-days 0)
  10298. ;; 0 or negative, enforce this value no matter what
  10299. (- org-deadline-warning-days))
  10300. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10301. ;; lead time is specified.
  10302. (floor (* (string-to-number (match-string 1 ts))
  10303. (cdr (assoc (match-string 2 ts)
  10304. '(("d" . 1) ("w" . 7)
  10305. ("m" . 30.4) ("y" . 365.25)))))))
  10306. ;; go for the default.
  10307. (t org-deadline-warning-days)))
  10308. (defun org-calendar-select-mouse (ev)
  10309. "Return to `org-read-date' with the date currently selected.
  10310. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10311. (interactive "e")
  10312. (mouse-set-point ev)
  10313. (when (calendar-cursor-to-date)
  10314. (let* ((date (calendar-cursor-to-date))
  10315. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10316. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10317. (if (active-minibuffer-window) (exit-minibuffer))))
  10318. (defun org-check-deadlines (ndays)
  10319. "Check if there are any deadlines due or past due.
  10320. A deadline is considered due if it happens within `org-deadline-warning-days'
  10321. days from today's date. If the deadline appears in an entry marked DONE,
  10322. it is not shown. The prefix arg NDAYS can be used to test that many
  10323. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10324. (interactive "P")
  10325. (let* ((org-warn-days
  10326. (cond
  10327. ((equal ndays '(4)) 100000)
  10328. (ndays (prefix-numeric-value ndays))
  10329. (t (abs org-deadline-warning-days))))
  10330. (case-fold-search nil)
  10331. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10332. (callback
  10333. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10334. (message "%d deadlines past-due or due within %d days"
  10335. (org-occur regexp nil callback)
  10336. org-warn-days)))
  10337. (defun org-check-before-date (date)
  10338. "Check if there are deadlines or scheduled entries before DATE."
  10339. (interactive (list (org-read-date)))
  10340. (let ((case-fold-search nil)
  10341. (regexp (concat "\\<\\(" org-deadline-string
  10342. "\\|" org-scheduled-string
  10343. "\\) *<\\([^>]+\\)>"))
  10344. (callback
  10345. (lambda () (time-less-p
  10346. (org-time-string-to-time (match-string 2))
  10347. (org-time-string-to-time date)))))
  10348. (message "%d entries before %s"
  10349. (org-occur regexp nil callback) date)))
  10350. (defun org-evaluate-time-range (&optional to-buffer)
  10351. "Evaluate a time range by computing the difference between start and end.
  10352. Normally the result is just printed in the echo area, but with prefix arg
  10353. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10354. If the time range is actually in a table, the result is inserted into the
  10355. next column.
  10356. For time difference computation, a year is assumed to be exactly 365
  10357. days in order to avoid rounding problems."
  10358. (interactive "P")
  10359. (or
  10360. (org-clock-update-time-maybe)
  10361. (save-excursion
  10362. (unless (org-at-date-range-p t)
  10363. (goto-char (point-at-bol))
  10364. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10365. (if (not (org-at-date-range-p t))
  10366. (error "Not at a time-stamp range, and none found in current line")))
  10367. (let* ((ts1 (match-string 1))
  10368. (ts2 (match-string 2))
  10369. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10370. (match-end (match-end 0))
  10371. (time1 (org-time-string-to-time ts1))
  10372. (time2 (org-time-string-to-time ts2))
  10373. (t1 (time-to-seconds time1))
  10374. (t2 (time-to-seconds time2))
  10375. (diff (abs (- t2 t1)))
  10376. (negative (< (- t2 t1) 0))
  10377. ;; (ys (floor (* 365 24 60 60)))
  10378. (ds (* 24 60 60))
  10379. (hs (* 60 60))
  10380. (fy "%dy %dd %02d:%02d")
  10381. (fy1 "%dy %dd")
  10382. (fd "%dd %02d:%02d")
  10383. (fd1 "%dd")
  10384. (fh "%02d:%02d")
  10385. y d h m align)
  10386. (if havetime
  10387. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10388. y 0
  10389. d (floor (/ diff ds)) diff (mod diff ds)
  10390. h (floor (/ diff hs)) diff (mod diff hs)
  10391. m (floor (/ diff 60)))
  10392. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10393. y 0
  10394. d (floor (+ (/ diff ds) 0.5))
  10395. h 0 m 0))
  10396. (if (not to-buffer)
  10397. (message "%s" (org-make-tdiff-string y d h m))
  10398. (if (org-at-table-p)
  10399. (progn
  10400. (goto-char match-end)
  10401. (setq align t)
  10402. (and (looking-at " *|") (goto-char (match-end 0))))
  10403. (goto-char match-end))
  10404. (if (looking-at
  10405. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10406. (replace-match ""))
  10407. (if negative (insert " -"))
  10408. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10409. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10410. (insert " " (format fh h m))))
  10411. (if align (org-table-align))
  10412. (message "Time difference inserted")))))
  10413. (defun org-make-tdiff-string (y d h m)
  10414. (let ((fmt "")
  10415. (l nil))
  10416. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10417. l (push y l)))
  10418. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10419. l (push d l)))
  10420. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10421. l (push h l)))
  10422. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10423. l (push m l)))
  10424. (apply 'format fmt (nreverse l))))
  10425. (defun org-time-string-to-time (s)
  10426. (apply 'encode-time (org-parse-time-string s)))
  10427. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10428. "Convert a time stamp to an absolute day number.
  10429. If there is a specifyer for a cyclic time stamp, get the closest date to
  10430. DAYNR.
  10431. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  10432. (cond
  10433. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10434. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10435. daynr
  10436. (+ daynr 1000)))
  10437. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10438. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10439. (time-to-days (current-time))) (match-string 0 s)
  10440. prefer show-all))
  10441. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10442. (defun org-days-to-iso-week (days)
  10443. "Return the iso week number."
  10444. (require 'cal-iso)
  10445. (car (calendar-iso-from-absolute days)))
  10446. (defun org-small-year-to-year (year)
  10447. "Convert 2-digit years into 4-digit years.
  10448. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10449. The year 2000 cannot be abbreviated. Any year lager than 99
  10450. is retrned unchanged."
  10451. (if (< year 38)
  10452. (setq year (+ 2000 year))
  10453. (if (< year 100)
  10454. (setq year (+ 1900 year))))
  10455. year)
  10456. (defun org-time-from-absolute (d)
  10457. "Return the time corresponding to date D.
  10458. D may be an absolute day number, or a calendar-type list (month day year)."
  10459. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10460. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10461. (defun org-calendar-holiday ()
  10462. "List of holidays, for Diary display in Org-mode."
  10463. (require 'holidays)
  10464. (let ((hl (funcall
  10465. (if (fboundp 'calendar-check-holidays)
  10466. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10467. (if hl (mapconcat 'identity hl "; "))))
  10468. (defun org-diary-sexp-entry (sexp entry date)
  10469. "Process a SEXP diary ENTRY for DATE."
  10470. (require 'diary-lib)
  10471. (let ((result (if calendar-debug-sexp
  10472. (let ((stack-trace-on-error t))
  10473. (eval (car (read-from-string sexp))))
  10474. (condition-case nil
  10475. (eval (car (read-from-string sexp)))
  10476. (error
  10477. (beep)
  10478. (message "Bad sexp at line %d in %s: %s"
  10479. (org-current-line)
  10480. (buffer-file-name) sexp)
  10481. (sleep-for 2))))))
  10482. (cond ((stringp result) result)
  10483. ((and (consp result)
  10484. (stringp (cdr result))) (cdr result))
  10485. (result entry)
  10486. (t nil))))
  10487. (defun org-diary-to-ical-string (frombuf)
  10488. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10489. This uses the icalendar.el library."
  10490. (let* ((tmpdir (if (featurep 'xemacs)
  10491. (temp-directory)
  10492. temporary-file-directory))
  10493. (tmpfile (make-temp-name
  10494. (expand-file-name "orgics" tmpdir)))
  10495. buf rtn b e)
  10496. (save-excursion
  10497. (set-buffer frombuf)
  10498. (icalendar-export-region (point-min) (point-max) tmpfile)
  10499. (setq buf (find-buffer-visiting tmpfile))
  10500. (set-buffer buf)
  10501. (goto-char (point-min))
  10502. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10503. (setq b (match-beginning 0)))
  10504. (goto-char (point-max))
  10505. (if (re-search-backward "^END:VEVENT" nil t)
  10506. (setq e (match-end 0)))
  10507. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10508. (kill-buffer buf)
  10509. (delete-file tmpfile)
  10510. rtn))
  10511. (defun org-closest-date (start current change prefer show-all)
  10512. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10513. When PREFER is `past' return a date that is either CURRENT or past.
  10514. When PREFER is `future', return a date that is either CURRENT or future.
  10515. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10516. ;; Make the proper lists from the dates
  10517. (catch 'exit
  10518. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10519. dn dw sday cday n1 n2
  10520. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10521. (setq start (org-date-to-gregorian start)
  10522. current (org-date-to-gregorian
  10523. (if show-all
  10524. current
  10525. (time-to-days (current-time))))
  10526. sday (calendar-absolute-from-gregorian start)
  10527. cday (calendar-absolute-from-gregorian current))
  10528. (if (<= cday sday) (throw 'exit sday))
  10529. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10530. (setq dn (string-to-number (match-string 1 change))
  10531. dw (cdr (assoc (match-string 2 change) a1)))
  10532. (error "Invalid change specifyer: %s" change))
  10533. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10534. (cond
  10535. ((eq dw 'day)
  10536. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10537. n2 (+ n1 dn)))
  10538. ((eq dw 'year)
  10539. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10540. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10541. (setq date1 (list m d y1)
  10542. n1 (calendar-absolute-from-gregorian date1)
  10543. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10544. n2 (calendar-absolute-from-gregorian date2)))
  10545. ((eq dw 'month)
  10546. ;; approx number of month between the two dates
  10547. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10548. ;; How often does dn fit in there?
  10549. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10550. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10551. m (+ m nm)
  10552. ny (floor (/ m 12))
  10553. y (+ y ny)
  10554. m (- m (* ny 12)))
  10555. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10556. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10557. (setq m2 (+ m dn) y2 y)
  10558. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10559. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10560. (while (<= n2 cday)
  10561. (setq n1 n2 m m2 y y2)
  10562. (setq m2 (+ m dn) y2 y)
  10563. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10564. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10565. (if show-all
  10566. (cond
  10567. ((eq prefer 'past) n1)
  10568. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10569. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10570. (cond
  10571. ((eq prefer 'past) n1)
  10572. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10573. (t (if (= cday n1) n1 n2)))))))
  10574. (defun org-date-to-gregorian (date)
  10575. "Turn any specification of DATE into a gregorian date for the calendar."
  10576. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10577. ((and (listp date) (= (length date) 3)) date)
  10578. ((stringp date)
  10579. (setq date (org-parse-time-string date))
  10580. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10581. ((listp date)
  10582. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10583. (defun org-parse-time-string (s &optional nodefault)
  10584. "Parse the standard Org-mode time string.
  10585. This should be a lot faster than the normal `parse-time-string'.
  10586. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10587. hour and minute fields will be nil if not given."
  10588. (if (string-match org-ts-regexp0 s)
  10589. (list 0
  10590. (if (or (match-beginning 8) (not nodefault))
  10591. (string-to-number (or (match-string 8 s) "0")))
  10592. (if (or (match-beginning 7) (not nodefault))
  10593. (string-to-number (or (match-string 7 s) "0")))
  10594. (string-to-number (match-string 4 s))
  10595. (string-to-number (match-string 3 s))
  10596. (string-to-number (match-string 2 s))
  10597. nil nil nil)
  10598. (make-list 9 0)))
  10599. (defun org-timestamp-up (&optional arg)
  10600. "Increase the date item at the cursor by one.
  10601. If the cursor is on the year, change the year. If it is on the month or
  10602. the day, change that.
  10603. With prefix ARG, change by that many units."
  10604. (interactive "p")
  10605. (org-timestamp-change (prefix-numeric-value arg)))
  10606. (defun org-timestamp-down (&optional arg)
  10607. "Decrease the date item at the cursor by one.
  10608. If the cursor is on the year, change the year. If it is on the month or
  10609. the day, change that.
  10610. With prefix ARG, change by that many units."
  10611. (interactive "p")
  10612. (org-timestamp-change (- (prefix-numeric-value arg))))
  10613. (defun org-timestamp-up-day (&optional arg)
  10614. "Increase the date in the time stamp by one day.
  10615. With prefix ARG, change that many days."
  10616. (interactive "p")
  10617. (if (and (not (org-at-timestamp-p t))
  10618. (org-on-heading-p))
  10619. (org-todo 'up)
  10620. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10621. (defun org-timestamp-down-day (&optional arg)
  10622. "Decrease the date in the time stamp by one day.
  10623. With prefix ARG, change that many days."
  10624. (interactive "p")
  10625. (if (and (not (org-at-timestamp-p t))
  10626. (org-on-heading-p))
  10627. (org-todo 'down)
  10628. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10629. (defun org-at-timestamp-p (&optional inactive-ok)
  10630. "Determine if the cursor is in or at a timestamp."
  10631. (interactive)
  10632. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10633. (pos (point))
  10634. (ans (or (looking-at tsr)
  10635. (save-excursion
  10636. (skip-chars-backward "^[<\n\r\t")
  10637. (if (> (point) (point-min)) (backward-char 1))
  10638. (and (looking-at tsr)
  10639. (> (- (match-end 0) pos) -1))))))
  10640. (and ans
  10641. (boundp 'org-ts-what)
  10642. (setq org-ts-what
  10643. (cond
  10644. ((= pos (match-beginning 0)) 'bracket)
  10645. ((= pos (1- (match-end 0))) 'bracket)
  10646. ((org-pos-in-match-range pos 2) 'year)
  10647. ((org-pos-in-match-range pos 3) 'month)
  10648. ((org-pos-in-match-range pos 7) 'hour)
  10649. ((org-pos-in-match-range pos 8) 'minute)
  10650. ((or (org-pos-in-match-range pos 4)
  10651. (org-pos-in-match-range pos 5)) 'day)
  10652. ((and (> pos (or (match-end 8) (match-end 5)))
  10653. (< pos (match-end 0)))
  10654. (- pos (or (match-end 8) (match-end 5))))
  10655. (t 'day))))
  10656. ans))
  10657. (defun org-toggle-timestamp-type ()
  10658. "Toggle the type (<active> or [inactive]) of a time stamp."
  10659. (interactive)
  10660. (when (org-at-timestamp-p t)
  10661. (save-excursion
  10662. (goto-char (match-beginning 0))
  10663. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10664. (goto-char (1- (match-end 0)))
  10665. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10666. (message "Timestamp is now %sactive"
  10667. (if (equal (char-before) ?>) "in" ""))))
  10668. (defun org-timestamp-change (n &optional what)
  10669. "Change the date in the time stamp at point.
  10670. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10671. `year', `minute', `second'. If WHAT is not given, the cursor position
  10672. in the timestamp determines what will be changed."
  10673. (let ((pos (point))
  10674. with-hm inactive
  10675. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10676. org-ts-what
  10677. extra rem
  10678. ts time time0)
  10679. (if (not (org-at-timestamp-p t))
  10680. (error "Not at a timestamp"))
  10681. (if (and (not what) (eq org-ts-what 'bracket))
  10682. (org-toggle-timestamp-type)
  10683. (if (and (not what) (not (eq org-ts-what 'day))
  10684. org-display-custom-times
  10685. (get-text-property (point) 'display)
  10686. (not (get-text-property (1- (point)) 'display)))
  10687. (setq org-ts-what 'day))
  10688. (setq org-ts-what (or what org-ts-what)
  10689. inactive (= (char-after (match-beginning 0)) ?\[)
  10690. ts (match-string 0))
  10691. (replace-match "")
  10692. (if (string-match
  10693. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10694. ts)
  10695. (setq extra (match-string 1 ts)))
  10696. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10697. (setq with-hm t))
  10698. (setq time0 (org-parse-time-string ts))
  10699. (when (and (eq org-ts-what 'minute)
  10700. (eq current-prefix-arg nil))
  10701. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10702. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10703. (setcar (cdr time0) (+ (nth 1 time0)
  10704. (if (> n 0) (- rem) (- dm rem))))))
  10705. (setq time
  10706. (encode-time (or (car time0) 0)
  10707. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10708. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10709. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10710. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10711. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10712. (nthcdr 6 time0)))
  10713. (when (integerp org-ts-what)
  10714. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10715. (if (eq what 'calendar)
  10716. (let ((cal-date (org-get-date-from-calendar)))
  10717. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10718. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10719. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10720. (setcar time0 (or (car time0) 0))
  10721. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10722. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10723. (setq time (apply 'encode-time time0))))
  10724. (setq org-last-changed-timestamp
  10725. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10726. (org-clock-update-time-maybe)
  10727. (goto-char pos)
  10728. ;; Try to recenter the calendar window, if any
  10729. (if (and org-calendar-follow-timestamp-change
  10730. (get-buffer-window "*Calendar*" t)
  10731. (memq org-ts-what '(day month year)))
  10732. (org-recenter-calendar (time-to-days time))))))
  10733. (defun org-modify-ts-extra (s pos n dm)
  10734. "Change the different parts of the lead-time and repeat fields in timestamp."
  10735. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10736. ng h m new rem)
  10737. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10738. (cond
  10739. ((or (org-pos-in-match-range pos 2)
  10740. (org-pos-in-match-range pos 3))
  10741. (setq m (string-to-number (match-string 3 s))
  10742. h (string-to-number (match-string 2 s)))
  10743. (if (org-pos-in-match-range pos 2)
  10744. (setq h (+ h n))
  10745. (setq n (* dm (org-no-warnings (signum n))))
  10746. (when (not (= 0 (setq rem (% m dm))))
  10747. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10748. (setq m (+ m n)))
  10749. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10750. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10751. (setq h (min 24 (max 0 h)))
  10752. (setq ng 1 new (format "-%02d:%02d" h m)))
  10753. ((org-pos-in-match-range pos 6)
  10754. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10755. ((org-pos-in-match-range pos 5)
  10756. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10757. ((org-pos-in-match-range pos 9)
  10758. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10759. ((org-pos-in-match-range pos 8)
  10760. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10761. (when ng
  10762. (setq s (concat
  10763. (substring s 0 (match-beginning ng))
  10764. new
  10765. (substring s (match-end ng))))))
  10766. s))
  10767. (defun org-recenter-calendar (date)
  10768. "If the calendar is visible, recenter it to DATE."
  10769. (let* ((win (selected-window))
  10770. (cwin (get-buffer-window "*Calendar*" t))
  10771. (calendar-move-hook nil))
  10772. (when cwin
  10773. (select-window cwin)
  10774. (calendar-goto-date (if (listp date) date
  10775. (calendar-gregorian-from-absolute date)))
  10776. (select-window win))))
  10777. (defun org-goto-calendar (&optional arg)
  10778. "Go to the Emacs calendar at the current date.
  10779. If there is a time stamp in the current line, go to that date.
  10780. A prefix ARG can be used to force the current date."
  10781. (interactive "P")
  10782. (let ((tsr org-ts-regexp) diff
  10783. (calendar-move-hook nil)
  10784. (calendar-view-holidays-initially-flag nil)
  10785. (view-calendar-holidays-initially nil)
  10786. (calendar-view-diary-initially-flag nil)
  10787. (view-diary-entries-initially nil))
  10788. (if (or (org-at-timestamp-p)
  10789. (save-excursion
  10790. (beginning-of-line 1)
  10791. (looking-at (concat ".*" tsr))))
  10792. (let ((d1 (time-to-days (current-time)))
  10793. (d2 (time-to-days
  10794. (org-time-string-to-time (match-string 1)))))
  10795. (setq diff (- d2 d1))))
  10796. (calendar)
  10797. (calendar-goto-today)
  10798. (if (and diff (not arg)) (calendar-forward-day diff))))
  10799. (defun org-get-date-from-calendar ()
  10800. "Return a list (month day year) of date at point in calendar."
  10801. (with-current-buffer "*Calendar*"
  10802. (save-match-data
  10803. (calendar-cursor-to-date))))
  10804. (defun org-date-from-calendar ()
  10805. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10806. If there is already a time stamp at the cursor position, update it."
  10807. (interactive)
  10808. (if (org-at-timestamp-p t)
  10809. (org-timestamp-change 0 'calendar)
  10810. (let ((cal-date (org-get-date-from-calendar)))
  10811. (org-insert-time-stamp
  10812. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10813. (defun org-minutes-to-hh:mm-string (m)
  10814. "Compute H:MM from a number of minutes."
  10815. (let ((h (/ m 60)))
  10816. (setq m (- m (* 60 h)))
  10817. (format org-time-clocksum-format h m)))
  10818. (defun org-hh:mm-string-to-minutes (s)
  10819. "Convert a string H:MM to a number of minutes."
  10820. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10821. (+ (* (string-to-number (match-string 1 s)) 60)
  10822. (string-to-number (match-string 2 s)))
  10823. 0))
  10824. ;;;; Agenda files
  10825. ;;;###autoload
  10826. (defun org-iswitchb (&optional arg)
  10827. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10828. With a prefix argument, restrict available to files.
  10829. With two prefix arguments, restrict available buffers to agenda files.
  10830. Due to some yet unresolved reason, global function
  10831. `iswitchb-mode' needs to be active for this function to work."
  10832. (interactive "P")
  10833. (require 'iswitchb)
  10834. (let ((enabled iswitchb-mode) blist)
  10835. (or enabled (iswitchb-mode 1))
  10836. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10837. ((equal arg '(16)) (org-buffer-list 'agenda))
  10838. (t (org-buffer-list))))
  10839. (unwind-protect
  10840. (let ((iswitchb-make-buflist-hook
  10841. (lambda ()
  10842. (setq iswitchb-temp-buflist
  10843. (mapcar 'buffer-name blist)))))
  10844. (switch-to-buffer
  10845. (iswitchb-read-buffer
  10846. "Switch-to: " nil t))
  10847. (or enabled (iswitchb-mode -1))))))
  10848. (defun org-buffer-list (&optional predicate tmp)
  10849. "Return a list of Org buffers.
  10850. PREDICATE can be either 'export, 'files or 'agenda.
  10851. 'export restrict the list to Export buffers.
  10852. 'files restrict the list to buffers visiting Org files.
  10853. 'agenda restrict the list to buffers visiting agenda files.
  10854. If TMP is non-nil, don't include temporary buffers."
  10855. (let (filter blist)
  10856. (setq filter
  10857. (cond ((eq predicate 'files) "\.org$")
  10858. ((eq predicate 'export) "\*Org .*Export")
  10859. (t "\*Org \\|\.org$")))
  10860. (setq blist
  10861. (mapcar
  10862. (lambda(b)
  10863. (let ((bname (buffer-name b))
  10864. (bfile (buffer-file-name b)))
  10865. (if (and (string-match filter bname)
  10866. (if (eq predicate 'agenda)
  10867. (member bfile
  10868. (mapcar (lambda(f) (file-truename f))
  10869. org-agenda-files)) t)
  10870. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10871. (buffer-list)))
  10872. (delete nil blist)))
  10873. (defun org-agenda-files (&optional unrestricted archives)
  10874. "Get the list of agenda files.
  10875. Optional UNRESTRICTED means return the full list even if a restriction
  10876. is currently in place.
  10877. When ARCHIVES is t, include all archive files hat are really being
  10878. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10879. `org-agenda-archives-mode' is t."
  10880. (let ((files
  10881. (cond
  10882. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10883. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10884. ((listp org-agenda-files) org-agenda-files)
  10885. (t (error "Invalid value of `org-agenda-files'")))))
  10886. (setq files (apply 'append
  10887. (mapcar (lambda (f)
  10888. (if (file-directory-p f)
  10889. (directory-files
  10890. f t org-agenda-file-regexp)
  10891. (list f)))
  10892. files)))
  10893. (when org-agenda-skip-unavailable-files
  10894. (setq files (delq nil
  10895. (mapcar (function
  10896. (lambda (file)
  10897. (and (file-readable-p file) file)))
  10898. files))))
  10899. (when (or (eq archives t)
  10900. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10901. (setq files (org-add-archive-files files)))
  10902. files))
  10903. (defun org-edit-agenda-file-list ()
  10904. "Edit the list of agenda files.
  10905. Depending on setup, this either uses customize to edit the variable
  10906. `org-agenda-files', or it visits the file that is holding the list. In the
  10907. latter case, the buffer is set up in a way that saving it automatically kills
  10908. the buffer and restores the previous window configuration."
  10909. (interactive)
  10910. (if (stringp org-agenda-files)
  10911. (let ((cw (current-window-configuration)))
  10912. (find-file org-agenda-files)
  10913. (org-set-local 'org-window-configuration cw)
  10914. (org-add-hook 'after-save-hook
  10915. (lambda ()
  10916. (set-window-configuration
  10917. (prog1 org-window-configuration
  10918. (kill-buffer (current-buffer))))
  10919. (org-install-agenda-files-menu)
  10920. (message "New agenda file list installed"))
  10921. nil 'local)
  10922. (message "%s" (substitute-command-keys
  10923. "Edit list and finish with \\[save-buffer]")))
  10924. (customize-variable 'org-agenda-files)))
  10925. (defun org-store-new-agenda-file-list (list)
  10926. "Set new value for the agenda file list and save it correcly."
  10927. (if (stringp org-agenda-files)
  10928. (let ((f org-agenda-files) b)
  10929. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10930. (with-temp-file f
  10931. (insert (mapconcat 'identity list "\n") "\n")))
  10932. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10933. (setq org-agenda-files list)
  10934. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10935. (defun org-read-agenda-file-list ()
  10936. "Read the list of agenda files from a file."
  10937. (when (file-directory-p org-agenda-files)
  10938. (error "`org-agenda-files' cannot be a single directory"))
  10939. (when (stringp org-agenda-files)
  10940. (with-temp-buffer
  10941. (insert-file-contents org-agenda-files)
  10942. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10943. ;;;###autoload
  10944. (defun org-cycle-agenda-files ()
  10945. "Cycle through the files in `org-agenda-files'.
  10946. If the current buffer visits an agenda file, find the next one in the list.
  10947. If the current buffer does not, find the first agenda file."
  10948. (interactive)
  10949. (let* ((fs (org-agenda-files t))
  10950. (files (append fs (list (car fs))))
  10951. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10952. file)
  10953. (unless files (error "No agenda files"))
  10954. (catch 'exit
  10955. (while (setq file (pop files))
  10956. (if (equal (file-truename file) tcf)
  10957. (when (car files)
  10958. (find-file (car files))
  10959. (throw 'exit t))))
  10960. (find-file (car fs)))
  10961. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10962. (defun org-agenda-file-to-front (&optional to-end)
  10963. "Move/add the current file to the top of the agenda file list.
  10964. If the file is not present in the list, it is added to the front. If it is
  10965. present, it is moved there. With optional argument TO-END, add/move to the
  10966. end of the list."
  10967. (interactive "P")
  10968. (let ((org-agenda-skip-unavailable-files nil)
  10969. (file-alist (mapcar (lambda (x)
  10970. (cons (file-truename x) x))
  10971. (org-agenda-files t)))
  10972. (ctf (file-truename buffer-file-name))
  10973. x had)
  10974. (setq x (assoc ctf file-alist) had x)
  10975. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10976. (if to-end
  10977. (setq file-alist (append (delq x file-alist) (list x)))
  10978. (setq file-alist (cons x (delq x file-alist))))
  10979. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10980. (org-install-agenda-files-menu)
  10981. (message "File %s to %s of agenda file list"
  10982. (if had "moved" "added") (if to-end "end" "front"))))
  10983. (defun org-remove-file (&optional file)
  10984. "Remove current file from the list of files in variable `org-agenda-files'.
  10985. These are the files which are being checked for agenda entries.
  10986. Optional argument FILE means, use this file instead of the current."
  10987. (interactive)
  10988. (let* ((org-agenda-skip-unavailable-files nil)
  10989. (file (or file buffer-file-name))
  10990. (true-file (file-truename file))
  10991. (afile (abbreviate-file-name file))
  10992. (files (delq nil (mapcar
  10993. (lambda (x)
  10994. (if (equal true-file
  10995. (file-truename x))
  10996. nil x))
  10997. (org-agenda-files t)))))
  10998. (if (not (= (length files) (length (org-agenda-files t))))
  10999. (progn
  11000. (org-store-new-agenda-file-list files)
  11001. (org-install-agenda-files-menu)
  11002. (message "Removed file: %s" afile))
  11003. (message "File was not in list: %s (not removed)" afile))))
  11004. (defun org-file-menu-entry (file)
  11005. (vector file (list 'find-file file) t))
  11006. (defun org-check-agenda-file (file)
  11007. "Make sure FILE exists. If not, ask user what to do."
  11008. (when (not (file-exists-p file))
  11009. (message "non-existent file %s. [R]emove from list or [A]bort?"
  11010. (abbreviate-file-name file))
  11011. (let ((r (downcase (read-char-exclusive))))
  11012. (cond
  11013. ((equal r ?r)
  11014. (org-remove-file file)
  11015. (throw 'nextfile t))
  11016. (t (error "Abort"))))))
  11017. (defun org-get-agenda-file-buffer (file)
  11018. "Get a buffer visiting FILE. If the buffer needs to be created, add
  11019. it to the list of buffers which might be released later."
  11020. (let ((buf (org-find-base-buffer-visiting file)))
  11021. (if buf
  11022. buf ; just return it
  11023. ;; Make a new buffer and remember it
  11024. (setq buf (find-file-noselect file))
  11025. (if buf (push buf org-agenda-new-buffers))
  11026. buf)))
  11027. (defun org-release-buffers (blist)
  11028. "Release all buffers in list, asking the user for confirmation when needed.
  11029. When a buffer is unmodified, it is just killed. When modified, it is saved
  11030. \(if the user agrees) and then killed."
  11031. (let (buf file)
  11032. (while (setq buf (pop blist))
  11033. (setq file (buffer-file-name buf))
  11034. (when (and (buffer-modified-p buf)
  11035. file
  11036. (y-or-n-p (format "Save file %s? " file)))
  11037. (with-current-buffer buf (save-buffer)))
  11038. (kill-buffer buf))))
  11039. (defun org-prepare-agenda-buffers (files)
  11040. "Create buffers for all agenda files, protect archived trees and comments."
  11041. (interactive)
  11042. (let ((pa '(:org-archived t))
  11043. (pc '(:org-comment t))
  11044. (pall '(:org-archived t :org-comment t))
  11045. (inhibit-read-only t)
  11046. (rea (concat ":" org-archive-tag ":"))
  11047. bmp file re)
  11048. (save-excursion
  11049. (save-restriction
  11050. (while (setq file (pop files))
  11051. (if (bufferp file)
  11052. (set-buffer file)
  11053. (org-check-agenda-file file)
  11054. (set-buffer (org-get-agenda-file-buffer file)))
  11055. (widen)
  11056. (setq bmp (buffer-modified-p))
  11057. (org-refresh-category-properties)
  11058. (setq org-todo-keywords-for-agenda
  11059. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  11060. (setq org-done-keywords-for-agenda
  11061. (append org-done-keywords-for-agenda org-done-keywords))
  11062. (save-excursion
  11063. (remove-text-properties (point-min) (point-max) pall)
  11064. (when org-agenda-skip-archived-trees
  11065. (goto-char (point-min))
  11066. (while (re-search-forward rea nil t)
  11067. (if (org-on-heading-p t)
  11068. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  11069. (goto-char (point-min))
  11070. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  11071. (while (re-search-forward re nil t)
  11072. (add-text-properties
  11073. (match-beginning 0) (org-end-of-subtree t) pc)))
  11074. (set-buffer-modified-p bmp))))))
  11075. ;;;; Embedded LaTeX
  11076. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  11077. "Keymap for the minor `org-cdlatex-mode'.")
  11078. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  11079. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  11080. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  11081. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  11082. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  11083. (defvar org-cdlatex-texmathp-advice-is-done nil
  11084. "Flag remembering if we have applied the advice to texmathp already.")
  11085. (define-minor-mode org-cdlatex-mode
  11086. "Toggle the minor `org-cdlatex-mode'.
  11087. This mode supports entering LaTeX environment and math in LaTeX fragments
  11088. in Org-mode.
  11089. \\{org-cdlatex-mode-map}"
  11090. nil " OCDL" nil
  11091. (when org-cdlatex-mode (require 'cdlatex))
  11092. (unless org-cdlatex-texmathp-advice-is-done
  11093. (setq org-cdlatex-texmathp-advice-is-done t)
  11094. (defadvice texmathp (around org-math-always-on activate)
  11095. "Always return t in org-mode buffers.
  11096. This is because we want to insert math symbols without dollars even outside
  11097. the LaTeX math segments. If Orgmode thinks that point is actually inside
  11098. en embedded LaTeX fragement, let texmathp do its job.
  11099. \\[org-cdlatex-mode-map]"
  11100. (interactive)
  11101. (let (p)
  11102. (cond
  11103. ((not (org-mode-p)) ad-do-it)
  11104. ((eq this-command 'cdlatex-math-symbol)
  11105. (setq ad-return-value t
  11106. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11107. (t
  11108. (let ((p (org-inside-LaTeX-fragment-p)))
  11109. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11110. (setq ad-return-value t
  11111. texmathp-why '("Org-mode embedded math" . 0))
  11112. (if p ad-do-it)))))))))
  11113. (defun turn-on-org-cdlatex ()
  11114. "Unconditionally turn on `org-cdlatex-mode'."
  11115. (org-cdlatex-mode 1))
  11116. (defun org-inside-LaTeX-fragment-p ()
  11117. "Test if point is inside a LaTeX fragment.
  11118. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11119. sequence appearing also before point.
  11120. Even though the matchers for math are configurable, this function assumes
  11121. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11122. delimiters are skipped when they have been removed by customization.
  11123. The return value is nil, or a cons cell with the delimiter and
  11124. and the position of this delimiter.
  11125. This function does a reasonably good job, but can locally be fooled by
  11126. for example currency specifications. For example it will assume being in
  11127. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11128. fragments that are properly closed, but during editing, we have to live
  11129. with the uncertainty caused by missing closing delimiters. This function
  11130. looks only before point, not after."
  11131. (catch 'exit
  11132. (let ((pos (point))
  11133. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11134. (lim (progn
  11135. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11136. (point)))
  11137. dd-on str (start 0) m re)
  11138. (goto-char pos)
  11139. (when dodollar
  11140. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11141. re (nth 1 (assoc "$" org-latex-regexps)))
  11142. (while (string-match re str start)
  11143. (cond
  11144. ((= (match-end 0) (length str))
  11145. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11146. ((= (match-end 0) (- (length str) 5))
  11147. (throw 'exit nil))
  11148. (t (setq start (match-end 0))))))
  11149. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11150. (goto-char pos)
  11151. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11152. (and (match-beginning 2) (throw 'exit nil))
  11153. ;; count $$
  11154. (while (re-search-backward "\\$\\$" lim t)
  11155. (setq dd-on (not dd-on)))
  11156. (goto-char pos)
  11157. (if dd-on (cons "$$" m))))))
  11158. (defun org-try-cdlatex-tab ()
  11159. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11160. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11161. - inside a LaTeX fragment, or
  11162. - after the first word in a line, where an abbreviation expansion could
  11163. insert a LaTeX environment."
  11164. (when org-cdlatex-mode
  11165. (cond
  11166. ((save-excursion
  11167. (skip-chars-backward "a-zA-Z0-9*")
  11168. (skip-chars-backward " \t")
  11169. (bolp))
  11170. (cdlatex-tab) t)
  11171. ((org-inside-LaTeX-fragment-p)
  11172. (cdlatex-tab) t)
  11173. (t nil))))
  11174. (defun org-cdlatex-underscore-caret (&optional arg)
  11175. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11176. Revert to the normal definition outside of these fragments."
  11177. (interactive "P")
  11178. (if (org-inside-LaTeX-fragment-p)
  11179. (call-interactively 'cdlatex-sub-superscript)
  11180. (let (org-cdlatex-mode)
  11181. (call-interactively (key-binding (vector last-input-event))))))
  11182. (defun org-cdlatex-math-modify (&optional arg)
  11183. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11184. Revert to the normal definition outside of these fragments."
  11185. (interactive "P")
  11186. (if (org-inside-LaTeX-fragment-p)
  11187. (call-interactively 'cdlatex-math-modify)
  11188. (let (org-cdlatex-mode)
  11189. (call-interactively (key-binding (vector last-input-event))))))
  11190. (defvar org-latex-fragment-image-overlays nil
  11191. "List of overlays carrying the images of latex fragments.")
  11192. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11193. (defun org-remove-latex-fragment-image-overlays ()
  11194. "Remove all overlays with LaTeX fragment images in current buffer."
  11195. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11196. (setq org-latex-fragment-image-overlays nil))
  11197. (defun org-preview-latex-fragment (&optional subtree)
  11198. "Preview the LaTeX fragment at point, or all locally or globally.
  11199. If the cursor is in a LaTeX fragment, create the image and overlay
  11200. it over the source code. If there is no fragment at point, display
  11201. all fragments in the current text, from one headline to the next. With
  11202. prefix SUBTREE, display all fragments in the current subtree. With a
  11203. double prefix `C-u C-u', or when the cursor is before the first headline,
  11204. display all fragments in the buffer.
  11205. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11206. (interactive "P")
  11207. (org-remove-latex-fragment-image-overlays)
  11208. (save-excursion
  11209. (save-restriction
  11210. (let (beg end at msg)
  11211. (cond
  11212. ((or (equal subtree '(16))
  11213. (not (save-excursion
  11214. (re-search-backward (concat "^" outline-regexp) nil t))))
  11215. (setq beg (point-min) end (point-max)
  11216. msg "Creating images for buffer...%s"))
  11217. ((equal subtree '(4))
  11218. (org-back-to-heading)
  11219. (setq beg (point) end (org-end-of-subtree t)
  11220. msg "Creating images for subtree...%s"))
  11221. (t
  11222. (if (setq at (org-inside-LaTeX-fragment-p))
  11223. (goto-char (max (point-min) (- (cdr at) 2)))
  11224. (org-back-to-heading))
  11225. (setq beg (point) end (progn (outline-next-heading) (point))
  11226. msg (if at "Creating image...%s"
  11227. "Creating images for entry...%s"))))
  11228. (message msg "")
  11229. (narrow-to-region beg end)
  11230. (goto-char beg)
  11231. (org-format-latex
  11232. (concat "ltxpng/" (file-name-sans-extension
  11233. (file-name-nondirectory
  11234. buffer-file-name)))
  11235. default-directory 'overlays msg at 'forbuffer)
  11236. (message msg "done. Use `C-c C-c' to remove images.")))))
  11237. (defvar org-latex-regexps
  11238. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11239. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11240. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11241. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11242. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11243. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11244. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11245. "Regular expressions for matching embedded LaTeX.")
  11246. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11247. "Replace LaTeX fragments with links to an image, and produce images."
  11248. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11249. (let* ((prefixnodir (file-name-nondirectory prefix))
  11250. (absprefix (expand-file-name prefix dir))
  11251. (todir (file-name-directory absprefix))
  11252. (opt org-format-latex-options)
  11253. (matchers (plist-get opt :matchers))
  11254. (re-list org-latex-regexps)
  11255. (cnt 0) txt link beg end re e checkdir
  11256. m n block linkfile movefile ov)
  11257. ;; Check if there are old images files with this prefix, and remove them
  11258. (when (file-directory-p todir)
  11259. (mapc 'delete-file
  11260. (directory-files
  11261. todir 'full
  11262. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11263. ;; Check the different regular expressions
  11264. (while (setq e (pop re-list))
  11265. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11266. block (if (nth 3 e) "\n\n" ""))
  11267. (when (member m matchers)
  11268. (goto-char (point-min))
  11269. (while (re-search-forward re nil t)
  11270. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11271. (setq txt (match-string n)
  11272. beg (match-beginning n) end (match-end n)
  11273. cnt (1+ cnt)
  11274. linkfile (format "%s_%04d.png" prefix cnt)
  11275. movefile (format "%s_%04d.png" absprefix cnt)
  11276. link (concat block "[[file:" linkfile "]]" block))
  11277. (if msg (message msg cnt))
  11278. (goto-char beg)
  11279. (unless checkdir ; make sure the directory exists
  11280. (setq checkdir t)
  11281. (or (file-directory-p todir) (make-directory todir)))
  11282. (org-create-formula-image
  11283. txt movefile opt forbuffer)
  11284. (if overlays
  11285. (progn
  11286. (setq ov (org-make-overlay beg end))
  11287. (if (featurep 'xemacs)
  11288. (progn
  11289. (org-overlay-put ov 'invisible t)
  11290. (org-overlay-put
  11291. ov 'end-glyph
  11292. (make-glyph (vector 'png :file movefile))))
  11293. (org-overlay-put
  11294. ov 'display
  11295. (list 'image :type 'png :file movefile :ascent 'center)))
  11296. (push ov org-latex-fragment-image-overlays)
  11297. (goto-char end))
  11298. (delete-region beg end)
  11299. (insert link))))))))
  11300. ;; This function borrows from Ganesh Swami's latex2png.el
  11301. (defun org-create-formula-image (string tofile options buffer)
  11302. (let* ((tmpdir (if (featurep 'xemacs)
  11303. (temp-directory)
  11304. temporary-file-directory))
  11305. (texfilebase (make-temp-name
  11306. (expand-file-name "orgtex" tmpdir)))
  11307. (texfile (concat texfilebase ".tex"))
  11308. (dvifile (concat texfilebase ".dvi"))
  11309. (pngfile (concat texfilebase ".png"))
  11310. (fnh (if (featurep 'xemacs)
  11311. (font-height (get-face-font 'default))
  11312. (face-attribute 'default :height nil)))
  11313. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11314. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11315. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11316. "Black"))
  11317. (bg (or (plist-get options (if buffer :background :html-background))
  11318. "Transparent")))
  11319. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11320. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11321. (with-temp-file texfile
  11322. (insert org-format-latex-header
  11323. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11324. (let ((dir default-directory))
  11325. (condition-case nil
  11326. (progn
  11327. (cd tmpdir)
  11328. (call-process "latex" nil nil nil texfile))
  11329. (error nil))
  11330. (cd dir))
  11331. (if (not (file-exists-p dvifile))
  11332. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11333. (call-process "dvipng" nil nil nil
  11334. "-E" "-fg" fg "-bg" bg
  11335. "-D" dpi
  11336. ;;"-x" scale "-y" scale
  11337. "-T" "tight"
  11338. "-o" pngfile
  11339. dvifile)
  11340. (if (not (file-exists-p pngfile))
  11341. (progn (message "Failed to create png file from %s" texfile) nil)
  11342. ;; Use the requested file name and clean up
  11343. (copy-file pngfile tofile 'replace)
  11344. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11345. (delete-file (concat texfilebase e)))
  11346. pngfile))))
  11347. (defun org-dvipng-color (attr)
  11348. "Return an rgb color specification for dvipng."
  11349. (apply 'format "rgb %s %s %s"
  11350. (mapcar 'org-normalize-color
  11351. (color-values (face-attribute 'default attr nil)))))
  11352. (defun org-normalize-color (value)
  11353. "Return string to be used as color value for an RGB component."
  11354. (format "%g" (/ value 65535.0)))
  11355. ;;;; Key bindings
  11356. ;; Make `C-c C-x' a prefix key
  11357. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11358. ;; TAB key with modifiers
  11359. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11360. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11361. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11362. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11363. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11364. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11365. ;; The following line is necessary under Suse GNU/Linux
  11366. (unless (featurep 'xemacs)
  11367. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11368. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11369. (define-key org-mode-map [backtab] 'org-shifttab)
  11370. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11371. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11372. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11373. ;; Cursor keys with modifiers
  11374. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11375. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11376. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11377. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11378. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11379. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11380. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11381. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11382. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11383. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11384. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11385. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11386. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11387. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11388. ;;; Extra keys for tty access.
  11389. ;; We only set them when really needed because otherwise the
  11390. ;; menus don't show the simple keys
  11391. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11392. (not window-system))
  11393. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11394. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11395. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11396. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11397. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11398. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11399. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11400. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11401. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11402. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11403. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11404. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11405. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11406. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11407. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11408. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11409. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11410. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11411. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11412. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11413. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11414. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11415. ;; All the other keys
  11416. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11417. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11418. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  11419. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11420. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11421. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11422. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11423. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11424. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11425. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11426. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  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-after-current)
  11437. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11438. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11439. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11440. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11441. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11442. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11443. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11444. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11445. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11446. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11447. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11448. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11449. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11450. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11451. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11452. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11453. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11454. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11455. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11456. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11457. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11458. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11459. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11460. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11461. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11462. (org-defkey org-mode-map "\C-m" 'org-return)
  11463. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11464. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11465. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11466. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11467. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11468. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11469. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11470. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11471. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11472. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11473. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11474. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11475. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11476. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11477. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11478. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11479. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11480. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11481. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11482. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11483. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11484. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11485. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11486. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11487. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11488. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11489. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11490. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11491. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11492. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11493. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11494. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11495. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11496. (when (featurep 'xemacs)
  11497. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11498. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11499. (defun org-self-insert-command (N)
  11500. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11501. If the cursor is in a table looking at whitespace, the whitespace is
  11502. overwritten, and the table is not marked as requiring realignment."
  11503. (interactive "p")
  11504. (if (and (org-table-p)
  11505. (progn
  11506. ;; check if we blank the field, and if that triggers align
  11507. (and (featurep 'org-table) org-table-auto-blank-field
  11508. (member last-command
  11509. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11510. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11511. ;; got extra space, this field does not determine column width
  11512. (let (org-table-may-need-update) (org-table-blank-field))
  11513. ;; no extra space, this field may determine column width
  11514. (org-table-blank-field)))
  11515. t)
  11516. (eq N 1)
  11517. (looking-at "[^|\n]* |"))
  11518. (let (org-table-may-need-update)
  11519. (goto-char (1- (match-end 0)))
  11520. (delete-backward-char 1)
  11521. (goto-char (match-beginning 0))
  11522. (self-insert-command N))
  11523. (setq org-table-may-need-update t)
  11524. (self-insert-command N)
  11525. (org-fix-tags-on-the-fly)))
  11526. (defun org-fix-tags-on-the-fly ()
  11527. (when (and (equal (char-after (point-at-bol)) ?*)
  11528. (org-on-heading-p))
  11529. (org-align-tags-here org-tags-column)))
  11530. (defun org-delete-backward-char (N)
  11531. "Like `delete-backward-char', insert whitespace at field end in tables.
  11532. When deleting backwards, in tables this function will insert whitespace in
  11533. front of the next \"|\" separator, to keep the table aligned. The table will
  11534. still be marked for re-alignment if the field did fill the entire column,
  11535. because, in this case the deletion might narrow the column."
  11536. (interactive "p")
  11537. (if (and (org-table-p)
  11538. (eq N 1)
  11539. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11540. (looking-at ".*?|"))
  11541. (let ((pos (point))
  11542. (noalign (looking-at "[^|\n\r]* |"))
  11543. (c org-table-may-need-update))
  11544. (backward-delete-char N)
  11545. (skip-chars-forward "^|")
  11546. (insert " ")
  11547. (goto-char (1- pos))
  11548. ;; noalign: if there were two spaces at the end, this field
  11549. ;; does not determine the width of the column.
  11550. (if noalign (setq org-table-may-need-update c)))
  11551. (backward-delete-char N)
  11552. (org-fix-tags-on-the-fly)))
  11553. (defun org-delete-char (N)
  11554. "Like `delete-char', but insert whitespace at field end in tables.
  11555. When deleting characters, in tables this function will insert whitespace in
  11556. front of the next \"|\" separator, to keep the table aligned. The table will
  11557. still be marked for re-alignment if the field did fill the entire column,
  11558. because, in this case the deletion might narrow the column."
  11559. (interactive "p")
  11560. (if (and (org-table-p)
  11561. (not (bolp))
  11562. (not (= (char-after) ?|))
  11563. (eq N 1))
  11564. (if (looking-at ".*?|")
  11565. (let ((pos (point))
  11566. (noalign (looking-at "[^|\n\r]* |"))
  11567. (c org-table-may-need-update))
  11568. (replace-match (concat
  11569. (substring (match-string 0) 1 -1)
  11570. " |"))
  11571. (goto-char pos)
  11572. ;; noalign: if there were two spaces at the end, this field
  11573. ;; does not determine the width of the column.
  11574. (if noalign (setq org-table-may-need-update c)))
  11575. (delete-char N))
  11576. (delete-char N)
  11577. (org-fix-tags-on-the-fly)))
  11578. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11579. (put 'org-self-insert-command 'delete-selection t)
  11580. (put 'orgtbl-self-insert-command 'delete-selection t)
  11581. (put 'org-delete-char 'delete-selection 'supersede)
  11582. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11583. ;; Make `flyspell-mode' delay after some commands
  11584. (put 'org-self-insert-command 'flyspell-delayed t)
  11585. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11586. (put 'org-delete-char 'flyspell-delayed t)
  11587. (put 'org-delete-backward-char 'flyspell-delayed t)
  11588. ;; Make pabbrev-mode expand after org-mode commands
  11589. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11590. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11591. ;; How to do this: Measure non-white length of current string
  11592. ;; If equal to column width, we should realign.
  11593. (defun org-remap (map &rest commands)
  11594. "In MAP, remap the functions given in COMMANDS.
  11595. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11596. (let (new old)
  11597. (while commands
  11598. (setq old (pop commands) new (pop commands))
  11599. (if (fboundp 'command-remapping)
  11600. (org-defkey map (vector 'remap old) new)
  11601. (substitute-key-definition old new map global-map)))))
  11602. (when (eq org-enable-table-editor 'optimized)
  11603. ;; If the user wants maximum table support, we need to hijack
  11604. ;; some standard editing functions
  11605. (org-remap org-mode-map
  11606. 'self-insert-command 'org-self-insert-command
  11607. 'delete-char 'org-delete-char
  11608. 'delete-backward-char 'org-delete-backward-char)
  11609. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11610. (defun org-shiftcursor-error ()
  11611. "Throw an error because Shift-Cursor command was applied in wrong context."
  11612. (error "This command is active in special context like tables, headlines or timestamps"))
  11613. (defun org-shifttab (&optional arg)
  11614. "Global visibility cycling or move to previous table field.
  11615. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11616. on context.
  11617. See the individual commands for more information."
  11618. (interactive "P")
  11619. (cond
  11620. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11621. ((integerp arg)
  11622. (message "Content view to level: %d" arg)
  11623. (org-content (prefix-numeric-value arg))
  11624. (setq org-cycle-global-status 'overview))
  11625. (t (call-interactively 'org-global-cycle))))
  11626. (defun org-shiftmetaleft ()
  11627. "Promote subtree or delete table column.
  11628. Calls `org-promote-subtree', `org-outdent-item',
  11629. or `org-table-delete-column', depending on context.
  11630. See the individual commands for more information."
  11631. (interactive)
  11632. (cond
  11633. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11634. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11635. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11636. (t (org-shiftcursor-error))))
  11637. (defun org-shiftmetaright ()
  11638. "Demote subtree or insert table column.
  11639. Calls `org-demote-subtree', `org-indent-item',
  11640. or `org-table-insert-column', depending on context.
  11641. See the individual commands for more information."
  11642. (interactive)
  11643. (cond
  11644. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11645. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11646. ((org-at-item-p) (call-interactively 'org-indent-item))
  11647. (t (org-shiftcursor-error))))
  11648. (defun org-shiftmetaup (&optional arg)
  11649. "Move subtree up or kill table row.
  11650. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11651. `org-move-item-up' depending on context. See the individual commands
  11652. for more information."
  11653. (interactive "P")
  11654. (cond
  11655. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11656. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11657. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11658. (t (org-shiftcursor-error))))
  11659. (defun org-shiftmetadown (&optional arg)
  11660. "Move subtree down or insert table row.
  11661. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11662. `org-move-item-down', depending on context. See the individual
  11663. commands for more information."
  11664. (interactive "P")
  11665. (cond
  11666. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11667. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11668. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11669. (t (org-shiftcursor-error))))
  11670. (defun org-metaleft (&optional arg)
  11671. "Promote heading or move table column to left.
  11672. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11673. With no specific context, calls the Emacs default `backward-word'.
  11674. See the individual commands for more information."
  11675. (interactive "P")
  11676. (cond
  11677. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11678. ((or (org-on-heading-p) (org-region-active-p))
  11679. (call-interactively 'org-do-promote))
  11680. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11681. (t (call-interactively 'backward-word))))
  11682. (defun org-metaright (&optional arg)
  11683. "Demote subtree or move table column to right.
  11684. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11685. With no specific context, calls the Emacs default `forward-word'.
  11686. See the individual commands for more information."
  11687. (interactive "P")
  11688. (cond
  11689. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11690. ((or (org-on-heading-p) (org-region-active-p))
  11691. (call-interactively 'org-do-demote))
  11692. ((org-at-item-p) (call-interactively 'org-indent-item))
  11693. (t (call-interactively 'forward-word))))
  11694. (defun org-metaup (&optional arg)
  11695. "Move subtree up or move table row up.
  11696. Calls `org-move-subtree-up' or `org-table-move-row' or
  11697. `org-move-item-up', depending on context. See the individual commands
  11698. for more information."
  11699. (interactive "P")
  11700. (cond
  11701. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11702. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11703. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11704. (t (transpose-lines 1) (beginning-of-line -1))))
  11705. (defun org-metadown (&optional arg)
  11706. "Move subtree down or move table row down.
  11707. Calls `org-move-subtree-down' or `org-table-move-row' or
  11708. `org-move-item-down', depending on context. See the individual
  11709. commands for more information."
  11710. (interactive "P")
  11711. (cond
  11712. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11713. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11714. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11715. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11716. (defun org-shiftup (&optional arg)
  11717. "Increase item in timestamp or increase priority of current headline.
  11718. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11719. depending on context. See the individual commands for more information."
  11720. (interactive "P")
  11721. (cond
  11722. ((org-at-timestamp-p t)
  11723. (call-interactively (if org-edit-timestamp-down-means-later
  11724. 'org-timestamp-down 'org-timestamp-up)))
  11725. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11726. ((org-at-item-p) (call-interactively 'org-previous-item))
  11727. ((org-clocktable-try-shift 'up arg))
  11728. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11729. (defun org-shiftdown (&optional arg)
  11730. "Decrease item in timestamp or decrease priority of current headline.
  11731. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11732. depending on context. See the individual commands for more information."
  11733. (interactive "P")
  11734. (cond
  11735. ((org-at-timestamp-p t)
  11736. (call-interactively (if org-edit-timestamp-down-means-later
  11737. 'org-timestamp-up 'org-timestamp-down)))
  11738. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11739. ((org-clocktable-try-shift 'down arg))
  11740. (t (call-interactively 'org-next-item))))
  11741. (defun org-shiftright (&optional arg)
  11742. "Next TODO keyword or timestamp one day later, depending on context."
  11743. (interactive "P")
  11744. (cond
  11745. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11746. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11747. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11748. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11749. ((org-clocktable-try-shift 'right arg))
  11750. (t (org-shiftcursor-error))))
  11751. (defun org-shiftleft (&optional arg)
  11752. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11753. (interactive "P")
  11754. (cond
  11755. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11756. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11757. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11758. ((org-at-property-p)
  11759. (call-interactively 'org-property-previous-allowed-value))
  11760. ((org-clocktable-try-shift 'left arg))
  11761. (t (org-shiftcursor-error))))
  11762. (defun org-shiftcontrolright ()
  11763. "Switch to next TODO set."
  11764. (interactive)
  11765. (cond
  11766. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11767. (t (org-shiftcursor-error))))
  11768. (defun org-shiftcontrolleft ()
  11769. "Switch to previous TODO set."
  11770. (interactive)
  11771. (cond
  11772. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11773. (t (org-shiftcursor-error))))
  11774. (defun org-ctrl-c-ret ()
  11775. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11776. (interactive)
  11777. (cond
  11778. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11779. (t (call-interactively 'org-insert-heading))))
  11780. (defun org-copy-special ()
  11781. "Copy region in table or copy current subtree.
  11782. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11783. See the individual commands for more information."
  11784. (interactive)
  11785. (call-interactively
  11786. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11787. (defun org-cut-special ()
  11788. "Cut region in table or cut current subtree.
  11789. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11790. See the individual commands for more information."
  11791. (interactive)
  11792. (call-interactively
  11793. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11794. (defun org-paste-special (arg)
  11795. "Paste rectangular region into table, or past subtree relative to level.
  11796. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11797. See the individual commands for more information."
  11798. (interactive "P")
  11799. (if (org-at-table-p)
  11800. (org-table-paste-rectangle)
  11801. (org-paste-subtree arg)))
  11802. (defun org-edit-special ()
  11803. "Call a special editor for the stuff at point.
  11804. When at a table, call the formula editor with `org-table-edit-formulas'.
  11805. When at the first line of an src example, call `org-edit-src-code'.
  11806. When in an #+include line, visit the include file. Otherwise call
  11807. `ffap' to visit the file at point."
  11808. (interactive)
  11809. (cond
  11810. ((org-at-table-p)
  11811. (call-interactively 'org-table-edit-formulas))
  11812. ((save-excursion
  11813. (beginning-of-line 1)
  11814. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11815. (find-file (org-trim (match-string 1))))
  11816. ((org-edit-src-code))
  11817. (t (call-interactively 'ffap))))
  11818. (defun org-ctrl-c-ctrl-c (&optional arg)
  11819. "Set tags in headline, or update according to changed information at point.
  11820. This command does many different things, depending on context:
  11821. - If the cursor is in a headline, prompt for tags and insert them
  11822. into the current line, aligned to `org-tags-column'. When called
  11823. with prefix arg, realign all tags in the current buffer.
  11824. - If the cursor is in one of the special #+KEYWORD lines, this
  11825. triggers scanning the buffer for these lines and updating the
  11826. information.
  11827. - If the cursor is inside a table, realign the table. This command
  11828. works even if the automatic table editor has been turned off.
  11829. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11830. the entire table.
  11831. - If the cursor is a the beginning of a dynamic block, update it.
  11832. - If the cursor is inside a table created by the table.el package,
  11833. activate that table.
  11834. - If the current buffer is a remember buffer, close note and file it.
  11835. with a prefix argument, file it without further interaction to the default
  11836. location.
  11837. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11838. links in this buffer.
  11839. - If the cursor is on a numbered item in a plain list, renumber the
  11840. ordered list.
  11841. - If the cursor is on a checkbox, toggle it."
  11842. (interactive "P")
  11843. (let ((org-enable-table-editor t))
  11844. (cond
  11845. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11846. org-occur-highlights
  11847. org-latex-fragment-image-overlays)
  11848. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11849. (org-remove-occur-highlights)
  11850. (org-remove-latex-fragment-image-overlays)
  11851. (message "Temporary highlights/overlays removed from current buffer"))
  11852. ((and (local-variable-p 'org-finish-function (current-buffer))
  11853. (fboundp org-finish-function))
  11854. (funcall org-finish-function))
  11855. ((org-at-property-p)
  11856. (call-interactively 'org-property-action))
  11857. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11858. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11859. ((org-at-table.el-p)
  11860. (require 'table)
  11861. (beginning-of-line 1)
  11862. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11863. (call-interactively 'table-recognize-table))
  11864. ((org-at-table-p)
  11865. (org-table-maybe-eval-formula)
  11866. (if arg
  11867. (call-interactively 'org-table-recalculate)
  11868. (org-table-maybe-recalculate-line))
  11869. (call-interactively 'org-table-align))
  11870. ((org-at-item-checkbox-p)
  11871. (call-interactively 'org-toggle-checkbox))
  11872. ((org-at-item-p)
  11873. (call-interactively 'org-maybe-renumber-ordered-list))
  11874. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11875. ;; Dynamic block
  11876. (beginning-of-line 1)
  11877. (org-update-dblock))
  11878. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11879. (cond
  11880. ((equal (match-string 1) "TBLFM")
  11881. ;; Recalculate the table before this line
  11882. (save-excursion
  11883. (beginning-of-line 1)
  11884. (skip-chars-backward " \r\n\t")
  11885. (if (org-at-table-p)
  11886. (org-call-with-arg 'org-table-recalculate t))))
  11887. (t
  11888. ; (org-set-regexps-and-options)
  11889. ; (org-restart-font-lock)
  11890. (let ((org-inhibit-startup t)) (org-mode-restart))
  11891. (message "Local setup has been refreshed"))))
  11892. (t (error "C-c C-c can do nothing useful at this location.")))))
  11893. (defun org-mode-restart ()
  11894. "Restart Org-mode, to scan again for special lines.
  11895. Also updates the keyword regular expressions."
  11896. (interactive)
  11897. (org-mode)
  11898. (message "Org-mode restarted"))
  11899. (defun org-kill-note-or-show-branches ()
  11900. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11901. (interactive)
  11902. (if (not org-finish-function)
  11903. (call-interactively 'show-branches)
  11904. (let ((org-note-abort t))
  11905. (funcall org-finish-function))))
  11906. (defun org-return (&optional indent)
  11907. "Goto next table row or insert a newline.
  11908. Calls `org-table-next-row' or `newline', depending on context.
  11909. See the individual commands for more information."
  11910. (interactive)
  11911. (cond
  11912. ((bobp) (if indent (newline-and-indent) (newline)))
  11913. ((and (org-at-heading-p)
  11914. (looking-at
  11915. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11916. (org-show-entry)
  11917. (end-of-line 1)
  11918. (newline))
  11919. ((org-at-table-p)
  11920. (org-table-justify-field-maybe)
  11921. (call-interactively 'org-table-next-row))
  11922. (t (if indent (newline-and-indent) (newline)))))
  11923. (defun org-return-indent ()
  11924. "Goto next table row or insert a newline and indent.
  11925. Calls `org-table-next-row' or `newline-and-indent', depending on
  11926. context. See the individual commands for more information."
  11927. (interactive)
  11928. (org-return t))
  11929. (defun org-ctrl-c-star ()
  11930. "Compute table, or change heading status of lines.
  11931. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11932. depending on context. This will also turn a plain list item or a normal
  11933. line into a subheading."
  11934. (interactive)
  11935. (cond
  11936. ((org-at-table-p)
  11937. (call-interactively 'org-table-recalculate))
  11938. ((org-region-active-p)
  11939. ;; Convert all lines in region to list items
  11940. (call-interactively 'org-toggle-region-headings))
  11941. ((org-on-heading-p)
  11942. (org-toggle-region-headings (point-at-bol)
  11943. (min (1+ (point-at-eol)) (point-max))))
  11944. ((org-at-item-p)
  11945. ;; Convert to heading
  11946. (let ((level (save-match-data
  11947. (save-excursion
  11948. (condition-case nil
  11949. (progn
  11950. (org-back-to-heading t)
  11951. (funcall outline-level))
  11952. (error 0))))))
  11953. (replace-match
  11954. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11955. (t (org-toggle-region-headings (point-at-bol)
  11956. (min (1+ (point-at-eol)) (point-max))))))
  11957. (defun org-ctrl-c-minus ()
  11958. "Insert separator line in table or modify bullet status of line.
  11959. Also turns a plain line or a region of lines into list items.
  11960. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11961. `org-cycle-list-bullet', depending on context."
  11962. (interactive)
  11963. (cond
  11964. ((org-at-table-p)
  11965. (call-interactively 'org-table-insert-hline))
  11966. ((org-on-heading-p)
  11967. ;; Convert to item
  11968. (save-excursion
  11969. (beginning-of-line 1)
  11970. (if (looking-at "\\*+ ")
  11971. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11972. ((org-region-active-p)
  11973. ;; Convert all lines in region to list items
  11974. (call-interactively 'org-toggle-region-items))
  11975. ((org-in-item-p)
  11976. (call-interactively 'org-cycle-list-bullet))
  11977. (t (org-toggle-region-items (point-at-bol)
  11978. (min (1+ (point-at-eol)) (point-max))))))
  11979. (defun org-toggle-region-items (beg end)
  11980. "Convert all lines in region to list items.
  11981. If the first line is already an item, convert all list items in the region
  11982. to normal lines."
  11983. (interactive "r")
  11984. (let (l2 l)
  11985. (save-excursion
  11986. (goto-char end)
  11987. (setq l2 (org-current-line))
  11988. (goto-char beg)
  11989. (beginning-of-line 1)
  11990. (setq l (1- (org-current-line)))
  11991. (if (org-at-item-p)
  11992. ;; We already have items, de-itemize
  11993. (while (< (setq l (1+ l)) l2)
  11994. (when (org-at-item-p)
  11995. (goto-char (match-beginning 2))
  11996. (delete-region (match-beginning 2) (match-end 2))
  11997. (and (looking-at "[ \t]+") (replace-match "")))
  11998. (beginning-of-line 2))
  11999. (while (< (setq l (1+ l)) l2)
  12000. (unless (org-at-item-p)
  12001. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12002. (replace-match "\\1- \\2")))
  12003. (beginning-of-line 2))))))
  12004. (defun org-toggle-region-headings (beg end)
  12005. "Convert all lines in region to list items.
  12006. If the first line is already an item, convert all list items in the region
  12007. to normal lines."
  12008. (interactive "r")
  12009. (let (l2 l)
  12010. (save-excursion
  12011. (goto-char end)
  12012. (setq l2 (org-current-line))
  12013. (goto-char beg)
  12014. (beginning-of-line 1)
  12015. (setq l (1- (org-current-line)))
  12016. (if (org-on-heading-p)
  12017. ;; We already have headlines, de-star them
  12018. (while (< (setq l (1+ l)) l2)
  12019. (when (org-on-heading-p t)
  12020. (and (looking-at outline-regexp) (replace-match "")))
  12021. (beginning-of-line 2))
  12022. (let* ((stars (save-excursion
  12023. (re-search-backward org-complex-heading-regexp nil t)
  12024. (or (match-string 1) "*")))
  12025. (add-stars (if org-odd-levels-only "**" "*"))
  12026. (rpl (concat stars add-stars " \\2")))
  12027. (while (< (setq l (1+ l)) l2)
  12028. (unless (org-on-heading-p)
  12029. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  12030. (replace-match rpl)))
  12031. (beginning-of-line 2)))))))
  12032. (defun org-meta-return (&optional arg)
  12033. "Insert a new heading or wrap a region in a table.
  12034. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  12035. See the individual commands for more information."
  12036. (interactive "P")
  12037. (cond
  12038. ((org-at-table-p)
  12039. (call-interactively 'org-table-wrap-region))
  12040. (t (call-interactively 'org-insert-heading))))
  12041. ;;; Menu entries
  12042. ;; Define the Org-mode menus
  12043. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  12044. '("Tbl"
  12045. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  12046. ["Next Field" org-cycle (org-at-table-p)]
  12047. ["Previous Field" org-shifttab (org-at-table-p)]
  12048. ["Next Row" org-return (org-at-table-p)]
  12049. "--"
  12050. ["Blank Field" org-table-blank-field (org-at-table-p)]
  12051. ["Edit Field" org-table-edit-field (org-at-table-p)]
  12052. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  12053. "--"
  12054. ("Column"
  12055. ["Move Column Left" org-metaleft (org-at-table-p)]
  12056. ["Move Column Right" org-metaright (org-at-table-p)]
  12057. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  12058. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  12059. ("Row"
  12060. ["Move Row Up" org-metaup (org-at-table-p)]
  12061. ["Move Row Down" org-metadown (org-at-table-p)]
  12062. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  12063. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  12064. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  12065. "--"
  12066. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  12067. ("Rectangle"
  12068. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  12069. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  12070. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  12071. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  12072. "--"
  12073. ("Calculate"
  12074. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  12075. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  12076. ["Edit Formulas" org-edit-special (org-at-table-p)]
  12077. "--"
  12078. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  12079. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  12080. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12081. "--"
  12082. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12083. "--"
  12084. ["Sum Column/Rectangle" org-table-sum
  12085. (or (org-at-table-p) (org-region-active-p))]
  12086. ["Which Column?" org-table-current-column (org-at-table-p)])
  12087. ["Debug Formulas"
  12088. org-table-toggle-formula-debugger
  12089. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12090. ["Show Col/Row Numbers"
  12091. org-table-toggle-coordinate-overlays
  12092. :style toggle
  12093. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12094. "--"
  12095. ["Create" org-table-create (and (not (org-at-table-p))
  12096. org-enable-table-editor)]
  12097. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12098. ["Import from File" org-table-import (not (org-at-table-p))]
  12099. ["Export to File" org-table-export (org-at-table-p)]
  12100. "--"
  12101. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12102. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12103. '("Org"
  12104. ("Show/Hide"
  12105. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12106. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12107. ["Sparse Tree..." org-sparse-tree t]
  12108. ["Reveal Context" org-reveal t]
  12109. ["Show All" show-all t]
  12110. "--"
  12111. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12112. "--"
  12113. ["New Heading" org-insert-heading t]
  12114. ("Navigate Headings"
  12115. ["Up" outline-up-heading t]
  12116. ["Next" outline-next-visible-heading t]
  12117. ["Previous" outline-previous-visible-heading t]
  12118. ["Next Same Level" outline-forward-same-level t]
  12119. ["Previous Same Level" outline-backward-same-level t]
  12120. "--"
  12121. ["Jump" org-goto t])
  12122. ("Edit Structure"
  12123. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12124. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12125. "--"
  12126. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12127. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12128. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12129. "--"
  12130. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12131. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12132. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12133. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12134. "--"
  12135. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12136. "--"
  12137. ["Convert to odd levels" org-convert-to-odd-levels t]
  12138. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12139. ("Editing"
  12140. ["Emphasis..." org-emphasize t]
  12141. ["Edit Source Example" org-edit-special t])
  12142. ("Archive"
  12143. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12144. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12145. ; :active t :keys "C-u C-c C-x C-a"]
  12146. ["Sparse trees open ARCHIVE trees"
  12147. (setq org-sparse-tree-open-archived-trees
  12148. (not org-sparse-tree-open-archived-trees))
  12149. :style toggle :selected org-sparse-tree-open-archived-trees]
  12150. ["Cycling opens ARCHIVE trees"
  12151. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12152. :style toggle :selected org-cycle-open-archived-trees]
  12153. "--"
  12154. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12155. ; ["Check and Move Children" (org-archive-subtree '(4))
  12156. ; :active t :keys "C-u C-c C-x C-s"]
  12157. )
  12158. "--"
  12159. ("TODO Lists"
  12160. ["TODO/DONE/-" org-todo t]
  12161. ("Select keyword"
  12162. ["Next keyword" org-shiftright (org-on-heading-p)]
  12163. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12164. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12165. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12166. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12167. ["Show TODO Tree" org-show-todo-tree t]
  12168. ["Global TODO list" org-todo-list t]
  12169. "--"
  12170. ["Set Priority" org-priority t]
  12171. ["Priority Up" org-shiftup t]
  12172. ["Priority Down" org-shiftdown t])
  12173. ("TAGS and Properties"
  12174. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  12175. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12176. "--"
  12177. ["Set property" 'org-set-property t]
  12178. ["Column view of properties" org-columns t]
  12179. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12180. ("Dates and Scheduling"
  12181. ["Timestamp" org-time-stamp t]
  12182. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12183. ("Change Date"
  12184. ["1 Day Later" org-shiftright t]
  12185. ["1 Day Earlier" org-shiftleft t]
  12186. ["1 ... Later" org-shiftup t]
  12187. ["1 ... Earlier" org-shiftdown t])
  12188. ["Compute Time Range" org-evaluate-time-range t]
  12189. ["Schedule Item" org-schedule t]
  12190. ["Deadline" org-deadline t]
  12191. "--"
  12192. ["Custom time format" org-toggle-time-stamp-overlays
  12193. :style radio :selected org-display-custom-times]
  12194. "--"
  12195. ["Goto Calendar" org-goto-calendar t]
  12196. ["Date from Calendar" org-date-from-calendar t])
  12197. ("Logging work"
  12198. ["Clock in" org-clock-in t]
  12199. ["Clock out" org-clock-out t]
  12200. ["Clock cancel" org-clock-cancel t]
  12201. ["Goto running clock" org-clock-goto t]
  12202. ["Display times" org-clock-display t]
  12203. ["Create clock table" org-clock-report t]
  12204. "--"
  12205. ["Record DONE time"
  12206. (progn (setq org-log-done (not org-log-done))
  12207. (message "Switching to %s will %s record a timestamp"
  12208. (car org-done-keywords)
  12209. (if org-log-done "automatically" "not")))
  12210. :style toggle :selected org-log-done])
  12211. "--"
  12212. ["Agenda Command..." org-agenda t]
  12213. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12214. ("File List for Agenda")
  12215. ("Special views current file"
  12216. ["TODO Tree" org-show-todo-tree t]
  12217. ["Check Deadlines" org-check-deadlines t]
  12218. ["Timeline" org-timeline t]
  12219. ["Tags Tree" org-tags-sparse-tree t])
  12220. "--"
  12221. ("Hyperlinks"
  12222. ["Store Link (Global)" org-store-link t]
  12223. ["Insert Link" org-insert-link t]
  12224. ["Follow Link" org-open-at-point t]
  12225. "--"
  12226. ["Next link" org-next-link t]
  12227. ["Previous link" org-previous-link t]
  12228. "--"
  12229. ["Descriptive Links"
  12230. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12231. :style radio
  12232. :selected (member '(org-link) buffer-invisibility-spec)]
  12233. ["Literal Links"
  12234. (progn
  12235. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12236. :style radio
  12237. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12238. "--"
  12239. ["Export/Publish..." org-export t]
  12240. ("LaTeX"
  12241. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12242. :selected org-cdlatex-mode]
  12243. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12244. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12245. ["Modify math symbol" org-cdlatex-math-modify
  12246. (org-inside-LaTeX-fragment-p)]
  12247. ["Export LaTeX fragments as images"
  12248. (if (featurep 'org-exp)
  12249. (setq org-export-with-LaTeX-fragments
  12250. (not org-export-with-LaTeX-fragments))
  12251. (require 'org-exp))
  12252. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12253. org-export-with-LaTeX-fragments)])
  12254. "--"
  12255. ("Documentation"
  12256. ["Show Version" org-version t]
  12257. ["Info Documentation" org-info t])
  12258. ("Customize"
  12259. ["Browse Org Group" org-customize t]
  12260. "--"
  12261. ["Expand This Menu" org-create-customize-menu
  12262. (fboundp 'customize-menu-create)])
  12263. "--"
  12264. ["Refresh setup" org-mode-restart t]
  12265. ))
  12266. (defun org-info (&optional node)
  12267. "Read documentation for Org-mode in the info system.
  12268. With optional NODE, go directly to that node."
  12269. (interactive)
  12270. (info (format "(org)%s" (or node ""))))
  12271. (defun org-install-agenda-files-menu ()
  12272. (let ((bl (buffer-list)))
  12273. (save-excursion
  12274. (while bl
  12275. (set-buffer (pop bl))
  12276. (if (org-mode-p) (setq bl nil)))
  12277. (when (org-mode-p)
  12278. (easy-menu-change
  12279. '("Org") "File List for Agenda"
  12280. (append
  12281. (list
  12282. ["Edit File List" (org-edit-agenda-file-list) t]
  12283. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12284. ["Remove Current File from List" org-remove-file t]
  12285. ["Cycle through agenda files" org-cycle-agenda-files t]
  12286. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12287. "--")
  12288. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12289. ;;;; Documentation
  12290. ;;;###autoload
  12291. (defun org-require-autoloaded-modules ()
  12292. (interactive)
  12293. (mapc 'require
  12294. '(org-agenda org-archive org-clock org-colview
  12295. org-exp org-id org-export-latex org-publish
  12296. org-remember org-table)))
  12297. ;;;###autoload
  12298. (defun org-customize ()
  12299. "Call the customize function with org as argument."
  12300. (interactive)
  12301. (org-load-modules-maybe)
  12302. (org-require-autoloaded-modules)
  12303. (customize-browse 'org))
  12304. (defun org-create-customize-menu ()
  12305. "Create a full customization menu for Org-mode, insert it into the menu."
  12306. (interactive)
  12307. (org-load-modules-maybe)
  12308. (org-require-autoloaded-modules)
  12309. (if (fboundp 'customize-menu-create)
  12310. (progn
  12311. (easy-menu-change
  12312. '("Org") "Customize"
  12313. `(["Browse Org group" org-customize t]
  12314. "--"
  12315. ,(customize-menu-create 'org)
  12316. ["Set" Custom-set t]
  12317. ["Save" Custom-save t]
  12318. ["Reset to Current" Custom-reset-current t]
  12319. ["Reset to Saved" Custom-reset-saved t]
  12320. ["Reset to Standard Settings" Custom-reset-standard t]))
  12321. (message "\"Org\"-menu now contains full customization menu"))
  12322. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12323. ;;;; Miscellaneous stuff
  12324. ;;; Generally useful functions
  12325. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12326. "Display the given MESSAGE as a warning."
  12327. (if (fboundp 'display-warning)
  12328. (display-warning 'org message
  12329. (if (featurep 'xemacs)
  12330. 'warning
  12331. :warning))
  12332. (let ((buf (get-buffer-create "*Org warnings*")))
  12333. (with-current-buffer buf
  12334. (goto-char (point-max))
  12335. (insert "Warning (Org): " message)
  12336. (unless (bolp)
  12337. (newline)))
  12338. (display-buffer buf)
  12339. (sit-for 0))))
  12340. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12341. "Go to MARKER, widen if necesary. When marker is not live, try BOOKMARK."
  12342. (if (and marker (marker-buffer marker)
  12343. (buffer-live-p (marker-buffer marker)))
  12344. (progn
  12345. (switch-to-buffer (marker-buffer marker))
  12346. (if (or (> marker (point-max)) (< marker (point-min)))
  12347. (widen))
  12348. (goto-char marker))
  12349. (if bookmark
  12350. (bookmark-jump bookmark)
  12351. (error "Cannot find location"))))
  12352. (defun org-quote-csv-field (s)
  12353. "Quote field for inclusion in CSV material."
  12354. (if (string-match "[\",]" s)
  12355. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12356. s))
  12357. (defun org-plist-delete (plist property)
  12358. "Delete PROPERTY from PLIST.
  12359. This is in contrast to merely setting it to 0."
  12360. (let (p)
  12361. (while plist
  12362. (if (not (eq property (car plist)))
  12363. (setq p (plist-put p (car plist) (nth 1 plist))))
  12364. (setq plist (cddr plist)))
  12365. p))
  12366. (defun org-force-self-insert (N)
  12367. "Needed to enforce self-insert under remapping."
  12368. (interactive "p")
  12369. (self-insert-command N))
  12370. (defun org-string-width (s)
  12371. "Compute width of string, ignoring invisible characters.
  12372. This ignores character with invisibility property `org-link', and also
  12373. characters with property `org-cwidth', because these will become invisible
  12374. upon the next fontification round."
  12375. (let (b l)
  12376. (when (or (eq t buffer-invisibility-spec)
  12377. (assq 'org-link buffer-invisibility-spec))
  12378. (while (setq b (text-property-any 0 (length s)
  12379. 'invisible 'org-link s))
  12380. (setq s (concat (substring s 0 b)
  12381. (substring s (or (next-single-property-change
  12382. b 'invisible s) (length s)))))))
  12383. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12384. (setq s (concat (substring s 0 b)
  12385. (substring s (or (next-single-property-change
  12386. b 'org-cwidth s) (length s))))))
  12387. (setq l (string-width s) b -1)
  12388. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12389. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12390. l))
  12391. (defun org-base-buffer (buffer)
  12392. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12393. (if (not buffer)
  12394. buffer
  12395. (or (buffer-base-buffer buffer)
  12396. buffer)))
  12397. (defun org-trim (s)
  12398. "Remove whitespace at beginning and end of string."
  12399. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12400. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12401. s)
  12402. (defun org-wrap (string &optional width lines)
  12403. "Wrap string to either a number of lines, or a width in characters.
  12404. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12405. that costs. If there is a word longer than WIDTH, the text is actually
  12406. wrapped to the length of that word.
  12407. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12408. many lines, whatever width that takes.
  12409. The return value is a list of lines, without newlines at the end."
  12410. (let* ((words (org-split-string string "[ \t\n]+"))
  12411. (maxword (apply 'max (mapcar 'org-string-width words)))
  12412. w ll)
  12413. (cond (width
  12414. (org-do-wrap words (max maxword width)))
  12415. (lines
  12416. (setq w maxword)
  12417. (setq ll (org-do-wrap words maxword))
  12418. (if (<= (length ll) lines)
  12419. ll
  12420. (setq ll words)
  12421. (while (> (length ll) lines)
  12422. (setq w (1+ w))
  12423. (setq ll (org-do-wrap words w)))
  12424. ll))
  12425. (t (error "Cannot wrap this")))))
  12426. (defun org-do-wrap (words width)
  12427. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12428. (let (lines line)
  12429. (while words
  12430. (setq line (pop words))
  12431. (while (and words (< (+ (length line) (length (car words))) width))
  12432. (setq line (concat line " " (pop words))))
  12433. (setq lines (push line lines)))
  12434. (nreverse lines)))
  12435. (defun org-split-string (string &optional separators)
  12436. "Splits STRING into substrings at SEPARATORS.
  12437. No empty strings are returned if there are matches at the beginning
  12438. and end of string."
  12439. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12440. (start 0)
  12441. notfirst
  12442. (list nil))
  12443. (while (and (string-match rexp string
  12444. (if (and notfirst
  12445. (= start (match-beginning 0))
  12446. (< start (length string)))
  12447. (1+ start) start))
  12448. (< (match-beginning 0) (length string)))
  12449. (setq notfirst t)
  12450. (or (eq (match-beginning 0) 0)
  12451. (and (eq (match-beginning 0) (match-end 0))
  12452. (eq (match-beginning 0) start))
  12453. (setq list
  12454. (cons (substring string start (match-beginning 0))
  12455. list)))
  12456. (setq start (match-end 0)))
  12457. (or (eq start (length string))
  12458. (setq list
  12459. (cons (substring string start)
  12460. list)))
  12461. (nreverse list)))
  12462. (defun org-context ()
  12463. "Return a list of contexts of the current cursor position.
  12464. If several contexts apply, all are returned.
  12465. Each context entry is a list with a symbol naming the context, and
  12466. two positions indicating start and end of the context. Possible
  12467. contexts are:
  12468. :headline anywhere in a headline
  12469. :headline-stars on the leading stars in a headline
  12470. :todo-keyword on a TODO keyword (including DONE) in a headline
  12471. :tags on the TAGS in a headline
  12472. :priority on the priority cookie in a headline
  12473. :item on the first line of a plain list item
  12474. :item-bullet on the bullet/number of a plain list item
  12475. :checkbox on the checkbox in a plain list item
  12476. :table in an org-mode table
  12477. :table-special on a special filed in a table
  12478. :table-table in a table.el table
  12479. :link on a hyperlink
  12480. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12481. :target on a <<target>>
  12482. :radio-target on a <<<radio-target>>>
  12483. :latex-fragment on a LaTeX fragment
  12484. :latex-preview on a LaTeX fragment with overlayed preview image
  12485. This function expects the position to be visible because it uses font-lock
  12486. faces as a help to recognize the following contexts: :table-special, :link,
  12487. and :keyword."
  12488. (let* ((f (get-text-property (point) 'face))
  12489. (faces (if (listp f) f (list f)))
  12490. (p (point)) clist o)
  12491. ;; First the large context
  12492. (cond
  12493. ((org-on-heading-p t)
  12494. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12495. (when (progn
  12496. (beginning-of-line 1)
  12497. (looking-at org-todo-line-tags-regexp))
  12498. (push (org-point-in-group p 1 :headline-stars) clist)
  12499. (push (org-point-in-group p 2 :todo-keyword) clist)
  12500. (push (org-point-in-group p 4 :tags) clist))
  12501. (goto-char p)
  12502. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12503. (if (looking-at "\\[#[A-Z0-9]\\]")
  12504. (push (org-point-in-group p 0 :priority) clist)))
  12505. ((org-at-item-p)
  12506. (push (org-point-in-group p 2 :item-bullet) clist)
  12507. (push (list :item (point-at-bol)
  12508. (save-excursion (org-end-of-item) (point)))
  12509. clist)
  12510. (and (org-at-item-checkbox-p)
  12511. (push (org-point-in-group p 0 :checkbox) clist)))
  12512. ((org-at-table-p)
  12513. (push (list :table (org-table-begin) (org-table-end)) clist)
  12514. (if (memq 'org-formula faces)
  12515. (push (list :table-special
  12516. (previous-single-property-change p 'face)
  12517. (next-single-property-change p 'face)) clist)))
  12518. ((org-at-table-p 'any)
  12519. (push (list :table-table) clist)))
  12520. (goto-char p)
  12521. ;; Now the small context
  12522. (cond
  12523. ((org-at-timestamp-p)
  12524. (push (org-point-in-group p 0 :timestamp) clist))
  12525. ((memq 'org-link faces)
  12526. (push (list :link
  12527. (previous-single-property-change p 'face)
  12528. (next-single-property-change p 'face)) clist))
  12529. ((memq 'org-special-keyword faces)
  12530. (push (list :keyword
  12531. (previous-single-property-change p 'face)
  12532. (next-single-property-change p 'face)) clist))
  12533. ((org-on-target-p)
  12534. (push (org-point-in-group p 0 :target) clist)
  12535. (goto-char (1- (match-beginning 0)))
  12536. (if (looking-at org-radio-target-regexp)
  12537. (push (org-point-in-group p 0 :radio-target) clist))
  12538. (goto-char p))
  12539. ((setq o (car (delq nil
  12540. (mapcar
  12541. (lambda (x)
  12542. (if (memq x org-latex-fragment-image-overlays) x))
  12543. (org-overlays-at (point))))))
  12544. (push (list :latex-fragment
  12545. (org-overlay-start o) (org-overlay-end o)) clist)
  12546. (push (list :latex-preview
  12547. (org-overlay-start o) (org-overlay-end o)) clist))
  12548. ((org-inside-LaTeX-fragment-p)
  12549. ;; FIXME: positions wrong.
  12550. (push (list :latex-fragment (point) (point)) clist)))
  12551. (setq clist (nreverse (delq nil clist)))
  12552. clist))
  12553. ;; FIXME: Compare with at-regexp-p Do we need both?
  12554. (defun org-in-regexp (re &optional nlines visually)
  12555. "Check if point is inside a match of regexp.
  12556. Normally only the current line is checked, but you can include NLINES extra
  12557. lines both before and after point into the search.
  12558. If VISUALLY is set, require that the cursor is not after the match but
  12559. really on, so that the block visually is on the match."
  12560. (catch 'exit
  12561. (let ((pos (point))
  12562. (eol (point-at-eol (+ 1 (or nlines 0))))
  12563. (inc (if visually 1 0)))
  12564. (save-excursion
  12565. (beginning-of-line (- 1 (or nlines 0)))
  12566. (while (re-search-forward re eol t)
  12567. (if (and (<= (match-beginning 0) pos)
  12568. (>= (+ inc (match-end 0)) pos))
  12569. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12570. (defun org-at-regexp-p (regexp)
  12571. "Is point inside a match of REGEXP in the current line?"
  12572. (catch 'exit
  12573. (save-excursion
  12574. (let ((pos (point)) (end (point-at-eol)))
  12575. (beginning-of-line 1)
  12576. (while (re-search-forward regexp end t)
  12577. (if (and (<= (match-beginning 0) pos)
  12578. (>= (match-end 0) pos))
  12579. (throw 'exit t)))
  12580. nil))))
  12581. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12582. "Call `multi-occur' with buffers for all agenda files."
  12583. (interactive "sOrg-files matching: \np")
  12584. (let* ((files (org-agenda-files))
  12585. (tnames (mapcar 'file-truename files))
  12586. (extra org-agenda-text-search-extra-files)
  12587. f)
  12588. (when (eq (car extra) 'agenda-archives)
  12589. (setq extra (cdr extra))
  12590. (setq files (org-add-archive-files files)))
  12591. (while (setq f (pop extra))
  12592. (unless (member (file-truename f) tnames)
  12593. (add-to-list 'files f 'append)
  12594. (add-to-list 'tnames (file-truename f) 'append)))
  12595. (multi-occur
  12596. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12597. regexp)))
  12598. (if (boundp 'occur-mode-find-occurrence-hook)
  12599. ;; Emacs 23
  12600. (add-hook 'occur-mode-find-occurrence-hook
  12601. (lambda ()
  12602. (when (org-mode-p)
  12603. (org-reveal))))
  12604. ;; Emacs 22
  12605. (defadvice occur-mode-goto-occurrence
  12606. (after org-occur-reveal activate)
  12607. (and (org-mode-p) (org-reveal)))
  12608. (defadvice occur-mode-goto-occurrence-other-window
  12609. (after org-occur-reveal activate)
  12610. (and (org-mode-p) (org-reveal)))
  12611. (defadvice occur-mode-display-occurrence
  12612. (after org-occur-reveal activate)
  12613. (when (org-mode-p)
  12614. (let ((pos (occur-mode-find-occurrence)))
  12615. (with-current-buffer (marker-buffer pos)
  12616. (save-excursion
  12617. (goto-char pos)
  12618. (org-reveal)))))))
  12619. (defun org-uniquify (list)
  12620. "Remove duplicate elements from LIST."
  12621. (let (res)
  12622. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12623. res))
  12624. (defun org-delete-all (elts list)
  12625. "Remove all elements in ELTS from LIST."
  12626. (while elts
  12627. (setq list (delete (pop elts) list)))
  12628. list)
  12629. (defun org-back-over-empty-lines ()
  12630. "Move backwards over witespace, to the beginning of the first empty line.
  12631. Returns the number of empty lines passed."
  12632. (let ((pos (point)))
  12633. (skip-chars-backward " \t\n\r")
  12634. (beginning-of-line 2)
  12635. (goto-char (min (point) pos))
  12636. (count-lines (point) pos)))
  12637. (defun org-skip-whitespace ()
  12638. (skip-chars-forward " \t\n\r"))
  12639. (defun org-point-in-group (point group &optional context)
  12640. "Check if POINT is in match-group GROUP.
  12641. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12642. match. If the match group does ot exist or point is not inside it,
  12643. return nil."
  12644. (and (match-beginning group)
  12645. (>= point (match-beginning group))
  12646. (<= point (match-end group))
  12647. (if context
  12648. (list context (match-beginning group) (match-end group))
  12649. t)))
  12650. (defun org-switch-to-buffer-other-window (&rest args)
  12651. "Switch to buffer in a second window on the current frame.
  12652. In particular, do not allow pop-up frames."
  12653. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12654. special-display-function)
  12655. (apply 'switch-to-buffer-other-window args)))
  12656. (defun org-combine-plists (&rest plists)
  12657. "Create a single property list from all plists in PLISTS.
  12658. The process starts by copying the first list, and then setting properties
  12659. from the other lists. Settings in the last list are the most significant
  12660. ones and overrule settings in the other lists."
  12661. (let ((rtn (copy-sequence (pop plists)))
  12662. p v ls)
  12663. (while plists
  12664. (setq ls (pop plists))
  12665. (while ls
  12666. (setq p (pop ls) v (pop ls))
  12667. (setq rtn (plist-put rtn p v))))
  12668. rtn))
  12669. (defun org-move-line-down (arg)
  12670. "Move the current line down. With prefix argument, move it past ARG lines."
  12671. (interactive "p")
  12672. (let ((col (current-column))
  12673. beg end pos)
  12674. (beginning-of-line 1) (setq beg (point))
  12675. (beginning-of-line 2) (setq end (point))
  12676. (beginning-of-line (+ 1 arg))
  12677. (setq pos (move-marker (make-marker) (point)))
  12678. (insert (delete-and-extract-region beg end))
  12679. (goto-char pos)
  12680. (org-move-to-column col)))
  12681. (defun org-move-line-up (arg)
  12682. "Move the current line up. With prefix argument, move it past ARG lines."
  12683. (interactive "p")
  12684. (let ((col (current-column))
  12685. beg end pos)
  12686. (beginning-of-line 1) (setq beg (point))
  12687. (beginning-of-line 2) (setq end (point))
  12688. (beginning-of-line (- arg))
  12689. (setq pos (move-marker (make-marker) (point)))
  12690. (insert (delete-and-extract-region beg end))
  12691. (goto-char pos)
  12692. (org-move-to-column col)))
  12693. (defun org-replace-escapes (string table)
  12694. "Replace %-escapes in STRING with values in TABLE.
  12695. TABLE is an association list with keys like \"%a\" and string values.
  12696. The sequences in STRING may contain normal field width and padding information,
  12697. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12698. so values can contain further %-escapes if they are define later in TABLE."
  12699. (let ((case-fold-search nil)
  12700. e re rpl)
  12701. (while (setq e (pop table))
  12702. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12703. (while (string-match re string)
  12704. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12705. (cdr e)))
  12706. (setq string (replace-match rpl t t string))))
  12707. string))
  12708. (defun org-sublist (list start end)
  12709. "Return a section of LIST, from START to END.
  12710. Counting starts at 1."
  12711. (let (rtn (c start))
  12712. (setq list (nthcdr (1- start) list))
  12713. (while (and list (<= c end))
  12714. (push (pop list) rtn)
  12715. (setq c (1+ c)))
  12716. (nreverse rtn)))
  12717. (defun org-find-base-buffer-visiting (file)
  12718. "Like `find-buffer-visiting' but alway return the base buffer and
  12719. not an indirect buffer."
  12720. (let ((buf (find-buffer-visiting file)))
  12721. (if buf
  12722. (or (buffer-base-buffer buf) buf)
  12723. nil)))
  12724. (defun org-image-file-name-regexp ()
  12725. "Return regexp matching the file names of images."
  12726. (if (fboundp 'image-file-name-regexp)
  12727. (image-file-name-regexp)
  12728. (let ((image-file-name-extensions
  12729. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12730. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12731. (concat "\\."
  12732. (regexp-opt (nconc (mapcar 'upcase
  12733. image-file-name-extensions)
  12734. image-file-name-extensions)
  12735. t)
  12736. "\\'"))))
  12737. (defun org-file-image-p (file)
  12738. "Return non-nil if FILE is an image."
  12739. (save-match-data
  12740. (string-match (org-image-file-name-regexp) file)))
  12741. (defun org-get-cursor-date ()
  12742. "Return the date at cursor in as a time.
  12743. This works in the calendar and in the agenda, anywhere else it just
  12744. returns the current time."
  12745. (let (date day defd)
  12746. (cond
  12747. ((eq major-mode 'calendar-mode)
  12748. (setq date (calendar-cursor-to-date)
  12749. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12750. ((eq major-mode 'org-agenda-mode)
  12751. (setq day (get-text-property (point) 'day))
  12752. (if day
  12753. (setq date (calendar-gregorian-from-absolute day)
  12754. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12755. (nth 2 date))))))
  12756. (or defd (current-time))))
  12757. (defvar org-agenda-action-marker (make-marker)
  12758. "Marker pointing to the entry for the next agenda action.")
  12759. (defun org-mark-entry-for-agenda-action ()
  12760. "Mark the current entry as target of an agenda action.
  12761. Agenda actions are actions executed from the agenda with the key `k',
  12762. which make use of the date at the cursor."
  12763. (interactive)
  12764. (move-marker org-agenda-action-marker
  12765. (save-excursion (org-back-to-heading t) (point))
  12766. (current-buffer))
  12767. (message
  12768. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12769. ;;; Paragraph filling stuff.
  12770. ;; We want this to be just right, so use the full arsenal.
  12771. (defun org-indent-line-function ()
  12772. "Indent line like previous, but further if previous was headline or item."
  12773. (interactive)
  12774. (let* ((pos (point))
  12775. (itemp (org-at-item-p))
  12776. column bpos bcol tpos tcol bullet btype bullet-type)
  12777. ;; Find the previous relevant line
  12778. (beginning-of-line 1)
  12779. (cond
  12780. ((looking-at "#") (setq column 0))
  12781. ((looking-at "\\*+ ") (setq column 0))
  12782. (t
  12783. (beginning-of-line 0)
  12784. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12785. (beginning-of-line 0))
  12786. (cond
  12787. ((looking-at "\\*+[ \t]+")
  12788. (if (not org-adapt-indentation)
  12789. (setq column 0)
  12790. (goto-char (match-end 0))
  12791. (setq column (current-column))))
  12792. ((org-in-item-p)
  12793. (org-beginning-of-item)
  12794. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12795. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12796. (setq bpos (match-beginning 1) tpos (match-end 0)
  12797. bcol (progn (goto-char bpos) (current-column))
  12798. tcol (progn (goto-char tpos) (current-column))
  12799. bullet (match-string 1)
  12800. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12801. (if (> tcol (+ bcol org-description-max-indent))
  12802. (setq tcol (+ bcol 5)))
  12803. (if (not itemp)
  12804. (setq column tcol)
  12805. (goto-char pos)
  12806. (beginning-of-line 1)
  12807. (if (looking-at "\\S-")
  12808. (progn
  12809. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12810. (setq bullet (match-string 1)
  12811. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12812. (setq column (if (equal btype bullet-type) bcol tcol)))
  12813. (setq column (org-get-indentation)))))
  12814. (t (setq column (org-get-indentation))))))
  12815. (goto-char pos)
  12816. (if (<= (current-column) (current-indentation))
  12817. (org-indent-line-to column)
  12818. (save-excursion (org-indent-line-to column)))
  12819. (setq column (current-column))
  12820. (beginning-of-line 1)
  12821. (if (looking-at
  12822. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12823. (replace-match (concat "\\1" (format org-property-format
  12824. (match-string 2) (match-string 3)))
  12825. t nil))
  12826. (org-move-to-column column)))
  12827. (defun org-set-autofill-regexps ()
  12828. (interactive)
  12829. ;; In the paragraph separator we include headlines, because filling
  12830. ;; text in a line directly attached to a headline would otherwise
  12831. ;; fill the headline as well.
  12832. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12833. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12834. ;; The paragraph starter includes hand-formatted lists.
  12835. (org-set-local 'paragraph-start
  12836. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12837. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12838. ;; But only if the user has not turned off tables or fixed-width regions
  12839. (org-set-local
  12840. 'auto-fill-inhibit-regexp
  12841. (concat "\\*+ \\|#\\+"
  12842. "\\|[ \t]*" org-keyword-time-regexp
  12843. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12844. (concat
  12845. "\\|[ \t]*["
  12846. (if org-enable-table-editor "|" "")
  12847. (if org-enable-fixed-width-editor ":" "")
  12848. "]"))))
  12849. ;; We use our own fill-paragraph function, to make sure that tables
  12850. ;; and fixed-width regions are not wrapped. That function will pass
  12851. ;; through to `fill-paragraph' when appropriate.
  12852. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12853. ; Adaptive filling: To get full control, first make sure that
  12854. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12855. (org-set-local 'adaptive-fill-regexp "\000")
  12856. (org-set-local 'adaptive-fill-function
  12857. 'org-adaptive-fill-function)
  12858. (org-set-local
  12859. 'align-mode-rules-list
  12860. '((org-in-buffer-settings
  12861. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12862. (modes . '(org-mode))))))
  12863. (defun org-fill-paragraph (&optional justify)
  12864. "Re-align a table, pass through to fill-paragraph if no table."
  12865. (let ((table-p (org-at-table-p))
  12866. (table.el-p (org-at-table.el-p)))
  12867. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12868. (save-excursion (goto-char (point-at-bol))
  12869. (looking-at outline-regexp)))
  12870. t) ; skip headlines
  12871. (table.el-p t) ; skip table.el tables
  12872. (table-p (org-table-align) t) ; align org-mode tables
  12873. (t nil)))) ; call paragraph-fill
  12874. ;; For reference, this is the default value of adaptive-fill-regexp
  12875. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12876. (defun org-adaptive-fill-function ()
  12877. "Return a fill prefix for org-mode files.
  12878. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12879. work correctly."
  12880. (cond ((looking-at "#[ \t]+")
  12881. (match-string 0))
  12882. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12883. (save-excursion
  12884. (if (> (match-end 1) (+ (match-beginning 1)
  12885. org-description-max-indent))
  12886. (goto-char (+ (match-beginning 1) 5))
  12887. (goto-char (match-end 0)))
  12888. (make-string (current-column) ?\ )))
  12889. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12890. (save-excursion
  12891. (goto-char (match-end 0))
  12892. (make-string (current-column) ?\ )))
  12893. (t nil)))
  12894. ;;; Other stuff.
  12895. (defun org-toggle-fixed-width-section (arg)
  12896. "Toggle the fixed-width export.
  12897. If there is no active region, the QUOTE keyword at the current headline is
  12898. inserted or removed. When present, it causes the text between this headline
  12899. and the next to be exported as fixed-width text, and unmodified.
  12900. If there is an active region, this command adds or removes a colon as the
  12901. first character of this line. If the first character of a line is a colon,
  12902. this line is also exported in fixed-width font."
  12903. (interactive "P")
  12904. (let* ((cc 0)
  12905. (regionp (org-region-active-p))
  12906. (beg (if regionp (region-beginning) (point)))
  12907. (end (if regionp (region-end)))
  12908. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12909. (case-fold-search nil)
  12910. (re "[ \t]*\\(:\\)")
  12911. off)
  12912. (if regionp
  12913. (save-excursion
  12914. (goto-char beg)
  12915. (setq cc (current-column))
  12916. (beginning-of-line 1)
  12917. (setq off (looking-at re))
  12918. (while (> nlines 0)
  12919. (setq nlines (1- nlines))
  12920. (beginning-of-line 1)
  12921. (cond
  12922. (arg
  12923. (org-move-to-column cc t)
  12924. (insert ":\n")
  12925. (forward-line -1))
  12926. ((and off (looking-at re))
  12927. (replace-match "" t t nil 1))
  12928. ((not off) (org-move-to-column cc t) (insert ":")))
  12929. (forward-line 1)))
  12930. (save-excursion
  12931. (org-back-to-heading)
  12932. (if (looking-at (concat outline-regexp
  12933. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12934. (replace-match "" t t nil 1)
  12935. (if (looking-at outline-regexp)
  12936. (progn
  12937. (goto-char (match-end 0))
  12938. (insert org-quote-string " "))))))))
  12939. ;;;; Functions extending outline functionality
  12940. (defun org-beginning-of-line (&optional arg)
  12941. "Go to the beginning of the current line. If that is invisible, continue
  12942. to a visible line beginning. This makes the function of C-a more intuitive.
  12943. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12944. first attempt, and only move to after the tags when the cursor is already
  12945. beyond the end of the headline."
  12946. (interactive "P")
  12947. (let ((pos (point)) refpos)
  12948. (beginning-of-line 1)
  12949. (if (bobp)
  12950. nil
  12951. (backward-char 1)
  12952. (if (org-invisible-p)
  12953. (while (and (not (bobp)) (org-invisible-p))
  12954. (backward-char 1)
  12955. (beginning-of-line 1))
  12956. (forward-char 1)))
  12957. (when org-special-ctrl-a/e
  12958. (cond
  12959. ((and (looking-at org-complex-heading-regexp)
  12960. (= (char-after (match-end 1)) ?\ ))
  12961. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12962. (point-at-eol)))
  12963. (goto-char
  12964. (if (eq org-special-ctrl-a/e t)
  12965. (cond ((> pos refpos) refpos)
  12966. ((= pos (point)) refpos)
  12967. (t (point)))
  12968. (cond ((> pos (point)) (point))
  12969. ((not (eq last-command this-command)) (point))
  12970. (t refpos)))))
  12971. ((org-at-item-p)
  12972. (goto-char
  12973. (if (eq org-special-ctrl-a/e t)
  12974. (cond ((> pos (match-end 4)) (match-end 4))
  12975. ((= pos (point)) (match-end 4))
  12976. (t (point)))
  12977. (cond ((> pos (point)) (point))
  12978. ((not (eq last-command this-command)) (point))
  12979. (t (match-end 4))))))))
  12980. (org-no-warnings
  12981. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12982. (defun org-end-of-line (&optional arg)
  12983. "Go to the end of the line.
  12984. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12985. first attempt, and only move to after the tags when the cursor is already
  12986. beyond the end of the headline."
  12987. (interactive "P")
  12988. (if (or (not org-special-ctrl-a/e)
  12989. (not (org-on-heading-p)))
  12990. (end-of-line arg)
  12991. (let ((pos (point)))
  12992. (beginning-of-line 1)
  12993. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12994. (if (eq org-special-ctrl-a/e t)
  12995. (if (or (< pos (match-beginning 1))
  12996. (= pos (match-end 0)))
  12997. (goto-char (match-beginning 1))
  12998. (goto-char (match-end 0)))
  12999. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  13000. (goto-char (match-end 0))
  13001. (goto-char (match-beginning 1))))
  13002. (end-of-line arg))))
  13003. (org-no-warnings
  13004. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  13005. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  13006. (define-key org-mode-map "\C-e" 'org-end-of-line)
  13007. (defun org-kill-line (&optional arg)
  13008. "Kill line, to tags or end of line."
  13009. (interactive "P")
  13010. (cond
  13011. ((or (not org-special-ctrl-k)
  13012. (bolp)
  13013. (not (org-on-heading-p)))
  13014. (call-interactively 'kill-line))
  13015. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  13016. (kill-region (point) (match-beginning 1))
  13017. (org-set-tags nil t))
  13018. (t (kill-region (point) (point-at-eol)))))
  13019. (define-key org-mode-map "\C-k" 'org-kill-line)
  13020. (defun org-invisible-p ()
  13021. "Check if point is at a character currently not visible."
  13022. ;; Early versions of noutline don't have `outline-invisible-p'.
  13023. (if (fboundp 'outline-invisible-p)
  13024. (outline-invisible-p)
  13025. (get-char-property (point) 'invisible)))
  13026. (defun org-invisible-p2 ()
  13027. "Check if point is at a character currently not visible."
  13028. (save-excursion
  13029. (if (and (eolp) (not (bobp))) (backward-char 1))
  13030. ;; Early versions of noutline don't have `outline-invisible-p'.
  13031. (if (fboundp 'outline-invisible-p)
  13032. (outline-invisible-p)
  13033. (get-char-property (point) 'invisible))))
  13034. (defalias 'org-back-to-heading 'outline-back-to-heading)
  13035. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13036. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13037. (defun org-at-heading-or-item-p ()
  13038. (or (org-on-heading-p) (org-at-item-p)))
  13039. (defun org-on-target-p ()
  13040. (or (org-in-regexp org-radio-target-regexp)
  13041. (org-in-regexp org-target-regexp)))
  13042. (defun org-up-heading-all (arg)
  13043. "Move to the heading line of which the present line is a subheading.
  13044. This function considers both visible and invisible heading lines.
  13045. With argument, move up ARG levels."
  13046. (if (fboundp 'outline-up-heading-all)
  13047. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13048. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13049. (defun org-up-heading-safe ()
  13050. "Move to the heading line of which the present line is a subheading.
  13051. This version will not throw an error. It will return the level of the
  13052. headline found, or nil if no higher level is found."
  13053. (let ((pos (point)) start-level level
  13054. (re (concat "^" outline-regexp)))
  13055. (catch 'exit
  13056. (outline-back-to-heading t)
  13057. (setq start-level (funcall outline-level))
  13058. (if (equal start-level 1) (throw 'exit nil))
  13059. (while (re-search-backward re nil t)
  13060. (setq level (funcall outline-level))
  13061. (if (< level start-level) (throw 'exit level)))
  13062. nil)))
  13063. (defun org-first-sibling-p ()
  13064. "Is this heading the first child of its parents?"
  13065. (interactive)
  13066. (let ((re (concat "^" outline-regexp))
  13067. level l)
  13068. (unless (org-at-heading-p t)
  13069. (error "Not at a heading"))
  13070. (setq level (funcall outline-level))
  13071. (save-excursion
  13072. (if (not (re-search-backward re nil t))
  13073. t
  13074. (setq l (funcall outline-level))
  13075. (< l level)))))
  13076. (defun org-goto-sibling (&optional previous)
  13077. "Goto the next sibling, even if it is invisible.
  13078. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13079. when a sibling was found. When none is found, return nil and don't
  13080. move point."
  13081. (let ((fun (if previous 're-search-backward 're-search-forward))
  13082. (pos (point))
  13083. (re (concat "^" outline-regexp))
  13084. level l)
  13085. (when (condition-case nil (org-back-to-heading t) (error nil))
  13086. (setq level (funcall outline-level))
  13087. (catch 'exit
  13088. (or previous (forward-char 1))
  13089. (while (funcall fun re nil t)
  13090. (setq l (funcall outline-level))
  13091. (when (< l level) (goto-char pos) (throw 'exit nil))
  13092. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13093. (goto-char pos)
  13094. nil))))
  13095. (defun org-show-siblings ()
  13096. "Show all siblings of the current headline."
  13097. (save-excursion
  13098. (while (org-goto-sibling) (org-flag-heading nil)))
  13099. (save-excursion
  13100. (while (org-goto-sibling 'previous)
  13101. (org-flag-heading nil))))
  13102. (defun org-show-hidden-entry ()
  13103. "Show an entry where even the heading is hidden."
  13104. (save-excursion
  13105. (org-show-entry)))
  13106. (defun org-flag-heading (flag &optional entry)
  13107. "Flag the current heading. FLAG non-nil means make invisible.
  13108. When ENTRY is non-nil, show the entire entry."
  13109. (save-excursion
  13110. (org-back-to-heading t)
  13111. ;; Check if we should show the entire entry
  13112. (if entry
  13113. (progn
  13114. (org-show-entry)
  13115. (save-excursion
  13116. (and (outline-next-heading)
  13117. (org-flag-heading nil))))
  13118. (outline-flag-region (max (point-min) (1- (point)))
  13119. (save-excursion (outline-end-of-heading) (point))
  13120. flag))))
  13121. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13122. ;; This is an exact copy of the original function, but it uses
  13123. ;; `org-back-to-heading', to make it work also in invisible
  13124. ;; trees. And is uses an invisible-OK argument.
  13125. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13126. (org-back-to-heading invisible-OK)
  13127. (let ((first t)
  13128. (level (funcall outline-level)))
  13129. (while (and (not (eobp))
  13130. (or first (> (funcall outline-level) level)))
  13131. (setq first nil)
  13132. (outline-next-heading))
  13133. (unless to-heading
  13134. (if (memq (preceding-char) '(?\n ?\^M))
  13135. (progn
  13136. ;; Go to end of line before heading
  13137. (forward-char -1)
  13138. (if (memq (preceding-char) '(?\n ?\^M))
  13139. ;; leave blank line before heading
  13140. (forward-char -1))))))
  13141. (point))
  13142. (defun org-show-subtree ()
  13143. "Show everything after this heading at deeper levels."
  13144. (outline-flag-region
  13145. (point)
  13146. (save-excursion
  13147. (outline-end-of-subtree) (outline-next-heading) (point))
  13148. nil))
  13149. (defun org-show-entry ()
  13150. "Show the body directly following this heading.
  13151. Show the heading too, if it is currently invisible."
  13152. (interactive)
  13153. (save-excursion
  13154. (condition-case nil
  13155. (progn
  13156. (org-back-to-heading t)
  13157. (outline-flag-region
  13158. (max (point-min) (1- (point)))
  13159. (save-excursion
  13160. (re-search-forward
  13161. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13162. (or (match-beginning 1) (point-max)))
  13163. nil))
  13164. (error nil))))
  13165. (defun org-make-options-regexp (kwds)
  13166. "Make a regular expression for keyword lines."
  13167. (concat
  13168. "^"
  13169. "#?[ \t]*\\+\\("
  13170. (mapconcat 'regexp-quote kwds "\\|")
  13171. "\\):[ \t]*"
  13172. "\\(.+\\)"))
  13173. ;; Make isearch reveal the necessary context
  13174. (defun org-isearch-end ()
  13175. "Reveal context after isearch exits."
  13176. (when isearch-success ; only if search was successful
  13177. (if (featurep 'xemacs)
  13178. ;; Under XEmacs, the hook is run in the correct place,
  13179. ;; we directly show the context.
  13180. (org-show-context 'isearch)
  13181. ;; In Emacs the hook runs *before* restoring the overlays.
  13182. ;; So we have to use a one-time post-command-hook to do this.
  13183. ;; (Emacs 22 has a special variable, see function `org-mode')
  13184. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13185. isearch-mode-end-hook-quit)
  13186. ;; Only when the isearch was not quitted.
  13187. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13188. 'append 'local)))))
  13189. (defun org-isearch-post-command ()
  13190. "Remove self from hook, and show context."
  13191. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13192. (org-show-context 'isearch))
  13193. ;;;; Integration with and fixes for other packages
  13194. ;;; Imenu support
  13195. (defvar org-imenu-markers nil
  13196. "All markers currently used by Imenu.")
  13197. (make-variable-buffer-local 'org-imenu-markers)
  13198. (defun org-imenu-new-marker (&optional pos)
  13199. "Return a new marker for use by Imenu, and remember the marker."
  13200. (let ((m (make-marker)))
  13201. (move-marker m (or pos (point)))
  13202. (push m org-imenu-markers)
  13203. m))
  13204. (defun org-imenu-get-tree ()
  13205. "Produce the index for Imenu."
  13206. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13207. (setq org-imenu-markers nil)
  13208. (let* ((n org-imenu-depth)
  13209. (re (concat "^" outline-regexp))
  13210. (subs (make-vector (1+ n) nil))
  13211. (last-level 0)
  13212. m tree level head)
  13213. (save-excursion
  13214. (save-restriction
  13215. (widen)
  13216. (goto-char (point-max))
  13217. (while (re-search-backward re nil t)
  13218. (setq level (org-reduced-level (funcall outline-level)))
  13219. (when (<= level n)
  13220. (looking-at org-complex-heading-regexp)
  13221. (setq head (org-match-string-no-properties 4)
  13222. m (org-imenu-new-marker))
  13223. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13224. (if (>= level last-level)
  13225. (push (cons head m) (aref subs level))
  13226. (push (cons head (aref subs (1+ level))) (aref subs level))
  13227. (loop for i from (1+ level) to n do (aset subs i nil)))
  13228. (setq last-level level)))))
  13229. (aref subs 1)))
  13230. (eval-after-load "imenu"
  13231. '(progn
  13232. (add-hook 'imenu-after-jump-hook
  13233. (lambda () (org-show-context 'org-goto)))))
  13234. ;; Speedbar support
  13235. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13236. "Overlay marking the agenda restriction line in speedbar.")
  13237. (org-overlay-put org-speedbar-restriction-lock-overlay
  13238. 'face 'org-agenda-restriction-lock)
  13239. (org-overlay-put org-speedbar-restriction-lock-overlay
  13240. 'help-echo "Agendas are currently limited to this item.")
  13241. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13242. (defun org-speedbar-set-agenda-restriction ()
  13243. "Restrict future agenda commands to the location at point in speedbar.
  13244. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13245. (interactive)
  13246. (require 'org-agenda)
  13247. (let (p m tp np dir txt w)
  13248. (cond
  13249. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13250. 'org-imenu t))
  13251. (setq m (get-text-property p 'org-imenu-marker))
  13252. (save-excursion
  13253. (save-restriction
  13254. (set-buffer (marker-buffer m))
  13255. (goto-char m)
  13256. (org-agenda-set-restriction-lock 'subtree))))
  13257. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13258. 'speedbar-function 'speedbar-find-file))
  13259. (setq tp (previous-single-property-change
  13260. (1+ p) 'speedbar-function)
  13261. np (next-single-property-change
  13262. tp 'speedbar-function)
  13263. dir (speedbar-line-directory)
  13264. txt (buffer-substring-no-properties (or tp (point-min))
  13265. (or np (point-max))))
  13266. (save-excursion
  13267. (save-restriction
  13268. (set-buffer (find-file-noselect
  13269. (let ((default-directory dir))
  13270. (expand-file-name txt))))
  13271. (unless (org-mode-p)
  13272. (error "Cannot restrict to non-Org-mode file"))
  13273. (org-agenda-set-restriction-lock 'file))))
  13274. (t (error "Don't know how to restrict Org-mode's agenda")))
  13275. (org-move-overlay org-speedbar-restriction-lock-overlay
  13276. (point-at-bol) (point-at-eol))
  13277. (setq current-prefix-arg nil)
  13278. (org-agenda-maybe-redo)))
  13279. (eval-after-load "speedbar"
  13280. '(progn
  13281. (speedbar-add-supported-extension ".org")
  13282. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13283. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13284. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13285. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13286. (add-hook 'speedbar-visiting-tag-hook
  13287. (lambda () (org-show-context 'org-goto)))))
  13288. ;;; Fixes and Hacks for problems with other packages
  13289. ;; Make flyspell not check words in links, to not mess up our keymap
  13290. (defun org-mode-flyspell-verify ()
  13291. "Don't let flyspell put overlays at active buttons."
  13292. (not (get-text-property (point) 'keymap)))
  13293. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13294. (eval-after-load "bookmark"
  13295. '(if (boundp 'bookmark-after-jump-hook)
  13296. ;; We can use the hook
  13297. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13298. ;; Hook not available, use advice
  13299. (defadvice bookmark-jump (after org-make-visible activate)
  13300. "Make the position visible."
  13301. (org-bookmark-jump-unhide))))
  13302. (defun org-bookmark-jump-unhide ()
  13303. "Unhide the current position, to show the bookmark location."
  13304. (and (org-mode-p)
  13305. (or (org-invisible-p)
  13306. (save-excursion (goto-char (max (point-min) (1- (point))))
  13307. (org-invisible-p)))
  13308. (org-show-context 'bookmark-jump)))
  13309. ;; Make session.el ignore our circular variable
  13310. (eval-after-load "session"
  13311. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13312. ;;;; Experimental code
  13313. (defun org-closed-in-range ()
  13314. "Sparse tree of items closed in a certain time range.
  13315. Still experimental, may disappear in the future."
  13316. (interactive)
  13317. ;; Get the time interval from the user.
  13318. (let* ((time1 (time-to-seconds
  13319. (org-read-date nil 'to-time nil "Starting date: ")))
  13320. (time2 (time-to-seconds
  13321. (org-read-date nil 'to-time nil "End date:")))
  13322. ;; callback function
  13323. (callback (lambda ()
  13324. (let ((time
  13325. (time-to-seconds
  13326. (apply 'encode-time
  13327. (org-parse-time-string
  13328. (match-string 1))))))
  13329. ;; check if time in interval
  13330. (and (>= time time1) (<= time time2))))))
  13331. ;; make tree, check each match with the callback
  13332. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13333. ;;;; Finish up
  13334. (provide 'org)
  13335. (run-hooks 'org-load-hook)
  13336. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13337. ;;; org.el ends here