org.el 512 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954
  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.03pre01
  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.03pre01"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (when (org-bound-and-true-p org-modules)
  124. (let ((a (member 'org-infojs org-modules)))
  125. (and a (setcar a 'org-jsinfo))))
  126. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-wl)
  127. "Modules that should always be loaded together with org.el.
  128. If a description starts with <C>, the file is not part of Emacs
  129. and loading it will require that you have downloaded and properly installed
  130. the org-mode distribution.
  131. You can also use this system to load external packages (i.e. neither Org
  132. core modules, not modules from the CONTRIB directory). Just add symbols
  133. to the end of the list. If the package is called org-xyz.el, then you need
  134. to add the symbol `xyz', and the package must have a call to
  135. (provide 'org-xyz)"
  136. :group 'org
  137. :set 'org-set-modules
  138. :type
  139. '(set :greedy t
  140. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  141. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  142. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  143. (const :tag " info: Links to Info nodes" org-info)
  144. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  145. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  146. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  147. (const :tag " mew Links to Mew folders/messages" org-mew)
  148. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  149. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  150. (const :tag " vm: Links to VM folders/messages" org-vm)
  151. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  152. (const :tag " mouse: Additional mouse support" org-mouse)
  153. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  154. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  155. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  156. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  157. (const :tag "C eval: Include command output as text" org-eval)
  158. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  159. (const :tag "C id: Global id's for identifying entries" org-id)
  160. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  161. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  162. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  163. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  164. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  165. (const :tag "C registry: A registry for Org links" org-registry)
  166. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  167. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  168. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  169. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  170. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  171. (defgroup org-startup nil
  172. "Options concerning startup of Org-mode."
  173. :tag "Org Startup"
  174. :group 'org)
  175. (defcustom org-startup-folded t
  176. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  177. This can also be configured on a per-file basis by adding one of
  178. the following lines anywhere in the buffer:
  179. #+STARTUP: fold
  180. #+STARTUP: nofold
  181. #+STARTUP: content"
  182. :group 'org-startup
  183. :type '(choice
  184. (const :tag "nofold: show all" nil)
  185. (const :tag "fold: overview" t)
  186. (const :tag "content: all headlines" content)))
  187. (defcustom org-startup-truncated t
  188. "Non-nil means, entering Org-mode will set `truncate-lines'.
  189. This is useful since some lines containing links can be very long and
  190. uninteresting. Also tables look terrible when wrapped."
  191. :group 'org-startup
  192. :type 'boolean)
  193. (defcustom org-startup-align-all-tables nil
  194. "Non-nil means, align all tables when visiting a file.
  195. This is useful when the column width in tables is forced with <N> cookies
  196. in table fields. Such tables will look correct only after the first re-align.
  197. This can also be configured on a per-file basis by adding one of
  198. the following lines anywhere in the buffer:
  199. #+STARTUP: align
  200. #+STARTUP: noalign"
  201. :group 'org-startup
  202. :type 'boolean)
  203. (defcustom org-insert-mode-line-in-empty-file nil
  204. "Non-nil means insert the first line setting Org-mode in empty files.
  205. When the function `org-mode' is called interactively in an empty file, this
  206. normally means that the file name does not automatically trigger Org-mode.
  207. To ensure that the file will always be in Org-mode in the future, a
  208. line enforcing Org-mode will be inserted into the buffer, if this option
  209. has been set."
  210. :group 'org-startup
  211. :type 'boolean)
  212. (defcustom org-replace-disputed-keys nil
  213. "Non-nil means use alternative key bindings for some keys.
  214. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  215. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  216. If you want to use Org-mode together with one of these other modes,
  217. or more generally if you would like to move some Org-mode commands to
  218. other keys, set this variable and configure the keys with the variable
  219. `org-disputed-keys'.
  220. This option is only relevant at load-time of Org-mode, and must be set
  221. *before* org.el is loaded. Changing it requires a restart of Emacs to
  222. become effective."
  223. :group 'org-startup
  224. :type 'boolean)
  225. (if (fboundp 'defvaralias)
  226. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  227. (defcustom org-disputed-keys
  228. '(([(shift up)] . [(meta p)])
  229. ([(shift down)] . [(meta n)])
  230. ([(shift left)] . [(meta -)])
  231. ([(shift right)] . [(meta +)])
  232. ([(control shift right)] . [(meta shift +)])
  233. ([(control shift left)] . [(meta shift -)]))
  234. "Keys for which Org-mode and other modes compete.
  235. This is an alist, cars are the default keys, second element specifies
  236. the alternative to use when `org-replace-disputed-keys' is t.
  237. Keys can be specified in any syntax supported by `define-key'.
  238. The value of this option takes effect only at Org-mode's startup,
  239. therefore you'll have to restart Emacs to apply it after changing."
  240. :group 'org-startup
  241. :type 'alist)
  242. (defun org-key (key)
  243. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  244. Or return the original if not disputed."
  245. (if org-replace-disputed-keys
  246. (let* ((nkey (key-description key))
  247. (x (org-find-if (lambda (x)
  248. (equal (key-description (car x)) nkey))
  249. org-disputed-keys)))
  250. (if x (cdr x) key))
  251. key))
  252. (defun org-find-if (predicate seq)
  253. (catch 'exit
  254. (while seq
  255. (if (funcall predicate (car seq))
  256. (throw 'exit (car seq))
  257. (pop seq)))))
  258. (defun org-defkey (keymap key def)
  259. "Define a key, possibly translated, as returned by `org-key'."
  260. (define-key keymap (org-key key) def))
  261. (defcustom org-ellipsis nil
  262. "The ellipsis to use in the Org-mode outline.
  263. When nil, just use the standard three dots. When a string, use that instead,
  264. When a face, use the standart 3 dots, but with the specified face.
  265. The change affects only Org-mode (which will then use its own display table).
  266. Changing this requires executing `M-x org-mode' in a buffer to become
  267. effective."
  268. :group 'org-startup
  269. :type '(choice (const :tag "Default" nil)
  270. (face :tag "Face" :value org-warning)
  271. (string :tag "String" :value "...#")))
  272. (defvar org-display-table nil
  273. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  274. (defgroup org-keywords nil
  275. "Keywords in Org-mode."
  276. :tag "Org Keywords"
  277. :group 'org)
  278. (defcustom org-deadline-string "DEADLINE:"
  279. "String to mark deadline entries.
  280. A deadline is this string, followed by a time stamp. Should be a word,
  281. terminated by a colon. You can insert a schedule keyword and
  282. a timestamp with \\[org-deadline].
  283. Changes become only effective after restarting Emacs."
  284. :group 'org-keywords
  285. :type 'string)
  286. (defcustom org-scheduled-string "SCHEDULED:"
  287. "String to mark scheduled TODO entries.
  288. A schedule is this string, followed by a time stamp. Should be a word,
  289. terminated by a colon. You can insert a schedule keyword and
  290. a timestamp with \\[org-schedule].
  291. Changes become only effective after restarting Emacs."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-closed-string "CLOSED:"
  295. "String used as the prefix for timestamps logging closing a TODO entry."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-clock-string "CLOCK:"
  299. "String used as prefix for timestamps clocking work hours on an item."
  300. :group 'org-keywords
  301. :type 'string)
  302. (defcustom org-comment-string "COMMENT"
  303. "Entries starting with this keyword will never be exported.
  304. An entry can be toggled between COMMENT and normal with
  305. \\[org-toggle-comment].
  306. Changes become only effective after restarting Emacs."
  307. :group 'org-keywords
  308. :type 'string)
  309. (defcustom org-quote-string "QUOTE"
  310. "Entries starting with this keyword will be exported in fixed-width font.
  311. Quoting applies only to the text in the entry following the headline, and does
  312. not extend beyond the next headline, even if that is lower level.
  313. An entry can be toggled between QUOTE and normal with
  314. \\[org-toggle-fixed-width-section]."
  315. :group 'org-keywords
  316. :type 'string)
  317. (defconst org-repeat-re
  318. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  319. "Regular expression for specifying repeated events.
  320. After a match, group 1 contains the repeat expression.")
  321. (defgroup org-structure nil
  322. "Options concerning the general structure of Org-mode files."
  323. :tag "Org Structure"
  324. :group 'org)
  325. (defgroup org-reveal-location nil
  326. "Options about how to make context of a location visible."
  327. :tag "Org Reveal Location"
  328. :group 'org-structure)
  329. (defconst org-context-choice
  330. '(choice
  331. (const :tag "Always" t)
  332. (const :tag "Never" nil)
  333. (repeat :greedy t :tag "Individual contexts"
  334. (cons
  335. (choice :tag "Context"
  336. (const agenda)
  337. (const org-goto)
  338. (const occur-tree)
  339. (const tags-tree)
  340. (const link-search)
  341. (const mark-goto)
  342. (const bookmark-jump)
  343. (const isearch)
  344. (const default))
  345. (boolean))))
  346. "Contexts for the reveal options.")
  347. (defcustom org-show-hierarchy-above '((default . t))
  348. "Non-nil means, show full hierarchy when revealing a location.
  349. Org-mode often shows locations in an org-mode file which might have
  350. been invisible before. When this is set, the hierarchy of headings
  351. above the exposed location is shown.
  352. Turning this off for example for sparse trees makes them very compact.
  353. Instead of t, this can also be an alist specifying this option for different
  354. contexts. Valid contexts are
  355. agenda when exposing an entry from the agenda
  356. org-goto when using the command `org-goto' on key C-c C-j
  357. occur-tree when using the command `org-occur' on key C-c /
  358. tags-tree when constructing a sparse tree based on tags matches
  359. link-search when exposing search matches associated with a link
  360. mark-goto when exposing the jump goal of a mark
  361. bookmark-jump when exposing a bookmark location
  362. isearch when exiting from an incremental search
  363. default default for all contexts not set explicitly"
  364. :group 'org-reveal-location
  365. :type org-context-choice)
  366. (defcustom org-show-following-heading '((default . nil))
  367. "Non-nil means, show following heading when revealing a location.
  368. Org-mode often shows locations in an org-mode file which might have
  369. been invisible before. When this is set, the heading following the
  370. match is shown.
  371. Turning this off for example for sparse trees makes them very compact,
  372. but makes it harder to edit the location of the match. In such a case,
  373. use the command \\[org-reveal] to show more context.
  374. Instead of t, this can also be an alist specifying this option for different
  375. contexts. See `org-show-hierarchy-above' for valid contexts."
  376. :group 'org-reveal-location
  377. :type org-context-choice)
  378. (defcustom org-show-siblings '((default . nil) (isearch t))
  379. "Non-nil means, show all sibling heading when revealing a location.
  380. Org-mode often shows locations in an org-mode file which might have
  381. been invisible before. When this is set, the sibling of the current entry
  382. heading are all made visible. If `org-show-hierarchy-above' is t,
  383. the same happens on each level of the hierarchy above the current entry.
  384. By default this is on for the isearch context, off for all other contexts.
  385. Turning this off for example for sparse trees makes them very compact,
  386. but makes it harder to edit the location of the match. In such a case,
  387. use the command \\[org-reveal] to show more context.
  388. Instead of t, this can also be an alist specifying this option for different
  389. contexts. See `org-show-hierarchy-above' for valid contexts."
  390. :group 'org-reveal-location
  391. :type org-context-choice)
  392. (defcustom org-show-entry-below '((default . nil))
  393. "Non-nil means, show the entry below a headline when revealing a location.
  394. Org-mode often shows locations in an org-mode file which might have
  395. been invisible before. When this is set, the text below the headline that is
  396. exposed is also shown.
  397. By default this is off for all contexts.
  398. Instead of t, this can also be an alist specifying this option for different
  399. contexts. See `org-show-hierarchy-above' for valid contexts."
  400. :group 'org-reveal-location
  401. :type org-context-choice)
  402. (defcustom org-indirect-buffer-display 'other-window
  403. "How should indirect tree buffers be displayed?
  404. This applies to indirect buffers created with the commands
  405. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  406. Valid values are:
  407. current-window Display in the current window
  408. other-window Just display in another window.
  409. dedicated-frame Create one new frame, and re-use it each time.
  410. new-frame Make a new frame each time. Note that in this case
  411. previously-made indirect buffers are kept, and you need to
  412. kill these buffers yourself."
  413. :group 'org-structure
  414. :group 'org-agenda-windows
  415. :type '(choice
  416. (const :tag "In current window" current-window)
  417. (const :tag "In current frame, other window" other-window)
  418. (const :tag "Each time a new frame" new-frame)
  419. (const :tag "One dedicated frame" dedicated-frame)))
  420. (defgroup org-cycle nil
  421. "Options concerning visibility cycling in Org-mode."
  422. :tag "Org Cycle"
  423. :group 'org-structure)
  424. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  425. "Names of drawers. Drawers are not opened by cycling on the headline above.
  426. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  427. this:
  428. :DRAWERNAME:
  429. .....
  430. :END:
  431. The drawer \"PROPERTIES\" is special for capturing properties through
  432. the property API.
  433. Drawers can be defined on the per-file basis with a line like:
  434. #+DRAWERS: HIDDEN STATE PROPERTIES"
  435. :group 'org-structure
  436. :type '(repeat (string :tag "Drawer Name")))
  437. (defcustom org-cycle-global-at-bob nil
  438. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  439. This makes it possible to do global cycling without having to use S-TAB or
  440. C-u TAB. For this special case to work, the first line of the buffer
  441. must not be a headline - it may be empty ot some other text. When used in
  442. this way, `org-cycle-hook' is disables temporarily, to make sure the
  443. cursor stays at the beginning of the buffer.
  444. When this option is nil, don't do anything special at the beginning
  445. of the buffer."
  446. :group 'org-cycle
  447. :type 'boolean)
  448. (defcustom org-cycle-emulate-tab t
  449. "Where should `org-cycle' emulate TAB.
  450. nil Never
  451. white Only in completely white lines
  452. whitestart Only at the beginning of lines, before the first non-white char
  453. t Everywhere except in headlines
  454. exc-hl-bol Everywhere except at the start of a headline
  455. If TAB is used in a place where it does not emulate TAB, the current subtree
  456. visibility is cycled."
  457. :group 'org-cycle
  458. :type '(choice (const :tag "Never" nil)
  459. (const :tag "Only in completely white lines" white)
  460. (const :tag "Before first char in a line" whitestart)
  461. (const :tag "Everywhere except in headlines" t)
  462. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  463. ))
  464. (defcustom org-cycle-separator-lines 2
  465. "Number of empty lines needed to keep an empty line between collapsed trees.
  466. If you leave an empty line between the end of a subtree and the following
  467. headline, this empty line is hidden when the subtree is folded.
  468. Org-mode will leave (exactly) one empty line visible if the number of
  469. empty lines is equal or larger to the number given in this variable.
  470. So the default 2 means, at least 2 empty lines after the end of a subtree
  471. are needed to produce free space between a collapsed subtree and the
  472. following headline.
  473. Special case: when 0, never leave empty lines in collapsed view."
  474. :group 'org-cycle
  475. :type 'integer)
  476. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  477. org-cycle-hide-drawers
  478. org-cycle-show-empty-lines
  479. org-optimize-window-after-visibility-change)
  480. "Hook that is run after `org-cycle' has changed the buffer visibility.
  481. The function(s) in this hook must accept a single argument which indicates
  482. the new state that was set by the most recent `org-cycle' command. The
  483. argument is a symbol. After a global state change, it can have the values
  484. `overview', `content', or `all'. After a local state change, it can have
  485. the values `folded', `children', or `subtree'."
  486. :group 'org-cycle
  487. :type 'hook)
  488. (defgroup org-edit-structure nil
  489. "Options concerning structure editing in Org-mode."
  490. :tag "Org Edit Structure"
  491. :group 'org-structure)
  492. (defcustom org-odd-levels-only nil
  493. "Non-nil means, skip even levels and only use odd levels for the outline.
  494. This has the effect that two stars are being added/taken away in
  495. promotion/demotion commands. It also influences how levels are
  496. handled by the exporters.
  497. Changing it requires restart of `font-lock-mode' to become effective
  498. for fontification also in regions already fontified.
  499. You may also set this on a per-file basis by adding one of the following
  500. lines to the buffer:
  501. #+STARTUP: odd
  502. #+STARTUP: oddeven"
  503. :group 'org-edit-structure
  504. :group 'org-font-lock
  505. :type 'boolean)
  506. (defcustom org-adapt-indentation t
  507. "Non-nil means, adapt indentation when promoting and demoting.
  508. When this is set and the *entire* text in an entry is indented, the
  509. indentation is increased by one space in a demotion command, and
  510. decreased by one in a promotion command. If any line in the entry
  511. body starts at column 0, indentation is not changed at all."
  512. :group 'org-edit-structure
  513. :type 'boolean)
  514. (defcustom org-special-ctrl-a/e nil
  515. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  516. When t, `C-a' will bring back the cursor to the beginning of the
  517. headline text, i.e. after the stars and after a possible TODO keyword.
  518. In an item, this will be the position after the bullet.
  519. When the cursor is already at that position, another `C-a' will bring
  520. it to the beginning of the line.
  521. `C-e' will jump to the end of the headline, ignoring the presence of tags
  522. in the headline. A second `C-e' will then jump to the true end of the
  523. line, after any tags.
  524. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  525. and only a directly following, identical keypress will bring the cursor
  526. to the special positions."
  527. :group 'org-edit-structure
  528. :type '(choice
  529. (const :tag "off" nil)
  530. (const :tag "after bullet first" t)
  531. (const :tag "border first" reversed)))
  532. (if (fboundp 'defvaralias)
  533. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  534. (defcustom org-special-ctrl-k nil
  535. "Non-nil means `C-k' will behave specially in headlines.
  536. When nil, `C-k' will call the default `kill-line' command.
  537. When t, the following will happen while the cursor is in the headline:
  538. - When the cursor is at the beginning of a headline, kill the entire
  539. line and possible the folded subtree below the line.
  540. - When in the middle of the headline text, kill the headline up to the tags.
  541. - When after the headline text, kill the tags."
  542. :group 'org-edit-structure
  543. :type 'boolean)
  544. (defcustom org-M-RET-may-split-line '((default . t))
  545. "Non-nil means, M-RET will split the line at the cursor position.
  546. When nil, it will go to the end of the line before making a
  547. new line.
  548. You may also set this option in a different way for different
  549. contexts. Valid contexts are:
  550. headline when creating a new headline
  551. item when creating a new item
  552. table in a table field
  553. default the value to be used for all contexts not explicitly
  554. customized"
  555. :group 'org-structure
  556. :group 'org-table
  557. :type '(choice
  558. (const :tag "Always" t)
  559. (const :tag "Never" nil)
  560. (repeat :greedy t :tag "Individual contexts"
  561. (cons
  562. (choice :tag "Context"
  563. (const headline)
  564. (const item)
  565. (const table)
  566. (const default))
  567. (boolean)))))
  568. (defcustom org-blank-before-new-entry '((heading . nil)
  569. (plain-list-item . nil))
  570. "Should `org-insert-heading' leave a blank line before new heading/item?
  571. The value is an alist, with `heading' and `plain-list-item' as car,
  572. and a boolean flag as cdr."
  573. :group 'org-edit-structure
  574. :type '(list
  575. (cons (const heading) (boolean))
  576. (cons (const plain-list-item) (boolean))))
  577. (defcustom org-insert-heading-hook nil
  578. "Hook being run after inserting a new heading."
  579. :group 'org-edit-structure
  580. :type 'hook)
  581. (defcustom org-enable-fixed-width-editor t
  582. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  583. This currently only means, they are never auto-wrapped.
  584. When nil, such lines will be treated like ordinary lines.
  585. See also the QUOTE keyword."
  586. :group 'org-edit-structure
  587. :type 'boolean)
  588. (defcustom org-goto-auto-isearch t
  589. "Non-nil means, typing characters in org-goto starts incremental search."
  590. :group 'org-edit-structure
  591. :type 'boolean)
  592. (defgroup org-sparse-trees nil
  593. "Options concerning sparse trees in Org-mode."
  594. :tag "Org Sparse Trees"
  595. :group 'org-structure)
  596. (defcustom org-highlight-sparse-tree-matches t
  597. "Non-nil means, highlight all matches that define a sparse tree.
  598. The highlights will automatically disappear the next time the buffer is
  599. changed by an edit command."
  600. :group 'org-sparse-trees
  601. :type 'boolean)
  602. (defcustom org-remove-highlights-with-change t
  603. "Non-nil means, any change to the buffer will remove temporary highlights.
  604. Such highlights are created by `org-occur' and `org-clock-display'.
  605. When nil, `C-c C-c needs to be used to get rid of the highlights.
  606. The highlights created by `org-preview-latex-fragment' always need
  607. `C-c C-c' to be removed."
  608. :group 'org-sparse-trees
  609. :group 'org-time
  610. :type 'boolean)
  611. (defcustom org-occur-hook '(org-first-headline-recenter)
  612. "Hook that is run after `org-occur' has constructed a sparse tree.
  613. This can be used to recenter the window to show as much of the structure
  614. as possible."
  615. :group 'org-sparse-trees
  616. :type 'hook)
  617. (defgroup org-plain-lists nil
  618. "Options concerning plain lists in Org-mode."
  619. :tag "Org Plain lists"
  620. :group 'org-structure)
  621. (defcustom org-cycle-include-plain-lists nil
  622. "Non-nil means, include plain lists into visibility cycling.
  623. This means that during cycling, plain list items will *temporarily* be
  624. interpreted as outline headlines with a level given by 1000+i where i is the
  625. indentation of the bullet. In all other operations, plain list items are
  626. not seen as headlines. For example, you cannot assign a TODO keyword to
  627. such an item."
  628. :group 'org-plain-lists
  629. :type 'boolean)
  630. (defcustom org-plain-list-ordered-item-terminator t
  631. "The character that makes a line with leading number an ordered list item.
  632. Valid values are ?. and ?\). To get both terminators, use t. While
  633. ?. may look nicer, it creates the danger that a line with leading
  634. number may be incorrectly interpreted as an item. ?\) therefore is
  635. the safe choice."
  636. :group 'org-plain-lists
  637. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  638. (const :tag "paren like in \"2)\"" ?\))
  639. (const :tab "both" t)))
  640. (defcustom org-empty-line-terminates-plain-lists nil
  641. "Non-nil means, an empty line ends all plain list levels.
  642. When nil, empty lines are part of the preceeding item."
  643. :group 'org-plain-lists
  644. :type 'boolean)
  645. (defcustom org-auto-renumber-ordered-lists t
  646. "Non-nil means, automatically renumber ordered plain lists.
  647. Renumbering happens when the sequence have been changed with
  648. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  649. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  650. :group 'org-plain-lists
  651. :type 'boolean)
  652. (defcustom org-provide-checkbox-statistics t
  653. "Non-nil means, update checkbox statistics after insert and toggle.
  654. When this is set, checkbox statistics is updated each time you either insert
  655. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  656. with \\[org-ctrl-c-ctrl-c\\]."
  657. :group 'org-plain-lists
  658. :type 'boolean)
  659. (defgroup org-imenu-and-speedbar nil
  660. "Options concerning imenu and speedbar in Org-mode."
  661. :tag "Org Imenu and Speedbar"
  662. :group 'org-structure)
  663. (defcustom org-imenu-depth 2
  664. "The maximum level for Imenu access to Org-mode headlines.
  665. This also applied for speedbar access."
  666. :group 'org-imenu-and-speedbar
  667. :type 'number)
  668. (defgroup org-table nil
  669. "Options concerning tables in Org-mode."
  670. :tag "Org Table"
  671. :group 'org)
  672. (defcustom org-enable-table-editor 'optimized
  673. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  674. When nil, such lines will be treated like ordinary lines.
  675. When equal to the symbol `optimized', the table editor will be optimized to
  676. do the following:
  677. - Automatic overwrite mode in front of whitespace in table fields.
  678. This makes the structure of the table stay in tact as long as the edited
  679. field does not exceed the column width.
  680. - Minimize the number of realigns. Normally, the table is aligned each time
  681. TAB or RET are pressed to move to another field. With optimization this
  682. happens only if changes to a field might have changed the column width.
  683. Optimization requires replacing the functions `self-insert-command',
  684. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  685. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  686. very good at guessing when a re-align will be necessary, but you can always
  687. force one with \\[org-ctrl-c-ctrl-c].
  688. If you would like to use the optimized version in Org-mode, but the
  689. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  690. This variable can be used to turn on and off the table editor during a session,
  691. but in order to toggle optimization, a restart is required.
  692. See also the variable `org-table-auto-blank-field'."
  693. :group 'org-table
  694. :type '(choice
  695. (const :tag "off" nil)
  696. (const :tag "on" t)
  697. (const :tag "on, optimized" optimized)))
  698. (defcustom org-table-tab-recognizes-table.el t
  699. "Non-nil means, TAB will automatically notice a table.el table.
  700. When it sees such a table, it moves point into it and - if necessary -
  701. calls `table-recognize-table'."
  702. :group 'org-table-editing
  703. :type 'boolean)
  704. (defgroup org-link nil
  705. "Options concerning links in Org-mode."
  706. :tag "Org Link"
  707. :group 'org)
  708. (defvar org-link-abbrev-alist-local nil
  709. "Buffer-local version of `org-link-abbrev-alist', which see.
  710. The value of this is taken from the #+LINK lines.")
  711. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  712. (defcustom org-link-abbrev-alist nil
  713. "Alist of link abbreviations.
  714. The car of each element is a string, to be replaced at the start of a link.
  715. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  716. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  717. [[linkkey:tag][description]]
  718. If REPLACE is a string, the tag will simply be appended to create the link.
  719. If the string contains \"%s\", the tag will be inserted there.
  720. REPLACE may also be a function that will be called with the tag as the
  721. only argument to create the link, which should be returned as a string.
  722. See the manual for examples."
  723. :group 'org-link
  724. :type 'alist)
  725. (defcustom org-descriptive-links t
  726. "Non-nil means, hide link part and only show description of bracket links.
  727. Bracket links are like [[link][descritpion]]. This variable sets the initial
  728. state in new org-mode buffers. The setting can then be toggled on a
  729. per-buffer basis from the Org->Hyperlinks menu."
  730. :group 'org-link
  731. :type 'boolean)
  732. (defcustom org-link-file-path-type 'adaptive
  733. "How the path name in file links should be stored.
  734. Valid values are:
  735. relative Relative to the current directory, i.e. the directory of the file
  736. into which the link is being inserted.
  737. absolute Absolute path, if possible with ~ for home directory.
  738. noabbrev Absolute path, no abbreviation of home directory.
  739. adaptive Use relative path for files in the current directory and sub-
  740. directories of it. For other files, use an absolute path."
  741. :group 'org-link
  742. :type '(choice
  743. (const relative)
  744. (const absolute)
  745. (const noabbrev)
  746. (const adaptive)))
  747. (defcustom org-activate-links '(bracket angle plain radio tag date)
  748. "Types of links that should be activated in Org-mode files.
  749. This is a list of symbols, each leading to the activation of a certain link
  750. type. In principle, it does not hurt to turn on most link types - there may
  751. be a small gain when turning off unused link types. The types are:
  752. bracket The recommended [[link][description]] or [[link]] links with hiding.
  753. angular Links in angular brackes that may contain whitespace like
  754. <bbdb:Carsten Dominik>.
  755. plain Plain links in normal text, no whitespace, like http://google.com.
  756. radio Text that is matched by a radio target, see manual for details.
  757. tag Tag settings in a headline (link to tag search).
  758. date Time stamps (link to calendar).
  759. Changing this variable requires a restart of Emacs to become effective."
  760. :group 'org-link
  761. :type '(set (const :tag "Double bracket links (new style)" bracket)
  762. (const :tag "Angular bracket links (old style)" angular)
  763. (const :tag "Plain text links" plain)
  764. (const :tag "Radio target matches" radio)
  765. (const :tag "Tags" tag)
  766. (const :tag "Timestamps" date)))
  767. (defcustom org-make-link-description-function nil
  768. "Function to use to generate link descriptions from links. If
  769. nil the link location will be used. This function must take two
  770. parameters; the first is the link and the second the description
  771. org-insert-link has generated, and should return the description
  772. to use."
  773. :group 'org-link
  774. :type 'function)
  775. (defgroup org-link-store nil
  776. "Options concerning storing links in Org-mode."
  777. :tag "Org Store Link"
  778. :group 'org-link)
  779. (defcustom org-email-link-description-format "Email %c: %.30s"
  780. "Format of the description part of a link to an email or usenet message.
  781. The following %-excapes will be replaced by corresponding information:
  782. %F full \"From\" field
  783. %f name, taken from \"From\" field, address if no name
  784. %T full \"To\" field
  785. %t first name in \"To\" field, address if no name
  786. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  787. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  788. %s subject
  789. %m message-id.
  790. You may use normal field width specification between the % and the letter.
  791. This is for example useful to limit the length of the subject.
  792. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  793. :group 'org-link-store
  794. :type 'string)
  795. (defcustom org-from-is-user-regexp
  796. (let (r1 r2)
  797. (when (and user-mail-address (not (string= user-mail-address "")))
  798. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  799. (when (and user-full-name (not (string= user-full-name "")))
  800. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  801. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  802. "Regexp mached against the \"From:\" header of an email or usenet message.
  803. It should match if the message is from the user him/herself."
  804. :group 'org-link-store
  805. :type 'regexp)
  806. (defcustom org-context-in-file-links t
  807. "Non-nil means, file links from `org-store-link' contain context.
  808. A search string will be added to the file name with :: as separator and
  809. used to find the context when the link is activated by the command
  810. `org-open-at-point'.
  811. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  812. negates this setting for the duration of the command."
  813. :group 'org-link-store
  814. :type 'boolean)
  815. (defcustom org-keep-stored-link-after-insertion nil
  816. "Non-nil means, keep link in list for entire session.
  817. The command `org-store-link' adds a link pointing to the current
  818. location to an internal list. These links accumulate during a session.
  819. The command `org-insert-link' can be used to insert links into any
  820. Org-mode file (offering completion for all stored links). When this
  821. option is nil, every link which has been inserted once using \\[org-insert-link]
  822. will be removed from the list, to make completing the unused links
  823. more efficient."
  824. :group 'org-link-store
  825. :type 'boolean)
  826. (defgroup org-link-follow nil
  827. "Options concerning following links in Org-mode."
  828. :tag "Org Follow Link"
  829. :group 'org-link)
  830. (defcustom org-follow-link-hook nil
  831. "Hook that is run after a link has been followed."
  832. :group 'org-link-follow
  833. :type 'hook)
  834. (defcustom org-tab-follows-link nil
  835. "Non-nil means, on links TAB will follow the link.
  836. Needs to be set before org.el is loaded."
  837. :group 'org-link-follow
  838. :type 'boolean)
  839. (defcustom org-return-follows-link nil
  840. "Non-nil means, on links RET will follow the link.
  841. Needs to be set before org.el is loaded."
  842. :group 'org-link-follow
  843. :type 'boolean)
  844. (defcustom org-mouse-1-follows-link
  845. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  846. "Non-nil means, mouse-1 on a link will follow the link.
  847. A longer mouse click will still set point. Does not work on XEmacs.
  848. Needs to be set before org.el is loaded."
  849. :group 'org-link-follow
  850. :type 'boolean)
  851. (defcustom org-mark-ring-length 4
  852. "Number of different positions to be recorded in the ring
  853. Changing this requires a restart of Emacs to work correctly."
  854. :group 'org-link-follow
  855. :type 'interger)
  856. (defcustom org-link-frame-setup
  857. '((vm . vm-visit-folder-other-frame)
  858. (gnus . gnus-other-frame)
  859. (file . find-file-other-window))
  860. "Setup the frame configuration for following links.
  861. When following a link with Emacs, it may often be useful to display
  862. this link in another window or frame. This variable can be used to
  863. set this up for the different types of links.
  864. For VM, use any of
  865. `vm-visit-folder'
  866. `vm-visit-folder-other-frame'
  867. For Gnus, use any of
  868. `gnus'
  869. `gnus-other-frame'
  870. For FILE, use any of
  871. `find-file'
  872. `find-file-other-window'
  873. `find-file-other-frame'
  874. For the calendar, use the variable `calendar-setup'.
  875. For BBDB, it is currently only possible to display the matches in
  876. another window."
  877. :group 'org-link-follow
  878. :type '(list
  879. (cons (const vm)
  880. (choice
  881. (const vm-visit-folder)
  882. (const vm-visit-folder-other-window)
  883. (const vm-visit-folder-other-frame)))
  884. (cons (const gnus)
  885. (choice
  886. (const gnus)
  887. (const gnus-other-frame)))
  888. (cons (const file)
  889. (choice
  890. (const find-file)
  891. (const find-file-other-window)
  892. (const find-file-other-frame)))))
  893. (defcustom org-display-internal-link-with-indirect-buffer nil
  894. "Non-nil means, use indirect buffer to display infile links.
  895. Activating internal links (from one location in a file to another location
  896. in the same file) normally just jumps to the location. When the link is
  897. activated with a C-u prefix (or with mouse-3), the link is displayed in
  898. another window. When this option is set, the other window actually displays
  899. an indirect buffer clone of the current buffer, to avoid any visibility
  900. changes to the current buffer."
  901. :group 'org-link-follow
  902. :type 'boolean)
  903. (defcustom org-open-non-existing-files nil
  904. "Non-nil means, `org-open-file' will open non-existing files.
  905. When nil, an error will be generated."
  906. :group 'org-link-follow
  907. :type 'boolean)
  908. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  909. "Function and arguments to call for following mailto links.
  910. This is a list with the first element being a lisp function, and the
  911. remaining elements being arguments to the function. In string arguments,
  912. %a will be replaced by the address, and %s will be replaced by the subject
  913. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  914. :group 'org-link-follow
  915. :type '(choice
  916. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  917. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  918. (const :tag "message-mail" (message-mail "%a" "%s"))
  919. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  920. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  921. "Non-nil means, ask for confirmation before executing shell links.
  922. Shell links can be dangerous: just think about a link
  923. [[shell:rm -rf ~/*][Google Search]]
  924. This link would show up in your Org-mode document as \"Google Search\",
  925. but really it would remove your entire home directory.
  926. Therefore we advise against setting this variable to nil.
  927. Just change it to `y-or-n-p' of you want to confirm with a
  928. single keystroke rather than having to type \"yes\"."
  929. :group 'org-link-follow
  930. :type '(choice
  931. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  932. (const :tag "with y-or-n (faster)" y-or-n-p)
  933. (const :tag "no confirmation (dangerous)" nil)))
  934. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  935. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  936. Elisp links can be dangerous: just think about a link
  937. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  938. This link would show up in your Org-mode document as \"Google Search\",
  939. but really it would remove your entire home directory.
  940. Therefore we advise against setting this variable to nil.
  941. Just change it to `y-or-n-p' of you want to confirm with a
  942. single keystroke rather than having to type \"yes\"."
  943. :group 'org-link-follow
  944. :type '(choice
  945. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  946. (const :tag "with y-or-n (faster)" y-or-n-p)
  947. (const :tag "no confirmation (dangerous)" nil)))
  948. (defconst org-file-apps-defaults-gnu
  949. '((remote . emacs)
  950. (t . mailcap))
  951. "Default file applications on a UNIX or GNU/Linux system.
  952. See `org-file-apps'.")
  953. (defconst org-file-apps-defaults-macosx
  954. '((remote . emacs)
  955. (t . "open %s")
  956. ("ps" . "gv %s")
  957. ("ps.gz" . "gv %s")
  958. ("eps" . "gv %s")
  959. ("eps.gz" . "gv %s")
  960. ("dvi" . "xdvi %s")
  961. ("fig" . "xfig %s"))
  962. "Default file applications on a MacOS X system.
  963. The system \"open\" is known as a default, but we use X11 applications
  964. for some files for which the OS does not have a good default.
  965. See `org-file-apps'.")
  966. (defconst org-file-apps-defaults-windowsnt
  967. (list
  968. '(remote . emacs)
  969. (cons t
  970. (list (if (featurep 'xemacs)
  971. 'mswindows-shell-execute
  972. 'w32-shell-execute)
  973. "open" 'file)))
  974. "Default file applications on a Windows NT system.
  975. The system \"open\" is used for most files.
  976. See `org-file-apps'.")
  977. (defcustom org-file-apps
  978. '(
  979. ("txt" . emacs)
  980. ("tex" . emacs)
  981. ("ltx" . emacs)
  982. ("org" . emacs)
  983. ("el" . emacs)
  984. ("bib" . emacs)
  985. )
  986. "External applications for opening `file:path' items in a document.
  987. Org-mode uses system defaults for different file types, but
  988. you can use this variable to set the application for a given file
  989. extension. The entries in this list are cons cells where the car identifies
  990. files and the cdr the corresponding command. Possible values for the
  991. file identifier are
  992. \"ext\" A string identifying an extension
  993. `directory' Matches a directory
  994. `remote' Matches a remote file, accessible through tramp or efs.
  995. Remote files most likely should be visited through Emacs
  996. because external applications cannot handle such paths.
  997. t Default for all remaining files
  998. Possible values for the command are:
  999. `emacs' The file will be visited by the current Emacs process.
  1000. `default' Use the default application for this file type.
  1001. string A command to be executed by a shell; %s will be replaced
  1002. by the path to the file.
  1003. sexp A Lisp form which will be evaluated. The file path will
  1004. be available in the Lisp variable `file'.
  1005. For more examples, see the system specific constants
  1006. `org-file-apps-defaults-macosx'
  1007. `org-file-apps-defaults-windowsnt'
  1008. `org-file-apps-defaults-gnu'."
  1009. :group 'org-link-follow
  1010. :type '(repeat
  1011. (cons (choice :value ""
  1012. (string :tag "Extension")
  1013. (const :tag "Default for unrecognized files" t)
  1014. (const :tag "Remote file" remote)
  1015. (const :tag "Links to a directory" directory))
  1016. (choice :value ""
  1017. (const :tag "Visit with Emacs" emacs)
  1018. (const :tag "Use system default" default)
  1019. (string :tag "Command")
  1020. (sexp :tag "Lisp form")))))
  1021. (defgroup org-refile nil
  1022. "Options concerning refiling entries in Org-mode."
  1023. :tag "Org Remember"
  1024. :group 'org)
  1025. (defcustom org-directory "~/org"
  1026. "Directory with org files.
  1027. This directory will be used as default to prompt for org files.
  1028. Used by the hooks for remember.el."
  1029. :group 'org-refile
  1030. :group 'org-remember
  1031. :type 'directory)
  1032. (defcustom org-default-notes-file "~/.notes"
  1033. "Default target for storing notes.
  1034. Used by the hooks for remember.el. This can be a string, or nil to mean
  1035. the value of `remember-data-file'.
  1036. You can set this on a per-template basis with the variable
  1037. `org-remember-templates'."
  1038. :group 'org-refile
  1039. :group 'org-remember
  1040. :type '(choice
  1041. (const :tag "Default from remember-data-file" nil)
  1042. file))
  1043. (defcustom org-goto-interface 'outline
  1044. "The default interface to be used for `org-goto'.
  1045. Allowed vaues are:
  1046. outline The interface shows an outline of the relevant file
  1047. and the correct heading is found by moving through
  1048. the outline or by searching with incremental search.
  1049. outline-path-completion Headlines in the current buffer are offered via
  1050. completion."
  1051. :group 'org-refile
  1052. :type '(choice
  1053. (const :tag "Outline" outline)
  1054. (const :tag "Outline-path-completion" outline-path-completion)))
  1055. (defcustom org-reverse-note-order nil
  1056. "Non-nil means, store new notes at the beginning of a file or entry.
  1057. When nil, new notes will be filed to the end of a file or entry.
  1058. This can also be a list with cons cells of regular expressions that
  1059. are matched against file names, and values."
  1060. :group 'org-remember
  1061. :type '(choice
  1062. (const :tag "Reverse always" t)
  1063. (const :tag "Reverse never" nil)
  1064. (repeat :tag "By file name regexp"
  1065. (cons regexp boolean))))
  1066. (defcustom org-refile-targets nil
  1067. "Targets for refiling entries with \\[org-refile].
  1068. This is list of cons cells. Each cell contains:
  1069. - a specification of the files to be considered, either a list of files,
  1070. or a symbol whose function or variable value will be used to retrieve
  1071. a file name or a list of file names. Nil means, refile to a different
  1072. heading in the current buffer.
  1073. - A specification of how to find candidate refile targets. This may be
  1074. any of
  1075. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1076. This tag has to be present in all target headlines, inheritance will
  1077. not be considered.
  1078. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1079. todo keyword.
  1080. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1081. headlines that are refiling targets.
  1082. - a cons cell (:level . N). Any headline of level N is considered a target.
  1083. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1084. :group 'org-remember
  1085. :type '(repeat
  1086. (cons
  1087. (choice :value org-agenda-files
  1088. (const :tag "All agenda files" org-agenda-files)
  1089. (const :tag "Current buffer" nil)
  1090. (function) (variable) (file))
  1091. (choice :tag "Identify target headline by"
  1092. (cons :tag "Specific tag" (const :tag) (string))
  1093. (cons :tag "TODO keyword" (const :todo) (string))
  1094. (cons :tag "Regular expression" (const :regexp) (regexp))
  1095. (cons :tag "Level number" (const :level) (integer))
  1096. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1097. (defcustom org-refile-use-outline-path nil
  1098. "Non-nil means, provide refile targets as paths.
  1099. So a level 3 headline will be available as level1/level2/level3.
  1100. When the value is `file', also include the file name (without directory)
  1101. into the path. When `full-file-path', include the full file path."
  1102. :group 'org-remember
  1103. :type '(choice
  1104. (const :tag "Not" nil)
  1105. (const :tag "Yes" t)
  1106. (const :tag "Start with file name" file)
  1107. (const :tag "Start with full file path" full-file-path)))
  1108. (defgroup org-todo nil
  1109. "Options concerning TODO items in Org-mode."
  1110. :tag "Org TODO"
  1111. :group 'org)
  1112. (defgroup org-progress nil
  1113. "Options concerning Progress logging in Org-mode."
  1114. :tag "Org Progress"
  1115. :group 'org-time)
  1116. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1117. "List of TODO entry keyword sequences and their interpretation.
  1118. \\<org-mode-map>This is a list of sequences.
  1119. Each sequence starts with a symbol, either `sequence' or `type',
  1120. indicating if the keywords should be interpreted as a sequence of
  1121. action steps, or as different types of TODO items. The first
  1122. keywords are states requiring action - these states will select a headline
  1123. for inclusion into the global TODO list Org-mode produces. If one of
  1124. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1125. signify that no further action is necessary. If \"|\" is not found,
  1126. the last keyword is treated as the only DONE state of the sequence.
  1127. The command \\[org-todo] cycles an entry through these states, and one
  1128. additional state where no keyword is present. For details about this
  1129. cycling, see the manual.
  1130. TODO keywords and interpretation can also be set on a per-file basis with
  1131. the special #+SEQ_TODO and #+TYP_TODO lines.
  1132. Each keyword can optionally specify a character for fast state selection
  1133. \(in combination with the variable `org-use-fast-todo-selection')
  1134. and specifiers for state change logging, using the same syntax
  1135. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1136. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1137. indicates to record a time stamp each time this state is selected.
  1138. Each keyword may also specify if a timestamp or a note should be
  1139. recorded when entering or leaving the state, by adding additional
  1140. characters in the parenthesis after the keyword. This looks like this:
  1141. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1142. record only the time of the state change. With X and Y being either
  1143. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1144. Y when leaving the state if and only if the *target* state does not
  1145. define X. You may omit any of the fast-selection key or X or /Y,
  1146. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1147. For backward compatibility, this variable may also be just a list
  1148. of keywords - in this case the interptetation (sequence or type) will be
  1149. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1150. :group 'org-todo
  1151. :group 'org-keywords
  1152. :type '(choice
  1153. (repeat :tag "Old syntax, just keywords"
  1154. (string :tag "Keyword"))
  1155. (repeat :tag "New syntax"
  1156. (cons
  1157. (choice
  1158. :tag "Interpretation"
  1159. (const :tag "Sequence (cycling hits every state)" sequence)
  1160. (const :tag "Type (cycling directly to DONE)" type))
  1161. (repeat
  1162. (string :tag "Keyword"))))))
  1163. (defvar org-todo-keywords-1 nil
  1164. "All TODO and DONE keywords active in a buffer.")
  1165. (make-variable-buffer-local 'org-todo-keywords-1)
  1166. (defvar org-todo-keywords-for-agenda nil)
  1167. (defvar org-done-keywords-for-agenda nil)
  1168. (defvar org-agenda-contributing-files nil)
  1169. (defvar org-not-done-keywords nil)
  1170. (make-variable-buffer-local 'org-not-done-keywords)
  1171. (defvar org-done-keywords nil)
  1172. (make-variable-buffer-local 'org-done-keywords)
  1173. (defvar org-todo-heads nil)
  1174. (make-variable-buffer-local 'org-todo-heads)
  1175. (defvar org-todo-sets nil)
  1176. (make-variable-buffer-local 'org-todo-sets)
  1177. (defvar org-todo-log-states nil)
  1178. (make-variable-buffer-local 'org-todo-log-states)
  1179. (defvar org-todo-kwd-alist nil)
  1180. (make-variable-buffer-local 'org-todo-kwd-alist)
  1181. (defvar org-todo-key-alist nil)
  1182. (make-variable-buffer-local 'org-todo-key-alist)
  1183. (defvar org-todo-key-trigger nil)
  1184. (make-variable-buffer-local 'org-todo-key-trigger)
  1185. (defcustom org-todo-interpretation 'sequence
  1186. "Controls how TODO keywords are interpreted.
  1187. This variable is in principle obsolete and is only used for
  1188. backward compatibility, if the interpretation of todo keywords is
  1189. not given already in `org-todo-keywords'. See that variable for
  1190. more information."
  1191. :group 'org-todo
  1192. :group 'org-keywords
  1193. :type '(choice (const sequence)
  1194. (const type)))
  1195. (defcustom org-use-fast-todo-selection 'prefix
  1196. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1197. This variable describes if and under what circumstances the cycling
  1198. mechanism for TODO keywords will be replaced by a single-key, direct
  1199. selection scheme.
  1200. When nil, fast selection is never used.
  1201. When the symbol `prefix', it will be used when `org-todo' is called with
  1202. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1203. in an agenda buffer.
  1204. When t, fast selection is used by default. In this case, the prefix
  1205. argument forces cycling instead.
  1206. In all cases, the special interface is only used if access keys have actually
  1207. been assigned by the user, i.e. if keywords in the configuration are followed
  1208. by a letter in parenthesis, like TODO(t)."
  1209. :group 'org-todo
  1210. :type '(choice
  1211. (const :tag "Never" nil)
  1212. (const :tag "By default" t)
  1213. (const :tag "Only with C-u C-c C-t" prefix)))
  1214. (defcustom org-after-todo-state-change-hook nil
  1215. "Hook which is run after the state of a TODO item was changed.
  1216. The new state (a string with a TODO keyword, or nil) is available in the
  1217. Lisp variable `state'."
  1218. :group 'org-todo
  1219. :type 'hook)
  1220. (defcustom org-log-done nil
  1221. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1222. When equal to the list (done), also prompt for a closing note.
  1223. This can also be configured on a per-file basis by adding one of
  1224. the following lines anywhere in the buffer:
  1225. #+STARTUP: logdone
  1226. #+STARTUP: lognotedone
  1227. #+STARTUP: nologdone"
  1228. :group 'org-todo
  1229. :group 'org-progress
  1230. :type '(choice
  1231. (const :tag "No logging" nil)
  1232. (const :tag "Record CLOSED timestamp" time)
  1233. (const :tag "Record CLOSED timestamp with closing note." note)))
  1234. ;; Normalize old uses of org-log-done.
  1235. (cond
  1236. ((eq org-log-done t) (setq org-log-done 'time))
  1237. ((and (listp org-log-done) (memq 'done org-log-done))
  1238. (setq org-log-done 'note)))
  1239. (defcustom org-log-note-clock-out nil
  1240. "Non-nil means, recored a note when clocking out of an item.
  1241. This can also be configured on a per-file basis by adding one of
  1242. the following lines anywhere in the buffer:
  1243. #+STARTUP: lognoteclock-out
  1244. #+STARTUP: nolognoteclock-out"
  1245. :group 'org-todo
  1246. :group 'org-progress
  1247. :type 'boolean)
  1248. (defcustom org-log-done-with-time t
  1249. "Non-nil means, the CLOSED time stamp will contain date and time.
  1250. When nil, only the date will be recorded."
  1251. :group 'org-progress
  1252. :type 'boolean)
  1253. (defcustom org-log-note-headings
  1254. '((done . "CLOSING NOTE %t")
  1255. (state . "State %-12s %t")
  1256. (note . "Note taken on %t")
  1257. (clock-out . ""))
  1258. "Headings for notes added to entries.
  1259. The value is an alist, with the car being a symbol indicating the note
  1260. context, and the cdr is the heading to be used. The heading may also be the
  1261. empty string.
  1262. %t in the heading will be replaced by a time stamp.
  1263. %s will be replaced by the new TODO state, in double quotes.
  1264. %u will be replaced by the user name.
  1265. %U will be replaced by the full user name."
  1266. :group 'org-todo
  1267. :group 'org-progress
  1268. :type '(list :greedy t
  1269. (cons (const :tag "Heading when closing an item" done) string)
  1270. (cons (const :tag
  1271. "Heading when changing todo state (todo sequence only)"
  1272. state) string)
  1273. (cons (const :tag "Heading when just taking a note" note) string)
  1274. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1275. (unless (assq 'note org-log-note-headings)
  1276. (push '(note . "%t") org-log-note-headings))
  1277. (defcustom org-log-states-order-reversed t
  1278. "Non-nil means, the latest state change note will be directly after heading.
  1279. When nil, the notes will be orderer according to time."
  1280. :group 'org-todo
  1281. :group 'org-progress
  1282. :type 'boolean)
  1283. (defcustom org-log-repeat 'time
  1284. "Non-nil means, record moving through the DONE state when triggering repeat.
  1285. An auto-repeating tasks is immediately switched back to TODO when marked
  1286. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1287. the TODO keyword definition, or recording a closing note by setting
  1288. `org-log-done', there will be no record of the task moving through DONE.
  1289. This variable forces taking a note anyway. Possible values are:
  1290. nil Don't force a record
  1291. time Record a time stamp
  1292. note Record a note
  1293. This option can also be set with on a per-file-basis with
  1294. #+STARTUP: logrepeat
  1295. #+STARTUP: lognoterepeat
  1296. #+STARTUP: nologrepeat
  1297. You can have local logging settings for a subtree by setting the LOGGING
  1298. property to one or more of these keywords."
  1299. :group 'org-todo
  1300. :group 'org-progress
  1301. :type '(choice
  1302. (const :tag "Don't force a record" nil)
  1303. (const :tag "Force recording the DONE state" time)
  1304. (const :tag "Force recording a note with the DONE state" note)))
  1305. (defgroup org-priorities nil
  1306. "Priorities in Org-mode."
  1307. :tag "Org Priorities"
  1308. :group 'org-todo)
  1309. (defcustom org-highest-priority ?A
  1310. "The highest priority of TODO items. A character like ?A, ?B etc.
  1311. Must have a smaller ASCII number than `org-lowest-priority'."
  1312. :group 'org-priorities
  1313. :type 'character)
  1314. (defcustom org-lowest-priority ?C
  1315. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1316. Must have a larger ASCII number than `org-highest-priority'."
  1317. :group 'org-priorities
  1318. :type 'character)
  1319. (defcustom org-default-priority ?B
  1320. "The default priority of TODO items.
  1321. This is the priority an item get if no explicit priority is given."
  1322. :group 'org-priorities
  1323. :type 'character)
  1324. (defcustom org-priority-start-cycle-with-default t
  1325. "Non-nil means, start with default priority when starting to cycle.
  1326. When this is nil, the first step in the cycle will be (depending on the
  1327. command used) one higher or lower that the default priority."
  1328. :group 'org-priorities
  1329. :type 'boolean)
  1330. (defgroup org-time nil
  1331. "Options concerning time stamps and deadlines in Org-mode."
  1332. :tag "Org Time"
  1333. :group 'org)
  1334. (defcustom org-insert-labeled-timestamps-at-point nil
  1335. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1336. When nil, these labeled time stamps are forces into the second line of an
  1337. entry, just after the headline. When scheduling from the global TODO list,
  1338. the time stamp will always be forced into the second line."
  1339. :group 'org-time
  1340. :type 'boolean)
  1341. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1342. "Formats for `format-time-string' which are used for time stamps.
  1343. It is not recommended to change this constant.")
  1344. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1345. "Number of minutes to round time stamps to.
  1346. These are two values, the first applies when first creating a time stamp.
  1347. The second applies when changing it with the commands `S-up' and `S-down'.
  1348. When changing the time stamp, this means that it will change in steps
  1349. of N minutes, as given by the second value.
  1350. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1351. numbers should be factors of 60, so for example 5, 10, 15.
  1352. When this is larger than 1, you can still force an exact time-stamp by using
  1353. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1354. and by using a prefix arg to `S-up/down' to specify the exact number
  1355. of minutes to shift."
  1356. :group 'org-time
  1357. :get '(lambda (var) ; Make sure all entries have 5 elements
  1358. (if (integerp (default-value var))
  1359. (list (default-value var) 5)
  1360. (default-value var)))
  1361. :type '(list
  1362. (integer :tag "when inserting times")
  1363. (integer :tag "when modifying times")))
  1364. ;; Normalize old customizations of this variable.
  1365. (when (integerp org-time-stamp-rounding-minutes)
  1366. (setq org-time-stamp-rounding-minutes
  1367. (list org-time-stamp-rounding-minutes
  1368. org-time-stamp-rounding-minutes)))
  1369. (defcustom org-display-custom-times nil
  1370. "Non-nil means, overlay custom formats over all time stamps.
  1371. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1372. To turn this on on a per-file basis, insert anywhere in the file:
  1373. #+STARTUP: customtime"
  1374. :group 'org-time
  1375. :set 'set-default
  1376. :type 'sexp)
  1377. (make-variable-buffer-local 'org-display-custom-times)
  1378. (defcustom org-time-stamp-custom-formats
  1379. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1380. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1381. These are overlayed over the default ISO format if the variable
  1382. `org-display-custom-times' is set. Time like %H:%M should be at the
  1383. end of the second format."
  1384. :group 'org-time
  1385. :type 'sexp)
  1386. (defun org-time-stamp-format (&optional long inactive)
  1387. "Get the right format for a time string."
  1388. (let ((f (if long (cdr org-time-stamp-formats)
  1389. (car org-time-stamp-formats))))
  1390. (if inactive
  1391. (concat "[" (substring f 1 -1) "]")
  1392. f)))
  1393. (defcustom org-deadline-warning-days 14
  1394. "No. of days before expiration during which a deadline becomes active.
  1395. This variable governs the display in sparse trees and in the agenda.
  1396. When 0 or negative, it means use this number (the absolute value of it)
  1397. even if a deadline has a different individual lead time specified."
  1398. :group 'org-time
  1399. :group 'org-agenda-daily/weekly
  1400. :type 'number)
  1401. (defcustom org-read-date-prefer-future t
  1402. "Non-nil means, assume future for incomplete date input from user.
  1403. This affects the following situations:
  1404. 1. The user gives a day, but no month.
  1405. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1406. read this as the third of *next* month. However, if you enter \"17\",
  1407. it will be considered as *this* month.
  1408. 2. The user gives a month but not a year.
  1409. For example, if it is april and you enter \"feb 2\", this will be read
  1410. as feb 2, *next* year. \"May 5\", however, will be this year.
  1411. Currently this does not work for ISO week specifications.
  1412. When this option is nil, the current month and year will always be used
  1413. as defaults."
  1414. :group 'org-time
  1415. :type 'boolean)
  1416. (defcustom org-read-date-display-live t
  1417. "Non-nil means, display current interpretation of date prompt live.
  1418. This display will be in an overlay, in the minibuffer."
  1419. :group 'org-time
  1420. :type 'boolean)
  1421. (defcustom org-read-date-popup-calendar t
  1422. "Non-nil means, pop up a calendar when prompting for a date.
  1423. In the calendar, the date can be selected with mouse-1. However, the
  1424. minibuffer will also be active, and you can simply enter the date as well.
  1425. When nil, only the minibuffer will be available."
  1426. :group 'org-time
  1427. :type 'boolean)
  1428. (if (fboundp 'defvaralias)
  1429. (defvaralias 'org-popup-calendar-for-date-prompt
  1430. 'org-read-date-popup-calendar))
  1431. (defcustom org-extend-today-until 0
  1432. "The hour when your day really ends.
  1433. This has influence for the following applications:
  1434. - When switching the agenda to \"today\". It it is still earlier than
  1435. the time given here, the day recognized as TODAY is actually yesterday.
  1436. - When a date is read from the user and it is still before the time given
  1437. here, the current date and time will be assumed to be yesterday, 23:59.
  1438. FIXME:
  1439. IMPORTANT: This is still a very experimental feature, it may disappear
  1440. again or it may be extended to mean more things."
  1441. :group 'org-time
  1442. :type 'number)
  1443. (defcustom org-edit-timestamp-down-means-later nil
  1444. "Non-nil means, S-down will increase the time in a time stamp.
  1445. When nil, S-up will increase."
  1446. :group 'org-time
  1447. :type 'boolean)
  1448. (defcustom org-calendar-follow-timestamp-change t
  1449. "Non-nil means, make the calendar window follow timestamp changes.
  1450. When a timestamp is modified and the calendar window is visible, it will be
  1451. moved to the new date."
  1452. :group 'org-time
  1453. :type 'boolean)
  1454. (defgroup org-tags nil
  1455. "Options concerning tags in Org-mode."
  1456. :tag "Org Tags"
  1457. :group 'org)
  1458. (defcustom org-tag-alist nil
  1459. "List of tags allowed in Org-mode files.
  1460. When this list is nil, Org-mode will base TAG input on what is already in the
  1461. buffer.
  1462. The value of this variable is an alist, the car of each entry must be a
  1463. keyword as a string, the cdr may be a character that is used to select
  1464. that tag through the fast-tag-selection interface.
  1465. See the manual for details."
  1466. :group 'org-tags
  1467. :type '(repeat
  1468. (choice
  1469. (cons (string :tag "Tag name")
  1470. (character :tag "Access char"))
  1471. (const :tag "Start radio group" (:startgroup))
  1472. (const :tag "End radio group" (:endgroup)))))
  1473. (defcustom org-use-fast-tag-selection 'auto
  1474. "Non-nil means, use fast tag selection scheme.
  1475. This is a special interface to select and deselect tags with single keys.
  1476. When nil, fast selection is never used.
  1477. When the symbol `auto', fast selection is used if and only if selection
  1478. characters for tags have been configured, either through the variable
  1479. `org-tag-alist' or through a #+TAGS line in the buffer.
  1480. When t, fast selection is always used and selection keys are assigned
  1481. automatically if necessary."
  1482. :group 'org-tags
  1483. :type '(choice
  1484. (const :tag "Always" t)
  1485. (const :tag "Never" nil)
  1486. (const :tag "When selection characters are configured" 'auto)))
  1487. (defcustom org-fast-tag-selection-single-key nil
  1488. "Non-nil means, fast tag selection exits after first change.
  1489. When nil, you have to press RET to exit it.
  1490. During fast tag selection, you can toggle this flag with `C-c'.
  1491. This variable can also have the value `expert'. In this case, the window
  1492. displaying the tags menu is not even shown, until you press C-c again."
  1493. :group 'org-tags
  1494. :type '(choice
  1495. (const :tag "No" nil)
  1496. (const :tag "Yes" t)
  1497. (const :tag "Expert" expert)))
  1498. (defvar org-fast-tag-selection-include-todo nil
  1499. "Non-nil means, fast tags selection interface will also offer TODO states.
  1500. This is an undocumented feature, you should not rely on it.")
  1501. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1502. "The column to which tags should be indented in a headline.
  1503. If this number is positive, it specifies the column. If it is negative,
  1504. it means that the tags should be flushright to that column. For example,
  1505. -80 works well for a normal 80 character screen."
  1506. :group 'org-tags
  1507. :type 'integer)
  1508. (defcustom org-auto-align-tags t
  1509. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1510. These operations change the length of a headline and therefore shift
  1511. the tags around. With this options turned on, after each such operation
  1512. the tags are again aligned to `org-tags-column'."
  1513. :group 'org-tags
  1514. :type 'boolean)
  1515. (defcustom org-use-tag-inheritance t
  1516. "Non-nil means, tags in levels apply also for sublevels.
  1517. When nil, only the tags directly given in a specific line apply there.
  1518. If you turn off this option, you very likely want to turn on the
  1519. companion option `org-tags-match-list-sublevels'.
  1520. This may also be a list of tags that should be inherited, or a regexp that
  1521. matches tags that should be inherited."
  1522. :group 'org-tags
  1523. :type '(choice
  1524. (const :tag "Not" nil)
  1525. (const :tag "Always" t)
  1526. (repeat :tag "Specific tags" (string :tag "Tag"))
  1527. (regexp :tag "Tags matched by regexp")))
  1528. (defun org-tag-inherit-p (tag)
  1529. "Check if TAG is one that should be inherited."
  1530. (cond
  1531. ((eq org-use-tag-inheritance t) t)
  1532. ((not org-use-tag-inheritance) nil)
  1533. ((stringp org-use-tag-inheritance)
  1534. (string-match org-use-tag-inheritance tag))
  1535. ((listp org-use-tag-inheritance)
  1536. (member tag org-use-tag-inheritance))
  1537. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1538. (defcustom org-tags-match-list-sublevels nil
  1539. "Non-nil means list also sublevels of headlines matching tag search.
  1540. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1541. the sublevels of a headline matching a tag search often also match
  1542. the same search. Listing all of them can create very long lists.
  1543. Setting this variable to nil causes subtrees of a match to be skipped.
  1544. This option is off by default, because inheritance in on. If you turn
  1545. inheritance off, you very likely want to turn this option on.
  1546. As a special case, if the tag search is restricted to TODO items, the
  1547. value of this variable is ignored and sublevels are always checked, to
  1548. make sure all corresponding TODO items find their way into the list."
  1549. :group 'org-tags
  1550. :type 'boolean)
  1551. (defvar org-tags-history nil
  1552. "History of minibuffer reads for tags.")
  1553. (defvar org-last-tags-completion-table nil
  1554. "The last used completion table for tags.")
  1555. (defvar org-after-tags-change-hook nil
  1556. "Hook that is run after the tags in a line have changed.")
  1557. (defgroup org-properties nil
  1558. "Options concerning properties in Org-mode."
  1559. :tag "Org Properties"
  1560. :group 'org)
  1561. (defcustom org-property-format "%-10s %s"
  1562. "How property key/value pairs should be formatted by `indent-line'.
  1563. When `indent-line' hits a property definition, it will format the line
  1564. according to this format, mainly to make sure that the values are
  1565. lined-up with respect to each other."
  1566. :group 'org-properties
  1567. :type 'string)
  1568. (defcustom org-use-property-inheritance nil
  1569. "Non-nil means, properties apply also for sublevels.
  1570. This setting is chiefly used during property searches. Turning it on can
  1571. cause significant overhead when doing a search, which is why it is not
  1572. on by default.
  1573. When nil, only the properties directly given in the current entry count.
  1574. When t, every property is inherited. The value may also be a list of
  1575. properties that should have inheritance, or a regular expression matching
  1576. properties that should be inherited.
  1577. However, note that some special properties use inheritance under special
  1578. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1579. and the properties ending in \"_ALL\" when they are used as descriptor
  1580. for valid values of a property.
  1581. Note for programmers:
  1582. When querying an entry with `org-entry-get', you can control if inheritance
  1583. should be used. By default, `org-entry-get' looks only at the local
  1584. properties. You can request inheritance by setting the inherit argument
  1585. to t (to force inheritance) or to `selective' (to respect the setting
  1586. in this variable)."
  1587. :group 'org-properties
  1588. :type '(choice
  1589. (const :tag "Not" nil)
  1590. (const :tag "Always" t)
  1591. (repeat :tag "Specific properties" (string :tag "Property"))
  1592. (regexp :tag "Properties matched by regexp")))
  1593. (defun org-property-inherit-p (property)
  1594. "Check if PROPERTY is one that should be inherited."
  1595. (cond
  1596. ((eq org-use-property-inheritance t) t)
  1597. ((not org-use-property-inheritance) nil)
  1598. ((stringp org-use-property-inheritance)
  1599. (string-match org-use-property-inheritance property))
  1600. ((listp org-use-property-inheritance)
  1601. (member property org-use-property-inheritance))
  1602. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1603. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1604. "The default column format, if no other format has been defined.
  1605. This variable can be set on the per-file basis by inserting a line
  1606. #+COLUMNS: %25ITEM ....."
  1607. :group 'org-properties
  1608. :type 'string)
  1609. (defcustom org-effort-property "Effort"
  1610. "The property that is being used to keep track of effort estimates.
  1611. Effort estimates given in this property need to have the format H:MM."
  1612. :group 'org-properties
  1613. :group 'org-progress
  1614. :type '(string :tag "Property"))
  1615. (defconst org-global-properties-fixed
  1616. '(("VISIBILITY_ALL" . "folded children content all"))
  1617. "List of property/value pairs that can be inherited by any entry.
  1618. These are fixed values, for the preset properties.")
  1619. (defcustom org-global-properties nil
  1620. "List of property/value pairs that can be inherited by any entry.
  1621. You can set buffer-local values for this by adding lines like
  1622. #+PROPERTY: NAME VALUE"
  1623. :group 'org-properties
  1624. :type '(repeat
  1625. (cons (string :tag "Property")
  1626. (string :tag "Value"))))
  1627. (defvar org-local-properties nil
  1628. "List of property/value pairs that can be inherited by any entry.
  1629. Valid for the current buffer.
  1630. This variable is populated from #+PROPERTY lines.")
  1631. (defgroup org-agenda nil
  1632. "Options concerning agenda views in Org-mode."
  1633. :tag "Org Agenda"
  1634. :group 'org)
  1635. (defvar org-category nil
  1636. "Variable used by org files to set a category for agenda display.
  1637. Such files should use a file variable to set it, for example
  1638. # -*- mode: org; org-category: \"ELisp\"
  1639. or contain a special line
  1640. #+CATEGORY: ELisp
  1641. If the file does not specify a category, then file's base name
  1642. is used instead.")
  1643. (make-variable-buffer-local 'org-category)
  1644. (defcustom org-agenda-files nil
  1645. "The files to be used for agenda display.
  1646. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1647. \\[org-remove-file]. You can also use customize to edit the list.
  1648. If an entry is a directory, all files in that directory that are matched by
  1649. `org-agenda-file-regexp' will be part of the file list.
  1650. If the value of the variable is not a list but a single file name, then
  1651. the list of agenda files is actually stored and maintained in that file, one
  1652. agenda file per line."
  1653. :group 'org-agenda
  1654. :type '(choice
  1655. (repeat :tag "List of files and directories" file)
  1656. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1657. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1658. "Regular expression to match files for `org-agenda-files'.
  1659. If any element in the list in that variable contains a directory instead
  1660. of a normal file, all files in that directory that are matched by this
  1661. regular expression will be included."
  1662. :group 'org-agenda
  1663. :type 'regexp)
  1664. (defcustom org-agenda-text-search-extra-files nil
  1665. "List of extra files to be searched by text search commands.
  1666. These files will be search in addition to the agenda files by the
  1667. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1668. Note that these files will only be searched for text search commands,
  1669. not for the other agenda views like todo lists, tag searches or the weekly
  1670. agenda. This variable is intended to list notes and possibly archive files
  1671. that should also be searched by these two commands.
  1672. In fact, if the first element in the list is the symbol `agenda-archives',
  1673. than all archive files of all agenda files will be added to the search
  1674. scope."
  1675. :group 'org-agenda
  1676. :type '(set :greedy t
  1677. (const :tag "Agenda Archives" agenda-archives)
  1678. (repeat :inline t (file))))
  1679. (if (fboundp 'defvaralias)
  1680. (defvaralias 'org-agenda-multi-occur-extra-files
  1681. 'org-agenda-text-search-extra-files))
  1682. (defcustom org-agenda-skip-unavailable-files nil
  1683. "t means to just skip non-reachable files in `org-agenda-files'.
  1684. Nil means to remove them, after a query, from the list."
  1685. :group 'org-agenda
  1686. :type 'boolean)
  1687. (defcustom org-calendar-to-agenda-key [?c]
  1688. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1689. The command `org-calendar-goto-agenda' will be bound to this key. The
  1690. default is the character `c' because then `c' can be used to switch back and
  1691. forth between agenda and calendar."
  1692. :group 'org-agenda
  1693. :type 'sexp)
  1694. (eval-after-load "calendar"
  1695. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1696. 'org-calendar-goto-agenda))
  1697. (defgroup org-latex nil
  1698. "Options for embedding LaTeX code into Org-mode."
  1699. :tag "Org LaTeX"
  1700. :group 'org)
  1701. (defcustom org-format-latex-options
  1702. '(:foreground default :background default :scale 1.0
  1703. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1704. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1705. "Options for creating images from LaTeX fragments.
  1706. This is a property list with the following properties:
  1707. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1708. `default' means use the foreground of the default face.
  1709. :background the background color, or \"Transparent\".
  1710. `default' means use the background of the default face.
  1711. :scale a scaling factor for the size of the images.
  1712. :html-foreground, :html-background, :html-scale
  1713. the same numbers for HTML export.
  1714. :matchers a list indicating which matchers should be used to
  1715. find LaTeX fragments. Valid members of this list are:
  1716. \"begin\" find environments
  1717. \"$\" find math expressions surrounded by $...$
  1718. \"$$\" find math expressions surrounded by $$....$$
  1719. \"\\(\" find math expressions surrounded by \\(...\\)
  1720. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1721. :group 'org-latex
  1722. :type 'plist)
  1723. (defcustom org-format-latex-header "\\documentclass{article}
  1724. \\usepackage{fullpage} % do not remove
  1725. \\usepackage{amssymb}
  1726. \\usepackage[usenames]{color}
  1727. \\usepackage{amsmath}
  1728. \\usepackage{latexsym}
  1729. \\usepackage[mathscr]{eucal}
  1730. \\pagestyle{empty} % do not remove"
  1731. "The document header used for processing LaTeX fragments."
  1732. :group 'org-latex
  1733. :type 'string)
  1734. (defgroup org-font-lock nil
  1735. "Font-lock settings for highlighting in Org-mode."
  1736. :tag "Org Font Lock"
  1737. :group 'org)
  1738. (defcustom org-level-color-stars-only nil
  1739. "Non-nil means fontify only the stars in each headline.
  1740. When nil, the entire headline is fontified.
  1741. Changing it requires restart of `font-lock-mode' to become effective
  1742. also in regions already fontified."
  1743. :group 'org-font-lock
  1744. :type 'boolean)
  1745. (defcustom org-hide-leading-stars nil
  1746. "Non-nil means, hide the first N-1 stars in a headline.
  1747. This works by using the face `org-hide' for these stars. This
  1748. face is white for a light background, and black for a dark
  1749. background. You may have to customize the face `org-hide' to
  1750. make this work.
  1751. Changing it requires restart of `font-lock-mode' to become effective
  1752. also in regions already fontified.
  1753. You may also set this on a per-file basis by adding one of the following
  1754. lines to the buffer:
  1755. #+STARTUP: hidestars
  1756. #+STARTUP: showstars"
  1757. :group 'org-font-lock
  1758. :type 'boolean)
  1759. (defcustom org-fontify-done-headline nil
  1760. "Non-nil means, change the face of a headline if it is marked DONE.
  1761. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1762. When this is non-nil, the headline after the keyword is set to the
  1763. `org-headline-done' as an additional indication."
  1764. :group 'org-font-lock
  1765. :type 'boolean)
  1766. (defcustom org-fontify-emphasized-text t
  1767. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1768. Changing this variable requires a restart of Emacs to take effect."
  1769. :group 'org-font-lock
  1770. :type 'boolean)
  1771. (defcustom org-highlight-latex-fragments-and-specials nil
  1772. "Non-nil means, fontify what is treated specially by the exporters."
  1773. :group 'org-font-lock
  1774. :type 'boolean)
  1775. (defcustom org-hide-emphasis-markers nil
  1776. "Non-nil mean font-lock should hide the emphasis marker characters."
  1777. :group 'org-font-lock
  1778. :type 'boolean)
  1779. (defvar org-emph-re nil
  1780. "Regular expression for matching emphasis.")
  1781. (defvar org-verbatim-re nil
  1782. "Regular expression for matching verbatim text.")
  1783. (defvar org-emphasis-regexp-components) ; defined just below
  1784. (defvar org-emphasis-alist) ; defined just below
  1785. (defun org-set-emph-re (var val)
  1786. "Set variable and compute the emphasis regular expression."
  1787. (set var val)
  1788. (when (and (boundp 'org-emphasis-alist)
  1789. (boundp 'org-emphasis-regexp-components)
  1790. org-emphasis-alist org-emphasis-regexp-components)
  1791. (let* ((e org-emphasis-regexp-components)
  1792. (pre (car e))
  1793. (post (nth 1 e))
  1794. (border (nth 2 e))
  1795. (body (nth 3 e))
  1796. (nl (nth 4 e))
  1797. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1798. (body1 (concat body "*?"))
  1799. (markers (mapconcat 'car org-emphasis-alist ""))
  1800. (vmarkers (mapconcat
  1801. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1802. org-emphasis-alist "")))
  1803. ;; make sure special characters appear at the right position in the class
  1804. (if (string-match "\\^" markers)
  1805. (setq markers (concat (replace-match "" t t markers) "^")))
  1806. (if (string-match "-" markers)
  1807. (setq markers (concat (replace-match "" t t markers) "-")))
  1808. (if (string-match "\\^" vmarkers)
  1809. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1810. (if (string-match "-" vmarkers)
  1811. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1812. (if (> nl 0)
  1813. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1814. (int-to-string nl) "\\}")))
  1815. ;; Make the regexp
  1816. (setq org-emph-re
  1817. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1818. "\\("
  1819. "\\([" markers "]\\)"
  1820. "\\("
  1821. "[^" border "]\\|"
  1822. "[^" border (if (and nil stacked) markers) "]"
  1823. body1
  1824. "[^" border (if (and nil stacked) markers) "]"
  1825. "\\)"
  1826. "\\3\\)"
  1827. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1828. (setq org-verbatim-re
  1829. (concat "\\([" pre "]\\|^\\)"
  1830. "\\("
  1831. "\\([" vmarkers "]\\)"
  1832. "\\("
  1833. "[^" border "]\\|"
  1834. "[^" border "]"
  1835. body1
  1836. "[^" border "]"
  1837. "\\)"
  1838. "\\3\\)"
  1839. "\\([" post "]\\|$\\)")))))
  1840. (defcustom org-emphasis-regexp-components
  1841. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1842. "Components used to build the regular expression for emphasis.
  1843. This is a list with 6 entries. Terminology: In an emphasis string
  1844. like \" *strong word* \", we call the initial space PREMATCH, the final
  1845. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1846. and \"trong wor\" is the body. The different components in this variable
  1847. specify what is allowed/forbidden in each part:
  1848. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1849. post Chars allowed as postmatch. End of line will be allowed too.
  1850. border The chars *forbidden* as border characters.
  1851. body-regexp A regexp like \".\" to match a body character. Don't use
  1852. non-shy groups here, and don't allow newline here.
  1853. newline The maximum number of newlines allowed in an emphasis exp.
  1854. Use customize to modify this, or restart Emacs after changing it."
  1855. :group 'org-font-lock
  1856. :set 'org-set-emph-re
  1857. :type '(list
  1858. (sexp :tag "Allowed chars in pre ")
  1859. (sexp :tag "Allowed chars in post ")
  1860. (sexp :tag "Forbidden chars in border ")
  1861. (sexp :tag "Regexp for body ")
  1862. (integer :tag "number of newlines allowed")
  1863. (option (boolean :tag "Stacking (DISABLED) "))))
  1864. (defcustom org-emphasis-alist
  1865. `(("*" bold "<b>" "</b>")
  1866. ("/" italic "<i>" "</i>")
  1867. ("_" underline "<u>" "</u>")
  1868. ("=" org-code "<code>" "</code>" verbatim)
  1869. ("~" org-verbatim "" "" verbatim)
  1870. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1871. "<del>" "</del>")
  1872. )
  1873. "Special syntax for emphasized text.
  1874. Text starting and ending with a special character will be emphasized, for
  1875. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1876. characters, the face to be used by font-lock for highlighting in Org-mode
  1877. Emacs buffers, and the HTML tags to be used for this.
  1878. Use customize to modify this, or restart Emacs after changing it."
  1879. :group 'org-font-lock
  1880. :set 'org-set-emph-re
  1881. :type '(repeat
  1882. (list
  1883. (string :tag "Marker character")
  1884. (choice
  1885. (face :tag "Font-lock-face")
  1886. (plist :tag "Face property list"))
  1887. (string :tag "HTML start tag")
  1888. (string :tag "HTML end tag")
  1889. (option (const verbatim)))))
  1890. ;;; Miscellaneous options
  1891. (defgroup org-completion nil
  1892. "Completion in Org-mode."
  1893. :tag "Org Completion"
  1894. :group 'org)
  1895. (defcustom org-completion-fallback-command 'hippie-expand
  1896. "The expansion command called by \\[org-complete] in normal context.
  1897. Normal means, no org-mode-specific context."
  1898. :group 'org-completion
  1899. :type 'function)
  1900. ;;; Functions and variables from ther packages
  1901. ;; Declared here to avoid compiler warnings
  1902. ;; XEmacs only
  1903. (defvar outline-mode-menu-heading)
  1904. (defvar outline-mode-menu-show)
  1905. (defvar outline-mode-menu-hide)
  1906. (defvar zmacs-regions) ; XEmacs regions
  1907. ;; Emacs only
  1908. (defvar mark-active)
  1909. ;; Various packages
  1910. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  1911. (declare-function calendar-forward-day "cal-move" (arg))
  1912. (declare-function calendar-goto-date "cal-move" (date))
  1913. (declare-function calendar-goto-today "cal-move" ())
  1914. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  1915. (defvar calc-embedded-close-formula)
  1916. (defvar calc-embedded-open-formula)
  1917. (declare-function cdlatex-tab "ext:cdlatex" ())
  1918. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1919. (defvar font-lock-unfontify-region-function)
  1920. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1921. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1922. (defvar iswitchb-temp-buflist)
  1923. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1924. (declare-function org-agenda-skip "org-agenda" ())
  1925. (declare-function org-format-agenda-item "org-agenda"
  1926. (extra txt &optional category tags dotime noprefix remove-re))
  1927. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1928. (declare-function org-agenda-change-all-lines "org-agenda"
  1929. (newhead hdmarker &optional fixface))
  1930. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1931. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1932. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1933. (beg end))
  1934. (declare-function parse-time-string "parse-time" (string))
  1935. (declare-function remember "remember" (&optional initial))
  1936. (declare-function remember-buffer-desc "remember" ())
  1937. (declare-function remember-finalize "remember" ())
  1938. (defvar remember-save-after-remembering)
  1939. (defvar remember-data-file)
  1940. (defvar remember-register)
  1941. (defvar remember-buffer)
  1942. (defvar remember-handler-functions)
  1943. (defvar remember-annotation-functions)
  1944. (defvar texmathp-why)
  1945. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1946. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1947. (defvar w3m-current-url)
  1948. (defvar w3m-current-title)
  1949. (defvar org-latex-regexps)
  1950. ;;; Autoload and prepare some org modules
  1951. ;; Some table stuff that needs to be defined here, because it is used
  1952. ;; by the functions setting up org-mode or checking for table context.
  1953. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1954. "Detects an org-type or table-type table.")
  1955. (defconst org-table-line-regexp "^[ \t]*|"
  1956. "Detects an org-type table line.")
  1957. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1958. "Detects an org-type table line.")
  1959. (defconst org-table-hline-regexp "^[ \t]*|-"
  1960. "Detects an org-type table hline.")
  1961. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1962. "Detects a table-type table hline.")
  1963. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1964. "Searching from within a table (any type) this finds the first line
  1965. outside the table.")
  1966. ;; Autoload the functions in org-table.el that are needed by functions here.
  1967. (eval-and-compile
  1968. (org-autoload "org-table"
  1969. '(org-table-align org-table-begin org-table-blank-field
  1970. org-table-convert org-table-convert-region org-table-copy-down
  1971. org-table-copy-region org-table-create
  1972. org-table-create-or-convert-from-region
  1973. org-table-create-with-table.el org-table-current-dline
  1974. org-table-cut-region org-table-delete-column org-table-edit-field
  1975. org-table-edit-formulas org-table-end org-table-eval-formula
  1976. org-table-export org-table-field-info
  1977. org-table-get-stored-formulas org-table-goto-column
  1978. org-table-hline-and-move org-table-import org-table-insert-column
  1979. org-table-insert-hline org-table-insert-row org-table-iterate
  1980. org-table-justify-field-maybe org-table-kill-row
  1981. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1982. org-table-move-column org-table-move-column-left
  1983. org-table-move-column-right org-table-move-row
  1984. org-table-move-row-down org-table-move-row-up
  1985. org-table-next-field org-table-next-row org-table-paste-rectangle
  1986. org-table-previous-field org-table-recalculate
  1987. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1988. org-table-toggle-coordinate-overlays
  1989. org-table-toggle-formula-debugger org-table-wrap-region
  1990. orgtbl-mode turn-on-orgtbl)))
  1991. (defun org-at-table-p (&optional table-type)
  1992. "Return t if the cursor is inside an org-type table.
  1993. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  1994. (if org-enable-table-editor
  1995. (save-excursion
  1996. (beginning-of-line 1)
  1997. (looking-at (if table-type org-table-any-line-regexp
  1998. org-table-line-regexp)))
  1999. nil))
  2000. (defsubst org-table-p () (org-at-table-p))
  2001. (defun org-at-table.el-p ()
  2002. "Return t if and only if we are at a table.el table."
  2003. (and (org-at-table-p 'any)
  2004. (save-excursion
  2005. (goto-char (org-table-begin 'any))
  2006. (looking-at org-table1-hline-regexp))))
  2007. (defun org-table-recognize-table.el ()
  2008. "If there is a table.el table nearby, recognize it and move into it."
  2009. (if org-table-tab-recognizes-table.el
  2010. (if (org-at-table.el-p)
  2011. (progn
  2012. (beginning-of-line 1)
  2013. (if (looking-at org-table-dataline-regexp)
  2014. nil
  2015. (if (looking-at org-table1-hline-regexp)
  2016. (progn
  2017. (beginning-of-line 2)
  2018. (if (looking-at org-table-any-border-regexp)
  2019. (beginning-of-line -1)))))
  2020. (if (re-search-forward "|" (org-table-end t) t)
  2021. (progn
  2022. (require 'table)
  2023. (if (table--at-cell-p (point))
  2024. t
  2025. (message "recognizing table.el table...")
  2026. (table-recognize-table)
  2027. (message "recognizing table.el table...done")))
  2028. (error "This should not happen..."))
  2029. t)
  2030. nil)
  2031. nil))
  2032. (defun org-at-table-hline-p ()
  2033. "Return t if the cursor is inside a hline in a table."
  2034. (if org-enable-table-editor
  2035. (save-excursion
  2036. (beginning-of-line 1)
  2037. (looking-at org-table-hline-regexp))
  2038. nil))
  2039. (defvar org-table-clean-did-remove-column nil)
  2040. (defun org-table-map-tables (function)
  2041. "Apply FUNCTION to the start of all tables in the buffer."
  2042. (save-excursion
  2043. (save-restriction
  2044. (widen)
  2045. (goto-char (point-min))
  2046. (while (re-search-forward org-table-any-line-regexp nil t)
  2047. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2048. (beginning-of-line 1)
  2049. (if (looking-at org-table-line-regexp)
  2050. (save-excursion (funcall function)))
  2051. (re-search-forward org-table-any-border-regexp nil 1))))
  2052. (message "Mapping tables: done"))
  2053. ;; Declare and autoload functions from org-exp.el
  2054. (declare-function org-default-export-plist "org-exp")
  2055. (declare-function org-infile-export-plist "org-exp")
  2056. (declare-function org-get-current-options "org-exp")
  2057. (eval-and-compile
  2058. (org-autoload "org-exp"
  2059. '(org-export org-export-as-ascii org-export-visible
  2060. org-insert-export-options-template org-export-as-html-and-open
  2061. org-export-as-html-batch org-export-as-html-to-buffer
  2062. org-replace-region-by-html org-export-region-as-html
  2063. org-export-as-html org-export-icalendar-this-file
  2064. org-export-icalendar-all-agenda-files
  2065. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2066. ;; Declare and autoload functions from org-exp.el
  2067. (eval-and-compile
  2068. (org-autoload "org-exp"
  2069. '(org-agenda org-agenda-list org-search-view
  2070. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2071. org-diary org-agenda-to-appt)))
  2072. ;; Autoload org-remember
  2073. (eval-and-compile
  2074. (org-autoload "org-remember"
  2075. '(org-remember-insinuate org-remember-annotation
  2076. org-remember-apply-template org-remember org-remember-handler)))
  2077. ;; Autoload org-clock.el
  2078. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2079. (beg end))
  2080. (defvar org-clock-marker (make-marker)
  2081. "Marker recording the last clock-in.")
  2082. (eval-and-compile
  2083. (org-autoload
  2084. "org-clock"
  2085. '(org-clock-in org-clock-out org-clock-cancel
  2086. org-clock-goto org-clock-sum org-clock-display
  2087. org-remove-clock-overlays org-clock-report
  2088. org-clocktable-shift org-dblock-write:clocktable
  2089. org-get-clocktable)))
  2090. (defun org-clock-update-time-maybe ()
  2091. "If this is a CLOCK line, update it and return t.
  2092. Otherwise, return nil."
  2093. (interactive)
  2094. (save-excursion
  2095. (beginning-of-line 1)
  2096. (skip-chars-forward " \t")
  2097. (when (looking-at org-clock-string)
  2098. (let ((re (concat "[ \t]*" org-clock-string
  2099. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2100. "\\([ \t]*=>.*\\)?"))
  2101. ts te h m s)
  2102. (if (not (looking-at re))
  2103. nil
  2104. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2105. (end-of-line 1)
  2106. (setq ts (match-string 1)
  2107. te (match-string 2))
  2108. (setq s (- (time-to-seconds
  2109. (apply 'encode-time (org-parse-time-string te)))
  2110. (time-to-seconds
  2111. (apply 'encode-time (org-parse-time-string ts))))
  2112. h (floor (/ s 3600))
  2113. s (- s (* 3600 h))
  2114. m (floor (/ s 60))
  2115. s (- s (* 60 s)))
  2116. (insert " => " (format "%2d:%02d" h m))
  2117. t)))))
  2118. (defun org-check-running-clock ()
  2119. "Check if the current buffer contains the running clock.
  2120. If yes, offer to stop it and to save the buffer with the changes."
  2121. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2122. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2123. (buffer-name))))
  2124. (org-clock-out)
  2125. (when (y-or-n-p "Save changed buffer?")
  2126. (save-buffer))))
  2127. (defun org-clocktable-try-shift (dir n)
  2128. "Check if this line starts a clock table, if yes, shift the time block."
  2129. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2130. (org-clocktable-shift dir n)))
  2131. ;; Autoload archiving code
  2132. ;; The stuff that is needed for cycling and tags has to be defined here.
  2133. (defgroup org-archive nil
  2134. "Options concerning archiving in Org-mode."
  2135. :tag "Org Archive"
  2136. :group 'org-structure)
  2137. (defcustom org-archive-location "%s_archive::"
  2138. "The location where subtrees should be archived.
  2139. Otherwise, the value of this variable is a string, consisting of two
  2140. parts, separated by a double-colon.
  2141. The first part is a file name - when omitted, archiving happens in the same
  2142. file. %s will be replaced by the current file name (without directory part).
  2143. Archiving to a different file is useful to keep archived entries from
  2144. contributing to the Org-mode Agenda.
  2145. The part after the double colon is a headline. The archived entries will be
  2146. filed under that headline. When omitted, the subtrees are simply filed away
  2147. at the end of the file, as top-level entries.
  2148. Here are a few examples:
  2149. \"%s_archive::\"
  2150. If the current file is Projects.org, archive in file
  2151. Projects.org_archive, as top-level trees. This is the default.
  2152. \"::* Archived Tasks\"
  2153. Archive in the current file, under the top-level headline
  2154. \"* Archived Tasks\".
  2155. \"~/org/archive.org::\"
  2156. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2157. \"basement::** Finished Tasks\"
  2158. Archive in file ./basement (relative path), as level 3 trees
  2159. below the level 2 heading \"** Finished Tasks\".
  2160. You may set this option on a per-file basis by adding to the buffer a
  2161. line like
  2162. #+ARCHIVE: basement::** Finished Tasks
  2163. You may also define it locally for a subtree by setting an ARCHIVE property
  2164. in the entry. If such a property is found in an entry, or anywhere up
  2165. the hierarchy, it will be used."
  2166. :group 'org-archive
  2167. :type 'string)
  2168. (defcustom org-archive-tag "ARCHIVE"
  2169. "The tag that marks a subtree as archived.
  2170. An archived subtree does not open during visibility cycling, and does
  2171. not contribute to the agenda listings.
  2172. After changing this, font-lock must be restarted in the relevant buffers to
  2173. get the proper fontification."
  2174. :group 'org-archive
  2175. :group 'org-keywords
  2176. :type 'string)
  2177. (defcustom org-agenda-skip-archived-trees t
  2178. "Non-nil means, the agenda will skip any items located in archived trees.
  2179. An archived tree is a tree marked with the tag ARCHIVE."
  2180. :group 'org-archive
  2181. :group 'org-agenda-skip
  2182. :type 'boolean)
  2183. (defcustom org-cycle-open-archived-trees nil
  2184. "Non-nil means, `org-cycle' will open archived trees.
  2185. An archived tree is a tree marked with the tag ARCHIVE.
  2186. When nil, archived trees will stay folded. You can still open them with
  2187. normal outline commands like `show-all', but not with the cycling commands."
  2188. :group 'org-archive
  2189. :group 'org-cycle
  2190. :type 'boolean)
  2191. (defcustom org-sparse-tree-open-archived-trees nil
  2192. "Non-nil means sparse tree construction shows matches in archived trees.
  2193. When nil, matches in these trees are highlighted, but the trees are kept in
  2194. collapsed state."
  2195. :group 'org-archive
  2196. :group 'org-sparse-trees
  2197. :type 'boolean)
  2198. (defun org-cycle-hide-archived-subtrees (state)
  2199. "Re-hide all archived subtrees after a visibility state change."
  2200. (when (and (not org-cycle-open-archived-trees)
  2201. (not (memq state '(overview folded))))
  2202. (save-excursion
  2203. (let* ((globalp (memq state '(contents all)))
  2204. (beg (if globalp (point-min) (point)))
  2205. (end (if globalp (point-max) (org-end-of-subtree t))))
  2206. (org-hide-archived-subtrees beg end)
  2207. (goto-char beg)
  2208. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2209. (message "%s" (substitute-command-keys
  2210. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2211. (defun org-force-cycle-archived ()
  2212. "Cycle subtree even if it is archived."
  2213. (interactive)
  2214. (setq this-command 'org-cycle)
  2215. (let ((org-cycle-open-archived-trees t))
  2216. (call-interactively 'org-cycle)))
  2217. (defun org-hide-archived-subtrees (beg end)
  2218. "Re-hide all archived subtrees after a visibility state change."
  2219. (save-excursion
  2220. (let* ((re (concat ":" org-archive-tag ":")))
  2221. (goto-char beg)
  2222. (while (re-search-forward re end t)
  2223. (and (org-on-heading-p) (hide-subtree))
  2224. (org-end-of-subtree t)))))
  2225. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2226. (eval-and-compile
  2227. (org-autoload "org-archive"
  2228. '(org-add-archive-files org-archive-subtree
  2229. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2230. ;; Autoload Column View Code
  2231. (declare-function org-columns-number-to-string "org-colview")
  2232. (declare-function org-columns-get-format-and-top-level "org-colview")
  2233. (declare-function org-columns-compute "org-colview")
  2234. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2235. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2236. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2237. org-columns org-insert-columns-dblock))
  2238. ;;; Variables for pre-computed regular expressions, all buffer local
  2239. (defvar org-drawer-regexp nil
  2240. "Matches first line of a hidden block.")
  2241. (make-variable-buffer-local 'org-drawer-regexp)
  2242. (defvar org-todo-regexp nil
  2243. "Matches any of the TODO state keywords.")
  2244. (make-variable-buffer-local 'org-todo-regexp)
  2245. (defvar org-not-done-regexp nil
  2246. "Matches any of the TODO state keywords except the last one.")
  2247. (make-variable-buffer-local 'org-not-done-regexp)
  2248. (defvar org-todo-line-regexp nil
  2249. "Matches a headline and puts TODO state into group 2 if present.")
  2250. (make-variable-buffer-local 'org-todo-line-regexp)
  2251. (defvar org-complex-heading-regexp nil
  2252. "Matches a headline and puts everything into groups:
  2253. group 1: the stars
  2254. group 2: The todo keyword, maybe
  2255. group 3: Priority cookie
  2256. group 4: True headline
  2257. group 5: Tags")
  2258. (make-variable-buffer-local 'org-complex-heading-regexp)
  2259. (defvar org-todo-line-tags-regexp nil
  2260. "Matches a headline and puts TODO state into group 2 if present.
  2261. Also put tags into group 4 if tags are present.")
  2262. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2263. (defvar org-nl-done-regexp nil
  2264. "Matches newline followed by a headline with the DONE keyword.")
  2265. (make-variable-buffer-local 'org-nl-done-regexp)
  2266. (defvar org-looking-at-done-regexp nil
  2267. "Matches the DONE keyword a point.")
  2268. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2269. (defvar org-ds-keyword-length 12
  2270. "Maximum length of the Deadline and SCHEDULED keywords.")
  2271. (make-variable-buffer-local 'org-ds-keyword-length)
  2272. (defvar org-deadline-regexp nil
  2273. "Matches the DEADLINE keyword.")
  2274. (make-variable-buffer-local 'org-deadline-regexp)
  2275. (defvar org-deadline-time-regexp nil
  2276. "Matches the DEADLINE keyword together with a time stamp.")
  2277. (make-variable-buffer-local 'org-deadline-time-regexp)
  2278. (defvar org-deadline-line-regexp nil
  2279. "Matches the DEADLINE keyword and the rest of the line.")
  2280. (make-variable-buffer-local 'org-deadline-line-regexp)
  2281. (defvar org-scheduled-regexp nil
  2282. "Matches the SCHEDULED keyword.")
  2283. (make-variable-buffer-local 'org-scheduled-regexp)
  2284. (defvar org-scheduled-time-regexp nil
  2285. "Matches the SCHEDULED keyword together with a time stamp.")
  2286. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2287. (defvar org-closed-time-regexp nil
  2288. "Matches the CLOSED keyword together with a time stamp.")
  2289. (make-variable-buffer-local 'org-closed-time-regexp)
  2290. (defvar org-keyword-time-regexp nil
  2291. "Matches any of the 4 keywords, together with the time stamp.")
  2292. (make-variable-buffer-local 'org-keyword-time-regexp)
  2293. (defvar org-keyword-time-not-clock-regexp nil
  2294. "Matches any of the 3 keywords, together with the time stamp.")
  2295. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2296. (defvar org-maybe-keyword-time-regexp nil
  2297. "Matches a timestamp, possibly preceeded by a keyword.")
  2298. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2299. (defvar org-planning-or-clock-line-re nil
  2300. "Matches a line with planning or clock info.")
  2301. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2302. (defconst org-plain-time-of-day-regexp
  2303. (concat
  2304. "\\(\\<[012]?[0-9]"
  2305. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2306. "\\(--?"
  2307. "\\(\\<[012]?[0-9]"
  2308. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2309. "\\)?")
  2310. "Regular expression to match a plain time or time range.
  2311. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2312. groups carry important information:
  2313. 0 the full match
  2314. 1 the first time, range or not
  2315. 8 the second time, if it is a range.")
  2316. (defconst org-plain-time-extension-regexp
  2317. (concat
  2318. "\\(\\<[012]?[0-9]"
  2319. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2320. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2321. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2322. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2323. groups carry important information:
  2324. 0 the full match
  2325. 7 hours of duration
  2326. 9 minutes of duration")
  2327. (defconst org-stamp-time-of-day-regexp
  2328. (concat
  2329. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2330. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2331. "\\(--?"
  2332. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2333. "Regular expression to match a timestamp time or time range.
  2334. After a match, the following groups carry important information:
  2335. 0 the full match
  2336. 1 date plus weekday, for backreferencing to make sure both times on same day
  2337. 2 the first time, range or not
  2338. 4 the second time, if it is a range.")
  2339. (defconst org-startup-options
  2340. '(("fold" org-startup-folded t)
  2341. ("overview" org-startup-folded t)
  2342. ("nofold" org-startup-folded nil)
  2343. ("showall" org-startup-folded nil)
  2344. ("content" org-startup-folded content)
  2345. ("hidestars" org-hide-leading-stars t)
  2346. ("showstars" org-hide-leading-stars nil)
  2347. ("odd" org-odd-levels-only t)
  2348. ("oddeven" org-odd-levels-only nil)
  2349. ("align" org-startup-align-all-tables t)
  2350. ("noalign" org-startup-align-all-tables nil)
  2351. ("customtime" org-display-custom-times t)
  2352. ("logdone" org-log-done time)
  2353. ("lognotedone" org-log-done note)
  2354. ("nologdone" org-log-done nil)
  2355. ("lognoteclock-out" org-log-note-clock-out t)
  2356. ("nolognoteclock-out" org-log-note-clock-out nil)
  2357. ("logrepeat" org-log-repeat state)
  2358. ("lognoterepeat" org-log-repeat note)
  2359. ("nologrepeat" org-log-repeat nil)
  2360. ("constcgs" constants-unit-system cgs)
  2361. ("constSI" constants-unit-system SI))
  2362. "Variable associated with STARTUP options for org-mode.
  2363. Each element is a list of three items: The startup options as written
  2364. in the #+STARTUP line, the corresponding variable, and the value to
  2365. set this variable to if the option is found. An optional forth element PUSH
  2366. means to push this value onto the list in the variable.")
  2367. (defun org-set-regexps-and-options ()
  2368. "Precompute regular expressions for current buffer."
  2369. (when (org-mode-p)
  2370. (org-set-local 'org-todo-kwd-alist nil)
  2371. (org-set-local 'org-todo-key-alist nil)
  2372. (org-set-local 'org-todo-key-trigger nil)
  2373. (org-set-local 'org-todo-keywords-1 nil)
  2374. (org-set-local 'org-done-keywords nil)
  2375. (org-set-local 'org-todo-heads nil)
  2376. (org-set-local 'org-todo-sets nil)
  2377. (org-set-local 'org-todo-log-states nil)
  2378. (let ((re (org-make-options-regexp
  2379. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2380. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2381. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2382. (splitre "[ \t]+")
  2383. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2384. tail sep kws1 prio props drawers
  2385. ext-setup-or-nil setup-contents (start 0))
  2386. (save-excursion
  2387. (save-restriction
  2388. (widen)
  2389. (goto-char (point-min))
  2390. (while (or (and ext-setup-or-nil
  2391. (string-match re ext-setup-or-nil start)
  2392. (setq start (match-end 0)))
  2393. (and (setq ext-setup-or-nil nil start 0)
  2394. (re-search-forward re nil t)))
  2395. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2396. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2397. (cond
  2398. ((equal key "CATEGORY")
  2399. (if (string-match "[ \t]+$" value)
  2400. (setq value (replace-match "" t t value)))
  2401. (setq cat value))
  2402. ((member key '("SEQ_TODO" "TODO"))
  2403. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2404. ((equal key "TYP_TODO")
  2405. (push (cons 'type (org-split-string value splitre)) kwds))
  2406. ((equal key "TAGS")
  2407. (setq tags (append tags (org-split-string value splitre))))
  2408. ((equal key "COLUMNS")
  2409. (org-set-local 'org-columns-default-format value))
  2410. ((equal key "LINK")
  2411. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2412. (push (cons (match-string 1 value)
  2413. (org-trim (match-string 2 value)))
  2414. links)))
  2415. ((equal key "PRIORITIES")
  2416. (setq prio (org-split-string value " +")))
  2417. ((equal key "PROPERTY")
  2418. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2419. (push (cons (match-string 1 value) (match-string 2 value))
  2420. props)))
  2421. ((equal key "DRAWERS")
  2422. (setq drawers (org-split-string value splitre)))
  2423. ((equal key "CONSTANTS")
  2424. (setq const (append const (org-split-string value splitre))))
  2425. ((equal key "STARTUP")
  2426. (let ((opts (org-split-string value splitre))
  2427. l var val)
  2428. (while (setq l (pop opts))
  2429. (when (setq l (assoc l org-startup-options))
  2430. (setq var (nth 1 l) val (nth 2 l))
  2431. (if (not (nth 3 l))
  2432. (set (make-local-variable var) val)
  2433. (if (not (listp (symbol-value var)))
  2434. (set (make-local-variable var) nil))
  2435. (set (make-local-variable var) (symbol-value var))
  2436. (add-to-list var val))))))
  2437. ((equal key "ARCHIVE")
  2438. (string-match " *$" value)
  2439. (setq arch (replace-match "" t t value))
  2440. (remove-text-properties 0 (length arch)
  2441. '(face t fontified t) arch))
  2442. ((equal key "SETUPFILE")
  2443. (setq setup-contents (org-file-contents
  2444. (expand-file-name
  2445. (org-remove-double-quotes value))
  2446. 'noerror))
  2447. (if (not ext-setup-or-nil)
  2448. (setq ext-setup-or-nil setup-contents start 0)
  2449. (setq ext-setup-or-nil
  2450. (concat (substring ext-setup-or-nil 0 start)
  2451. "\n" setup-contents "\n"
  2452. (substring ext-setup-or-nil start)))))
  2453. ))))
  2454. (when cat
  2455. (org-set-local 'org-category (intern cat))
  2456. (push (cons "CATEGORY" cat) props))
  2457. (when prio
  2458. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2459. (setq prio (mapcar 'string-to-char prio))
  2460. (org-set-local 'org-highest-priority (nth 0 prio))
  2461. (org-set-local 'org-lowest-priority (nth 1 prio))
  2462. (org-set-local 'org-default-priority (nth 2 prio)))
  2463. (and props (org-set-local 'org-local-properties (nreverse props)))
  2464. (and drawers (org-set-local 'org-drawers drawers))
  2465. (and arch (org-set-local 'org-archive-location arch))
  2466. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2467. ;; Process the TODO keywords
  2468. (unless kwds
  2469. ;; Use the global values as if they had been given locally.
  2470. (setq kwds (default-value 'org-todo-keywords))
  2471. (if (stringp (car kwds))
  2472. (setq kwds (list (cons org-todo-interpretation
  2473. (default-value 'org-todo-keywords)))))
  2474. (setq kwds (reverse kwds)))
  2475. (setq kwds (nreverse kwds))
  2476. (let (inter kws kw)
  2477. (while (setq kws (pop kwds))
  2478. (setq inter (pop kws) sep (member "|" kws)
  2479. kws0 (delete "|" (copy-sequence kws))
  2480. kwsa nil
  2481. kws1 (mapcar
  2482. (lambda (x)
  2483. ;; 1 2
  2484. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2485. (progn
  2486. (setq kw (match-string 1 x)
  2487. key (and (match-end 2) (match-string 2 x))
  2488. log (org-extract-log-state-settings x))
  2489. (push (cons kw (and key (string-to-char key))) kwsa)
  2490. (and log (push log org-todo-log-states))
  2491. kw)
  2492. (error "Invalid TODO keyword %s" x)))
  2493. kws0)
  2494. kwsa (if kwsa (append '((:startgroup))
  2495. (nreverse kwsa)
  2496. '((:endgroup))))
  2497. hw (car kws1)
  2498. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2499. tail (list inter hw (car dws) (org-last dws)))
  2500. (add-to-list 'org-todo-heads hw 'append)
  2501. (push kws1 org-todo-sets)
  2502. (setq org-done-keywords (append org-done-keywords dws nil))
  2503. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2504. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2505. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2506. (setq org-todo-sets (nreverse org-todo-sets)
  2507. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2508. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2509. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2510. ;; Process the constants
  2511. (when const
  2512. (let (e cst)
  2513. (while (setq e (pop const))
  2514. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2515. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2516. (setq org-table-formula-constants-local cst)))
  2517. ;; Process the tags.
  2518. (when tags
  2519. (let (e tgs)
  2520. (while (setq e (pop tags))
  2521. (cond
  2522. ((equal e "{") (push '(:startgroup) tgs))
  2523. ((equal e "}") (push '(:endgroup) tgs))
  2524. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2525. (push (cons (match-string 1 e)
  2526. (string-to-char (match-string 2 e)))
  2527. tgs))
  2528. (t (push (list e) tgs))))
  2529. (org-set-local 'org-tag-alist nil)
  2530. (while (setq e (pop tgs))
  2531. (or (and (stringp (car e))
  2532. (assoc (car e) org-tag-alist))
  2533. (push e org-tag-alist))))))
  2534. ;; Compute the regular expressions and other local variables
  2535. (if (not org-done-keywords)
  2536. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2537. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2538. (length org-scheduled-string)
  2539. (length org-clock-string)
  2540. (length org-closed-string)))
  2541. org-drawer-regexp
  2542. (concat "^[ \t]*:\\("
  2543. (mapconcat 'regexp-quote org-drawers "\\|")
  2544. "\\):[ \t]*$")
  2545. org-not-done-keywords
  2546. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2547. org-todo-regexp
  2548. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2549. "\\|") "\\)\\>")
  2550. org-not-done-regexp
  2551. (concat "\\<\\("
  2552. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2553. "\\)\\>")
  2554. org-todo-line-regexp
  2555. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2556. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2557. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2558. org-complex-heading-regexp
  2559. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2560. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2561. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2562. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2563. org-nl-done-regexp
  2564. (concat "\n\\*+[ \t]+"
  2565. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2566. "\\)" "\\>")
  2567. org-todo-line-tags-regexp
  2568. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2569. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2570. (org-re
  2571. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2572. org-looking-at-done-regexp
  2573. (concat "^" "\\(?:"
  2574. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2575. "\\>")
  2576. org-deadline-regexp (concat "\\<" org-deadline-string)
  2577. org-deadline-time-regexp
  2578. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2579. org-deadline-line-regexp
  2580. (concat "\\<\\(" org-deadline-string "\\).*")
  2581. org-scheduled-regexp
  2582. (concat "\\<" org-scheduled-string)
  2583. org-scheduled-time-regexp
  2584. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2585. org-closed-time-regexp
  2586. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2587. org-keyword-time-regexp
  2588. (concat "\\<\\(" org-scheduled-string
  2589. "\\|" org-deadline-string
  2590. "\\|" org-closed-string
  2591. "\\|" org-clock-string "\\)"
  2592. " *[[<]\\([^]>]+\\)[]>]")
  2593. org-keyword-time-not-clock-regexp
  2594. (concat "\\<\\(" org-scheduled-string
  2595. "\\|" org-deadline-string
  2596. "\\|" org-closed-string
  2597. "\\)"
  2598. " *[[<]\\([^]>]+\\)[]>]")
  2599. org-maybe-keyword-time-regexp
  2600. (concat "\\(\\<\\(" org-scheduled-string
  2601. "\\|" org-deadline-string
  2602. "\\|" org-closed-string
  2603. "\\|" org-clock-string "\\)\\)?"
  2604. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2605. org-planning-or-clock-line-re
  2606. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2607. "\\|" org-deadline-string
  2608. "\\|" org-closed-string "\\|" org-clock-string
  2609. "\\)\\>\\)")
  2610. )
  2611. (org-compute-latex-and-specials-regexp)
  2612. (org-set-font-lock-defaults)))
  2613. (defun org-file-contents (file &optional noerror)
  2614. "Return the contents of FILE, as a string."
  2615. (if (or (not file)
  2616. (not (file-readable-p file)))
  2617. (if noerror
  2618. (progn
  2619. (message "Cannot read file %s" file)
  2620. (ding) (sit-for 2)
  2621. "")
  2622. (error "Cannot read file %s" file))
  2623. (with-temp-buffer
  2624. (insert-file-contents file)
  2625. (buffer-string))))
  2626. (defun org-extract-log-state-settings (x)
  2627. "Extract the log state setting from a TODO keyword string.
  2628. This will extract info from a string like \"WAIT(w@/!)\"."
  2629. (let (kw key log1 log2)
  2630. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2631. (setq kw (match-string 1 x)
  2632. key (and (match-end 2) (match-string 2 x))
  2633. log1 (and (match-end 3) (match-string 3 x))
  2634. log2 (and (match-end 4) (match-string 4 x)))
  2635. (and (or log1 log2)
  2636. (list kw
  2637. (and log1 (if (equal log1 "!") 'time 'note))
  2638. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2639. (defun org-remove-keyword-keys (list)
  2640. "Remove a pair of parenthesis at the end of each string in LIST."
  2641. (mapcar (lambda (x)
  2642. (if (string-match "(.*)$" x)
  2643. (substring x 0 (match-beginning 0))
  2644. x))
  2645. list))
  2646. ;; FIXME: this could be done much better, using second characters etc.
  2647. (defun org-assign-fast-keys (alist)
  2648. "Assign fast keys to a keyword-key alist.
  2649. Respect keys that are already there."
  2650. (let (new e k c c1 c2 (char ?a))
  2651. (while (setq e (pop alist))
  2652. (cond
  2653. ((equal e '(:startgroup)) (push e new))
  2654. ((equal e '(:endgroup)) (push e new))
  2655. (t
  2656. (setq k (car e) c2 nil)
  2657. (if (cdr e)
  2658. (setq c (cdr e))
  2659. ;; automatically assign a character.
  2660. (setq c1 (string-to-char
  2661. (downcase (substring
  2662. k (if (= (string-to-char k) ?@) 1 0)))))
  2663. (if (or (rassoc c1 new) (rassoc c1 alist))
  2664. (while (or (rassoc char new) (rassoc char alist))
  2665. (setq char (1+ char)))
  2666. (setq c2 c1))
  2667. (setq c (or c2 char)))
  2668. (push (cons k c) new))))
  2669. (nreverse new)))
  2670. ;;; Some variables used in various places
  2671. (defvar org-window-configuration nil
  2672. "Used in various places to store a window configuration.")
  2673. (defvar org-finish-function nil
  2674. "Function to be called when `C-c C-c' is used.
  2675. This is for getting out of special buffers like remember.")
  2676. ;; FIXME: Occasionally check by commenting these, to make sure
  2677. ;; no other functions uses these, forgetting to let-bind them.
  2678. (defvar entry)
  2679. (defvar state)
  2680. (defvar last-state)
  2681. (defvar date)
  2682. (defvar description)
  2683. ;; Defined somewhere in this file, but used before definition.
  2684. (defvar org-html-entities)
  2685. (defvar org-struct-menu)
  2686. (defvar org-org-menu)
  2687. (defvar org-tbl-menu)
  2688. (defvar org-agenda-keymap)
  2689. ;;;; Define the Org-mode
  2690. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2691. (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."))
  2692. ;; We use a before-change function to check if a table might need
  2693. ;; an update.
  2694. (defvar org-table-may-need-update t
  2695. "Indicates that a table might need an update.
  2696. This variable is set by `org-before-change-function'.
  2697. `org-table-align' sets it back to nil.")
  2698. (defun org-before-change-function (beg end)
  2699. "Every change indicates that a table might need an update."
  2700. (setq org-table-may-need-update t))
  2701. (defvar org-mode-map)
  2702. (defvar org-mode-hook nil)
  2703. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2704. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2705. (defvar org-table-buffer-is-an nil)
  2706. (defconst org-outline-regexp "\\*+ ")
  2707. ;;;###autoload
  2708. (define-derived-mode org-mode outline-mode "Org"
  2709. "Outline-based notes management and organizer, alias
  2710. \"Carsten's outline-mode for keeping track of everything.\"
  2711. Org-mode develops organizational tasks around a NOTES file which
  2712. contains information about projects as plain text. Org-mode is
  2713. implemented on top of outline-mode, which is ideal to keep the content
  2714. of large files well structured. It supports ToDo items, deadlines and
  2715. time stamps, which magically appear in the diary listing of the Emacs
  2716. calendar. Tables are easily created with a built-in table editor.
  2717. Plain text URL-like links connect to websites, emails (VM), Usenet
  2718. messages (Gnus), BBDB entries, and any files related to the project.
  2719. For printing and sharing of notes, an Org-mode file (or a part of it)
  2720. can be exported as a structured ASCII or HTML file.
  2721. The following commands are available:
  2722. \\{org-mode-map}"
  2723. ;; Get rid of Outline menus, they are not needed
  2724. ;; Need to do this here because define-derived-mode sets up
  2725. ;; the keymap so late. Still, it is a waste to call this each time
  2726. ;; we switch another buffer into org-mode.
  2727. (if (featurep 'xemacs)
  2728. (when (boundp 'outline-mode-menu-heading)
  2729. ;; Assume this is Greg's port, it used easymenu
  2730. (easy-menu-remove outline-mode-menu-heading)
  2731. (easy-menu-remove outline-mode-menu-show)
  2732. (easy-menu-remove outline-mode-menu-hide))
  2733. (define-key org-mode-map [menu-bar headings] 'undefined)
  2734. (define-key org-mode-map [menu-bar hide] 'undefined)
  2735. (define-key org-mode-map [menu-bar show] 'undefined))
  2736. (org-load-modules-maybe)
  2737. (easy-menu-add org-org-menu)
  2738. (easy-menu-add org-tbl-menu)
  2739. (org-install-agenda-files-menu)
  2740. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2741. (org-add-to-invisibility-spec '(org-cwidth))
  2742. (when (featurep 'xemacs)
  2743. (org-set-local 'line-move-ignore-invisible t))
  2744. (org-set-local 'outline-regexp org-outline-regexp)
  2745. (org-set-local 'outline-level 'org-outline-level)
  2746. (when (and org-ellipsis
  2747. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2748. (fboundp 'make-glyph-code))
  2749. (unless org-display-table
  2750. (setq org-display-table (make-display-table)))
  2751. (set-display-table-slot
  2752. org-display-table 4
  2753. (vconcat (mapcar
  2754. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2755. org-ellipsis)))
  2756. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2757. (setq buffer-display-table org-display-table))
  2758. (org-set-regexps-and-options)
  2759. ;; Calc embedded
  2760. (org-set-local 'calc-embedded-open-mode "# ")
  2761. (modify-syntax-entry ?# "<")
  2762. (modify-syntax-entry ?@ "w")
  2763. (if org-startup-truncated (setq truncate-lines t))
  2764. (org-set-local 'font-lock-unfontify-region-function
  2765. 'org-unfontify-region)
  2766. ;; Activate before-change-function
  2767. (org-set-local 'org-table-may-need-update t)
  2768. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2769. 'local)
  2770. ;; Check for running clock before killing a buffer
  2771. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2772. ;; Paragraphs and auto-filling
  2773. (org-set-autofill-regexps)
  2774. (setq indent-line-function 'org-indent-line-function)
  2775. (org-update-radio-target-regexp)
  2776. ;; Comment characters
  2777. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2778. (org-set-local 'comment-padding " ")
  2779. ;; Align options lines
  2780. (org-set-local
  2781. 'align-mode-rules-list
  2782. '((org-in-buffer-settings
  2783. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2784. (modes . '(org-mode)))))
  2785. ;; Imenu
  2786. (org-set-local 'imenu-create-index-function
  2787. 'org-imenu-get-tree)
  2788. ;; Make isearch reveal context
  2789. (if (or (featurep 'xemacs)
  2790. (not (boundp 'outline-isearch-open-invisible-function)))
  2791. ;; Emacs 21 and XEmacs make use of the hook
  2792. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2793. ;; Emacs 22 deals with this through a special variable
  2794. (org-set-local 'outline-isearch-open-invisible-function
  2795. (lambda (&rest ignore) (org-show-context 'isearch))))
  2796. ;; If empty file that did not turn on org-mode automatically, make it to.
  2797. (if (and org-insert-mode-line-in-empty-file
  2798. (interactive-p)
  2799. (= (point-min) (point-max)))
  2800. (insert "# -*- mode: org -*-\n\n"))
  2801. (unless org-inhibit-startup
  2802. (when org-startup-align-all-tables
  2803. (let ((bmp (buffer-modified-p)))
  2804. (org-table-map-tables 'org-table-align)
  2805. (set-buffer-modified-p bmp)))
  2806. (org-set-startup-visibility)))
  2807. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2808. (defun org-current-time ()
  2809. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2810. (if (> (car org-time-stamp-rounding-minutes) 1)
  2811. (let ((r (car org-time-stamp-rounding-minutes))
  2812. (time (decode-time)))
  2813. (apply 'encode-time
  2814. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2815. (nthcdr 2 time))))
  2816. (current-time)))
  2817. ;;;; Font-Lock stuff, including the activators
  2818. (defvar org-mouse-map (make-sparse-keymap))
  2819. (org-defkey org-mouse-map
  2820. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2821. (org-defkey org-mouse-map
  2822. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2823. (when org-mouse-1-follows-link
  2824. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2825. (when org-tab-follows-link
  2826. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2827. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2828. (when org-return-follows-link
  2829. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2830. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2831. (require 'font-lock)
  2832. (defconst org-non-link-chars "]\t\n\r<>")
  2833. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2834. "shell" "elisp"))
  2835. (defvar org-link-types-re nil
  2836. "Matches a link that has a url-like prefix like \"http:\"")
  2837. (defvar org-link-re-with-space nil
  2838. "Matches a link with spaces, optional angular brackets around it.")
  2839. (defvar org-link-re-with-space2 nil
  2840. "Matches a link with spaces, optional angular brackets around it.")
  2841. (defvar org-angle-link-re nil
  2842. "Matches link with angular brackets, spaces are allowed.")
  2843. (defvar org-plain-link-re nil
  2844. "Matches plain link, without spaces.")
  2845. (defvar org-bracket-link-regexp nil
  2846. "Matches a link in double brackets.")
  2847. (defvar org-bracket-link-analytic-regexp nil
  2848. "Regular expression used to analyze links.
  2849. Here is what the match groups contain after a match:
  2850. 1: http:
  2851. 2: http
  2852. 3: path
  2853. 4: [desc]
  2854. 5: desc")
  2855. (defvar org-any-link-re nil
  2856. "Regular expression matching any link.")
  2857. (defun org-make-link-regexps ()
  2858. "Update the link regular expressions.
  2859. This should be called after the variable `org-link-types' has changed."
  2860. (setq org-link-types-re
  2861. (concat
  2862. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2863. org-link-re-with-space
  2864. (concat
  2865. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2866. "\\([^" org-non-link-chars " ]"
  2867. "[^" org-non-link-chars "]*"
  2868. "[^" org-non-link-chars " ]\\)>?")
  2869. org-link-re-with-space2
  2870. (concat
  2871. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2872. "\\([^" org-non-link-chars " ]"
  2873. "[^]\t\n\r]*"
  2874. "[^" org-non-link-chars " ]\\)>?")
  2875. org-angle-link-re
  2876. (concat
  2877. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2878. "\\([^" org-non-link-chars " ]"
  2879. "[^" org-non-link-chars "]*"
  2880. "\\)>")
  2881. org-plain-link-re
  2882. (concat
  2883. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2884. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2885. org-bracket-link-regexp
  2886. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2887. org-bracket-link-analytic-regexp
  2888. (concat
  2889. "\\[\\["
  2890. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2891. "\\([^]]+\\)"
  2892. "\\]"
  2893. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2894. "\\]")
  2895. org-any-link-re
  2896. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2897. org-angle-link-re "\\)\\|\\("
  2898. org-plain-link-re "\\)")))
  2899. (org-make-link-regexps)
  2900. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2901. "Regular expression for fast time stamp matching.")
  2902. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2903. "Regular expression for fast time stamp matching.")
  2904. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2905. "Regular expression matching time strings for analysis.
  2906. This one does not require the space after the date, so it can be used
  2907. on a string that terminates immediately after the date.")
  2908. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2909. "Regular expression matching time strings for analysis.")
  2910. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2911. "Regular expression matching time stamps, with groups.")
  2912. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2913. "Regular expression matching time stamps (also [..]), with groups.")
  2914. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2915. "Regular expression matching a time stamp range.")
  2916. (defconst org-tr-regexp-both
  2917. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2918. "Regular expression matching a time stamp range.")
  2919. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2920. org-ts-regexp "\\)?")
  2921. "Regular expression matching a time stamp or time stamp range.")
  2922. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2923. org-ts-regexp-both "\\)?")
  2924. "Regular expression matching a time stamp or time stamp range.
  2925. The time stamps may be either active or inactive.")
  2926. (defvar org-emph-face nil)
  2927. (defun org-do-emphasis-faces (limit)
  2928. "Run through the buffer and add overlays to links."
  2929. (let (rtn)
  2930. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2931. (if (not (= (char-after (match-beginning 3))
  2932. (char-after (match-beginning 4))))
  2933. (progn
  2934. (setq rtn t)
  2935. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2936. 'face
  2937. (nth 1 (assoc (match-string 3)
  2938. org-emphasis-alist)))
  2939. (add-text-properties (match-beginning 2) (match-end 2)
  2940. '(font-lock-multiline t))
  2941. (when org-hide-emphasis-markers
  2942. (add-text-properties (match-end 4) (match-beginning 5)
  2943. '(invisible org-link))
  2944. (add-text-properties (match-beginning 3) (match-end 3)
  2945. '(invisible org-link)))))
  2946. (backward-char 1))
  2947. rtn))
  2948. (defun org-emphasize (&optional char)
  2949. "Insert or change an emphasis, i.e. a font like bold or italic.
  2950. If there is an active region, change that region to a new emphasis.
  2951. If there is no region, just insert the marker characters and position
  2952. the cursor between them.
  2953. CHAR should be either the marker character, or the first character of the
  2954. HTML tag associated with that emphasis. If CHAR is a space, the means
  2955. to remove the emphasis of the selected region.
  2956. If char is not given (for example in an interactive call) it
  2957. will be prompted for."
  2958. (interactive)
  2959. (let ((eal org-emphasis-alist) e det
  2960. (erc org-emphasis-regexp-components)
  2961. (prompt "")
  2962. (string "") beg end move tag c s)
  2963. (if (org-region-active-p)
  2964. (setq beg (region-beginning) end (region-end)
  2965. string (buffer-substring beg end))
  2966. (setq move t))
  2967. (while (setq e (pop eal))
  2968. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2969. c (aref tag 0))
  2970. (push (cons c (string-to-char (car e))) det)
  2971. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2972. (substring tag 1)))))
  2973. (unless char
  2974. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2975. (setq char (read-char-exclusive)))
  2976. (setq char (or (cdr (assoc char det)) char))
  2977. (if (equal char ?\ )
  2978. (setq s "" move nil)
  2979. (unless (assoc (char-to-string char) org-emphasis-alist)
  2980. (error "No such emphasis marker: \"%c\"" char))
  2981. (setq s (char-to-string char)))
  2982. (while (and (> (length string) 1)
  2983. (equal (substring string 0 1) (substring string -1))
  2984. (assoc (substring string 0 1) org-emphasis-alist))
  2985. (setq string (substring string 1 -1)))
  2986. (setq string (concat s string s))
  2987. (if beg (delete-region beg end))
  2988. (unless (or (bolp)
  2989. (string-match (concat "[" (nth 0 erc) "\n]")
  2990. (char-to-string (char-before (point)))))
  2991. (insert " "))
  2992. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2993. (char-to-string (char-after (point))))
  2994. (insert " ") (backward-char 1))
  2995. (insert string)
  2996. (and move (backward-char 1))))
  2997. (defconst org-nonsticky-props
  2998. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2999. (defun org-activate-plain-links (limit)
  3000. "Run through the buffer and add overlays to links."
  3001. (catch 'exit
  3002. (let (f)
  3003. (while (re-search-forward org-plain-link-re limit t)
  3004. (setq f (get-text-property (match-beginning 0) 'face))
  3005. (if (or (eq f 'org-tag)
  3006. (and (listp f) (memq 'org-tag f)))
  3007. nil
  3008. (add-text-properties (match-beginning 0) (match-end 0)
  3009. (list 'mouse-face 'highlight
  3010. 'rear-nonsticky org-nonsticky-props
  3011. 'keymap org-mouse-map
  3012. ))
  3013. (throw 'exit t))))))
  3014. (defun org-activate-code (limit)
  3015. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3016. (unless (get-text-property (match-beginning 1) 'face)
  3017. (remove-text-properties (match-beginning 0) (match-end 0)
  3018. '(display t invisible t intangible t))
  3019. t)))
  3020. (defun org-activate-angle-links (limit)
  3021. "Run through the buffer and add overlays to links."
  3022. (if (re-search-forward org-angle-link-re limit t)
  3023. (progn
  3024. (add-text-properties (match-beginning 0) (match-end 0)
  3025. (list 'mouse-face 'highlight
  3026. 'rear-nonsticky org-nonsticky-props
  3027. 'keymap org-mouse-map
  3028. ))
  3029. t)))
  3030. (defun org-activate-bracket-links (limit)
  3031. "Run through the buffer and add overlays to bracketed links."
  3032. (if (re-search-forward org-bracket-link-regexp limit t)
  3033. (let* ((help (concat "LINK: "
  3034. (org-match-string-no-properties 1)))
  3035. ;; FIXME: above we should remove the escapes.
  3036. ;; but that requires another match, protecting match data,
  3037. ;; a lot of overhead for font-lock.
  3038. (ip (org-maybe-intangible
  3039. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3040. 'keymap org-mouse-map 'mouse-face 'highlight
  3041. 'font-lock-multiline t 'help-echo help)))
  3042. (vp (list 'rear-nonsticky org-nonsticky-props
  3043. 'keymap org-mouse-map 'mouse-face 'highlight
  3044. ' font-lock-multiline t 'help-echo help)))
  3045. ;; We need to remove the invisible property here. Table narrowing
  3046. ;; may have made some of this invisible.
  3047. (remove-text-properties (match-beginning 0) (match-end 0)
  3048. '(invisible nil))
  3049. (if (match-end 3)
  3050. (progn
  3051. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3052. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3053. (add-text-properties (match-end 3) (match-end 0) ip))
  3054. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3055. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3056. (add-text-properties (match-end 1) (match-end 0) ip))
  3057. t)))
  3058. (defun org-activate-dates (limit)
  3059. "Run through the buffer and add overlays to dates."
  3060. (if (re-search-forward org-tsr-regexp-both limit t)
  3061. (progn
  3062. (add-text-properties (match-beginning 0) (match-end 0)
  3063. (list 'mouse-face 'highlight
  3064. 'rear-nonsticky org-nonsticky-props
  3065. 'keymap org-mouse-map))
  3066. (when org-display-custom-times
  3067. (if (match-end 3)
  3068. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3069. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3070. t)))
  3071. (defvar org-target-link-regexp nil
  3072. "Regular expression matching radio targets in plain text.")
  3073. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3074. "Regular expression matching a link target.")
  3075. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3076. "Regular expression matching a radio target.")
  3077. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3078. "Regular expression matching any target.")
  3079. (defun org-activate-target-links (limit)
  3080. "Run through the buffer and add overlays to target matches."
  3081. (when org-target-link-regexp
  3082. (let ((case-fold-search t))
  3083. (if (re-search-forward org-target-link-regexp limit t)
  3084. (progn
  3085. (add-text-properties (match-beginning 0) (match-end 0)
  3086. (list 'mouse-face 'highlight
  3087. 'rear-nonsticky org-nonsticky-props
  3088. 'keymap org-mouse-map
  3089. 'help-echo "Radio target link"
  3090. 'org-linked-text t))
  3091. t)))))
  3092. (defun org-update-radio-target-regexp ()
  3093. "Find all radio targets in this file and update the regular expression."
  3094. (interactive)
  3095. (when (memq 'radio org-activate-links)
  3096. (setq org-target-link-regexp
  3097. (org-make-target-link-regexp (org-all-targets 'radio)))
  3098. (org-restart-font-lock)))
  3099. (defun org-hide-wide-columns (limit)
  3100. (let (s e)
  3101. (setq s (text-property-any (point) (or limit (point-max))
  3102. 'org-cwidth t))
  3103. (when s
  3104. (setq e (next-single-property-change s 'org-cwidth))
  3105. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3106. (goto-char e)
  3107. t)))
  3108. (defvar org-latex-and-specials-regexp nil
  3109. "Regular expression for highlighting export special stuff.")
  3110. (defvar org-match-substring-regexp)
  3111. (defvar org-match-substring-with-braces-regexp)
  3112. (defvar org-export-html-special-string-regexps)
  3113. (defun org-compute-latex-and-specials-regexp ()
  3114. "Compute regular expression for stuff treated specially by exporters."
  3115. (if (not org-highlight-latex-fragments-and-specials)
  3116. (org-set-local 'org-latex-and-specials-regexp nil)
  3117. (require 'org-exp)
  3118. (let*
  3119. ((matchers (plist-get org-format-latex-options :matchers))
  3120. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3121. org-latex-regexps)))
  3122. (options (org-combine-plists (org-default-export-plist)
  3123. (org-infile-export-plist)))
  3124. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3125. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3126. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3127. (org-export-html-expand (plist-get options :expand-quoted-html))
  3128. (org-export-with-special-strings (plist-get options :special-strings))
  3129. (re-sub
  3130. (cond
  3131. ((equal org-export-with-sub-superscripts '{})
  3132. (list org-match-substring-with-braces-regexp))
  3133. (org-export-with-sub-superscripts
  3134. (list org-match-substring-regexp))
  3135. (t nil)))
  3136. (re-latex
  3137. (if org-export-with-LaTeX-fragments
  3138. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3139. (re-macros
  3140. (if org-export-with-TeX-macros
  3141. (list (concat "\\\\"
  3142. (regexp-opt
  3143. (append (mapcar 'car org-html-entities)
  3144. (if (boundp 'org-latex-entities)
  3145. org-latex-entities nil))
  3146. 'words))) ; FIXME
  3147. ))
  3148. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3149. (re-special (if org-export-with-special-strings
  3150. (mapcar (lambda (x) (car x))
  3151. org-export-html-special-string-regexps)))
  3152. (re-rest
  3153. (delq nil
  3154. (list
  3155. (if org-export-html-expand "@<[^>\n]+>")
  3156. ))))
  3157. (org-set-local
  3158. 'org-latex-and-specials-regexp
  3159. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3160. re-rest) "\\|")))))
  3161. (defun org-do-latex-and-special-faces (limit)
  3162. "Run through the buffer and add overlays to links."
  3163. (when org-latex-and-specials-regexp
  3164. (let (rtn d)
  3165. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3166. limit t))
  3167. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3168. 'face))
  3169. '(org-code org-verbatim underline)))
  3170. (progn
  3171. (setq rtn t
  3172. d (cond ((member (char-after (1+ (match-beginning 0)))
  3173. '(?_ ?^)) 1)
  3174. (t 0)))
  3175. (font-lock-prepend-text-property
  3176. (+ d (match-beginning 0)) (match-end 0)
  3177. 'face 'org-latex-and-export-specials)
  3178. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3179. '(font-lock-multiline t)))))
  3180. rtn)))
  3181. (defun org-restart-font-lock ()
  3182. "Restart font-lock-mode, to force refontification."
  3183. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3184. (font-lock-mode -1)
  3185. (font-lock-mode 1)))
  3186. (defun org-all-targets (&optional radio)
  3187. "Return a list of all targets in this file.
  3188. With optional argument RADIO, only find radio targets."
  3189. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3190. rtn)
  3191. (save-excursion
  3192. (goto-char (point-min))
  3193. (while (re-search-forward re nil t)
  3194. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3195. rtn)))
  3196. (defun org-make-target-link-regexp (targets)
  3197. "Make regular expression matching all strings in TARGETS.
  3198. The regular expression finds the targets also if there is a line break
  3199. between words."
  3200. (and targets
  3201. (concat
  3202. "\\<\\("
  3203. (mapconcat
  3204. (lambda (x)
  3205. (while (string-match " +" x)
  3206. (setq x (replace-match "\\s-+" t t x)))
  3207. x)
  3208. targets
  3209. "\\|")
  3210. "\\)\\>")))
  3211. (defun org-activate-tags (limit)
  3212. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3213. (progn
  3214. (add-text-properties (match-beginning 1) (match-end 1)
  3215. (list 'mouse-face 'highlight
  3216. 'rear-nonsticky org-nonsticky-props
  3217. 'keymap org-mouse-map))
  3218. t)))
  3219. (defun org-outline-level ()
  3220. (save-excursion
  3221. (looking-at outline-regexp)
  3222. (if (match-beginning 1)
  3223. (+ (org-get-string-indentation (match-string 1)) 1000)
  3224. (1- (- (match-end 0) (match-beginning 0))))))
  3225. (defvar org-font-lock-keywords nil)
  3226. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3227. "Regular expression matching a property line.")
  3228. (defvar org-font-lock-hook nil
  3229. "Functions to be called for special font loch stuff.")
  3230. (defun org-font-lock-hook (limit)
  3231. (run-hook-with-args 'org-font-lock-hook limit))
  3232. (defun org-set-font-lock-defaults ()
  3233. (let* ((em org-fontify-emphasized-text)
  3234. (lk org-activate-links)
  3235. (org-font-lock-extra-keywords
  3236. (list
  3237. ;; Call the hook
  3238. '(org-font-lock-hook)
  3239. ;; Headlines
  3240. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3241. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3242. ;; Table lines
  3243. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3244. (1 'org-table t))
  3245. ;; Table internals
  3246. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3247. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3248. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3249. ;; Drawers
  3250. (list org-drawer-regexp '(0 'org-special-keyword t))
  3251. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3252. ;; Properties
  3253. (list org-property-re
  3254. '(1 'org-special-keyword t)
  3255. '(3 'org-property-value t))
  3256. (if org-format-transports-properties-p
  3257. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3258. ;; Links
  3259. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3260. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3261. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3262. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3263. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3264. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3265. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3266. '(org-hide-wide-columns (0 nil append))
  3267. ;; TODO lines
  3268. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3269. '(1 (org-get-todo-face 1) t))
  3270. ;; DONE
  3271. (if org-fontify-done-headline
  3272. (list (concat "^[*]+ +\\<\\("
  3273. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3274. "\\)\\(.*\\)")
  3275. '(2 'org-headline-done t))
  3276. nil)
  3277. ;; Priorities
  3278. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3279. ;; Special keywords
  3280. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3281. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3282. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3283. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3284. ;; Emphasis
  3285. (if em
  3286. (if (featurep 'xemacs)
  3287. '(org-do-emphasis-faces (0 nil append))
  3288. '(org-do-emphasis-faces)))
  3289. ;; Checkboxes
  3290. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3291. 2 'bold prepend)
  3292. (if org-provide-checkbox-statistics
  3293. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3294. (0 (org-get-checkbox-statistics-face) t)))
  3295. ;; Description list items
  3296. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*?\\) ::"
  3297. 2 'bold prepend)
  3298. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3299. '(1 'org-archived prepend))
  3300. ;; Specials
  3301. '(org-do-latex-and-special-faces)
  3302. ;; Code
  3303. '(org-activate-code (1 'org-code t))
  3304. ;; COMMENT
  3305. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3306. "\\|" org-quote-string "\\)\\>")
  3307. '(1 'org-special-keyword t))
  3308. '("^#.*" (0 'font-lock-comment-face t))
  3309. )))
  3310. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3311. ;; Now set the full font-lock-keywords
  3312. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3313. (org-set-local 'font-lock-defaults
  3314. '(org-font-lock-keywords t nil nil backward-paragraph))
  3315. (kill-local-variable 'font-lock-keywords) nil))
  3316. (defvar org-m nil)
  3317. (defvar org-l nil)
  3318. (defvar org-f nil)
  3319. (defun org-get-level-face (n)
  3320. "Get the right face for match N in font-lock matching of healdines."
  3321. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3322. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3323. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3324. (cond
  3325. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3326. ((eq n 2) org-f)
  3327. (t (if org-level-color-stars-only nil org-f))))
  3328. (defun org-get-todo-face (kwd)
  3329. "Get the right face for a TODO keyword KWD.
  3330. If KWD is a number, get the corresponding match group."
  3331. (if (numberp kwd) (setq kwd (match-string kwd)))
  3332. (or (cdr (assoc kwd org-todo-keyword-faces))
  3333. (and (member kwd org-done-keywords) 'org-done)
  3334. 'org-todo))
  3335. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3336. "Remove fontification and activation overlays from links."
  3337. (font-lock-default-unfontify-region beg end)
  3338. (let* ((buffer-undo-list t)
  3339. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3340. (inhibit-modification-hooks t)
  3341. deactivate-mark buffer-file-name buffer-file-truename)
  3342. (remove-text-properties beg end
  3343. '(mouse-face t keymap t org-linked-text t
  3344. invisible t intangible t))))
  3345. ;;;; Visibility cycling, including org-goto and indirect buffer
  3346. ;;; Cycling
  3347. (defvar org-cycle-global-status nil)
  3348. (make-variable-buffer-local 'org-cycle-global-status)
  3349. (defvar org-cycle-subtree-status nil)
  3350. (make-variable-buffer-local 'org-cycle-subtree-status)
  3351. ;;;###autoload
  3352. (defun org-cycle (&optional arg)
  3353. "Visibility cycling for Org-mode.
  3354. - When this function is called with a prefix argument, rotate the entire
  3355. buffer through 3 states (global cycling)
  3356. 1. OVERVIEW: Show only top-level headlines.
  3357. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3358. 3. SHOW ALL: Show everything.
  3359. When called with two C-c C-u prefixes, switch to the startup visibility,
  3360. determined by the variable `org-startup-folded', and by any VISIBILITY
  3361. properties in the buffer.
  3362. - When point is at the beginning of a headline, rotate the subtree started
  3363. by this line through 3 different states (local cycling)
  3364. 1. FOLDED: Only the main headline is shown.
  3365. 2. CHILDREN: The main headline and the direct children are shown.
  3366. From this state, you can move to one of the children
  3367. and zoom in further.
  3368. 3. SUBTREE: Show the entire subtree, including body text.
  3369. - When there is a numeric prefix, go up to a heading with level ARG, do
  3370. a `show-subtree' and return to the previous cursor position. If ARG
  3371. is negative, go up that many levels.
  3372. - When point is not at the beginning of a headline, execute the global
  3373. binding for TAB, which is re-indenting the line. See the option
  3374. `org-cycle-emulate-tab' for details.
  3375. - Special case: if point is at the beginning of the buffer and there is
  3376. no headline in line 1, this function will act as if called with prefix arg.
  3377. But only if also the variable `org-cycle-global-at-bob' is t."
  3378. (interactive "P")
  3379. (org-load-modules-maybe)
  3380. (let* ((outline-regexp
  3381. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3382. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3383. outline-regexp))
  3384. (bob-special (and org-cycle-global-at-bob (bobp)
  3385. (not (looking-at outline-regexp))))
  3386. (org-cycle-hook
  3387. (if bob-special
  3388. (delq 'org-optimize-window-after-visibility-change
  3389. (copy-sequence org-cycle-hook))
  3390. org-cycle-hook))
  3391. (pos (point)))
  3392. (if (or bob-special (equal arg '(4)))
  3393. ;; special case: use global cycling
  3394. (setq arg t))
  3395. (cond
  3396. ((equal arg '(16))
  3397. (org-set-startup-visibility)
  3398. (message "Startup visibility, plus VISIBILITY properties."))
  3399. ((org-at-table-p 'any)
  3400. ;; Enter the table or move to the next field in the table
  3401. (or (org-table-recognize-table.el)
  3402. (progn
  3403. (if arg (org-table-edit-field t)
  3404. (org-table-justify-field-maybe)
  3405. (call-interactively 'org-table-next-field)))))
  3406. ((eq arg t) ;; Global cycling
  3407. (cond
  3408. ((and (eq last-command this-command)
  3409. (eq org-cycle-global-status 'overview))
  3410. ;; We just created the overview - now do table of contents
  3411. ;; This can be slow in very large buffers, so indicate action
  3412. (message "CONTENTS...")
  3413. (org-content)
  3414. (message "CONTENTS...done")
  3415. (setq org-cycle-global-status 'contents)
  3416. (run-hook-with-args 'org-cycle-hook 'contents))
  3417. ((and (eq last-command this-command)
  3418. (eq org-cycle-global-status 'contents))
  3419. ;; We just showed the table of contents - now show everything
  3420. (show-all)
  3421. (message "SHOW ALL")
  3422. (setq org-cycle-global-status 'all)
  3423. (run-hook-with-args 'org-cycle-hook 'all))
  3424. (t
  3425. ;; Default action: go to overview
  3426. (org-overview)
  3427. (message "OVERVIEW")
  3428. (setq org-cycle-global-status 'overview)
  3429. (run-hook-with-args 'org-cycle-hook 'overview))))
  3430. ((and org-drawers org-drawer-regexp
  3431. (save-excursion
  3432. (beginning-of-line 1)
  3433. (looking-at org-drawer-regexp)))
  3434. ;; Toggle block visibility
  3435. (org-flag-drawer
  3436. (not (get-char-property (match-end 0) 'invisible))))
  3437. ((integerp arg)
  3438. ;; Show-subtree, ARG levels up from here.
  3439. (save-excursion
  3440. (org-back-to-heading)
  3441. (outline-up-heading (if (< arg 0) (- arg)
  3442. (- (funcall outline-level) arg)))
  3443. (org-show-subtree)))
  3444. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3445. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3446. ;; At a heading: rotate between three different views
  3447. (org-back-to-heading)
  3448. (let ((goal-column 0) eoh eol eos)
  3449. ;; First, some boundaries
  3450. (save-excursion
  3451. (org-back-to-heading)
  3452. (save-excursion
  3453. (beginning-of-line 2)
  3454. (while (and (not (eobp)) ;; this is like `next-line'
  3455. (get-char-property (1- (point)) 'invisible))
  3456. (beginning-of-line 2)) (setq eol (point)))
  3457. (outline-end-of-heading) (setq eoh (point))
  3458. (org-end-of-subtree t)
  3459. (unless (eobp)
  3460. (skip-chars-forward " \t\n")
  3461. (beginning-of-line 1) ; in case this is an item
  3462. )
  3463. (setq eos (1- (point))))
  3464. ;; Find out what to do next and set `this-command'
  3465. (cond
  3466. ((= eos eoh)
  3467. ;; Nothing is hidden behind this heading
  3468. (message "EMPTY ENTRY")
  3469. (setq org-cycle-subtree-status nil)
  3470. (save-excursion
  3471. (goto-char eos)
  3472. (outline-next-heading)
  3473. (if (org-invisible-p) (org-flag-heading nil))))
  3474. ((or (>= eol eos)
  3475. (not (string-match "\\S-" (buffer-substring eol eos))))
  3476. ;; Entire subtree is hidden in one line: open it
  3477. (org-show-entry)
  3478. (show-children)
  3479. (message "CHILDREN")
  3480. (save-excursion
  3481. (goto-char eos)
  3482. (outline-next-heading)
  3483. (if (org-invisible-p) (org-flag-heading nil)))
  3484. (setq org-cycle-subtree-status 'children)
  3485. (run-hook-with-args 'org-cycle-hook 'children))
  3486. ((and (eq last-command this-command)
  3487. (eq org-cycle-subtree-status 'children))
  3488. ;; We just showed the children, now show everything.
  3489. (org-show-subtree)
  3490. (message "SUBTREE")
  3491. (setq org-cycle-subtree-status 'subtree)
  3492. (run-hook-with-args 'org-cycle-hook 'subtree))
  3493. (t
  3494. ;; Default action: hide the subtree.
  3495. (hide-subtree)
  3496. (message "FOLDED")
  3497. (setq org-cycle-subtree-status 'folded)
  3498. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3499. ;; TAB emulation
  3500. (buffer-read-only (org-back-to-heading))
  3501. ((org-try-cdlatex-tab))
  3502. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3503. (or (not (bolp))
  3504. (not (looking-at outline-regexp))))
  3505. (call-interactively (global-key-binding "\t")))
  3506. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3507. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3508. (or (and (eq org-cycle-emulate-tab 'white)
  3509. (= (match-end 0) (point-at-eol)))
  3510. (and (eq org-cycle-emulate-tab 'whitestart)
  3511. (>= (match-end 0) pos))))
  3512. t
  3513. (eq org-cycle-emulate-tab t))
  3514. (call-interactively (global-key-binding "\t")))
  3515. (t (save-excursion
  3516. (org-back-to-heading)
  3517. (org-cycle))))))
  3518. ;;;###autoload
  3519. (defun org-global-cycle (&optional arg)
  3520. "Cycle the global visibility. For details see `org-cycle'.
  3521. With C-u prefix arg, switch to startup visibility.
  3522. With a numeric prefix, show all headlines up to that level."
  3523. (interactive "P")
  3524. (let ((org-cycle-include-plain-lists
  3525. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3526. (cond
  3527. ((integerp arg)
  3528. (show-all)
  3529. (hide-sublevels arg)
  3530. (setq org-cycle-global-status 'contents))
  3531. ((equal arg '(4))
  3532. (org-set-startup-visibility)
  3533. (message "Startup visibility, plus VISIBILITY properties."))
  3534. (t
  3535. (org-cycle '(4))))))
  3536. (defun org-set-startup-visibility ()
  3537. "Set the visibility required by startup options and properties."
  3538. (cond
  3539. ((eq org-startup-folded t)
  3540. (org-cycle '(4)))
  3541. ((eq org-startup-folded 'content)
  3542. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3543. (org-cycle '(4)) (org-cycle '(4)))))
  3544. (org-set-visibility-according-to-property 'no-cleanup)
  3545. (org-cycle-hide-archived-subtrees 'all)
  3546. (org-cycle-hide-drawers 'all)
  3547. (org-cycle-show-empty-lines 'all))
  3548. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3549. "Switch subtree visibilities according to :VISIBILITY: property."
  3550. (interactive)
  3551. (let (state)
  3552. (save-excursion
  3553. (goto-char (point-min))
  3554. (while (re-search-forward
  3555. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3556. nil t)
  3557. (setq state (match-string 1))
  3558. (save-excursion
  3559. (org-back-to-heading t)
  3560. (hide-subtree)
  3561. (org-reveal)
  3562. (cond
  3563. ((equal state '("fold" "folded"))
  3564. (hide-subtree))
  3565. ((equal state "children")
  3566. (org-show-hidden-entry)
  3567. (show-children))
  3568. ((equal state "content")
  3569. (save-excursion
  3570. (save-restriction
  3571. (org-narrow-to-subtree)
  3572. (org-content))))
  3573. ((member state '("all" "showall"))
  3574. (show-subtree)))))
  3575. (unless no-cleanup
  3576. (org-cycle-hide-archived-subtrees 'all)
  3577. (org-cycle-hide-drawers 'all)
  3578. (org-cycle-show-empty-lines 'all)))))
  3579. (defun org-overview ()
  3580. "Switch to overview mode, shoing only top-level headlines.
  3581. Really, this shows all headlines with level equal or greater than the level
  3582. of the first headline in the buffer. This is important, because if the
  3583. first headline is not level one, then (hide-sublevels 1) gives confusing
  3584. results."
  3585. (interactive)
  3586. (let ((level (save-excursion
  3587. (goto-char (point-min))
  3588. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3589. (progn
  3590. (goto-char (match-beginning 0))
  3591. (funcall outline-level))))))
  3592. (and level (hide-sublevels level))))
  3593. (defun org-content (&optional arg)
  3594. "Show all headlines in the buffer, like a table of contents.
  3595. With numerical argument N, show content up to level N."
  3596. (interactive "P")
  3597. (save-excursion
  3598. ;; Visit all headings and show their offspring
  3599. (and (integerp arg) (org-overview))
  3600. (goto-char (point-max))
  3601. (catch 'exit
  3602. (while (and (progn (condition-case nil
  3603. (outline-previous-visible-heading 1)
  3604. (error (goto-char (point-min))))
  3605. t)
  3606. (looking-at outline-regexp))
  3607. (if (integerp arg)
  3608. (show-children (1- arg))
  3609. (show-branches))
  3610. (if (bobp) (throw 'exit nil))))))
  3611. (defun org-optimize-window-after-visibility-change (state)
  3612. "Adjust the window after a change in outline visibility.
  3613. This function is the default value of the hook `org-cycle-hook'."
  3614. (when (get-buffer-window (current-buffer))
  3615. (cond
  3616. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3617. ; ((eq state 'overview) (org-beginning-of-line))
  3618. ((eq state 'content) nil)
  3619. ((eq state 'all) nil)
  3620. ((eq state 'folded) nil)
  3621. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3622. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3623. (defun org-compact-display-after-subtree-move ()
  3624. (let (beg end)
  3625. (save-excursion
  3626. (if (org-up-heading-safe)
  3627. (progn
  3628. (hide-subtree)
  3629. (show-entry)
  3630. (show-children)
  3631. (org-cycle-show-empty-lines 'children)
  3632. (org-cycle-hide-drawers 'children))
  3633. (org-overview)))))
  3634. (defun org-cycle-show-empty-lines (state)
  3635. "Show empty lines above all visible headlines.
  3636. The region to be covered depends on STATE when called through
  3637. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3638. entire buffer covered. Note that an empty line is only shown if there
  3639. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3640. (when (> org-cycle-separator-lines 0)
  3641. (save-excursion
  3642. (let* ((n org-cycle-separator-lines)
  3643. (re (cond
  3644. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3645. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3646. (t (let ((ns (number-to-string (- n 2))))
  3647. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3648. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3649. beg end)
  3650. (cond
  3651. ((memq state '(overview contents t))
  3652. (setq beg (point-min) end (point-max)))
  3653. ((memq state '(children folded))
  3654. (setq beg (point) end (progn (org-end-of-subtree t t)
  3655. (beginning-of-line 2)
  3656. (point)))))
  3657. (when beg
  3658. (goto-char beg)
  3659. (while (re-search-forward re end t)
  3660. (if (not (get-char-property (match-end 1) 'invisible))
  3661. (outline-flag-region
  3662. (match-beginning 1) (match-end 1) nil)))))))
  3663. ;; Never hide empty lines at the end of the file.
  3664. (save-excursion
  3665. (goto-char (point-max))
  3666. (outline-previous-heading)
  3667. (outline-end-of-heading)
  3668. (if (and (looking-at "[ \t\n]+")
  3669. (= (match-end 0) (point-max)))
  3670. (outline-flag-region (point) (match-end 0) nil))))
  3671. (defun org-cycle-hide-drawers (state)
  3672. "Re-hide all drawers after a visibility state change."
  3673. (when (and (org-mode-p)
  3674. (not (memq state '(overview folded))))
  3675. (save-excursion
  3676. (let* ((globalp (memq state '(contents all)))
  3677. (beg (if globalp (point-min) (point)))
  3678. (end (if globalp (point-max) (org-end-of-subtree t))))
  3679. (goto-char beg)
  3680. (while (re-search-forward org-drawer-regexp end t)
  3681. (org-flag-drawer t))))))
  3682. (defun org-flag-drawer (flag)
  3683. (save-excursion
  3684. (beginning-of-line 1)
  3685. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3686. (let ((b (match-end 0))
  3687. (outline-regexp org-outline-regexp))
  3688. (if (re-search-forward
  3689. "^[ \t]*:END:"
  3690. (save-excursion (outline-next-heading) (point)) t)
  3691. (outline-flag-region b (point-at-eol) flag)
  3692. (error ":END: line missing"))))))
  3693. (defun org-subtree-end-visible-p ()
  3694. "Is the end of the current subtree visible?"
  3695. (pos-visible-in-window-p
  3696. (save-excursion (org-end-of-subtree t) (point))))
  3697. (defun org-first-headline-recenter (&optional N)
  3698. "Move cursor to the first headline and recenter the headline.
  3699. Optional argument N means, put the headline into the Nth line of the window."
  3700. (goto-char (point-min))
  3701. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3702. (beginning-of-line)
  3703. (recenter (prefix-numeric-value N))))
  3704. ;;; Org-goto
  3705. (defvar org-goto-window-configuration nil)
  3706. (defvar org-goto-marker nil)
  3707. (defvar org-goto-map
  3708. (let ((map (make-sparse-keymap)))
  3709. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3710. (while (setq cmd (pop cmds))
  3711. (substitute-key-definition cmd cmd map global-map)))
  3712. (suppress-keymap map)
  3713. (org-defkey map "\C-m" 'org-goto-ret)
  3714. (org-defkey map [(return)] 'org-goto-ret)
  3715. (org-defkey map [(left)] 'org-goto-left)
  3716. (org-defkey map [(right)] 'org-goto-right)
  3717. (org-defkey map [(control ?g)] 'org-goto-quit)
  3718. (org-defkey map "\C-i" 'org-cycle)
  3719. (org-defkey map [(tab)] 'org-cycle)
  3720. (org-defkey map [(down)] 'outline-next-visible-heading)
  3721. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3722. (if org-goto-auto-isearch
  3723. (if (fboundp 'define-key-after)
  3724. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3725. nil)
  3726. (org-defkey map "q" 'org-goto-quit)
  3727. (org-defkey map "n" 'outline-next-visible-heading)
  3728. (org-defkey map "p" 'outline-previous-visible-heading)
  3729. (org-defkey map "f" 'outline-forward-same-level)
  3730. (org-defkey map "b" 'outline-backward-same-level)
  3731. (org-defkey map "u" 'outline-up-heading))
  3732. (org-defkey map "/" 'org-occur)
  3733. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3734. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3735. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3736. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3737. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3738. map))
  3739. (defconst org-goto-help
  3740. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3741. RET=jump to location [Q]uit and return to previous location
  3742. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3743. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3744. (defun org-goto (&optional alternative-interface)
  3745. "Look up a different location in the current file, keeping current visibility.
  3746. When you want look-up or go to a different location in a document, the
  3747. fastest way is often to fold the entire buffer and then dive into the tree.
  3748. This method has the disadvantage, that the previous location will be folded,
  3749. which may not be what you want.
  3750. This command works around this by showing a copy of the current buffer
  3751. in an indirect buffer, in overview mode. You can dive into the tree in
  3752. that copy, use org-occur and incremental search to find a location.
  3753. When pressing RET or `Q', the command returns to the original buffer in
  3754. which the visibility is still unchanged. After RET is will also jump to
  3755. the location selected in the indirect buffer and expose the
  3756. the headline hierarchy above."
  3757. (interactive "P")
  3758. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3759. (org-refile-use-outline-path t)
  3760. (interface
  3761. (if (not alternative-interface)
  3762. org-goto-interface
  3763. (if (eq org-goto-interface 'outline)
  3764. 'outline-path-completion
  3765. 'outline)))
  3766. (org-goto-start-pos (point))
  3767. (selected-point
  3768. (if (eq interface 'outline)
  3769. (car (org-get-location (current-buffer) org-goto-help))
  3770. (nth 3 (org-refile-get-location "Goto: ")))))
  3771. (if selected-point
  3772. (progn
  3773. (org-mark-ring-push org-goto-start-pos)
  3774. (goto-char selected-point)
  3775. (if (or (org-invisible-p) (org-invisible-p2))
  3776. (org-show-context 'org-goto)))
  3777. (message "Quit"))))
  3778. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3779. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3780. (defvar org-goto-local-auto-isearch-map) ; defined below
  3781. (defun org-get-location (buf help)
  3782. "Let the user select a location in the Org-mode buffer BUF.
  3783. This function uses a recursive edit. It returns the selected position
  3784. or nil."
  3785. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3786. (isearch-hide-immediately nil)
  3787. (isearch-search-fun-function
  3788. (lambda () 'org-goto-local-search-forward-headings))
  3789. (org-goto-selected-point org-goto-exit-command))
  3790. (save-excursion
  3791. (save-window-excursion
  3792. (delete-other-windows)
  3793. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3794. (switch-to-buffer
  3795. (condition-case nil
  3796. (make-indirect-buffer (current-buffer) "*org-goto*")
  3797. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3798. (with-output-to-temp-buffer "*Help*"
  3799. (princ help))
  3800. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3801. (setq buffer-read-only nil)
  3802. (let ((org-startup-truncated t)
  3803. (org-startup-folded nil)
  3804. (org-startup-align-all-tables nil))
  3805. (org-mode)
  3806. (org-overview))
  3807. (setq buffer-read-only t)
  3808. (if (and (boundp 'org-goto-start-pos)
  3809. (integer-or-marker-p org-goto-start-pos))
  3810. (let ((org-show-hierarchy-above t)
  3811. (org-show-siblings t)
  3812. (org-show-following-heading t))
  3813. (goto-char org-goto-start-pos)
  3814. (and (org-invisible-p) (org-show-context)))
  3815. (goto-char (point-min)))
  3816. (org-beginning-of-line)
  3817. (message "Select location and press RET")
  3818. (use-local-map org-goto-map)
  3819. (recursive-edit)
  3820. ))
  3821. (kill-buffer "*org-goto*")
  3822. (cons org-goto-selected-point org-goto-exit-command)))
  3823. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3824. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3825. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3826. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3827. (defun org-goto-local-search-forward-headings (string bound noerror)
  3828. "Search and make sure that anu matches are in headlines."
  3829. (catch 'return
  3830. (while (search-forward string bound noerror)
  3831. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3832. (and (member :headline context)
  3833. (not (member :tags context))))
  3834. (throw 'return (point))))))
  3835. (defun org-goto-local-auto-isearch ()
  3836. "Start isearch."
  3837. (interactive)
  3838. (goto-char (point-min))
  3839. (let ((keys (this-command-keys)))
  3840. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3841. (isearch-mode t)
  3842. (isearch-process-search-char (string-to-char keys)))))
  3843. (defun org-goto-ret (&optional arg)
  3844. "Finish `org-goto' by going to the new location."
  3845. (interactive "P")
  3846. (setq org-goto-selected-point (point)
  3847. org-goto-exit-command 'return)
  3848. (throw 'exit nil))
  3849. (defun org-goto-left ()
  3850. "Finish `org-goto' by going to the new location."
  3851. (interactive)
  3852. (if (org-on-heading-p)
  3853. (progn
  3854. (beginning-of-line 1)
  3855. (setq org-goto-selected-point (point)
  3856. org-goto-exit-command 'left)
  3857. (throw 'exit nil))
  3858. (error "Not on a heading")))
  3859. (defun org-goto-right ()
  3860. "Finish `org-goto' by going to the new location."
  3861. (interactive)
  3862. (if (org-on-heading-p)
  3863. (progn
  3864. (setq org-goto-selected-point (point)
  3865. org-goto-exit-command 'right)
  3866. (throw 'exit nil))
  3867. (error "Not on a heading")))
  3868. (defun org-goto-quit ()
  3869. "Finish `org-goto' without cursor motion."
  3870. (interactive)
  3871. (setq org-goto-selected-point nil)
  3872. (setq org-goto-exit-command 'quit)
  3873. (throw 'exit nil))
  3874. ;;; Indirect buffer display of subtrees
  3875. (defvar org-indirect-dedicated-frame nil
  3876. "This is the frame being used for indirect tree display.")
  3877. (defvar org-last-indirect-buffer nil)
  3878. (defun org-tree-to-indirect-buffer (&optional arg)
  3879. "Create indirect buffer and narrow it to current subtree.
  3880. With numerical prefix ARG, go up to this level and then take that tree.
  3881. If ARG is negative, go up that many levels.
  3882. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3883. indirect buffer previously made with this command, to avoid proliferation of
  3884. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3885. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3886. is kept so that you can work with several indirect buffers at the same time.
  3887. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3888. requests that a new frame be made for the new buffer, so that the dedicated
  3889. frame is not changed."
  3890. (interactive "P")
  3891. (let ((cbuf (current-buffer))
  3892. (cwin (selected-window))
  3893. (pos (point))
  3894. beg end level heading ibuf)
  3895. (save-excursion
  3896. (org-back-to-heading t)
  3897. (when (numberp arg)
  3898. (setq level (org-outline-level))
  3899. (if (< arg 0) (setq arg (+ level arg)))
  3900. (while (> (setq level (org-outline-level)) arg)
  3901. (outline-up-heading 1 t)))
  3902. (setq beg (point)
  3903. heading (org-get-heading))
  3904. (org-end-of-subtree t) (setq end (point)))
  3905. (if (and (buffer-live-p org-last-indirect-buffer)
  3906. (not (eq org-indirect-buffer-display 'new-frame))
  3907. (not arg))
  3908. (kill-buffer org-last-indirect-buffer))
  3909. (setq ibuf (org-get-indirect-buffer cbuf)
  3910. org-last-indirect-buffer ibuf)
  3911. (cond
  3912. ((or (eq org-indirect-buffer-display 'new-frame)
  3913. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3914. (select-frame (make-frame))
  3915. (delete-other-windows)
  3916. (switch-to-buffer ibuf)
  3917. (org-set-frame-title heading))
  3918. ((eq org-indirect-buffer-display 'dedicated-frame)
  3919. (raise-frame
  3920. (select-frame (or (and org-indirect-dedicated-frame
  3921. (frame-live-p org-indirect-dedicated-frame)
  3922. org-indirect-dedicated-frame)
  3923. (setq org-indirect-dedicated-frame (make-frame)))))
  3924. (delete-other-windows)
  3925. (switch-to-buffer ibuf)
  3926. (org-set-frame-title (concat "Indirect: " heading)))
  3927. ((eq org-indirect-buffer-display 'current-window)
  3928. (switch-to-buffer ibuf))
  3929. ((eq org-indirect-buffer-display 'other-window)
  3930. (pop-to-buffer ibuf))
  3931. (t (error "Invalid value.")))
  3932. (if (featurep 'xemacs)
  3933. (save-excursion (org-mode) (turn-on-font-lock)))
  3934. (narrow-to-region beg end)
  3935. (show-all)
  3936. (goto-char pos)
  3937. (and (window-live-p cwin) (select-window cwin))))
  3938. (defun org-get-indirect-buffer (&optional buffer)
  3939. (setq buffer (or buffer (current-buffer)))
  3940. (let ((n 1) (base (buffer-name buffer)) bname)
  3941. (while (buffer-live-p
  3942. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3943. (setq n (1+ n)))
  3944. (condition-case nil
  3945. (make-indirect-buffer buffer bname 'clone)
  3946. (error (make-indirect-buffer buffer bname)))))
  3947. (defun org-set-frame-title (title)
  3948. "Set the title of the current frame to the string TITLE."
  3949. ;; FIXME: how to name a single frame in XEmacs???
  3950. (unless (featurep 'xemacs)
  3951. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3952. ;;;; Structure editing
  3953. ;;; Inserting headlines
  3954. (defun org-insert-heading (&optional force-heading)
  3955. "Insert a new heading or item with same depth at point.
  3956. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3957. If point is at the beginning of a headline, insert a sibling before the
  3958. current headline. If point is not at the beginning, do not split the line,
  3959. but create the new hedline after the current line."
  3960. (interactive "P")
  3961. (if (= (buffer-size) 0)
  3962. (insert "\n* ")
  3963. (when (or force-heading (not (org-insert-item)))
  3964. (let* ((head (save-excursion
  3965. (condition-case nil
  3966. (progn
  3967. (org-back-to-heading)
  3968. (match-string 0))
  3969. (error "*"))))
  3970. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3971. pos)
  3972. (cond
  3973. ((and (org-on-heading-p) (bolp)
  3974. (or (bobp)
  3975. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3976. ;; insert before the current line
  3977. (open-line (if blank 2 1)))
  3978. ((and (bolp)
  3979. (or (bobp)
  3980. (save-excursion
  3981. (backward-char 1) (not (org-invisible-p)))))
  3982. ;; insert right here
  3983. nil)
  3984. (t
  3985. ;; in the middle of the line
  3986. (org-show-entry)
  3987. (let ((split
  3988. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3989. tags pos)
  3990. (if (org-on-heading-p)
  3991. (progn
  3992. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3993. (setq tags (and (match-end 2) (match-string 2)))
  3994. (and (match-end 1)
  3995. (delete-region (match-beginning 1) (match-end 1)))
  3996. (setq pos (point-at-bol))
  3997. (or split (end-of-line 1))
  3998. (delete-horizontal-space)
  3999. (newline (if blank 2 1))
  4000. (when tags
  4001. (save-excursion
  4002. (goto-char pos)
  4003. (end-of-line 1)
  4004. (insert " " tags)
  4005. (org-set-tags nil 'align))))
  4006. (or split (end-of-line 1))
  4007. (newline (if blank 2 1))))))
  4008. (insert head) (just-one-space)
  4009. (setq pos (point))
  4010. (end-of-line 1)
  4011. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4012. (run-hooks 'org-insert-heading-hook)))))
  4013. (defun org-get-heading (&optional no-tags)
  4014. "Return the heading of the current entry, without the stars."
  4015. (save-excursion
  4016. (org-back-to-heading t)
  4017. (if (looking-at
  4018. (if no-tags
  4019. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4020. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4021. (match-string 1) "")))
  4022. (defun org-insert-heading-after-current ()
  4023. "Insert a new heading with same level as current, after current subtree."
  4024. (interactive)
  4025. (org-back-to-heading)
  4026. (org-insert-heading)
  4027. (org-move-subtree-down)
  4028. (end-of-line 1))
  4029. (defun org-insert-todo-heading (arg)
  4030. "Insert a new heading with the same level and TODO state as current heading.
  4031. If the heading has no TODO state, or if the state is DONE, use the first
  4032. state (TODO by default). Also with prefix arg, force first state."
  4033. (interactive "P")
  4034. (when (not (org-insert-item 'checkbox))
  4035. (org-insert-heading)
  4036. (save-excursion
  4037. (org-back-to-heading)
  4038. (outline-previous-heading)
  4039. (looking-at org-todo-line-regexp))
  4040. (if (or arg
  4041. (not (match-beginning 2))
  4042. (member (match-string 2) org-done-keywords))
  4043. (insert (car org-todo-keywords-1) " ")
  4044. (insert (match-string 2) " "))))
  4045. (defun org-insert-subheading (arg)
  4046. "Insert a new subheading and demote it.
  4047. Works for outline headings and for plain lists alike."
  4048. (interactive "P")
  4049. (org-insert-heading arg)
  4050. (cond
  4051. ((org-on-heading-p) (org-do-demote))
  4052. ((org-at-item-p) (org-indent-item 1))))
  4053. (defun org-insert-todo-subheading (arg)
  4054. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4055. Works for outline headings and for plain lists alike."
  4056. (interactive "P")
  4057. (org-insert-todo-heading arg)
  4058. (cond
  4059. ((org-on-heading-p) (org-do-demote))
  4060. ((org-at-item-p) (org-indent-item 1))))
  4061. ;;; Promotion and Demotion
  4062. (defun org-promote-subtree ()
  4063. "Promote the entire subtree.
  4064. See also `org-promote'."
  4065. (interactive)
  4066. (save-excursion
  4067. (org-map-tree 'org-promote))
  4068. (org-fix-position-after-promote))
  4069. (defun org-demote-subtree ()
  4070. "Demote the entire subtree. See `org-demote'.
  4071. See also `org-promote'."
  4072. (interactive)
  4073. (save-excursion
  4074. (org-map-tree 'org-demote))
  4075. (org-fix-position-after-promote))
  4076. (defun org-do-promote ()
  4077. "Promote the current heading higher up the tree.
  4078. If the region is active in `transient-mark-mode', promote all headings
  4079. in the region."
  4080. (interactive)
  4081. (save-excursion
  4082. (if (org-region-active-p)
  4083. (org-map-region 'org-promote (region-beginning) (region-end))
  4084. (org-promote)))
  4085. (org-fix-position-after-promote))
  4086. (defun org-do-demote ()
  4087. "Demote the current heading lower down the tree.
  4088. If the region is active in `transient-mark-mode', demote all headings
  4089. in the region."
  4090. (interactive)
  4091. (save-excursion
  4092. (if (org-region-active-p)
  4093. (org-map-region 'org-demote (region-beginning) (region-end))
  4094. (org-demote)))
  4095. (org-fix-position-after-promote))
  4096. (defun org-fix-position-after-promote ()
  4097. "Make sure that after pro/demotion cursor position is right."
  4098. (let ((pos (point)))
  4099. (when (save-excursion
  4100. (beginning-of-line 1)
  4101. (looking-at org-todo-line-regexp)
  4102. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4103. (cond ((eobp) (insert " "))
  4104. ((eolp) (insert " "))
  4105. ((equal (char-after) ?\ ) (forward-char 1))))))
  4106. (defun org-reduced-level (l)
  4107. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4108. (defun org-get-valid-level (level &optional change)
  4109. "Rectify a level change under the influence of `org-odd-levels-only'
  4110. LEVEL is a current level, CHANGE is by how much the level should be
  4111. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4112. even level numbers will become the next higher odd number."
  4113. (if org-odd-levels-only
  4114. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4115. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4116. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4117. (max 1 (+ level change))))
  4118. (if (boundp 'define-obsolete-function-alias)
  4119. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4120. (define-obsolete-function-alias 'org-get-legal-level
  4121. 'org-get-valid-level)
  4122. (define-obsolete-function-alias 'org-get-legal-level
  4123. 'org-get-valid-level "23.1")))
  4124. (defun org-promote ()
  4125. "Promote the current heading higher up the tree.
  4126. If the region is active in `transient-mark-mode', promote all headings
  4127. in the region."
  4128. (org-back-to-heading t)
  4129. (let* ((level (save-match-data (funcall outline-level)))
  4130. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4131. (diff (abs (- level (length up-head) -1))))
  4132. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4133. (replace-match up-head nil t)
  4134. ;; Fixup tag positioning
  4135. (and org-auto-align-tags (org-set-tags nil t))
  4136. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4137. (defun org-demote ()
  4138. "Demote the current heading lower down the tree.
  4139. If the region is active in `transient-mark-mode', demote all headings
  4140. in the region."
  4141. (org-back-to-heading t)
  4142. (let* ((level (save-match-data (funcall outline-level)))
  4143. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4144. (diff (abs (- level (length down-head) -1))))
  4145. (replace-match down-head nil t)
  4146. ;; Fixup tag positioning
  4147. (and org-auto-align-tags (org-set-tags nil t))
  4148. (if org-adapt-indentation (org-fixup-indentation diff))))
  4149. (defun org-map-tree (fun)
  4150. "Call FUN for every heading underneath the current one."
  4151. (org-back-to-heading)
  4152. (let ((level (funcall outline-level)))
  4153. (save-excursion
  4154. (funcall fun)
  4155. (while (and (progn
  4156. (outline-next-heading)
  4157. (> (funcall outline-level) level))
  4158. (not (eobp)))
  4159. (funcall fun)))))
  4160. (defun org-map-region (fun beg end)
  4161. "Call FUN for every heading between BEG and END."
  4162. (let ((org-ignore-region t))
  4163. (save-excursion
  4164. (setq end (copy-marker end))
  4165. (goto-char beg)
  4166. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4167. (< (point) end))
  4168. (funcall fun))
  4169. (while (and (progn
  4170. (outline-next-heading)
  4171. (< (point) end))
  4172. (not (eobp)))
  4173. (funcall fun)))))
  4174. (defun org-fixup-indentation (diff)
  4175. "Change the indentation in the current entry by DIFF
  4176. However, if any line in the current entry has no indentation, or if it
  4177. would end up with no indentation after the change, nothing at all is done."
  4178. (save-excursion
  4179. (let ((end (save-excursion (outline-next-heading)
  4180. (point-marker)))
  4181. (prohibit (if (> diff 0)
  4182. "^\\S-"
  4183. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4184. col)
  4185. (unless (save-excursion (end-of-line 1)
  4186. (re-search-forward prohibit end t))
  4187. (while (and (< (point) end)
  4188. (re-search-forward "^[ \t]+" end t))
  4189. (goto-char (match-end 0))
  4190. (setq col (current-column))
  4191. (if (< diff 0) (replace-match ""))
  4192. (indent-to (+ diff col))))
  4193. (move-marker end nil))))
  4194. (defun org-convert-to-odd-levels ()
  4195. "Convert an org-mode file with all levels allowed to one with odd levels.
  4196. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4197. level 5 etc."
  4198. (interactive)
  4199. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4200. (let ((org-odd-levels-only nil) n)
  4201. (save-excursion
  4202. (goto-char (point-min))
  4203. (while (re-search-forward "^\\*\\*+ " nil t)
  4204. (setq n (- (length (match-string 0)) 2))
  4205. (while (>= (setq n (1- n)) 0)
  4206. (org-demote))
  4207. (end-of-line 1))))))
  4208. (defun org-convert-to-oddeven-levels ()
  4209. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4210. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4211. section with an even level, conversion would destroy the structure of the file. An error
  4212. is signaled in this case."
  4213. (interactive)
  4214. (goto-char (point-min))
  4215. ;; First check if there are no even levels
  4216. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4217. (org-show-context t)
  4218. (error "Not all levels are odd in this file. Conversion not possible."))
  4219. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4220. (let ((org-odd-levels-only nil) n)
  4221. (save-excursion
  4222. (goto-char (point-min))
  4223. (while (re-search-forward "^\\*\\*+ " nil t)
  4224. (setq n (/ (1- (length (match-string 0))) 2))
  4225. (while (>= (setq n (1- n)) 0)
  4226. (org-promote))
  4227. (end-of-line 1))))))
  4228. (defun org-tr-level (n)
  4229. "Make N odd if required."
  4230. (if org-odd-levels-only (1+ (/ n 2)) n))
  4231. ;;; Vertical tree motion, cutting and pasting of subtrees
  4232. (defun org-move-subtree-up (&optional arg)
  4233. "Move the current subtree up past ARG headlines of the same level."
  4234. (interactive "p")
  4235. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4236. (defun org-move-subtree-down (&optional arg)
  4237. "Move the current subtree down past ARG headlines of the same level."
  4238. (interactive "p")
  4239. (setq arg (prefix-numeric-value arg))
  4240. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4241. 'outline-get-last-sibling))
  4242. (ins-point (make-marker))
  4243. (cnt (abs arg))
  4244. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4245. ;; Select the tree
  4246. (org-back-to-heading)
  4247. (setq beg0 (point))
  4248. (save-excursion
  4249. (setq ne-beg (org-back-over-empty-lines))
  4250. (setq beg (point)))
  4251. (save-match-data
  4252. (save-excursion (outline-end-of-heading)
  4253. (setq folded (org-invisible-p)))
  4254. (outline-end-of-subtree))
  4255. (outline-next-heading)
  4256. (setq ne-end (org-back-over-empty-lines))
  4257. (setq end (point))
  4258. (goto-char beg0)
  4259. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4260. ;; include less whitespace
  4261. (save-excursion
  4262. (goto-char beg)
  4263. (forward-line (- ne-beg ne-end))
  4264. (setq beg (point))))
  4265. ;; Find insertion point, with error handling
  4266. (while (> cnt 0)
  4267. (or (and (funcall movfunc) (looking-at outline-regexp))
  4268. (progn (goto-char beg0)
  4269. (error "Cannot move past superior level or buffer limit")))
  4270. (setq cnt (1- cnt)))
  4271. (if (> arg 0)
  4272. ;; Moving forward - still need to move over subtree
  4273. (progn (org-end-of-subtree t t)
  4274. (save-excursion
  4275. (org-back-over-empty-lines)
  4276. (or (bolp) (newline)))))
  4277. (setq ne-ins (org-back-over-empty-lines))
  4278. (move-marker ins-point (point))
  4279. (setq txt (buffer-substring beg end))
  4280. (org-save-markers-in-region beg end)
  4281. (delete-region beg end)
  4282. (outline-flag-region (1- beg) beg nil)
  4283. (outline-flag-region (1- (point)) (point) nil)
  4284. (let ((bbb (point)))
  4285. (insert-before-markers txt)
  4286. (org-reinstall-markers-in-region bbb)
  4287. (move-marker ins-point bbb))
  4288. (or (bolp) (insert "\n"))
  4289. (setq ins-end (point))
  4290. (goto-char ins-point)
  4291. (org-skip-whitespace)
  4292. (when (and (< arg 0)
  4293. (org-first-sibling-p)
  4294. (> ne-ins ne-beg))
  4295. ;; Move whitespace back to beginning
  4296. (save-excursion
  4297. (goto-char ins-end)
  4298. (let ((kill-whole-line t))
  4299. (kill-line (- ne-ins ne-beg)) (point)))
  4300. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4301. (move-marker ins-point nil)
  4302. (org-compact-display-after-subtree-move)
  4303. (unless folded
  4304. (org-show-entry)
  4305. (show-children)
  4306. (org-cycle-hide-drawers 'children))))
  4307. (defvar org-subtree-clip ""
  4308. "Clipboard for cut and paste of subtrees.
  4309. This is actually only a copy of the kill, because we use the normal kill
  4310. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4311. (defvar org-subtree-clip-folded nil
  4312. "Was the last copied subtree folded?
  4313. This is used to fold the tree back after pasting.")
  4314. (defun org-cut-subtree (&optional n)
  4315. "Cut the current subtree into the clipboard.
  4316. With prefix arg N, cut this many sequential subtrees.
  4317. This is a short-hand for marking the subtree and then cutting it."
  4318. (interactive "p")
  4319. (org-copy-subtree n 'cut))
  4320. (defun org-copy-subtree (&optional n cut force-store-markers)
  4321. "Cut the current subtree into the clipboard.
  4322. With prefix arg N, cut this many sequential subtrees.
  4323. This is a short-hand for marking the subtree and then copying it.
  4324. If CUT is non-nil, actually cut the subtree.
  4325. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4326. of some markers in the region, even if CUT is non-nil. This is
  4327. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4328. (interactive "p")
  4329. (let (beg end folded (beg0 (point)))
  4330. (if (interactive-p)
  4331. (org-back-to-heading nil) ; take what looks like a subtree
  4332. (org-back-to-heading t)) ; take what is really there
  4333. (org-back-over-empty-lines)
  4334. (setq beg (point))
  4335. (skip-chars-forward " \t\r\n")
  4336. (save-match-data
  4337. (save-excursion (outline-end-of-heading)
  4338. (setq folded (org-invisible-p)))
  4339. (condition-case nil
  4340. (outline-forward-same-level (1- n))
  4341. (error nil))
  4342. (org-end-of-subtree t t))
  4343. (org-back-over-empty-lines)
  4344. (setq end (point))
  4345. (goto-char beg0)
  4346. (when (> end beg)
  4347. (setq org-subtree-clip-folded folded)
  4348. (when (or cut force-store-markers)
  4349. (org-save-markers-in-region beg end))
  4350. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4351. (setq org-subtree-clip (current-kill 0))
  4352. (message "%s: Subtree(s) with %d characters"
  4353. (if cut "Cut" "Copied")
  4354. (length org-subtree-clip)))))
  4355. (defun org-paste-subtree (&optional level tree)
  4356. "Paste the clipboard as a subtree, with modification of headline level.
  4357. The entire subtree is promoted or demoted in order to match a new headline
  4358. level. By default, the new level is derived from the visible headings
  4359. before and after the insertion point, and taken to be the inferior headline
  4360. level of the two. So if the previous visible heading is level 3 and the
  4361. next is level 4 (or vice versa), level 4 will be used for insertion.
  4362. This makes sure that the subtree remains an independent subtree and does
  4363. not swallow low level entries.
  4364. You can also force a different level, either by using a numeric prefix
  4365. argument, or by inserting the heading marker by hand. For example, if the
  4366. cursor is after \"*****\", then the tree will be shifted to level 5.
  4367. If you want to insert the tree as is, just use \\[yank].
  4368. If optional TREE is given, use this text instead of the kill ring."
  4369. (interactive "P")
  4370. (unless (org-kill-is-subtree-p tree)
  4371. (error "%s"
  4372. (substitute-command-keys
  4373. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4374. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4375. (^re (concat "^\\(" outline-regexp "\\)"))
  4376. (re (concat "\\(" outline-regexp "\\)"))
  4377. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4378. (old-level (if (string-match ^re txt)
  4379. (- (match-end 0) (match-beginning 0) 1)
  4380. -1))
  4381. (force-level (cond (level (prefix-numeric-value level))
  4382. ((string-match
  4383. ^re_ (buffer-substring (point-at-bol) (point)))
  4384. (- (match-end 1) (match-beginning 1)))
  4385. (t nil)))
  4386. (previous-level (save-excursion
  4387. (condition-case nil
  4388. (progn
  4389. (outline-previous-visible-heading 1)
  4390. (if (looking-at re)
  4391. (- (match-end 0) (match-beginning 0) 1)
  4392. 1))
  4393. (error 1))))
  4394. (next-level (save-excursion
  4395. (condition-case nil
  4396. (progn
  4397. (or (looking-at outline-regexp)
  4398. (outline-next-visible-heading 1))
  4399. (if (looking-at re)
  4400. (- (match-end 0) (match-beginning 0) 1)
  4401. 1))
  4402. (error 1))))
  4403. (new-level (or force-level (max previous-level next-level)))
  4404. (shift (if (or (= old-level -1)
  4405. (= new-level -1)
  4406. (= old-level new-level))
  4407. 0
  4408. (- new-level old-level)))
  4409. (delta (if (> shift 0) -1 1))
  4410. (func (if (> shift 0) 'org-demote 'org-promote))
  4411. (org-odd-levels-only nil)
  4412. beg end)
  4413. ;; Remove the forced level indicator
  4414. (if force-level
  4415. (delete-region (point-at-bol) (point)))
  4416. ;; Paste
  4417. (beginning-of-line 1)
  4418. (org-back-over-empty-lines)
  4419. (setq beg (point))
  4420. (insert-before-markers txt)
  4421. (unless (string-match "\n\\'" txt) (insert "\n"))
  4422. (org-reinstall-markers-in-region beg)
  4423. (setq end (point))
  4424. (goto-char beg)
  4425. (skip-chars-forward " \t\n\r")
  4426. (setq beg (point))
  4427. ;; Shift if necessary
  4428. (unless (= shift 0)
  4429. (save-restriction
  4430. (narrow-to-region beg end)
  4431. (while (not (= shift 0))
  4432. (org-map-region func (point-min) (point-max))
  4433. (setq shift (+ delta shift)))
  4434. (goto-char (point-min))))
  4435. (when (interactive-p)
  4436. (message "Clipboard pasted as level %d subtree" new-level))
  4437. (if (and kill-ring
  4438. (eq org-subtree-clip (current-kill 0))
  4439. org-subtree-clip-folded)
  4440. ;; The tree was folded before it was killed/copied
  4441. (hide-subtree))))
  4442. (defun org-kill-is-subtree-p (&optional txt)
  4443. "Check if the current kill is an outline subtree, or a set of trees.
  4444. Returns nil if kill does not start with a headline, or if the first
  4445. headline level is not the largest headline level in the tree.
  4446. So this will actually accept several entries of equal levels as well,
  4447. which is OK for `org-paste-subtree'.
  4448. If optional TXT is given, check this string instead of the current kill."
  4449. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4450. (start-level (and kill
  4451. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4452. org-outline-regexp "\\)")
  4453. kill)
  4454. (- (match-end 2) (match-beginning 2) 1)))
  4455. (re (concat "^" org-outline-regexp))
  4456. (start (1+ (match-beginning 2))))
  4457. (if (not start-level)
  4458. (progn
  4459. nil) ;; does not even start with a heading
  4460. (catch 'exit
  4461. (while (setq start (string-match re kill (1+ start)))
  4462. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4463. (throw 'exit nil)))
  4464. t))))
  4465. (defvar org-markers-to-move nil
  4466. "Markers that should be moved with a cut-and-paste operation.
  4467. Those markers are stored together with their positions relative to
  4468. the start of the region.")
  4469. (defun org-save-markers-in-region (beg end)
  4470. "Check markers in region.
  4471. If these markers are between BEG and END, record their position relative
  4472. to BEG, so that after moving the block of text, we can put the markers back
  4473. into place.
  4474. This function gets called just before an entry or tree gets cut from the
  4475. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4476. called immediately, to move the markers with the entries."
  4477. (setq org-markers-to-move nil)
  4478. (when (featurep 'org-clock)
  4479. (org-clock-save-markers-for-cut-and-paste beg end))
  4480. (when (featurep 'org-agenda)
  4481. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4482. (defun org-check-and-save-marker (marker beg end)
  4483. "Check if MARKER is between BEG and END.
  4484. If yes, remember the marker and the distance to BEG."
  4485. (when (and (marker-buffer marker)
  4486. (equal (marker-buffer marker) (current-buffer)))
  4487. (if (and (>= marker beg) (< marker end))
  4488. (push (cons marker (- marker beg)) org-markers-to-move))))
  4489. (defun org-reinstall-markers-in-region (beg)
  4490. "Move all remembered markers to their position relative to BEG."
  4491. (mapc (lambda (x)
  4492. (move-marker (car x) (+ beg (cdr x))))
  4493. org-markers-to-move)
  4494. (setq org-markers-to-move nil))
  4495. (defun org-narrow-to-subtree ()
  4496. "Narrow buffer to the current subtree."
  4497. (interactive)
  4498. (save-excursion
  4499. (save-match-data
  4500. (narrow-to-region
  4501. (progn (org-back-to-heading) (point))
  4502. (progn (org-end-of-subtree t t) (point))))))
  4503. ;;; Outline Sorting
  4504. (defun org-sort (with-case)
  4505. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4506. Optional argument WITH-CASE means sort case-sensitively."
  4507. (interactive "P")
  4508. (if (org-at-table-p)
  4509. (org-call-with-arg 'org-table-sort-lines with-case)
  4510. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4511. (defun org-sort-remove-invisible (s)
  4512. (remove-text-properties 0 (length s) org-rm-props s)
  4513. (while (string-match org-bracket-link-regexp s)
  4514. (setq s (replace-match (if (match-end 2)
  4515. (match-string 3 s)
  4516. (match-string 1 s)) t t s)))
  4517. s)
  4518. (defvar org-priority-regexp) ; defined later in the file
  4519. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4520. "Sort entries on a certain level of an outline tree.
  4521. If there is an active region, the entries in the region are sorted.
  4522. Else, if the cursor is before the first entry, sort the top-level items.
  4523. Else, the children of the entry at point are sorted.
  4524. Sorting can be alphabetically, numerically, and by date/time as given by
  4525. the first time stamp in the entry. The command prompts for the sorting
  4526. type unless it has been given to the function through the SORTING-TYPE
  4527. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4528. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4529. called with point at the beginning of the record. It must return either
  4530. a string or a number that should serve as the sorting key for that record.
  4531. Comparing entries ignores case by default. However, with an optional argument
  4532. WITH-CASE, the sorting considers case as well."
  4533. (interactive "P")
  4534. (let ((case-func (if with-case 'identity 'downcase))
  4535. start beg end stars re re2
  4536. txt what tmp plain-list-p)
  4537. ;; Find beginning and end of region to sort
  4538. (cond
  4539. ((org-region-active-p)
  4540. ;; we will sort the region
  4541. (setq end (region-end)
  4542. what "region")
  4543. (goto-char (region-beginning))
  4544. (if (not (org-on-heading-p)) (outline-next-heading))
  4545. (setq start (point)))
  4546. ((org-at-item-p)
  4547. ;; we will sort this plain list
  4548. (org-beginning-of-item-list) (setq start (point))
  4549. (org-end-of-item-list) (setq end (point))
  4550. (goto-char start)
  4551. (setq plain-list-p t
  4552. what "plain list"))
  4553. ((or (org-on-heading-p)
  4554. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4555. ;; we will sort the children of the current headline
  4556. (org-back-to-heading)
  4557. (setq start (point)
  4558. end (progn (org-end-of-subtree t t)
  4559. (org-back-over-empty-lines)
  4560. (point))
  4561. what "children")
  4562. (goto-char start)
  4563. (show-subtree)
  4564. (outline-next-heading))
  4565. (t
  4566. ;; we will sort the top-level entries in this file
  4567. (goto-char (point-min))
  4568. (or (org-on-heading-p) (outline-next-heading))
  4569. (setq start (point) end (point-max) what "top-level")
  4570. (goto-char start)
  4571. (show-all)))
  4572. (setq beg (point))
  4573. (if (>= beg end) (error "Nothing to sort"))
  4574. (unless plain-list-p
  4575. (looking-at "\\(\\*+\\)")
  4576. (setq stars (match-string 1)
  4577. re (concat "^" (regexp-quote stars) " +")
  4578. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4579. txt (buffer-substring beg end))
  4580. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4581. (if (and (not (equal stars "*")) (string-match re2 txt))
  4582. (error "Region to sort contains a level above the first entry")))
  4583. (unless sorting-type
  4584. (message
  4585. (if plain-list-p
  4586. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4587. "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:")
  4588. what)
  4589. (setq sorting-type (read-char-exclusive))
  4590. (and (= (downcase sorting-type) ?f)
  4591. (setq getkey-func
  4592. (completing-read "Sort using function: "
  4593. obarray 'fboundp t nil nil))
  4594. (setq getkey-func (intern getkey-func)))
  4595. (and (= (downcase sorting-type) ?r)
  4596. (setq property
  4597. (completing-read "Property: "
  4598. (mapcar 'list (org-buffer-property-keys t))
  4599. nil t))))
  4600. (message "Sorting entries...")
  4601. (save-restriction
  4602. (narrow-to-region start end)
  4603. (let ((dcst (downcase sorting-type))
  4604. (now (current-time)))
  4605. (sort-subr
  4606. (/= dcst sorting-type)
  4607. ;; This function moves to the beginning character of the "record" to
  4608. ;; be sorted.
  4609. (if plain-list-p
  4610. (lambda nil
  4611. (if (org-at-item-p) t (goto-char (point-max))))
  4612. (lambda nil
  4613. (if (re-search-forward re nil t)
  4614. (goto-char (match-beginning 0))
  4615. (goto-char (point-max)))))
  4616. ;; This function moves to the last character of the "record" being
  4617. ;; sorted.
  4618. (if plain-list-p
  4619. 'org-end-of-item
  4620. (lambda nil
  4621. (save-match-data
  4622. (condition-case nil
  4623. (outline-forward-same-level 1)
  4624. (error
  4625. (goto-char (point-max)))))))
  4626. ;; This function returns the value that gets sorted against.
  4627. (if plain-list-p
  4628. (lambda nil
  4629. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4630. (cond
  4631. ((= dcst ?n)
  4632. (string-to-number (buffer-substring (match-end 0)
  4633. (point-at-eol))))
  4634. ((= dcst ?a)
  4635. (buffer-substring (match-end 0) (point-at-eol)))
  4636. ((= dcst ?t)
  4637. (if (re-search-forward org-ts-regexp
  4638. (point-at-eol) t)
  4639. (org-time-string-to-time (match-string 0))
  4640. now))
  4641. ((= dcst ?f)
  4642. (if getkey-func
  4643. (progn
  4644. (setq tmp (funcall getkey-func))
  4645. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4646. tmp)
  4647. (error "Invalid key function `%s'" getkey-func)))
  4648. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4649. (lambda nil
  4650. (cond
  4651. ((= dcst ?n)
  4652. (if (looking-at outline-regexp)
  4653. (string-to-number (buffer-substring (match-end 0)
  4654. (point-at-eol)))
  4655. nil))
  4656. ((= dcst ?a)
  4657. (funcall case-func (buffer-substring (point-at-bol)
  4658. (point-at-eol))))
  4659. ((= dcst ?t)
  4660. (if (re-search-forward org-ts-regexp
  4661. (save-excursion
  4662. (forward-line 2)
  4663. (point)) t)
  4664. (org-time-string-to-time (match-string 0))
  4665. now))
  4666. ((= dcst ?p)
  4667. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4668. (string-to-char (match-string 2))
  4669. org-default-priority))
  4670. ((= dcst ?r)
  4671. (or (org-entry-get nil property) ""))
  4672. ((= dcst ?o)
  4673. (if (looking-at org-complex-heading-regexp)
  4674. (- 9999 (length (member (match-string 2)
  4675. org-todo-keywords-1)))))
  4676. ((= dcst ?f)
  4677. (if getkey-func
  4678. (progn
  4679. (setq tmp (funcall getkey-func))
  4680. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4681. tmp)
  4682. (error "Invalid key function `%s'" getkey-func)))
  4683. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4684. nil
  4685. (cond
  4686. ((= dcst ?a) 'string<)
  4687. ((= dcst ?t) 'time-less-p)
  4688. (t nil)))))
  4689. (message "Sorting entries...done")))
  4690. (defun org-do-sort (table what &optional with-case sorting-type)
  4691. "Sort TABLE of WHAT according to SORTING-TYPE.
  4692. The user will be prompted for the SORTING-TYPE if the call to this
  4693. function does not specify it. WHAT is only for the prompt, to indicate
  4694. what is being sorted. The sorting key will be extracted from
  4695. the car of the elements of the table.
  4696. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4697. (unless sorting-type
  4698. (message
  4699. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4700. what)
  4701. (setq sorting-type (read-char-exclusive)))
  4702. (let ((dcst (downcase sorting-type))
  4703. extractfun comparefun)
  4704. ;; Define the appropriate functions
  4705. (cond
  4706. ((= dcst ?n)
  4707. (setq extractfun 'string-to-number
  4708. comparefun (if (= dcst sorting-type) '< '>)))
  4709. ((= dcst ?a)
  4710. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4711. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4712. comparefun (if (= dcst sorting-type)
  4713. 'string<
  4714. (lambda (a b) (and (not (string< a b))
  4715. (not (string= a b)))))))
  4716. ((= dcst ?t)
  4717. (setq extractfun
  4718. (lambda (x)
  4719. (if (string-match org-ts-regexp x)
  4720. (time-to-seconds
  4721. (org-time-string-to-time (match-string 0 x)))
  4722. 0))
  4723. comparefun (if (= dcst sorting-type) '< '>)))
  4724. (t (error "Invalid sorting type `%c'" sorting-type)))
  4725. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4726. table)
  4727. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4728. ;;;; Plain list items, including checkboxes
  4729. ;;; Plain list items
  4730. (defun org-at-item-p ()
  4731. "Is point in a line starting a hand-formatted item?"
  4732. (let ((llt org-plain-list-ordered-item-terminator))
  4733. (save-excursion
  4734. (goto-char (point-at-bol))
  4735. (looking-at
  4736. (cond
  4737. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4738. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4739. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4740. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4741. (defun org-in-item-p ()
  4742. "It the cursor inside a plain list item.
  4743. Does not have to be the first line."
  4744. (save-excursion
  4745. (condition-case nil
  4746. (progn
  4747. (org-beginning-of-item)
  4748. (org-at-item-p)
  4749. t)
  4750. (error nil))))
  4751. (defun org-insert-item (&optional checkbox)
  4752. "Insert a new item at the current level.
  4753. Return t when things worked, nil when we are not in an item."
  4754. (when (save-excursion
  4755. (condition-case nil
  4756. (progn
  4757. (org-beginning-of-item)
  4758. (org-at-item-p)
  4759. (if (org-invisible-p) (error "Invisible item"))
  4760. t)
  4761. (error nil)))
  4762. (let* ((bul (match-string 0))
  4763. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4764. (match-end 0)))
  4765. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4766. pos)
  4767. (cond
  4768. ((and (org-at-item-p) (<= (point) eow))
  4769. ;; before the bullet
  4770. (beginning-of-line 1)
  4771. (open-line (if blank 2 1)))
  4772. ((<= (point) eow)
  4773. (beginning-of-line 1))
  4774. (t
  4775. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4776. (end-of-line 1)
  4777. (delete-horizontal-space))
  4778. (newline (if blank 2 1))))
  4779. (insert bul (if checkbox "[ ]" ""))
  4780. (just-one-space)
  4781. (setq pos (point))
  4782. (end-of-line 1)
  4783. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4784. (org-maybe-renumber-ordered-list)
  4785. (and checkbox (org-update-checkbox-count-maybe))
  4786. t))
  4787. ;;; Checkboxes
  4788. (defun org-at-item-checkbox-p ()
  4789. "Is point at a line starting a plain-list item with a checklet?"
  4790. (and (org-at-item-p)
  4791. (save-excursion
  4792. (goto-char (match-end 0))
  4793. (skip-chars-forward " \t")
  4794. (looking-at "\\[[- X]\\]"))))
  4795. (defun org-toggle-checkbox (&optional arg)
  4796. "Toggle the checkbox in the current line."
  4797. (interactive "P")
  4798. (catch 'exit
  4799. (let (beg end status (firstnew 'unknown))
  4800. (cond
  4801. ((org-region-active-p)
  4802. (setq beg (region-beginning) end (region-end)))
  4803. ((org-on-heading-p)
  4804. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4805. ((org-at-item-checkbox-p)
  4806. (let ((pos (point)))
  4807. (replace-match
  4808. (cond (arg "[-]")
  4809. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4810. (t "[ ]"))
  4811. t t)
  4812. (goto-char pos))
  4813. (throw 'exit t))
  4814. (t (error "Not at a checkbox or heading, and no active region")))
  4815. (save-excursion
  4816. (goto-char beg)
  4817. (while (< (point) end)
  4818. (when (org-at-item-checkbox-p)
  4819. (setq status (equal (match-string 0) "[X]"))
  4820. (when (eq firstnew 'unknown)
  4821. (setq firstnew (not status)))
  4822. (replace-match
  4823. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4824. (beginning-of-line 2)))))
  4825. (org-update-checkbox-count-maybe))
  4826. (defun org-update-checkbox-count-maybe ()
  4827. "Update checkbox statistics unless turned off by user."
  4828. (when org-provide-checkbox-statistics
  4829. (org-update-checkbox-count)))
  4830. (defun org-update-checkbox-count (&optional all)
  4831. "Update the checkbox statistics in the current section.
  4832. This will find all statistic cookies like [57%] and [6/12] and update them
  4833. with the current numbers. With optional prefix argument ALL, do this for
  4834. the whole buffer."
  4835. (interactive "P")
  4836. (save-excursion
  4837. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4838. (beg (condition-case nil
  4839. (progn (outline-back-to-heading) (point))
  4840. (error (point-min))))
  4841. (end (move-marker (make-marker)
  4842. (progn (outline-next-heading) (point))))
  4843. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4844. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4845. (re-find (concat re "\\|" re-box))
  4846. beg-cookie end-cookie is-percent c-on c-off lim
  4847. eline curr-ind next-ind continue-from startsearch
  4848. (cstat 0)
  4849. )
  4850. (when all
  4851. (goto-char (point-min))
  4852. (outline-next-heading)
  4853. (setq beg (point) end (point-max)))
  4854. (goto-char end)
  4855. ;; find each statistic cookie
  4856. (while (re-search-backward re-find beg t)
  4857. (setq beg-cookie (match-beginning 1)
  4858. end-cookie (match-end 1)
  4859. cstat (+ cstat (if end-cookie 1 0))
  4860. startsearch (point-at-eol)
  4861. continue-from (point-at-bol)
  4862. is-percent (match-beginning 2)
  4863. lim (cond
  4864. ((org-on-heading-p) (outline-next-heading) (point))
  4865. ((org-at-item-p) (org-end-of-item) (point))
  4866. (t nil))
  4867. c-on 0
  4868. c-off 0)
  4869. (when lim
  4870. ;; find first checkbox for this cookie and gather
  4871. ;; statistics from all that are at this indentation level
  4872. (goto-char startsearch)
  4873. (if (re-search-forward re-box lim t)
  4874. (progn
  4875. (org-beginning-of-item)
  4876. (setq curr-ind (org-get-indentation))
  4877. (setq next-ind curr-ind)
  4878. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4879. (save-excursion (end-of-line) (setq eline (point)))
  4880. (if (re-search-forward re-box eline t)
  4881. (if (member (match-string 2) '("[ ]" "[-]"))
  4882. (setq c-off (1+ c-off))
  4883. (setq c-on (1+ c-on))
  4884. )
  4885. )
  4886. (org-end-of-item)
  4887. (setq next-ind (org-get-indentation))
  4888. )))
  4889. (goto-char continue-from)
  4890. ;; update cookie
  4891. (when end-cookie
  4892. (delete-region beg-cookie end-cookie)
  4893. (goto-char beg-cookie)
  4894. (insert
  4895. (if is-percent
  4896. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4897. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4898. ;; update items checkbox if it has one
  4899. (when (org-at-item-p)
  4900. (org-beginning-of-item)
  4901. (when (and (> (+ c-on c-off) 0)
  4902. (re-search-forward re-box (point-at-eol) t))
  4903. (setq beg-cookie (match-beginning 2)
  4904. end-cookie (match-end 2))
  4905. (delete-region beg-cookie end-cookie)
  4906. (goto-char beg-cookie)
  4907. (cond ((= c-off 0) (insert "[X]"))
  4908. ((= c-on 0) (insert "[ ]"))
  4909. (t (insert "[-]")))
  4910. )))
  4911. (goto-char continue-from))
  4912. (when (interactive-p)
  4913. (message "Checkbox satistics updated %s (%d places)"
  4914. (if all "in entire file" "in current outline entry") cstat)))))
  4915. (defun org-get-checkbox-statistics-face ()
  4916. "Select the face for checkbox statistics.
  4917. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4918. it will be `org-todo'."
  4919. (if (match-end 1)
  4920. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4921. (if (and (> (match-end 2) (match-beginning 2))
  4922. (equal (match-string 2) (match-string 3)))
  4923. 'org-done
  4924. 'org-todo)))
  4925. (defun org-get-indentation (&optional line)
  4926. "Get the indentation of the current line, interpreting tabs.
  4927. When LINE is given, assume it represents a line and compute its indentation."
  4928. (if line
  4929. (if (string-match "^ *" (org-remove-tabs line))
  4930. (match-end 0))
  4931. (save-excursion
  4932. (beginning-of-line 1)
  4933. (skip-chars-forward " \t")
  4934. (current-column))))
  4935. (defun org-remove-tabs (s &optional width)
  4936. "Replace tabulators in S with spaces.
  4937. Assumes that s is a single line, starting in column 0."
  4938. (setq width (or width tab-width))
  4939. (while (string-match "\t" s)
  4940. (setq s (replace-match
  4941. (make-string
  4942. (- (* width (/ (+ (match-beginning 0) width) width))
  4943. (match-beginning 0)) ?\ )
  4944. t t s)))
  4945. s)
  4946. (defun org-fix-indentation (line ind)
  4947. "Fix indentation in LINE.
  4948. IND is a cons cell with target and minimum indentation.
  4949. If the current indenation in LINE is smaller than the minimum,
  4950. leave it alone. If it is larger than ind, set it to the target."
  4951. (let* ((l (org-remove-tabs line))
  4952. (i (org-get-indentation l))
  4953. (i1 (car ind)) (i2 (cdr ind)))
  4954. (if (>= i i2) (setq l (substring line i2)))
  4955. (if (> i1 0)
  4956. (concat (make-string i1 ?\ ) l)
  4957. l)))
  4958. (defun org-beginning-of-item ()
  4959. "Go to the beginning of the current hand-formatted item.
  4960. If the cursor is not in an item, throw an error."
  4961. (interactive)
  4962. (let ((pos (point))
  4963. (limit (save-excursion
  4964. (condition-case nil
  4965. (progn
  4966. (org-back-to-heading)
  4967. (beginning-of-line 2) (point))
  4968. (error (point-min)))))
  4969. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4970. ind ind1)
  4971. (if (org-at-item-p)
  4972. (beginning-of-line 1)
  4973. (beginning-of-line 1)
  4974. (skip-chars-forward " \t")
  4975. (setq ind (current-column))
  4976. (if (catch 'exit
  4977. (while t
  4978. (beginning-of-line 0)
  4979. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4980. (if (looking-at "[ \t]*$")
  4981. (setq ind1 ind-empty)
  4982. (skip-chars-forward " \t")
  4983. (setq ind1 (current-column)))
  4984. (if (< ind1 ind)
  4985. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4986. nil
  4987. (goto-char pos)
  4988. (error "Not in an item")))))
  4989. (defun org-end-of-item ()
  4990. "Go to the end of the current hand-formatted item.
  4991. If the cursor is not in an item, throw an error."
  4992. (interactive)
  4993. (let* ((pos (point))
  4994. ind1
  4995. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4996. (limit (save-excursion (outline-next-heading) (point)))
  4997. (ind (save-excursion
  4998. (org-beginning-of-item)
  4999. (skip-chars-forward " \t")
  5000. (current-column)))
  5001. (end (catch 'exit
  5002. (while t
  5003. (beginning-of-line 2)
  5004. (if (eobp) (throw 'exit (point)))
  5005. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  5006. (if (looking-at "[ \t]*$")
  5007. (setq ind1 ind-empty)
  5008. (skip-chars-forward " \t")
  5009. (setq ind1 (current-column)))
  5010. (if (<= ind1 ind)
  5011. (throw 'exit (point-at-bol)))))))
  5012. (if end
  5013. (goto-char end)
  5014. (goto-char pos)
  5015. (error "Not in an item"))))
  5016. (defun org-next-item ()
  5017. "Move to the beginning of the next item in the current plain list.
  5018. Error if not at a plain list, or if this is the last item in the list."
  5019. (interactive)
  5020. (let (ind ind1 (pos (point)))
  5021. (org-beginning-of-item)
  5022. (setq ind (org-get-indentation))
  5023. (org-end-of-item)
  5024. (setq ind1 (org-get-indentation))
  5025. (unless (and (org-at-item-p) (= ind ind1))
  5026. (goto-char pos)
  5027. (error "On last item"))))
  5028. (defun org-previous-item ()
  5029. "Move to the beginning of the previous item in the current plain list.
  5030. Error if not at a plain list, or if this is the first item in the list."
  5031. (interactive)
  5032. (let (beg ind ind1 (pos (point)))
  5033. (org-beginning-of-item)
  5034. (setq beg (point))
  5035. (setq ind (org-get-indentation))
  5036. (goto-char beg)
  5037. (catch 'exit
  5038. (while t
  5039. (beginning-of-line 0)
  5040. (if (looking-at "[ \t]*$")
  5041. nil
  5042. (if (<= (setq ind1 (org-get-indentation)) ind)
  5043. (throw 'exit t)))))
  5044. (condition-case nil
  5045. (if (or (not (org-at-item-p))
  5046. (< ind1 (1- ind)))
  5047. (error "")
  5048. (org-beginning-of-item))
  5049. (error (goto-char pos)
  5050. (error "On first item")))))
  5051. (defun org-first-list-item-p ()
  5052. "Is this heading the item in a plain list?"
  5053. (unless (org-at-item-p)
  5054. (error "Not at a plain list item"))
  5055. (org-beginning-of-item)
  5056. (= (point) (save-excursion (org-beginning-of-item-list))))
  5057. (defun org-move-item-down ()
  5058. "Move the plain list item at point down, i.e. swap with following item.
  5059. Subitems (items with larger indentation) are considered part of the item,
  5060. so this really moves item trees."
  5061. (interactive)
  5062. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5063. (org-beginning-of-item)
  5064. (setq beg0 (point))
  5065. (save-excursion
  5066. (setq ne-beg (org-back-over-empty-lines))
  5067. (setq beg (point)))
  5068. (goto-char beg0)
  5069. (setq ind (org-get-indentation))
  5070. (org-end-of-item)
  5071. (setq end0 (point))
  5072. (setq ind1 (org-get-indentation))
  5073. (setq ne-end (org-back-over-empty-lines))
  5074. (setq end (point))
  5075. (goto-char beg0)
  5076. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5077. ;; include less whitespace
  5078. (save-excursion
  5079. (goto-char beg)
  5080. (forward-line (- ne-beg ne-end))
  5081. (setq beg (point))))
  5082. (goto-char end0)
  5083. (if (and (org-at-item-p) (= ind ind1))
  5084. (progn
  5085. (org-end-of-item)
  5086. (org-back-over-empty-lines)
  5087. (setq txt (buffer-substring beg end))
  5088. (save-excursion
  5089. (delete-region beg end))
  5090. (setq pos (point))
  5091. (insert txt)
  5092. (goto-char pos) (org-skip-whitespace)
  5093. (org-maybe-renumber-ordered-list))
  5094. (goto-char pos)
  5095. (error "Cannot move this item further down"))))
  5096. (defun org-move-item-up (arg)
  5097. "Move the plain list item at point up, i.e. swap with previous item.
  5098. Subitems (items with larger indentation) are considered part of the item,
  5099. so this really moves item trees."
  5100. (interactive "p")
  5101. (let (beg beg0 end ind ind1 (pos (point)) txt
  5102. ne-beg ne-ins ins-end)
  5103. (org-beginning-of-item)
  5104. (setq beg0 (point))
  5105. (setq ind (org-get-indentation))
  5106. (save-excursion
  5107. (setq ne-beg (org-back-over-empty-lines))
  5108. (setq beg (point)))
  5109. (goto-char beg0)
  5110. (org-end-of-item)
  5111. (setq end (point))
  5112. (goto-char beg0)
  5113. (catch 'exit
  5114. (while t
  5115. (beginning-of-line 0)
  5116. (if (looking-at "[ \t]*$")
  5117. (if org-empty-line-terminates-plain-lists
  5118. (progn
  5119. (goto-char pos)
  5120. (error "Cannot move this item further up"))
  5121. nil)
  5122. (if (<= (setq ind1 (org-get-indentation)) ind)
  5123. (throw 'exit t)))))
  5124. (condition-case nil
  5125. (org-beginning-of-item)
  5126. (error (goto-char beg)
  5127. (error "Cannot move this item further up")))
  5128. (setq ind1 (org-get-indentation))
  5129. (if (and (org-at-item-p) (= ind ind1))
  5130. (progn
  5131. (setq ne-ins (org-back-over-empty-lines))
  5132. (setq txt (buffer-substring beg end))
  5133. (save-excursion
  5134. (delete-region beg end))
  5135. (setq pos (point))
  5136. (insert txt)
  5137. (setq ins-end (point))
  5138. (goto-char pos) (org-skip-whitespace)
  5139. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5140. ;; Move whitespace back to beginning
  5141. (save-excursion
  5142. (goto-char ins-end)
  5143. (let ((kill-whole-line t))
  5144. (kill-line (- ne-ins ne-beg)) (point)))
  5145. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5146. (org-maybe-renumber-ordered-list))
  5147. (goto-char pos)
  5148. (error "Cannot move this item further up"))))
  5149. (defun org-maybe-renumber-ordered-list ()
  5150. "Renumber the ordered list at point if setup allows it.
  5151. This tests the user option `org-auto-renumber-ordered-lists' before
  5152. doing the renumbering."
  5153. (interactive)
  5154. (when (and org-auto-renumber-ordered-lists
  5155. (org-at-item-p))
  5156. (if (match-beginning 3)
  5157. (org-renumber-ordered-list 1)
  5158. (org-fix-bullet-type))))
  5159. (defun org-maybe-renumber-ordered-list-safe ()
  5160. (condition-case nil
  5161. (save-excursion
  5162. (org-maybe-renumber-ordered-list))
  5163. (error nil)))
  5164. (defun org-cycle-list-bullet (&optional which)
  5165. "Cycle through the different itemize/enumerate bullets.
  5166. This cycle the entire list level through the sequence:
  5167. `-' -> `+' -> `*' -> `1.' -> `1)'
  5168. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5169. 0 meand `-', 1 means `+' etc."
  5170. (interactive "P")
  5171. (org-preserve-lc
  5172. (org-beginning-of-item-list)
  5173. (org-at-item-p)
  5174. (beginning-of-line 1)
  5175. (let ((current (match-string 0))
  5176. (prevp (eq which 'previous))
  5177. new)
  5178. (setq new (cond
  5179. ((and (numberp which)
  5180. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5181. ((string-match "-" current) (if prevp "1)" "+"))
  5182. ((string-match "\\+" current)
  5183. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5184. ((string-match "\\*" current) (if prevp "+" "1."))
  5185. ((string-match "\\." current) (if prevp "*" "1)"))
  5186. ((string-match ")" current) (if prevp "1." "-"))
  5187. (t (error "This should not happen"))))
  5188. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5189. (org-fix-bullet-type)
  5190. (org-maybe-renumber-ordered-list))))
  5191. (defun org-get-string-indentation (s)
  5192. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5193. (let ((n -1) (i 0) (w tab-width) c)
  5194. (catch 'exit
  5195. (while (< (setq n (1+ n)) (length s))
  5196. (setq c (aref s n))
  5197. (cond ((= c ?\ ) (setq i (1+ i)))
  5198. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5199. (t (throw 'exit t)))))
  5200. i))
  5201. (defun org-renumber-ordered-list (arg)
  5202. "Renumber an ordered plain list.
  5203. Cursor needs to be in the first line of an item, the line that starts
  5204. with something like \"1.\" or \"2)\"."
  5205. (interactive "p")
  5206. (unless (and (org-at-item-p)
  5207. (match-beginning 3))
  5208. (error "This is not an ordered list"))
  5209. (let ((line (org-current-line))
  5210. (col (current-column))
  5211. (ind (org-get-string-indentation
  5212. (buffer-substring (point-at-bol) (match-beginning 3))))
  5213. ;; (term (substring (match-string 3) -1))
  5214. ind1 (n (1- arg))
  5215. fmt)
  5216. ;; find where this list begins
  5217. (org-beginning-of-item-list)
  5218. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5219. (setq fmt (concat "%d" (match-string 1)))
  5220. (beginning-of-line 0)
  5221. ;; walk forward and replace these numbers
  5222. (catch 'exit
  5223. (while t
  5224. (catch 'next
  5225. (beginning-of-line 2)
  5226. (if (eobp) (throw 'exit nil))
  5227. (if (looking-at "[ \t]*$") (throw 'next nil))
  5228. (skip-chars-forward " \t") (setq ind1 (current-column))
  5229. (if (> ind1 ind) (throw 'next t))
  5230. (if (< ind1 ind) (throw 'exit t))
  5231. (if (not (org-at-item-p)) (throw 'exit nil))
  5232. (delete-region (match-beginning 2) (match-end 2))
  5233. (goto-char (match-beginning 2))
  5234. (insert (format fmt (setq n (1+ n)))))))
  5235. (goto-line line)
  5236. (org-move-to-column col)))
  5237. (defun org-fix-bullet-type ()
  5238. "Make sure all items in this list have the same bullet as the firsst item."
  5239. (interactive)
  5240. (unless (org-at-item-p) (error "This is not a list"))
  5241. (let ((line (org-current-line))
  5242. (col (current-column))
  5243. (ind (current-indentation))
  5244. ind1 bullet)
  5245. ;; find where this list begins
  5246. (org-beginning-of-item-list)
  5247. (beginning-of-line 1)
  5248. ;; find out what the bullet type is
  5249. (looking-at "[ \t]*\\(\\S-+\\)")
  5250. (setq bullet (match-string 1))
  5251. ;; walk forward and replace these numbers
  5252. (beginning-of-line 0)
  5253. (catch 'exit
  5254. (while t
  5255. (catch 'next
  5256. (beginning-of-line 2)
  5257. (if (eobp) (throw 'exit nil))
  5258. (if (looking-at "[ \t]*$") (throw 'next nil))
  5259. (skip-chars-forward " \t") (setq ind1 (current-column))
  5260. (if (> ind1 ind) (throw 'next t))
  5261. (if (< ind1 ind) (throw 'exit t))
  5262. (if (not (org-at-item-p)) (throw 'exit nil))
  5263. (skip-chars-forward " \t")
  5264. (looking-at "\\S-+")
  5265. (replace-match bullet))))
  5266. (goto-line line)
  5267. (org-move-to-column col)
  5268. (if (string-match "[0-9]" bullet)
  5269. (org-renumber-ordered-list 1))))
  5270. (defun org-beginning-of-item-list ()
  5271. "Go to the beginning of the current item list.
  5272. I.e. to the first item in this list."
  5273. (interactive)
  5274. (org-beginning-of-item)
  5275. (let ((pos (point-at-bol))
  5276. (ind (org-get-indentation))
  5277. ind1)
  5278. ;; find where this list begins
  5279. (catch 'exit
  5280. (while t
  5281. (catch 'next
  5282. (beginning-of-line 0)
  5283. (if (looking-at "[ \t]*$")
  5284. (throw (if (bobp) 'exit 'next) t))
  5285. (skip-chars-forward " \t") (setq ind1 (current-column))
  5286. (if (or (< ind1 ind)
  5287. (and (= ind1 ind)
  5288. (not (org-at-item-p)))
  5289. (bobp))
  5290. (throw 'exit t)
  5291. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5292. (goto-char pos)))
  5293. (defun org-end-of-item-list ()
  5294. "Go to the end of the current item list.
  5295. I.e. to the text after the last item."
  5296. (interactive)
  5297. (org-beginning-of-item)
  5298. (let ((pos (point-at-bol))
  5299. (ind (org-get-indentation))
  5300. ind1)
  5301. ;; find where this list begins
  5302. (catch 'exit
  5303. (while t
  5304. (catch 'next
  5305. (beginning-of-line 2)
  5306. (if (looking-at "[ \t]*$")
  5307. (throw (if (eobp) 'exit 'next) t))
  5308. (skip-chars-forward " \t") (setq ind1 (current-column))
  5309. (if (or (< ind1 ind)
  5310. (and (= ind1 ind)
  5311. (not (org-at-item-p)))
  5312. (eobp))
  5313. (progn
  5314. (setq pos (point-at-bol))
  5315. (throw 'exit t))))))
  5316. (goto-char pos)))
  5317. (defvar org-last-indent-begin-marker (make-marker))
  5318. (defvar org-last-indent-end-marker (make-marker))
  5319. (defun org-outdent-item (arg)
  5320. "Outdent a local list item."
  5321. (interactive "p")
  5322. (org-indent-item (- arg)))
  5323. (defun org-indent-item (arg)
  5324. "Indent a local list item."
  5325. (interactive "p")
  5326. (unless (org-at-item-p)
  5327. (error "Not on an item"))
  5328. (save-excursion
  5329. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5330. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5331. (setq beg org-last-indent-begin-marker
  5332. end org-last-indent-end-marker)
  5333. (org-beginning-of-item)
  5334. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5335. (org-end-of-item)
  5336. (setq end (move-marker org-last-indent-end-marker (point))))
  5337. (goto-char beg)
  5338. (setq tmp (org-item-indent-positions)
  5339. ind (car tmp)
  5340. ind-down (nth 2 tmp)
  5341. ind-up (nth 1 tmp)
  5342. delta (if (> arg 0)
  5343. (if ind-down (- ind-down ind) 2)
  5344. (if ind-up (- ind-up ind) -2)))
  5345. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5346. (while (< (point) end)
  5347. (beginning-of-line 1)
  5348. (skip-chars-forward " \t") (setq ind1 (current-column))
  5349. (delete-region (point-at-bol) (point))
  5350. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5351. (beginning-of-line 2))))
  5352. (org-fix-bullet-type)
  5353. (org-maybe-renumber-ordered-list-safe)
  5354. (save-excursion
  5355. (beginning-of-line 0)
  5356. (condition-case nil (org-beginning-of-item) (error nil))
  5357. (org-maybe-renumber-ordered-list-safe)))
  5358. (defun org-item-indent-positions ()
  5359. "Return indentation for plain list items.
  5360. This returns a list with three values: The current indentation, the
  5361. parent indentation and the indentation a child should habe.
  5362. Assumes cursor in item line."
  5363. (let* ((bolpos (point-at-bol))
  5364. (ind (org-get-indentation))
  5365. ind-down ind-up pos)
  5366. (save-excursion
  5367. (org-beginning-of-item-list)
  5368. (skip-chars-backward "\n\r \t")
  5369. (when (org-in-item-p)
  5370. (org-beginning-of-item)
  5371. (setq ind-up (org-get-indentation))))
  5372. (setq pos (point))
  5373. (save-excursion
  5374. (cond
  5375. ((and (condition-case nil (progn (org-previous-item) t)
  5376. (error nil))
  5377. (or (forward-char 1) t)
  5378. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5379. (setq ind-down (org-get-indentation)))
  5380. ((and (goto-char pos)
  5381. (org-at-item-p))
  5382. (goto-char (match-end 0))
  5383. (skip-chars-forward " \t")
  5384. (setq ind-down (current-column)))))
  5385. (list ind ind-up ind-down)))
  5386. ;;; The orgstruct minor mode
  5387. ;; Define a minor mode which can be used in other modes in order to
  5388. ;; integrate the org-mode structure editing commands.
  5389. ;; This is really a hack, because the org-mode structure commands use
  5390. ;; keys which normally belong to the major mode. Here is how it
  5391. ;; works: The minor mode defines all the keys necessary to operate the
  5392. ;; structure commands, but wraps the commands into a function which
  5393. ;; tests if the cursor is currently at a headline or a plain list
  5394. ;; item. If that is the case, the structure command is used,
  5395. ;; temporarily setting many Org-mode variables like regular
  5396. ;; expressions for filling etc. However, when any of those keys is
  5397. ;; used at a different location, function uses `key-binding' to look
  5398. ;; up if the key has an associated command in another currently active
  5399. ;; keymap (minor modes, major mode, global), and executes that
  5400. ;; command. There might be problems if any of the keys is otherwise
  5401. ;; used as a prefix key.
  5402. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5403. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5404. ;; addresses this by checking explicitly for both bindings.
  5405. (defvar orgstruct-mode-map (make-sparse-keymap)
  5406. "Keymap for the minor `orgstruct-mode'.")
  5407. (defvar org-local-vars nil
  5408. "List of local variables, for use by `orgstruct-mode'")
  5409. ;;;###autoload
  5410. (define-minor-mode orgstruct-mode
  5411. "Toggle the minor more `orgstruct-mode'.
  5412. This mode is for using Org-mode structure commands in other modes.
  5413. The following key behave as if Org-mode was active, if the cursor
  5414. is on a headline, or on a plain list item (both in the definition
  5415. of Org-mode).
  5416. M-up Move entry/item up
  5417. M-down Move entry/item down
  5418. M-left Promote
  5419. M-right Demote
  5420. M-S-up Move entry/item up
  5421. M-S-down Move entry/item down
  5422. M-S-left Promote subtree
  5423. M-S-right Demote subtree
  5424. M-q Fill paragraph and items like in Org-mode
  5425. C-c ^ Sort entries
  5426. C-c - Cycle list bullet
  5427. TAB Cycle item visibility
  5428. M-RET Insert new heading/item
  5429. S-M-RET Insert new TODO heading / Chekbox item
  5430. C-c C-c Set tags / toggle checkbox"
  5431. nil " OrgStruct" nil
  5432. (org-load-modules-maybe)
  5433. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5434. ;;;###autoload
  5435. (defun turn-on-orgstruct ()
  5436. "Unconditionally turn on `orgstruct-mode'."
  5437. (orgstruct-mode 1))
  5438. ;;;###autoload
  5439. (defun turn-on-orgstruct++ ()
  5440. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5441. In addition to setting orgstruct-mode, this also exports all indentation and
  5442. autofilling variables from org-mode into the buffer. Note that turning
  5443. off orgstruct-mode will *not* remove these additional settings."
  5444. (orgstruct-mode 1)
  5445. (let (var val)
  5446. (mapc
  5447. (lambda (x)
  5448. (when (string-match
  5449. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5450. (symbol-name (car x)))
  5451. (setq var (car x) val (nth 1 x))
  5452. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5453. org-local-vars)))
  5454. (defun orgstruct-error ()
  5455. "Error when there is no default binding for a structure key."
  5456. (interactive)
  5457. (error "This key has no function outside structure elements"))
  5458. (defun orgstruct-setup ()
  5459. "Setup orgstruct keymaps."
  5460. (let ((nfunc 0)
  5461. (bindings
  5462. (list
  5463. '([(meta up)] org-metaup)
  5464. '([(meta down)] org-metadown)
  5465. '([(meta left)] org-metaleft)
  5466. '([(meta right)] org-metaright)
  5467. '([(meta shift up)] org-shiftmetaup)
  5468. '([(meta shift down)] org-shiftmetadown)
  5469. '([(meta shift left)] org-shiftmetaleft)
  5470. '([(meta shift right)] org-shiftmetaright)
  5471. '([(shift up)] org-shiftup)
  5472. '([(shift down)] org-shiftdown)
  5473. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5474. '("\M-q" fill-paragraph)
  5475. '("\C-c^" org-sort)
  5476. '("\C-c-" org-cycle-list-bullet)))
  5477. elt key fun cmd)
  5478. (while (setq elt (pop bindings))
  5479. (setq nfunc (1+ nfunc))
  5480. (setq key (org-key (car elt))
  5481. fun (nth 1 elt)
  5482. cmd (orgstruct-make-binding fun nfunc key))
  5483. (org-defkey orgstruct-mode-map key cmd))
  5484. ;; Special treatment needed for TAB and RET
  5485. (org-defkey orgstruct-mode-map [(tab)]
  5486. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5487. (org-defkey orgstruct-mode-map "\C-i"
  5488. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5489. (org-defkey orgstruct-mode-map "\M-\C-m"
  5490. (orgstruct-make-binding 'org-insert-heading 105
  5491. "\M-\C-m" [(meta return)]))
  5492. (org-defkey orgstruct-mode-map [(meta return)]
  5493. (orgstruct-make-binding 'org-insert-heading 106
  5494. [(meta return)] "\M-\C-m"))
  5495. (org-defkey orgstruct-mode-map [(shift meta return)]
  5496. (orgstruct-make-binding 'org-insert-todo-heading 107
  5497. [(meta return)] "\M-\C-m"))
  5498. (unless org-local-vars
  5499. (setq org-local-vars (org-get-local-variables)))
  5500. t))
  5501. (defun orgstruct-make-binding (fun n &rest keys)
  5502. "Create a function for binding in the structure minor mode.
  5503. FUN is the command to call inside a table. N is used to create a unique
  5504. command name. KEYS are keys that should be checked in for a command
  5505. to execute outside of tables."
  5506. (eval
  5507. (list 'defun
  5508. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5509. '(arg)
  5510. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5511. "Outside of structure, run the binding of `"
  5512. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5513. "'.")
  5514. '(interactive "p")
  5515. (list 'if
  5516. '(org-context-p 'headline 'item)
  5517. (list 'org-run-like-in-org-mode (list 'quote fun))
  5518. (list 'let '(orgstruct-mode)
  5519. (list 'call-interactively
  5520. (append '(or)
  5521. (mapcar (lambda (k)
  5522. (list 'key-binding k))
  5523. keys)
  5524. '('orgstruct-error))))))))
  5525. (defun org-context-p (&rest contexts)
  5526. "Check if local context is and of CONTEXTS.
  5527. Possible values in the list of contexts are `table', `headline', and `item'."
  5528. (let ((pos (point)))
  5529. (goto-char (point-at-bol))
  5530. (prog1 (or (and (memq 'table contexts)
  5531. (looking-at "[ \t]*|"))
  5532. (and (memq 'headline contexts)
  5533. (looking-at "\\*+"))
  5534. (and (memq 'item contexts)
  5535. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5536. (goto-char pos))))
  5537. (defun org-get-local-variables ()
  5538. "Return a list of all local variables in an org-mode buffer."
  5539. (let (varlist)
  5540. (with-current-buffer (get-buffer-create "*Org tmp*")
  5541. (erase-buffer)
  5542. (org-mode)
  5543. (setq varlist (buffer-local-variables)))
  5544. (kill-buffer "*Org tmp*")
  5545. (delq nil
  5546. (mapcar
  5547. (lambda (x)
  5548. (setq x
  5549. (if (symbolp x)
  5550. (list x)
  5551. (list (car x) (list 'quote (cdr x)))))
  5552. (if (string-match
  5553. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5554. (symbol-name (car x)))
  5555. x nil))
  5556. varlist))))
  5557. ;;;###autoload
  5558. (defun org-run-like-in-org-mode (cmd)
  5559. (org-load-modules-maybe)
  5560. (unless org-local-vars
  5561. (setq org-local-vars (org-get-local-variables)))
  5562. (eval (list 'let org-local-vars
  5563. (list 'call-interactively (list 'quote cmd)))))
  5564. ;;;; Archiving
  5565. (defun org-get-category (&optional pos)
  5566. "Get the category applying to position POS."
  5567. (get-text-property (or pos (point)) 'org-category))
  5568. (defun org-refresh-category-properties ()
  5569. "Refresh category text properties in the buffer."
  5570. (let ((def-cat (cond
  5571. ((null org-category)
  5572. (if buffer-file-name
  5573. (file-name-sans-extension
  5574. (file-name-nondirectory buffer-file-name))
  5575. "???"))
  5576. ((symbolp org-category) (symbol-name org-category))
  5577. (t org-category)))
  5578. beg end cat pos optionp)
  5579. (org-unmodified
  5580. (save-excursion
  5581. (save-restriction
  5582. (widen)
  5583. (goto-char (point-min))
  5584. (put-text-property (point) (point-max) 'org-category def-cat)
  5585. (while (re-search-forward
  5586. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5587. (setq pos (match-end 0)
  5588. optionp (equal (char-after (match-beginning 0)) ?#)
  5589. cat (org-trim (match-string 2)))
  5590. (if optionp
  5591. (setq beg (point-at-bol) end (point-max))
  5592. (org-back-to-heading t)
  5593. (setq beg (point) end (org-end-of-subtree t t)))
  5594. (put-text-property beg end 'org-category cat)
  5595. (goto-char pos)))))))
  5596. ;;;; Link Stuff
  5597. ;;; Link abbreviations
  5598. (defun org-link-expand-abbrev (link)
  5599. "Apply replacements as defined in `org-link-abbrev-alist."
  5600. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5601. (let* ((key (match-string 1 link))
  5602. (as (or (assoc key org-link-abbrev-alist-local)
  5603. (assoc key org-link-abbrev-alist)))
  5604. (tag (and (match-end 2) (match-string 3 link)))
  5605. rpl)
  5606. (if (not as)
  5607. link
  5608. (setq rpl (cdr as))
  5609. (cond
  5610. ((symbolp rpl) (funcall rpl tag))
  5611. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5612. (t (concat rpl tag)))))
  5613. link))
  5614. ;;; Storing and inserting links
  5615. (defvar org-insert-link-history nil
  5616. "Minibuffer history for links inserted with `org-insert-link'.")
  5617. (defvar org-stored-links nil
  5618. "Contains the links stored with `org-store-link'.")
  5619. (defvar org-store-link-plist nil
  5620. "Plist with info about the most recently link created with `org-store-link'.")
  5621. (defvar org-link-protocols nil
  5622. "Link protocols added to Org-mode using `org-add-link-type'.")
  5623. (defvar org-store-link-functions nil
  5624. "List of functions that are called to create and store a link.
  5625. Each function will be called in turn until one returns a non-nil
  5626. value. Each function should check if it is responsible for creating
  5627. this link (for example by looking at the major mode).
  5628. If not, it must exit and return nil.
  5629. If yes, it should return a non-nil value after a calling
  5630. `org-store-link-props' with a list of properties and values.
  5631. Special properties are:
  5632. :type The link prefix. like \"http\". This must be given.
  5633. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5634. This is obligatory as well.
  5635. :description Optional default description for the second pair
  5636. of brackets in an Org-mode link. The user can still change
  5637. this when inserting this link into an Org-mode buffer.
  5638. In addition to these, any additional properties can be specified
  5639. and then used in remember templates.")
  5640. (defun org-add-link-type (type &optional follow export)
  5641. "Add TYPE to the list of `org-link-types'.
  5642. Re-compute all regular expressions depending on `org-link-types'
  5643. FOLLOW and EXPORT are two functions.
  5644. FOLLOW should take the link path as the single argument and do whatever
  5645. is necessary to follow the link, for example find a file or display
  5646. a mail message.
  5647. EXPORT should format the link path for export to one of the export formats.
  5648. It should be a function accepting three arguments:
  5649. path the path of the link, the text after the prefix (like \"http:\")
  5650. desc the description of the link, if any, nil if there was no descripton
  5651. format the export format, a symbol like `html' or `latex'.
  5652. The function may use the FORMAT information to return different values
  5653. depending on the format. The return value will be put literally into
  5654. the exported file.
  5655. Org-mode has a built-in default for exporting links. If you are happy with
  5656. this default, there is no need to define an export function for the link
  5657. type. For a simple example of an export function, see `org-bbdb.el'."
  5658. (add-to-list 'org-link-types type t)
  5659. (org-make-link-regexps)
  5660. (if (assoc type org-link-protocols)
  5661. (setcdr (assoc type org-link-protocols) (list follow export))
  5662. (push (list type follow export) org-link-protocols)))
  5663. ;;;###autoload
  5664. (defun org-store-link (arg)
  5665. "\\<org-mode-map>Store an org-link to the current location.
  5666. This link is added to `org-stored-links' and can later be inserted
  5667. into an org-buffer with \\[org-insert-link].
  5668. For some link types, a prefix arg is interpreted:
  5669. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5670. For file links, arg negates `org-context-in-file-links'."
  5671. (interactive "P")
  5672. (org-load-modules-maybe)
  5673. (setq org-store-link-plist nil) ; reset
  5674. (let (link cpltxt desc description search txt)
  5675. (cond
  5676. ((run-hook-with-args-until-success 'org-store-link-functions)
  5677. (setq link (plist-get org-store-link-plist :link)
  5678. desc (or (plist-get org-store-link-plist :description) link)))
  5679. ((eq major-mode 'calendar-mode)
  5680. (let ((cd (calendar-cursor-to-date)))
  5681. (setq link
  5682. (format-time-string
  5683. (car org-time-stamp-formats)
  5684. (apply 'encode-time
  5685. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5686. nil nil nil))))
  5687. (org-store-link-props :type "calendar" :date cd)))
  5688. ((eq major-mode 'w3-mode)
  5689. (setq cpltxt (url-view-url t)
  5690. link (org-make-link cpltxt))
  5691. (org-store-link-props :type "w3" :url (url-view-url t)))
  5692. ((eq major-mode 'w3m-mode)
  5693. (setq cpltxt (or w3m-current-title w3m-current-url)
  5694. link (org-make-link w3m-current-url))
  5695. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5696. ((setq search (run-hook-with-args-until-success
  5697. 'org-create-file-search-functions))
  5698. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5699. "::" search))
  5700. (setq cpltxt (or description link)))
  5701. ((eq major-mode 'image-mode)
  5702. (setq cpltxt (concat "file:"
  5703. (abbreviate-file-name buffer-file-name))
  5704. link (org-make-link cpltxt))
  5705. (org-store-link-props :type "image" :file buffer-file-name))
  5706. ((eq major-mode 'dired-mode)
  5707. ;; link to the file in the current line
  5708. (setq cpltxt (concat "file:"
  5709. (abbreviate-file-name
  5710. (expand-file-name
  5711. (dired-get-filename nil t))))
  5712. link (org-make-link cpltxt)))
  5713. ((and buffer-file-name (org-mode-p))
  5714. ;; Just link to current headline
  5715. (setq cpltxt (concat "file:"
  5716. (abbreviate-file-name buffer-file-name)))
  5717. ;; Add a context search string
  5718. (when (org-xor org-context-in-file-links arg)
  5719. ;; Check if we are on a target
  5720. (if (org-in-regexp "<<\\(.*?\\)>>")
  5721. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5722. (setq txt (cond
  5723. ((org-on-heading-p) nil)
  5724. ((org-region-active-p)
  5725. (buffer-substring (region-beginning) (region-end)))
  5726. (t nil)))
  5727. (when (or (null txt) (string-match "\\S-" txt))
  5728. (setq cpltxt
  5729. (concat cpltxt "::"
  5730. (condition-case nil
  5731. (org-make-org-heading-search-string txt)
  5732. (error "")))
  5733. desc "NONE"))))
  5734. (if (string-match "::\\'" cpltxt)
  5735. (setq cpltxt (substring cpltxt 0 -2)))
  5736. (setq link (org-make-link cpltxt)))
  5737. ((buffer-file-name (buffer-base-buffer))
  5738. ;; Just link to this file here.
  5739. (setq cpltxt (concat "file:"
  5740. (abbreviate-file-name
  5741. (buffer-file-name (buffer-base-buffer)))))
  5742. ;; Add a context string
  5743. (when (org-xor org-context-in-file-links arg)
  5744. (setq txt (if (org-region-active-p)
  5745. (buffer-substring (region-beginning) (region-end))
  5746. (buffer-substring (point-at-bol) (point-at-eol))))
  5747. ;; Only use search option if there is some text.
  5748. (when (string-match "\\S-" txt)
  5749. (setq cpltxt
  5750. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5751. desc "NONE")))
  5752. (setq link (org-make-link cpltxt)))
  5753. ((interactive-p)
  5754. (error "Cannot link to a buffer which is not visiting a file"))
  5755. (t (setq link nil)))
  5756. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5757. (setq link (or link cpltxt)
  5758. desc (or desc cpltxt))
  5759. (if (equal desc "NONE") (setq desc nil))
  5760. (if (and (interactive-p) link)
  5761. (progn
  5762. (setq org-stored-links
  5763. (cons (list link desc) org-stored-links))
  5764. (message "Stored: %s" (or desc link)))
  5765. (and link (org-make-link-string link desc)))))
  5766. (defun org-store-link-props (&rest plist)
  5767. "Store link properties, extract names and addresses."
  5768. (let (x adr)
  5769. (when (setq x (plist-get plist :from))
  5770. (setq adr (mail-extract-address-components x))
  5771. (plist-put plist :fromname (car adr))
  5772. (plist-put plist :fromaddress (nth 1 adr)))
  5773. (when (setq x (plist-get plist :to))
  5774. (setq adr (mail-extract-address-components x))
  5775. (plist-put plist :toname (car adr))
  5776. (plist-put plist :toaddress (nth 1 adr))))
  5777. (let ((from (plist-get plist :from))
  5778. (to (plist-get plist :to)))
  5779. (when (and from to org-from-is-user-regexp)
  5780. (plist-put plist :fromto
  5781. (if (string-match org-from-is-user-regexp from)
  5782. (concat "to %t")
  5783. (concat "from %f")))))
  5784. (setq org-store-link-plist plist))
  5785. (defun org-add-link-props (&rest plist)
  5786. "Add these properties to the link property list."
  5787. (let (key value)
  5788. (while plist
  5789. (setq key (pop plist) value (pop plist))
  5790. (setq org-store-link-plist
  5791. (plist-put org-store-link-plist key value)))))
  5792. (defun org-email-link-description (&optional fmt)
  5793. "Return the description part of an email link.
  5794. This takes information from `org-store-link-plist' and formats it
  5795. according to FMT (default from `org-email-link-description-format')."
  5796. (setq fmt (or fmt org-email-link-description-format))
  5797. (let* ((p org-store-link-plist)
  5798. (to (plist-get p :toaddress))
  5799. (from (plist-get p :fromaddress))
  5800. (table
  5801. (list
  5802. (cons "%c" (plist-get p :fromto))
  5803. (cons "%F" (plist-get p :from))
  5804. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5805. (cons "%T" (plist-get p :to))
  5806. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5807. (cons "%s" (plist-get p :subject))
  5808. (cons "%m" (plist-get p :message-id)))))
  5809. (when (string-match "%c" fmt)
  5810. ;; Check if the user wrote this message
  5811. (if (and org-from-is-user-regexp from to
  5812. (save-match-data (string-match org-from-is-user-regexp from)))
  5813. (setq fmt (replace-match "to %t" t t fmt))
  5814. (setq fmt (replace-match "from %f" t t fmt))))
  5815. (org-replace-escapes fmt table)))
  5816. (defun org-make-org-heading-search-string (&optional string heading)
  5817. "Make search string for STRING or current headline."
  5818. (interactive)
  5819. (let ((s (or string (org-get-heading))))
  5820. (unless (and string (not heading))
  5821. ;; We are using a headline, clean up garbage in there.
  5822. (if (string-match org-todo-regexp s)
  5823. (setq s (replace-match "" t t s)))
  5824. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5825. (setq s (replace-match "" t t s)))
  5826. (setq s (org-trim s))
  5827. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5828. org-comment-string "\\)") s)
  5829. (setq s (replace-match "" t t s)))
  5830. (while (string-match org-ts-regexp s)
  5831. (setq s (replace-match "" t t s))))
  5832. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5833. (setq s (replace-match " " t t s)))
  5834. (or string (setq s (concat "*" s))) ; Add * for headlines
  5835. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5836. (defun org-make-link (&rest strings)
  5837. "Concatenate STRINGS."
  5838. (apply 'concat strings))
  5839. (defun org-make-link-string (link &optional description)
  5840. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5841. (unless (string-match "\\S-" link)
  5842. (error "Empty link"))
  5843. (when (stringp description)
  5844. ;; Remove brackets from the description, they are fatal.
  5845. (while (string-match "\\[" description)
  5846. (setq description (replace-match "{" t t description)))
  5847. (while (string-match "\\]" description)
  5848. (setq description (replace-match "}" t t description))))
  5849. (when (equal (org-link-escape link) description)
  5850. ;; No description needed, it is identical
  5851. (setq description nil))
  5852. (when (and (not description)
  5853. (not (equal link (org-link-escape link))))
  5854. (setq description link))
  5855. (concat "[[" (org-link-escape link) "]"
  5856. (if description (concat "[" description "]") "")
  5857. "]"))
  5858. (defconst org-link-escape-chars
  5859. '((?\ . "%20")
  5860. (?\[ . "%5B")
  5861. (?\] . "%5D")
  5862. (?\340 . "%E0") ; `a
  5863. (?\342 . "%E2") ; ^a
  5864. (?\347 . "%E7") ; ,c
  5865. (?\350 . "%E8") ; `e
  5866. (?\351 . "%E9") ; 'e
  5867. (?\352 . "%EA") ; ^e
  5868. (?\356 . "%EE") ; ^i
  5869. (?\364 . "%F4") ; ^o
  5870. (?\371 . "%F9") ; `u
  5871. (?\373 . "%FB") ; ^u
  5872. (?\; . "%3B")
  5873. (?? . "%3F")
  5874. (?= . "%3D")
  5875. (?+ . "%2B")
  5876. )
  5877. "Association list of escapes for some characters problematic in links.
  5878. This is the list that is used for internal purposes.")
  5879. (defconst org-link-escape-chars-browser
  5880. '((?\ . "%20")) ; 32 for the SPC char
  5881. "Association list of escapes for some characters problematic in links.
  5882. This is the list that is used before handing over to the browser.")
  5883. (defun org-link-escape (text &optional table)
  5884. "Escape charaters in TEXT that are problematic for links."
  5885. (setq table (or table org-link-escape-chars))
  5886. (when text
  5887. (let ((re (mapconcat (lambda (x) (regexp-quote
  5888. (char-to-string (car x))))
  5889. table "\\|")))
  5890. (while (string-match re text)
  5891. (setq text
  5892. (replace-match
  5893. (cdr (assoc (string-to-char (match-string 0 text))
  5894. table))
  5895. t t text)))
  5896. text)))
  5897. (defun org-link-unescape (text &optional table)
  5898. "Reverse the action of `org-link-escape'."
  5899. (setq table (or table org-link-escape-chars))
  5900. (when text
  5901. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5902. table "\\|")))
  5903. (while (string-match re text)
  5904. (setq text
  5905. (replace-match
  5906. (char-to-string (car (rassoc (match-string 0 text) table)))
  5907. t t text)))
  5908. text)))
  5909. (defun org-xor (a b)
  5910. "Exclusive or."
  5911. (if a (not b) b))
  5912. (defun org-get-header (header)
  5913. "Find a header field in the current buffer."
  5914. (save-excursion
  5915. (goto-char (point-min))
  5916. (let ((case-fold-search t) s)
  5917. (cond
  5918. ((eq header 'from)
  5919. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5920. (setq s (match-string 1)))
  5921. (while (string-match "\"" s)
  5922. (setq s (replace-match "" t t s)))
  5923. (if (string-match "[<(].*" s)
  5924. (setq s (replace-match "" t t s))))
  5925. ((eq header 'message-id)
  5926. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5927. (setq s (match-string 1))))
  5928. ((eq header 'subject)
  5929. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5930. (setq s (match-string 1)))))
  5931. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5932. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5933. s)))
  5934. (defun org-fixup-message-id-for-http (s)
  5935. "Replace special characters in a message id, so it can be used in an http query."
  5936. (while (string-match "<" s)
  5937. (setq s (replace-match "%3C" t t s)))
  5938. (while (string-match ">" s)
  5939. (setq s (replace-match "%3E" t t s)))
  5940. (while (string-match "@" s)
  5941. (setq s (replace-match "%40" t t s)))
  5942. s)
  5943. ;;;###autoload
  5944. (defun org-insert-link-global ()
  5945. "Insert a link like Org-mode does.
  5946. This command can be called in any mode to insert a link in Org-mode syntax."
  5947. (interactive)
  5948. (org-load-modules-maybe)
  5949. (org-run-like-in-org-mode 'org-insert-link))
  5950. (defun org-insert-link (&optional complete-file link-location)
  5951. "Insert a link. At the prompt, enter the link.
  5952. Completion can be used to select a link previously stored with
  5953. `org-store-link'. When the empty string is entered (i.e. if you just
  5954. press RET at the prompt), the link defaults to the most recently
  5955. stored link. As SPC triggers completion in the minibuffer, you need to
  5956. use M-SPC or C-q SPC to force the insertion of a space character.
  5957. You will also be prompted for a description, and if one is given, it will
  5958. be displayed in the buffer instead of the link.
  5959. If there is already a link at point, this command will allow you to edit link
  5960. and description parts.
  5961. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5962. be selected using completion. The path to the file will be relative to the
  5963. current directory if the file is in the current directory or a subdirectory.
  5964. Otherwise, the link will be the absolute path as completed in the minibuffer
  5965. \(i.e. normally ~/path/to/file).
  5966. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5967. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5968. of `org-keep-stored-link-after-insertion'.
  5969. If `org-make-link-description-function' is non-nil, this function will be
  5970. called with the link target, and the result will be the default
  5971. link description.
  5972. If the LINK-LOCATION parameter is non-nil, this value will be
  5973. used as the link location instead of reading one interactively."
  5974. (interactive "P")
  5975. (let* ((wcf (current-window-configuration))
  5976. (region (if (org-region-active-p)
  5977. (buffer-substring (region-beginning) (region-end))))
  5978. (remove (and region (list (region-beginning) (region-end))))
  5979. (desc region)
  5980. tmphist ; byte-compile incorrectly complains about this
  5981. (link link-location)
  5982. entry file)
  5983. (cond
  5984. (link-location) ; specified by arg, just use it.
  5985. ((org-in-regexp org-bracket-link-regexp 1)
  5986. ;; We do have a link at point, and we are going to edit it.
  5987. (setq remove (list (match-beginning 0) (match-end 0)))
  5988. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5989. (setq link (read-string "Link: "
  5990. (org-link-unescape
  5991. (org-match-string-no-properties 1)))))
  5992. ((or (org-in-regexp org-angle-link-re)
  5993. (org-in-regexp org-plain-link-re))
  5994. ;; Convert to bracket link
  5995. (setq remove (list (match-beginning 0) (match-end 0))
  5996. link (read-string "Link: "
  5997. (org-remove-angle-brackets (match-string 0)))))
  5998. ((equal complete-file '(4))
  5999. ;; Completing read for file names.
  6000. (setq file (read-file-name "File: "))
  6001. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  6002. (pwd1 (file-name-as-directory (abbreviate-file-name
  6003. (expand-file-name ".")))))
  6004. (cond
  6005. ((equal complete-file '(16))
  6006. (setq link (org-make-link
  6007. "file:"
  6008. (abbreviate-file-name (expand-file-name file)))))
  6009. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  6010. (setq link (org-make-link "file:" (match-string 1 file))))
  6011. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  6012. (expand-file-name file))
  6013. (setq link (org-make-link
  6014. "file:" (match-string 1 (expand-file-name file)))))
  6015. (t (setq link (org-make-link "file:" file))))))
  6016. (t
  6017. ;; Read link, with completion for stored links.
  6018. (with-output-to-temp-buffer "*Org Links*"
  6019. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  6020. (when org-stored-links
  6021. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  6022. (princ (mapconcat
  6023. (lambda (x)
  6024. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  6025. (reverse org-stored-links) "\n"))))
  6026. (let ((cw (selected-window)))
  6027. (select-window (get-buffer-window "*Org Links*"))
  6028. (shrink-window-if-larger-than-buffer)
  6029. (setq truncate-lines t)
  6030. (select-window cw))
  6031. ;; Fake a link history, containing the stored links.
  6032. (setq tmphist (append (mapcar 'car org-stored-links)
  6033. org-insert-link-history))
  6034. (unwind-protect
  6035. (setq link (org-completing-read
  6036. "Link: "
  6037. (append
  6038. (mapcar (lambda (x) (list (concat (car x) ":")))
  6039. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  6040. (mapcar (lambda (x) (list (concat x ":")))
  6041. org-link-types))
  6042. nil nil nil
  6043. 'tmphist
  6044. (or (car (car org-stored-links)))))
  6045. (set-window-configuration wcf)
  6046. (kill-buffer "*Org Links*"))
  6047. (setq entry (assoc link org-stored-links))
  6048. (or entry (push link org-insert-link-history))
  6049. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  6050. (not org-keep-stored-link-after-insertion))
  6051. (setq org-stored-links (delq (assoc link org-stored-links)
  6052. org-stored-links)))
  6053. (setq desc (or desc (nth 1 entry)))))
  6054. (if (string-match org-plain-link-re link)
  6055. ;; URL-like link, normalize the use of angular brackets.
  6056. (setq link (org-make-link (org-remove-angle-brackets link))))
  6057. ;; Check if we are linking to the current file with a search option
  6058. ;; If yes, simplify the link by using only the search option.
  6059. (when (and buffer-file-name
  6060. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6061. (let* ((path (match-string 1 link))
  6062. (case-fold-search nil)
  6063. (search (match-string 2 link)))
  6064. (save-match-data
  6065. (if (equal (file-truename buffer-file-name) (file-truename path))
  6066. ;; We are linking to this same file, with a search option
  6067. (setq link search)))))
  6068. ;; Check if we can/should use a relative path. If yes, simplify the link
  6069. (when (string-match "\\<file:\\(.*\\)" link)
  6070. (let* ((path (match-string 1 link))
  6071. (origpath path)
  6072. (case-fold-search nil))
  6073. (cond
  6074. ((eq org-link-file-path-type 'absolute)
  6075. (setq path (abbreviate-file-name (expand-file-name path))))
  6076. ((eq org-link-file-path-type 'noabbrev)
  6077. (setq path (expand-file-name path)))
  6078. ((eq org-link-file-path-type 'relative)
  6079. (setq path (file-relative-name path)))
  6080. (t
  6081. (save-match-data
  6082. (if (string-match (concat "^" (regexp-quote
  6083. (file-name-as-directory
  6084. (expand-file-name "."))))
  6085. (expand-file-name path))
  6086. ;; We are linking a file with relative path name.
  6087. (setq path (substring (expand-file-name path)
  6088. (match-end 0)))))))
  6089. (setq link (concat "file:" path))
  6090. (if (equal desc origpath)
  6091. (setq desc path))))
  6092. (if org-make-link-description-function
  6093. (setq desc (funcall org-make-link-description-function link desc)))
  6094. (setq desc (read-string "Description: " desc))
  6095. (unless (string-match "\\S-" desc) (setq desc nil))
  6096. (if remove (apply 'delete-region remove))
  6097. (insert (org-make-link-string link desc))))
  6098. (defun org-completing-read (&rest args)
  6099. (let ((minibuffer-local-completion-map
  6100. (copy-keymap minibuffer-local-completion-map)))
  6101. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6102. (apply 'completing-read args)))
  6103. ;;; Opening/following a link
  6104. (defvar org-link-search-failed nil)
  6105. (defun org-next-link ()
  6106. "Move forward to the next link.
  6107. If the link is in hidden text, expose it."
  6108. (interactive)
  6109. (when (and org-link-search-failed (eq this-command last-command))
  6110. (goto-char (point-min))
  6111. (message "Link search wrapped back to beginning of buffer"))
  6112. (setq org-link-search-failed nil)
  6113. (let* ((pos (point))
  6114. (ct (org-context))
  6115. (a (assoc :link ct)))
  6116. (if a (goto-char (nth 2 a)))
  6117. (if (re-search-forward org-any-link-re nil t)
  6118. (progn
  6119. (goto-char (match-beginning 0))
  6120. (if (org-invisible-p) (org-show-context)))
  6121. (goto-char pos)
  6122. (setq org-link-search-failed t)
  6123. (error "No further link found"))))
  6124. (defun org-previous-link ()
  6125. "Move backward to the previous link.
  6126. If the link is in hidden text, expose it."
  6127. (interactive)
  6128. (when (and org-link-search-failed (eq this-command last-command))
  6129. (goto-char (point-max))
  6130. (message "Link search wrapped back to end of buffer"))
  6131. (setq org-link-search-failed nil)
  6132. (let* ((pos (point))
  6133. (ct (org-context))
  6134. (a (assoc :link ct)))
  6135. (if a (goto-char (nth 1 a)))
  6136. (if (re-search-backward org-any-link-re nil t)
  6137. (progn
  6138. (goto-char (match-beginning 0))
  6139. (if (org-invisible-p) (org-show-context)))
  6140. (goto-char pos)
  6141. (setq org-link-search-failed t)
  6142. (error "No further link found"))))
  6143. (defun org-find-file-at-mouse (ev)
  6144. "Open file link or URL at mouse."
  6145. (interactive "e")
  6146. (mouse-set-point ev)
  6147. (org-open-at-point 'in-emacs))
  6148. (defun org-open-at-mouse (ev)
  6149. "Open file link or URL at mouse."
  6150. (interactive "e")
  6151. (mouse-set-point ev)
  6152. (org-open-at-point))
  6153. (defvar org-window-config-before-follow-link nil
  6154. "The window configuration before following a link.
  6155. This is saved in case the need arises to restore it.")
  6156. (defvar org-open-link-marker (make-marker)
  6157. "Marker pointing to the location where `org-open-at-point; was called.")
  6158. ;;;###autoload
  6159. (defun org-open-at-point-global ()
  6160. "Follow a link like Org-mode does.
  6161. This command can be called in any mode to follow a link that has
  6162. Org-mode syntax."
  6163. (interactive)
  6164. (org-run-like-in-org-mode 'org-open-at-point))
  6165. ;;;###autoload
  6166. (defun org-open-link-from-string (s &optional arg)
  6167. "Open a link in the string S, as if it was in Org-mode."
  6168. (interactive "sLink: \nP")
  6169. (with-temp-buffer
  6170. (let ((org-inhibit-startup t))
  6171. (org-mode)
  6172. (insert s)
  6173. (goto-char (point-min))
  6174. (org-open-at-point arg))))
  6175. (defun org-open-at-point (&optional in-emacs)
  6176. "Open link at or after point.
  6177. If there is no link at point, this function will search forward up to
  6178. the end of the current subtree.
  6179. Normally, files will be opened by an appropriate application. If the
  6180. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6181. (interactive "P")
  6182. (org-load-modules-maybe)
  6183. (move-marker org-open-link-marker (point))
  6184. (setq org-window-config-before-follow-link (current-window-configuration))
  6185. (org-remove-occur-highlights nil nil t)
  6186. (if (org-at-timestamp-p t)
  6187. (org-follow-timestamp-link)
  6188. (let (type path link line search (pos (point)))
  6189. (catch 'match
  6190. (save-excursion
  6191. (skip-chars-forward "^]\n\r")
  6192. (when (org-in-regexp org-bracket-link-regexp)
  6193. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6194. (while (string-match " *\n *" link)
  6195. (setq link (replace-match " " t t link)))
  6196. (setq link (org-link-expand-abbrev link))
  6197. (if (string-match org-link-re-with-space2 link)
  6198. (setq type (match-string 1 link) path (match-string 2 link))
  6199. (setq type "thisfile" path link))
  6200. (throw 'match t)))
  6201. (when (get-text-property (point) 'org-linked-text)
  6202. (setq type "thisfile"
  6203. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6204. (1+ (point)) (point))
  6205. path (buffer-substring
  6206. (previous-single-property-change pos 'org-linked-text)
  6207. (next-single-property-change pos 'org-linked-text)))
  6208. (throw 'match t))
  6209. (save-excursion
  6210. (when (or (org-in-regexp org-angle-link-re)
  6211. (org-in-regexp org-plain-link-re))
  6212. (setq type (match-string 1) path (match-string 2))
  6213. (throw 'match t)))
  6214. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6215. (setq type "tree-match"
  6216. path (match-string 1))
  6217. (throw 'match t))
  6218. (save-excursion
  6219. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6220. (setq type "tags"
  6221. path (match-string 1))
  6222. (while (string-match ":" path)
  6223. (setq path (replace-match "+" t t path)))
  6224. (throw 'match t))))
  6225. (unless path
  6226. (error "No link found"))
  6227. ;; Remove any trailing spaces in path
  6228. (if (string-match " +\\'" path)
  6229. (setq path (replace-match "" t t path)))
  6230. (cond
  6231. ((assoc type org-link-protocols)
  6232. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6233. ((equal type "mailto")
  6234. (let ((cmd (car org-link-mailto-program))
  6235. (args (cdr org-link-mailto-program)) args1
  6236. (address path) (subject "") a)
  6237. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6238. (setq address (match-string 1 path)
  6239. subject (org-link-escape (match-string 2 path))))
  6240. (while args
  6241. (cond
  6242. ((not (stringp (car args))) (push (pop args) args1))
  6243. (t (setq a (pop args))
  6244. (if (string-match "%a" a)
  6245. (setq a (replace-match address t t a)))
  6246. (if (string-match "%s" a)
  6247. (setq a (replace-match subject t t a)))
  6248. (push a args1))))
  6249. (apply cmd (nreverse args1))))
  6250. ((member type '("http" "https" "ftp" "news"))
  6251. (browse-url (concat type ":" (org-link-escape
  6252. path org-link-escape-chars-browser))))
  6253. ((member type '("message"))
  6254. (browse-url (concat type ":" path)))
  6255. ((string= type "tags")
  6256. (org-tags-view in-emacs path))
  6257. ((string= type "thisfile")
  6258. (if in-emacs
  6259. (switch-to-buffer-other-window
  6260. (org-get-buffer-for-internal-link (current-buffer)))
  6261. (org-mark-ring-push))
  6262. (let ((cmd `(org-link-search
  6263. ,path
  6264. ,(cond ((equal in-emacs '(4)) 'occur)
  6265. ((equal in-emacs '(16)) 'org-occur)
  6266. (t nil))
  6267. ,pos)))
  6268. (condition-case nil (eval cmd)
  6269. (error (progn (widen) (eval cmd))))))
  6270. ((string= type "tree-match")
  6271. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6272. ((string= type "file")
  6273. (if (string-match "::\\([0-9]+\\)\\'" path)
  6274. (setq line (string-to-number (match-string 1 path))
  6275. path (substring path 0 (match-beginning 0)))
  6276. (if (string-match "::\\(.+\\)\\'" path)
  6277. (setq search (match-string 1 path)
  6278. path (substring path 0 (match-beginning 0)))))
  6279. (if (string-match "[*?{]" (file-name-nondirectory path))
  6280. (dired path)
  6281. (org-open-file path in-emacs line search)))
  6282. ((string= type "news")
  6283. (require 'org-gnus)
  6284. (org-gnus-follow-link path))
  6285. ((string= type "shell")
  6286. (let ((cmd path))
  6287. (if (or (not org-confirm-shell-link-function)
  6288. (funcall org-confirm-shell-link-function
  6289. (format "Execute \"%s\" in shell? "
  6290. (org-add-props cmd nil
  6291. 'face 'org-warning))))
  6292. (progn
  6293. (message "Executing %s" cmd)
  6294. (shell-command cmd))
  6295. (error "Abort"))))
  6296. ((string= type "elisp")
  6297. (let ((cmd path))
  6298. (if (or (not org-confirm-elisp-link-function)
  6299. (funcall org-confirm-elisp-link-function
  6300. (format "Execute \"%s\" as elisp? "
  6301. (org-add-props cmd nil
  6302. 'face 'org-warning))))
  6303. (message "%s => %s" cmd (eval (read cmd)))
  6304. (error "Abort"))))
  6305. (t
  6306. (browse-url-at-point)))))
  6307. (move-marker org-open-link-marker nil)
  6308. (run-hook-with-args 'org-follow-link-hook))
  6309. ;;;; Time estimates
  6310. (defun org-get-effort (&optional pom)
  6311. "Get the effort estimate for the current entry."
  6312. (org-entry-get pom org-effort-property))
  6313. ;;; File search
  6314. (defvar org-create-file-search-functions nil
  6315. "List of functions to construct the right search string for a file link.
  6316. These functions are called in turn with point at the location to
  6317. which the link should point.
  6318. A function in the hook should first test if it would like to
  6319. handle this file type, for example by checking the major-mode or
  6320. the file extension. If it decides not to handle this file, it
  6321. should just return nil to give other functions a chance. If it
  6322. does handle the file, it must return the search string to be used
  6323. when following the link. The search string will be part of the
  6324. file link, given after a double colon, and `org-open-at-point'
  6325. will automatically search for it. If special measures must be
  6326. taken to make the search successful, another function should be
  6327. added to the companion hook `org-execute-file-search-functions',
  6328. which see.
  6329. A function in this hook may also use `setq' to set the variable
  6330. `description' to provide a suggestion for the descriptive text to
  6331. be used for this link when it gets inserted into an Org-mode
  6332. buffer with \\[org-insert-link].")
  6333. (defvar org-execute-file-search-functions nil
  6334. "List of functions to execute a file search triggered by a link.
  6335. Functions added to this hook must accept a single argument, the
  6336. search string that was part of the file link, the part after the
  6337. double colon. The function must first check if it would like to
  6338. handle this search, for example by checking the major-mode or the
  6339. file extension. If it decides not to handle this search, it
  6340. should just return nil to give other functions a chance. If it
  6341. does handle the search, it must return a non-nil value to keep
  6342. other functions from trying.
  6343. Each function can access the current prefix argument through the
  6344. variable `current-prefix-argument'. Note that a single prefix is
  6345. used to force opening a link in Emacs, so it may be good to only
  6346. use a numeric or double prefix to guide the search function.
  6347. In case this is needed, a function in this hook can also restore
  6348. the window configuration before `org-open-at-point' was called using:
  6349. (set-window-configuration org-window-config-before-follow-link)")
  6350. (defun org-link-search (s &optional type avoid-pos)
  6351. "Search for a link search option.
  6352. If S is surrounded by forward slashes, it is interpreted as a
  6353. regular expression. In org-mode files, this will create an `org-occur'
  6354. sparse tree. In ordinary files, `occur' will be used to list matches.
  6355. If the current buffer is in `dired-mode', grep will be used to search
  6356. in all files. If AVOID-POS is given, ignore matches near that position."
  6357. (let ((case-fold-search t)
  6358. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6359. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6360. (append '(("") (" ") ("\t") ("\n"))
  6361. org-emphasis-alist)
  6362. "\\|") "\\)"))
  6363. (pos (point))
  6364. (pre nil) (post nil)
  6365. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6366. (cond
  6367. ;; First check if there are any special
  6368. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6369. ;; Now try the builtin stuff
  6370. ((save-excursion
  6371. (goto-char (point-min))
  6372. (and
  6373. (re-search-forward
  6374. (concat "<<" (regexp-quote s0) ">>") nil t)
  6375. (setq type 'dedicated
  6376. pos (match-beginning 0))))
  6377. ;; There is an exact target for this
  6378. (goto-char pos))
  6379. ((string-match "^/\\(.*\\)/$" s)
  6380. ;; A regular expression
  6381. (cond
  6382. ((org-mode-p)
  6383. (org-occur (match-string 1 s)))
  6384. ;;((eq major-mode 'dired-mode)
  6385. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6386. (t (org-do-occur (match-string 1 s)))))
  6387. (t
  6388. ;; A normal search strings
  6389. (when (equal (string-to-char s) ?*)
  6390. ;; Anchor on headlines, post may include tags.
  6391. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6392. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6393. s (substring s 1)))
  6394. (remove-text-properties
  6395. 0 (length s)
  6396. '(face nil mouse-face nil keymap nil fontified nil) s)
  6397. ;; Make a series of regular expressions to find a match
  6398. (setq words (org-split-string s "[ \n\r\t]+")
  6399. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6400. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6401. "\\)" markers)
  6402. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6403. re2a (concat "[ \t\r\n]" re2a_)
  6404. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6405. re4 (concat "[^a-zA-Z_]" re4_)
  6406. re1 (concat pre re2 post)
  6407. re3 (concat pre (if pre re4_ re4) post)
  6408. re5 (concat pre ".*" re4)
  6409. re2 (concat pre re2)
  6410. re2a (concat pre (if pre re2a_ re2a))
  6411. re4 (concat pre (if pre re4_ re4))
  6412. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6413. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6414. re5 "\\)"
  6415. ))
  6416. (cond
  6417. ((eq type 'org-occur) (org-occur reall))
  6418. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6419. (t (goto-char (point-min))
  6420. (setq type 'fuzzy)
  6421. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6422. (org-search-not-self 1 re1 nil t)
  6423. (org-search-not-self 1 re2 nil t)
  6424. (org-search-not-self 1 re2a nil t)
  6425. (org-search-not-self 1 re3 nil t)
  6426. (org-search-not-self 1 re4 nil t)
  6427. (org-search-not-self 1 re5 nil t)
  6428. )
  6429. (goto-char (match-beginning 1))
  6430. (goto-char pos)
  6431. (error "No match")))))
  6432. (t
  6433. ;; Normal string-search
  6434. (goto-char (point-min))
  6435. (if (search-forward s nil t)
  6436. (goto-char (match-beginning 0))
  6437. (error "No match"))))
  6438. (and (org-mode-p) (org-show-context 'link-search))
  6439. type))
  6440. (defun org-search-not-self (group &rest args)
  6441. "Execute `re-search-forward', but only accept matches that do not
  6442. enclose the position of `org-open-link-marker'."
  6443. (let ((m org-open-link-marker))
  6444. (catch 'exit
  6445. (while (apply 're-search-forward args)
  6446. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6447. (goto-char (match-end group))
  6448. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6449. (> (match-beginning 0) (marker-position m))
  6450. (< (match-end 0) (marker-position m)))
  6451. (save-match-data
  6452. (or (not (org-in-regexp
  6453. org-bracket-link-analytic-regexp 1))
  6454. (not (match-end 4)) ; no description
  6455. (and (<= (match-beginning 4) (point))
  6456. (>= (match-end 4) (point))))))
  6457. (throw 'exit (point))))))))
  6458. (defun org-get-buffer-for-internal-link (buffer)
  6459. "Return a buffer to be used for displaying the link target of internal links."
  6460. (cond
  6461. ((not org-display-internal-link-with-indirect-buffer)
  6462. buffer)
  6463. ((string-match "(Clone)$" (buffer-name buffer))
  6464. (message "Buffer is already a clone, not making another one")
  6465. ;; we also do not modify visibility in this case
  6466. buffer)
  6467. (t ; make a new indirect buffer for displaying the link
  6468. (let* ((bn (buffer-name buffer))
  6469. (ibn (concat bn "(Clone)"))
  6470. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6471. (with-current-buffer ib (org-overview))
  6472. ib))))
  6473. (defun org-do-occur (regexp &optional cleanup)
  6474. "Call the Emacs command `occur'.
  6475. If CLEANUP is non-nil, remove the printout of the regular expression
  6476. in the *Occur* buffer. This is useful if the regex is long and not useful
  6477. to read."
  6478. (occur regexp)
  6479. (when cleanup
  6480. (let ((cwin (selected-window)) win beg end)
  6481. (when (setq win (get-buffer-window "*Occur*"))
  6482. (select-window win))
  6483. (goto-char (point-min))
  6484. (when (re-search-forward "match[a-z]+" nil t)
  6485. (setq beg (match-end 0))
  6486. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6487. (setq end (1- (match-beginning 0)))))
  6488. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6489. (goto-char (point-min))
  6490. (select-window cwin))))
  6491. ;;; The mark ring for links jumps
  6492. (defvar org-mark-ring nil
  6493. "Mark ring for positions before jumps in Org-mode.")
  6494. (defvar org-mark-ring-last-goto nil
  6495. "Last position in the mark ring used to go back.")
  6496. ;; Fill and close the ring
  6497. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6498. (loop for i from 1 to org-mark-ring-length do
  6499. (push (make-marker) org-mark-ring))
  6500. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6501. org-mark-ring)
  6502. (defun org-mark-ring-push (&optional pos buffer)
  6503. "Put the current position or POS into the mark ring and rotate it."
  6504. (interactive)
  6505. (setq pos (or pos (point)))
  6506. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6507. (move-marker (car org-mark-ring)
  6508. (or pos (point))
  6509. (or buffer (current-buffer)))
  6510. (message "%s"
  6511. (substitute-command-keys
  6512. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6513. (defun org-mark-ring-goto (&optional n)
  6514. "Jump to the previous position in the mark ring.
  6515. With prefix arg N, jump back that many stored positions. When
  6516. called several times in succession, walk through the entire ring.
  6517. Org-mode commands jumping to a different position in the current file,
  6518. or to another Org-mode file, automatically push the old position
  6519. onto the ring."
  6520. (interactive "p")
  6521. (let (p m)
  6522. (if (eq last-command this-command)
  6523. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6524. (setq p org-mark-ring))
  6525. (setq org-mark-ring-last-goto p)
  6526. (setq m (car p))
  6527. (switch-to-buffer (marker-buffer m))
  6528. (goto-char m)
  6529. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6530. (defun org-remove-angle-brackets (s)
  6531. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6532. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6533. s)
  6534. (defun org-add-angle-brackets (s)
  6535. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6536. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6537. s)
  6538. (defun org-remove-double-quotes (s)
  6539. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6540. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6541. s)
  6542. ;;; Following specific links
  6543. (defun org-follow-timestamp-link ()
  6544. (cond
  6545. ((org-at-date-range-p t)
  6546. (let ((org-agenda-start-on-weekday)
  6547. (t1 (match-string 1))
  6548. (t2 (match-string 2)))
  6549. (setq t1 (time-to-days (org-time-string-to-time t1))
  6550. t2 (time-to-days (org-time-string-to-time t2)))
  6551. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6552. ((org-at-timestamp-p t)
  6553. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6554. (substring (match-string 1) 0 10)))
  6555. 1))
  6556. (t (error "This should not happen"))))
  6557. ;;; Following file links
  6558. (defvar org-wait nil)
  6559. (defun org-open-file (path &optional in-emacs line search)
  6560. "Open the file at PATH.
  6561. First, this expands any special file name abbreviations. Then the
  6562. configuration variable `org-file-apps' is checked if it contains an
  6563. entry for this file type, and if yes, the corresponding command is launched.
  6564. If no application is found, Emacs simply visits the file.
  6565. With optional argument IN-EMACS, Emacs will visit the file.
  6566. Optional LINE specifies a line to go to, optional SEARCH a string to
  6567. search for. If LINE or SEARCH is given, the file will always be
  6568. opened in Emacs.
  6569. If the file does not exist, an error is thrown."
  6570. (setq in-emacs (or in-emacs line search))
  6571. (let* ((file (if (equal path "")
  6572. buffer-file-name
  6573. (substitute-in-file-name (expand-file-name path))))
  6574. (apps (append org-file-apps (org-default-apps)))
  6575. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6576. (dirp (if remp nil (file-directory-p file)))
  6577. (dfile (downcase file))
  6578. (old-buffer (current-buffer))
  6579. (old-pos (point))
  6580. (old-mode major-mode)
  6581. ext cmd)
  6582. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6583. (setq ext (match-string 1 dfile))
  6584. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6585. (setq ext (match-string 1 dfile))))
  6586. (if in-emacs
  6587. (setq cmd 'emacs)
  6588. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6589. (and dirp (cdr (assoc 'directory apps)))
  6590. (cdr (assoc ext apps))
  6591. (cdr (assoc t apps)))))
  6592. (when (eq cmd 'mailcap)
  6593. (require 'mailcap)
  6594. (mailcap-parse-mailcaps)
  6595. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6596. (command (mailcap-mime-info mime-type)))
  6597. (if (stringp command)
  6598. (setq cmd command)
  6599. (setq cmd 'emacs))))
  6600. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6601. (not (file-exists-p file))
  6602. (not org-open-non-existing-files))
  6603. (error "No such file: %s" file))
  6604. (cond
  6605. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6606. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6607. (while (string-match "['\"]%s['\"]" cmd)
  6608. (setq cmd (replace-match "%s" t t cmd)))
  6609. (while (string-match "%s" cmd)
  6610. (setq cmd (replace-match
  6611. (save-match-data (shell-quote-argument file))
  6612. t t cmd)))
  6613. (save-window-excursion
  6614. (start-process-shell-command cmd nil cmd)
  6615. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6616. ))
  6617. ((or (stringp cmd)
  6618. (eq cmd 'emacs))
  6619. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6620. (widen)
  6621. (if line (goto-line line)
  6622. (if search (org-link-search search))))
  6623. ((consp cmd)
  6624. (eval cmd))
  6625. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6626. (and (org-mode-p) (eq old-mode 'org-mode)
  6627. (or (not (equal old-buffer (current-buffer)))
  6628. (not (equal old-pos (point))))
  6629. (org-mark-ring-push old-pos old-buffer))))
  6630. (defun org-default-apps ()
  6631. "Return the default applications for this operating system."
  6632. (cond
  6633. ((eq system-type 'darwin)
  6634. org-file-apps-defaults-macosx)
  6635. ((eq system-type 'windows-nt)
  6636. org-file-apps-defaults-windowsnt)
  6637. (t org-file-apps-defaults-gnu)))
  6638. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6639. (defun org-file-remote-p (file)
  6640. "Test whether FILE specifies a location on a remote system.
  6641. Return non-nil if the location is indeed remote.
  6642. For example, the filename \"/user@host:/foo\" specifies a location
  6643. on the system \"/user@host:\"."
  6644. (cond ((fboundp 'file-remote-p)
  6645. (file-remote-p file))
  6646. ((fboundp 'tramp-handle-file-remote-p)
  6647. (tramp-handle-file-remote-p file))
  6648. ((and (boundp 'ange-ftp-name-format)
  6649. (string-match (car ange-ftp-name-format) file))
  6650. t)
  6651. (t nil)))
  6652. ;;;; Refiling
  6653. (defun org-get-org-file ()
  6654. "Read a filename, with default directory `org-directory'."
  6655. (let ((default (or org-default-notes-file remember-data-file)))
  6656. (read-file-name (format "File name [%s]: " default)
  6657. (file-name-as-directory org-directory)
  6658. default)))
  6659. (defun org-notes-order-reversed-p ()
  6660. "Check if the current file should receive notes in reversed order."
  6661. (cond
  6662. ((not org-reverse-note-order) nil)
  6663. ((eq t org-reverse-note-order) t)
  6664. ((not (listp org-reverse-note-order)) nil)
  6665. (t (catch 'exit
  6666. (let ((all org-reverse-note-order)
  6667. entry)
  6668. (while (setq entry (pop all))
  6669. (if (string-match (car entry) buffer-file-name)
  6670. (throw 'exit (cdr entry))))
  6671. nil)))))
  6672. (defvar org-refile-target-table nil
  6673. "The list of refile targets, created by `org-refile'.")
  6674. (defvar org-agenda-new-buffers nil
  6675. "Buffers created to visit agenda files.")
  6676. (defun org-get-refile-targets (&optional default-buffer)
  6677. "Produce a table with refile targets."
  6678. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6679. targets txt re files f desc descre)
  6680. (with-current-buffer (or default-buffer (current-buffer))
  6681. (while (setq entry (pop entries))
  6682. (setq files (car entry) desc (cdr entry))
  6683. (cond
  6684. ((null files) (setq files (list (current-buffer))))
  6685. ((eq files 'org-agenda-files)
  6686. (setq files (org-agenda-files 'unrestricted)))
  6687. ((and (symbolp files) (fboundp files))
  6688. (setq files (funcall files)))
  6689. ((and (symbolp files) (boundp files))
  6690. (setq files (symbol-value files))))
  6691. (if (stringp files) (setq files (list files)))
  6692. (cond
  6693. ((eq (car desc) :tag)
  6694. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6695. ((eq (car desc) :todo)
  6696. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6697. ((eq (car desc) :regexp)
  6698. (setq descre (cdr desc)))
  6699. ((eq (car desc) :level)
  6700. (setq descre (concat "^\\*\\{" (number-to-string
  6701. (if org-odd-levels-only
  6702. (1- (* 2 (cdr desc)))
  6703. (cdr desc)))
  6704. "\\}[ \t]")))
  6705. ((eq (car desc) :maxlevel)
  6706. (setq descre (concat "^\\*\\{1," (number-to-string
  6707. (if org-odd-levels-only
  6708. (1- (* 2 (cdr desc)))
  6709. (cdr desc)))
  6710. "\\}[ \t]")))
  6711. (t (error "Bad refiling target description %s" desc)))
  6712. (while (setq f (pop files))
  6713. (save-excursion
  6714. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6715. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6716. (save-excursion
  6717. (save-restriction
  6718. (widen)
  6719. (goto-char (point-min))
  6720. (while (re-search-forward descre nil t)
  6721. (goto-char (point-at-bol))
  6722. (when (looking-at org-complex-heading-regexp)
  6723. (setq txt (match-string 4)
  6724. re (concat "^" (regexp-quote
  6725. (buffer-substring (match-beginning 1)
  6726. (match-end 4)))))
  6727. (if (match-end 5) (setq re (concat re "[ \t]+"
  6728. (regexp-quote
  6729. (match-string 5)))))
  6730. (setq re (concat re "[ \t]*$"))
  6731. (when org-refile-use-outline-path
  6732. (setq txt (mapconcat 'identity
  6733. (append
  6734. (if (eq org-refile-use-outline-path 'file)
  6735. (list (file-name-nondirectory
  6736. (buffer-file-name (buffer-base-buffer))))
  6737. (if (eq org-refile-use-outline-path 'full-file-path)
  6738. (list (buffer-file-name (buffer-base-buffer)))))
  6739. (org-get-outline-path)
  6740. (list txt))
  6741. "/")))
  6742. (push (list txt f re (point)) targets))
  6743. (goto-char (point-at-eol))))))))
  6744. (nreverse targets))))
  6745. (defun org-get-outline-path ()
  6746. "Return the outline path to the current entry, as a list."
  6747. (let (rtn)
  6748. (save-excursion
  6749. (while (org-up-heading-safe)
  6750. (when (looking-at org-complex-heading-regexp)
  6751. (push (org-match-string-no-properties 4) rtn)))
  6752. rtn)))
  6753. (defvar org-refile-history nil
  6754. "History for refiling operations.")
  6755. (defun org-refile (&optional goto default-buffer)
  6756. "Move the entry at point to another heading.
  6757. The list of target headings is compiled using the information in
  6758. `org-refile-targets', which see. This list is created before each use
  6759. and will therefore always be up-to-date.
  6760. At the target location, the entry is filed as a subitem of the target heading.
  6761. Depending on `org-reverse-note-order', the new subitem will either be the
  6762. first of the last subitem.
  6763. With prefix arg GOTO, the command will only visit the target location,
  6764. not actually move anything.
  6765. With a double prefix `C-c C-c', go to the location where the last refiling
  6766. operation has put the subtree."
  6767. (interactive "P")
  6768. (let* ((cbuf (current-buffer))
  6769. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6770. pos it nbuf file re level reversed)
  6771. (if (equal goto '(16))
  6772. (org-refile-goto-last-stored)
  6773. (when (setq it (org-refile-get-location
  6774. (if goto "Goto: " "Refile to: ") default-buffer))
  6775. (setq file (nth 1 it)
  6776. re (nth 2 it)
  6777. pos (nth 3 it))
  6778. (setq nbuf (or (find-buffer-visiting file)
  6779. (find-file-noselect file)))
  6780. (if goto
  6781. (progn
  6782. (switch-to-buffer nbuf)
  6783. (goto-char pos)
  6784. (org-show-context 'org-goto))
  6785. (org-copy-subtree 1 nil t)
  6786. (save-excursion
  6787. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6788. (find-file-noselect file))))
  6789. (setq reversed (org-notes-order-reversed-p))
  6790. (save-excursion
  6791. (save-restriction
  6792. (widen)
  6793. (goto-char pos)
  6794. (looking-at outline-regexp)
  6795. (setq level (org-get-valid-level (funcall outline-level) 1))
  6796. (goto-char
  6797. (if reversed
  6798. (outline-next-heading)
  6799. (or (save-excursion (outline-get-next-sibling))
  6800. (org-end-of-subtree t t)
  6801. (point-max))))
  6802. (bookmark-set "org-refile-last-stored")
  6803. (org-paste-subtree level))))
  6804. (org-cut-subtree)
  6805. (setq org-markers-to-move nil)
  6806. (message "Entry refiled to \"%s\"" (car it)))))))
  6807. (defun org-refile-goto-last-stored ()
  6808. "Go to the location where the last refile was stored."
  6809. (interactive)
  6810. (bookmark-jump "org-refile-last-stored")
  6811. (message "This is the location of the last refile"))
  6812. (defun org-refile-get-location (&optional prompt default-buffer)
  6813. "Prompt the user for a refile location, using PROMPT."
  6814. (let ((org-refile-targets org-refile-targets)
  6815. (org-refile-use-outline-path org-refile-use-outline-path))
  6816. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6817. (unless org-refile-target-table
  6818. (error "No refile targets"))
  6819. (let* ((cbuf (current-buffer))
  6820. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6821. (fname (and filename (file-truename filename)))
  6822. (tbl (mapcar
  6823. (lambda (x)
  6824. (if (not (equal fname (file-truename (nth 1 x))))
  6825. (cons (concat (car x) " (" (file-name-nondirectory
  6826. (nth 1 x)) ")")
  6827. (cdr x))
  6828. x))
  6829. org-refile-target-table))
  6830. (completion-ignore-case t))
  6831. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6832. tbl)))
  6833. ;;;; Dynamic blocks
  6834. (defun org-find-dblock (name)
  6835. "Find the first dynamic block with name NAME in the buffer.
  6836. If not found, stay at current position and return nil."
  6837. (let (pos)
  6838. (save-excursion
  6839. (goto-char (point-min))
  6840. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6841. nil t)
  6842. (match-beginning 0))))
  6843. (if pos (goto-char pos))
  6844. pos))
  6845. (defconst org-dblock-start-re
  6846. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6847. "Matches the startline of a dynamic block, with parameters.")
  6848. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6849. "Matches the end of a dyhamic block.")
  6850. (defun org-create-dblock (plist)
  6851. "Create a dynamic block section, with parameters taken from PLIST.
  6852. PLIST must containe a :name entry which is used as name of the block."
  6853. (unless (bolp) (newline))
  6854. (let ((name (plist-get plist :name)))
  6855. (insert "#+BEGIN: " name)
  6856. (while plist
  6857. (if (eq (car plist) :name)
  6858. (setq plist (cddr plist))
  6859. (insert " " (prin1-to-string (pop plist)))))
  6860. (insert "\n\n#+END:\n")
  6861. (beginning-of-line -2)))
  6862. (defun org-prepare-dblock ()
  6863. "Prepare dynamic block for refresh.
  6864. This empties the block, puts the cursor at the insert position and returns
  6865. the property list including an extra property :name with the block name."
  6866. (unless (looking-at org-dblock-start-re)
  6867. (error "Not at a dynamic block"))
  6868. (let* ((begdel (1+ (match-end 0)))
  6869. (name (org-no-properties (match-string 1)))
  6870. (params (append (list :name name)
  6871. (read (concat "(" (match-string 3) ")")))))
  6872. (unless (re-search-forward org-dblock-end-re nil t)
  6873. (error "Dynamic block not terminated"))
  6874. (setq params
  6875. (append params
  6876. (list :content (buffer-substring
  6877. begdel (match-beginning 0)))))
  6878. (delete-region begdel (match-beginning 0))
  6879. (goto-char begdel)
  6880. (open-line 1)
  6881. params))
  6882. (defun org-map-dblocks (&optional command)
  6883. "Apply COMMAND to all dynamic blocks in the current buffer.
  6884. If COMMAND is not given, use `org-update-dblock'."
  6885. (let ((cmd (or command 'org-update-dblock))
  6886. pos)
  6887. (save-excursion
  6888. (goto-char (point-min))
  6889. (while (re-search-forward org-dblock-start-re nil t)
  6890. (goto-char (setq pos (match-beginning 0)))
  6891. (condition-case nil
  6892. (funcall cmd)
  6893. (error (message "Error during update of dynamic block")))
  6894. (goto-char pos)
  6895. (unless (re-search-forward org-dblock-end-re nil t)
  6896. (error "Dynamic block not terminated"))))))
  6897. (defun org-dblock-update (&optional arg)
  6898. "User command for updating dynamic blocks.
  6899. Update the dynamic block at point. With prefix ARG, update all dynamic
  6900. blocks in the buffer."
  6901. (interactive "P")
  6902. (if arg
  6903. (org-update-all-dblocks)
  6904. (or (looking-at org-dblock-start-re)
  6905. (org-beginning-of-dblock))
  6906. (org-update-dblock)))
  6907. (defun org-update-dblock ()
  6908. "Update the dynamic block at point
  6909. This means to empty the block, parse for parameters and then call
  6910. the correct writing function."
  6911. (save-window-excursion
  6912. (let* ((pos (point))
  6913. (line (org-current-line))
  6914. (params (org-prepare-dblock))
  6915. (name (plist-get params :name))
  6916. (cmd (intern (concat "org-dblock-write:" name))))
  6917. (message "Updating dynamic block `%s' at line %d..." name line)
  6918. (funcall cmd params)
  6919. (message "Updating dynamic block `%s' at line %d...done" name line)
  6920. (goto-char pos))))
  6921. (defun org-beginning-of-dblock ()
  6922. "Find the beginning of the dynamic block at point.
  6923. Error if there is no scuh block at point."
  6924. (let ((pos (point))
  6925. beg)
  6926. (end-of-line 1)
  6927. (if (and (re-search-backward org-dblock-start-re nil t)
  6928. (setq beg (match-beginning 0))
  6929. (re-search-forward org-dblock-end-re nil t)
  6930. (> (match-end 0) pos))
  6931. (goto-char beg)
  6932. (goto-char pos)
  6933. (error "Not in a dynamic block"))))
  6934. (defun org-update-all-dblocks ()
  6935. "Update all dynamic blocks in the buffer.
  6936. This function can be used in a hook."
  6937. (when (org-mode-p)
  6938. (org-map-dblocks 'org-update-dblock)))
  6939. ;;;; Completion
  6940. (defconst org-additional-option-like-keywords
  6941. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6942. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6943. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6944. (defcustom org-structure-template-alist
  6945. '(
  6946. ("s" "#+begin_src ?\n\n#+end_src")
  6947. ("e" "#+begin_example\n?\n#+end_example")
  6948. ("q" "#+begin_quote\n?\n#+end_quote")
  6949. ("v" "#+begin_verse\n?\n#+end_verse")
  6950. ("l" "#+begin_latex\n?\n#+end_latex")
  6951. ("L" "#+latex: ")
  6952. ("h" "#+begin_html\n?\n#+end_html")
  6953. ("H" "#+html: ")
  6954. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6955. ("A" "#+ascii: ")
  6956. ("i" "#+include: %file ?")
  6957. )
  6958. "Structure completion elements.
  6959. This is a list of abbreviation keys and values. The value gets inserted
  6960. it you type @samp{.} followed by the key and then the completion key,
  6961. usually `M-TAB'. %file will be replaced by a file name after prompting
  6962. for the file uning completion.
  6963. This is an experimental feature, it is undecided if it is going to stay in."
  6964. :group 'org-completion
  6965. :type '(repeat
  6966. (string :tag "Key") (string :tag "Template")))
  6967. (defun org-complete-expand-structure-template (start cell)
  6968. "Expand a structure template."
  6969. (let ((rpl (nth 1 cell)))
  6970. (delete-region start (point))
  6971. (when (string-match "\\`#\\+" rpl)
  6972. (cond
  6973. ((bolp))
  6974. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6975. (delete-region (point-at-bol) (point)))
  6976. (t (newline))))
  6977. (setq start (point))
  6978. (if (string-match "%file" rpl)
  6979. (setq rpl (replace-match
  6980. (concat
  6981. "\""
  6982. (save-match-data
  6983. (abbreviate-file-name (read-file-name "Include file: ")))
  6984. "\"")
  6985. t t rpl)))
  6986. (insert rpl)
  6987. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6988. (defun org-complete (&optional arg)
  6989. "Perform completion on word at point.
  6990. At the beginning of a headline, this completes TODO keywords as given in
  6991. `org-todo-keywords'.
  6992. If the current word is preceded by a backslash, completes the TeX symbols
  6993. that are supported for HTML support.
  6994. If the current word is preceded by \"#+\", completes special words for
  6995. setting file options.
  6996. In the line after \"#+STARTUP:, complete valid keywords.\"
  6997. At all other locations, this simply calls the value of
  6998. `org-completion-fallback-command'."
  6999. (interactive "P")
  7000. (org-without-partial-completion
  7001. (catch 'exit
  7002. (let* ((a nil)
  7003. (end (point))
  7004. (beg1 (save-excursion
  7005. (skip-chars-backward (org-re "[:alnum:]_@"))
  7006. (point)))
  7007. (beg (save-excursion
  7008. (skip-chars-backward "a-zA-Z0-9_:$")
  7009. (point)))
  7010. (confirm (lambda (x) (stringp (car x))))
  7011. (searchhead (equal (char-before beg) ?*))
  7012. (struct
  7013. (when (and (equal (char-before beg1) ?.)
  7014. (setq a (assoc (buffer-substring beg1 (point))
  7015. org-structure-template-alist)))
  7016. (org-complete-expand-structure-template (1- beg1) a)
  7017. (throw 'exit t)))
  7018. (tag (and (equal (char-before beg1) ?:)
  7019. (equal (char-after (point-at-bol)) ?*)))
  7020. (prop (and (equal (char-before beg1) ?:)
  7021. (not (equal (char-after (point-at-bol)) ?*))))
  7022. (texp (equal (char-before beg) ?\\))
  7023. (link (equal (char-before beg) ?\[))
  7024. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7025. beg)
  7026. "#+"))
  7027. (startup (string-match "^#\\+STARTUP:.*"
  7028. (buffer-substring (point-at-bol) (point))))
  7029. (completion-ignore-case opt)
  7030. (type nil)
  7031. (tbl nil)
  7032. (table (cond
  7033. (opt
  7034. (setq type :opt)
  7035. (require 'org-exp)
  7036. (append
  7037. (mapcar
  7038. (lambda (x)
  7039. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7040. (cons (match-string 2 x) (match-string 1 x)))
  7041. (org-split-string (org-get-current-options) "\n"))
  7042. (mapcar 'list org-additional-option-like-keywords)))
  7043. (startup
  7044. (setq type :startup)
  7045. org-startup-options)
  7046. (link (append org-link-abbrev-alist-local
  7047. org-link-abbrev-alist))
  7048. (texp
  7049. (setq type :tex)
  7050. org-html-entities)
  7051. ((string-match "\\`\\*+[ \t]+\\'"
  7052. (buffer-substring (point-at-bol) beg))
  7053. (setq type :todo)
  7054. (mapcar 'list org-todo-keywords-1))
  7055. (searchhead
  7056. (setq type :searchhead)
  7057. (save-excursion
  7058. (goto-char (point-min))
  7059. (while (re-search-forward org-todo-line-regexp nil t)
  7060. (push (list
  7061. (org-make-org-heading-search-string
  7062. (match-string 3) t))
  7063. tbl)))
  7064. tbl)
  7065. (tag (setq type :tag beg beg1)
  7066. (or org-tag-alist (org-get-buffer-tags)))
  7067. (prop (setq type :prop beg beg1)
  7068. (mapcar 'list (org-buffer-property-keys nil t t)))
  7069. (t (progn
  7070. (call-interactively org-completion-fallback-command)
  7071. (throw 'exit nil)))))
  7072. (pattern (buffer-substring-no-properties beg end))
  7073. (completion (try-completion pattern table confirm)))
  7074. (cond ((eq completion t)
  7075. (if (not (assoc (upcase pattern) table))
  7076. (message "Already complete")
  7077. (if (and (equal type :opt)
  7078. (not (member (car (assoc (upcase pattern) table))
  7079. org-additional-option-like-keywords)))
  7080. (insert (substring (cdr (assoc (upcase pattern) table))
  7081. (length pattern)))
  7082. (if (memq type '(:tag :prop)) (insert ":")))))
  7083. ((null completion)
  7084. (message "Can't find completion for \"%s\"" pattern)
  7085. (ding))
  7086. ((not (string= pattern completion))
  7087. (delete-region beg end)
  7088. (if (string-match " +$" completion)
  7089. (setq completion (replace-match "" t t completion)))
  7090. (insert completion)
  7091. (if (get-buffer-window "*Completions*")
  7092. (delete-window (get-buffer-window "*Completions*")))
  7093. (if (assoc completion table)
  7094. (if (eq type :todo) (insert " ")
  7095. (if (memq type '(:tag :prop)) (insert ":"))))
  7096. (if (and (equal type :opt) (assoc completion table))
  7097. (message "%s" (substitute-command-keys
  7098. "Press \\[org-complete] again to insert example settings"))))
  7099. (t
  7100. (message "Making completion list...")
  7101. (let ((list (sort (all-completions pattern table confirm)
  7102. 'string<)))
  7103. (with-output-to-temp-buffer "*Completions*"
  7104. (condition-case nil
  7105. ;; Protection needed for XEmacs and emacs 21
  7106. (display-completion-list list pattern)
  7107. (error (display-completion-list list)))))
  7108. (message "Making completion list...%s" "done")))))))
  7109. ;;;; TODO, DEADLINE, Comments
  7110. (defun org-toggle-comment ()
  7111. "Change the COMMENT state of an entry."
  7112. (interactive)
  7113. (save-excursion
  7114. (org-back-to-heading)
  7115. (let (case-fold-search)
  7116. (if (looking-at (concat outline-regexp
  7117. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7118. (replace-match "" t t nil 1)
  7119. (if (looking-at outline-regexp)
  7120. (progn
  7121. (goto-char (match-end 0))
  7122. (insert org-comment-string " ")))))))
  7123. (defvar org-last-todo-state-is-todo nil
  7124. "This is non-nil when the last TODO state change led to a TODO state.
  7125. If the last change removed the TODO tag or switched to DONE, then
  7126. this is nil.")
  7127. (defvar org-setting-tags nil) ; dynamically skiped
  7128. (defun org-parse-local-options (string var)
  7129. "Parse STRING for startup setting relevant for variable VAR."
  7130. (let ((rtn (symbol-value var))
  7131. e opts)
  7132. (save-match-data
  7133. (if (or (not string) (not (string-match "\\S-" string)))
  7134. rtn
  7135. (setq opts (delq nil (mapcar (lambda (x)
  7136. (setq e (assoc x org-startup-options))
  7137. (if (eq (nth 1 e) var) e nil))
  7138. (org-split-string string "[ \t]+"))))
  7139. (if (not opts)
  7140. rtn
  7141. (setq rtn nil)
  7142. (while (setq e (pop opts))
  7143. (if (not (nth 3 e))
  7144. (setq rtn (nth 2 e))
  7145. (if (not (listp rtn)) (setq rtn nil))
  7146. (push (nth 2 e) rtn)))
  7147. rtn)))))
  7148. (defvar org-blocker-hook nil
  7149. "Hook for functions that are allowed to block a state change.
  7150. Each function gets as its single argument a property list, see
  7151. `org-trigger-hook' for more information about this list.
  7152. If any of the functions in this hook returns nil, the state change
  7153. is blocked.")
  7154. (defvar org-trigger-hook nil
  7155. "Hook for functions that are triggered by a state change.
  7156. Each function gets as its single argument a property list with at least
  7157. the following elements:
  7158. (:type type-of-change :position pos-at-entry-start
  7159. :from old-state :to new-state)
  7160. Depending on the type, more properties may be present.
  7161. This mechanism is currently implemented for:
  7162. TODO state changes
  7163. ------------------
  7164. :type todo-state-change
  7165. :from previous state (keyword as a string), or nil
  7166. :to new state (keyword as a string), or nil")
  7167. (defun org-todo (&optional arg)
  7168. "Change the TODO state of an item.
  7169. The state of an item is given by a keyword at the start of the heading,
  7170. like
  7171. *** TODO Write paper
  7172. *** DONE Call mom
  7173. The different keywords are specified in the variable `org-todo-keywords'.
  7174. By default the available states are \"TODO\" and \"DONE\".
  7175. So for this example: when the item starts with TODO, it is changed to DONE.
  7176. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7177. DONE are present, add TODO at the beginning of the heading.
  7178. With C-u prefix arg, use completion to determine the new state.
  7179. With numeric prefix arg, switch to that state.
  7180. For calling through lisp, arg is also interpreted in the following way:
  7181. 'none -> empty state
  7182. \"\"(empty string) -> switch to empty state
  7183. 'done -> switch to DONE
  7184. 'nextset -> switch to the next set of keywords
  7185. 'previousset -> switch to the previous set of keywords
  7186. \"WAITING\" -> switch to the specified keyword, but only if it
  7187. really is a member of `org-todo-keywords'."
  7188. (interactive "P")
  7189. (save-excursion
  7190. (catch 'exit
  7191. (org-back-to-heading)
  7192. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7193. (or (looking-at (concat " +" org-todo-regexp " *"))
  7194. (looking-at " *"))
  7195. (let* ((match-data (match-data))
  7196. (startpos (point-at-bol))
  7197. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7198. (org-log-done org-log-done)
  7199. (org-log-repeat org-log-repeat)
  7200. (org-todo-log-states org-todo-log-states)
  7201. (this (match-string 1))
  7202. (hl-pos (match-beginning 0))
  7203. (head (org-get-todo-sequence-head this))
  7204. (ass (assoc head org-todo-kwd-alist))
  7205. (interpret (nth 1 ass))
  7206. (done-word (nth 3 ass))
  7207. (final-done-word (nth 4 ass))
  7208. (last-state (or this ""))
  7209. (completion-ignore-case t)
  7210. (member (member this org-todo-keywords-1))
  7211. (tail (cdr member))
  7212. (state (cond
  7213. ((and org-todo-key-trigger
  7214. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7215. (and (not arg) org-use-fast-todo-selection
  7216. (not (eq org-use-fast-todo-selection 'prefix)))))
  7217. ;; Use fast selection
  7218. (org-fast-todo-selection))
  7219. ((and (equal arg '(4))
  7220. (or (not org-use-fast-todo-selection)
  7221. (not org-todo-key-trigger)))
  7222. ;; Read a state with completion
  7223. (completing-read "State: " (mapcar (lambda(x) (list x))
  7224. org-todo-keywords-1)
  7225. nil t))
  7226. ((eq arg 'right)
  7227. (if this
  7228. (if tail (car tail) nil)
  7229. (car org-todo-keywords-1)))
  7230. ((eq arg 'left)
  7231. (if (equal member org-todo-keywords-1)
  7232. nil
  7233. (if this
  7234. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7235. org-todo-keywords-1)
  7236. (org-last org-todo-keywords-1))))
  7237. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7238. (setq arg nil))) ; hack to fall back to cycling
  7239. (arg
  7240. ;; user or caller requests a specific state
  7241. (cond
  7242. ((equal arg "") nil)
  7243. ((eq arg 'none) nil)
  7244. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7245. ((eq arg 'nextset)
  7246. (or (car (cdr (member head org-todo-heads)))
  7247. (car org-todo-heads)))
  7248. ((eq arg 'previousset)
  7249. (let ((org-todo-heads (reverse org-todo-heads)))
  7250. (or (car (cdr (member head org-todo-heads)))
  7251. (car org-todo-heads))))
  7252. ((car (member arg org-todo-keywords-1)))
  7253. ((nth (1- (prefix-numeric-value arg))
  7254. org-todo-keywords-1))))
  7255. ((null member) (or head (car org-todo-keywords-1)))
  7256. ((equal this final-done-word) nil) ;; -> make empty
  7257. ((null tail) nil) ;; -> first entry
  7258. ((eq interpret 'sequence)
  7259. (car tail))
  7260. ((memq interpret '(type priority))
  7261. (if (eq this-command last-command)
  7262. (car tail)
  7263. (if (> (length tail) 0)
  7264. (or done-word (car org-done-keywords))
  7265. nil)))
  7266. (t nil)))
  7267. (next (if state (concat " " state " ") " "))
  7268. (change-plist (list :type 'todo-state-change :from this :to state
  7269. :position startpos))
  7270. dolog now-done-p)
  7271. (when org-blocker-hook
  7272. (unless (save-excursion
  7273. (save-match-data
  7274. (run-hook-with-args-until-failure
  7275. 'org-blocker-hook change-plist)))
  7276. (if (interactive-p)
  7277. (error "TODO state change from %s to %s blocked" this state)
  7278. ;; fail silently
  7279. (message "TODO state change from %s to %s blocked" this state)
  7280. (throw 'exit nil))))
  7281. (store-match-data match-data)
  7282. (replace-match next t t)
  7283. (unless (pos-visible-in-window-p hl-pos)
  7284. (message "TODO state changed to %s" (org-trim next)))
  7285. (unless head
  7286. (setq head (org-get-todo-sequence-head state)
  7287. ass (assoc head org-todo-kwd-alist)
  7288. interpret (nth 1 ass)
  7289. done-word (nth 3 ass)
  7290. final-done-word (nth 4 ass)))
  7291. (when (memq arg '(nextset previousset))
  7292. (message "Keyword-Set %d/%d: %s"
  7293. (- (length org-todo-sets) -1
  7294. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7295. (length org-todo-sets)
  7296. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7297. (setq org-last-todo-state-is-todo
  7298. (not (member state org-done-keywords)))
  7299. (setq now-done-p (and (member state org-done-keywords)
  7300. (not (member this org-done-keywords))))
  7301. (and logging (org-local-logging logging))
  7302. (when (and (or org-todo-log-states org-log-done)
  7303. (not (memq arg '(nextset previousset))))
  7304. ;; we need to look at recording a time and note
  7305. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7306. (nth 2 (assoc this org-todo-log-states))))
  7307. (when (and state
  7308. (member state org-not-done-keywords)
  7309. (not (member this org-not-done-keywords)))
  7310. ;; This is now a todo state and was not one before
  7311. ;; If there was a CLOSED time stamp, get rid of it.
  7312. (org-add-planning-info nil nil 'closed))
  7313. (when (and now-done-p org-log-done)
  7314. ;; It is now done, and it was not done before
  7315. (org-add-planning-info 'closed (org-current-time))
  7316. (if (and (not dolog) (eq 'note org-log-done))
  7317. (org-add-log-setup 'done state 'findpos 'note)))
  7318. (when (and state dolog)
  7319. ;; This is a non-nil state, and we need to log it
  7320. (org-add-log-setup 'state state 'findpos dolog)))
  7321. ;; Fixup tag positioning
  7322. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7323. (run-hooks 'org-after-todo-state-change-hook)
  7324. (if (and arg (not (member state org-done-keywords)))
  7325. (setq head (org-get-todo-sequence-head state)))
  7326. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7327. ;; Do we need to trigger a repeat?
  7328. (when now-done-p (org-auto-repeat-maybe state))
  7329. ;; Fixup cursor location if close to the keyword
  7330. (if (and (outline-on-heading-p)
  7331. (not (bolp))
  7332. (save-excursion (beginning-of-line 1)
  7333. (looking-at org-todo-line-regexp))
  7334. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7335. (progn
  7336. (goto-char (or (match-end 2) (match-end 1)))
  7337. (just-one-space)))
  7338. (when org-trigger-hook
  7339. (save-excursion
  7340. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7341. (defun org-local-logging (value)
  7342. "Get logging settings from a property VALUE."
  7343. (let* (words w a)
  7344. ;; directly set the variables, they are already local.
  7345. (setq org-log-done nil
  7346. org-log-repeat nil
  7347. org-todo-log-states nil)
  7348. (setq words (org-split-string value))
  7349. (while (setq w (pop words))
  7350. (cond
  7351. ((setq a (assoc w org-startup-options))
  7352. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7353. (set (nth 1 a) (nth 2 a))))
  7354. ((setq a (org-extract-log-state-settings w))
  7355. (and (member (car a) org-todo-keywords-1)
  7356. (push a org-todo-log-states)))))))
  7357. (defun org-get-todo-sequence-head (kwd)
  7358. "Return the head of the TODO sequence to which KWD belongs.
  7359. If KWD is not set, check if there is a text property remembering the
  7360. right sequence."
  7361. (let (p)
  7362. (cond
  7363. ((not kwd)
  7364. (or (get-text-property (point-at-bol) 'org-todo-head)
  7365. (progn
  7366. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7367. nil (point-at-eol)))
  7368. (get-text-property p 'org-todo-head))))
  7369. ((not (member kwd org-todo-keywords-1))
  7370. (car org-todo-keywords-1))
  7371. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7372. (defun org-fast-todo-selection ()
  7373. "Fast TODO keyword selection with single keys.
  7374. Returns the new TODO keyword, or nil if no state change should occur."
  7375. (let* ((fulltable org-todo-key-alist)
  7376. (done-keywords org-done-keywords) ;; needed for the faces.
  7377. (maxlen (apply 'max (mapcar
  7378. (lambda (x)
  7379. (if (stringp (car x)) (string-width (car x)) 0))
  7380. fulltable)))
  7381. (expert nil)
  7382. (fwidth (+ maxlen 3 1 3))
  7383. (ncol (/ (- (window-width) 4) fwidth))
  7384. tg cnt e c tbl
  7385. groups ingroup)
  7386. (save-window-excursion
  7387. (if expert
  7388. (set-buffer (get-buffer-create " *Org todo*"))
  7389. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7390. (erase-buffer)
  7391. (org-set-local 'org-done-keywords done-keywords)
  7392. (setq tbl fulltable cnt 0)
  7393. (while (setq e (pop tbl))
  7394. (cond
  7395. ((equal e '(:startgroup))
  7396. (push '() groups) (setq ingroup t)
  7397. (when (not (= cnt 0))
  7398. (setq cnt 0)
  7399. (insert "\n"))
  7400. (insert "{ "))
  7401. ((equal e '(:endgroup))
  7402. (setq ingroup nil cnt 0)
  7403. (insert "}\n"))
  7404. (t
  7405. (setq tg (car e) c (cdr e))
  7406. (if ingroup (push tg (car groups)))
  7407. (setq tg (org-add-props tg nil 'face
  7408. (org-get-todo-face tg)))
  7409. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7410. (insert "[" c "] " tg (make-string
  7411. (- fwidth 4 (length tg)) ?\ ))
  7412. (when (= (setq cnt (1+ cnt)) ncol)
  7413. (insert "\n")
  7414. (if ingroup (insert " "))
  7415. (setq cnt 0)))))
  7416. (insert "\n")
  7417. (goto-char (point-min))
  7418. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7419. (fit-window-to-buffer))
  7420. (message "[a-z..]:Set [SPC]:clear")
  7421. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7422. (cond
  7423. ((or (= c ?\C-g)
  7424. (and (= c ?q) (not (rassoc c fulltable))))
  7425. (setq quit-flag t))
  7426. ((= c ?\ ) nil)
  7427. ((setq e (rassoc c fulltable) tg (car e))
  7428. tg)
  7429. (t (setq quit-flag t))))))
  7430. (defun org-entry-is-todo-p ()
  7431. (member (org-get-todo-state) org-not-done-keywords))
  7432. (defun org-entry-is-done-p ()
  7433. (member (org-get-todo-state) org-done-keywords))
  7434. (defun org-get-todo-state ()
  7435. (save-excursion
  7436. (org-back-to-heading t)
  7437. (and (looking-at org-todo-line-regexp)
  7438. (match-end 2)
  7439. (match-string 2))))
  7440. (defun org-at-date-range-p (&optional inactive-ok)
  7441. "Is the cursor inside a date range?"
  7442. (interactive)
  7443. (save-excursion
  7444. (catch 'exit
  7445. (let ((pos (point)))
  7446. (skip-chars-backward "^[<\r\n")
  7447. (skip-chars-backward "<[")
  7448. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7449. (>= (match-end 0) pos)
  7450. (throw 'exit t))
  7451. (skip-chars-backward "^<[\r\n")
  7452. (skip-chars-backward "<[")
  7453. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7454. (>= (match-end 0) pos)
  7455. (throw 'exit t)))
  7456. nil)))
  7457. (defun org-get-repeat ()
  7458. "Check if tere is a deadline/schedule with repeater in this entry."
  7459. (save-match-data
  7460. (save-excursion
  7461. (org-back-to-heading t)
  7462. (if (re-search-forward
  7463. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7464. (match-string 1)))))
  7465. (defvar org-last-changed-timestamp)
  7466. (defvar org-log-post-message)
  7467. (defvar org-log-note-purpose)
  7468. (defvar org-log-note-how)
  7469. (defun org-auto-repeat-maybe (done-word)
  7470. "Check if the current headline contains a repeated deadline/schedule.
  7471. If yes, set TODO state back to what it was and change the base date
  7472. of repeating deadline/scheduled time stamps to new date.
  7473. This function is run automatically after each state change to a DONE state."
  7474. ;; last-state is dynamically scoped into this function
  7475. (let* ((repeat (org-get-repeat))
  7476. (aa (assoc last-state org-todo-kwd-alist))
  7477. (interpret (nth 1 aa))
  7478. (head (nth 2 aa))
  7479. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7480. (msg "Entry repeats: ")
  7481. (org-log-done nil)
  7482. (org-todo-log-states nil)
  7483. (nshiftmax 10) (nshift 0)
  7484. re type n what ts mb0 time)
  7485. (when repeat
  7486. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7487. (org-todo (if (eq interpret 'type) last-state head))
  7488. (when org-log-repeat
  7489. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7490. (memq 'org-add-log-note post-command-hook))
  7491. ;; OK, we are already setup for some record
  7492. (if (eq org-log-repeat 'note)
  7493. ;; make sure we take a note, not only a time stamp
  7494. (setq org-log-note-how 'note))
  7495. ;; Set up for taking a record
  7496. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7497. 'findpos org-log-repeat)))
  7498. (org-back-to-heading t)
  7499. (org-add-planning-info nil nil 'closed)
  7500. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7501. org-deadline-time-regexp "\\)\\|\\("
  7502. org-ts-regexp "\\)"))
  7503. (while (re-search-forward
  7504. re (save-excursion (outline-next-heading) (point)) t)
  7505. (setq type (if (match-end 1) org-scheduled-string
  7506. (if (match-end 3) org-deadline-string "Plain:"))
  7507. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7508. mb0 (match-beginning 0))
  7509. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7510. (setq n (string-to-number (match-string 2 ts))
  7511. what (match-string 3 ts))
  7512. (if (equal what "w") (setq n (* n 7) what "d"))
  7513. ;; Preparation, see if we need to modify the start date for the change
  7514. (when (match-end 1)
  7515. (setq time (save-match-data (org-time-string-to-time ts)))
  7516. (cond
  7517. ((equal (match-string 1 ts) ".")
  7518. ;; Shift starting date to today
  7519. (org-timestamp-change
  7520. (- (time-to-days (current-time)) (time-to-days time))
  7521. 'day))
  7522. ((equal (match-string 1 ts) "+")
  7523. (while (or (= nshift 0)
  7524. (<= (time-to-days time) (time-to-days (current-time))))
  7525. (when (= (incf nshift) nshiftmax)
  7526. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7527. (error "Abort")))
  7528. (org-timestamp-change n (cdr (assoc what whata)))
  7529. (org-at-timestamp-p t)
  7530. (setq ts (match-string 1))
  7531. (setq time (save-match-data (org-time-string-to-time ts))))
  7532. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7533. ;; rematch, so that we have everything in place for the real shift
  7534. (org-at-timestamp-p t)
  7535. (setq ts (match-string 1))
  7536. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7537. (org-timestamp-change n (cdr (assoc what whata)))
  7538. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7539. (setq org-log-post-message msg)
  7540. (message "%s" msg))))
  7541. (defun org-show-todo-tree (arg)
  7542. "Make a compact tree which shows all headlines marked with TODO.
  7543. The tree will show the lines where the regexp matches, and all higher
  7544. headlines above the match.
  7545. With a \\[universal-argument] prefix, also show the DONE entries.
  7546. With a numeric prefix N, construct a sparse tree for the Nth element
  7547. of `org-todo-keywords-1'."
  7548. (interactive "P")
  7549. (let ((case-fold-search nil)
  7550. (kwd-re
  7551. (cond ((null arg) org-not-done-regexp)
  7552. ((equal arg '(4))
  7553. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7554. (mapcar 'list org-todo-keywords-1))))
  7555. (concat "\\("
  7556. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7557. "\\)\\>")))
  7558. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7559. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7560. org-todo-keywords-1)))
  7561. (t (error "Invalid prefix argument: %s" arg)))))
  7562. (message "%d TODO entries found"
  7563. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7564. (defun org-deadline (&optional remove)
  7565. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7566. With argument REMOVE, remove any deadline from the item."
  7567. (interactive "P")
  7568. (if remove
  7569. (progn
  7570. (org-remove-timestamp-with-keyword org-deadline-string)
  7571. (message "Item no longer has a deadline."))
  7572. (org-add-planning-info 'deadline nil 'closed)))
  7573. (defun org-schedule (&optional remove)
  7574. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7575. With argument REMOVE, remove any scheduling date from the item."
  7576. (interactive "P")
  7577. (if remove
  7578. (progn
  7579. (org-remove-timestamp-with-keyword org-scheduled-string)
  7580. (message "Item is no longer scheduled."))
  7581. (org-add-planning-info 'scheduled nil 'closed)))
  7582. (defun org-remove-timestamp-with-keyword (keyword)
  7583. "Remove all time stamps with KEYWORD in the current entry."
  7584. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7585. beg)
  7586. (save-excursion
  7587. (org-back-to-heading t)
  7588. (setq beg (point))
  7589. (org-end-of-subtree t t)
  7590. (while (re-search-backward re beg t)
  7591. (replace-match "")
  7592. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7593. (equal (char-before) ?\ ))
  7594. (backward-delete-char 1)
  7595. (if (string-match "^[ \t]*$" (buffer-substring
  7596. (point-at-bol) (point-at-eol)))
  7597. (delete-region (point-at-bol)
  7598. (min (point-max) (1+ (point-at-eol))))))))))
  7599. (defun org-add-planning-info (what &optional time &rest remove)
  7600. "Insert new timestamp with keyword in the line directly after the headline.
  7601. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7602. If non is given, the user is prompted for a date.
  7603. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7604. be removed."
  7605. (interactive)
  7606. (let (org-time-was-given org-end-time-was-given ts
  7607. end default-time default-input)
  7608. (when (and (not time) (memq what '(scheduled deadline)))
  7609. ;; Try to get a default date/time from existing timestamp
  7610. (save-excursion
  7611. (org-back-to-heading t)
  7612. (setq end (save-excursion (outline-next-heading) (point)))
  7613. (when (re-search-forward (if (eq what 'scheduled)
  7614. org-scheduled-time-regexp
  7615. org-deadline-time-regexp)
  7616. end t)
  7617. (setq ts (match-string 1)
  7618. default-time
  7619. (apply 'encode-time (org-parse-time-string ts))
  7620. default-input (and ts (org-get-compact-tod ts))))))
  7621. (when what
  7622. ;; If necessary, get the time from the user
  7623. (setq time (or time (org-read-date nil 'to-time nil nil
  7624. default-time default-input))))
  7625. (when (and org-insert-labeled-timestamps-at-point
  7626. (member what '(scheduled deadline)))
  7627. (insert
  7628. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7629. (org-insert-time-stamp time org-time-was-given
  7630. nil nil nil (list org-end-time-was-given))
  7631. (setq what nil))
  7632. (save-excursion
  7633. (save-restriction
  7634. (let (col list elt ts buffer-invisibility-spec)
  7635. (org-back-to-heading t)
  7636. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7637. (goto-char (match-end 1))
  7638. (setq col (current-column))
  7639. (goto-char (match-end 0))
  7640. (if (eobp) (insert "\n") (forward-char 1))
  7641. (if (and (not (looking-at outline-regexp))
  7642. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7643. "[^\r\n]*"))
  7644. (not (equal (match-string 1) org-clock-string)))
  7645. (narrow-to-region (match-beginning 0) (match-end 0))
  7646. (insert-before-markers "\n")
  7647. (backward-char 1)
  7648. (narrow-to-region (point) (point))
  7649. (org-indent-to-column col))
  7650. ;; Check if we have to remove something.
  7651. (setq list (cons what remove))
  7652. (while list
  7653. (setq elt (pop list))
  7654. (goto-char (point-min))
  7655. (when (or (and (eq elt 'scheduled)
  7656. (re-search-forward org-scheduled-time-regexp nil t))
  7657. (and (eq elt 'deadline)
  7658. (re-search-forward org-deadline-time-regexp nil t))
  7659. (and (eq elt 'closed)
  7660. (re-search-forward org-closed-time-regexp nil t)))
  7661. (replace-match "")
  7662. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7663. (if (looking-at " +") (replace-match ""))))
  7664. (goto-char (point-max))
  7665. (when what
  7666. (insert
  7667. (if (not (equal (char-before) ?\ )) " " "")
  7668. (cond ((eq what 'scheduled) org-scheduled-string)
  7669. ((eq what 'deadline) org-deadline-string)
  7670. ((eq what 'closed) org-closed-string))
  7671. " ")
  7672. (setq ts (org-insert-time-stamp
  7673. time
  7674. (or org-time-was-given
  7675. (and (eq what 'closed) org-log-done-with-time))
  7676. (eq what 'closed)
  7677. nil nil (list org-end-time-was-given)))
  7678. (end-of-line 1))
  7679. (goto-char (point-min))
  7680. (widen)
  7681. (if (and (looking-at "[ \t]+\n")
  7682. (equal (char-before) ?\n))
  7683. (delete-region (1- (point)) (point-at-eol)))
  7684. ts)))))
  7685. (defvar org-log-note-marker (make-marker))
  7686. (defvar org-log-note-purpose nil)
  7687. (defvar org-log-note-state nil)
  7688. (defvar org-log-note-how nil)
  7689. (defvar org-log-note-window-configuration nil)
  7690. (defvar org-log-note-return-to (make-marker))
  7691. (defvar org-log-post-message nil
  7692. "Message to be displayed after a log note has been stored.
  7693. The auto-repeater uses this.")
  7694. (defun org-add-note ()
  7695. "Add a note to the current entry.
  7696. This is done in the same way as adding a state change note."
  7697. (interactive)
  7698. (org-add-log-setup 'note nil t nil))
  7699. (defun org-add-log-setup (&optional purpose state findpos how)
  7700. "Set up the post command hook to take a note.
  7701. If this is about to TODO state change, the new state is expected in STATE.
  7702. When FINDPOS is non-nil, find the correct position for the note in
  7703. the current entry. If not, assume that it can be inserted at point."
  7704. (save-excursion
  7705. (when findpos
  7706. (org-back-to-heading t)
  7707. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7708. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7709. "[^\r\n]*\\)?"))
  7710. (goto-char (match-end 0))
  7711. (unless org-log-states-order-reversed
  7712. (and (= (char-after) ?\n) (forward-char 1))
  7713. (org-skip-over-state-notes)
  7714. (skip-chars-backward " \t\n\r")))
  7715. (move-marker org-log-note-marker (point))
  7716. (setq org-log-note-purpose purpose
  7717. org-log-note-state state
  7718. org-log-note-how how)
  7719. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7720. (defun org-skip-over-state-notes ()
  7721. "Skip past the list of State notes in an entry."
  7722. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7723. (while (looking-at "[ \t]*- State")
  7724. (condition-case nil
  7725. (org-next-item)
  7726. (error (org-end-of-item)))))
  7727. (defun org-add-log-note (&optional purpose)
  7728. "Pop up a window for taking a note, and add this note later at point."
  7729. (remove-hook 'post-command-hook 'org-add-log-note)
  7730. (setq org-log-note-window-configuration (current-window-configuration))
  7731. (delete-other-windows)
  7732. (move-marker org-log-note-return-to (point))
  7733. (switch-to-buffer (marker-buffer org-log-note-marker))
  7734. (goto-char org-log-note-marker)
  7735. (org-switch-to-buffer-other-window "*Org Note*")
  7736. (erase-buffer)
  7737. (if (memq org-log-note-how '(time state))
  7738. (org-store-log-note)
  7739. (let ((org-inhibit-startup t)) (org-mode))
  7740. (insert (format "# Insert note for %s.
  7741. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7742. (cond
  7743. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7744. ((eq org-log-note-purpose 'done) "closed todo item")
  7745. ((eq org-log-note-purpose 'state)
  7746. (format "state change to \"%s\"" org-log-note-state))
  7747. ((eq org-log-note-purpose 'note)
  7748. "this entry")
  7749. (t (error "This should not happen")))))
  7750. (org-set-local 'org-finish-function 'org-store-log-note)))
  7751. (defvar org-note-abort nil) ; dynamically scoped
  7752. (defun org-store-log-note ()
  7753. "Finish taking a log note, and insert it to where it belongs."
  7754. (let ((txt (buffer-string))
  7755. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7756. lines ind)
  7757. (kill-buffer (current-buffer))
  7758. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7759. (setq txt (replace-match "" t t txt)))
  7760. (if (string-match "\\s-+\\'" txt)
  7761. (setq txt (replace-match "" t t txt)))
  7762. (setq lines (org-split-string txt "\n"))
  7763. (when (and note (string-match "\\S-" note))
  7764. (setq note
  7765. (org-replace-escapes
  7766. note
  7767. (list (cons "%u" (user-login-name))
  7768. (cons "%U" user-full-name)
  7769. (cons "%t" (format-time-string
  7770. (org-time-stamp-format 'long 'inactive)
  7771. (current-time)))
  7772. (cons "%s" (if org-log-note-state
  7773. (concat "\"" org-log-note-state "\"")
  7774. "")))))
  7775. (if lines (setq note (concat note " \\\\")))
  7776. (push note lines))
  7777. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7778. (when lines
  7779. (save-excursion
  7780. (set-buffer (marker-buffer org-log-note-marker))
  7781. (save-excursion
  7782. (goto-char org-log-note-marker)
  7783. (move-marker org-log-note-marker nil)
  7784. (end-of-line 1)
  7785. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7786. (indent-relative nil)
  7787. (insert "- " (pop lines))
  7788. (org-indent-line-function)
  7789. (beginning-of-line 1)
  7790. (looking-at "[ \t]*")
  7791. (setq ind (concat (match-string 0) " "))
  7792. (end-of-line 1)
  7793. (while lines (insert "\n" ind (pop lines)))))))
  7794. (set-window-configuration org-log-note-window-configuration)
  7795. (with-current-buffer (marker-buffer org-log-note-return-to)
  7796. (goto-char org-log-note-return-to))
  7797. (move-marker org-log-note-return-to nil)
  7798. (and org-log-post-message (message "%s" org-log-post-message)))
  7799. (defun org-sparse-tree (&optional arg)
  7800. "Create a sparse tree, prompt for the details.
  7801. This command can create sparse trees. You first need to select the type
  7802. of match used to create the tree:
  7803. t Show entries with a specific TODO keyword.
  7804. T Show entries selected by a tags match.
  7805. p Enter a property name and its value (both with completion on existing
  7806. names/values) and show entries with that property.
  7807. r Show entries matching a regular expression
  7808. d Show deadlines due within `org-deadline-warning-days'."
  7809. (interactive "P")
  7810. (let (ans kwd value)
  7811. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7812. (setq ans (read-char-exclusive))
  7813. (cond
  7814. ((equal ans ?d)
  7815. (call-interactively 'org-check-deadlines))
  7816. ((equal ans ?b)
  7817. (call-interactively 'org-check-before-date))
  7818. ((equal ans ?t)
  7819. (org-show-todo-tree '(4)))
  7820. ((equal ans ?T)
  7821. (call-interactively 'org-tags-sparse-tree))
  7822. ((member ans '(?p ?P))
  7823. (setq kwd (completing-read "Property: "
  7824. (mapcar 'list (org-buffer-property-keys))))
  7825. (setq value (completing-read "Value: "
  7826. (mapcar 'list (org-property-values kwd))))
  7827. (unless (string-match "\\`{.*}\\'" value)
  7828. (setq value (concat "\"" value "\"")))
  7829. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7830. ((member ans '(?r ?R ?/))
  7831. (call-interactively 'org-occur))
  7832. (t (error "No such sparse tree command \"%c\"" ans)))))
  7833. (defvar org-occur-highlights nil
  7834. "List of overlays used for occur matches.")
  7835. (make-variable-buffer-local 'org-occur-highlights)
  7836. (defvar org-occur-parameters nil
  7837. "Parameters of the active org-occur calls.
  7838. This is a list, each call to org-occur pushes as cons cell,
  7839. containing the regular expression and the callback, onto the list.
  7840. The list can contain several entries if `org-occur' has been called
  7841. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7842. will only contain one set of parameters. When the highlights are
  7843. removed (for example with `C-c C-c', or with the next edit (depending
  7844. on `org-remove-highlights-with-change'), this variable is emptied
  7845. as well.")
  7846. (make-variable-buffer-local 'org-occur-parameters)
  7847. (defun org-occur (regexp &optional keep-previous callback)
  7848. "Make a compact tree which shows all matches of REGEXP.
  7849. The tree will show the lines where the regexp matches, and all higher
  7850. headlines above the match. It will also show the heading after the match,
  7851. to make sure editing the matching entry is easy.
  7852. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7853. call to `org-occur' will be kept, to allow stacking of calls to this
  7854. command.
  7855. If CALLBACK is non-nil, it is a function which is called to confirm
  7856. that the match should indeed be shown."
  7857. (interactive "sRegexp: \nP")
  7858. (unless keep-previous
  7859. (org-remove-occur-highlights nil nil t))
  7860. (push (cons regexp callback) org-occur-parameters)
  7861. (let ((cnt 0))
  7862. (save-excursion
  7863. (goto-char (point-min))
  7864. (if (or (not keep-previous) ; do not want to keep
  7865. (not org-occur-highlights)) ; no previous matches
  7866. ;; hide everything
  7867. (org-overview))
  7868. (while (re-search-forward regexp nil t)
  7869. (when (or (not callback)
  7870. (save-match-data (funcall callback)))
  7871. (setq cnt (1+ cnt))
  7872. (when org-highlight-sparse-tree-matches
  7873. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7874. (org-show-context 'occur-tree))))
  7875. (when org-remove-highlights-with-change
  7876. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7877. nil 'local))
  7878. (unless org-sparse-tree-open-archived-trees
  7879. (org-hide-archived-subtrees (point-min) (point-max)))
  7880. (run-hooks 'org-occur-hook)
  7881. (if (interactive-p)
  7882. (message "%d match(es) for regexp %s" cnt regexp))
  7883. cnt))
  7884. (defun org-show-context (&optional key)
  7885. "Make sure point and context and visible.
  7886. How much context is shown depends upon the variables
  7887. `org-show-hierarchy-above', `org-show-following-heading'. and
  7888. `org-show-siblings'."
  7889. (let ((heading-p (org-on-heading-p t))
  7890. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7891. (following-p (org-get-alist-option org-show-following-heading key))
  7892. (entry-p (org-get-alist-option org-show-entry-below key))
  7893. (siblings-p (org-get-alist-option org-show-siblings key)))
  7894. (catch 'exit
  7895. ;; Show heading or entry text
  7896. (if (and heading-p (not entry-p))
  7897. (org-flag-heading nil) ; only show the heading
  7898. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7899. (org-show-hidden-entry))) ; show entire entry
  7900. (when following-p
  7901. ;; Show next sibling, or heading below text
  7902. (save-excursion
  7903. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7904. (org-flag-heading nil))))
  7905. (when siblings-p (org-show-siblings))
  7906. (when hierarchy-p
  7907. ;; show all higher headings, possibly with siblings
  7908. (save-excursion
  7909. (while (and (condition-case nil
  7910. (progn (org-up-heading-all 1) t)
  7911. (error nil))
  7912. (not (bobp)))
  7913. (org-flag-heading nil)
  7914. (when siblings-p (org-show-siblings))))))))
  7915. (defun org-reveal (&optional siblings)
  7916. "Show current entry, hierarchy above it, and the following headline.
  7917. This can be used to show a consistent set of context around locations
  7918. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7919. not t for the search context.
  7920. With optional argument SIBLINGS, on each level of the hierarchy all
  7921. siblings are shown. This repairs the tree structure to what it would
  7922. look like when opened with hierarchical calls to `org-cycle'."
  7923. (interactive "P")
  7924. (let ((org-show-hierarchy-above t)
  7925. (org-show-following-heading t)
  7926. (org-show-siblings (if siblings t org-show-siblings)))
  7927. (org-show-context nil)))
  7928. (defun org-highlight-new-match (beg end)
  7929. "Highlight from BEG to END and mark the highlight is an occur headline."
  7930. (let ((ov (org-make-overlay beg end)))
  7931. (org-overlay-put ov 'face 'secondary-selection)
  7932. (push ov org-occur-highlights)))
  7933. (defun org-remove-occur-highlights (&optional beg end noremove)
  7934. "Remove the occur highlights from the buffer.
  7935. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7936. from the `before-change-functions' in the current buffer."
  7937. (interactive)
  7938. (unless org-inhibit-highlight-removal
  7939. (mapc 'org-delete-overlay org-occur-highlights)
  7940. (setq org-occur-highlights nil)
  7941. (setq org-occur-parameters nil)
  7942. (unless noremove
  7943. (remove-hook 'before-change-functions
  7944. 'org-remove-occur-highlights 'local))))
  7945. ;;;; Priorities
  7946. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7947. "Regular expression matching the priority indicator.")
  7948. (defvar org-remove-priority-next-time nil)
  7949. (defun org-priority-up ()
  7950. "Increase the priority of the current item."
  7951. (interactive)
  7952. (org-priority 'up))
  7953. (defun org-priority-down ()
  7954. "Decrease the priority of the current item."
  7955. (interactive)
  7956. (org-priority 'down))
  7957. (defun org-priority (&optional action)
  7958. "Change the priority of an item by ARG.
  7959. ACTION can be `set', `up', `down', or a character."
  7960. (interactive)
  7961. (setq action (or action 'set))
  7962. (let (current new news have remove)
  7963. (save-excursion
  7964. (org-back-to-heading)
  7965. (if (looking-at org-priority-regexp)
  7966. (setq current (string-to-char (match-string 2))
  7967. have t)
  7968. (setq current org-default-priority))
  7969. (cond
  7970. ((or (eq action 'set)
  7971. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7972. (if (not (eq action 'set))
  7973. (setq new action)
  7974. (message "Priority %c-%c, SPC to remove: "
  7975. org-highest-priority org-lowest-priority)
  7976. (setq new (read-char-exclusive)))
  7977. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7978. (= (upcase org-lowest-priority) org-lowest-priority))
  7979. (setq new (upcase new)))
  7980. (cond ((equal new ?\ ) (setq remove t))
  7981. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7982. (error "Priority must be between `%c' and `%c'"
  7983. org-highest-priority org-lowest-priority))))
  7984. ((eq action 'up)
  7985. (if (and (not have) (eq last-command this-command))
  7986. (setq new org-lowest-priority)
  7987. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7988. org-default-priority (1- current)))))
  7989. ((eq action 'down)
  7990. (if (and (not have) (eq last-command this-command))
  7991. (setq new org-highest-priority)
  7992. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7993. org-default-priority (1+ current)))))
  7994. (t (error "Invalid action")))
  7995. (if (or (< (upcase new) org-highest-priority)
  7996. (> (upcase new) org-lowest-priority))
  7997. (setq remove t))
  7998. (setq news (format "%c" new))
  7999. (if have
  8000. (if remove
  8001. (replace-match "" t t nil 1)
  8002. (replace-match news t t nil 2))
  8003. (if remove
  8004. (error "No priority cookie found in line")
  8005. (looking-at org-todo-line-regexp)
  8006. (if (match-end 2)
  8007. (progn
  8008. (goto-char (match-end 2))
  8009. (insert " [#" news "]"))
  8010. (goto-char (match-beginning 3))
  8011. (insert "[#" news "] ")))))
  8012. (org-preserve-lc (org-set-tags nil 'align))
  8013. (if remove
  8014. (message "Priority removed")
  8015. (message "Priority of current item set to %s" news))))
  8016. (defun org-get-priority (s)
  8017. "Find priority cookie and return priority."
  8018. (save-match-data
  8019. (if (not (string-match org-priority-regexp s))
  8020. (* 1000 (- org-lowest-priority org-default-priority))
  8021. (* 1000 (- org-lowest-priority
  8022. (string-to-char (match-string 2 s)))))))
  8023. ;;;; Tags
  8024. (defun org-scan-tags (action matcher &optional todo-only)
  8025. "Scan headline tags with inheritance and produce output ACTION.
  8026. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  8027. evaluated, testing if a given set of tags qualifies a headline for
  8028. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  8029. are included in the output."
  8030. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8031. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8032. (org-re
  8033. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8034. (props (list 'face nil
  8035. 'done-face 'org-done
  8036. 'undone-face nil
  8037. 'mouse-face 'highlight
  8038. 'org-not-done-regexp org-not-done-regexp
  8039. 'org-todo-regexp org-todo-regexp
  8040. 'keymap org-agenda-keymap
  8041. 'help-echo
  8042. (format "mouse-2 or RET jump to org file %s"
  8043. (abbreviate-file-name
  8044. (or (buffer-file-name (buffer-base-buffer))
  8045. (buffer-name (buffer-base-buffer)))))))
  8046. (case-fold-search nil)
  8047. lspos
  8048. tags tags-list tags-alist (llast 0) rtn level category i txt
  8049. todo marker entry priority)
  8050. (save-excursion
  8051. (goto-char (point-min))
  8052. (when (eq action 'sparse-tree)
  8053. (org-overview)
  8054. (org-remove-occur-highlights))
  8055. (while (re-search-forward re nil t)
  8056. (catch :skip
  8057. (setq todo (if (match-end 1) (match-string 2))
  8058. tags (if (match-end 4) (match-string 4)))
  8059. (goto-char (setq lspos (1+ (match-beginning 0))))
  8060. (setq level (org-reduced-level (funcall outline-level))
  8061. category (org-get-category))
  8062. (setq i llast llast level)
  8063. ;; remove tag lists from same and sublevels
  8064. (while (>= i level)
  8065. (when (setq entry (assoc i tags-alist))
  8066. (setq tags-alist (delete entry tags-alist)))
  8067. (setq i (1- i)))
  8068. ;; add the next tags
  8069. (when tags
  8070. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8071. tags-alist
  8072. (cons (cons level tags) tags-alist)))
  8073. ;; compile tags for current headline
  8074. (setq tags-list
  8075. (if org-use-tag-inheritance
  8076. (apply 'append (mapcar 'cdr tags-alist))
  8077. tags))
  8078. (when (and tags org-use-tag-inheritance
  8079. (not (eq t org-use-tag-inheritance)))
  8080. ;; selective inheritance, remove uninherited ones
  8081. (setcdr (car tags-alist)
  8082. (org-remove-uniherited-tags (cdar tags-alist))))
  8083. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8084. (eval matcher)
  8085. (or (not org-agenda-skip-archived-trees)
  8086. (not (member org-archive-tag tags-list))))
  8087. (and (eq action 'agenda) (org-agenda-skip))
  8088. ;; list this headline
  8089. (if (eq action 'sparse-tree)
  8090. (progn
  8091. (and org-highlight-sparse-tree-matches
  8092. (org-get-heading) (match-end 0)
  8093. (org-highlight-new-match
  8094. (match-beginning 0) (match-beginning 1)))
  8095. (org-show-context 'tags-tree))
  8096. (setq txt (org-format-agenda-item
  8097. ""
  8098. (concat
  8099. (if org-tags-match-list-sublevels
  8100. (make-string (1- level) ?.) "")
  8101. (org-get-heading))
  8102. category tags-list)
  8103. priority (org-get-priority txt))
  8104. (goto-char lspos)
  8105. (setq marker (org-agenda-new-marker))
  8106. (org-add-props txt props
  8107. 'org-marker marker 'org-hd-marker marker 'org-category category
  8108. 'priority priority 'type "tagsmatch")
  8109. (push txt rtn))
  8110. ;; if we are to skip sublevels, jump to end of subtree
  8111. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8112. (when (and (eq action 'sparse-tree)
  8113. (not org-sparse-tree-open-archived-trees))
  8114. (org-hide-archived-subtrees (point-min) (point-max)))
  8115. (nreverse rtn)))
  8116. (defun org-remove-uniherited-tags (tags)
  8117. "Remove all tags that are not inherited from the list TAGS."
  8118. (cond
  8119. ((eq org-use-tag-inheritance t) tags)
  8120. ((not org-use-tag-inheritance) nil)
  8121. ((stringp org-use-tag-inheritance)
  8122. (delq nil (mapcar
  8123. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8124. tags)))
  8125. ((listp org-use-tag-inheritance)
  8126. (org-delete-all org-use-tag-inheritance tags))))
  8127. (defvar todo-only) ;; dynamically scoped
  8128. (defun org-tags-sparse-tree (&optional todo-only match)
  8129. "Create a sparse tree according to tags string MATCH.
  8130. MATCH can contain positive and negative selection of tags, like
  8131. \"+WORK+URGENT-WITHBOSS\".
  8132. If optional argument TODO_ONLY is non-nil, only select lines that are
  8133. also TODO lines."
  8134. (interactive "P")
  8135. (org-prepare-agenda-buffers (list (current-buffer)))
  8136. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8137. (defvar org-cached-props nil)
  8138. (defun org-cached-entry-get (pom property)
  8139. (if (or (eq t org-use-property-inheritance)
  8140. (and (stringp org-use-property-inheritance)
  8141. (string-match org-use-property-inheritance property))
  8142. (and (listp org-use-property-inheritance)
  8143. (member property org-use-property-inheritance)))
  8144. ;; Caching is not possible, check it directly
  8145. (org-entry-get pom property 'inherit)
  8146. ;; Get all properties, so that we can do complicated checks easily
  8147. (cdr (assoc property (or org-cached-props
  8148. (setq org-cached-props
  8149. (org-entry-properties pom)))))))
  8150. (defun org-global-tags-completion-table (&optional files)
  8151. "Return the list of all tags in all agenda buffer/files."
  8152. (save-excursion
  8153. (org-uniquify
  8154. (delq nil
  8155. (apply 'append
  8156. (mapcar
  8157. (lambda (file)
  8158. (set-buffer (find-file-noselect file))
  8159. (append (org-get-buffer-tags)
  8160. (mapcar (lambda (x) (if (stringp (car-safe x))
  8161. (list (car-safe x)) nil))
  8162. org-tag-alist)))
  8163. (if (and files (car files))
  8164. files
  8165. (org-agenda-files))))))))
  8166. (defun org-make-tags-matcher (match)
  8167. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8168. ;; todo-only is scoped dynamically into this function, and the function
  8169. ;; may change it it the matcher asksk for it.
  8170. (unless match
  8171. ;; Get a new match request, with completion
  8172. (let ((org-last-tags-completion-table
  8173. (org-global-tags-completion-table)))
  8174. (setq match (completing-read
  8175. "Match: " 'org-tags-completion-function nil nil nil
  8176. 'org-tags-history))))
  8177. ;; Parse the string and create a lisp form
  8178. (let ((match0 match)
  8179. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8180. minus tag mm
  8181. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8182. orterms term orlist re-p str-p level-p level-op
  8183. prop-p pn pv po cat-p gv)
  8184. (if (string-match "/+" match)
  8185. ;; match contains also a todo-matching request
  8186. (progn
  8187. (setq tagsmatch (substring match 0 (match-beginning 0))
  8188. todomatch (substring match (match-end 0)))
  8189. (if (string-match "^!" todomatch)
  8190. (setq todo-only t todomatch (substring todomatch 1)))
  8191. (if (string-match "^\\s-*$" todomatch)
  8192. (setq todomatch nil)))
  8193. ;; only matching tags
  8194. (setq tagsmatch match todomatch nil))
  8195. ;; Make the tags matcher
  8196. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8197. (setq tagsmatcher t)
  8198. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8199. (while (setq term (pop orterms))
  8200. (while (and (equal (substring term -1) "\\") orterms)
  8201. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8202. (while (string-match re term)
  8203. (setq minus (and (match-end 1)
  8204. (equal (match-string 1 term) "-"))
  8205. tag (match-string 2 term)
  8206. re-p (equal (string-to-char tag) ?{)
  8207. level-p (match-end 4)
  8208. prop-p (match-end 5)
  8209. mm (cond
  8210. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8211. (level-p
  8212. (setq level-op (org-op-to-function (match-string 3 term)))
  8213. `(,level-op level ,(string-to-number
  8214. (match-string 4 term))))
  8215. (prop-p
  8216. (setq pn (match-string 5 term)
  8217. po (match-string 6 term)
  8218. pv (match-string 7 term)
  8219. cat-p (equal pn "CATEGORY")
  8220. re-p (equal (string-to-char pv) ?{)
  8221. str-p (equal (string-to-char pv) ?\")
  8222. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8223. (setq po (org-op-to-function po str-p))
  8224. (if (equal pn "CATEGORY")
  8225. (setq gv '(get-text-property (point) 'org-category))
  8226. (setq gv `(org-cached-entry-get nil ,pn)))
  8227. (if re-p
  8228. (if (eq po 'org<>)
  8229. `(not (string-match ,pv (or ,gv "")))
  8230. `(string-match ,pv (or ,gv "")))
  8231. (if str-p
  8232. `(,po (or ,gv "") ,pv)
  8233. `(,po (string-to-number (or ,gv ""))
  8234. ,(string-to-number pv) ))))
  8235. (t `(member ,(downcase tag) tags-list)))
  8236. mm (if minus (list 'not mm) mm)
  8237. term (substring term (match-end 0)))
  8238. (push mm tagsmatcher))
  8239. (push (if (> (length tagsmatcher) 1)
  8240. (cons 'and tagsmatcher)
  8241. (car tagsmatcher))
  8242. orlist)
  8243. (setq tagsmatcher nil))
  8244. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8245. (setq tagsmatcher
  8246. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8247. ;; Make the todo matcher
  8248. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8249. (setq todomatcher t)
  8250. (setq orterms (org-split-string todomatch "|") orlist nil)
  8251. (while (setq term (pop orterms))
  8252. (while (string-match re term)
  8253. (setq minus (and (match-end 1)
  8254. (equal (match-string 1 term) "-"))
  8255. kwd (match-string 2 term)
  8256. re-p (equal (string-to-char kwd) ?{)
  8257. term (substring term (match-end 0))
  8258. mm (if re-p
  8259. `(string-match ,(substring kwd 1 -1) todo)
  8260. (list 'equal 'todo kwd))
  8261. mm (if minus (list 'not mm) mm))
  8262. (push mm todomatcher))
  8263. (push (if (> (length todomatcher) 1)
  8264. (cons 'and todomatcher)
  8265. (car todomatcher))
  8266. orlist)
  8267. (setq todomatcher nil))
  8268. (setq todomatcher (if (> (length orlist) 1)
  8269. (cons 'or orlist) (car orlist))))
  8270. ;; Return the string and lisp forms of the matcher
  8271. (setq matcher (if todomatcher
  8272. (list 'and tagsmatcher todomatcher)
  8273. tagsmatcher))
  8274. (cons match0 matcher)))
  8275. (defun org-op-to-function (op &optional stringp)
  8276. (setq op
  8277. (cond
  8278. ((equal op "<" ) '(< string< ))
  8279. ((equal op ">" ) '(> org-string> ))
  8280. ((member op '("<=" "=<")) '(<= org-string<= ))
  8281. ((member op '(">=" "=>")) '(>= org-string>= ))
  8282. ((member op '("=" "==")) '(= string= ))
  8283. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8284. (nth (if stringp 1 0) op))
  8285. (defun org<> (a b) (not (= a b)))
  8286. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8287. (defun org-string>= (a b) (not (string< a b)))
  8288. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8289. (defun org-string<> (a b) (not (string= a b)))
  8290. (defun org-match-any-p (re list)
  8291. "Does re match any element of list?"
  8292. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8293. (delq nil list))
  8294. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8295. (defvar org-tags-overlay (org-make-overlay 1 1))
  8296. (org-detach-overlay org-tags-overlay)
  8297. (defun org-get-tags-at (&optional pos)
  8298. "Get a list of all headline tags applicable at POS.
  8299. POS defaults to point. If tags are inherited, the list contains
  8300. the targets in the same sequence as the headlines appear, i.e.
  8301. sthe tags of the current headline come last."
  8302. (interactive)
  8303. (let (tags ltags lastpos parent)
  8304. (save-excursion
  8305. (save-restriction
  8306. (widen)
  8307. (goto-char (or pos (point)))
  8308. (save-match-data
  8309. (condition-case nil
  8310. (progn
  8311. (org-back-to-heading t)
  8312. (while (not (equal lastpos (point)))
  8313. (setq lastpos (point))
  8314. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8315. (setq ltags (org-split-string
  8316. (org-match-string-no-properties 1) ":"))
  8317. (setq tags (append (org-remove-uniherited-tags ltags)
  8318. tags)))
  8319. (or org-use-tag-inheritance (error ""))
  8320. (org-up-heading-all 1)
  8321. (setq parent t)))
  8322. (error nil))))
  8323. tags)))
  8324. (defun org-toggle-tag (tag &optional onoff)
  8325. "Toggle the tag TAG for the current line.
  8326. If ONOFF is `on' or `off', don't toggle but set to this state."
  8327. (unless (org-on-heading-p t) (error "Not on headling"))
  8328. (let (res current)
  8329. (save-excursion
  8330. (beginning-of-line)
  8331. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8332. (point-at-eol) t)
  8333. (progn
  8334. (setq current (match-string 1))
  8335. (replace-match ""))
  8336. (setq current ""))
  8337. (setq current (nreverse (org-split-string current ":")))
  8338. (cond
  8339. ((eq onoff 'on)
  8340. (setq res t)
  8341. (or (member tag current) (push tag current)))
  8342. ((eq onoff 'off)
  8343. (or (not (member tag current)) (setq current (delete tag current))))
  8344. (t (if (member tag current)
  8345. (setq current (delete tag current))
  8346. (setq res t)
  8347. (push tag current))))
  8348. (end-of-line 1)
  8349. (if current
  8350. (progn
  8351. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8352. (org-set-tags nil t))
  8353. (delete-horizontal-space))
  8354. (run-hooks 'org-after-tags-change-hook))
  8355. res))
  8356. (defun org-align-tags-here (to-col)
  8357. ;; Assumes that this is a headline
  8358. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8359. (beginning-of-line 1)
  8360. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8361. (< pos (match-beginning 2)))
  8362. (progn
  8363. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8364. (goto-char (match-beginning 1))
  8365. (insert " ")
  8366. (delete-region (point) (1+ (match-beginning 2)))
  8367. (setq ncol (max (1+ (current-column))
  8368. (1+ col)
  8369. (if (> to-col 0)
  8370. to-col
  8371. (- (abs to-col) tags-l))))
  8372. (setq p (point))
  8373. (insert (make-string (- ncol (current-column)) ?\ ))
  8374. (setq ncol (current-column))
  8375. (tabify p (point-at-eol))
  8376. (org-move-to-column (min ncol col) t))
  8377. (goto-char pos))))
  8378. (defun org-set-tags (&optional arg just-align)
  8379. "Set the tags for the current headline.
  8380. With prefix ARG, realign all tags in headings in the current buffer."
  8381. (interactive "P")
  8382. (let* ((re (concat "^" outline-regexp))
  8383. (current (org-get-tags-string))
  8384. (col (current-column))
  8385. (org-setting-tags t)
  8386. table current-tags inherited-tags ; computed below when needed
  8387. tags p0 c0 c1 rpl)
  8388. (if arg
  8389. (save-excursion
  8390. (goto-char (point-min))
  8391. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8392. (while (re-search-forward re nil t)
  8393. (org-set-tags nil t)
  8394. (end-of-line 1)))
  8395. (message "All tags realigned to column %d" org-tags-column))
  8396. (if just-align
  8397. (setq tags current)
  8398. ;; Get a new set of tags from the user
  8399. (save-excursion
  8400. (setq table (or org-tag-alist (org-get-buffer-tags))
  8401. org-last-tags-completion-table table
  8402. current-tags (org-split-string current ":")
  8403. inherited-tags (nreverse
  8404. (nthcdr (length current-tags)
  8405. (nreverse (org-get-tags-at))))
  8406. tags
  8407. (if (or (eq t org-use-fast-tag-selection)
  8408. (and org-use-fast-tag-selection
  8409. (delq nil (mapcar 'cdr table))))
  8410. (org-fast-tag-selection
  8411. current-tags inherited-tags table
  8412. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8413. (let ((org-add-colon-after-tag-completion t))
  8414. (org-trim
  8415. (org-without-partial-completion
  8416. (completing-read "Tags: " 'org-tags-completion-function
  8417. nil nil current 'org-tags-history)))))))
  8418. (while (string-match "[-+&]+" tags)
  8419. ;; No boolean logic, just a list
  8420. (setq tags (replace-match ":" t t tags))))
  8421. (if (string-match "\\`[\t ]*\\'" tags)
  8422. (setq tags "")
  8423. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8424. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8425. ;; Insert new tags at the correct column
  8426. (beginning-of-line 1)
  8427. (cond
  8428. ((and (equal current "") (equal tags "")))
  8429. ((re-search-forward
  8430. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8431. (point-at-eol) t)
  8432. (if (equal tags "")
  8433. (setq rpl "")
  8434. (goto-char (match-beginning 0))
  8435. (setq c0 (current-column) p0 (point)
  8436. c1 (max (1+ c0) (if (> org-tags-column 0)
  8437. org-tags-column
  8438. (- (- org-tags-column) (length tags))))
  8439. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8440. (replace-match rpl t t)
  8441. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8442. tags)
  8443. (t (error "Tags alignment failed")))
  8444. (org-move-to-column col)
  8445. (unless just-align
  8446. (run-hooks 'org-after-tags-change-hook)))))
  8447. (defun org-change-tag-in-region (beg end tag off)
  8448. "Add or remove TAG for each entry in the region.
  8449. This works in the agenda, and also in an org-mode buffer."
  8450. (interactive
  8451. (list (region-beginning) (region-end)
  8452. (let ((org-last-tags-completion-table
  8453. (if (org-mode-p)
  8454. (org-get-buffer-tags)
  8455. (org-global-tags-completion-table))))
  8456. (completing-read
  8457. "Tag: " 'org-tags-completion-function nil nil nil
  8458. 'org-tags-history))
  8459. (progn
  8460. (message "[s]et or [r]emove? ")
  8461. (equal (read-char-exclusive) ?r))))
  8462. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8463. (let ((agendap (equal major-mode 'org-agenda-mode))
  8464. l1 l2 m buf pos newhead (cnt 0))
  8465. (goto-char end)
  8466. (setq l2 (1- (org-current-line)))
  8467. (goto-char beg)
  8468. (setq l1 (org-current-line))
  8469. (loop for l from l1 to l2 do
  8470. (goto-line l)
  8471. (setq m (get-text-property (point) 'org-hd-marker))
  8472. (when (or (and (org-mode-p) (org-on-heading-p))
  8473. (and agendap m))
  8474. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8475. pos (if agendap m (point)))
  8476. (with-current-buffer buf
  8477. (save-excursion
  8478. (save-restriction
  8479. (goto-char pos)
  8480. (setq cnt (1+ cnt))
  8481. (org-toggle-tag tag (if off 'off 'on))
  8482. (setq newhead (org-get-heading)))))
  8483. (and agendap (org-agenda-change-all-lines newhead m))))
  8484. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8485. (defun org-tags-completion-function (string predicate &optional flag)
  8486. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8487. (confirm (lambda (x) (stringp (car x)))))
  8488. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8489. (setq s1 (match-string 1 string)
  8490. s2 (match-string 2 string))
  8491. (setq s1 "" s2 string))
  8492. (cond
  8493. ((eq flag nil)
  8494. ;; try completion
  8495. (setq rtn (try-completion s2 ctable confirm))
  8496. (if (stringp rtn)
  8497. (setq rtn
  8498. (concat s1 s2 (substring rtn (length s2))
  8499. (if (and org-add-colon-after-tag-completion
  8500. (assoc rtn ctable))
  8501. ":" ""))))
  8502. rtn)
  8503. ((eq flag t)
  8504. ;; all-completions
  8505. (all-completions s2 ctable confirm)
  8506. )
  8507. ((eq flag 'lambda)
  8508. ;; exact match?
  8509. (assoc s2 ctable)))
  8510. ))
  8511. (defun org-fast-tag-insert (kwd tags face &optional end)
  8512. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8513. (insert (format "%-12s" (concat kwd ":"))
  8514. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8515. (or end "")))
  8516. (defun org-fast-tag-show-exit (flag)
  8517. (save-excursion
  8518. (goto-line 3)
  8519. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8520. (replace-match ""))
  8521. (when flag
  8522. (end-of-line 1)
  8523. (org-move-to-column (- (window-width) 19) t)
  8524. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8525. (defun org-set-current-tags-overlay (current prefix)
  8526. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8527. (if (featurep 'xemacs)
  8528. (org-overlay-display org-tags-overlay (concat prefix s)
  8529. 'secondary-selection)
  8530. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8531. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8532. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8533. "Fast tag selection with single keys.
  8534. CURRENT is the current list of tags in the headline, INHERITED is the
  8535. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8536. possibly with grouping information. TODO-TABLE is a similar table with
  8537. TODO keywords, should these have keys assigned to them.
  8538. If the keys are nil, a-z are automatically assigned.
  8539. Returns the new tags string, or nil to not change the current settings."
  8540. (let* ((fulltable (append table todo-table))
  8541. (maxlen (apply 'max (mapcar
  8542. (lambda (x)
  8543. (if (stringp (car x)) (string-width (car x)) 0))
  8544. fulltable)))
  8545. (buf (current-buffer))
  8546. (expert (eq org-fast-tag-selection-single-key 'expert))
  8547. (buffer-tags nil)
  8548. (fwidth (+ maxlen 3 1 3))
  8549. (ncol (/ (- (window-width) 4) fwidth))
  8550. (i-face 'org-done)
  8551. (c-face 'org-todo)
  8552. tg cnt e c char c1 c2 ntable tbl rtn
  8553. ov-start ov-end ov-prefix
  8554. (exit-after-next org-fast-tag-selection-single-key)
  8555. (done-keywords org-done-keywords)
  8556. groups ingroup)
  8557. (save-excursion
  8558. (beginning-of-line 1)
  8559. (if (looking-at
  8560. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8561. (setq ov-start (match-beginning 1)
  8562. ov-end (match-end 1)
  8563. ov-prefix "")
  8564. (setq ov-start (1- (point-at-eol))
  8565. ov-end (1+ ov-start))
  8566. (skip-chars-forward "^\n\r")
  8567. (setq ov-prefix
  8568. (concat
  8569. (buffer-substring (1- (point)) (point))
  8570. (if (> (current-column) org-tags-column)
  8571. " "
  8572. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8573. (org-move-overlay org-tags-overlay ov-start ov-end)
  8574. (save-window-excursion
  8575. (if expert
  8576. (set-buffer (get-buffer-create " *Org tags*"))
  8577. (delete-other-windows)
  8578. (split-window-vertically)
  8579. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8580. (erase-buffer)
  8581. (org-set-local 'org-done-keywords done-keywords)
  8582. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8583. (org-fast-tag-insert "Current" current c-face "\n\n")
  8584. (org-fast-tag-show-exit exit-after-next)
  8585. (org-set-current-tags-overlay current ov-prefix)
  8586. (setq tbl fulltable char ?a cnt 0)
  8587. (while (setq e (pop tbl))
  8588. (cond
  8589. ((equal e '(:startgroup))
  8590. (push '() groups) (setq ingroup t)
  8591. (when (not (= cnt 0))
  8592. (setq cnt 0)
  8593. (insert "\n"))
  8594. (insert "{ "))
  8595. ((equal e '(:endgroup))
  8596. (setq ingroup nil cnt 0)
  8597. (insert "}\n"))
  8598. (t
  8599. (setq tg (car e) c2 nil)
  8600. (if (cdr e)
  8601. (setq c (cdr e))
  8602. ;; automatically assign a character.
  8603. (setq c1 (string-to-char
  8604. (downcase (substring
  8605. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8606. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8607. (while (or (rassoc char ntable) (rassoc char table))
  8608. (setq char (1+ char)))
  8609. (setq c2 c1))
  8610. (setq c (or c2 char)))
  8611. (if ingroup (push tg (car groups)))
  8612. (setq tg (org-add-props tg nil 'face
  8613. (cond
  8614. ((not (assoc tg table))
  8615. (org-get-todo-face tg))
  8616. ((member tg current) c-face)
  8617. ((member tg inherited) i-face)
  8618. (t nil))))
  8619. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8620. (insert "[" c "] " tg (make-string
  8621. (- fwidth 4 (length tg)) ?\ ))
  8622. (push (cons tg c) ntable)
  8623. (when (= (setq cnt (1+ cnt)) ncol)
  8624. (insert "\n")
  8625. (if ingroup (insert " "))
  8626. (setq cnt 0)))))
  8627. (setq ntable (nreverse ntable))
  8628. (insert "\n")
  8629. (goto-char (point-min))
  8630. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8631. (fit-window-to-buffer))
  8632. (setq rtn
  8633. (catch 'exit
  8634. (while t
  8635. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8636. (if groups " [!] no groups" " [!]groups")
  8637. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8638. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8639. (cond
  8640. ((= c ?\r) (throw 'exit t))
  8641. ((= c ?!)
  8642. (setq groups (not groups))
  8643. (goto-char (point-min))
  8644. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8645. ((= c ?\C-c)
  8646. (if (not expert)
  8647. (org-fast-tag-show-exit
  8648. (setq exit-after-next (not exit-after-next)))
  8649. (setq expert nil)
  8650. (delete-other-windows)
  8651. (split-window-vertically)
  8652. (org-switch-to-buffer-other-window " *Org tags*")
  8653. (and (fboundp 'fit-window-to-buffer)
  8654. (fit-window-to-buffer))))
  8655. ((or (= c ?\C-g)
  8656. (and (= c ?q) (not (rassoc c ntable))))
  8657. (org-detach-overlay org-tags-overlay)
  8658. (setq quit-flag t))
  8659. ((= c ?\ )
  8660. (setq current nil)
  8661. (if exit-after-next (setq exit-after-next 'now)))
  8662. ((= c ?\t)
  8663. (condition-case nil
  8664. (setq tg (completing-read
  8665. "Tag: "
  8666. (or buffer-tags
  8667. (with-current-buffer buf
  8668. (org-get-buffer-tags)))))
  8669. (quit (setq tg "")))
  8670. (when (string-match "\\S-" tg)
  8671. (add-to-list 'buffer-tags (list tg))
  8672. (if (member tg current)
  8673. (setq current (delete tg current))
  8674. (push tg current)))
  8675. (if exit-after-next (setq exit-after-next 'now)))
  8676. ((setq e (rassoc c todo-table) tg (car e))
  8677. (with-current-buffer buf
  8678. (save-excursion (org-todo tg)))
  8679. (if exit-after-next (setq exit-after-next 'now)))
  8680. ((setq e (rassoc c ntable) tg (car e))
  8681. (if (member tg current)
  8682. (setq current (delete tg current))
  8683. (loop for g in groups do
  8684. (if (member tg g)
  8685. (mapc (lambda (x)
  8686. (setq current (delete x current)))
  8687. g)))
  8688. (push tg current))
  8689. (if exit-after-next (setq exit-after-next 'now))))
  8690. ;; Create a sorted list
  8691. (setq current
  8692. (sort current
  8693. (lambda (a b)
  8694. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8695. (if (eq exit-after-next 'now) (throw 'exit t))
  8696. (goto-char (point-min))
  8697. (beginning-of-line 2)
  8698. (delete-region (point) (point-at-eol))
  8699. (org-fast-tag-insert "Current" current c-face)
  8700. (org-set-current-tags-overlay current ov-prefix)
  8701. (while (re-search-forward
  8702. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8703. (setq tg (match-string 1))
  8704. (add-text-properties
  8705. (match-beginning 1) (match-end 1)
  8706. (list 'face
  8707. (cond
  8708. ((member tg current) c-face)
  8709. ((member tg inherited) i-face)
  8710. (t (get-text-property (match-beginning 1) 'face))))))
  8711. (goto-char (point-min)))))
  8712. (org-detach-overlay org-tags-overlay)
  8713. (if rtn
  8714. (mapconcat 'identity current ":")
  8715. nil))))
  8716. (defun org-get-tags-string ()
  8717. "Get the TAGS string in the current headline."
  8718. (unless (org-on-heading-p t)
  8719. (error "Not on a heading"))
  8720. (save-excursion
  8721. (beginning-of-line 1)
  8722. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8723. (org-match-string-no-properties 1)
  8724. "")))
  8725. (defun org-get-tags ()
  8726. "Get the list of tags specified in the current headline."
  8727. (org-split-string (org-get-tags-string) ":"))
  8728. (defun org-get-buffer-tags ()
  8729. "Get a table of all tags used in the buffer, for completion."
  8730. (let (tags)
  8731. (save-excursion
  8732. (goto-char (point-min))
  8733. (while (re-search-forward
  8734. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8735. (when (equal (char-after (point-at-bol 0)) ?*)
  8736. (mapc (lambda (x) (add-to-list 'tags x))
  8737. (org-split-string (org-match-string-no-properties 1) ":")))))
  8738. (mapcar 'list tags)))
  8739. ;;;; Properties
  8740. ;;; Setting and retrieving properties
  8741. (defconst org-special-properties
  8742. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8743. "TIMESTAMP" "TIMESTAMP_IA")
  8744. "The special properties valid in Org-mode.
  8745. These are properties that are not defined in the property drawer,
  8746. but in some other way.")
  8747. (defconst org-default-properties
  8748. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8749. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  8750. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE")
  8751. "Some properties that are used by Org-mode for various purposes.
  8752. Being in this list makes sure that they are offered for completion.")
  8753. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8754. "Regular expression matching the first line of a property drawer.")
  8755. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8756. "Regular expression matching the first line of a property drawer.")
  8757. (defun org-property-action ()
  8758. "Do an action on properties."
  8759. (interactive)
  8760. (let (c)
  8761. (org-at-property-p)
  8762. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8763. (setq c (read-char-exclusive))
  8764. (cond
  8765. ((equal c ?s)
  8766. (call-interactively 'org-set-property))
  8767. ((equal c ?d)
  8768. (call-interactively 'org-delete-property))
  8769. ((equal c ?D)
  8770. (call-interactively 'org-delete-property-globally))
  8771. ((equal c ?c)
  8772. (call-interactively 'org-compute-property-at-point))
  8773. (t (error "No such property action %c" c)))))
  8774. (defun org-at-property-p ()
  8775. "Is the cursor in a property line?"
  8776. ;; FIXME: Does not check if we are actually in the drawer.
  8777. ;; FIXME: also returns true on any drawers.....
  8778. ;; This is used by C-c C-c for property action.
  8779. (save-excursion
  8780. (beginning-of-line 1)
  8781. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8782. (defun org-get-property-block (&optional beg end force)
  8783. "Return the (beg . end) range of the body of the property drawer.
  8784. BEG and END can be beginning and end of subtree, if not given
  8785. they will be found.
  8786. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8787. (catch 'exit
  8788. (save-excursion
  8789. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8790. (end (or end (progn (outline-next-heading) (point)))))
  8791. (goto-char beg)
  8792. (if (re-search-forward org-property-start-re end t)
  8793. (setq beg (1+ (match-end 0)))
  8794. (if force
  8795. (save-excursion
  8796. (org-insert-property-drawer)
  8797. (setq end (progn (outline-next-heading) (point))))
  8798. (throw 'exit nil))
  8799. (goto-char beg)
  8800. (if (re-search-forward org-property-start-re end t)
  8801. (setq beg (1+ (match-end 0)))))
  8802. (if (re-search-forward org-property-end-re end t)
  8803. (setq end (match-beginning 0))
  8804. (or force (throw 'exit nil))
  8805. (goto-char beg)
  8806. (setq end beg)
  8807. (org-indent-line-function)
  8808. (insert ":END:\n"))
  8809. (cons beg end)))))
  8810. (defun org-entry-properties (&optional pom which)
  8811. "Get all properties of the entry at point-or-marker POM.
  8812. This includes the TODO keyword, the tags, time strings for deadline,
  8813. scheduled, and clocking, and any additional properties defined in the
  8814. entry. The return value is an alist, keys may occur multiple times
  8815. if the property key was used several times.
  8816. POM may also be nil, in which case the current entry is used.
  8817. If WHICH is nil or `all', get all properties. If WHICH is
  8818. `special' or `standard', only get that subclass."
  8819. (setq which (or which 'all))
  8820. (org-with-point-at pom
  8821. (let ((clockstr (substring org-clock-string 0 -1))
  8822. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8823. beg end range props sum-props key value string clocksum)
  8824. (save-excursion
  8825. (when (condition-case nil (org-back-to-heading t) (error nil))
  8826. (setq beg (point))
  8827. (setq sum-props (get-text-property (point) 'org-summaries))
  8828. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8829. (outline-next-heading)
  8830. (setq end (point))
  8831. (when (memq which '(all special))
  8832. ;; Get the special properties, like TODO and tags
  8833. (goto-char beg)
  8834. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8835. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8836. (when (looking-at org-priority-regexp)
  8837. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8838. (when (and (setq value (org-get-tags-string))
  8839. (string-match "\\S-" value))
  8840. (push (cons "TAGS" value) props))
  8841. (when (setq value (org-get-tags-at))
  8842. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8843. props))
  8844. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8845. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8846. string (if (equal key clockstr)
  8847. (org-no-properties
  8848. (org-trim
  8849. (buffer-substring
  8850. (match-beginning 3) (goto-char (point-at-eol)))))
  8851. (substring (org-match-string-no-properties 3) 1 -1)))
  8852. (unless key
  8853. (if (= (char-after (match-beginning 3)) ?\[)
  8854. (setq key "TIMESTAMP_IA")
  8855. (setq key "TIMESTAMP")))
  8856. (when (or (equal key clockstr) (not (assoc key props)))
  8857. (push (cons key string) props)))
  8858. )
  8859. (when (memq which '(all standard))
  8860. ;; Get the standard properties, like :PORP: ...
  8861. (setq range (org-get-property-block beg end))
  8862. (when range
  8863. (goto-char (car range))
  8864. (while (re-search-forward
  8865. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8866. (cdr range) t)
  8867. (setq key (org-match-string-no-properties 1)
  8868. value (org-trim (or (org-match-string-no-properties 2) "")))
  8869. (unless (member key excluded)
  8870. (push (cons key (or value "")) props)))))
  8871. (if clocksum
  8872. (push (cons "CLOCKSUM"
  8873. (org-columns-number-to-string (/ (float clocksum) 60.)
  8874. 'add_times))
  8875. props))
  8876. (append sum-props (nreverse props)))))))
  8877. (defun org-entry-get (pom property &optional inherit)
  8878. "Get value of PROPERTY for entry at point-or-marker POM.
  8879. If INHERIT is non-nil and the entry does not have the property,
  8880. then also check higher levels of the hierarchy.
  8881. If INHERIT is the symbol `selective', use inheritance only if the setting
  8882. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8883. If the property is present but empty, the return value is the empty string.
  8884. If the property is not present at all, nil is returned."
  8885. (org-with-point-at pom
  8886. (if (and inherit (if (eq inherit 'selective)
  8887. (org-property-inherit-p property)
  8888. t))
  8889. (org-entry-get-with-inheritance property)
  8890. (if (member property org-special-properties)
  8891. ;; We need a special property. Use brute force, get all properties.
  8892. (cdr (assoc property (org-entry-properties nil 'special)))
  8893. (let ((range (org-get-property-block)))
  8894. (if (and range
  8895. (goto-char (car range))
  8896. (re-search-forward
  8897. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8898. (cdr range) t))
  8899. ;; Found the property, return it.
  8900. (if (match-end 1)
  8901. (org-match-string-no-properties 1)
  8902. "")))))))
  8903. (defun org-property-or-variable-value (var &optional inherit)
  8904. "Check if there is a property fixing the value of VAR.
  8905. If yes, return this value. If not, return the current value of the variable."
  8906. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8907. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8908. (read prop)
  8909. (symbol-value var))))
  8910. (defun org-entry-delete (pom property)
  8911. "Delete the property PROPERTY from entry at point-or-marker POM."
  8912. (org-with-point-at pom
  8913. (if (member property org-special-properties)
  8914. nil ; cannot delete these properties.
  8915. (let ((range (org-get-property-block)))
  8916. (if (and range
  8917. (goto-char (car range))
  8918. (re-search-forward
  8919. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8920. (cdr range) t))
  8921. (progn
  8922. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8923. t)
  8924. nil)))))
  8925. ;; Multi-values properties are properties that contain multiple values
  8926. ;; These values are assumed to be single words, separated by whitespace.
  8927. (defun org-entry-add-to-multivalued-property (pom property value)
  8928. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8929. (let* ((old (org-entry-get pom property))
  8930. (values (and old (org-split-string old "[ \t]"))))
  8931. (unless (member value values)
  8932. (setq values (cons value values))
  8933. (org-entry-put pom property
  8934. (mapconcat 'identity values " ")))))
  8935. (defun org-entry-remove-from-multivalued-property (pom property value)
  8936. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8937. (let* ((old (org-entry-get pom property))
  8938. (values (and old (org-split-string old "[ \t]"))))
  8939. (when (member value values)
  8940. (setq values (delete value values))
  8941. (org-entry-put pom property
  8942. (mapconcat 'identity values " ")))))
  8943. (defun org-entry-member-in-multivalued-property (pom property value)
  8944. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8945. (let* ((old (org-entry-get pom property))
  8946. (values (and old (org-split-string old "[ \t]"))))
  8947. (member value values)))
  8948. (defvar org-entry-property-inherited-from (make-marker))
  8949. (defun org-entry-get-with-inheritance (property)
  8950. "Get entry property, and search higher levels if not present."
  8951. (let (tmp)
  8952. (save-excursion
  8953. (save-restriction
  8954. (widen)
  8955. (catch 'ex
  8956. (while t
  8957. (when (setq tmp (org-entry-get nil property))
  8958. (org-back-to-heading t)
  8959. (move-marker org-entry-property-inherited-from (point))
  8960. (throw 'ex tmp))
  8961. (or (org-up-heading-safe) (throw 'ex nil)))))
  8962. (or tmp
  8963. (cdr (assoc property org-local-properties))
  8964. (cdr (assoc property org-global-properties))
  8965. (cdr (assoc property org-global-properties-fixed))))))
  8966. (defun org-entry-put (pom property value)
  8967. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8968. (org-with-point-at pom
  8969. (org-back-to-heading t)
  8970. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8971. range)
  8972. (cond
  8973. ((equal property "TODO")
  8974. (when (and (stringp value) (string-match "\\S-" value)
  8975. (not (member value org-todo-keywords-1)))
  8976. (error "\"%s\" is not a valid TODO state" value))
  8977. (if (or (not value)
  8978. (not (string-match "\\S-" value)))
  8979. (setq value 'none))
  8980. (org-todo value)
  8981. (org-set-tags nil 'align))
  8982. ((equal property "PRIORITY")
  8983. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8984. (string-to-char value) ?\ ))
  8985. (org-set-tags nil 'align))
  8986. ((equal property "SCHEDULED")
  8987. (if (re-search-forward org-scheduled-time-regexp end t)
  8988. (cond
  8989. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8990. ((eq value 'later) (org-timestamp-change 1 'day))
  8991. (t (call-interactively 'org-schedule)))
  8992. (call-interactively 'org-schedule)))
  8993. ((equal property "DEADLINE")
  8994. (if (re-search-forward org-deadline-time-regexp end t)
  8995. (cond
  8996. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8997. ((eq value 'later) (org-timestamp-change 1 'day))
  8998. (t (call-interactively 'org-deadline)))
  8999. (call-interactively 'org-deadline)))
  9000. ((member property org-special-properties)
  9001. (error "The %s property can not yet be set with `org-entry-put'"
  9002. property))
  9003. (t ; a non-special property
  9004. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9005. (setq range (org-get-property-block beg end 'force))
  9006. (goto-char (car range))
  9007. (if (re-search-forward
  9008. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9009. (progn
  9010. (delete-region (match-beginning 1) (match-end 1))
  9011. (goto-char (match-beginning 1)))
  9012. (goto-char (cdr range))
  9013. (insert "\n")
  9014. (backward-char 1)
  9015. (org-indent-line-function)
  9016. (insert ":" property ":"))
  9017. (and value (insert " " value))
  9018. (org-indent-line-function)))))))
  9019. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9020. "Get all property keys in the current buffer.
  9021. With INCLUDE-SPECIALS, also list the special properties that relect things
  9022. like tags and TODO state.
  9023. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9024. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9025. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9026. formats in the current buffer."
  9027. (let (rtn range cfmt cols s p)
  9028. (save-excursion
  9029. (save-restriction
  9030. (widen)
  9031. (goto-char (point-min))
  9032. (while (re-search-forward org-property-start-re nil t)
  9033. (setq range (org-get-property-block))
  9034. (goto-char (car range))
  9035. (while (re-search-forward
  9036. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9037. (cdr range) t)
  9038. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9039. (outline-next-heading))))
  9040. (when include-specials
  9041. (setq rtn (append org-special-properties rtn)))
  9042. (when include-defaults
  9043. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9044. (when include-columns
  9045. (save-excursion
  9046. (save-restriction
  9047. (widen)
  9048. (goto-char (point-min))
  9049. (while (re-search-forward
  9050. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9051. nil t)
  9052. (setq cfmt (match-string 2) s 0)
  9053. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9054. cfmt s)
  9055. (setq s (match-end 0)
  9056. p (match-string 1 cfmt))
  9057. (unless (or (equal p "ITEM")
  9058. (member p org-special-properties))
  9059. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9060. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9061. (defun org-property-values (key)
  9062. "Return a list of all values of property KEY."
  9063. (save-excursion
  9064. (save-restriction
  9065. (widen)
  9066. (goto-char (point-min))
  9067. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9068. values)
  9069. (while (re-search-forward re nil t)
  9070. (add-to-list 'values (org-trim (match-string 1))))
  9071. (delete "" values)))))
  9072. (defun org-insert-property-drawer ()
  9073. "Insert a property drawer into the current entry."
  9074. (interactive)
  9075. (org-back-to-heading t)
  9076. (looking-at outline-regexp)
  9077. (let ((indent (- (match-end 0)(match-beginning 0)))
  9078. (beg (point))
  9079. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9080. end hiddenp)
  9081. (outline-next-heading)
  9082. (setq end (point))
  9083. (goto-char beg)
  9084. (while (re-search-forward re end t))
  9085. (setq hiddenp (org-invisible-p))
  9086. (end-of-line 1)
  9087. (and (equal (char-after) ?\n) (forward-char 1))
  9088. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9089. (beginning-of-line 2))
  9090. (org-skip-over-state-notes)
  9091. (skip-chars-backward " \t\n\r")
  9092. (if (eq (char-before) ?*) (forward-char 1))
  9093. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9094. (beginning-of-line 0)
  9095. (org-indent-to-column indent)
  9096. (beginning-of-line 2)
  9097. (org-indent-to-column indent)
  9098. (beginning-of-line 0)
  9099. (if hiddenp
  9100. (save-excursion
  9101. (org-back-to-heading t)
  9102. (hide-entry))
  9103. (org-flag-drawer t))))
  9104. (defun org-set-property (property value)
  9105. "In the current entry, set PROPERTY to VALUE.
  9106. When called interactively, this will prompt for a property name, offering
  9107. completion on existing and default properties. And then it will prompt
  9108. for a value, offering competion either on allowed values (via an inherited
  9109. xxx_ALL property) or on existing values in other instances of this property
  9110. in the current file."
  9111. (interactive
  9112. (let* ((completion-ignore-case t)
  9113. (keys (org-buffer-property-keys nil t t))
  9114. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9115. (prop (if (member prop0 keys)
  9116. prop0
  9117. (or (cdr (assoc (downcase prop0)
  9118. (mapcar (lambda (x) (cons (downcase x) x))
  9119. keys)))
  9120. prop0)))
  9121. (cur (org-entry-get nil prop))
  9122. (allowed (org-property-get-allowed-values nil prop 'table))
  9123. (existing (mapcar 'list (org-property-values prop)))
  9124. (val (if allowed
  9125. (org-completing-read "Value: " allowed nil 'req-match)
  9126. (org-completing-read
  9127. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9128. (concat "[" cur "]") "")
  9129. ": ")
  9130. existing nil nil "" nil cur))))
  9131. (list prop (if (equal val "") cur val))))
  9132. (unless (equal (org-entry-get nil property) value)
  9133. (org-entry-put nil property value)))
  9134. (defun org-delete-property (property)
  9135. "In the current entry, delete PROPERTY."
  9136. (interactive
  9137. (let* ((completion-ignore-case t)
  9138. (prop (completing-read
  9139. "Property: " (org-entry-properties nil 'standard))))
  9140. (list prop)))
  9141. (message "Property %s %s" property
  9142. (if (org-entry-delete nil property)
  9143. "deleted"
  9144. "was not present in the entry")))
  9145. (defun org-delete-property-globally (property)
  9146. "Remove PROPERTY globally, from all entries."
  9147. (interactive
  9148. (let* ((completion-ignore-case t)
  9149. (prop (completing-read
  9150. "Globally remove property: "
  9151. (mapcar 'list (org-buffer-property-keys)))))
  9152. (list prop)))
  9153. (save-excursion
  9154. (save-restriction
  9155. (widen)
  9156. (goto-char (point-min))
  9157. (let ((cnt 0))
  9158. (while (re-search-forward
  9159. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9160. nil t)
  9161. (setq cnt (1+ cnt))
  9162. (replace-match ""))
  9163. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9164. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9165. (defun org-compute-property-at-point ()
  9166. "Compute the property at point.
  9167. This looks for an enclosing column format, extracts the operator and
  9168. then applies it to the proerty in the column format's scope."
  9169. (interactive)
  9170. (unless (org-at-property-p)
  9171. (error "Not at a property"))
  9172. (let ((prop (org-match-string-no-properties 2)))
  9173. (org-columns-get-format-and-top-level)
  9174. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9175. (error "No operator defined for property %s" prop))
  9176. (org-columns-compute prop)))
  9177. (defun org-property-get-allowed-values (pom property &optional table)
  9178. "Get allowed values for the property PROPERTY.
  9179. When TABLE is non-nil, return an alist that can directly be used for
  9180. completion."
  9181. (let (vals)
  9182. (cond
  9183. ((equal property "TODO")
  9184. (setq vals (org-with-point-at pom
  9185. (append org-todo-keywords-1 '("")))))
  9186. ((equal property "PRIORITY")
  9187. (let ((n org-lowest-priority))
  9188. (while (>= n org-highest-priority)
  9189. (push (char-to-string n) vals)
  9190. (setq n (1- n)))))
  9191. ((member property org-special-properties))
  9192. (t
  9193. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9194. (when (and vals (string-match "\\S-" vals))
  9195. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9196. (setq vals (mapcar (lambda (x)
  9197. (cond ((stringp x) x)
  9198. ((numberp x) (number-to-string x))
  9199. ((symbolp x) (symbol-name x))
  9200. (t "???")))
  9201. vals)))))
  9202. (if table (mapcar 'list vals) vals)))
  9203. (defun org-property-previous-allowed-value (&optional previous)
  9204. "Switch to the next allowed value for this property."
  9205. (interactive)
  9206. (org-property-next-allowed-value t))
  9207. (defun org-property-next-allowed-value (&optional previous)
  9208. "Switch to the next allowed value for this property."
  9209. (interactive)
  9210. (unless (org-at-property-p)
  9211. (error "Not at a property"))
  9212. (let* ((key (match-string 2))
  9213. (value (match-string 3))
  9214. (allowed (or (org-property-get-allowed-values (point) key)
  9215. (and (member value '("[ ]" "[-]" "[X]"))
  9216. '("[ ]" "[X]"))))
  9217. nval)
  9218. (unless allowed
  9219. (error "Allowed values for this property have not been defined"))
  9220. (if previous (setq allowed (reverse allowed)))
  9221. (if (member value allowed)
  9222. (setq nval (car (cdr (member value allowed)))))
  9223. (setq nval (or nval (car allowed)))
  9224. (if (equal nval value)
  9225. (error "Only one allowed value for this property"))
  9226. (org-at-property-p)
  9227. (replace-match (concat " :" key ": " nval) t t)
  9228. (org-indent-line-function)
  9229. (beginning-of-line 1)
  9230. (skip-chars-forward " \t")))
  9231. (defun org-find-entry-with-id (ident)
  9232. "Locate the entry that contains the ID property with exact value IDENT.
  9233. IDENT can be a string, a symbol or a number, this function will search for
  9234. the string representation of it.
  9235. Return the position where this entry starts, or nil if there is no such entry."
  9236. (let ((id (cond
  9237. ((stringp ident) ident)
  9238. ((symbol-name ident) (symbol-name ident))
  9239. ((numberp ident) (number-to-string ident))
  9240. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9241. (case-fold-search nil))
  9242. (save-excursion
  9243. (save-restriction
  9244. (widen)
  9245. (goto-char (point-min))
  9246. (when (re-search-forward
  9247. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9248. nil t)
  9249. (org-back-to-heading)
  9250. (point))))))
  9251. ;;;; Timestamps
  9252. (defvar org-last-changed-timestamp nil)
  9253. (defvar org-time-was-given) ; dynamically scoped parameter
  9254. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9255. (defvar org-ts-what) ; dynamically scoped parameter
  9256. (defun org-time-stamp (arg)
  9257. "Prompt for a date/time and insert a time stamp.
  9258. If the user specifies a time like HH:MM, or if this command is called
  9259. with a prefix argument, the time stamp will contain date and time.
  9260. Otherwise, only the date will be included. All parts of a date not
  9261. specified by the user will be filled in from the current date/time.
  9262. So if you press just return without typing anything, the time stamp
  9263. will represent the current date/time. If there is already a timestamp
  9264. at the cursor, it will be modified."
  9265. (interactive "P")
  9266. (let* ((ts nil)
  9267. (default-time
  9268. ;; Default time is either today, or, when entering a range,
  9269. ;; the range start.
  9270. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9271. (save-excursion
  9272. (re-search-backward
  9273. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9274. (- (point) 20) t)))
  9275. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9276. (current-time)))
  9277. (default-input (and ts (org-get-compact-tod ts)))
  9278. org-time-was-given org-end-time-was-given time)
  9279. (cond
  9280. ((and (org-at-timestamp-p)
  9281. (eq last-command 'org-time-stamp)
  9282. (eq this-command 'org-time-stamp))
  9283. (insert "--")
  9284. (setq time (let ((this-command this-command))
  9285. (org-read-date arg 'totime nil nil default-time default-input)))
  9286. (org-insert-time-stamp time (or org-time-was-given arg)))
  9287. ((org-at-timestamp-p)
  9288. (setq time (let ((this-command this-command))
  9289. (org-read-date arg 'totime nil nil default-time default-input)))
  9290. (when (org-at-timestamp-p) ; just to get the match data
  9291. (replace-match "")
  9292. (setq org-last-changed-timestamp
  9293. (org-insert-time-stamp
  9294. time (or org-time-was-given arg)
  9295. nil nil nil (list org-end-time-was-given))))
  9296. (message "Timestamp updated"))
  9297. (t
  9298. (setq time (let ((this-command this-command))
  9299. (org-read-date arg 'totime nil nil default-time default-input)))
  9300. (org-insert-time-stamp time (or org-time-was-given arg)
  9301. nil nil nil (list org-end-time-was-given))))))
  9302. ;; FIXME: can we use this for something else, like computing time differences?
  9303. (defun org-get-compact-tod (s)
  9304. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9305. (let* ((t1 (match-string 1 s))
  9306. (h1 (string-to-number (match-string 2 s)))
  9307. (m1 (string-to-number (match-string 3 s)))
  9308. (t2 (and (match-end 4) (match-string 5 s)))
  9309. (h2 (and t2 (string-to-number (match-string 6 s))))
  9310. (m2 (and t2 (string-to-number (match-string 7 s))))
  9311. dh dm)
  9312. (if (not t2)
  9313. t1
  9314. (setq dh (- h2 h1) dm (- m2 m1))
  9315. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9316. (concat t1 "+" (number-to-string dh)
  9317. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9318. (defun org-time-stamp-inactive (&optional arg)
  9319. "Insert an inactive time stamp.
  9320. An inactive time stamp is enclosed in square brackets instead of angle
  9321. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9322. does not link to the calendar and cannot be changed with the S-cursor keys.
  9323. So these are more for recording a certain time/date."
  9324. (interactive "P")
  9325. (let (org-time-was-given org-end-time-was-given time)
  9326. (setq time (org-read-date arg 'totime))
  9327. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9328. nil nil (list org-end-time-was-given))))
  9329. (defvar org-date-ovl (org-make-overlay 1 1))
  9330. (org-overlay-put org-date-ovl 'face 'org-warning)
  9331. (org-detach-overlay org-date-ovl)
  9332. (defvar org-ans1) ; dynamically scoped parameter
  9333. (defvar org-ans2) ; dynamically scoped parameter
  9334. (defvar org-plain-time-of-day-regexp) ; defined below
  9335. (defvar org-read-date-overlay nil)
  9336. (defvar org-dcst nil) ; dynamically scoped
  9337. (defun org-read-date (&optional with-time to-time from-string prompt
  9338. default-time default-input)
  9339. "Read a date, possibly a time, and make things smooth for the user.
  9340. The prompt will suggest to enter an ISO date, but you can also enter anything
  9341. which will at least partially be understood by `parse-time-string'.
  9342. Unrecognized parts of the date will default to the current day, month, year,
  9343. hour and minute. If this command is called to replace a timestamp at point,
  9344. of to enter the second timestamp of a range, the default time is taken from the
  9345. existing stamp. For example,
  9346. 3-2-5 --> 2003-02-05
  9347. feb 15 --> currentyear-02-15
  9348. sep 12 9 --> 2009-09-12
  9349. 12:45 --> today 12:45
  9350. 22 sept 0:34 --> currentyear-09-22 0:34
  9351. 12 --> currentyear-currentmonth-12
  9352. Fri --> nearest Friday (today or later)
  9353. etc.
  9354. Furthermore you can specify a relative date by giving, as the *first* thing
  9355. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9356. change in days weeks, months, years.
  9357. With a single plus or minus, the date is relative to today. With a double
  9358. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9359. +4d --> four days from today
  9360. +4 --> same as above
  9361. +2w --> two weeks from today
  9362. ++5 --> five days from default date
  9363. The function understands only English month and weekday abbreviations,
  9364. but this can be configured with the variables `parse-time-months' and
  9365. `parse-time-weekdays'.
  9366. While prompting, a calendar is popped up - you can also select the
  9367. date with the mouse (button 1). The calendar shows a period of three
  9368. months. To scroll it to other months, use the keys `>' and `<'.
  9369. If you don't like the calendar, turn it off with
  9370. \(setq org-read-date-popup-calendar nil)
  9371. With optional argument TO-TIME, the date will immediately be converted
  9372. to an internal time.
  9373. With an optional argument WITH-TIME, the prompt will suggest to also
  9374. insert a time. Note that when WITH-TIME is not set, you can still
  9375. enter a time, and this function will inform the calling routine about
  9376. this change. The calling routine may then choose to change the format
  9377. used to insert the time stamp into the buffer to include the time.
  9378. With optional argument FROM-STRING, read from this string instead from
  9379. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9380. the time/date that is used for everything that is not specified by the
  9381. user."
  9382. (require 'parse-time)
  9383. (let* ((org-time-stamp-rounding-minutes
  9384. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9385. (org-dcst org-display-custom-times)
  9386. (ct (org-current-time))
  9387. (def (or default-time ct))
  9388. (defdecode (decode-time def))
  9389. (dummy (progn
  9390. (when (< (nth 2 defdecode) org-extend-today-until)
  9391. (setcar (nthcdr 2 defdecode) -1)
  9392. (setcar (nthcdr 1 defdecode) 59)
  9393. (setq def (apply 'encode-time defdecode)
  9394. defdecode (decode-time def)))))
  9395. (calendar-move-hook nil)
  9396. (calendar-view-diary-initially-flag nil)
  9397. (view-diary-entries-initially nil)
  9398. (calendar-view-holidays-initially-flag nil)
  9399. (view-calendar-holidays-initially nil)
  9400. (timestr (format-time-string
  9401. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9402. (prompt (concat (if prompt (concat prompt " ") "")
  9403. (format "Date+time [%s]: " timestr)))
  9404. ans (org-ans0 "") org-ans1 org-ans2 final)
  9405. (cond
  9406. (from-string (setq ans from-string))
  9407. (org-read-date-popup-calendar
  9408. (save-excursion
  9409. (save-window-excursion
  9410. (calendar)
  9411. (calendar-forward-day (- (time-to-days def)
  9412. (calendar-absolute-from-gregorian
  9413. (calendar-current-date))))
  9414. (org-eval-in-calendar nil t)
  9415. (let* ((old-map (current-local-map))
  9416. (map (copy-keymap calendar-mode-map))
  9417. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9418. (org-defkey map (kbd "RET") 'org-calendar-select)
  9419. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9420. 'org-calendar-select-mouse)
  9421. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9422. 'org-calendar-select-mouse)
  9423. (org-defkey minibuffer-local-map [(meta shift left)]
  9424. (lambda () (interactive)
  9425. (org-eval-in-calendar '(calendar-backward-month 1))))
  9426. (org-defkey minibuffer-local-map [(meta shift right)]
  9427. (lambda () (interactive)
  9428. (org-eval-in-calendar '(calendar-forward-month 1))))
  9429. (org-defkey minibuffer-local-map [(meta shift up)]
  9430. (lambda () (interactive)
  9431. (org-eval-in-calendar '(calendar-backward-year 1))))
  9432. (org-defkey minibuffer-local-map [(meta shift down)]
  9433. (lambda () (interactive)
  9434. (org-eval-in-calendar '(calendar-forward-year 1))))
  9435. (org-defkey minibuffer-local-map [(shift up)]
  9436. (lambda () (interactive)
  9437. (org-eval-in-calendar '(calendar-backward-week 1))))
  9438. (org-defkey minibuffer-local-map [(shift down)]
  9439. (lambda () (interactive)
  9440. (org-eval-in-calendar '(calendar-forward-week 1))))
  9441. (org-defkey minibuffer-local-map [(shift left)]
  9442. (lambda () (interactive)
  9443. (org-eval-in-calendar '(calendar-backward-day 1))))
  9444. (org-defkey minibuffer-local-map [(shift right)]
  9445. (lambda () (interactive)
  9446. (org-eval-in-calendar '(calendar-forward-day 1))))
  9447. (org-defkey minibuffer-local-map ">"
  9448. (lambda () (interactive)
  9449. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9450. (org-defkey minibuffer-local-map "<"
  9451. (lambda () (interactive)
  9452. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9453. (unwind-protect
  9454. (progn
  9455. (use-local-map map)
  9456. (add-hook 'post-command-hook 'org-read-date-display)
  9457. (setq org-ans0 (read-string prompt default-input nil nil))
  9458. ;; org-ans0: from prompt
  9459. ;; org-ans1: from mouse click
  9460. ;; org-ans2: from calendar motion
  9461. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9462. (remove-hook 'post-command-hook 'org-read-date-display)
  9463. (use-local-map old-map)
  9464. (when org-read-date-overlay
  9465. (org-delete-overlay org-read-date-overlay)
  9466. (setq org-read-date-overlay nil)))))))
  9467. (t ; Naked prompt only
  9468. (unwind-protect
  9469. (setq ans (read-string prompt default-input nil timestr))
  9470. (when org-read-date-overlay
  9471. (org-delete-overlay org-read-date-overlay)
  9472. (setq org-read-date-overlay nil)))))
  9473. (setq final (org-read-date-analyze ans def defdecode))
  9474. (if to-time
  9475. (apply 'encode-time final)
  9476. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9477. (format "%04d-%02d-%02d %02d:%02d"
  9478. (nth 5 final) (nth 4 final) (nth 3 final)
  9479. (nth 2 final) (nth 1 final))
  9480. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9481. (defvar def)
  9482. (defvar defdecode)
  9483. (defvar with-time)
  9484. (defun org-read-date-display ()
  9485. "Display the currrent date prompt interpretation in the minibuffer."
  9486. (when org-read-date-display-live
  9487. (when org-read-date-overlay
  9488. (org-delete-overlay org-read-date-overlay))
  9489. (let ((p (point)))
  9490. (end-of-line 1)
  9491. (while (not (equal (buffer-substring
  9492. (max (point-min) (- (point) 4)) (point))
  9493. " "))
  9494. (insert " "))
  9495. (goto-char p))
  9496. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9497. " " (or org-ans1 org-ans2)))
  9498. (org-end-time-was-given nil)
  9499. (f (org-read-date-analyze ans def defdecode))
  9500. (fmts (if org-dcst
  9501. org-time-stamp-custom-formats
  9502. org-time-stamp-formats))
  9503. (fmt (if (or with-time
  9504. (and (boundp 'org-time-was-given) org-time-was-given))
  9505. (cdr fmts)
  9506. (car fmts)))
  9507. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9508. (when (and org-end-time-was-given
  9509. (string-match org-plain-time-of-day-regexp txt))
  9510. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9511. org-end-time-was-given
  9512. (substring txt (match-end 0)))))
  9513. (setq org-read-date-overlay
  9514. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9515. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9516. (defun org-read-date-analyze (ans def defdecode)
  9517. "Analyze the combined answer of the date prompt."
  9518. ;; FIXME: cleanup and comment
  9519. (let (delta deltan deltaw deltadef year month day
  9520. hour minute second wday pm h2 m2 tl wday1
  9521. iso-year iso-weekday iso-week iso-year iso-date)
  9522. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9523. (setq ans "+0"))
  9524. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9525. (setq ans (replace-match "" t t ans)
  9526. deltan (car delta)
  9527. deltaw (nth 1 delta)
  9528. deltadef (nth 2 delta)))
  9529. ;; Check if there is an iso week date in there
  9530. ;; If yes, sore the info and ostpone interpreting it until the rest
  9531. ;; of the parsing is done
  9532. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9533. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9534. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9535. iso-week (string-to-number (match-string 2 ans)))
  9536. (setq ans (replace-match "" t t ans)))
  9537. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9538. (when (string-match
  9539. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9540. (setq year (if (match-end 2)
  9541. (string-to-number (match-string 2 ans))
  9542. (string-to-number (format-time-string "%Y")))
  9543. month (string-to-number (match-string 3 ans))
  9544. day (string-to-number (match-string 4 ans)))
  9545. (if (< year 100) (setq year (+ 2000 year)))
  9546. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9547. t nil ans)))
  9548. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9549. ;; If there is a time with am/pm, and *no* time without it, we convert
  9550. ;; so that matching will be successful.
  9551. (loop for i from 1 to 2 do ; twice, for end time as well
  9552. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9553. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9554. (setq hour (string-to-number (match-string 1 ans))
  9555. minute (if (match-end 3)
  9556. (string-to-number (match-string 3 ans))
  9557. 0)
  9558. pm (equal ?p
  9559. (string-to-char (downcase (match-string 4 ans)))))
  9560. (if (and (= hour 12) (not pm))
  9561. (setq hour 0)
  9562. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9563. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9564. t t ans))))
  9565. ;; Check if a time range is given as a duration
  9566. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9567. (setq hour (string-to-number (match-string 1 ans))
  9568. h2 (+ hour (string-to-number (match-string 3 ans)))
  9569. minute (string-to-number (match-string 2 ans))
  9570. m2 (+ minute (if (match-end 5) (string-to-number
  9571. (match-string 5 ans))0)))
  9572. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9573. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9574. t t ans)))
  9575. ;; Check if there is a time range
  9576. (when (boundp 'org-end-time-was-given)
  9577. (setq org-time-was-given nil)
  9578. (when (and (string-match org-plain-time-of-day-regexp ans)
  9579. (match-end 8))
  9580. (setq org-end-time-was-given (match-string 8 ans))
  9581. (setq ans (concat (substring ans 0 (match-beginning 7))
  9582. (substring ans (match-end 7))))))
  9583. (setq tl (parse-time-string ans)
  9584. day (or (nth 3 tl) (nth 3 defdecode))
  9585. month (or (nth 4 tl)
  9586. (if (and org-read-date-prefer-future
  9587. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9588. (1+ (nth 4 defdecode))
  9589. (nth 4 defdecode)))
  9590. year (or (nth 5 tl)
  9591. (if (and org-read-date-prefer-future
  9592. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9593. (1+ (nth 5 defdecode))
  9594. (nth 5 defdecode)))
  9595. hour (or (nth 2 tl) (nth 2 defdecode))
  9596. minute (or (nth 1 tl) (nth 1 defdecode))
  9597. second (or (nth 0 tl) 0)
  9598. wday (nth 6 tl))
  9599. ;; Special date definitions below
  9600. (cond
  9601. (iso-week
  9602. ;; There was an iso week
  9603. (setq year (or iso-year year)
  9604. day (or iso-weekday wday 1)
  9605. wday nil ; to make sure that the trigger below does not match
  9606. iso-date (calendar-gregorian-from-absolute
  9607. (calendar-absolute-from-iso
  9608. (list iso-week day year))))
  9609. ; FIXME: Should we also push ISO weeks into the future?
  9610. ; (when (and org-read-date-prefer-future
  9611. ; (not iso-year)
  9612. ; (< (calendar-absolute-from-gregorian iso-date)
  9613. ; (time-to-days (current-time))))
  9614. ; (setq year (1+ year)
  9615. ; iso-date (calendar-gregorian-from-absolute
  9616. ; (calendar-absolute-from-iso
  9617. ; (list iso-week day year)))))
  9618. (setq month (car iso-date)
  9619. year (nth 2 iso-date)
  9620. day (nth 1 iso-date)))
  9621. (deltan
  9622. (unless deltadef
  9623. (let ((now (decode-time (current-time))))
  9624. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9625. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9626. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9627. ((equal deltaw "m") (setq month (+ month deltan)))
  9628. ((equal deltaw "y") (setq year (+ year deltan)))))
  9629. ((and wday (not (nth 3 tl)))
  9630. ;; Weekday was given, but no day, so pick that day in the week
  9631. ;; on or after the derived date.
  9632. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9633. (unless (equal wday wday1)
  9634. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9635. (if (and (boundp 'org-time-was-given)
  9636. (nth 2 tl))
  9637. (setq org-time-was-given t))
  9638. (if (< year 100) (setq year (+ 2000 year)))
  9639. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9640. (list second minute hour day month year)))
  9641. (defvar parse-time-weekdays)
  9642. (defun org-read-date-get-relative (s today default)
  9643. "Check string S for special relative date string.
  9644. TODAY and DEFAULT are internal times, for today and for a default.
  9645. Return shift list (N what def-flag)
  9646. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9647. N is the number of WHATs to shift.
  9648. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9649. the DEFAULT date rather than TODAY."
  9650. (when (and
  9651. (string-match
  9652. (concat
  9653. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9654. "\\([0-9]+\\)?"
  9655. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9656. "\\([ \t]\\|$\\)") s)
  9657. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9658. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9659. (string-to-char (substring (match-string 1 s) -1))
  9660. ?+))
  9661. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9662. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9663. (what (if (match-end 3) (match-string 3 s) "d"))
  9664. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9665. (date (if rel default today))
  9666. (wday (nth 6 (decode-time date)))
  9667. delta)
  9668. (if wday1
  9669. (progn
  9670. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9671. (if (= dir ?-) (setq delta (- delta 7)))
  9672. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9673. (list delta "d" rel))
  9674. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9675. (defun org-eval-in-calendar (form &optional keepdate)
  9676. "Eval FORM in the calendar window and return to current window.
  9677. Also, store the cursor date in variable org-ans2."
  9678. (let ((sw (selected-window)))
  9679. (select-window (get-buffer-window "*Calendar*"))
  9680. (eval form)
  9681. (when (and (not keepdate) (calendar-cursor-to-date))
  9682. (let* ((date (calendar-cursor-to-date))
  9683. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9684. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9685. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9686. (select-window sw)))
  9687. ; ;; Update the prompt to show new default date
  9688. ; (save-excursion
  9689. ; (goto-char (point-min))
  9690. ; (when (and org-ans2
  9691. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9692. ; (get-text-property (match-end 0) 'field))
  9693. ; (let ((inhibit-read-only t))
  9694. ; (replace-match (concat "[" org-ans2 "]") t t)
  9695. ; (add-text-properties (point-min) (1+ (match-end 0))
  9696. ; (text-properties-at (1+ (point-min)))))))))
  9697. (defun org-calendar-select ()
  9698. "Return to `org-read-date' with the date currently selected.
  9699. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9700. (interactive)
  9701. (when (calendar-cursor-to-date)
  9702. (let* ((date (calendar-cursor-to-date))
  9703. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9704. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9705. (if (active-minibuffer-window) (exit-minibuffer))))
  9706. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9707. "Insert a date stamp for the date given by the internal TIME.
  9708. WITH-HM means, use the stamp format that includes the time of the day.
  9709. INACTIVE means use square brackets instead of angular ones, so that the
  9710. stamp will not contribute to the agenda.
  9711. PRE and POST are optional strings to be inserted before and after the
  9712. stamp.
  9713. The command returns the inserted time stamp."
  9714. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9715. stamp)
  9716. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9717. (insert-before-markers (or pre ""))
  9718. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9719. (when (listp extra)
  9720. (setq extra (car extra))
  9721. (if (and (stringp extra)
  9722. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9723. (setq extra (format "-%02d:%02d"
  9724. (string-to-number (match-string 1 extra))
  9725. (string-to-number (match-string 2 extra))))
  9726. (setq extra nil)))
  9727. (when extra
  9728. (backward-char 1)
  9729. (insert-before-markers extra)
  9730. (forward-char 1))
  9731. (insert-before-markers (or post ""))
  9732. stamp))
  9733. (defun org-toggle-time-stamp-overlays ()
  9734. "Toggle the use of custom time stamp formats."
  9735. (interactive)
  9736. (setq org-display-custom-times (not org-display-custom-times))
  9737. (unless org-display-custom-times
  9738. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9739. (while (setq p (next-single-property-change p 'display))
  9740. (if (and (get-text-property p 'display)
  9741. (eq (get-text-property p 'face) 'org-date))
  9742. (remove-text-properties
  9743. p (setq p (next-single-property-change p 'display))
  9744. '(display t))))
  9745. (set-buffer-modified-p bmp)))
  9746. (if (featurep 'xemacs)
  9747. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9748. (org-restart-font-lock)
  9749. (setq org-table-may-need-update t)
  9750. (if org-display-custom-times
  9751. (message "Time stamps are overlayed with custom format")
  9752. (message "Time stamp overlays removed")))
  9753. (defun org-display-custom-time (beg end)
  9754. "Overlay modified time stamp format over timestamp between BEG and END."
  9755. (let* ((ts (buffer-substring beg end))
  9756. t1 w1 with-hm tf time str w2 (off 0))
  9757. (save-match-data
  9758. (setq t1 (org-parse-time-string ts t))
  9759. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9760. (setq off (- (match-end 0) (match-beginning 0)))))
  9761. (setq end (- end off))
  9762. (setq w1 (- end beg)
  9763. with-hm (and (nth 1 t1) (nth 2 t1))
  9764. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9765. time (org-fix-decoded-time t1)
  9766. str (org-add-props
  9767. (format-time-string
  9768. (substring tf 1 -1) (apply 'encode-time time))
  9769. nil 'mouse-face 'highlight)
  9770. w2 (length str))
  9771. (if (not (= w2 w1))
  9772. (add-text-properties (1+ beg) (+ 2 beg)
  9773. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9774. (if (featurep 'xemacs)
  9775. (progn
  9776. (put-text-property beg end 'invisible t)
  9777. (put-text-property beg end 'end-glyph (make-glyph str)))
  9778. (put-text-property beg end 'display str))))
  9779. (defun org-translate-time (string)
  9780. "Translate all timestamps in STRING to custom format.
  9781. But do this only if the variable `org-display-custom-times' is set."
  9782. (when org-display-custom-times
  9783. (save-match-data
  9784. (let* ((start 0)
  9785. (re org-ts-regexp-both)
  9786. t1 with-hm inactive tf time str beg end)
  9787. (while (setq start (string-match re string start))
  9788. (setq beg (match-beginning 0)
  9789. end (match-end 0)
  9790. t1 (save-match-data
  9791. (org-parse-time-string (substring string beg end) t))
  9792. with-hm (and (nth 1 t1) (nth 2 t1))
  9793. inactive (equal (substring string beg (1+ beg)) "[")
  9794. tf (funcall (if with-hm 'cdr 'car)
  9795. org-time-stamp-custom-formats)
  9796. time (org-fix-decoded-time t1)
  9797. str (format-time-string
  9798. (concat
  9799. (if inactive "[" "<") (substring tf 1 -1)
  9800. (if inactive "]" ">"))
  9801. (apply 'encode-time time))
  9802. string (replace-match str t t string)
  9803. start (+ start (length str)))))))
  9804. string)
  9805. (defun org-fix-decoded-time (time)
  9806. "Set 0 instead of nil for the first 6 elements of time.
  9807. Don't touch the rest."
  9808. (let ((n 0))
  9809. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9810. (defun org-days-to-time (timestamp-string)
  9811. "Difference between TIMESTAMP-STRING and now in days."
  9812. (- (time-to-days (org-time-string-to-time timestamp-string))
  9813. (time-to-days (current-time))))
  9814. (defun org-deadline-close (timestamp-string &optional ndays)
  9815. "Is the time in TIMESTAMP-STRING close to the current date?"
  9816. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9817. (and (< (org-days-to-time timestamp-string) ndays)
  9818. (not (org-entry-is-done-p))))
  9819. (defun org-get-wdays (ts)
  9820. "Get the deadline lead time appropriate for timestring TS."
  9821. (cond
  9822. ((<= org-deadline-warning-days 0)
  9823. ;; 0 or negative, enforce this value no matter what
  9824. (- org-deadline-warning-days))
  9825. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9826. ;; lead time is specified.
  9827. (floor (* (string-to-number (match-string 1 ts))
  9828. (cdr (assoc (match-string 2 ts)
  9829. '(("d" . 1) ("w" . 7)
  9830. ("m" . 30.4) ("y" . 365.25)))))))
  9831. ;; go for the default.
  9832. (t org-deadline-warning-days)))
  9833. (defun org-calendar-select-mouse (ev)
  9834. "Return to `org-read-date' with the date currently selected.
  9835. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9836. (interactive "e")
  9837. (mouse-set-point ev)
  9838. (when (calendar-cursor-to-date)
  9839. (let* ((date (calendar-cursor-to-date))
  9840. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9841. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9842. (if (active-minibuffer-window) (exit-minibuffer))))
  9843. (defun org-check-deadlines (ndays)
  9844. "Check if there are any deadlines due or past due.
  9845. A deadline is considered due if it happens within `org-deadline-warning-days'
  9846. days from today's date. If the deadline appears in an entry marked DONE,
  9847. it is not shown. The prefix arg NDAYS can be used to test that many
  9848. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9849. (interactive "P")
  9850. (let* ((org-warn-days
  9851. (cond
  9852. ((equal ndays '(4)) 100000)
  9853. (ndays (prefix-numeric-value ndays))
  9854. (t (abs org-deadline-warning-days))))
  9855. (case-fold-search nil)
  9856. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9857. (callback
  9858. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9859. (message "%d deadlines past-due or due within %d days"
  9860. (org-occur regexp nil callback)
  9861. org-warn-days)))
  9862. (defun org-check-before-date (date)
  9863. "Check if there are deadlines or scheduled entries before DATE."
  9864. (interactive (list (org-read-date)))
  9865. (let ((case-fold-search nil)
  9866. (regexp (concat "\\<\\(" org-deadline-string
  9867. "\\|" org-scheduled-string
  9868. "\\) *<\\([^>]+\\)>"))
  9869. (callback
  9870. (lambda () (time-less-p
  9871. (org-time-string-to-time (match-string 2))
  9872. (org-time-string-to-time date)))))
  9873. (message "%d entries before %s"
  9874. (org-occur regexp nil callback) date)))
  9875. (defun org-evaluate-time-range (&optional to-buffer)
  9876. "Evaluate a time range by computing the difference between start and end.
  9877. Normally the result is just printed in the echo area, but with prefix arg
  9878. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9879. If the time range is actually in a table, the result is inserted into the
  9880. next column.
  9881. For time difference computation, a year is assumed to be exactly 365
  9882. days in order to avoid rounding problems."
  9883. (interactive "P")
  9884. (or
  9885. (org-clock-update-time-maybe)
  9886. (save-excursion
  9887. (unless (org-at-date-range-p t)
  9888. (goto-char (point-at-bol))
  9889. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9890. (if (not (org-at-date-range-p t))
  9891. (error "Not at a time-stamp range, and none found in current line")))
  9892. (let* ((ts1 (match-string 1))
  9893. (ts2 (match-string 2))
  9894. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9895. (match-end (match-end 0))
  9896. (time1 (org-time-string-to-time ts1))
  9897. (time2 (org-time-string-to-time ts2))
  9898. (t1 (time-to-seconds time1))
  9899. (t2 (time-to-seconds time2))
  9900. (diff (abs (- t2 t1)))
  9901. (negative (< (- t2 t1) 0))
  9902. ;; (ys (floor (* 365 24 60 60)))
  9903. (ds (* 24 60 60))
  9904. (hs (* 60 60))
  9905. (fy "%dy %dd %02d:%02d")
  9906. (fy1 "%dy %dd")
  9907. (fd "%dd %02d:%02d")
  9908. (fd1 "%dd")
  9909. (fh "%02d:%02d")
  9910. y d h m align)
  9911. (if havetime
  9912. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9913. y 0
  9914. d (floor (/ diff ds)) diff (mod diff ds)
  9915. h (floor (/ diff hs)) diff (mod diff hs)
  9916. m (floor (/ diff 60)))
  9917. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9918. y 0
  9919. d (floor (+ (/ diff ds) 0.5))
  9920. h 0 m 0))
  9921. (if (not to-buffer)
  9922. (message "%s" (org-make-tdiff-string y d h m))
  9923. (if (org-at-table-p)
  9924. (progn
  9925. (goto-char match-end)
  9926. (setq align t)
  9927. (and (looking-at " *|") (goto-char (match-end 0))))
  9928. (goto-char match-end))
  9929. (if (looking-at
  9930. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9931. (replace-match ""))
  9932. (if negative (insert " -"))
  9933. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9934. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9935. (insert " " (format fh h m))))
  9936. (if align (org-table-align))
  9937. (message "Time difference inserted")))))
  9938. (defun org-make-tdiff-string (y d h m)
  9939. (let ((fmt "")
  9940. (l nil))
  9941. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9942. l (push y l)))
  9943. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9944. l (push d l)))
  9945. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9946. l (push h l)))
  9947. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9948. l (push m l)))
  9949. (apply 'format fmt (nreverse l))))
  9950. (defun org-time-string-to-time (s)
  9951. (apply 'encode-time (org-parse-time-string s)))
  9952. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9953. "Convert a time stamp to an absolute day number.
  9954. If there is a specifyer for a cyclic time stamp, get the closest date to
  9955. DAYNR.
  9956. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9957. (cond
  9958. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9959. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9960. daynr
  9961. (+ daynr 1000)))
  9962. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9963. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9964. (time-to-days (current-time))) (match-string 0 s)
  9965. prefer show-all))
  9966. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9967. (defun org-days-to-iso-week (days)
  9968. "Return the iso week number."
  9969. (require 'cal-iso)
  9970. (car (calendar-iso-from-absolute days)))
  9971. (defun org-small-year-to-year (year)
  9972. "Convert 2-digit years into 4-digit years.
  9973. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9974. The year 2000 cannot be abbreviated. Any year lager than 99
  9975. is retrned unchanged."
  9976. (if (< year 38)
  9977. (setq year (+ 2000 year))
  9978. (if (< year 100)
  9979. (setq year (+ 1900 year))))
  9980. year)
  9981. (defun org-time-from-absolute (d)
  9982. "Return the time corresponding to date D.
  9983. D may be an absolute day number, or a calendar-type list (month day year)."
  9984. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9985. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9986. (defun org-calendar-holiday ()
  9987. "List of holidays, for Diary display in Org-mode."
  9988. (require 'holidays)
  9989. (let ((hl (funcall
  9990. (if (fboundp 'calendar-check-holidays)
  9991. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9992. (if hl (mapconcat 'identity hl "; "))))
  9993. (defun org-diary-sexp-entry (sexp entry date)
  9994. "Process a SEXP diary ENTRY for DATE."
  9995. (require 'diary-lib)
  9996. (let ((result (if calendar-debug-sexp
  9997. (let ((stack-trace-on-error t))
  9998. (eval (car (read-from-string sexp))))
  9999. (condition-case nil
  10000. (eval (car (read-from-string sexp)))
  10001. (error
  10002. (beep)
  10003. (message "Bad sexp at line %d in %s: %s"
  10004. (org-current-line)
  10005. (buffer-file-name) sexp)
  10006. (sleep-for 2))))))
  10007. (cond ((stringp result) result)
  10008. ((and (consp result)
  10009. (stringp (cdr result))) (cdr result))
  10010. (result entry)
  10011. (t nil))))
  10012. (defun org-diary-to-ical-string (frombuf)
  10013. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10014. This uses the icalendar.el library."
  10015. (let* ((tmpdir (if (featurep 'xemacs)
  10016. (temp-directory)
  10017. temporary-file-directory))
  10018. (tmpfile (make-temp-name
  10019. (expand-file-name "orgics" tmpdir)))
  10020. buf rtn b e)
  10021. (save-excursion
  10022. (set-buffer frombuf)
  10023. (icalendar-export-region (point-min) (point-max) tmpfile)
  10024. (setq buf (find-buffer-visiting tmpfile))
  10025. (set-buffer buf)
  10026. (goto-char (point-min))
  10027. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10028. (setq b (match-beginning 0)))
  10029. (goto-char (point-max))
  10030. (if (re-search-backward "^END:VEVENT" nil t)
  10031. (setq e (match-end 0)))
  10032. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10033. (kill-buffer buf)
  10034. (kill-buffer frombuf)
  10035. (delete-file tmpfile)
  10036. rtn))
  10037. (defun org-closest-date (start current change prefer show-all)
  10038. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10039. When PREFER is `past' return a date that is either CURRENT or past.
  10040. When PREFER is `future', return a date that is either CURRENT or future.
  10041. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10042. ;; Make the proper lists from the dates
  10043. (catch 'exit
  10044. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10045. dn dw sday cday n1 n2
  10046. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10047. (setq start (org-date-to-gregorian start)
  10048. current (org-date-to-gregorian
  10049. (if show-all
  10050. current
  10051. (time-to-days (current-time))))
  10052. sday (calendar-absolute-from-gregorian start)
  10053. cday (calendar-absolute-from-gregorian current))
  10054. (if (<= cday sday) (throw 'exit sday))
  10055. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10056. (setq dn (string-to-number (match-string 1 change))
  10057. dw (cdr (assoc (match-string 2 change) a1)))
  10058. (error "Invalid change specifyer: %s" change))
  10059. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10060. (cond
  10061. ((eq dw 'day)
  10062. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10063. n2 (+ n1 dn)))
  10064. ((eq dw 'year)
  10065. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10066. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10067. (setq date1 (list m d y1)
  10068. n1 (calendar-absolute-from-gregorian date1)
  10069. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10070. n2 (calendar-absolute-from-gregorian date2)))
  10071. ((eq dw 'month)
  10072. ;; approx number of month between the tow dates
  10073. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10074. ;; How often does dn fit in there?
  10075. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10076. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10077. m (+ m nm)
  10078. ny (floor (/ m 12))
  10079. y (+ y ny)
  10080. m (- m (* ny 12)))
  10081. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10082. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10083. (setq m2 (+ m dn) y2 y)
  10084. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10085. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10086. (while (< n2 cday)
  10087. (setq n1 n2 m m2 y y2)
  10088. (setq m2 (+ m dn) y2 y)
  10089. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10090. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10091. (if show-all
  10092. (cond
  10093. ((eq prefer 'past) n1)
  10094. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10095. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10096. (cond
  10097. ((eq prefer 'past) n1)
  10098. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10099. (t (if (= cday n1) n1 n2)))))))
  10100. (defun org-date-to-gregorian (date)
  10101. "Turn any specification of DATE into a gregorian date for the calendar."
  10102. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10103. ((and (listp date) (= (length date) 3)) date)
  10104. ((stringp date)
  10105. (setq date (org-parse-time-string date))
  10106. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10107. ((listp date)
  10108. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10109. (defun org-parse-time-string (s &optional nodefault)
  10110. "Parse the standard Org-mode time string.
  10111. This should be a lot faster than the normal `parse-time-string'.
  10112. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10113. hour and minute fields will be nil if not given."
  10114. (if (string-match org-ts-regexp0 s)
  10115. (list 0
  10116. (if (or (match-beginning 8) (not nodefault))
  10117. (string-to-number (or (match-string 8 s) "0")))
  10118. (if (or (match-beginning 7) (not nodefault))
  10119. (string-to-number (or (match-string 7 s) "0")))
  10120. (string-to-number (match-string 4 s))
  10121. (string-to-number (match-string 3 s))
  10122. (string-to-number (match-string 2 s))
  10123. nil nil nil)
  10124. (make-list 9 0)))
  10125. (defun org-timestamp-up (&optional arg)
  10126. "Increase the date item at the cursor by one.
  10127. If the cursor is on the year, change the year. If it is on the month or
  10128. the day, change that.
  10129. With prefix ARG, change by that many units."
  10130. (interactive "p")
  10131. (org-timestamp-change (prefix-numeric-value arg)))
  10132. (defun org-timestamp-down (&optional arg)
  10133. "Decrease the date item at the cursor by one.
  10134. If the cursor is on the year, change the year. If it is on the month or
  10135. the day, change that.
  10136. With prefix ARG, change by that many units."
  10137. (interactive "p")
  10138. (org-timestamp-change (- (prefix-numeric-value arg))))
  10139. (defun org-timestamp-up-day (&optional arg)
  10140. "Increase the date in the time stamp by one day.
  10141. With prefix ARG, change that many days."
  10142. (interactive "p")
  10143. (if (and (not (org-at-timestamp-p t))
  10144. (org-on-heading-p))
  10145. (org-todo 'up)
  10146. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10147. (defun org-timestamp-down-day (&optional arg)
  10148. "Decrease the date in the time stamp by one day.
  10149. With prefix ARG, change that many days."
  10150. (interactive "p")
  10151. (if (and (not (org-at-timestamp-p t))
  10152. (org-on-heading-p))
  10153. (org-todo 'down)
  10154. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10155. (defun org-at-timestamp-p (&optional inactive-ok)
  10156. "Determine if the cursor is in or at a timestamp."
  10157. (interactive)
  10158. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10159. (pos (point))
  10160. (ans (or (looking-at tsr)
  10161. (save-excursion
  10162. (skip-chars-backward "^[<\n\r\t")
  10163. (if (> (point) (point-min)) (backward-char 1))
  10164. (and (looking-at tsr)
  10165. (> (- (match-end 0) pos) -1))))))
  10166. (and ans
  10167. (boundp 'org-ts-what)
  10168. (setq org-ts-what
  10169. (cond
  10170. ((= pos (match-beginning 0)) 'bracket)
  10171. ((= pos (1- (match-end 0))) 'bracket)
  10172. ((org-pos-in-match-range pos 2) 'year)
  10173. ((org-pos-in-match-range pos 3) 'month)
  10174. ((org-pos-in-match-range pos 7) 'hour)
  10175. ((org-pos-in-match-range pos 8) 'minute)
  10176. ((or (org-pos-in-match-range pos 4)
  10177. (org-pos-in-match-range pos 5)) 'day)
  10178. ((and (> pos (or (match-end 8) (match-end 5)))
  10179. (< pos (match-end 0)))
  10180. (- pos (or (match-end 8) (match-end 5))))
  10181. (t 'day))))
  10182. ans))
  10183. (defun org-toggle-timestamp-type ()
  10184. "Toggle the type (<active> or [inactive]) of a time stamp."
  10185. (interactive)
  10186. (when (org-at-timestamp-p t)
  10187. (save-excursion
  10188. (goto-char (match-beginning 0))
  10189. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10190. (goto-char (1- (match-end 0)))
  10191. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10192. (message "Timestamp is now %sactive"
  10193. (if (equal (char-before) ?>) "in" ""))))
  10194. (defun org-timestamp-change (n &optional what)
  10195. "Change the date in the time stamp at point.
  10196. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10197. `year', `minute', `second'. If WHAT is not given, the cursor position
  10198. in the timestamp determines what will be changed."
  10199. (let ((pos (point))
  10200. with-hm inactive
  10201. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10202. org-ts-what
  10203. extra rem
  10204. ts time time0)
  10205. (if (not (org-at-timestamp-p t))
  10206. (error "Not at a timestamp"))
  10207. (if (and (not what) (eq org-ts-what 'bracket))
  10208. (org-toggle-timestamp-type)
  10209. (if (and (not what) (not (eq org-ts-what 'day))
  10210. org-display-custom-times
  10211. (get-text-property (point) 'display)
  10212. (not (get-text-property (1- (point)) 'display)))
  10213. (setq org-ts-what 'day))
  10214. (setq org-ts-what (or what org-ts-what)
  10215. inactive (= (char-after (match-beginning 0)) ?\[)
  10216. ts (match-string 0))
  10217. (replace-match "")
  10218. (if (string-match
  10219. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10220. ts)
  10221. (setq extra (match-string 1 ts)))
  10222. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10223. (setq with-hm t))
  10224. (setq time0 (org-parse-time-string ts))
  10225. (when (and (eq org-ts-what 'minute)
  10226. (eq current-prefix-arg nil))
  10227. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10228. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10229. (setcar (cdr time0) (+ (nth 1 time0)
  10230. (if (> n 0) (- rem) (- dm rem))))))
  10231. (setq time
  10232. (encode-time (or (car time0) 0)
  10233. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10234. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10235. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10236. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10237. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10238. (nthcdr 6 time0)))
  10239. (when (integerp org-ts-what)
  10240. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10241. (if (eq what 'calendar)
  10242. (let ((cal-date (org-get-date-from-calendar)))
  10243. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10244. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10245. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10246. (setcar time0 (or (car time0) 0))
  10247. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10248. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10249. (setq time (apply 'encode-time time0))))
  10250. (setq org-last-changed-timestamp
  10251. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10252. (org-clock-update-time-maybe)
  10253. (goto-char pos)
  10254. ;; Try to recenter the calendar window, if any
  10255. (if (and org-calendar-follow-timestamp-change
  10256. (get-buffer-window "*Calendar*" t)
  10257. (memq org-ts-what '(day month year)))
  10258. (org-recenter-calendar (time-to-days time))))))
  10259. (defun org-modify-ts-extra (s pos n dm)
  10260. "Change the different parts of the lead-time and repeat fields in timestamp."
  10261. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10262. ng h m new rem)
  10263. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10264. (cond
  10265. ((or (org-pos-in-match-range pos 2)
  10266. (org-pos-in-match-range pos 3))
  10267. (setq m (string-to-number (match-string 3 s))
  10268. h (string-to-number (match-string 2 s)))
  10269. (if (org-pos-in-match-range pos 2)
  10270. (setq h (+ h n))
  10271. (setq n (* dm (org-no-warnings (signum n))))
  10272. (when (not (= 0 (setq rem (% m dm))))
  10273. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10274. (setq m (+ m n)))
  10275. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10276. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10277. (setq h (min 24 (max 0 h)))
  10278. (setq ng 1 new (format "-%02d:%02d" h m)))
  10279. ((org-pos-in-match-range pos 6)
  10280. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10281. ((org-pos-in-match-range pos 5)
  10282. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10283. ((org-pos-in-match-range pos 9)
  10284. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10285. ((org-pos-in-match-range pos 8)
  10286. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10287. (when ng
  10288. (setq s (concat
  10289. (substring s 0 (match-beginning ng))
  10290. new
  10291. (substring s (match-end ng))))))
  10292. s))
  10293. (defun org-recenter-calendar (date)
  10294. "If the calendar is visible, recenter it to DATE."
  10295. (let* ((win (selected-window))
  10296. (cwin (get-buffer-window "*Calendar*" t))
  10297. (calendar-move-hook nil))
  10298. (when cwin
  10299. (select-window cwin)
  10300. (calendar-goto-date (if (listp date) date
  10301. (calendar-gregorian-from-absolute date)))
  10302. (select-window win))))
  10303. (defun org-goto-calendar (&optional arg)
  10304. "Go to the Emacs calendar at the current date.
  10305. If there is a time stamp in the current line, go to that date.
  10306. A prefix ARG can be used to force the current date."
  10307. (interactive "P")
  10308. (let ((tsr org-ts-regexp) diff
  10309. (calendar-move-hook nil)
  10310. (calendar-view-holidays-initially-flag nil)
  10311. (view-calendar-holidays-initially nil)
  10312. (calendar-view-diary-initially-flag nil)
  10313. (view-diary-entries-initially nil))
  10314. (if (or (org-at-timestamp-p)
  10315. (save-excursion
  10316. (beginning-of-line 1)
  10317. (looking-at (concat ".*" tsr))))
  10318. (let ((d1 (time-to-days (current-time)))
  10319. (d2 (time-to-days
  10320. (org-time-string-to-time (match-string 1)))))
  10321. (setq diff (- d2 d1))))
  10322. (calendar)
  10323. (calendar-goto-today)
  10324. (if (and diff (not arg)) (calendar-forward-day diff))))
  10325. (defun org-get-date-from-calendar ()
  10326. "Return a list (month day year) of date at point in calendar."
  10327. (with-current-buffer "*Calendar*"
  10328. (save-match-data
  10329. (calendar-cursor-to-date))))
  10330. (defun org-date-from-calendar ()
  10331. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10332. If there is already a time stamp at the cursor position, update it."
  10333. (interactive)
  10334. (if (org-at-timestamp-p t)
  10335. (org-timestamp-change 0 'calendar)
  10336. (let ((cal-date (org-get-date-from-calendar)))
  10337. (org-insert-time-stamp
  10338. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10339. (defun org-minutes-to-hh:mm-string (m)
  10340. "Compute H:MM from a number of minutes."
  10341. (let ((h (/ m 60)))
  10342. (setq m (- m (* 60 h)))
  10343. (format "%d:%02d" h m)))
  10344. (defun org-hh:mm-string-to-minutes (s)
  10345. "Convert a string H:MM to a number of minutes."
  10346. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10347. (+ (* (string-to-number (match-string 1 s)) 60)
  10348. (string-to-number (match-string 2 s)))
  10349. 0))
  10350. ;;;; Agenda files
  10351. ;;;###autoload
  10352. (defun org-iswitchb (&optional arg)
  10353. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10354. With a prefix argument, restrict available to files.
  10355. With two prefix arguments, restrict available buffers to agenda files.
  10356. Due to some yet unresolved reason, global function
  10357. `iswitchb-mode' needs to be active for this function to work."
  10358. (interactive "P")
  10359. (require 'iswitchb)
  10360. (let ((enabled iswitchb-mode) blist)
  10361. (or enabled (iswitchb-mode 1))
  10362. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10363. ((equal arg '(16)) (org-buffer-list 'agenda))
  10364. (t (org-buffer-list))))
  10365. (unwind-protect
  10366. (let ((iswitchb-make-buflist-hook
  10367. (lambda ()
  10368. (setq iswitchb-temp-buflist
  10369. (mapcar 'buffer-name blist)))))
  10370. (switch-to-buffer
  10371. (iswitchb-read-buffer
  10372. "Switch-to: " nil t))
  10373. (or enabled (iswitchb-mode -1))))))
  10374. (defun org-buffer-list (&optional predicate tmp)
  10375. "Return a list of Org buffers.
  10376. PREDICATE can be either 'export, 'files or 'agenda.
  10377. 'export restrict the list to Export buffers.
  10378. 'files restrict the list to buffers visiting Org files.
  10379. 'agenda restrict the list to buffers visiting agenda files.
  10380. If TMP is non-nil, don't include temporary buffers."
  10381. (let (filter blist)
  10382. (setq filter
  10383. (cond ((eq predicate 'files) "\.org$")
  10384. ((eq predicate 'export) "\*Org .*Export")
  10385. (t "\*Org \\|\.org$")))
  10386. (setq blist
  10387. (mapcar
  10388. (lambda(b)
  10389. (let ((bname (buffer-name b))
  10390. (bfile (buffer-file-name b)))
  10391. (if (and (string-match filter bname)
  10392. (if (eq predicate 'agenda)
  10393. (member bfile
  10394. (mapcar (lambda(f) (file-truename f))
  10395. org-agenda-files)) t)
  10396. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10397. (buffer-list)))
  10398. (delete nil blist)))
  10399. (defun org-agenda-files (&optional unrestricted ext)
  10400. "Get the list of agenda files.
  10401. Optional UNRESTRICTED means return the full list even if a restriction
  10402. is currently in place.
  10403. When EXT is non-nil, try to add all files that are created by adding EXT
  10404. to the file nemes. Basically, this is a way to add the archive files
  10405. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10406. a string, \"_archive\" will be used."
  10407. (let ((files
  10408. (cond
  10409. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10410. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10411. ((listp org-agenda-files) org-agenda-files)
  10412. (t (error "Invalid value of `org-agenda-files'")))))
  10413. (setq files (apply 'append
  10414. (mapcar (lambda (f)
  10415. (if (file-directory-p f)
  10416. (directory-files
  10417. f t org-agenda-file-regexp)
  10418. (list f)))
  10419. files)))
  10420. (when org-agenda-skip-unavailable-files
  10421. (setq files (delq nil
  10422. (mapcar (function
  10423. (lambda (file)
  10424. (and (file-readable-p file) file)))
  10425. files))))
  10426. (when ext
  10427. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10428. ext "_archive"))
  10429. (setq files (apply 'append
  10430. (mapcar
  10431. (lambda (f)
  10432. (if (file-exists-p (concat f ext))
  10433. (list f (concat f ext))
  10434. (list f)))
  10435. files))))
  10436. files))
  10437. (defun org-edit-agenda-file-list ()
  10438. "Edit the list of agenda files.
  10439. Depending on setup, this either uses customize to edit the variable
  10440. `org-agenda-files', or it visits the file that is holding the list. In the
  10441. latter case, the buffer is set up in a way that saving it automatically kills
  10442. the buffer and restores the previous window configuration."
  10443. (interactive)
  10444. (if (stringp org-agenda-files)
  10445. (let ((cw (current-window-configuration)))
  10446. (find-file org-agenda-files)
  10447. (org-set-local 'org-window-configuration cw)
  10448. (org-add-hook 'after-save-hook
  10449. (lambda ()
  10450. (set-window-configuration
  10451. (prog1 org-window-configuration
  10452. (kill-buffer (current-buffer))))
  10453. (org-install-agenda-files-menu)
  10454. (message "New agenda file list installed"))
  10455. nil 'local)
  10456. (message "%s" (substitute-command-keys
  10457. "Edit list and finish with \\[save-buffer]")))
  10458. (customize-variable 'org-agenda-files)))
  10459. (defun org-store-new-agenda-file-list (list)
  10460. "Set new value for the agenda file list and save it correcly."
  10461. (if (stringp org-agenda-files)
  10462. (let ((f org-agenda-files) b)
  10463. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10464. (with-temp-file f
  10465. (insert (mapconcat 'identity list "\n") "\n")))
  10466. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10467. (setq org-agenda-files list)
  10468. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10469. (defun org-read-agenda-file-list ()
  10470. "Read the list of agenda files from a file."
  10471. (when (file-directory-p org-agenda-files)
  10472. (error "`org-agenda-files' cannot be a single directory"))
  10473. (when (stringp org-agenda-files)
  10474. (with-temp-buffer
  10475. (insert-file-contents org-agenda-files)
  10476. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10477. ;;;###autoload
  10478. (defun org-cycle-agenda-files ()
  10479. "Cycle through the files in `org-agenda-files'.
  10480. If the current buffer visits an agenda file, find the next one in the list.
  10481. If the current buffer does not, find the first agenda file."
  10482. (interactive)
  10483. (let* ((fs (org-agenda-files t))
  10484. (files (append fs (list (car fs))))
  10485. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10486. file)
  10487. (unless files (error "No agenda files"))
  10488. (catch 'exit
  10489. (while (setq file (pop files))
  10490. (if (equal (file-truename file) tcf)
  10491. (when (car files)
  10492. (find-file (car files))
  10493. (throw 'exit t))))
  10494. (find-file (car fs)))
  10495. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10496. (defun org-agenda-file-to-front (&optional to-end)
  10497. "Move/add the current file to the top of the agenda file list.
  10498. If the file is not present in the list, it is added to the front. If it is
  10499. present, it is moved there. With optional argument TO-END, add/move to the
  10500. end of the list."
  10501. (interactive "P")
  10502. (let ((org-agenda-skip-unavailable-files nil)
  10503. (file-alist (mapcar (lambda (x)
  10504. (cons (file-truename x) x))
  10505. (org-agenda-files t)))
  10506. (ctf (file-truename buffer-file-name))
  10507. x had)
  10508. (setq x (assoc ctf file-alist) had x)
  10509. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10510. (if to-end
  10511. (setq file-alist (append (delq x file-alist) (list x)))
  10512. (setq file-alist (cons x (delq x file-alist))))
  10513. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10514. (org-install-agenda-files-menu)
  10515. (message "File %s to %s of agenda file list"
  10516. (if had "moved" "added") (if to-end "end" "front"))))
  10517. (defun org-remove-file (&optional file)
  10518. "Remove current file from the list of files in variable `org-agenda-files'.
  10519. These are the files which are being checked for agenda entries.
  10520. Optional argument FILE means, use this file instead of the current."
  10521. (interactive)
  10522. (let* ((org-agenda-skip-unavailable-files nil)
  10523. (file (or file buffer-file-name))
  10524. (true-file (file-truename file))
  10525. (afile (abbreviate-file-name file))
  10526. (files (delq nil (mapcar
  10527. (lambda (x)
  10528. (if (equal true-file
  10529. (file-truename x))
  10530. nil x))
  10531. (org-agenda-files t)))))
  10532. (if (not (= (length files) (length (org-agenda-files t))))
  10533. (progn
  10534. (org-store-new-agenda-file-list files)
  10535. (org-install-agenda-files-menu)
  10536. (message "Removed file: %s" afile))
  10537. (message "File was not in list: %s (not removed)" afile))))
  10538. (defun org-file-menu-entry (file)
  10539. (vector file (list 'find-file file) t))
  10540. (defun org-check-agenda-file (file)
  10541. "Make sure FILE exists. If not, ask user what to do."
  10542. (when (not (file-exists-p file))
  10543. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10544. (abbreviate-file-name file))
  10545. (let ((r (downcase (read-char-exclusive))))
  10546. (cond
  10547. ((equal r ?r)
  10548. (org-remove-file file)
  10549. (throw 'nextfile t))
  10550. (t (error "Abort"))))))
  10551. (defun org-get-agenda-file-buffer (file)
  10552. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10553. it to the list of buffers which might be released later."
  10554. (let ((buf (org-find-base-buffer-visiting file)))
  10555. (if buf
  10556. buf ; just return it
  10557. ;; Make a new buffer and remember it
  10558. (setq buf (find-file-noselect file))
  10559. (if buf (push buf org-agenda-new-buffers))
  10560. buf)))
  10561. (defun org-release-buffers (blist)
  10562. "Release all buffers in list, asking the user for confirmation when needed.
  10563. When a buffer is unmodified, it is just killed. When modified, it is saved
  10564. \(if the user agrees) and then killed."
  10565. (let (buf file)
  10566. (while (setq buf (pop blist))
  10567. (setq file (buffer-file-name buf))
  10568. (when (and (buffer-modified-p buf)
  10569. file
  10570. (y-or-n-p (format "Save file %s? " file)))
  10571. (with-current-buffer buf (save-buffer)))
  10572. (kill-buffer buf))))
  10573. (defun org-prepare-agenda-buffers (files)
  10574. "Create buffers for all agenda files, protect archived trees and comments."
  10575. (interactive)
  10576. (let ((pa '(:org-archived t))
  10577. (pc '(:org-comment t))
  10578. (pall '(:org-archived t :org-comment t))
  10579. (inhibit-read-only t)
  10580. (rea (concat ":" org-archive-tag ":"))
  10581. bmp file re)
  10582. (save-excursion
  10583. (save-restriction
  10584. (while (setq file (pop files))
  10585. (if (bufferp file)
  10586. (set-buffer file)
  10587. (org-check-agenda-file file)
  10588. (set-buffer (org-get-agenda-file-buffer file)))
  10589. (widen)
  10590. (setq bmp (buffer-modified-p))
  10591. (org-refresh-category-properties)
  10592. (setq org-todo-keywords-for-agenda
  10593. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10594. (setq org-done-keywords-for-agenda
  10595. (append org-done-keywords-for-agenda org-done-keywords))
  10596. (save-excursion
  10597. (remove-text-properties (point-min) (point-max) pall)
  10598. (when org-agenda-skip-archived-trees
  10599. (goto-char (point-min))
  10600. (while (re-search-forward rea nil t)
  10601. (if (org-on-heading-p t)
  10602. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10603. (goto-char (point-min))
  10604. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10605. (while (re-search-forward re nil t)
  10606. (add-text-properties
  10607. (match-beginning 0) (org-end-of-subtree t) pc)))
  10608. (set-buffer-modified-p bmp))))))
  10609. ;;;; Embedded LaTeX
  10610. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10611. "Keymap for the minor `org-cdlatex-mode'.")
  10612. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10613. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10614. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10615. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10616. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10617. (defvar org-cdlatex-texmathp-advice-is-done nil
  10618. "Flag remembering if we have applied the advice to texmathp already.")
  10619. (define-minor-mode org-cdlatex-mode
  10620. "Toggle the minor `org-cdlatex-mode'.
  10621. This mode supports entering LaTeX environment and math in LaTeX fragments
  10622. in Org-mode.
  10623. \\{org-cdlatex-mode-map}"
  10624. nil " OCDL" nil
  10625. (when org-cdlatex-mode (require 'cdlatex))
  10626. (unless org-cdlatex-texmathp-advice-is-done
  10627. (setq org-cdlatex-texmathp-advice-is-done t)
  10628. (defadvice texmathp (around org-math-always-on activate)
  10629. "Always return t in org-mode buffers.
  10630. This is because we want to insert math symbols without dollars even outside
  10631. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10632. en embedded LaTeX fragement, let texmathp do its job.
  10633. \\[org-cdlatex-mode-map]"
  10634. (interactive)
  10635. (let (p)
  10636. (cond
  10637. ((not (org-mode-p)) ad-do-it)
  10638. ((eq this-command 'cdlatex-math-symbol)
  10639. (setq ad-return-value t
  10640. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10641. (t
  10642. (let ((p (org-inside-LaTeX-fragment-p)))
  10643. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10644. (setq ad-return-value t
  10645. texmathp-why '("Org-mode embedded math" . 0))
  10646. (if p ad-do-it)))))))))
  10647. (defun turn-on-org-cdlatex ()
  10648. "Unconditionally turn on `org-cdlatex-mode'."
  10649. (org-cdlatex-mode 1))
  10650. (defun org-inside-LaTeX-fragment-p ()
  10651. "Test if point is inside a LaTeX fragment.
  10652. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10653. sequence appearing also before point.
  10654. Even though the matchers for math are configurable, this function assumes
  10655. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10656. delimiters are skipped when they have been removed by customization.
  10657. The return value is nil, or a cons cell with the delimiter and
  10658. and the position of this delimiter.
  10659. This function does a reasonably good job, but can locally be fooled by
  10660. for example currency specifications. For example it will assume being in
  10661. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10662. fragments that are properly closed, but during editing, we have to live
  10663. with the uncertainty caused by missing closing delimiters. This function
  10664. looks only before point, not after."
  10665. (catch 'exit
  10666. (let ((pos (point))
  10667. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10668. (lim (progn
  10669. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10670. (point)))
  10671. dd-on str (start 0) m re)
  10672. (goto-char pos)
  10673. (when dodollar
  10674. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10675. re (nth 1 (assoc "$" org-latex-regexps)))
  10676. (while (string-match re str start)
  10677. (cond
  10678. ((= (match-end 0) (length str))
  10679. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10680. ((= (match-end 0) (- (length str) 5))
  10681. (throw 'exit nil))
  10682. (t (setq start (match-end 0))))))
  10683. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10684. (goto-char pos)
  10685. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10686. (and (match-beginning 2) (throw 'exit nil))
  10687. ;; count $$
  10688. (while (re-search-backward "\\$\\$" lim t)
  10689. (setq dd-on (not dd-on)))
  10690. (goto-char pos)
  10691. (if dd-on (cons "$$" m))))))
  10692. (defun org-try-cdlatex-tab ()
  10693. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10694. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10695. - inside a LaTeX fragment, or
  10696. - after the first word in a line, where an abbreviation expansion could
  10697. insert a LaTeX environment."
  10698. (when org-cdlatex-mode
  10699. (cond
  10700. ((save-excursion
  10701. (skip-chars-backward "a-zA-Z0-9*")
  10702. (skip-chars-backward " \t")
  10703. (bolp))
  10704. (cdlatex-tab) t)
  10705. ((org-inside-LaTeX-fragment-p)
  10706. (cdlatex-tab) t)
  10707. (t nil))))
  10708. (defun org-cdlatex-underscore-caret (&optional arg)
  10709. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10710. Revert to the normal definition outside of these fragments."
  10711. (interactive "P")
  10712. (if (org-inside-LaTeX-fragment-p)
  10713. (call-interactively 'cdlatex-sub-superscript)
  10714. (let (org-cdlatex-mode)
  10715. (call-interactively (key-binding (vector last-input-event))))))
  10716. (defun org-cdlatex-math-modify (&optional arg)
  10717. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10718. Revert to the normal definition outside of these fragments."
  10719. (interactive "P")
  10720. (if (org-inside-LaTeX-fragment-p)
  10721. (call-interactively 'cdlatex-math-modify)
  10722. (let (org-cdlatex-mode)
  10723. (call-interactively (key-binding (vector last-input-event))))))
  10724. (defvar org-latex-fragment-image-overlays nil
  10725. "List of overlays carrying the images of latex fragments.")
  10726. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10727. (defun org-remove-latex-fragment-image-overlays ()
  10728. "Remove all overlays with LaTeX fragment images in current buffer."
  10729. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10730. (setq org-latex-fragment-image-overlays nil))
  10731. (defun org-preview-latex-fragment (&optional subtree)
  10732. "Preview the LaTeX fragment at point, or all locally or globally.
  10733. If the cursor is in a LaTeX fragment, create the image and overlay
  10734. it over the source code. If there is no fragment at point, display
  10735. all fragments in the current text, from one headline to the next. With
  10736. prefix SUBTREE, display all fragments in the current subtree. With a
  10737. double prefix `C-u C-u', or when the cursor is before the first headline,
  10738. display all fragments in the buffer.
  10739. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10740. (interactive "P")
  10741. (org-remove-latex-fragment-image-overlays)
  10742. (save-excursion
  10743. (save-restriction
  10744. (let (beg end at msg)
  10745. (cond
  10746. ((or (equal subtree '(16))
  10747. (not (save-excursion
  10748. (re-search-backward (concat "^" outline-regexp) nil t))))
  10749. (setq beg (point-min) end (point-max)
  10750. msg "Creating images for buffer...%s"))
  10751. ((equal subtree '(4))
  10752. (org-back-to-heading)
  10753. (setq beg (point) end (org-end-of-subtree t)
  10754. msg "Creating images for subtree...%s"))
  10755. (t
  10756. (if (setq at (org-inside-LaTeX-fragment-p))
  10757. (goto-char (max (point-min) (- (cdr at) 2)))
  10758. (org-back-to-heading))
  10759. (setq beg (point) end (progn (outline-next-heading) (point))
  10760. msg (if at "Creating image...%s"
  10761. "Creating images for entry...%s"))))
  10762. (message msg "")
  10763. (narrow-to-region beg end)
  10764. (goto-char beg)
  10765. (org-format-latex
  10766. (concat "ltxpng/" (file-name-sans-extension
  10767. (file-name-nondirectory
  10768. buffer-file-name)))
  10769. default-directory 'overlays msg at 'forbuffer)
  10770. (message msg "done. Use `C-c C-c' to remove images.")))))
  10771. (defvar org-latex-regexps
  10772. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10773. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10774. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10775. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10776. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10777. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10778. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10779. "Regular expressions for matching embedded LaTeX.")
  10780. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10781. "Replace LaTeX fragments with links to an image, and produce images."
  10782. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10783. (let* ((prefixnodir (file-name-nondirectory prefix))
  10784. (absprefix (expand-file-name prefix dir))
  10785. (todir (file-name-directory absprefix))
  10786. (opt org-format-latex-options)
  10787. (matchers (plist-get opt :matchers))
  10788. (re-list org-latex-regexps)
  10789. (cnt 0) txt link beg end re e checkdir
  10790. m n block linkfile movefile ov)
  10791. ;; Check if there are old images files with this prefix, and remove them
  10792. (when (file-directory-p todir)
  10793. (mapc 'delete-file
  10794. (directory-files
  10795. todir 'full
  10796. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10797. ;; Check the different regular expressions
  10798. (while (setq e (pop re-list))
  10799. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10800. block (if (nth 3 e) "\n\n" ""))
  10801. (when (member m matchers)
  10802. (goto-char (point-min))
  10803. (while (re-search-forward re nil t)
  10804. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10805. (setq txt (match-string n)
  10806. beg (match-beginning n) end (match-end n)
  10807. cnt (1+ cnt)
  10808. linkfile (format "%s_%04d.png" prefix cnt)
  10809. movefile (format "%s_%04d.png" absprefix cnt)
  10810. link (concat block "[[file:" linkfile "]]" block))
  10811. (if msg (message msg cnt))
  10812. (goto-char beg)
  10813. (unless checkdir ; make sure the directory exists
  10814. (setq checkdir t)
  10815. (or (file-directory-p todir) (make-directory todir)))
  10816. (org-create-formula-image
  10817. txt movefile opt forbuffer)
  10818. (if overlays
  10819. (progn
  10820. (setq ov (org-make-overlay beg end))
  10821. (if (featurep 'xemacs)
  10822. (progn
  10823. (org-overlay-put ov 'invisible t)
  10824. (org-overlay-put
  10825. ov 'end-glyph
  10826. (make-glyph (vector 'png :file movefile))))
  10827. (org-overlay-put
  10828. ov 'display
  10829. (list 'image :type 'png :file movefile :ascent 'center)))
  10830. (push ov org-latex-fragment-image-overlays)
  10831. (goto-char end))
  10832. (delete-region beg end)
  10833. (insert link))))))))
  10834. ;; This function borrows from Ganesh Swami's latex2png.el
  10835. (defun org-create-formula-image (string tofile options buffer)
  10836. (let* ((tmpdir (if (featurep 'xemacs)
  10837. (temp-directory)
  10838. temporary-file-directory))
  10839. (texfilebase (make-temp-name
  10840. (expand-file-name "orgtex" tmpdir)))
  10841. (texfile (concat texfilebase ".tex"))
  10842. (dvifile (concat texfilebase ".dvi"))
  10843. (pngfile (concat texfilebase ".png"))
  10844. (fnh (if (featurep 'xemacs)
  10845. (font-height (get-face-font 'default))
  10846. (face-attribute 'default :height nil)))
  10847. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10848. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10849. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10850. "Black"))
  10851. (bg (or (plist-get options (if buffer :background :html-background))
  10852. "Transparent")))
  10853. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10854. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10855. (with-temp-file texfile
  10856. (insert org-format-latex-header
  10857. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10858. (let ((dir default-directory))
  10859. (condition-case nil
  10860. (progn
  10861. (cd tmpdir)
  10862. (call-process "latex" nil nil nil texfile))
  10863. (error nil))
  10864. (cd dir))
  10865. (if (not (file-exists-p dvifile))
  10866. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10867. (call-process "dvipng" nil nil nil
  10868. "-E" "-fg" fg "-bg" bg
  10869. "-D" dpi
  10870. ;;"-x" scale "-y" scale
  10871. "-T" "tight"
  10872. "-o" pngfile
  10873. dvifile)
  10874. (if (not (file-exists-p pngfile))
  10875. (progn (message "Failed to create png file from %s" texfile) nil)
  10876. ;; Use the requested file name and clean up
  10877. (copy-file pngfile tofile 'replace)
  10878. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10879. (delete-file (concat texfilebase e)))
  10880. pngfile))))
  10881. (defun org-dvipng-color (attr)
  10882. "Return an rgb color specification for dvipng."
  10883. (apply 'format "rgb %s %s %s"
  10884. (mapcar 'org-normalize-color
  10885. (color-values (face-attribute 'default attr nil)))))
  10886. (defun org-normalize-color (value)
  10887. "Return string to be used as color value for an RGB component."
  10888. (format "%g" (/ value 65535.0)))
  10889. ;;;; Key bindings
  10890. ;; Make `C-c C-x' a prefix key
  10891. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10892. ;; TAB key with modifiers
  10893. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10894. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10895. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10896. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10897. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10898. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10899. ;; The following line is necessary under Suse GNU/Linux
  10900. (unless (featurep 'xemacs)
  10901. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10902. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10903. (define-key org-mode-map [backtab] 'org-shifttab)
  10904. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10905. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10906. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10907. ;; Cursor keys with modifiers
  10908. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10909. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10910. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10911. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10912. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10913. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10914. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10915. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10916. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10917. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10918. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10919. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10920. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10921. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10922. ;;; Extra keys for tty access.
  10923. ;; We only set them when really needed because otherwise the
  10924. ;; menus don't show the simple keys
  10925. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10926. (not window-system))
  10927. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10928. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10929. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10930. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10931. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10932. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10933. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10934. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10935. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10936. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10937. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10938. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10939. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10940. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10941. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10942. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10943. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10944. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10945. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10946. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10947. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10948. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10949. ;; All the other keys
  10950. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10951. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10952. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10953. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10954. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10955. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10956. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10957. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10958. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10959. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10960. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10961. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10962. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10963. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10964. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10965. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10966. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10967. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10968. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10969. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10970. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10971. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10972. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10973. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10974. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10975. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10976. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10977. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10978. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10979. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10980. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10981. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10982. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10983. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10984. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10985. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10986. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10987. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10988. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10989. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10990. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10991. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10992. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10993. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10994. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10995. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10996. (org-defkey org-mode-map "\C-m" 'org-return)
  10997. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10998. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10999. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11000. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11001. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11002. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  11003. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11004. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11005. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11006. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11007. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  11008. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11009. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11010. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11011. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11012. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11013. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  11014. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11015. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11016. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11017. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11018. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11019. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11020. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11021. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11022. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11023. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11024. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11025. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11026. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11027. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11028. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  11029. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11030. (when (featurep 'xemacs)
  11031. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11032. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11033. (defun org-self-insert-command (N)
  11034. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11035. If the cursor is in a table looking at whitespace, the whitespace is
  11036. overwritten, and the table is not marked as requiring realignment."
  11037. (interactive "p")
  11038. (if (and (org-table-p)
  11039. (progn
  11040. ;; check if we blank the field, and if that triggers align
  11041. (and (featurep 'org-table) org-table-auto-blank-field
  11042. (member last-command
  11043. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11044. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11045. ;; got extra space, this field does not determine column width
  11046. (let (org-table-may-need-update) (org-table-blank-field))
  11047. ;; no extra space, this field may determine column width
  11048. (org-table-blank-field)))
  11049. t)
  11050. (eq N 1)
  11051. (looking-at "[^|\n]* |"))
  11052. (let (org-table-may-need-update)
  11053. (goto-char (1- (match-end 0)))
  11054. (delete-backward-char 1)
  11055. (goto-char (match-beginning 0))
  11056. (self-insert-command N))
  11057. (setq org-table-may-need-update t)
  11058. (self-insert-command N)
  11059. (org-fix-tags-on-the-fly)))
  11060. (defun org-fix-tags-on-the-fly ()
  11061. (when (and (equal (char-after (point-at-bol)) ?*)
  11062. (org-on-heading-p))
  11063. (org-align-tags-here org-tags-column)))
  11064. (defun org-delete-backward-char (N)
  11065. "Like `delete-backward-char', insert whitespace at field end in tables.
  11066. When deleting backwards, in tables this function will insert whitespace in
  11067. front of the next \"|\" separator, to keep the table aligned. The table will
  11068. still be marked for re-alignment if the field did fill the entire column,
  11069. because, in this case the deletion might narrow the column."
  11070. (interactive "p")
  11071. (if (and (org-table-p)
  11072. (eq N 1)
  11073. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11074. (looking-at ".*?|"))
  11075. (let ((pos (point))
  11076. (noalign (looking-at "[^|\n\r]* |"))
  11077. (c org-table-may-need-update))
  11078. (backward-delete-char N)
  11079. (skip-chars-forward "^|")
  11080. (insert " ")
  11081. (goto-char (1- pos))
  11082. ;; noalign: if there were two spaces at the end, this field
  11083. ;; does not determine the width of the column.
  11084. (if noalign (setq org-table-may-need-update c)))
  11085. (backward-delete-char N)
  11086. (org-fix-tags-on-the-fly)))
  11087. (defun org-delete-char (N)
  11088. "Like `delete-char', but insert whitespace at field end in tables.
  11089. When deleting characters, in tables this function will insert whitespace in
  11090. front of the next \"|\" separator, to keep the table aligned. The table will
  11091. still be marked for re-alignment if the field did fill the entire column,
  11092. because, in this case the deletion might narrow the column."
  11093. (interactive "p")
  11094. (if (and (org-table-p)
  11095. (not (bolp))
  11096. (not (= (char-after) ?|))
  11097. (eq N 1))
  11098. (if (looking-at ".*?|")
  11099. (let ((pos (point))
  11100. (noalign (looking-at "[^|\n\r]* |"))
  11101. (c org-table-may-need-update))
  11102. (replace-match (concat
  11103. (substring (match-string 0) 1 -1)
  11104. " |"))
  11105. (goto-char pos)
  11106. ;; noalign: if there were two spaces at the end, this field
  11107. ;; does not determine the width of the column.
  11108. (if noalign (setq org-table-may-need-update c)))
  11109. (delete-char N))
  11110. (delete-char N)
  11111. (org-fix-tags-on-the-fly)))
  11112. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11113. (put 'org-self-insert-command 'delete-selection t)
  11114. (put 'orgtbl-self-insert-command 'delete-selection t)
  11115. (put 'org-delete-char 'delete-selection 'supersede)
  11116. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11117. ;; Make `flyspell-mode' delay after some commands
  11118. (put 'org-self-insert-command 'flyspell-delayed t)
  11119. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11120. (put 'org-delete-char 'flyspell-delayed t)
  11121. (put 'org-delete-backward-char 'flyspell-delayed t)
  11122. ;; Make pabbrev-mode expand after org-mode commands
  11123. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11124. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11125. ;; How to do this: Measure non-white length of current string
  11126. ;; If equal to column width, we should realign.
  11127. (defun org-remap (map &rest commands)
  11128. "In MAP, remap the functions given in COMMANDS.
  11129. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11130. (let (new old)
  11131. (while commands
  11132. (setq old (pop commands) new (pop commands))
  11133. (if (fboundp 'command-remapping)
  11134. (org-defkey map (vector 'remap old) new)
  11135. (substitute-key-definition old new map global-map)))))
  11136. (when (eq org-enable-table-editor 'optimized)
  11137. ;; If the user wants maximum table support, we need to hijack
  11138. ;; some standard editing functions
  11139. (org-remap org-mode-map
  11140. 'self-insert-command 'org-self-insert-command
  11141. 'delete-char 'org-delete-char
  11142. 'delete-backward-char 'org-delete-backward-char)
  11143. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11144. (defun org-shiftcursor-error ()
  11145. "Throw an error because Shift-Cursor command was applied in wrong context."
  11146. (error "This command is active in special context like tables, headlines or timestamps"))
  11147. (defun org-shifttab (&optional arg)
  11148. "Global visibility cycling or move to previous table field.
  11149. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11150. on context.
  11151. See the individual commands for more information."
  11152. (interactive "P")
  11153. (cond
  11154. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11155. ((integerp arg)
  11156. (message "Content view to level: %d" arg)
  11157. (org-content (prefix-numeric-value arg))
  11158. (setq org-cycle-global-status 'overview))
  11159. (t (call-interactively 'org-global-cycle))))
  11160. (defun org-shiftmetaleft ()
  11161. "Promote subtree or delete table column.
  11162. Calls `org-promote-subtree', `org-outdent-item',
  11163. or `org-table-delete-column', depending on context.
  11164. See the individual commands for more information."
  11165. (interactive)
  11166. (cond
  11167. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11168. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11169. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11170. (t (org-shiftcursor-error))))
  11171. (defun org-shiftmetaright ()
  11172. "Demote subtree or insert table column.
  11173. Calls `org-demote-subtree', `org-indent-item',
  11174. or `org-table-insert-column', depending on context.
  11175. See the individual commands for more information."
  11176. (interactive)
  11177. (cond
  11178. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11179. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11180. ((org-at-item-p) (call-interactively 'org-indent-item))
  11181. (t (org-shiftcursor-error))))
  11182. (defun org-shiftmetaup (&optional arg)
  11183. "Move subtree up or kill table row.
  11184. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11185. `org-move-item-up' depending on context. See the individual commands
  11186. for more information."
  11187. (interactive "P")
  11188. (cond
  11189. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11190. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11191. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11192. (t (org-shiftcursor-error))))
  11193. (defun org-shiftmetadown (&optional arg)
  11194. "Move subtree down or insert table row.
  11195. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11196. `org-move-item-down', depending on context. See the individual
  11197. commands for more information."
  11198. (interactive "P")
  11199. (cond
  11200. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11201. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11202. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11203. (t (org-shiftcursor-error))))
  11204. (defun org-metaleft (&optional arg)
  11205. "Promote heading or move table column to left.
  11206. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11207. With no specific context, calls the Emacs default `backward-word'.
  11208. See the individual commands for more information."
  11209. (interactive "P")
  11210. (cond
  11211. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11212. ((or (org-on-heading-p) (org-region-active-p))
  11213. (call-interactively 'org-do-promote))
  11214. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11215. (t (call-interactively 'backward-word))))
  11216. (defun org-metaright (&optional arg)
  11217. "Demote subtree or move table column to right.
  11218. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11219. With no specific context, calls the Emacs default `forward-word'.
  11220. See the individual commands for more information."
  11221. (interactive "P")
  11222. (cond
  11223. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11224. ((or (org-on-heading-p) (org-region-active-p))
  11225. (call-interactively 'org-do-demote))
  11226. ((org-at-item-p) (call-interactively 'org-indent-item))
  11227. (t (call-interactively 'forward-word))))
  11228. (defun org-metaup (&optional arg)
  11229. "Move subtree up or move table row up.
  11230. Calls `org-move-subtree-up' or `org-table-move-row' or
  11231. `org-move-item-up', depending on context. See the individual commands
  11232. for more information."
  11233. (interactive "P")
  11234. (cond
  11235. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11236. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11237. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11238. (t (transpose-lines 1) (beginning-of-line -1))))
  11239. (defun org-metadown (&optional arg)
  11240. "Move subtree down or move table row down.
  11241. Calls `org-move-subtree-down' or `org-table-move-row' or
  11242. `org-move-item-down', depending on context. See the individual
  11243. commands for more information."
  11244. (interactive "P")
  11245. (cond
  11246. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11247. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11248. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11249. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11250. (defun org-shiftup (&optional arg)
  11251. "Increase item in timestamp or increase priority of current headline.
  11252. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11253. depending on context. See the individual commands for more information."
  11254. (interactive "P")
  11255. (cond
  11256. ((org-at-timestamp-p t)
  11257. (call-interactively (if org-edit-timestamp-down-means-later
  11258. 'org-timestamp-down 'org-timestamp-up)))
  11259. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11260. ((org-at-item-p) (call-interactively 'org-previous-item))
  11261. ((org-clocktable-try-shift 'up arg))
  11262. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11263. (defun org-shiftdown (&optional arg)
  11264. "Decrease item in timestamp or decrease priority of current headline.
  11265. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11266. depending on context. See the individual commands for more information."
  11267. (interactive "P")
  11268. (cond
  11269. ((org-at-timestamp-p t)
  11270. (call-interactively (if org-edit-timestamp-down-means-later
  11271. 'org-timestamp-up 'org-timestamp-down)))
  11272. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11273. ((org-clocktable-try-shift 'down arg))
  11274. (t (call-interactively 'org-next-item))))
  11275. (defun org-shiftright (&optional arg)
  11276. "Next TODO keyword or timestamp one day later, depending on context."
  11277. (interactive "P")
  11278. (cond
  11279. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11280. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11281. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11282. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11283. ((org-clocktable-try-shift 'right arg))
  11284. (t (org-shiftcursor-error))))
  11285. (defun org-shiftleft (&optional arg)
  11286. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11287. (interactive "P")
  11288. (cond
  11289. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11290. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11291. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11292. ((org-at-property-p)
  11293. (call-interactively 'org-property-previous-allowed-value))
  11294. ((org-clocktable-try-shift 'left arg))
  11295. (t (org-shiftcursor-error))))
  11296. (defun org-shiftcontrolright ()
  11297. "Switch to next TODO set."
  11298. (interactive)
  11299. (cond
  11300. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11301. (t (org-shiftcursor-error))))
  11302. (defun org-shiftcontrolleft ()
  11303. "Switch to previous TODO set."
  11304. (interactive)
  11305. (cond
  11306. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11307. (t (org-shiftcursor-error))))
  11308. (defun org-ctrl-c-ret ()
  11309. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11310. (interactive)
  11311. (cond
  11312. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11313. (t (call-interactively 'org-insert-heading))))
  11314. (defun org-copy-special ()
  11315. "Copy region in table or copy current subtree.
  11316. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11317. See the individual commands for more information."
  11318. (interactive)
  11319. (call-interactively
  11320. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11321. (defun org-cut-special ()
  11322. "Cut region in table or cut current subtree.
  11323. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11324. See the individual commands for more information."
  11325. (interactive)
  11326. (call-interactively
  11327. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11328. (defun org-paste-special (arg)
  11329. "Paste rectangular region into table, or past subtree relative to level.
  11330. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11331. See the individual commands for more information."
  11332. (interactive "P")
  11333. (if (org-at-table-p)
  11334. (org-table-paste-rectangle)
  11335. (org-paste-subtree arg)))
  11336. (defun org-ctrl-c-ctrl-c (&optional arg)
  11337. "Set tags in headline, or update according to changed information at point.
  11338. This command does many different things, depending on context:
  11339. - If the cursor is in a headline, prompt for tags and insert them
  11340. into the current line, aligned to `org-tags-column'. When called
  11341. with prefix arg, realign all tags in the current buffer.
  11342. - If the cursor is in one of the special #+KEYWORD lines, this
  11343. triggers scanning the buffer for these lines and updating the
  11344. information.
  11345. - If the cursor is inside a table, realign the table. This command
  11346. works even if the automatic table editor has been turned off.
  11347. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11348. the entire table.
  11349. - If the cursor is a the beginning of a dynamic block, update it.
  11350. - If the cursor is inside a table created by the table.el package,
  11351. activate that table.
  11352. - If the current buffer is a remember buffer, close note and file it.
  11353. with a prefix argument, file it without further interaction to the default
  11354. location.
  11355. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11356. links in this buffer.
  11357. - If the cursor is on a numbered item in a plain list, renumber the
  11358. ordered list.
  11359. - If the cursor is on a checkbox, toggle it."
  11360. (interactive "P")
  11361. (let ((org-enable-table-editor t))
  11362. (cond
  11363. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11364. org-occur-highlights
  11365. org-latex-fragment-image-overlays)
  11366. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11367. (org-remove-occur-highlights)
  11368. (org-remove-latex-fragment-image-overlays)
  11369. (message "Temporary highlights/overlays removed from current buffer"))
  11370. ((and (local-variable-p 'org-finish-function (current-buffer))
  11371. (fboundp org-finish-function))
  11372. (funcall org-finish-function))
  11373. ((org-at-property-p)
  11374. (call-interactively 'org-property-action))
  11375. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11376. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11377. ((org-at-table.el-p)
  11378. (require 'table)
  11379. (beginning-of-line 1)
  11380. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11381. (call-interactively 'table-recognize-table))
  11382. ((org-at-table-p)
  11383. (org-table-maybe-eval-formula)
  11384. (if arg
  11385. (call-interactively 'org-table-recalculate)
  11386. (org-table-maybe-recalculate-line))
  11387. (call-interactively 'org-table-align))
  11388. ((org-at-item-checkbox-p)
  11389. (call-interactively 'org-toggle-checkbox))
  11390. ((org-at-item-p)
  11391. (call-interactively 'org-maybe-renumber-ordered-list))
  11392. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11393. ;; Dynamic block
  11394. (beginning-of-line 1)
  11395. (org-update-dblock))
  11396. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11397. (cond
  11398. ((equal (match-string 1) "TBLFM")
  11399. ;; Recalculate the table before this line
  11400. (save-excursion
  11401. (beginning-of-line 1)
  11402. (skip-chars-backward " \r\n\t")
  11403. (if (org-at-table-p)
  11404. (org-call-with-arg 'org-table-recalculate t))))
  11405. (t
  11406. ; (org-set-regexps-and-options)
  11407. ; (org-restart-font-lock)
  11408. (let ((org-inhibit-startup t)) (org-mode-restart))
  11409. (message "Local setup has been refreshed"))))
  11410. (t (error "C-c C-c can do nothing useful at this location.")))))
  11411. (defun org-mode-restart ()
  11412. "Restart Org-mode, to scan again for special lines.
  11413. Also updates the keyword regular expressions."
  11414. (interactive)
  11415. (org-mode)
  11416. (message "Org-mode restarted"))
  11417. (defun org-kill-note-or-show-branches ()
  11418. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11419. (interactive)
  11420. (if (not org-finish-function)
  11421. (call-interactively 'show-branches)
  11422. (let ((org-note-abort t))
  11423. (funcall org-finish-function))))
  11424. (defun org-return (&optional indent)
  11425. "Goto next table row or insert a newline.
  11426. Calls `org-table-next-row' or `newline', depending on context.
  11427. See the individual commands for more information."
  11428. (interactive)
  11429. (cond
  11430. ((bobp) (if indent (newline-and-indent) (newline)))
  11431. ((and (org-at-heading-p)
  11432. (looking-at
  11433. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11434. (org-show-entry)
  11435. (end-of-line 1)
  11436. (newline))
  11437. ((org-at-table-p)
  11438. (org-table-justify-field-maybe)
  11439. (call-interactively 'org-table-next-row))
  11440. (t (if indent (newline-and-indent) (newline)))))
  11441. (defun org-return-indent ()
  11442. "Goto next table row or insert a newline and indent.
  11443. Calls `org-table-next-row' or `newline-and-indent', depending on
  11444. context. See the individual commands for more information."
  11445. (interactive)
  11446. (org-return t))
  11447. (defun org-ctrl-c-star ()
  11448. "Compute table, or change heading status of lines.
  11449. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11450. depending on context. This will also turn a plain list item or a normal
  11451. line into a subheading."
  11452. (interactive)
  11453. (cond
  11454. ((org-at-table-p)
  11455. (call-interactively 'org-table-recalculate))
  11456. ((org-region-active-p)
  11457. ;; Convert all lines in region to list items
  11458. (call-interactively 'org-toggle-region-headings))
  11459. ((org-on-heading-p)
  11460. (org-toggle-region-headings (point-at-bol)
  11461. (min (1+ (point-at-eol)) (point-max))))
  11462. ((org-at-item-p)
  11463. ;; Convert to heading
  11464. (let ((level (save-match-data
  11465. (save-excursion
  11466. (condition-case nil
  11467. (progn
  11468. (org-back-to-heading t)
  11469. (funcall outline-level))
  11470. (error 0))))))
  11471. (replace-match
  11472. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11473. (t (org-toggle-region-headings (point-at-bol)
  11474. (min (1+ (point-at-eol)) (point-max))))))
  11475. (defun org-ctrl-c-minus ()
  11476. "Insert separator line in table or modify bullet status of line.
  11477. Also turns a plain line or a region of lines into list items.
  11478. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11479. `org-cycle-list-bullet', depending on context."
  11480. (interactive)
  11481. (cond
  11482. ((org-at-table-p)
  11483. (call-interactively 'org-table-insert-hline))
  11484. ((org-on-heading-p)
  11485. ;; Convert to item
  11486. (save-excursion
  11487. (beginning-of-line 1)
  11488. (if (looking-at "\\*+ ")
  11489. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11490. ((org-region-active-p)
  11491. ;; Convert all lines in region to list items
  11492. (call-interactively 'org-toggle-region-items))
  11493. ((org-in-item-p)
  11494. (call-interactively 'org-cycle-list-bullet))
  11495. (t (org-toggle-region-items (point-at-bol)
  11496. (min (1+ (point-at-eol)) (point-max))))))
  11497. (defun org-toggle-region-items (beg end)
  11498. "Convert all lines in region to list items.
  11499. If the first line is already an item, convert all list items in the region
  11500. to normal lines."
  11501. (interactive "r")
  11502. (let (l2 l)
  11503. (save-excursion
  11504. (goto-char end)
  11505. (setq l2 (org-current-line))
  11506. (goto-char beg)
  11507. (beginning-of-line 1)
  11508. (setq l (1- (org-current-line)))
  11509. (if (org-at-item-p)
  11510. ;; We already have items, de-itemize
  11511. (while (< (setq l (1+ l)) l2)
  11512. (when (org-at-item-p)
  11513. (goto-char (match-beginning 2))
  11514. (delete-region (match-beginning 2) (match-end 2))
  11515. (and (looking-at "[ \t]+") (replace-match "")))
  11516. (beginning-of-line 2))
  11517. (while (< (setq l (1+ l)) l2)
  11518. (unless (org-at-item-p)
  11519. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11520. (replace-match "\\1- \\2")))
  11521. (beginning-of-line 2))))))
  11522. (defun org-toggle-region-headings (beg end)
  11523. "Convert all lines in region to list items.
  11524. If the first line is already an item, convert all list items in the region
  11525. to normal lines."
  11526. (interactive "r")
  11527. (let (l2 l)
  11528. (save-excursion
  11529. (goto-char end)
  11530. (setq l2 (org-current-line))
  11531. (goto-char beg)
  11532. (beginning-of-line 1)
  11533. (setq l (1- (org-current-line)))
  11534. (if (org-on-heading-p)
  11535. ;; We already have headlines, de-star them
  11536. (while (< (setq l (1+ l)) l2)
  11537. (when (org-on-heading-p t)
  11538. (and (looking-at outline-regexp) (replace-match "")))
  11539. (beginning-of-line 2))
  11540. (let* ((stars (save-excursion
  11541. (re-search-backward org-complex-heading-regexp nil t)
  11542. (or (match-string 1) "*")))
  11543. (add-stars (if org-odd-levels-only "**" "*"))
  11544. (rpl (concat stars add-stars " \\2")))
  11545. (while (< (setq l (1+ l)) l2)
  11546. (unless (org-on-heading-p)
  11547. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11548. (replace-match rpl)))
  11549. (beginning-of-line 2)))))))
  11550. (defun org-meta-return (&optional arg)
  11551. "Insert a new heading or wrap a region in a table.
  11552. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11553. See the individual commands for more information."
  11554. (interactive "P")
  11555. (cond
  11556. ((org-at-table-p)
  11557. (call-interactively 'org-table-wrap-region))
  11558. (t (call-interactively 'org-insert-heading))))
  11559. ;;; Menu entries
  11560. ;; Define the Org-mode menus
  11561. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11562. '("Tbl"
  11563. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11564. ["Next Field" org-cycle (org-at-table-p)]
  11565. ["Previous Field" org-shifttab (org-at-table-p)]
  11566. ["Next Row" org-return (org-at-table-p)]
  11567. "--"
  11568. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11569. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11570. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11571. "--"
  11572. ("Column"
  11573. ["Move Column Left" org-metaleft (org-at-table-p)]
  11574. ["Move Column Right" org-metaright (org-at-table-p)]
  11575. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11576. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11577. ("Row"
  11578. ["Move Row Up" org-metaup (org-at-table-p)]
  11579. ["Move Row Down" org-metadown (org-at-table-p)]
  11580. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11581. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11582. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11583. "--"
  11584. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11585. ("Rectangle"
  11586. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11587. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11588. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11589. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11590. "--"
  11591. ("Calculate"
  11592. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11593. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11594. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11595. "--"
  11596. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11597. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11598. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11599. "--"
  11600. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11601. "--"
  11602. ["Sum Column/Rectangle" org-table-sum
  11603. (or (org-at-table-p) (org-region-active-p))]
  11604. ["Which Column?" org-table-current-column (org-at-table-p)])
  11605. ["Debug Formulas"
  11606. org-table-toggle-formula-debugger
  11607. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11608. ["Show Col/Row Numbers"
  11609. org-table-toggle-coordinate-overlays
  11610. :style toggle
  11611. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11612. "--"
  11613. ["Create" org-table-create (and (not (org-at-table-p))
  11614. org-enable-table-editor)]
  11615. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11616. ["Import from File" org-table-import (not (org-at-table-p))]
  11617. ["Export to File" org-table-export (org-at-table-p)]
  11618. "--"
  11619. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11620. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11621. '("Org"
  11622. ("Show/Hide"
  11623. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11624. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11625. ["Sparse Tree..." org-sparse-tree t]
  11626. ["Reveal Context" org-reveal t]
  11627. ["Show All" show-all t]
  11628. "--"
  11629. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11630. "--"
  11631. ["New Heading" org-insert-heading t]
  11632. ("Navigate Headings"
  11633. ["Up" outline-up-heading t]
  11634. ["Next" outline-next-visible-heading t]
  11635. ["Previous" outline-previous-visible-heading t]
  11636. ["Next Same Level" outline-forward-same-level t]
  11637. ["Previous Same Level" outline-backward-same-level t]
  11638. "--"
  11639. ["Jump" org-goto t])
  11640. ("Edit Structure"
  11641. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11642. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11643. "--"
  11644. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11645. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11646. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11647. "--"
  11648. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11649. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11650. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11651. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11652. "--"
  11653. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11654. "--"
  11655. ["Convert to odd levels" org-convert-to-odd-levels t]
  11656. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11657. ("Editing"
  11658. ["Emphasis..." org-emphasize t])
  11659. ("Archive"
  11660. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11661. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11662. ; :active t :keys "C-u C-c C-x C-a"]
  11663. ["Sparse trees open ARCHIVE trees"
  11664. (setq org-sparse-tree-open-archived-trees
  11665. (not org-sparse-tree-open-archived-trees))
  11666. :style toggle :selected org-sparse-tree-open-archived-trees]
  11667. ["Cycling opens ARCHIVE trees"
  11668. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11669. :style toggle :selected org-cycle-open-archived-trees]
  11670. ["Agenda includes ARCHIVE trees"
  11671. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11672. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11673. "--"
  11674. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11675. ; ["Check and Move Children" (org-archive-subtree '(4))
  11676. ; :active t :keys "C-u C-c C-x C-s"]
  11677. )
  11678. "--"
  11679. ("TODO Lists"
  11680. ["TODO/DONE/-" org-todo t]
  11681. ("Select keyword"
  11682. ["Next keyword" org-shiftright (org-on-heading-p)]
  11683. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11684. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11685. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11686. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11687. ["Show TODO Tree" org-show-todo-tree t]
  11688. ["Global TODO list" org-todo-list t]
  11689. "--"
  11690. ["Set Priority" org-priority t]
  11691. ["Priority Up" org-shiftup t]
  11692. ["Priority Down" org-shiftdown t])
  11693. ("TAGS and Properties"
  11694. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11695. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11696. "--"
  11697. ["Set property" 'org-set-property t]
  11698. ["Column view of properties" org-columns t]
  11699. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11700. ("Dates and Scheduling"
  11701. ["Timestamp" org-time-stamp t]
  11702. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11703. ("Change Date"
  11704. ["1 Day Later" org-shiftright t]
  11705. ["1 Day Earlier" org-shiftleft t]
  11706. ["1 ... Later" org-shiftup t]
  11707. ["1 ... Earlier" org-shiftdown t])
  11708. ["Compute Time Range" org-evaluate-time-range t]
  11709. ["Schedule Item" org-schedule t]
  11710. ["Deadline" org-deadline t]
  11711. "--"
  11712. ["Custom time format" org-toggle-time-stamp-overlays
  11713. :style radio :selected org-display-custom-times]
  11714. "--"
  11715. ["Goto Calendar" org-goto-calendar t]
  11716. ["Date from Calendar" org-date-from-calendar t])
  11717. ("Logging work"
  11718. ["Clock in" org-clock-in t]
  11719. ["Clock out" org-clock-out t]
  11720. ["Clock cancel" org-clock-cancel t]
  11721. ["Goto running clock" org-clock-goto t]
  11722. ["Display times" org-clock-display t]
  11723. ["Create clock table" org-clock-report t]
  11724. "--"
  11725. ["Record DONE time"
  11726. (progn (setq org-log-done (not org-log-done))
  11727. (message "Switching to %s will %s record a timestamp"
  11728. (car org-done-keywords)
  11729. (if org-log-done "automatically" "not")))
  11730. :style toggle :selected org-log-done])
  11731. "--"
  11732. ["Agenda Command..." org-agenda t]
  11733. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11734. ("File List for Agenda")
  11735. ("Special views current file"
  11736. ["TODO Tree" org-show-todo-tree t]
  11737. ["Check Deadlines" org-check-deadlines t]
  11738. ["Timeline" org-timeline t]
  11739. ["Tags Tree" org-tags-sparse-tree t])
  11740. "--"
  11741. ("Hyperlinks"
  11742. ["Store Link (Global)" org-store-link t]
  11743. ["Insert Link" org-insert-link t]
  11744. ["Follow Link" org-open-at-point t]
  11745. "--"
  11746. ["Next link" org-next-link t]
  11747. ["Previous link" org-previous-link t]
  11748. "--"
  11749. ["Descriptive Links"
  11750. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11751. :style radio
  11752. :selected (member '(org-link) buffer-invisibility-spec)]
  11753. ["Literal Links"
  11754. (progn
  11755. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11756. :style radio
  11757. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11758. "--"
  11759. ["Export/Publish..." org-export t]
  11760. ("LaTeX"
  11761. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11762. :selected org-cdlatex-mode]
  11763. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11764. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11765. ["Modify math symbol" org-cdlatex-math-modify
  11766. (org-inside-LaTeX-fragment-p)]
  11767. ["Export LaTeX fragments as images"
  11768. (if (featurep 'org-exp)
  11769. (setq org-export-with-LaTeX-fragments
  11770. (not org-export-with-LaTeX-fragments))
  11771. (require 'org-exp))
  11772. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11773. org-export-with-LaTeX-fragments)])
  11774. "--"
  11775. ("Documentation"
  11776. ["Show Version" org-version t]
  11777. ["Info Documentation" org-info t])
  11778. ("Customize"
  11779. ["Browse Org Group" org-customize t]
  11780. "--"
  11781. ["Expand This Menu" org-create-customize-menu
  11782. (fboundp 'customize-menu-create)])
  11783. "--"
  11784. ["Refresh setup" org-mode-restart t]
  11785. ))
  11786. (defun org-info (&optional node)
  11787. "Read documentation for Org-mode in the info system.
  11788. With optional NODE, go directly to that node."
  11789. (interactive)
  11790. (info (format "(org)%s" (or node ""))))
  11791. (defun org-install-agenda-files-menu ()
  11792. (let ((bl (buffer-list)))
  11793. (save-excursion
  11794. (while bl
  11795. (set-buffer (pop bl))
  11796. (if (org-mode-p) (setq bl nil)))
  11797. (when (org-mode-p)
  11798. (easy-menu-change
  11799. '("Org") "File List for Agenda"
  11800. (append
  11801. (list
  11802. ["Edit File List" (org-edit-agenda-file-list) t]
  11803. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11804. ["Remove Current File from List" org-remove-file t]
  11805. ["Cycle through agenda files" org-cycle-agenda-files t]
  11806. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11807. "--")
  11808. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11809. ;;;; Documentation
  11810. (defun org-require-autoloaded-modules ()
  11811. (interactive)
  11812. (mapc 'require
  11813. '(org-agenda org-archive org-clock org-colview
  11814. org-exp org-export-latex org-publish
  11815. org-remember org-table)))
  11816. (defun org-customize ()
  11817. "Call the customize function with org as argument."
  11818. (interactive)
  11819. (org-load-modules-maybe)
  11820. (org-require-autoloaded-modules)
  11821. (customize-browse 'org))
  11822. (defun org-create-customize-menu ()
  11823. "Create a full customization menu for Org-mode, insert it into the menu."
  11824. (interactive)
  11825. (org-load-modules-maybe)
  11826. (org-require-autoloaded-modules)
  11827. (if (fboundp 'customize-menu-create)
  11828. (progn
  11829. (easy-menu-change
  11830. '("Org") "Customize"
  11831. `(["Browse Org group" org-customize t]
  11832. "--"
  11833. ,(customize-menu-create 'org)
  11834. ["Set" Custom-set t]
  11835. ["Save" Custom-save t]
  11836. ["Reset to Current" Custom-reset-current t]
  11837. ["Reset to Saved" Custom-reset-saved t]
  11838. ["Reset to Standard Settings" Custom-reset-standard t]))
  11839. (message "\"Org\"-menu now contains full customization menu"))
  11840. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11841. ;;;; Miscellaneous stuff
  11842. ;;; Generally useful functions
  11843. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11844. "Display the given MESSAGE as a warning."
  11845. (if (fboundp 'display-warning)
  11846. (display-warning 'org message
  11847. (if (featurep 'xemacs)
  11848. 'warning
  11849. :warning))
  11850. (let ((buf (get-buffer-create "*Org warnings*")))
  11851. (with-current-buffer buf
  11852. (goto-char (point-max))
  11853. (insert "Warning (Org): " message)
  11854. (unless (bolp)
  11855. (newline)))
  11856. (display-buffer buf)
  11857. (sit-for 0))))
  11858. (defun org-quote-csv-field (s)
  11859. "Quote field for inclusion in CSV material."
  11860. (if (string-match "[\",]" s)
  11861. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  11862. s))
  11863. (defun org-plist-delete (plist property)
  11864. "Delete PROPERTY from PLIST.
  11865. This is in contrast to merely setting it to 0."
  11866. (let (p)
  11867. (while plist
  11868. (if (not (eq property (car plist)))
  11869. (setq p (plist-put p (car plist) (nth 1 plist))))
  11870. (setq plist (cddr plist)))
  11871. p))
  11872. (defun org-force-self-insert (N)
  11873. "Needed to enforce self-insert under remapping."
  11874. (interactive "p")
  11875. (self-insert-command N))
  11876. (defun org-string-width (s)
  11877. "Compute width of string, ignoring invisible characters.
  11878. This ignores character with invisibility property `org-link', and also
  11879. characters with property `org-cwidth', because these will become invisible
  11880. upon the next fontification round."
  11881. (let (b l)
  11882. (when (or (eq t buffer-invisibility-spec)
  11883. (assq 'org-link buffer-invisibility-spec))
  11884. (while (setq b (text-property-any 0 (length s)
  11885. 'invisible 'org-link s))
  11886. (setq s (concat (substring s 0 b)
  11887. (substring s (or (next-single-property-change
  11888. b 'invisible s) (length s)))))))
  11889. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11890. (setq s (concat (substring s 0 b)
  11891. (substring s (or (next-single-property-change
  11892. b 'org-cwidth s) (length s))))))
  11893. (setq l (string-width s) b -1)
  11894. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11895. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11896. l))
  11897. (defun org-base-buffer (buffer)
  11898. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  11899. (if (not buffer)
  11900. buffer
  11901. (or (buffer-base-buffer buffer)
  11902. buffer)))
  11903. (defun org-trim (s)
  11904. "Remove whitespace at beginning and end of string."
  11905. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11906. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11907. s)
  11908. (defun org-wrap (string &optional width lines)
  11909. "Wrap string to either a number of lines, or a width in characters.
  11910. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11911. that costs. If there is a word longer than WIDTH, the text is actually
  11912. wrapped to the length of that word.
  11913. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11914. many lines, whatever width that takes.
  11915. The return value is a list of lines, without newlines at the end."
  11916. (let* ((words (org-split-string string "[ \t\n]+"))
  11917. (maxword (apply 'max (mapcar 'org-string-width words)))
  11918. w ll)
  11919. (cond (width
  11920. (org-do-wrap words (max maxword width)))
  11921. (lines
  11922. (setq w maxword)
  11923. (setq ll (org-do-wrap words maxword))
  11924. (if (<= (length ll) lines)
  11925. ll
  11926. (setq ll words)
  11927. (while (> (length ll) lines)
  11928. (setq w (1+ w))
  11929. (setq ll (org-do-wrap words w)))
  11930. ll))
  11931. (t (error "Cannot wrap this")))))
  11932. (defun org-do-wrap (words width)
  11933. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11934. (let (lines line)
  11935. (while words
  11936. (setq line (pop words))
  11937. (while (and words (< (+ (length line) (length (car words))) width))
  11938. (setq line (concat line " " (pop words))))
  11939. (setq lines (push line lines)))
  11940. (nreverse lines)))
  11941. (defun org-split-string (string &optional separators)
  11942. "Splits STRING into substrings at SEPARATORS.
  11943. No empty strings are returned if there are matches at the beginning
  11944. and end of string."
  11945. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11946. (start 0)
  11947. notfirst
  11948. (list nil))
  11949. (while (and (string-match rexp string
  11950. (if (and notfirst
  11951. (= start (match-beginning 0))
  11952. (< start (length string)))
  11953. (1+ start) start))
  11954. (< (match-beginning 0) (length string)))
  11955. (setq notfirst t)
  11956. (or (eq (match-beginning 0) 0)
  11957. (and (eq (match-beginning 0) (match-end 0))
  11958. (eq (match-beginning 0) start))
  11959. (setq list
  11960. (cons (substring string start (match-beginning 0))
  11961. list)))
  11962. (setq start (match-end 0)))
  11963. (or (eq start (length string))
  11964. (setq list
  11965. (cons (substring string start)
  11966. list)))
  11967. (nreverse list)))
  11968. (defun org-context ()
  11969. "Return a list of contexts of the current cursor position.
  11970. If several contexts apply, all are returned.
  11971. Each context entry is a list with a symbol naming the context, and
  11972. two positions indicating start and end of the context. Possible
  11973. contexts are:
  11974. :headline anywhere in a headline
  11975. :headline-stars on the leading stars in a headline
  11976. :todo-keyword on a TODO keyword (including DONE) in a headline
  11977. :tags on the TAGS in a headline
  11978. :priority on the priority cookie in a headline
  11979. :item on the first line of a plain list item
  11980. :item-bullet on the bullet/number of a plain list item
  11981. :checkbox on the checkbox in a plain list item
  11982. :table in an org-mode table
  11983. :table-special on a special filed in a table
  11984. :table-table in a table.el table
  11985. :link on a hyperlink
  11986. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11987. :target on a <<target>>
  11988. :radio-target on a <<<radio-target>>>
  11989. :latex-fragment on a LaTeX fragment
  11990. :latex-preview on a LaTeX fragment with overlayed preview image
  11991. This function expects the position to be visible because it uses font-lock
  11992. faces as a help to recognize the following contexts: :table-special, :link,
  11993. and :keyword."
  11994. (let* ((f (get-text-property (point) 'face))
  11995. (faces (if (listp f) f (list f)))
  11996. (p (point)) clist o)
  11997. ;; First the large context
  11998. (cond
  11999. ((org-on-heading-p t)
  12000. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12001. (when (progn
  12002. (beginning-of-line 1)
  12003. (looking-at org-todo-line-tags-regexp))
  12004. (push (org-point-in-group p 1 :headline-stars) clist)
  12005. (push (org-point-in-group p 2 :todo-keyword) clist)
  12006. (push (org-point-in-group p 4 :tags) clist))
  12007. (goto-char p)
  12008. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12009. (if (looking-at "\\[#[A-Z0-9]\\]")
  12010. (push (org-point-in-group p 0 :priority) clist)))
  12011. ((org-at-item-p)
  12012. (push (org-point-in-group p 2 :item-bullet) clist)
  12013. (push (list :item (point-at-bol)
  12014. (save-excursion (org-end-of-item) (point)))
  12015. clist)
  12016. (and (org-at-item-checkbox-p)
  12017. (push (org-point-in-group p 0 :checkbox) clist)))
  12018. ((org-at-table-p)
  12019. (push (list :table (org-table-begin) (org-table-end)) clist)
  12020. (if (memq 'org-formula faces)
  12021. (push (list :table-special
  12022. (previous-single-property-change p 'face)
  12023. (next-single-property-change p 'face)) clist)))
  12024. ((org-at-table-p 'any)
  12025. (push (list :table-table) clist)))
  12026. (goto-char p)
  12027. ;; Now the small context
  12028. (cond
  12029. ((org-at-timestamp-p)
  12030. (push (org-point-in-group p 0 :timestamp) clist))
  12031. ((memq 'org-link faces)
  12032. (push (list :link
  12033. (previous-single-property-change p 'face)
  12034. (next-single-property-change p 'face)) clist))
  12035. ((memq 'org-special-keyword faces)
  12036. (push (list :keyword
  12037. (previous-single-property-change p 'face)
  12038. (next-single-property-change p 'face)) clist))
  12039. ((org-on-target-p)
  12040. (push (org-point-in-group p 0 :target) clist)
  12041. (goto-char (1- (match-beginning 0)))
  12042. (if (looking-at org-radio-target-regexp)
  12043. (push (org-point-in-group p 0 :radio-target) clist))
  12044. (goto-char p))
  12045. ((setq o (car (delq nil
  12046. (mapcar
  12047. (lambda (x)
  12048. (if (memq x org-latex-fragment-image-overlays) x))
  12049. (org-overlays-at (point))))))
  12050. (push (list :latex-fragment
  12051. (org-overlay-start o) (org-overlay-end o)) clist)
  12052. (push (list :latex-preview
  12053. (org-overlay-start o) (org-overlay-end o)) clist))
  12054. ((org-inside-LaTeX-fragment-p)
  12055. ;; FIXME: positions wrong.
  12056. (push (list :latex-fragment (point) (point)) clist)))
  12057. (setq clist (nreverse (delq nil clist)))
  12058. clist))
  12059. ;; FIXME: Compare with at-regexp-p Do we need both?
  12060. (defun org-in-regexp (re &optional nlines visually)
  12061. "Check if point is inside a match of regexp.
  12062. Normally only the current line is checked, but you can include NLINES extra
  12063. lines both before and after point into the search.
  12064. If VISUALLY is set, require that the cursor is not after the match but
  12065. really on, so that the block visually is on the match."
  12066. (catch 'exit
  12067. (let ((pos (point))
  12068. (eol (point-at-eol (+ 1 (or nlines 0))))
  12069. (inc (if visually 1 0)))
  12070. (save-excursion
  12071. (beginning-of-line (- 1 (or nlines 0)))
  12072. (while (re-search-forward re eol t)
  12073. (if (and (<= (match-beginning 0) pos)
  12074. (>= (+ inc (match-end 0)) pos))
  12075. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12076. (defun org-at-regexp-p (regexp)
  12077. "Is point inside a match of REGEXP in the current line?"
  12078. (catch 'exit
  12079. (save-excursion
  12080. (let ((pos (point)) (end (point-at-eol)))
  12081. (beginning-of-line 1)
  12082. (while (re-search-forward regexp end t)
  12083. (if (and (<= (match-beginning 0) pos)
  12084. (>= (match-end 0) pos))
  12085. (throw 'exit t)))
  12086. nil))))
  12087. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12088. "Call `multi-occur' with buffers for all agenda files."
  12089. (interactive "sOrg-files matching: \np")
  12090. (let* ((files (org-agenda-files))
  12091. (tnames (mapcar 'file-truename files))
  12092. (extra org-agenda-text-search-extra-files)
  12093. f)
  12094. (when (eq (car extra) 'agenda-archives)
  12095. (setq extra (cdr extra))
  12096. (setq files (org-add-archive-files files)))
  12097. (while (setq f (pop extra))
  12098. (unless (member (file-truename f) tnames)
  12099. (add-to-list 'files f 'append)
  12100. (add-to-list 'tnames (file-truename f) 'append)))
  12101. (multi-occur
  12102. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12103. regexp)))
  12104. (if (boundp 'occur-mode-find-occurrence-hook)
  12105. ;; Emacs 23
  12106. (add-hook 'occur-mode-find-occurrence-hook
  12107. (lambda ()
  12108. (when (org-mode-p)
  12109. (org-reveal))))
  12110. ;; Emacs 22
  12111. (defadvice occur-mode-goto-occurrence
  12112. (after org-occur-reveal activate)
  12113. (and (org-mode-p) (org-reveal)))
  12114. (defadvice occur-mode-goto-occurrence-other-window
  12115. (after org-occur-reveal activate)
  12116. (and (org-mode-p) (org-reveal)))
  12117. (defadvice occur-mode-display-occurrence
  12118. (after org-occur-reveal activate)
  12119. (when (org-mode-p)
  12120. (let ((pos (occur-mode-find-occurrence)))
  12121. (with-current-buffer (marker-buffer pos)
  12122. (save-excursion
  12123. (goto-char pos)
  12124. (org-reveal)))))))
  12125. (defun org-uniquify (list)
  12126. "Remove duplicate elements from LIST."
  12127. (let (res)
  12128. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12129. res))
  12130. (defun org-delete-all (elts list)
  12131. "Remove all elements in ELTS from LIST."
  12132. (while elts
  12133. (setq list (delete (pop elts) list)))
  12134. list)
  12135. (defun org-back-over-empty-lines ()
  12136. "Move backwards over witespace, to the beginning of the first empty line.
  12137. Returns the number of empty lines passed."
  12138. (let ((pos (point)))
  12139. (skip-chars-backward " \t\n\r")
  12140. (beginning-of-line 2)
  12141. (goto-char (min (point) pos))
  12142. (count-lines (point) pos)))
  12143. (defun org-skip-whitespace ()
  12144. (skip-chars-forward " \t\n\r"))
  12145. (defun org-point-in-group (point group &optional context)
  12146. "Check if POINT is in match-group GROUP.
  12147. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12148. match. If the match group does ot exist or point is not inside it,
  12149. return nil."
  12150. (and (match-beginning group)
  12151. (>= point (match-beginning group))
  12152. (<= point (match-end group))
  12153. (if context
  12154. (list context (match-beginning group) (match-end group))
  12155. t)))
  12156. (defun org-switch-to-buffer-other-window (&rest args)
  12157. "Switch to buffer in a second window on the current frame.
  12158. In particular, do not allow pop-up frames."
  12159. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12160. special-display-function)
  12161. (apply 'switch-to-buffer-other-window args)))
  12162. (defun org-combine-plists (&rest plists)
  12163. "Create a single property list from all plists in PLISTS.
  12164. The process starts by copying the first list, and then setting properties
  12165. from the other lists. Settings in the last list are the most significant
  12166. ones and overrule settings in the other lists."
  12167. (let ((rtn (copy-sequence (pop plists)))
  12168. p v ls)
  12169. (while plists
  12170. (setq ls (pop plists))
  12171. (while ls
  12172. (setq p (pop ls) v (pop ls))
  12173. (setq rtn (plist-put rtn p v))))
  12174. rtn))
  12175. (defun org-move-line-down (arg)
  12176. "Move the current line down. With prefix argument, move it past ARG lines."
  12177. (interactive "p")
  12178. (let ((col (current-column))
  12179. beg end pos)
  12180. (beginning-of-line 1) (setq beg (point))
  12181. (beginning-of-line 2) (setq end (point))
  12182. (beginning-of-line (+ 1 arg))
  12183. (setq pos (move-marker (make-marker) (point)))
  12184. (insert (delete-and-extract-region beg end))
  12185. (goto-char pos)
  12186. (org-move-to-column col)))
  12187. (defun org-move-line-up (arg)
  12188. "Move the current line up. With prefix argument, move it past ARG lines."
  12189. (interactive "p")
  12190. (let ((col (current-column))
  12191. beg end pos)
  12192. (beginning-of-line 1) (setq beg (point))
  12193. (beginning-of-line 2) (setq end (point))
  12194. (beginning-of-line (- arg))
  12195. (setq pos (move-marker (make-marker) (point)))
  12196. (insert (delete-and-extract-region beg end))
  12197. (goto-char pos)
  12198. (org-move-to-column col)))
  12199. (defun org-replace-escapes (string table)
  12200. "Replace %-escapes in STRING with values in TABLE.
  12201. TABLE is an association list with keys like \"%a\" and string values.
  12202. The sequences in STRING may contain normal field width and padding information,
  12203. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12204. so values can contain further %-escapes if they are define later in TABLE."
  12205. (let ((case-fold-search nil)
  12206. e re rpl)
  12207. (while (setq e (pop table))
  12208. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12209. (while (string-match re string)
  12210. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12211. (cdr e)))
  12212. (setq string (replace-match rpl t t string))))
  12213. string))
  12214. (defun org-sublist (list start end)
  12215. "Return a section of LIST, from START to END.
  12216. Counting starts at 1."
  12217. (let (rtn (c start))
  12218. (setq list (nthcdr (1- start) list))
  12219. (while (and list (<= c end))
  12220. (push (pop list) rtn)
  12221. (setq c (1+ c)))
  12222. (nreverse rtn)))
  12223. (defun org-find-base-buffer-visiting (file)
  12224. "Like `find-buffer-visiting' but alway return the base buffer and
  12225. not an indirect buffer."
  12226. (let ((buf (find-buffer-visiting file)))
  12227. (if buf
  12228. (or (buffer-base-buffer buf) buf)
  12229. nil)))
  12230. (defun org-image-file-name-regexp ()
  12231. "Return regexp matching the file names of images."
  12232. (if (fboundp 'image-file-name-regexp)
  12233. (image-file-name-regexp)
  12234. (let ((image-file-name-extensions
  12235. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12236. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12237. (concat "\\."
  12238. (regexp-opt (nconc (mapcar 'upcase
  12239. image-file-name-extensions)
  12240. image-file-name-extensions)
  12241. t)
  12242. "\\'"))))
  12243. (defun org-file-image-p (file)
  12244. "Return non-nil if FILE is an image."
  12245. (save-match-data
  12246. (string-match (org-image-file-name-regexp) file)))
  12247. ;;; Paragraph filling stuff.
  12248. ;; We want this to be just right, so use the full arsenal.
  12249. (defun org-indent-line-function ()
  12250. "Indent line like previous, but further if previous was headline or item."
  12251. (interactive)
  12252. (let* ((pos (point))
  12253. (itemp (org-at-item-p))
  12254. column bpos bcol tpos tcol bullet btype bullet-type)
  12255. ;; Find the previous relevant line
  12256. (beginning-of-line 1)
  12257. (cond
  12258. ((looking-at "#") (setq column 0))
  12259. ((looking-at "\\*+ ") (setq column 0))
  12260. (t
  12261. (beginning-of-line 0)
  12262. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12263. (beginning-of-line 0))
  12264. (cond
  12265. ((looking-at "\\*+[ \t]+")
  12266. (goto-char (match-end 0))
  12267. (setq column (current-column)))
  12268. ((org-in-item-p)
  12269. (org-beginning-of-item)
  12270. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12271. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  12272. (setq bpos (match-beginning 1) tpos (match-end 0)
  12273. bcol (progn (goto-char bpos) (current-column))
  12274. tcol (progn (goto-char tpos) (current-column))
  12275. bullet (match-string 1)
  12276. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12277. (if (not itemp)
  12278. (setq column tcol)
  12279. (goto-char pos)
  12280. (beginning-of-line 1)
  12281. (if (looking-at "\\S-")
  12282. (progn
  12283. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12284. (setq bullet (match-string 1)
  12285. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12286. (setq column (if (equal btype bullet-type) bcol tcol)))
  12287. (setq column (org-get-indentation)))))
  12288. (t (setq column (org-get-indentation))))))
  12289. (goto-char pos)
  12290. (if (<= (current-column) (current-indentation))
  12291. (org-indent-line-to column)
  12292. (save-excursion (org-indent-line-to column)))
  12293. (setq column (current-column))
  12294. (beginning-of-line 1)
  12295. (if (looking-at
  12296. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12297. (replace-match (concat "\\1" (format org-property-format
  12298. (match-string 2) (match-string 3)))
  12299. t nil))
  12300. (org-move-to-column column)))
  12301. (defun org-set-autofill-regexps ()
  12302. (interactive)
  12303. ;; In the paragraph separator we include headlines, because filling
  12304. ;; text in a line directly attached to a headline would otherwise
  12305. ;; fill the headline as well.
  12306. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12307. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12308. ;; The paragraph starter includes hand-formatted lists.
  12309. (org-set-local 'paragraph-start
  12310. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12311. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12312. ;; But only if the user has not turned off tables or fixed-width regions
  12313. (org-set-local
  12314. 'auto-fill-inhibit-regexp
  12315. (concat "\\*+ \\|#\\+"
  12316. "\\|[ \t]*" org-keyword-time-regexp
  12317. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12318. (concat
  12319. "\\|[ \t]*["
  12320. (if org-enable-table-editor "|" "")
  12321. (if org-enable-fixed-width-editor ":" "")
  12322. "]"))))
  12323. ;; We use our own fill-paragraph function, to make sure that tables
  12324. ;; and fixed-width regions are not wrapped. That function will pass
  12325. ;; through to `fill-paragraph' when appropriate.
  12326. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12327. ; Adaptive filling: To get full control, first make sure that
  12328. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12329. (org-set-local 'adaptive-fill-regexp "\000")
  12330. (org-set-local 'adaptive-fill-function
  12331. 'org-adaptive-fill-function)
  12332. (org-set-local
  12333. 'align-mode-rules-list
  12334. '((org-in-buffer-settings
  12335. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12336. (modes . '(org-mode))))))
  12337. (defun org-fill-paragraph (&optional justify)
  12338. "Re-align a table, pass through to fill-paragraph if no table."
  12339. (let ((table-p (org-at-table-p))
  12340. (table.el-p (org-at-table.el-p)))
  12341. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12342. (save-excursion (goto-char (point-at-bol))
  12343. (looking-at outline-regexp)))
  12344. t) ; skip headlines
  12345. (table.el-p t) ; skip table.el tables
  12346. (table-p (org-table-align) t) ; align org-mode tables
  12347. (t nil)))) ; call paragraph-fill
  12348. ;; For reference, this is the default value of adaptive-fill-regexp
  12349. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12350. (defun org-adaptive-fill-function ()
  12351. "Return a fill prefix for org-mode files.
  12352. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12353. work correctly."
  12354. (cond ((looking-at "#[ \t]+")
  12355. (match-string 0))
  12356. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12357. (save-excursion
  12358. (goto-char (match-end 0))
  12359. (make-string (current-column) ?\ )))
  12360. (t nil)))
  12361. ;;; Other stuff.
  12362. (defun org-toggle-fixed-width-section (arg)
  12363. "Toggle the fixed-width export.
  12364. If there is no active region, the QUOTE keyword at the current headline is
  12365. inserted or removed. When present, it causes the text between this headline
  12366. and the next to be exported as fixed-width text, and unmodified.
  12367. If there is an active region, this command adds or removes a colon as the
  12368. first character of this line. If the first character of a line is a colon,
  12369. this line is also exported in fixed-width font."
  12370. (interactive "P")
  12371. (let* ((cc 0)
  12372. (regionp (org-region-active-p))
  12373. (beg (if regionp (region-beginning) (point)))
  12374. (end (if regionp (region-end)))
  12375. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12376. (case-fold-search nil)
  12377. (re "[ \t]*\\(:\\)")
  12378. off)
  12379. (if regionp
  12380. (save-excursion
  12381. (goto-char beg)
  12382. (setq cc (current-column))
  12383. (beginning-of-line 1)
  12384. (setq off (looking-at re))
  12385. (while (> nlines 0)
  12386. (setq nlines (1- nlines))
  12387. (beginning-of-line 1)
  12388. (cond
  12389. (arg
  12390. (org-move-to-column cc t)
  12391. (insert ":\n")
  12392. (forward-line -1))
  12393. ((and off (looking-at re))
  12394. (replace-match "" t t nil 1))
  12395. ((not off) (org-move-to-column cc t) (insert ":")))
  12396. (forward-line 1)))
  12397. (save-excursion
  12398. (org-back-to-heading)
  12399. (if (looking-at (concat outline-regexp
  12400. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12401. (replace-match "" t t nil 1)
  12402. (if (looking-at outline-regexp)
  12403. (progn
  12404. (goto-char (match-end 0))
  12405. (insert org-quote-string " "))))))))
  12406. ;;;; Functions extending outline functionality
  12407. (defun org-beginning-of-line (&optional arg)
  12408. "Go to the beginning of the current line. If that is invisible, continue
  12409. to a visible line beginning. This makes the function of C-a more intuitive.
  12410. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12411. first attempt, and only move to after the tags when the cursor is already
  12412. beyond the end of the headline."
  12413. (interactive "P")
  12414. (let ((pos (point)))
  12415. (beginning-of-line 1)
  12416. (if (bobp)
  12417. nil
  12418. (backward-char 1)
  12419. (if (org-invisible-p)
  12420. (while (and (not (bobp)) (org-invisible-p))
  12421. (backward-char 1)
  12422. (beginning-of-line 1))
  12423. (forward-char 1)))
  12424. (when org-special-ctrl-a/e
  12425. (cond
  12426. ((and (looking-at org-todo-line-regexp)
  12427. (= (char-after (match-end 1)) ?\ ))
  12428. (goto-char
  12429. (if (eq org-special-ctrl-a/e t)
  12430. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12431. ((= pos (point)) (match-beginning 3))
  12432. (t (point)))
  12433. (cond ((> pos (point)) (point))
  12434. ((not (eq last-command this-command)) (point))
  12435. (t (match-beginning 3))))))
  12436. ((org-at-item-p)
  12437. (goto-char
  12438. (if (eq org-special-ctrl-a/e t)
  12439. (cond ((> pos (match-end 4)) (match-end 4))
  12440. ((= pos (point)) (match-end 4))
  12441. (t (point)))
  12442. (cond ((> pos (point)) (point))
  12443. ((not (eq last-command this-command)) (point))
  12444. (t (match-end 4))))))))))
  12445. (defun org-end-of-line (&optional arg)
  12446. "Go to the end of the line.
  12447. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12448. first attempt, and only move to after the tags when the cursor is already
  12449. beyond the end of the headline."
  12450. (interactive "P")
  12451. (if (or (not org-special-ctrl-a/e)
  12452. (not (org-on-heading-p)))
  12453. (end-of-line arg)
  12454. (let ((pos (point)))
  12455. (beginning-of-line 1)
  12456. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12457. (if (eq org-special-ctrl-a/e t)
  12458. (if (or (< pos (match-beginning 1))
  12459. (= pos (match-end 0)))
  12460. (goto-char (match-beginning 1))
  12461. (goto-char (match-end 0)))
  12462. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12463. (goto-char (match-end 0))
  12464. (goto-char (match-beginning 1))))
  12465. (end-of-line arg)))))
  12466. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12467. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12468. (defun org-kill-line (&optional arg)
  12469. "Kill line, to tags or end of line."
  12470. (interactive "P")
  12471. (cond
  12472. ((or (not org-special-ctrl-k)
  12473. (bolp)
  12474. (not (org-on-heading-p)))
  12475. (call-interactively 'kill-line))
  12476. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12477. (kill-region (point) (match-beginning 1))
  12478. (org-set-tags nil t))
  12479. (t (kill-region (point) (point-at-eol)))))
  12480. (define-key org-mode-map "\C-k" 'org-kill-line)
  12481. (defun org-invisible-p ()
  12482. "Check if point is at a character currently not visible."
  12483. ;; Early versions of noutline don't have `outline-invisible-p'.
  12484. (if (fboundp 'outline-invisible-p)
  12485. (outline-invisible-p)
  12486. (get-char-property (point) 'invisible)))
  12487. (defun org-invisible-p2 ()
  12488. "Check if point is at a character currently not visible."
  12489. (save-excursion
  12490. (if (and (eolp) (not (bobp))) (backward-char 1))
  12491. ;; Early versions of noutline don't have `outline-invisible-p'.
  12492. (if (fboundp 'outline-invisible-p)
  12493. (outline-invisible-p)
  12494. (get-char-property (point) 'invisible))))
  12495. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12496. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12497. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12498. (defun org-at-heading-or-item-p ()
  12499. (or (org-on-heading-p) (org-at-item-p)))
  12500. (defun org-on-target-p ()
  12501. (or (org-in-regexp org-radio-target-regexp)
  12502. (org-in-regexp org-target-regexp)))
  12503. (defun org-up-heading-all (arg)
  12504. "Move to the heading line of which the present line is a subheading.
  12505. This function considers both visible and invisible heading lines.
  12506. With argument, move up ARG levels."
  12507. (if (fboundp 'outline-up-heading-all)
  12508. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12509. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12510. (defun org-up-heading-safe ()
  12511. "Move to the heading line of which the present line is a subheading.
  12512. This version will not throw an error. It will return the level of the
  12513. headline found, or nil if no higher level is found."
  12514. (let ((pos (point)) start-level level
  12515. (re (concat "^" outline-regexp)))
  12516. (catch 'exit
  12517. (outline-back-to-heading t)
  12518. (setq start-level (funcall outline-level))
  12519. (if (equal start-level 1) (throw 'exit nil))
  12520. (while (re-search-backward re nil t)
  12521. (setq level (funcall outline-level))
  12522. (if (< level start-level) (throw 'exit level)))
  12523. nil)))
  12524. (defun org-first-sibling-p ()
  12525. "Is this heading the first child of its parents?"
  12526. (interactive)
  12527. (let ((re (concat "^" outline-regexp))
  12528. level l)
  12529. (unless (org-at-heading-p t)
  12530. (error "Not at a heading"))
  12531. (setq level (funcall outline-level))
  12532. (save-excursion
  12533. (if (not (re-search-backward re nil t))
  12534. t
  12535. (setq l (funcall outline-level))
  12536. (< l level)))))
  12537. (defun org-goto-sibling (&optional previous)
  12538. "Goto the next sibling, even if it is invisible.
  12539. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12540. when a sibling was found. When none is found, return nil and don't
  12541. move point."
  12542. (let ((fun (if previous 're-search-backward 're-search-forward))
  12543. (pos (point))
  12544. (re (concat "^" outline-regexp))
  12545. level l)
  12546. (when (condition-case nil (org-back-to-heading t) (error nil))
  12547. (setq level (funcall outline-level))
  12548. (catch 'exit
  12549. (or previous (forward-char 1))
  12550. (while (funcall fun re nil t)
  12551. (setq l (funcall outline-level))
  12552. (when (< l level) (goto-char pos) (throw 'exit nil))
  12553. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12554. (goto-char pos)
  12555. nil))))
  12556. (defun org-show-siblings ()
  12557. "Show all siblings of the current headline."
  12558. (save-excursion
  12559. (while (org-goto-sibling) (org-flag-heading nil)))
  12560. (save-excursion
  12561. (while (org-goto-sibling 'previous)
  12562. (org-flag-heading nil))))
  12563. (defun org-show-hidden-entry ()
  12564. "Show an entry where even the heading is hidden."
  12565. (save-excursion
  12566. (org-show-entry)))
  12567. (defun org-flag-heading (flag &optional entry)
  12568. "Flag the current heading. FLAG non-nil means make invisible.
  12569. When ENTRY is non-nil, show the entire entry."
  12570. (save-excursion
  12571. (org-back-to-heading t)
  12572. ;; Check if we should show the entire entry
  12573. (if entry
  12574. (progn
  12575. (org-show-entry)
  12576. (save-excursion
  12577. (and (outline-next-heading)
  12578. (org-flag-heading nil))))
  12579. (outline-flag-region (max (point-min) (1- (point)))
  12580. (save-excursion (outline-end-of-heading) (point))
  12581. flag))))
  12582. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12583. ;; This is an exact copy of the original function, but it uses
  12584. ;; `org-back-to-heading', to make it work also in invisible
  12585. ;; trees. And is uses an invisible-OK argument.
  12586. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12587. (org-back-to-heading invisible-OK)
  12588. (let ((first t)
  12589. (level (funcall outline-level)))
  12590. (while (and (not (eobp))
  12591. (or first (> (funcall outline-level) level)))
  12592. (setq first nil)
  12593. (outline-next-heading))
  12594. (unless to-heading
  12595. (if (memq (preceding-char) '(?\n ?\^M))
  12596. (progn
  12597. ;; Go to end of line before heading
  12598. (forward-char -1)
  12599. (if (memq (preceding-char) '(?\n ?\^M))
  12600. ;; leave blank line before heading
  12601. (forward-char -1))))))
  12602. (point))
  12603. (defun org-show-subtree ()
  12604. "Show everything after this heading at deeper levels."
  12605. (outline-flag-region
  12606. (point)
  12607. (save-excursion
  12608. (outline-end-of-subtree) (outline-next-heading) (point))
  12609. nil))
  12610. (defun org-show-entry ()
  12611. "Show the body directly following this heading.
  12612. Show the heading too, if it is currently invisible."
  12613. (interactive)
  12614. (save-excursion
  12615. (condition-case nil
  12616. (progn
  12617. (org-back-to-heading t)
  12618. (outline-flag-region
  12619. (max (point-min) (1- (point)))
  12620. (save-excursion
  12621. (re-search-forward
  12622. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12623. (or (match-beginning 1) (point-max)))
  12624. nil))
  12625. (error nil))))
  12626. (defun org-make-options-regexp (kwds)
  12627. "Make a regular expression for keyword lines."
  12628. (concat
  12629. "^"
  12630. "#?[ \t]*\\+\\("
  12631. (mapconcat 'regexp-quote kwds "\\|")
  12632. "\\):[ \t]*"
  12633. "\\(.+\\)"))
  12634. ;; Make isearch reveal the necessary context
  12635. (defun org-isearch-end ()
  12636. "Reveal context after isearch exits."
  12637. (when isearch-success ; only if search was successful
  12638. (if (featurep 'xemacs)
  12639. ;; Under XEmacs, the hook is run in the correct place,
  12640. ;; we directly show the context.
  12641. (org-show-context 'isearch)
  12642. ;; In Emacs the hook runs *before* restoring the overlays.
  12643. ;; So we have to use a one-time post-command-hook to do this.
  12644. ;; (Emacs 22 has a special variable, see function `org-mode')
  12645. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12646. isearch-mode-end-hook-quit)
  12647. ;; Only when the isearch was not quitted.
  12648. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12649. 'append 'local)))))
  12650. (defun org-isearch-post-command ()
  12651. "Remove self from hook, and show context."
  12652. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12653. (org-show-context 'isearch))
  12654. ;;;; Integration with and fixes for other packages
  12655. ;;; Imenu support
  12656. (defvar org-imenu-markers nil
  12657. "All markers currently used by Imenu.")
  12658. (make-variable-buffer-local 'org-imenu-markers)
  12659. (defun org-imenu-new-marker (&optional pos)
  12660. "Return a new marker for use by Imenu, and remember the marker."
  12661. (let ((m (make-marker)))
  12662. (move-marker m (or pos (point)))
  12663. (push m org-imenu-markers)
  12664. m))
  12665. (defun org-imenu-get-tree ()
  12666. "Produce the index for Imenu."
  12667. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12668. (setq org-imenu-markers nil)
  12669. (let* ((n org-imenu-depth)
  12670. (re (concat "^" outline-regexp))
  12671. (subs (make-vector (1+ n) nil))
  12672. (last-level 0)
  12673. m tree level head)
  12674. (save-excursion
  12675. (save-restriction
  12676. (widen)
  12677. (goto-char (point-max))
  12678. (while (re-search-backward re nil t)
  12679. (setq level (org-reduced-level (funcall outline-level)))
  12680. (when (<= level n)
  12681. (looking-at org-complex-heading-regexp)
  12682. (setq head (org-match-string-no-properties 4)
  12683. m (org-imenu-new-marker))
  12684. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12685. (if (>= level last-level)
  12686. (push (cons head m) (aref subs level))
  12687. (push (cons head (aref subs (1+ level))) (aref subs level))
  12688. (loop for i from (1+ level) to n do (aset subs i nil)))
  12689. (setq last-level level)))))
  12690. (aref subs 1)))
  12691. (eval-after-load "imenu"
  12692. '(progn
  12693. (add-hook 'imenu-after-jump-hook
  12694. (lambda () (org-show-context 'org-goto)))))
  12695. ;; Speedbar support
  12696. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12697. "Overlay marking the agenda restriction line in speedbar.")
  12698. (org-overlay-put org-speedbar-restriction-lock-overlay
  12699. 'face 'org-agenda-restriction-lock)
  12700. (org-overlay-put org-speedbar-restriction-lock-overlay
  12701. 'help-echo "Agendas are currently limited to this item.")
  12702. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12703. (defun org-speedbar-set-agenda-restriction ()
  12704. "Restrict future agenda commands to the location at point in speedbar.
  12705. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12706. (interactive)
  12707. (require 'org-agenda)
  12708. (let (p m tp np dir txt w)
  12709. (cond
  12710. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12711. 'org-imenu t))
  12712. (setq m (get-text-property p 'org-imenu-marker))
  12713. (save-excursion
  12714. (save-restriction
  12715. (set-buffer (marker-buffer m))
  12716. (goto-char m)
  12717. (org-agenda-set-restriction-lock 'subtree))))
  12718. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12719. 'speedbar-function 'speedbar-find-file))
  12720. (setq tp (previous-single-property-change
  12721. (1+ p) 'speedbar-function)
  12722. np (next-single-property-change
  12723. tp 'speedbar-function)
  12724. dir (speedbar-line-directory)
  12725. txt (buffer-substring-no-properties (or tp (point-min))
  12726. (or np (point-max))))
  12727. (save-excursion
  12728. (save-restriction
  12729. (set-buffer (find-file-noselect
  12730. (let ((default-directory dir))
  12731. (expand-file-name txt))))
  12732. (unless (org-mode-p)
  12733. (error "Cannot restrict to non-Org-mode file"))
  12734. (org-agenda-set-restriction-lock 'file))))
  12735. (t (error "Don't know how to restrict Org-mode's agenda")))
  12736. (org-move-overlay org-speedbar-restriction-lock-overlay
  12737. (point-at-bol) (point-at-eol))
  12738. (setq current-prefix-arg nil)
  12739. (org-agenda-maybe-redo)))
  12740. (eval-after-load "speedbar"
  12741. '(progn
  12742. (speedbar-add-supported-extension ".org")
  12743. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12744. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12745. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12746. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12747. (add-hook 'speedbar-visiting-tag-hook
  12748. (lambda () (org-show-context 'org-goto)))))
  12749. ;;; Fixes and Hacks for problems with other packages
  12750. ;; Make flyspell not check words in links, to not mess up our keymap
  12751. (defun org-mode-flyspell-verify ()
  12752. "Don't let flyspell put overlays at active buttons."
  12753. (not (get-text-property (point) 'keymap)))
  12754. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12755. (eval-after-load "bookmark"
  12756. '(if (boundp 'bookmark-after-jump-hook)
  12757. ;; We can use the hook
  12758. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12759. ;; Hook not available, use advice
  12760. (defadvice bookmark-jump (after org-make-visible activate)
  12761. "Make the position visible."
  12762. (org-bookmark-jump-unhide))))
  12763. (defun org-bookmark-jump-unhide ()
  12764. "Unhide the current position, to show the bookmark location."
  12765. (and (org-mode-p)
  12766. (or (org-invisible-p)
  12767. (save-excursion (goto-char (max (point-min) (1- (point))))
  12768. (org-invisible-p)))
  12769. (org-show-context 'bookmark-jump)))
  12770. ;; Make session.el ignore our circular variable
  12771. (eval-after-load "session"
  12772. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12773. ;;;; Experimental code
  12774. (defun org-closed-in-range ()
  12775. "Sparse tree of items closed in a certain time range.
  12776. Still experimental, may disappear in the future."
  12777. (interactive)
  12778. ;; Get the time interval from the user.
  12779. (let* ((time1 (time-to-seconds
  12780. (org-read-date nil 'to-time nil "Starting date: ")))
  12781. (time2 (time-to-seconds
  12782. (org-read-date nil 'to-time nil "End date:")))
  12783. ;; callback function
  12784. (callback (lambda ()
  12785. (let ((time
  12786. (time-to-seconds
  12787. (apply 'encode-time
  12788. (org-parse-time-string
  12789. (match-string 1))))))
  12790. ;; check if time in interval
  12791. (and (>= time time1) (<= time time2))))))
  12792. ;; make tree, check each match with the callback
  12793. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12794. ;;;; Finish up
  12795. (provide 'org)
  12796. (run-hooks 'org-load-hook)
  12797. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12798. ;;; org.el ends here