org.el 543 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650
  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.13trans
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. (require 'org-list)
  84. ;;;; Customization variables
  85. ;;; Version
  86. (defconst org-version "6.13trans"
  87. "The version number of the file org.el.")
  88. (defun org-version (&optional here)
  89. "Show the org-mode version in the echo area.
  90. With prefix arg HERE, insert it at point."
  91. (interactive "P")
  92. (let ((version (format "Org-mode version %s" org-version)))
  93. (message version)
  94. (if here
  95. (insert version))))
  96. ;;; Compatibility constants
  97. ;;; The custom variables
  98. (defgroup org nil
  99. "Outline-based notes management and organizer."
  100. :tag "Org"
  101. :group 'outlines
  102. :group 'hypermedia
  103. :group 'calendar)
  104. (defcustom org-load-hook nil
  105. "Hook that is run after org.el has been loaded."
  106. :group 'org
  107. :type 'hook)
  108. (defvar org-modules) ; defined below
  109. (defvar org-modules-loaded nil
  110. "Have the modules been loaded already?")
  111. (defun org-load-modules-maybe (&optional force)
  112. "Load all extensions listed in `org-modules'."
  113. (when (or force (not org-modules-loaded))
  114. (mapc (lambda (ext)
  115. (condition-case nil (require ext)
  116. (error (message "Problems while trying to load feature `%s'" ext))))
  117. org-modules)
  118. (setq org-modules-loaded t)))
  119. (defun org-set-modules (var value)
  120. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  121. (set var value)
  122. (when (featurep 'org)
  123. (org-load-modules-maybe 'force)))
  124. (when (org-bound-and-true-p org-modules)
  125. (let ((a (member 'org-infojs org-modules)))
  126. (and a (setcar a 'org-jsinfo))))
  127. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-jsinfo org-irc org-mew org-mhe org-rmail org-vm org-w3m org-wl)
  128. "Modules that should always be loaded together with org.el.
  129. If a description starts with <C>, the file is not part of Emacs
  130. and loading it will require that you have downloaded and properly installed
  131. the org-mode distribution.
  132. You can also use this system to load external packages (i.e. neither Org
  133. core modules, not modules from the CONTRIB directory). Just add symbols
  134. to the end of the list. If the package is called org-xyz.el, then you need
  135. to add the symbol `xyz', and the package must have a call to
  136. (provide 'org-xyz)"
  137. :group 'org
  138. :set 'org-set-modules
  139. :type
  140. '(set :greedy t
  141. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  142. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  143. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  144. (const :tag " id: Global id's for identifying entries" org-id)
  145. (const :tag " info: Links to Info nodes" org-info)
  146. (const :tag " jsinfo: Set up Sebastian Rose's JavaScript org-info.js" org-jsinfo)
  147. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  148. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  149. (const :tag " mew Links to Mew folders/messages" org-mew)
  150. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  151. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  152. (const :tag " vm: Links to VM folders/messages" org-vm)
  153. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  154. (const :tag " w3m: Special cut/past from w3m to Org." org-w3m)
  155. (const :tag " mouse: Additional mouse support" org-mouse)
  156. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  157. (const :tag "C annotation-helper: Call Remeber directly from Browser" org-annotation-helper)
  158. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  159. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  160. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  161. (const :tag "C eval: Include command output as text" org-eval)
  162. (const :tag "C eval-light: Evaluate inbuffer-code on demand" org-eval-light)
  163. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  164. (const :tag "C exp-blocks: Pre-process blocks for export" org-exp-blocks)
  165. (const :tag "C id: Global id's for identifying entries" org-id)
  166. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  167. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  168. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  169. (const :tag "C mtags: Support for muse-like tags" org-mtags)
  170. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  171. (const :tag "C registry: A registry for Org links" org-registry)
  172. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  173. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  174. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  175. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  176. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  177. (defgroup org-startup nil
  178. "Options concerning startup of Org-mode."
  179. :tag "Org Startup"
  180. :group 'org)
  181. (defcustom org-startup-folded t
  182. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  183. This can also be configured on a per-file basis by adding one of
  184. the following lines anywhere in the buffer:
  185. #+STARTUP: fold
  186. #+STARTUP: nofold
  187. #+STARTUP: content"
  188. :group 'org-startup
  189. :type '(choice
  190. (const :tag "nofold: show all" nil)
  191. (const :tag "fold: overview" t)
  192. (const :tag "content: all headlines" content)))
  193. (defcustom org-startup-truncated t
  194. "Non-nil means, entering Org-mode will set `truncate-lines'.
  195. This is useful since some lines containing links can be very long and
  196. uninteresting. Also tables look terrible when wrapped."
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-startup-align-all-tables nil
  200. "Non-nil means, align all tables when visiting a file.
  201. This is useful when the column width in tables is forced with <N> cookies
  202. in table fields. Such tables will look correct only after the first re-align.
  203. This can also be configured on a per-file basis by adding one of
  204. the following lines anywhere in the buffer:
  205. #+STARTUP: align
  206. #+STARTUP: noalign"
  207. :group 'org-startup
  208. :type 'boolean)
  209. (defcustom org-insert-mode-line-in-empty-file nil
  210. "Non-nil means insert the first line setting Org-mode in empty files.
  211. When the function `org-mode' is called interactively in an empty file, this
  212. normally means that the file name does not automatically trigger Org-mode.
  213. To ensure that the file will always be in Org-mode in the future, a
  214. line enforcing Org-mode will be inserted into the buffer, if this option
  215. has been set."
  216. :group 'org-startup
  217. :type 'boolean)
  218. (defcustom org-replace-disputed-keys nil
  219. "Non-nil means use alternative key bindings for some keys.
  220. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  221. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  222. If you want to use Org-mode together with one of these other modes,
  223. or more generally if you would like to move some Org-mode commands to
  224. other keys, set this variable and configure the keys with the variable
  225. `org-disputed-keys'.
  226. This option is only relevant at load-time of Org-mode, and must be set
  227. *before* org.el is loaded. Changing it requires a restart of Emacs to
  228. become effective."
  229. :group 'org-startup
  230. :type 'boolean)
  231. (defcustom org-use-extra-keys nil
  232. "Non-nil means use extra key sequence definitions for certain
  233. commands. This happens automatically if you run XEmacs or if
  234. window-system is nil. This variable lets you do the same
  235. manually. You must set it before loading org.
  236. Example: on Carbon Emacs 22 running graphically, with an external
  237. keyboard on a Powerbook, the default way of setting M-left might
  238. not work for either Alt or ESC. Setting this variable will make
  239. it work for ESC."
  240. :group 'org-startup
  241. :type 'boolean)
  242. (if (fboundp 'defvaralias)
  243. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  244. (defcustom org-disputed-keys
  245. '(([(shift up)] . [(meta p)])
  246. ([(shift down)] . [(meta n)])
  247. ([(shift left)] . [(meta -)])
  248. ([(shift right)] . [(meta +)])
  249. ([(control shift right)] . [(meta shift +)])
  250. ([(control shift left)] . [(meta shift -)]))
  251. "Keys for which Org-mode and other modes compete.
  252. This is an alist, cars are the default keys, second element specifies
  253. the alternative to use when `org-replace-disputed-keys' is t.
  254. Keys can be specified in any syntax supported by `define-key'.
  255. The value of this option takes effect only at Org-mode's startup,
  256. therefore you'll have to restart Emacs to apply it after changing."
  257. :group 'org-startup
  258. :type 'alist)
  259. (defun org-key (key)
  260. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  261. Or return the original if not disputed."
  262. (if org-replace-disputed-keys
  263. (let* ((nkey (key-description key))
  264. (x (org-find-if (lambda (x)
  265. (equal (key-description (car x)) nkey))
  266. org-disputed-keys)))
  267. (if x (cdr x) key))
  268. key))
  269. (defun org-find-if (predicate seq)
  270. (catch 'exit
  271. (while seq
  272. (if (funcall predicate (car seq))
  273. (throw 'exit (car seq))
  274. (pop seq)))))
  275. (defun org-defkey (keymap key def)
  276. "Define a key, possibly translated, as returned by `org-key'."
  277. (define-key keymap (org-key key) def))
  278. (defcustom org-ellipsis nil
  279. "The ellipsis to use in the Org-mode outline.
  280. When nil, just use the standard three dots. When a string, use that instead,
  281. When a face, use the standart 3 dots, but with the specified face.
  282. The change affects only Org-mode (which will then use its own display table).
  283. Changing this requires executing `M-x org-mode' in a buffer to become
  284. effective."
  285. :group 'org-startup
  286. :type '(choice (const :tag "Default" nil)
  287. (face :tag "Face" :value org-warning)
  288. (string :tag "String" :value "...#")))
  289. (defvar org-display-table nil
  290. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  291. (defgroup org-keywords nil
  292. "Keywords in Org-mode."
  293. :tag "Org Keywords"
  294. :group 'org)
  295. (defcustom org-deadline-string "DEADLINE:"
  296. "String to mark deadline entries.
  297. A deadline is this string, followed by a time stamp. Should be a word,
  298. terminated by a colon. You can insert a schedule keyword and
  299. a timestamp with \\[org-deadline].
  300. Changes become only effective after restarting Emacs."
  301. :group 'org-keywords
  302. :type 'string)
  303. (defcustom org-scheduled-string "SCHEDULED:"
  304. "String to mark scheduled TODO entries.
  305. A schedule is this string, followed by a time stamp. Should be a word,
  306. terminated by a colon. You can insert a schedule keyword and
  307. a timestamp with \\[org-schedule].
  308. Changes become only effective after restarting Emacs."
  309. :group 'org-keywords
  310. :type 'string)
  311. (defcustom org-closed-string "CLOSED:"
  312. "String used as the prefix for timestamps logging closing a TODO entry."
  313. :group 'org-keywords
  314. :type 'string)
  315. (defcustom org-clock-string "CLOCK:"
  316. "String used as prefix for timestamps clocking work hours on an item."
  317. :group 'org-keywords
  318. :type 'string)
  319. (defcustom org-comment-string "COMMENT"
  320. "Entries starting with this keyword will never be exported.
  321. An entry can be toggled between COMMENT and normal with
  322. \\[org-toggle-comment].
  323. Changes become only effective after restarting Emacs."
  324. :group 'org-keywords
  325. :type 'string)
  326. (defcustom org-quote-string "QUOTE"
  327. "Entries starting with this keyword will be exported in fixed-width font.
  328. Quoting applies only to the text in the entry following the headline, and does
  329. not extend beyond the next headline, even if that is lower level.
  330. An entry can be toggled between QUOTE and normal with
  331. \\[org-toggle-fixed-width-section]."
  332. :group 'org-keywords
  333. :type 'string)
  334. (defconst org-repeat-re
  335. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  336. "Regular expression for specifying repeated events.
  337. After a match, group 1 contains the repeat expression.")
  338. (defgroup org-structure nil
  339. "Options concerning the general structure of Org-mode files."
  340. :tag "Org Structure"
  341. :group 'org)
  342. (defgroup org-reveal-location nil
  343. "Options about how to make context of a location visible."
  344. :tag "Org Reveal Location"
  345. :group 'org-structure)
  346. (defconst org-context-choice
  347. '(choice
  348. (const :tag "Always" t)
  349. (const :tag "Never" nil)
  350. (repeat :greedy t :tag "Individual contexts"
  351. (cons
  352. (choice :tag "Context"
  353. (const agenda)
  354. (const org-goto)
  355. (const occur-tree)
  356. (const tags-tree)
  357. (const link-search)
  358. (const mark-goto)
  359. (const bookmark-jump)
  360. (const isearch)
  361. (const default))
  362. (boolean))))
  363. "Contexts for the reveal options.")
  364. (defcustom org-show-hierarchy-above '((default . t))
  365. "Non-nil means, show full hierarchy when revealing a location.
  366. Org-mode often shows locations in an org-mode file which might have
  367. been invisible before. When this is set, the hierarchy of headings
  368. above the exposed location is shown.
  369. Turning this off for example for sparse trees makes them very compact.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. Valid contexts are
  372. agenda when exposing an entry from the agenda
  373. org-goto when using the command `org-goto' on key C-c C-j
  374. occur-tree when using the command `org-occur' on key C-c /
  375. tags-tree when constructing a sparse tree based on tags matches
  376. link-search when exposing search matches associated with a link
  377. mark-goto when exposing the jump goal of a mark
  378. bookmark-jump when exposing a bookmark location
  379. isearch when exiting from an incremental search
  380. default default for all contexts not set explicitly"
  381. :group 'org-reveal-location
  382. :type org-context-choice)
  383. (defcustom org-show-following-heading '((default . nil))
  384. "Non-nil means, show following heading when revealing a location.
  385. Org-mode often shows locations in an org-mode file which might have
  386. been invisible before. When this is set, the heading following the
  387. match is shown.
  388. Turning this off for example for sparse trees makes them very compact,
  389. but makes it harder to edit the location of the match. In such a case,
  390. use the command \\[org-reveal] to show more context.
  391. Instead of t, this can also be an alist specifying this option for different
  392. contexts. See `org-show-hierarchy-above' for valid contexts."
  393. :group 'org-reveal-location
  394. :type org-context-choice)
  395. (defcustom org-show-siblings '((default . nil) (isearch t))
  396. "Non-nil means, show all sibling heading when revealing a location.
  397. Org-mode often shows locations in an org-mode file which might have
  398. been invisible before. When this is set, the sibling of the current entry
  399. heading are all made visible. If `org-show-hierarchy-above' is t,
  400. the same happens on each level of the hierarchy above the current entry.
  401. By default this is on for the isearch context, off for all other contexts.
  402. Turning this off for example for sparse trees makes them very compact,
  403. but makes it harder to edit the location of the match. In such a case,
  404. use the command \\[org-reveal] to show more context.
  405. Instead of t, this can also be an alist specifying this option for different
  406. contexts. See `org-show-hierarchy-above' for valid contexts."
  407. :group 'org-reveal-location
  408. :type org-context-choice)
  409. (defcustom org-show-entry-below '((default . nil))
  410. "Non-nil means, show the entry below a headline when revealing a location.
  411. Org-mode often shows locations in an org-mode file which might have
  412. been invisible before. When this is set, the text below the headline that is
  413. exposed is also shown.
  414. By default this is off for all contexts.
  415. Instead of t, this can also be an alist specifying this option for different
  416. contexts. See `org-show-hierarchy-above' for valid contexts."
  417. :group 'org-reveal-location
  418. :type org-context-choice)
  419. (defcustom org-indirect-buffer-display 'other-window
  420. "How should indirect tree buffers be displayed?
  421. This applies to indirect buffers created with the commands
  422. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  423. Valid values are:
  424. current-window Display in the current window
  425. other-window Just display in another window.
  426. dedicated-frame Create one new frame, and re-use it each time.
  427. new-frame Make a new frame each time. Note that in this case
  428. previously-made indirect buffers are kept, and you need to
  429. kill these buffers yourself."
  430. :group 'org-structure
  431. :group 'org-agenda-windows
  432. :type '(choice
  433. (const :tag "In current window" current-window)
  434. (const :tag "In current frame, other window" other-window)
  435. (const :tag "Each time a new frame" new-frame)
  436. (const :tag "One dedicated frame" dedicated-frame)))
  437. (defgroup org-cycle nil
  438. "Options concerning visibility cycling in Org-mode."
  439. :tag "Org Cycle"
  440. :group 'org-structure)
  441. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  442. "Names of drawers. Drawers are not opened by cycling on the headline above.
  443. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  444. this:
  445. :DRAWERNAME:
  446. .....
  447. :END:
  448. The drawer \"PROPERTIES\" is special for capturing properties through
  449. the property API.
  450. Drawers can be defined on the per-file basis with a line like:
  451. #+DRAWERS: HIDDEN STATE PROPERTIES"
  452. :group 'org-structure
  453. :type '(repeat (string :tag "Drawer Name")))
  454. (defcustom org-cycle-global-at-bob nil
  455. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  456. This makes it possible to do global cycling without having to use S-TAB or
  457. C-u TAB. For this special case to work, the first line of the buffer
  458. must not be a headline - it may be empty ot some other text. When used in
  459. this way, `org-cycle-hook' is disables temporarily, to make sure the
  460. cursor stays at the beginning of the buffer.
  461. When this option is nil, don't do anything special at the beginning
  462. of the buffer."
  463. :group 'org-cycle
  464. :type 'boolean)
  465. (defcustom org-cycle-emulate-tab t
  466. "Where should `org-cycle' emulate TAB.
  467. nil Never
  468. white Only in completely white lines
  469. whitestart Only at the beginning of lines, before the first non-white char
  470. t Everywhere except in headlines
  471. exc-hl-bol Everywhere except at the start of a headline
  472. If TAB is used in a place where it does not emulate TAB, the current subtree
  473. visibility is cycled."
  474. :group 'org-cycle
  475. :type '(choice (const :tag "Never" nil)
  476. (const :tag "Only in completely white lines" white)
  477. (const :tag "Before first char in a line" whitestart)
  478. (const :tag "Everywhere except in headlines" t)
  479. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  480. ))
  481. (defcustom org-cycle-separator-lines 2
  482. "Number of empty lines needed to keep an empty line between collapsed trees.
  483. If you leave an empty line between the end of a subtree and the following
  484. headline, this empty line is hidden when the subtree is folded.
  485. Org-mode will leave (exactly) one empty line visible if the number of
  486. empty lines is equal or larger to the number given in this variable.
  487. So the default 2 means, at least 2 empty lines after the end of a subtree
  488. are needed to produce free space between a collapsed subtree and the
  489. following headline.
  490. Special case: when 0, never leave empty lines in collapsed view."
  491. :group 'org-cycle
  492. :type 'integer)
  493. (put 'org-cycle-separator-lines 'safe-local-variable 'integerp)
  494. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  495. org-cycle-hide-drawers
  496. org-cycle-show-empty-lines
  497. org-optimize-window-after-visibility-change)
  498. "Hook that is run after `org-cycle' has changed the buffer visibility.
  499. The function(s) in this hook must accept a single argument which indicates
  500. the new state that was set by the most recent `org-cycle' command. The
  501. argument is a symbol. After a global state change, it can have the values
  502. `overview', `content', or `all'. After a local state change, it can have
  503. the values `folded', `children', or `subtree'."
  504. :group 'org-cycle
  505. :type 'hook)
  506. (defgroup org-edit-structure nil
  507. "Options concerning structure editing in Org-mode."
  508. :tag "Org Edit Structure"
  509. :group 'org-structure)
  510. (defcustom org-odd-levels-only nil
  511. "Non-nil means, skip even levels and only use odd levels for the outline.
  512. This has the effect that two stars are being added/taken away in
  513. promotion/demotion commands. It also influences how levels are
  514. handled by the exporters.
  515. Changing it requires restart of `font-lock-mode' to become effective
  516. for fontification also in regions already fontified.
  517. You may also set this on a per-file basis by adding one of the following
  518. lines to the buffer:
  519. #+STARTUP: odd
  520. #+STARTUP: oddeven"
  521. :group 'org-edit-structure
  522. :group 'org-font-lock
  523. :type 'boolean)
  524. (defcustom org-adapt-indentation t
  525. "Non-nil means, adapt indentation when promoting and demoting.
  526. When this is set and the *entire* text in an entry is indented, the
  527. indentation is increased by one space in a demotion command, and
  528. decreased by one in a promotion command. If any line in the entry
  529. body starts at column 0, indentation is not changed at all."
  530. :group 'org-edit-structure
  531. :type 'boolean)
  532. (defcustom org-special-ctrl-a/e nil
  533. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  534. When t, `C-a' will bring back the cursor to the beginning of the
  535. headline text, i.e. after the stars and after a possible TODO keyword.
  536. In an item, this will be the position after the bullet.
  537. When the cursor is already at that position, another `C-a' will bring
  538. it to the beginning of the line.
  539. `C-e' will jump to the end of the headline, ignoring the presence of tags
  540. in the headline. A second `C-e' will then jump to the true end of the
  541. line, after any tags.
  542. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  543. and only a directly following, identical keypress will bring the cursor
  544. to the special positions."
  545. :group 'org-edit-structure
  546. :type '(choice
  547. (const :tag "off" nil)
  548. (const :tag "after bullet first" t)
  549. (const :tag "border first" reversed)))
  550. (if (fboundp 'defvaralias)
  551. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  552. (defcustom org-special-ctrl-k nil
  553. "Non-nil means `C-k' will behave specially in headlines.
  554. When nil, `C-k' will call the default `kill-line' command.
  555. When t, the following will happen while the cursor is in the headline:
  556. - When the cursor is at the beginning of a headline, kill the entire
  557. line and possible the folded subtree below the line.
  558. - When in the middle of the headline text, kill the headline up to the tags.
  559. - When after the headline text, kill the tags."
  560. :group 'org-edit-structure
  561. :type 'boolean)
  562. (defcustom org-yank-folded-subtrees t
  563. "Non-nil means, when yanking subtrees, fold them.
  564. If the kill is a single subtree, or a sequence of subtrees, i.e. if
  565. it starts with a heading and all other headings in it are either children
  566. or siblings, then fold all the subtrees. However, do this only if no
  567. text after the yank would be swallowed into a folded tree by this action."
  568. :group 'org-edit-structure
  569. :type 'boolean)
  570. (defcustom org-yank-adjusted-subtrees t
  571. "Non-nil means, when yanking subtrees, adjust the level.
  572. With this setting, `org-paste-subtree' is used to insert the subtree, see
  573. this function for details."
  574. :group 'org-edit-structure
  575. :type 'boolean)
  576. (defcustom org-M-RET-may-split-line '((default . t))
  577. "Non-nil means, M-RET will split the line at the cursor position.
  578. When nil, it will go to the end of the line before making a
  579. new line.
  580. You may also set this option in a different way for different
  581. contexts. Valid contexts are:
  582. headline when creating a new headline
  583. item when creating a new item
  584. table in a table field
  585. default the value to be used for all contexts not explicitly
  586. customized"
  587. :group 'org-structure
  588. :group 'org-table
  589. :type '(choice
  590. (const :tag "Always" t)
  591. (const :tag "Never" nil)
  592. (repeat :greedy t :tag "Individual contexts"
  593. (cons
  594. (choice :tag "Context"
  595. (const headline)
  596. (const item)
  597. (const table)
  598. (const default))
  599. (boolean)))))
  600. (defcustom org-insert-heading-respect-content nil
  601. "Non-nil means, insert new headings after the current subtree.
  602. When nil, the new heading is created directly after the current line.
  603. The commands \\[org-insert-heading-respect-content] and
  604. \\[org-insert-todo-heading-respect-content] turn this variable on
  605. for the duration of the command."
  606. :group 'org-structure
  607. :type 'boolean)
  608. (defcustom org-blank-before-new-entry '((heading . nil)
  609. (plain-list-item . nil))
  610. "Should `org-insert-heading' leave a blank line before new heading/item?
  611. The value is an alist, with `heading' and `plain-list-item' as car,
  612. and a boolean flag as cdr."
  613. :group 'org-edit-structure
  614. :type '(list
  615. (cons (const heading) (boolean))
  616. (cons (const plain-list-item) (boolean))))
  617. (defcustom org-insert-heading-hook nil
  618. "Hook being run after inserting a new heading."
  619. :group 'org-edit-structure
  620. :type 'hook)
  621. (defcustom org-enable-fixed-width-editor t
  622. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  623. This currently only means, they are never auto-wrapped.
  624. When nil, such lines will be treated like ordinary lines.
  625. See also the QUOTE keyword."
  626. :group 'org-edit-structure
  627. :type 'boolean)
  628. (defcustom org-edit-src-region-extra nil
  629. "Additional regexps to identify regions for editing with `org-edit-src-code'.
  630. For examples see the function `org-edit-src-find-region-and-lang'.
  631. The regular expression identifying the begin marker should end with a newline,
  632. and the regexp marking the end line should start with a newline, to make sure
  633. there are kept outside the narrowed region."
  634. :group 'org-edit-structure
  635. :type '(repeat
  636. (list
  637. (regexp :tag "begin regexp")
  638. (regexp :tag "end regexp")
  639. (choice :tag "language"
  640. (string :tag "specify")
  641. (integer :tag "from match group")
  642. (const :tag "from `lang' element")
  643. (const :tag "from `style' element")))))
  644. (defcustom org-edit-fixed-width-region-mode 'artist-mode
  645. "The mode that should be used to edit fixed-width regions.
  646. These are the regions where each line starts with a colon."
  647. :group 'org-edit-structure
  648. :type '(choice
  649. (const artist-mode)
  650. (const picture-mode)
  651. (const fundamental-mode)
  652. (function :tag "Other (specify)")))
  653. (defcustom org-goto-auto-isearch t
  654. "Non-nil means, typing characters in org-goto starts incremental search."
  655. :group 'org-edit-structure
  656. :type 'boolean)
  657. (defgroup org-sparse-trees nil
  658. "Options concerning sparse trees in Org-mode."
  659. :tag "Org Sparse Trees"
  660. :group 'org-structure)
  661. (defcustom org-highlight-sparse-tree-matches t
  662. "Non-nil means, highlight all matches that define a sparse tree.
  663. The highlights will automatically disappear the next time the buffer is
  664. changed by an edit command."
  665. :group 'org-sparse-trees
  666. :type 'boolean)
  667. (defcustom org-remove-highlights-with-change t
  668. "Non-nil means, any change to the buffer will remove temporary highlights.
  669. Such highlights are created by `org-occur' and `org-clock-display'.
  670. When nil, `C-c C-c needs to be used to get rid of the highlights.
  671. The highlights created by `org-preview-latex-fragment' always need
  672. `C-c C-c' to be removed."
  673. :group 'org-sparse-trees
  674. :group 'org-time
  675. :type 'boolean)
  676. (defcustom org-occur-hook '(org-first-headline-recenter)
  677. "Hook that is run after `org-occur' has constructed a sparse tree.
  678. This can be used to recenter the window to show as much of the structure
  679. as possible."
  680. :group 'org-sparse-trees
  681. :type 'hook)
  682. (defgroup org-imenu-and-speedbar nil
  683. "Options concerning imenu and speedbar in Org-mode."
  684. :tag "Org Imenu and Speedbar"
  685. :group 'org-structure)
  686. (defcustom org-imenu-depth 2
  687. "The maximum level for Imenu access to Org-mode headlines.
  688. This also applied for speedbar access."
  689. :group 'org-imenu-and-speedbar
  690. :type 'number)
  691. (defgroup org-table nil
  692. "Options concerning tables in Org-mode."
  693. :tag "Org Table"
  694. :group 'org)
  695. (defcustom org-enable-table-editor 'optimized
  696. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  697. When nil, such lines will be treated like ordinary lines.
  698. When equal to the symbol `optimized', the table editor will be optimized to
  699. do the following:
  700. - Automatic overwrite mode in front of whitespace in table fields.
  701. This makes the structure of the table stay in tact as long as the edited
  702. field does not exceed the column width.
  703. - Minimize the number of realigns. Normally, the table is aligned each time
  704. TAB or RET are pressed to move to another field. With optimization this
  705. happens only if changes to a field might have changed the column width.
  706. Optimization requires replacing the functions `self-insert-command',
  707. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  708. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  709. very good at guessing when a re-align will be necessary, but you can always
  710. force one with \\[org-ctrl-c-ctrl-c].
  711. If you would like to use the optimized version in Org-mode, but the
  712. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  713. This variable can be used to turn on and off the table editor during a session,
  714. but in order to toggle optimization, a restart is required.
  715. See also the variable `org-table-auto-blank-field'."
  716. :group 'org-table
  717. :type '(choice
  718. (const :tag "off" nil)
  719. (const :tag "on" t)
  720. (const :tag "on, optimized" optimized)))
  721. (defcustom org-table-tab-recognizes-table.el t
  722. "Non-nil means, TAB will automatically notice a table.el table.
  723. When it sees such a table, it moves point into it and - if necessary -
  724. calls `table-recognize-table'."
  725. :group 'org-table-editing
  726. :type 'boolean)
  727. (defgroup org-link nil
  728. "Options concerning links in Org-mode."
  729. :tag "Org Link"
  730. :group 'org)
  731. (defvar org-link-abbrev-alist-local nil
  732. "Buffer-local version of `org-link-abbrev-alist', which see.
  733. The value of this is taken from the #+LINK lines.")
  734. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  735. (defcustom org-link-abbrev-alist nil
  736. "Alist of link abbreviations.
  737. The car of each element is a string, to be replaced at the start of a link.
  738. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  739. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  740. [[linkkey:tag][description]]
  741. If REPLACE is a string, the tag will simply be appended to create the link.
  742. If the string contains \"%s\", the tag will be inserted there. Alternatively,
  743. the placeholder \"%h\" will cause a url-encoded version of the tag to
  744. be inserted at that point (see the function `url-hexify-string').
  745. REPLACE may also be a function that will be called with the tag as the
  746. only argument to create the link, which should be returned as a string.
  747. See the manual for examples."
  748. :group 'org-link
  749. :type '(repeat
  750. (cons
  751. (string :tag "Protocol")
  752. (choice
  753. (string :tag "Format")
  754. (function)))))
  755. (defcustom org-descriptive-links t
  756. "Non-nil means, hide link part and only show description of bracket links.
  757. Bracket links are like [[link][descritpion]]. This variable sets the initial
  758. state in new org-mode buffers. The setting can then be toggled on a
  759. per-buffer basis from the Org->Hyperlinks menu."
  760. :group 'org-link
  761. :type 'boolean)
  762. (defcustom org-link-file-path-type 'adaptive
  763. "How the path name in file links should be stored.
  764. Valid values are:
  765. relative Relative to the current directory, i.e. the directory of the file
  766. into which the link is being inserted.
  767. absolute Absolute path, if possible with ~ for home directory.
  768. noabbrev Absolute path, no abbreviation of home directory.
  769. adaptive Use relative path for files in the current directory and sub-
  770. directories of it. For other files, use an absolute path."
  771. :group 'org-link
  772. :type '(choice
  773. (const relative)
  774. (const absolute)
  775. (const noabbrev)
  776. (const adaptive)))
  777. (defcustom org-activate-links '(bracket angle plain radio tag date)
  778. "Types of links that should be activated in Org-mode files.
  779. This is a list of symbols, each leading to the activation of a certain link
  780. type. In principle, it does not hurt to turn on most link types - there may
  781. be a small gain when turning off unused link types. The types are:
  782. bracket The recommended [[link][description]] or [[link]] links with hiding.
  783. angular Links in angular brackes that may contain whitespace like
  784. <bbdb:Carsten Dominik>.
  785. plain Plain links in normal text, no whitespace, like http://google.com.
  786. radio Text that is matched by a radio target, see manual for details.
  787. tag Tag settings in a headline (link to tag search).
  788. date Time stamps (link to calendar).
  789. Changing this variable requires a restart of Emacs to become effective."
  790. :group 'org-link
  791. :type '(set (const :tag "Double bracket links (new style)" bracket)
  792. (const :tag "Angular bracket links (old style)" angular)
  793. (const :tag "Plain text links" plain)
  794. (const :tag "Radio target matches" radio)
  795. (const :tag "Tags" tag)
  796. (const :tag "Timestamps" date)))
  797. (defcustom org-make-link-description-function nil
  798. "Function to use to generate link descriptions from links. If
  799. nil the link location will be used. This function must take two
  800. parameters; the first is the link and the second the description
  801. org-insert-link has generated, and should return the description
  802. to use."
  803. :group 'org-link
  804. :type 'function)
  805. (defgroup org-link-store nil
  806. "Options concerning storing links in Org-mode."
  807. :tag "Org Store Link"
  808. :group 'org-link)
  809. (defcustom org-email-link-description-format "Email %c: %.30s"
  810. "Format of the description part of a link to an email or usenet message.
  811. The following %-excapes will be replaced by corresponding information:
  812. %F full \"From\" field
  813. %f name, taken from \"From\" field, address if no name
  814. %T full \"To\" field
  815. %t first name in \"To\" field, address if no name
  816. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  817. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  818. %s subject
  819. %m message-id.
  820. You may use normal field width specification between the % and the letter.
  821. This is for example useful to limit the length of the subject.
  822. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  823. :group 'org-link-store
  824. :type 'string)
  825. (defcustom org-from-is-user-regexp
  826. (let (r1 r2)
  827. (when (and user-mail-address (not (string= user-mail-address "")))
  828. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  829. (when (and user-full-name (not (string= user-full-name "")))
  830. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  831. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  832. "Regexp mached against the \"From:\" header of an email or usenet message.
  833. It should match if the message is from the user him/herself."
  834. :group 'org-link-store
  835. :type 'regexp)
  836. (defcustom org-context-in-file-links t
  837. "Non-nil means, file links from `org-store-link' contain context.
  838. A search string will be added to the file name with :: as separator and
  839. used to find the context when the link is activated by the command
  840. `org-open-at-point'.
  841. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  842. negates this setting for the duration of the command."
  843. :group 'org-link-store
  844. :type 'boolean)
  845. (defcustom org-keep-stored-link-after-insertion nil
  846. "Non-nil means, keep link in list for entire session.
  847. The command `org-store-link' adds a link pointing to the current
  848. location to an internal list. These links accumulate during a session.
  849. The command `org-insert-link' can be used to insert links into any
  850. Org-mode file (offering completion for all stored links). When this
  851. option is nil, every link which has been inserted once using \\[org-insert-link]
  852. will be removed from the list, to make completing the unused links
  853. more efficient."
  854. :group 'org-link-store
  855. :type 'boolean)
  856. (defgroup org-link-follow nil
  857. "Options concerning following links in Org-mode."
  858. :tag "Org Follow Link"
  859. :group 'org-link)
  860. (defcustom org-link-translation-function nil
  861. "Function to translate links with different syntax to Org syntax.
  862. This can be used to translate links created for example by the Planner
  863. or emacs-wiki packages to Org syntax.
  864. The function must accept two parameters, a TYPE containing the link
  865. protocol name like \"rmail\" or \"gnus\" as a string, and the linked path,
  866. which is everything after the link protocol. It should return a cons
  867. with possibly modifed values of type and path.
  868. Org contains a function for this, so if you set this variable to
  869. `org-translate-link-from-planner', you should be able follow many
  870. links created by planner."
  871. :group 'org-link-follow
  872. :type 'function)
  873. (defcustom org-follow-link-hook nil
  874. "Hook that is run after a link has been followed."
  875. :group 'org-link-follow
  876. :type 'hook)
  877. (defcustom org-tab-follows-link nil
  878. "Non-nil means, on links TAB will follow the link.
  879. Needs to be set before org.el is loaded."
  880. :group 'org-link-follow
  881. :type 'boolean)
  882. (defcustom org-return-follows-link nil
  883. "Non-nil means, on links RET will follow the link.
  884. Needs to be set before org.el is loaded."
  885. :group 'org-link-follow
  886. :type 'boolean)
  887. (defcustom org-mouse-1-follows-link
  888. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  889. "Non-nil means, mouse-1 on a link will follow the link.
  890. A longer mouse click will still set point. Does not work on XEmacs.
  891. Needs to be set before org.el is loaded."
  892. :group 'org-link-follow
  893. :type 'boolean)
  894. (defcustom org-mark-ring-length 4
  895. "Number of different positions to be recorded in the ring
  896. Changing this requires a restart of Emacs to work correctly."
  897. :group 'org-link-follow
  898. :type 'interger)
  899. (defcustom org-link-frame-setup
  900. '((vm . vm-visit-folder-other-frame)
  901. (gnus . gnus-other-frame)
  902. (file . find-file-other-window))
  903. "Setup the frame configuration for following links.
  904. When following a link with Emacs, it may often be useful to display
  905. this link in another window or frame. This variable can be used to
  906. set this up for the different types of links.
  907. For VM, use any of
  908. `vm-visit-folder'
  909. `vm-visit-folder-other-frame'
  910. For Gnus, use any of
  911. `gnus'
  912. `gnus-other-frame'
  913. `org-gnus-no-new-news'
  914. For FILE, use any of
  915. `find-file'
  916. `find-file-other-window'
  917. `find-file-other-frame'
  918. For the calendar, use the variable `calendar-setup'.
  919. For BBDB, it is currently only possible to display the matches in
  920. another window."
  921. :group 'org-link-follow
  922. :type '(list
  923. (cons (const vm)
  924. (choice
  925. (const vm-visit-folder)
  926. (const vm-visit-folder-other-window)
  927. (const vm-visit-folder-other-frame)))
  928. (cons (const gnus)
  929. (choice
  930. (const gnus)
  931. (const gnus-other-frame)
  932. (const org-gnus-no-new-news)))
  933. (cons (const file)
  934. (choice
  935. (const find-file)
  936. (const find-file-other-window)
  937. (const find-file-other-frame)))))
  938. (defcustom org-display-internal-link-with-indirect-buffer nil
  939. "Non-nil means, use indirect buffer to display infile links.
  940. Activating internal links (from one location in a file to another location
  941. in the same file) normally just jumps to the location. When the link is
  942. activated with a C-u prefix (or with mouse-3), the link is displayed in
  943. another window. When this option is set, the other window actually displays
  944. an indirect buffer clone of the current buffer, to avoid any visibility
  945. changes to the current buffer."
  946. :group 'org-link-follow
  947. :type 'boolean)
  948. (defcustom org-open-non-existing-files nil
  949. "Non-nil means, `org-open-file' will open non-existing files.
  950. When nil, an error will be generated."
  951. :group 'org-link-follow
  952. :type 'boolean)
  953. (defcustom org-open-directory-means-index-dot-org nil
  954. "Non-nil means, a link to a directory really means to index.org.
  955. When nil, following a directory link will run dired or open a finder/explorer
  956. window on that directory."
  957. :group 'org-link-follow
  958. :type 'boolean)
  959. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  960. "Function and arguments to call for following mailto links.
  961. This is a list with the first element being a lisp function, and the
  962. remaining elements being arguments to the function. In string arguments,
  963. %a will be replaced by the address, and %s will be replaced by the subject
  964. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  965. :group 'org-link-follow
  966. :type '(choice
  967. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  968. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  969. (const :tag "message-mail" (message-mail "%a" "%s"))
  970. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  971. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  972. "Non-nil means, ask for confirmation before executing shell links.
  973. Shell links can be dangerous: just think about a link
  974. [[shell:rm -rf ~/*][Google Search]]
  975. This link would show up in your Org-mode document as \"Google Search\",
  976. but really it would remove your entire home directory.
  977. Therefore we advise against setting this variable to nil.
  978. Just change it to `y-or-n-p' of you want to confirm with a
  979. single keystroke rather than having to type \"yes\"."
  980. :group 'org-link-follow
  981. :type '(choice
  982. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  983. (const :tag "with y-or-n (faster)" y-or-n-p)
  984. (const :tag "no confirmation (dangerous)" nil)))
  985. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  986. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  987. Elisp links can be dangerous: just think about a link
  988. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  989. This link would show up in your Org-mode document as \"Google Search\",
  990. but really it would remove your entire home directory.
  991. Therefore we advise against setting this variable to nil.
  992. Just change it to `y-or-n-p' of you want to confirm with a
  993. single keystroke rather than having to type \"yes\"."
  994. :group 'org-link-follow
  995. :type '(choice
  996. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  997. (const :tag "with y-or-n (faster)" y-or-n-p)
  998. (const :tag "no confirmation (dangerous)" nil)))
  999. (defconst org-file-apps-defaults-gnu
  1000. '((remote . emacs)
  1001. (system . mailcap)
  1002. (t . mailcap))
  1003. "Default file applications on a UNIX or GNU/Linux system.
  1004. See `org-file-apps'.")
  1005. (defconst org-file-apps-defaults-macosx
  1006. '((remote . emacs)
  1007. (t . "open %s")
  1008. (system . "open %s")
  1009. ("ps.gz" . "gv %s")
  1010. ("eps.gz" . "gv %s")
  1011. ("dvi" . "xdvi %s")
  1012. ("fig" . "xfig %s"))
  1013. "Default file applications on a MacOS X system.
  1014. The system \"open\" is known as a default, but we use X11 applications
  1015. for some files for which the OS does not have a good default.
  1016. See `org-file-apps'.")
  1017. (defconst org-file-apps-defaults-windowsnt
  1018. (list
  1019. '(remote . emacs)
  1020. (cons t
  1021. (list (if (featurep 'xemacs)
  1022. 'mswindows-shell-execute
  1023. 'w32-shell-execute)
  1024. "open" 'file))
  1025. (cons 'system
  1026. (list (if (featurep 'xemacs)
  1027. 'mswindows-shell-execute
  1028. 'w32-shell-execute)
  1029. "open" 'file)))
  1030. "Default file applications on a Windows NT system.
  1031. The system \"open\" is used for most files.
  1032. See `org-file-apps'.")
  1033. (defcustom org-file-apps
  1034. '(
  1035. (auto-mode . emacs)
  1036. ("\\.x?html?\\'" . default)
  1037. ("\\.pdf\\'" . default)
  1038. )
  1039. "External applications for opening `file:path' items in a document.
  1040. Org-mode uses system defaults for different file types, but
  1041. you can use this variable to set the application for a given file
  1042. extension. The entries in this list are cons cells where the car identifies
  1043. files and the cdr the corresponding command. Possible values for the
  1044. file identifier are
  1045. \"regex\" Regular expression matched against the file name. For backward
  1046. compatibility, this can also be a string with only alphanumeric
  1047. characters, which is then interpreted as an extension.
  1048. `directory' Matches a directory
  1049. `remote' Matches a remote file, accessible through tramp or efs.
  1050. Remote files most likely should be visited through Emacs
  1051. because external applications cannot handle such paths.
  1052. `auto-mode' Matches files that are mached by any entry in `auto-mode-alist',
  1053. so all files Emacs knows how to handle. Using this with
  1054. command `emacs' will open most files in Emacs. Beware that this
  1055. will also open html files insite Emacs, unless you add
  1056. (\"html\" . default) to the list as well.
  1057. t Default for files not matched by any of the other options.
  1058. `system' The system command to open files, like `open' on Windows
  1059. and Mac OS X, and mailcap under GNU/Linux. This is the command
  1060. that will be selected if you call `C-c C-o' with a double
  1061. `C-u C-u' prefix.
  1062. Possible values for the command are:
  1063. `emacs' The file will be visited by the current Emacs process.
  1064. `default' Use the default application for this file type, which is the
  1065. association for t in the list, most likely in the system-specific
  1066. part.
  1067. This can be used to overrule an unwanted seting in the
  1068. system-specific variable.
  1069. `system' Use the system command for opening files, like \"open\".
  1070. This command is specified by the entry whose car is `system'.
  1071. Most likely, the system-specific version of this variable
  1072. does define this command, but you can overrule/replace it
  1073. here.
  1074. string A command to be executed by a shell; %s will be replaced
  1075. by the path to the file.
  1076. sexp A Lisp form which will be evaluated. The file path will
  1077. be available in the Lisp variable `file'.
  1078. For more examples, see the system specific constants
  1079. `org-file-apps-defaults-macosx'
  1080. `org-file-apps-defaults-windowsnt'
  1081. `org-file-apps-defaults-gnu'."
  1082. :group 'org-link-follow
  1083. :type '(repeat
  1084. (cons (choice :value ""
  1085. (string :tag "Extension")
  1086. (const :tag "System command to open files" system)
  1087. (const :tag "Default for unrecognized files" t)
  1088. (const :tag "Remote file" remote)
  1089. (const :tag "Links to a directory" directory)
  1090. (const :tag "Any files that have Emacs modes"
  1091. auto-mode))
  1092. (choice :value ""
  1093. (const :tag "Visit with Emacs" emacs)
  1094. (const :tag "Use default" default)
  1095. (const :tag "Use the system command" system)
  1096. (string :tag "Command")
  1097. (sexp :tag "Lisp form")))))
  1098. (defgroup org-refile nil
  1099. "Options concerning refiling entries in Org-mode."
  1100. :tag "Org Refile"
  1101. :group 'org)
  1102. (defcustom org-directory "~/org"
  1103. "Directory with org files.
  1104. This directory will be used as default to prompt for org files.
  1105. Used by the hooks for remember.el."
  1106. :group 'org-refile
  1107. :group 'org-remember
  1108. :type 'directory)
  1109. (defcustom org-default-notes-file (convert-standard-filename "~/.notes")
  1110. "Default target for storing notes.
  1111. Used by the hooks for remember.el. This can be a string, or nil to mean
  1112. the value of `remember-data-file'.
  1113. You can set this on a per-template basis with the variable
  1114. `org-remember-templates'."
  1115. :group 'org-refile
  1116. :group 'org-remember
  1117. :type '(choice
  1118. (const :tag "Default from remember-data-file" nil)
  1119. file))
  1120. (defcustom org-goto-interface 'outline
  1121. "The default interface to be used for `org-goto'.
  1122. Allowed vaues are:
  1123. outline The interface shows an outline of the relevant file
  1124. and the correct heading is found by moving through
  1125. the outline or by searching with incremental search.
  1126. outline-path-completion Headlines in the current buffer are offered via
  1127. completion. This is the interface also used by
  1128. the refile command."
  1129. :group 'org-refile
  1130. :type '(choice
  1131. (const :tag "Outline" outline)
  1132. (const :tag "Outline-path-completion" outline-path-completion)))
  1133. (defcustom org-reverse-note-order nil
  1134. "Non-nil means, store new notes at the beginning of a file or entry.
  1135. When nil, new notes will be filed to the end of a file or entry.
  1136. This can also be a list with cons cells of regular expressions that
  1137. are matched against file names, and values."
  1138. :group 'org-remember
  1139. :group 'org-refile
  1140. :type '(choice
  1141. (const :tag "Reverse always" t)
  1142. (const :tag "Reverse never" nil)
  1143. (repeat :tag "By file name regexp"
  1144. (cons regexp boolean))))
  1145. (defcustom org-refile-targets nil
  1146. "Targets for refiling entries with \\[org-refile].
  1147. This is list of cons cells. Each cell contains:
  1148. - a specification of the files to be considered, either a list of files,
  1149. or a symbol whose function or variable value will be used to retrieve
  1150. a file name or a list of file names. Nil means, refile to a different
  1151. heading in the current buffer.
  1152. - A specification of how to find candidate refile targets. This may be
  1153. any of
  1154. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1155. This tag has to be present in all target headlines, inheritance will
  1156. not be considered.
  1157. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1158. todo keyword.
  1159. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1160. headlines that are refiling targets.
  1161. - a cons cell (:level . N). Any headline of level N is considered a target.
  1162. - a cons cell (:maxlevel . N). Any headline with level <= N is a target.
  1163. When this variable is nil, all top-level headlines in the current buffer
  1164. are used, equivalent to the value `((nil . (:level . 1))'."
  1165. :group 'org-refile
  1166. :type '(repeat
  1167. (cons
  1168. (choice :value org-agenda-files
  1169. (const :tag "All agenda files" org-agenda-files)
  1170. (const :tag "Current buffer" nil)
  1171. (function) (variable) (file))
  1172. (choice :tag "Identify target headline by"
  1173. (cons :tag "Specific tag" (const :value :tag) (string))
  1174. (cons :tag "TODO keyword" (const :value :todo) (string))
  1175. (cons :tag "Regular expression" (const :value :regexp) (regexp))
  1176. (cons :tag "Level number" (const :value :level) (integer))
  1177. (cons :tag "Max Level number" (const :value :maxlevel) (integer))))))
  1178. (defcustom org-refile-use-outline-path nil
  1179. "Non-nil means, provide refile targets as paths.
  1180. So a level 3 headline will be available as level1/level2/level3.
  1181. When the value is `file', also include the file name (without directory)
  1182. into the path. When `full-file-path', include the full file path."
  1183. :group 'org-refile
  1184. :type '(choice
  1185. (const :tag "Not" nil)
  1186. (const :tag "Yes" t)
  1187. (const :tag "Start with file name" file)
  1188. (const :tag "Start with full file path" full-file-path)))
  1189. (defcustom org-outline-path-complete-in-steps t
  1190. "Non-nil means, complete the outline path in hierarchical steps.
  1191. When Org-mode uses the refile interface to select an outline path
  1192. \(see variable `org-refile-use-outline-path'), the completion of
  1193. the path can be done is a single go, or if can be done in steps down
  1194. the headline hierarchy. Going in steps is probably the best if you
  1195. do not use a special completion package like `ido' or `icicles'.
  1196. However, when using these packages, going in one step can be very
  1197. fast, while still showing the whole path to the entry."
  1198. :group 'org-refile
  1199. :type 'boolean)
  1200. (defgroup org-todo nil
  1201. "Options concerning TODO items in Org-mode."
  1202. :tag "Org TODO"
  1203. :group 'org)
  1204. (defgroup org-progress nil
  1205. "Options concerning Progress logging in Org-mode."
  1206. :tag "Org Progress"
  1207. :group 'org-time)
  1208. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1209. "List of TODO entry keyword sequences and their interpretation.
  1210. \\<org-mode-map>This is a list of sequences.
  1211. Each sequence starts with a symbol, either `sequence' or `type',
  1212. indicating if the keywords should be interpreted as a sequence of
  1213. action steps, or as different types of TODO items. The first
  1214. keywords are states requiring action - these states will select a headline
  1215. for inclusion into the global TODO list Org-mode produces. If one of
  1216. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1217. signify that no further action is necessary. If \"|\" is not found,
  1218. the last keyword is treated as the only DONE state of the sequence.
  1219. The command \\[org-todo] cycles an entry through these states, and one
  1220. additional state where no keyword is present. For details about this
  1221. cycling, see the manual.
  1222. TODO keywords and interpretation can also be set on a per-file basis with
  1223. the special #+SEQ_TODO and #+TYP_TODO lines.
  1224. Each keyword can optionally specify a character for fast state selection
  1225. \(in combination with the variable `org-use-fast-todo-selection')
  1226. and specifiers for state change logging, using the same syntax
  1227. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1228. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1229. indicates to record a time stamp each time this state is selected.
  1230. Each keyword may also specify if a timestamp or a note should be
  1231. recorded when entering or leaving the state, by adding additional
  1232. characters in the parenthesis after the keyword. This looks like this:
  1233. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1234. record only the time of the state change. With X and Y being either
  1235. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1236. Y when leaving the state if and only if the *target* state does not
  1237. define X. You may omit any of the fast-selection key or X or /Y,
  1238. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1239. For backward compatibility, this variable may also be just a list
  1240. of keywords - in this case the interptetation (sequence or type) will be
  1241. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1242. :group 'org-todo
  1243. :group 'org-keywords
  1244. :type '(choice
  1245. (repeat :tag "Old syntax, just keywords"
  1246. (string :tag "Keyword"))
  1247. (repeat :tag "New syntax"
  1248. (cons
  1249. (choice
  1250. :tag "Interpretation"
  1251. (const :tag "Sequence (cycling hits every state)" sequence)
  1252. (const :tag "Type (cycling directly to DONE)" type))
  1253. (repeat
  1254. (string :tag "Keyword"))))))
  1255. (defvar org-todo-keywords-1 nil
  1256. "All TODO and DONE keywords active in a buffer.")
  1257. (make-variable-buffer-local 'org-todo-keywords-1)
  1258. (defvar org-todo-keywords-for-agenda nil)
  1259. (defvar org-done-keywords-for-agenda nil)
  1260. (defvar org-todo-keyword-alist-for-agenda nil)
  1261. (defvar org-tag-alist-for-agenda nil)
  1262. (defvar org-agenda-contributing-files nil)
  1263. (defvar org-not-done-keywords nil)
  1264. (make-variable-buffer-local 'org-not-done-keywords)
  1265. (defvar org-done-keywords nil)
  1266. (make-variable-buffer-local 'org-done-keywords)
  1267. (defvar org-todo-heads nil)
  1268. (make-variable-buffer-local 'org-todo-heads)
  1269. (defvar org-todo-sets nil)
  1270. (make-variable-buffer-local 'org-todo-sets)
  1271. (defvar org-todo-log-states nil)
  1272. (make-variable-buffer-local 'org-todo-log-states)
  1273. (defvar org-todo-kwd-alist nil)
  1274. (make-variable-buffer-local 'org-todo-kwd-alist)
  1275. (defvar org-todo-key-alist nil)
  1276. (make-variable-buffer-local 'org-todo-key-alist)
  1277. (defvar org-todo-key-trigger nil)
  1278. (make-variable-buffer-local 'org-todo-key-trigger)
  1279. (defcustom org-todo-interpretation 'sequence
  1280. "Controls how TODO keywords are interpreted.
  1281. This variable is in principle obsolete and is only used for
  1282. backward compatibility, if the interpretation of todo keywords is
  1283. not given already in `org-todo-keywords'. See that variable for
  1284. more information."
  1285. :group 'org-todo
  1286. :group 'org-keywords
  1287. :type '(choice (const sequence)
  1288. (const type)))
  1289. (defcustom org-use-fast-todo-selection 'prefix
  1290. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1291. This variable describes if and under what circumstances the cycling
  1292. mechanism for TODO keywords will be replaced by a single-key, direct
  1293. selection scheme.
  1294. When nil, fast selection is never used.
  1295. When the symbol `prefix', it will be used when `org-todo' is called with
  1296. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1297. in an agenda buffer.
  1298. When t, fast selection is used by default. In this case, the prefix
  1299. argument forces cycling instead.
  1300. In all cases, the special interface is only used if access keys have actually
  1301. been assigned by the user, i.e. if keywords in the configuration are followed
  1302. by a letter in parenthesis, like TODO(t)."
  1303. :group 'org-todo
  1304. :type '(choice
  1305. (const :tag "Never" nil)
  1306. (const :tag "By default" t)
  1307. (const :tag "Only with C-u C-c C-t" prefix)))
  1308. (defcustom org-provide-todo-statistics t
  1309. "Non-nil means, update todo statistics after insert and toggle.
  1310. When this is set, todo statistics is updated in the parent of the current
  1311. entry each time a todo state is changed."
  1312. :group 'org-todo
  1313. :type 'boolean)
  1314. (defcustom org-after-todo-state-change-hook nil
  1315. "Hook which is run after the state of a TODO item was changed.
  1316. The new state (a string with a TODO keyword, or nil) is available in the
  1317. Lisp variable `state'."
  1318. :group 'org-todo
  1319. :type 'hook)
  1320. (defcustom org-todo-state-tags-triggers nil
  1321. "Tag changes that should be triggered by TODO state changes.
  1322. This is a list. Each entry is
  1323. (state-change (tag . flag) .......)
  1324. State-change can be a string with a state, and empty string to indicate the
  1325. state that has no TODO keyword, or it can be one of the symbols `todo'
  1326. or `done', meaning any not-done or done state, respectively."
  1327. :group 'org-todo
  1328. :group 'org-tags
  1329. :type '(repeat
  1330. (cons (choice :tag "When changing to"
  1331. (const :tag "Not-done state" todo)
  1332. (const :tag "Done state" done)
  1333. (string :tag "State"))
  1334. (repeat
  1335. (cons :tag "Tag action"
  1336. (string :tag "Tag")
  1337. (choice (const :tag "Add" t) (const :tag "Remove" nil)))))))
  1338. (defcustom org-log-done nil
  1339. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1340. When equal to the list (done), also prompt for a closing note.
  1341. This can also be configured on a per-file basis by adding one of
  1342. the following lines anywhere in the buffer:
  1343. #+STARTUP: logdone
  1344. #+STARTUP: lognotedone
  1345. #+STARTUP: nologdone"
  1346. :group 'org-todo
  1347. :group 'org-progress
  1348. :type '(choice
  1349. (const :tag "No logging" nil)
  1350. (const :tag "Record CLOSED timestamp" time)
  1351. (const :tag "Record CLOSED timestamp with closing note." note)))
  1352. ;; Normalize old uses of org-log-done.
  1353. (cond
  1354. ((eq org-log-done t) (setq org-log-done 'time))
  1355. ((and (listp org-log-done) (memq 'done org-log-done))
  1356. (setq org-log-done 'note)))
  1357. (defcustom org-log-note-clock-out nil
  1358. "Non-nil means, record a note when clocking out of an item.
  1359. This can also be configured on a per-file basis by adding one of
  1360. the following lines anywhere in the buffer:
  1361. #+STARTUP: lognoteclock-out
  1362. #+STARTUP: nolognoteclock-out"
  1363. :group 'org-todo
  1364. :group 'org-progress
  1365. :type 'boolean)
  1366. (defcustom org-log-done-with-time t
  1367. "Non-nil means, the CLOSED time stamp will contain date and time.
  1368. When nil, only the date will be recorded."
  1369. :group 'org-progress
  1370. :type 'boolean)
  1371. (defcustom org-log-note-headings
  1372. '((done . "CLOSING NOTE %t")
  1373. (state . "State %-12s %t")
  1374. (note . "Note taken on %t")
  1375. (clock-out . ""))
  1376. "Headings for notes added to entries.
  1377. The value is an alist, with the car being a symbol indicating the note
  1378. context, and the cdr is the heading to be used. The heading may also be the
  1379. empty string.
  1380. %t in the heading will be replaced by a time stamp.
  1381. %s will be replaced by the new TODO state, in double quotes.
  1382. %u will be replaced by the user name.
  1383. %U will be replaced by the full user name."
  1384. :group 'org-todo
  1385. :group 'org-progress
  1386. :type '(list :greedy t
  1387. (cons (const :tag "Heading when closing an item" done) string)
  1388. (cons (const :tag
  1389. "Heading when changing todo state (todo sequence only)"
  1390. state) string)
  1391. (cons (const :tag "Heading when just taking a note" note) string)
  1392. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1393. (unless (assq 'note org-log-note-headings)
  1394. (push '(note . "%t") org-log-note-headings))
  1395. (defcustom org-log-state-notes-insert-after-drawers nil
  1396. "Non-nil means, insert state change notes after any drawers in entry.
  1397. Only the drawers that *immediately* follow the headline and the
  1398. deadline/scheduled line are skipped.
  1399. When nil, insert notes right after the heading and perhaps the line
  1400. with deadline/scheduling if present."
  1401. :group 'org-todo
  1402. :group 'org-progress
  1403. :type 'boolean)
  1404. (defcustom org-log-states-order-reversed t
  1405. "Non-nil means, the latest state change note will be directly after heading.
  1406. When nil, the notes will be orderer according to time."
  1407. :group 'org-todo
  1408. :group 'org-progress
  1409. :type 'boolean)
  1410. (defcustom org-log-repeat 'time
  1411. "Non-nil means, record moving through the DONE state when triggering repeat.
  1412. An auto-repeating tasks is immediately switched back to TODO when marked
  1413. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1414. the TODO keyword definition, or recording a closing note by setting
  1415. `org-log-done', there will be no record of the task moving through DONE.
  1416. This variable forces taking a note anyway. Possible values are:
  1417. nil Don't force a record
  1418. time Record a time stamp
  1419. note Record a note
  1420. This option can also be set with on a per-file-basis with
  1421. #+STARTUP: logrepeat
  1422. #+STARTUP: lognoterepeat
  1423. #+STARTUP: nologrepeat
  1424. You can have local logging settings for a subtree by setting the LOGGING
  1425. property to one or more of these keywords."
  1426. :group 'org-todo
  1427. :group 'org-progress
  1428. :type '(choice
  1429. (const :tag "Don't force a record" nil)
  1430. (const :tag "Force recording the DONE state" time)
  1431. (const :tag "Force recording a note with the DONE state" note)))
  1432. (defgroup org-priorities nil
  1433. "Priorities in Org-mode."
  1434. :tag "Org Priorities"
  1435. :group 'org-todo)
  1436. (defcustom org-highest-priority ?A
  1437. "The highest priority of TODO items. A character like ?A, ?B etc.
  1438. Must have a smaller ASCII number than `org-lowest-priority'."
  1439. :group 'org-priorities
  1440. :type 'character)
  1441. (defcustom org-lowest-priority ?C
  1442. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1443. Must have a larger ASCII number than `org-highest-priority'."
  1444. :group 'org-priorities
  1445. :type 'character)
  1446. (defcustom org-default-priority ?B
  1447. "The default priority of TODO items.
  1448. This is the priority an item get if no explicit priority is given."
  1449. :group 'org-priorities
  1450. :type 'character)
  1451. (defcustom org-priority-start-cycle-with-default t
  1452. "Non-nil means, start with default priority when starting to cycle.
  1453. When this is nil, the first step in the cycle will be (depending on the
  1454. command used) one higher or lower that the default priority."
  1455. :group 'org-priorities
  1456. :type 'boolean)
  1457. (defgroup org-time nil
  1458. "Options concerning time stamps and deadlines in Org-mode."
  1459. :tag "Org Time"
  1460. :group 'org)
  1461. (defcustom org-insert-labeled-timestamps-at-point nil
  1462. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1463. When nil, these labeled time stamps are forces into the second line of an
  1464. entry, just after the headline. When scheduling from the global TODO list,
  1465. the time stamp will always be forced into the second line."
  1466. :group 'org-time
  1467. :type 'boolean)
  1468. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1469. "Formats for `format-time-string' which are used for time stamps.
  1470. It is not recommended to change this constant.")
  1471. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1472. "Number of minutes to round time stamps to.
  1473. These are two values, the first applies when first creating a time stamp.
  1474. The second applies when changing it with the commands `S-up' and `S-down'.
  1475. When changing the time stamp, this means that it will change in steps
  1476. of N minutes, as given by the second value.
  1477. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1478. numbers should be factors of 60, so for example 5, 10, 15.
  1479. When this is larger than 1, you can still force an exact time-stamp by using
  1480. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1481. and by using a prefix arg to `S-up/down' to specify the exact number
  1482. of minutes to shift."
  1483. :group 'org-time
  1484. :get '(lambda (var) ; Make sure all entries have 5 elements
  1485. (if (integerp (default-value var))
  1486. (list (default-value var) 5)
  1487. (default-value var)))
  1488. :type '(list
  1489. (integer :tag "when inserting times")
  1490. (integer :tag "when modifying times")))
  1491. ;; Normalize old customizations of this variable.
  1492. (when (integerp org-time-stamp-rounding-minutes)
  1493. (setq org-time-stamp-rounding-minutes
  1494. (list org-time-stamp-rounding-minutes
  1495. org-time-stamp-rounding-minutes)))
  1496. (defcustom org-display-custom-times nil
  1497. "Non-nil means, overlay custom formats over all time stamps.
  1498. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1499. To turn this on on a per-file basis, insert anywhere in the file:
  1500. #+STARTUP: customtime"
  1501. :group 'org-time
  1502. :set 'set-default
  1503. :type 'sexp)
  1504. (make-variable-buffer-local 'org-display-custom-times)
  1505. (defcustom org-time-stamp-custom-formats
  1506. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1507. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1508. These are overlayed over the default ISO format if the variable
  1509. `org-display-custom-times' is set. Time like %H:%M should be at the
  1510. end of the second format."
  1511. :group 'org-time
  1512. :type 'sexp)
  1513. (defun org-time-stamp-format (&optional long inactive)
  1514. "Get the right format for a time string."
  1515. (let ((f (if long (cdr org-time-stamp-formats)
  1516. (car org-time-stamp-formats))))
  1517. (if inactive
  1518. (concat "[" (substring f 1 -1) "]")
  1519. f)))
  1520. (defcustom org-time-clocksum-format "%d:%02d"
  1521. "The format string used when creating CLOCKSUM lines, or when
  1522. org-mode generates a time duration."
  1523. :group 'org-time
  1524. :type 'string)
  1525. (defcustom org-deadline-warning-days 14
  1526. "No. of days before expiration during which a deadline becomes active.
  1527. This variable governs the display in sparse trees and in the agenda.
  1528. When 0 or negative, it means use this number (the absolute value of it)
  1529. even if a deadline has a different individual lead time specified."
  1530. :group 'org-time
  1531. :group 'org-agenda-daily/weekly
  1532. :type 'number)
  1533. (defcustom org-read-date-prefer-future t
  1534. "Non-nil means, assume future for incomplete date input from user.
  1535. This affects the following situations:
  1536. 1. The user gives a day, but no month.
  1537. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1538. read this as the third of *next* month. However, if you enter \"17\",
  1539. it will be considered as *this* month.
  1540. 2. The user gives a month but not a year.
  1541. For example, if it is april and you enter \"feb 2\", this will be read
  1542. as feb 2, *next* year. \"May 5\", however, will be this year.
  1543. Currently this does not work for ISO week specifications.
  1544. When this option is nil, the current month and year will always be used
  1545. as defaults."
  1546. :group 'org-time
  1547. :type 'boolean)
  1548. (defcustom org-read-date-display-live t
  1549. "Non-nil means, display current interpretation of date prompt live.
  1550. This display will be in an overlay, in the minibuffer."
  1551. :group 'org-time
  1552. :type 'boolean)
  1553. (defcustom org-read-date-popup-calendar t
  1554. "Non-nil means, pop up a calendar when prompting for a date.
  1555. In the calendar, the date can be selected with mouse-1. However, the
  1556. minibuffer will also be active, and you can simply enter the date as well.
  1557. When nil, only the minibuffer will be available."
  1558. :group 'org-time
  1559. :type 'boolean)
  1560. (if (fboundp 'defvaralias)
  1561. (defvaralias 'org-popup-calendar-for-date-prompt
  1562. 'org-read-date-popup-calendar))
  1563. (defcustom org-extend-today-until 0
  1564. "The hour when your day really ends. Must be an integer.
  1565. This has influence for the following applications:
  1566. - When switching the agenda to \"today\". It it is still earlier than
  1567. the time given here, the day recognized as TODAY is actually yesterday.
  1568. - When a date is read from the user and it is still before the time given
  1569. here, the current date and time will be assumed to be yesterday, 23:59.
  1570. Also, timestamps inserted in remember templates follow this rule.
  1571. IMPORTANT: This is a feature whose implementation is and likely will
  1572. remain incomplete. Really, it is only here because past midnight seems to
  1573. be the favorite working time of John Wiegley :-)"
  1574. :group 'org-time
  1575. :type 'number)
  1576. (defcustom org-edit-timestamp-down-means-later nil
  1577. "Non-nil means, S-down will increase the time in a time stamp.
  1578. When nil, S-up will increase."
  1579. :group 'org-time
  1580. :type 'boolean)
  1581. (defcustom org-calendar-follow-timestamp-change t
  1582. "Non-nil means, make the calendar window follow timestamp changes.
  1583. When a timestamp is modified and the calendar window is visible, it will be
  1584. moved to the new date."
  1585. :group 'org-time
  1586. :type 'boolean)
  1587. (defgroup org-tags nil
  1588. "Options concerning tags in Org-mode."
  1589. :tag "Org Tags"
  1590. :group 'org)
  1591. (defcustom org-tag-alist nil
  1592. "List of tags allowed in Org-mode files.
  1593. When this list is nil, Org-mode will base TAG input on what is already in the
  1594. buffer.
  1595. The value of this variable is an alist, the car of each entry must be a
  1596. keyword as a string, the cdr may be a character that is used to select
  1597. that tag through the fast-tag-selection interface.
  1598. See the manual for details."
  1599. :group 'org-tags
  1600. :type '(repeat
  1601. (choice
  1602. (cons (string :tag "Tag name")
  1603. (character :tag "Access char"))
  1604. (const :tag "Start radio group" (:startgroup))
  1605. (const :tag "End radio group" (:endgroup)))))
  1606. (defvar org-file-tags nil
  1607. "List of tags that can be inherited by all entries in the file.
  1608. The tags will be inherited if the variable `org-use-tag-inheritance'
  1609. says they should be.
  1610. This variable is populated from #+TAG lines.")
  1611. (defcustom org-use-fast-tag-selection 'auto
  1612. "Non-nil means, use fast tag selection scheme.
  1613. This is a special interface to select and deselect tags with single keys.
  1614. When nil, fast selection is never used.
  1615. When the symbol `auto', fast selection is used if and only if selection
  1616. characters for tags have been configured, either through the variable
  1617. `org-tag-alist' or through a #+TAGS line in the buffer.
  1618. When t, fast selection is always used and selection keys are assigned
  1619. automatically if necessary."
  1620. :group 'org-tags
  1621. :type '(choice
  1622. (const :tag "Always" t)
  1623. (const :tag "Never" nil)
  1624. (const :tag "When selection characters are configured" 'auto)))
  1625. (defcustom org-fast-tag-selection-single-key nil
  1626. "Non-nil means, fast tag selection exits after first change.
  1627. When nil, you have to press RET to exit it.
  1628. During fast tag selection, you can toggle this flag with `C-c'.
  1629. This variable can also have the value `expert'. In this case, the window
  1630. displaying the tags menu is not even shown, until you press C-c again."
  1631. :group 'org-tags
  1632. :type '(choice
  1633. (const :tag "No" nil)
  1634. (const :tag "Yes" t)
  1635. (const :tag "Expert" expert)))
  1636. (defvar org-fast-tag-selection-include-todo nil
  1637. "Non-nil means, fast tags selection interface will also offer TODO states.
  1638. This is an undocumented feature, you should not rely on it.")
  1639. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1640. "The column to which tags should be indented in a headline.
  1641. If this number is positive, it specifies the column. If it is negative,
  1642. it means that the tags should be flushright to that column. For example,
  1643. -80 works well for a normal 80 character screen."
  1644. :group 'org-tags
  1645. :type 'integer)
  1646. (defcustom org-auto-align-tags t
  1647. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1648. These operations change the length of a headline and therefore shift
  1649. the tags around. With this options turned on, after each such operation
  1650. the tags are again aligned to `org-tags-column'."
  1651. :group 'org-tags
  1652. :type 'boolean)
  1653. (defcustom org-use-tag-inheritance t
  1654. "Non-nil means, tags in levels apply also for sublevels.
  1655. When nil, only the tags directly given in a specific line apply there.
  1656. If this option is t, a match early-on in a tree can lead to a large
  1657. number of matches in the subtree. If you only want to see the first
  1658. match in a tree during a search, check out the variable
  1659. `org-tags-match-list-sublevels'.
  1660. This may also be a list of tags that should be inherited, or a regexp that
  1661. matches tags that should be inherited."
  1662. :group 'org-tags
  1663. :type '(choice
  1664. (const :tag "Not" nil)
  1665. (const :tag "Always" t)
  1666. (repeat :tag "Specific tags" (string :tag "Tag"))
  1667. (regexp :tag "Tags matched by regexp")))
  1668. (defun org-tag-inherit-p (tag)
  1669. "Check if TAG is one that should be inherited."
  1670. (cond
  1671. ((eq org-use-tag-inheritance t) t)
  1672. ((not org-use-tag-inheritance) nil)
  1673. ((stringp org-use-tag-inheritance)
  1674. (string-match org-use-tag-inheritance tag))
  1675. ((listp org-use-tag-inheritance)
  1676. (member tag org-use-tag-inheritance))
  1677. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1678. (defcustom org-tags-match-list-sublevels t
  1679. "Non-nil means list also sublevels of headlines matching tag search.
  1680. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1681. the sublevels of a headline matching a tag search often also match
  1682. the same search. Listing all of them can create very long lists.
  1683. Setting this variable to nil causes subtrees of a match to be skipped.
  1684. This option is off by default, because inheritance in on. If you turn
  1685. inheritance off, you very likely want to turn this option on.
  1686. As a special case, if the tag search is restricted to TODO items, the
  1687. value of this variable is ignored and sublevels are always checked, to
  1688. make sure all corresponding TODO items find their way into the list."
  1689. :group 'org-tags
  1690. :type 'boolean)
  1691. (defvar org-tags-history nil
  1692. "History of minibuffer reads for tags.")
  1693. (defvar org-last-tags-completion-table nil
  1694. "The last used completion table for tags.")
  1695. (defvar org-after-tags-change-hook nil
  1696. "Hook that is run after the tags in a line have changed.")
  1697. (defgroup org-properties nil
  1698. "Options concerning properties in Org-mode."
  1699. :tag "Org Properties"
  1700. :group 'org)
  1701. (defcustom org-property-format "%-10s %s"
  1702. "How property key/value pairs should be formatted by `indent-line'.
  1703. When `indent-line' hits a property definition, it will format the line
  1704. according to this format, mainly to make sure that the values are
  1705. lined-up with respect to each other."
  1706. :group 'org-properties
  1707. :type 'string)
  1708. (defcustom org-use-property-inheritance nil
  1709. "Non-nil means, properties apply also for sublevels.
  1710. This setting is chiefly used during property searches. Turning it on can
  1711. cause significant overhead when doing a search, which is why it is not
  1712. on by default.
  1713. When nil, only the properties directly given in the current entry count.
  1714. When t, every property is inherited. The value may also be a list of
  1715. properties that should have inheritance, or a regular expression matching
  1716. properties that should be inherited.
  1717. However, note that some special properties use inheritance under special
  1718. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1719. and the properties ending in \"_ALL\" when they are used as descriptor
  1720. for valid values of a property.
  1721. Note for programmers:
  1722. When querying an entry with `org-entry-get', you can control if inheritance
  1723. should be used. By default, `org-entry-get' looks only at the local
  1724. properties. You can request inheritance by setting the inherit argument
  1725. to t (to force inheritance) or to `selective' (to respect the setting
  1726. in this variable)."
  1727. :group 'org-properties
  1728. :type '(choice
  1729. (const :tag "Not" nil)
  1730. (const :tag "Always" t)
  1731. (repeat :tag "Specific properties" (string :tag "Property"))
  1732. (regexp :tag "Properties matched by regexp")))
  1733. (defun org-property-inherit-p (property)
  1734. "Check if PROPERTY is one that should be inherited."
  1735. (cond
  1736. ((eq org-use-property-inheritance t) t)
  1737. ((not org-use-property-inheritance) nil)
  1738. ((stringp org-use-property-inheritance)
  1739. (string-match org-use-property-inheritance property))
  1740. ((listp org-use-property-inheritance)
  1741. (member property org-use-property-inheritance))
  1742. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1743. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1744. "The default column format, if no other format has been defined.
  1745. This variable can be set on the per-file basis by inserting a line
  1746. #+COLUMNS: %25ITEM ....."
  1747. :group 'org-properties
  1748. :type 'string)
  1749. (defcustom org-columns-ellipses ".."
  1750. "The ellipses to be used when a field in column view is truncated.
  1751. When this is the empty string, as many characters as possible are shown,
  1752. but then there will be no visual indication that the field has been truncated.
  1753. When this is a string of length N, the last N characters of a truncated
  1754. field are replaced by this string. If the column is narrower than the
  1755. ellipses string, only part of the ellipses string will be shown."
  1756. :group 'org-properties
  1757. :type 'string)
  1758. (defcustom org-columns-modify-value-for-display-function nil
  1759. "Function that modifies values for display in column view.
  1760. For example, it can be used to cut out a certain part from a time stamp.
  1761. The function must take 2 arguments:
  1762. column-title The tite of the column (*not* the property name)
  1763. value The value that should be modified.
  1764. The function should return the value that should be displayed,
  1765. or nil if the normal value should be used."
  1766. :group 'org-properties
  1767. :type 'function)
  1768. (defcustom org-effort-property "Effort"
  1769. "The property that is being used to keep track of effort estimates.
  1770. Effort estimates given in this property need to have the format H:MM."
  1771. :group 'org-properties
  1772. :group 'org-progress
  1773. :type '(string :tag "Property"))
  1774. (defconst org-global-properties-fixed
  1775. '(("VISIBILITY_ALL" . "folded children content all"))
  1776. "List of property/value pairs that can be inherited by any entry.
  1777. These are fixed values, for the preset properties.")
  1778. (defcustom org-global-properties nil
  1779. "List of property/value pairs that can be inherited by any entry.
  1780. You can set buffer-local values for the same purpose in the variable
  1781. `org-file-properties' this by adding lines like
  1782. #+PROPERTY: NAME VALUE"
  1783. :group 'org-properties
  1784. :type '(repeat
  1785. (cons (string :tag "Property")
  1786. (string :tag "Value"))))
  1787. (defvar org-file-properties nil
  1788. "List of property/value pairs that can be inherited by any entry.
  1789. Valid for the current buffer.
  1790. This variable is populated from #+PROPERTY lines.")
  1791. (make-variable-buffer-local 'org-file-properties)
  1792. (defgroup org-agenda nil
  1793. "Options concerning agenda views in Org-mode."
  1794. :tag "Org Agenda"
  1795. :group 'org)
  1796. (defvar org-category nil
  1797. "Variable used by org files to set a category for agenda display.
  1798. Such files should use a file variable to set it, for example
  1799. # -*- mode: org; org-category: \"ELisp\"
  1800. or contain a special line
  1801. #+CATEGORY: ELisp
  1802. If the file does not specify a category, then file's base name
  1803. is used instead.")
  1804. (make-variable-buffer-local 'org-category)
  1805. (put 'org-category 'safe-local-variable '(lambda (x) (or (symbolp x) (stringp x))))
  1806. (defcustom org-agenda-files nil
  1807. "The files to be used for agenda display.
  1808. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1809. \\[org-remove-file]. You can also use customize to edit the list.
  1810. If an entry is a directory, all files in that directory that are matched by
  1811. `org-agenda-file-regexp' will be part of the file list.
  1812. If the value of the variable is not a list but a single file name, then
  1813. the list of agenda files is actually stored and maintained in that file, one
  1814. agenda file per line."
  1815. :group 'org-agenda
  1816. :type '(choice
  1817. (repeat :tag "List of files and directories" file)
  1818. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1819. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1820. "Regular expression to match files for `org-agenda-files'.
  1821. If any element in the list in that variable contains a directory instead
  1822. of a normal file, all files in that directory that are matched by this
  1823. regular expression will be included."
  1824. :group 'org-agenda
  1825. :type 'regexp)
  1826. (defcustom org-agenda-text-search-extra-files nil
  1827. "List of extra files to be searched by text search commands.
  1828. These files will be search in addition to the agenda files by the
  1829. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1830. Note that these files will only be searched for text search commands,
  1831. not for the other agenda views like todo lists, tag searches or the weekly
  1832. agenda. This variable is intended to list notes and possibly archive files
  1833. that should also be searched by these two commands.
  1834. In fact, if the first element in the list is the symbol `agenda-archives',
  1835. than all archive files of all agenda files will be added to the search
  1836. scope."
  1837. :group 'org-agenda
  1838. :type '(set :greedy t
  1839. (const :tag "Agenda Archives" agenda-archives)
  1840. (repeat :inline t (file))))
  1841. (if (fboundp 'defvaralias)
  1842. (defvaralias 'org-agenda-multi-occur-extra-files
  1843. 'org-agenda-text-search-extra-files))
  1844. (defcustom org-agenda-skip-unavailable-files nil
  1845. "Non-nil means to just skip non-reachable files in `org-agenda-files'.
  1846. A nil value means to remove them, after a query, from the list."
  1847. :group 'org-agenda
  1848. :type 'boolean)
  1849. (defcustom org-calendar-to-agenda-key [?c]
  1850. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1851. The command `org-calendar-goto-agenda' will be bound to this key. The
  1852. default is the character `c' because then `c' can be used to switch back and
  1853. forth between agenda and calendar."
  1854. :group 'org-agenda
  1855. :type 'sexp)
  1856. (defcustom org-calendar-agenda-action-key [?k]
  1857. "The key to be installed in `calendar-mode-map' for agenda-action.
  1858. The command `org-agenda-action' will be bound to this key. The
  1859. default is the character `k' because we use the same key in the agenda."
  1860. :group 'org-agenda
  1861. :type 'sexp)
  1862. (eval-after-load "calendar"
  1863. '(progn
  1864. (org-defkey calendar-mode-map org-calendar-to-agenda-key
  1865. 'org-calendar-goto-agenda)
  1866. (org-defkey calendar-mode-map org-calendar-agenda-action-key
  1867. 'org-agenda-action)))
  1868. (defgroup org-latex nil
  1869. "Options for embedding LaTeX code into Org-mode."
  1870. :tag "Org LaTeX"
  1871. :group 'org)
  1872. (defcustom org-format-latex-options
  1873. '(:foreground default :background default :scale 1.0
  1874. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1875. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1876. "Options for creating images from LaTeX fragments.
  1877. This is a property list with the following properties:
  1878. :foreground the foreground color for images embedded in Emacs, e.g. \"Black\".
  1879. `default' means use the foreground of the default face.
  1880. :background the background color, or \"Transparent\".
  1881. `default' means use the background of the default face.
  1882. :scale a scaling factor for the size of the images.
  1883. :html-foreground, :html-background, :html-scale
  1884. the same numbers for HTML export.
  1885. :matchers a list indicating which matchers should be used to
  1886. find LaTeX fragments. Valid members of this list are:
  1887. \"begin\" find environments
  1888. \"$\" find math expressions surrounded by $...$
  1889. \"$$\" find math expressions surrounded by $$....$$
  1890. \"\\(\" find math expressions surrounded by \\(...\\)
  1891. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1892. :group 'org-latex
  1893. :type 'plist)
  1894. (defcustom org-format-latex-header "\\documentclass{article}
  1895. \\usepackage{fullpage} % do not remove
  1896. \\usepackage{amssymb}
  1897. \\usepackage[usenames]{color}
  1898. \\usepackage{amsmath}
  1899. \\usepackage{latexsym}
  1900. \\usepackage[mathscr]{eucal}
  1901. \\pagestyle{empty} % do not remove"
  1902. "The document header used for processing LaTeX fragments."
  1903. :group 'org-latex
  1904. :type 'string)
  1905. (defgroup org-font-lock nil
  1906. "Font-lock settings for highlighting in Org-mode."
  1907. :tag "Org Font Lock"
  1908. :group 'org)
  1909. (defcustom org-level-color-stars-only nil
  1910. "Non-nil means fontify only the stars in each headline.
  1911. When nil, the entire headline is fontified.
  1912. Changing it requires restart of `font-lock-mode' to become effective
  1913. also in regions already fontified."
  1914. :group 'org-font-lock
  1915. :type 'boolean)
  1916. (defcustom org-hide-leading-stars nil
  1917. "Non-nil means, hide the first N-1 stars in a headline.
  1918. This works by using the face `org-hide' for these stars. This
  1919. face is white for a light background, and black for a dark
  1920. background. You may have to customize the face `org-hide' to
  1921. make this work.
  1922. Changing it requires restart of `font-lock-mode' to become effective
  1923. also in regions already fontified.
  1924. You may also set this on a per-file basis by adding one of the following
  1925. lines to the buffer:
  1926. #+STARTUP: hidestars
  1927. #+STARTUP: showstars"
  1928. :group 'org-font-lock
  1929. :type 'boolean)
  1930. (defcustom org-fontify-done-headline nil
  1931. "Non-nil means, change the face of a headline if it is marked DONE.
  1932. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1933. When this is non-nil, the headline after the keyword is set to the
  1934. `org-headline-done' as an additional indication."
  1935. :group 'org-font-lock
  1936. :type 'boolean)
  1937. (defcustom org-fontify-emphasized-text t
  1938. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1939. Changing this variable requires a restart of Emacs to take effect."
  1940. :group 'org-font-lock
  1941. :type 'boolean)
  1942. (defcustom org-highlight-latex-fragments-and-specials nil
  1943. "Non-nil means, fontify what is treated specially by the exporters."
  1944. :group 'org-font-lock
  1945. :type 'boolean)
  1946. (defcustom org-hide-emphasis-markers nil
  1947. "Non-nil mean font-lock should hide the emphasis marker characters."
  1948. :group 'org-font-lock
  1949. :type 'boolean)
  1950. (defvar org-emph-re nil
  1951. "Regular expression for matching emphasis.")
  1952. (defvar org-verbatim-re nil
  1953. "Regular expression for matching verbatim text.")
  1954. (defvar org-emphasis-regexp-components) ; defined just below
  1955. (defvar org-emphasis-alist) ; defined just below
  1956. (defun org-set-emph-re (var val)
  1957. "Set variable and compute the emphasis regular expression."
  1958. (set var val)
  1959. (when (and (boundp 'org-emphasis-alist)
  1960. (boundp 'org-emphasis-regexp-components)
  1961. org-emphasis-alist org-emphasis-regexp-components)
  1962. (let* ((e org-emphasis-regexp-components)
  1963. (pre (car e))
  1964. (post (nth 1 e))
  1965. (border (nth 2 e))
  1966. (body (nth 3 e))
  1967. (nl (nth 4 e))
  1968. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1969. (body1 (concat body "*?"))
  1970. (markers (mapconcat 'car org-emphasis-alist ""))
  1971. (vmarkers (mapconcat
  1972. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1973. org-emphasis-alist "")))
  1974. ;; make sure special characters appear at the right position in the class
  1975. (if (string-match "\\^" markers)
  1976. (setq markers (concat (replace-match "" t t markers) "^")))
  1977. (if (string-match "-" markers)
  1978. (setq markers (concat (replace-match "" t t markers) "-")))
  1979. (if (string-match "\\^" vmarkers)
  1980. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1981. (if (string-match "-" vmarkers)
  1982. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1983. (if (> nl 0)
  1984. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1985. (int-to-string nl) "\\}")))
  1986. ;; Make the regexp
  1987. (setq org-emph-re
  1988. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1989. "\\("
  1990. "\\([" markers "]\\)"
  1991. "\\("
  1992. "[^" border "]\\|"
  1993. "[^" border (if (and nil stacked) markers) "]"
  1994. body1
  1995. "[^" border (if (and nil stacked) markers) "]"
  1996. "\\)"
  1997. "\\3\\)"
  1998. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1999. (setq org-verbatim-re
  2000. (concat "\\([" pre "]\\|^\\)"
  2001. "\\("
  2002. "\\([" vmarkers "]\\)"
  2003. "\\("
  2004. "[^" border "]\\|"
  2005. "[^" border "]"
  2006. body1
  2007. "[^" border "]"
  2008. "\\)"
  2009. "\\3\\)"
  2010. "\\([" post "]\\|$\\)")))))
  2011. (defcustom org-emphasis-regexp-components
  2012. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  2013. "Components used to build the regular expression for emphasis.
  2014. This is a list with 6 entries. Terminology: In an emphasis string
  2015. like \" *strong word* \", we call the initial space PREMATCH, the final
  2016. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  2017. and \"trong wor\" is the body. The different components in this variable
  2018. specify what is allowed/forbidden in each part:
  2019. pre Chars allowed as prematch. Beginning of line will be allowed too.
  2020. post Chars allowed as postmatch. End of line will be allowed too.
  2021. border The chars *forbidden* as border characters.
  2022. body-regexp A regexp like \".\" to match a body character. Don't use
  2023. non-shy groups here, and don't allow newline here.
  2024. newline The maximum number of newlines allowed in an emphasis exp.
  2025. Use customize to modify this, or restart Emacs after changing it."
  2026. :group 'org-font-lock
  2027. :set 'org-set-emph-re
  2028. :type '(list
  2029. (sexp :tag "Allowed chars in pre ")
  2030. (sexp :tag "Allowed chars in post ")
  2031. (sexp :tag "Forbidden chars in border ")
  2032. (sexp :tag "Regexp for body ")
  2033. (integer :tag "number of newlines allowed")
  2034. (option (boolean :tag "Please ignore this button"))))
  2035. (defcustom org-emphasis-alist
  2036. `(("*" bold "<b>" "</b>")
  2037. ("/" italic "<i>" "</i>")
  2038. ("_" underline "<span style=\"text-decoration:underline;\">" "</span>")
  2039. ("=" org-code "<code>" "</code>" verbatim)
  2040. ("~" org-verbatim "<code>" "</code>" verbatim)
  2041. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  2042. "<del>" "</del>")
  2043. )
  2044. "Special syntax for emphasized text.
  2045. Text starting and ending with a special character will be emphasized, for
  2046. example *bold*, _underlined_ and /italic/. This variable sets the marker
  2047. characters, the face to be used by font-lock for highlighting in Org-mode
  2048. Emacs buffers, and the HTML tags to be used for this.
  2049. Use customize to modify this, or restart Emacs after changing it."
  2050. :group 'org-font-lock
  2051. :set 'org-set-emph-re
  2052. :type '(repeat
  2053. (list
  2054. (string :tag "Marker character")
  2055. (choice
  2056. (face :tag "Font-lock-face")
  2057. (plist :tag "Face property list"))
  2058. (string :tag "HTML start tag")
  2059. (string :tag "HTML end tag")
  2060. (option (const verbatim)))))
  2061. ;;; Miscellaneous options
  2062. (defgroup org-completion nil
  2063. "Completion in Org-mode."
  2064. :tag "Org Completion"
  2065. :group 'org)
  2066. (defcustom org-completion-use-ido nil
  2067. "Non-nil means, use ido completion wherever possible."
  2068. :group 'org-completion
  2069. :type 'boolean)
  2070. (defcustom org-completion-fallback-command 'hippie-expand
  2071. "The expansion command called by \\[org-complete] in normal context.
  2072. Normal means, no org-mode-specific context."
  2073. :group 'org-completion
  2074. :type 'function)
  2075. ;;; Functions and variables from ther packages
  2076. ;; Declared here to avoid compiler warnings
  2077. ;; XEmacs only
  2078. (defvar outline-mode-menu-heading)
  2079. (defvar outline-mode-menu-show)
  2080. (defvar outline-mode-menu-hide)
  2081. (defvar zmacs-regions) ; XEmacs regions
  2082. ;; Emacs only
  2083. (defvar mark-active)
  2084. ;; Various packages
  2085. (declare-function calendar-absolute-from-iso "cal-iso" (date))
  2086. (declare-function calendar-forward-day "cal-move" (arg))
  2087. (declare-function calendar-goto-date "cal-move" (date))
  2088. (declare-function calendar-goto-today "cal-move" ())
  2089. (declare-function calendar-iso-from-absolute "cal-iso" (date))
  2090. (defvar calc-embedded-close-formula)
  2091. (defvar calc-embedded-open-formula)
  2092. (declare-function cdlatex-tab "ext:cdlatex" ())
  2093. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  2094. (defvar font-lock-unfontify-region-function)
  2095. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  2096. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  2097. (defvar iswitchb-temp-buflist)
  2098. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  2099. (declare-function org-agenda-skip "org-agenda" ())
  2100. (declare-function org-format-agenda-item "org-agenda"
  2101. (extra txt &optional category tags dotime noprefix remove-re))
  2102. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  2103. (declare-function org-agenda-change-all-lines "org-agenda"
  2104. (newhead hdmarker &optional fixface just-this))
  2105. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  2106. (declare-function org-agenda-maybe-redo "org-agenda" ())
  2107. (declare-function org-agenda-save-markers-for-cut-and-paste "org-agenda"
  2108. (beg end))
  2109. (declare-function org-agenda-copy-local-variable "org-agenda" (var))
  2110. (declare-function parse-time-string "parse-time" (string))
  2111. (declare-function remember "remember" (&optional initial))
  2112. (declare-function remember-buffer-desc "remember" ())
  2113. (declare-function remember-finalize "remember" ())
  2114. (defvar remember-save-after-remembering)
  2115. (defvar remember-data-file)
  2116. (defvar remember-register)
  2117. (defvar remember-buffer)
  2118. (defvar remember-handler-functions)
  2119. (defvar remember-annotation-functions)
  2120. (defvar texmathp-why)
  2121. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  2122. (declare-function table--at-cell-p "table" (position &optional object at-column))
  2123. (defvar w3m-current-url)
  2124. (defvar w3m-current-title)
  2125. (defvar org-latex-regexps)
  2126. ;;; Autoload and prepare some org modules
  2127. ;; Some table stuff that needs to be defined here, because it is used
  2128. ;; by the functions setting up org-mode or checking for table context.
  2129. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  2130. "Detects an org-type or table-type table.")
  2131. (defconst org-table-line-regexp "^[ \t]*|"
  2132. "Detects an org-type table line.")
  2133. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  2134. "Detects an org-type table line.")
  2135. (defconst org-table-hline-regexp "^[ \t]*|-"
  2136. "Detects an org-type table hline.")
  2137. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  2138. "Detects a table-type table hline.")
  2139. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  2140. "Searching from within a table (any type) this finds the first line
  2141. outside the table.")
  2142. ;; Autoload the functions in org-table.el that are needed by functions here.
  2143. (eval-and-compile
  2144. (org-autoload "org-table"
  2145. '(org-table-align org-table-begin org-table-blank-field
  2146. org-table-convert org-table-convert-region org-table-copy-down
  2147. org-table-copy-region org-table-create
  2148. org-table-create-or-convert-from-region
  2149. org-table-create-with-table.el org-table-current-dline
  2150. org-table-cut-region org-table-delete-column org-table-edit-field
  2151. org-table-edit-formulas org-table-end org-table-eval-formula
  2152. org-table-export org-table-field-info
  2153. org-table-get-stored-formulas org-table-goto-column
  2154. org-table-hline-and-move org-table-import org-table-insert-column
  2155. org-table-insert-hline org-table-insert-row org-table-iterate
  2156. org-table-justify-field-maybe org-table-kill-row
  2157. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  2158. org-table-move-column org-table-move-column-left
  2159. org-table-move-column-right org-table-move-row
  2160. org-table-move-row-down org-table-move-row-up
  2161. org-table-next-field org-table-next-row org-table-paste-rectangle
  2162. org-table-previous-field org-table-recalculate
  2163. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  2164. org-table-toggle-coordinate-overlays
  2165. org-table-toggle-formula-debugger org-table-wrap-region
  2166. orgtbl-mode turn-on-orgtbl org-table-to-lisp)))
  2167. (defun org-at-table-p (&optional table-type)
  2168. "Return t if the cursor is inside an org-type table.
  2169. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  2170. (if org-enable-table-editor
  2171. (save-excursion
  2172. (beginning-of-line 1)
  2173. (looking-at (if table-type org-table-any-line-regexp
  2174. org-table-line-regexp)))
  2175. nil))
  2176. (defsubst org-table-p () (org-at-table-p))
  2177. (defun org-at-table.el-p ()
  2178. "Return t if and only if we are at a table.el table."
  2179. (and (org-at-table-p 'any)
  2180. (save-excursion
  2181. (goto-char (org-table-begin 'any))
  2182. (looking-at org-table1-hline-regexp))))
  2183. (defun org-table-recognize-table.el ()
  2184. "If there is a table.el table nearby, recognize it and move into it."
  2185. (if org-table-tab-recognizes-table.el
  2186. (if (org-at-table.el-p)
  2187. (progn
  2188. (beginning-of-line 1)
  2189. (if (looking-at org-table-dataline-regexp)
  2190. nil
  2191. (if (looking-at org-table1-hline-regexp)
  2192. (progn
  2193. (beginning-of-line 2)
  2194. (if (looking-at org-table-any-border-regexp)
  2195. (beginning-of-line -1)))))
  2196. (if (re-search-forward "|" (org-table-end t) t)
  2197. (progn
  2198. (require 'table)
  2199. (if (table--at-cell-p (point))
  2200. t
  2201. (message "recognizing table.el table...")
  2202. (table-recognize-table)
  2203. (message "recognizing table.el table...done")))
  2204. (error "This should not happen..."))
  2205. t)
  2206. nil)
  2207. nil))
  2208. (defun org-at-table-hline-p ()
  2209. "Return t if the cursor is inside a hline in a table."
  2210. (if org-enable-table-editor
  2211. (save-excursion
  2212. (beginning-of-line 1)
  2213. (looking-at org-table-hline-regexp))
  2214. nil))
  2215. (defvar org-table-clean-did-remove-column nil)
  2216. (defun org-table-map-tables (function)
  2217. "Apply FUNCTION to the start of all tables in the buffer."
  2218. (save-excursion
  2219. (save-restriction
  2220. (widen)
  2221. (goto-char (point-min))
  2222. (while (re-search-forward org-table-any-line-regexp nil t)
  2223. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2224. (beginning-of-line 1)
  2225. (if (looking-at org-table-line-regexp)
  2226. (save-excursion (funcall function)))
  2227. (re-search-forward org-table-any-border-regexp nil 1))))
  2228. (message "Mapping tables: done"))
  2229. ;; Declare and autoload functions from org-exp.el
  2230. (declare-function org-default-export-plist "org-exp")
  2231. (declare-function org-infile-export-plist "org-exp")
  2232. (declare-function org-get-current-options "org-exp")
  2233. (eval-and-compile
  2234. (org-autoload "org-exp"
  2235. '(org-export org-export-as-ascii org-export-visible
  2236. org-insert-export-options-template org-export-as-html-and-open
  2237. org-export-as-html-batch org-export-as-html-to-buffer
  2238. org-replace-region-by-html org-export-region-as-html
  2239. org-export-as-html org-export-icalendar-this-file
  2240. org-export-icalendar-all-agenda-files
  2241. org-table-clean-before-export
  2242. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2243. ;; Declare and autoload functions from org-agenda.el
  2244. (eval-and-compile
  2245. (org-autoload "org-agenda"
  2246. '(org-agenda org-agenda-list org-search-view
  2247. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2248. org-diary org-agenda-to-appt)))
  2249. ;; Autoload org-remember
  2250. (eval-and-compile
  2251. (org-autoload "org-remember"
  2252. '(org-remember-insinuate org-remember-annotation
  2253. org-remember-apply-template org-remember org-remember-handler)))
  2254. ;; Autoload org-clock.el
  2255. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2256. (beg end))
  2257. (declare-function org-update-mode-line "org-clock" ())
  2258. (defvar org-clock-start-time)
  2259. (defvar org-clock-marker (make-marker)
  2260. "Marker recording the last clock-in.")
  2261. (eval-and-compile
  2262. (org-autoload
  2263. "org-clock"
  2264. '(org-clock-in org-clock-out org-clock-cancel
  2265. org-clock-goto org-clock-sum org-clock-display
  2266. org-remove-clock-overlays org-clock-report
  2267. org-clocktable-shift org-dblock-write:clocktable
  2268. org-get-clocktable)))
  2269. (defun org-clock-update-time-maybe ()
  2270. "If this is a CLOCK line, update it and return t.
  2271. Otherwise, return nil."
  2272. (interactive)
  2273. (save-excursion
  2274. (beginning-of-line 1)
  2275. (skip-chars-forward " \t")
  2276. (when (looking-at org-clock-string)
  2277. (let ((re (concat "[ \t]*" org-clock-string
  2278. " *[[<]\\([^]>]+\\)[]>]\\(-+[[<]\\([^]>]+\\)[]>]"
  2279. "\\([ \t]*=>.*\\)?\\)?"))
  2280. ts te h m s neg)
  2281. (cond
  2282. ((not (looking-at re))
  2283. nil)
  2284. ((not (match-end 2))
  2285. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2286. (> org-clock-marker (point))
  2287. (<= org-clock-marker (point-at-eol)))
  2288. ;; The clock is running here
  2289. (setq org-clock-start-time
  2290. (apply 'encode-time
  2291. (org-parse-time-string (match-string 1))))
  2292. (org-update-mode-line)))
  2293. (t
  2294. (and (match-end 4) (delete-region (match-beginning 4) (match-end 4)))
  2295. (end-of-line 1)
  2296. (setq ts (match-string 1)
  2297. te (match-string 3))
  2298. (setq s (- (time-to-seconds
  2299. (apply 'encode-time (org-parse-time-string te)))
  2300. (time-to-seconds
  2301. (apply 'encode-time (org-parse-time-string ts))))
  2302. neg (< s 0)
  2303. s (abs s)
  2304. h (floor (/ s 3600))
  2305. s (- s (* 3600 h))
  2306. m (floor (/ s 60))
  2307. s (- s (* 60 s)))
  2308. (insert " => " (format (if neg "-%d:%02d" "%2d:%02d") h m))
  2309. t))))))
  2310. (defun org-check-running-clock ()
  2311. "Check if the current buffer contains the running clock.
  2312. If yes, offer to stop it and to save the buffer with the changes."
  2313. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2314. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2315. (buffer-name))))
  2316. (org-clock-out)
  2317. (when (y-or-n-p "Save changed buffer?")
  2318. (save-buffer))))
  2319. (defun org-clocktable-try-shift (dir n)
  2320. "Check if this line starts a clock table, if yes, shift the time block."
  2321. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2322. (org-clocktable-shift dir n)))
  2323. ;; Autoload org-timer.el
  2324. ;(declare-function org-timer "org-timer")
  2325. (eval-and-compile
  2326. (org-autoload
  2327. "org-timer"
  2328. '(org-timer-start org-timer org-timer-item
  2329. org-timer-change-times-in-region)))
  2330. ;; Autoload archiving code
  2331. ;; The stuff that is needed for cycling and tags has to be defined here.
  2332. (defgroup org-archive nil
  2333. "Options concerning archiving in Org-mode."
  2334. :tag "Org Archive"
  2335. :group 'org-structure)
  2336. (defcustom org-archive-location "%s_archive::"
  2337. "The location where subtrees should be archived.
  2338. The value of this variable is a string, consisting of two parts,
  2339. separated by a double-colon. The first part is a filename and
  2340. the second part is a headline.
  2341. When the filename is omitted, archiving happens in the same file.
  2342. %s in the filename will be replaced by the current file
  2343. name (without the directory part). Archiving to a different file
  2344. is useful to keep archived entries from contributing to the
  2345. Org-mode Agenda.
  2346. The archived entries will be filed as subtrees of the specified
  2347. headline. When the headline is omitted, the subtrees are simply
  2348. filed away at the end of the file, as top-level entries.
  2349. Here are a few examples:
  2350. \"%s_archive::\"
  2351. If the current file is Projects.org, archive in file
  2352. Projects.org_archive, as top-level trees. This is the default.
  2353. \"::* Archived Tasks\"
  2354. Archive in the current file, under the top-level headline
  2355. \"* Archived Tasks\".
  2356. \"~/org/archive.org::\"
  2357. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2358. \"basement::** Finished Tasks\"
  2359. Archive in file ./basement (relative path), as level 3 trees
  2360. below the level 2 heading \"** Finished Tasks\".
  2361. You may set this option on a per-file basis by adding to the buffer a
  2362. line like
  2363. #+ARCHIVE: basement::** Finished Tasks
  2364. You may also define it locally for a subtree by setting an ARCHIVE property
  2365. in the entry. If such a property is found in an entry, or anywhere up
  2366. the hierarchy, it will be used."
  2367. :group 'org-archive
  2368. :type 'string)
  2369. (defcustom org-archive-tag "ARCHIVE"
  2370. "The tag that marks a subtree as archived.
  2371. An archived subtree does not open during visibility cycling, and does
  2372. not contribute to the agenda listings.
  2373. After changing this, font-lock must be restarted in the relevant buffers to
  2374. get the proper fontification."
  2375. :group 'org-archive
  2376. :group 'org-keywords
  2377. :type 'string)
  2378. (defcustom org-agenda-skip-archived-trees t
  2379. "Non-nil means, the agenda will skip any items located in archived trees.
  2380. An archived tree is a tree marked with the tag ARCHIVE. The use of this
  2381. variable is no longer recommended, you should leave it at the value t.
  2382. Instead, use the key `v' to cycle the archives-mode in the agenda."
  2383. :group 'org-archive
  2384. :group 'org-agenda-skip
  2385. :type 'boolean)
  2386. (defcustom org-cycle-open-archived-trees nil
  2387. "Non-nil means, `org-cycle' will open archived trees.
  2388. An archived tree is a tree marked with the tag ARCHIVE.
  2389. When nil, archived trees will stay folded. You can still open them with
  2390. normal outline commands like `show-all', but not with the cycling commands."
  2391. :group 'org-archive
  2392. :group 'org-cycle
  2393. :type 'boolean)
  2394. (defcustom org-sparse-tree-open-archived-trees nil
  2395. "Non-nil means sparse tree construction shows matches in archived trees.
  2396. When nil, matches in these trees are highlighted, but the trees are kept in
  2397. collapsed state."
  2398. :group 'org-archive
  2399. :group 'org-sparse-trees
  2400. :type 'boolean)
  2401. (defun org-cycle-hide-archived-subtrees (state)
  2402. "Re-hide all archived subtrees after a visibility state change."
  2403. (when (and (not org-cycle-open-archived-trees)
  2404. (not (memq state '(overview folded))))
  2405. (save-excursion
  2406. (let* ((globalp (memq state '(contents all)))
  2407. (beg (if globalp (point-min) (point)))
  2408. (end (if globalp (point-max) (org-end-of-subtree t))))
  2409. (org-hide-archived-subtrees beg end)
  2410. (goto-char beg)
  2411. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2412. (message "%s" (substitute-command-keys
  2413. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2414. (defun org-force-cycle-archived ()
  2415. "Cycle subtree even if it is archived."
  2416. (interactive)
  2417. (setq this-command 'org-cycle)
  2418. (let ((org-cycle-open-archived-trees t))
  2419. (call-interactively 'org-cycle)))
  2420. (defun org-hide-archived-subtrees (beg end)
  2421. "Re-hide all archived subtrees after a visibility state change."
  2422. (save-excursion
  2423. (let* ((re (concat ":" org-archive-tag ":")))
  2424. (goto-char beg)
  2425. (while (re-search-forward re end t)
  2426. (and (org-on-heading-p) (hide-subtree))
  2427. (org-end-of-subtree t)))))
  2428. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2429. (eval-and-compile
  2430. (org-autoload "org-archive"
  2431. '(org-add-archive-files org-archive-subtree
  2432. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2433. ;; Autoload Column View Code
  2434. (declare-function org-columns-number-to-string "org-colview")
  2435. (declare-function org-columns-get-format-and-top-level "org-colview")
  2436. (declare-function org-columns-compute "org-colview")
  2437. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2438. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2439. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2440. org-columns org-insert-columns-dblock org-dblock-write:columnview))
  2441. ;; Autoload ID code
  2442. (org-autoload "org-id"
  2443. '(org-id-get-create org-id-new org-id-copy org-id-get
  2444. org-id-get-with-outline-path-completion
  2445. org-id-get-with-outline-drilling
  2446. org-id-goto org-id-find))
  2447. ;;; Variables for pre-computed regular expressions, all buffer local
  2448. (defvar org-drawer-regexp nil
  2449. "Matches first line of a hidden block.")
  2450. (make-variable-buffer-local 'org-drawer-regexp)
  2451. (defvar org-todo-regexp nil
  2452. "Matches any of the TODO state keywords.")
  2453. (make-variable-buffer-local 'org-todo-regexp)
  2454. (defvar org-not-done-regexp nil
  2455. "Matches any of the TODO state keywords except the last one.")
  2456. (make-variable-buffer-local 'org-not-done-regexp)
  2457. (defvar org-todo-line-regexp nil
  2458. "Matches a headline and puts TODO state into group 2 if present.")
  2459. (make-variable-buffer-local 'org-todo-line-regexp)
  2460. (defvar org-complex-heading-regexp nil
  2461. "Matches a headline and puts everything into groups:
  2462. group 1: the stars
  2463. group 2: The todo keyword, maybe
  2464. group 3: Priority cookie
  2465. group 4: True headline
  2466. group 5: Tags")
  2467. (make-variable-buffer-local 'org-complex-heading-regexp)
  2468. (defvar org-todo-line-tags-regexp nil
  2469. "Matches a headline and puts TODO state into group 2 if present.
  2470. Also put tags into group 4 if tags are present.")
  2471. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2472. (defvar org-nl-done-regexp nil
  2473. "Matches newline followed by a headline with the DONE keyword.")
  2474. (make-variable-buffer-local 'org-nl-done-regexp)
  2475. (defvar org-looking-at-done-regexp nil
  2476. "Matches the DONE keyword a point.")
  2477. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2478. (defvar org-ds-keyword-length 12
  2479. "Maximum length of the Deadline and SCHEDULED keywords.")
  2480. (make-variable-buffer-local 'org-ds-keyword-length)
  2481. (defvar org-deadline-regexp nil
  2482. "Matches the DEADLINE keyword.")
  2483. (make-variable-buffer-local 'org-deadline-regexp)
  2484. (defvar org-deadline-time-regexp nil
  2485. "Matches the DEADLINE keyword together with a time stamp.")
  2486. (make-variable-buffer-local 'org-deadline-time-regexp)
  2487. (defvar org-deadline-line-regexp nil
  2488. "Matches the DEADLINE keyword and the rest of the line.")
  2489. (make-variable-buffer-local 'org-deadline-line-regexp)
  2490. (defvar org-scheduled-regexp nil
  2491. "Matches the SCHEDULED keyword.")
  2492. (make-variable-buffer-local 'org-scheduled-regexp)
  2493. (defvar org-scheduled-time-regexp nil
  2494. "Matches the SCHEDULED keyword together with a time stamp.")
  2495. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2496. (defvar org-closed-time-regexp nil
  2497. "Matches the CLOSED keyword together with a time stamp.")
  2498. (make-variable-buffer-local 'org-closed-time-regexp)
  2499. (defvar org-keyword-time-regexp nil
  2500. "Matches any of the 4 keywords, together with the time stamp.")
  2501. (make-variable-buffer-local 'org-keyword-time-regexp)
  2502. (defvar org-keyword-time-not-clock-regexp nil
  2503. "Matches any of the 3 keywords, together with the time stamp.")
  2504. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2505. (defvar org-maybe-keyword-time-regexp nil
  2506. "Matches a timestamp, possibly preceeded by a keyword.")
  2507. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2508. (defvar org-planning-or-clock-line-re nil
  2509. "Matches a line with planning or clock info.")
  2510. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2511. (defconst org-plain-time-of-day-regexp
  2512. (concat
  2513. "\\(\\<[012]?[0-9]"
  2514. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2515. "\\(--?"
  2516. "\\(\\<[012]?[0-9]"
  2517. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2518. "\\)?")
  2519. "Regular expression to match a plain time or time range.
  2520. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2521. groups carry important information:
  2522. 0 the full match
  2523. 1 the first time, range or not
  2524. 8 the second time, if it is a range.")
  2525. (defconst org-plain-time-extension-regexp
  2526. (concat
  2527. "\\(\\<[012]?[0-9]"
  2528. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2529. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2530. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2531. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2532. groups carry important information:
  2533. 0 the full match
  2534. 7 hours of duration
  2535. 9 minutes of duration")
  2536. (defconst org-stamp-time-of-day-regexp
  2537. (concat
  2538. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2539. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2540. "\\(--?"
  2541. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2542. "Regular expression to match a timestamp time or time range.
  2543. After a match, the following groups carry important information:
  2544. 0 the full match
  2545. 1 date plus weekday, for backreferencing to make sure both times on same day
  2546. 2 the first time, range or not
  2547. 4 the second time, if it is a range.")
  2548. (defconst org-startup-options
  2549. '(("fold" org-startup-folded t)
  2550. ("overview" org-startup-folded t)
  2551. ("nofold" org-startup-folded nil)
  2552. ("showall" org-startup-folded nil)
  2553. ("content" org-startup-folded content)
  2554. ("hidestars" org-hide-leading-stars t)
  2555. ("showstars" org-hide-leading-stars nil)
  2556. ("odd" org-odd-levels-only t)
  2557. ("oddeven" org-odd-levels-only nil)
  2558. ("align" org-startup-align-all-tables t)
  2559. ("noalign" org-startup-align-all-tables nil)
  2560. ("customtime" org-display-custom-times t)
  2561. ("logdone" org-log-done time)
  2562. ("lognotedone" org-log-done note)
  2563. ("nologdone" org-log-done nil)
  2564. ("lognoteclock-out" org-log-note-clock-out t)
  2565. ("nolognoteclock-out" org-log-note-clock-out nil)
  2566. ("logrepeat" org-log-repeat state)
  2567. ("lognoterepeat" org-log-repeat note)
  2568. ("nologrepeat" org-log-repeat nil)
  2569. ("constcgs" constants-unit-system cgs)
  2570. ("constSI" constants-unit-system SI))
  2571. "Variable associated with STARTUP options for org-mode.
  2572. Each element is a list of three items: The startup options as written
  2573. in the #+STARTUP line, the corresponding variable, and the value to
  2574. set this variable to if the option is found. An optional forth element PUSH
  2575. means to push this value onto the list in the variable.")
  2576. (defun org-set-regexps-and-options ()
  2577. "Precompute regular expressions for current buffer."
  2578. (when (org-mode-p)
  2579. (org-set-local 'org-todo-kwd-alist nil)
  2580. (org-set-local 'org-todo-key-alist nil)
  2581. (org-set-local 'org-todo-key-trigger nil)
  2582. (org-set-local 'org-todo-keywords-1 nil)
  2583. (org-set-local 'org-done-keywords nil)
  2584. (org-set-local 'org-todo-heads nil)
  2585. (org-set-local 'org-todo-sets nil)
  2586. (org-set-local 'org-todo-log-states nil)
  2587. (org-set-local 'org-file-properties nil)
  2588. (org-set-local 'org-file-tags nil)
  2589. (let ((re (org-make-options-regexp
  2590. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2591. "STARTUP" "ARCHIVE" "FILETAGS" "TAGS" "LINK" "PRIORITIES"
  2592. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2593. (splitre "[ \t]+")
  2594. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2595. tail sep kws1 prio props ftags drawers
  2596. ext-setup-or-nil setup-contents (start 0))
  2597. (save-excursion
  2598. (save-restriction
  2599. (widen)
  2600. (goto-char (point-min))
  2601. (while (or (and ext-setup-or-nil
  2602. (string-match re ext-setup-or-nil start)
  2603. (setq start (match-end 0)))
  2604. (and (setq ext-setup-or-nil nil start 0)
  2605. (re-search-forward re nil t)))
  2606. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2607. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2608. (cond
  2609. ((equal key "CATEGORY")
  2610. (if (string-match "[ \t]+$" value)
  2611. (setq value (replace-match "" t t value)))
  2612. (setq cat value))
  2613. ((member key '("SEQ_TODO" "TODO"))
  2614. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2615. ((equal key "TYP_TODO")
  2616. (push (cons 'type (org-split-string value splitre)) kwds))
  2617. ((equal key "TAGS")
  2618. (setq tags (append tags (org-split-string value splitre))))
  2619. ((equal key "COLUMNS")
  2620. (org-set-local 'org-columns-default-format value))
  2621. ((equal key "LINK")
  2622. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2623. (push (cons (match-string 1 value)
  2624. (org-trim (match-string 2 value)))
  2625. links)))
  2626. ((equal key "PRIORITIES")
  2627. (setq prio (org-split-string value " +")))
  2628. ((equal key "PROPERTY")
  2629. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2630. (push (cons (match-string 1 value) (match-string 2 value))
  2631. props)))
  2632. ((equal key "FILETAGS")
  2633. (when (string-match "\\S-" value)
  2634. (setq ftags
  2635. (append
  2636. ftags
  2637. (apply 'append
  2638. (mapcar (lambda (x) (org-split-string x ":"))
  2639. (org-split-string value)))))))
  2640. ((equal key "DRAWERS")
  2641. (setq drawers (org-split-string value splitre)))
  2642. ((equal key "CONSTANTS")
  2643. (setq const (append const (org-split-string value splitre))))
  2644. ((equal key "STARTUP")
  2645. (let ((opts (org-split-string value splitre))
  2646. l var val)
  2647. (while (setq l (pop opts))
  2648. (when (setq l (assoc l org-startup-options))
  2649. (setq var (nth 1 l) val (nth 2 l))
  2650. (if (not (nth 3 l))
  2651. (set (make-local-variable var) val)
  2652. (if (not (listp (symbol-value var)))
  2653. (set (make-local-variable var) nil))
  2654. (set (make-local-variable var) (symbol-value var))
  2655. (add-to-list var val))))))
  2656. ((equal key "ARCHIVE")
  2657. (string-match " *$" value)
  2658. (setq arch (replace-match "" t t value))
  2659. (remove-text-properties 0 (length arch)
  2660. '(face t fontified t) arch))
  2661. ((equal key "SETUPFILE")
  2662. (setq setup-contents (org-file-contents
  2663. (expand-file-name
  2664. (org-remove-double-quotes value))
  2665. 'noerror))
  2666. (if (not ext-setup-or-nil)
  2667. (setq ext-setup-or-nil setup-contents start 0)
  2668. (setq ext-setup-or-nil
  2669. (concat (substring ext-setup-or-nil 0 start)
  2670. "\n" setup-contents "\n"
  2671. (substring ext-setup-or-nil start)))))
  2672. ))))
  2673. (when cat
  2674. (org-set-local 'org-category (intern cat))
  2675. (push (cons "CATEGORY" cat) props))
  2676. (when prio
  2677. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2678. (setq prio (mapcar 'string-to-char prio))
  2679. (org-set-local 'org-highest-priority (nth 0 prio))
  2680. (org-set-local 'org-lowest-priority (nth 1 prio))
  2681. (org-set-local 'org-default-priority (nth 2 prio)))
  2682. (and props (org-set-local 'org-file-properties (nreverse props)))
  2683. (and ftags (org-set-local 'org-file-tags ftags))
  2684. (and drawers (org-set-local 'org-drawers drawers))
  2685. (and arch (org-set-local 'org-archive-location arch))
  2686. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2687. ;; Process the TODO keywords
  2688. (unless kwds
  2689. ;; Use the global values as if they had been given locally.
  2690. (setq kwds (default-value 'org-todo-keywords))
  2691. (if (stringp (car kwds))
  2692. (setq kwds (list (cons org-todo-interpretation
  2693. (default-value 'org-todo-keywords)))))
  2694. (setq kwds (reverse kwds)))
  2695. (setq kwds (nreverse kwds))
  2696. (let (inter kws kw)
  2697. (while (setq kws (pop kwds))
  2698. (setq inter (pop kws) sep (member "|" kws)
  2699. kws0 (delete "|" (copy-sequence kws))
  2700. kwsa nil
  2701. kws1 (mapcar
  2702. (lambda (x)
  2703. ;; 1 2
  2704. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2705. (progn
  2706. (setq kw (match-string 1 x)
  2707. key (and (match-end 2) (match-string 2 x))
  2708. log (org-extract-log-state-settings x))
  2709. (push (cons kw (and key (string-to-char key))) kwsa)
  2710. (and log (push log org-todo-log-states))
  2711. kw)
  2712. (error "Invalid TODO keyword %s" x)))
  2713. kws0)
  2714. kwsa (if kwsa (append '((:startgroup))
  2715. (nreverse kwsa)
  2716. '((:endgroup))))
  2717. hw (car kws1)
  2718. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2719. tail (list inter hw (car dws) (org-last dws)))
  2720. (add-to-list 'org-todo-heads hw 'append)
  2721. (push kws1 org-todo-sets)
  2722. (setq org-done-keywords (append org-done-keywords dws nil))
  2723. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2724. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2725. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2726. (setq org-todo-sets (nreverse org-todo-sets)
  2727. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2728. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2729. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2730. ;; Process the constants
  2731. (when const
  2732. (let (e cst)
  2733. (while (setq e (pop const))
  2734. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2735. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2736. (setq org-table-formula-constants-local cst)))
  2737. ;; Process the tags.
  2738. (when tags
  2739. (let (e tgs)
  2740. (while (setq e (pop tags))
  2741. (cond
  2742. ((equal e "{") (push '(:startgroup) tgs))
  2743. ((equal e "}") (push '(:endgroup) tgs))
  2744. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2745. (push (cons (match-string 1 e)
  2746. (string-to-char (match-string 2 e)))
  2747. tgs))
  2748. (t (push (list e) tgs))))
  2749. (org-set-local 'org-tag-alist nil)
  2750. (while (setq e (pop tgs))
  2751. (or (and (stringp (car e))
  2752. (assoc (car e) org-tag-alist))
  2753. (push e org-tag-alist)))))
  2754. ;; Compute the regular expressions and other local variables
  2755. (if (not org-done-keywords)
  2756. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2757. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2758. (length org-scheduled-string)
  2759. (length org-clock-string)
  2760. (length org-closed-string)))
  2761. org-drawer-regexp
  2762. (concat "^[ \t]*:\\("
  2763. (mapconcat 'regexp-quote org-drawers "\\|")
  2764. "\\):[ \t]*$")
  2765. org-not-done-keywords
  2766. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2767. org-todo-regexp
  2768. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2769. "\\|") "\\)\\>")
  2770. org-not-done-regexp
  2771. (concat "\\<\\("
  2772. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2773. "\\)\\>")
  2774. org-todo-line-regexp
  2775. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2776. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2777. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2778. org-complex-heading-regexp
  2779. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2780. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2781. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2782. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2783. org-nl-done-regexp
  2784. (concat "\n\\*+[ \t]+"
  2785. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2786. "\\)" "\\>")
  2787. org-todo-line-tags-regexp
  2788. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2789. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2790. (org-re
  2791. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2792. org-looking-at-done-regexp
  2793. (concat "^" "\\(?:"
  2794. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2795. "\\>")
  2796. org-deadline-regexp (concat "\\<" org-deadline-string)
  2797. org-deadline-time-regexp
  2798. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2799. org-deadline-line-regexp
  2800. (concat "\\<\\(" org-deadline-string "\\).*")
  2801. org-scheduled-regexp
  2802. (concat "\\<" org-scheduled-string)
  2803. org-scheduled-time-regexp
  2804. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2805. org-closed-time-regexp
  2806. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2807. org-keyword-time-regexp
  2808. (concat "\\<\\(" org-scheduled-string
  2809. "\\|" org-deadline-string
  2810. "\\|" org-closed-string
  2811. "\\|" org-clock-string "\\)"
  2812. " *[[<]\\([^]>]+\\)[]>]")
  2813. org-keyword-time-not-clock-regexp
  2814. (concat "\\<\\(" org-scheduled-string
  2815. "\\|" org-deadline-string
  2816. "\\|" org-closed-string
  2817. "\\)"
  2818. " *[[<]\\([^]>]+\\)[]>]")
  2819. org-maybe-keyword-time-regexp
  2820. (concat "\\(\\<\\(" org-scheduled-string
  2821. "\\|" org-deadline-string
  2822. "\\|" org-closed-string
  2823. "\\|" org-clock-string "\\)\\)?"
  2824. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2825. org-planning-or-clock-line-re
  2826. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2827. "\\|" org-deadline-string
  2828. "\\|" org-closed-string "\\|" org-clock-string
  2829. "\\)\\>\\)")
  2830. )
  2831. (org-compute-latex-and-specials-regexp)
  2832. (org-set-font-lock-defaults))))
  2833. (defun org-file-contents (file &optional noerror)
  2834. "Return the contents of FILE, as a string."
  2835. (if (or (not file)
  2836. (not (file-readable-p file)))
  2837. (if noerror
  2838. (progn
  2839. (message "Cannot read file %s" file)
  2840. (ding) (sit-for 2)
  2841. "")
  2842. (error "Cannot read file %s" file))
  2843. (with-temp-buffer
  2844. (insert-file-contents file)
  2845. (buffer-string))))
  2846. (defun org-extract-log-state-settings (x)
  2847. "Extract the log state setting from a TODO keyword string.
  2848. This will extract info from a string like \"WAIT(w@/!)\"."
  2849. (let (kw key log1 log2)
  2850. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2851. (setq kw (match-string 1 x)
  2852. key (and (match-end 2) (match-string 2 x))
  2853. log1 (and (match-end 3) (match-string 3 x))
  2854. log2 (and (match-end 4) (match-string 4 x)))
  2855. (and (or log1 log2)
  2856. (list kw
  2857. (and log1 (if (equal log1 "!") 'time 'note))
  2858. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2859. (defun org-remove-keyword-keys (list)
  2860. "Remove a pair of parenthesis at the end of each string in LIST."
  2861. (mapcar (lambda (x)
  2862. (if (string-match "(.*)$" x)
  2863. (substring x 0 (match-beginning 0))
  2864. x))
  2865. list))
  2866. ;; FIXME: this could be done much better, using second characters etc.
  2867. (defun org-assign-fast-keys (alist)
  2868. "Assign fast keys to a keyword-key alist.
  2869. Respect keys that are already there."
  2870. (let (new e k c c1 c2 (char ?a))
  2871. (while (setq e (pop alist))
  2872. (cond
  2873. ((equal e '(:startgroup)) (push e new))
  2874. ((equal e '(:endgroup)) (push e new))
  2875. (t
  2876. (setq k (car e) c2 nil)
  2877. (if (cdr e)
  2878. (setq c (cdr e))
  2879. ;; automatically assign a character.
  2880. (setq c1 (string-to-char
  2881. (downcase (substring
  2882. k (if (= (string-to-char k) ?@) 1 0)))))
  2883. (if (or (rassoc c1 new) (rassoc c1 alist))
  2884. (while (or (rassoc char new) (rassoc char alist))
  2885. (setq char (1+ char)))
  2886. (setq c2 c1))
  2887. (setq c (or c2 char)))
  2888. (push (cons k c) new))))
  2889. (nreverse new)))
  2890. ;;; Some variables used in various places
  2891. (defvar org-window-configuration nil
  2892. "Used in various places to store a window configuration.")
  2893. (defvar org-finish-function nil
  2894. "Function to be called when `C-c C-c' is used.
  2895. This is for getting out of special buffers like remember.")
  2896. ;; FIXME: Occasionally check by commenting these, to make sure
  2897. ;; no other functions uses these, forgetting to let-bind them.
  2898. (defvar entry)
  2899. (defvar state)
  2900. (defvar last-state)
  2901. (defvar date)
  2902. (defvar description)
  2903. ;; Defined somewhere in this file, but used before definition.
  2904. (defvar org-html-entities)
  2905. (defvar org-struct-menu)
  2906. (defvar org-org-menu)
  2907. (defvar org-tbl-menu)
  2908. (defvar org-agenda-keymap)
  2909. ;;;; Define the Org-mode
  2910. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2911. (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."))
  2912. ;; We use a before-change function to check if a table might need
  2913. ;; an update.
  2914. (defvar org-table-may-need-update t
  2915. "Indicates that a table might need an update.
  2916. This variable is set by `org-before-change-function'.
  2917. `org-table-align' sets it back to nil.")
  2918. (defun org-before-change-function (beg end)
  2919. "Every change indicates that a table might need an update."
  2920. (setq org-table-may-need-update t))
  2921. (defvar org-mode-map)
  2922. (defvar org-mode-hook nil)
  2923. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2924. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2925. (defvar org-table-buffer-is-an nil)
  2926. (defconst org-outline-regexp "\\*+ ")
  2927. ;;;###autoload
  2928. (define-derived-mode org-mode outline-mode "Org"
  2929. "Outline-based notes management and organizer, alias
  2930. \"Carsten's outline-mode for keeping track of everything.\"
  2931. Org-mode develops organizational tasks around a NOTES file which
  2932. contains information about projects as plain text. Org-mode is
  2933. implemented on top of outline-mode, which is ideal to keep the content
  2934. of large files well structured. It supports ToDo items, deadlines and
  2935. time stamps, which magically appear in the diary listing of the Emacs
  2936. calendar. Tables are easily created with a built-in table editor.
  2937. Plain text URL-like links connect to websites, emails (VM), Usenet
  2938. messages (Gnus), BBDB entries, and any files related to the project.
  2939. For printing and sharing of notes, an Org-mode file (or a part of it)
  2940. can be exported as a structured ASCII or HTML file.
  2941. The following commands are available:
  2942. \\{org-mode-map}"
  2943. ;; Get rid of Outline menus, they are not needed
  2944. ;; Need to do this here because define-derived-mode sets up
  2945. ;; the keymap so late. Still, it is a waste to call this each time
  2946. ;; we switch another buffer into org-mode.
  2947. (if (featurep 'xemacs)
  2948. (when (boundp 'outline-mode-menu-heading)
  2949. ;; Assume this is Greg's port, it used easymenu
  2950. (easy-menu-remove outline-mode-menu-heading)
  2951. (easy-menu-remove outline-mode-menu-show)
  2952. (easy-menu-remove outline-mode-menu-hide))
  2953. (define-key org-mode-map [menu-bar headings] 'undefined)
  2954. (define-key org-mode-map [menu-bar hide] 'undefined)
  2955. (define-key org-mode-map [menu-bar show] 'undefined))
  2956. (org-load-modules-maybe)
  2957. (easy-menu-add org-org-menu)
  2958. (easy-menu-add org-tbl-menu)
  2959. (org-install-agenda-files-menu)
  2960. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2961. (org-add-to-invisibility-spec '(org-cwidth))
  2962. (when (featurep 'xemacs)
  2963. (org-set-local 'line-move-ignore-invisible t))
  2964. (org-set-local 'outline-regexp org-outline-regexp)
  2965. (org-set-local 'outline-level 'org-outline-level)
  2966. (when (and org-ellipsis
  2967. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2968. (fboundp 'make-glyph-code))
  2969. (unless org-display-table
  2970. (setq org-display-table (make-display-table)))
  2971. (set-display-table-slot
  2972. org-display-table 4
  2973. (vconcat (mapcar
  2974. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2975. org-ellipsis)))
  2976. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2977. (setq buffer-display-table org-display-table))
  2978. (org-set-regexps-and-options)
  2979. ;; Calc embedded
  2980. (org-set-local 'calc-embedded-open-mode "# ")
  2981. (modify-syntax-entry ?# "<")
  2982. (modify-syntax-entry ?@ "w")
  2983. (if org-startup-truncated (setq truncate-lines t))
  2984. (org-set-local 'font-lock-unfontify-region-function
  2985. 'org-unfontify-region)
  2986. ;; Activate before-change-function
  2987. (org-set-local 'org-table-may-need-update t)
  2988. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2989. 'local)
  2990. ;; Check for running clock before killing a buffer
  2991. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2992. ;; Paragraphs and auto-filling
  2993. (org-set-autofill-regexps)
  2994. (setq indent-line-function 'org-indent-line-function)
  2995. (org-update-radio-target-regexp)
  2996. ;; Comment characters
  2997. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2998. (org-set-local 'comment-padding " ")
  2999. ;; Align options lines
  3000. (org-set-local
  3001. 'align-mode-rules-list
  3002. '((org-in-buffer-settings
  3003. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  3004. (modes . '(org-mode)))))
  3005. ;; Imenu
  3006. (org-set-local 'imenu-create-index-function
  3007. 'org-imenu-get-tree)
  3008. ;; Make isearch reveal context
  3009. (if (or (featurep 'xemacs)
  3010. (not (boundp 'outline-isearch-open-invisible-function)))
  3011. ;; Emacs 21 and XEmacs make use of the hook
  3012. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  3013. ;; Emacs 22 deals with this through a special variable
  3014. (org-set-local 'outline-isearch-open-invisible-function
  3015. (lambda (&rest ignore) (org-show-context 'isearch))))
  3016. ;; If empty file that did not turn on org-mode automatically, make it to.
  3017. (if (and org-insert-mode-line-in-empty-file
  3018. (interactive-p)
  3019. (= (point-min) (point-max)))
  3020. (insert "# -*- mode: org -*-\n\n"))
  3021. (unless org-inhibit-startup
  3022. (when org-startup-align-all-tables
  3023. (let ((bmp (buffer-modified-p)))
  3024. (org-table-map-tables 'org-table-align)
  3025. (set-buffer-modified-p bmp)))
  3026. (org-set-startup-visibility)))
  3027. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  3028. (defun org-current-time ()
  3029. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  3030. (if (> (car org-time-stamp-rounding-minutes) 1)
  3031. (let ((r (car org-time-stamp-rounding-minutes))
  3032. (time (decode-time)))
  3033. (apply 'encode-time
  3034. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  3035. (nthcdr 2 time))))
  3036. (current-time)))
  3037. ;;;; Font-Lock stuff, including the activators
  3038. (defvar org-mouse-map (make-sparse-keymap))
  3039. (org-defkey org-mouse-map
  3040. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  3041. (org-defkey org-mouse-map
  3042. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  3043. (when org-mouse-1-follows-link
  3044. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  3045. (when org-tab-follows-link
  3046. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  3047. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  3048. (when org-return-follows-link
  3049. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  3050. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  3051. (require 'font-lock)
  3052. (defconst org-non-link-chars "]\t\n\r<>")
  3053. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  3054. "shell" "elisp"))
  3055. (defvar org-link-types-re nil
  3056. "Matches a link that has a url-like prefix like \"http:\"")
  3057. (defvar org-link-re-with-space nil
  3058. "Matches a link with spaces, optional angular brackets around it.")
  3059. (defvar org-link-re-with-space2 nil
  3060. "Matches a link with spaces, optional angular brackets around it.")
  3061. (defvar org-link-re-with-space3 nil
  3062. "Matches a link with spaces, only for internal part in bracket links.")
  3063. (defvar org-angle-link-re nil
  3064. "Matches link with angular brackets, spaces are allowed.")
  3065. (defvar org-plain-link-re nil
  3066. "Matches plain link, without spaces.")
  3067. (defvar org-bracket-link-regexp nil
  3068. "Matches a link in double brackets.")
  3069. (defvar org-bracket-link-analytic-regexp nil
  3070. "Regular expression used to analyze links.
  3071. Here is what the match groups contain after a match:
  3072. 1: http:
  3073. 2: http
  3074. 3: path
  3075. 4: [desc]
  3076. 5: desc")
  3077. (defvar org-any-link-re nil
  3078. "Regular expression matching any link.")
  3079. (defun org-make-link-regexps ()
  3080. "Update the link regular expressions.
  3081. This should be called after the variable `org-link-types' has changed."
  3082. (setq org-link-types-re
  3083. (concat
  3084. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  3085. org-link-re-with-space
  3086. (concat
  3087. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3088. "\\([^" org-non-link-chars " ]"
  3089. "[^" org-non-link-chars "]*"
  3090. "[^" org-non-link-chars " ]\\)>?")
  3091. org-link-re-with-space2
  3092. (concat
  3093. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3094. "\\([^" org-non-link-chars " ]"
  3095. "[^\t\n\r]*"
  3096. "[^" org-non-link-chars " ]\\)>?")
  3097. org-link-re-with-space3
  3098. (concat
  3099. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3100. "\\([^" org-non-link-chars " ]"
  3101. "[^\t\n\r]*\\)")
  3102. org-angle-link-re
  3103. (concat
  3104. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3105. "\\([^" org-non-link-chars " ]"
  3106. "[^" org-non-link-chars "]*"
  3107. "\\)>")
  3108. org-plain-link-re
  3109. (concat
  3110. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  3111. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  3112. org-bracket-link-regexp
  3113. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  3114. org-bracket-link-analytic-regexp
  3115. (concat
  3116. "\\[\\["
  3117. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  3118. "\\([^]]+\\)"
  3119. "\\]"
  3120. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  3121. "\\]")
  3122. org-any-link-re
  3123. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  3124. org-angle-link-re "\\)\\|\\("
  3125. org-plain-link-re "\\)")))
  3126. (org-make-link-regexps)
  3127. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  3128. "Regular expression for fast time stamp matching.")
  3129. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  3130. "Regular expression for fast time stamp matching.")
  3131. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3132. "Regular expression matching time strings for analysis.
  3133. This one does not require the space after the date, so it can be used
  3134. on a string that terminates immediately after the date.")
  3135. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  3136. "Regular expression matching time strings for analysis.")
  3137. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  3138. "Regular expression matching time stamps, with groups.")
  3139. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  3140. "Regular expression matching time stamps (also [..]), with groups.")
  3141. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  3142. "Regular expression matching a time stamp range.")
  3143. (defconst org-tr-regexp-both
  3144. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  3145. "Regular expression matching a time stamp range.")
  3146. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  3147. org-ts-regexp "\\)?")
  3148. "Regular expression matching a time stamp or time stamp range.")
  3149. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  3150. org-ts-regexp-both "\\)?")
  3151. "Regular expression matching a time stamp or time stamp range.
  3152. The time stamps may be either active or inactive.")
  3153. (defvar org-emph-face nil)
  3154. (defun org-do-emphasis-faces (limit)
  3155. "Run through the buffer and add overlays to links."
  3156. (let (rtn)
  3157. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  3158. (if (not (= (char-after (match-beginning 3))
  3159. (char-after (match-beginning 4))))
  3160. (progn
  3161. (setq rtn t)
  3162. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  3163. 'face
  3164. (nth 1 (assoc (match-string 3)
  3165. org-emphasis-alist)))
  3166. (add-text-properties (match-beginning 2) (match-end 2)
  3167. '(font-lock-multiline t))
  3168. (when org-hide-emphasis-markers
  3169. (add-text-properties (match-end 4) (match-beginning 5)
  3170. '(invisible org-link))
  3171. (add-text-properties (match-beginning 3) (match-end 3)
  3172. '(invisible org-link)))))
  3173. (backward-char 1))
  3174. rtn))
  3175. (defun org-emphasize (&optional char)
  3176. "Insert or change an emphasis, i.e. a font like bold or italic.
  3177. If there is an active region, change that region to a new emphasis.
  3178. If there is no region, just insert the marker characters and position
  3179. the cursor between them.
  3180. CHAR should be either the marker character, or the first character of the
  3181. HTML tag associated with that emphasis. If CHAR is a space, the means
  3182. to remove the emphasis of the selected region.
  3183. If char is not given (for example in an interactive call) it
  3184. will be prompted for."
  3185. (interactive)
  3186. (let ((eal org-emphasis-alist) e det
  3187. (erc org-emphasis-regexp-components)
  3188. (prompt "")
  3189. (string "") beg end move tag c s)
  3190. (if (org-region-active-p)
  3191. (setq beg (region-beginning) end (region-end)
  3192. string (buffer-substring beg end))
  3193. (setq move t))
  3194. (while (setq e (pop eal))
  3195. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  3196. c (aref tag 0))
  3197. (push (cons c (string-to-char (car e))) det)
  3198. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  3199. (substring tag 1)))))
  3200. (setq det (nreverse det))
  3201. (unless char
  3202. (message "%s" (concat "Emphasis marker or tag:" prompt))
  3203. (setq char (read-char-exclusive)))
  3204. (setq char (or (cdr (assoc char det)) char))
  3205. (if (equal char ?\ )
  3206. (setq s "" move nil)
  3207. (unless (assoc (char-to-string char) org-emphasis-alist)
  3208. (error "No such emphasis marker: \"%c\"" char))
  3209. (setq s (char-to-string char)))
  3210. (while (and (> (length string) 1)
  3211. (equal (substring string 0 1) (substring string -1))
  3212. (assoc (substring string 0 1) org-emphasis-alist))
  3213. (setq string (substring string 1 -1)))
  3214. (setq string (concat s string s))
  3215. (if beg (delete-region beg end))
  3216. (unless (or (bolp)
  3217. (string-match (concat "[" (nth 0 erc) "\n]")
  3218. (char-to-string (char-before (point)))))
  3219. (insert " "))
  3220. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  3221. (char-to-string (char-after (point))))
  3222. (insert " ") (backward-char 1))
  3223. (insert string)
  3224. (and move (backward-char 1))))
  3225. (defconst org-nonsticky-props
  3226. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  3227. (defun org-activate-plain-links (limit)
  3228. "Run through the buffer and add overlays to links."
  3229. (catch 'exit
  3230. (let (f)
  3231. (while (re-search-forward org-plain-link-re limit t)
  3232. (setq f (get-text-property (match-beginning 0) 'face))
  3233. (if (or (eq f 'org-tag)
  3234. (and (listp f) (memq 'org-tag f)))
  3235. nil
  3236. (add-text-properties (match-beginning 0) (match-end 0)
  3237. (list 'mouse-face 'highlight
  3238. 'rear-nonsticky org-nonsticky-props
  3239. 'keymap org-mouse-map
  3240. ))
  3241. (throw 'exit t))))))
  3242. (defun org-activate-code (limit)
  3243. (if (re-search-forward "^[ \t]*\\(: .*\n?\\)" limit t)
  3244. (progn
  3245. (remove-text-properties (match-beginning 0) (match-end 0)
  3246. '(display t invisible t intangible t))
  3247. t)))
  3248. (defun org-activate-angle-links (limit)
  3249. "Run through the buffer and add overlays to links."
  3250. (if (re-search-forward org-angle-link-re limit t)
  3251. (progn
  3252. (add-text-properties (match-beginning 0) (match-end 0)
  3253. (list 'mouse-face 'highlight
  3254. 'rear-nonsticky org-nonsticky-props
  3255. 'keymap org-mouse-map
  3256. ))
  3257. t)))
  3258. (defun org-activate-bracket-links (limit)
  3259. "Run through the buffer and add overlays to bracketed links."
  3260. (if (re-search-forward org-bracket-link-regexp limit t)
  3261. (let* ((help (concat "LINK: "
  3262. (org-match-string-no-properties 1)))
  3263. ;; FIXME: above we should remove the escapes.
  3264. ;; but that requires another match, protecting match data,
  3265. ;; a lot of overhead for font-lock.
  3266. (ip (org-maybe-intangible
  3267. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3268. 'keymap org-mouse-map 'mouse-face 'highlight
  3269. 'font-lock-multiline t 'help-echo help)))
  3270. (vp (list 'rear-nonsticky org-nonsticky-props
  3271. 'keymap org-mouse-map 'mouse-face 'highlight
  3272. ' font-lock-multiline t 'help-echo help)))
  3273. ;; We need to remove the invisible property here. Table narrowing
  3274. ;; may have made some of this invisible.
  3275. (remove-text-properties (match-beginning 0) (match-end 0)
  3276. '(invisible nil))
  3277. (if (match-end 3)
  3278. (progn
  3279. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3280. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3281. (add-text-properties (match-end 3) (match-end 0) ip))
  3282. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3283. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3284. (add-text-properties (match-end 1) (match-end 0) ip))
  3285. t)))
  3286. (defun org-activate-dates (limit)
  3287. "Run through the buffer and add overlays to dates."
  3288. (if (re-search-forward org-tsr-regexp-both limit t)
  3289. (progn
  3290. (add-text-properties (match-beginning 0) (match-end 0)
  3291. (list 'mouse-face 'highlight
  3292. 'rear-nonsticky org-nonsticky-props
  3293. 'keymap org-mouse-map))
  3294. (when org-display-custom-times
  3295. (if (match-end 3)
  3296. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3297. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3298. t)))
  3299. (defvar org-target-link-regexp nil
  3300. "Regular expression matching radio targets in plain text.")
  3301. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3302. "Regular expression matching a link target.")
  3303. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3304. "Regular expression matching a radio target.")
  3305. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3306. "Regular expression matching any target.")
  3307. (defun org-activate-target-links (limit)
  3308. "Run through the buffer and add overlays to target matches."
  3309. (when org-target-link-regexp
  3310. (let ((case-fold-search t))
  3311. (if (re-search-forward org-target-link-regexp limit t)
  3312. (progn
  3313. (add-text-properties (match-beginning 0) (match-end 0)
  3314. (list 'mouse-face 'highlight
  3315. 'rear-nonsticky org-nonsticky-props
  3316. 'keymap org-mouse-map
  3317. 'help-echo "Radio target link"
  3318. 'org-linked-text t))
  3319. t)))))
  3320. (defun org-update-radio-target-regexp ()
  3321. "Find all radio targets in this file and update the regular expression."
  3322. (interactive)
  3323. (when (memq 'radio org-activate-links)
  3324. (setq org-target-link-regexp
  3325. (org-make-target-link-regexp (org-all-targets 'radio)))
  3326. (org-restart-font-lock)))
  3327. (defun org-hide-wide-columns (limit)
  3328. (let (s e)
  3329. (setq s (text-property-any (point) (or limit (point-max))
  3330. 'org-cwidth t))
  3331. (when s
  3332. (setq e (next-single-property-change s 'org-cwidth))
  3333. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3334. (goto-char e)
  3335. t)))
  3336. (defvar org-latex-and-specials-regexp nil
  3337. "Regular expression for highlighting export special stuff.")
  3338. (defvar org-match-substring-regexp)
  3339. (defvar org-match-substring-with-braces-regexp)
  3340. (defvar org-export-html-special-string-regexps)
  3341. (defun org-compute-latex-and-specials-regexp ()
  3342. "Compute regular expression for stuff treated specially by exporters."
  3343. (if (not org-highlight-latex-fragments-and-specials)
  3344. (org-set-local 'org-latex-and-specials-regexp nil)
  3345. (require 'org-exp)
  3346. (let*
  3347. ((matchers (plist-get org-format-latex-options :matchers))
  3348. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3349. org-latex-regexps)))
  3350. (options (org-combine-plists (org-default-export-plist)
  3351. (org-infile-export-plist)))
  3352. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3353. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3354. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3355. (org-export-html-expand (plist-get options :expand-quoted-html))
  3356. (org-export-with-special-strings (plist-get options :special-strings))
  3357. (re-sub
  3358. (cond
  3359. ((equal org-export-with-sub-superscripts '{})
  3360. (list org-match-substring-with-braces-regexp))
  3361. (org-export-with-sub-superscripts
  3362. (list org-match-substring-regexp))
  3363. (t nil)))
  3364. (re-latex
  3365. (if org-export-with-LaTeX-fragments
  3366. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3367. (re-macros
  3368. (if org-export-with-TeX-macros
  3369. (list (concat "\\\\"
  3370. (regexp-opt
  3371. (append (mapcar 'car org-html-entities)
  3372. (if (boundp 'org-latex-entities)
  3373. org-latex-entities nil))
  3374. 'words))) ; FIXME
  3375. ))
  3376. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3377. (re-special (if org-export-with-special-strings
  3378. (mapcar (lambda (x) (car x))
  3379. org-export-html-special-string-regexps)))
  3380. (re-rest
  3381. (delq nil
  3382. (list
  3383. (if org-export-html-expand "@<[^>\n]+>")
  3384. ))))
  3385. (org-set-local
  3386. 'org-latex-and-specials-regexp
  3387. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3388. re-rest) "\\|")))))
  3389. (defun org-do-latex-and-special-faces (limit)
  3390. "Run through the buffer and add overlays to links."
  3391. (when org-latex-and-specials-regexp
  3392. (let (rtn d)
  3393. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3394. limit t))
  3395. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3396. 'face))
  3397. '(org-code org-verbatim underline)))
  3398. (progn
  3399. (setq rtn t
  3400. d (cond ((member (char-after (1+ (match-beginning 0)))
  3401. '(?_ ?^)) 1)
  3402. (t 0)))
  3403. (font-lock-prepend-text-property
  3404. (+ d (match-beginning 0)) (match-end 0)
  3405. 'face 'org-latex-and-export-specials)
  3406. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3407. '(font-lock-multiline t)))))
  3408. rtn)))
  3409. (defun org-restart-font-lock ()
  3410. "Restart font-lock-mode, to force refontification."
  3411. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3412. (font-lock-mode -1)
  3413. (font-lock-mode 1)))
  3414. (defun org-all-targets (&optional radio)
  3415. "Return a list of all targets in this file.
  3416. With optional argument RADIO, only find radio targets."
  3417. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3418. rtn)
  3419. (save-excursion
  3420. (goto-char (point-min))
  3421. (while (re-search-forward re nil t)
  3422. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3423. rtn)))
  3424. (defun org-make-target-link-regexp (targets)
  3425. "Make regular expression matching all strings in TARGETS.
  3426. The regular expression finds the targets also if there is a line break
  3427. between words."
  3428. (and targets
  3429. (concat
  3430. "\\<\\("
  3431. (mapconcat
  3432. (lambda (x)
  3433. (while (string-match " +" x)
  3434. (setq x (replace-match "\\s-+" t t x)))
  3435. x)
  3436. targets
  3437. "\\|")
  3438. "\\)\\>")))
  3439. (defun org-activate-tags (limit)
  3440. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3441. (progn
  3442. (add-text-properties (match-beginning 1) (match-end 1)
  3443. (list 'mouse-face 'highlight
  3444. 'rear-nonsticky org-nonsticky-props
  3445. 'keymap org-mouse-map))
  3446. t)))
  3447. (defun org-outline-level ()
  3448. (save-excursion
  3449. (looking-at outline-regexp)
  3450. (if (match-beginning 1)
  3451. (+ (org-get-string-indentation (match-string 1)) 1000)
  3452. (1- (- (match-end 0) (match-beginning 0))))))
  3453. (defvar org-font-lock-keywords nil)
  3454. (defconst org-property-re (org-re "^[ \t]*\\(:\\([-[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3455. "Regular expression matching a property line.")
  3456. (defvar org-font-lock-hook nil
  3457. "Functions to be called for special font lock stuff.")
  3458. (defun org-font-lock-hook (limit)
  3459. (run-hook-with-args 'org-font-lock-hook limit))
  3460. (defun org-set-font-lock-defaults ()
  3461. (let* ((em org-fontify-emphasized-text)
  3462. (lk org-activate-links)
  3463. (org-font-lock-extra-keywords
  3464. (list
  3465. ;; Call the hook
  3466. '(org-font-lock-hook)
  3467. ;; Headlines
  3468. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3469. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3470. ;; Table lines
  3471. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3472. (1 'org-table t))
  3473. ;; Table internals
  3474. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3475. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3476. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3477. ;; Drawers
  3478. (list org-drawer-regexp '(0 'org-special-keyword t))
  3479. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3480. ;; Properties
  3481. (list org-property-re
  3482. '(1 'org-special-keyword t)
  3483. '(3 'org-property-value t))
  3484. (if org-format-transports-properties-p
  3485. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3486. ;; Links
  3487. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3488. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3489. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3490. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3491. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3492. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3493. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3494. '(org-hide-wide-columns (0 nil append))
  3495. ;; TODO lines
  3496. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3497. '(1 (org-get-todo-face 1) t))
  3498. ;; DONE
  3499. (if org-fontify-done-headline
  3500. (list (concat "^[*]+ +\\<\\("
  3501. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3502. "\\)\\(.*\\)")
  3503. '(2 'org-headline-done t))
  3504. nil)
  3505. ;; Priorities
  3506. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3507. ;; Special keywords
  3508. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3509. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3510. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3511. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3512. ;; Emphasis
  3513. (if em
  3514. (if (featurep 'xemacs)
  3515. '(org-do-emphasis-faces (0 nil append))
  3516. '(org-do-emphasis-faces)))
  3517. ;; Checkboxes
  3518. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3519. 2 'bold prepend)
  3520. (if org-provide-checkbox-statistics
  3521. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3522. (0 (org-get-checkbox-statistics-face) t)))
  3523. ;; Description list items
  3524. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*? ::\\)"
  3525. 2 'bold prepend)
  3526. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3527. '(1 'org-archived prepend))
  3528. ;; Specials
  3529. '(org-do-latex-and-special-faces)
  3530. ;; Code
  3531. '(org-activate-code (1 'org-code t))
  3532. ;; COMMENT
  3533. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3534. "\\|" org-quote-string "\\)\\>")
  3535. '(1 'org-special-keyword t))
  3536. '("^#.*" (0 'font-lock-comment-face t))
  3537. )))
  3538. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3539. ;; Now set the full font-lock-keywords
  3540. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3541. (org-set-local 'font-lock-defaults
  3542. '(org-font-lock-keywords t nil nil backward-paragraph))
  3543. (kill-local-variable 'font-lock-keywords) nil))
  3544. (defvar org-m nil)
  3545. (defvar org-l nil)
  3546. (defvar org-f nil)
  3547. (defun org-get-level-face (n)
  3548. "Get the right face for match N in font-lock matching of healdines."
  3549. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3550. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3551. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3552. (cond
  3553. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3554. ((eq n 2) org-f)
  3555. (t (if org-level-color-stars-only nil org-f))))
  3556. (defun org-get-todo-face (kwd)
  3557. "Get the right face for a TODO keyword KWD.
  3558. If KWD is a number, get the corresponding match group."
  3559. (if (numberp kwd) (setq kwd (match-string kwd)))
  3560. (or (cdr (assoc kwd org-todo-keyword-faces))
  3561. (and (member kwd org-done-keywords) 'org-done)
  3562. 'org-todo))
  3563. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3564. "Remove fontification and activation overlays from links."
  3565. (font-lock-default-unfontify-region beg end)
  3566. (let* ((buffer-undo-list t)
  3567. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3568. (inhibit-modification-hooks t)
  3569. deactivate-mark buffer-file-name buffer-file-truename)
  3570. (remove-text-properties beg end
  3571. '(mouse-face t keymap t org-linked-text t
  3572. invisible t intangible t))))
  3573. ;;;; Visibility cycling, including org-goto and indirect buffer
  3574. ;;; Cycling
  3575. (defvar org-cycle-global-status nil)
  3576. (make-variable-buffer-local 'org-cycle-global-status)
  3577. (defvar org-cycle-subtree-status nil)
  3578. (make-variable-buffer-local 'org-cycle-subtree-status)
  3579. ;;;###autoload
  3580. (defun org-cycle (&optional arg)
  3581. "Visibility cycling for Org-mode.
  3582. - When this function is called with a prefix argument, rotate the entire
  3583. buffer through 3 states (global cycling)
  3584. 1. OVERVIEW: Show only top-level headlines.
  3585. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3586. 3. SHOW ALL: Show everything.
  3587. When called with two C-u C-u prefixes, switch to the startup visibility,
  3588. determined by the variable `org-startup-folded', and by any VISIBILITY
  3589. properties in the buffer.
  3590. When called with three C-u C-u C-u prefixed, show the entire buffer,
  3591. including drawers.
  3592. - When point is at the beginning of a headline, rotate the subtree started
  3593. by this line through 3 different states (local cycling)
  3594. 1. FOLDED: Only the main headline is shown.
  3595. 2. CHILDREN: The main headline and the direct children are shown.
  3596. From this state, you can move to one of the children
  3597. and zoom in further.
  3598. 3. SUBTREE: Show the entire subtree, including body text.
  3599. - When there is a numeric prefix, go up to a heading with level ARG, do
  3600. a `show-subtree' and return to the previous cursor position. If ARG
  3601. is negative, go up that many levels.
  3602. - When point is not at the beginning of a headline, execute the global
  3603. binding for TAB, which is re-indenting the line. See the option
  3604. `org-cycle-emulate-tab' for details.
  3605. - Special case: if point is at the beginning of the buffer and there is
  3606. no headline in line 1, this function will act as if called with prefix arg.
  3607. But only if also the variable `org-cycle-global-at-bob' is t."
  3608. (interactive "P")
  3609. (org-load-modules-maybe)
  3610. (let* ((outline-regexp
  3611. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3612. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3613. outline-regexp))
  3614. (bob-special (and org-cycle-global-at-bob (bobp)
  3615. (not (looking-at outline-regexp))))
  3616. (org-cycle-hook
  3617. (if bob-special
  3618. (delq 'org-optimize-window-after-visibility-change
  3619. (copy-sequence org-cycle-hook))
  3620. org-cycle-hook))
  3621. (pos (point)))
  3622. (if (or bob-special (equal arg '(4)))
  3623. ;; special case: use global cycling
  3624. (setq arg t))
  3625. (cond
  3626. ((equal arg '(16))
  3627. (org-set-startup-visibility)
  3628. (message "Startup visibility, plus VISIBILITY properties"))
  3629. ((equal arg '(64))
  3630. (show-all)
  3631. (message "Entire buffer visible, including drawers"))
  3632. ((org-at-table-p 'any)
  3633. ;; Enter the table or move to the next field in the table
  3634. (or (org-table-recognize-table.el)
  3635. (progn
  3636. (if arg (org-table-edit-field t)
  3637. (org-table-justify-field-maybe)
  3638. (call-interactively 'org-table-next-field)))))
  3639. ((eq arg t) ;; Global cycling
  3640. (cond
  3641. ((and (eq last-command this-command)
  3642. (eq org-cycle-global-status 'overview))
  3643. ;; We just created the overview - now do table of contents
  3644. ;; This can be slow in very large buffers, so indicate action
  3645. (message "CONTENTS...")
  3646. (org-content)
  3647. (message "CONTENTS...done")
  3648. (setq org-cycle-global-status 'contents)
  3649. (run-hook-with-args 'org-cycle-hook 'contents))
  3650. ((and (eq last-command this-command)
  3651. (eq org-cycle-global-status 'contents))
  3652. ;; We just showed the table of contents - now show everything
  3653. (show-all)
  3654. (message "SHOW ALL")
  3655. (setq org-cycle-global-status 'all)
  3656. (run-hook-with-args 'org-cycle-hook 'all))
  3657. (t
  3658. ;; Default action: go to overview
  3659. (org-overview)
  3660. (message "OVERVIEW")
  3661. (setq org-cycle-global-status 'overview)
  3662. (run-hook-with-args 'org-cycle-hook 'overview))))
  3663. ((and org-drawers org-drawer-regexp
  3664. (save-excursion
  3665. (beginning-of-line 1)
  3666. (looking-at org-drawer-regexp)))
  3667. ;; Toggle block visibility
  3668. (org-flag-drawer
  3669. (not (get-char-property (match-end 0) 'invisible))))
  3670. ((integerp arg)
  3671. ;; Show-subtree, ARG levels up from here.
  3672. (save-excursion
  3673. (org-back-to-heading)
  3674. (outline-up-heading (if (< arg 0) (- arg)
  3675. (- (funcall outline-level) arg)))
  3676. (org-show-subtree)))
  3677. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3678. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3679. ;; At a heading: rotate between three different views
  3680. (org-back-to-heading)
  3681. (let ((goal-column 0) eoh eol eos)
  3682. ;; First, some boundaries
  3683. (save-excursion
  3684. (org-back-to-heading)
  3685. (save-excursion
  3686. (beginning-of-line 2)
  3687. (while (and (not (eobp)) ;; this is like `next-line'
  3688. (get-char-property (1- (point)) 'invisible))
  3689. (beginning-of-line 2)) (setq eol (point)))
  3690. (outline-end-of-heading) (setq eoh (point))
  3691. (org-end-of-subtree t)
  3692. (unless (eobp)
  3693. (skip-chars-forward " \t\n")
  3694. (beginning-of-line 1) ; in case this is an item
  3695. )
  3696. (setq eos (1- (point))))
  3697. ;; Find out what to do next and set `this-command'
  3698. (cond
  3699. ((= eos eoh)
  3700. ;; Nothing is hidden behind this heading
  3701. (message "EMPTY ENTRY")
  3702. (setq org-cycle-subtree-status nil)
  3703. (save-excursion
  3704. (goto-char eos)
  3705. (outline-next-heading)
  3706. (if (org-invisible-p) (org-flag-heading nil))))
  3707. ((or (>= eol eos)
  3708. (not (string-match "\\S-" (buffer-substring eol eos))))
  3709. ;; Entire subtree is hidden in one line: open it
  3710. (org-show-entry)
  3711. (show-children)
  3712. (message "CHILDREN")
  3713. (save-excursion
  3714. (goto-char eos)
  3715. (outline-next-heading)
  3716. (if (org-invisible-p) (org-flag-heading nil)))
  3717. (setq org-cycle-subtree-status 'children)
  3718. (run-hook-with-args 'org-cycle-hook 'children))
  3719. ((and (eq last-command this-command)
  3720. (eq org-cycle-subtree-status 'children))
  3721. ;; We just showed the children, now show everything.
  3722. (org-show-subtree)
  3723. (message "SUBTREE")
  3724. (setq org-cycle-subtree-status 'subtree)
  3725. (run-hook-with-args 'org-cycle-hook 'subtree))
  3726. (t
  3727. ;; Default action: hide the subtree.
  3728. (hide-subtree)
  3729. (message "FOLDED")
  3730. (setq org-cycle-subtree-status 'folded)
  3731. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3732. ;; TAB emulation and template completion
  3733. (buffer-read-only (org-back-to-heading))
  3734. ((org-try-structure-completion))
  3735. ((org-try-cdlatex-tab))
  3736. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3737. (or (not (bolp))
  3738. (not (looking-at outline-regexp))))
  3739. (call-interactively (global-key-binding "\t")))
  3740. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3741. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3742. (or (and (eq org-cycle-emulate-tab 'white)
  3743. (= (match-end 0) (point-at-eol)))
  3744. (and (eq org-cycle-emulate-tab 'whitestart)
  3745. (>= (match-end 0) pos))))
  3746. t
  3747. (eq org-cycle-emulate-tab t))
  3748. (call-interactively (global-key-binding "\t")))
  3749. (t (save-excursion
  3750. (org-back-to-heading)
  3751. (org-cycle))))))
  3752. ;;;###autoload
  3753. (defun org-global-cycle (&optional arg)
  3754. "Cycle the global visibility. For details see `org-cycle'.
  3755. With C-u prefix arg, switch to startup visibility.
  3756. With a numeric prefix, show all headlines up to that level."
  3757. (interactive "P")
  3758. (let ((org-cycle-include-plain-lists
  3759. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3760. (cond
  3761. ((integerp arg)
  3762. (show-all)
  3763. (hide-sublevels arg)
  3764. (setq org-cycle-global-status 'contents))
  3765. ((equal arg '(4))
  3766. (org-set-startup-visibility)
  3767. (message "Startup visibility, plus VISIBILITY properties."))
  3768. (t
  3769. (org-cycle '(4))))))
  3770. (defun org-set-startup-visibility ()
  3771. "Set the visibility required by startup options and properties."
  3772. (cond
  3773. ((eq org-startup-folded t)
  3774. (org-cycle '(4)))
  3775. ((eq org-startup-folded 'content)
  3776. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  3777. (org-cycle '(4)) (org-cycle '(4)))))
  3778. (org-set-visibility-according-to-property 'no-cleanup)
  3779. (org-cycle-hide-archived-subtrees 'all)
  3780. (org-cycle-hide-drawers 'all)
  3781. (org-cycle-show-empty-lines 'all))
  3782. (defun org-set-visibility-according-to-property (&optional no-cleanup)
  3783. "Switch subtree visibilities according to :VISIBILITY: property."
  3784. (interactive)
  3785. (let (state)
  3786. (save-excursion
  3787. (goto-char (point-min))
  3788. (while (re-search-forward
  3789. "^[ \t]*:VISIBILITY:[ \t]+\\([a-z]+\\)"
  3790. nil t)
  3791. (setq state (match-string 1))
  3792. (save-excursion
  3793. (org-back-to-heading t)
  3794. (hide-subtree)
  3795. (org-reveal)
  3796. (cond
  3797. ((equal state '("fold" "folded"))
  3798. (hide-subtree))
  3799. ((equal state "children")
  3800. (org-show-hidden-entry)
  3801. (show-children))
  3802. ((equal state "content")
  3803. (save-excursion
  3804. (save-restriction
  3805. (org-narrow-to-subtree)
  3806. (org-content))))
  3807. ((member state '("all" "showall"))
  3808. (show-subtree)))))
  3809. (unless no-cleanup
  3810. (org-cycle-hide-archived-subtrees 'all)
  3811. (org-cycle-hide-drawers 'all)
  3812. (org-cycle-show-empty-lines 'all)))))
  3813. (defun org-overview ()
  3814. "Switch to overview mode, shoing only top-level headlines.
  3815. Really, this shows all headlines with level equal or greater than the level
  3816. of the first headline in the buffer. This is important, because if the
  3817. first headline is not level one, then (hide-sublevels 1) gives confusing
  3818. results."
  3819. (interactive)
  3820. (let ((level (save-excursion
  3821. (goto-char (point-min))
  3822. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3823. (progn
  3824. (goto-char (match-beginning 0))
  3825. (funcall outline-level))))))
  3826. (and level (hide-sublevels level))))
  3827. (defun org-content (&optional arg)
  3828. "Show all headlines in the buffer, like a table of contents.
  3829. With numerical argument N, show content up to level N."
  3830. (interactive "P")
  3831. (save-excursion
  3832. ;; Visit all headings and show their offspring
  3833. (and (integerp arg) (org-overview))
  3834. (goto-char (point-max))
  3835. (catch 'exit
  3836. (while (and (progn (condition-case nil
  3837. (outline-previous-visible-heading 1)
  3838. (error (goto-char (point-min))))
  3839. t)
  3840. (looking-at outline-regexp))
  3841. (if (integerp arg)
  3842. (show-children (1- arg))
  3843. (show-branches))
  3844. (if (bobp) (throw 'exit nil))))))
  3845. (defun org-optimize-window-after-visibility-change (state)
  3846. "Adjust the window after a change in outline visibility.
  3847. This function is the default value of the hook `org-cycle-hook'."
  3848. (when (get-buffer-window (current-buffer))
  3849. (cond
  3850. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3851. ; ((eq state 'overview) (org-beginning-of-line))
  3852. ((eq state 'content) nil)
  3853. ((eq state 'all) nil)
  3854. ((eq state 'folded) nil)
  3855. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3856. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3857. (defun org-compact-display-after-subtree-move ()
  3858. (let (beg end)
  3859. (save-excursion
  3860. (if (org-up-heading-safe)
  3861. (progn
  3862. (hide-subtree)
  3863. (show-entry)
  3864. (show-children)
  3865. (org-cycle-show-empty-lines 'children)
  3866. (org-cycle-hide-drawers 'children))
  3867. (org-overview)))))
  3868. (defun org-cycle-show-empty-lines (state)
  3869. "Show empty lines above all visible headlines.
  3870. The region to be covered depends on STATE when called through
  3871. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3872. entire buffer covered. Note that an empty line is only shown if there
  3873. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3874. (when (> org-cycle-separator-lines 0)
  3875. (save-excursion
  3876. (let* ((n org-cycle-separator-lines)
  3877. (re (cond
  3878. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3879. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3880. (t (let ((ns (number-to-string (- n 2))))
  3881. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3882. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3883. beg end)
  3884. (cond
  3885. ((memq state '(overview contents t))
  3886. (setq beg (point-min) end (point-max)))
  3887. ((memq state '(children folded))
  3888. (setq beg (point) end (progn (org-end-of-subtree t t)
  3889. (beginning-of-line 2)
  3890. (point)))))
  3891. (when beg
  3892. (goto-char beg)
  3893. (while (re-search-forward re end t)
  3894. (if (not (get-char-property (match-end 1) 'invisible))
  3895. (outline-flag-region
  3896. (match-beginning 1) (match-end 1) nil)))))))
  3897. ;; Never hide empty lines at the end of the file.
  3898. (save-excursion
  3899. (goto-char (point-max))
  3900. (outline-previous-heading)
  3901. (outline-end-of-heading)
  3902. (if (and (looking-at "[ \t\n]+")
  3903. (= (match-end 0) (point-max)))
  3904. (outline-flag-region (point) (match-end 0) nil))))
  3905. (defun org-show-empty-lines-in-parent ()
  3906. "Move to the parent and re-show empty lines before visible headlines."
  3907. (save-excursion
  3908. (let ((context (if (org-up-heading-safe) 'children 'overview)))
  3909. (org-cycle-show-empty-lines context))))
  3910. (defun org-cycle-hide-drawers (state)
  3911. "Re-hide all drawers after a visibility state change."
  3912. (when (and (org-mode-p)
  3913. (not (memq state '(overview folded))))
  3914. (save-excursion
  3915. (let* ((globalp (memq state '(contents all)))
  3916. (beg (if globalp (point-min) (point)))
  3917. (end (if globalp (point-max) (org-end-of-subtree t))))
  3918. (goto-char beg)
  3919. (while (re-search-forward org-drawer-regexp end t)
  3920. (org-flag-drawer t))))))
  3921. (defun org-flag-drawer (flag)
  3922. (save-excursion
  3923. (beginning-of-line 1)
  3924. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3925. (let ((b (match-end 0))
  3926. (outline-regexp org-outline-regexp))
  3927. (if (re-search-forward
  3928. "^[ \t]*:END:"
  3929. (save-excursion (outline-next-heading) (point)) t)
  3930. (outline-flag-region b (point-at-eol) flag)
  3931. (error ":END: line missing"))))))
  3932. (defun org-subtree-end-visible-p ()
  3933. "Is the end of the current subtree visible?"
  3934. (pos-visible-in-window-p
  3935. (save-excursion (org-end-of-subtree t) (point))))
  3936. (defun org-first-headline-recenter (&optional N)
  3937. "Move cursor to the first headline and recenter the headline.
  3938. Optional argument N means, put the headline into the Nth line of the window."
  3939. (goto-char (point-min))
  3940. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3941. (beginning-of-line)
  3942. (recenter (prefix-numeric-value N))))
  3943. ;;; Org-goto
  3944. (defvar org-goto-window-configuration nil)
  3945. (defvar org-goto-marker nil)
  3946. (defvar org-goto-map
  3947. (let ((map (make-sparse-keymap)))
  3948. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3949. (while (setq cmd (pop cmds))
  3950. (substitute-key-definition cmd cmd map global-map)))
  3951. (suppress-keymap map)
  3952. (org-defkey map "\C-m" 'org-goto-ret)
  3953. (org-defkey map [(return)] 'org-goto-ret)
  3954. (org-defkey map [(left)] 'org-goto-left)
  3955. (org-defkey map [(right)] 'org-goto-right)
  3956. (org-defkey map [(control ?g)] 'org-goto-quit)
  3957. (org-defkey map "\C-i" 'org-cycle)
  3958. (org-defkey map [(tab)] 'org-cycle)
  3959. (org-defkey map [(down)] 'outline-next-visible-heading)
  3960. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3961. (if org-goto-auto-isearch
  3962. (if (fboundp 'define-key-after)
  3963. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3964. nil)
  3965. (org-defkey map "q" 'org-goto-quit)
  3966. (org-defkey map "n" 'outline-next-visible-heading)
  3967. (org-defkey map "p" 'outline-previous-visible-heading)
  3968. (org-defkey map "f" 'outline-forward-same-level)
  3969. (org-defkey map "b" 'outline-backward-same-level)
  3970. (org-defkey map "u" 'outline-up-heading))
  3971. (org-defkey map "/" 'org-occur)
  3972. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3973. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3974. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3975. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3976. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3977. map))
  3978. (defconst org-goto-help
  3979. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3980. RET=jump to location [Q]uit and return to previous location
  3981. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3982. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3983. ;; FIXME: Docstring doe not mention both interfaces
  3984. (defun org-goto (&optional alternative-interface)
  3985. "Look up a different location in the current file, keeping current visibility.
  3986. When you want look-up or go to a different location in a document, the
  3987. fastest way is often to fold the entire buffer and then dive into the tree.
  3988. This method has the disadvantage, that the previous location will be folded,
  3989. which may not be what you want.
  3990. This command works around this by showing a copy of the current buffer
  3991. in an indirect buffer, in overview mode. You can dive into the tree in
  3992. that copy, use org-occur and incremental search to find a location.
  3993. When pressing RET or `Q', the command returns to the original buffer in
  3994. which the visibility is still unchanged. After RET is will also jump to
  3995. the location selected in the indirect buffer and expose the
  3996. the headline hierarchy above."
  3997. (interactive "P")
  3998. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3999. (org-refile-use-outline-path t)
  4000. (interface
  4001. (if (not alternative-interface)
  4002. org-goto-interface
  4003. (if (eq org-goto-interface 'outline)
  4004. 'outline-path-completion
  4005. 'outline)))
  4006. (org-goto-start-pos (point))
  4007. (selected-point
  4008. (if (eq interface 'outline)
  4009. (car (org-get-location (current-buffer) org-goto-help))
  4010. (nth 3 (org-refile-get-location "Goto: ")))))
  4011. (if selected-point
  4012. (progn
  4013. (org-mark-ring-push org-goto-start-pos)
  4014. (goto-char selected-point)
  4015. (if (or (org-invisible-p) (org-invisible-p2))
  4016. (org-show-context 'org-goto)))
  4017. (message "Quit"))))
  4018. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  4019. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  4020. (defvar org-goto-local-auto-isearch-map) ; defined below
  4021. (defun org-get-location (buf help)
  4022. "Let the user select a location in the Org-mode buffer BUF.
  4023. This function uses a recursive edit. It returns the selected position
  4024. or nil."
  4025. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  4026. (isearch-hide-immediately nil)
  4027. (isearch-search-fun-function
  4028. (lambda () 'org-goto-local-search-headings))
  4029. (org-goto-selected-point org-goto-exit-command))
  4030. (save-excursion
  4031. (save-window-excursion
  4032. (delete-other-windows)
  4033. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  4034. (switch-to-buffer
  4035. (condition-case nil
  4036. (make-indirect-buffer (current-buffer) "*org-goto*")
  4037. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  4038. (with-output-to-temp-buffer "*Help*"
  4039. (princ help))
  4040. (org-fit-window-to-buffer (get-buffer-window "*Help*"))
  4041. (setq buffer-read-only nil)
  4042. (let ((org-startup-truncated t)
  4043. (org-startup-folded nil)
  4044. (org-startup-align-all-tables nil))
  4045. (org-mode)
  4046. (org-overview))
  4047. (setq buffer-read-only t)
  4048. (if (and (boundp 'org-goto-start-pos)
  4049. (integer-or-marker-p org-goto-start-pos))
  4050. (let ((org-show-hierarchy-above t)
  4051. (org-show-siblings t)
  4052. (org-show-following-heading t))
  4053. (goto-char org-goto-start-pos)
  4054. (and (org-invisible-p) (org-show-context)))
  4055. (goto-char (point-min)))
  4056. (org-beginning-of-line)
  4057. (message "Select location and press RET")
  4058. (use-local-map org-goto-map)
  4059. (recursive-edit)
  4060. ))
  4061. (kill-buffer "*org-goto*")
  4062. (cons org-goto-selected-point org-goto-exit-command)))
  4063. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  4064. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  4065. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  4066. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  4067. (defun org-goto-local-search-headings (string bound noerror)
  4068. "Search and make sure that any matches are in headlines."
  4069. (catch 'return
  4070. (while (if isearch-forward
  4071. (search-forward string bound noerror)
  4072. (search-backward string bound noerror))
  4073. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  4074. (and (member :headline context)
  4075. (not (member :tags context))))
  4076. (throw 'return (point))))))
  4077. (defun org-goto-local-auto-isearch ()
  4078. "Start isearch."
  4079. (interactive)
  4080. (goto-char (point-min))
  4081. (let ((keys (this-command-keys)))
  4082. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  4083. (isearch-mode t)
  4084. (isearch-process-search-char (string-to-char keys)))))
  4085. (defun org-goto-ret (&optional arg)
  4086. "Finish `org-goto' by going to the new location."
  4087. (interactive "P")
  4088. (setq org-goto-selected-point (point)
  4089. org-goto-exit-command 'return)
  4090. (throw 'exit nil))
  4091. (defun org-goto-left ()
  4092. "Finish `org-goto' by going to the new location."
  4093. (interactive)
  4094. (if (org-on-heading-p)
  4095. (progn
  4096. (beginning-of-line 1)
  4097. (setq org-goto-selected-point (point)
  4098. org-goto-exit-command 'left)
  4099. (throw 'exit nil))
  4100. (error "Not on a heading")))
  4101. (defun org-goto-right ()
  4102. "Finish `org-goto' by going to the new location."
  4103. (interactive)
  4104. (if (org-on-heading-p)
  4105. (progn
  4106. (setq org-goto-selected-point (point)
  4107. org-goto-exit-command 'right)
  4108. (throw 'exit nil))
  4109. (error "Not on a heading")))
  4110. (defun org-goto-quit ()
  4111. "Finish `org-goto' without cursor motion."
  4112. (interactive)
  4113. (setq org-goto-selected-point nil)
  4114. (setq org-goto-exit-command 'quit)
  4115. (throw 'exit nil))
  4116. ;;; Indirect buffer display of subtrees
  4117. (defvar org-indirect-dedicated-frame nil
  4118. "This is the frame being used for indirect tree display.")
  4119. (defvar org-last-indirect-buffer nil)
  4120. (defun org-tree-to-indirect-buffer (&optional arg)
  4121. "Create indirect buffer and narrow it to current subtree.
  4122. With numerical prefix ARG, go up to this level and then take that tree.
  4123. If ARG is negative, go up that many levels.
  4124. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  4125. indirect buffer previously made with this command, to avoid proliferation of
  4126. indirect buffers. However, when you call the command with a `C-u' prefix, or
  4127. when `org-indirect-buffer-display' is `new-frame', the last buffer
  4128. is kept so that you can work with several indirect buffers at the same time.
  4129. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  4130. requests that a new frame be made for the new buffer, so that the dedicated
  4131. frame is not changed."
  4132. (interactive "P")
  4133. (let ((cbuf (current-buffer))
  4134. (cwin (selected-window))
  4135. (pos (point))
  4136. beg end level heading ibuf)
  4137. (save-excursion
  4138. (org-back-to-heading t)
  4139. (when (numberp arg)
  4140. (setq level (org-outline-level))
  4141. (if (< arg 0) (setq arg (+ level arg)))
  4142. (while (> (setq level (org-outline-level)) arg)
  4143. (outline-up-heading 1 t)))
  4144. (setq beg (point)
  4145. heading (org-get-heading))
  4146. (org-end-of-subtree t) (setq end (point)))
  4147. (if (and (buffer-live-p org-last-indirect-buffer)
  4148. (not (eq org-indirect-buffer-display 'new-frame))
  4149. (not arg))
  4150. (kill-buffer org-last-indirect-buffer))
  4151. (setq ibuf (org-get-indirect-buffer cbuf)
  4152. org-last-indirect-buffer ibuf)
  4153. (cond
  4154. ((or (eq org-indirect-buffer-display 'new-frame)
  4155. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  4156. (select-frame (make-frame))
  4157. (delete-other-windows)
  4158. (switch-to-buffer ibuf)
  4159. (org-set-frame-title heading))
  4160. ((eq org-indirect-buffer-display 'dedicated-frame)
  4161. (raise-frame
  4162. (select-frame (or (and org-indirect-dedicated-frame
  4163. (frame-live-p org-indirect-dedicated-frame)
  4164. org-indirect-dedicated-frame)
  4165. (setq org-indirect-dedicated-frame (make-frame)))))
  4166. (delete-other-windows)
  4167. (switch-to-buffer ibuf)
  4168. (org-set-frame-title (concat "Indirect: " heading)))
  4169. ((eq org-indirect-buffer-display 'current-window)
  4170. (switch-to-buffer ibuf))
  4171. ((eq org-indirect-buffer-display 'other-window)
  4172. (pop-to-buffer ibuf))
  4173. (t (error "Invalid value.")))
  4174. (if (featurep 'xemacs)
  4175. (save-excursion (org-mode) (turn-on-font-lock)))
  4176. (narrow-to-region beg end)
  4177. (show-all)
  4178. (goto-char pos)
  4179. (and (window-live-p cwin) (select-window cwin))))
  4180. (defun org-get-indirect-buffer (&optional buffer)
  4181. (setq buffer (or buffer (current-buffer)))
  4182. (let ((n 1) (base (buffer-name buffer)) bname)
  4183. (while (buffer-live-p
  4184. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  4185. (setq n (1+ n)))
  4186. (condition-case nil
  4187. (make-indirect-buffer buffer bname 'clone)
  4188. (error (make-indirect-buffer buffer bname)))))
  4189. (defun org-set-frame-title (title)
  4190. "Set the title of the current frame to the string TITLE."
  4191. ;; FIXME: how to name a single frame in XEmacs???
  4192. (unless (featurep 'xemacs)
  4193. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  4194. ;;;; Structure editing
  4195. ;;; Inserting headlines
  4196. (defun org-insert-heading (&optional force-heading)
  4197. "Insert a new heading or item with same depth at point.
  4198. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  4199. If point is at the beginning of a headline, insert a sibling before the
  4200. current headline. If point is not at the beginning, do not split the line,
  4201. but create the new headline after the current line."
  4202. (interactive "P")
  4203. (if (= (buffer-size) 0)
  4204. (insert "\n* ")
  4205. (when (or force-heading (not (org-insert-item)))
  4206. (let* ((head (save-excursion
  4207. (condition-case nil
  4208. (progn
  4209. (org-back-to-heading)
  4210. (match-string 0))
  4211. (error "*"))))
  4212. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  4213. pos hide-previous previous-pos)
  4214. (cond
  4215. ((and (org-on-heading-p) (bolp)
  4216. (or (bobp)
  4217. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  4218. ;; insert before the current line
  4219. (open-line (if blank 2 1)))
  4220. ((and (bolp)
  4221. (or (bobp)
  4222. (save-excursion
  4223. (backward-char 1) (not (org-invisible-p)))))
  4224. ;; insert right here
  4225. nil)
  4226. (t
  4227. ;; somewhere in the line
  4228. (save-excursion
  4229. (setq previous-pos (point-at-bol))
  4230. (end-of-line)
  4231. (setq hide-previous (org-invisible-p)))
  4232. (and org-insert-heading-respect-content (org-show-subtree))
  4233. (let ((split
  4234. (and (org-get-alist-option org-M-RET-may-split-line 'headline)
  4235. (save-excursion
  4236. (let ((p (point)))
  4237. (goto-char (point-at-bol))
  4238. (and (looking-at org-complex-heading-regexp)
  4239. (> p (match-beginning 4)))))))
  4240. tags pos)
  4241. (cond
  4242. (org-insert-heading-respect-content
  4243. (org-end-of-subtree nil t)
  4244. (or (bolp) (newline))
  4245. (open-line 1))
  4246. ((org-on-heading-p)
  4247. (when hide-previous
  4248. (show-children)
  4249. (org-show-entry))
  4250. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  4251. (setq tags (and (match-end 2) (match-string 2)))
  4252. (and (match-end 1)
  4253. (delete-region (match-beginning 1) (match-end 1)))
  4254. (setq pos (point-at-bol))
  4255. (or split (end-of-line 1))
  4256. (delete-horizontal-space)
  4257. (newline (if blank 2 1))
  4258. (when tags
  4259. (save-excursion
  4260. (goto-char pos)
  4261. (end-of-line 1)
  4262. (insert " " tags)
  4263. (org-set-tags nil 'align))))
  4264. (t
  4265. (or split (end-of-line 1))
  4266. (newline (if blank 2 1)))))))
  4267. (insert head) (just-one-space)
  4268. (setq pos (point))
  4269. (end-of-line 1)
  4270. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  4271. (when (and org-insert-heading-respect-content hide-previous)
  4272. (save-excursion
  4273. (goto-char previous-pos)
  4274. (hide-subtree)))
  4275. (run-hooks 'org-insert-heading-hook)))))
  4276. (defun org-get-heading (&optional no-tags)
  4277. "Return the heading of the current entry, without the stars."
  4278. (save-excursion
  4279. (org-back-to-heading t)
  4280. (if (looking-at
  4281. (if no-tags
  4282. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  4283. "\\*+[ \t]+\\([^\r\n]*\\)"))
  4284. (match-string 1) "")))
  4285. (defun org-insert-heading-after-current ()
  4286. "Insert a new heading with same level as current, after current subtree."
  4287. (interactive)
  4288. (org-back-to-heading)
  4289. (org-insert-heading)
  4290. (org-move-subtree-down)
  4291. (end-of-line 1))
  4292. (defun org-insert-heading-respect-content ()
  4293. (interactive)
  4294. (let ((org-insert-heading-respect-content t))
  4295. (org-insert-heading t)))
  4296. (defun org-insert-todo-heading-respect-content (&optional force-state)
  4297. (interactive "P")
  4298. (let ((org-insert-heading-respect-content t))
  4299. (org-insert-todo-heading force-state t)))
  4300. (defun org-insert-todo-heading (arg &optional force-heading)
  4301. "Insert a new heading with the same level and TODO state as current heading.
  4302. If the heading has no TODO state, or if the state is DONE, use the first
  4303. state (TODO by default). Also with prefix arg, force first state."
  4304. (interactive "P")
  4305. (when (or force-heading (not (org-insert-item 'checkbox)))
  4306. (org-insert-heading force-heading)
  4307. (save-excursion
  4308. (org-back-to-heading)
  4309. (outline-previous-heading)
  4310. (looking-at org-todo-line-regexp))
  4311. (if (or arg
  4312. (not (match-beginning 2))
  4313. (member (match-string 2) org-done-keywords))
  4314. (insert (car org-todo-keywords-1) " ")
  4315. (insert (match-string 2) " "))
  4316. (when org-provide-todo-statistics
  4317. (org-update-parent-todo-statistics))))
  4318. (defun org-insert-subheading (arg)
  4319. "Insert a new subheading and demote it.
  4320. Works for outline headings and for plain lists alike."
  4321. (interactive "P")
  4322. (org-insert-heading arg)
  4323. (cond
  4324. ((org-on-heading-p) (org-do-demote))
  4325. ((org-at-item-p) (org-indent-item 1))))
  4326. (defun org-insert-todo-subheading (arg)
  4327. "Insert a new subheading with TODO keyword or checkbox and demote it.
  4328. Works for outline headings and for plain lists alike."
  4329. (interactive "P")
  4330. (org-insert-todo-heading arg)
  4331. (cond
  4332. ((org-on-heading-p) (org-do-demote))
  4333. ((org-at-item-p) (org-indent-item 1))))
  4334. ;;; Promotion and Demotion
  4335. (defun org-promote-subtree ()
  4336. "Promote the entire subtree.
  4337. See also `org-promote'."
  4338. (interactive)
  4339. (save-excursion
  4340. (org-map-tree 'org-promote))
  4341. (org-fix-position-after-promote))
  4342. (defun org-demote-subtree ()
  4343. "Demote the entire subtree. See `org-demote'.
  4344. See also `org-promote'."
  4345. (interactive)
  4346. (save-excursion
  4347. (org-map-tree 'org-demote))
  4348. (org-fix-position-after-promote))
  4349. (defun org-do-promote ()
  4350. "Promote the current heading higher up the tree.
  4351. If the region is active in `transient-mark-mode', promote all headings
  4352. in the region."
  4353. (interactive)
  4354. (save-excursion
  4355. (if (org-region-active-p)
  4356. (org-map-region 'org-promote (region-beginning) (region-end))
  4357. (org-promote)))
  4358. (org-fix-position-after-promote))
  4359. (defun org-do-demote ()
  4360. "Demote the current heading lower down the tree.
  4361. If the region is active in `transient-mark-mode', demote all headings
  4362. in the region."
  4363. (interactive)
  4364. (save-excursion
  4365. (if (org-region-active-p)
  4366. (org-map-region 'org-demote (region-beginning) (region-end))
  4367. (org-demote)))
  4368. (org-fix-position-after-promote))
  4369. (defun org-fix-position-after-promote ()
  4370. "Make sure that after pro/demotion cursor position is right."
  4371. (let ((pos (point)))
  4372. (when (save-excursion
  4373. (beginning-of-line 1)
  4374. (looking-at org-todo-line-regexp)
  4375. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4376. (cond ((eobp) (insert " "))
  4377. ((eolp) (insert " "))
  4378. ((equal (char-after) ?\ ) (forward-char 1))))))
  4379. (defun org-reduced-level (l)
  4380. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4381. (defun org-get-valid-level (level &optional change)
  4382. "Rectify a level change under the influence of `org-odd-levels-only'
  4383. LEVEL is a current level, CHANGE is by how much the level should be
  4384. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4385. even level numbers will become the next higher odd number."
  4386. (if org-odd-levels-only
  4387. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4388. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4389. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4390. (max 1 (+ level change))))
  4391. (if (boundp 'define-obsolete-function-alias)
  4392. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4393. (define-obsolete-function-alias 'org-get-legal-level
  4394. 'org-get-valid-level)
  4395. (define-obsolete-function-alias 'org-get-legal-level
  4396. 'org-get-valid-level "23.1")))
  4397. (defun org-promote ()
  4398. "Promote the current heading higher up the tree.
  4399. If the region is active in `transient-mark-mode', promote all headings
  4400. in the region."
  4401. (org-back-to-heading t)
  4402. (let* ((level (save-match-data (funcall outline-level)))
  4403. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4404. (diff (abs (- level (length up-head) -1))))
  4405. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4406. (replace-match up-head nil t)
  4407. ;; Fixup tag positioning
  4408. (and org-auto-align-tags (org-set-tags nil t))
  4409. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4410. (defun org-demote ()
  4411. "Demote the current heading lower down the tree.
  4412. If the region is active in `transient-mark-mode', demote all headings
  4413. in the region."
  4414. (org-back-to-heading t)
  4415. (let* ((level (save-match-data (funcall outline-level)))
  4416. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4417. (diff (abs (- level (length down-head) -1))))
  4418. (replace-match down-head nil t)
  4419. ;; Fixup tag positioning
  4420. (and org-auto-align-tags (org-set-tags nil t))
  4421. (if org-adapt-indentation (org-fixup-indentation diff))))
  4422. (defun org-map-tree (fun)
  4423. "Call FUN for every heading underneath the current one."
  4424. (org-back-to-heading)
  4425. (let ((level (funcall outline-level)))
  4426. (save-excursion
  4427. (funcall fun)
  4428. (while (and (progn
  4429. (outline-next-heading)
  4430. (> (funcall outline-level) level))
  4431. (not (eobp)))
  4432. (funcall fun)))))
  4433. (defun org-map-region (fun beg end)
  4434. "Call FUN for every heading between BEG and END."
  4435. (let ((org-ignore-region t))
  4436. (save-excursion
  4437. (setq end (copy-marker end))
  4438. (goto-char beg)
  4439. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4440. (< (point) end))
  4441. (funcall fun))
  4442. (while (and (progn
  4443. (outline-next-heading)
  4444. (< (point) end))
  4445. (not (eobp)))
  4446. (funcall fun)))))
  4447. (defun org-fixup-indentation (diff)
  4448. "Change the indentation in the current entry by DIFF
  4449. However, if any line in the current entry has no indentation, or if it
  4450. would end up with no indentation after the change, nothing at all is done."
  4451. (save-excursion
  4452. (let ((end (save-excursion (outline-next-heading)
  4453. (point-marker)))
  4454. (prohibit (if (> diff 0)
  4455. "^\\S-"
  4456. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4457. col)
  4458. (unless (save-excursion (end-of-line 1)
  4459. (re-search-forward prohibit end t))
  4460. (while (and (< (point) end)
  4461. (re-search-forward "^[ \t]+" end t))
  4462. (goto-char (match-end 0))
  4463. (setq col (current-column))
  4464. (if (< diff 0) (replace-match ""))
  4465. (org-indent-to-column (+ diff col))))
  4466. (move-marker end nil))))
  4467. (defun org-convert-to-odd-levels ()
  4468. "Convert an org-mode file with all levels allowed to one with odd levels.
  4469. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4470. level 5 etc."
  4471. (interactive)
  4472. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4473. (let ((org-odd-levels-only nil) n)
  4474. (save-excursion
  4475. (goto-char (point-min))
  4476. (while (re-search-forward "^\\*\\*+ " nil t)
  4477. (setq n (- (length (match-string 0)) 2))
  4478. (while (>= (setq n (1- n)) 0)
  4479. (org-demote))
  4480. (end-of-line 1))))))
  4481. (defun org-convert-to-oddeven-levels ()
  4482. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4483. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4484. section with an even level, conversion would destroy the structure of the file. An error
  4485. is signaled in this case."
  4486. (interactive)
  4487. (goto-char (point-min))
  4488. ;; First check if there are no even levels
  4489. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4490. (org-show-context t)
  4491. (error "Not all levels are odd in this file. Conversion not possible."))
  4492. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4493. (let ((org-odd-levels-only nil) n)
  4494. (save-excursion
  4495. (goto-char (point-min))
  4496. (while (re-search-forward "^\\*\\*+ " nil t)
  4497. (setq n (/ (1- (length (match-string 0))) 2))
  4498. (while (>= (setq n (1- n)) 0)
  4499. (org-promote))
  4500. (end-of-line 1))))))
  4501. (defun org-tr-level (n)
  4502. "Make N odd if required."
  4503. (if org-odd-levels-only (1+ (/ n 2)) n))
  4504. ;;; Vertical tree motion, cutting and pasting of subtrees
  4505. (defun org-move-subtree-up (&optional arg)
  4506. "Move the current subtree up past ARG headlines of the same level."
  4507. (interactive "p")
  4508. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4509. (defun org-move-subtree-down (&optional arg)
  4510. "Move the current subtree down past ARG headlines of the same level."
  4511. (interactive "p")
  4512. (setq arg (prefix-numeric-value arg))
  4513. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4514. 'outline-get-last-sibling))
  4515. (ins-point (make-marker))
  4516. (cnt (abs arg))
  4517. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4518. ;; Select the tree
  4519. (org-back-to-heading)
  4520. (setq beg0 (point))
  4521. (save-excursion
  4522. (setq ne-beg (org-back-over-empty-lines))
  4523. (setq beg (point)))
  4524. (save-match-data
  4525. (save-excursion (outline-end-of-heading)
  4526. (setq folded (org-invisible-p)))
  4527. (outline-end-of-subtree))
  4528. (outline-next-heading)
  4529. (setq ne-end (org-back-over-empty-lines))
  4530. (setq end (point))
  4531. (goto-char beg0)
  4532. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4533. ;; include less whitespace
  4534. (save-excursion
  4535. (goto-char beg)
  4536. (forward-line (- ne-beg ne-end))
  4537. (setq beg (point))))
  4538. ;; Find insertion point, with error handling
  4539. (while (> cnt 0)
  4540. (or (and (funcall movfunc) (looking-at outline-regexp))
  4541. (progn (goto-char beg0)
  4542. (error "Cannot move past superior level or buffer limit")))
  4543. (setq cnt (1- cnt)))
  4544. (if (> arg 0)
  4545. ;; Moving forward - still need to move over subtree
  4546. (progn (org-end-of-subtree t t)
  4547. (save-excursion
  4548. (org-back-over-empty-lines)
  4549. (or (bolp) (newline)))))
  4550. (setq ne-ins (org-back-over-empty-lines))
  4551. (move-marker ins-point (point))
  4552. (setq txt (buffer-substring beg end))
  4553. (org-save-markers-in-region beg end)
  4554. (delete-region beg end)
  4555. (outline-flag-region (1- beg) beg nil)
  4556. (outline-flag-region (1- (point)) (point) nil)
  4557. (let ((bbb (point)))
  4558. (insert-before-markers txt)
  4559. (org-reinstall-markers-in-region bbb)
  4560. (move-marker ins-point bbb))
  4561. (or (bolp) (insert "\n"))
  4562. (setq ins-end (point))
  4563. (goto-char ins-point)
  4564. (org-skip-whitespace)
  4565. (when (and (< arg 0)
  4566. (org-first-sibling-p)
  4567. (> ne-ins ne-beg))
  4568. ;; Move whitespace back to beginning
  4569. (save-excursion
  4570. (goto-char ins-end)
  4571. (let ((kill-whole-line t))
  4572. (kill-line (- ne-ins ne-beg)) (point)))
  4573. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4574. (move-marker ins-point nil)
  4575. (org-compact-display-after-subtree-move)
  4576. (org-show-empty-lines-in-parent)
  4577. (unless folded
  4578. (org-show-entry)
  4579. (show-children)
  4580. (org-cycle-hide-drawers 'children))))
  4581. (defvar org-subtree-clip ""
  4582. "Clipboard for cut and paste of subtrees.
  4583. This is actually only a copy of the kill, because we use the normal kill
  4584. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4585. (defvar org-subtree-clip-folded nil
  4586. "Was the last copied subtree folded?
  4587. This is used to fold the tree back after pasting.")
  4588. (defun org-cut-subtree (&optional n)
  4589. "Cut the current subtree into the clipboard.
  4590. With prefix arg N, cut this many sequential subtrees.
  4591. This is a short-hand for marking the subtree and then cutting it."
  4592. (interactive "p")
  4593. (org-copy-subtree n 'cut))
  4594. (defun org-copy-subtree (&optional n cut force-store-markers)
  4595. "Cut the current subtree into the clipboard.
  4596. With prefix arg N, cut this many sequential subtrees.
  4597. This is a short-hand for marking the subtree and then copying it.
  4598. If CUT is non-nil, actually cut the subtree.
  4599. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4600. of some markers in the region, even if CUT is non-nil. This is
  4601. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4602. (interactive "p")
  4603. (let (beg end folded (beg0 (point)))
  4604. (if (interactive-p)
  4605. (org-back-to-heading nil) ; take what looks like a subtree
  4606. (org-back-to-heading t)) ; take what is really there
  4607. (org-back-over-empty-lines)
  4608. (setq beg (point))
  4609. (skip-chars-forward " \t\r\n")
  4610. (save-match-data
  4611. (save-excursion (outline-end-of-heading)
  4612. (setq folded (org-invisible-p)))
  4613. (condition-case nil
  4614. (outline-forward-same-level (1- n))
  4615. (error nil))
  4616. (org-end-of-subtree t t))
  4617. (org-back-over-empty-lines)
  4618. (setq end (point))
  4619. (goto-char beg0)
  4620. (when (> end beg)
  4621. (setq org-subtree-clip-folded folded)
  4622. (when (or cut force-store-markers)
  4623. (org-save-markers-in-region beg end))
  4624. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4625. (setq org-subtree-clip (current-kill 0))
  4626. (message "%s: Subtree(s) with %d characters"
  4627. (if cut "Cut" "Copied")
  4628. (length org-subtree-clip)))))
  4629. (defun org-paste-subtree (&optional level tree for-yank)
  4630. "Paste the clipboard as a subtree, with modification of headline level.
  4631. The entire subtree is promoted or demoted in order to match a new headline
  4632. level.
  4633. If the cursor is at the beginning of a headline, the same level as
  4634. that headline is used to paste the tree
  4635. If not, the new level is derived from the *visible* headings
  4636. before and after the insertion point, and taken to be the inferior headline
  4637. level of the two. So if the previous visible heading is level 3 and the
  4638. next is level 4 (or vice versa), level 4 will be used for insertion.
  4639. This makes sure that the subtree remains an independent subtree and does
  4640. not swallow low level entries.
  4641. You can also force a different level, either by using a numeric prefix
  4642. argument, or by inserting the heading marker by hand. For example, if the
  4643. cursor is after \"*****\", then the tree will be shifted to level 5.
  4644. If optional TREE is given, use this text instead of the kill ring.
  4645. When FOR-YANK is set, this is called by `org-yank'. In this case, do not
  4646. move back over whitespace before inserting, and move point to the end of
  4647. the inserted text when done."
  4648. (interactive "P")
  4649. (unless (org-kill-is-subtree-p tree)
  4650. (error "%s"
  4651. (substitute-command-keys
  4652. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4653. (let* ((visp (not (org-invisible-p)))
  4654. (txt (or tree (and kill-ring (current-kill 0))))
  4655. (^re (concat "^\\(" outline-regexp "\\)"))
  4656. (re (concat "\\(" outline-regexp "\\)"))
  4657. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4658. (old-level (if (string-match ^re txt)
  4659. (- (match-end 0) (match-beginning 0) 1)
  4660. -1))
  4661. (force-level (cond (level (prefix-numeric-value level))
  4662. ((and (looking-at "[ \t]*$")
  4663. (string-match
  4664. ^re_ (buffer-substring
  4665. (point-at-bol) (point))))
  4666. (- (match-end 1) (match-beginning 1)))
  4667. ((and (bolp)
  4668. (looking-at org-outline-regexp))
  4669. (- (match-end 0) (point) 1))
  4670. (t nil)))
  4671. (previous-level (save-excursion
  4672. (condition-case nil
  4673. (progn
  4674. (outline-previous-visible-heading 1)
  4675. (if (looking-at re)
  4676. (- (match-end 0) (match-beginning 0) 1)
  4677. 1))
  4678. (error 1))))
  4679. (next-level (save-excursion
  4680. (condition-case nil
  4681. (progn
  4682. (or (looking-at outline-regexp)
  4683. (outline-next-visible-heading 1))
  4684. (if (looking-at re)
  4685. (- (match-end 0) (match-beginning 0) 1)
  4686. 1))
  4687. (error 1))))
  4688. (new-level (or force-level (max previous-level next-level)))
  4689. (shift (if (or (= old-level -1)
  4690. (= new-level -1)
  4691. (= old-level new-level))
  4692. 0
  4693. (- new-level old-level)))
  4694. (delta (if (> shift 0) -1 1))
  4695. (func (if (> shift 0) 'org-demote 'org-promote))
  4696. (org-odd-levels-only nil)
  4697. beg end newend)
  4698. ;; Remove the forced level indicator
  4699. (if force-level
  4700. (delete-region (point-at-bol) (point)))
  4701. ;; Paste
  4702. (beginning-of-line 1)
  4703. (unless for-yank (org-back-over-empty-lines))
  4704. (setq beg (point))
  4705. (insert-before-markers txt)
  4706. (unless (string-match "\n\\'" txt) (insert "\n"))
  4707. (setq newend (point))
  4708. (org-reinstall-markers-in-region beg)
  4709. (setq end (point))
  4710. (goto-char beg)
  4711. (skip-chars-forward " \t\n\r")
  4712. (setq beg (point))
  4713. (if (and (org-invisible-p) visp)
  4714. (save-excursion (outline-show-heading)))
  4715. ;; Shift if necessary
  4716. (unless (= shift 0)
  4717. (save-restriction
  4718. (narrow-to-region beg end)
  4719. (while (not (= shift 0))
  4720. (org-map-region func (point-min) (point-max))
  4721. (setq shift (+ delta shift)))
  4722. (goto-char (point-min))
  4723. (setq newend (point-max))))
  4724. (when (or (interactive-p) for-yank)
  4725. (message "Clipboard pasted as level %d subtree" new-level))
  4726. (if (and (not for-yank) ; in this case, org-yank will decide about folding
  4727. kill-ring
  4728. (eq org-subtree-clip (current-kill 0))
  4729. org-subtree-clip-folded)
  4730. ;; The tree was folded before it was killed/copied
  4731. (hide-subtree))
  4732. (and for-yank (goto-char newend))))
  4733. (defun org-kill-is-subtree-p (&optional txt)
  4734. "Check if the current kill is an outline subtree, or a set of trees.
  4735. Returns nil if kill does not start with a headline, or if the first
  4736. headline level is not the largest headline level in the tree.
  4737. So this will actually accept several entries of equal levels as well,
  4738. which is OK for `org-paste-subtree'.
  4739. If optional TXT is given, check this string instead of the current kill."
  4740. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4741. (start-level (and kill
  4742. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4743. org-outline-regexp "\\)")
  4744. kill)
  4745. (- (match-end 2) (match-beginning 2) 1)))
  4746. (re (concat "^" org-outline-regexp))
  4747. (start (1+ (or (match-beginning 2) -1))))
  4748. (if (not start-level)
  4749. (progn
  4750. nil) ;; does not even start with a heading
  4751. (catch 'exit
  4752. (while (setq start (string-match re kill (1+ start)))
  4753. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4754. (throw 'exit nil)))
  4755. t))))
  4756. (defvar org-markers-to-move nil
  4757. "Markers that should be moved with a cut-and-paste operation.
  4758. Those markers are stored together with their positions relative to
  4759. the start of the region.")
  4760. (defun org-save-markers-in-region (beg end)
  4761. "Check markers in region.
  4762. If these markers are between BEG and END, record their position relative
  4763. to BEG, so that after moving the block of text, we can put the markers back
  4764. into place.
  4765. This function gets called just before an entry or tree gets cut from the
  4766. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4767. called immediately, to move the markers with the entries."
  4768. (setq org-markers-to-move nil)
  4769. (when (featurep 'org-clock)
  4770. (org-clock-save-markers-for-cut-and-paste beg end))
  4771. (when (featurep 'org-agenda)
  4772. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4773. (defun org-check-and-save-marker (marker beg end)
  4774. "Check if MARKER is between BEG and END.
  4775. If yes, remember the marker and the distance to BEG."
  4776. (when (and (marker-buffer marker)
  4777. (equal (marker-buffer marker) (current-buffer)))
  4778. (if (and (>= marker beg) (< marker end))
  4779. (push (cons marker (- marker beg)) org-markers-to-move))))
  4780. (defun org-reinstall-markers-in-region (beg)
  4781. "Move all remembered markers to their position relative to BEG."
  4782. (mapc (lambda (x)
  4783. (move-marker (car x) (+ beg (cdr x))))
  4784. org-markers-to-move)
  4785. (setq org-markers-to-move nil))
  4786. (defun org-narrow-to-subtree ()
  4787. "Narrow buffer to the current subtree."
  4788. (interactive)
  4789. (save-excursion
  4790. (save-match-data
  4791. (narrow-to-region
  4792. (progn (org-back-to-heading) (point))
  4793. (progn (org-end-of-subtree t) (point))))))
  4794. ;;; Outline Sorting
  4795. (defun org-sort (with-case)
  4796. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4797. Optional argument WITH-CASE means sort case-sensitively."
  4798. (interactive "P")
  4799. (if (org-at-table-p)
  4800. (org-call-with-arg 'org-table-sort-lines with-case)
  4801. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4802. (defun org-sort-remove-invisible (s)
  4803. (remove-text-properties 0 (length s) org-rm-props s)
  4804. (while (string-match org-bracket-link-regexp s)
  4805. (setq s (replace-match (if (match-end 2)
  4806. (match-string 3 s)
  4807. (match-string 1 s)) t t s)))
  4808. s)
  4809. (defvar org-priority-regexp) ; defined later in the file
  4810. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4811. "Sort entries on a certain level of an outline tree.
  4812. If there is an active region, the entries in the region are sorted.
  4813. Else, if the cursor is before the first entry, sort the top-level items.
  4814. Else, the children of the entry at point are sorted.
  4815. Sorting can be alphabetically, numerically, and by date/time as given by
  4816. the first time stamp in the entry. The command prompts for the sorting
  4817. type unless it has been given to the function through the SORTING-TYPE
  4818. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4819. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4820. called with point at the beginning of the record. It must return either
  4821. a string or a number that should serve as the sorting key for that record.
  4822. Comparing entries ignores case by default. However, with an optional argument
  4823. WITH-CASE, the sorting considers case as well."
  4824. (interactive "P")
  4825. (let ((case-func (if with-case 'identity 'downcase))
  4826. start beg end stars re re2
  4827. txt what tmp plain-list-p)
  4828. ;; Find beginning and end of region to sort
  4829. (cond
  4830. ((org-region-active-p)
  4831. ;; we will sort the region
  4832. (setq end (region-end)
  4833. what "region")
  4834. (goto-char (region-beginning))
  4835. (if (not (org-on-heading-p)) (outline-next-heading))
  4836. (setq start (point)))
  4837. ((org-at-item-p)
  4838. ;; we will sort this plain list
  4839. (org-beginning-of-item-list) (setq start (point))
  4840. (org-end-of-item-list) (setq end (point))
  4841. (goto-char start)
  4842. (setq plain-list-p t
  4843. what "plain list"))
  4844. ((or (org-on-heading-p)
  4845. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4846. ;; we will sort the children of the current headline
  4847. (org-back-to-heading)
  4848. (setq start (point)
  4849. end (progn (org-end-of-subtree t t)
  4850. (org-back-over-empty-lines)
  4851. (point))
  4852. what "children")
  4853. (goto-char start)
  4854. (show-subtree)
  4855. (outline-next-heading))
  4856. (t
  4857. ;; we will sort the top-level entries in this file
  4858. (goto-char (point-min))
  4859. (or (org-on-heading-p) (outline-next-heading))
  4860. (setq start (point) end (point-max) what "top-level")
  4861. (goto-char start)
  4862. (show-all)))
  4863. (setq beg (point))
  4864. (if (>= beg end) (error "Nothing to sort"))
  4865. (unless plain-list-p
  4866. (looking-at "\\(\\*+\\)")
  4867. (setq stars (match-string 1)
  4868. re (concat "^" (regexp-quote stars) " +")
  4869. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4870. txt (buffer-substring beg end))
  4871. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4872. (if (and (not (equal stars "*")) (string-match re2 txt))
  4873. (error "Region to sort contains a level above the first entry")))
  4874. (unless sorting-type
  4875. (message
  4876. (if plain-list-p
  4877. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4878. "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:")
  4879. what)
  4880. (setq sorting-type (read-char-exclusive))
  4881. (and (= (downcase sorting-type) ?f)
  4882. (setq getkey-func
  4883. (org-ido-completing-read "Sort using function: "
  4884. obarray 'fboundp t nil nil))
  4885. (setq getkey-func (intern getkey-func)))
  4886. (and (= (downcase sorting-type) ?r)
  4887. (setq property
  4888. (org-ido-completing-read "Property: "
  4889. (mapcar 'list (org-buffer-property-keys t))
  4890. nil t))))
  4891. (message "Sorting entries...")
  4892. (save-restriction
  4893. (narrow-to-region start end)
  4894. (let ((dcst (downcase sorting-type))
  4895. (now (current-time)))
  4896. (sort-subr
  4897. (/= dcst sorting-type)
  4898. ;; This function moves to the beginning character of the "record" to
  4899. ;; be sorted.
  4900. (if plain-list-p
  4901. (lambda nil
  4902. (if (org-at-item-p) t (goto-char (point-max))))
  4903. (lambda nil
  4904. (if (re-search-forward re nil t)
  4905. (goto-char (match-beginning 0))
  4906. (goto-char (point-max)))))
  4907. ;; This function moves to the last character of the "record" being
  4908. ;; sorted.
  4909. (if plain-list-p
  4910. 'org-end-of-item
  4911. (lambda nil
  4912. (save-match-data
  4913. (condition-case nil
  4914. (outline-forward-same-level 1)
  4915. (error
  4916. (goto-char (point-max)))))))
  4917. ;; This function returns the value that gets sorted against.
  4918. (if plain-list-p
  4919. (lambda nil
  4920. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4921. (cond
  4922. ((= dcst ?n)
  4923. (string-to-number (buffer-substring (match-end 0)
  4924. (point-at-eol))))
  4925. ((= dcst ?a)
  4926. (buffer-substring (match-end 0) (point-at-eol)))
  4927. ((= dcst ?t)
  4928. (if (re-search-forward org-ts-regexp
  4929. (point-at-eol) t)
  4930. (org-time-string-to-time (match-string 0))
  4931. now))
  4932. ((= dcst ?f)
  4933. (if getkey-func
  4934. (progn
  4935. (setq tmp (funcall getkey-func))
  4936. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4937. tmp)
  4938. (error "Invalid key function `%s'" getkey-func)))
  4939. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4940. (lambda nil
  4941. (cond
  4942. ((= dcst ?n)
  4943. (if (looking-at org-complex-heading-regexp)
  4944. (string-to-number (match-string 4))
  4945. nil))
  4946. ((= dcst ?a)
  4947. (if (looking-at org-complex-heading-regexp)
  4948. (funcall case-func (match-string 4))
  4949. nil))
  4950. ((= dcst ?t)
  4951. (if (re-search-forward org-ts-regexp
  4952. (save-excursion
  4953. (forward-line 2)
  4954. (point)) t)
  4955. (org-time-string-to-time (match-string 0))
  4956. now))
  4957. ((= dcst ?p)
  4958. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4959. (string-to-char (match-string 2))
  4960. org-default-priority))
  4961. ((= dcst ?r)
  4962. (or (org-entry-get nil property) ""))
  4963. ((= dcst ?o)
  4964. (if (looking-at org-complex-heading-regexp)
  4965. (- 9999 (length (member (match-string 2)
  4966. org-todo-keywords-1)))))
  4967. ((= dcst ?f)
  4968. (if getkey-func
  4969. (progn
  4970. (setq tmp (funcall getkey-func))
  4971. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4972. tmp)
  4973. (error "Invalid key function `%s'" getkey-func)))
  4974. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4975. nil
  4976. (cond
  4977. ((= dcst ?a) 'string<)
  4978. ((= dcst ?t) 'time-less-p)
  4979. (t nil)))))
  4980. (message "Sorting entries...done")))
  4981. (defun org-do-sort (table what &optional with-case sorting-type)
  4982. "Sort TABLE of WHAT according to SORTING-TYPE.
  4983. The user will be prompted for the SORTING-TYPE if the call to this
  4984. function does not specify it. WHAT is only for the prompt, to indicate
  4985. what is being sorted. The sorting key will be extracted from
  4986. the car of the elements of the table.
  4987. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4988. (unless sorting-type
  4989. (message
  4990. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4991. what)
  4992. (setq sorting-type (read-char-exclusive)))
  4993. (let ((dcst (downcase sorting-type))
  4994. extractfun comparefun)
  4995. ;; Define the appropriate functions
  4996. (cond
  4997. ((= dcst ?n)
  4998. (setq extractfun 'string-to-number
  4999. comparefun (if (= dcst sorting-type) '< '>)))
  5000. ((= dcst ?a)
  5001. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  5002. (lambda(x) (downcase (org-sort-remove-invisible x))))
  5003. comparefun (if (= dcst sorting-type)
  5004. 'string<
  5005. (lambda (a b) (and (not (string< a b))
  5006. (not (string= a b)))))))
  5007. ((= dcst ?t)
  5008. (setq extractfun
  5009. (lambda (x)
  5010. (if (string-match org-ts-regexp x)
  5011. (time-to-seconds
  5012. (org-time-string-to-time (match-string 0 x)))
  5013. 0))
  5014. comparefun (if (= dcst sorting-type) '< '>)))
  5015. (t (error "Invalid sorting type `%c'" sorting-type)))
  5016. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  5017. table)
  5018. (lambda (a b) (funcall comparefun (car a) (car b))))))
  5019. ;;; Editing source examples
  5020. (defvar org-exit-edit-mode-map (make-sparse-keymap))
  5021. (define-key org-exit-edit-mode-map "\C-c'" 'org-edit-src-exit)
  5022. (defvar org-edit-src-force-single-line nil)
  5023. (defvar org-edit-src-from-org-mode nil)
  5024. (defvar org-edit-src-picture nil)
  5025. (define-minor-mode org-exit-edit-mode
  5026. "Minor mode installing a single key binding, \"C-c '\" to exit special edit.")
  5027. (defun org-edit-src-code ()
  5028. "Edit the source code example at point.
  5029. An indirect buffer is created, and that buffer is then narrowed to the
  5030. example at point and switched to the correct language mode. When done,
  5031. exit by killing the buffer with \\[org-edit-src-exit]."
  5032. (interactive)
  5033. (let ((line (org-current-line))
  5034. (case-fold-search t)
  5035. (msg (substitute-command-keys
  5036. "Edit, then exit with C-c ' (C-c and single quote)"))
  5037. (info (org-edit-src-find-region-and-lang))
  5038. (org-mode-p (eq major-mode 'org-mode))
  5039. beg end lang lang-f single)
  5040. (if (not info)
  5041. nil
  5042. (setq beg (nth 0 info)
  5043. end (nth 1 info)
  5044. lang (nth 2 info)
  5045. single (nth 3 info)
  5046. lang-f (intern (concat lang "-mode")))
  5047. (unless (functionp lang-f)
  5048. (error "No such language mode: %s" lang-f))
  5049. (goto-line line)
  5050. (if (get-buffer "*Org Edit Src Example*")
  5051. (kill-buffer "*Org Edit Src Example*"))
  5052. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5053. "*Org Edit Src Example*"))
  5054. (narrow-to-region beg end)
  5055. (remove-text-properties beg end '(display nil invisible nil
  5056. intangible nil))
  5057. (let ((org-inhibit-startup t))
  5058. (funcall lang-f))
  5059. (set (make-local-variable 'org-edit-src-force-single-line) single)
  5060. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5061. (when org-mode-p
  5062. (goto-char (point-min))
  5063. (while (re-search-forward "^," nil t)
  5064. (replace-match "")))
  5065. (goto-line line)
  5066. (org-exit-edit-mode)
  5067. (org-set-local 'header-line-format msg)
  5068. (message "%s" msg)
  5069. t)))
  5070. (defun org-edit-fixed-width-region ()
  5071. "Edit the fixed-width ascii drawing at point.
  5072. This must be a region where each line starts with ca colon followed by
  5073. a space character.
  5074. An indirect buffer is created, and that buffer is then narrowed to the
  5075. example at point and switched to artist-mode. When done,
  5076. exit by killing the buffer with \\[org-edit-src-exit]."
  5077. (interactive)
  5078. (let ((line (org-current-line))
  5079. (case-fold-search t)
  5080. (msg (substitute-command-keys
  5081. "Edit, then exit with C-c ' (C-c and single quote)"))
  5082. (org-mode-p (eq major-mode 'org-mode))
  5083. beg end lang lang-f)
  5084. (beginning-of-line 1)
  5085. (if (looking-at "[ \t]*[^:\n \t]")
  5086. nil
  5087. (if (looking-at "[ \t]*\\(\n\\|\\'\\)")
  5088. (setq beg (point) end beg)
  5089. (save-excursion
  5090. (if (re-search-backward "^[ \t]*[^:]" nil 'move)
  5091. (setq beg (point-at-bol 2))
  5092. (setq beg (point))))
  5093. (save-excursion
  5094. (if (re-search-forward "^[ \t]*[^:]" nil 'move)
  5095. (setq end (1- (match-beginning 0)))
  5096. (setq end (point))))
  5097. (goto-line line))
  5098. (if (get-buffer "*Org Edit Picture*")
  5099. (kill-buffer "*Org Edit Picture*"))
  5100. (switch-to-buffer (make-indirect-buffer (current-buffer)
  5101. "*Org Edit Picture*"))
  5102. (narrow-to-region beg end)
  5103. (remove-text-properties beg end '(display nil invisible nil
  5104. intangible nil))
  5105. (when (fboundp 'font-lock-unfontify-region)
  5106. (font-lock-unfontify-region (point-min) (point-max)))
  5107. (cond
  5108. ((eq org-edit-fixed-width-region-mode 'artist-mode)
  5109. (fundamental-mode)
  5110. (artist-mode 1))
  5111. (t (funcall org-edit-fixed-width-region-mode)))
  5112. (set (make-local-variable 'org-edit-src-force-single-line) nil)
  5113. (set (make-local-variable 'org-edit-src-from-org-mode) org-mode-p)
  5114. (set (make-local-variable 'org-edit-src-picture) t)
  5115. (goto-char (point-min))
  5116. (while (re-search-forward "^[ \t]*: ?" nil t)
  5117. (replace-match ""))
  5118. (goto-line line)
  5119. (org-exit-edit-mode)
  5120. (org-set-local 'header-line-format msg)
  5121. (message "%s" msg)
  5122. t)))
  5123. (defun org-edit-src-find-region-and-lang ()
  5124. "Find the region and language for a local edit.
  5125. Return a list with beginning and end of the region, a string representing
  5126. the language, a switch telling of the content should be in a single line."
  5127. (let ((re-list
  5128. (append
  5129. org-edit-src-region-extra
  5130. '(
  5131. ("<src\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</src>" lang)
  5132. ("<literal\\>[^<]*>[ \t]*\n?" "\n?[ \t]*</literal>" style)
  5133. ("<example>[ \t]*\n?" "\n?[ \t]*</example>" "fundamental")
  5134. ("<lisp>[ \t]*\n?" "\n?[ \t]*</lisp>" "emacs-lisp")
  5135. ("<perl>[ \t]*\n?" "\n?[ \t]*</perl>" "perl")
  5136. ("<python>[ \t]*\n?" "\n?[ \t]*</python>" "python")
  5137. ("<ruby>[ \t]*\n?" "\n?[ \t]*</ruby>" "ruby")
  5138. ("^#\\+begin_src\\( \\([^ \t\n]+\\)\\)?.*\n" "\n#\\+end_src" 2)
  5139. ("^#\\+begin_example.*\n" "\n#\\+end_example" "fundamental")
  5140. ("^#\\+html:" "\n" "html" single-line)
  5141. ("^#\\+begin_html.*\n" "\n#\\+end_html" "html")
  5142. ("^#\\+begin_latex.*\n" "\n#\\+end_latex" "latex")
  5143. ("^#\\+latex:" "\n" "latex" single-line)
  5144. ("^#\\+begin_ascii.*\n" "\n#\\+end_ascii" "fundamental")
  5145. ("^#\\+ascii:" "\n" "ascii" single-line)
  5146. )))
  5147. (pos (point))
  5148. re re1 re2 single beg end lang)
  5149. (catch 'exit
  5150. (while (setq entry (pop re-list))
  5151. (setq re1 (car entry) re2 (nth 1 entry) lang (nth 2 entry)
  5152. single (nth 3 entry))
  5153. (save-excursion
  5154. (if (or (looking-at re1)
  5155. (re-search-backward re1 nil t))
  5156. (progn
  5157. (setq beg (match-end 0) lang (org-edit-src-get-lang lang))
  5158. (if (and (re-search-forward re2 nil t)
  5159. (>= (match-end 0) pos))
  5160. (throw 'exit (list beg (match-beginning 0) lang single))))
  5161. (if (or (looking-at re2)
  5162. (re-search-forward re2 nil t))
  5163. (progn
  5164. (setq end (match-beginning 0))
  5165. (if (and (re-search-backward re1 nil t)
  5166. (<= (match-beginning 0) pos))
  5167. (throw 'exit
  5168. (list (match-end 0) end
  5169. (org-edit-src-get-lang lang) single)))))))))))
  5170. (defun org-edit-src-get-lang (lang)
  5171. "Extract the src language."
  5172. (let ((m (match-string 0)))
  5173. (cond
  5174. ((stringp lang) lang)
  5175. ((integerp lang) (match-string lang))
  5176. ((and (eq lang 'lang)
  5177. (string-match "\\<lang=\"\\([^ \t\n\"]+\\)\"" m))
  5178. (match-string 1 m))
  5179. ((and (eq lang 'style)
  5180. (string-match "\\<style=\"\\([^ \t\n\"]+\\)\"" m))
  5181. (match-string 1 m))
  5182. (t "fundamental"))))
  5183. (defun org-edit-src-exit ()
  5184. "Exit special edit and protect problematic lines."
  5185. (interactive)
  5186. (unless (buffer-base-buffer (current-buffer))
  5187. (error "This is not an indirect buffer, something is wrong..."))
  5188. (unless (> (point-min) 1)
  5189. (error "This buffer is not narrowed, something is wrong..."))
  5190. (goto-char (point-min))
  5191. (if (looking-at "[ \t\n]*\n") (replace-match ""))
  5192. (if (re-search-forward "\n[ \t\n]*\\'" nil t) (replace-match ""))
  5193. (when (org-bound-and-true-p org-edit-src-force-single-line)
  5194. (goto-char (point-min))
  5195. (while (re-search-forward "\n" nil t)
  5196. (replace-match " "))
  5197. (goto-char (point-min))
  5198. (if (looking-at "\\s-*") (replace-match " "))
  5199. (if (re-search-forward "\\s-+\\'" nil t)
  5200. (replace-match "")))
  5201. (when (org-bound-and-true-p org-edit-src-from-org-mode)
  5202. (goto-char (point-min))
  5203. (while (re-search-forward (if (org-mode-p) "^\\(.\\)" "^\\([*#]\\)") nil t)
  5204. (replace-match ",\\1"))
  5205. (when font-lock-mode
  5206. (font-lock-unfontify-region (point-min) (point-max)))
  5207. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5208. (when (org-bound-and-true-p org-edit-src-picture)
  5209. (untabify (point-min) (point-max))
  5210. (goto-char (point-min))
  5211. (while (re-search-forward "^" nil t)
  5212. (replace-match ": "))
  5213. (when font-lock-mode
  5214. (font-lock-unfontify-region (point-min) (point-max)))
  5215. (put-text-property (point-min) (point-max) 'font-lock-fontified t))
  5216. (kill-buffer (current-buffer))
  5217. (and (org-mode-p) (org-restart-font-lock)))
  5218. ;;; The orgstruct minor mode
  5219. ;; Define a minor mode which can be used in other modes in order to
  5220. ;; integrate the org-mode structure editing commands.
  5221. ;; This is really a hack, because the org-mode structure commands use
  5222. ;; keys which normally belong to the major mode. Here is how it
  5223. ;; works: The minor mode defines all the keys necessary to operate the
  5224. ;; structure commands, but wraps the commands into a function which
  5225. ;; tests if the cursor is currently at a headline or a plain list
  5226. ;; item. If that is the case, the structure command is used,
  5227. ;; temporarily setting many Org-mode variables like regular
  5228. ;; expressions for filling etc. However, when any of those keys is
  5229. ;; used at a different location, function uses `key-binding' to look
  5230. ;; up if the key has an associated command in another currently active
  5231. ;; keymap (minor modes, major mode, global), and executes that
  5232. ;; command. There might be problems if any of the keys is otherwise
  5233. ;; used as a prefix key.
  5234. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5235. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5236. ;; addresses this by checking explicitly for both bindings.
  5237. (defvar orgstruct-mode-map (make-sparse-keymap)
  5238. "Keymap for the minor `orgstruct-mode'.")
  5239. (defvar org-local-vars nil
  5240. "List of local variables, for use by `orgstruct-mode'")
  5241. ;;;###autoload
  5242. (define-minor-mode orgstruct-mode
  5243. "Toggle the minor more `orgstruct-mode'.
  5244. This mode is for using Org-mode structure commands in other modes.
  5245. The following key behave as if Org-mode was active, if the cursor
  5246. is on a headline, or on a plain list item (both in the definition
  5247. of Org-mode).
  5248. M-up Move entry/item up
  5249. M-down Move entry/item down
  5250. M-left Promote
  5251. M-right Demote
  5252. M-S-up Move entry/item up
  5253. M-S-down Move entry/item down
  5254. M-S-left Promote subtree
  5255. M-S-right Demote subtree
  5256. M-q Fill paragraph and items like in Org-mode
  5257. C-c ^ Sort entries
  5258. C-c - Cycle list bullet
  5259. TAB Cycle item visibility
  5260. M-RET Insert new heading/item
  5261. S-M-RET Insert new TODO heading / Chekbox item
  5262. C-c C-c Set tags / toggle checkbox"
  5263. nil " OrgStruct" nil
  5264. (org-load-modules-maybe)
  5265. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5266. ;;;###autoload
  5267. (defun turn-on-orgstruct ()
  5268. "Unconditionally turn on `orgstruct-mode'."
  5269. (orgstruct-mode 1))
  5270. ;;;###autoload
  5271. (defun turn-on-orgstruct++ ()
  5272. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5273. In addition to setting orgstruct-mode, this also exports all indentation and
  5274. autofilling variables from org-mode into the buffer. Note that turning
  5275. off orgstruct-mode will *not* remove these additional settings."
  5276. (orgstruct-mode 1)
  5277. (let (var val)
  5278. (mapc
  5279. (lambda (x)
  5280. (when (string-match
  5281. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5282. (symbol-name (car x)))
  5283. (setq var (car x) val (nth 1 x))
  5284. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5285. org-local-vars)))
  5286. (defun orgstruct-error ()
  5287. "Error when there is no default binding for a structure key."
  5288. (interactive)
  5289. (error "This key has no function outside structure elements"))
  5290. (defun orgstruct-setup ()
  5291. "Setup orgstruct keymaps."
  5292. (let ((nfunc 0)
  5293. (bindings
  5294. (list
  5295. '([(meta up)] org-metaup)
  5296. '([(meta down)] org-metadown)
  5297. '([(meta left)] org-metaleft)
  5298. '([(meta right)] org-metaright)
  5299. '([(meta shift up)] org-shiftmetaup)
  5300. '([(meta shift down)] org-shiftmetadown)
  5301. '([(meta shift left)] org-shiftmetaleft)
  5302. '([(meta shift right)] org-shiftmetaright)
  5303. '([(shift up)] org-shiftup)
  5304. '([(shift down)] org-shiftdown)
  5305. '([(shift left)] org-shiftleft)
  5306. '([(shift right)] org-shiftright)
  5307. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5308. '("\M-q" fill-paragraph)
  5309. '("\C-c^" org-sort)
  5310. '("\C-c-" org-cycle-list-bullet)))
  5311. elt key fun cmd)
  5312. (while (setq elt (pop bindings))
  5313. (setq nfunc (1+ nfunc))
  5314. (setq key (org-key (car elt))
  5315. fun (nth 1 elt)
  5316. cmd (orgstruct-make-binding fun nfunc key))
  5317. (org-defkey orgstruct-mode-map key cmd))
  5318. ;; Special treatment needed for TAB and RET
  5319. (org-defkey orgstruct-mode-map [(tab)]
  5320. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5321. (org-defkey orgstruct-mode-map "\C-i"
  5322. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5323. (org-defkey orgstruct-mode-map "\M-\C-m"
  5324. (orgstruct-make-binding 'org-insert-heading 105
  5325. "\M-\C-m" [(meta return)]))
  5326. (org-defkey orgstruct-mode-map [(meta return)]
  5327. (orgstruct-make-binding 'org-insert-heading 106
  5328. [(meta return)] "\M-\C-m"))
  5329. (org-defkey orgstruct-mode-map [(shift meta return)]
  5330. (orgstruct-make-binding 'org-insert-todo-heading 107
  5331. [(meta return)] "\M-\C-m"))
  5332. (unless org-local-vars
  5333. (setq org-local-vars (org-get-local-variables)))
  5334. t))
  5335. (defun orgstruct-make-binding (fun n &rest keys)
  5336. "Create a function for binding in the structure minor mode.
  5337. FUN is the command to call inside a table. N is used to create a unique
  5338. command name. KEYS are keys that should be checked in for a command
  5339. to execute outside of tables."
  5340. (eval
  5341. (list 'defun
  5342. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5343. '(arg)
  5344. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5345. "Outside of structure, run the binding of `"
  5346. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5347. "'.")
  5348. '(interactive "p")
  5349. (list 'if
  5350. '(org-context-p 'headline 'item)
  5351. (list 'org-run-like-in-org-mode (list 'quote fun))
  5352. (list 'let '(orgstruct-mode)
  5353. (list 'call-interactively
  5354. (append '(or)
  5355. (mapcar (lambda (k)
  5356. (list 'key-binding k))
  5357. keys)
  5358. '('orgstruct-error))))))))
  5359. (defun org-context-p (&rest contexts)
  5360. "Check if local context is any of CONTEXTS.
  5361. Possible values in the list of contexts are `table', `headline', and `item'."
  5362. (let ((pos (point)))
  5363. (goto-char (point-at-bol))
  5364. (prog1 (or (and (memq 'table contexts)
  5365. (looking-at "[ \t]*|"))
  5366. (and (memq 'headline contexts)
  5367. ;;????????? (looking-at "\\*+"))
  5368. (looking-at outline-regexp))
  5369. (and (memq 'item contexts)
  5370. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5371. (goto-char pos))))
  5372. (defun org-get-local-variables ()
  5373. "Return a list of all local variables in an org-mode buffer."
  5374. (let (varlist)
  5375. (with-current-buffer (get-buffer-create "*Org tmp*")
  5376. (erase-buffer)
  5377. (org-mode)
  5378. (setq varlist (buffer-local-variables)))
  5379. (kill-buffer "*Org tmp*")
  5380. (delq nil
  5381. (mapcar
  5382. (lambda (x)
  5383. (setq x
  5384. (if (symbolp x)
  5385. (list x)
  5386. (list (car x) (list 'quote (cdr x)))))
  5387. (if (string-match
  5388. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5389. (symbol-name (car x)))
  5390. x nil))
  5391. varlist))))
  5392. ;;;###autoload
  5393. (defun org-run-like-in-org-mode (cmd)
  5394. (org-load-modules-maybe)
  5395. (unless org-local-vars
  5396. (setq org-local-vars (org-get-local-variables)))
  5397. (eval (list 'let org-local-vars
  5398. (list 'call-interactively (list 'quote cmd)))))
  5399. ;;;; Archiving
  5400. (defun org-get-category (&optional pos)
  5401. "Get the category applying to position POS."
  5402. (get-text-property (or pos (point)) 'org-category))
  5403. (defun org-refresh-category-properties ()
  5404. "Refresh category text properties in the buffer."
  5405. (let ((def-cat (cond
  5406. ((null org-category)
  5407. (if buffer-file-name
  5408. (file-name-sans-extension
  5409. (file-name-nondirectory buffer-file-name))
  5410. "???"))
  5411. ((symbolp org-category) (symbol-name org-category))
  5412. (t org-category)))
  5413. beg end cat pos optionp)
  5414. (org-unmodified
  5415. (save-excursion
  5416. (save-restriction
  5417. (widen)
  5418. (goto-char (point-min))
  5419. (put-text-property (point) (point-max) 'org-category def-cat)
  5420. (while (re-search-forward
  5421. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5422. (setq pos (match-end 0)
  5423. optionp (equal (char-after (match-beginning 0)) ?#)
  5424. cat (org-trim (match-string 2)))
  5425. (if optionp
  5426. (setq beg (point-at-bol) end (point-max))
  5427. (org-back-to-heading t)
  5428. (setq beg (point) end (org-end-of-subtree t t)))
  5429. (put-text-property beg end 'org-category cat)
  5430. (goto-char pos)))))))
  5431. ;;;; Link Stuff
  5432. ;;; Link abbreviations
  5433. (defun org-link-expand-abbrev (link)
  5434. "Apply replacements as defined in `org-link-abbrev-alist."
  5435. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5436. (let* ((key (match-string 1 link))
  5437. (as (or (assoc key org-link-abbrev-alist-local)
  5438. (assoc key org-link-abbrev-alist)))
  5439. (tag (and (match-end 2) (match-string 3 link)))
  5440. rpl)
  5441. (if (not as)
  5442. link
  5443. (setq rpl (cdr as))
  5444. (cond
  5445. ((symbolp rpl) (funcall rpl tag))
  5446. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5447. ((string-match "%h" rpl)
  5448. (replace-match (url-hexify-string (or tag "")) t t rpl))
  5449. (t (concat rpl tag)))))
  5450. link))
  5451. ;;; Storing and inserting links
  5452. (defvar org-insert-link-history nil
  5453. "Minibuffer history for links inserted with `org-insert-link'.")
  5454. (defvar org-stored-links nil
  5455. "Contains the links stored with `org-store-link'.")
  5456. (defvar org-store-link-plist nil
  5457. "Plist with info about the most recently link created with `org-store-link'.")
  5458. (defvar org-link-protocols nil
  5459. "Link protocols added to Org-mode using `org-add-link-type'.")
  5460. (defvar org-store-link-functions nil
  5461. "List of functions that are called to create and store a link.
  5462. Each function will be called in turn until one returns a non-nil
  5463. value. Each function should check if it is responsible for creating
  5464. this link (for example by looking at the major mode).
  5465. If not, it must exit and return nil.
  5466. If yes, it should return a non-nil value after a calling
  5467. `org-store-link-props' with a list of properties and values.
  5468. Special properties are:
  5469. :type The link prefix. like \"http\". This must be given.
  5470. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5471. This is obligatory as well.
  5472. :description Optional default description for the second pair
  5473. of brackets in an Org-mode link. The user can still change
  5474. this when inserting this link into an Org-mode buffer.
  5475. In addition to these, any additional properties can be specified
  5476. and then used in remember templates.")
  5477. (defun org-add-link-type (type &optional follow export)
  5478. "Add TYPE to the list of `org-link-types'.
  5479. Re-compute all regular expressions depending on `org-link-types'
  5480. FOLLOW and EXPORT are two functions.
  5481. FOLLOW should take the link path as the single argument and do whatever
  5482. is necessary to follow the link, for example find a file or display
  5483. a mail message.
  5484. EXPORT should format the link path for export to one of the export formats.
  5485. It should be a function accepting three arguments:
  5486. path the path of the link, the text after the prefix (like \"http:\")
  5487. desc the description of the link, if any, nil if there was no descripton
  5488. format the export format, a symbol like `html' or `latex'.
  5489. The function may use the FORMAT information to return different values
  5490. depending on the format. The return value will be put literally into
  5491. the exported file.
  5492. Org-mode has a built-in default for exporting links. If you are happy with
  5493. this default, there is no need to define an export function for the link
  5494. type. For a simple example of an export function, see `org-bbdb.el'."
  5495. (add-to-list 'org-link-types type t)
  5496. (org-make-link-regexps)
  5497. (if (assoc type org-link-protocols)
  5498. (setcdr (assoc type org-link-protocols) (list follow export))
  5499. (push (list type follow export) org-link-protocols)))
  5500. ;;;###autoload
  5501. (defun org-store-link (arg)
  5502. "\\<org-mode-map>Store an org-link to the current location.
  5503. This link is added to `org-stored-links' and can later be inserted
  5504. into an org-buffer with \\[org-insert-link].
  5505. For some link types, a prefix arg is interpreted:
  5506. For links to usenet articles, arg negates `org-gnus-prefer-web-links'.
  5507. For file links, arg negates `org-context-in-file-links'."
  5508. (interactive "P")
  5509. (org-load-modules-maybe)
  5510. (setq org-store-link-plist nil) ; reset
  5511. (let (link cpltxt desc description search txt)
  5512. (cond
  5513. ((run-hook-with-args-until-success 'org-store-link-functions)
  5514. (setq link (plist-get org-store-link-plist :link)
  5515. desc (or (plist-get org-store-link-plist :description) link)))
  5516. ((eq major-mode 'calendar-mode)
  5517. (let ((cd (calendar-cursor-to-date)))
  5518. (setq link
  5519. (format-time-string
  5520. (car org-time-stamp-formats)
  5521. (apply 'encode-time
  5522. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5523. nil nil nil))))
  5524. (org-store-link-props :type "calendar" :date cd)))
  5525. ((eq major-mode 'w3-mode)
  5526. (setq cpltxt (url-view-url t)
  5527. link (org-make-link cpltxt))
  5528. (org-store-link-props :type "w3" :url (url-view-url t)))
  5529. ((eq major-mode 'w3m-mode)
  5530. (setq cpltxt (or w3m-current-title w3m-current-url)
  5531. link (org-make-link w3m-current-url))
  5532. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5533. ((setq search (run-hook-with-args-until-success
  5534. 'org-create-file-search-functions))
  5535. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5536. "::" search))
  5537. (setq cpltxt (or description link)))
  5538. ((eq major-mode 'image-mode)
  5539. (setq cpltxt (concat "file:"
  5540. (abbreviate-file-name buffer-file-name))
  5541. link (org-make-link cpltxt))
  5542. (org-store-link-props :type "image" :file buffer-file-name))
  5543. ((eq major-mode 'dired-mode)
  5544. ;; link to the file in the current line
  5545. (setq cpltxt (concat "file:"
  5546. (abbreviate-file-name
  5547. (expand-file-name
  5548. (dired-get-filename nil t))))
  5549. link (org-make-link cpltxt)))
  5550. ((and buffer-file-name (org-mode-p))
  5551. ;; Just link to current headline
  5552. (setq cpltxt (concat "file:"
  5553. (abbreviate-file-name buffer-file-name)))
  5554. ;; Add a context search string
  5555. (when (org-xor org-context-in-file-links arg)
  5556. ;; Check if we are on a target
  5557. (if (org-in-regexp "<<\\(.*?\\)>>")
  5558. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5559. (setq txt (cond
  5560. ((org-on-heading-p) nil)
  5561. ((org-region-active-p)
  5562. (buffer-substring (region-beginning) (region-end)))
  5563. (t nil)))
  5564. (when (or (null txt) (string-match "\\S-" txt))
  5565. (setq cpltxt
  5566. (concat cpltxt "::"
  5567. (condition-case nil
  5568. (org-make-org-heading-search-string txt)
  5569. (error "")))
  5570. desc "NONE"))))
  5571. (if (string-match "::\\'" cpltxt)
  5572. (setq cpltxt (substring cpltxt 0 -2)))
  5573. (setq link (org-make-link cpltxt)))
  5574. ((buffer-file-name (buffer-base-buffer))
  5575. ;; Just link to this file here.
  5576. (setq cpltxt (concat "file:"
  5577. (abbreviate-file-name
  5578. (buffer-file-name (buffer-base-buffer)))))
  5579. ;; Add a context string
  5580. (when (org-xor org-context-in-file-links arg)
  5581. (setq txt (if (org-region-active-p)
  5582. (buffer-substring (region-beginning) (region-end))
  5583. (buffer-substring (point-at-bol) (point-at-eol))))
  5584. ;; Only use search option if there is some text.
  5585. (when (string-match "\\S-" txt)
  5586. (setq cpltxt
  5587. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5588. desc "NONE")))
  5589. (setq link (org-make-link cpltxt)))
  5590. ((interactive-p)
  5591. (error "Cannot link to a buffer which is not visiting a file"))
  5592. (t (setq link nil)))
  5593. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5594. (setq link (or link cpltxt)
  5595. desc (or desc cpltxt))
  5596. (if (equal desc "NONE") (setq desc nil))
  5597. (if (and (interactive-p) link)
  5598. (progn
  5599. (setq org-stored-links
  5600. (cons (list link desc) org-stored-links))
  5601. (message "Stored: %s" (or desc link)))
  5602. (and link (org-make-link-string link desc)))))
  5603. (defun org-store-link-props (&rest plist)
  5604. "Store link properties, extract names and addresses."
  5605. (let (x adr)
  5606. (when (setq x (plist-get plist :from))
  5607. (setq adr (mail-extract-address-components x))
  5608. (setq plist (plist-put plist :fromname (car adr)))
  5609. (setq plist (plist-put plist :fromaddress (nth 1 adr))))
  5610. (when (setq x (plist-get plist :to))
  5611. (setq adr (mail-extract-address-components x))
  5612. (setq plist (plist-put plist :toname (car adr)))
  5613. (setq plist (plist-put plist :toaddress (nth 1 adr)))))
  5614. (let ((from (plist-get plist :from))
  5615. (to (plist-get plist :to)))
  5616. (when (and from to org-from-is-user-regexp)
  5617. (setq plist
  5618. (plist-put plist :fromto
  5619. (if (string-match org-from-is-user-regexp from)
  5620. (concat "to %t")
  5621. (concat "from %f"))))))
  5622. (setq org-store-link-plist plist))
  5623. (defun org-add-link-props (&rest plist)
  5624. "Add these properties to the link property list."
  5625. (let (key value)
  5626. (while plist
  5627. (setq key (pop plist) value (pop plist))
  5628. (setq org-store-link-plist
  5629. (plist-put org-store-link-plist key value)))))
  5630. (defun org-email-link-description (&optional fmt)
  5631. "Return the description part of an email link.
  5632. This takes information from `org-store-link-plist' and formats it
  5633. according to FMT (default from `org-email-link-description-format')."
  5634. (setq fmt (or fmt org-email-link-description-format))
  5635. (let* ((p org-store-link-plist)
  5636. (to (plist-get p :toaddress))
  5637. (from (plist-get p :fromaddress))
  5638. (table
  5639. (list
  5640. (cons "%c" (plist-get p :fromto))
  5641. (cons "%F" (plist-get p :from))
  5642. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5643. (cons "%T" (plist-get p :to))
  5644. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5645. (cons "%s" (plist-get p :subject))
  5646. (cons "%m" (plist-get p :message-id)))))
  5647. (when (string-match "%c" fmt)
  5648. ;; Check if the user wrote this message
  5649. (if (and org-from-is-user-regexp from to
  5650. (save-match-data (string-match org-from-is-user-regexp from)))
  5651. (setq fmt (replace-match "to %t" t t fmt))
  5652. (setq fmt (replace-match "from %f" t t fmt))))
  5653. (org-replace-escapes fmt table)))
  5654. (defun org-make-org-heading-search-string (&optional string heading)
  5655. "Make search string for STRING or current headline."
  5656. (interactive)
  5657. (let ((s (or string (org-get-heading))))
  5658. (unless (and string (not heading))
  5659. ;; We are using a headline, clean up garbage in there.
  5660. (if (string-match org-todo-regexp s)
  5661. (setq s (replace-match "" t t s)))
  5662. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5663. (setq s (replace-match "" t t s)))
  5664. (setq s (org-trim s))
  5665. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5666. org-comment-string "\\)") s)
  5667. (setq s (replace-match "" t t s)))
  5668. (while (string-match org-ts-regexp s)
  5669. (setq s (replace-match "" t t s))))
  5670. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5671. (setq s (replace-match " " t t s)))
  5672. (or string (setq s (concat "*" s))) ; Add * for headlines
  5673. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5674. (defun org-make-link (&rest strings)
  5675. "Concatenate STRINGS."
  5676. (apply 'concat strings))
  5677. (defun org-make-link-string (link &optional description)
  5678. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5679. (unless (string-match "\\S-" link)
  5680. (error "Empty link"))
  5681. (when (stringp description)
  5682. ;; Remove brackets from the description, they are fatal.
  5683. (while (string-match "\\[" description)
  5684. (setq description (replace-match "{" t t description)))
  5685. (while (string-match "\\]" description)
  5686. (setq description (replace-match "}" t t description))))
  5687. (when (equal (org-link-escape link) description)
  5688. ;; No description needed, it is identical
  5689. (setq description nil))
  5690. (when (and (not description)
  5691. (not (equal link (org-link-escape link))))
  5692. (setq description (org-extract-attributes link)))
  5693. (concat "[[" (org-link-escape link) "]"
  5694. (if description (concat "[" description "]") "")
  5695. "]"))
  5696. (defconst org-link-escape-chars
  5697. '((?\ . "%20")
  5698. (?\[ . "%5B")
  5699. (?\] . "%5D")
  5700. (?\340 . "%E0") ; `a
  5701. (?\342 . "%E2") ; ^a
  5702. (?\347 . "%E7") ; ,c
  5703. (?\350 . "%E8") ; `e
  5704. (?\351 . "%E9") ; 'e
  5705. (?\352 . "%EA") ; ^e
  5706. (?\356 . "%EE") ; ^i
  5707. (?\364 . "%F4") ; ^o
  5708. (?\371 . "%F9") ; `u
  5709. (?\373 . "%FB") ; ^u
  5710. (?\; . "%3B")
  5711. (?? . "%3F")
  5712. (?= . "%3D")
  5713. (?+ . "%2B")
  5714. )
  5715. "Association list of escapes for some characters problematic in links.
  5716. This is the list that is used for internal purposes.")
  5717. (defconst org-link-escape-chars-browser
  5718. '((?\ . "%20")) ; 32 for the SPC char
  5719. "Association list of escapes for some characters problematic in links.
  5720. This is the list that is used before handing over to the browser.")
  5721. (defun org-link-escape (text &optional table)
  5722. "Escape characters in TEXT that are problematic for links."
  5723. (setq table (or table org-link-escape-chars))
  5724. (when text
  5725. (let ((re (mapconcat (lambda (x) (regexp-quote
  5726. (char-to-string (car x))))
  5727. table "\\|")))
  5728. (while (string-match re text)
  5729. (setq text
  5730. (replace-match
  5731. (cdr (assoc (string-to-char (match-string 0 text))
  5732. table))
  5733. t t text)))
  5734. text)))
  5735. (defun org-link-unescape (text &optional table)
  5736. "Reverse the action of `org-link-escape'."
  5737. (setq table (or table org-link-escape-chars))
  5738. (when text
  5739. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5740. table "\\|")))
  5741. (while (string-match re text)
  5742. (setq text
  5743. (replace-match
  5744. (char-to-string (car (rassoc (match-string 0 text) table)))
  5745. t t text)))
  5746. text)))
  5747. (defun org-xor (a b)
  5748. "Exclusive or."
  5749. (if a (not b) b))
  5750. (defun org-get-header (header)
  5751. "Find a header field in the current buffer."
  5752. (save-excursion
  5753. (goto-char (point-min))
  5754. (let ((case-fold-search t) s)
  5755. (cond
  5756. ((eq header 'from)
  5757. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5758. (setq s (match-string 1)))
  5759. (while (string-match "\"" s)
  5760. (setq s (replace-match "" t t s)))
  5761. (if (string-match "[<(].*" s)
  5762. (setq s (replace-match "" t t s))))
  5763. ((eq header 'message-id)
  5764. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5765. (setq s (match-string 1))))
  5766. ((eq header 'subject)
  5767. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5768. (setq s (match-string 1)))))
  5769. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5770. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5771. s)))
  5772. (defun org-fixup-message-id-for-http (s)
  5773. "Replace special characters in a message id, so it can be used in an http query."
  5774. (while (string-match "<" s)
  5775. (setq s (replace-match "%3C" t t s)))
  5776. (while (string-match ">" s)
  5777. (setq s (replace-match "%3E" t t s)))
  5778. (while (string-match "@" s)
  5779. (setq s (replace-match "%40" t t s)))
  5780. s)
  5781. ;;;###autoload
  5782. (defun org-insert-link-global ()
  5783. "Insert a link like Org-mode does.
  5784. This command can be called in any mode to insert a link in Org-mode syntax."
  5785. (interactive)
  5786. (org-load-modules-maybe)
  5787. (org-run-like-in-org-mode 'org-insert-link))
  5788. (defun org-insert-link (&optional complete-file link-location)
  5789. "Insert a link. At the prompt, enter the link.
  5790. Completion can be used to insert any of the link protocol prefixes like
  5791. http or ftp in use.
  5792. The history can be used to select a link previously stored with
  5793. `org-store-link'. When the empty string is entered (i.e. if you just
  5794. press RET at the prompt), the link defaults to the most recently
  5795. stored link. As SPC triggers completion in the minibuffer, you need to
  5796. use M-SPC or C-q SPC to force the insertion of a space character.
  5797. You will also be prompted for a description, and if one is given, it will
  5798. be displayed in the buffer instead of the link.
  5799. If there is already a link at point, this command will allow you to edit link
  5800. and description parts.
  5801. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5802. be selected using completion. The path to the file will be relative to the
  5803. current directory if the file is in the current directory or a subdirectory.
  5804. Otherwise, the link will be the absolute path as completed in the minibuffer
  5805. \(i.e. normally ~/path/to/file). You can configure this behavior using the
  5806. option `org-link-file-path-type'.
  5807. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5808. the current directory or below.
  5809. With three \\[universal-argument] prefixes, negate the meaning of
  5810. `org-keep-stored-link-after-insertion'.
  5811. If `org-make-link-description-function' is non-nil, this function will be
  5812. called with the link target, and the result will be the default
  5813. link description.
  5814. If the LINK-LOCATION parameter is non-nil, this value will be
  5815. used as the link location instead of reading one interactively."
  5816. (interactive "P")
  5817. (let* ((wcf (current-window-configuration))
  5818. (region (if (org-region-active-p)
  5819. (buffer-substring (region-beginning) (region-end))))
  5820. (remove (and region (list (region-beginning) (region-end))))
  5821. (desc region)
  5822. tmphist ; byte-compile incorrectly complains about this
  5823. (link link-location)
  5824. entry file)
  5825. (cond
  5826. (link-location) ; specified by arg, just use it.
  5827. ((org-in-regexp org-bracket-link-regexp 1)
  5828. ;; We do have a link at point, and we are going to edit it.
  5829. (setq remove (list (match-beginning 0) (match-end 0)))
  5830. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5831. (setq link (read-string "Link: "
  5832. (org-link-unescape
  5833. (org-match-string-no-properties 1)))))
  5834. ((or (org-in-regexp org-angle-link-re)
  5835. (org-in-regexp org-plain-link-re))
  5836. ;; Convert to bracket link
  5837. (setq remove (list (match-beginning 0) (match-end 0))
  5838. link (read-string "Link: "
  5839. (org-remove-angle-brackets (match-string 0)))))
  5840. ((member complete-file '((4) (16)))
  5841. ;; Completing read for file names.
  5842. (setq file (read-file-name "File: "))
  5843. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5844. (pwd1 (file-name-as-directory (abbreviate-file-name
  5845. (expand-file-name ".")))))
  5846. (cond
  5847. ((equal complete-file '(16))
  5848. (setq link (org-make-link
  5849. "file:"
  5850. (abbreviate-file-name (expand-file-name file)))))
  5851. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5852. (setq link (org-make-link "file:" (match-string 1 file))))
  5853. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5854. (expand-file-name file))
  5855. (setq link (org-make-link
  5856. "file:" (match-string 1 (expand-file-name file)))))
  5857. (t (setq link (org-make-link "file:" file))))))
  5858. (t
  5859. ;; Read link, with completion for stored links.
  5860. (with-output-to-temp-buffer "*Org Links*"
  5861. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5862. (when org-stored-links
  5863. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5864. (princ (mapconcat
  5865. (lambda (x)
  5866. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5867. (reverse org-stored-links) "\n"))))
  5868. (let ((cw (selected-window)))
  5869. (select-window (get-buffer-window "*Org Links*"))
  5870. (org-fit-window-to-buffer)
  5871. (setq truncate-lines t)
  5872. (select-window cw))
  5873. ;; Fake a link history, containing the stored links.
  5874. (setq tmphist (append (mapcar 'car org-stored-links)
  5875. org-insert-link-history))
  5876. (unwind-protect
  5877. (setq link (org-completing-read
  5878. "Link: "
  5879. (append
  5880. (mapcar (lambda (x) (list (concat (car x) ":")))
  5881. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5882. (mapcar (lambda (x) (list (concat x ":")))
  5883. org-link-types))
  5884. nil nil nil
  5885. 'tmphist
  5886. (or (car (car org-stored-links)))))
  5887. (set-window-configuration wcf)
  5888. (kill-buffer "*Org Links*"))
  5889. (setq entry (assoc link org-stored-links))
  5890. (or entry (push link org-insert-link-history))
  5891. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5892. (not org-keep-stored-link-after-insertion))
  5893. (setq org-stored-links (delq (assoc link org-stored-links)
  5894. org-stored-links)))
  5895. (setq desc (or desc (nth 1 entry)))))
  5896. (if (string-match org-plain-link-re link)
  5897. ;; URL-like link, normalize the use of angular brackets.
  5898. (setq link (org-make-link (org-remove-angle-brackets link))))
  5899. ;; Check if we are linking to the current file with a search option
  5900. ;; If yes, simplify the link by using only the search option.
  5901. (when (and buffer-file-name
  5902. (string-match "^file:\\(.+?\\)::\\([^>]+\\)" link))
  5903. (let* ((path (match-string 1 link))
  5904. (case-fold-search nil)
  5905. (search (match-string 2 link)))
  5906. (save-match-data
  5907. (if (equal (file-truename buffer-file-name) (file-truename path))
  5908. ;; We are linking to this same file, with a search option
  5909. (setq link search)))))
  5910. ;; Check if we can/should use a relative path. If yes, simplify the link
  5911. (when (string-match "^file:\\(.*\\)" link)
  5912. (let* ((path (match-string 1 link))
  5913. (origpath path)
  5914. (case-fold-search nil))
  5915. (cond
  5916. ((or (eq org-link-file-path-type 'absolute)
  5917. (equal complete-file '(16)))
  5918. (setq path (abbreviate-file-name (expand-file-name path))))
  5919. ((eq org-link-file-path-type 'noabbrev)
  5920. (setq path (expand-file-name path)))
  5921. ((eq org-link-file-path-type 'relative)
  5922. (setq path (file-relative-name path)))
  5923. (t
  5924. (save-match-data
  5925. (if (string-match (concat "^" (regexp-quote
  5926. (file-name-as-directory
  5927. (expand-file-name "."))))
  5928. (expand-file-name path))
  5929. ;; We are linking a file with relative path name.
  5930. (setq path (substring (expand-file-name path)
  5931. (match-end 0)))
  5932. (setq path (abbreviate-file-name (expand-file-name path)))))))
  5933. (setq link (concat "file:" path))
  5934. (if (equal desc origpath)
  5935. (setq desc path))))
  5936. (if org-make-link-description-function
  5937. (setq desc (funcall org-make-link-description-function link desc)))
  5938. (setq desc (read-string "Description: " desc))
  5939. (unless (string-match "\\S-" desc) (setq desc nil))
  5940. (if remove (apply 'delete-region remove))
  5941. (insert (org-make-link-string link desc))))
  5942. (defun org-completing-read (&rest args)
  5943. "Completing-read with SPACE being a normal character."
  5944. (let ((minibuffer-local-completion-map
  5945. (copy-keymap minibuffer-local-completion-map)))
  5946. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5947. (apply 'org-ido-completing-read args)))
  5948. (defun org-ido-completing-read (&rest args)
  5949. "Completing-read using `ido-mode' speedups if available"
  5950. (if (and org-completion-use-ido
  5951. (fboundp 'ido-completing-read)
  5952. (boundp 'ido-mode) ido-mode
  5953. (listp (second args)))
  5954. (apply 'ido-completing-read (concat (car args)) (cdr args))
  5955. (apply 'completing-read args)))
  5956. (defun org-extract-attributes (s)
  5957. "Extract the attributes cookie from a string and set as text property."
  5958. (let (a attr (start 0) key value)
  5959. (save-match-data
  5960. (when (string-match "{{\\([^}]+\\)}}$" s)
  5961. (setq a (match-string 1 s) s (substring s 0 (match-beginning 0)))
  5962. (while (string-match "\\([a-zA-Z]+\\)=\"\\([^\"]*\\)\"" a start)
  5963. (setq key (match-string 1 a) value (match-string 2 a)
  5964. start (match-end 0)
  5965. attr (plist-put attr (intern key) value))))
  5966. (org-add-props s nil 'org-attributes attr))
  5967. s))
  5968. (defun org-attributes-to-string (plist)
  5969. "Format a property list into an HTML attribute list."
  5970. (let ((s "") key value)
  5971. (while plist
  5972. (setq key (pop plist) value (pop plist))
  5973. (setq s (concat s " "(symbol-name key) "=\"" value "\"")))
  5974. s))
  5975. ;;; Opening/following a link
  5976. (defvar org-link-search-failed nil)
  5977. (defun org-next-link ()
  5978. "Move forward to the next link.
  5979. If the link is in hidden text, expose it."
  5980. (interactive)
  5981. (when (and org-link-search-failed (eq this-command last-command))
  5982. (goto-char (point-min))
  5983. (message "Link search wrapped back to beginning of buffer"))
  5984. (setq org-link-search-failed nil)
  5985. (let* ((pos (point))
  5986. (ct (org-context))
  5987. (a (assoc :link ct)))
  5988. (if a (goto-char (nth 2 a)))
  5989. (if (re-search-forward org-any-link-re nil t)
  5990. (progn
  5991. (goto-char (match-beginning 0))
  5992. (if (org-invisible-p) (org-show-context)))
  5993. (goto-char pos)
  5994. (setq org-link-search-failed t)
  5995. (error "No further link found"))))
  5996. (defun org-previous-link ()
  5997. "Move backward to the previous link.
  5998. If the link is in hidden text, expose it."
  5999. (interactive)
  6000. (when (and org-link-search-failed (eq this-command last-command))
  6001. (goto-char (point-max))
  6002. (message "Link search wrapped back to end of buffer"))
  6003. (setq org-link-search-failed nil)
  6004. (let* ((pos (point))
  6005. (ct (org-context))
  6006. (a (assoc :link ct)))
  6007. (if a (goto-char (nth 1 a)))
  6008. (if (re-search-backward org-any-link-re nil t)
  6009. (progn
  6010. (goto-char (match-beginning 0))
  6011. (if (org-invisible-p) (org-show-context)))
  6012. (goto-char pos)
  6013. (setq org-link-search-failed t)
  6014. (error "No further link found"))))
  6015. (defun org-translate-link (s)
  6016. "Translate a link string if a translation function has been defined."
  6017. (if (and org-link-translation-function
  6018. (fboundp org-link-translation-function)
  6019. (string-match "\\([a-zA-Z0-9]+\\):\\(.*\\)" s))
  6020. (progn
  6021. (setq s (funcall org-link-translation-function
  6022. (match-string 1) (match-string 2)))
  6023. (concat (car s) ":" (cdr s)))
  6024. s))
  6025. (defun org-translate-link-from-planner (type path)
  6026. "Translate a link from Emacs Planner syntax so that Org can follow it.
  6027. This is still an experimental function, your mileage may vary."
  6028. (cond
  6029. ((member type '("http" "https" "news" "ftp"))
  6030. ;; standard Internet links are the same.
  6031. nil)
  6032. ((and (equal type "irc") (string-match "^//" path))
  6033. ;; Planner has two / at the beginning of an irc link, we have 1.
  6034. ;; We should have zero, actually....
  6035. (setq path (substring path 1)))
  6036. ((and (equal type "lisp") (string-match "^/" path))
  6037. ;; Planner has a slash, we do not.
  6038. (setq type "elisp" path (substring path 1)))
  6039. ((string-match "^//\\(.?*\\)/\\(<.*>\\)$" path)
  6040. ;; A typical message link. Planner has the id after the fina slash,
  6041. ;; we separate it with a hash mark
  6042. (setq path (concat (match-string 1 path) "#"
  6043. (org-remove-angle-brackets (match-string 2 path)))))
  6044. )
  6045. (cons type path))
  6046. (defun org-find-file-at-mouse (ev)
  6047. "Open file link or URL at mouse."
  6048. (interactive "e")
  6049. (mouse-set-point ev)
  6050. (org-open-at-point 'in-emacs))
  6051. (defun org-open-at-mouse (ev)
  6052. "Open file link or URL at mouse."
  6053. (interactive "e")
  6054. (mouse-set-point ev)
  6055. (if (eq major-mode 'org-agenda-mode)
  6056. (org-agenda-copy-local-variable 'org-link-abbrev-alist-local))
  6057. (org-open-at-point))
  6058. (defvar org-window-config-before-follow-link nil
  6059. "The window configuration before following a link.
  6060. This is saved in case the need arises to restore it.")
  6061. (defvar org-open-link-marker (make-marker)
  6062. "Marker pointing to the location where `org-open-at-point; was called.")
  6063. ;;;###autoload
  6064. (defun org-open-at-point-global ()
  6065. "Follow a link like Org-mode does.
  6066. This command can be called in any mode to follow a link that has
  6067. Org-mode syntax."
  6068. (interactive)
  6069. (org-run-like-in-org-mode 'org-open-at-point))
  6070. ;;;###autoload
  6071. (defun org-open-link-from-string (s &optional arg)
  6072. "Open a link in the string S, as if it was in Org-mode."
  6073. (interactive "sLink: \nP")
  6074. (with-temp-buffer
  6075. (let ((org-inhibit-startup t))
  6076. (org-mode)
  6077. (insert s)
  6078. (goto-char (point-min))
  6079. (org-open-at-point arg))))
  6080. (defun org-open-at-point (&optional in-emacs)
  6081. "Open link at or after point.
  6082. If there is no link at point, this function will search forward up to
  6083. the end of the current subtree.
  6084. Normally, files will be opened by an appropriate application. If the
  6085. optional argument IN-EMACS is non-nil, Emacs will visit the file.
  6086. With a double prefix argument, try to open outside of Emacs, in the
  6087. application the system uses for this file type."
  6088. (interactive "P")
  6089. (org-load-modules-maybe)
  6090. (move-marker org-open-link-marker (point))
  6091. (setq org-window-config-before-follow-link (current-window-configuration))
  6092. (org-remove-occur-highlights nil nil t)
  6093. (if (org-at-timestamp-p t)
  6094. (org-follow-timestamp-link)
  6095. (let (type path link line search (pos (point)))
  6096. (catch 'match
  6097. (save-excursion
  6098. (skip-chars-forward "^]\n\r")
  6099. (when (org-in-regexp org-bracket-link-regexp)
  6100. (setq link (org-extract-attributes
  6101. (org-link-unescape (org-match-string-no-properties 1))))
  6102. (while (string-match " *\n *" link)
  6103. (setq link (replace-match " " t t link)))
  6104. (setq link (org-link-expand-abbrev link))
  6105. (cond
  6106. ((or (file-name-absolute-p link)
  6107. (string-match "^\\.\\.?/" link))
  6108. (setq type "file" path link))
  6109. ((string-match org-link-re-with-space3 link)
  6110. (setq type (match-string 1 link) path (match-string 2 link)))
  6111. (t (setq type "thisfile" path link)))
  6112. (throw 'match t)))
  6113. (when (get-text-property (point) 'org-linked-text)
  6114. (setq type "thisfile"
  6115. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6116. (1+ (point)) (point))
  6117. path (buffer-substring
  6118. (previous-single-property-change pos 'org-linked-text)
  6119. (next-single-property-change pos 'org-linked-text)))
  6120. (throw 'match t))
  6121. (save-excursion
  6122. (when (or (org-in-regexp org-angle-link-re)
  6123. (org-in-regexp org-plain-link-re))
  6124. (setq type (match-string 1) path (match-string 2))
  6125. (throw 'match t)))
  6126. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6127. (setq type "tree-match"
  6128. path (match-string 1))
  6129. (throw 'match t))
  6130. (save-excursion
  6131. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6132. (setq type "tags"
  6133. path (match-string 1))
  6134. (while (string-match ":" path)
  6135. (setq path (replace-match "+" t t path)))
  6136. (throw 'match t))))
  6137. (unless path
  6138. (error "No link found"))
  6139. ;; Remove any trailing spaces in path
  6140. (if (string-match " +\\'" path)
  6141. (setq path (replace-match "" t t path)))
  6142. (if (and org-link-translation-function
  6143. (fboundp org-link-translation-function))
  6144. ;; Check if we need to translate the link
  6145. (let ((tmp (funcall org-link-translation-function type path)))
  6146. (setq type (car tmp) path (cdr tmp))))
  6147. (cond
  6148. ((assoc type org-link-protocols)
  6149. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6150. ((equal type "mailto")
  6151. (let ((cmd (car org-link-mailto-program))
  6152. (args (cdr org-link-mailto-program)) args1
  6153. (address path) (subject "") a)
  6154. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6155. (setq address (match-string 1 path)
  6156. subject (org-link-escape (match-string 2 path))))
  6157. (while args
  6158. (cond
  6159. ((not (stringp (car args))) (push (pop args) args1))
  6160. (t (setq a (pop args))
  6161. (if (string-match "%a" a)
  6162. (setq a (replace-match address t t a)))
  6163. (if (string-match "%s" a)
  6164. (setq a (replace-match subject t t a)))
  6165. (push a args1))))
  6166. (apply cmd (nreverse args1))))
  6167. ((member type '("http" "https" "ftp" "news"))
  6168. (browse-url (concat type ":" (org-link-escape
  6169. path org-link-escape-chars-browser))))
  6170. ((member type '("message"))
  6171. (browse-url (concat type ":" path)))
  6172. ((string= type "tags")
  6173. (org-tags-view in-emacs path))
  6174. ((string= type "thisfile")
  6175. (if in-emacs
  6176. (switch-to-buffer-other-window
  6177. (org-get-buffer-for-internal-link (current-buffer)))
  6178. (org-mark-ring-push))
  6179. (let ((cmd `(org-link-search
  6180. ,path
  6181. ,(cond ((equal in-emacs '(4)) 'occur)
  6182. ((equal in-emacs '(16)) 'org-occur)
  6183. (t nil))
  6184. ,pos)))
  6185. (condition-case nil (eval cmd)
  6186. (error (progn (widen) (eval cmd))))))
  6187. ((string= type "tree-match")
  6188. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6189. ((string= type "file")
  6190. (if (string-match "::\\([0-9]+\\)\\'" path)
  6191. (setq line (string-to-number (match-string 1 path))
  6192. path (substring path 0 (match-beginning 0)))
  6193. (if (string-match "::\\(.+\\)\\'" path)
  6194. (setq search (match-string 1 path)
  6195. path (substring path 0 (match-beginning 0)))))
  6196. (if (string-match "[*?{]" (file-name-nondirectory path))
  6197. (dired path)
  6198. (org-open-file path in-emacs line search)))
  6199. ((string= type "news")
  6200. (require 'org-gnus)
  6201. (org-gnus-follow-link path))
  6202. ((string= type "shell")
  6203. (let ((cmd path))
  6204. (if (or (not org-confirm-shell-link-function)
  6205. (funcall org-confirm-shell-link-function
  6206. (format "Execute \"%s\" in shell? "
  6207. (org-add-props cmd nil
  6208. 'face 'org-warning))))
  6209. (progn
  6210. (message "Executing %s" cmd)
  6211. (shell-command cmd))
  6212. (error "Abort"))))
  6213. ((string= type "elisp")
  6214. (let ((cmd path))
  6215. (if (or (not org-confirm-elisp-link-function)
  6216. (funcall org-confirm-elisp-link-function
  6217. (format "Execute \"%s\" as elisp? "
  6218. (org-add-props cmd nil
  6219. 'face 'org-warning))))
  6220. (message "%s => %s" cmd
  6221. (if (equal (string-to-char cmd) ?\()
  6222. (eval (read cmd))
  6223. (call-interactively (read cmd))))
  6224. (error "Abort"))))
  6225. (t
  6226. (browse-url-at-point)))))
  6227. (move-marker org-open-link-marker nil)
  6228. (run-hook-with-args 'org-follow-link-hook))
  6229. ;;;; Time estimates
  6230. (defun org-get-effort (&optional pom)
  6231. "Get the effort estimate for the current entry."
  6232. (org-entry-get pom org-effort-property))
  6233. ;;; File search
  6234. (defvar org-create-file-search-functions nil
  6235. "List of functions to construct the right search string for a file link.
  6236. These functions are called in turn with point at the location to
  6237. which the link should point.
  6238. A function in the hook should first test if it would like to
  6239. handle this file type, for example by checking the major-mode or
  6240. the file extension. If it decides not to handle this file, it
  6241. should just return nil to give other functions a chance. If it
  6242. does handle the file, it must return the search string to be used
  6243. when following the link. The search string will be part of the
  6244. file link, given after a double colon, and `org-open-at-point'
  6245. will automatically search for it. If special measures must be
  6246. taken to make the search successful, another function should be
  6247. added to the companion hook `org-execute-file-search-functions',
  6248. which see.
  6249. A function in this hook may also use `setq' to set the variable
  6250. `description' to provide a suggestion for the descriptive text to
  6251. be used for this link when it gets inserted into an Org-mode
  6252. buffer with \\[org-insert-link].")
  6253. (defvar org-execute-file-search-functions nil
  6254. "List of functions to execute a file search triggered by a link.
  6255. Functions added to this hook must accept a single argument, the
  6256. search string that was part of the file link, the part after the
  6257. double colon. The function must first check if it would like to
  6258. handle this search, for example by checking the major-mode or the
  6259. file extension. If it decides not to handle this search, it
  6260. should just return nil to give other functions a chance. If it
  6261. does handle the search, it must return a non-nil value to keep
  6262. other functions from trying.
  6263. Each function can access the current prefix argument through the
  6264. variable `current-prefix-argument'. Note that a single prefix is
  6265. used to force opening a link in Emacs, so it may be good to only
  6266. use a numeric or double prefix to guide the search function.
  6267. In case this is needed, a function in this hook can also restore
  6268. the window configuration before `org-open-at-point' was called using:
  6269. (set-window-configuration org-window-config-before-follow-link)")
  6270. (defun org-link-search (s &optional type avoid-pos)
  6271. "Search for a link search option.
  6272. If S is surrounded by forward slashes, it is interpreted as a
  6273. regular expression. In org-mode files, this will create an `org-occur'
  6274. sparse tree. In ordinary files, `occur' will be used to list matches.
  6275. If the current buffer is in `dired-mode', grep will be used to search
  6276. in all files. If AVOID-POS is given, ignore matches near that position."
  6277. (let ((case-fold-search t)
  6278. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6279. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6280. (append '(("") (" ") ("\t") ("\n"))
  6281. org-emphasis-alist)
  6282. "\\|") "\\)"))
  6283. (pos (point))
  6284. (pre nil) (post nil)
  6285. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6286. (cond
  6287. ;; First check if there are any special
  6288. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6289. ;; Now try the builtin stuff
  6290. ((save-excursion
  6291. (goto-char (point-min))
  6292. (and
  6293. (re-search-forward
  6294. (concat "<<" (regexp-quote s0) ">>") nil t)
  6295. (setq type 'dedicated
  6296. pos (match-beginning 0))))
  6297. ;; There is an exact target for this
  6298. (goto-char pos))
  6299. ((string-match "^/\\(.*\\)/$" s)
  6300. ;; A regular expression
  6301. (cond
  6302. ((org-mode-p)
  6303. (org-occur (match-string 1 s)))
  6304. ;;((eq major-mode 'dired-mode)
  6305. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6306. (t (org-do-occur (match-string 1 s)))))
  6307. (t
  6308. ;; A normal search strings
  6309. (when (equal (string-to-char s) ?*)
  6310. ;; Anchor on headlines, post may include tags.
  6311. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6312. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6313. s (substring s 1)))
  6314. (remove-text-properties
  6315. 0 (length s)
  6316. '(face nil mouse-face nil keymap nil fontified nil) s)
  6317. ;; Make a series of regular expressions to find a match
  6318. (setq words (org-split-string s "[ \n\r\t]+")
  6319. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6320. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6321. "\\)" markers)
  6322. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6323. re2a (concat "[ \t\r\n]" re2a_)
  6324. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6325. re4 (concat "[^a-zA-Z_]" re4_)
  6326. re1 (concat pre re2 post)
  6327. re3 (concat pre (if pre re4_ re4) post)
  6328. re5 (concat pre ".*" re4)
  6329. re2 (concat pre re2)
  6330. re2a (concat pre (if pre re2a_ re2a))
  6331. re4 (concat pre (if pre re4_ re4))
  6332. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6333. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6334. re5 "\\)"
  6335. ))
  6336. (cond
  6337. ((eq type 'org-occur) (org-occur reall))
  6338. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6339. (t (goto-char (point-min))
  6340. (setq type 'fuzzy)
  6341. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6342. (org-search-not-self 1 re1 nil t)
  6343. (org-search-not-self 1 re2 nil t)
  6344. (org-search-not-self 1 re2a nil t)
  6345. (org-search-not-self 1 re3 nil t)
  6346. (org-search-not-self 1 re4 nil t)
  6347. (org-search-not-self 1 re5 nil t)
  6348. )
  6349. (goto-char (match-beginning 1))
  6350. (goto-char pos)
  6351. (error "No match")))))
  6352. (t
  6353. ;; Normal string-search
  6354. (goto-char (point-min))
  6355. (if (search-forward s nil t)
  6356. (goto-char (match-beginning 0))
  6357. (error "No match"))))
  6358. (and (org-mode-p) (org-show-context 'link-search))
  6359. type))
  6360. (defun org-search-not-self (group &rest args)
  6361. "Execute `re-search-forward', but only accept matches that do not
  6362. enclose the position of `org-open-link-marker'."
  6363. (let ((m org-open-link-marker))
  6364. (catch 'exit
  6365. (while (apply 're-search-forward args)
  6366. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6367. (goto-char (match-end group))
  6368. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6369. (> (match-beginning 0) (marker-position m))
  6370. (< (match-end 0) (marker-position m)))
  6371. (save-match-data
  6372. (or (not (org-in-regexp
  6373. org-bracket-link-analytic-regexp 1))
  6374. (not (match-end 4)) ; no description
  6375. (and (<= (match-beginning 4) (point))
  6376. (>= (match-end 4) (point))))))
  6377. (throw 'exit (point))))))))
  6378. (defun org-get-buffer-for-internal-link (buffer)
  6379. "Return a buffer to be used for displaying the link target of internal links."
  6380. (cond
  6381. ((not org-display-internal-link-with-indirect-buffer)
  6382. buffer)
  6383. ((string-match "(Clone)$" (buffer-name buffer))
  6384. (message "Buffer is already a clone, not making another one")
  6385. ;; we also do not modify visibility in this case
  6386. buffer)
  6387. (t ; make a new indirect buffer for displaying the link
  6388. (let* ((bn (buffer-name buffer))
  6389. (ibn (concat bn "(Clone)"))
  6390. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6391. (with-current-buffer ib (org-overview))
  6392. ib))))
  6393. (defun org-do-occur (regexp &optional cleanup)
  6394. "Call the Emacs command `occur'.
  6395. If CLEANUP is non-nil, remove the printout of the regular expression
  6396. in the *Occur* buffer. This is useful if the regex is long and not useful
  6397. to read."
  6398. (occur regexp)
  6399. (when cleanup
  6400. (let ((cwin (selected-window)) win beg end)
  6401. (when (setq win (get-buffer-window "*Occur*"))
  6402. (select-window win))
  6403. (goto-char (point-min))
  6404. (when (re-search-forward "match[a-z]+" nil t)
  6405. (setq beg (match-end 0))
  6406. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6407. (setq end (1- (match-beginning 0)))))
  6408. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6409. (goto-char (point-min))
  6410. (select-window cwin))))
  6411. ;;; The mark ring for links jumps
  6412. (defvar org-mark-ring nil
  6413. "Mark ring for positions before jumps in Org-mode.")
  6414. (defvar org-mark-ring-last-goto nil
  6415. "Last position in the mark ring used to go back.")
  6416. ;; Fill and close the ring
  6417. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6418. (loop for i from 1 to org-mark-ring-length do
  6419. (push (make-marker) org-mark-ring))
  6420. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6421. org-mark-ring)
  6422. (defun org-mark-ring-push (&optional pos buffer)
  6423. "Put the current position or POS into the mark ring and rotate it."
  6424. (interactive)
  6425. (setq pos (or pos (point)))
  6426. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6427. (move-marker (car org-mark-ring)
  6428. (or pos (point))
  6429. (or buffer (current-buffer)))
  6430. (message "%s"
  6431. (substitute-command-keys
  6432. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6433. (defun org-mark-ring-goto (&optional n)
  6434. "Jump to the previous position in the mark ring.
  6435. With prefix arg N, jump back that many stored positions. When
  6436. called several times in succession, walk through the entire ring.
  6437. Org-mode commands jumping to a different position in the current file,
  6438. or to another Org-mode file, automatically push the old position
  6439. onto the ring."
  6440. (interactive "p")
  6441. (let (p m)
  6442. (if (eq last-command this-command)
  6443. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6444. (setq p org-mark-ring))
  6445. (setq org-mark-ring-last-goto p)
  6446. (setq m (car p))
  6447. (switch-to-buffer (marker-buffer m))
  6448. (goto-char m)
  6449. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6450. (defun org-remove-angle-brackets (s)
  6451. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6452. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6453. s)
  6454. (defun org-add-angle-brackets (s)
  6455. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6456. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6457. s)
  6458. (defun org-remove-double-quotes (s)
  6459. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6460. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6461. s)
  6462. ;;; Following specific links
  6463. (defun org-follow-timestamp-link ()
  6464. (cond
  6465. ((org-at-date-range-p t)
  6466. (let ((org-agenda-start-on-weekday)
  6467. (t1 (match-string 1))
  6468. (t2 (match-string 2)))
  6469. (setq t1 (time-to-days (org-time-string-to-time t1))
  6470. t2 (time-to-days (org-time-string-to-time t2)))
  6471. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6472. ((org-at-timestamp-p t)
  6473. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6474. (substring (match-string 1) 0 10)))
  6475. 1))
  6476. (t (error "This should not happen"))))
  6477. ;;; Following file links
  6478. (defvar org-wait nil)
  6479. (defun org-open-file (path &optional in-emacs line search)
  6480. "Open the file at PATH.
  6481. First, this expands any special file name abbreviations. Then the
  6482. configuration variable `org-file-apps' is checked if it contains an
  6483. entry for this file type, and if yes, the corresponding command is launched.
  6484. If no application is found, Emacs simply visits the file.
  6485. With optional prefix argument IN-EMACS, Emacs will visit the file.
  6486. With a double C-c C-u prefix arg, Org tries to avoid opening in Emacs
  6487. and o use an external application to visit the file.
  6488. Optional LINE specifies a line to go to, optional SEARCH a string to
  6489. search for. If LINE or SEARCH is given, the file will always be
  6490. opened in Emacs.
  6491. If the file does not exist, an error is thrown."
  6492. (setq in-emacs (or in-emacs line search))
  6493. (let* ((file (if (equal path "")
  6494. buffer-file-name
  6495. (substitute-in-file-name (expand-file-name path))))
  6496. (apps (append org-file-apps (org-default-apps)))
  6497. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6498. (dirp (if remp nil (file-directory-p file)))
  6499. (file (if (and dirp org-open-directory-means-index-dot-org)
  6500. (concat (file-name-as-directory file) "index.org")
  6501. file))
  6502. (a-m-a-p (assq 'auto-mode apps))
  6503. (dfile (downcase file))
  6504. (old-buffer (current-buffer))
  6505. (old-pos (point))
  6506. (old-mode major-mode)
  6507. ext cmd)
  6508. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6509. (setq ext (match-string 1 dfile))
  6510. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6511. (setq ext (match-string 1 dfile))))
  6512. (cond
  6513. ((equal in-emacs '(16))
  6514. (setq cmd (cdr (assoc 'system apps))))
  6515. (in-emacs (setq cmd 'emacs))
  6516. (t
  6517. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6518. (and dirp (cdr (assoc 'directory apps)))
  6519. (assoc-default dfile (org-apps-regexp-alist apps a-m-a-p)
  6520. 'string-match)
  6521. (cdr (assoc ext apps))
  6522. (cdr (assoc t apps))))))
  6523. (when (eq cmd 'system)
  6524. (setq cmd (cdr (assoc 'system apps))))
  6525. (when (eq cmd 'default)
  6526. (setq cmd (cdr (assoc t apps))))
  6527. (when (eq cmd 'mailcap)
  6528. (require 'mailcap)
  6529. (mailcap-parse-mailcaps)
  6530. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6531. (command (mailcap-mime-info mime-type)))
  6532. (if (stringp command)
  6533. (setq cmd command)
  6534. (setq cmd 'emacs))))
  6535. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6536. (not (file-exists-p file))
  6537. (not org-open-non-existing-files))
  6538. (error "No such file: %s" file))
  6539. (cond
  6540. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6541. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6542. (while (string-match "['\"]%s['\"]" cmd)
  6543. (setq cmd (replace-match "%s" t t cmd)))
  6544. (while (string-match "%s" cmd)
  6545. (setq cmd (replace-match
  6546. (save-match-data
  6547. (shell-quote-argument
  6548. (convert-standard-filename file)))
  6549. t t cmd)))
  6550. (save-window-excursion
  6551. (start-process-shell-command cmd nil cmd)
  6552. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6553. ))
  6554. ((or (stringp cmd)
  6555. (eq cmd 'emacs))
  6556. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6557. (widen)
  6558. (if line (goto-line line)
  6559. (if search (org-link-search search))))
  6560. ((consp cmd)
  6561. (let ((file (convert-standard-filename file)))
  6562. (eval cmd)))
  6563. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6564. (and (org-mode-p) (eq old-mode 'org-mode)
  6565. (or (not (equal old-buffer (current-buffer)))
  6566. (not (equal old-pos (point))))
  6567. (org-mark-ring-push old-pos old-buffer))))
  6568. (defun org-default-apps ()
  6569. "Return the default applications for this operating system."
  6570. (cond
  6571. ((eq system-type 'darwin)
  6572. org-file-apps-defaults-macosx)
  6573. ((eq system-type 'windows-nt)
  6574. org-file-apps-defaults-windowsnt)
  6575. (t org-file-apps-defaults-gnu)))
  6576. (defun org-apps-regexp-alist (list &optional add-auto-mode)
  6577. "Convert extensions to regular expressions in the cars of LIST.
  6578. Also, weed out any non-string entries, because the return value is used
  6579. only for regexp matching.
  6580. When ADD-AUTO-MODE is set, make all matches in `auto-mode-alist'
  6581. point to the symbol `emacs', indicating that the file should
  6582. be opened in Emacs."
  6583. (append
  6584. (delq nil
  6585. (mapcar (lambda (x)
  6586. (if (not (stringp (car x)))
  6587. nil
  6588. (if (string-match "\\W" (car x))
  6589. x
  6590. (cons (concat "\\." (car x) "\\'") (cdr x)))))
  6591. list))
  6592. (if add-auto-mode
  6593. (mapcar (lambda (x) (cons (car x) 'emacs)) auto-mode-alist))))
  6594. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6595. (defun org-file-remote-p (file)
  6596. "Test whether FILE specifies a location on a remote system.
  6597. Return non-nil if the location is indeed remote.
  6598. For example, the filename \"/user@host:/foo\" specifies a location
  6599. on the system \"/user@host:\"."
  6600. (cond ((fboundp 'file-remote-p)
  6601. (file-remote-p file))
  6602. ((fboundp 'tramp-handle-file-remote-p)
  6603. (tramp-handle-file-remote-p file))
  6604. ((and (boundp 'ange-ftp-name-format)
  6605. (string-match (car ange-ftp-name-format) file))
  6606. t)
  6607. (t nil)))
  6608. ;;;; Refiling
  6609. (defun org-get-org-file ()
  6610. "Read a filename, with default directory `org-directory'."
  6611. (let ((default (or org-default-notes-file remember-data-file)))
  6612. (read-file-name (format "File name [%s]: " default)
  6613. (file-name-as-directory org-directory)
  6614. default)))
  6615. (defun org-notes-order-reversed-p ()
  6616. "Check if the current file should receive notes in reversed order."
  6617. (cond
  6618. ((not org-reverse-note-order) nil)
  6619. ((eq t org-reverse-note-order) t)
  6620. ((not (listp org-reverse-note-order)) nil)
  6621. (t (catch 'exit
  6622. (let ((all org-reverse-note-order)
  6623. entry)
  6624. (while (setq entry (pop all))
  6625. (if (string-match (car entry) buffer-file-name)
  6626. (throw 'exit (cdr entry))))
  6627. nil)))))
  6628. (defvar org-refile-target-table nil
  6629. "The list of refile targets, created by `org-refile'.")
  6630. (defvar org-agenda-new-buffers nil
  6631. "Buffers created to visit agenda files.")
  6632. (defun org-get-refile-targets (&optional default-buffer)
  6633. "Produce a table with refile targets."
  6634. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6635. targets txt re files f desc descre)
  6636. (with-current-buffer (or default-buffer (current-buffer))
  6637. (while (setq entry (pop entries))
  6638. (setq files (car entry) desc (cdr entry))
  6639. (cond
  6640. ((null files) (setq files (list (current-buffer))))
  6641. ((eq files 'org-agenda-files)
  6642. (setq files (org-agenda-files 'unrestricted)))
  6643. ((and (symbolp files) (fboundp files))
  6644. (setq files (funcall files)))
  6645. ((and (symbolp files) (boundp files))
  6646. (setq files (symbol-value files))))
  6647. (if (stringp files) (setq files (list files)))
  6648. (cond
  6649. ((eq (car desc) :tag)
  6650. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6651. ((eq (car desc) :todo)
  6652. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6653. ((eq (car desc) :regexp)
  6654. (setq descre (cdr desc)))
  6655. ((eq (car desc) :level)
  6656. (setq descre (concat "^\\*\\{" (number-to-string
  6657. (if org-odd-levels-only
  6658. (1- (* 2 (cdr desc)))
  6659. (cdr desc)))
  6660. "\\}[ \t]")))
  6661. ((eq (car desc) :maxlevel)
  6662. (setq descre (concat "^\\*\\{1," (number-to-string
  6663. (if org-odd-levels-only
  6664. (1- (* 2 (cdr desc)))
  6665. (cdr desc)))
  6666. "\\}[ \t]")))
  6667. (t (error "Bad refiling target description %s" desc)))
  6668. (while (setq f (pop files))
  6669. (save-excursion
  6670. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6671. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6672. (save-excursion
  6673. (save-restriction
  6674. (widen)
  6675. (goto-char (point-min))
  6676. (while (re-search-forward descre nil t)
  6677. (goto-char (point-at-bol))
  6678. (when (looking-at org-complex-heading-regexp)
  6679. (setq txt (org-link-display-format (match-string 4))
  6680. re (concat "^" (regexp-quote
  6681. (buffer-substring (match-beginning 1)
  6682. (match-end 4)))))
  6683. (if (match-end 5) (setq re (concat re "[ \t]+"
  6684. (regexp-quote
  6685. (match-string 5)))))
  6686. (setq re (concat re "[ \t]*$"))
  6687. (when org-refile-use-outline-path
  6688. (setq txt (mapconcat 'org-protect-slash
  6689. (append
  6690. (if (eq org-refile-use-outline-path 'file)
  6691. (list (file-name-nondirectory
  6692. (buffer-file-name (buffer-base-buffer))))
  6693. (if (eq org-refile-use-outline-path 'full-file-path)
  6694. (list (buffer-file-name (buffer-base-buffer)))))
  6695. (org-get-outline-path)
  6696. (list txt))
  6697. "/")))
  6698. (push (list txt f re (point)) targets))
  6699. (goto-char (point-at-eol))))))))
  6700. (nreverse targets))))
  6701. (defun org-protect-slash (s)
  6702. (while (string-match "/" s)
  6703. (setq s (replace-match "\\" t t s)))
  6704. s)
  6705. (defun org-get-outline-path ()
  6706. "Return the outline path to the current entry, as a list."
  6707. (let (rtn)
  6708. (save-excursion
  6709. (while (org-up-heading-safe)
  6710. (when (looking-at org-complex-heading-regexp)
  6711. (push (org-match-string-no-properties 4) rtn)))
  6712. rtn)))
  6713. (defvar org-refile-history nil
  6714. "History for refiling operations.")
  6715. (defun org-refile (&optional goto default-buffer)
  6716. "Move the entry at point to another heading.
  6717. The list of target headings is compiled using the information in
  6718. `org-refile-targets', which see. This list is created before each use
  6719. and will therefore always be up-to-date.
  6720. At the target location, the entry is filed as a subitem of the target heading.
  6721. Depending on `org-reverse-note-order', the new subitem will either be the
  6722. first or the last subitem.
  6723. If there is an active region, all entries in that region will be moved.
  6724. However, the region must fulfil the requirement that the first heading
  6725. is the first one sets the top-level of the moved text - at most siblings
  6726. below it are allowed.
  6727. With prefix arg GOTO, the command will only visit the target location,
  6728. not actually move anything.
  6729. With a double prefix `C-u C-u', go to the location where the last refiling
  6730. operation has put the subtree."
  6731. (interactive "P")
  6732. (let* ((cbuf (current-buffer))
  6733. (regionp (org-region-active-p))
  6734. (region-start (and regionp (region-beginning)))
  6735. (region-end (and regionp (region-end)))
  6736. (region-length (and regionp (- region-end region-start)))
  6737. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6738. pos it nbuf file re level reversed)
  6739. (when regionp (goto-char region-start)
  6740. (unless (org-kill-is-subtree-p
  6741. (buffer-substring region-start region-end))
  6742. (error "The region is not a (sequence of) subtree(s)")))
  6743. (if (equal goto '(16))
  6744. (org-refile-goto-last-stored)
  6745. (when (setq it (org-refile-get-location
  6746. (if goto "Goto: " "Refile to: ") default-buffer))
  6747. (setq file (nth 1 it)
  6748. re (nth 2 it)
  6749. pos (nth 3 it))
  6750. (setq nbuf (or (find-buffer-visiting file)
  6751. (find-file-noselect file)))
  6752. (if goto
  6753. (progn
  6754. (switch-to-buffer nbuf)
  6755. (goto-char pos)
  6756. (org-show-context 'org-goto))
  6757. (if regionp
  6758. (progn
  6759. (kill-new (buffer-substring region-start region-end))
  6760. (org-save-markers-in-region region-start region-end))
  6761. (org-copy-subtree 1 nil t))
  6762. (save-excursion
  6763. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6764. (find-file-noselect file))))
  6765. (setq reversed (org-notes-order-reversed-p))
  6766. (save-excursion
  6767. (save-restriction
  6768. (widen)
  6769. (goto-char pos)
  6770. (looking-at outline-regexp)
  6771. (setq level (org-get-valid-level (funcall outline-level) 1))
  6772. (goto-char
  6773. (if reversed
  6774. (or (outline-next-heading) (point-max))
  6775. (or (save-excursion (outline-get-next-sibling))
  6776. (org-end-of-subtree t t)
  6777. (point-max))))
  6778. (if (not (bolp)) (newline))
  6779. (bookmark-set "org-refile-last-stored")
  6780. (org-paste-subtree level))))
  6781. (if regionp
  6782. (delete-region (point) (+ (point) region-length))
  6783. (org-cut-subtree))
  6784. (setq org-markers-to-move nil)
  6785. (message "Refiled to \"%s\"" (car it)))))))
  6786. (defun org-refile-goto-last-stored ()
  6787. "Go to the location where the last refile was stored."
  6788. (interactive)
  6789. (bookmark-jump "org-refile-last-stored")
  6790. (message "This is the location of the last refile"))
  6791. (defun org-refile-get-location (&optional prompt default-buffer)
  6792. "Prompt the user for a refile location, using PROMPT."
  6793. (let ((org-refile-targets org-refile-targets)
  6794. (org-refile-use-outline-path org-refile-use-outline-path))
  6795. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6796. (unless org-refile-target-table
  6797. (error "No refile targets"))
  6798. (let* ((cbuf (current-buffer))
  6799. (cfunc (if (and org-refile-use-outline-path
  6800. org-outline-path-complete-in-steps)
  6801. 'org-olpath-completing-read
  6802. 'org-ido-completing-read))
  6803. (extra (if org-refile-use-outline-path "/" ""))
  6804. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6805. (fname (and filename (file-truename filename)))
  6806. (tbl (mapcar
  6807. (lambda (x)
  6808. (if (not (equal fname (file-truename (nth 1 x))))
  6809. (cons (concat (car x) extra " ("
  6810. (file-name-nondirectory (nth 1 x)) ")")
  6811. (cdr x))
  6812. (cons (concat (car x) extra) (cdr x))))
  6813. org-refile-target-table))
  6814. (completion-ignore-case t))
  6815. (assoc (funcall cfunc prompt tbl nil t nil 'org-refile-history)
  6816. tbl)))
  6817. (defun org-olpath-completing-read (prompt collection &rest args)
  6818. "Read an outline path like a file name."
  6819. (let ((thetable collection))
  6820. (apply
  6821. 'org-ido-completing-read prompt
  6822. (lambda (string predicate &optional flag)
  6823. (let (rtn r s f (l (length string)))
  6824. (cond
  6825. ((eq flag nil)
  6826. ;; try completion
  6827. (try-completion string thetable))
  6828. ((eq flag t)
  6829. ;; all-completions
  6830. (setq rtn (all-completions string thetable predicate))
  6831. (mapcar
  6832. (lambda (x)
  6833. (setq r (substring x l))
  6834. (if (string-match " ([^)]*)$" x)
  6835. (setq f (match-string 0 x))
  6836. (setq f ""))
  6837. (if (string-match "/" r)
  6838. (concat string (substring r 0 (match-end 0)) f)
  6839. x))
  6840. rtn))
  6841. ((eq flag 'lambda)
  6842. ;; exact match?
  6843. (assoc string thetable)))
  6844. ))
  6845. args)))
  6846. ;;;; Dynamic blocks
  6847. (defun org-find-dblock (name)
  6848. "Find the first dynamic block with name NAME in the buffer.
  6849. If not found, stay at current position and return nil."
  6850. (let (pos)
  6851. (save-excursion
  6852. (goto-char (point-min))
  6853. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6854. nil t)
  6855. (match-beginning 0))))
  6856. (if pos (goto-char pos))
  6857. pos))
  6858. (defconst org-dblock-start-re
  6859. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6860. "Matches the startline of a dynamic block, with parameters.")
  6861. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6862. "Matches the end of a dyhamic block.")
  6863. (defun org-create-dblock (plist)
  6864. "Create a dynamic block section, with parameters taken from PLIST.
  6865. PLIST must containe a :name entry which is used as name of the block."
  6866. (unless (bolp) (newline))
  6867. (let ((name (plist-get plist :name)))
  6868. (insert "#+BEGIN: " name)
  6869. (while plist
  6870. (if (eq (car plist) :name)
  6871. (setq plist (cddr plist))
  6872. (insert " " (prin1-to-string (pop plist)))))
  6873. (insert "\n\n#+END:\n")
  6874. (beginning-of-line -2)))
  6875. (defun org-prepare-dblock ()
  6876. "Prepare dynamic block for refresh.
  6877. This empties the block, puts the cursor at the insert position and returns
  6878. the property list including an extra property :name with the block name."
  6879. (unless (looking-at org-dblock-start-re)
  6880. (error "Not at a dynamic block"))
  6881. (let* ((begdel (1+ (match-end 0)))
  6882. (name (org-no-properties (match-string 1)))
  6883. (params (append (list :name name)
  6884. (read (concat "(" (match-string 3) ")")))))
  6885. (unless (re-search-forward org-dblock-end-re nil t)
  6886. (error "Dynamic block not terminated"))
  6887. (setq params
  6888. (append params
  6889. (list :content (buffer-substring
  6890. begdel (match-beginning 0)))))
  6891. (delete-region begdel (match-beginning 0))
  6892. (goto-char begdel)
  6893. (open-line 1)
  6894. params))
  6895. (defun org-map-dblocks (&optional command)
  6896. "Apply COMMAND to all dynamic blocks in the current buffer.
  6897. If COMMAND is not given, use `org-update-dblock'."
  6898. (let ((cmd (or command 'org-update-dblock))
  6899. pos)
  6900. (save-excursion
  6901. (goto-char (point-min))
  6902. (while (re-search-forward org-dblock-start-re nil t)
  6903. (goto-char (setq pos (match-beginning 0)))
  6904. (condition-case nil
  6905. (funcall cmd)
  6906. (error (message "Error during update of dynamic block")))
  6907. (goto-char pos)
  6908. (unless (re-search-forward org-dblock-end-re nil t)
  6909. (error "Dynamic block not terminated"))))))
  6910. (defun org-dblock-update (&optional arg)
  6911. "User command for updating dynamic blocks.
  6912. Update the dynamic block at point. With prefix ARG, update all dynamic
  6913. blocks in the buffer."
  6914. (interactive "P")
  6915. (if arg
  6916. (org-update-all-dblocks)
  6917. (or (looking-at org-dblock-start-re)
  6918. (org-beginning-of-dblock))
  6919. (org-update-dblock)))
  6920. (defun org-update-dblock ()
  6921. "Update the dynamic block at point
  6922. This means to empty the block, parse for parameters and then call
  6923. the correct writing function."
  6924. (save-window-excursion
  6925. (let* ((pos (point))
  6926. (line (org-current-line))
  6927. (params (org-prepare-dblock))
  6928. (name (plist-get params :name))
  6929. (cmd (intern (concat "org-dblock-write:" name))))
  6930. (message "Updating dynamic block `%s' at line %d..." name line)
  6931. (funcall cmd params)
  6932. (message "Updating dynamic block `%s' at line %d...done" name line)
  6933. (goto-char pos))))
  6934. (defun org-beginning-of-dblock ()
  6935. "Find the beginning of the dynamic block at point.
  6936. Error if there is no scuh block at point."
  6937. (let ((pos (point))
  6938. beg)
  6939. (end-of-line 1)
  6940. (if (and (re-search-backward org-dblock-start-re nil t)
  6941. (setq beg (match-beginning 0))
  6942. (re-search-forward org-dblock-end-re nil t)
  6943. (> (match-end 0) pos))
  6944. (goto-char beg)
  6945. (goto-char pos)
  6946. (error "Not in a dynamic block"))))
  6947. (defun org-update-all-dblocks ()
  6948. "Update all dynamic blocks in the buffer.
  6949. This function can be used in a hook."
  6950. (when (org-mode-p)
  6951. (org-map-dblocks 'org-update-dblock)))
  6952. ;;;; Completion
  6953. (defconst org-additional-option-like-keywords
  6954. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6955. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6956. "BEGIN_EXAMPLE" "END_EXAMPLE"
  6957. "BEGIN_QUOTE" "END_QUOTE"
  6958. "BEGIN_VERSE" "END_VERSE"
  6959. "BEGIN_SRC" "END_SRC"))
  6960. (defcustom org-structure-template-alist
  6961. '(
  6962. ("s" "#+begin_src ?\n\n#+end_src"
  6963. "<src lang=\"?\">\n\n</src>")
  6964. ("e" "#+begin_example\n?\n#+end_example"
  6965. "<example>\n?\n</example>")
  6966. ("q" "#+begin_quote\n?\n#+end_quote"
  6967. "<quote>\n?\n</quote>")
  6968. ("v" "#+begin_verse\n?\n#+end_verse"
  6969. "<verse>\n?\n/verse>")
  6970. ("l" "#+begin_latex\n?\n#+end_latex"
  6971. "<literal style=\"latex\">\n?\n</literal>")
  6972. ("L" "#+latex: "
  6973. "<literal style=\"latex\">?</literal>")
  6974. ("h" "#+begin_html\n?\n#+end_html"
  6975. "<literal style=\"html\">\n?\n</literal>")
  6976. ("H" "#+html: "
  6977. "<literal style=\"html\">?</literal>")
  6978. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6979. ("A" "#+ascii: ")
  6980. ("i" "#+include %file ?"
  6981. "<include file=%file markup=\"?\">")
  6982. )
  6983. "Structure completion elements.
  6984. This is a list of abbreviation keys and values. The value gets inserted
  6985. it you type @samp{.} followed by the key and then the completion key,
  6986. usually `M-TAB'. %file will be replaced by a file name after prompting
  6987. for the file uning completion.
  6988. There are two templates for each key, the first uses the original Org syntax,
  6989. the second uses Emacs Muse-like syntax tags. These Muse-like tags become
  6990. the default when the /org-mtags.el/ module has been loaded. See also the
  6991. variable `org-mtags-prefer-muse-templates'.
  6992. This is an experimental feature, it is undecided if it is going to stay in."
  6993. :group 'org-completion
  6994. :type '(repeat
  6995. (string :tag "Key")
  6996. (string :tag "Template")
  6997. (string :tag "Muse Template")))
  6998. (defun org-try-structure-completion ()
  6999. "Try to complete a structure template before point.
  7000. This looks for strings like \"<e\" on an otherwise empty line and
  7001. expands them."
  7002. (let ((l (buffer-substring (point-at-bol) (point)))
  7003. a)
  7004. (when (and (looking-at "[ \t]*$")
  7005. (string-match "^[ \t]*<\\([a-z]+\\)$"l)
  7006. (setq a (assoc (match-string 1 l) org-structure-template-alist)))
  7007. (org-complete-expand-structure-template (+ -1 (point-at-bol)
  7008. (match-beginning 1)) a)
  7009. t)))
  7010. (defun org-complete-expand-structure-template (start cell)
  7011. "Expand a structure template."
  7012. (let* ((musep (org-bound-and-true-p org-mtags-prefer-muse-templates))
  7013. (rpl (nth (if musep 2 1) cell)))
  7014. (delete-region start (point))
  7015. (when (string-match "\\`#\\+" rpl)
  7016. (cond
  7017. ((bolp))
  7018. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  7019. (delete-region (point-at-bol) (point)))
  7020. (t (newline))))
  7021. (setq start (point))
  7022. (if (string-match "%file" rpl)
  7023. (setq rpl (replace-match
  7024. (concat
  7025. "\""
  7026. (save-match-data
  7027. (abbreviate-file-name (read-file-name "Include file: ")))
  7028. "\"")
  7029. t t rpl)))
  7030. (insert rpl)
  7031. (if (re-search-backward "\\?" start t) (delete-char 1))))
  7032. (defun org-complete (&optional arg)
  7033. "Perform completion on word at point.
  7034. At the beginning of a headline, this completes TODO keywords as given in
  7035. `org-todo-keywords'.
  7036. If the current word is preceded by a backslash, completes the TeX symbols
  7037. that are supported for HTML support.
  7038. If the current word is preceded by \"#+\", completes special words for
  7039. setting file options.
  7040. In the line after \"#+STARTUP:, complete valid keywords.\"
  7041. At all other locations, this simply calls the value of
  7042. `org-completion-fallback-command'."
  7043. (interactive "P")
  7044. (org-without-partial-completion
  7045. (catch 'exit
  7046. (let* ((a nil)
  7047. (end (point))
  7048. (beg1 (save-excursion
  7049. (skip-chars-backward (org-re "[:alnum:]_@"))
  7050. (point)))
  7051. (beg (save-excursion
  7052. (skip-chars-backward "a-zA-Z0-9_:$")
  7053. (point)))
  7054. (confirm (lambda (x) (stringp (car x))))
  7055. (searchhead (equal (char-before beg) ?*))
  7056. (struct
  7057. (when (and (member (char-before beg1) '(?. ?<))
  7058. (setq a (assoc (buffer-substring beg1 (point))
  7059. org-structure-template-alist)))
  7060. (org-complete-expand-structure-template (1- beg1) a)
  7061. (throw 'exit t)))
  7062. (tag (and (equal (char-before beg1) ?:)
  7063. (equal (char-after (point-at-bol)) ?*)))
  7064. (prop (and (equal (char-before beg1) ?:)
  7065. (not (equal (char-after (point-at-bol)) ?*))))
  7066. (texp (equal (char-before beg) ?\\))
  7067. (link (equal (char-before beg) ?\[))
  7068. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  7069. beg)
  7070. "#+"))
  7071. (startup (string-match "^#\\+STARTUP:.*"
  7072. (buffer-substring (point-at-bol) (point))))
  7073. (completion-ignore-case opt)
  7074. (type nil)
  7075. (tbl nil)
  7076. (table (cond
  7077. (opt
  7078. (setq type :opt)
  7079. (require 'org-exp)
  7080. (append
  7081. (mapcar
  7082. (lambda (x)
  7083. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  7084. (cons (match-string 2 x) (match-string 1 x)))
  7085. (org-split-string (org-get-current-options) "\n"))
  7086. (mapcar 'list org-additional-option-like-keywords)))
  7087. (startup
  7088. (setq type :startup)
  7089. org-startup-options)
  7090. (link (append org-link-abbrev-alist-local
  7091. org-link-abbrev-alist))
  7092. (texp
  7093. (setq type :tex)
  7094. org-html-entities)
  7095. ((string-match "\\`\\*+[ \t]+\\'"
  7096. (buffer-substring (point-at-bol) beg))
  7097. (setq type :todo)
  7098. (mapcar 'list org-todo-keywords-1))
  7099. (searchhead
  7100. (setq type :searchhead)
  7101. (save-excursion
  7102. (goto-char (point-min))
  7103. (while (re-search-forward org-todo-line-regexp nil t)
  7104. (push (list
  7105. (org-make-org-heading-search-string
  7106. (match-string 3) t))
  7107. tbl)))
  7108. tbl)
  7109. (tag (setq type :tag beg beg1)
  7110. (or org-tag-alist (org-get-buffer-tags)))
  7111. (prop (setq type :prop beg beg1)
  7112. (mapcar 'list (org-buffer-property-keys nil t t)))
  7113. (t (progn
  7114. (call-interactively org-completion-fallback-command)
  7115. (throw 'exit nil)))))
  7116. (pattern (buffer-substring-no-properties beg end))
  7117. (completion (try-completion pattern table confirm)))
  7118. (cond ((eq completion t)
  7119. (if (not (assoc (upcase pattern) table))
  7120. (message "Already complete")
  7121. (if (and (equal type :opt)
  7122. (not (member (car (assoc (upcase pattern) table))
  7123. org-additional-option-like-keywords)))
  7124. (insert (substring (cdr (assoc (upcase pattern) table))
  7125. (length pattern)))
  7126. (if (memq type '(:tag :prop)) (insert ":")))))
  7127. ((null completion)
  7128. (message "Can't find completion for \"%s\"" pattern)
  7129. (ding))
  7130. ((not (string= pattern completion))
  7131. (delete-region beg end)
  7132. (if (string-match " +$" completion)
  7133. (setq completion (replace-match "" t t completion)))
  7134. (insert completion)
  7135. (if (get-buffer-window "*Completions*")
  7136. (delete-window (get-buffer-window "*Completions*")))
  7137. (if (assoc completion table)
  7138. (if (eq type :todo) (insert " ")
  7139. (if (memq type '(:tag :prop)) (insert ":"))))
  7140. (if (and (equal type :opt) (assoc completion table))
  7141. (message "%s" (substitute-command-keys
  7142. "Press \\[org-complete] again to insert example settings"))))
  7143. (t
  7144. (message "Making completion list...")
  7145. (let ((list (sort (all-completions pattern table confirm)
  7146. 'string<)))
  7147. (with-output-to-temp-buffer "*Completions*"
  7148. (condition-case nil
  7149. ;; Protection needed for XEmacs and emacs 21
  7150. (display-completion-list list pattern)
  7151. (error (display-completion-list list)))))
  7152. (message "Making completion list...%s" "done")))))))
  7153. ;;;; TODO, DEADLINE, Comments
  7154. (defun org-toggle-comment ()
  7155. "Change the COMMENT state of an entry."
  7156. (interactive)
  7157. (save-excursion
  7158. (org-back-to-heading)
  7159. (let (case-fold-search)
  7160. (if (looking-at (concat outline-regexp
  7161. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7162. (replace-match "" t t nil 1)
  7163. (if (looking-at outline-regexp)
  7164. (progn
  7165. (goto-char (match-end 0))
  7166. (insert org-comment-string " ")))))))
  7167. (defvar org-last-todo-state-is-todo nil
  7168. "This is non-nil when the last TODO state change led to a TODO state.
  7169. If the last change removed the TODO tag or switched to DONE, then
  7170. this is nil.")
  7171. (defvar org-setting-tags nil) ; dynamically skiped
  7172. (defun org-parse-local-options (string var)
  7173. "Parse STRING for startup setting relevant for variable VAR."
  7174. (let ((rtn (symbol-value var))
  7175. e opts)
  7176. (save-match-data
  7177. (if (or (not string) (not (string-match "\\S-" string)))
  7178. rtn
  7179. (setq opts (delq nil (mapcar (lambda (x)
  7180. (setq e (assoc x org-startup-options))
  7181. (if (eq (nth 1 e) var) e nil))
  7182. (org-split-string string "[ \t]+"))))
  7183. (if (not opts)
  7184. rtn
  7185. (setq rtn nil)
  7186. (while (setq e (pop opts))
  7187. (if (not (nth 3 e))
  7188. (setq rtn (nth 2 e))
  7189. (if (not (listp rtn)) (setq rtn nil))
  7190. (push (nth 2 e) rtn)))
  7191. rtn)))))
  7192. (defvar org-blocker-hook nil
  7193. "Hook for functions that are allowed to block a state change.
  7194. Each function gets as its single argument a property list, see
  7195. `org-trigger-hook' for more information about this list.
  7196. If any of the functions in this hook returns nil, the state change
  7197. is blocked.")
  7198. (defvar org-trigger-hook nil
  7199. "Hook for functions that are triggered by a state change.
  7200. Each function gets as its single argument a property list with at least
  7201. the following elements:
  7202. (:type type-of-change :position pos-at-entry-start
  7203. :from old-state :to new-state)
  7204. Depending on the type, more properties may be present.
  7205. This mechanism is currently implemented for:
  7206. TODO state changes
  7207. ------------------
  7208. :type todo-state-change
  7209. :from previous state (keyword as a string), or nil
  7210. :to new state (keyword as a string), or nil")
  7211. (defvar org-agenda-headline-snapshot-before-repeat)
  7212. (defun org-todo (&optional arg)
  7213. "Change the TODO state of an item.
  7214. The state of an item is given by a keyword at the start of the heading,
  7215. like
  7216. *** TODO Write paper
  7217. *** DONE Call mom
  7218. The different keywords are specified in the variable `org-todo-keywords'.
  7219. By default the available states are \"TODO\" and \"DONE\".
  7220. So for this example: when the item starts with TODO, it is changed to DONE.
  7221. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7222. DONE are present, add TODO at the beginning of the heading.
  7223. With C-u prefix arg, use completion to determine the new state.
  7224. With numeric prefix arg, switch to that state.
  7225. For calling through lisp, arg is also interpreted in the following way:
  7226. 'none -> empty state
  7227. \"\"(empty string) -> switch to empty state
  7228. 'done -> switch to DONE
  7229. 'nextset -> switch to the next set of keywords
  7230. 'previousset -> switch to the previous set of keywords
  7231. \"WAITING\" -> switch to the specified keyword, but only if it
  7232. really is a member of `org-todo-keywords'."
  7233. (interactive "P")
  7234. (save-excursion
  7235. (catch 'exit
  7236. (org-back-to-heading)
  7237. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7238. (or (looking-at (concat " +" org-todo-regexp " *"))
  7239. (looking-at " *"))
  7240. (let* ((match-data (match-data))
  7241. (startpos (point-at-bol))
  7242. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7243. (org-log-done org-log-done)
  7244. (org-log-repeat org-log-repeat)
  7245. (org-todo-log-states org-todo-log-states)
  7246. (this (match-string 1))
  7247. (hl-pos (match-beginning 0))
  7248. (head (org-get-todo-sequence-head this))
  7249. (ass (assoc head org-todo-kwd-alist))
  7250. (interpret (nth 1 ass))
  7251. (done-word (nth 3 ass))
  7252. (final-done-word (nth 4 ass))
  7253. (last-state (or this ""))
  7254. (completion-ignore-case t)
  7255. (member (member this org-todo-keywords-1))
  7256. (tail (cdr member))
  7257. (state (cond
  7258. ((and org-todo-key-trigger
  7259. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7260. (and (not arg) org-use-fast-todo-selection
  7261. (not (eq org-use-fast-todo-selection 'prefix)))))
  7262. ;; Use fast selection
  7263. (org-fast-todo-selection))
  7264. ((and (equal arg '(4))
  7265. (or (not org-use-fast-todo-selection)
  7266. (not org-todo-key-trigger)))
  7267. ;; Read a state with completion
  7268. (org-ido-completing-read "State: " (mapcar (lambda(x) (list x))
  7269. org-todo-keywords-1)
  7270. nil t))
  7271. ((eq arg 'right)
  7272. (if this
  7273. (if tail (car tail) nil)
  7274. (car org-todo-keywords-1)))
  7275. ((eq arg 'left)
  7276. (if (equal member org-todo-keywords-1)
  7277. nil
  7278. (if this
  7279. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7280. org-todo-keywords-1)
  7281. (org-last org-todo-keywords-1))))
  7282. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7283. (setq arg nil))) ; hack to fall back to cycling
  7284. (arg
  7285. ;; user or caller requests a specific state
  7286. (cond
  7287. ((equal arg "") nil)
  7288. ((eq arg 'none) nil)
  7289. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7290. ((eq arg 'nextset)
  7291. (or (car (cdr (member head org-todo-heads)))
  7292. (car org-todo-heads)))
  7293. ((eq arg 'previousset)
  7294. (let ((org-todo-heads (reverse org-todo-heads)))
  7295. (or (car (cdr (member head org-todo-heads)))
  7296. (car org-todo-heads))))
  7297. ((car (member arg org-todo-keywords-1)))
  7298. ((nth (1- (prefix-numeric-value arg))
  7299. org-todo-keywords-1))))
  7300. ((null member) (or head (car org-todo-keywords-1)))
  7301. ((equal this final-done-word) nil) ;; -> make empty
  7302. ((null tail) nil) ;; -> first entry
  7303. ((eq interpret 'sequence)
  7304. (car tail))
  7305. ((memq interpret '(type priority))
  7306. (if (eq this-command last-command)
  7307. (car tail)
  7308. (if (> (length tail) 0)
  7309. (or done-word (car org-done-keywords))
  7310. nil)))
  7311. (t nil)))
  7312. (next (if state (concat " " state " ") " "))
  7313. (change-plist (list :type 'todo-state-change :from this :to state
  7314. :position startpos))
  7315. dolog now-done-p)
  7316. (when org-blocker-hook
  7317. (unless (save-excursion
  7318. (save-match-data
  7319. (run-hook-with-args-until-failure
  7320. 'org-blocker-hook change-plist)))
  7321. (if (interactive-p)
  7322. (error "TODO state change from %s to %s blocked" this state)
  7323. ;; fail silently
  7324. (message "TODO state change from %s to %s blocked" this state)
  7325. (throw 'exit nil))))
  7326. (store-match-data match-data)
  7327. (replace-match next t t)
  7328. (unless (pos-visible-in-window-p hl-pos)
  7329. (message "TODO state changed to %s" (org-trim next)))
  7330. (unless head
  7331. (setq head (org-get-todo-sequence-head state)
  7332. ass (assoc head org-todo-kwd-alist)
  7333. interpret (nth 1 ass)
  7334. done-word (nth 3 ass)
  7335. final-done-word (nth 4 ass)))
  7336. (when (memq arg '(nextset previousset))
  7337. (message "Keyword-Set %d/%d: %s"
  7338. (- (length org-todo-sets) -1
  7339. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7340. (length org-todo-sets)
  7341. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7342. (setq org-last-todo-state-is-todo
  7343. (not (member state org-done-keywords)))
  7344. (setq now-done-p (and (member state org-done-keywords)
  7345. (not (member this org-done-keywords))))
  7346. (and logging (org-local-logging logging))
  7347. (when (and (or org-todo-log-states org-log-done)
  7348. (not (memq arg '(nextset previousset))))
  7349. ;; we need to look at recording a time and note
  7350. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7351. (nth 2 (assoc this org-todo-log-states))))
  7352. (when (and state
  7353. (member state org-not-done-keywords)
  7354. (not (member this org-not-done-keywords)))
  7355. ;; This is now a todo state and was not one before
  7356. ;; If there was a CLOSED time stamp, get rid of it.
  7357. (org-add-planning-info nil nil 'closed))
  7358. (when (and now-done-p org-log-done)
  7359. ;; It is now done, and it was not done before
  7360. (org-add-planning-info 'closed (org-current-time))
  7361. (if (and (not dolog) (eq 'note org-log-done))
  7362. (org-add-log-setup 'done state 'findpos 'note)))
  7363. (when (and state dolog)
  7364. ;; This is a non-nil state, and we need to log it
  7365. (org-add-log-setup 'state state 'findpos dolog)))
  7366. ;; Fixup tag positioning
  7367. (org-todo-trigger-tag-changes state)
  7368. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7369. (when org-provide-todo-statistics
  7370. (org-update-parent-todo-statistics))
  7371. (run-hooks 'org-after-todo-state-change-hook)
  7372. (if (and arg (not (member state org-done-keywords)))
  7373. (setq head (org-get-todo-sequence-head state)))
  7374. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7375. ;; Do we need to trigger a repeat?
  7376. (when now-done-p
  7377. (when (boundp 'org-agenda-headline-snapshot-before-repeat)
  7378. ;; This is for the agenda, take a snapshot of the headline.
  7379. (save-match-data
  7380. (setq org-agenda-headline-snapshot-before-repeat
  7381. (org-get-heading))))
  7382. (org-auto-repeat-maybe state))
  7383. ;; Fixup cursor location if close to the keyword
  7384. (if (and (outline-on-heading-p)
  7385. (not (bolp))
  7386. (save-excursion (beginning-of-line 1)
  7387. (looking-at org-todo-line-regexp))
  7388. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7389. (progn
  7390. (goto-char (or (match-end 2) (match-end 1)))
  7391. (just-one-space)))
  7392. (when org-trigger-hook
  7393. (save-excursion
  7394. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7395. (defun org-update-parent-todo-statistics ()
  7396. "Update any statistics cookie in the parent of the current headline."
  7397. (interactive)
  7398. (let ((box-re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  7399. level (cnt-all 0) (cnt-done 0) is-percent kwd)
  7400. (catch 'exit
  7401. (save-excursion
  7402. (setq level (org-up-heading-safe))
  7403. (unless (and level
  7404. (re-search-forward box-re (point-at-eol) t))
  7405. (throw 'exit nil))
  7406. (setq is-percent (match-end 2))
  7407. (save-match-data
  7408. (unless (outline-next-heading) (throw 'exit nil))
  7409. (while (looking-at org-todo-line-regexp)
  7410. (setq kwd (match-string 2))
  7411. (and kwd (setq cnt-all (1+ cnt-all)))
  7412. (and (member kwd org-done-keywords)
  7413. (setq cnt-done (1+ cnt-done)))
  7414. (condition-case nil
  7415. (org-forward-same-level 1)
  7416. (error (end-of-line 1)))))
  7417. (replace-match
  7418. (if is-percent
  7419. (format "[%d%%]" (/ (* 100 cnt-done) (max 1 cnt-all)))
  7420. (format "[%d/%d]" cnt-done cnt-all)))
  7421. (run-hook-with-args 'org-after-todo-statistics-hook
  7422. cnt-done (- cnt-all cnt-done))))))
  7423. (defvar org-after-todo-statistics-hook nil
  7424. "Hook that is called after a TODO statistics cookie has been updated.
  7425. Each function is called with two arguments: the number of not-done entries
  7426. and the number of done entries.
  7427. For example, the following function, when added to this hook, will switch
  7428. an entry to DONE when all children are done, and back to TODO when new
  7429. entries are set to a TODO status. Note that this hook is only called
  7430. when there is a statistics cookie in the headline!
  7431. (defun org-summary-todo (n-done n-not-done)
  7432. \"Switch entry to DONE when all subentries are done, to TODO otherwise.\"
  7433. (let (org-log-done org-log-states) ; turn off logging
  7434. (org-todo (if (= n-not-done 0) \"DONE\" \"TODO\"))))
  7435. ")
  7436. (defun org-todo-trigger-tag-changes (state)
  7437. "Apply the changes defined in `org-todo-state-tags-triggers'."
  7438. (let ((l org-todo-state-tags-triggers)
  7439. changes)
  7440. (when (or (not state) (equal state ""))
  7441. (setq changes (append changes (cdr (assoc "" l)))))
  7442. (when (and (stringp state) (> (length state) 0))
  7443. (setq changes (append changes (cdr (assoc state l)))))
  7444. (when (member state org-not-done-keywords)
  7445. (setq changes (append changes (cdr (assoc 'todo l)))))
  7446. (when (member state org-done-keywords)
  7447. (setq changes (append changes (cdr (assoc 'done l)))))
  7448. (dolist (c changes)
  7449. (org-toggle-tag (car c) (if (cdr c) 'on 'off)))))
  7450. (defun org-local-logging (value)
  7451. "Get logging settings from a property VALUE."
  7452. (let* (words w a)
  7453. ;; directly set the variables, they are already local.
  7454. (setq org-log-done nil
  7455. org-log-repeat nil
  7456. org-todo-log-states nil)
  7457. (setq words (org-split-string value))
  7458. (while (setq w (pop words))
  7459. (cond
  7460. ((setq a (assoc w org-startup-options))
  7461. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7462. (set (nth 1 a) (nth 2 a))))
  7463. ((setq a (org-extract-log-state-settings w))
  7464. (and (member (car a) org-todo-keywords-1)
  7465. (push a org-todo-log-states)))))))
  7466. (defun org-get-todo-sequence-head (kwd)
  7467. "Return the head of the TODO sequence to which KWD belongs.
  7468. If KWD is not set, check if there is a text property remembering the
  7469. right sequence."
  7470. (let (p)
  7471. (cond
  7472. ((not kwd)
  7473. (or (get-text-property (point-at-bol) 'org-todo-head)
  7474. (progn
  7475. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7476. nil (point-at-eol)))
  7477. (get-text-property p 'org-todo-head))))
  7478. ((not (member kwd org-todo-keywords-1))
  7479. (car org-todo-keywords-1))
  7480. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7481. (defun org-fast-todo-selection ()
  7482. "Fast TODO keyword selection with single keys.
  7483. Returns the new TODO keyword, or nil if no state change should occur."
  7484. (let* ((fulltable org-todo-key-alist)
  7485. (done-keywords org-done-keywords) ;; needed for the faces.
  7486. (maxlen (apply 'max (mapcar
  7487. (lambda (x)
  7488. (if (stringp (car x)) (string-width (car x)) 0))
  7489. fulltable)))
  7490. (expert nil)
  7491. (fwidth (+ maxlen 3 1 3))
  7492. (ncol (/ (- (window-width) 4) fwidth))
  7493. tg cnt e c tbl
  7494. groups ingroup)
  7495. (save-window-excursion
  7496. (if expert
  7497. (set-buffer (get-buffer-create " *Org todo*"))
  7498. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7499. (erase-buffer)
  7500. (org-set-local 'org-done-keywords done-keywords)
  7501. (setq tbl fulltable cnt 0)
  7502. (while (setq e (pop tbl))
  7503. (cond
  7504. ((equal e '(:startgroup))
  7505. (push '() groups) (setq ingroup t)
  7506. (when (not (= cnt 0))
  7507. (setq cnt 0)
  7508. (insert "\n"))
  7509. (insert "{ "))
  7510. ((equal e '(:endgroup))
  7511. (setq ingroup nil cnt 0)
  7512. (insert "}\n"))
  7513. (t
  7514. (setq tg (car e) c (cdr e))
  7515. (if ingroup (push tg (car groups)))
  7516. (setq tg (org-add-props tg nil 'face
  7517. (org-get-todo-face tg)))
  7518. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7519. (insert "[" c "] " tg (make-string
  7520. (- fwidth 4 (length tg)) ?\ ))
  7521. (when (= (setq cnt (1+ cnt)) ncol)
  7522. (insert "\n")
  7523. (if ingroup (insert " "))
  7524. (setq cnt 0)))))
  7525. (insert "\n")
  7526. (goto-char (point-min))
  7527. (if (not expert) (org-fit-window-to-buffer))
  7528. (message "[a-z..]:Set [SPC]:clear")
  7529. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7530. (cond
  7531. ((or (= c ?\C-g)
  7532. (and (= c ?q) (not (rassoc c fulltable))))
  7533. (setq quit-flag t))
  7534. ((= c ?\ ) nil)
  7535. ((setq e (rassoc c fulltable) tg (car e))
  7536. tg)
  7537. (t (setq quit-flag t))))))
  7538. (defun org-entry-is-todo-p ()
  7539. (member (org-get-todo-state) org-not-done-keywords))
  7540. (defun org-entry-is-done-p ()
  7541. (member (org-get-todo-state) org-done-keywords))
  7542. (defun org-get-todo-state ()
  7543. (save-excursion
  7544. (org-back-to-heading t)
  7545. (and (looking-at org-todo-line-regexp)
  7546. (match-end 2)
  7547. (match-string 2))))
  7548. (defun org-at-date-range-p (&optional inactive-ok)
  7549. "Is the cursor inside a date range?"
  7550. (interactive)
  7551. (save-excursion
  7552. (catch 'exit
  7553. (let ((pos (point)))
  7554. (skip-chars-backward "^[<\r\n")
  7555. (skip-chars-backward "<[")
  7556. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7557. (>= (match-end 0) pos)
  7558. (throw 'exit t))
  7559. (skip-chars-backward "^<[\r\n")
  7560. (skip-chars-backward "<[")
  7561. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7562. (>= (match-end 0) pos)
  7563. (throw 'exit t)))
  7564. nil)))
  7565. (defun org-get-repeat ()
  7566. "Check if there is a deadline/schedule with repeater in this entry."
  7567. (save-match-data
  7568. (save-excursion
  7569. (org-back-to-heading t)
  7570. (if (re-search-forward
  7571. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7572. (match-string 1)))))
  7573. (defvar org-last-changed-timestamp)
  7574. (defvar org-last-inserted-timestamp)
  7575. (defvar org-log-post-message)
  7576. (defvar org-log-note-purpose)
  7577. (defvar org-log-note-how)
  7578. (defvar org-log-note-extra)
  7579. (defun org-auto-repeat-maybe (done-word)
  7580. "Check if the current headline contains a repeated deadline/schedule.
  7581. If yes, set TODO state back to what it was and change the base date
  7582. of repeating deadline/scheduled time stamps to new date.
  7583. This function is run automatically after each state change to a DONE state."
  7584. ;; last-state is dynamically scoped into this function
  7585. (let* ((repeat (org-get-repeat))
  7586. (aa (assoc last-state org-todo-kwd-alist))
  7587. (interpret (nth 1 aa))
  7588. (head (nth 2 aa))
  7589. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7590. (msg "Entry repeats: ")
  7591. (org-log-done nil)
  7592. (org-todo-log-states nil)
  7593. (nshiftmax 10) (nshift 0)
  7594. re type n what ts mb0 time)
  7595. (when repeat
  7596. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7597. (org-todo (if (eq interpret 'type) last-state head))
  7598. (when org-log-repeat
  7599. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7600. (memq 'org-add-log-note post-command-hook))
  7601. ;; OK, we are already setup for some record
  7602. (if (eq org-log-repeat 'note)
  7603. ;; make sure we take a note, not only a time stamp
  7604. (setq org-log-note-how 'note))
  7605. ;; Set up for taking a record
  7606. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7607. 'findpos org-log-repeat)))
  7608. (org-back-to-heading t)
  7609. (org-add-planning-info nil nil 'closed)
  7610. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7611. org-deadline-time-regexp "\\)\\|\\("
  7612. org-ts-regexp "\\)"))
  7613. (while (re-search-forward
  7614. re (save-excursion (outline-next-heading) (point)) t)
  7615. (setq type (if (match-end 1) org-scheduled-string
  7616. (if (match-end 3) org-deadline-string "Plain:"))
  7617. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7618. mb0 (match-beginning 0))
  7619. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7620. (setq n (string-to-number (match-string 2 ts))
  7621. what (match-string 3 ts))
  7622. (if (equal what "w") (setq n (* n 7) what "d"))
  7623. ;; Preparation, see if we need to modify the start date for the change
  7624. (when (match-end 1)
  7625. (setq time (save-match-data (org-time-string-to-time ts)))
  7626. (cond
  7627. ((equal (match-string 1 ts) ".")
  7628. ;; Shift starting date to today
  7629. (org-timestamp-change
  7630. (- (time-to-days (current-time)) (time-to-days time))
  7631. 'day))
  7632. ((equal (match-string 1 ts) "+")
  7633. (while (or (= nshift 0)
  7634. (<= (time-to-days time) (time-to-days (current-time))))
  7635. (when (= (incf nshift) nshiftmax)
  7636. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7637. (error "Abort")))
  7638. (org-timestamp-change n (cdr (assoc what whata)))
  7639. (org-at-timestamp-p t)
  7640. (setq ts (match-string 1))
  7641. (setq time (save-match-data (org-time-string-to-time ts))))
  7642. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7643. ;; rematch, so that we have everything in place for the real shift
  7644. (org-at-timestamp-p t)
  7645. (setq ts (match-string 1))
  7646. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7647. (org-timestamp-change n (cdr (assoc what whata)))
  7648. (setq msg (concat msg type " " org-last-changed-timestamp " "))))
  7649. (setq org-log-post-message msg)
  7650. (message "%s" msg))))
  7651. (defun org-show-todo-tree (arg)
  7652. "Make a compact tree which shows all headlines marked with TODO.
  7653. The tree will show the lines where the regexp matches, and all higher
  7654. headlines above the match.
  7655. With a \\[universal-argument] prefix, also show the DONE entries.
  7656. With a numeric prefix N, construct a sparse tree for the Nth element
  7657. of `org-todo-keywords-1'."
  7658. (interactive "P")
  7659. (let ((case-fold-search nil)
  7660. (kwd-re
  7661. (cond ((null arg) org-not-done-regexp)
  7662. ((equal arg '(4))
  7663. (let ((kwd (org-ido-completing-read "Keyword (or KWD1|KWD2|...): "
  7664. (mapcar 'list org-todo-keywords-1))))
  7665. (concat "\\("
  7666. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7667. "\\)\\>")))
  7668. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7669. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7670. org-todo-keywords-1)))
  7671. (t (error "Invalid prefix argument: %s" arg)))))
  7672. (message "%d TODO entries found"
  7673. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7674. (defun org-deadline (&optional remove time)
  7675. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7676. With argument REMOVE, remove any deadline from the item.
  7677. When TIME is set, it should be an internal time specification, and the
  7678. scheduling will use the corresponding date."
  7679. (interactive "P")
  7680. (if remove
  7681. (progn
  7682. (org-remove-timestamp-with-keyword org-deadline-string)
  7683. (message "Item no longer has a deadline."))
  7684. (if (org-get-repeat)
  7685. (error "Cannot change deadline on task with repeater, please do that by hand")
  7686. (org-add-planning-info 'deadline time 'closed)
  7687. (message "Deadline on %s" org-last-inserted-timestamp))))
  7688. (defun org-schedule (&optional remove time)
  7689. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7690. With argument REMOVE, remove any scheduling date from the item.
  7691. When TIME is set, it should be an internal time specification, and the
  7692. scheduling will use the corresponding date."
  7693. (interactive "P")
  7694. (if remove
  7695. (progn
  7696. (org-remove-timestamp-with-keyword org-scheduled-string)
  7697. (message "Item is no longer scheduled."))
  7698. (if (org-get-repeat)
  7699. (error "Cannot reschedule task with repeater, please do that by hand")
  7700. (org-add-planning-info 'scheduled time 'closed)
  7701. (message "Scheduled to %s" org-last-inserted-timestamp))))
  7702. (defun org-remove-timestamp-with-keyword (keyword)
  7703. "Remove all time stamps with KEYWORD in the current entry."
  7704. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7705. beg)
  7706. (save-excursion
  7707. (org-back-to-heading t)
  7708. (setq beg (point))
  7709. (org-end-of-subtree t t)
  7710. (while (re-search-backward re beg t)
  7711. (replace-match "")
  7712. (if (and (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7713. (equal (char-before) ?\ ))
  7714. (backward-delete-char 1)
  7715. (if (string-match "^[ \t]*$" (buffer-substring
  7716. (point-at-bol) (point-at-eol)))
  7717. (delete-region (point-at-bol)
  7718. (min (point-max) (1+ (point-at-eol))))))))))
  7719. (defun org-add-planning-info (what &optional time &rest remove)
  7720. "Insert new timestamp with keyword in the line directly after the headline.
  7721. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7722. If non is given, the user is prompted for a date.
  7723. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7724. be removed."
  7725. (interactive)
  7726. (let (org-time-was-given org-end-time-was-given ts
  7727. end default-time default-input)
  7728. (when (and (not time) (memq what '(scheduled deadline)))
  7729. ;; Try to get a default date/time from existing timestamp
  7730. (save-excursion
  7731. (org-back-to-heading t)
  7732. (setq end (save-excursion (outline-next-heading) (point)))
  7733. (when (re-search-forward (if (eq what 'scheduled)
  7734. org-scheduled-time-regexp
  7735. org-deadline-time-regexp)
  7736. end t)
  7737. (setq ts (match-string 1)
  7738. default-time
  7739. (apply 'encode-time (org-parse-time-string ts))
  7740. default-input (and ts (org-get-compact-tod ts))))))
  7741. (when what
  7742. ;; If necessary, get the time from the user
  7743. (setq time (or time (org-read-date nil 'to-time nil nil
  7744. default-time default-input))))
  7745. (when (and org-insert-labeled-timestamps-at-point
  7746. (member what '(scheduled deadline)))
  7747. (insert
  7748. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7749. (org-insert-time-stamp time org-time-was-given
  7750. nil nil nil (list org-end-time-was-given))
  7751. (setq what nil))
  7752. (save-excursion
  7753. (save-restriction
  7754. (let (col list elt ts buffer-invisibility-spec)
  7755. (org-back-to-heading t)
  7756. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7757. (goto-char (match-end 1))
  7758. (setq col (current-column))
  7759. (goto-char (match-end 0))
  7760. (if (eobp) (insert "\n") (forward-char 1))
  7761. (if (and (not (looking-at outline-regexp))
  7762. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7763. "[^\r\n]*"))
  7764. (not (equal (match-string 1) org-clock-string)))
  7765. (narrow-to-region (match-beginning 0) (match-end 0))
  7766. (insert-before-markers "\n")
  7767. (backward-char 1)
  7768. (narrow-to-region (point) (point))
  7769. (and org-adapt-indentation (org-indent-to-column col)))
  7770. ;; Check if we have to remove something.
  7771. (setq list (cons what remove))
  7772. (while list
  7773. (setq elt (pop list))
  7774. (goto-char (point-min))
  7775. (when (or (and (eq elt 'scheduled)
  7776. (re-search-forward org-scheduled-time-regexp nil t))
  7777. (and (eq elt 'deadline)
  7778. (re-search-forward org-deadline-time-regexp nil t))
  7779. (and (eq elt 'closed)
  7780. (re-search-forward org-closed-time-regexp nil t)))
  7781. (replace-match "")
  7782. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7783. (if (looking-at " +") (replace-match ""))))
  7784. (goto-char (point-max))
  7785. (when what
  7786. (insert
  7787. (if (not (or (bolp) (eq (char-before) ?\ ))) " " "")
  7788. (cond ((eq what 'scheduled) org-scheduled-string)
  7789. ((eq what 'deadline) org-deadline-string)
  7790. ((eq what 'closed) org-closed-string))
  7791. " ")
  7792. (setq ts (org-insert-time-stamp
  7793. time
  7794. (or org-time-was-given
  7795. (and (eq what 'closed) org-log-done-with-time))
  7796. (eq what 'closed)
  7797. nil nil (list org-end-time-was-given)))
  7798. (end-of-line 1))
  7799. (goto-char (point-min))
  7800. (widen)
  7801. (if (and (looking-at "[ \t]+\n")
  7802. (equal (char-before) ?\n))
  7803. (delete-region (1- (point)) (point-at-eol)))
  7804. ts)))))
  7805. (defvar org-log-note-marker (make-marker))
  7806. (defvar org-log-note-purpose nil)
  7807. (defvar org-log-note-state nil)
  7808. (defvar org-log-note-how nil)
  7809. (defvar org-log-note-extra nil)
  7810. (defvar org-log-note-window-configuration nil)
  7811. (defvar org-log-note-return-to (make-marker))
  7812. (defvar org-log-post-message nil
  7813. "Message to be displayed after a log note has been stored.
  7814. The auto-repeater uses this.")
  7815. (defun org-add-note ()
  7816. "Add a note to the current entry.
  7817. This is done in the same way as adding a state change note."
  7818. (interactive)
  7819. (org-add-log-setup 'note nil 'findpos nil))
  7820. (defvar org-property-end-re)
  7821. (defun org-add-log-setup (&optional purpose state findpos how &optional extra)
  7822. "Set up the post command hook to take a note.
  7823. If this is about to TODO state change, the new state is expected in STATE.
  7824. When FINDPOS is non-nil, find the correct position for the note in
  7825. the current entry. If not, assume that it can be inserted at point.
  7826. HOW is an indicator what kind of note should be created.
  7827. EXTRA is additional text that will be inserted into the notes buffer."
  7828. (save-restriction
  7829. (save-excursion
  7830. (when findpos
  7831. (org-back-to-heading t)
  7832. (narrow-to-region (point) (save-excursion
  7833. (outline-next-heading) (point)))
  7834. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7835. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7836. "[^\r\n]*\\)?"))
  7837. (goto-char (match-end 0))
  7838. (when (and org-log-state-notes-insert-after-drawers
  7839. (save-excursion
  7840. (forward-line) (looking-at org-drawer-regexp)))
  7841. (progn (forward-line)
  7842. (while (looking-at org-drawer-regexp)
  7843. (goto-char (match-end 0))
  7844. (re-search-forward org-property-end-re (point-max) t)
  7845. (forward-line))
  7846. (forward-line -1)))
  7847. (unless org-log-states-order-reversed
  7848. (and (= (char-after) ?\n) (forward-char 1))
  7849. (org-skip-over-state-notes)
  7850. (skip-chars-backward " \t\n\r")))
  7851. (move-marker org-log-note-marker (point))
  7852. (setq org-log-note-purpose purpose
  7853. org-log-note-state state
  7854. org-log-note-how how
  7855. org-log-note-extra extra)
  7856. (add-hook 'post-command-hook 'org-add-log-note 'append))))
  7857. (defun org-skip-over-state-notes ()
  7858. "Skip past the list of State notes in an entry."
  7859. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7860. (while (looking-at "[ \t]*- State")
  7861. (condition-case nil
  7862. (org-next-item)
  7863. (error (org-end-of-item)))))
  7864. (defun org-add-log-note (&optional purpose)
  7865. "Pop up a window for taking a note, and add this note later at point."
  7866. (remove-hook 'post-command-hook 'org-add-log-note)
  7867. (setq org-log-note-window-configuration (current-window-configuration))
  7868. (delete-other-windows)
  7869. (move-marker org-log-note-return-to (point))
  7870. (switch-to-buffer (marker-buffer org-log-note-marker))
  7871. (goto-char org-log-note-marker)
  7872. (org-switch-to-buffer-other-window "*Org Note*")
  7873. (erase-buffer)
  7874. (if (memq org-log-note-how '(time state))
  7875. (let (current-prefix-arg) (org-store-log-note))
  7876. (let ((org-inhibit-startup t)) (org-mode))
  7877. (insert (format "# Insert note for %s.
  7878. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7879. (cond
  7880. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7881. ((eq org-log-note-purpose 'done) "closed todo item")
  7882. ((eq org-log-note-purpose 'state)
  7883. (format "state change to \"%s\"" org-log-note-state))
  7884. ((eq org-log-note-purpose 'note)
  7885. "this entry")
  7886. (t (error "This should not happen")))))
  7887. (if org-log-note-extra (insert org-log-note-extra))
  7888. (org-set-local 'org-finish-function 'org-store-log-note)))
  7889. (defvar org-note-abort nil) ; dynamically scoped
  7890. (defun org-store-log-note ()
  7891. "Finish taking a log note, and insert it to where it belongs."
  7892. (let ((txt (buffer-string))
  7893. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7894. lines ind)
  7895. (kill-buffer (current-buffer))
  7896. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7897. (setq txt (replace-match "" t t txt)))
  7898. (if (string-match "\\s-+\\'" txt)
  7899. (setq txt (replace-match "" t t txt)))
  7900. (setq lines (org-split-string txt "\n"))
  7901. (when (and note (string-match "\\S-" note))
  7902. (setq note
  7903. (org-replace-escapes
  7904. note
  7905. (list (cons "%u" (user-login-name))
  7906. (cons "%U" user-full-name)
  7907. (cons "%t" (format-time-string
  7908. (org-time-stamp-format 'long 'inactive)
  7909. (current-time)))
  7910. (cons "%s" (if org-log-note-state
  7911. (concat "\"" org-log-note-state "\"")
  7912. "")))))
  7913. (if lines (setq note (concat note " \\\\")))
  7914. (push note lines))
  7915. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7916. (when lines
  7917. (save-excursion
  7918. (set-buffer (marker-buffer org-log-note-marker))
  7919. (save-excursion
  7920. (goto-char org-log-note-marker)
  7921. (move-marker org-log-note-marker nil)
  7922. (end-of-line 1)
  7923. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7924. (indent-relative nil)
  7925. (insert "- " (pop lines))
  7926. (org-indent-line-function)
  7927. (beginning-of-line 1)
  7928. (looking-at "[ \t]*")
  7929. (setq ind (concat (match-string 0) " "))
  7930. (end-of-line 1)
  7931. (while lines (insert "\n" ind (pop lines)))))))
  7932. (set-window-configuration org-log-note-window-configuration)
  7933. (with-current-buffer (marker-buffer org-log-note-return-to)
  7934. (goto-char org-log-note-return-to))
  7935. (move-marker org-log-note-return-to nil)
  7936. (and org-log-post-message (message "%s" org-log-post-message)))
  7937. (defun org-sparse-tree (&optional arg)
  7938. "Create a sparse tree, prompt for the details.
  7939. This command can create sparse trees. You first need to select the type
  7940. of match used to create the tree:
  7941. t Show entries with a specific TODO keyword.
  7942. T Show entries selected by a tags match.
  7943. p Enter a property name and its value (both with completion on existing
  7944. names/values) and show entries with that property.
  7945. r Show entries matching a regular expression
  7946. d Show deadlines due within `org-deadline-warning-days'."
  7947. (interactive "P")
  7948. (let (ans kwd value)
  7949. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7950. (setq ans (read-char-exclusive))
  7951. (cond
  7952. ((equal ans ?d)
  7953. (call-interactively 'org-check-deadlines))
  7954. ((equal ans ?b)
  7955. (call-interactively 'org-check-before-date))
  7956. ((equal ans ?t)
  7957. (org-show-todo-tree '(4)))
  7958. ((equal ans ?T)
  7959. (call-interactively 'org-tags-sparse-tree))
  7960. ((member ans '(?p ?P))
  7961. (setq kwd (org-ido-completing-read "Property: "
  7962. (mapcar 'list (org-buffer-property-keys))))
  7963. (setq value (org-ido-completing-read "Value: "
  7964. (mapcar 'list (org-property-values kwd))))
  7965. (unless (string-match "\\`{.*}\\'" value)
  7966. (setq value (concat "\"" value "\"")))
  7967. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7968. ((member ans '(?r ?R ?/))
  7969. (call-interactively 'org-occur))
  7970. (t (error "No such sparse tree command \"%c\"" ans)))))
  7971. (defvar org-occur-highlights nil
  7972. "List of overlays used for occur matches.")
  7973. (make-variable-buffer-local 'org-occur-highlights)
  7974. (defvar org-occur-parameters nil
  7975. "Parameters of the active org-occur calls.
  7976. This is a list, each call to org-occur pushes as cons cell,
  7977. containing the regular expression and the callback, onto the list.
  7978. The list can contain several entries if `org-occur' has been called
  7979. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7980. will only contain one set of parameters. When the highlights are
  7981. removed (for example with `C-c C-c', or with the next edit (depending
  7982. on `org-remove-highlights-with-change'), this variable is emptied
  7983. as well.")
  7984. (make-variable-buffer-local 'org-occur-parameters)
  7985. (defun org-occur (regexp &optional keep-previous callback)
  7986. "Make a compact tree which shows all matches of REGEXP.
  7987. The tree will show the lines where the regexp matches, and all higher
  7988. headlines above the match. It will also show the heading after the match,
  7989. to make sure editing the matching entry is easy.
  7990. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7991. call to `org-occur' will be kept, to allow stacking of calls to this
  7992. command.
  7993. If CALLBACK is non-nil, it is a function which is called to confirm
  7994. that the match should indeed be shown."
  7995. (interactive "sRegexp: \nP")
  7996. (unless keep-previous
  7997. (org-remove-occur-highlights nil nil t))
  7998. (push (cons regexp callback) org-occur-parameters)
  7999. (let ((cnt 0))
  8000. (save-excursion
  8001. (goto-char (point-min))
  8002. (if (or (not keep-previous) ; do not want to keep
  8003. (not org-occur-highlights)) ; no previous matches
  8004. ;; hide everything
  8005. (org-overview))
  8006. (while (re-search-forward regexp nil t)
  8007. (when (or (not callback)
  8008. (save-match-data (funcall callback)))
  8009. (setq cnt (1+ cnt))
  8010. (when org-highlight-sparse-tree-matches
  8011. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  8012. (org-show-context 'occur-tree))))
  8013. (when org-remove-highlights-with-change
  8014. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  8015. nil 'local))
  8016. (unless org-sparse-tree-open-archived-trees
  8017. (org-hide-archived-subtrees (point-min) (point-max)))
  8018. (run-hooks 'org-occur-hook)
  8019. (if (interactive-p)
  8020. (message "%d match(es) for regexp %s" cnt regexp))
  8021. cnt))
  8022. (defun org-show-context (&optional key)
  8023. "Make sure point and context and visible.
  8024. How much context is shown depends upon the variables
  8025. `org-show-hierarchy-above', `org-show-following-heading'. and
  8026. `org-show-siblings'."
  8027. (let ((heading-p (org-on-heading-p t))
  8028. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  8029. (following-p (org-get-alist-option org-show-following-heading key))
  8030. (entry-p (org-get-alist-option org-show-entry-below key))
  8031. (siblings-p (org-get-alist-option org-show-siblings key)))
  8032. (catch 'exit
  8033. ;; Show heading or entry text
  8034. (if (and heading-p (not entry-p))
  8035. (org-flag-heading nil) ; only show the heading
  8036. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  8037. (org-show-hidden-entry))) ; show entire entry
  8038. (when following-p
  8039. ;; Show next sibling, or heading below text
  8040. (save-excursion
  8041. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  8042. (org-flag-heading nil))))
  8043. (when siblings-p (org-show-siblings))
  8044. (when hierarchy-p
  8045. ;; show all higher headings, possibly with siblings
  8046. (save-excursion
  8047. (while (and (condition-case nil
  8048. (progn (org-up-heading-all 1) t)
  8049. (error nil))
  8050. (not (bobp)))
  8051. (org-flag-heading nil)
  8052. (when siblings-p (org-show-siblings))))))))
  8053. (defun org-reveal (&optional siblings)
  8054. "Show current entry, hierarchy above it, and the following headline.
  8055. This can be used to show a consistent set of context around locations
  8056. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  8057. not t for the search context.
  8058. With optional argument SIBLINGS, on each level of the hierarchy all
  8059. siblings are shown. This repairs the tree structure to what it would
  8060. look like when opened with hierarchical calls to `org-cycle'."
  8061. (interactive "P")
  8062. (let ((org-show-hierarchy-above t)
  8063. (org-show-following-heading t)
  8064. (org-show-siblings (if siblings t org-show-siblings)))
  8065. (org-show-context nil)))
  8066. (defun org-highlight-new-match (beg end)
  8067. "Highlight from BEG to END and mark the highlight is an occur headline."
  8068. (let ((ov (org-make-overlay beg end)))
  8069. (org-overlay-put ov 'face 'secondary-selection)
  8070. (push ov org-occur-highlights)))
  8071. (defun org-remove-occur-highlights (&optional beg end noremove)
  8072. "Remove the occur highlights from the buffer.
  8073. BEG and END are ignored. If NOREMOVE is nil, remove this function
  8074. from the `before-change-functions' in the current buffer."
  8075. (interactive)
  8076. (unless org-inhibit-highlight-removal
  8077. (mapc 'org-delete-overlay org-occur-highlights)
  8078. (setq org-occur-highlights nil)
  8079. (setq org-occur-parameters nil)
  8080. (unless noremove
  8081. (remove-hook 'before-change-functions
  8082. 'org-remove-occur-highlights 'local))))
  8083. ;;;; Priorities
  8084. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  8085. "Regular expression matching the priority indicator.")
  8086. (defvar org-remove-priority-next-time nil)
  8087. (defun org-priority-up ()
  8088. "Increase the priority of the current item."
  8089. (interactive)
  8090. (org-priority 'up))
  8091. (defun org-priority-down ()
  8092. "Decrease the priority of the current item."
  8093. (interactive)
  8094. (org-priority 'down))
  8095. (defun org-priority (&optional action)
  8096. "Change the priority of an item by ARG.
  8097. ACTION can be `set', `up', `down', or a character."
  8098. (interactive)
  8099. (setq action (or action 'set))
  8100. (let (current new news have remove)
  8101. (save-excursion
  8102. (org-back-to-heading)
  8103. (if (looking-at org-priority-regexp)
  8104. (setq current (string-to-char (match-string 2))
  8105. have t)
  8106. (setq current org-default-priority))
  8107. (cond
  8108. ((or (eq action 'set)
  8109. (if (featurep 'xemacs) (characterp action) (integerp action)))
  8110. (if (not (eq action 'set))
  8111. (setq new action)
  8112. (message "Priority %c-%c, SPC to remove: "
  8113. org-highest-priority org-lowest-priority)
  8114. (setq new (read-char-exclusive)))
  8115. (if (and (= (upcase org-highest-priority) org-highest-priority)
  8116. (= (upcase org-lowest-priority) org-lowest-priority))
  8117. (setq new (upcase new)))
  8118. (cond ((equal new ?\ ) (setq remove t))
  8119. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  8120. (error "Priority must be between `%c' and `%c'"
  8121. org-highest-priority org-lowest-priority))))
  8122. ((eq action 'up)
  8123. (if (and (not have) (eq last-command this-command))
  8124. (setq new org-lowest-priority)
  8125. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8126. org-default-priority (1- current)))))
  8127. ((eq action 'down)
  8128. (if (and (not have) (eq last-command this-command))
  8129. (setq new org-highest-priority)
  8130. (setq new (if (and org-priority-start-cycle-with-default (not have))
  8131. org-default-priority (1+ current)))))
  8132. (t (error "Invalid action")))
  8133. (if (or (< (upcase new) org-highest-priority)
  8134. (> (upcase new) org-lowest-priority))
  8135. (setq remove t))
  8136. (setq news (format "%c" new))
  8137. (if have
  8138. (if remove
  8139. (replace-match "" t t nil 1)
  8140. (replace-match news t t nil 2))
  8141. (if remove
  8142. (error "No priority cookie found in line")
  8143. (looking-at org-todo-line-regexp)
  8144. (if (match-end 2)
  8145. (progn
  8146. (goto-char (match-end 2))
  8147. (insert " [#" news "]"))
  8148. (goto-char (match-beginning 3))
  8149. (insert "[#" news "] ")))))
  8150. (org-preserve-lc (org-set-tags nil 'align))
  8151. (if remove
  8152. (message "Priority removed")
  8153. (message "Priority of current item set to %s" news))))
  8154. (defun org-get-priority (s)
  8155. "Find priority cookie and return priority."
  8156. (save-match-data
  8157. (if (not (string-match org-priority-regexp s))
  8158. (* 1000 (- org-lowest-priority org-default-priority))
  8159. (* 1000 (- org-lowest-priority
  8160. (string-to-char (match-string 2 s)))))))
  8161. ;;;; Tags
  8162. (defvar org-agenda-archives-mode)
  8163. (defun org-scan-tags (action matcher &optional todo-only)
  8164. "Scan headline tags with inheritance and produce output ACTION.
  8165. ACTION can be `sparse-tree' to produce a sparse tree in the current buffer,
  8166. or `agenda' to produce an entry list for an agenda view. It can also be
  8167. a Lisp form or a function that should be called at each matched headline, in
  8168. this case the return value is a list of all return values from these calls.
  8169. MATCHER is a Lisp form to be evaluated, testing if a given set of tags
  8170. qualifies a headline for inclusion. When TODO-ONLY is non-nil,
  8171. only lines with a TODO keyword are included in the output."
  8172. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  8173. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  8174. (org-re
  8175. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  8176. (props (list 'face 'default
  8177. 'done-face 'org-done
  8178. 'undone-face 'default
  8179. 'mouse-face 'highlight
  8180. 'org-not-done-regexp org-not-done-regexp
  8181. 'org-todo-regexp org-todo-regexp
  8182. 'keymap org-agenda-keymap
  8183. 'help-echo
  8184. (format "mouse-2 or RET jump to org file %s"
  8185. (abbreviate-file-name
  8186. (or (buffer-file-name (buffer-base-buffer))
  8187. (buffer-name (buffer-base-buffer)))))))
  8188. (case-fold-search nil)
  8189. lspos tags tags-list
  8190. (tags-alist (list (cons 0 (mapcar 'downcase org-file-tags))))
  8191. (llast 0) rtn rtn1 level category i txt
  8192. todo marker entry priority)
  8193. (when (not (or (member action '(agenda sparse-tree)) (functionp action)))
  8194. (setq action (list 'lambda nil action)))
  8195. (save-excursion
  8196. (goto-char (point-min))
  8197. (when (eq action 'sparse-tree)
  8198. (org-overview)
  8199. (org-remove-occur-highlights))
  8200. (while (re-search-forward re nil t)
  8201. (catch :skip
  8202. (setq todo (if (match-end 1) (match-string 2))
  8203. tags (if (match-end 4) (match-string 4)))
  8204. (goto-char (setq lspos (1+ (match-beginning 0))))
  8205. (setq level (org-reduced-level (funcall outline-level))
  8206. category (org-get-category))
  8207. (setq i llast llast level)
  8208. ;; remove tag lists from same and sublevels
  8209. (while (>= i level)
  8210. (when (setq entry (assoc i tags-alist))
  8211. (setq tags-alist (delete entry tags-alist)))
  8212. (setq i (1- i)))
  8213. ;; add the next tags
  8214. (when tags
  8215. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8216. tags-alist
  8217. (cons (cons level tags) tags-alist)))
  8218. ;; compile tags for current headline
  8219. (setq tags-list
  8220. (if org-use-tag-inheritance
  8221. (apply 'append (mapcar 'cdr (reverse tags-alist)))
  8222. tags))
  8223. (when org-use-tag-inheritance
  8224. (setcdr (car tags-alist)
  8225. (mapcar (lambda (x)
  8226. (setq x (copy-sequence x))
  8227. (org-add-prop-inherited x))
  8228. (cdar tags-alist))))
  8229. (when (and tags org-use-tag-inheritance
  8230. (not (eq t org-use-tag-inheritance)))
  8231. ;; selective inheritance, remove uninherited ones
  8232. (setcdr (car tags-alist)
  8233. (org-remove-uniherited-tags (cdar tags-alist))))
  8234. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8235. (let ((case-fold-search t)) (eval matcher))
  8236. (or
  8237. (not (member org-archive-tag tags-list))
  8238. ;; we have an archive tag, should we use this anyway?
  8239. (or (not org-agenda-skip-archived-trees)
  8240. (and (eq action 'agenda) org-agenda-archives-mode))))
  8241. (unless (eq action 'sparse-tree) (org-agenda-skip))
  8242. ;; select this headline
  8243. (cond
  8244. ((eq action 'sparse-tree)
  8245. (and org-highlight-sparse-tree-matches
  8246. (org-get-heading) (match-end 0)
  8247. (org-highlight-new-match
  8248. (match-beginning 0) (match-beginning 1)))
  8249. (org-show-context 'tags-tree))
  8250. ((eq action 'agenda)
  8251. (setq txt (org-format-agenda-item
  8252. ""
  8253. (concat
  8254. (if org-tags-match-list-sublevels
  8255. (make-string (1- level) ?.) "")
  8256. (org-get-heading))
  8257. category tags-list)
  8258. priority (org-get-priority txt))
  8259. (goto-char lspos)
  8260. (setq marker (org-agenda-new-marker))
  8261. (org-add-props txt props
  8262. 'org-marker marker 'org-hd-marker marker 'org-category category
  8263. 'priority priority 'type "tagsmatch")
  8264. (push txt rtn))
  8265. ((functionp action)
  8266. (save-excursion
  8267. (setq rtn1 (funcall action))
  8268. (push rtn1 rtn))
  8269. (goto-char (point-at-eol)))
  8270. (t (error "Invalid action")))
  8271. ;; if we are to skip sublevels, jump to end of subtree
  8272. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8273. (when (and (eq action 'sparse-tree)
  8274. (not org-sparse-tree-open-archived-trees))
  8275. (org-hide-archived-subtrees (point-min) (point-max)))
  8276. (nreverse rtn)))
  8277. (defun org-remove-uniherited-tags (tags)
  8278. "Remove all tags that are not inherited from the list TAGS."
  8279. (cond
  8280. ((eq org-use-tag-inheritance t) tags)
  8281. ((not org-use-tag-inheritance) nil)
  8282. ((stringp org-use-tag-inheritance)
  8283. (delq nil (mapcar
  8284. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8285. tags)))
  8286. ((listp org-use-tag-inheritance)
  8287. (delq nil (mapcar
  8288. (lambda (x) (if (member x org-use-tag-inheritance) x nil))
  8289. tags)))))
  8290. (defvar todo-only) ;; dynamically scoped
  8291. (defun org-tags-sparse-tree (&optional todo-only match)
  8292. "Create a sparse tree according to tags string MATCH.
  8293. MATCH can contain positive and negative selection of tags, like
  8294. \"+WORK+URGENT-WITHBOSS\".
  8295. If optional argument TODO-ONLY is non-nil, only select lines that are
  8296. also TODO lines."
  8297. (interactive "P")
  8298. (org-prepare-agenda-buffers (list (current-buffer)))
  8299. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8300. (defvar org-cached-props nil)
  8301. (defun org-cached-entry-get (pom property)
  8302. (if (or (eq t org-use-property-inheritance)
  8303. (and (stringp org-use-property-inheritance)
  8304. (string-match org-use-property-inheritance property))
  8305. (and (listp org-use-property-inheritance)
  8306. (member property org-use-property-inheritance)))
  8307. ;; Caching is not possible, check it directly
  8308. (org-entry-get pom property 'inherit)
  8309. ;; Get all properties, so that we can do complicated checks easily
  8310. (cdr (assoc property (or org-cached-props
  8311. (setq org-cached-props
  8312. (org-entry-properties pom)))))))
  8313. (defun org-global-tags-completion-table (&optional files)
  8314. "Return the list of all tags in all agenda buffer/files."
  8315. (save-excursion
  8316. (org-uniquify
  8317. (delq nil
  8318. (apply 'append
  8319. (mapcar
  8320. (lambda (file)
  8321. (set-buffer (find-file-noselect file))
  8322. (append (org-get-buffer-tags)
  8323. (mapcar (lambda (x) (if (stringp (car-safe x))
  8324. (list (car-safe x)) nil))
  8325. org-tag-alist)))
  8326. (if (and files (car files))
  8327. files
  8328. (org-agenda-files))))))))
  8329. (defun org-make-tags-matcher (match)
  8330. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8331. ;; todo-only is scoped dynamically into this function, and the function
  8332. ;; may change it it the matcher asksk for it.
  8333. (unless match
  8334. ;; Get a new match request, with completion
  8335. (let ((org-last-tags-completion-table
  8336. (org-global-tags-completion-table)))
  8337. (setq match (org-ido-completing-read
  8338. "Match: " 'org-tags-completion-function nil nil nil
  8339. 'org-tags-history))))
  8340. ;; Parse the string and create a lisp form
  8341. (let ((match0 match)
  8342. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8343. minus tag mm
  8344. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8345. orterms term orlist re-p str-p level-p level-op time-p
  8346. prop-p pn pv po cat-p gv rest)
  8347. (if (string-match "/+" match)
  8348. ;; match contains also a todo-matching request
  8349. (progn
  8350. (setq tagsmatch (substring match 0 (match-beginning 0))
  8351. todomatch (substring match (match-end 0)))
  8352. (if (string-match "^!" todomatch)
  8353. (setq todo-only t todomatch (substring todomatch 1)))
  8354. (if (string-match "^\\s-*$" todomatch)
  8355. (setq todomatch nil)))
  8356. ;; only matching tags
  8357. (setq tagsmatch match todomatch nil))
  8358. ;; Make the tags matcher
  8359. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8360. (setq tagsmatcher t)
  8361. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8362. (while (setq term (pop orterms))
  8363. (while (and (equal (substring term -1) "\\") orterms)
  8364. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8365. (while (string-match re term)
  8366. (setq rest (substring term (match-end 0))
  8367. minus (and (match-end 1)
  8368. (equal (match-string 1 term) "-"))
  8369. tag (match-string 2 term)
  8370. re-p (equal (string-to-char tag) ?{)
  8371. level-p (match-end 4)
  8372. prop-p (match-end 5)
  8373. mm (cond
  8374. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8375. (level-p
  8376. (setq level-op (org-op-to-function (match-string 3 term)))
  8377. `(,level-op level ,(string-to-number
  8378. (match-string 4 term))))
  8379. (prop-p
  8380. (setq pn (match-string 5 term)
  8381. po (match-string 6 term)
  8382. pv (match-string 7 term)
  8383. cat-p (equal pn "CATEGORY")
  8384. re-p (equal (string-to-char pv) ?{)
  8385. str-p (equal (string-to-char pv) ?\")
  8386. time-p (save-match-data
  8387. (string-match "^\"[[<].*[]>]\"$" pv))
  8388. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8389. (if time-p (setq pv (org-matcher-time pv)))
  8390. (setq po (org-op-to-function po (if time-p 'time str-p)))
  8391. (cond
  8392. ((equal pn "CATEGORY")
  8393. (setq gv '(get-text-property (point) 'org-category)))
  8394. ((equal pn "TODO")
  8395. (setq gv 'todo))
  8396. (t
  8397. (setq gv `(org-cached-entry-get nil ,pn))))
  8398. (if re-p
  8399. (if (eq po 'org<>)
  8400. `(not (string-match ,pv (or ,gv "")))
  8401. `(string-match ,pv (or ,gv "")))
  8402. (if str-p
  8403. `(,po (or ,gv "") ,pv)
  8404. `(,po (string-to-number (or ,gv ""))
  8405. ,(string-to-number pv) ))))
  8406. (t `(member ,(downcase tag) tags-list)))
  8407. mm (if minus (list 'not mm) mm)
  8408. term rest)
  8409. (push mm tagsmatcher))
  8410. (push (if (> (length tagsmatcher) 1)
  8411. (cons 'and tagsmatcher)
  8412. (car tagsmatcher))
  8413. orlist)
  8414. (setq tagsmatcher nil))
  8415. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8416. (setq tagsmatcher
  8417. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8418. ;; Make the todo matcher
  8419. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8420. (setq todomatcher t)
  8421. (setq orterms (org-split-string todomatch "|") orlist nil)
  8422. (while (setq term (pop orterms))
  8423. (while (string-match re term)
  8424. (setq minus (and (match-end 1)
  8425. (equal (match-string 1 term) "-"))
  8426. kwd (match-string 2 term)
  8427. re-p (equal (string-to-char kwd) ?{)
  8428. term (substring term (match-end 0))
  8429. mm (if re-p
  8430. `(string-match ,(substring kwd 1 -1) todo)
  8431. (list 'equal 'todo kwd))
  8432. mm (if minus (list 'not mm) mm))
  8433. (push mm todomatcher))
  8434. (push (if (> (length todomatcher) 1)
  8435. (cons 'and todomatcher)
  8436. (car todomatcher))
  8437. orlist)
  8438. (setq todomatcher nil))
  8439. (setq todomatcher (if (> (length orlist) 1)
  8440. (cons 'or orlist) (car orlist))))
  8441. ;; Return the string and lisp forms of the matcher
  8442. (setq matcher (if todomatcher
  8443. (list 'and tagsmatcher todomatcher)
  8444. tagsmatcher))
  8445. (cons match0 matcher)))
  8446. (defun org-op-to-function (op &optional stringp)
  8447. "Turn an operator into the appropriate function."
  8448. (setq op
  8449. (cond
  8450. ((equal op "<" ) '(< string< org-time<))
  8451. ((equal op ">" ) '(> org-string> org-time>))
  8452. ((member op '("<=" "=<")) '(<= org-string<= org-time<=))
  8453. ((member op '(">=" "=>")) '(>= org-string>= org-time>=))
  8454. ((member op '("=" "==")) '(= string= org-time=))
  8455. ((member op '("<>" "!=")) '(org<> org-string<> org-time<>))))
  8456. (nth (if (eq stringp 'time) 2 (if stringp 1 0)) op))
  8457. (defun org<> (a b) (not (= a b)))
  8458. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8459. (defun org-string>= (a b) (not (string< a b)))
  8460. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8461. (defun org-string<> (a b) (not (string= a b)))
  8462. (defun org-time= (a b) (= (org-2ft a) (org-2ft b)))
  8463. (defun org-time< (a b) (< (org-2ft a) (org-2ft b)))
  8464. (defun org-time<= (a b) (<= (org-2ft a) (org-2ft b)))
  8465. (defun org-time> (a b) (> (org-2ft a) (org-2ft b)))
  8466. (defun org-time>= (a b) (>= (org-2ft a) (org-2ft b)))
  8467. (defun org-time<> (a b) (org<> (org-2ft a) (org-2ft b)))
  8468. (defun org-2ft (s)
  8469. "Convert S to a floating point time.
  8470. If S is already a number, just return it. If it is a string, parse
  8471. it as a time string and apply `float-time' to it. f S is nil, just return 0."
  8472. (cond
  8473. ((numberp s) s)
  8474. ((stringp s)
  8475. (condition-case nil
  8476. (float-time (apply 'encode-time (org-parse-time-string s)))
  8477. (error 0.)))
  8478. (t 0.)))
  8479. (defun org-time-today ()
  8480. "Time in seconds today at 0:00.
  8481. Returns the float number of seconds since the beginning of the
  8482. epoch to the beginning of today (00:00)."
  8483. (float-time (apply 'encode-time
  8484. (append '(0 0 0) (nthcdr 3 (decode-time))))))
  8485. (defun org-matcher-time (s)
  8486. (cond
  8487. ((string= s "<now>") (float-time))
  8488. ((string= s "<today>") (org-time-today))
  8489. ((string= s "<tomorrow>") (+ 86400.0 (org-time-today)))
  8490. ((string= s "<yesterday>") (- (org-time-today) 86400.0))
  8491. (t (org-2ft s))))
  8492. (defun org-match-any-p (re list)
  8493. "Does re match any element of list?"
  8494. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8495. (delq nil list))
  8496. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8497. (defvar org-tags-overlay (org-make-overlay 1 1))
  8498. (org-detach-overlay org-tags-overlay)
  8499. (defun org-get-local-tags-at (&optional pos)
  8500. "Get a list of tags defined in the current headline."
  8501. (org-get-tags-at pos 'local))
  8502. (defun org-get-local-tags ()
  8503. "Get a list of tags defined in the current headline."
  8504. (org-get-tags-at nil 'local))
  8505. (defun org-get-tags-at (&optional pos local)
  8506. "Get a list of all headline tags applicable at POS.
  8507. POS defaults to point. If tags are inherited, the list contains
  8508. the targets in the same sequence as the headlines appear, i.e.
  8509. the tags of the current headline come last.
  8510. When LOCAL is non-nil, only return tags from the current headline,
  8511. ignore inherited ones."
  8512. (interactive)
  8513. (let (tags ltags lastpos parent)
  8514. (save-excursion
  8515. (save-restriction
  8516. (widen)
  8517. (goto-char (or pos (point)))
  8518. (save-match-data
  8519. (catch 'done
  8520. (condition-case nil
  8521. (progn
  8522. (org-back-to-heading t)
  8523. (while (not (equal lastpos (point)))
  8524. (setq lastpos (point))
  8525. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8526. (setq ltags (org-split-string
  8527. (org-match-string-no-properties 1) ":"))
  8528. (when parent
  8529. (setq ltags (mapcar 'org-add-prop-inherited ltags)))
  8530. (setq tags (append
  8531. (if parent
  8532. (org-remove-uniherited-tags ltags)
  8533. ltags)
  8534. tags)))
  8535. (or org-use-tag-inheritance (throw 'done t))
  8536. (if local (throw 'done t))
  8537. (org-up-heading-all 1)
  8538. (setq parent t)))
  8539. (error nil)))))
  8540. (append (org-remove-uniherited-tags org-file-tags) tags))))
  8541. (defun org-add-prop-inherited (s)
  8542. (add-text-properties 0 (length s) '(inherited t) s)
  8543. s)
  8544. (defun org-toggle-tag (tag &optional onoff)
  8545. "Toggle the tag TAG for the current line.
  8546. If ONOFF is `on' or `off', don't toggle but set to this state."
  8547. (unless (org-on-heading-p t) (error "Not on headling"))
  8548. (let (res current)
  8549. (save-excursion
  8550. (beginning-of-line)
  8551. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8552. (point-at-eol) t)
  8553. (progn
  8554. (setq current (match-string 1))
  8555. (replace-match ""))
  8556. (setq current ""))
  8557. (setq current (nreverse (org-split-string current ":")))
  8558. (cond
  8559. ((eq onoff 'on)
  8560. (setq res t)
  8561. (or (member tag current) (push tag current)))
  8562. ((eq onoff 'off)
  8563. (or (not (member tag current)) (setq current (delete tag current))))
  8564. (t (if (member tag current)
  8565. (setq current (delete tag current))
  8566. (setq res t)
  8567. (push tag current))))
  8568. (end-of-line 1)
  8569. (if current
  8570. (progn
  8571. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8572. (org-set-tags nil t))
  8573. (delete-horizontal-space))
  8574. (run-hooks 'org-after-tags-change-hook))
  8575. res))
  8576. (defun org-align-tags-here (to-col)
  8577. ;; Assumes that this is a headline
  8578. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8579. (beginning-of-line 1)
  8580. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8581. (< pos (match-beginning 2)))
  8582. (progn
  8583. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8584. (goto-char (match-beginning 1))
  8585. (insert " ")
  8586. (delete-region (point) (1+ (match-beginning 2)))
  8587. (setq ncol (max (1+ (current-column))
  8588. (1+ col)
  8589. (if (> to-col 0)
  8590. to-col
  8591. (- (abs to-col) tags-l))))
  8592. (setq p (point))
  8593. (insert (make-string (- ncol (current-column)) ?\ ))
  8594. (setq ncol (current-column))
  8595. (when indent-tabs-mode (tabify p (point-at-eol)))
  8596. (org-move-to-column (min ncol col) t))
  8597. (goto-char pos))))
  8598. (defun org-set-tags-command (&optional arg just-align)
  8599. "Call the set-tags command for the current entry."
  8600. (interactive "P")
  8601. (if (org-on-heading-p)
  8602. (org-set-tags arg just-align)
  8603. (save-excursion
  8604. (org-back-to-heading t)
  8605. (org-set-tags arg just-align))))
  8606. (defun org-set-tags (&optional arg just-align)
  8607. "Set the tags for the current headline.
  8608. With prefix ARG, realign all tags in headings in the current buffer."
  8609. (interactive "P")
  8610. (let* ((re (concat "^" outline-regexp))
  8611. (current (org-get-tags-string))
  8612. (col (current-column))
  8613. (org-setting-tags t)
  8614. table current-tags inherited-tags ; computed below when needed
  8615. tags p0 c0 c1 rpl)
  8616. (if arg
  8617. (save-excursion
  8618. (goto-char (point-min))
  8619. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8620. (while (re-search-forward re nil t)
  8621. (org-set-tags nil t)
  8622. (end-of-line 1)))
  8623. (message "All tags realigned to column %d" org-tags-column))
  8624. (if just-align
  8625. (setq tags current)
  8626. ;; Get a new set of tags from the user
  8627. (save-excursion
  8628. (setq table (or org-tag-alist (org-get-buffer-tags))
  8629. org-last-tags-completion-table table
  8630. current-tags (org-split-string current ":")
  8631. inherited-tags (nreverse
  8632. (nthcdr (length current-tags)
  8633. (nreverse (org-get-tags-at))))
  8634. tags
  8635. (if (or (eq t org-use-fast-tag-selection)
  8636. (and org-use-fast-tag-selection
  8637. (delq nil (mapcar 'cdr table))))
  8638. (org-fast-tag-selection
  8639. current-tags inherited-tags table
  8640. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8641. (let ((org-add-colon-after-tag-completion t))
  8642. (org-trim
  8643. (org-without-partial-completion
  8644. (org-ido-completing-read "Tags: " 'org-tags-completion-function
  8645. nil nil current 'org-tags-history)))))))
  8646. (while (string-match "[-+&]+" tags)
  8647. ;; No boolean logic, just a list
  8648. (setq tags (replace-match ":" t t tags))))
  8649. (if (string-match "\\`[\t ]*\\'" tags)
  8650. (setq tags "")
  8651. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8652. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8653. ;; Insert new tags at the correct column
  8654. (beginning-of-line 1)
  8655. (cond
  8656. ((and (equal current "") (equal tags "")))
  8657. ((re-search-forward
  8658. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8659. (point-at-eol) t)
  8660. (if (equal tags "")
  8661. (setq rpl "")
  8662. (goto-char (match-beginning 0))
  8663. (setq c0 (current-column) p0 (point)
  8664. c1 (max (1+ c0) (if (> org-tags-column 0)
  8665. org-tags-column
  8666. (- (- org-tags-column) (length tags))))
  8667. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8668. (replace-match rpl t t)
  8669. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8670. tags)
  8671. (t (error "Tags alignment failed")))
  8672. (org-move-to-column col)
  8673. (unless just-align
  8674. (run-hooks 'org-after-tags-change-hook)))))
  8675. (defun org-change-tag-in-region (beg end tag off)
  8676. "Add or remove TAG for each entry in the region.
  8677. This works in the agenda, and also in an org-mode buffer."
  8678. (interactive
  8679. (list (region-beginning) (region-end)
  8680. (let ((org-last-tags-completion-table
  8681. (if (org-mode-p)
  8682. (org-get-buffer-tags)
  8683. (org-global-tags-completion-table))))
  8684. (org-ido-completing-read
  8685. "Tag: " 'org-tags-completion-function nil nil nil
  8686. 'org-tags-history))
  8687. (progn
  8688. (message "[s]et or [r]emove? ")
  8689. (equal (read-char-exclusive) ?r))))
  8690. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8691. (let ((agendap (equal major-mode 'org-agenda-mode))
  8692. l1 l2 m buf pos newhead (cnt 0))
  8693. (goto-char end)
  8694. (setq l2 (1- (org-current-line)))
  8695. (goto-char beg)
  8696. (setq l1 (org-current-line))
  8697. (loop for l from l1 to l2 do
  8698. (goto-line l)
  8699. (setq m (get-text-property (point) 'org-hd-marker))
  8700. (when (or (and (org-mode-p) (org-on-heading-p))
  8701. (and agendap m))
  8702. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8703. pos (if agendap m (point)))
  8704. (with-current-buffer buf
  8705. (save-excursion
  8706. (save-restriction
  8707. (goto-char pos)
  8708. (setq cnt (1+ cnt))
  8709. (org-toggle-tag tag (if off 'off 'on))
  8710. (setq newhead (org-get-heading)))))
  8711. (and agendap (org-agenda-change-all-lines newhead m))))
  8712. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8713. (defun org-tags-completion-function (string predicate &optional flag)
  8714. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8715. (confirm (lambda (x) (stringp (car x)))))
  8716. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8717. (setq s1 (match-string 1 string)
  8718. s2 (match-string 2 string))
  8719. (setq s1 "" s2 string))
  8720. (cond
  8721. ((eq flag nil)
  8722. ;; try completion
  8723. (setq rtn (try-completion s2 ctable confirm))
  8724. (if (stringp rtn)
  8725. (setq rtn
  8726. (concat s1 s2 (substring rtn (length s2))
  8727. (if (and org-add-colon-after-tag-completion
  8728. (assoc rtn ctable))
  8729. ":" ""))))
  8730. rtn)
  8731. ((eq flag t)
  8732. ;; all-completions
  8733. (all-completions s2 ctable confirm)
  8734. )
  8735. ((eq flag 'lambda)
  8736. ;; exact match?
  8737. (assoc s2 ctable)))
  8738. ))
  8739. (defun org-fast-tag-insert (kwd tags face &optional end)
  8740. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8741. (insert (format "%-12s" (concat kwd ":"))
  8742. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8743. (or end "")))
  8744. (defun org-fast-tag-show-exit (flag)
  8745. (save-excursion
  8746. (goto-line 3)
  8747. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8748. (replace-match ""))
  8749. (when flag
  8750. (end-of-line 1)
  8751. (org-move-to-column (- (window-width) 19) t)
  8752. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8753. (defun org-set-current-tags-overlay (current prefix)
  8754. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8755. (if (featurep 'xemacs)
  8756. (org-overlay-display org-tags-overlay (concat prefix s)
  8757. 'secondary-selection)
  8758. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8759. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8760. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8761. "Fast tag selection with single keys.
  8762. CURRENT is the current list of tags in the headline, INHERITED is the
  8763. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8764. possibly with grouping information. TODO-TABLE is a similar table with
  8765. TODO keywords, should these have keys assigned to them.
  8766. If the keys are nil, a-z are automatically assigned.
  8767. Returns the new tags string, or nil to not change the current settings."
  8768. (let* ((fulltable (append table todo-table))
  8769. (maxlen (apply 'max (mapcar
  8770. (lambda (x)
  8771. (if (stringp (car x)) (string-width (car x)) 0))
  8772. fulltable)))
  8773. (buf (current-buffer))
  8774. (expert (eq org-fast-tag-selection-single-key 'expert))
  8775. (buffer-tags nil)
  8776. (fwidth (+ maxlen 3 1 3))
  8777. (ncol (/ (- (window-width) 4) fwidth))
  8778. (i-face 'org-done)
  8779. (c-face 'org-todo)
  8780. tg cnt e c char c1 c2 ntable tbl rtn
  8781. ov-start ov-end ov-prefix
  8782. (exit-after-next org-fast-tag-selection-single-key)
  8783. (done-keywords org-done-keywords)
  8784. groups ingroup)
  8785. (save-excursion
  8786. (beginning-of-line 1)
  8787. (if (looking-at
  8788. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8789. (setq ov-start (match-beginning 1)
  8790. ov-end (match-end 1)
  8791. ov-prefix "")
  8792. (setq ov-start (1- (point-at-eol))
  8793. ov-end (1+ ov-start))
  8794. (skip-chars-forward "^\n\r")
  8795. (setq ov-prefix
  8796. (concat
  8797. (buffer-substring (1- (point)) (point))
  8798. (if (> (current-column) org-tags-column)
  8799. " "
  8800. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8801. (org-move-overlay org-tags-overlay ov-start ov-end)
  8802. (save-window-excursion
  8803. (if expert
  8804. (set-buffer (get-buffer-create " *Org tags*"))
  8805. (delete-other-windows)
  8806. (split-window-vertically)
  8807. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8808. (erase-buffer)
  8809. (org-set-local 'org-done-keywords done-keywords)
  8810. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8811. (org-fast-tag-insert "Current" current c-face "\n\n")
  8812. (org-fast-tag-show-exit exit-after-next)
  8813. (org-set-current-tags-overlay current ov-prefix)
  8814. (setq tbl fulltable char ?a cnt 0)
  8815. (while (setq e (pop tbl))
  8816. (cond
  8817. ((equal e '(:startgroup))
  8818. (push '() groups) (setq ingroup t)
  8819. (when (not (= cnt 0))
  8820. (setq cnt 0)
  8821. (insert "\n"))
  8822. (insert "{ "))
  8823. ((equal e '(:endgroup))
  8824. (setq ingroup nil cnt 0)
  8825. (insert "}\n"))
  8826. (t
  8827. (setq tg (car e) c2 nil)
  8828. (if (cdr e)
  8829. (setq c (cdr e))
  8830. ;; automatically assign a character.
  8831. (setq c1 (string-to-char
  8832. (downcase (substring
  8833. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8834. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8835. (while (or (rassoc char ntable) (rassoc char table))
  8836. (setq char (1+ char)))
  8837. (setq c2 c1))
  8838. (setq c (or c2 char)))
  8839. (if ingroup (push tg (car groups)))
  8840. (setq tg (org-add-props tg nil 'face
  8841. (cond
  8842. ((not (assoc tg table))
  8843. (org-get-todo-face tg))
  8844. ((member tg current) c-face)
  8845. ((member tg inherited) i-face)
  8846. (t nil))))
  8847. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8848. (insert "[" c "] " tg (make-string
  8849. (- fwidth 4 (length tg)) ?\ ))
  8850. (push (cons tg c) ntable)
  8851. (when (= (setq cnt (1+ cnt)) ncol)
  8852. (insert "\n")
  8853. (if ingroup (insert " "))
  8854. (setq cnt 0)))))
  8855. (setq ntable (nreverse ntable))
  8856. (insert "\n")
  8857. (goto-char (point-min))
  8858. (if (not expert) (org-fit-window-to-buffer))
  8859. (setq rtn
  8860. (catch 'exit
  8861. (while t
  8862. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8863. (if groups " [!] no groups" " [!]groups")
  8864. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8865. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8866. (cond
  8867. ((= c ?\r) (throw 'exit t))
  8868. ((= c ?!)
  8869. (setq groups (not groups))
  8870. (goto-char (point-min))
  8871. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8872. ((= c ?\C-c)
  8873. (if (not expert)
  8874. (org-fast-tag-show-exit
  8875. (setq exit-after-next (not exit-after-next)))
  8876. (setq expert nil)
  8877. (delete-other-windows)
  8878. (split-window-vertically)
  8879. (org-switch-to-buffer-other-window " *Org tags*")
  8880. (org-fit-window-to-buffer)))
  8881. ((or (= c ?\C-g)
  8882. (and (= c ?q) (not (rassoc c ntable))))
  8883. (org-detach-overlay org-tags-overlay)
  8884. (setq quit-flag t))
  8885. ((= c ?\ )
  8886. (setq current nil)
  8887. (if exit-after-next (setq exit-after-next 'now)))
  8888. ((= c ?\t)
  8889. (condition-case nil
  8890. (setq tg (org-ido-completing-read
  8891. "Tag: "
  8892. (or buffer-tags
  8893. (with-current-buffer buf
  8894. (org-get-buffer-tags)))))
  8895. (quit (setq tg "")))
  8896. (when (string-match "\\S-" tg)
  8897. (add-to-list 'buffer-tags (list tg))
  8898. (if (member tg current)
  8899. (setq current (delete tg current))
  8900. (push tg current)))
  8901. (if exit-after-next (setq exit-after-next 'now)))
  8902. ((setq e (rassoc c todo-table) tg (car e))
  8903. (with-current-buffer buf
  8904. (save-excursion (org-todo tg)))
  8905. (if exit-after-next (setq exit-after-next 'now)))
  8906. ((setq e (rassoc c ntable) tg (car e))
  8907. (if (member tg current)
  8908. (setq current (delete tg current))
  8909. (loop for g in groups do
  8910. (if (member tg g)
  8911. (mapc (lambda (x)
  8912. (setq current (delete x current)))
  8913. g)))
  8914. (push tg current))
  8915. (if exit-after-next (setq exit-after-next 'now))))
  8916. ;; Create a sorted list
  8917. (setq current
  8918. (sort current
  8919. (lambda (a b)
  8920. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8921. (if (eq exit-after-next 'now) (throw 'exit t))
  8922. (goto-char (point-min))
  8923. (beginning-of-line 2)
  8924. (delete-region (point) (point-at-eol))
  8925. (org-fast-tag-insert "Current" current c-face)
  8926. (org-set-current-tags-overlay current ov-prefix)
  8927. (while (re-search-forward
  8928. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8929. (setq tg (match-string 1))
  8930. (add-text-properties
  8931. (match-beginning 1) (match-end 1)
  8932. (list 'face
  8933. (cond
  8934. ((member tg current) c-face)
  8935. ((member tg inherited) i-face)
  8936. (t (get-text-property (match-beginning 1) 'face))))))
  8937. (goto-char (point-min)))))
  8938. (org-detach-overlay org-tags-overlay)
  8939. (if rtn
  8940. (mapconcat 'identity current ":")
  8941. nil))))
  8942. (defun org-get-tags-string ()
  8943. "Get the TAGS string in the current headline."
  8944. (unless (org-on-heading-p t)
  8945. (error "Not on a heading"))
  8946. (save-excursion
  8947. (beginning-of-line 1)
  8948. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8949. (org-match-string-no-properties 1)
  8950. "")))
  8951. (defun org-get-tags ()
  8952. "Get the list of tags specified in the current headline."
  8953. (org-split-string (org-get-tags-string) ":"))
  8954. (defun org-get-buffer-tags ()
  8955. "Get a table of all tags used in the buffer, for completion."
  8956. (let (tags)
  8957. (save-excursion
  8958. (goto-char (point-min))
  8959. (while (re-search-forward
  8960. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8961. (when (equal (char-after (point-at-bol 0)) ?*)
  8962. (mapc (lambda (x) (add-to-list 'tags x))
  8963. (org-split-string (org-match-string-no-properties 1) ":")))))
  8964. (mapcar 'list tags)))
  8965. ;;;; The mapping API
  8966. ;;;###autoload
  8967. (defun org-map-entries (func &optional match scope &rest skip)
  8968. "Call FUNC at each headline selected by MATCH in SCOPE.
  8969. FUNC is a function or a lisp form. The function will be called without
  8970. arguments, with the cursor positioned at the beginning of the headline.
  8971. The return values of all calls to the function will be collected and
  8972. returned as a list.
  8973. MATCH is a tags/property/todo match as it is used in the agenda tags view.
  8974. Only headlines that are matched by this query will be considered during
  8975. the iteration. When MATCH is nil or t, all headlines will be
  8976. visited by the iteration.
  8977. SCOPE determines the scope of this command. It can be any of:
  8978. nil The current buffer, respecting the restriction if any
  8979. tree The subtree started with the entry at point
  8980. file The current buffer, without restriction
  8981. file-with-archives
  8982. The current buffer, and any archives associated with it
  8983. agenda All agenda files
  8984. agenda-with-archives
  8985. All agenda files with any archive files associated with them
  8986. \(file1 file2 ...)
  8987. If this is a list, all files in the list will be scanned
  8988. The remaining args are treated as settings for the skipping facilities of
  8989. the scanner. The following items can be given here:
  8990. archive skip trees with the archive tag.
  8991. comment skip trees with the COMMENT keyword
  8992. function or Emacs Lisp form:
  8993. will be used as value for `org-agenda-skip-function', so whenever
  8994. the the function returns t, FUNC will not be called for that
  8995. entry and search will continue from the point where the
  8996. function leaves it."
  8997. (let* ((org-agenda-archives-mode nil) ; just to make sure
  8998. (org-agenda-skip-archived-trees (memq 'archive skip))
  8999. (org-agenda-skip-comment-trees (memq 'comment skip))
  9000. (org-agenda-skip-function
  9001. (car (org-delete-all '(comment archive) skip)))
  9002. (org-tags-match-list-sublevels t)
  9003. matcher pos file
  9004. org-todo-keywords-for-agenda
  9005. org-done-keywords-for-agenda
  9006. org-todo-keyword-alist-for-agenda
  9007. org-tag-alist-for-agenda)
  9008. (cond
  9009. ((eq match t) (setq matcher t))
  9010. ((eq match nil) (setq matcher t))
  9011. (t (setq matcher (if match (org-make-tags-matcher match) t))))
  9012. (when (eq scope 'tree)
  9013. (org-back-to-heading t)
  9014. (org-narrow-to-subtree)
  9015. (setq scope nil))
  9016. (if (not scope)
  9017. (progn
  9018. (org-prepare-agenda-buffers
  9019. (list (buffer-file-name (current-buffer))))
  9020. (org-scan-tags func matcher))
  9021. ;; Get the right scope
  9022. (setq pos (point))
  9023. (cond
  9024. ((and scope (listp scope) (symbolp (car scope)))
  9025. (setq scope (eval scope)))
  9026. ((eq scope 'agenda)
  9027. (setq scope (org-agenda-files t)))
  9028. ((eq scope 'agenda-with-archives)
  9029. (setq scope (org-agenda-files t))
  9030. (setq scope (org-add-archive-files scope)))
  9031. ((eq scope 'file)
  9032. (setq scope (list (buffer-file-name))))
  9033. ((eq scope 'file-with-archives)
  9034. (setq scope (org-add-archive-files (list (buffer-file-name))))))
  9035. (org-prepare-agenda-buffers scope)
  9036. (while (setq file (pop scope))
  9037. (with-current-buffer (org-find-base-buffer-visiting file)
  9038. (save-excursion
  9039. (save-restriction
  9040. (widen)
  9041. (goto-char (point-min))
  9042. (org-scan-tags func matcher))))))))
  9043. ;;;; Properties
  9044. ;;; Setting and retrieving properties
  9045. (defconst org-special-properties
  9046. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "CLOSED" "PRIORITY"
  9047. "TIMESTAMP" "TIMESTAMP_IA")
  9048. "The special properties valid in Org-mode.
  9049. These are properties that are not defined in the property drawer,
  9050. but in some other way.")
  9051. (defconst org-default-properties
  9052. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  9053. "LOCATION" "LOGGING" "COLUMNS" "VISIBILITY"
  9054. "TABLE_EXPORT_FORMAT" "TABLE_EXPORT_FILE"
  9055. "EXPORT_FILE_NAME" "EXPORT_TITLE")
  9056. "Some properties that are used by Org-mode for various purposes.
  9057. Being in this list makes sure that they are offered for completion.")
  9058. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  9059. "Regular expression matching the first line of a property drawer.")
  9060. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  9061. "Regular expression matching the first line of a property drawer.")
  9062. (defconst org-clock-drawer-start-re "^[ \t]*:CLOCK:[ \t]*$"
  9063. "Regular expression matching the first line of a property drawer.")
  9064. (defconst org-clock-drawer-end-re "^[ \t]*:END:[ \t]*$"
  9065. "Regular expression matching the first line of a property drawer.")
  9066. (defconst org-property-drawer-re
  9067. (concat "\\(" org-property-start-re "\\)[^\000]*\\("
  9068. org-property-end-re "\\)\n?")
  9069. "Matches an entire property drawer.")
  9070. (defconst org-clock-drawer-re
  9071. (concat "\\(" org-clock-drawer-start-re "\\)[^\000]*\\("
  9072. org-property-end-re "\\)\n?")
  9073. "Matches an entire clock drawer.")
  9074. (defun org-property-action ()
  9075. "Do an action on properties."
  9076. (interactive)
  9077. (let (c)
  9078. (org-at-property-p)
  9079. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  9080. (setq c (read-char-exclusive))
  9081. (cond
  9082. ((equal c ?s)
  9083. (call-interactively 'org-set-property))
  9084. ((equal c ?d)
  9085. (call-interactively 'org-delete-property))
  9086. ((equal c ?D)
  9087. (call-interactively 'org-delete-property-globally))
  9088. ((equal c ?c)
  9089. (call-interactively 'org-compute-property-at-point))
  9090. (t (error "No such property action %c" c)))))
  9091. (defun org-at-property-p ()
  9092. "Is the cursor in a property line?"
  9093. ;; FIXME: Does not check if we are actually in the drawer.
  9094. ;; FIXME: also returns true on any drawers.....
  9095. ;; This is used by C-c C-c for property action.
  9096. (save-excursion
  9097. (beginning-of-line 1)
  9098. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  9099. (defun org-get-property-block (&optional beg end force)
  9100. "Return the (beg . end) range of the body of the property drawer.
  9101. BEG and END can be beginning and end of subtree, if not given
  9102. they will be found.
  9103. If the drawer does not exist and FORCE is non-nil, create the drawer."
  9104. (catch 'exit
  9105. (save-excursion
  9106. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  9107. (end (or end (progn (outline-next-heading) (point)))))
  9108. (goto-char beg)
  9109. (if (re-search-forward org-property-start-re end t)
  9110. (setq beg (1+ (match-end 0)))
  9111. (if force
  9112. (save-excursion
  9113. (org-insert-property-drawer)
  9114. (setq end (progn (outline-next-heading) (point))))
  9115. (throw 'exit nil))
  9116. (goto-char beg)
  9117. (if (re-search-forward org-property-start-re end t)
  9118. (setq beg (1+ (match-end 0)))))
  9119. (if (re-search-forward org-property-end-re end t)
  9120. (setq end (match-beginning 0))
  9121. (or force (throw 'exit nil))
  9122. (goto-char beg)
  9123. (setq end beg)
  9124. (org-indent-line-function)
  9125. (insert ":END:\n"))
  9126. (cons beg end)))))
  9127. (defun org-entry-properties (&optional pom which)
  9128. "Get all properties of the entry at point-or-marker POM.
  9129. This includes the TODO keyword, the tags, time strings for deadline,
  9130. scheduled, and clocking, and any additional properties defined in the
  9131. entry. The return value is an alist, keys may occur multiple times
  9132. if the property key was used several times.
  9133. POM may also be nil, in which case the current entry is used.
  9134. If WHICH is nil or `all', get all properties. If WHICH is
  9135. `special' or `standard', only get that subclass."
  9136. (setq which (or which 'all))
  9137. (org-with-point-at pom
  9138. (let ((clockstr (substring org-clock-string 0 -1))
  9139. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  9140. beg end range props sum-props key value string clocksum)
  9141. (save-excursion
  9142. (when (condition-case nil (org-back-to-heading t) (error nil))
  9143. (setq beg (point))
  9144. (setq sum-props (get-text-property (point) 'org-summaries))
  9145. (setq clocksum (get-text-property (point) :org-clock-minutes))
  9146. (outline-next-heading)
  9147. (setq end (point))
  9148. (when (memq which '(all special))
  9149. ;; Get the special properties, like TODO and tags
  9150. (goto-char beg)
  9151. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  9152. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  9153. (when (looking-at org-priority-regexp)
  9154. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  9155. (when (and (setq value (org-get-tags-string))
  9156. (string-match "\\S-" value))
  9157. (push (cons "TAGS" value) props))
  9158. (when (setq value (org-get-tags-at))
  9159. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  9160. props))
  9161. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  9162. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  9163. string (if (equal key clockstr)
  9164. (org-no-properties
  9165. (org-trim
  9166. (buffer-substring
  9167. (match-beginning 3) (goto-char (point-at-eol)))))
  9168. (substring (org-match-string-no-properties 3) 1 -1)))
  9169. (unless key
  9170. (if (= (char-after (match-beginning 3)) ?\[)
  9171. (setq key "TIMESTAMP_IA")
  9172. (setq key "TIMESTAMP")))
  9173. (when (or (equal key clockstr) (not (assoc key props)))
  9174. (push (cons key string) props)))
  9175. )
  9176. (when (memq which '(all standard))
  9177. ;; Get the standard properties, like :PORP: ...
  9178. (setq range (org-get-property-block beg end))
  9179. (when range
  9180. (goto-char (car range))
  9181. (while (re-search-forward
  9182. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  9183. (cdr range) t)
  9184. (setq key (org-match-string-no-properties 1)
  9185. value (org-trim (or (org-match-string-no-properties 2) "")))
  9186. (unless (member key excluded)
  9187. (push (cons key (or value "")) props)))))
  9188. (if clocksum
  9189. (push (cons "CLOCKSUM"
  9190. (org-columns-number-to-string (/ (float clocksum) 60.)
  9191. 'add_times))
  9192. props))
  9193. (unless (assoc "CATEGORY" props)
  9194. (setq value (or (org-get-category)
  9195. (progn (org-refresh-category-properties)
  9196. (org-get-category))))
  9197. (push (cons "CATEGORY" value) props))
  9198. (append sum-props (nreverse props)))))))
  9199. (defun org-entry-get (pom property &optional inherit)
  9200. "Get value of PROPERTY for entry at point-or-marker POM.
  9201. If INHERIT is non-nil and the entry does not have the property,
  9202. then also check higher levels of the hierarchy.
  9203. If INHERIT is the symbol `selective', use inheritance only if the setting
  9204. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  9205. If the property is present but empty, the return value is the empty string.
  9206. If the property is not present at all, nil is returned."
  9207. (org-with-point-at pom
  9208. (if (and inherit (if (eq inherit 'selective)
  9209. (org-property-inherit-p property)
  9210. t))
  9211. (org-entry-get-with-inheritance property)
  9212. (if (member property org-special-properties)
  9213. ;; We need a special property. Use brute force, get all properties.
  9214. (cdr (assoc property (org-entry-properties nil 'special)))
  9215. (let ((range (org-get-property-block)))
  9216. (if (and range
  9217. (goto-char (car range))
  9218. (re-search-forward
  9219. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)?")
  9220. (cdr range) t))
  9221. ;; Found the property, return it.
  9222. (if (match-end 1)
  9223. (org-match-string-no-properties 1)
  9224. "")))))))
  9225. (defun org-property-or-variable-value (var &optional inherit)
  9226. "Check if there is a property fixing the value of VAR.
  9227. If yes, return this value. If not, return the current value of the variable."
  9228. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  9229. (if (and prop (stringp prop) (string-match "\\S-" prop))
  9230. (read prop)
  9231. (symbol-value var))))
  9232. (defun org-entry-delete (pom property)
  9233. "Delete the property PROPERTY from entry at point-or-marker POM."
  9234. (org-with-point-at pom
  9235. (if (member property org-special-properties)
  9236. nil ; cannot delete these properties.
  9237. (let ((range (org-get-property-block)))
  9238. (if (and range
  9239. (goto-char (car range))
  9240. (re-search-forward
  9241. (concat "^[ \t]*:" property ":[ \t]*\\(.*[^ \t\r\n\f\v]\\)")
  9242. (cdr range) t))
  9243. (progn
  9244. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  9245. t)
  9246. nil)))))
  9247. ;; Multi-values properties are properties that contain multiple values
  9248. ;; These values are assumed to be single words, separated by whitespace.
  9249. (defun org-entry-add-to-multivalued-property (pom property value)
  9250. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  9251. (let* ((old (org-entry-get pom property))
  9252. (values (and old (org-split-string old "[ \t]"))))
  9253. (setq value (org-entry-protect-space value))
  9254. (unless (member value values)
  9255. (setq values (cons value values))
  9256. (org-entry-put pom property
  9257. (mapconcat 'identity values " ")))))
  9258. (defun org-entry-remove-from-multivalued-property (pom property value)
  9259. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  9260. (let* ((old (org-entry-get pom property))
  9261. (values (and old (org-split-string old "[ \t]"))))
  9262. (setq value (org-entry-protect-space value))
  9263. (when (member value values)
  9264. (setq values (delete value values))
  9265. (org-entry-put pom property
  9266. (mapconcat 'identity values " ")))))
  9267. (defun org-entry-member-in-multivalued-property (pom property value)
  9268. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  9269. (let* ((old (org-entry-get pom property))
  9270. (values (and old (org-split-string old "[ \t]"))))
  9271. (setq value (org-entry-protect-space value))
  9272. (member value values)))
  9273. (defun org-entry-get-multivalued-property (pom property)
  9274. "Return a list of values in a multivalued property."
  9275. (let* ((value (org-entry-get pom property))
  9276. (values (and value (org-split-string value "[ \t]"))))
  9277. (mapcar 'org-entry-restore-space values)))
  9278. (defun org-entry-put-multivalued-property (pom property &rest values)
  9279. "Set multivalued PROPERTY at point-or-marker POM to VALUES.
  9280. VALUES should be a list of strings. Spaces will be protected."
  9281. (org-entry-put pom property
  9282. (mapconcat 'org-entry-protect-space values " "))
  9283. (let* ((value (org-entry-get pom property))
  9284. (values (and value (org-split-string value "[ \t]"))))
  9285. (mapcar 'org-entry-restore-space values)))
  9286. (defun org-entry-protect-space (s)
  9287. "Protect spaces and newline in string S."
  9288. (while (string-match " " s)
  9289. (setq s (replace-match "%20" t t s)))
  9290. (while (string-match "\n" s)
  9291. (setq s (replace-match "%0A" t t s)))
  9292. s)
  9293. (defun org-entry-restore-space (s)
  9294. "Restore spaces and newline in string S."
  9295. (while (string-match "%20" s)
  9296. (setq s (replace-match " " t t s)))
  9297. (while (string-match "%0A" s)
  9298. (setq s (replace-match "\n" t t s)))
  9299. s)
  9300. (defvar org-entry-property-inherited-from (make-marker)
  9301. "Marker pointing to the entry from where a proerty was inherited.
  9302. Each call to `org-entry-get-with-inheritance' will set this marker to the
  9303. location of the entry where the inheriance search matched. If there was
  9304. no match, the marker will point nowhere.
  9305. Note that also `org-entry-get' calls this function, if the INHERIT flag
  9306. is set.")
  9307. (defun org-entry-get-with-inheritance (property)
  9308. "Get entry property, and search higher levels if not present."
  9309. (move-marker org-entry-property-inherited-from nil)
  9310. (let (tmp)
  9311. (save-excursion
  9312. (save-restriction
  9313. (widen)
  9314. (catch 'ex
  9315. (while t
  9316. (when (setq tmp (org-entry-get nil property))
  9317. (org-back-to-heading t)
  9318. (move-marker org-entry-property-inherited-from (point))
  9319. (throw 'ex tmp))
  9320. (or (org-up-heading-safe) (throw 'ex nil)))))
  9321. (or tmp
  9322. (cdr (assoc property org-file-properties))
  9323. (cdr (assoc property org-global-properties))
  9324. (cdr (assoc property org-global-properties-fixed))))))
  9325. (defun org-entry-put (pom property value)
  9326. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  9327. (org-with-point-at pom
  9328. (org-back-to-heading t)
  9329. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  9330. range)
  9331. (cond
  9332. ((equal property "TODO")
  9333. (when (and (stringp value) (string-match "\\S-" value)
  9334. (not (member value org-todo-keywords-1)))
  9335. (error "\"%s\" is not a valid TODO state" value))
  9336. (if (or (not value)
  9337. (not (string-match "\\S-" value)))
  9338. (setq value 'none))
  9339. (org-todo value)
  9340. (org-set-tags nil 'align))
  9341. ((equal property "PRIORITY")
  9342. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  9343. (string-to-char value) ?\ ))
  9344. (org-set-tags nil 'align))
  9345. ((equal property "SCHEDULED")
  9346. (if (re-search-forward org-scheduled-time-regexp end t)
  9347. (cond
  9348. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9349. ((eq value 'later) (org-timestamp-change 1 'day))
  9350. (t (call-interactively 'org-schedule)))
  9351. (call-interactively 'org-schedule)))
  9352. ((equal property "DEADLINE")
  9353. (if (re-search-forward org-deadline-time-regexp end t)
  9354. (cond
  9355. ((eq value 'earlier) (org-timestamp-change -1 'day))
  9356. ((eq value 'later) (org-timestamp-change 1 'day))
  9357. (t (call-interactively 'org-deadline)))
  9358. (call-interactively 'org-deadline)))
  9359. ((member property org-special-properties)
  9360. (error "The %s property can not yet be set with `org-entry-put'"
  9361. property))
  9362. (t ; a non-special property
  9363. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  9364. (setq range (org-get-property-block beg end 'force))
  9365. (goto-char (car range))
  9366. (if (re-search-forward
  9367. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  9368. (progn
  9369. (delete-region (match-beginning 1) (match-end 1))
  9370. (goto-char (match-beginning 1)))
  9371. (goto-char (cdr range))
  9372. (insert "\n")
  9373. (backward-char 1)
  9374. (org-indent-line-function)
  9375. (insert ":" property ":"))
  9376. (and value (insert " " value))
  9377. (org-indent-line-function)))))))
  9378. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  9379. "Get all property keys in the current buffer.
  9380. With INCLUDE-SPECIALS, also list the special properties that relect things
  9381. like tags and TODO state.
  9382. With INCLUDE-DEFAULTS, also include properties that has special meaning
  9383. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  9384. With INCLUDE-COLUMNS, also include property names given in COLUMN
  9385. formats in the current buffer."
  9386. (let (rtn range cfmt cols s p)
  9387. (save-excursion
  9388. (save-restriction
  9389. (widen)
  9390. (goto-char (point-min))
  9391. (while (re-search-forward org-property-start-re nil t)
  9392. (setq range (org-get-property-block))
  9393. (goto-char (car range))
  9394. (while (re-search-forward
  9395. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  9396. (cdr range) t)
  9397. (add-to-list 'rtn (org-match-string-no-properties 1)))
  9398. (outline-next-heading))))
  9399. (when include-specials
  9400. (setq rtn (append org-special-properties rtn)))
  9401. (when include-defaults
  9402. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  9403. (when include-columns
  9404. (save-excursion
  9405. (save-restriction
  9406. (widen)
  9407. (goto-char (point-min))
  9408. (while (re-search-forward
  9409. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  9410. nil t)
  9411. (setq cfmt (match-string 2) s 0)
  9412. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  9413. cfmt s)
  9414. (setq s (match-end 0)
  9415. p (match-string 1 cfmt))
  9416. (unless (or (equal p "ITEM")
  9417. (member p org-special-properties))
  9418. (add-to-list 'rtn (match-string 1 cfmt))))))))
  9419. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  9420. (defun org-property-values (key)
  9421. "Return a list of all values of property KEY."
  9422. (save-excursion
  9423. (save-restriction
  9424. (widen)
  9425. (goto-char (point-min))
  9426. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9427. values)
  9428. (while (re-search-forward re nil t)
  9429. (add-to-list 'values (org-trim (match-string 1))))
  9430. (delete "" values)))))
  9431. (defun org-insert-property-drawer ()
  9432. "Insert a property drawer into the current entry."
  9433. (interactive)
  9434. (org-back-to-heading t)
  9435. (looking-at outline-regexp)
  9436. (let ((indent (- (match-end 0)(match-beginning 0)))
  9437. (beg (point))
  9438. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9439. end hiddenp)
  9440. (outline-next-heading)
  9441. (setq end (point))
  9442. (goto-char beg)
  9443. (while (re-search-forward re end t))
  9444. (setq hiddenp (org-invisible-p))
  9445. (end-of-line 1)
  9446. (and (equal (char-after) ?\n) (forward-char 1))
  9447. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9448. (beginning-of-line 2))
  9449. (org-skip-over-state-notes)
  9450. (skip-chars-backward " \t\n\r")
  9451. (if (eq (char-before) ?*) (forward-char 1))
  9452. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9453. (beginning-of-line 0)
  9454. (org-indent-to-column indent)
  9455. (beginning-of-line 2)
  9456. (org-indent-to-column indent)
  9457. (beginning-of-line 0)
  9458. (if hiddenp
  9459. (save-excursion
  9460. (org-back-to-heading t)
  9461. (hide-entry))
  9462. (org-flag-drawer t))))
  9463. (defun org-set-property (property value)
  9464. "In the current entry, set PROPERTY to VALUE.
  9465. When called interactively, this will prompt for a property name, offering
  9466. completion on existing and default properties. And then it will prompt
  9467. for a value, offering competion either on allowed values (via an inherited
  9468. xxx_ALL property) or on existing values in other instances of this property
  9469. in the current file."
  9470. (interactive
  9471. (let* ((completion-ignore-case t)
  9472. (keys (org-buffer-property-keys nil t t))
  9473. (prop0 (org-ido-completing-read "Property: " (mapcar 'list keys)))
  9474. (prop (if (member prop0 keys)
  9475. prop0
  9476. (or (cdr (assoc (downcase prop0)
  9477. (mapcar (lambda (x) (cons (downcase x) x))
  9478. keys)))
  9479. prop0)))
  9480. (cur (org-entry-get nil prop))
  9481. (allowed (org-property-get-allowed-values nil prop 'table))
  9482. (existing (mapcar 'list (org-property-values prop)))
  9483. (val (if allowed
  9484. (org-completing-read "Value: " allowed nil 'req-match)
  9485. (org-completing-read
  9486. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9487. (concat "[" cur "]") "")
  9488. ": ")
  9489. existing nil nil "" nil cur))))
  9490. (list prop (if (equal val "") cur val))))
  9491. (unless (equal (org-entry-get nil property) value)
  9492. (org-entry-put nil property value)))
  9493. (defun org-delete-property (property)
  9494. "In the current entry, delete PROPERTY."
  9495. (interactive
  9496. (let* ((completion-ignore-case t)
  9497. (prop (org-ido-completing-read
  9498. "Property: " (org-entry-properties nil 'standard))))
  9499. (list prop)))
  9500. (message "Property %s %s" property
  9501. (if (org-entry-delete nil property)
  9502. "deleted"
  9503. "was not present in the entry")))
  9504. (defun org-delete-property-globally (property)
  9505. "Remove PROPERTY globally, from all entries."
  9506. (interactive
  9507. (let* ((completion-ignore-case t)
  9508. (prop (org-ido-completing-read
  9509. "Globally remove property: "
  9510. (mapcar 'list (org-buffer-property-keys)))))
  9511. (list prop)))
  9512. (save-excursion
  9513. (save-restriction
  9514. (widen)
  9515. (goto-char (point-min))
  9516. (let ((cnt 0))
  9517. (while (re-search-forward
  9518. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9519. nil t)
  9520. (setq cnt (1+ cnt))
  9521. (replace-match ""))
  9522. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9523. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9524. (defun org-compute-property-at-point ()
  9525. "Compute the property at point.
  9526. This looks for an enclosing column format, extracts the operator and
  9527. then applies it to the proerty in the column format's scope."
  9528. (interactive)
  9529. (unless (org-at-property-p)
  9530. (error "Not at a property"))
  9531. (let ((prop (org-match-string-no-properties 2)))
  9532. (org-columns-get-format-and-top-level)
  9533. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9534. (error "No operator defined for property %s" prop))
  9535. (org-columns-compute prop)))
  9536. (defun org-property-get-allowed-values (pom property &optional table)
  9537. "Get allowed values for the property PROPERTY.
  9538. When TABLE is non-nil, return an alist that can directly be used for
  9539. completion."
  9540. (let (vals)
  9541. (cond
  9542. ((equal property "TODO")
  9543. (setq vals (org-with-point-at pom
  9544. (append org-todo-keywords-1 '("")))))
  9545. ((equal property "PRIORITY")
  9546. (let ((n org-lowest-priority))
  9547. (while (>= n org-highest-priority)
  9548. (push (char-to-string n) vals)
  9549. (setq n (1- n)))))
  9550. ((member property org-special-properties))
  9551. (t
  9552. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9553. (when (and vals (string-match "\\S-" vals))
  9554. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9555. (setq vals (mapcar (lambda (x)
  9556. (cond ((stringp x) x)
  9557. ((numberp x) (number-to-string x))
  9558. ((symbolp x) (symbol-name x))
  9559. (t "???")))
  9560. vals)))))
  9561. (if table (mapcar 'list vals) vals)))
  9562. (defun org-property-previous-allowed-value (&optional previous)
  9563. "Switch to the next allowed value for this property."
  9564. (interactive)
  9565. (org-property-next-allowed-value t))
  9566. (defun org-property-next-allowed-value (&optional previous)
  9567. "Switch to the next allowed value for this property."
  9568. (interactive)
  9569. (unless (org-at-property-p)
  9570. (error "Not at a property"))
  9571. (let* ((key (match-string 2))
  9572. (value (match-string 3))
  9573. (allowed (or (org-property-get-allowed-values (point) key)
  9574. (and (member value '("[ ]" "[-]" "[X]"))
  9575. '("[ ]" "[X]"))))
  9576. nval)
  9577. (unless allowed
  9578. (error "Allowed values for this property have not been defined"))
  9579. (if previous (setq allowed (reverse allowed)))
  9580. (if (member value allowed)
  9581. (setq nval (car (cdr (member value allowed)))))
  9582. (setq nval (or nval (car allowed)))
  9583. (if (equal nval value)
  9584. (error "Only one allowed value for this property"))
  9585. (org-at-property-p)
  9586. (replace-match (concat " :" key ": " nval) t t)
  9587. (org-indent-line-function)
  9588. (beginning-of-line 1)
  9589. (skip-chars-forward " \t")))
  9590. (defun org-find-entry-with-id (ident)
  9591. "Locate the entry that contains the ID property with exact value IDENT.
  9592. IDENT can be a string, a symbol or a number, this function will search for
  9593. the string representation of it.
  9594. Return the position where this entry starts, or nil if there is no such entry."
  9595. (let ((id (cond
  9596. ((stringp ident) ident)
  9597. ((symbol-name ident) (symbol-name ident))
  9598. ((numberp ident) (number-to-string ident))
  9599. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9600. (case-fold-search nil))
  9601. (save-excursion
  9602. (save-restriction
  9603. (widen)
  9604. (goto-char (point-min))
  9605. (when (re-search-forward
  9606. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9607. nil t)
  9608. (org-back-to-heading)
  9609. (point))))))
  9610. ;;;; Timestamps
  9611. (defvar org-last-changed-timestamp nil)
  9612. (defvar org-last-inserted-timestamp nil
  9613. "The last time stamp inserted with `org-insert-time-stamp'.")
  9614. (defvar org-time-was-given) ; dynamically scoped parameter
  9615. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9616. (defvar org-ts-what) ; dynamically scoped parameter
  9617. (defun org-time-stamp (arg &optional inactive)
  9618. "Prompt for a date/time and insert a time stamp.
  9619. If the user specifies a time like HH:MM, or if this command is called
  9620. with a prefix argument, the time stamp will contain date and time.
  9621. Otherwise, only the date will be included. All parts of a date not
  9622. specified by the user will be filled in from the current date/time.
  9623. So if you press just return without typing anything, the time stamp
  9624. will represent the current date/time. If there is already a timestamp
  9625. at the cursor, it will be modified."
  9626. (interactive "P")
  9627. (let* ((ts nil)
  9628. (default-time
  9629. ;; Default time is either today, or, when entering a range,
  9630. ;; the range start.
  9631. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9632. (save-excursion
  9633. (re-search-backward
  9634. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9635. (- (point) 20) t)))
  9636. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9637. (current-time)))
  9638. (default-input (and ts (org-get-compact-tod ts)))
  9639. org-time-was-given org-end-time-was-given time)
  9640. (cond
  9641. ((and (org-at-timestamp-p t)
  9642. (memq last-command '(org-time-stamp org-time-stamp-inactive))
  9643. (memq this-command '(org-time-stamp org-time-stamp-inactive)))
  9644. (insert "--")
  9645. (setq time (let ((this-command this-command))
  9646. (org-read-date arg 'totime nil nil
  9647. default-time default-input)))
  9648. (org-insert-time-stamp time (or org-time-was-given arg) inactive))
  9649. ((org-at-timestamp-p t)
  9650. (setq time (let ((this-command this-command))
  9651. (org-read-date arg 'totime nil nil default-time default-input)))
  9652. (when (org-at-timestamp-p t) ; just to get the match data
  9653. ; (setq inactive (eq (char-after (match-beginning 0)) ?\[))
  9654. (replace-match "")
  9655. (setq org-last-changed-timestamp
  9656. (org-insert-time-stamp
  9657. time (or org-time-was-given arg)
  9658. inactive nil nil (list org-end-time-was-given))))
  9659. (message "Timestamp updated"))
  9660. (t
  9661. (setq time (let ((this-command this-command))
  9662. (org-read-date arg 'totime nil nil default-time default-input)))
  9663. (org-insert-time-stamp time (or org-time-was-given arg) inactive
  9664. nil nil (list org-end-time-was-given))))))
  9665. ;; FIXME: can we use this for something else, like computing time differences?
  9666. (defun org-get-compact-tod (s)
  9667. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9668. (let* ((t1 (match-string 1 s))
  9669. (h1 (string-to-number (match-string 2 s)))
  9670. (m1 (string-to-number (match-string 3 s)))
  9671. (t2 (and (match-end 4) (match-string 5 s)))
  9672. (h2 (and t2 (string-to-number (match-string 6 s))))
  9673. (m2 (and t2 (string-to-number (match-string 7 s))))
  9674. dh dm)
  9675. (if (not t2)
  9676. t1
  9677. (setq dh (- h2 h1) dm (- m2 m1))
  9678. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9679. (concat t1 "+" (number-to-string dh)
  9680. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9681. (defun org-time-stamp-inactive (&optional arg)
  9682. "Insert an inactive time stamp.
  9683. An inactive time stamp is enclosed in square brackets instead of angle
  9684. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9685. does not link to the calendar and cannot be changed with the S-cursor keys.
  9686. So these are more for recording a certain time/date."
  9687. (interactive "P")
  9688. (org-time-stamp arg 'inactive))
  9689. (defvar org-date-ovl (org-make-overlay 1 1))
  9690. (org-overlay-put org-date-ovl 'face 'org-warning)
  9691. (org-detach-overlay org-date-ovl)
  9692. (defvar org-ans1) ; dynamically scoped parameter
  9693. (defvar org-ans2) ; dynamically scoped parameter
  9694. (defvar org-plain-time-of-day-regexp) ; defined below
  9695. (defvar org-overriding-default-time nil) ; dynamically scoped
  9696. (defvar org-read-date-overlay nil)
  9697. (defvar org-dcst nil) ; dynamically scoped
  9698. (defun org-read-date (&optional with-time to-time from-string prompt
  9699. default-time default-input)
  9700. "Read a date, possibly a time, and make things smooth for the user.
  9701. The prompt will suggest to enter an ISO date, but you can also enter anything
  9702. which will at least partially be understood by `parse-time-string'.
  9703. Unrecognized parts of the date will default to the current day, month, year,
  9704. hour and minute. If this command is called to replace a timestamp at point,
  9705. of to enter the second timestamp of a range, the default time is taken from the
  9706. existing stamp. For example,
  9707. 3-2-5 --> 2003-02-05
  9708. feb 15 --> currentyear-02-15
  9709. sep 12 9 --> 2009-09-12
  9710. 12:45 --> today 12:45
  9711. 22 sept 0:34 --> currentyear-09-22 0:34
  9712. 12 --> currentyear-currentmonth-12
  9713. Fri --> nearest Friday (today or later)
  9714. etc.
  9715. Furthermore you can specify a relative date by giving, as the *first* thing
  9716. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9717. change in days weeks, months, years.
  9718. With a single plus or minus, the date is relative to today. With a double
  9719. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9720. +4d --> four days from today
  9721. +4 --> same as above
  9722. +2w --> two weeks from today
  9723. ++5 --> five days from default date
  9724. The function understands only English month and weekday abbreviations,
  9725. but this can be configured with the variables `parse-time-months' and
  9726. `parse-time-weekdays'.
  9727. While prompting, a calendar is popped up - you can also select the
  9728. date with the mouse (button 1). The calendar shows a period of three
  9729. months. To scroll it to other months, use the keys `>' and `<'.
  9730. If you don't like the calendar, turn it off with
  9731. \(setq org-read-date-popup-calendar nil)
  9732. With optional argument TO-TIME, the date will immediately be converted
  9733. to an internal time.
  9734. With an optional argument WITH-TIME, the prompt will suggest to also
  9735. insert a time. Note that when WITH-TIME is not set, you can still
  9736. enter a time, and this function will inform the calling routine about
  9737. this change. The calling routine may then choose to change the format
  9738. used to insert the time stamp into the buffer to include the time.
  9739. With optional argument FROM-STRING, read from this string instead from
  9740. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9741. the time/date that is used for everything that is not specified by the
  9742. user."
  9743. (require 'parse-time)
  9744. (let* ((org-time-stamp-rounding-minutes
  9745. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9746. (org-dcst org-display-custom-times)
  9747. (ct (org-current-time))
  9748. (def (or org-overriding-default-time default-time ct))
  9749. (defdecode (decode-time def))
  9750. (dummy (progn
  9751. (when (< (nth 2 defdecode) org-extend-today-until)
  9752. (setcar (nthcdr 2 defdecode) -1)
  9753. (setcar (nthcdr 1 defdecode) 59)
  9754. (setq def (apply 'encode-time defdecode)
  9755. defdecode (decode-time def)))))
  9756. (calendar-move-hook nil)
  9757. (calendar-view-diary-initially-flag nil)
  9758. (view-diary-entries-initially nil)
  9759. (calendar-view-holidays-initially-flag nil)
  9760. (view-calendar-holidays-initially nil)
  9761. (timestr (format-time-string
  9762. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9763. (prompt (concat (if prompt (concat prompt " ") "")
  9764. (format "Date+time [%s]: " timestr)))
  9765. ans (org-ans0 "") org-ans1 org-ans2 final)
  9766. (cond
  9767. (from-string (setq ans from-string))
  9768. (org-read-date-popup-calendar
  9769. (save-excursion
  9770. (save-window-excursion
  9771. (calendar)
  9772. (calendar-forward-day (- (time-to-days def)
  9773. (calendar-absolute-from-gregorian
  9774. (calendar-current-date))))
  9775. (org-eval-in-calendar nil t)
  9776. (let* ((old-map (current-local-map))
  9777. (map (copy-keymap calendar-mode-map))
  9778. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9779. (org-defkey map (kbd "RET") 'org-calendar-select)
  9780. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9781. 'org-calendar-select-mouse)
  9782. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9783. 'org-calendar-select-mouse)
  9784. (org-defkey minibuffer-local-map [(meta shift left)]
  9785. (lambda () (interactive)
  9786. (org-eval-in-calendar '(calendar-backward-month 1))))
  9787. (org-defkey minibuffer-local-map [(meta shift right)]
  9788. (lambda () (interactive)
  9789. (org-eval-in-calendar '(calendar-forward-month 1))))
  9790. (org-defkey minibuffer-local-map [(meta shift up)]
  9791. (lambda () (interactive)
  9792. (org-eval-in-calendar '(calendar-backward-year 1))))
  9793. (org-defkey minibuffer-local-map [(meta shift down)]
  9794. (lambda () (interactive)
  9795. (org-eval-in-calendar '(calendar-forward-year 1))))
  9796. (org-defkey minibuffer-local-map [(shift up)]
  9797. (lambda () (interactive)
  9798. (org-eval-in-calendar '(calendar-backward-week 1))))
  9799. (org-defkey minibuffer-local-map [(shift down)]
  9800. (lambda () (interactive)
  9801. (org-eval-in-calendar '(calendar-forward-week 1))))
  9802. (org-defkey minibuffer-local-map [(shift left)]
  9803. (lambda () (interactive)
  9804. (org-eval-in-calendar '(calendar-backward-day 1))))
  9805. (org-defkey minibuffer-local-map [(shift right)]
  9806. (lambda () (interactive)
  9807. (org-eval-in-calendar '(calendar-forward-day 1))))
  9808. (org-defkey minibuffer-local-map ">"
  9809. (lambda () (interactive)
  9810. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9811. (org-defkey minibuffer-local-map "<"
  9812. (lambda () (interactive)
  9813. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9814. (unwind-protect
  9815. (progn
  9816. (use-local-map map)
  9817. (add-hook 'post-command-hook 'org-read-date-display)
  9818. (setq org-ans0 (read-string prompt default-input nil nil))
  9819. ;; org-ans0: from prompt
  9820. ;; org-ans1: from mouse click
  9821. ;; org-ans2: from calendar motion
  9822. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9823. (remove-hook 'post-command-hook 'org-read-date-display)
  9824. (use-local-map old-map)
  9825. (when org-read-date-overlay
  9826. (org-delete-overlay org-read-date-overlay)
  9827. (setq org-read-date-overlay nil)))))))
  9828. (t ; Naked prompt only
  9829. (unwind-protect
  9830. (setq ans (read-string prompt default-input nil timestr))
  9831. (when org-read-date-overlay
  9832. (org-delete-overlay org-read-date-overlay)
  9833. (setq org-read-date-overlay nil)))))
  9834. (setq final (org-read-date-analyze ans def defdecode))
  9835. (if to-time
  9836. (apply 'encode-time final)
  9837. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9838. (format "%04d-%02d-%02d %02d:%02d"
  9839. (nth 5 final) (nth 4 final) (nth 3 final)
  9840. (nth 2 final) (nth 1 final))
  9841. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9842. (defvar def)
  9843. (defvar defdecode)
  9844. (defvar with-time)
  9845. (defun org-read-date-display ()
  9846. "Display the currrent date prompt interpretation in the minibuffer."
  9847. (when org-read-date-display-live
  9848. (when org-read-date-overlay
  9849. (org-delete-overlay org-read-date-overlay))
  9850. (let ((p (point)))
  9851. (end-of-line 1)
  9852. (while (not (equal (buffer-substring
  9853. (max (point-min) (- (point) 4)) (point))
  9854. " "))
  9855. (insert " "))
  9856. (goto-char p))
  9857. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9858. " " (or org-ans1 org-ans2)))
  9859. (org-end-time-was-given nil)
  9860. (f (org-read-date-analyze ans def defdecode))
  9861. (fmts (if org-dcst
  9862. org-time-stamp-custom-formats
  9863. org-time-stamp-formats))
  9864. (fmt (if (or with-time
  9865. (and (boundp 'org-time-was-given) org-time-was-given))
  9866. (cdr fmts)
  9867. (car fmts)))
  9868. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9869. (when (and org-end-time-was-given
  9870. (string-match org-plain-time-of-day-regexp txt))
  9871. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9872. org-end-time-was-given
  9873. (substring txt (match-end 0)))))
  9874. (setq org-read-date-overlay
  9875. (org-make-overlay (1- (point-at-eol)) (point-at-eol)))
  9876. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9877. (defun org-read-date-analyze (ans def defdecode)
  9878. "Analyze the combined answer of the date prompt."
  9879. ;; FIXME: cleanup and comment
  9880. (let (delta deltan deltaw deltadef year month day
  9881. hour minute second wday pm h2 m2 tl wday1
  9882. iso-year iso-weekday iso-week iso-year iso-date)
  9883. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9884. (setq ans "+0"))
  9885. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9886. (setq ans (replace-match "" t t ans)
  9887. deltan (car delta)
  9888. deltaw (nth 1 delta)
  9889. deltadef (nth 2 delta)))
  9890. ;; Check if there is an iso week date in there
  9891. ;; If yes, sore the info and ostpone interpreting it until the rest
  9892. ;; of the parsing is done
  9893. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9894. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9895. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9896. iso-week (string-to-number (match-string 2 ans)))
  9897. (setq ans (replace-match "" t t ans)))
  9898. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9899. (when (string-match
  9900. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9901. (setq year (if (match-end 2)
  9902. (string-to-number (match-string 2 ans))
  9903. (string-to-number (format-time-string "%Y")))
  9904. month (string-to-number (match-string 3 ans))
  9905. day (string-to-number (match-string 4 ans)))
  9906. (if (< year 100) (setq year (+ 2000 year)))
  9907. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9908. t nil ans)))
  9909. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9910. ;; If there is a time with am/pm, and *no* time without it, we convert
  9911. ;; so that matching will be successful.
  9912. (loop for i from 1 to 2 do ; twice, for end time as well
  9913. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9914. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9915. (setq hour (string-to-number (match-string 1 ans))
  9916. minute (if (match-end 3)
  9917. (string-to-number (match-string 3 ans))
  9918. 0)
  9919. pm (equal ?p
  9920. (string-to-char (downcase (match-string 4 ans)))))
  9921. (if (and (= hour 12) (not pm))
  9922. (setq hour 0)
  9923. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9924. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9925. t t ans))))
  9926. ;; Check if a time range is given as a duration
  9927. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9928. (setq hour (string-to-number (match-string 1 ans))
  9929. h2 (+ hour (string-to-number (match-string 3 ans)))
  9930. minute (string-to-number (match-string 2 ans))
  9931. m2 (+ minute (if (match-end 5) (string-to-number
  9932. (match-string 5 ans))0)))
  9933. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9934. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9935. t t ans)))
  9936. ;; Check if there is a time range
  9937. (when (boundp 'org-end-time-was-given)
  9938. (setq org-time-was-given nil)
  9939. (when (and (string-match org-plain-time-of-day-regexp ans)
  9940. (match-end 8))
  9941. (setq org-end-time-was-given (match-string 8 ans))
  9942. (setq ans (concat (substring ans 0 (match-beginning 7))
  9943. (substring ans (match-end 7))))))
  9944. (setq tl (parse-time-string ans)
  9945. day (or (nth 3 tl) (nth 3 defdecode))
  9946. month (or (nth 4 tl)
  9947. (if (and org-read-date-prefer-future
  9948. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9949. (1+ (nth 4 defdecode))
  9950. (nth 4 defdecode)))
  9951. year (or (nth 5 tl)
  9952. (if (and org-read-date-prefer-future
  9953. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9954. (1+ (nth 5 defdecode))
  9955. (nth 5 defdecode)))
  9956. hour (or (nth 2 tl) (nth 2 defdecode))
  9957. minute (or (nth 1 tl) (nth 1 defdecode))
  9958. second (or (nth 0 tl) 0)
  9959. wday (nth 6 tl))
  9960. ;; Special date definitions below
  9961. (cond
  9962. (iso-week
  9963. ;; There was an iso week
  9964. (setq year (or iso-year year)
  9965. day (or iso-weekday wday 1)
  9966. wday nil ; to make sure that the trigger below does not match
  9967. iso-date (calendar-gregorian-from-absolute
  9968. (calendar-absolute-from-iso
  9969. (list iso-week day year))))
  9970. ; FIXME: Should we also push ISO weeks into the future?
  9971. ; (when (and org-read-date-prefer-future
  9972. ; (not iso-year)
  9973. ; (< (calendar-absolute-from-gregorian iso-date)
  9974. ; (time-to-days (current-time))))
  9975. ; (setq year (1+ year)
  9976. ; iso-date (calendar-gregorian-from-absolute
  9977. ; (calendar-absolute-from-iso
  9978. ; (list iso-week day year)))))
  9979. (setq month (car iso-date)
  9980. year (nth 2 iso-date)
  9981. day (nth 1 iso-date)))
  9982. (deltan
  9983. (unless deltadef
  9984. (let ((now (decode-time (current-time))))
  9985. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9986. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9987. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9988. ((equal deltaw "m") (setq month (+ month deltan)))
  9989. ((equal deltaw "y") (setq year (+ year deltan)))))
  9990. ((and wday (not (nth 3 tl)))
  9991. ;; Weekday was given, but no day, so pick that day in the week
  9992. ;; on or after the derived date.
  9993. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9994. (unless (equal wday wday1)
  9995. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9996. (if (and (boundp 'org-time-was-given)
  9997. (nth 2 tl))
  9998. (setq org-time-was-given t))
  9999. (if (< year 100) (setq year (+ 2000 year)))
  10000. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  10001. (list second minute hour day month year)))
  10002. (defvar parse-time-weekdays)
  10003. (defun org-read-date-get-relative (s today default)
  10004. "Check string S for special relative date string.
  10005. TODAY and DEFAULT are internal times, for today and for a default.
  10006. Return shift list (N what def-flag)
  10007. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  10008. N is the number of WHATs to shift.
  10009. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  10010. the DEFAULT date rather than TODAY."
  10011. (when (and
  10012. (string-match
  10013. (concat
  10014. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  10015. "\\([0-9]+\\)?"
  10016. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  10017. "\\([ \t]\\|$\\)") s)
  10018. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  10019. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  10020. (string-to-char (substring (match-string 1 s) -1))
  10021. ?+))
  10022. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  10023. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  10024. (what (if (match-end 3) (match-string 3 s) "d"))
  10025. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  10026. (date (if rel default today))
  10027. (wday (nth 6 (decode-time date)))
  10028. delta)
  10029. (if wday1
  10030. (progn
  10031. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  10032. (if (= dir ?-) (setq delta (- delta 7)))
  10033. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  10034. (list delta "d" rel))
  10035. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  10036. (defun org-eval-in-calendar (form &optional keepdate)
  10037. "Eval FORM in the calendar window and return to current window.
  10038. Also, store the cursor date in variable org-ans2."
  10039. (let ((sw (selected-window)))
  10040. (select-window (get-buffer-window "*Calendar*"))
  10041. (eval form)
  10042. (when (and (not keepdate) (calendar-cursor-to-date))
  10043. (let* ((date (calendar-cursor-to-date))
  10044. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10045. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  10046. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  10047. (select-window sw)))
  10048. (defun org-calendar-select ()
  10049. "Return to `org-read-date' with the date currently selected.
  10050. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10051. (interactive)
  10052. (when (calendar-cursor-to-date)
  10053. (let* ((date (calendar-cursor-to-date))
  10054. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10055. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10056. (if (active-minibuffer-window) (exit-minibuffer))))
  10057. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  10058. "Insert a date stamp for the date given by the internal TIME.
  10059. WITH-HM means, use the stamp format that includes the time of the day.
  10060. INACTIVE means use square brackets instead of angular ones, so that the
  10061. stamp will not contribute to the agenda.
  10062. PRE and POST are optional strings to be inserted before and after the
  10063. stamp.
  10064. The command returns the inserted time stamp."
  10065. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  10066. stamp)
  10067. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  10068. (insert-before-markers (or pre ""))
  10069. (insert-before-markers (setq stamp (format-time-string fmt time)))
  10070. (when (listp extra)
  10071. (setq extra (car extra))
  10072. (if (and (stringp extra)
  10073. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  10074. (setq extra (format "-%02d:%02d"
  10075. (string-to-number (match-string 1 extra))
  10076. (string-to-number (match-string 2 extra))))
  10077. (setq extra nil)))
  10078. (when extra
  10079. (backward-char 1)
  10080. (insert-before-markers extra)
  10081. (forward-char 1))
  10082. (insert-before-markers (or post ""))
  10083. (setq org-last-inserted-timestamp stamp)))
  10084. (defun org-toggle-time-stamp-overlays ()
  10085. "Toggle the use of custom time stamp formats."
  10086. (interactive)
  10087. (setq org-display-custom-times (not org-display-custom-times))
  10088. (unless org-display-custom-times
  10089. (let ((p (point-min)) (bmp (buffer-modified-p)))
  10090. (while (setq p (next-single-property-change p 'display))
  10091. (if (and (get-text-property p 'display)
  10092. (eq (get-text-property p 'face) 'org-date))
  10093. (remove-text-properties
  10094. p (setq p (next-single-property-change p 'display))
  10095. '(display t))))
  10096. (set-buffer-modified-p bmp)))
  10097. (if (featurep 'xemacs)
  10098. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  10099. (org-restart-font-lock)
  10100. (setq org-table-may-need-update t)
  10101. (if org-display-custom-times
  10102. (message "Time stamps are overlayed with custom format")
  10103. (message "Time stamp overlays removed")))
  10104. (defun org-display-custom-time (beg end)
  10105. "Overlay modified time stamp format over timestamp between BEG and END."
  10106. (let* ((ts (buffer-substring beg end))
  10107. t1 w1 with-hm tf time str w2 (off 0))
  10108. (save-match-data
  10109. (setq t1 (org-parse-time-string ts t))
  10110. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  10111. (setq off (- (match-end 0) (match-beginning 0)))))
  10112. (setq end (- end off))
  10113. (setq w1 (- end beg)
  10114. with-hm (and (nth 1 t1) (nth 2 t1))
  10115. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  10116. time (org-fix-decoded-time t1)
  10117. str (org-add-props
  10118. (format-time-string
  10119. (substring tf 1 -1) (apply 'encode-time time))
  10120. nil 'mouse-face 'highlight)
  10121. w2 (length str))
  10122. (if (not (= w2 w1))
  10123. (add-text-properties (1+ beg) (+ 2 beg)
  10124. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  10125. (if (featurep 'xemacs)
  10126. (progn
  10127. (put-text-property beg end 'invisible t)
  10128. (put-text-property beg end 'end-glyph (make-glyph str)))
  10129. (put-text-property beg end 'display str))))
  10130. (defun org-translate-time (string)
  10131. "Translate all timestamps in STRING to custom format.
  10132. But do this only if the variable `org-display-custom-times' is set."
  10133. (when org-display-custom-times
  10134. (save-match-data
  10135. (let* ((start 0)
  10136. (re org-ts-regexp-both)
  10137. t1 with-hm inactive tf time str beg end)
  10138. (while (setq start (string-match re string start))
  10139. (setq beg (match-beginning 0)
  10140. end (match-end 0)
  10141. t1 (save-match-data
  10142. (org-parse-time-string (substring string beg end) t))
  10143. with-hm (and (nth 1 t1) (nth 2 t1))
  10144. inactive (equal (substring string beg (1+ beg)) "[")
  10145. tf (funcall (if with-hm 'cdr 'car)
  10146. org-time-stamp-custom-formats)
  10147. time (org-fix-decoded-time t1)
  10148. str (format-time-string
  10149. (concat
  10150. (if inactive "[" "<") (substring tf 1 -1)
  10151. (if inactive "]" ">"))
  10152. (apply 'encode-time time))
  10153. string (replace-match str t t string)
  10154. start (+ start (length str)))))))
  10155. string)
  10156. (defun org-fix-decoded-time (time)
  10157. "Set 0 instead of nil for the first 6 elements of time.
  10158. Don't touch the rest."
  10159. (let ((n 0))
  10160. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  10161. (defun org-days-to-time (timestamp-string)
  10162. "Difference between TIMESTAMP-STRING and now in days."
  10163. (- (time-to-days (org-time-string-to-time timestamp-string))
  10164. (time-to-days (current-time))))
  10165. (defun org-deadline-close (timestamp-string &optional ndays)
  10166. "Is the time in TIMESTAMP-STRING close to the current date?"
  10167. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  10168. (and (< (org-days-to-time timestamp-string) ndays)
  10169. (not (org-entry-is-done-p))))
  10170. (defun org-get-wdays (ts)
  10171. "Get the deadline lead time appropriate for timestring TS."
  10172. (cond
  10173. ((<= org-deadline-warning-days 0)
  10174. ;; 0 or negative, enforce this value no matter what
  10175. (- org-deadline-warning-days))
  10176. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  10177. ;; lead time is specified.
  10178. (floor (* (string-to-number (match-string 1 ts))
  10179. (cdr (assoc (match-string 2 ts)
  10180. '(("d" . 1) ("w" . 7)
  10181. ("m" . 30.4) ("y" . 365.25)))))))
  10182. ;; go for the default.
  10183. (t org-deadline-warning-days)))
  10184. (defun org-calendar-select-mouse (ev)
  10185. "Return to `org-read-date' with the date currently selected.
  10186. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  10187. (interactive "e")
  10188. (mouse-set-point ev)
  10189. (when (calendar-cursor-to-date)
  10190. (let* ((date (calendar-cursor-to-date))
  10191. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  10192. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  10193. (if (active-minibuffer-window) (exit-minibuffer))))
  10194. (defun org-check-deadlines (ndays)
  10195. "Check if there are any deadlines due or past due.
  10196. A deadline is considered due if it happens within `org-deadline-warning-days'
  10197. days from today's date. If the deadline appears in an entry marked DONE,
  10198. it is not shown. The prefix arg NDAYS can be used to test that many
  10199. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  10200. (interactive "P")
  10201. (let* ((org-warn-days
  10202. (cond
  10203. ((equal ndays '(4)) 100000)
  10204. (ndays (prefix-numeric-value ndays))
  10205. (t (abs org-deadline-warning-days))))
  10206. (case-fold-search nil)
  10207. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  10208. (callback
  10209. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  10210. (message "%d deadlines past-due or due within %d days"
  10211. (org-occur regexp nil callback)
  10212. org-warn-days)))
  10213. (defun org-check-before-date (date)
  10214. "Check if there are deadlines or scheduled entries before DATE."
  10215. (interactive (list (org-read-date)))
  10216. (let ((case-fold-search nil)
  10217. (regexp (concat "\\<\\(" org-deadline-string
  10218. "\\|" org-scheduled-string
  10219. "\\) *<\\([^>]+\\)>"))
  10220. (callback
  10221. (lambda () (time-less-p
  10222. (org-time-string-to-time (match-string 2))
  10223. (org-time-string-to-time date)))))
  10224. (message "%d entries before %s"
  10225. (org-occur regexp nil callback) date)))
  10226. (defun org-evaluate-time-range (&optional to-buffer)
  10227. "Evaluate a time range by computing the difference between start and end.
  10228. Normally the result is just printed in the echo area, but with prefix arg
  10229. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  10230. If the time range is actually in a table, the result is inserted into the
  10231. next column.
  10232. For time difference computation, a year is assumed to be exactly 365
  10233. days in order to avoid rounding problems."
  10234. (interactive "P")
  10235. (or
  10236. (org-clock-update-time-maybe)
  10237. (save-excursion
  10238. (unless (org-at-date-range-p t)
  10239. (goto-char (point-at-bol))
  10240. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  10241. (if (not (org-at-date-range-p t))
  10242. (error "Not at a time-stamp range, and none found in current line")))
  10243. (let* ((ts1 (match-string 1))
  10244. (ts2 (match-string 2))
  10245. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  10246. (match-end (match-end 0))
  10247. (time1 (org-time-string-to-time ts1))
  10248. (time2 (org-time-string-to-time ts2))
  10249. (t1 (time-to-seconds time1))
  10250. (t2 (time-to-seconds time2))
  10251. (diff (abs (- t2 t1)))
  10252. (negative (< (- t2 t1) 0))
  10253. ;; (ys (floor (* 365 24 60 60)))
  10254. (ds (* 24 60 60))
  10255. (hs (* 60 60))
  10256. (fy "%dy %dd %02d:%02d")
  10257. (fy1 "%dy %dd")
  10258. (fd "%dd %02d:%02d")
  10259. (fd1 "%dd")
  10260. (fh "%02d:%02d")
  10261. y d h m align)
  10262. (if havetime
  10263. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10264. y 0
  10265. d (floor (/ diff ds)) diff (mod diff ds)
  10266. h (floor (/ diff hs)) diff (mod diff hs)
  10267. m (floor (/ diff 60)))
  10268. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  10269. y 0
  10270. d (floor (+ (/ diff ds) 0.5))
  10271. h 0 m 0))
  10272. (if (not to-buffer)
  10273. (message "%s" (org-make-tdiff-string y d h m))
  10274. (if (org-at-table-p)
  10275. (progn
  10276. (goto-char match-end)
  10277. (setq align t)
  10278. (and (looking-at " *|") (goto-char (match-end 0))))
  10279. (goto-char match-end))
  10280. (if (looking-at
  10281. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  10282. (replace-match ""))
  10283. (if negative (insert " -"))
  10284. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  10285. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  10286. (insert " " (format fh h m))))
  10287. (if align (org-table-align))
  10288. (message "Time difference inserted")))))
  10289. (defun org-make-tdiff-string (y d h m)
  10290. (let ((fmt "")
  10291. (l nil))
  10292. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  10293. l (push y l)))
  10294. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  10295. l (push d l)))
  10296. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  10297. l (push h l)))
  10298. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  10299. l (push m l)))
  10300. (apply 'format fmt (nreverse l))))
  10301. (defun org-time-string-to-time (s)
  10302. (apply 'encode-time (org-parse-time-string s)))
  10303. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  10304. "Convert a time stamp to an absolute day number.
  10305. If there is a specifyer for a cyclic time stamp, get the closest date to
  10306. DAYNR.
  10307. PREFER and SHOW-ALL are passed through to `org-closest-date'."
  10308. (cond
  10309. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  10310. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  10311. daynr
  10312. (+ daynr 1000)))
  10313. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  10314. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  10315. (time-to-days (current-time))) (match-string 0 s)
  10316. prefer show-all))
  10317. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  10318. (defun org-days-to-iso-week (days)
  10319. "Return the iso week number."
  10320. (require 'cal-iso)
  10321. (car (calendar-iso-from-absolute days)))
  10322. (defun org-small-year-to-year (year)
  10323. "Convert 2-digit years into 4-digit years.
  10324. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  10325. The year 2000 cannot be abbreviated. Any year larger than 99
  10326. is returned unchanged."
  10327. (if (< year 38)
  10328. (setq year (+ 2000 year))
  10329. (if (< year 100)
  10330. (setq year (+ 1900 year))))
  10331. year)
  10332. (defun org-time-from-absolute (d)
  10333. "Return the time corresponding to date D.
  10334. D may be an absolute day number, or a calendar-type list (month day year)."
  10335. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  10336. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  10337. (defun org-calendar-holiday ()
  10338. "List of holidays, for Diary display in Org-mode."
  10339. (require 'holidays)
  10340. (let ((hl (funcall
  10341. (if (fboundp 'calendar-check-holidays)
  10342. 'calendar-check-holidays 'check-calendar-holidays) date)))
  10343. (if hl (mapconcat 'identity hl "; "))))
  10344. (defun org-diary-sexp-entry (sexp entry date)
  10345. "Process a SEXP diary ENTRY for DATE."
  10346. (require 'diary-lib)
  10347. (let ((result (if calendar-debug-sexp
  10348. (let ((stack-trace-on-error t))
  10349. (eval (car (read-from-string sexp))))
  10350. (condition-case nil
  10351. (eval (car (read-from-string sexp)))
  10352. (error
  10353. (beep)
  10354. (message "Bad sexp at line %d in %s: %s"
  10355. (org-current-line)
  10356. (buffer-file-name) sexp)
  10357. (sleep-for 2))))))
  10358. (cond ((stringp result) result)
  10359. ((and (consp result)
  10360. (stringp (cdr result))) (cdr result))
  10361. (result entry)
  10362. (t nil))))
  10363. (defun org-diary-to-ical-string (frombuf)
  10364. "Get iCalendar entries from diary entries in buffer FROMBUF.
  10365. This uses the icalendar.el library."
  10366. (let* ((tmpdir (if (featurep 'xemacs)
  10367. (temp-directory)
  10368. temporary-file-directory))
  10369. (tmpfile (make-temp-name
  10370. (expand-file-name "orgics" tmpdir)))
  10371. buf rtn b e)
  10372. (save-excursion
  10373. (set-buffer frombuf)
  10374. (icalendar-export-region (point-min) (point-max) tmpfile)
  10375. (setq buf (find-buffer-visiting tmpfile))
  10376. (set-buffer buf)
  10377. (goto-char (point-min))
  10378. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  10379. (setq b (match-beginning 0)))
  10380. (goto-char (point-max))
  10381. (if (re-search-backward "^END:VEVENT" nil t)
  10382. (setq e (match-end 0)))
  10383. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  10384. (kill-buffer buf)
  10385. (delete-file tmpfile)
  10386. rtn))
  10387. (defun org-closest-date (start current change prefer show-all)
  10388. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  10389. When PREFER is `past' return a date that is either CURRENT or past.
  10390. When PREFER is `future', return a date that is either CURRENT or future.
  10391. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  10392. ;; Make the proper lists from the dates
  10393. (catch 'exit
  10394. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  10395. dn dw sday cday n1 n2
  10396. d m y y1 y2 date1 date2 nmonths nm ny m2)
  10397. (setq start (org-date-to-gregorian start)
  10398. current (org-date-to-gregorian
  10399. (if show-all
  10400. current
  10401. (time-to-days (current-time))))
  10402. sday (calendar-absolute-from-gregorian start)
  10403. cday (calendar-absolute-from-gregorian current))
  10404. (if (<= cday sday) (throw 'exit sday))
  10405. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  10406. (setq dn (string-to-number (match-string 1 change))
  10407. dw (cdr (assoc (match-string 2 change) a1)))
  10408. (error "Invalid change specifyer: %s" change))
  10409. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  10410. (cond
  10411. ((eq dw 'day)
  10412. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  10413. n2 (+ n1 dn)))
  10414. ((eq dw 'year)
  10415. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10416. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10417. (setq date1 (list m d y1)
  10418. n1 (calendar-absolute-from-gregorian date1)
  10419. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10420. n2 (calendar-absolute-from-gregorian date2)))
  10421. ((eq dw 'month)
  10422. ;; approx number of month between the two dates
  10423. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10424. ;; How often does dn fit in there?
  10425. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10426. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10427. m (+ m nm)
  10428. ny (floor (/ m 12))
  10429. y (+ y ny)
  10430. m (- m (* ny 12)))
  10431. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10432. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10433. (setq m2 (+ m dn) y2 y)
  10434. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10435. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10436. (while (<= n2 cday)
  10437. (setq n1 n2 m m2 y y2)
  10438. (setq m2 (+ m dn) y2 y)
  10439. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10440. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10441. (if show-all
  10442. (cond
  10443. ((eq prefer 'past) n1)
  10444. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10445. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10446. (cond
  10447. ((eq prefer 'past) n1)
  10448. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10449. (t (if (= cday n1) n1 n2)))))))
  10450. (defun org-date-to-gregorian (date)
  10451. "Turn any specification of DATE into a gregorian date for the calendar."
  10452. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10453. ((and (listp date) (= (length date) 3)) date)
  10454. ((stringp date)
  10455. (setq date (org-parse-time-string date))
  10456. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10457. ((listp date)
  10458. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10459. (defun org-parse-time-string (s &optional nodefault)
  10460. "Parse the standard Org-mode time string.
  10461. This should be a lot faster than the normal `parse-time-string'.
  10462. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10463. hour and minute fields will be nil if not given."
  10464. (if (string-match org-ts-regexp0 s)
  10465. (list 0
  10466. (if (or (match-beginning 8) (not nodefault))
  10467. (string-to-number (or (match-string 8 s) "0")))
  10468. (if (or (match-beginning 7) (not nodefault))
  10469. (string-to-number (or (match-string 7 s) "0")))
  10470. (string-to-number (match-string 4 s))
  10471. (string-to-number (match-string 3 s))
  10472. (string-to-number (match-string 2 s))
  10473. nil nil nil)
  10474. (make-list 9 0)))
  10475. (defun org-timestamp-up (&optional arg)
  10476. "Increase the date item at the cursor by one.
  10477. If the cursor is on the year, change the year. If it is on the month or
  10478. the day, change that.
  10479. With prefix ARG, change by that many units."
  10480. (interactive "p")
  10481. (org-timestamp-change (prefix-numeric-value arg)))
  10482. (defun org-timestamp-down (&optional arg)
  10483. "Decrease the date item at the cursor by one.
  10484. If the cursor is on the year, change the year. If it is on the month or
  10485. the day, change that.
  10486. With prefix ARG, change by that many units."
  10487. (interactive "p")
  10488. (org-timestamp-change (- (prefix-numeric-value arg))))
  10489. (defun org-timestamp-up-day (&optional arg)
  10490. "Increase the date in the time stamp by one day.
  10491. With prefix ARG, change that many days."
  10492. (interactive "p")
  10493. (if (and (not (org-at-timestamp-p t))
  10494. (org-on-heading-p))
  10495. (org-todo 'up)
  10496. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10497. (defun org-timestamp-down-day (&optional arg)
  10498. "Decrease the date in the time stamp by one day.
  10499. With prefix ARG, change that many days."
  10500. (interactive "p")
  10501. (if (and (not (org-at-timestamp-p t))
  10502. (org-on-heading-p))
  10503. (org-todo 'down)
  10504. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10505. (defun org-at-timestamp-p (&optional inactive-ok)
  10506. "Determine if the cursor is in or at a timestamp."
  10507. (interactive)
  10508. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10509. (pos (point))
  10510. (ans (or (looking-at tsr)
  10511. (save-excursion
  10512. (skip-chars-backward "^[<\n\r\t")
  10513. (if (> (point) (point-min)) (backward-char 1))
  10514. (and (looking-at tsr)
  10515. (> (- (match-end 0) pos) -1))))))
  10516. (and ans
  10517. (boundp 'org-ts-what)
  10518. (setq org-ts-what
  10519. (cond
  10520. ((= pos (match-beginning 0)) 'bracket)
  10521. ((= pos (1- (match-end 0))) 'bracket)
  10522. ((org-pos-in-match-range pos 2) 'year)
  10523. ((org-pos-in-match-range pos 3) 'month)
  10524. ((org-pos-in-match-range pos 7) 'hour)
  10525. ((org-pos-in-match-range pos 8) 'minute)
  10526. ((or (org-pos-in-match-range pos 4)
  10527. (org-pos-in-match-range pos 5)) 'day)
  10528. ((and (> pos (or (match-end 8) (match-end 5)))
  10529. (< pos (match-end 0)))
  10530. (- pos (or (match-end 8) (match-end 5))))
  10531. (t 'day))))
  10532. ans))
  10533. (defun org-toggle-timestamp-type ()
  10534. "Toggle the type (<active> or [inactive]) of a time stamp."
  10535. (interactive)
  10536. (when (org-at-timestamp-p t)
  10537. (let ((beg (match-beginning 0)) (end (match-end 0))
  10538. (map '((?\[ . "<") (?\] . ">") (?< . "[") (?> . "]"))))
  10539. (save-excursion
  10540. (goto-char beg)
  10541. (while (re-search-forward "[][<>]" end t)
  10542. (replace-match (cdr (assoc (char-after (match-beginning 0)) map))
  10543. t t)))
  10544. (message "Timestamp is now %sactive"
  10545. (if (equal (char-after beg) ?<) "" "in")))))
  10546. (defun org-timestamp-change (n &optional what)
  10547. "Change the date in the time stamp at point.
  10548. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10549. `year', `minute', `second'. If WHAT is not given, the cursor position
  10550. in the timestamp determines what will be changed."
  10551. (let ((pos (point))
  10552. with-hm inactive
  10553. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10554. org-ts-what
  10555. extra rem
  10556. ts time time0)
  10557. (if (not (org-at-timestamp-p t))
  10558. (error "Not at a timestamp"))
  10559. (if (and (not what) (eq org-ts-what 'bracket))
  10560. (org-toggle-timestamp-type)
  10561. (if (and (not what) (not (eq org-ts-what 'day))
  10562. org-display-custom-times
  10563. (get-text-property (point) 'display)
  10564. (not (get-text-property (1- (point)) 'display)))
  10565. (setq org-ts-what 'day))
  10566. (setq org-ts-what (or what org-ts-what)
  10567. inactive (= (char-after (match-beginning 0)) ?\[)
  10568. ts (match-string 0))
  10569. (replace-match "")
  10570. (if (string-match
  10571. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10572. ts)
  10573. (setq extra (match-string 1 ts)))
  10574. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10575. (setq with-hm t))
  10576. (setq time0 (org-parse-time-string ts))
  10577. (when (and (eq org-ts-what 'minute)
  10578. (eq current-prefix-arg nil))
  10579. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10580. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10581. (setcar (cdr time0) (+ (nth 1 time0)
  10582. (if (> n 0) (- rem) (- dm rem))))))
  10583. (setq time
  10584. (encode-time (or (car time0) 0)
  10585. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10586. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10587. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10588. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10589. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10590. (nthcdr 6 time0)))
  10591. (when (integerp org-ts-what)
  10592. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10593. (if (eq what 'calendar)
  10594. (let ((cal-date (org-get-date-from-calendar)))
  10595. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10596. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10597. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10598. (setcar time0 (or (car time0) 0))
  10599. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10600. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10601. (setq time (apply 'encode-time time0))))
  10602. (setq org-last-changed-timestamp
  10603. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10604. (org-clock-update-time-maybe)
  10605. (goto-char pos)
  10606. ;; Try to recenter the calendar window, if any
  10607. (if (and org-calendar-follow-timestamp-change
  10608. (get-buffer-window "*Calendar*" t)
  10609. (memq org-ts-what '(day month year)))
  10610. (org-recenter-calendar (time-to-days time))))))
  10611. (defun org-modify-ts-extra (s pos n dm)
  10612. "Change the different parts of the lead-time and repeat fields in timestamp."
  10613. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10614. ng h m new rem)
  10615. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10616. (cond
  10617. ((or (org-pos-in-match-range pos 2)
  10618. (org-pos-in-match-range pos 3))
  10619. (setq m (string-to-number (match-string 3 s))
  10620. h (string-to-number (match-string 2 s)))
  10621. (if (org-pos-in-match-range pos 2)
  10622. (setq h (+ h n))
  10623. (setq n (* dm (org-no-warnings (signum n))))
  10624. (when (not (= 0 (setq rem (% m dm))))
  10625. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10626. (setq m (+ m n)))
  10627. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10628. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10629. (setq h (min 24 (max 0 h)))
  10630. (setq ng 1 new (format "-%02d:%02d" h m)))
  10631. ((org-pos-in-match-range pos 6)
  10632. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10633. ((org-pos-in-match-range pos 5)
  10634. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10635. ((org-pos-in-match-range pos 9)
  10636. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10637. ((org-pos-in-match-range pos 8)
  10638. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10639. (when ng
  10640. (setq s (concat
  10641. (substring s 0 (match-beginning ng))
  10642. new
  10643. (substring s (match-end ng))))))
  10644. s))
  10645. (defun org-recenter-calendar (date)
  10646. "If the calendar is visible, recenter it to DATE."
  10647. (let* ((win (selected-window))
  10648. (cwin (get-buffer-window "*Calendar*" t))
  10649. (calendar-move-hook nil))
  10650. (when cwin
  10651. (select-window cwin)
  10652. (calendar-goto-date (if (listp date) date
  10653. (calendar-gregorian-from-absolute date)))
  10654. (select-window win))))
  10655. (defun org-goto-calendar (&optional arg)
  10656. "Go to the Emacs calendar at the current date.
  10657. If there is a time stamp in the current line, go to that date.
  10658. A prefix ARG can be used to force the current date."
  10659. (interactive "P")
  10660. (let ((tsr org-ts-regexp) diff
  10661. (calendar-move-hook nil)
  10662. (calendar-view-holidays-initially-flag nil)
  10663. (view-calendar-holidays-initially nil)
  10664. (calendar-view-diary-initially-flag nil)
  10665. (view-diary-entries-initially nil))
  10666. (if (or (org-at-timestamp-p)
  10667. (save-excursion
  10668. (beginning-of-line 1)
  10669. (looking-at (concat ".*" tsr))))
  10670. (let ((d1 (time-to-days (current-time)))
  10671. (d2 (time-to-days
  10672. (org-time-string-to-time (match-string 1)))))
  10673. (setq diff (- d2 d1))))
  10674. (calendar)
  10675. (calendar-goto-today)
  10676. (if (and diff (not arg)) (calendar-forward-day diff))))
  10677. (defun org-get-date-from-calendar ()
  10678. "Return a list (month day year) of date at point in calendar."
  10679. (with-current-buffer "*Calendar*"
  10680. (save-match-data
  10681. (calendar-cursor-to-date))))
  10682. (defun org-date-from-calendar ()
  10683. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10684. If there is already a time stamp at the cursor position, update it."
  10685. (interactive)
  10686. (if (org-at-timestamp-p t)
  10687. (org-timestamp-change 0 'calendar)
  10688. (let ((cal-date (org-get-date-from-calendar)))
  10689. (org-insert-time-stamp
  10690. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10691. (defun org-minutes-to-hh:mm-string (m)
  10692. "Compute H:MM from a number of minutes."
  10693. (let ((h (/ m 60)))
  10694. (setq m (- m (* 60 h)))
  10695. (format org-time-clocksum-format h m)))
  10696. (defun org-hh:mm-string-to-minutes (s)
  10697. "Convert a string H:MM to a number of minutes."
  10698. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10699. (+ (* (string-to-number (match-string 1 s)) 60)
  10700. (string-to-number (match-string 2 s)))
  10701. 0))
  10702. ;;;; Agenda files
  10703. ;;;###autoload
  10704. (defun org-iswitchb (&optional arg)
  10705. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10706. With a prefix argument, restrict available to files.
  10707. With two prefix arguments, restrict available buffers to agenda files.
  10708. Due to some yet unresolved reason, the global function
  10709. `iswitchb-mode' needs to be active for this function to work."
  10710. (interactive "P")
  10711. (require 'iswitchb)
  10712. (let ((enabled iswitchb-mode) blist)
  10713. (or enabled (iswitchb-mode 1))
  10714. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10715. ((equal arg '(16)) (org-buffer-list 'agenda))
  10716. (t (org-buffer-list))))
  10717. (unwind-protect
  10718. (let ((iswitchb-make-buflist-hook
  10719. (lambda ()
  10720. (setq iswitchb-temp-buflist
  10721. (mapcar 'buffer-name blist)))))
  10722. (switch-to-buffer
  10723. (iswitchb-read-buffer
  10724. "Switch-to: " nil t))
  10725. (or enabled (iswitchb-mode -1))))))
  10726. (defun org-buffer-list (&optional predicate exclude-tmp)
  10727. "Return a list of Org buffers.
  10728. PREDICATE can be `export', `files' or `agenda'.
  10729. export restrict the list to Export buffers.
  10730. files restrict the list to buffers visiting Org files.
  10731. agenda restrict the list to buffers visiting agenda files.
  10732. If EXCLUDE-TMP is non-nil, ignore temporary buffers."
  10733. (let* ((bfn nil)
  10734. (agenda-files (and (eq predicate 'agenda)
  10735. (mapcar 'file-truename (org-agenda-files t))))
  10736. (filter
  10737. (cond
  10738. ((eq predicate 'files)
  10739. (lambda (b) (with-current-buffer b (eq major-mode 'org-mode))))
  10740. ((eq predicate 'export)
  10741. (lambda (b) (string-match "\*Org .*Export" (buffer-name b))))
  10742. ((eq predicate 'agenda)
  10743. (lambda (b)
  10744. (with-current-buffer b
  10745. (and (eq major-mode 'org-mode)
  10746. (setq bfn (buffer-file-name b))
  10747. (member (file-truename bfn) agenda-files)))))
  10748. (t (lambda (b) (with-current-buffer b
  10749. (or (eq major-mode 'org-mode)
  10750. (string-match "\*Org .*Export"
  10751. (buffer-name b)))))))))
  10752. (delq nil
  10753. (mapcar
  10754. (lambda(b)
  10755. (if (and (funcall filter b)
  10756. (or (not exclude-tmp)
  10757. (not (string-match "tmp" (buffer-name b)))))
  10758. b
  10759. nil))
  10760. (buffer-list)))))
  10761. (defun org-agenda-files (&optional unrestricted archives)
  10762. "Get the list of agenda files.
  10763. Optional UNRESTRICTED means return the full list even if a restriction
  10764. is currently in place.
  10765. When ARCHIVES is t, include all archive files hat are really being
  10766. used by the agenda files. If ARCHIVE is `ifmode', do this only if
  10767. `org-agenda-archives-mode' is t."
  10768. (let ((files
  10769. (cond
  10770. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10771. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10772. ((listp org-agenda-files) org-agenda-files)
  10773. (t (error "Invalid value of `org-agenda-files'")))))
  10774. (setq files (apply 'append
  10775. (mapcar (lambda (f)
  10776. (if (file-directory-p f)
  10777. (directory-files
  10778. f t org-agenda-file-regexp)
  10779. (list f)))
  10780. files)))
  10781. (when org-agenda-skip-unavailable-files
  10782. (setq files (delq nil
  10783. (mapcar (function
  10784. (lambda (file)
  10785. (and (file-readable-p file) file)))
  10786. files))))
  10787. (when (or (eq archives t)
  10788. (and (eq archives 'ifmode) (eq org-agenda-archives-mode t)))
  10789. (setq files (org-add-archive-files files)))
  10790. files))
  10791. (defun org-edit-agenda-file-list ()
  10792. "Edit the list of agenda files.
  10793. Depending on setup, this either uses customize to edit the variable
  10794. `org-agenda-files', or it visits the file that is holding the list. In the
  10795. latter case, the buffer is set up in a way that saving it automatically kills
  10796. the buffer and restores the previous window configuration."
  10797. (interactive)
  10798. (if (stringp org-agenda-files)
  10799. (let ((cw (current-window-configuration)))
  10800. (find-file org-agenda-files)
  10801. (org-set-local 'org-window-configuration cw)
  10802. (org-add-hook 'after-save-hook
  10803. (lambda ()
  10804. (set-window-configuration
  10805. (prog1 org-window-configuration
  10806. (kill-buffer (current-buffer))))
  10807. (org-install-agenda-files-menu)
  10808. (message "New agenda file list installed"))
  10809. nil 'local)
  10810. (message "%s" (substitute-command-keys
  10811. "Edit list and finish with \\[save-buffer]")))
  10812. (customize-variable 'org-agenda-files)))
  10813. (defun org-store-new-agenda-file-list (list)
  10814. "Set new value for the agenda file list and save it correcly."
  10815. (if (stringp org-agenda-files)
  10816. (let ((f org-agenda-files) b)
  10817. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10818. (with-temp-file f
  10819. (insert (mapconcat 'identity list "\n") "\n")))
  10820. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10821. (setq org-agenda-files list)
  10822. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10823. (defun org-read-agenda-file-list ()
  10824. "Read the list of agenda files from a file."
  10825. (when (file-directory-p org-agenda-files)
  10826. (error "`org-agenda-files' cannot be a single directory"))
  10827. (when (stringp org-agenda-files)
  10828. (with-temp-buffer
  10829. (insert-file-contents org-agenda-files)
  10830. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10831. ;;;###autoload
  10832. (defun org-cycle-agenda-files ()
  10833. "Cycle through the files in `org-agenda-files'.
  10834. If the current buffer visits an agenda file, find the next one in the list.
  10835. If the current buffer does not, find the first agenda file."
  10836. (interactive)
  10837. (let* ((fs (org-agenda-files t))
  10838. (files (append fs (list (car fs))))
  10839. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10840. file)
  10841. (unless files (error "No agenda files"))
  10842. (catch 'exit
  10843. (while (setq file (pop files))
  10844. (if (equal (file-truename file) tcf)
  10845. (when (car files)
  10846. (find-file (car files))
  10847. (throw 'exit t))))
  10848. (find-file (car fs)))
  10849. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10850. (defun org-agenda-file-to-front (&optional to-end)
  10851. "Move/add the current file to the top of the agenda file list.
  10852. If the file is not present in the list, it is added to the front. If it is
  10853. present, it is moved there. With optional argument TO-END, add/move to the
  10854. end of the list."
  10855. (interactive "P")
  10856. (let ((org-agenda-skip-unavailable-files nil)
  10857. (file-alist (mapcar (lambda (x)
  10858. (cons (file-truename x) x))
  10859. (org-agenda-files t)))
  10860. (ctf (file-truename buffer-file-name))
  10861. x had)
  10862. (setq x (assoc ctf file-alist) had x)
  10863. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10864. (if to-end
  10865. (setq file-alist (append (delq x file-alist) (list x)))
  10866. (setq file-alist (cons x (delq x file-alist))))
  10867. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10868. (org-install-agenda-files-menu)
  10869. (message "File %s to %s of agenda file list"
  10870. (if had "moved" "added") (if to-end "end" "front"))))
  10871. (defun org-remove-file (&optional file)
  10872. "Remove current file from the list of files in variable `org-agenda-files'.
  10873. These are the files which are being checked for agenda entries.
  10874. Optional argument FILE means, use this file instead of the current."
  10875. (interactive)
  10876. (let* ((org-agenda-skip-unavailable-files nil)
  10877. (file (or file buffer-file-name))
  10878. (true-file (file-truename file))
  10879. (afile (abbreviate-file-name file))
  10880. (files (delq nil (mapcar
  10881. (lambda (x)
  10882. (if (equal true-file
  10883. (file-truename x))
  10884. nil x))
  10885. (org-agenda-files t)))))
  10886. (if (not (= (length files) (length (org-agenda-files t))))
  10887. (progn
  10888. (org-store-new-agenda-file-list files)
  10889. (org-install-agenda-files-menu)
  10890. (message "Removed file: %s" afile))
  10891. (message "File was not in list: %s (not removed)" afile))))
  10892. (defun org-file-menu-entry (file)
  10893. (vector file (list 'find-file file) t))
  10894. (defun org-check-agenda-file (file)
  10895. "Make sure FILE exists. If not, ask user what to do."
  10896. (when (not (file-exists-p file))
  10897. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10898. (abbreviate-file-name file))
  10899. (let ((r (downcase (read-char-exclusive))))
  10900. (cond
  10901. ((equal r ?r)
  10902. (org-remove-file file)
  10903. (throw 'nextfile t))
  10904. (t (error "Abort"))))))
  10905. (defun org-get-agenda-file-buffer (file)
  10906. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10907. it to the list of buffers which might be released later."
  10908. (let ((buf (org-find-base-buffer-visiting file)))
  10909. (if buf
  10910. buf ; just return it
  10911. ;; Make a new buffer and remember it
  10912. (setq buf (find-file-noselect file))
  10913. (if buf (push buf org-agenda-new-buffers))
  10914. buf)))
  10915. (defun org-release-buffers (blist)
  10916. "Release all buffers in list, asking the user for confirmation when needed.
  10917. When a buffer is unmodified, it is just killed. When modified, it is saved
  10918. \(if the user agrees) and then killed."
  10919. (let (buf file)
  10920. (while (setq buf (pop blist))
  10921. (setq file (buffer-file-name buf))
  10922. (when (and (buffer-modified-p buf)
  10923. file
  10924. (y-or-n-p (format "Save file %s? " file)))
  10925. (with-current-buffer buf (save-buffer)))
  10926. (kill-buffer buf))))
  10927. (defun org-prepare-agenda-buffers (files)
  10928. "Create buffers for all agenda files, protect archived trees and comments."
  10929. (interactive)
  10930. (let ((pa '(:org-archived t))
  10931. (pc '(:org-comment t))
  10932. (pall '(:org-archived t :org-comment t))
  10933. (inhibit-read-only t)
  10934. (rea (concat ":" org-archive-tag ":"))
  10935. bmp file re)
  10936. (save-excursion
  10937. (save-restriction
  10938. (while (setq file (pop files))
  10939. (if (bufferp file)
  10940. (set-buffer file)
  10941. (org-check-agenda-file file)
  10942. (set-buffer (org-get-agenda-file-buffer file)))
  10943. (widen)
  10944. (setq bmp (buffer-modified-p))
  10945. (org-refresh-category-properties)
  10946. (setq org-todo-keywords-for-agenda
  10947. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10948. (setq org-done-keywords-for-agenda
  10949. (append org-done-keywords-for-agenda org-done-keywords))
  10950. (setq org-todo-keyword-alist-for-agenda
  10951. (append org-todo-keyword-alist-for-agenda org-todo-key-alist))
  10952. (setq org-tag-alist-for-agenda
  10953. (append org-tag-alist-for-agenda org-tag-alist))
  10954. (save-excursion
  10955. (remove-text-properties (point-min) (point-max) pall)
  10956. (when org-agenda-skip-archived-trees
  10957. (goto-char (point-min))
  10958. (while (re-search-forward rea nil t)
  10959. (if (org-on-heading-p t)
  10960. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10961. (goto-char (point-min))
  10962. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10963. (while (re-search-forward re nil t)
  10964. (add-text-properties
  10965. (match-beginning 0) (org-end-of-subtree t) pc)))
  10966. (set-buffer-modified-p bmp))))
  10967. (setq org-todo-keyword-alist-for-agenda
  10968. (org-uniquify org-todo-keyword-alist-for-agenda)
  10969. org-tag-alist-for-agenda (org-uniquify org-tag-alist-for-agenda))))
  10970. ;;;; Embedded LaTeX
  10971. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10972. "Keymap for the minor `org-cdlatex-mode'.")
  10973. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10974. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10975. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10976. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10977. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10978. (defvar org-cdlatex-texmathp-advice-is-done nil
  10979. "Flag remembering if we have applied the advice to texmathp already.")
  10980. (define-minor-mode org-cdlatex-mode
  10981. "Toggle the minor `org-cdlatex-mode'.
  10982. This mode supports entering LaTeX environment and math in LaTeX fragments
  10983. in Org-mode.
  10984. \\{org-cdlatex-mode-map}"
  10985. nil " OCDL" nil
  10986. (when org-cdlatex-mode (require 'cdlatex))
  10987. (unless org-cdlatex-texmathp-advice-is-done
  10988. (setq org-cdlatex-texmathp-advice-is-done t)
  10989. (defadvice texmathp (around org-math-always-on activate)
  10990. "Always return t in org-mode buffers.
  10991. This is because we want to insert math symbols without dollars even outside
  10992. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10993. en embedded LaTeX fragement, let texmathp do its job.
  10994. \\[org-cdlatex-mode-map]"
  10995. (interactive)
  10996. (let (p)
  10997. (cond
  10998. ((not (org-mode-p)) ad-do-it)
  10999. ((eq this-command 'cdlatex-math-symbol)
  11000. (setq ad-return-value t
  11001. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  11002. (t
  11003. (let ((p (org-inside-LaTeX-fragment-p)))
  11004. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  11005. (setq ad-return-value t
  11006. texmathp-why '("Org-mode embedded math" . 0))
  11007. (if p ad-do-it)))))))))
  11008. (defun turn-on-org-cdlatex ()
  11009. "Unconditionally turn on `org-cdlatex-mode'."
  11010. (org-cdlatex-mode 1))
  11011. (defun org-inside-LaTeX-fragment-p ()
  11012. "Test if point is inside a LaTeX fragment.
  11013. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  11014. sequence appearing also before point.
  11015. Even though the matchers for math are configurable, this function assumes
  11016. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  11017. delimiters are skipped when they have been removed by customization.
  11018. The return value is nil, or a cons cell with the delimiter and
  11019. and the position of this delimiter.
  11020. This function does a reasonably good job, but can locally be fooled by
  11021. for example currency specifications. For example it will assume being in
  11022. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  11023. fragments that are properly closed, but during editing, we have to live
  11024. with the uncertainty caused by missing closing delimiters. This function
  11025. looks only before point, not after."
  11026. (catch 'exit
  11027. (let ((pos (point))
  11028. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  11029. (lim (progn
  11030. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  11031. (point)))
  11032. dd-on str (start 0) m re)
  11033. (goto-char pos)
  11034. (when dodollar
  11035. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  11036. re (nth 1 (assoc "$" org-latex-regexps)))
  11037. (while (string-match re str start)
  11038. (cond
  11039. ((= (match-end 0) (length str))
  11040. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  11041. ((= (match-end 0) (- (length str) 5))
  11042. (throw 'exit nil))
  11043. (t (setq start (match-end 0))))))
  11044. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  11045. (goto-char pos)
  11046. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  11047. (and (match-beginning 2) (throw 'exit nil))
  11048. ;; count $$
  11049. (while (re-search-backward "\\$\\$" lim t)
  11050. (setq dd-on (not dd-on)))
  11051. (goto-char pos)
  11052. (if dd-on (cons "$$" m))))))
  11053. (defun org-try-cdlatex-tab ()
  11054. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  11055. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  11056. - inside a LaTeX fragment, or
  11057. - after the first word in a line, where an abbreviation expansion could
  11058. insert a LaTeX environment."
  11059. (when org-cdlatex-mode
  11060. (cond
  11061. ((save-excursion
  11062. (skip-chars-backward "a-zA-Z0-9*")
  11063. (skip-chars-backward " \t")
  11064. (bolp))
  11065. (cdlatex-tab) t)
  11066. ((org-inside-LaTeX-fragment-p)
  11067. (cdlatex-tab) t)
  11068. (t nil))))
  11069. (defun org-cdlatex-underscore-caret (&optional arg)
  11070. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  11071. Revert to the normal definition outside of these fragments."
  11072. (interactive "P")
  11073. (if (org-inside-LaTeX-fragment-p)
  11074. (call-interactively 'cdlatex-sub-superscript)
  11075. (let (org-cdlatex-mode)
  11076. (call-interactively (key-binding (vector last-input-event))))))
  11077. (defun org-cdlatex-math-modify (&optional arg)
  11078. "Execute `cdlatex-math-modify' in LaTeX fragments.
  11079. Revert to the normal definition outside of these fragments."
  11080. (interactive "P")
  11081. (if (org-inside-LaTeX-fragment-p)
  11082. (call-interactively 'cdlatex-math-modify)
  11083. (let (org-cdlatex-mode)
  11084. (call-interactively (key-binding (vector last-input-event))))))
  11085. (defvar org-latex-fragment-image-overlays nil
  11086. "List of overlays carrying the images of latex fragments.")
  11087. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  11088. (defun org-remove-latex-fragment-image-overlays ()
  11089. "Remove all overlays with LaTeX fragment images in current buffer."
  11090. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  11091. (setq org-latex-fragment-image-overlays nil))
  11092. (defun org-preview-latex-fragment (&optional subtree)
  11093. "Preview the LaTeX fragment at point, or all locally or globally.
  11094. If the cursor is in a LaTeX fragment, create the image and overlay
  11095. it over the source code. If there is no fragment at point, display
  11096. all fragments in the current text, from one headline to the next. With
  11097. prefix SUBTREE, display all fragments in the current subtree. With a
  11098. double prefix `C-u C-u', or when the cursor is before the first headline,
  11099. display all fragments in the buffer.
  11100. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  11101. (interactive "P")
  11102. (org-remove-latex-fragment-image-overlays)
  11103. (save-excursion
  11104. (save-restriction
  11105. (let (beg end at msg)
  11106. (cond
  11107. ((or (equal subtree '(16))
  11108. (not (save-excursion
  11109. (re-search-backward (concat "^" outline-regexp) nil t))))
  11110. (setq beg (point-min) end (point-max)
  11111. msg "Creating images for buffer...%s"))
  11112. ((equal subtree '(4))
  11113. (org-back-to-heading)
  11114. (setq beg (point) end (org-end-of-subtree t)
  11115. msg "Creating images for subtree...%s"))
  11116. (t
  11117. (if (setq at (org-inside-LaTeX-fragment-p))
  11118. (goto-char (max (point-min) (- (cdr at) 2)))
  11119. (org-back-to-heading))
  11120. (setq beg (point) end (progn (outline-next-heading) (point))
  11121. msg (if at "Creating image...%s"
  11122. "Creating images for entry...%s"))))
  11123. (message msg "")
  11124. (narrow-to-region beg end)
  11125. (goto-char beg)
  11126. (org-format-latex
  11127. (concat "ltxpng/" (file-name-sans-extension
  11128. (file-name-nondirectory
  11129. buffer-file-name)))
  11130. default-directory 'overlays msg at 'forbuffer)
  11131. (message msg "done. Use `C-c C-c' to remove images.")))))
  11132. (defvar org-latex-regexps
  11133. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  11134. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  11135. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  11136. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  11137. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  11138. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  11139. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  11140. "Regular expressions for matching embedded LaTeX.")
  11141. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  11142. "Replace LaTeX fragments with links to an image, and produce images."
  11143. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  11144. (let* ((prefixnodir (file-name-nondirectory prefix))
  11145. (absprefix (expand-file-name prefix dir))
  11146. (todir (file-name-directory absprefix))
  11147. (opt org-format-latex-options)
  11148. (matchers (plist-get opt :matchers))
  11149. (re-list org-latex-regexps)
  11150. (cnt 0) txt link beg end re e checkdir
  11151. m n block linkfile movefile ov)
  11152. ;; Check if there are old images files with this prefix, and remove them
  11153. (when (file-directory-p todir)
  11154. (mapc 'delete-file
  11155. (directory-files
  11156. todir 'full
  11157. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  11158. ;; Check the different regular expressions
  11159. (while (setq e (pop re-list))
  11160. (setq m (car e) re (nth 1 e) n (nth 2 e)
  11161. block (if (nth 3 e) "\n\n" ""))
  11162. (when (member m matchers)
  11163. (goto-char (point-min))
  11164. (while (re-search-forward re nil t)
  11165. (when (or (not at) (equal (cdr at) (match-beginning n)))
  11166. (setq txt (match-string n)
  11167. beg (match-beginning n) end (match-end n)
  11168. cnt (1+ cnt)
  11169. linkfile (format "%s_%04d.png" prefix cnt)
  11170. movefile (format "%s_%04d.png" absprefix cnt)
  11171. link (concat block "[[file:" linkfile "]]" block))
  11172. (if msg (message msg cnt))
  11173. (goto-char beg)
  11174. (unless checkdir ; make sure the directory exists
  11175. (setq checkdir t)
  11176. (or (file-directory-p todir) (make-directory todir)))
  11177. (org-create-formula-image
  11178. txt movefile opt forbuffer)
  11179. (if overlays
  11180. (progn
  11181. (setq ov (org-make-overlay beg end))
  11182. (if (featurep 'xemacs)
  11183. (progn
  11184. (org-overlay-put ov 'invisible t)
  11185. (org-overlay-put
  11186. ov 'end-glyph
  11187. (make-glyph (vector 'png :file movefile))))
  11188. (org-overlay-put
  11189. ov 'display
  11190. (list 'image :type 'png :file movefile :ascent 'center)))
  11191. (push ov org-latex-fragment-image-overlays)
  11192. (goto-char end))
  11193. (delete-region beg end)
  11194. (insert link))))))))
  11195. ;; This function borrows from Ganesh Swami's latex2png.el
  11196. (defun org-create-formula-image (string tofile options buffer)
  11197. (let* ((tmpdir (if (featurep 'xemacs)
  11198. (temp-directory)
  11199. temporary-file-directory))
  11200. (texfilebase (make-temp-name
  11201. (expand-file-name "orgtex" tmpdir)))
  11202. (texfile (concat texfilebase ".tex"))
  11203. (dvifile (concat texfilebase ".dvi"))
  11204. (pngfile (concat texfilebase ".png"))
  11205. (fnh (if (featurep 'xemacs)
  11206. (font-height (get-face-font 'default))
  11207. (face-attribute 'default :height nil)))
  11208. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  11209. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  11210. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  11211. "Black"))
  11212. (bg (or (plist-get options (if buffer :background :html-background))
  11213. "Transparent")))
  11214. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  11215. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  11216. (with-temp-file texfile
  11217. (insert org-format-latex-header
  11218. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  11219. (let ((dir default-directory))
  11220. (condition-case nil
  11221. (progn
  11222. (cd tmpdir)
  11223. (call-process "latex" nil nil nil texfile))
  11224. (error nil))
  11225. (cd dir))
  11226. (if (not (file-exists-p dvifile))
  11227. (progn (message "Failed to create dvi file from %s" texfile) nil)
  11228. (condition-case nil
  11229. (call-process "dvipng" nil nil nil
  11230. "-E" "-fg" fg "-bg" bg
  11231. "-D" dpi
  11232. ;;"-x" scale "-y" scale
  11233. "-T" "tight"
  11234. "-o" pngfile
  11235. dvifile)
  11236. (error nil))
  11237. (if (not (file-exists-p pngfile))
  11238. (progn (message "Failed to create png file from %s" texfile) nil)
  11239. ;; Use the requested file name and clean up
  11240. (copy-file pngfile tofile 'replace)
  11241. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  11242. (delete-file (concat texfilebase e)))
  11243. pngfile))))
  11244. (defun org-dvipng-color (attr)
  11245. "Return an rgb color specification for dvipng."
  11246. (apply 'format "rgb %s %s %s"
  11247. (mapcar 'org-normalize-color
  11248. (color-values (face-attribute 'default attr nil)))))
  11249. (defun org-normalize-color (value)
  11250. "Return string to be used as color value for an RGB component."
  11251. (format "%g" (/ value 65535.0)))
  11252. ;;;; Key bindings
  11253. ;; Make `C-c C-x' a prefix key
  11254. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  11255. ;; TAB key with modifiers
  11256. (org-defkey org-mode-map "\C-i" 'org-cycle)
  11257. (org-defkey org-mode-map [(tab)] 'org-cycle)
  11258. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  11259. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  11260. (org-defkey org-mode-map "\M-\t" 'org-complete)
  11261. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  11262. ;; The following line is necessary under Suse GNU/Linux
  11263. (unless (featurep 'xemacs)
  11264. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  11265. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  11266. (define-key org-mode-map [backtab] 'org-shifttab)
  11267. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  11268. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  11269. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  11270. ;; Cursor keys with modifiers
  11271. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  11272. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  11273. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  11274. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  11275. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  11276. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  11277. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  11278. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  11279. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  11280. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  11281. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  11282. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  11283. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  11284. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  11285. ;;; Extra keys for tty access.
  11286. ;; We only set them when really needed because otherwise the
  11287. ;; menus don't show the simple keys
  11288. (when (or org-use-extra-keys
  11289. (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  11290. (not window-system))
  11291. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  11292. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  11293. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  11294. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  11295. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  11296. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  11297. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  11298. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  11299. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  11300. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  11301. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  11302. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  11303. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  11304. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  11305. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  11306. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  11307. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  11308. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  11309. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  11310. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  11311. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  11312. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  11313. ;; All the other keys
  11314. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  11315. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  11316. (if (boundp 'narrow-map)
  11317. (org-defkey narrow-map "s" 'org-narrow-to-subtree)
  11318. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree))
  11319. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  11320. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  11321. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  11322. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  11323. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  11324. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  11325. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  11326. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  11327. (org-defkey org-mode-map "\C-c\C-q" 'org-set-tags-command)
  11328. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  11329. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  11330. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  11331. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  11332. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  11333. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  11334. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  11335. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  11336. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  11337. (org-defkey org-mode-map [(control return)] 'org-insert-heading-respect-content)
  11338. (org-defkey org-mode-map [(shift control return)] 'org-insert-todo-heading-respect-content)
  11339. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  11340. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  11341. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  11342. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  11343. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  11344. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  11345. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  11346. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  11347. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  11348. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  11349. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  11350. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  11351. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  11352. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  11353. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  11354. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  11355. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  11356. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  11357. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  11358. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  11359. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  11360. (org-defkey org-mode-map "\C-c^" 'org-sort)
  11361. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  11362. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  11363. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  11364. (org-defkey org-mode-map "\C-m" 'org-return)
  11365. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  11366. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  11367. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  11368. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  11369. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  11370. (org-defkey org-mode-map "\C-c'" 'org-edit-special)
  11371. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  11372. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  11373. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  11374. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  11375. (org-defkey org-mode-map "\C-c\C-a" 'org-attach)
  11376. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  11377. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  11378. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  11379. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  11380. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  11381. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-mark-entry-for-agenda-action)
  11382. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  11383. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  11384. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  11385. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  11386. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  11387. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  11388. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  11389. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  11390. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  11391. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  11392. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  11393. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  11394. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  11395. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  11396. (org-defkey org-mode-map "\C-c\C-xi" 'org-insert-columns-dblock)
  11397. (org-defkey org-mode-map "\C-c\C-x." 'org-timer)
  11398. (org-defkey org-mode-map "\C-c\C-x-" 'org-timer-item)
  11399. (org-defkey org-mode-map "\C-c\C-x0" 'org-timer-start)
  11400. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  11401. (when (featurep 'xemacs)
  11402. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  11403. (defvar org-table-auto-blank-field) ; defined in org-table.el
  11404. (defun org-self-insert-command (N)
  11405. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  11406. If the cursor is in a table looking at whitespace, the whitespace is
  11407. overwritten, and the table is not marked as requiring realignment."
  11408. (interactive "p")
  11409. (if (and (org-table-p)
  11410. (progn
  11411. ;; check if we blank the field, and if that triggers align
  11412. (and (featurep 'org-table) org-table-auto-blank-field
  11413. (member last-command
  11414. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  11415. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  11416. ;; got extra space, this field does not determine column width
  11417. (let (org-table-may-need-update) (org-table-blank-field))
  11418. ;; no extra space, this field may determine column width
  11419. (org-table-blank-field)))
  11420. t)
  11421. (eq N 1)
  11422. (looking-at "[^|\n]* |"))
  11423. (let (org-table-may-need-update)
  11424. (goto-char (1- (match-end 0)))
  11425. (delete-backward-char 1)
  11426. (goto-char (match-beginning 0))
  11427. (self-insert-command N))
  11428. (setq org-table-may-need-update t)
  11429. (self-insert-command N)
  11430. (org-fix-tags-on-the-fly)))
  11431. (defun org-fix-tags-on-the-fly ()
  11432. (when (and (equal (char-after (point-at-bol)) ?*)
  11433. (org-on-heading-p))
  11434. (org-align-tags-here org-tags-column)))
  11435. (defun org-delete-backward-char (N)
  11436. "Like `delete-backward-char', insert whitespace at field end in tables.
  11437. When deleting backwards, in tables this function will insert whitespace in
  11438. front of the next \"|\" separator, to keep the table aligned. The table will
  11439. still be marked for re-alignment if the field did fill the entire column,
  11440. because, in this case the deletion might narrow the column."
  11441. (interactive "p")
  11442. (if (and (org-table-p)
  11443. (eq N 1)
  11444. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11445. (looking-at ".*?|"))
  11446. (let ((pos (point))
  11447. (noalign (looking-at "[^|\n\r]* |"))
  11448. (c org-table-may-need-update))
  11449. (backward-delete-char N)
  11450. (skip-chars-forward "^|")
  11451. (insert " ")
  11452. (goto-char (1- pos))
  11453. ;; noalign: if there were two spaces at the end, this field
  11454. ;; does not determine the width of the column.
  11455. (if noalign (setq org-table-may-need-update c)))
  11456. (backward-delete-char N)
  11457. (org-fix-tags-on-the-fly)))
  11458. (defun org-delete-char (N)
  11459. "Like `delete-char', but insert whitespace at field end in tables.
  11460. When deleting characters, in tables this function will insert whitespace in
  11461. front of the next \"|\" separator, to keep the table aligned. The table will
  11462. still be marked for re-alignment if the field did fill the entire column,
  11463. because, in this case the deletion might narrow the column."
  11464. (interactive "p")
  11465. (if (and (org-table-p)
  11466. (not (bolp))
  11467. (not (= (char-after) ?|))
  11468. (eq N 1))
  11469. (if (looking-at ".*?|")
  11470. (let ((pos (point))
  11471. (noalign (looking-at "[^|\n\r]* |"))
  11472. (c org-table-may-need-update))
  11473. (replace-match (concat
  11474. (substring (match-string 0) 1 -1)
  11475. " |"))
  11476. (goto-char pos)
  11477. ;; noalign: if there were two spaces at the end, this field
  11478. ;; does not determine the width of the column.
  11479. (if noalign (setq org-table-may-need-update c)))
  11480. (delete-char N))
  11481. (delete-char N)
  11482. (org-fix-tags-on-the-fly)))
  11483. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11484. (put 'org-self-insert-command 'delete-selection t)
  11485. (put 'orgtbl-self-insert-command 'delete-selection t)
  11486. (put 'org-delete-char 'delete-selection 'supersede)
  11487. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11488. ;; Make `flyspell-mode' delay after some commands
  11489. (put 'org-self-insert-command 'flyspell-delayed t)
  11490. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11491. (put 'org-delete-char 'flyspell-delayed t)
  11492. (put 'org-delete-backward-char 'flyspell-delayed t)
  11493. ;; Make pabbrev-mode expand after org-mode commands
  11494. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11495. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11496. ;; How to do this: Measure non-white length of current string
  11497. ;; If equal to column width, we should realign.
  11498. (defun org-remap (map &rest commands)
  11499. "In MAP, remap the functions given in COMMANDS.
  11500. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11501. (let (new old)
  11502. (while commands
  11503. (setq old (pop commands) new (pop commands))
  11504. (if (fboundp 'command-remapping)
  11505. (org-defkey map (vector 'remap old) new)
  11506. (substitute-key-definition old new map global-map)))))
  11507. (when (eq org-enable-table-editor 'optimized)
  11508. ;; If the user wants maximum table support, we need to hijack
  11509. ;; some standard editing functions
  11510. (org-remap org-mode-map
  11511. 'self-insert-command 'org-self-insert-command
  11512. 'delete-char 'org-delete-char
  11513. 'delete-backward-char 'org-delete-backward-char)
  11514. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11515. (defun org-shiftcursor-error ()
  11516. "Throw an error because Shift-Cursor command was applied in wrong context."
  11517. (error "This command is active in special context like tables, headlines or timestamps"))
  11518. (defun org-shifttab (&optional arg)
  11519. "Global visibility cycling or move to previous table field.
  11520. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11521. on context.
  11522. See the individual commands for more information."
  11523. (interactive "P")
  11524. (cond
  11525. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11526. ((integerp arg)
  11527. (message "Content view to level: %d" arg)
  11528. (org-content (prefix-numeric-value arg))
  11529. (setq org-cycle-global-status 'overview))
  11530. (t (call-interactively 'org-global-cycle))))
  11531. (defun org-shiftmetaleft ()
  11532. "Promote subtree or delete table column.
  11533. Calls `org-promote-subtree', `org-outdent-item',
  11534. or `org-table-delete-column', depending on context.
  11535. See the individual commands for more information."
  11536. (interactive)
  11537. (cond
  11538. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11539. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11540. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11541. (t (org-shiftcursor-error))))
  11542. (defun org-shiftmetaright ()
  11543. "Demote subtree or insert table column.
  11544. Calls `org-demote-subtree', `org-indent-item',
  11545. or `org-table-insert-column', depending on context.
  11546. See the individual commands for more information."
  11547. (interactive)
  11548. (cond
  11549. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11550. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11551. ((org-at-item-p) (call-interactively 'org-indent-item))
  11552. (t (org-shiftcursor-error))))
  11553. (defun org-shiftmetaup (&optional arg)
  11554. "Move subtree up or kill table row.
  11555. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11556. `org-move-item-up' depending on context. See the individual commands
  11557. for more information."
  11558. (interactive "P")
  11559. (cond
  11560. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11561. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11562. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11563. (t (org-shiftcursor-error))))
  11564. (defun org-shiftmetadown (&optional arg)
  11565. "Move subtree down or insert table row.
  11566. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11567. `org-move-item-down', depending on context. See the individual
  11568. commands for more information."
  11569. (interactive "P")
  11570. (cond
  11571. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11572. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11573. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11574. (t (org-shiftcursor-error))))
  11575. (defun org-metaleft (&optional arg)
  11576. "Promote heading or move table column to left.
  11577. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11578. With no specific context, calls the Emacs default `backward-word'.
  11579. See the individual commands for more information."
  11580. (interactive "P")
  11581. (cond
  11582. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11583. ((or (org-on-heading-p) (org-region-active-p))
  11584. (call-interactively 'org-do-promote))
  11585. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11586. (t (call-interactively 'backward-word))))
  11587. (defun org-metaright (&optional arg)
  11588. "Demote subtree or move table column to right.
  11589. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11590. With no specific context, calls the Emacs default `forward-word'.
  11591. See the individual commands for more information."
  11592. (interactive "P")
  11593. (cond
  11594. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11595. ((or (org-on-heading-p) (org-region-active-p))
  11596. (call-interactively 'org-do-demote))
  11597. ((org-at-item-p) (call-interactively 'org-indent-item))
  11598. (t (call-interactively 'forward-word))))
  11599. (defun org-metaup (&optional arg)
  11600. "Move subtree up or move table row up.
  11601. Calls `org-move-subtree-up' or `org-table-move-row' or
  11602. `org-move-item-up', depending on context. See the individual commands
  11603. for more information."
  11604. (interactive "P")
  11605. (cond
  11606. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11607. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11608. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11609. (t (transpose-lines 1) (beginning-of-line -1))))
  11610. (defun org-metadown (&optional arg)
  11611. "Move subtree down or move table row down.
  11612. Calls `org-move-subtree-down' or `org-table-move-row' or
  11613. `org-move-item-down', depending on context. See the individual
  11614. commands for more information."
  11615. (interactive "P")
  11616. (cond
  11617. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11618. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11619. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11620. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11621. (defun org-shiftup (&optional arg)
  11622. "Increase item in timestamp or increase priority of current headline.
  11623. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11624. depending on context. See the individual commands for more information."
  11625. (interactive "P")
  11626. (cond
  11627. ((org-at-timestamp-p t)
  11628. (call-interactively (if org-edit-timestamp-down-means-later
  11629. 'org-timestamp-down 'org-timestamp-up)))
  11630. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11631. ((org-at-item-p) (call-interactively 'org-previous-item))
  11632. ((org-clocktable-try-shift 'up arg))
  11633. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11634. (defun org-shiftdown (&optional arg)
  11635. "Decrease item in timestamp or decrease priority of current headline.
  11636. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11637. depending on context. See the individual commands for more information."
  11638. (interactive "P")
  11639. (cond
  11640. ((org-at-timestamp-p t)
  11641. (call-interactively (if org-edit-timestamp-down-means-later
  11642. 'org-timestamp-up 'org-timestamp-down)))
  11643. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11644. ((org-clocktable-try-shift 'down arg))
  11645. (t (call-interactively 'org-next-item))))
  11646. (defun org-shiftright (&optional arg)
  11647. "Cycle the thing at point or in the current line, depending on context.
  11648. Depending on context, this does one of the following:
  11649. - switch a timestamp at point one day into the future
  11650. - on a headline, switch to the next TODO keyword.
  11651. - on an item, switch entire list to the next bullet type
  11652. - on a property line, switch to the next allowed value
  11653. - on a clocktable definition line, move time block into the future"
  11654. (interactive "P")
  11655. (cond
  11656. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11657. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11658. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11659. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11660. ((org-clocktable-try-shift 'right arg))
  11661. (t (org-shiftcursor-error))))
  11662. (defun org-shiftleft (&optional arg)
  11663. "Cycle the thing at point or in the current line, depending on context.
  11664. Depending on context, this does one of the following:
  11665. - switch a timestamp at point one day into the past
  11666. - on a headline, switch to the previous TODO keyword.
  11667. - on an item, switch entire list to the previous bullet type
  11668. - on a property line, switch to the previous allowed value
  11669. - on a clocktable definition line, move time block into the past"
  11670. (interactive "P")
  11671. (cond
  11672. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11673. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11674. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11675. ((org-at-property-p)
  11676. (call-interactively 'org-property-previous-allowed-value))
  11677. ((org-clocktable-try-shift 'left arg))
  11678. (t (org-shiftcursor-error))))
  11679. (defun org-shiftcontrolright ()
  11680. "Switch to next TODO set."
  11681. (interactive)
  11682. (cond
  11683. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11684. (t (org-shiftcursor-error))))
  11685. (defun org-shiftcontrolleft ()
  11686. "Switch to previous TODO set."
  11687. (interactive)
  11688. (cond
  11689. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11690. (t (org-shiftcursor-error))))
  11691. (defun org-ctrl-c-ret ()
  11692. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11693. (interactive)
  11694. (cond
  11695. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11696. (t (call-interactively 'org-insert-heading))))
  11697. (defun org-copy-special ()
  11698. "Copy region in table or copy current subtree.
  11699. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11700. See the individual commands for more information."
  11701. (interactive)
  11702. (call-interactively
  11703. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11704. (defun org-cut-special ()
  11705. "Cut region in table or cut current subtree.
  11706. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11707. See the individual commands for more information."
  11708. (interactive)
  11709. (call-interactively
  11710. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11711. (defun org-paste-special (arg)
  11712. "Paste rectangular region into table, or past subtree relative to level.
  11713. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11714. See the individual commands for more information."
  11715. (interactive "P")
  11716. (if (org-at-table-p)
  11717. (org-table-paste-rectangle)
  11718. (org-paste-subtree arg)))
  11719. (defun org-edit-special ()
  11720. "Call a special editor for the stuff at point.
  11721. When at a table, call the formula editor with `org-table-edit-formulas'.
  11722. When at the first line of an src example, call `org-edit-src-code'.
  11723. When in an #+include line, visit the include file. Otherwise call
  11724. `ffap' to visit the file at point."
  11725. (interactive)
  11726. (cond
  11727. ((org-at-table-p)
  11728. (call-interactively 'org-table-edit-formulas))
  11729. ((save-excursion
  11730. (beginning-of-line 1)
  11731. (looking-at "\\(?:#\\+\\(?:setupfile\\|include\\):?[ \t]+\"?\\|[ \t]*<include\\>.*?file=\"\\)\\([^\"\n>]+\\)"))
  11732. (find-file (org-trim (match-string 1))))
  11733. ((org-edit-src-code))
  11734. ((org-edit-fixed-width-region))
  11735. (t (call-interactively 'ffap))))
  11736. (defun org-ctrl-c-ctrl-c (&optional arg)
  11737. "Set tags in headline, or update according to changed information at point.
  11738. This command does many different things, depending on context:
  11739. - If the cursor is in a headline, prompt for tags and insert them
  11740. into the current line, aligned to `org-tags-column'. When called
  11741. with prefix arg, realign all tags in the current buffer.
  11742. - If the cursor is in one of the special #+KEYWORD lines, this
  11743. triggers scanning the buffer for these lines and updating the
  11744. information.
  11745. - If the cursor is inside a table, realign the table. This command
  11746. works even if the automatic table editor has been turned off.
  11747. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11748. the entire table.
  11749. - If the cursor is a the beginning of a dynamic block, update it.
  11750. - If the cursor is inside a table created by the table.el package,
  11751. activate that table.
  11752. - If the current buffer is a remember buffer, close note and file
  11753. it. A prefix argument of 1 files to the default location
  11754. without further interaction. A prefix argument of 2 files to
  11755. the currently clocking task.
  11756. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11757. links in this buffer.
  11758. - If the cursor is on a numbered item in a plain list, renumber the
  11759. ordered list.
  11760. - If the cursor is on a checkbox, toggle it."
  11761. (interactive "P")
  11762. (let ((org-enable-table-editor t))
  11763. (cond
  11764. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11765. org-occur-highlights
  11766. org-latex-fragment-image-overlays)
  11767. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11768. (org-remove-occur-highlights)
  11769. (org-remove-latex-fragment-image-overlays)
  11770. (message "Temporary highlights/overlays removed from current buffer"))
  11771. ((and (local-variable-p 'org-finish-function (current-buffer))
  11772. (fboundp org-finish-function))
  11773. (funcall org-finish-function))
  11774. ((org-at-property-p)
  11775. (call-interactively 'org-property-action))
  11776. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11777. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11778. ((org-at-table.el-p)
  11779. (require 'table)
  11780. (beginning-of-line 1)
  11781. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11782. (call-interactively 'table-recognize-table))
  11783. ((org-at-table-p)
  11784. (org-table-maybe-eval-formula)
  11785. (if arg
  11786. (call-interactively 'org-table-recalculate)
  11787. (org-table-maybe-recalculate-line))
  11788. (call-interactively 'org-table-align))
  11789. ((org-at-item-checkbox-p)
  11790. (call-interactively 'org-toggle-checkbox))
  11791. ((org-at-item-p)
  11792. (call-interactively 'org-maybe-renumber-ordered-list))
  11793. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11794. ;; Dynamic block
  11795. (beginning-of-line 1)
  11796. (save-excursion (org-update-dblock)))
  11797. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11798. (cond
  11799. ((equal (match-string 1) "TBLFM")
  11800. ;; Recalculate the table before this line
  11801. (save-excursion
  11802. (beginning-of-line 1)
  11803. (skip-chars-backward " \r\n\t")
  11804. (if (org-at-table-p)
  11805. (org-call-with-arg 'org-table-recalculate t))))
  11806. (t
  11807. ; (org-set-regexps-and-options)
  11808. ; (org-restart-font-lock)
  11809. (let ((org-inhibit-startup t)) (org-mode-restart))
  11810. (message "Local setup has been refreshed"))))
  11811. (t (error "C-c C-c can do nothing useful at this location.")))))
  11812. (defun org-mode-restart ()
  11813. "Restart Org-mode, to scan again for special lines.
  11814. Also updates the keyword regular expressions."
  11815. (interactive)
  11816. (org-mode)
  11817. (message "Org-mode restarted"))
  11818. (defun org-kill-note-or-show-branches ()
  11819. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11820. (interactive)
  11821. (if (not org-finish-function)
  11822. (call-interactively 'show-branches)
  11823. (let ((org-note-abort t))
  11824. (funcall org-finish-function))))
  11825. (defun org-return (&optional indent)
  11826. "Goto next table row or insert a newline.
  11827. Calls `org-table-next-row' or `newline', depending on context.
  11828. See the individual commands for more information."
  11829. (interactive)
  11830. (cond
  11831. ((bobp) (if indent (newline-and-indent) (newline)))
  11832. ((and (org-at-heading-p)
  11833. (looking-at
  11834. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11835. (org-show-entry)
  11836. (end-of-line 1)
  11837. (newline))
  11838. ((org-at-table-p)
  11839. (org-table-justify-field-maybe)
  11840. (call-interactively 'org-table-next-row))
  11841. (t (if indent (newline-and-indent) (newline)))))
  11842. (defun org-return-indent ()
  11843. "Goto next table row or insert a newline and indent.
  11844. Calls `org-table-next-row' or `newline-and-indent', depending on
  11845. context. See the individual commands for more information."
  11846. (interactive)
  11847. (org-return t))
  11848. (defun org-ctrl-c-star ()
  11849. "Compute table, or change heading status of lines.
  11850. Calls `org-table-recalculate' or `org-toggle-region-headings',
  11851. depending on context. This will also turn a plain list item or a normal
  11852. line into a subheading."
  11853. (interactive)
  11854. (cond
  11855. ((org-at-table-p)
  11856. (call-interactively 'org-table-recalculate))
  11857. ((org-region-active-p)
  11858. ;; Convert all lines in region to list items
  11859. (call-interactively 'org-toggle-region-headings))
  11860. ((org-on-heading-p)
  11861. (org-toggle-region-headings (point-at-bol)
  11862. (min (1+ (point-at-eol)) (point-max))))
  11863. ((org-at-item-p)
  11864. ;; Convert to heading
  11865. (let ((level (save-match-data
  11866. (save-excursion
  11867. (condition-case nil
  11868. (progn
  11869. (org-back-to-heading t)
  11870. (funcall outline-level))
  11871. (error 0))))))
  11872. (replace-match
  11873. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11874. (t (org-toggle-region-headings (point-at-bol)
  11875. (min (1+ (point-at-eol)) (point-max))))))
  11876. (defun org-ctrl-c-minus ()
  11877. "Insert separator line in table or modify bullet status of line.
  11878. Also turns a plain line or a region of lines into list items.
  11879. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11880. `org-cycle-list-bullet', depending on context."
  11881. (interactive)
  11882. (cond
  11883. ((org-at-table-p)
  11884. (call-interactively 'org-table-insert-hline))
  11885. ((org-on-heading-p)
  11886. ;; Convert to item
  11887. (save-excursion
  11888. (beginning-of-line 1)
  11889. (if (looking-at "\\*+ ")
  11890. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11891. ((org-region-active-p)
  11892. ;; Convert all lines in region to list items
  11893. (call-interactively 'org-toggle-region-items))
  11894. ((org-in-item-p)
  11895. (call-interactively 'org-cycle-list-bullet))
  11896. (t (org-toggle-region-items (point-at-bol)
  11897. (min (1+ (point-at-eol)) (point-max))))))
  11898. (defun org-toggle-region-items (beg end)
  11899. "Convert all lines in region to list items.
  11900. If the first line is already an item, convert all list items in the region
  11901. to normal lines."
  11902. (interactive "r")
  11903. (let (l2 l)
  11904. (save-excursion
  11905. (goto-char end)
  11906. (setq l2 (org-current-line))
  11907. (goto-char beg)
  11908. (beginning-of-line 1)
  11909. (setq l (1- (org-current-line)))
  11910. (if (org-at-item-p)
  11911. ;; We already have items, de-itemize
  11912. (while (< (setq l (1+ l)) l2)
  11913. (when (org-at-item-p)
  11914. (goto-char (match-beginning 2))
  11915. (delete-region (match-beginning 2) (match-end 2))
  11916. (and (looking-at "[ \t]+") (replace-match "")))
  11917. (beginning-of-line 2))
  11918. (while (< (setq l (1+ l)) l2)
  11919. (unless (org-at-item-p)
  11920. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11921. (replace-match "\\1- \\2")))
  11922. (beginning-of-line 2))))))
  11923. (defun org-toggle-region-headings (beg end)
  11924. "Convert all lines in region to list items.
  11925. If the first line is already an item, convert all list items in the region
  11926. to normal lines."
  11927. (interactive "r")
  11928. (let (l2 l)
  11929. (save-excursion
  11930. (goto-char end)
  11931. (setq l2 (org-current-line))
  11932. (goto-char beg)
  11933. (beginning-of-line 1)
  11934. (setq l (1- (org-current-line)))
  11935. (if (org-on-heading-p)
  11936. ;; We already have headlines, de-star them
  11937. (while (< (setq l (1+ l)) l2)
  11938. (when (org-on-heading-p t)
  11939. (and (looking-at outline-regexp) (replace-match "")))
  11940. (beginning-of-line 2))
  11941. (let* ((stars (save-excursion
  11942. (re-search-backward org-complex-heading-regexp nil t)
  11943. (or (match-string 1) "*")))
  11944. (add-stars (if org-odd-levels-only "**" "*"))
  11945. (rpl (concat stars add-stars " \\2")))
  11946. (while (< (setq l (1+ l)) l2)
  11947. (unless (org-on-heading-p)
  11948. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11949. (replace-match rpl)))
  11950. (beginning-of-line 2)))))))
  11951. (defun org-meta-return (&optional arg)
  11952. "Insert a new heading or wrap a region in a table.
  11953. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11954. See the individual commands for more information."
  11955. (interactive "P")
  11956. (cond
  11957. ((org-at-table-p)
  11958. (call-interactively 'org-table-wrap-region))
  11959. (t (call-interactively 'org-insert-heading))))
  11960. ;;; Menu entries
  11961. ;; Define the Org-mode menus
  11962. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11963. '("Tbl"
  11964. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11965. ["Next Field" org-cycle (org-at-table-p)]
  11966. ["Previous Field" org-shifttab (org-at-table-p)]
  11967. ["Next Row" org-return (org-at-table-p)]
  11968. "--"
  11969. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11970. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11971. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11972. "--"
  11973. ("Column"
  11974. ["Move Column Left" org-metaleft (org-at-table-p)]
  11975. ["Move Column Right" org-metaright (org-at-table-p)]
  11976. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11977. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11978. ("Row"
  11979. ["Move Row Up" org-metaup (org-at-table-p)]
  11980. ["Move Row Down" org-metadown (org-at-table-p)]
  11981. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11982. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11983. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11984. "--"
  11985. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11986. ("Rectangle"
  11987. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11988. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11989. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11990. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11991. "--"
  11992. ("Calculate"
  11993. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11994. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11995. ["Edit Formulas" org-edit-special (org-at-table-p)]
  11996. "--"
  11997. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11998. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11999. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  12000. "--"
  12001. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  12002. "--"
  12003. ["Sum Column/Rectangle" org-table-sum
  12004. (or (org-at-table-p) (org-region-active-p))]
  12005. ["Which Column?" org-table-current-column (org-at-table-p)])
  12006. ["Debug Formulas"
  12007. org-table-toggle-formula-debugger
  12008. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  12009. ["Show Col/Row Numbers"
  12010. org-table-toggle-coordinate-overlays
  12011. :style toggle
  12012. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  12013. "--"
  12014. ["Create" org-table-create (and (not (org-at-table-p))
  12015. org-enable-table-editor)]
  12016. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  12017. ["Import from File" org-table-import (not (org-at-table-p))]
  12018. ["Export to File" org-table-export (org-at-table-p)]
  12019. "--"
  12020. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  12021. (easy-menu-define org-org-menu org-mode-map "Org menu"
  12022. '("Org"
  12023. ("Show/Hide"
  12024. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  12025. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  12026. ["Sparse Tree..." org-sparse-tree t]
  12027. ["Reveal Context" org-reveal t]
  12028. ["Show All" show-all t]
  12029. "--"
  12030. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  12031. "--"
  12032. ["New Heading" org-insert-heading t]
  12033. ("Navigate Headings"
  12034. ["Up" outline-up-heading t]
  12035. ["Next" outline-next-visible-heading t]
  12036. ["Previous" outline-previous-visible-heading t]
  12037. ["Next Same Level" outline-forward-same-level t]
  12038. ["Previous Same Level" outline-backward-same-level t]
  12039. "--"
  12040. ["Jump" org-goto t])
  12041. ("Edit Structure"
  12042. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  12043. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  12044. "--"
  12045. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  12046. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  12047. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  12048. "--"
  12049. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  12050. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  12051. ["Demote Heading" org-metaright (not (org-at-table-p))]
  12052. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  12053. "--"
  12054. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  12055. "--"
  12056. ["Convert to odd levels" org-convert-to-odd-levels t]
  12057. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  12058. ("Editing"
  12059. ["Emphasis..." org-emphasize t]
  12060. ["Edit Source Example" org-edit-special t])
  12061. ("Archive"
  12062. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  12063. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  12064. ; :active t :keys "C-u C-c C-x C-a"]
  12065. ["Sparse trees open ARCHIVE trees"
  12066. (setq org-sparse-tree-open-archived-trees
  12067. (not org-sparse-tree-open-archived-trees))
  12068. :style toggle :selected org-sparse-tree-open-archived-trees]
  12069. ["Cycling opens ARCHIVE trees"
  12070. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  12071. :style toggle :selected org-cycle-open-archived-trees]
  12072. "--"
  12073. ["Move subtree to archive sibling" org-archive-to-archive-sibling t]
  12074. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  12075. ; ["Check and Move Children" (org-archive-subtree '(4))
  12076. ; :active t :keys "C-u C-c C-x C-s"]
  12077. )
  12078. "--"
  12079. ("TODO Lists"
  12080. ["TODO/DONE/-" org-todo t]
  12081. ("Select keyword"
  12082. ["Next keyword" org-shiftright (org-on-heading-p)]
  12083. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  12084. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  12085. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  12086. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  12087. ["Show TODO Tree" org-show-todo-tree t]
  12088. ["Global TODO list" org-todo-list t]
  12089. "--"
  12090. ["Set Priority" org-priority t]
  12091. ["Priority Up" org-shiftup t]
  12092. ["Priority Down" org-shiftdown t])
  12093. ("TAGS and Properties"
  12094. ["Set Tags" 'org-set-tags-command t]
  12095. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  12096. "--"
  12097. ["Set property" 'org-set-property t]
  12098. ["Column view of properties" org-columns t]
  12099. ["Insert Column View DBlock" org-insert-columns-dblock t])
  12100. ("Dates and Scheduling"
  12101. ["Timestamp" org-time-stamp t]
  12102. ["Timestamp (inactive)" org-time-stamp-inactive t]
  12103. ("Change Date"
  12104. ["1 Day Later" org-shiftright t]
  12105. ["1 Day Earlier" org-shiftleft t]
  12106. ["1 ... Later" org-shiftup t]
  12107. ["1 ... Earlier" org-shiftdown t])
  12108. ["Compute Time Range" org-evaluate-time-range t]
  12109. ["Schedule Item" org-schedule t]
  12110. ["Deadline" org-deadline t]
  12111. "--"
  12112. ["Custom time format" org-toggle-time-stamp-overlays
  12113. :style radio :selected org-display-custom-times]
  12114. "--"
  12115. ["Goto Calendar" org-goto-calendar t]
  12116. ["Date from Calendar" org-date-from-calendar t]
  12117. "--"
  12118. ["Start/restart timer" org-timer-start t]
  12119. ["Insert timer string" org-timer t]
  12120. ["Insert timer item" org-timer-item t])
  12121. ("Logging work"
  12122. ["Clock in" org-clock-in t]
  12123. ["Clock out" org-clock-out t]
  12124. ["Clock cancel" org-clock-cancel t]
  12125. ["Goto running clock" org-clock-goto t]
  12126. ["Display times" org-clock-display t]
  12127. ["Create clock table" org-clock-report t]
  12128. "--"
  12129. ["Record DONE time"
  12130. (progn (setq org-log-done (not org-log-done))
  12131. (message "Switching to %s will %s record a timestamp"
  12132. (car org-done-keywords)
  12133. (if org-log-done "automatically" "not")))
  12134. :style toggle :selected org-log-done])
  12135. "--"
  12136. ["Agenda Command..." org-agenda t]
  12137. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  12138. ("File List for Agenda")
  12139. ("Special views current file"
  12140. ["TODO Tree" org-show-todo-tree t]
  12141. ["Check Deadlines" org-check-deadlines t]
  12142. ["Timeline" org-timeline t]
  12143. ["Tags Tree" org-tags-sparse-tree t])
  12144. "--"
  12145. ("Hyperlinks"
  12146. ["Store Link (Global)" org-store-link t]
  12147. ["Insert Link" org-insert-link t]
  12148. ["Follow Link" org-open-at-point t]
  12149. "--"
  12150. ["Next link" org-next-link t]
  12151. ["Previous link" org-previous-link t]
  12152. "--"
  12153. ["Descriptive Links"
  12154. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  12155. :style radio
  12156. :selected (member '(org-link) buffer-invisibility-spec)]
  12157. ["Literal Links"
  12158. (progn
  12159. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  12160. :style radio
  12161. :selected (not (member '(org-link) buffer-invisibility-spec))])
  12162. "--"
  12163. ["Export/Publish..." org-export t]
  12164. ("LaTeX"
  12165. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  12166. :selected org-cdlatex-mode]
  12167. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  12168. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  12169. ["Modify math symbol" org-cdlatex-math-modify
  12170. (org-inside-LaTeX-fragment-p)]
  12171. ["Export LaTeX fragments as images"
  12172. (if (featurep 'org-exp)
  12173. (setq org-export-with-LaTeX-fragments
  12174. (not org-export-with-LaTeX-fragments))
  12175. (require 'org-exp))
  12176. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  12177. org-export-with-LaTeX-fragments)])
  12178. "--"
  12179. ("Documentation"
  12180. ["Show Version" org-version t]
  12181. ["Info Documentation" org-info t])
  12182. ("Customize"
  12183. ["Browse Org Group" org-customize t]
  12184. "--"
  12185. ["Expand This Menu" org-create-customize-menu
  12186. (fboundp 'customize-menu-create)])
  12187. "--"
  12188. ["Refresh setup" org-mode-restart t]
  12189. ))
  12190. (defun org-info (&optional node)
  12191. "Read documentation for Org-mode in the info system.
  12192. With optional NODE, go directly to that node."
  12193. (interactive)
  12194. (info (format "(org)%s" (or node ""))))
  12195. (defun org-install-agenda-files-menu ()
  12196. (let ((bl (buffer-list)))
  12197. (save-excursion
  12198. (while bl
  12199. (set-buffer (pop bl))
  12200. (if (org-mode-p) (setq bl nil)))
  12201. (when (org-mode-p)
  12202. (easy-menu-change
  12203. '("Org") "File List for Agenda"
  12204. (append
  12205. (list
  12206. ["Edit File List" (org-edit-agenda-file-list) t]
  12207. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  12208. ["Remove Current File from List" org-remove-file t]
  12209. ["Cycle through agenda files" org-cycle-agenda-files t]
  12210. ["Occur in all agenda files" org-occur-in-agenda-files t]
  12211. "--")
  12212. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  12213. ;;;; Documentation
  12214. ;;;###autoload
  12215. (defun org-require-autoloaded-modules ()
  12216. (interactive)
  12217. (mapc 'require
  12218. '(org-agenda org-archive org-clock org-colview
  12219. org-exp org-id org-export-latex org-publish
  12220. org-remember org-table)))
  12221. ;;;###autoload
  12222. (defun org-customize ()
  12223. "Call the customize function with org as argument."
  12224. (interactive)
  12225. (org-load-modules-maybe)
  12226. (org-require-autoloaded-modules)
  12227. (customize-browse 'org))
  12228. (defun org-create-customize-menu ()
  12229. "Create a full customization menu for Org-mode, insert it into the menu."
  12230. (interactive)
  12231. (org-load-modules-maybe)
  12232. (org-require-autoloaded-modules)
  12233. (if (fboundp 'customize-menu-create)
  12234. (progn
  12235. (easy-menu-change
  12236. '("Org") "Customize"
  12237. `(["Browse Org group" org-customize t]
  12238. "--"
  12239. ,(customize-menu-create 'org)
  12240. ["Set" Custom-set t]
  12241. ["Save" Custom-save t]
  12242. ["Reset to Current" Custom-reset-current t]
  12243. ["Reset to Saved" Custom-reset-saved t]
  12244. ["Reset to Standard Settings" Custom-reset-standard t]))
  12245. (message "\"Org\"-menu now contains full customization menu"))
  12246. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  12247. ;;;; Miscellaneous stuff
  12248. ;;; Generally useful functions
  12249. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  12250. "Display the given MESSAGE as a warning."
  12251. (if (fboundp 'display-warning)
  12252. (display-warning 'org message
  12253. (if (featurep 'xemacs)
  12254. 'warning
  12255. :warning))
  12256. (let ((buf (get-buffer-create "*Org warnings*")))
  12257. (with-current-buffer buf
  12258. (goto-char (point-max))
  12259. (insert "Warning (Org): " message)
  12260. (unless (bolp)
  12261. (newline)))
  12262. (display-buffer buf)
  12263. (sit-for 0))))
  12264. (defun org-goto-marker-or-bmk (marker &optional bookmark)
  12265. "Go to MARKER, widen if necessary. When marker is not live, try BOOKMARK."
  12266. (if (and marker (marker-buffer marker)
  12267. (buffer-live-p (marker-buffer marker)))
  12268. (progn
  12269. (switch-to-buffer (marker-buffer marker))
  12270. (if (or (> marker (point-max)) (< marker (point-min)))
  12271. (widen))
  12272. (goto-char marker))
  12273. (if bookmark
  12274. (bookmark-jump bookmark)
  12275. (error "Cannot find location"))))
  12276. (defun org-quote-csv-field (s)
  12277. "Quote field for inclusion in CSV material."
  12278. (if (string-match "[\",]" s)
  12279. (concat "\"" (mapconcat 'identity (split-string s "\"") "\"\"") "\"")
  12280. s))
  12281. (defun org-plist-delete (plist property)
  12282. "Delete PROPERTY from PLIST.
  12283. This is in contrast to merely setting it to 0."
  12284. (let (p)
  12285. (while plist
  12286. (if (not (eq property (car plist)))
  12287. (setq p (plist-put p (car plist) (nth 1 plist))))
  12288. (setq plist (cddr plist)))
  12289. p))
  12290. (defun org-force-self-insert (N)
  12291. "Needed to enforce self-insert under remapping."
  12292. (interactive "p")
  12293. (self-insert-command N))
  12294. (defun org-string-width (s)
  12295. "Compute width of string, ignoring invisible characters.
  12296. This ignores character with invisibility property `org-link', and also
  12297. characters with property `org-cwidth', because these will become invisible
  12298. upon the next fontification round."
  12299. (let (b l)
  12300. (when (or (eq t buffer-invisibility-spec)
  12301. (assq 'org-link buffer-invisibility-spec))
  12302. (while (setq b (text-property-any 0 (length s)
  12303. 'invisible 'org-link s))
  12304. (setq s (concat (substring s 0 b)
  12305. (substring s (or (next-single-property-change
  12306. b 'invisible s) (length s)))))))
  12307. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  12308. (setq s (concat (substring s 0 b)
  12309. (substring s (or (next-single-property-change
  12310. b 'org-cwidth s) (length s))))))
  12311. (setq l (string-width s) b -1)
  12312. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  12313. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  12314. l))
  12315. (defun org-get-indentation (&optional line)
  12316. "Get the indentation of the current line, interpreting tabs.
  12317. When LINE is given, assume it represents a line and compute its indentation."
  12318. (if line
  12319. (if (string-match "^ *" (org-remove-tabs line))
  12320. (match-end 0))
  12321. (save-excursion
  12322. (beginning-of-line 1)
  12323. (skip-chars-forward " \t")
  12324. (current-column))))
  12325. (defun org-remove-tabs (s &optional width)
  12326. "Replace tabulators in S with spaces.
  12327. Assumes that s is a single line, starting in column 0."
  12328. (setq width (or width tab-width))
  12329. (while (string-match "\t" s)
  12330. (setq s (replace-match
  12331. (make-string
  12332. (- (* width (/ (+ (match-beginning 0) width) width))
  12333. (match-beginning 0)) ?\ )
  12334. t t s)))
  12335. s)
  12336. (defun org-fix-indentation (line ind)
  12337. "Fix indentation in LINE.
  12338. IND is a cons cell with target and minimum indentation.
  12339. If the current indenation in LINE is smaller than the minimum,
  12340. leave it alone. If it is larger than ind, set it to the target."
  12341. (let* ((l (org-remove-tabs line))
  12342. (i (org-get-indentation l))
  12343. (i1 (car ind)) (i2 (cdr ind)))
  12344. (if (>= i i2) (setq l (substring line i2)))
  12345. (if (> i1 0)
  12346. (concat (make-string i1 ?\ ) l)
  12347. l)))
  12348. (defun org-base-buffer (buffer)
  12349. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  12350. (if (not buffer)
  12351. buffer
  12352. (or (buffer-base-buffer buffer)
  12353. buffer)))
  12354. (defun org-trim (s)
  12355. "Remove whitespace at beginning and end of string."
  12356. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  12357. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  12358. s)
  12359. (defun org-wrap (string &optional width lines)
  12360. "Wrap string to either a number of lines, or a width in characters.
  12361. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  12362. that costs. If there is a word longer than WIDTH, the text is actually
  12363. wrapped to the length of that word.
  12364. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  12365. many lines, whatever width that takes.
  12366. The return value is a list of lines, without newlines at the end."
  12367. (let* ((words (org-split-string string "[ \t\n]+"))
  12368. (maxword (apply 'max (mapcar 'org-string-width words)))
  12369. w ll)
  12370. (cond (width
  12371. (org-do-wrap words (max maxword width)))
  12372. (lines
  12373. (setq w maxword)
  12374. (setq ll (org-do-wrap words maxword))
  12375. (if (<= (length ll) lines)
  12376. ll
  12377. (setq ll words)
  12378. (while (> (length ll) lines)
  12379. (setq w (1+ w))
  12380. (setq ll (org-do-wrap words w)))
  12381. ll))
  12382. (t (error "Cannot wrap this")))))
  12383. (defun org-do-wrap (words width)
  12384. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  12385. (let (lines line)
  12386. (while words
  12387. (setq line (pop words))
  12388. (while (and words (< (+ (length line) (length (car words))) width))
  12389. (setq line (concat line " " (pop words))))
  12390. (setq lines (push line lines)))
  12391. (nreverse lines)))
  12392. (defun org-split-string (string &optional separators)
  12393. "Splits STRING into substrings at SEPARATORS.
  12394. No empty strings are returned if there are matches at the beginning
  12395. and end of string."
  12396. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  12397. (start 0)
  12398. notfirst
  12399. (list nil))
  12400. (while (and (string-match rexp string
  12401. (if (and notfirst
  12402. (= start (match-beginning 0))
  12403. (< start (length string)))
  12404. (1+ start) start))
  12405. (< (match-beginning 0) (length string)))
  12406. (setq notfirst t)
  12407. (or (eq (match-beginning 0) 0)
  12408. (and (eq (match-beginning 0) (match-end 0))
  12409. (eq (match-beginning 0) start))
  12410. (setq list
  12411. (cons (substring string start (match-beginning 0))
  12412. list)))
  12413. (setq start (match-end 0)))
  12414. (or (eq start (length string))
  12415. (setq list
  12416. (cons (substring string start)
  12417. list)))
  12418. (nreverse list)))
  12419. (defun org-context ()
  12420. "Return a list of contexts of the current cursor position.
  12421. If several contexts apply, all are returned.
  12422. Each context entry is a list with a symbol naming the context, and
  12423. two positions indicating start and end of the context. Possible
  12424. contexts are:
  12425. :headline anywhere in a headline
  12426. :headline-stars on the leading stars in a headline
  12427. :todo-keyword on a TODO keyword (including DONE) in a headline
  12428. :tags on the TAGS in a headline
  12429. :priority on the priority cookie in a headline
  12430. :item on the first line of a plain list item
  12431. :item-bullet on the bullet/number of a plain list item
  12432. :checkbox on the checkbox in a plain list item
  12433. :table in an org-mode table
  12434. :table-special on a special filed in a table
  12435. :table-table in a table.el table
  12436. :link on a hyperlink
  12437. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  12438. :target on a <<target>>
  12439. :radio-target on a <<<radio-target>>>
  12440. :latex-fragment on a LaTeX fragment
  12441. :latex-preview on a LaTeX fragment with overlayed preview image
  12442. This function expects the position to be visible because it uses font-lock
  12443. faces as a help to recognize the following contexts: :table-special, :link,
  12444. and :keyword."
  12445. (let* ((f (get-text-property (point) 'face))
  12446. (faces (if (listp f) f (list f)))
  12447. (p (point)) clist o)
  12448. ;; First the large context
  12449. (cond
  12450. ((org-on-heading-p t)
  12451. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  12452. (when (progn
  12453. (beginning-of-line 1)
  12454. (looking-at org-todo-line-tags-regexp))
  12455. (push (org-point-in-group p 1 :headline-stars) clist)
  12456. (push (org-point-in-group p 2 :todo-keyword) clist)
  12457. (push (org-point-in-group p 4 :tags) clist))
  12458. (goto-char p)
  12459. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  12460. (if (looking-at "\\[#[A-Z0-9]\\]")
  12461. (push (org-point-in-group p 0 :priority) clist)))
  12462. ((org-at-item-p)
  12463. (push (org-point-in-group p 2 :item-bullet) clist)
  12464. (push (list :item (point-at-bol)
  12465. (save-excursion (org-end-of-item) (point)))
  12466. clist)
  12467. (and (org-at-item-checkbox-p)
  12468. (push (org-point-in-group p 0 :checkbox) clist)))
  12469. ((org-at-table-p)
  12470. (push (list :table (org-table-begin) (org-table-end)) clist)
  12471. (if (memq 'org-formula faces)
  12472. (push (list :table-special
  12473. (previous-single-property-change p 'face)
  12474. (next-single-property-change p 'face)) clist)))
  12475. ((org-at-table-p 'any)
  12476. (push (list :table-table) clist)))
  12477. (goto-char p)
  12478. ;; Now the small context
  12479. (cond
  12480. ((org-at-timestamp-p)
  12481. (push (org-point-in-group p 0 :timestamp) clist))
  12482. ((memq 'org-link faces)
  12483. (push (list :link
  12484. (previous-single-property-change p 'face)
  12485. (next-single-property-change p 'face)) clist))
  12486. ((memq 'org-special-keyword faces)
  12487. (push (list :keyword
  12488. (previous-single-property-change p 'face)
  12489. (next-single-property-change p 'face)) clist))
  12490. ((org-on-target-p)
  12491. (push (org-point-in-group p 0 :target) clist)
  12492. (goto-char (1- (match-beginning 0)))
  12493. (if (looking-at org-radio-target-regexp)
  12494. (push (org-point-in-group p 0 :radio-target) clist))
  12495. (goto-char p))
  12496. ((setq o (car (delq nil
  12497. (mapcar
  12498. (lambda (x)
  12499. (if (memq x org-latex-fragment-image-overlays) x))
  12500. (org-overlays-at (point))))))
  12501. (push (list :latex-fragment
  12502. (org-overlay-start o) (org-overlay-end o)) clist)
  12503. (push (list :latex-preview
  12504. (org-overlay-start o) (org-overlay-end o)) clist))
  12505. ((org-inside-LaTeX-fragment-p)
  12506. ;; FIXME: positions wrong.
  12507. (push (list :latex-fragment (point) (point)) clist)))
  12508. (setq clist (nreverse (delq nil clist)))
  12509. clist))
  12510. ;; FIXME: Compare with at-regexp-p Do we need both?
  12511. (defun org-in-regexp (re &optional nlines visually)
  12512. "Check if point is inside a match of regexp.
  12513. Normally only the current line is checked, but you can include NLINES extra
  12514. lines both before and after point into the search.
  12515. If VISUALLY is set, require that the cursor is not after the match but
  12516. really on, so that the block visually is on the match."
  12517. (catch 'exit
  12518. (let ((pos (point))
  12519. (eol (point-at-eol (+ 1 (or nlines 0))))
  12520. (inc (if visually 1 0)))
  12521. (save-excursion
  12522. (beginning-of-line (- 1 (or nlines 0)))
  12523. (while (re-search-forward re eol t)
  12524. (if (and (<= (match-beginning 0) pos)
  12525. (>= (+ inc (match-end 0)) pos))
  12526. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12527. (defun org-at-regexp-p (regexp)
  12528. "Is point inside a match of REGEXP in the current line?"
  12529. (catch 'exit
  12530. (save-excursion
  12531. (let ((pos (point)) (end (point-at-eol)))
  12532. (beginning-of-line 1)
  12533. (while (re-search-forward regexp end t)
  12534. (if (and (<= (match-beginning 0) pos)
  12535. (>= (match-end 0) pos))
  12536. (throw 'exit t)))
  12537. nil))))
  12538. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12539. "Call `multi-occur' with buffers for all agenda files."
  12540. (interactive "sOrg-files matching: \np")
  12541. (let* ((files (org-agenda-files))
  12542. (tnames (mapcar 'file-truename files))
  12543. (extra org-agenda-text-search-extra-files)
  12544. f)
  12545. (when (eq (car extra) 'agenda-archives)
  12546. (setq extra (cdr extra))
  12547. (setq files (org-add-archive-files files)))
  12548. (while (setq f (pop extra))
  12549. (unless (member (file-truename f) tnames)
  12550. (add-to-list 'files f 'append)
  12551. (add-to-list 'tnames (file-truename f) 'append)))
  12552. (multi-occur
  12553. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12554. regexp)))
  12555. (if (boundp 'occur-mode-find-occurrence-hook)
  12556. ;; Emacs 23
  12557. (add-hook 'occur-mode-find-occurrence-hook
  12558. (lambda ()
  12559. (when (org-mode-p)
  12560. (org-reveal))))
  12561. ;; Emacs 22
  12562. (defadvice occur-mode-goto-occurrence
  12563. (after org-occur-reveal activate)
  12564. (and (org-mode-p) (org-reveal)))
  12565. (defadvice occur-mode-goto-occurrence-other-window
  12566. (after org-occur-reveal activate)
  12567. (and (org-mode-p) (org-reveal)))
  12568. (defadvice occur-mode-display-occurrence
  12569. (after org-occur-reveal activate)
  12570. (when (org-mode-p)
  12571. (let ((pos (occur-mode-find-occurrence)))
  12572. (with-current-buffer (marker-buffer pos)
  12573. (save-excursion
  12574. (goto-char pos)
  12575. (org-reveal)))))))
  12576. (defun org-uniquify (list)
  12577. "Remove duplicate elements from LIST."
  12578. (let (res)
  12579. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12580. res))
  12581. (defun org-delete-all (elts list)
  12582. "Remove all elements in ELTS from LIST."
  12583. (while elts
  12584. (setq list (delete (pop elts) list)))
  12585. list)
  12586. (defun org-back-over-empty-lines ()
  12587. "Move backwards over witespace, to the beginning of the first empty line.
  12588. Returns the number of empty lines passed."
  12589. (let ((pos (point)))
  12590. (skip-chars-backward " \t\n\r")
  12591. (beginning-of-line 2)
  12592. (goto-char (min (point) pos))
  12593. (count-lines (point) pos)))
  12594. (defun org-skip-whitespace ()
  12595. (skip-chars-forward " \t\n\r"))
  12596. (defun org-point-in-group (point group &optional context)
  12597. "Check if POINT is in match-group GROUP.
  12598. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12599. match. If the match group does ot exist or point is not inside it,
  12600. return nil."
  12601. (and (match-beginning group)
  12602. (>= point (match-beginning group))
  12603. (<= point (match-end group))
  12604. (if context
  12605. (list context (match-beginning group) (match-end group))
  12606. t)))
  12607. (defun org-switch-to-buffer-other-window (&rest args)
  12608. "Switch to buffer in a second window on the current frame.
  12609. In particular, do not allow pop-up frames."
  12610. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12611. special-display-function)
  12612. (apply 'switch-to-buffer-other-window args)))
  12613. (defun org-combine-plists (&rest plists)
  12614. "Create a single property list from all plists in PLISTS.
  12615. The process starts by copying the first list, and then setting properties
  12616. from the other lists. Settings in the last list are the most significant
  12617. ones and overrule settings in the other lists."
  12618. (let ((rtn (copy-sequence (pop plists)))
  12619. p v ls)
  12620. (while plists
  12621. (setq ls (pop plists))
  12622. (while ls
  12623. (setq p (pop ls) v (pop ls))
  12624. (setq rtn (plist-put rtn p v))))
  12625. rtn))
  12626. (defun org-move-line-down (arg)
  12627. "Move the current line down. With prefix argument, move it past ARG lines."
  12628. (interactive "p")
  12629. (let ((col (current-column))
  12630. beg end pos)
  12631. (beginning-of-line 1) (setq beg (point))
  12632. (beginning-of-line 2) (setq end (point))
  12633. (beginning-of-line (+ 1 arg))
  12634. (setq pos (move-marker (make-marker) (point)))
  12635. (insert (delete-and-extract-region beg end))
  12636. (goto-char pos)
  12637. (org-move-to-column col)))
  12638. (defun org-move-line-up (arg)
  12639. "Move the current line up. With prefix argument, move it past ARG lines."
  12640. (interactive "p")
  12641. (let ((col (current-column))
  12642. beg end pos)
  12643. (beginning-of-line 1) (setq beg (point))
  12644. (beginning-of-line 2) (setq end (point))
  12645. (beginning-of-line (- arg))
  12646. (setq pos (move-marker (make-marker) (point)))
  12647. (insert (delete-and-extract-region beg end))
  12648. (goto-char pos)
  12649. (org-move-to-column col)))
  12650. (defun org-replace-escapes (string table)
  12651. "Replace %-escapes in STRING with values in TABLE.
  12652. TABLE is an association list with keys like \"%a\" and string values.
  12653. The sequences in STRING may contain normal field width and padding information,
  12654. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12655. so values can contain further %-escapes if they are define later in TABLE."
  12656. (let ((case-fold-search nil)
  12657. e re rpl)
  12658. (while (setq e (pop table))
  12659. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12660. (while (string-match re string)
  12661. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12662. (cdr e)))
  12663. (setq string (replace-match rpl t t string))))
  12664. string))
  12665. (defun org-sublist (list start end)
  12666. "Return a section of LIST, from START to END.
  12667. Counting starts at 1."
  12668. (let (rtn (c start))
  12669. (setq list (nthcdr (1- start) list))
  12670. (while (and list (<= c end))
  12671. (push (pop list) rtn)
  12672. (setq c (1+ c)))
  12673. (nreverse rtn)))
  12674. (defun org-find-base-buffer-visiting (file)
  12675. "Like `find-buffer-visiting' but alway return the base buffer and
  12676. not an indirect buffer."
  12677. (let ((buf (find-buffer-visiting file)))
  12678. (if buf
  12679. (or (buffer-base-buffer buf) buf)
  12680. nil)))
  12681. (defun org-image-file-name-regexp ()
  12682. "Return regexp matching the file names of images."
  12683. (if (fboundp 'image-file-name-regexp)
  12684. (image-file-name-regexp)
  12685. (let ((image-file-name-extensions
  12686. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12687. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12688. (concat "\\."
  12689. (regexp-opt (nconc (mapcar 'upcase
  12690. image-file-name-extensions)
  12691. image-file-name-extensions)
  12692. t)
  12693. "\\'"))))
  12694. (defun org-file-image-p (file)
  12695. "Return non-nil if FILE is an image."
  12696. (save-match-data
  12697. (string-match (org-image-file-name-regexp) file)))
  12698. (defun org-get-cursor-date ()
  12699. "Return the date at cursor in as a time.
  12700. This works in the calendar and in the agenda, anywhere else it just
  12701. returns the current time."
  12702. (let (date day defd)
  12703. (cond
  12704. ((eq major-mode 'calendar-mode)
  12705. (setq date (calendar-cursor-to-date)
  12706. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  12707. ((eq major-mode 'org-agenda-mode)
  12708. (setq day (get-text-property (point) 'day))
  12709. (if day
  12710. (setq date (calendar-gregorian-from-absolute day)
  12711. defd (encode-time 0 0 0 (nth 1 date) (nth 0 date)
  12712. (nth 2 date))))))
  12713. (or defd (current-time))))
  12714. (defvar org-agenda-action-marker (make-marker)
  12715. "Marker pointing to the entry for the next agenda action.")
  12716. (defun org-mark-entry-for-agenda-action ()
  12717. "Mark the current entry as target of an agenda action.
  12718. Agenda actions are actions executed from the agenda with the key `k',
  12719. which make use of the date at the cursor."
  12720. (interactive)
  12721. (move-marker org-agenda-action-marker
  12722. (save-excursion (org-back-to-heading t) (point))
  12723. (current-buffer))
  12724. (message
  12725. "Entry marked for action; press `k' at desired date in agenda or calendar"))
  12726. ;;; Paragraph filling stuff.
  12727. ;; We want this to be just right, so use the full arsenal.
  12728. (defun org-indent-line-function ()
  12729. "Indent line like previous, but further if previous was headline or item."
  12730. (interactive)
  12731. (let* ((pos (point))
  12732. (itemp (org-at-item-p))
  12733. column bpos bcol tpos tcol bullet btype bullet-type)
  12734. ;; Find the previous relevant line
  12735. (beginning-of-line 1)
  12736. (cond
  12737. ((looking-at "#") (setq column 0))
  12738. ((looking-at "\\*+ ") (setq column 0))
  12739. (t
  12740. (beginning-of-line 0)
  12741. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12742. (beginning-of-line 0))
  12743. (cond
  12744. ((looking-at "\\*+[ \t]+")
  12745. (if (not org-adapt-indentation)
  12746. (setq column 0)
  12747. (goto-char (match-end 0))
  12748. (setq column (current-column))))
  12749. ((org-in-item-p)
  12750. (org-beginning-of-item)
  12751. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\|.*? :: \\)?")
  12752. (setq bpos (match-beginning 1) tpos (match-end 0)
  12753. bcol (progn (goto-char bpos) (current-column))
  12754. tcol (progn (goto-char tpos) (current-column))
  12755. bullet (match-string 1)
  12756. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12757. (if (> tcol (+ bcol org-description-max-indent))
  12758. (setq tcol (+ bcol 5)))
  12759. (if (not itemp)
  12760. (setq column tcol)
  12761. (goto-char pos)
  12762. (beginning-of-line 1)
  12763. (if (looking-at "\\S-")
  12764. (progn
  12765. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12766. (setq bullet (match-string 1)
  12767. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12768. (setq column (if (equal btype bullet-type) bcol tcol)))
  12769. (setq column (org-get-indentation)))))
  12770. (t (setq column (org-get-indentation))))))
  12771. (goto-char pos)
  12772. (if (<= (current-column) (current-indentation))
  12773. (org-indent-line-to column)
  12774. (save-excursion (org-indent-line-to column)))
  12775. (setq column (current-column))
  12776. (beginning-of-line 1)
  12777. (if (looking-at
  12778. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12779. (replace-match (concat "\\1" (format org-property-format
  12780. (match-string 2) (match-string 3)))
  12781. t nil))
  12782. (org-move-to-column column)))
  12783. (defun org-set-autofill-regexps ()
  12784. (interactive)
  12785. ;; In the paragraph separator we include headlines, because filling
  12786. ;; text in a line directly attached to a headline would otherwise
  12787. ;; fill the headline as well.
  12788. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12789. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12790. ;; The paragraph starter includes hand-formatted lists.
  12791. (org-set-local 'paragraph-start
  12792. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12793. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12794. ;; But only if the user has not turned off tables or fixed-width regions
  12795. (org-set-local
  12796. 'auto-fill-inhibit-regexp
  12797. (concat "\\*+ \\|#\\+"
  12798. "\\|[ \t]*" org-keyword-time-regexp
  12799. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12800. (concat
  12801. "\\|[ \t]*["
  12802. (if org-enable-table-editor "|" "")
  12803. (if org-enable-fixed-width-editor ":" "")
  12804. "]"))))
  12805. ;; We use our own fill-paragraph function, to make sure that tables
  12806. ;; and fixed-width regions are not wrapped. That function will pass
  12807. ;; through to `fill-paragraph' when appropriate.
  12808. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12809. ; Adaptive filling: To get full control, first make sure that
  12810. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12811. (org-set-local 'adaptive-fill-regexp "\000")
  12812. (org-set-local 'adaptive-fill-function
  12813. 'org-adaptive-fill-function)
  12814. (org-set-local
  12815. 'align-mode-rules-list
  12816. '((org-in-buffer-settings
  12817. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12818. (modes . '(org-mode))))))
  12819. (defun org-fill-paragraph (&optional justify)
  12820. "Re-align a table, pass through to fill-paragraph if no table."
  12821. (let ((table-p (org-at-table-p))
  12822. (table.el-p (org-at-table.el-p)))
  12823. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12824. (save-excursion (goto-char (point-at-bol))
  12825. (looking-at outline-regexp)))
  12826. t) ; skip headlines
  12827. (table.el-p t) ; skip table.el tables
  12828. (table-p (org-table-align) t) ; align org-mode tables
  12829. (t nil)))) ; call paragraph-fill
  12830. ;; For reference, this is the default value of adaptive-fill-regexp
  12831. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12832. (defun org-adaptive-fill-function ()
  12833. "Return a fill prefix for org-mode files.
  12834. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12835. work correctly."
  12836. (cond ((looking-at "#[ \t]+")
  12837. (match-string 0))
  12838. ((looking-at "[ \t]*\\([-*+] .*? :: \\)")
  12839. (save-excursion
  12840. (if (> (match-end 1) (+ (match-beginning 1)
  12841. org-description-max-indent))
  12842. (goto-char (+ (match-beginning 1) 5))
  12843. (goto-char (match-end 0)))
  12844. (make-string (current-column) ?\ )))
  12845. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] ?\\)?")
  12846. (save-excursion
  12847. (goto-char (match-end 0))
  12848. (make-string (current-column) ?\ )))
  12849. (t nil)))
  12850. ;;; Other stuff.
  12851. (defun org-toggle-fixed-width-section (arg)
  12852. "Toggle the fixed-width export.
  12853. If there is no active region, the QUOTE keyword at the current headline is
  12854. inserted or removed. When present, it causes the text between this headline
  12855. and the next to be exported as fixed-width text, and unmodified.
  12856. If there is an active region, this command adds or removes a colon as the
  12857. first character of this line. If the first character of a line is a colon,
  12858. this line is also exported in fixed-width font."
  12859. (interactive "P")
  12860. (let* ((cc 0)
  12861. (regionp (org-region-active-p))
  12862. (beg (if regionp (region-beginning) (point)))
  12863. (end (if regionp (region-end)))
  12864. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12865. (case-fold-search nil)
  12866. (re "[ \t]*\\(:\\)")
  12867. off)
  12868. (if regionp
  12869. (save-excursion
  12870. (goto-char beg)
  12871. (setq cc (current-column))
  12872. (beginning-of-line 1)
  12873. (setq off (looking-at re))
  12874. (while (> nlines 0)
  12875. (setq nlines (1- nlines))
  12876. (beginning-of-line 1)
  12877. (cond
  12878. (arg
  12879. (org-move-to-column cc t)
  12880. (insert ":\n")
  12881. (forward-line -1))
  12882. ((and off (looking-at re))
  12883. (replace-match "" t t nil 1))
  12884. ((not off) (org-move-to-column cc t) (insert ":")))
  12885. (forward-line 1)))
  12886. (save-excursion
  12887. (org-back-to-heading)
  12888. (if (looking-at (concat outline-regexp
  12889. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12890. (replace-match "" t t nil 1)
  12891. (if (looking-at outline-regexp)
  12892. (progn
  12893. (goto-char (match-end 0))
  12894. (insert org-quote-string " "))))))))
  12895. ;;;; Functions extending outline functionality
  12896. (defun org-beginning-of-line (&optional arg)
  12897. "Go to the beginning of the current line. If that is invisible, continue
  12898. to a visible line beginning. This makes the function of C-a more intuitive.
  12899. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12900. first attempt, and only move to after the tags when the cursor is already
  12901. beyond the end of the headline."
  12902. (interactive "P")
  12903. (let ((pos (point)) refpos)
  12904. (beginning-of-line 1)
  12905. (if (bobp)
  12906. nil
  12907. (backward-char 1)
  12908. (if (org-invisible-p)
  12909. (while (and (not (bobp)) (org-invisible-p))
  12910. (backward-char 1)
  12911. (beginning-of-line 1))
  12912. (forward-char 1)))
  12913. (when org-special-ctrl-a/e
  12914. (cond
  12915. ((and (looking-at org-complex-heading-regexp)
  12916. (= (char-after (match-end 1)) ?\ ))
  12917. (setq refpos (min (1+ (or (match-end 3) (match-end 2) (match-end 1)))
  12918. (point-at-eol)))
  12919. (goto-char
  12920. (if (eq org-special-ctrl-a/e t)
  12921. (cond ((> pos refpos) refpos)
  12922. ((= pos (point)) refpos)
  12923. (t (point)))
  12924. (cond ((> pos (point)) (point))
  12925. ((not (eq last-command this-command)) (point))
  12926. (t refpos)))))
  12927. ((org-at-item-p)
  12928. (goto-char
  12929. (if (eq org-special-ctrl-a/e t)
  12930. (cond ((> pos (match-end 4)) (match-end 4))
  12931. ((= pos (point)) (match-end 4))
  12932. (t (point)))
  12933. (cond ((> pos (point)) (point))
  12934. ((not (eq last-command this-command)) (point))
  12935. (t (match-end 4))))))))
  12936. (org-no-warnings
  12937. (and (featurep 'xemacs) (setq zmacs-region-stays t)))))
  12938. (defun org-end-of-line (&optional arg)
  12939. "Go to the end of the line.
  12940. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12941. first attempt, and only move to after the tags when the cursor is already
  12942. beyond the end of the headline."
  12943. (interactive "P")
  12944. (if (or (not org-special-ctrl-a/e)
  12945. (not (org-on-heading-p)))
  12946. (end-of-line arg)
  12947. (let ((pos (point)))
  12948. (beginning-of-line 1)
  12949. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12950. (if (eq org-special-ctrl-a/e t)
  12951. (if (or (< pos (match-beginning 1))
  12952. (= pos (match-end 0)))
  12953. (goto-char (match-beginning 1))
  12954. (goto-char (match-end 0)))
  12955. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12956. (goto-char (match-end 0))
  12957. (goto-char (match-beginning 1))))
  12958. (end-of-line arg))))
  12959. (org-no-warnings
  12960. (and (featurep 'xemacs) (setq zmacs-region-stays t))))
  12961. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12962. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12963. (defun org-kill-line (&optional arg)
  12964. "Kill line, to tags or end of line."
  12965. (interactive "P")
  12966. (cond
  12967. ((or (not org-special-ctrl-k)
  12968. (bolp)
  12969. (not (org-on-heading-p)))
  12970. (call-interactively 'kill-line))
  12971. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12972. (kill-region (point) (match-beginning 1))
  12973. (org-set-tags nil t))
  12974. (t (kill-region (point) (point-at-eol)))))
  12975. (define-key org-mode-map "\C-k" 'org-kill-line)
  12976. (defun org-yank (&optional arg)
  12977. "Yank. If the kill is a subtree, treat it specially.
  12978. This command will look at the current kill and check if is a single
  12979. subtree, or a series of subtrees[1]. If it passes the test, and if the
  12980. cursor is at the beginning of a line or after the stars of a currently
  12981. empty headline, then the yank is handeled specially. How exactly depends
  12982. on the value of the following variables, both set by default.
  12983. org-yank-folded-subtrees
  12984. When set, the subree(s) will be folded after insertion, but only
  12985. if doing so would now swallow text after the yanked text.
  12986. org-yank-adjusted-subtrees
  12987. When set, the subtree will be promoted or demoted in order to
  12988. fit into the local outline tree structure, which means that the level
  12989. will be adjusted so that it becomes the smaller one of the two
  12990. *visible* surrounding headings.
  12991. Any prefix to this command will cause `yank' to be called directly with
  12992. no special treatment. In particular, a simple `C-u' prefix will just
  12993. plainly yank the text as it is.
  12994. \[1] Basically, the test checks if the first non-white line is a heading
  12995. and if there are no other headings with fewer stars."
  12996. (interactive "P")
  12997. (setq this-command 'yank)
  12998. (if arg
  12999. (call-interactively 'yank)
  13000. (let ((subtreep ; is kill a subtree, and the yank position appropriate?
  13001. (and (org-kill-is-subtree-p)
  13002. (or (bolp)
  13003. (and (looking-at "[ \t]*$")
  13004. (string-match
  13005. "\\`\\*+\\'"
  13006. (buffer-substring (point-at-bol) (point)))))))
  13007. swallowp)
  13008. (cond
  13009. ((and subtreep org-yank-folded-subtrees)
  13010. (let ((beg (point))
  13011. end)
  13012. (if (and subtreep org-yank-adjusted-subtrees)
  13013. (org-paste-subtree nil nil 'for-yank)
  13014. (call-interactively 'yank))
  13015. (setq end (point))
  13016. (goto-char beg)
  13017. (when (and (bolp) subtreep
  13018. (not (setq swallowp
  13019. (org-yank-folding-would-swallow-text beg end))))
  13020. (or (looking-at outline-regexp)
  13021. (re-search-forward (concat "^" outline-regexp) end t))
  13022. (while (and (< (point) end) (looking-at outline-regexp))
  13023. (hide-subtree)
  13024. (org-cycle-show-empty-lines 'folded)
  13025. (condition-case nil
  13026. (outline-forward-same-level 1)
  13027. (error (goto-char end)))))
  13028. (when swallowp
  13029. (message
  13030. "Yanked text not folded because that would swallow text"))
  13031. (goto-char end)
  13032. (skip-chars-forward " \t\n\r")
  13033. (beginning-of-line 1)
  13034. (push-mark beg 'nomsg)))
  13035. ((and subtreep org-yank-adjusted-subtrees)
  13036. (let ((beg (point-at-bol)))
  13037. (org-paste-subtree nil nil 'for-yank)
  13038. (push-mark beg 'nomsg)))
  13039. (t
  13040. (call-interactively 'yank))))))
  13041. (defun org-yank-folding-would-swallow-text (beg end)
  13042. "Would hide-subtree at BEG swallow any text after END?"
  13043. (let (level)
  13044. (save-excursion
  13045. (goto-char beg)
  13046. (when (or (looking-at outline-regexp)
  13047. (re-search-forward (concat "^" outline-regexp) end t))
  13048. (setq level (org-outline-level)))
  13049. (goto-char end)
  13050. (skip-chars-forward " \t\r\n\v\f")
  13051. (if (or (eobp)
  13052. (and (bolp) (looking-at org-outline-regexp)
  13053. (<= (org-outline-level) level)))
  13054. nil ; Nothing would be swallowed
  13055. t)))) ; something would swallow
  13056. (define-key org-mode-map "\C-y" 'org-yank)
  13057. (defun org-invisible-p ()
  13058. "Check if point is at a character currently not visible."
  13059. ;; Early versions of noutline don't have `outline-invisible-p'.
  13060. (if (fboundp 'outline-invisible-p)
  13061. (outline-invisible-p)
  13062. (get-char-property (point) 'invisible)))
  13063. (defun org-invisible-p2 ()
  13064. "Check if point is at a character currently not visible."
  13065. (save-excursion
  13066. (if (and (eolp) (not (bobp))) (backward-char 1))
  13067. ;; Early versions of noutline don't have `outline-invisible-p'.
  13068. (if (fboundp 'outline-invisible-p)
  13069. (outline-invisible-p)
  13070. (get-char-property (point) 'invisible))))
  13071. (defun org-back-to-heading (&optional invisible-ok)
  13072. "Call `outline-back-to-heading', but provide a better error message."
  13073. (condition-case nil
  13074. (outline-back-to-heading invisible-ok)
  13075. (error (error "Before first headline at position %d in buffer %s"
  13076. (point) (current-buffer)))))
  13077. (defalias 'org-on-heading-p 'outline-on-heading-p)
  13078. (defalias 'org-at-heading-p 'outline-on-heading-p)
  13079. (defun org-at-heading-or-item-p ()
  13080. (or (org-on-heading-p) (org-at-item-p)))
  13081. (defun org-on-target-p ()
  13082. (or (org-in-regexp org-radio-target-regexp)
  13083. (org-in-regexp org-target-regexp)))
  13084. (defun org-up-heading-all (arg)
  13085. "Move to the heading line of which the present line is a subheading.
  13086. This function considers both visible and invisible heading lines.
  13087. With argument, move up ARG levels."
  13088. (if (fboundp 'outline-up-heading-all)
  13089. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13090. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  13091. (defun org-up-heading-safe ()
  13092. "Move to the heading line of which the present line is a subheading.
  13093. This version will not throw an error. It will return the level of the
  13094. headline found, or nil if no higher level is found."
  13095. (let ((pos (point)) start-level level
  13096. (re (concat "^" outline-regexp)))
  13097. (catch 'exit
  13098. (org-back-to-heading t)
  13099. (setq start-level (funcall outline-level))
  13100. (if (equal start-level 1) (throw 'exit nil))
  13101. (while (re-search-backward re nil t)
  13102. (setq level (funcall outline-level))
  13103. (if (< level start-level) (throw 'exit level)))
  13104. nil)))
  13105. (defun org-first-sibling-p ()
  13106. "Is this heading the first child of its parents?"
  13107. (interactive)
  13108. (let ((re (concat "^" outline-regexp))
  13109. level l)
  13110. (unless (org-at-heading-p t)
  13111. (error "Not at a heading"))
  13112. (setq level (funcall outline-level))
  13113. (save-excursion
  13114. (if (not (re-search-backward re nil t))
  13115. t
  13116. (setq l (funcall outline-level))
  13117. (< l level)))))
  13118. (defun org-goto-sibling (&optional previous)
  13119. "Goto the next sibling, even if it is invisible.
  13120. When PREVIOUS is set, go to the previous sibling instead. Returns t
  13121. when a sibling was found. When none is found, return nil and don't
  13122. move point."
  13123. (let ((fun (if previous 're-search-backward 're-search-forward))
  13124. (pos (point))
  13125. (re (concat "^" outline-regexp))
  13126. level l)
  13127. (when (condition-case nil (org-back-to-heading t) (error nil))
  13128. (setq level (funcall outline-level))
  13129. (catch 'exit
  13130. (or previous (forward-char 1))
  13131. (while (funcall fun re nil t)
  13132. (setq l (funcall outline-level))
  13133. (when (< l level) (goto-char pos) (throw 'exit nil))
  13134. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  13135. (goto-char pos)
  13136. nil))))
  13137. (defun org-show-siblings ()
  13138. "Show all siblings of the current headline."
  13139. (save-excursion
  13140. (while (org-goto-sibling) (org-flag-heading nil)))
  13141. (save-excursion
  13142. (while (org-goto-sibling 'previous)
  13143. (org-flag-heading nil))))
  13144. (defun org-show-hidden-entry ()
  13145. "Show an entry where even the heading is hidden."
  13146. (save-excursion
  13147. (org-show-entry)))
  13148. (defun org-flag-heading (flag &optional entry)
  13149. "Flag the current heading. FLAG non-nil means make invisible.
  13150. When ENTRY is non-nil, show the entire entry."
  13151. (save-excursion
  13152. (org-back-to-heading t)
  13153. ;; Check if we should show the entire entry
  13154. (if entry
  13155. (progn
  13156. (org-show-entry)
  13157. (save-excursion
  13158. (and (outline-next-heading)
  13159. (org-flag-heading nil))))
  13160. (outline-flag-region (max (point-min) (1- (point)))
  13161. (save-excursion (outline-end-of-heading) (point))
  13162. flag))))
  13163. (defun org-forward-same-level (arg)
  13164. "Move forward to the ARG'th subheading at same level as this one.
  13165. Stop at the first and last subheadings of a superior heading.
  13166. This is like outline-forward-same-level, but invisible headings are ok."
  13167. (interactive "p")
  13168. (org-back-to-heading t)
  13169. (while (> arg 0)
  13170. (let ((point-to-move-to (save-excursion
  13171. (org-get-next-sibling))))
  13172. (if point-to-move-to
  13173. (progn
  13174. (goto-char point-to-move-to)
  13175. (setq arg (1- arg)))
  13176. (progn
  13177. (setq arg 0)
  13178. (error "No following same-level heading"))))))
  13179. (defun org-get-next-sibling ()
  13180. "Move to next heading of the same level, and return point.
  13181. If there is no such heading, return nil.
  13182. This is like outline-next-sibling, but invisible headings are ok."
  13183. (let ((level (funcall outline-level)))
  13184. (outline-next-heading)
  13185. (while (and (not (eobp)) (> (funcall outline-level) level))
  13186. (outline-next-heading))
  13187. (if (or (eobp) (< (funcall outline-level) level))
  13188. nil
  13189. (point))))
  13190. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  13191. ;; This is an exact copy of the original function, but it uses
  13192. ;; `org-back-to-heading', to make it work also in invisible
  13193. ;; trees. And is uses an invisible-OK argument.
  13194. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13195. (org-back-to-heading invisible-OK)
  13196. (let ((first t)
  13197. (level (funcall outline-level)))
  13198. (while (and (not (eobp))
  13199. (or first (> (funcall outline-level) level)))
  13200. (setq first nil)
  13201. (outline-next-heading))
  13202. (unless to-heading
  13203. (if (memq (preceding-char) '(?\n ?\^M))
  13204. (progn
  13205. ;; Go to end of line before heading
  13206. (forward-char -1)
  13207. (if (memq (preceding-char) '(?\n ?\^M))
  13208. ;; leave blank line before heading
  13209. (forward-char -1))))))
  13210. (point))
  13211. (defun org-show-subtree ()
  13212. "Show everything after this heading at deeper levels."
  13213. (outline-flag-region
  13214. (point)
  13215. (save-excursion
  13216. (outline-end-of-subtree) (outline-next-heading) (point))
  13217. nil))
  13218. (defun org-show-entry ()
  13219. "Show the body directly following this heading.
  13220. Show the heading too, if it is currently invisible."
  13221. (interactive)
  13222. (save-excursion
  13223. (condition-case nil
  13224. (progn
  13225. (org-back-to-heading t)
  13226. (outline-flag-region
  13227. (max (point-min) (1- (point)))
  13228. (save-excursion
  13229. (re-search-forward
  13230. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13231. (or (match-beginning 1) (point-max)))
  13232. nil))
  13233. (error nil))))
  13234. (defun org-make-options-regexp (kwds)
  13235. "Make a regular expression for keyword lines."
  13236. (concat
  13237. "^"
  13238. "#?[ \t]*\\+\\("
  13239. (mapconcat 'regexp-quote kwds "\\|")
  13240. "\\):[ \t]*"
  13241. "\\(.+\\)"))
  13242. ;; Make isearch reveal the necessary context
  13243. (defun org-isearch-end ()
  13244. "Reveal context after isearch exits."
  13245. (when isearch-success ; only if search was successful
  13246. (if (featurep 'xemacs)
  13247. ;; Under XEmacs, the hook is run in the correct place,
  13248. ;; we directly show the context.
  13249. (org-show-context 'isearch)
  13250. ;; In Emacs the hook runs *before* restoring the overlays.
  13251. ;; So we have to use a one-time post-command-hook to do this.
  13252. ;; (Emacs 22 has a special variable, see function `org-mode')
  13253. (unless (and (boundp 'isearch-mode-end-hook-quit)
  13254. isearch-mode-end-hook-quit)
  13255. ;; Only when the isearch was not quitted.
  13256. (org-add-hook 'post-command-hook 'org-isearch-post-command
  13257. 'append 'local)))))
  13258. (defun org-isearch-post-command ()
  13259. "Remove self from hook, and show context."
  13260. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  13261. (org-show-context 'isearch))
  13262. ;;;; Integration with and fixes for other packages
  13263. ;;; Imenu support
  13264. (defvar org-imenu-markers nil
  13265. "All markers currently used by Imenu.")
  13266. (make-variable-buffer-local 'org-imenu-markers)
  13267. (defun org-imenu-new-marker (&optional pos)
  13268. "Return a new marker for use by Imenu, and remember the marker."
  13269. (let ((m (make-marker)))
  13270. (move-marker m (or pos (point)))
  13271. (push m org-imenu-markers)
  13272. m))
  13273. (defun org-imenu-get-tree ()
  13274. "Produce the index for Imenu."
  13275. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  13276. (setq org-imenu-markers nil)
  13277. (let* ((n org-imenu-depth)
  13278. (re (concat "^" outline-regexp))
  13279. (subs (make-vector (1+ n) nil))
  13280. (last-level 0)
  13281. m tree level head)
  13282. (save-excursion
  13283. (save-restriction
  13284. (widen)
  13285. (goto-char (point-max))
  13286. (while (re-search-backward re nil t)
  13287. (setq level (org-reduced-level (funcall outline-level)))
  13288. (when (<= level n)
  13289. (looking-at org-complex-heading-regexp)
  13290. (setq head (org-link-display-format
  13291. (org-match-string-no-properties 4))
  13292. m (org-imenu-new-marker))
  13293. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  13294. (if (>= level last-level)
  13295. (push (cons head m) (aref subs level))
  13296. (push (cons head (aref subs (1+ level))) (aref subs level))
  13297. (loop for i from (1+ level) to n do (aset subs i nil)))
  13298. (setq last-level level)))))
  13299. (aref subs 1)))
  13300. (eval-after-load "imenu"
  13301. '(progn
  13302. (add-hook 'imenu-after-jump-hook
  13303. (lambda ()
  13304. (if (eq major-mode 'org-mode)
  13305. (org-show-context 'org-goto))))))
  13306. (defun org-link-display-format (link)
  13307. "Replace a link with either the description, or the link target
  13308. if no description is present"
  13309. (save-match-data
  13310. (if (string-match org-bracket-link-analytic-regexp link)
  13311. (replace-match (or (match-string 5 link)
  13312. (concat (match-string 1 link)
  13313. (match-string 3 link)))
  13314. nil nil link)
  13315. link)))
  13316. ;; Speedbar support
  13317. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  13318. "Overlay marking the agenda restriction line in speedbar.")
  13319. (org-overlay-put org-speedbar-restriction-lock-overlay
  13320. 'face 'org-agenda-restriction-lock)
  13321. (org-overlay-put org-speedbar-restriction-lock-overlay
  13322. 'help-echo "Agendas are currently limited to this item.")
  13323. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  13324. (defun org-speedbar-set-agenda-restriction ()
  13325. "Restrict future agenda commands to the location at point in speedbar.
  13326. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  13327. (interactive)
  13328. (require 'org-agenda)
  13329. (let (p m tp np dir txt w)
  13330. (cond
  13331. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13332. 'org-imenu t))
  13333. (setq m (get-text-property p 'org-imenu-marker))
  13334. (save-excursion
  13335. (save-restriction
  13336. (set-buffer (marker-buffer m))
  13337. (goto-char m)
  13338. (org-agenda-set-restriction-lock 'subtree))))
  13339. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  13340. 'speedbar-function 'speedbar-find-file))
  13341. (setq tp (previous-single-property-change
  13342. (1+ p) 'speedbar-function)
  13343. np (next-single-property-change
  13344. tp 'speedbar-function)
  13345. dir (speedbar-line-directory)
  13346. txt (buffer-substring-no-properties (or tp (point-min))
  13347. (or np (point-max))))
  13348. (save-excursion
  13349. (save-restriction
  13350. (set-buffer (find-file-noselect
  13351. (let ((default-directory dir))
  13352. (expand-file-name txt))))
  13353. (unless (org-mode-p)
  13354. (error "Cannot restrict to non-Org-mode file"))
  13355. (org-agenda-set-restriction-lock 'file))))
  13356. (t (error "Don't know how to restrict Org-mode's agenda")))
  13357. (org-move-overlay org-speedbar-restriction-lock-overlay
  13358. (point-at-bol) (point-at-eol))
  13359. (setq current-prefix-arg nil)
  13360. (org-agenda-maybe-redo)))
  13361. (eval-after-load "speedbar"
  13362. '(progn
  13363. (speedbar-add-supported-extension ".org")
  13364. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  13365. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  13366. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  13367. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  13368. (add-hook 'speedbar-visiting-tag-hook
  13369. (lambda () (and (org-mode-p) (org-show-context 'org-goto))))))
  13370. ;;; Fixes and Hacks for problems with other packages
  13371. ;; Make flyspell not check words in links, to not mess up our keymap
  13372. (defun org-mode-flyspell-verify ()
  13373. "Don't let flyspell put overlays at active buttons."
  13374. (not (get-text-property (point) 'keymap)))
  13375. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13376. (eval-after-load "bookmark"
  13377. '(if (boundp 'bookmark-after-jump-hook)
  13378. ;; We can use the hook
  13379. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13380. ;; Hook not available, use advice
  13381. (defadvice bookmark-jump (after org-make-visible activate)
  13382. "Make the position visible."
  13383. (org-bookmark-jump-unhide))))
  13384. ;; Make sure saveplace show the location if it was hidden
  13385. (eval-after-load "saveplace"
  13386. '(defadvice save-place-find-file-hook (after org-make-visible activate)
  13387. "Make the position visible."
  13388. (org-bookmark-jump-unhide)))
  13389. (defun org-bookmark-jump-unhide ()
  13390. "Unhide the current position, to show the bookmark location."
  13391. (and (org-mode-p)
  13392. (or (org-invisible-p)
  13393. (save-excursion (goto-char (max (point-min) (1- (point))))
  13394. (org-invisible-p)))
  13395. (org-show-context 'bookmark-jump)))
  13396. ;; Make session.el ignore our circular variable
  13397. (eval-after-load "session"
  13398. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  13399. ;;;; Experimental code
  13400. (defun org-closed-in-range ()
  13401. "Sparse tree of items closed in a certain time range.
  13402. Still experimental, may disappear in the future."
  13403. (interactive)
  13404. ;; Get the time interval from the user.
  13405. (let* ((time1 (time-to-seconds
  13406. (org-read-date nil 'to-time nil "Starting date: ")))
  13407. (time2 (time-to-seconds
  13408. (org-read-date nil 'to-time nil "End date:")))
  13409. ;; callback function
  13410. (callback (lambda ()
  13411. (let ((time
  13412. (time-to-seconds
  13413. (apply 'encode-time
  13414. (org-parse-time-string
  13415. (match-string 1))))))
  13416. ;; check if time in interval
  13417. (and (>= time time1) (<= time time2))))))
  13418. ;; make tree, check each match with the callback
  13419. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  13420. ;;;; Finish up
  13421. (provide 'org)
  13422. (run-hooks 'org-load-hook)
  13423. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13424. ;;; org.el ends here