org.el 501 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657
  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.02pre-04
  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, or (at your option)
  15. ;; 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; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;; Installation and Activation
  47. ;; ---------------------------
  48. ;; See the corresponding sections in the manual at
  49. ;;
  50. ;; http://orgmode.org/org.html#Installation
  51. ;;
  52. ;; Documentation
  53. ;; -------------
  54. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  55. ;; distribution also contains a PDF version of it. At the homepage of
  56. ;; Org-mode, you can read the same text online as HTML. There is also an
  57. ;; excellent reference card made by Philip Rooke. This card can be found
  58. ;; in the etc/ directory of Emacs 22.
  59. ;;
  60. ;; A list of recent changes can be found at
  61. ;; http://orgmode.org/Changes.html
  62. ;;
  63. ;;; Code:
  64. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  65. (defvar org-table-formula-constants-local nil
  66. "Local version of `org-table-formula-constants'.")
  67. (make-variable-buffer-local 'org-table-formula-constants-local)
  68. ;;;; Require other packages
  69. (eval-when-compile
  70. (require 'cl)
  71. (require 'gnus-sum)
  72. (require 'calendar))
  73. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  74. ;; the file noutline.el being loaded.
  75. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  76. ;; We require noutline, which might be provided in outline.el
  77. (require 'outline) (require 'noutline)
  78. ;; Other stuff we need.
  79. (require 'time-date)
  80. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  81. (require 'easymenu)
  82. (require 'org-macs)
  83. (require 'org-compat)
  84. (require 'org-faces)
  85. ;;;; Customization variables
  86. ;;; Version
  87. (defconst org-version "6.02pre-04"
  88. "The version number of the file org.el.")
  89. (defun org-version (&optional here)
  90. "Show the org-mode version in the echo area.
  91. With prefix arg HERE, insert it at point."
  92. (interactive "P")
  93. (let ((version (format "Org-mode version %s" org-version)))
  94. (message version)
  95. (if here
  96. (insert version))))
  97. ;;; Compatibility constants
  98. ;;; The custom variables
  99. (defgroup org nil
  100. "Outline-based notes management and organizer."
  101. :tag "Org"
  102. :group 'outlines
  103. :group 'hypermedia
  104. :group 'calendar)
  105. (defcustom org-load-hook nil
  106. "Hook that is run after org.el has been loaded."
  107. :group 'org
  108. :type 'hook)
  109. (defvar org-modules) ; defined below
  110. (defvar org-modules-loaded nil
  111. "Have the modules been loaded already?")
  112. (defun org-load-modules-maybe (&optional force)
  113. "Load all extensions listed in `org-default-extensions'."
  114. (when (or force (not org-modules-loaded))
  115. (mapc (lambda (ext)
  116. (condition-case nil (require ext)
  117. (error (message "Problems while trying to load feature `%s'" ext))))
  118. org-modules)
  119. (setq org-modules-loaded t)))
  120. (defun org-set-modules (var value)
  121. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  122. (set var value)
  123. (when (featurep 'org)
  124. (org-load-modules-maybe 'force)))
  125. (defcustom org-modules '(org-bbdb org-bibtex org-gnus org-info org-infojs org-irc org-mew org-mhe org-rmail org-vm org-wl)
  126. "Modules that should always be loaded together with org.el.
  127. If a description starts with <C>, the file is not part of emacs
  128. and loading it will require that you have downloaded and properly installed
  129. the org-mode distribution.
  130. You can also use this system to load external packages (i.e. neither Org
  131. core modules, not modules from the CONTRIB directory). Just add symbols
  132. to the end of the list. If the package is called org-xyz.e, then you need
  133. to add the symbol `xyz', and the package must have a call to
  134. (provide 'org-xyz)"
  135. :group 'org
  136. :set 'org-set-modules
  137. :type
  138. '(set :greedy t
  139. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  140. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  141. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  142. (const :tag " info: Links to Info nodes" org-info)
  143. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  144. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  145. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  146. (const :tag " mew Links to Mew folders/messages" org-mew)
  147. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  148. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  149. (const :tag " vm: Links to VM folders/messages" org-vm)
  150. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  151. (const :tag " mouse: Additional mouse support" org-mouse)
  152. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  153. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  154. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  155. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  156. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  157. (const :tag "C id: Global id's for identifying entries" org-id)
  158. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  159. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  160. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  161. (const :tag "C mew: Support for links to messages in Mew" org-mew)
  162. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  163. (const :tag "C registry: A registry for Org links" org-registry)
  164. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  165. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  166. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  167. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  168. (defgroup org-startup nil
  169. "Options concerning startup of Org-mode."
  170. :tag "Org Startup"
  171. :group 'org)
  172. (defcustom org-startup-folded t
  173. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  174. This can also be configured on a per-file basis by adding one of
  175. the following lines anywhere in the buffer:
  176. #+STARTUP: fold
  177. #+STARTUP: nofold
  178. #+STARTUP: content"
  179. :group 'org-startup
  180. :type '(choice
  181. (const :tag "nofold: show all" nil)
  182. (const :tag "fold: overview" t)
  183. (const :tag "content: all headlines" content)))
  184. (defcustom org-startup-truncated t
  185. "Non-nil means, entering Org-mode will set `truncate-lines'.
  186. This is useful since some lines containing links can be very long and
  187. uninteresting. Also tables look terrible when wrapped."
  188. :group 'org-startup
  189. :type 'boolean)
  190. (defcustom org-startup-align-all-tables nil
  191. "Non-nil means, align all tables when visiting a file.
  192. This is useful when the column width in tables is forced with <N> cookies
  193. in table fields. Such tables will look correct only after the first re-align.
  194. This can also be configured on a per-file basis by adding one of
  195. the following lines anywhere in the buffer:
  196. #+STARTUP: align
  197. #+STARTUP: noalign"
  198. :group 'org-startup
  199. :type 'boolean)
  200. (defcustom org-insert-mode-line-in-empty-file nil
  201. "Non-nil means insert the first line setting Org-mode in empty files.
  202. When the function `org-mode' is called interactively in an empty file, this
  203. normally means that the file name does not automatically trigger Org-mode.
  204. To ensure that the file will always be in Org-mode in the future, a
  205. line enforcing Org-mode will be inserted into the buffer, if this option
  206. has been set."
  207. :group 'org-startup
  208. :type 'boolean)
  209. (defcustom org-replace-disputed-keys nil
  210. "Non-nil means use alternative key bindings for some keys.
  211. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  212. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  213. If you want to use Org-mode together with one of these other modes,
  214. or more generally if you would like to move some Org-mode commands to
  215. other keys, set this variable and configure the keys with the variable
  216. `org-disputed-keys'.
  217. This option is only relevant at load-time of Org-mode, and must be set
  218. *before* org.el is loaded. Changing it requires a restart of Emacs to
  219. become effective."
  220. :group 'org-startup
  221. :type 'boolean)
  222. (if (fboundp 'defvaralias)
  223. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  224. (defcustom org-disputed-keys
  225. '(([(shift up)] . [(meta p)])
  226. ([(shift down)] . [(meta n)])
  227. ([(shift left)] . [(meta -)])
  228. ([(shift right)] . [(meta +)])
  229. ([(control shift right)] . [(meta shift +)])
  230. ([(control shift left)] . [(meta shift -)]))
  231. "Keys for which Org-mode and other modes compete.
  232. This is an alist, cars are the default keys, second element specifies
  233. the alternative to use when `org-replace-disputed-keys' is t.
  234. Keys can be specified in any syntax supported by `define-key'.
  235. The value of this option takes effect only at Org-mode's startup,
  236. therefore you'll have to restart Emacs to apply it after changing."
  237. :group 'org-startup
  238. :type 'alist)
  239. (defun org-key (key)
  240. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  241. Or return the original if not disputed."
  242. (if org-replace-disputed-keys
  243. (let* ((nkey (key-description key))
  244. (x (org-find-if (lambda (x)
  245. (equal (key-description (car x)) nkey))
  246. org-disputed-keys)))
  247. (if x (cdr x) key))
  248. key))
  249. (defun org-find-if (predicate seq)
  250. (catch 'exit
  251. (while seq
  252. (if (funcall predicate (car seq))
  253. (throw 'exit (car seq))
  254. (pop seq)))))
  255. (defun org-defkey (keymap key def)
  256. "Define a key, possibly translated, as returned by `org-key'."
  257. (define-key keymap (org-key key) def))
  258. (defcustom org-ellipsis nil
  259. "The ellipsis to use in the Org-mode outline.
  260. When nil, just use the standard three dots. When a string, use that instead,
  261. When a face, use the standart 3 dots, but with the specified face.
  262. The change affects only Org-mode (which will then use its own display table).
  263. Changing this requires executing `M-x org-mode' in a buffer to become
  264. effective."
  265. :group 'org-startup
  266. :type '(choice (const :tag "Default" nil)
  267. (face :tag "Face" :value org-warning)
  268. (string :tag "String" :value "...#")))
  269. (defvar org-display-table nil
  270. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  271. (defgroup org-keywords nil
  272. "Keywords in Org-mode."
  273. :tag "Org Keywords"
  274. :group 'org)
  275. (defcustom org-deadline-string "DEADLINE:"
  276. "String to mark deadline entries.
  277. A deadline is this string, followed by a time stamp. Should be a word,
  278. terminated by a colon. You can insert a schedule keyword and
  279. a timestamp with \\[org-deadline].
  280. Changes become only effective after restarting Emacs."
  281. :group 'org-keywords
  282. :type 'string)
  283. (defcustom org-scheduled-string "SCHEDULED:"
  284. "String to mark scheduled TODO entries.
  285. A schedule is this string, followed by a time stamp. Should be a word,
  286. terminated by a colon. You can insert a schedule keyword and
  287. a timestamp with \\[org-schedule].
  288. Changes become only effective after restarting Emacs."
  289. :group 'org-keywords
  290. :type 'string)
  291. (defcustom org-closed-string "CLOSED:"
  292. "String used as the prefix for timestamps logging closing a TODO entry."
  293. :group 'org-keywords
  294. :type 'string)
  295. (defcustom org-clock-string "CLOCK:"
  296. "String used as prefix for timestamps clocking work hours on an item."
  297. :group 'org-keywords
  298. :type 'string)
  299. (defcustom org-comment-string "COMMENT"
  300. "Entries starting with this keyword will never be exported.
  301. An entry can be toggled between COMMENT and normal with
  302. \\[org-toggle-comment].
  303. Changes become only effective after restarting Emacs."
  304. :group 'org-keywords
  305. :type 'string)
  306. (defcustom org-quote-string "QUOTE"
  307. "Entries starting with this keyword will be exported in fixed-width font.
  308. Quoting applies only to the text in the entry following the headline, and does
  309. not extend beyond the next headline, even if that is lower level.
  310. An entry can be toggled between QUOTE and normal with
  311. \\[org-toggle-fixed-width-section]."
  312. :group 'org-keywords
  313. :type 'string)
  314. (defconst org-repeat-re
  315. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  316. "Regular expression for specifying repeated events.
  317. After a match, group 1 contains the repeat expression.")
  318. (defgroup org-structure nil
  319. "Options concerning the general structure of Org-mode files."
  320. :tag "Org Structure"
  321. :group 'org)
  322. (defgroup org-reveal-location nil
  323. "Options about how to make context of a location visible."
  324. :tag "Org Reveal Location"
  325. :group 'org-structure)
  326. (defconst org-context-choice
  327. '(choice
  328. (const :tag "Always" t)
  329. (const :tag "Never" nil)
  330. (repeat :greedy t :tag "Individual contexts"
  331. (cons
  332. (choice :tag "Context"
  333. (const agenda)
  334. (const org-goto)
  335. (const occur-tree)
  336. (const tags-tree)
  337. (const link-search)
  338. (const mark-goto)
  339. (const bookmark-jump)
  340. (const isearch)
  341. (const default))
  342. (boolean))))
  343. "Contexts for the reveal options.")
  344. (defcustom org-show-hierarchy-above '((default . t))
  345. "Non-nil means, show full hierarchy when revealing a location.
  346. Org-mode often shows locations in an org-mode file which might have
  347. been invisible before. When this is set, the hierarchy of headings
  348. above the exposed location is shown.
  349. Turning this off for example for sparse trees makes them very compact.
  350. Instead of t, this can also be an alist specifying this option for different
  351. contexts. Valid contexts are
  352. agenda when exposing an entry from the agenda
  353. org-goto when using the command `org-goto' on key C-c C-j
  354. occur-tree when using the command `org-occur' on key C-c /
  355. tags-tree when constructing a sparse tree based on tags matches
  356. link-search when exposing search matches associated with a link
  357. mark-goto when exposing the jump goal of a mark
  358. bookmark-jump when exposing a bookmark location
  359. isearch when exiting from an incremental search
  360. default default for all contexts not set explicitly"
  361. :group 'org-reveal-location
  362. :type org-context-choice)
  363. (defcustom org-show-following-heading '((default . nil))
  364. "Non-nil means, show following heading when revealing a location.
  365. Org-mode often shows locations in an org-mode file which might have
  366. been invisible before. When this is set, the heading following the
  367. match is shown.
  368. Turning this off for example for sparse trees makes them very compact,
  369. but makes it harder to edit the location of the match. In such a case,
  370. use the command \\[org-reveal] to show more context.
  371. Instead of t, this can also be an alist specifying this option for different
  372. contexts. See `org-show-hierarchy-above' for valid contexts."
  373. :group 'org-reveal-location
  374. :type org-context-choice)
  375. (defcustom org-show-siblings '((default . nil) (isearch t))
  376. "Non-nil means, show all sibling heading when revealing a location.
  377. Org-mode often shows locations in an org-mode file which might have
  378. been invisible before. When this is set, the sibling of the current entry
  379. heading are all made visible. If `org-show-hierarchy-above' is t,
  380. the same happens on each level of the hierarchy above the current entry.
  381. By default this is on for the isearch context, off for all other contexts.
  382. Turning this off for example for sparse trees makes them very compact,
  383. but makes it harder to edit the location of the match. In such a case,
  384. use the command \\[org-reveal] to show more context.
  385. Instead of t, this can also be an alist specifying this option for different
  386. contexts. See `org-show-hierarchy-above' for valid contexts."
  387. :group 'org-reveal-location
  388. :type org-context-choice)
  389. (defcustom org-show-entry-below '((default . nil))
  390. "Non-nil means, show the entry below a headline when revealing a location.
  391. Org-mode often shows locations in an org-mode file which might have
  392. been invisible before. When this is set, the text below the headline that is
  393. exposed is also shown.
  394. By default this is off for all contexts.
  395. Instead of t, this can also be an alist specifying this option for different
  396. contexts. See `org-show-hierarchy-above' for valid contexts."
  397. :group 'org-reveal-location
  398. :type org-context-choice)
  399. (defcustom org-indirect-buffer-display 'other-window
  400. "How should indirect tree buffers be displayed?
  401. This applies to indirect buffers created with the commands
  402. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  403. Valid values are:
  404. current-window Display in the current window
  405. other-window Just display in another window.
  406. dedicated-frame Create one new frame, and re-use it each time.
  407. new-frame Make a new frame each time. Note that in this case
  408. previously-made indirect buffers are kept, and you need to
  409. kill these buffers yourself."
  410. :group 'org-structure
  411. :group 'org-agenda-windows
  412. :type '(choice
  413. (const :tag "In current window" current-window)
  414. (const :tag "In current frame, other window" other-window)
  415. (const :tag "Each time a new frame" new-frame)
  416. (const :tag "One dedicated frame" dedicated-frame)))
  417. (defgroup org-cycle nil
  418. "Options concerning visibility cycling in Org-mode."
  419. :tag "Org Cycle"
  420. :group 'org-structure)
  421. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  422. "Names of drawers. Drawers are not opened by cycling on the headline above.
  423. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  424. this:
  425. :DRAWERNAME:
  426. .....
  427. :END:
  428. The drawer \"PROPERTIES\" is special for capturing properties through
  429. the property API.
  430. Drawers can be defined on the per-file basis with a line like:
  431. #+DRAWERS: HIDDEN STATE PROPERTIES"
  432. :group 'org-structure
  433. :type '(repeat (string :tag "Drawer Name")))
  434. (defcustom org-cycle-global-at-bob nil
  435. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  436. This makes it possible to do global cycling without having to use S-TAB or
  437. C-u TAB. For this special case to work, the first line of the buffer
  438. must not be a headline - it may be empty ot some other text. When used in
  439. this way, `org-cycle-hook' is disables temporarily, to make sure the
  440. cursor stays at the beginning of the buffer.
  441. When this option is nil, don't do anything special at the beginning
  442. of the buffer."
  443. :group 'org-cycle
  444. :type 'boolean)
  445. (defcustom org-cycle-emulate-tab t
  446. "Where should `org-cycle' emulate TAB.
  447. nil Never
  448. white Only in completely white lines
  449. whitestart Only at the beginning of lines, before the first non-white char
  450. t Everywhere except in headlines
  451. exc-hl-bol Everywhere except at the start of a headline
  452. If TAB is used in a place where it does not emulate TAB, the current subtree
  453. visibility is cycled."
  454. :group 'org-cycle
  455. :type '(choice (const :tag "Never" nil)
  456. (const :tag "Only in completely white lines" white)
  457. (const :tag "Before first char in a line" whitestart)
  458. (const :tag "Everywhere except in headlines" t)
  459. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  460. ))
  461. (defcustom org-cycle-separator-lines 2
  462. "Number of empty lines needed to keep an empty line between collapsed trees.
  463. If you leave an empty line between the end of a subtree and the following
  464. headline, this empty line is hidden when the subtree is folded.
  465. Org-mode will leave (exactly) one empty line visible if the number of
  466. empty lines is equal or larger to the number given in this variable.
  467. So the default 2 means, at least 2 empty lines after the end of a subtree
  468. are needed to produce free space between a collapsed subtree and the
  469. following headline.
  470. Special case: when 0, never leave empty lines in collapsed view."
  471. :group 'org-cycle
  472. :type 'integer)
  473. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  474. org-cycle-hide-drawers
  475. org-cycle-show-empty-lines
  476. org-optimize-window-after-visibility-change)
  477. "Hook that is run after `org-cycle' has changed the buffer visibility.
  478. The function(s) in this hook must accept a single argument which indicates
  479. the new state that was set by the most recent `org-cycle' command. The
  480. argument is a symbol. After a global state change, it can have the values
  481. `overview', `content', or `all'. After a local state change, it can have
  482. the values `folded', `children', or `subtree'."
  483. :group 'org-cycle
  484. :type 'hook)
  485. (defgroup org-edit-structure nil
  486. "Options concerning structure editing in Org-mode."
  487. :tag "Org Edit Structure"
  488. :group 'org-structure)
  489. (defcustom org-odd-levels-only nil
  490. "Non-nil means, skip even levels and only use odd levels for the outline.
  491. This has the effect that two stars are being added/taken away in
  492. promotion/demotion commands. It also influences how levels are
  493. handled by the exporters.
  494. Changing it requires restart of `font-lock-mode' to become effective
  495. for fontification also in regions already fontified.
  496. You may also set this on a per-file basis by adding one of the following
  497. lines to the buffer:
  498. #+STARTUP: odd
  499. #+STARTUP: oddeven"
  500. :group 'org-edit-structure
  501. :group 'org-font-lock
  502. :type 'boolean)
  503. (defcustom org-adapt-indentation t
  504. "Non-nil means, adapt indentation when promoting and demoting.
  505. When this is set and the *entire* text in an entry is indented, the
  506. indentation is increased by one space in a demotion command, and
  507. decreased by one in a promotion command. If any line in the entry
  508. body starts at column 0, indentation is not changed at all."
  509. :group 'org-edit-structure
  510. :type 'boolean)
  511. (defcustom org-special-ctrl-a/e nil
  512. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  513. When t, `C-a' will bring back the cursor to the beginning of the
  514. headline text, i.e. after the stars and after a possible TODO keyword.
  515. In an item, this will be the position after the bullet.
  516. When the cursor is already at that position, another `C-a' will bring
  517. it to the beginning of the line.
  518. `C-e' will jump to the end of the headline, ignoring the presence of tags
  519. in the headline. A second `C-e' will then jump to the true end of the
  520. line, after any tags.
  521. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  522. and only a directly following, identical keypress will bring the cursor
  523. to the special positions."
  524. :group 'org-edit-structure
  525. :type '(choice
  526. (const :tag "off" nil)
  527. (const :tag "after bullet first" t)
  528. (const :tag "border first" reversed)))
  529. (if (fboundp 'defvaralias)
  530. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  531. (defcustom org-special-ctrl-k nil
  532. "Non-nil means `C-k' will behave specially in headlines.
  533. When nil, `C-k' will call the default `kill-line' command.
  534. When t, the following will happen while the cursor is in the headline:
  535. - When the cursor is at the beginning of a headline, kill the entire
  536. line and possible the folded subtree below the line.
  537. - When in the middle of the headline text, kill the headline up to the tags.
  538. - When after the headline text, kill the tags."
  539. :group 'org-edit-structure
  540. :type 'boolean)
  541. (defcustom org-M-RET-may-split-line '((default . t))
  542. "Non-nil means, M-RET will split the line at the cursor position.
  543. When nil, it will go to the end of the line before making a
  544. new line.
  545. You may also set this option in a different way for different
  546. contexts. Valid contexts are:
  547. headline when creating a new headline
  548. item when creating a new item
  549. table in a table field
  550. default the value to be used for all contexts not explicitly
  551. customized"
  552. :group 'org-structure
  553. :group 'org-table
  554. :type '(choice
  555. (const :tag "Always" t)
  556. (const :tag "Never" nil)
  557. (repeat :greedy t :tag "Individual contexts"
  558. (cons
  559. (choice :tag "Context"
  560. (const headline)
  561. (const item)
  562. (const table)
  563. (const default))
  564. (boolean)))))
  565. (defcustom org-blank-before-new-entry '((heading . nil)
  566. (plain-list-item . nil))
  567. "Should `org-insert-heading' leave a blank line before new heading/item?
  568. The value is an alist, with `heading' and `plain-list-item' as car,
  569. and a boolean flag as cdr."
  570. :group 'org-edit-structure
  571. :type '(list
  572. (cons (const heading) (boolean))
  573. (cons (const plain-list-item) (boolean))))
  574. (defcustom org-insert-heading-hook nil
  575. "Hook being run after inserting a new heading."
  576. :group 'org-edit-structure
  577. :type 'hook)
  578. (defcustom org-enable-fixed-width-editor t
  579. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  580. This currently only means, they are never auto-wrapped.
  581. When nil, such lines will be treated like ordinary lines.
  582. See also the QUOTE keyword."
  583. :group 'org-edit-structure
  584. :type 'boolean)
  585. (defcustom org-goto-auto-isearch t
  586. "Non-nil means, typing characters in org-goto starts incremental search."
  587. :group 'org-edit-structure
  588. :type 'boolean)
  589. (defgroup org-sparse-trees nil
  590. "Options concerning sparse trees in Org-mode."
  591. :tag "Org Sparse Trees"
  592. :group 'org-structure)
  593. (defcustom org-highlight-sparse-tree-matches t
  594. "Non-nil means, highlight all matches that define a sparse tree.
  595. The highlights will automatically disappear the next time the buffer is
  596. changed by an edit command."
  597. :group 'org-sparse-trees
  598. :type 'boolean)
  599. (defcustom org-remove-highlights-with-change t
  600. "Non-nil means, any change to the buffer will remove temporary highlights.
  601. Such highlights are created by `org-occur' and `org-clock-display'.
  602. When nil, `C-c C-c needs to be used to get rid of the highlights.
  603. The highlights created by `org-preview-latex-fragment' always need
  604. `C-c C-c' to be removed."
  605. :group 'org-sparse-trees
  606. :group 'org-time
  607. :type 'boolean)
  608. (defcustom org-occur-hook '(org-first-headline-recenter)
  609. "Hook that is run after `org-occur' has constructed a sparse tree.
  610. This can be used to recenter the window to show as much of the structure
  611. as possible."
  612. :group 'org-sparse-trees
  613. :type 'hook)
  614. (defgroup org-plain-lists nil
  615. "Options concerning plain lists in Org-mode."
  616. :tag "Org Plain lists"
  617. :group 'org-structure)
  618. (defcustom org-cycle-include-plain-lists nil
  619. "Non-nil means, include plain lists into visibility cycling.
  620. This means that during cycling, plain list items will *temporarily* be
  621. interpreted as outline headlines with a level given by 1000+i where i is the
  622. indentation of the bullet. In all other operations, plain list items are
  623. not seen as headlines. For example, you cannot assign a TODO keyword to
  624. such an item."
  625. :group 'org-plain-lists
  626. :type 'boolean)
  627. (defcustom org-plain-list-ordered-item-terminator t
  628. "The character that makes a line with leading number an ordered list item.
  629. Valid values are ?. and ?\). To get both terminators, use t. While
  630. ?. may look nicer, it creates the danger that a line with leading
  631. number may be incorrectly interpreted as an item. ?\) therefore is
  632. the safe choice."
  633. :group 'org-plain-lists
  634. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  635. (const :tag "paren like in \"2)\"" ?\))
  636. (const :tab "both" t)))
  637. (defcustom org-empty-line-terminates-plain-lists nil
  638. "Non-nil means, an empty line ends all plain list levels.
  639. When nil, empty lines are part of the preceeding item."
  640. :group 'org-plain-lists
  641. :type 'boolean)
  642. (defcustom org-auto-renumber-ordered-lists t
  643. "Non-nil means, automatically renumber ordered plain lists.
  644. Renumbering happens when the sequence have been changed with
  645. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  646. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  647. :group 'org-plain-lists
  648. :type 'boolean)
  649. (defcustom org-provide-checkbox-statistics t
  650. "Non-nil means, update checkbox statistics after insert and toggle.
  651. When this is set, checkbox statistics is updated each time you either insert
  652. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  653. with \\[org-ctrl-c-ctrl-c\\]."
  654. :group 'org-plain-lists
  655. :type 'boolean)
  656. (defgroup org-imenu-and-speedbar nil
  657. "Options concerning imenu and speedbar in Org-mode."
  658. :tag "Org Imenu and Speedbar"
  659. :group 'org-structure)
  660. (defcustom org-imenu-depth 2
  661. "The maximum level for Imenu access to Org-mode headlines.
  662. This also applied for speedbar access."
  663. :group 'org-imenu-and-speedbar
  664. :type 'number)
  665. (defgroup org-table nil
  666. "Options concerning tables in Org-mode."
  667. :tag "Org Table"
  668. :group 'org)
  669. (defcustom org-enable-table-editor 'optimized
  670. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  671. When nil, such lines will be treated like ordinary lines.
  672. When equal to the symbol `optimized', the table editor will be optimized to
  673. do the following:
  674. - Automatic overwrite mode in front of whitespace in table fields.
  675. This makes the structure of the table stay in tact as long as the edited
  676. field does not exceed the column width.
  677. - Minimize the number of realigns. Normally, the table is aligned each time
  678. TAB or RET are pressed to move to another field. With optimization this
  679. happens only if changes to a field might have changed the column width.
  680. Optimization requires replacing the functions `self-insert-command',
  681. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  682. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  683. very good at guessing when a re-align will be necessary, but you can always
  684. force one with \\[org-ctrl-c-ctrl-c].
  685. If you would like to use the optimized version in Org-mode, but the
  686. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  687. This variable can be used to turn on and off the table editor during a session,
  688. but in order to toggle optimization, a restart is required.
  689. See also the variable `org-table-auto-blank-field'."
  690. :group 'org-table
  691. :type '(choice
  692. (const :tag "off" nil)
  693. (const :tag "on" t)
  694. (const :tag "on, optimized" optimized)))
  695. (defcustom org-table-tab-recognizes-table.el t
  696. "Non-nil means, TAB will automatically notice a table.el table.
  697. When it sees such a table, it moves point into it and - if necessary -
  698. calls `table-recognize-table'."
  699. :group 'org-table-editing
  700. :type 'boolean)
  701. (defgroup org-link nil
  702. "Options concerning links in Org-mode."
  703. :tag "Org Link"
  704. :group 'org)
  705. (defvar org-link-abbrev-alist-local nil
  706. "Buffer-local version of `org-link-abbrev-alist', which see.
  707. The value of this is taken from the #+LINK lines.")
  708. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  709. (defcustom org-link-abbrev-alist nil
  710. "Alist of link abbreviations.
  711. The car of each element is a string, to be replaced at the start of a link.
  712. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  713. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  714. [[linkkey:tag][description]]
  715. If REPLACE is a string, the tag will simply be appended to create the link.
  716. If the string contains \"%s\", the tag will be inserted there.
  717. REPLACE may also be a function that will be called with the tag as the
  718. only argument to create the link, which should be returned as a string.
  719. See the manual for examples."
  720. :group 'org-link
  721. :type 'alist)
  722. (defcustom org-descriptive-links t
  723. "Non-nil means, hide link part and only show description of bracket links.
  724. Bracket links are like [[link][descritpion]]. This variable sets the initial
  725. state in new org-mode buffers. The setting can then be toggled on a
  726. per-buffer basis from the Org->Hyperlinks menu."
  727. :group 'org-link
  728. :type 'boolean)
  729. (defcustom org-link-file-path-type 'adaptive
  730. "How the path name in file links should be stored.
  731. Valid values are:
  732. relative Relative to the current directory, i.e. the directory of the file
  733. into which the link is being inserted.
  734. absolute Absolute path, if possible with ~ for home directory.
  735. noabbrev Absolute path, no abbreviation of home directory.
  736. adaptive Use relative path for files in the current directory and sub-
  737. directories of it. For other files, use an absolute path."
  738. :group 'org-link
  739. :type '(choice
  740. (const relative)
  741. (const absolute)
  742. (const noabbrev)
  743. (const adaptive)))
  744. (defcustom org-activate-links '(bracket angle plain radio tag date)
  745. "Types of links that should be activated in Org-mode files.
  746. This is a list of symbols, each leading to the activation of a certain link
  747. type. In principle, it does not hurt to turn on most link types - there may
  748. be a small gain when turning off unused link types. The types are:
  749. bracket The recommended [[link][description]] or [[link]] links with hiding.
  750. angular Links in angular brackes that may contain whitespace like
  751. <bbdb:Carsten Dominik>.
  752. plain Plain links in normal text, no whitespace, like http://google.com.
  753. radio Text that is matched by a radio target, see manual for details.
  754. tag Tag settings in a headline (link to tag search).
  755. date Time stamps (link to calendar).
  756. Changing this variable requires a restart of Emacs to become effective."
  757. :group 'org-link
  758. :type '(set (const :tag "Double bracket links (new style)" bracket)
  759. (const :tag "Angular bracket links (old style)" angular)
  760. (const :tag "Plain text links" plain)
  761. (const :tag "Radio target matches" radio)
  762. (const :tag "Tags" tag)
  763. (const :tag "Timestamps" date)))
  764. (defcustom org-make-link-description-function nil
  765. "Function to use to generate link descriptions from links. If
  766. nil the link location will be used. This function must take two
  767. parameters; the first is the link and the second the description
  768. org-insert-link has generated, and should return the description
  769. to use."
  770. :group 'org-link
  771. :type 'function)
  772. (defgroup org-link-store nil
  773. "Options concerning storing links in Org-mode."
  774. :tag "Org Store Link"
  775. :group 'org-link)
  776. (defcustom org-email-link-description-format "Email %c: %.30s"
  777. "Format of the description part of a link to an email or usenet message.
  778. The following %-excapes will be replaced by corresponding information:
  779. %F full \"From\" field
  780. %f name, taken from \"From\" field, address if no name
  781. %T full \"To\" field
  782. %t first name in \"To\" field, address if no name
  783. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  784. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  785. %s subject
  786. %m message-id.
  787. You may use normal field width specification between the % and the letter.
  788. This is for example useful to limit the length of the subject.
  789. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  790. :group 'org-link-store
  791. :type 'string)
  792. (defcustom org-from-is-user-regexp
  793. (let (r1 r2)
  794. (when (and user-mail-address (not (string= user-mail-address "")))
  795. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  796. (when (and user-full-name (not (string= user-full-name "")))
  797. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  798. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  799. "Regexp mached against the \"From:\" header of an email or usenet message.
  800. It should match if the message is from the user him/herself."
  801. :group 'org-link-store
  802. :type 'regexp)
  803. (defcustom org-context-in-file-links t
  804. "Non-nil means, file links from `org-store-link' contain context.
  805. A search string will be added to the file name with :: as separator and
  806. used to find the context when the link is activated by the command
  807. `org-open-at-point'.
  808. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  809. negates this setting for the duration of the command."
  810. :group 'org-link-store
  811. :type 'boolean)
  812. (defcustom org-keep-stored-link-after-insertion nil
  813. "Non-nil means, keep link in list for entire session.
  814. The command `org-store-link' adds a link pointing to the current
  815. location to an internal list. These links accumulate during a session.
  816. The command `org-insert-link' can be used to insert links into any
  817. Org-mode file (offering completion for all stored links). When this
  818. option is nil, every link which has been inserted once using \\[org-insert-link]
  819. will be removed from the list, to make completing the unused links
  820. more efficient."
  821. :group 'org-link-store
  822. :type 'boolean)
  823. (defgroup org-link-follow nil
  824. "Options concerning following links in Org-mode."
  825. :tag "Org Follow Link"
  826. :group 'org-link)
  827. (defcustom org-follow-link-hook nil
  828. "Hook that is run after a link has been followed."
  829. :group 'org-link-follow
  830. :type 'hook)
  831. (defcustom org-tab-follows-link nil
  832. "Non-nil means, on links TAB will follow the link.
  833. Needs to be set before org.el is loaded."
  834. :group 'org-link-follow
  835. :type 'boolean)
  836. (defcustom org-return-follows-link nil
  837. "Non-nil means, on links RET will follow the link.
  838. Needs to be set before org.el is loaded."
  839. :group 'org-link-follow
  840. :type 'boolean)
  841. (defcustom org-mouse-1-follows-link
  842. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  843. "Non-nil means, mouse-1 on a link will follow the link.
  844. A longer mouse click will still set point. Does not work on XEmacs.
  845. Needs to be set before org.el is loaded."
  846. :group 'org-link-follow
  847. :type 'boolean)
  848. (defcustom org-mark-ring-length 4
  849. "Number of different positions to be recorded in the ring
  850. Changing this requires a restart of Emacs to work correctly."
  851. :group 'org-link-follow
  852. :type 'interger)
  853. (defcustom org-link-frame-setup
  854. '((vm . vm-visit-folder-other-frame)
  855. (gnus . gnus-other-frame)
  856. (file . find-file-other-window))
  857. "Setup the frame configuration for following links.
  858. When following a link with Emacs, it may often be useful to display
  859. this link in another window or frame. This variable can be used to
  860. set this up for the different types of links.
  861. For VM, use any of
  862. `vm-visit-folder'
  863. `vm-visit-folder-other-frame'
  864. For Gnus, use any of
  865. `gnus'
  866. `gnus-other-frame'
  867. For FILE, use any of
  868. `find-file'
  869. `find-file-other-window'
  870. `find-file-other-frame'
  871. For the calendar, use the variable `calendar-setup'.
  872. For BBDB, it is currently only possible to display the matches in
  873. another window."
  874. :group 'org-link-follow
  875. :type '(list
  876. (cons (const vm)
  877. (choice
  878. (const vm-visit-folder)
  879. (const vm-visit-folder-other-window)
  880. (const vm-visit-folder-other-frame)))
  881. (cons (const gnus)
  882. (choice
  883. (const gnus)
  884. (const gnus-other-frame)))
  885. (cons (const file)
  886. (choice
  887. (const find-file)
  888. (const find-file-other-window)
  889. (const find-file-other-frame)))))
  890. (defcustom org-display-internal-link-with-indirect-buffer nil
  891. "Non-nil means, use indirect buffer to display infile links.
  892. Activating internal links (from one location in a file to another location
  893. in the same file) normally just jumps to the location. When the link is
  894. activated with a C-u prefix (or with mouse-3), the link is displayed in
  895. another window. When this option is set, the other window actually displays
  896. an indirect buffer clone of the current buffer, to avoid any visibility
  897. changes to the current buffer."
  898. :group 'org-link-follow
  899. :type 'boolean)
  900. (defcustom org-open-non-existing-files nil
  901. "Non-nil means, `org-open-file' will open non-existing files.
  902. When nil, an error will be generated."
  903. :group 'org-link-follow
  904. :type 'boolean)
  905. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  906. "Function and arguments to call for following mailto links.
  907. This is a list with the first element being a lisp function, and the
  908. remaining elements being arguments to the function. In string arguments,
  909. %a will be replaced by the address, and %s will be replaced by the subject
  910. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  911. :group 'org-link-follow
  912. :type '(choice
  913. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  914. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  915. (const :tag "message-mail" (message-mail "%a" "%s"))
  916. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  917. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  918. "Non-nil means, ask for confirmation before executing shell links.
  919. Shell links can be dangerous: just think about a link
  920. [[shell:rm -rf ~/*][Google Search]]
  921. This link would show up in your Org-mode document as \"Google Search\",
  922. but really it would remove your entire home directory.
  923. Therefore we advise against setting this variable to nil.
  924. Just change it to `y-or-n-p' of you want to confirm with a
  925. single keystroke rather than having to type \"yes\"."
  926. :group 'org-link-follow
  927. :type '(choice
  928. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  929. (const :tag "with y-or-n (faster)" y-or-n-p)
  930. (const :tag "no confirmation (dangerous)" nil)))
  931. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  932. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  933. Elisp links can be dangerous: just think about a link
  934. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  935. This link would show up in your Org-mode document as \"Google Search\",
  936. but really it would remove your entire home directory.
  937. Therefore we advise against setting this variable to nil.
  938. Just change it to `y-or-n-p' of you want to confirm with a
  939. single keystroke rather than having to type \"yes\"."
  940. :group 'org-link-follow
  941. :type '(choice
  942. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  943. (const :tag "with y-or-n (faster)" y-or-n-p)
  944. (const :tag "no confirmation (dangerous)" nil)))
  945. (defconst org-file-apps-defaults-gnu
  946. '((remote . emacs)
  947. (t . mailcap))
  948. "Default file applications on a UNIX or GNU/Linux system.
  949. See `org-file-apps'.")
  950. (defconst org-file-apps-defaults-macosx
  951. '((remote . emacs)
  952. (t . "open %s")
  953. ("ps" . "gv %s")
  954. ("ps.gz" . "gv %s")
  955. ("eps" . "gv %s")
  956. ("eps.gz" . "gv %s")
  957. ("dvi" . "xdvi %s")
  958. ("fig" . "xfig %s"))
  959. "Default file applications on a MacOS X system.
  960. The system \"open\" is known as a default, but we use X11 applications
  961. for some files for which the OS does not have a good default.
  962. See `org-file-apps'.")
  963. (defconst org-file-apps-defaults-windowsnt
  964. (list
  965. '(remote . emacs)
  966. (cons t
  967. (list (if (featurep 'xemacs)
  968. 'mswindows-shell-execute
  969. 'w32-shell-execute)
  970. "open" 'file)))
  971. "Default file applications on a Windows NT system.
  972. The system \"open\" is used for most files.
  973. See `org-file-apps'.")
  974. (defcustom org-file-apps
  975. '(
  976. ("txt" . emacs)
  977. ("tex" . emacs)
  978. ("ltx" . emacs)
  979. ("org" . emacs)
  980. ("el" . emacs)
  981. ("bib" . emacs)
  982. )
  983. "External applications for opening `file:path' items in a document.
  984. Org-mode uses system defaults for different file types, but
  985. you can use this variable to set the application for a given file
  986. extension. The entries in this list are cons cells where the car identifies
  987. files and the cdr the corresponding command. Possible values for the
  988. file identifier are
  989. \"ext\" A string identifying an extension
  990. `directory' Matches a directory
  991. `remote' Matches a remote file, accessible through tramp or efs.
  992. Remote files most likely should be visited through Emacs
  993. because external applications cannot handle such paths.
  994. t Default for all remaining files
  995. Possible values for the command are:
  996. `emacs' The file will be visited by the current Emacs process.
  997. `default' Use the default application for this file type.
  998. string A command to be executed by a shell; %s will be replaced
  999. by the path to the file.
  1000. sexp A Lisp form which will be evaluated. The file path will
  1001. be available in the Lisp variable `file'.
  1002. For more examples, see the system specific constants
  1003. `org-file-apps-defaults-macosx'
  1004. `org-file-apps-defaults-windowsnt'
  1005. `org-file-apps-defaults-gnu'."
  1006. :group 'org-link-follow
  1007. :type '(repeat
  1008. (cons (choice :value ""
  1009. (string :tag "Extension")
  1010. (const :tag "Default for unrecognized files" t)
  1011. (const :tag "Remote file" remote)
  1012. (const :tag "Links to a directory" directory))
  1013. (choice :value ""
  1014. (const :tag "Visit with Emacs" emacs)
  1015. (const :tag "Use system default" default)
  1016. (string :tag "Command")
  1017. (sexp :tag "Lisp form")))))
  1018. (defgroup org-refile nil
  1019. "Options concerning refiling entries in Org-mode."
  1020. :tag "Org Remember"
  1021. :group 'org)
  1022. (defcustom org-directory "~/org"
  1023. "Directory with org files.
  1024. This directory will be used as default to prompt for org files.
  1025. Used by the hooks for remember.el."
  1026. :group 'org-refile
  1027. :group 'org-remember
  1028. :type 'directory)
  1029. (defcustom org-default-notes-file "~/.notes"
  1030. "Default target for storing notes.
  1031. Used by the hooks for remember.el. This can be a string, or nil to mean
  1032. the value of `remember-data-file'.
  1033. You can set this on a per-template basis with the variable
  1034. `org-remember-templates'."
  1035. :group 'org-refile
  1036. :group 'org-remember
  1037. :type '(choice
  1038. (const :tag "Default from remember-data-file" nil)
  1039. file))
  1040. (defcustom org-goto-interface 'outline
  1041. "The default interface to be used for `org-goto'.
  1042. Allowed vaues are:
  1043. outline The interface shows an outline of the relevant file
  1044. and the correct heading is found by moving through
  1045. the outline or by searching with incremental search.
  1046. outline-path-completion Headlines in the current buffer are offered via
  1047. completion."
  1048. :group 'org-refile
  1049. :type '(choice
  1050. (const :tag "Outline" outline)
  1051. (const :tag "Outline-path-completion" outline-path-completion)))
  1052. (defcustom org-reverse-note-order nil
  1053. "Non-nil means, store new notes at the beginning of a file or entry.
  1054. When nil, new notes will be filed to the end of a file or entry.
  1055. This can also be a list with cons cells of regular expressions that
  1056. are matched against file names, and values."
  1057. :group 'org-remember
  1058. :type '(choice
  1059. (const :tag "Reverse always" t)
  1060. (const :tag "Reverse never" nil)
  1061. (repeat :tag "By file name regexp"
  1062. (cons regexp boolean))))
  1063. (defcustom org-refile-targets nil
  1064. "Targets for refiling entries with \\[org-refile].
  1065. This is list of cons cells. Each cell contains:
  1066. - a specification of the files to be considered, either a list of files,
  1067. or a symbol whose function or variable value will be used to retrieve
  1068. a file name or a list of file names. Nil means, refile to a different
  1069. heading in the current buffer.
  1070. - A specification of how to find candidate refile targets. This may be
  1071. any of
  1072. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1073. This tag has to be present in all target headlines, inheritance will
  1074. not be considered.
  1075. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1076. todo keyword.
  1077. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1078. headlines that are refiling targets.
  1079. - a cons cell (:level . N). Any headline of level N is considered a target.
  1080. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1081. :group 'org-remember
  1082. :type '(repeat
  1083. (cons
  1084. (choice :value org-agenda-files
  1085. (const :tag "All agenda files" org-agenda-files)
  1086. (const :tag "Current buffer" nil)
  1087. (function) (variable) (file))
  1088. (choice :tag "Identify target headline by"
  1089. (cons :tag "Specific tag" (const :tag) (string))
  1090. (cons :tag "TODO keyword" (const :todo) (string))
  1091. (cons :tag "Regular expression" (const :regexp) (regexp))
  1092. (cons :tag "Level number" (const :level) (integer))
  1093. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1094. (defcustom org-refile-use-outline-path nil
  1095. "Non-nil means, provide refile targets as paths.
  1096. So a level 3 headline will be available as level1/level2/level3.
  1097. When the value is `file', also include the file name (without directory)
  1098. into the path. When `full-file-path', include the full file path."
  1099. :group 'org-remember
  1100. :type '(choice
  1101. (const :tag "Not" nil)
  1102. (const :tag "Yes" t)
  1103. (const :tag "Start with file name" file)
  1104. (const :tag "Start with full file path" full-file-path)))
  1105. (defgroup org-todo nil
  1106. "Options concerning TODO items in Org-mode."
  1107. :tag "Org TODO"
  1108. :group 'org)
  1109. (defgroup org-progress nil
  1110. "Options concerning Progress logging in Org-mode."
  1111. :tag "Org Progress"
  1112. :group 'org-time)
  1113. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1114. "List of TODO entry keyword sequences and their interpretation.
  1115. \\<org-mode-map>This is a list of sequences.
  1116. Each sequence starts with a symbol, either `sequence' or `type',
  1117. indicating if the keywords should be interpreted as a sequence of
  1118. action steps, or as different types of TODO items. The first
  1119. keywords are states requiring action - these states will select a headline
  1120. for inclusion into the global TODO list Org-mode produces. If one of
  1121. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1122. signify that no further action is necessary. If \"|\" is not found,
  1123. the last keyword is treated as the only DONE state of the sequence.
  1124. The command \\[org-todo] cycles an entry through these states, and one
  1125. additional state where no keyword is present. For details about this
  1126. cycling, see the manual.
  1127. TODO keywords and interpretation can also be set on a per-file basis with
  1128. the special #+SEQ_TODO and #+TYP_TODO lines.
  1129. Each keyword can optionally specify a character for fast state selection
  1130. \(in combination with the variable `org-use-fast-todo-selection')
  1131. and specifiers for state change logging, using the same syntax
  1132. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1133. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1134. indicates to record a time stamp each time this state is selected.
  1135. Each keyword may also specify if a timestamp or a note should be
  1136. recorded when entering or leaving the state, by adding additional
  1137. characters in the parenthesis after the keyword. This looks like this:
  1138. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1139. record only the time of the state change. With X and Y being either
  1140. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1141. Y when leaving the state if and only if the *target* state does not
  1142. define X. You may omit any of the fast-selection key or X or /Y,
  1143. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1144. For backward compatibility, this variable may also be just a list
  1145. of keywords - in this case the interptetation (sequence or type) will be
  1146. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1147. :group 'org-todo
  1148. :group 'org-keywords
  1149. :type '(choice
  1150. (repeat :tag "Old syntax, just keywords"
  1151. (string :tag "Keyword"))
  1152. (repeat :tag "New syntax"
  1153. (cons
  1154. (choice
  1155. :tag "Interpretation"
  1156. (const :tag "Sequence (cycling hits every state)" sequence)
  1157. (const :tag "Type (cycling directly to DONE)" type))
  1158. (repeat
  1159. (string :tag "Keyword"))))))
  1160. (defvar org-todo-keywords-1 nil
  1161. "All TODO and DONE keywords active in a buffer.")
  1162. (make-variable-buffer-local 'org-todo-keywords-1)
  1163. (defvar org-todo-keywords-for-agenda nil)
  1164. (defvar org-done-keywords-for-agenda nil)
  1165. (defvar org-agenda-contributing-files nil)
  1166. (defvar org-not-done-keywords nil)
  1167. (make-variable-buffer-local 'org-not-done-keywords)
  1168. (defvar org-done-keywords nil)
  1169. (make-variable-buffer-local 'org-done-keywords)
  1170. (defvar org-todo-heads nil)
  1171. (make-variable-buffer-local 'org-todo-heads)
  1172. (defvar org-todo-sets nil)
  1173. (make-variable-buffer-local 'org-todo-sets)
  1174. (defvar org-todo-log-states nil)
  1175. (make-variable-buffer-local 'org-todo-log-states)
  1176. (defvar org-todo-kwd-alist nil)
  1177. (make-variable-buffer-local 'org-todo-kwd-alist)
  1178. (defvar org-todo-key-alist nil)
  1179. (make-variable-buffer-local 'org-todo-key-alist)
  1180. (defvar org-todo-key-trigger nil)
  1181. (make-variable-buffer-local 'org-todo-key-trigger)
  1182. (defcustom org-todo-interpretation 'sequence
  1183. "Controls how TODO keywords are interpreted.
  1184. This variable is in principle obsolete and is only used for
  1185. backward compatibility, if the interpretation of todo keywords is
  1186. not given already in `org-todo-keywords'. See that variable for
  1187. more information."
  1188. :group 'org-todo
  1189. :group 'org-keywords
  1190. :type '(choice (const sequence)
  1191. (const type)))
  1192. (defcustom org-use-fast-todo-selection 'prefix
  1193. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1194. This variable describes if and under what circumstances the cycling
  1195. mechanism for TODO keywords will be replaced by a single-key, direct
  1196. selection scheme.
  1197. When nil, fast selection is never used.
  1198. When the symbol `prefix', it will be used when `org-todo' is called with
  1199. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1200. in an agenda buffer.
  1201. When t, fast selection is used by default. In this case, the prefix
  1202. argument forces cycling instead.
  1203. In all cases, the special interface is only used if access keys have actually
  1204. been assigned by the user, i.e. if keywords in the configuration are followed
  1205. by a letter in parenthesis, like TODO(t)."
  1206. :group 'org-todo
  1207. :type '(choice
  1208. (const :tag "Never" nil)
  1209. (const :tag "By default" t)
  1210. (const :tag "Only with C-u C-c C-t" prefix)))
  1211. (defcustom org-after-todo-state-change-hook nil
  1212. "Hook which is run after the state of a TODO item was changed.
  1213. The new state (a string with a TODO keyword, or nil) is available in the
  1214. Lisp variable `state'."
  1215. :group 'org-todo
  1216. :type 'hook)
  1217. (defcustom org-log-done nil
  1218. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1219. When equal to the list (done), also prompt for a closing note.
  1220. This can also be configured on a per-file basis by adding one of
  1221. the following lines anywhere in the buffer:
  1222. #+STARTUP: logdone
  1223. #+STARTUP: lognotedone
  1224. #+STARTUP: nologdone"
  1225. :group 'org-todo
  1226. :group 'org-progress
  1227. :type '(choice
  1228. (const :tag "No logging" nil)
  1229. (const :tag "Record CLOSED timestamp" time)
  1230. (const :tag "Record CLOSED timestamp with closing note." note)))
  1231. ;; Normalize old uses of org-log-done.
  1232. (cond
  1233. ((eq org-log-done t) (setq org-log-done 'time))
  1234. ((and (listp org-log-done) (memq 'done org-log-done))
  1235. (setq org-log-done 'note)))
  1236. (defcustom org-log-note-clock-out nil
  1237. "Non-nil means, recored a note when clocking out of an item.
  1238. This can also be configured on a per-file basis by adding one of
  1239. the following lines anywhere in the buffer:
  1240. #+STARTUP: lognoteclock-out
  1241. #+STARTUP: nolognoteclock-out"
  1242. :group 'org-todo
  1243. :group 'org-progress
  1244. :type 'boolean)
  1245. (defcustom org-log-done-with-time t
  1246. "Non-nil means, the CLOSED time stamp will contain date and time.
  1247. When nil, only the date will be recorded."
  1248. :group 'org-progress
  1249. :type 'boolean)
  1250. (defcustom org-log-note-headings
  1251. '((done . "CLOSING NOTE %t")
  1252. (state . "State %-12s %t")
  1253. (note . "Note taken on %t")
  1254. (clock-out . ""))
  1255. "Headings for notes added to entries.
  1256. The value is an alist, with the car being a symbol indicating the note
  1257. context, and the cdr is the heading to be used. The heading may also be the
  1258. empty string.
  1259. %t in the heading will be replaced by a time stamp.
  1260. %s will be replaced by the new TODO state, in double quotes.
  1261. %u will be replaced by the user name.
  1262. %U will be replaced by the full user name."
  1263. :group 'org-todo
  1264. :group 'org-progress
  1265. :type '(list :greedy t
  1266. (cons (const :tag "Heading when closing an item" done) string)
  1267. (cons (const :tag
  1268. "Heading when changing todo state (todo sequence only)"
  1269. state) string)
  1270. (cons (const :tag "Heading when just taking a note" note) string)
  1271. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1272. (unless (assq 'note org-log-note-headings)
  1273. (push '(note . "%t") org-log-note-headings))
  1274. (defcustom org-log-states-order-reversed t
  1275. "Non-nil means, the latest state change note will be directly after heading.
  1276. When nil, the notes will be orderer according to time."
  1277. :group 'org-todo
  1278. :group 'org-progress
  1279. :type 'boolean)
  1280. (defcustom org-log-repeat 'time
  1281. "Non-nil means, record moving through the DONE state when triggering repeat.
  1282. An auto-repeating tasks is immediately switched back to TODO when marked
  1283. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1284. the TODO keyword definition, or recording a cloing note by setting
  1285. `org-log-done', there will be no record of the task moving trhough DONE.
  1286. This variable forces taking a note anyway. Possible values are:
  1287. nil Don't force a record
  1288. time Record a time stamp
  1289. note Record a note
  1290. This option can also be set with on a per-file-basis with
  1291. #+STARTUP: logrepeat
  1292. #+STARTUP: lognoterepeat
  1293. #+STARTUP: nologrepeat
  1294. You can have local logging settings for a subtree by setting the LOGGING
  1295. property to one or more of these keywords."
  1296. :group 'org-todo
  1297. :group 'org-progress
  1298. :type '(choice
  1299. (const :tag "Don't force a record" nil)
  1300. (const :tag "Force recording the DONE state" time)
  1301. (const :tag "Force recording a note with the DONE state" note)))
  1302. (defgroup org-priorities nil
  1303. "Priorities in Org-mode."
  1304. :tag "Org Priorities"
  1305. :group 'org-todo)
  1306. (defcustom org-highest-priority ?A
  1307. "The highest priority of TODO items. A character like ?A, ?B etc.
  1308. Must have a smaller ASCII number than `org-lowest-priority'."
  1309. :group 'org-priorities
  1310. :type 'character)
  1311. (defcustom org-lowest-priority ?C
  1312. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1313. Must have a larger ASCII number than `org-highest-priority'."
  1314. :group 'org-priorities
  1315. :type 'character)
  1316. (defcustom org-default-priority ?B
  1317. "The default priority of TODO items.
  1318. This is the priority an item get if no explicit priority is given."
  1319. :group 'org-priorities
  1320. :type 'character)
  1321. (defcustom org-priority-start-cycle-with-default t
  1322. "Non-nil means, start with default priority when starting to cycle.
  1323. When this is nil, the first step in the cycle will be (depending on the
  1324. command used) one higher or lower that the default priority."
  1325. :group 'org-priorities
  1326. :type 'boolean)
  1327. (defgroup org-time nil
  1328. "Options concerning time stamps and deadlines in Org-mode."
  1329. :tag "Org Time"
  1330. :group 'org)
  1331. (defcustom org-insert-labeled-timestamps-at-point nil
  1332. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1333. When nil, these labeled time stamps are forces into the second line of an
  1334. entry, just after the headline. When scheduling from the global TODO list,
  1335. the time stamp will always be forced into the second line."
  1336. :group 'org-time
  1337. :type 'boolean)
  1338. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1339. "Formats for `format-time-string' which are used for time stamps.
  1340. It is not recommended to change this constant.")
  1341. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1342. "Number of minutes to round time stamps to.
  1343. These are two values, the first applies when first creating a time stamp.
  1344. The second applies when changing it with the commands `S-up' and `S-down'.
  1345. When changing the time stamp, this means that it will change in steps
  1346. of N minutes, as given by the second value.
  1347. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1348. numbers should be factors of 60, so for example 5, 10, 15.
  1349. When this is larger than 1, you can still force an exact time-stamp by using
  1350. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1351. and by using a prefix arg to `S-up/down' to specify the exact number
  1352. of minutes to shift."
  1353. :group 'org-time
  1354. :get '(lambda (var) ; Make sure all entries have 5 elements
  1355. (if (integerp (default-value var))
  1356. (list (default-value var) 5)
  1357. (default-value var)))
  1358. :type '(list
  1359. (integer :tag "when inserting times")
  1360. (integer :tag "when modifying times")))
  1361. ;; Normalize old customizations of this variable.
  1362. (when (integerp org-time-stamp-rounding-minutes)
  1363. (setq org-time-stamp-rounding-minutes
  1364. (list org-time-stamp-rounding-minutes
  1365. org-time-stamp-rounding-minutes)))
  1366. (defcustom org-display-custom-times nil
  1367. "Non-nil means, overlay custom formats over all time stamps.
  1368. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1369. To turn this on on a per-file basis, insert anywhere in the file:
  1370. #+STARTUP: customtime"
  1371. :group 'org-time
  1372. :set 'set-default
  1373. :type 'sexp)
  1374. (make-variable-buffer-local 'org-display-custom-times)
  1375. (defcustom org-time-stamp-custom-formats
  1376. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1377. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1378. These are overlayed over the default ISO format if the variable
  1379. `org-display-custom-times' is set. Time like %H:%M should be at the
  1380. end of the second format."
  1381. :group 'org-time
  1382. :type 'sexp)
  1383. (defun org-time-stamp-format (&optional long inactive)
  1384. "Get the right format for a time string."
  1385. (let ((f (if long (cdr org-time-stamp-formats)
  1386. (car org-time-stamp-formats))))
  1387. (if inactive
  1388. (concat "[" (substring f 1 -1) "]")
  1389. f)))
  1390. (defcustom org-deadline-warning-days 14
  1391. "No. of days before expiration during which a deadline becomes active.
  1392. This variable governs the display in sparse trees and in the agenda.
  1393. When 0 or negative, it means use this number (the absolute value of it)
  1394. even if a deadline has a different individual lead time specified."
  1395. :group 'org-time
  1396. :group 'org-agenda-daily/weekly
  1397. :type 'number)
  1398. (defcustom org-read-date-prefer-future t
  1399. "Non-nil means, assume future for incomplete date input from user.
  1400. This affects the following situations:
  1401. 1. The user gives a day, but no month.
  1402. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1403. read this as the third of *next* month. However, if you enter \"17\",
  1404. it will be considered as *this* month.
  1405. 2. The user gives a month but not a year.
  1406. For example, if it is april and you enter \"feb 2\", this will be read
  1407. as feb 2, *next* year. \"May 5\", however, will be this year.
  1408. Currently this does not work for ISO week specifications.
  1409. When this option is nil, the current month and year will always be used
  1410. as defaults."
  1411. :group 'org-time
  1412. :type 'boolean)
  1413. (defcustom org-read-date-display-live t
  1414. "Non-nil means, display current interpretation of date prompt live.
  1415. This display will be in an overlay, in the minibuffer."
  1416. :group 'org-time
  1417. :type 'boolean)
  1418. (defcustom org-read-date-popup-calendar t
  1419. "Non-nil means, pop up a calendar when prompting for a date.
  1420. In the calendar, the date can be selected with mouse-1. However, the
  1421. minibuffer will also be active, and you can simply enter the date as well.
  1422. When nil, only the minibuffer will be available."
  1423. :group 'org-time
  1424. :type 'boolean)
  1425. (if (fboundp 'defvaralias)
  1426. (defvaralias 'org-popup-calendar-for-date-prompt
  1427. 'org-read-date-popup-calendar))
  1428. (defcustom org-extend-today-until 0
  1429. "The hour when your day really ends.
  1430. This has influence for the following applications:
  1431. - When switching the agenda to \"today\". It it is still earlier than
  1432. the time given here, the day recognized as TODAY is actually yesterday.
  1433. - When a date is read from the user and it is still before the time given
  1434. here, the current date and time will be assumed to be yesterday, 23:59.
  1435. FIXME:
  1436. IMPORTANT: This is still a very experimental feature, it may disappear
  1437. again or it may be extended to mean more things."
  1438. :group 'org-time
  1439. :type 'number)
  1440. (defcustom org-edit-timestamp-down-means-later nil
  1441. "Non-nil means, S-down will increase the time in a time stamp.
  1442. When nil, S-up will increase."
  1443. :group 'org-time
  1444. :type 'boolean)
  1445. (defcustom org-calendar-follow-timestamp-change t
  1446. "Non-nil means, make the calendar window follow timestamp changes.
  1447. When a timestamp is modified and the calendar window is visible, it will be
  1448. moved to the new date."
  1449. :group 'org-time
  1450. :type 'boolean)
  1451. (defgroup org-tags nil
  1452. "Options concerning tags in Org-mode."
  1453. :tag "Org Tags"
  1454. :group 'org)
  1455. (defcustom org-tag-alist nil
  1456. "List of tags allowed in Org-mode files.
  1457. When this list is nil, Org-mode will base TAG input on what is already in the
  1458. buffer.
  1459. The value of this variable is an alist, the car of each entry must be a
  1460. keyword as a string, the cdr may be a character that is used to select
  1461. that tag through the fast-tag-selection interface.
  1462. See the manual for details."
  1463. :group 'org-tags
  1464. :type '(repeat
  1465. (choice
  1466. (cons (string :tag "Tag name")
  1467. (character :tag "Access char"))
  1468. (const :tag "Start radio group" (:startgroup))
  1469. (const :tag "End radio group" (:endgroup)))))
  1470. (defcustom org-use-fast-tag-selection 'auto
  1471. "Non-nil means, use fast tag selection scheme.
  1472. This is a special interface to select and deselect tags with single keys.
  1473. When nil, fast selection is never used.
  1474. When the symbol `auto', fast selection is used if and only if selection
  1475. characters for tags have been configured, either through the variable
  1476. `org-tag-alist' or through a #+TAGS line in the buffer.
  1477. When t, fast selection is always used and selection keys are assigned
  1478. automatically if necessary."
  1479. :group 'org-tags
  1480. :type '(choice
  1481. (const :tag "Always" t)
  1482. (const :tag "Never" nil)
  1483. (const :tag "When selection characters are configured" 'auto)))
  1484. (defcustom org-fast-tag-selection-single-key nil
  1485. "Non-nil means, fast tag selection exits after first change.
  1486. When nil, you have to press RET to exit it.
  1487. During fast tag selection, you can toggle this flag with `C-c'.
  1488. This variable can also have the value `expert'. In this case, the window
  1489. displaying the tags menu is not even shown, until you press C-c again."
  1490. :group 'org-tags
  1491. :type '(choice
  1492. (const :tag "No" nil)
  1493. (const :tag "Yes" t)
  1494. (const :tag "Expert" expert)))
  1495. (defvar org-fast-tag-selection-include-todo nil
  1496. "Non-nil means, fast tags selection interface will also offer TODO states.
  1497. This is an undocumented feature, you should not rely on it.")
  1498. (defcustom org-tags-column -80
  1499. "The column to which tags should be indented in a headline.
  1500. If this number is positive, it specifies the column. If it is negative,
  1501. it means that the tags should be flushright to that column. For example,
  1502. -80 works well for a normal 80 character screen."
  1503. :group 'org-tags
  1504. :type 'integer)
  1505. (defcustom org-auto-align-tags t
  1506. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1507. These operations change the length of a headline and therefore shift
  1508. the tags around. With this options turned on, after each such operation
  1509. the tags are again aligned to `org-tags-column'."
  1510. :group 'org-tags
  1511. :type 'boolean)
  1512. (defcustom org-use-tag-inheritance t
  1513. "Non-nil means, tags in levels apply also for sublevels.
  1514. When nil, only the tags directly given in a specific line apply there.
  1515. If you turn off this option, you very likely want to turn on the
  1516. companion option `org-tags-match-list-sublevels'.
  1517. This may also be a list of tags that should be inherited, or a regexp that
  1518. matches tags that should be inherited."
  1519. :group 'org-tags
  1520. :type '(choice
  1521. (const :tag "Not" nil)
  1522. (const :tag "Always" t)
  1523. (repeat :tag "Specific tags" (string :tag "Tag"))
  1524. (regexp :tag "Tags matched by regexp")))
  1525. (defun org-tag-inherit-p (tag)
  1526. "Check if TAG is one that should be inherited."
  1527. (cond
  1528. ((eq org-use-tag-inheritance t) t)
  1529. ((not org-use-tag-inheritance) nil)
  1530. ((stringp org-use-tag-inheritance)
  1531. (string-match org-use-tag-inheritance tag))
  1532. ((listp org-use-tag-inheritance)
  1533. (member tag org-use-tag-inheritance))
  1534. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1535. (defcustom org-tags-match-list-sublevels nil
  1536. "Non-nil means list also sublevels of headlines matching tag search.
  1537. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1538. the sublevels of a headline matching a tag search often also match
  1539. the same search. Listing all of them can create very long lists.
  1540. Setting this variable to nil causes subtrees of a match to be skipped.
  1541. This option is off by default, because inheritance in on. If you turn
  1542. inheritance off, you very likely want to turn this option on.
  1543. As a special case, if the tag search is restricted to TODO items, the
  1544. value of this variable is ignored and sublevels are always checked, to
  1545. make sure all corresponding TODO items find their way into the list."
  1546. :group 'org-tags
  1547. :type 'boolean)
  1548. (defvar org-tags-history nil
  1549. "History of minibuffer reads for tags.")
  1550. (defvar org-last-tags-completion-table nil
  1551. "The last used completion table for tags.")
  1552. (defvar org-after-tags-change-hook nil
  1553. "Hook that is run after the tags in a line have changed.")
  1554. (defgroup org-properties nil
  1555. "Options concerning properties in Org-mode."
  1556. :tag "Org Properties"
  1557. :group 'org)
  1558. (defcustom org-property-format "%-10s %s"
  1559. "How property key/value pairs should be formatted by `indent-line'.
  1560. When `indent-line' hits a property definition, it will format the line
  1561. according to this format, mainly to make sure that the values are
  1562. lined-up with respect to each other."
  1563. :group 'org-properties
  1564. :type 'string)
  1565. (defcustom org-use-property-inheritance nil
  1566. "Non-nil means, properties apply also for sublevels.
  1567. This setting is chiefly used during property searches. Turning it on can
  1568. cause significant overhead when doing a search, which is why it is not
  1569. on by default.
  1570. When nil, only the properties directly given in the current entry count.
  1571. When t, every property is inherited. The value may also be a list of
  1572. properties that should have inheritance, or a regular expression matching
  1573. properties that should be inherited.
  1574. However, note that some special properties use inheritance under special
  1575. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1576. and the properties ending in \"_ALL\" when they are used as descriptor
  1577. for valid values of a property.
  1578. Note for programmers:
  1579. When querying an entry with `org-entry-get', you can control if inheritance
  1580. should be used. By default, `org-entry-get' looks only at the local
  1581. properties. You can request inheritance by setting the inherit argument
  1582. to t (to force inheritance) or to `selective' (to respect the setting
  1583. in this variable)."
  1584. :group 'org-properties
  1585. :type '(choice
  1586. (const :tag "Not" nil)
  1587. (const :tag "Always" t)
  1588. (repeat :tag "Specific properties" (string :tag "Property"))
  1589. (regexp :tag "Properties matched by regexp")))
  1590. (defun org-property-inherit-p (property)
  1591. "Check if PROPERTY is one that should be inherited."
  1592. (cond
  1593. ((eq org-use-property-inheritance t) t)
  1594. ((not org-use-property-inheritance) nil)
  1595. ((stringp org-use-property-inheritance)
  1596. (string-match org-use-property-inheritance property))
  1597. ((listp org-use-property-inheritance)
  1598. (member property org-use-property-inheritance))
  1599. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1600. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1601. "The default column format, if no other format has been defined.
  1602. This variable can be set on the per-file basis by inserting a line
  1603. #+COLUMNS: %25ITEM ....."
  1604. :group 'org-properties
  1605. :type 'string)
  1606. (defcustom org-effort-property "Effort"
  1607. "The property that is being used to keep track of effort estimates.
  1608. Effort estimates given in this property need to have the format H:MM."
  1609. :group 'org-properties
  1610. :group 'org-progress
  1611. :type '(string :tag "Property"))
  1612. (defcustom org-global-properties nil
  1613. "List of property/value pairs that can be inherited by any entry.
  1614. You can set buffer-local values for this by adding lines like
  1615. #+PROPERTY: NAME VALUE"
  1616. :group 'org-properties
  1617. :type '(repeat
  1618. (cons (string :tag "Property")
  1619. (string :tag "Value"))))
  1620. (defvar org-local-properties nil
  1621. "List of property/value pairs that can be inherited by any entry.
  1622. Valid for the current buffer.
  1623. This variable is populated from #+PROPERTY lines.")
  1624. (defgroup org-agenda nil
  1625. "Options concerning agenda views in Org-mode."
  1626. :tag "Org Agenda"
  1627. :group 'org)
  1628. (defvar org-category nil
  1629. "Variable used by org files to set a category for agenda display.
  1630. Such files should use a file variable to set it, for example
  1631. # -*- mode: org; org-category: \"ELisp\"
  1632. or contain a special line
  1633. #+CATEGORY: ELisp
  1634. If the file does not specify a category, then file's base name
  1635. is used instead.")
  1636. (make-variable-buffer-local 'org-category)
  1637. (defcustom org-agenda-files nil
  1638. "The files to be used for agenda display.
  1639. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1640. \\[org-remove-file]. You can also use customize to edit the list.
  1641. If an entry is a directory, all files in that directory that are matched by
  1642. `org-agenda-file-regexp' will be part of the file list.
  1643. If the value of the variable is not a list but a single file name, then
  1644. the list of agenda files is actually stored and maintained in that file, one
  1645. agenda file per line."
  1646. :group 'org-agenda
  1647. :type '(choice
  1648. (repeat :tag "List of files and directories" file)
  1649. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1650. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1651. "Regular expression to match files for `org-agenda-files'.
  1652. If any element in the list in that variable contains a directory instead
  1653. of a normal file, all files in that directory that are matched by this
  1654. regular expression will be included."
  1655. :group 'org-agenda
  1656. :type 'regexp)
  1657. (defcustom org-agenda-text-search-extra-files nil
  1658. "List of extra files to be searched by text search commands.
  1659. These files will be search in addition to the agenda files by the
  1660. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1661. Note that these files will only be searched for text search commands,
  1662. not for the other agenda views like todo lists, tag searches or the weekly
  1663. agenda. This variable is intended to list notes and possibly archive files
  1664. that should also be searched by these two commands.
  1665. In fact, if the first element in the list is the symbol `agenda-archives',
  1666. than all archive files of all agenda files will be added to the search
  1667. scope."
  1668. :group 'org-agenda
  1669. :type '(set :greedy t
  1670. (const :tag "Agenda Archives" agenda-archives)
  1671. (repeat :inline t (file))))
  1672. (if (fboundp 'defvaralias)
  1673. (defvaralias 'org-agenda-multi-occur-extra-files
  1674. 'org-agenda-text-search-extra-files))
  1675. (defcustom org-agenda-skip-unavailable-files nil
  1676. "t means to just skip non-reachable files in `org-agenda-files'.
  1677. Nil means to remove them, after a query, from the list."
  1678. :group 'org-agenda
  1679. :type 'boolean)
  1680. (defcustom org-calendar-to-agenda-key [?c]
  1681. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1682. The command `org-calendar-goto-agenda' will be bound to this key. The
  1683. default is the character `c' because then `c' can be used to switch back and
  1684. forth between agenda and calendar."
  1685. :group 'org-agenda
  1686. :type 'sexp)
  1687. (eval-after-load "calendar"
  1688. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1689. 'org-calendar-goto-agenda))
  1690. (defgroup org-latex nil
  1691. "Options for embedding LaTeX code into Org-mode."
  1692. :tag "Org LaTeX"
  1693. :group 'org)
  1694. (defcustom org-format-latex-options
  1695. '(:foreground default :background default :scale 1.0
  1696. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1697. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1698. "Options for creating images from LaTeX fragments.
  1699. This is a property list with the following properties:
  1700. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1701. `default' means use the forground of the default face.
  1702. :background the background color, or \"Transparent\".
  1703. `default' means use the background of the default face.
  1704. :scale a scaling factor for the size of the images
  1705. :html-foreground, :html-background, :html-scale
  1706. The same numbers for HTML export.
  1707. :matchers a list indicating which matchers should be used to
  1708. find LaTeX fragments. Valid members of this list are:
  1709. \"begin\" find environments
  1710. \"$\" find math expressions surrounded by $...$
  1711. \"$$\" find math expressions surrounded by $$....$$
  1712. \"\\(\" find math expressions surrounded by \\(...\\)
  1713. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1714. :group 'org-latex
  1715. :type 'plist)
  1716. (defcustom org-format-latex-header "\\documentclass{article}
  1717. \\usepackage{fullpage} % do not remove
  1718. \\usepackage{amssymb}
  1719. \\usepackage[usenames]{color}
  1720. \\usepackage{amsmath}
  1721. \\usepackage{latexsym}
  1722. \\usepackage[mathscr]{eucal}
  1723. \\pagestyle{empty} % do not remove"
  1724. "The document header used for processing LaTeX fragments."
  1725. :group 'org-latex
  1726. :type 'string)
  1727. (defgroup org-font-lock nil
  1728. "Font-lock settings for highlighting in Org-mode."
  1729. :tag "Org Font Lock"
  1730. :group 'org)
  1731. (defcustom org-level-color-stars-only nil
  1732. "Non-nil means fontify only the stars in each headline.
  1733. When nil, the entire headline is fontified.
  1734. Changing it requires restart of `font-lock-mode' to become effective
  1735. also in regions already fontified."
  1736. :group 'org-font-lock
  1737. :type 'boolean)
  1738. (defcustom org-hide-leading-stars nil
  1739. "Non-nil means, hide the first N-1 stars in a headline.
  1740. This works by using the face `org-hide' for these stars. This
  1741. face is white for a light background, and black for a dark
  1742. background. You may have to customize the face `org-hide' to
  1743. make this work.
  1744. Changing it requires restart of `font-lock-mode' to become effective
  1745. also in regions already fontified.
  1746. You may also set this on a per-file basis by adding one of the following
  1747. lines to the buffer:
  1748. #+STARTUP: hidestars
  1749. #+STARTUP: showstars"
  1750. :group 'org-font-lock
  1751. :type 'boolean)
  1752. (defcustom org-fontify-done-headline nil
  1753. "Non-nil means, change the face of a headline if it is marked DONE.
  1754. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1755. When this is non-nil, the headline after the keyword is set to the
  1756. `org-headline-done' as an additional indication."
  1757. :group 'org-font-lock
  1758. :type 'boolean)
  1759. (defcustom org-fontify-emphasized-text t
  1760. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1761. Changing this variable requires a restart of Emacs to take effect."
  1762. :group 'org-font-lock
  1763. :type 'boolean)
  1764. (defcustom org-highlight-latex-fragments-and-specials nil
  1765. "Non-nil means, fontify what is treated specially by the exporters."
  1766. :group 'org-font-lock
  1767. :type 'boolean)
  1768. (defcustom org-hide-emphasis-markers nil
  1769. "Non-nil mean font-lock should hide the emphasis marker characters."
  1770. :group 'org-font-lock
  1771. :type 'boolean)
  1772. (defvar org-emph-re nil
  1773. "Regular expression for matching emphasis.")
  1774. (defvar org-verbatim-re nil
  1775. "Regular expression for matching verbatim text.")
  1776. (defvar org-emphasis-regexp-components) ; defined just below
  1777. (defvar org-emphasis-alist) ; defined just below
  1778. (defun org-set-emph-re (var val)
  1779. "Set variable and compute the emphasis regular expression."
  1780. (set var val)
  1781. (when (and (boundp 'org-emphasis-alist)
  1782. (boundp 'org-emphasis-regexp-components)
  1783. org-emphasis-alist org-emphasis-regexp-components)
  1784. (let* ((e org-emphasis-regexp-components)
  1785. (pre (car e))
  1786. (post (nth 1 e))
  1787. (border (nth 2 e))
  1788. (body (nth 3 e))
  1789. (nl (nth 4 e))
  1790. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1791. (body1 (concat body "*?"))
  1792. (markers (mapconcat 'car org-emphasis-alist ""))
  1793. (vmarkers (mapconcat
  1794. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1795. org-emphasis-alist "")))
  1796. ;; make sure special characters appear at the right position in the class
  1797. (if (string-match "\\^" markers)
  1798. (setq markers (concat (replace-match "" t t markers) "^")))
  1799. (if (string-match "-" markers)
  1800. (setq markers (concat (replace-match "" t t markers) "-")))
  1801. (if (string-match "\\^" vmarkers)
  1802. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1803. (if (string-match "-" vmarkers)
  1804. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1805. (if (> nl 0)
  1806. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1807. (int-to-string nl) "\\}")))
  1808. ;; Make the regexp
  1809. (setq org-emph-re
  1810. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1811. "\\("
  1812. "\\([" markers "]\\)"
  1813. "\\("
  1814. "[^" border "]\\|"
  1815. "[^" border (if (and nil stacked) markers) "]"
  1816. body1
  1817. "[^" border (if (and nil stacked) markers) "]"
  1818. "\\)"
  1819. "\\3\\)"
  1820. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1821. (setq org-verbatim-re
  1822. (concat "\\([" pre "]\\|^\\)"
  1823. "\\("
  1824. "\\([" vmarkers "]\\)"
  1825. "\\("
  1826. "[^" border "]\\|"
  1827. "[^" border "]"
  1828. body1
  1829. "[^" border "]"
  1830. "\\)"
  1831. "\\3\\)"
  1832. "\\([" post "]\\|$\\)")))))
  1833. (defcustom org-emphasis-regexp-components
  1834. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1835. "Components used to build the regular expression for emphasis.
  1836. This is a list with 6 entries. Terminology: In an emphasis string
  1837. like \" *strong word* \", we call the initial space PREMATCH, the final
  1838. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1839. and \"trong wor\" is the body. The different components in this variable
  1840. specify what is allowed/forbidden in each part:
  1841. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1842. post Chars allowed as postmatch. End of line will be allowed too.
  1843. border The chars *forbidden* as border characters.
  1844. body-regexp A regexp like \".\" to match a body character. Don't use
  1845. non-shy groups here, and don't allow newline here.
  1846. newline The maximum number of newlines allowed in an emphasis exp.
  1847. Use customize to modify this, or restart Emacs after changing it."
  1848. :group 'org-font-lock
  1849. :set 'org-set-emph-re
  1850. :type '(list
  1851. (sexp :tag "Allowed chars in pre ")
  1852. (sexp :tag "Allowed chars in post ")
  1853. (sexp :tag "Forbidden chars in border ")
  1854. (sexp :tag "Regexp for body ")
  1855. (integer :tag "number of newlines allowed")
  1856. (option (boolean :tag "Stacking (DISABLED) "))))
  1857. (defcustom org-emphasis-alist
  1858. '(("*" bold "<b>" "</b>")
  1859. ("/" italic "<i>" "</i>")
  1860. ("_" underline "<u>" "</u>")
  1861. ("=" org-code "<code>" "</code>" verbatim)
  1862. ("~" org-verbatim "" "" verbatim)
  1863. ("+" (:strike-through t) "<del>" "</del>")
  1864. )
  1865. "Special syntax for emphasized text.
  1866. Text starting and ending with a special character will be emphasized, for
  1867. example *bold*, _underlined_ and /italic/. This variable sets the marker
  1868. characters, the face to be used by font-lock for highlighting in Org-mode
  1869. Emacs buffers, and the HTML tags to be used for this.
  1870. Use customize to modify this, or restart Emacs after changing it."
  1871. :group 'org-font-lock
  1872. :set 'org-set-emph-re
  1873. :type '(repeat
  1874. (list
  1875. (string :tag "Marker character")
  1876. (choice
  1877. (face :tag "Font-lock-face")
  1878. (plist :tag "Face property list"))
  1879. (string :tag "HTML start tag")
  1880. (string :tag "HTML end tag")
  1881. (option (const verbatim)))))
  1882. ;;; Miscellaneous options
  1883. (defgroup org-completion nil
  1884. "Completion in Org-mode."
  1885. :tag "Org Completion"
  1886. :group 'org)
  1887. (defcustom org-completion-fallback-command 'hippie-expand
  1888. "The expansion command called by \\[org-complete] in normal context.
  1889. Normal means, no org-mode-specific context."
  1890. :group 'org-completion
  1891. :type 'function)
  1892. ;;; Functions and variables from ther packages
  1893. ;; Declared here to avoid compiler warnings
  1894. ;; XEmacs only
  1895. (defvar outline-mode-menu-heading)
  1896. (defvar outline-mode-menu-show)
  1897. (defvar outline-mode-menu-hide)
  1898. (defvar zmacs-regions) ; XEmacs regions
  1899. ;; Emacs only
  1900. (defvar mark-active)
  1901. ;; Various packages
  1902. (declare-function calendar-absolute-from-iso "cal-iso" (&optional date))
  1903. (declare-function calendar-forward-day "cal-move" (arg))
  1904. (declare-function calendar-goto-date "cal-move" (date))
  1905. (declare-function calendar-goto-today "cal-move" ())
  1906. (declare-function calendar-iso-from-absolute "cal-iso" (&optional date))
  1907. (defvar calc-embedded-close-formula)
  1908. (defvar calc-embedded-open-formula)
  1909. (declare-function cdlatex-tab "ext:cdlatex" ())
  1910. (declare-function dired-get-filename "dired" (&optional localp no-error-if-not-filep))
  1911. (defvar font-lock-unfontify-region-function)
  1912. (declare-function iswitchb-mode "iswitchb" (&optional arg))
  1913. (declare-function iswitchb-read-buffer (prompt &optional default require-match start matches-set))
  1914. (defvar iswitchb-temp-buflist)
  1915. (declare-function org-gnus-follow-link "org-gnus" (&optional group article))
  1916. (declare-function org-agenda-skip "org-agenda" ())
  1917. (declare-function org-format-agenda-item "org-agenda"
  1918. (extra txt &optional category tags dotime noprefix remove-re))
  1919. (declare-function org-agenda-new-marker "org-agenda" (&optional pos))
  1920. (declare-function org-agenda-change-all-lines "org-agenda"
  1921. (newhead hdmarker &optional fixface))
  1922. (declare-function org-agenda-set-restriction-lock "org-agenda" (&optional type))
  1923. (declare-function org-agenda-maybe-redo "org-agenda" ())
  1924. (declare-function parse-time-string "parse-time" (string))
  1925. (declare-function remember "remember" (&optional initial))
  1926. (declare-function remember-buffer-desc "remember" ())
  1927. (declare-function remember-finalize "remember" ())
  1928. (defvar remember-save-after-remembering)
  1929. (defvar remember-data-file)
  1930. (defvar remember-register)
  1931. (defvar remember-buffer)
  1932. (defvar remember-handler-functions)
  1933. (defvar remember-annotation-functions)
  1934. (defvar texmathp-why)
  1935. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1936. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1937. (defvar w3m-current-url)
  1938. (defvar w3m-current-title)
  1939. (defvar org-latex-regexps)
  1940. ;;; Autoload and prepare some org modules
  1941. ;; Some table stuff that needs to be defined here, because it is used
  1942. ;; by the functions setting up org-mode or checking for table context.
  1943. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1944. "Detects an org-type or table-type table.")
  1945. (defconst org-table-line-regexp "^[ \t]*|"
  1946. "Detects an org-type table line.")
  1947. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1948. "Detects an org-type table line.")
  1949. (defconst org-table-hline-regexp "^[ \t]*|-"
  1950. "Detects an org-type table hline.")
  1951. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1952. "Detects a table-type table hline.")
  1953. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1954. "Searching from within a table (any type) this finds the first line
  1955. outside the table.")
  1956. ;; Autoload the functions in org-table.el that are needed by functions here.
  1957. (eval-and-compile
  1958. (org-autoload "org-table"
  1959. '(org-table-align org-table-begin org-table-blank-field
  1960. org-table-convert org-table-convert-region org-table-copy-down
  1961. org-table-copy-region org-table-create
  1962. org-table-create-or-convert-from-region
  1963. org-table-create-with-table.el org-table-current-dline
  1964. org-table-cut-region org-table-delete-column org-table-edit-field
  1965. org-table-edit-formulas org-table-end org-table-eval-formula
  1966. org-table-export org-table-field-info
  1967. org-table-get-stored-formulas org-table-goto-column
  1968. org-table-hline-and-move org-table-import org-table-insert-column
  1969. org-table-insert-hline org-table-insert-row org-table-iterate
  1970. org-table-justify-field-maybe org-table-kill-row
  1971. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1972. org-table-move-column org-table-move-column-left
  1973. org-table-move-column-right org-table-move-row
  1974. org-table-move-row-down org-table-move-row-up
  1975. org-table-next-field org-table-next-row org-table-paste-rectangle
  1976. org-table-previous-field org-table-recalculate
  1977. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1978. org-table-toggle-coordinate-overlays
  1979. org-table-toggle-formula-debugger org-table-wrap-region
  1980. orgtbl-mode turn-on-orgtbl)))
  1981. (defun org-at-table-p (&optional table-type)
  1982. "Return t if the cursor is inside an org-type table.
  1983. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  1984. (if org-enable-table-editor
  1985. (save-excursion
  1986. (beginning-of-line 1)
  1987. (looking-at (if table-type org-table-any-line-regexp
  1988. org-table-line-regexp)))
  1989. nil))
  1990. (defsubst org-table-p () (org-at-table-p))
  1991. (defun org-at-table.el-p ()
  1992. "Return t if and only if we are at a table.el table."
  1993. (and (org-at-table-p 'any)
  1994. (save-excursion
  1995. (goto-char (org-table-begin 'any))
  1996. (looking-at org-table1-hline-regexp))))
  1997. (defun org-table-recognize-table.el ()
  1998. "If there is a table.el table nearby, recognize it and move into it."
  1999. (if org-table-tab-recognizes-table.el
  2000. (if (org-at-table.el-p)
  2001. (progn
  2002. (beginning-of-line 1)
  2003. (if (looking-at org-table-dataline-regexp)
  2004. nil
  2005. (if (looking-at org-table1-hline-regexp)
  2006. (progn
  2007. (beginning-of-line 2)
  2008. (if (looking-at org-table-any-border-regexp)
  2009. (beginning-of-line -1)))))
  2010. (if (re-search-forward "|" (org-table-end t) t)
  2011. (progn
  2012. (require 'table)
  2013. (if (table--at-cell-p (point))
  2014. t
  2015. (message "recognizing table.el table...")
  2016. (table-recognize-table)
  2017. (message "recognizing table.el table...done")))
  2018. (error "This should not happen..."))
  2019. t)
  2020. nil)
  2021. nil))
  2022. (defun org-at-table-hline-p ()
  2023. "Return t if the cursor is inside a hline in a table."
  2024. (if org-enable-table-editor
  2025. (save-excursion
  2026. (beginning-of-line 1)
  2027. (looking-at org-table-hline-regexp))
  2028. nil))
  2029. (defvar org-table-clean-did-remove-column nil)
  2030. (defun org-table-map-tables (function)
  2031. "Apply FUNCTION to the start of all tables in the buffer."
  2032. (save-excursion
  2033. (save-restriction
  2034. (widen)
  2035. (goto-char (point-min))
  2036. (while (re-search-forward org-table-any-line-regexp nil t)
  2037. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2038. (beginning-of-line 1)
  2039. (if (looking-at org-table-line-regexp)
  2040. (save-excursion (funcall function)))
  2041. (re-search-forward org-table-any-border-regexp nil 1))))
  2042. (message "Mapping tables: done"))
  2043. ;; Declare and autoload functions from org-exp.el
  2044. (declare-function org-default-export-plist "org-exp")
  2045. (declare-function org-infile-export-plist "org-exp")
  2046. (declare-function org-get-current-options "org-exp")
  2047. (eval-and-compile
  2048. (org-autoload "org-exp"
  2049. '(org-export org-export-as-ascii org-export-visible
  2050. org-insert-export-options-template org-export-as-html-and-open
  2051. org-export-as-html-batch org-export-as-html-to-buffer
  2052. org-replace-region-by-html org-export-region-as-html
  2053. org-export-as-html org-export-icalendar-this-file
  2054. org-export-icalendar-all-agenda-files
  2055. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2056. ;; Declare and autoload functions from org-exp.el
  2057. (eval-and-compile
  2058. (org-autoload "org-exp"
  2059. '(org-agenda org-agenda-list org-search-view
  2060. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2061. org-diary org-agenda-to-appt)))
  2062. ;; Autoload org-remember
  2063. (eval-and-compile
  2064. (org-autoload "org-remember"
  2065. '(org-remember-insinuate org-remember-annotation
  2066. org-remember-apply-template org-remember org-remember-handler)))
  2067. ;; Autoload org-clock.el
  2068. (defvar org-clock-marker (make-marker)
  2069. "Marker recording the last clock-in.")
  2070. (eval-and-compile
  2071. (org-autoload
  2072. "org-clock"
  2073. '(org-clock-in org-clock-out org-clock-cancel
  2074. org-clock-goto org-clock-sum org-clock-display
  2075. org-remove-clock-overlays org-clock-report
  2076. org-clocktable-shift org-dblock-write:clocktable
  2077. org-get-clocktable)))
  2078. (defun org-clock-update-time-maybe ()
  2079. "If this is a CLOCK line, update it and return t.
  2080. Otherwise, return nil."
  2081. (interactive)
  2082. (save-excursion
  2083. (beginning-of-line 1)
  2084. (skip-chars-forward " \t")
  2085. (when (looking-at org-clock-string)
  2086. (let ((re (concat "[ \t]*" org-clock-string
  2087. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2088. "\\([ \t]*=>.*\\)?"))
  2089. ts te h m s)
  2090. (if (not (looking-at re))
  2091. nil
  2092. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2093. (end-of-line 1)
  2094. (setq ts (match-string 1)
  2095. te (match-string 2))
  2096. (setq s (- (time-to-seconds
  2097. (apply 'encode-time (org-parse-time-string te)))
  2098. (time-to-seconds
  2099. (apply 'encode-time (org-parse-time-string ts))))
  2100. h (floor (/ s 3600))
  2101. s (- s (* 3600 h))
  2102. m (floor (/ s 60))
  2103. s (- s (* 60 s)))
  2104. (insert " => " (format "%2d:%02d" h m))
  2105. t)))))
  2106. (defun org-check-running-clock ()
  2107. "Check if the current buffer contains the running clock.
  2108. If yes, offer to stop it and to save the buffer with the changes."
  2109. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2110. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2111. (buffer-name))))
  2112. (org-clock-out)
  2113. (when (y-or-n-p "Save changed buffer?")
  2114. (save-buffer))))
  2115. (defun org-clocktable-try-shift (dir n)
  2116. "Check if this line starts a clock table, if yes, shift the time block."
  2117. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2118. (org-clocktable-shift dir n)))
  2119. ;; Autoload archiving code
  2120. ;; The stuff that is needed for cycling and tags has to be defined here.
  2121. (defgroup org-archive nil
  2122. "Options concerning archiving in Org-mode."
  2123. :tag "Org Archive"
  2124. :group 'org-structure)
  2125. (defcustom org-archive-location "%s_archive::"
  2126. "The location where subtrees should be archived.
  2127. Otherwise, the value of this variable is a string, consisting of two
  2128. parts, separated by a double-colon.
  2129. The first part is a file name - when omitted, archiving happens in the same
  2130. file. %s will be replaced by the current file name (without directory part).
  2131. Archiving to a different file is useful to keep archived entries from
  2132. contributing to the Org-mode Agenda.
  2133. The part after the double colon is a headline. The archived entries will be
  2134. filed under that headline. When omitted, the subtrees are simply filed away
  2135. at the end of the file, as top-level entries.
  2136. Here are a few examples:
  2137. \"%s_archive::\"
  2138. If the current file is Projects.org, archive in file
  2139. Projects.org_archive, as top-level trees. This is the default.
  2140. \"::* Archived Tasks\"
  2141. Archive in the current file, under the top-level headline
  2142. \"* Archived Tasks\".
  2143. \"~/org/archive.org::\"
  2144. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2145. \"basement::** Finished Tasks\"
  2146. Archive in file ./basement (relative path), as level 3 trees
  2147. below the level 2 heading \"** Finished Tasks\".
  2148. You may set this option on a per-file basis by adding to the buffer a
  2149. line like
  2150. #+ARCHIVE: basement::** Finished Tasks
  2151. You may also define it locally for a subtree by setting an ARCHIVE property
  2152. in the entry. If such a property is found in an entry, or anywhere up
  2153. the hierarchy, it will be used."
  2154. :group 'org-archive
  2155. :type 'string)
  2156. (defcustom org-archive-tag "ARCHIVE"
  2157. "The tag that marks a subtree as archived.
  2158. An archived subtree does not open during visibility cycling, and does
  2159. not contribute to the agenda listings.
  2160. After changing this, font-lock must be restarted in the relevant buffers to
  2161. get the proper fontification."
  2162. :group 'org-archive
  2163. :group 'org-keywords
  2164. :type 'string)
  2165. (defcustom org-agenda-skip-archived-trees t
  2166. "Non-nil means, the agenda will skip any items located in archived trees.
  2167. An archived tree is a tree marked with the tag ARCHIVE."
  2168. :group 'org-archive
  2169. :group 'org-agenda-skip
  2170. :type 'boolean)
  2171. (defcustom org-cycle-open-archived-trees nil
  2172. "Non-nil means, `org-cycle' will open archived trees.
  2173. An archived tree is a tree marked with the tag ARCHIVE.
  2174. When nil, archived trees will stay folded. You can still open them with
  2175. normal outline commands like `show-all', but not with the cycling commands."
  2176. :group 'org-archive
  2177. :group 'org-cycle
  2178. :type 'boolean)
  2179. (defcustom org-sparse-tree-open-archived-trees nil
  2180. "Non-nil means sparse tree construction shows matches in archived trees.
  2181. When nil, matches in these trees are highlighted, but the trees are kept in
  2182. collapsed state."
  2183. :group 'org-archive
  2184. :group 'org-sparse-trees
  2185. :type 'boolean)
  2186. (defun org-cycle-hide-archived-subtrees (state)
  2187. "Re-hide all archived subtrees after a visibility state change."
  2188. (when (and (not org-cycle-open-archived-trees)
  2189. (not (memq state '(overview folded))))
  2190. (save-excursion
  2191. (let* ((globalp (memq state '(contents all)))
  2192. (beg (if globalp (point-min) (point)))
  2193. (end (if globalp (point-max) (org-end-of-subtree t))))
  2194. (org-hide-archived-subtrees beg end)
  2195. (goto-char beg)
  2196. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2197. (message "%s" (substitute-command-keys
  2198. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2199. (defun org-force-cycle-archived ()
  2200. "Cycle subtree even if it is archived."
  2201. (interactive)
  2202. (setq this-command 'org-cycle)
  2203. (let ((org-cycle-open-archived-trees t))
  2204. (call-interactively 'org-cycle)))
  2205. (defun org-hide-archived-subtrees (beg end)
  2206. "Re-hide all archived subtrees after a visibility state change."
  2207. (save-excursion
  2208. (let* ((re (concat ":" org-archive-tag ":")))
  2209. (goto-char beg)
  2210. (while (re-search-forward re end t)
  2211. (and (org-on-heading-p) (hide-subtree))
  2212. (org-end-of-subtree t)))))
  2213. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2214. (eval-and-compile
  2215. (org-autoload "org-archive"
  2216. '(org-add-archive-files org-archive-subtree
  2217. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2218. ;; Autoload Column View Code
  2219. (declare-function org-columns-number-to-string "org-colview")
  2220. (declare-function org-columns-get-format-and-top-level "org-colview")
  2221. (declare-function org-columns-compute "org-colview")
  2222. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2223. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2224. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2225. org-columns org-insert-columns-dblock))
  2226. ;;; Variables for pre-computed regular expressions, all buffer local
  2227. (defvar org-drawer-regexp nil
  2228. "Matches first line of a hidden block.")
  2229. (make-variable-buffer-local 'org-drawer-regexp)
  2230. (defvar org-todo-regexp nil
  2231. "Matches any of the TODO state keywords.")
  2232. (make-variable-buffer-local 'org-todo-regexp)
  2233. (defvar org-not-done-regexp nil
  2234. "Matches any of the TODO state keywords except the last one.")
  2235. (make-variable-buffer-local 'org-not-done-regexp)
  2236. (defvar org-todo-line-regexp nil
  2237. "Matches a headline and puts TODO state into group 2 if present.")
  2238. (make-variable-buffer-local 'org-todo-line-regexp)
  2239. (defvar org-complex-heading-regexp nil
  2240. "Matches a headline and puts everything into groups:
  2241. group 1: the stars
  2242. group 2: The todo keyword, maybe
  2243. group 3: Priority cookie
  2244. group 4: True headline
  2245. group 5: Tags")
  2246. (make-variable-buffer-local 'org-complex-heading-regexp)
  2247. (defvar org-todo-line-tags-regexp nil
  2248. "Matches a headline and puts TODO state into group 2 if present.
  2249. Also put tags into group 4 if tags are present.")
  2250. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2251. (defvar org-nl-done-regexp nil
  2252. "Matches newline followed by a headline with the DONE keyword.")
  2253. (make-variable-buffer-local 'org-nl-done-regexp)
  2254. (defvar org-looking-at-done-regexp nil
  2255. "Matches the DONE keyword a point.")
  2256. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2257. (defvar org-ds-keyword-length 12
  2258. "Maximum length of the Deadline and SCHEDULED keywords.")
  2259. (make-variable-buffer-local 'org-ds-keyword-length)
  2260. (defvar org-deadline-regexp nil
  2261. "Matches the DEADLINE keyword.")
  2262. (make-variable-buffer-local 'org-deadline-regexp)
  2263. (defvar org-deadline-time-regexp nil
  2264. "Matches the DEADLINE keyword together with a time stamp.")
  2265. (make-variable-buffer-local 'org-deadline-time-regexp)
  2266. (defvar org-deadline-line-regexp nil
  2267. "Matches the DEADLINE keyword and the rest of the line.")
  2268. (make-variable-buffer-local 'org-deadline-line-regexp)
  2269. (defvar org-scheduled-regexp nil
  2270. "Matches the SCHEDULED keyword.")
  2271. (make-variable-buffer-local 'org-scheduled-regexp)
  2272. (defvar org-scheduled-time-regexp nil
  2273. "Matches the SCHEDULED keyword together with a time stamp.")
  2274. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2275. (defvar org-closed-time-regexp nil
  2276. "Matches the CLOSED keyword together with a time stamp.")
  2277. (make-variable-buffer-local 'org-closed-time-regexp)
  2278. (defvar org-keyword-time-regexp nil
  2279. "Matches any of the 4 keywords, together with the time stamp.")
  2280. (make-variable-buffer-local 'org-keyword-time-regexp)
  2281. (defvar org-keyword-time-not-clock-regexp nil
  2282. "Matches any of the 3 keywords, together with the time stamp.")
  2283. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2284. (defvar org-maybe-keyword-time-regexp nil
  2285. "Matches a timestamp, possibly preceeded by a keyword.")
  2286. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2287. (defvar org-planning-or-clock-line-re nil
  2288. "Matches a line with planning or clock info.")
  2289. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2290. (defconst org-plain-time-of-day-regexp
  2291. (concat
  2292. "\\(\\<[012]?[0-9]"
  2293. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2294. "\\(--?"
  2295. "\\(\\<[012]?[0-9]"
  2296. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2297. "\\)?")
  2298. "Regular expression to match a plain time or time range.
  2299. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2300. groups carry important information:
  2301. 0 the full match
  2302. 1 the first time, range or not
  2303. 8 the second time, if it is a range.")
  2304. (defconst org-plain-time-extension-regexp
  2305. (concat
  2306. "\\(\\<[012]?[0-9]"
  2307. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2308. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2309. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2310. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2311. groups carry important information:
  2312. 0 the full match
  2313. 7 hours of duration
  2314. 9 minutes of duration")
  2315. (defconst org-stamp-time-of-day-regexp
  2316. (concat
  2317. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2318. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2319. "\\(--?"
  2320. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2321. "Regular expression to match a timestamp time or time range.
  2322. After a match, the following groups carry important information:
  2323. 0 the full match
  2324. 1 date plus weekday, for backreferencing to make sure both times on same day
  2325. 2 the first time, range or not
  2326. 4 the second time, if it is a range.")
  2327. (defconst org-startup-options
  2328. '(("fold" org-startup-folded t)
  2329. ("overview" org-startup-folded t)
  2330. ("nofold" org-startup-folded nil)
  2331. ("showall" org-startup-folded nil)
  2332. ("content" org-startup-folded content)
  2333. ("hidestars" org-hide-leading-stars t)
  2334. ("showstars" org-hide-leading-stars nil)
  2335. ("odd" org-odd-levels-only t)
  2336. ("oddeven" org-odd-levels-only nil)
  2337. ("align" org-startup-align-all-tables t)
  2338. ("noalign" org-startup-align-all-tables nil)
  2339. ("customtime" org-display-custom-times t)
  2340. ("logdone" org-log-done time)
  2341. ("lognotedone" org-log-done note)
  2342. ("nologdone" org-log-done nil)
  2343. ("lognoteclock-out" org-log-note-clock-out t)
  2344. ("nolognoteclock-out" org-log-note-clock-out nil)
  2345. ("logrepeat" org-log-repeat state)
  2346. ("lognoterepeat" org-log-repeat note)
  2347. ("nologrepeat" org-log-repeat nil)
  2348. ("constcgs" constants-unit-system cgs)
  2349. ("constSI" constants-unit-system SI))
  2350. "Variable associated with STARTUP options for org-mode.
  2351. Each element is a list of three items: The startup options as written
  2352. in the #+STARTUP line, the corresponding variable, and the value to
  2353. set this variable to if the option is found. An optional forth element PUSH
  2354. means to push this value onto the list in the variable.")
  2355. (defun org-set-regexps-and-options ()
  2356. "Precompute regular expressions for current buffer."
  2357. (when (org-mode-p)
  2358. (org-set-local 'org-todo-kwd-alist nil)
  2359. (org-set-local 'org-todo-key-alist nil)
  2360. (org-set-local 'org-todo-key-trigger nil)
  2361. (org-set-local 'org-todo-keywords-1 nil)
  2362. (org-set-local 'org-done-keywords nil)
  2363. (org-set-local 'org-todo-heads nil)
  2364. (org-set-local 'org-todo-sets nil)
  2365. (org-set-local 'org-todo-log-states nil)
  2366. (let ((re (org-make-options-regexp
  2367. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2368. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2369. "CONSTANTS" "PROPERTY" "DRAWERS")))
  2370. (splitre "[ \t]+")
  2371. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2372. tail sep kws1 prio props drawers)
  2373. (save-excursion
  2374. (save-restriction
  2375. (widen)
  2376. (goto-char (point-min))
  2377. (while (re-search-forward re nil t)
  2378. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2379. (cond
  2380. ((equal key "CATEGORY")
  2381. (if (string-match "[ \t]+$" value)
  2382. (setq value (replace-match "" t t value)))
  2383. (setq cat value))
  2384. ((member key '("SEQ_TODO" "TODO"))
  2385. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2386. ((equal key "TYP_TODO")
  2387. (push (cons 'type (org-split-string value splitre)) kwds))
  2388. ((equal key "TAGS")
  2389. (setq tags (append tags (org-split-string value splitre))))
  2390. ((equal key "COLUMNS")
  2391. (org-set-local 'org-columns-default-format value))
  2392. ((equal key "LINK")
  2393. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2394. (push (cons (match-string 1 value)
  2395. (org-trim (match-string 2 value)))
  2396. links)))
  2397. ((equal key "PRIORITIES")
  2398. (setq prio (org-split-string value " +")))
  2399. ((equal key "PROPERTY")
  2400. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2401. (push (cons (match-string 1 value) (match-string 2 value))
  2402. props)))
  2403. ((equal key "DRAWERS")
  2404. (setq drawers (org-split-string value splitre)))
  2405. ((equal key "CONSTANTS")
  2406. (setq const (append const (org-split-string value splitre))))
  2407. ((equal key "STARTUP")
  2408. (let ((opts (org-split-string value splitre))
  2409. l var val)
  2410. (while (setq l (pop opts))
  2411. (when (setq l (assoc l org-startup-options))
  2412. (setq var (nth 1 l) val (nth 2 l))
  2413. (if (not (nth 3 l))
  2414. (set (make-local-variable var) val)
  2415. (if (not (listp (symbol-value var)))
  2416. (set (make-local-variable var) nil))
  2417. (set (make-local-variable var) (symbol-value var))
  2418. (add-to-list var val))))))
  2419. ((equal key "ARCHIVE")
  2420. (string-match " *$" value)
  2421. (setq arch (replace-match "" t t value))
  2422. (remove-text-properties 0 (length arch)
  2423. '(face t fontified t) arch)))
  2424. )))
  2425. (when cat
  2426. (org-set-local 'org-category (intern cat))
  2427. (push (cons "CATEGORY" cat) props))
  2428. (when prio
  2429. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2430. (setq prio (mapcar 'string-to-char prio))
  2431. (org-set-local 'org-highest-priority (nth 0 prio))
  2432. (org-set-local 'org-lowest-priority (nth 1 prio))
  2433. (org-set-local 'org-default-priority (nth 2 prio)))
  2434. (and props (org-set-local 'org-local-properties (nreverse props)))
  2435. (and drawers (org-set-local 'org-drawers drawers))
  2436. (and arch (org-set-local 'org-archive-location arch))
  2437. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2438. ;; Process the TODO keywords
  2439. (unless kwds
  2440. ;; Use the global values as if they had been given locally.
  2441. (setq kwds (default-value 'org-todo-keywords))
  2442. (if (stringp (car kwds))
  2443. (setq kwds (list (cons org-todo-interpretation
  2444. (default-value 'org-todo-keywords)))))
  2445. (setq kwds (reverse kwds)))
  2446. (setq kwds (nreverse kwds))
  2447. (let (inter kws kw)
  2448. (while (setq kws (pop kwds))
  2449. (setq inter (pop kws) sep (member "|" kws)
  2450. kws0 (delete "|" (copy-sequence kws))
  2451. kwsa nil
  2452. kws1 (mapcar
  2453. (lambda (x)
  2454. ;; 1 2
  2455. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2456. (progn
  2457. (setq kw (match-string 1 x)
  2458. key (and (match-end 2) (match-string 2 x))
  2459. log (org-extract-log-state-settings x))
  2460. (push (cons kw (and key (string-to-char key))) kwsa)
  2461. (and log (push log org-todo-log-states))
  2462. kw)
  2463. (error "Invalid TODO keyword %s" x)))
  2464. kws0)
  2465. kwsa (if kwsa (append '((:startgroup))
  2466. (nreverse kwsa)
  2467. '((:endgroup))))
  2468. hw (car kws1)
  2469. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2470. tail (list inter hw (car dws) (org-last dws)))
  2471. (add-to-list 'org-todo-heads hw 'append)
  2472. (push kws1 org-todo-sets)
  2473. (setq org-done-keywords (append org-done-keywords dws nil))
  2474. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2475. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2476. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2477. (setq org-todo-sets (nreverse org-todo-sets)
  2478. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2479. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2480. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2481. ;; Process the constants
  2482. (when const
  2483. (let (e cst)
  2484. (while (setq e (pop const))
  2485. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2486. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2487. (setq org-table-formula-constants-local cst)))
  2488. ;; Process the tags.
  2489. (when tags
  2490. (let (e tgs)
  2491. (while (setq e (pop tags))
  2492. (cond
  2493. ((equal e "{") (push '(:startgroup) tgs))
  2494. ((equal e "}") (push '(:endgroup) tgs))
  2495. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2496. (push (cons (match-string 1 e)
  2497. (string-to-char (match-string 2 e)))
  2498. tgs))
  2499. (t (push (list e) tgs))))
  2500. (org-set-local 'org-tag-alist nil)
  2501. (while (setq e (pop tgs))
  2502. (or (and (stringp (car e))
  2503. (assoc (car e) org-tag-alist))
  2504. (push e org-tag-alist))))))
  2505. ;; Compute the regular expressions and other local variables
  2506. (if (not org-done-keywords)
  2507. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2508. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2509. (length org-scheduled-string)
  2510. (length org-clock-string)
  2511. (length org-closed-string)))
  2512. org-drawer-regexp
  2513. (concat "^[ \t]*:\\("
  2514. (mapconcat 'regexp-quote org-drawers "\\|")
  2515. "\\):[ \t]*$")
  2516. org-not-done-keywords
  2517. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2518. org-todo-regexp
  2519. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2520. "\\|") "\\)\\>")
  2521. org-not-done-regexp
  2522. (concat "\\<\\("
  2523. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2524. "\\)\\>")
  2525. org-todo-line-regexp
  2526. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2527. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2528. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2529. org-complex-heading-regexp
  2530. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2531. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2532. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2533. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2534. org-nl-done-regexp
  2535. (concat "\n\\*+[ \t]+"
  2536. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2537. "\\)" "\\>")
  2538. org-todo-line-tags-regexp
  2539. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2540. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2541. (org-re
  2542. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2543. org-looking-at-done-regexp
  2544. (concat "^" "\\(?:"
  2545. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2546. "\\>")
  2547. org-deadline-regexp (concat "\\<" org-deadline-string)
  2548. org-deadline-time-regexp
  2549. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2550. org-deadline-line-regexp
  2551. (concat "\\<\\(" org-deadline-string "\\).*")
  2552. org-scheduled-regexp
  2553. (concat "\\<" org-scheduled-string)
  2554. org-scheduled-time-regexp
  2555. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2556. org-closed-time-regexp
  2557. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2558. org-keyword-time-regexp
  2559. (concat "\\<\\(" org-scheduled-string
  2560. "\\|" org-deadline-string
  2561. "\\|" org-closed-string
  2562. "\\|" org-clock-string "\\)"
  2563. " *[[<]\\([^]>]+\\)[]>]")
  2564. org-keyword-time-not-clock-regexp
  2565. (concat "\\<\\(" org-scheduled-string
  2566. "\\|" org-deadline-string
  2567. "\\|" org-closed-string
  2568. "\\)"
  2569. " *[[<]\\([^]>]+\\)[]>]")
  2570. org-maybe-keyword-time-regexp
  2571. (concat "\\(\\<\\(" org-scheduled-string
  2572. "\\|" org-deadline-string
  2573. "\\|" org-closed-string
  2574. "\\|" org-clock-string "\\)\\)?"
  2575. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2576. org-planning-or-clock-line-re
  2577. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2578. "\\|" org-deadline-string
  2579. "\\|" org-closed-string "\\|" org-clock-string
  2580. "\\)\\>\\)")
  2581. )
  2582. (org-compute-latex-and-specials-regexp)
  2583. (org-set-font-lock-defaults)))
  2584. (defun org-extract-log-state-settings (x)
  2585. "Extract the log state setting from a TODO keyword string.
  2586. This will extract info from a string like \"WAIT(w@/!)\"."
  2587. (let (kw key log1 log2)
  2588. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2589. (setq kw (match-string 1 x)
  2590. key (and (match-end 2) (match-string 2 x))
  2591. log1 (and (match-end 3) (match-string 3 x))
  2592. log2 (and (match-end 4) (match-string 4 x)))
  2593. (and (or log1 log2)
  2594. (list kw
  2595. (and log1 (if (equal log1 "!") 'time 'note))
  2596. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2597. (defun org-remove-keyword-keys (list)
  2598. "Remove a pair of parenthesis at the end of each string in LIST."
  2599. (mapcar (lambda (x)
  2600. (if (string-match "(.*)$" x)
  2601. (substring x 0 (match-beginning 0))
  2602. x))
  2603. list))
  2604. ;; FIXME: this could be done much better, using second characters etc.
  2605. (defun org-assign-fast-keys (alist)
  2606. "Assign fast keys to a keyword-key alist.
  2607. Respect keys that are already there."
  2608. (let (new e k c c1 c2 (char ?a))
  2609. (while (setq e (pop alist))
  2610. (cond
  2611. ((equal e '(:startgroup)) (push e new))
  2612. ((equal e '(:endgroup)) (push e new))
  2613. (t
  2614. (setq k (car e) c2 nil)
  2615. (if (cdr e)
  2616. (setq c (cdr e))
  2617. ;; automatically assign a character.
  2618. (setq c1 (string-to-char
  2619. (downcase (substring
  2620. k (if (= (string-to-char k) ?@) 1 0)))))
  2621. (if (or (rassoc c1 new) (rassoc c1 alist))
  2622. (while (or (rassoc char new) (rassoc char alist))
  2623. (setq char (1+ char)))
  2624. (setq c2 c1))
  2625. (setq c (or c2 char)))
  2626. (push (cons k c) new))))
  2627. (nreverse new)))
  2628. ;;; Some variables used in various places
  2629. (defvar org-window-configuration nil
  2630. "Used in various places to store a window configuration.")
  2631. (defvar org-finish-function nil
  2632. "Function to be called when `C-c C-c' is used.
  2633. This is for getting out of special buffers like remember.")
  2634. ;; FIXME: Occasionally check by commenting these, to make sure
  2635. ;; no other functions uses these, forgetting to let-bind them.
  2636. (defvar entry)
  2637. (defvar state)
  2638. (defvar last-state)
  2639. (defvar date)
  2640. (defvar description)
  2641. ;; Defined somewhere in this file, but used before definition.
  2642. (defvar org-html-entities)
  2643. (defvar org-struct-menu)
  2644. (defvar org-org-menu)
  2645. (defvar org-tbl-menu)
  2646. (defvar org-agenda-keymap)
  2647. ;;;; Define the Org-mode
  2648. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2649. (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."))
  2650. ;; We use a before-change function to check if a table might need
  2651. ;; an update.
  2652. (defvar org-table-may-need-update t
  2653. "Indicates that a table might need an update.
  2654. This variable is set by `org-before-change-function'.
  2655. `org-table-align' sets it back to nil.")
  2656. (defun org-before-change-function (beg end)
  2657. "Every change indicates that a table might need an update."
  2658. (setq org-table-may-need-update t))
  2659. (defvar org-mode-map)
  2660. (defvar org-mode-hook nil)
  2661. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2662. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2663. (defvar org-table-buffer-is-an nil)
  2664. (defconst org-outline-regexp "\\*+ ")
  2665. ;;;###autoload
  2666. (define-derived-mode org-mode outline-mode "Org"
  2667. "Outline-based notes management and organizer, alias
  2668. \"Carsten's outline-mode for keeping track of everything.\"
  2669. Org-mode develops organizational tasks around a NOTES file which
  2670. contains information about projects as plain text. Org-mode is
  2671. implemented on top of outline-mode, which is ideal to keep the content
  2672. of large files well structured. It supports ToDo items, deadlines and
  2673. time stamps, which magically appear in the diary listing of the Emacs
  2674. calendar. Tables are easily created with a built-in table editor.
  2675. Plain text URL-like links connect to websites, emails (VM), Usenet
  2676. messages (Gnus), BBDB entries, and any files related to the project.
  2677. For printing and sharing of notes, an Org-mode file (or a part of it)
  2678. can be exported as a structured ASCII or HTML file.
  2679. The following commands are available:
  2680. \\{org-mode-map}"
  2681. ;; Get rid of Outline menus, they are not needed
  2682. ;; Need to do this here because define-derived-mode sets up
  2683. ;; the keymap so late. Still, it is a waste to call this each time
  2684. ;; we switch another buffer into org-mode.
  2685. (if (featurep 'xemacs)
  2686. (when (boundp 'outline-mode-menu-heading)
  2687. ;; Assume this is Greg's port, it used easymenu
  2688. (easy-menu-remove outline-mode-menu-heading)
  2689. (easy-menu-remove outline-mode-menu-show)
  2690. (easy-menu-remove outline-mode-menu-hide))
  2691. (define-key org-mode-map [menu-bar headings] 'undefined)
  2692. (define-key org-mode-map [menu-bar hide] 'undefined)
  2693. (define-key org-mode-map [menu-bar show] 'undefined))
  2694. (org-load-modules-maybe)
  2695. (easy-menu-add org-org-menu)
  2696. (easy-menu-add org-tbl-menu)
  2697. (org-install-agenda-files-menu)
  2698. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2699. (org-add-to-invisibility-spec '(org-cwidth))
  2700. (when (featurep 'xemacs)
  2701. (org-set-local 'line-move-ignore-invisible t))
  2702. (org-set-local 'outline-regexp org-outline-regexp)
  2703. (org-set-local 'outline-level 'org-outline-level)
  2704. (when (and org-ellipsis
  2705. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2706. (fboundp 'make-glyph-code))
  2707. (unless org-display-table
  2708. (setq org-display-table (make-display-table)))
  2709. (set-display-table-slot
  2710. org-display-table 4
  2711. (vconcat (mapcar
  2712. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2713. org-ellipsis)))
  2714. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2715. (setq buffer-display-table org-display-table))
  2716. (org-set-regexps-and-options)
  2717. ;; Calc embedded
  2718. (org-set-local 'calc-embedded-open-mode "# ")
  2719. (modify-syntax-entry ?# "<")
  2720. (modify-syntax-entry ?@ "w")
  2721. (if org-startup-truncated (setq truncate-lines t))
  2722. (org-set-local 'font-lock-unfontify-region-function
  2723. 'org-unfontify-region)
  2724. ;; Activate before-change-function
  2725. (org-set-local 'org-table-may-need-update t)
  2726. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2727. 'local)
  2728. ;; Check for running clock before killing a buffer
  2729. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2730. ;; Paragraphs and auto-filling
  2731. (org-set-autofill-regexps)
  2732. (setq indent-line-function 'org-indent-line-function)
  2733. (org-update-radio-target-regexp)
  2734. ;; Comment characters
  2735. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2736. (org-set-local 'comment-padding " ")
  2737. ;; Align options lines
  2738. (org-set-local
  2739. 'align-mode-rules-list
  2740. '((org-in-buffer-settings
  2741. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2742. (modes . '(org-mode)))))
  2743. ;; Imenu
  2744. (org-set-local 'imenu-create-index-function
  2745. 'org-imenu-get-tree)
  2746. ;; Make isearch reveal context
  2747. (if (or (featurep 'xemacs)
  2748. (not (boundp 'outline-isearch-open-invisible-function)))
  2749. ;; Emacs 21 and XEmacs make use of the hook
  2750. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2751. ;; Emacs 22 deals with this through a special variable
  2752. (org-set-local 'outline-isearch-open-invisible-function
  2753. (lambda (&rest ignore) (org-show-context 'isearch))))
  2754. ;; If empty file that did not turn on org-mode automatically, make it to.
  2755. (if (and org-insert-mode-line-in-empty-file
  2756. (interactive-p)
  2757. (= (point-min) (point-max)))
  2758. (insert "# -*- mode: org -*-\n\n"))
  2759. (unless org-inhibit-startup
  2760. (when org-startup-align-all-tables
  2761. (let ((bmp (buffer-modified-p)))
  2762. (org-table-map-tables 'org-table-align)
  2763. (set-buffer-modified-p bmp)))
  2764. (org-cycle-hide-drawers 'all)
  2765. (cond
  2766. ((eq org-startup-folded t)
  2767. (org-cycle '(4)))
  2768. ((eq org-startup-folded 'content)
  2769. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2770. (org-cycle '(4)) (org-cycle '(4)))))))
  2771. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2772. (defun org-current-time ()
  2773. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2774. (if (> (car org-time-stamp-rounding-minutes) 1)
  2775. (let ((r (car org-time-stamp-rounding-minutes))
  2776. (time (decode-time)))
  2777. (apply 'encode-time
  2778. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2779. (nthcdr 2 time))))
  2780. (current-time)))
  2781. ;;;; Font-Lock stuff, including the activators
  2782. (defvar org-mouse-map (make-sparse-keymap))
  2783. (org-defkey org-mouse-map
  2784. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2785. (org-defkey org-mouse-map
  2786. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2787. (when org-mouse-1-follows-link
  2788. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2789. (when org-tab-follows-link
  2790. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2791. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2792. (when org-return-follows-link
  2793. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2794. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2795. (require 'font-lock)
  2796. (defconst org-non-link-chars "]\t\n\r<>")
  2797. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2798. "shell" "elisp"))
  2799. (defvar org-link-types-re nil
  2800. "Matches a link that has a url-like prefix like \"http:\"")
  2801. (defvar org-link-re-with-space nil
  2802. "Matches a link with spaces, optional angular brackets around it.")
  2803. (defvar org-link-re-with-space2 nil
  2804. "Matches a link with spaces, optional angular brackets around it.")
  2805. (defvar org-angle-link-re nil
  2806. "Matches link with angular brackets, spaces are allowed.")
  2807. (defvar org-plain-link-re nil
  2808. "Matches plain link, without spaces.")
  2809. (defvar org-bracket-link-regexp nil
  2810. "Matches a link in double brackets.")
  2811. (defvar org-bracket-link-analytic-regexp nil
  2812. "Regular expression used to analyze links.
  2813. Here is what the match groups contain after a match:
  2814. 1: http:
  2815. 2: http
  2816. 3: path
  2817. 4: [desc]
  2818. 5: desc")
  2819. (defvar org-any-link-re nil
  2820. "Regular expression matching any link.")
  2821. (defun org-make-link-regexps ()
  2822. "Update the link regular expressions.
  2823. This should be called after the variable `org-link-types' has changed."
  2824. (setq org-link-types-re
  2825. (concat
  2826. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2827. org-link-re-with-space
  2828. (concat
  2829. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2830. "\\([^" org-non-link-chars " ]"
  2831. "[^" org-non-link-chars "]*"
  2832. "[^" org-non-link-chars " ]\\)>?")
  2833. org-link-re-with-space2
  2834. (concat
  2835. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2836. "\\([^" org-non-link-chars " ]"
  2837. "[^]\t\n\r]*"
  2838. "[^" org-non-link-chars " ]\\)>?")
  2839. org-angle-link-re
  2840. (concat
  2841. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2842. "\\([^" org-non-link-chars " ]"
  2843. "[^" org-non-link-chars "]*"
  2844. "\\)>")
  2845. org-plain-link-re
  2846. (concat
  2847. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2848. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2849. org-bracket-link-regexp
  2850. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2851. org-bracket-link-analytic-regexp
  2852. (concat
  2853. "\\[\\["
  2854. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2855. "\\([^]]+\\)"
  2856. "\\]"
  2857. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2858. "\\]")
  2859. org-any-link-re
  2860. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2861. org-angle-link-re "\\)\\|\\("
  2862. org-plain-link-re "\\)")))
  2863. (org-make-link-regexps)
  2864. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2865. "Regular expression for fast time stamp matching.")
  2866. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2867. "Regular expression for fast time stamp matching.")
  2868. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2869. "Regular expression matching time strings for analysis.
  2870. This one does not require the space after the date, so it can be used
  2871. on a string that terminates immediately after the date.")
  2872. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2873. "Regular expression matching time strings for analysis.")
  2874. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2875. "Regular expression matching time stamps, with groups.")
  2876. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2877. "Regular expression matching time stamps (also [..]), with groups.")
  2878. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2879. "Regular expression matching a time stamp range.")
  2880. (defconst org-tr-regexp-both
  2881. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2882. "Regular expression matching a time stamp range.")
  2883. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2884. org-ts-regexp "\\)?")
  2885. "Regular expression matching a time stamp or time stamp range.")
  2886. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2887. org-ts-regexp-both "\\)?")
  2888. "Regular expression matching a time stamp or time stamp range.
  2889. The time stamps may be either active or inactive.")
  2890. (defvar org-emph-face nil)
  2891. (defun org-do-emphasis-faces (limit)
  2892. "Run through the buffer and add overlays to links."
  2893. (let (rtn)
  2894. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2895. (if (not (= (char-after (match-beginning 3))
  2896. (char-after (match-beginning 4))))
  2897. (progn
  2898. (setq rtn t)
  2899. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2900. 'face
  2901. (nth 1 (assoc (match-string 3)
  2902. org-emphasis-alist)))
  2903. (add-text-properties (match-beginning 2) (match-end 2)
  2904. '(font-lock-multiline t))
  2905. (when org-hide-emphasis-markers
  2906. (add-text-properties (match-end 4) (match-beginning 5)
  2907. '(invisible org-link))
  2908. (add-text-properties (match-beginning 3) (match-end 3)
  2909. '(invisible org-link)))))
  2910. (backward-char 1))
  2911. rtn))
  2912. (defun org-emphasize (&optional char)
  2913. "Insert or change an emphasis, i.e. a font like bold or italic.
  2914. If there is an active region, change that region to a new emphasis.
  2915. If there is no region, just insert the marker characters and position
  2916. the cursor between them.
  2917. CHAR should be either the marker character, or the first character of the
  2918. HTML tag associated with that emphasis. If CHAR is a space, the means
  2919. to remove the emphasis of the selected region.
  2920. If char is not given (for example in an interactive call) it
  2921. will be prompted for."
  2922. (interactive)
  2923. (let ((eal org-emphasis-alist) e det
  2924. (erc org-emphasis-regexp-components)
  2925. (prompt "")
  2926. (string "") beg end move tag c s)
  2927. (if (org-region-active-p)
  2928. (setq beg (region-beginning) end (region-end)
  2929. string (buffer-substring beg end))
  2930. (setq move t))
  2931. (while (setq e (pop eal))
  2932. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2933. c (aref tag 0))
  2934. (push (cons c (string-to-char (car e))) det)
  2935. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2936. (substring tag 1)))))
  2937. (unless char
  2938. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2939. (setq char (read-char-exclusive)))
  2940. (setq char (or (cdr (assoc char det)) char))
  2941. (if (equal char ?\ )
  2942. (setq s "" move nil)
  2943. (unless (assoc (char-to-string char) org-emphasis-alist)
  2944. (error "No such emphasis marker: \"%c\"" char))
  2945. (setq s (char-to-string char)))
  2946. (while (and (> (length string) 1)
  2947. (equal (substring string 0 1) (substring string -1))
  2948. (assoc (substring string 0 1) org-emphasis-alist))
  2949. (setq string (substring string 1 -1)))
  2950. (setq string (concat s string s))
  2951. (if beg (delete-region beg end))
  2952. (unless (or (bolp)
  2953. (string-match (concat "[" (nth 0 erc) "\n]")
  2954. (char-to-string (char-before (point)))))
  2955. (insert " "))
  2956. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2957. (char-to-string (char-after (point))))
  2958. (insert " ") (backward-char 1))
  2959. (insert string)
  2960. (and move (backward-char 1))))
  2961. (defconst org-nonsticky-props
  2962. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2963. (defun org-activate-plain-links (limit)
  2964. "Run through the buffer and add overlays to links."
  2965. (catch 'exit
  2966. (let (f)
  2967. (while (re-search-forward org-plain-link-re limit t)
  2968. (setq f (get-text-property (match-beginning 0) 'face))
  2969. (if (or (eq f 'org-tag)
  2970. (and (listp f) (memq 'org-tag f)))
  2971. nil
  2972. (add-text-properties (match-beginning 0) (match-end 0)
  2973. (list 'mouse-face 'highlight
  2974. 'rear-nonsticky org-nonsticky-props
  2975. 'keymap org-mouse-map
  2976. ))
  2977. (throw 'exit t))))))
  2978. (defun org-activate-code (limit)
  2979. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  2980. (unless (get-text-property (match-beginning 1) 'face)
  2981. (remove-text-properties (match-beginning 0) (match-end 0)
  2982. '(display t invisible t intangible t))
  2983. t)))
  2984. (defun org-activate-angle-links (limit)
  2985. "Run through the buffer and add overlays to links."
  2986. (if (re-search-forward org-angle-link-re limit t)
  2987. (progn
  2988. (add-text-properties (match-beginning 0) (match-end 0)
  2989. (list 'mouse-face 'highlight
  2990. 'rear-nonsticky org-nonsticky-props
  2991. 'keymap org-mouse-map
  2992. ))
  2993. t)))
  2994. (defun org-activate-bracket-links (limit)
  2995. "Run through the buffer and add overlays to bracketed links."
  2996. (if (re-search-forward org-bracket-link-regexp limit t)
  2997. (let* ((help (concat "LINK: "
  2998. (org-match-string-no-properties 1)))
  2999. ;; FIXME: above we should remove the escapes.
  3000. ;; but that requires another match, protecting match data,
  3001. ;; a lot of overhead for font-lock.
  3002. (ip (org-maybe-intangible
  3003. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3004. 'keymap org-mouse-map 'mouse-face 'highlight
  3005. 'font-lock-multiline t 'help-echo help)))
  3006. (vp (list 'rear-nonsticky org-nonsticky-props
  3007. 'keymap org-mouse-map 'mouse-face 'highlight
  3008. ' font-lock-multiline t 'help-echo help)))
  3009. ;; We need to remove the invisible property here. Table narrowing
  3010. ;; may have made some of this invisible.
  3011. (remove-text-properties (match-beginning 0) (match-end 0)
  3012. '(invisible nil))
  3013. (if (match-end 3)
  3014. (progn
  3015. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3016. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3017. (add-text-properties (match-end 3) (match-end 0) ip))
  3018. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3019. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3020. (add-text-properties (match-end 1) (match-end 0) ip))
  3021. t)))
  3022. (defun org-activate-dates (limit)
  3023. "Run through the buffer and add overlays to dates."
  3024. (if (re-search-forward org-tsr-regexp-both limit t)
  3025. (progn
  3026. (add-text-properties (match-beginning 0) (match-end 0)
  3027. (list 'mouse-face 'highlight
  3028. 'rear-nonsticky org-nonsticky-props
  3029. 'keymap org-mouse-map))
  3030. (when org-display-custom-times
  3031. (if (match-end 3)
  3032. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3033. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3034. t)))
  3035. (defvar org-target-link-regexp nil
  3036. "Regular expression matching radio targets in plain text.")
  3037. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3038. "Regular expression matching a link target.")
  3039. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3040. "Regular expression matching a radio target.")
  3041. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3042. "Regular expression matching any target.")
  3043. (defun org-activate-target-links (limit)
  3044. "Run through the buffer and add overlays to target matches."
  3045. (when org-target-link-regexp
  3046. (let ((case-fold-search t))
  3047. (if (re-search-forward org-target-link-regexp limit t)
  3048. (progn
  3049. (add-text-properties (match-beginning 0) (match-end 0)
  3050. (list 'mouse-face 'highlight
  3051. 'rear-nonsticky org-nonsticky-props
  3052. 'keymap org-mouse-map
  3053. 'help-echo "Radio target link"
  3054. 'org-linked-text t))
  3055. t)))))
  3056. (defun org-update-radio-target-regexp ()
  3057. "Find all radio targets in this file and update the regular expression."
  3058. (interactive)
  3059. (when (memq 'radio org-activate-links)
  3060. (setq org-target-link-regexp
  3061. (org-make-target-link-regexp (org-all-targets 'radio)))
  3062. (org-restart-font-lock)))
  3063. (defun org-hide-wide-columns (limit)
  3064. (let (s e)
  3065. (setq s (text-property-any (point) (or limit (point-max))
  3066. 'org-cwidth t))
  3067. (when s
  3068. (setq e (next-single-property-change s 'org-cwidth))
  3069. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3070. (goto-char e)
  3071. t)))
  3072. (defvar org-latex-and-specials-regexp nil
  3073. "Regular expression for highlighting export special stuff.")
  3074. (defvar org-match-substring-regexp)
  3075. (defvar org-match-substring-with-braces-regexp)
  3076. (defvar org-export-html-special-string-regexps)
  3077. (defun org-compute-latex-and-specials-regexp ()
  3078. "Compute regular expression for stuff treated specially by exporters."
  3079. (if (not org-highlight-latex-fragments-and-specials)
  3080. (org-set-local 'org-latex-and-specials-regexp nil)
  3081. (require 'org-exp)
  3082. (let*
  3083. ((matchers (plist-get org-format-latex-options :matchers))
  3084. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3085. org-latex-regexps)))
  3086. (options (org-combine-plists (org-default-export-plist)
  3087. (org-infile-export-plist)))
  3088. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3089. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3090. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3091. (org-export-html-expand (plist-get options :expand-quoted-html))
  3092. (org-export-with-special-strings (plist-get options :special-strings))
  3093. (re-sub
  3094. (cond
  3095. ((equal org-export-with-sub-superscripts '{})
  3096. (list org-match-substring-with-braces-regexp))
  3097. (org-export-with-sub-superscripts
  3098. (list org-match-substring-regexp))
  3099. (t nil)))
  3100. (re-latex
  3101. (if org-export-with-LaTeX-fragments
  3102. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3103. (re-macros
  3104. (if org-export-with-TeX-macros
  3105. (list (concat "\\\\"
  3106. (regexp-opt
  3107. (append (mapcar 'car org-html-entities)
  3108. (if (boundp 'org-latex-entities)
  3109. org-latex-entities nil))
  3110. 'words))) ; FIXME
  3111. ))
  3112. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3113. (re-special (if org-export-with-special-strings
  3114. (mapcar (lambda (x) (car x))
  3115. org-export-html-special-string-regexps)))
  3116. (re-rest
  3117. (delq nil
  3118. (list
  3119. (if org-export-html-expand "@<[^>\n]+>")
  3120. ))))
  3121. (org-set-local
  3122. 'org-latex-and-specials-regexp
  3123. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3124. re-rest) "\\|")))))
  3125. (defun org-do-latex-and-special-faces (limit)
  3126. "Run through the buffer and add overlays to links."
  3127. (when org-latex-and-specials-regexp
  3128. (let (rtn d)
  3129. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3130. limit t))
  3131. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3132. 'face))
  3133. '(org-code org-verbatim underline)))
  3134. (progn
  3135. (setq rtn t
  3136. d (cond ((member (char-after (1+ (match-beginning 0)))
  3137. '(?_ ?^)) 1)
  3138. (t 0)))
  3139. (font-lock-prepend-text-property
  3140. (+ d (match-beginning 0)) (match-end 0)
  3141. 'face 'org-latex-and-export-specials)
  3142. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3143. '(font-lock-multiline t)))))
  3144. rtn)))
  3145. (defun org-restart-font-lock ()
  3146. "Restart font-lock-mode, to force refontification."
  3147. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3148. (font-lock-mode -1)
  3149. (font-lock-mode 1)))
  3150. (defun org-all-targets (&optional radio)
  3151. "Return a list of all targets in this file.
  3152. With optional argument RADIO, only find radio targets."
  3153. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3154. rtn)
  3155. (save-excursion
  3156. (goto-char (point-min))
  3157. (while (re-search-forward re nil t)
  3158. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3159. rtn)))
  3160. (defun org-make-target-link-regexp (targets)
  3161. "Make regular expression matching all strings in TARGETS.
  3162. The regular expression finds the targets also if there is a line break
  3163. between words."
  3164. (and targets
  3165. (concat
  3166. "\\<\\("
  3167. (mapconcat
  3168. (lambda (x)
  3169. (while (string-match " +" x)
  3170. (setq x (replace-match "\\s-+" t t x)))
  3171. x)
  3172. targets
  3173. "\\|")
  3174. "\\)\\>")))
  3175. (defun org-activate-tags (limit)
  3176. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3177. (progn
  3178. (add-text-properties (match-beginning 1) (match-end 1)
  3179. (list 'mouse-face 'highlight
  3180. 'rear-nonsticky org-nonsticky-props
  3181. 'keymap org-mouse-map))
  3182. t)))
  3183. (defun org-outline-level ()
  3184. (save-excursion
  3185. (looking-at outline-regexp)
  3186. (if (match-beginning 1)
  3187. (+ (org-get-string-indentation (match-string 1)) 1000)
  3188. (1- (- (match-end 0) (match-beginning 0))))))
  3189. (defvar org-font-lock-keywords nil)
  3190. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\(\\S-.*\\)")
  3191. "Regular expression matching a property line.")
  3192. (defun org-set-font-lock-defaults ()
  3193. (let* ((em org-fontify-emphasized-text)
  3194. (lk org-activate-links)
  3195. (org-font-lock-extra-keywords
  3196. (list
  3197. ;; Headlines
  3198. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3199. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3200. ;; Table lines
  3201. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3202. (1 'org-table t))
  3203. ;; Table internals
  3204. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3205. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3206. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3207. ;; Drawers
  3208. (list org-drawer-regexp '(0 'org-special-keyword t))
  3209. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3210. ;; Properties
  3211. (list org-property-re
  3212. '(1 'org-special-keyword t)
  3213. '(3 'org-property-value t))
  3214. (if org-format-transports-properties-p
  3215. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3216. ;; Links
  3217. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3218. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3219. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3220. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3221. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3222. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3223. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3224. '(org-hide-wide-columns (0 nil append))
  3225. ;; TODO lines
  3226. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3227. '(1 (org-get-todo-face 1) t))
  3228. ;; DONE
  3229. (if org-fontify-done-headline
  3230. (list (concat "^[*]+ +\\<\\("
  3231. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3232. "\\)\\(.*\\)")
  3233. '(2 'org-headline-done t))
  3234. nil)
  3235. ;; Priorities
  3236. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3237. ;; Special keywords
  3238. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3239. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3240. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3241. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3242. ;; Emphasis
  3243. (if em
  3244. (if (featurep 'xemacs)
  3245. '(org-do-emphasis-faces (0 nil append))
  3246. '(org-do-emphasis-faces)))
  3247. ;; Checkboxes
  3248. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3249. 2 'bold prepend)
  3250. (if org-provide-checkbox-statistics
  3251. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3252. (0 (org-get-checkbox-statistics-face) t)))
  3253. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3254. '(1 'org-archived prepend))
  3255. ;; Specials
  3256. '(org-do-latex-and-special-faces)
  3257. ;; Code
  3258. '(org-activate-code (1 'org-code t))
  3259. ;; COMMENT
  3260. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3261. "\\|" org-quote-string "\\)\\>")
  3262. '(1 'org-special-keyword t))
  3263. '("^#.*" (0 'font-lock-comment-face t))
  3264. )))
  3265. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3266. ;; Now set the full font-lock-keywords
  3267. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3268. (org-set-local 'font-lock-defaults
  3269. '(org-font-lock-keywords t nil nil backward-paragraph))
  3270. (kill-local-variable 'font-lock-keywords) nil))
  3271. (defvar org-m nil)
  3272. (defvar org-l nil)
  3273. (defvar org-f nil)
  3274. (defun org-get-level-face (n)
  3275. "Get the right face for match N in font-lock matching of healdines."
  3276. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3277. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3278. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3279. (cond
  3280. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3281. ((eq n 2) org-f)
  3282. (t (if org-level-color-stars-only nil org-f))))
  3283. (defun org-get-todo-face (kwd)
  3284. "Get the right face for a TODO keyword KWD.
  3285. If KWD is a number, get the corresponding match group."
  3286. (if (numberp kwd) (setq kwd (match-string kwd)))
  3287. (or (cdr (assoc kwd org-todo-keyword-faces))
  3288. (and (member kwd org-done-keywords) 'org-done)
  3289. 'org-todo))
  3290. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3291. "Remove fontification and activation overlays from links."
  3292. (font-lock-default-unfontify-region beg end)
  3293. (let* ((buffer-undo-list t)
  3294. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3295. (inhibit-modification-hooks t)
  3296. deactivate-mark buffer-file-name buffer-file-truename)
  3297. (remove-text-properties beg end
  3298. '(mouse-face t keymap t org-linked-text t
  3299. invisible t intangible t))))
  3300. ;;;; Visibility cycling, including org-goto and indirect buffer
  3301. ;;; Cycling
  3302. (defvar org-cycle-global-status nil)
  3303. (make-variable-buffer-local 'org-cycle-global-status)
  3304. (defvar org-cycle-subtree-status nil)
  3305. (make-variable-buffer-local 'org-cycle-subtree-status)
  3306. ;;;###autoload
  3307. (defun org-cycle (&optional arg)
  3308. "Visibility cycling for Org-mode.
  3309. - When this function is called with a prefix argument, rotate the entire
  3310. buffer through 3 states (global cycling)
  3311. 1. OVERVIEW: Show only top-level headlines.
  3312. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3313. 3. SHOW ALL: Show everything.
  3314. - When point is at the beginning of a headline, rotate the subtree started
  3315. by this line through 3 different states (local cycling)
  3316. 1. FOLDED: Only the main headline is shown.
  3317. 2. CHILDREN: The main headline and the direct children are shown.
  3318. From this state, you can move to one of the children
  3319. and zoom in further.
  3320. 3. SUBTREE: Show the entire subtree, including body text.
  3321. - When there is a numeric prefix, go up to a heading with level ARG, do
  3322. a `show-subtree' and return to the previous cursor position. If ARG
  3323. is negative, go up that many levels.
  3324. - When point is not at the beginning of a headline, execute
  3325. `indent-relative', like TAB normally does. See the option
  3326. `org-cycle-emulate-tab' for details.
  3327. - Special case: if point is at the beginning of the buffer and there is
  3328. no headline in line 1, this function will act as if called with prefix arg.
  3329. But only if also the variable `org-cycle-global-at-bob' is t."
  3330. (interactive "P")
  3331. (org-load-modules-maybe)
  3332. (let* ((outline-regexp
  3333. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3334. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3335. outline-regexp))
  3336. (bob-special (and org-cycle-global-at-bob (bobp)
  3337. (not (looking-at outline-regexp))))
  3338. (org-cycle-hook
  3339. (if bob-special
  3340. (delq 'org-optimize-window-after-visibility-change
  3341. (copy-sequence org-cycle-hook))
  3342. org-cycle-hook))
  3343. (pos (point)))
  3344. (if (or bob-special (equal arg '(4)))
  3345. ;; special case: use global cycling
  3346. (setq arg t))
  3347. (cond
  3348. ((org-at-table-p 'any)
  3349. ;; Enter the table or move to the next field in the table
  3350. (or (org-table-recognize-table.el)
  3351. (progn
  3352. (if arg (org-table-edit-field t)
  3353. (org-table-justify-field-maybe)
  3354. (call-interactively 'org-table-next-field)))))
  3355. ((eq arg t) ;; Global cycling
  3356. (cond
  3357. ((and (eq last-command this-command)
  3358. (eq org-cycle-global-status 'overview))
  3359. ;; We just created the overview - now do table of contents
  3360. ;; This can be slow in very large buffers, so indicate action
  3361. (message "CONTENTS...")
  3362. (org-content)
  3363. (message "CONTENTS...done")
  3364. (setq org-cycle-global-status 'contents)
  3365. (run-hook-with-args 'org-cycle-hook 'contents))
  3366. ((and (eq last-command this-command)
  3367. (eq org-cycle-global-status 'contents))
  3368. ;; We just showed the table of contents - now show everything
  3369. (show-all)
  3370. (message "SHOW ALL")
  3371. (setq org-cycle-global-status 'all)
  3372. (run-hook-with-args 'org-cycle-hook 'all))
  3373. (t
  3374. ;; Default action: go to overview
  3375. (org-overview)
  3376. (message "OVERVIEW")
  3377. (setq org-cycle-global-status 'overview)
  3378. (run-hook-with-args 'org-cycle-hook 'overview))))
  3379. ((and org-drawers org-drawer-regexp
  3380. (save-excursion
  3381. (beginning-of-line 1)
  3382. (looking-at org-drawer-regexp)))
  3383. ;; Toggle block visibility
  3384. (org-flag-drawer
  3385. (not (get-char-property (match-end 0) 'invisible))))
  3386. ((integerp arg)
  3387. ;; Show-subtree, ARG levels up from here.
  3388. (save-excursion
  3389. (org-back-to-heading)
  3390. (outline-up-heading (if (< arg 0) (- arg)
  3391. (- (funcall outline-level) arg)))
  3392. (org-show-subtree)))
  3393. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3394. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3395. ;; At a heading: rotate between three different views
  3396. (org-back-to-heading)
  3397. (let ((goal-column 0) eoh eol eos)
  3398. ;; First, some boundaries
  3399. (save-excursion
  3400. (org-back-to-heading)
  3401. (save-excursion
  3402. (beginning-of-line 2)
  3403. (while (and (not (eobp)) ;; this is like `next-line'
  3404. (get-char-property (1- (point)) 'invisible))
  3405. (beginning-of-line 2)) (setq eol (point)))
  3406. (outline-end-of-heading) (setq eoh (point))
  3407. (org-end-of-subtree t)
  3408. (unless (eobp)
  3409. (skip-chars-forward " \t\n")
  3410. (beginning-of-line 1) ; in case this is an item
  3411. )
  3412. (setq eos (1- (point))))
  3413. ;; Find out what to do next and set `this-command'
  3414. (cond
  3415. ((= eos eoh)
  3416. ;; Nothing is hidden behind this heading
  3417. (message "EMPTY ENTRY")
  3418. (setq org-cycle-subtree-status nil)
  3419. (save-excursion
  3420. (goto-char eos)
  3421. (outline-next-heading)
  3422. (if (org-invisible-p) (org-flag-heading nil))))
  3423. ((or (>= eol eos)
  3424. (not (string-match "\\S-" (buffer-substring eol eos))))
  3425. ;; Entire subtree is hidden in one line: open it
  3426. (org-show-entry)
  3427. (show-children)
  3428. (message "CHILDREN")
  3429. (save-excursion
  3430. (goto-char eos)
  3431. (outline-next-heading)
  3432. (if (org-invisible-p) (org-flag-heading nil)))
  3433. (setq org-cycle-subtree-status 'children)
  3434. (run-hook-with-args 'org-cycle-hook 'children))
  3435. ((and (eq last-command this-command)
  3436. (eq org-cycle-subtree-status 'children))
  3437. ;; We just showed the children, now show everything.
  3438. (org-show-subtree)
  3439. (message "SUBTREE")
  3440. (setq org-cycle-subtree-status 'subtree)
  3441. (run-hook-with-args 'org-cycle-hook 'subtree))
  3442. (t
  3443. ;; Default action: hide the subtree.
  3444. (hide-subtree)
  3445. (message "FOLDED")
  3446. (setq org-cycle-subtree-status 'folded)
  3447. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3448. ;; TAB emulation
  3449. (buffer-read-only (org-back-to-heading))
  3450. ((org-try-cdlatex-tab))
  3451. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3452. (or (not (bolp))
  3453. (not (looking-at outline-regexp))))
  3454. (call-interactively (global-key-binding "\t")))
  3455. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3456. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3457. (or (and (eq org-cycle-emulate-tab 'white)
  3458. (= (match-end 0) (point-at-eol)))
  3459. (and (eq org-cycle-emulate-tab 'whitestart)
  3460. (>= (match-end 0) pos))))
  3461. t
  3462. (eq org-cycle-emulate-tab t))
  3463. (call-interactively (global-key-binding "\t")))
  3464. (t (save-excursion
  3465. (org-back-to-heading)
  3466. (org-cycle))))))
  3467. ;;;###autoload
  3468. (defun org-global-cycle (&optional arg)
  3469. "Cycle the global visibility. For details see `org-cycle'."
  3470. (interactive "P")
  3471. (let ((org-cycle-include-plain-lists
  3472. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3473. (if (integerp arg)
  3474. (progn
  3475. (show-all)
  3476. (hide-sublevels arg)
  3477. (setq org-cycle-global-status 'contents))
  3478. (org-cycle '(4)))))
  3479. (defun org-overview ()
  3480. "Switch to overview mode, shoing only top-level headlines.
  3481. Really, this shows all headlines with level equal or greater than the level
  3482. of the first headline in the buffer. This is important, because if the
  3483. first headline is not level one, then (hide-sublevels 1) gives confusing
  3484. results."
  3485. (interactive)
  3486. (let ((level (save-excursion
  3487. (goto-char (point-min))
  3488. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3489. (progn
  3490. (goto-char (match-beginning 0))
  3491. (funcall outline-level))))))
  3492. (and level (hide-sublevels level))))
  3493. (defun org-content (&optional arg)
  3494. "Show all headlines in the buffer, like a table of contents.
  3495. With numerical argument N, show content up to level N."
  3496. (interactive "P")
  3497. (save-excursion
  3498. ;; Visit all headings and show their offspring
  3499. (and (integerp arg) (org-overview))
  3500. (goto-char (point-max))
  3501. (catch 'exit
  3502. (while (and (progn (condition-case nil
  3503. (outline-previous-visible-heading 1)
  3504. (error (goto-char (point-min))))
  3505. t)
  3506. (looking-at outline-regexp))
  3507. (if (integerp arg)
  3508. (show-children (1- arg))
  3509. (show-branches))
  3510. (if (bobp) (throw 'exit nil))))))
  3511. (defun org-optimize-window-after-visibility-change (state)
  3512. "Adjust the window after a change in outline visibility.
  3513. This function is the default value of the hook `org-cycle-hook'."
  3514. (when (get-buffer-window (current-buffer))
  3515. (cond
  3516. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3517. ; ((eq state 'overview) (org-beginning-of-line))
  3518. ((eq state 'content) nil)
  3519. ((eq state 'all) nil)
  3520. ((eq state 'folded) nil)
  3521. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3522. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3523. (defun org-compact-display-after-subtree-move ()
  3524. (let (beg end)
  3525. (save-excursion
  3526. (if (org-up-heading-safe)
  3527. (progn
  3528. (hide-subtree)
  3529. (show-entry)
  3530. (show-children)
  3531. (org-cycle-show-empty-lines 'children)
  3532. (org-cycle-hide-drawers 'children))
  3533. (org-overview)))))
  3534. (defun org-cycle-show-empty-lines (state)
  3535. "Show empty lines above all visible headlines.
  3536. The region to be covered depends on STATE when called through
  3537. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3538. entire buffer covered. Note that an empty line is only shown if there
  3539. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3540. (when (> org-cycle-separator-lines 0)
  3541. (save-excursion
  3542. (let* ((n org-cycle-separator-lines)
  3543. (re (cond
  3544. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3545. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3546. (t (let ((ns (number-to-string (- n 2))))
  3547. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3548. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3549. beg end)
  3550. (cond
  3551. ((memq state '(overview contents t))
  3552. (setq beg (point-min) end (point-max)))
  3553. ((memq state '(children folded))
  3554. (setq beg (point) end (progn (org-end-of-subtree t t)
  3555. (beginning-of-line 2)
  3556. (point)))))
  3557. (when beg
  3558. (goto-char beg)
  3559. (while (re-search-forward re end t)
  3560. (if (not (get-char-property (match-end 1) 'invisible))
  3561. (outline-flag-region
  3562. (match-beginning 1) (match-end 1) nil)))))))
  3563. ;; Never hide empty lines at the end of the file.
  3564. (save-excursion
  3565. (goto-char (point-max))
  3566. (outline-previous-heading)
  3567. (outline-end-of-heading)
  3568. (if (and (looking-at "[ \t\n]+")
  3569. (= (match-end 0) (point-max)))
  3570. (outline-flag-region (point) (match-end 0) nil))))
  3571. (defun org-cycle-hide-drawers (state)
  3572. "Re-hide all drawers after a visibility state change."
  3573. (when (and (org-mode-p)
  3574. (not (memq state '(overview folded))))
  3575. (save-excursion
  3576. (let* ((globalp (memq state '(contents all)))
  3577. (beg (if globalp (point-min) (point)))
  3578. (end (if globalp (point-max) (org-end-of-subtree t))))
  3579. (goto-char beg)
  3580. (while (re-search-forward org-drawer-regexp end t)
  3581. (org-flag-drawer t))))))
  3582. (defun org-flag-drawer (flag)
  3583. (save-excursion
  3584. (beginning-of-line 1)
  3585. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3586. (let ((b (match-end 0))
  3587. (outline-regexp org-outline-regexp))
  3588. (if (re-search-forward
  3589. "^[ \t]*:END:"
  3590. (save-excursion (outline-next-heading) (point)) t)
  3591. (outline-flag-region b (point-at-eol) flag)
  3592. (error ":END: line missing"))))))
  3593. (defun org-subtree-end-visible-p ()
  3594. "Is the end of the current subtree visible?"
  3595. (pos-visible-in-window-p
  3596. (save-excursion (org-end-of-subtree t) (point))))
  3597. (defun org-first-headline-recenter (&optional N)
  3598. "Move cursor to the first headline and recenter the headline.
  3599. Optional argument N means, put the headline into the Nth line of the window."
  3600. (goto-char (point-min))
  3601. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3602. (beginning-of-line)
  3603. (recenter (prefix-numeric-value N))))
  3604. ;;; Org-goto
  3605. (defvar org-goto-window-configuration nil)
  3606. (defvar org-goto-marker nil)
  3607. (defvar org-goto-map
  3608. (let ((map (make-sparse-keymap)))
  3609. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3610. (while (setq cmd (pop cmds))
  3611. (substitute-key-definition cmd cmd map global-map)))
  3612. (suppress-keymap map)
  3613. (org-defkey map "\C-m" 'org-goto-ret)
  3614. (org-defkey map [(return)] 'org-goto-ret)
  3615. (org-defkey map [(left)] 'org-goto-left)
  3616. (org-defkey map [(right)] 'org-goto-right)
  3617. (org-defkey map [(control ?g)] 'org-goto-quit)
  3618. (org-defkey map "\C-i" 'org-cycle)
  3619. (org-defkey map [(tab)] 'org-cycle)
  3620. (org-defkey map [(down)] 'outline-next-visible-heading)
  3621. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3622. (if org-goto-auto-isearch
  3623. (if (fboundp 'define-key-after)
  3624. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3625. nil)
  3626. (org-defkey map "q" 'org-goto-quit)
  3627. (org-defkey map "n" 'outline-next-visible-heading)
  3628. (org-defkey map "p" 'outline-previous-visible-heading)
  3629. (org-defkey map "f" 'outline-forward-same-level)
  3630. (org-defkey map "b" 'outline-backward-same-level)
  3631. (org-defkey map "u" 'outline-up-heading))
  3632. (org-defkey map "/" 'org-occur)
  3633. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3634. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3635. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3636. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3637. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3638. map))
  3639. (defconst org-goto-help
  3640. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3641. RET=jump to location [Q]uit and return to previous location
  3642. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3643. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3644. (defun org-goto (&optional alternative-interface)
  3645. "Look up a different location in the current file, keeping current visibility.
  3646. When you want look-up or go to a different location in a document, the
  3647. fastest way is often to fold the entire buffer and then dive into the tree.
  3648. This method has the disadvantage, that the previous location will be folded,
  3649. which may not be what you want.
  3650. This command works around this by showing a copy of the current buffer
  3651. in an indirect buffer, in overview mode. You can dive into the tree in
  3652. that copy, use org-occur and incremental search to find a location.
  3653. When pressing RET or `Q', the command returns to the original buffer in
  3654. which the visibility is still unchanged. After RET is will also jump to
  3655. the location selected in the indirect buffer and expose the
  3656. the headline hierarchy above."
  3657. (interactive "P")
  3658. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3659. (org-refile-use-outline-path t)
  3660. (interface
  3661. (if (not alternative-interface)
  3662. org-goto-interface
  3663. (if (eq org-goto-interface 'outline)
  3664. 'outline-path-completion
  3665. 'outline)))
  3666. (org-goto-start-pos (point))
  3667. (selected-point
  3668. (if (eq interface 'outline)
  3669. (car (org-get-location (current-buffer) org-goto-help))
  3670. (nth 3 (org-refile-get-location "Goto: ")))))
  3671. (if selected-point
  3672. (progn
  3673. (org-mark-ring-push org-goto-start-pos)
  3674. (goto-char selected-point)
  3675. (if (or (org-invisible-p) (org-invisible-p2))
  3676. (org-show-context 'org-goto)))
  3677. (message "Quit"))))
  3678. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3679. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3680. (defvar org-goto-local-auto-isearch-map) ; defined below
  3681. (defun org-get-location (buf help)
  3682. "Let the user select a location in the Org-mode buffer BUF.
  3683. This function uses a recursive edit. It returns the selected position
  3684. or nil."
  3685. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3686. (isearch-hide-immediately nil)
  3687. (isearch-search-fun-function
  3688. (lambda () 'org-goto-local-search-forward-headings))
  3689. (org-goto-selected-point org-goto-exit-command))
  3690. (save-excursion
  3691. (save-window-excursion
  3692. (delete-other-windows)
  3693. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3694. (switch-to-buffer
  3695. (condition-case nil
  3696. (make-indirect-buffer (current-buffer) "*org-goto*")
  3697. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3698. (with-output-to-temp-buffer "*Help*"
  3699. (princ help))
  3700. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3701. (setq buffer-read-only nil)
  3702. (let ((org-startup-truncated t)
  3703. (org-startup-folded nil)
  3704. (org-startup-align-all-tables nil))
  3705. (org-mode)
  3706. (org-overview))
  3707. (setq buffer-read-only t)
  3708. (if (and (boundp 'org-goto-start-pos)
  3709. (integer-or-marker-p org-goto-start-pos))
  3710. (let ((org-show-hierarchy-above t)
  3711. (org-show-siblings t)
  3712. (org-show-following-heading t))
  3713. (goto-char org-goto-start-pos)
  3714. (and (org-invisible-p) (org-show-context)))
  3715. (goto-char (point-min)))
  3716. (org-beginning-of-line)
  3717. (message "Select location and press RET")
  3718. (use-local-map org-goto-map)
  3719. (recursive-edit)
  3720. ))
  3721. (kill-buffer "*org-goto*")
  3722. (cons org-goto-selected-point org-goto-exit-command)))
  3723. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3724. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3725. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3726. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3727. (defun org-goto-local-search-forward-headings (string bound noerror)
  3728. "Search and make sure that anu matches are in headlines."
  3729. (catch 'return
  3730. (while (search-forward string bound noerror)
  3731. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3732. (and (member :headline context)
  3733. (not (member :tags context))))
  3734. (throw 'return (point))))))
  3735. (defun org-goto-local-auto-isearch ()
  3736. "Start isearch."
  3737. (interactive)
  3738. (goto-char (point-min))
  3739. (let ((keys (this-command-keys)))
  3740. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3741. (isearch-mode t)
  3742. (isearch-process-search-char (string-to-char keys)))))
  3743. (defun org-goto-ret (&optional arg)
  3744. "Finish `org-goto' by going to the new location."
  3745. (interactive "P")
  3746. (setq org-goto-selected-point (point)
  3747. org-goto-exit-command 'return)
  3748. (throw 'exit nil))
  3749. (defun org-goto-left ()
  3750. "Finish `org-goto' by going to the new location."
  3751. (interactive)
  3752. (if (org-on-heading-p)
  3753. (progn
  3754. (beginning-of-line 1)
  3755. (setq org-goto-selected-point (point)
  3756. org-goto-exit-command 'left)
  3757. (throw 'exit nil))
  3758. (error "Not on a heading")))
  3759. (defun org-goto-right ()
  3760. "Finish `org-goto' by going to the new location."
  3761. (interactive)
  3762. (if (org-on-heading-p)
  3763. (progn
  3764. (setq org-goto-selected-point (point)
  3765. org-goto-exit-command 'right)
  3766. (throw 'exit nil))
  3767. (error "Not on a heading")))
  3768. (defun org-goto-quit ()
  3769. "Finish `org-goto' without cursor motion."
  3770. (interactive)
  3771. (setq org-goto-selected-point nil)
  3772. (setq org-goto-exit-command 'quit)
  3773. (throw 'exit nil))
  3774. ;;; Indirect buffer display of subtrees
  3775. (defvar org-indirect-dedicated-frame nil
  3776. "This is the frame being used for indirect tree display.")
  3777. (defvar org-last-indirect-buffer nil)
  3778. (defun org-tree-to-indirect-buffer (&optional arg)
  3779. "Create indirect buffer and narrow it to current subtree.
  3780. With numerical prefix ARG, go up to this level and then take that tree.
  3781. If ARG is negative, go up that many levels.
  3782. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3783. indirect buffer previously made with this command, to avoid proliferation of
  3784. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3785. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3786. is kept so that you can work with several indirect buffers at the same time.
  3787. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3788. requests that a new frame be made for the new buffer, so that the dedicated
  3789. frame is not changed."
  3790. (interactive "P")
  3791. (let ((cbuf (current-buffer))
  3792. (cwin (selected-window))
  3793. (pos (point))
  3794. beg end level heading ibuf)
  3795. (save-excursion
  3796. (org-back-to-heading t)
  3797. (when (numberp arg)
  3798. (setq level (org-outline-level))
  3799. (if (< arg 0) (setq arg (+ level arg)))
  3800. (while (> (setq level (org-outline-level)) arg)
  3801. (outline-up-heading 1 t)))
  3802. (setq beg (point)
  3803. heading (org-get-heading))
  3804. (org-end-of-subtree t) (setq end (point)))
  3805. (if (and (buffer-live-p org-last-indirect-buffer)
  3806. (not (eq org-indirect-buffer-display 'new-frame))
  3807. (not arg))
  3808. (kill-buffer org-last-indirect-buffer))
  3809. (setq ibuf (org-get-indirect-buffer cbuf)
  3810. org-last-indirect-buffer ibuf)
  3811. (cond
  3812. ((or (eq org-indirect-buffer-display 'new-frame)
  3813. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3814. (select-frame (make-frame))
  3815. (delete-other-windows)
  3816. (switch-to-buffer ibuf)
  3817. (org-set-frame-title heading))
  3818. ((eq org-indirect-buffer-display 'dedicated-frame)
  3819. (raise-frame
  3820. (select-frame (or (and org-indirect-dedicated-frame
  3821. (frame-live-p org-indirect-dedicated-frame)
  3822. org-indirect-dedicated-frame)
  3823. (setq org-indirect-dedicated-frame (make-frame)))))
  3824. (delete-other-windows)
  3825. (switch-to-buffer ibuf)
  3826. (org-set-frame-title (concat "Indirect: " heading)))
  3827. ((eq org-indirect-buffer-display 'current-window)
  3828. (switch-to-buffer ibuf))
  3829. ((eq org-indirect-buffer-display 'other-window)
  3830. (pop-to-buffer ibuf))
  3831. (t (error "Invalid value.")))
  3832. (if (featurep 'xemacs)
  3833. (save-excursion (org-mode) (turn-on-font-lock)))
  3834. (narrow-to-region beg end)
  3835. (show-all)
  3836. (goto-char pos)
  3837. (and (window-live-p cwin) (select-window cwin))))
  3838. (defun org-get-indirect-buffer (&optional buffer)
  3839. (setq buffer (or buffer (current-buffer)))
  3840. (let ((n 1) (base (buffer-name buffer)) bname)
  3841. (while (buffer-live-p
  3842. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3843. (setq n (1+ n)))
  3844. (condition-case nil
  3845. (make-indirect-buffer buffer bname 'clone)
  3846. (error (make-indirect-buffer buffer bname)))))
  3847. (defun org-set-frame-title (title)
  3848. "Set the title of the current frame to the string TITLE."
  3849. ;; FIXME: how to name a single frame in XEmacs???
  3850. (unless (featurep 'xemacs)
  3851. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3852. ;;;; Structure editing
  3853. ;;; Inserting headlines
  3854. (defun org-insert-heading (&optional force-heading)
  3855. "Insert a new heading or item with same depth at point.
  3856. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3857. If point is at the beginning of a headline, insert a sibling before the
  3858. current headline. If point is not at the beginning, do not split the line,
  3859. but create the new hedline after the current line."
  3860. (interactive "P")
  3861. (if (= (buffer-size) 0)
  3862. (insert "\n* ")
  3863. (when (or force-heading (not (org-insert-item)))
  3864. (let* ((head (save-excursion
  3865. (condition-case nil
  3866. (progn
  3867. (org-back-to-heading)
  3868. (match-string 0))
  3869. (error "*"))))
  3870. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3871. pos)
  3872. (cond
  3873. ((and (org-on-heading-p) (bolp)
  3874. (or (bobp)
  3875. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3876. ;; insert before the current line
  3877. (open-line (if blank 2 1)))
  3878. ((and (bolp)
  3879. (or (bobp)
  3880. (save-excursion
  3881. (backward-char 1) (not (org-invisible-p)))))
  3882. ;; insert right here
  3883. nil)
  3884. (t
  3885. ;; in the middle of the line
  3886. (org-show-entry)
  3887. (let ((split
  3888. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3889. tags pos)
  3890. (if (org-on-heading-p)
  3891. (progn
  3892. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3893. (setq tags (and (match-end 2) (match-string 2)))
  3894. (and (match-end 1)
  3895. (delete-region (match-beginning 1) (match-end 1)))
  3896. (setq pos (point-at-bol))
  3897. (or split (end-of-line 1))
  3898. (delete-horizontal-space)
  3899. (newline (if blank 2 1))
  3900. (when tags
  3901. (save-excursion
  3902. (goto-char pos)
  3903. (end-of-line 1)
  3904. (insert " " tags)
  3905. (org-set-tags nil 'align))))
  3906. (or split (end-of-line 1))
  3907. (newline (if blank 2 1))))))
  3908. (insert head) (just-one-space)
  3909. (setq pos (point))
  3910. (end-of-line 1)
  3911. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3912. (run-hooks 'org-insert-heading-hook)))))
  3913. (defun org-get-heading (&optional no-tags)
  3914. "Return the heading of the current entry, without the stars."
  3915. (save-excursion
  3916. (org-back-to-heading t)
  3917. (if (looking-at
  3918. (if no-tags
  3919. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  3920. "\\*+[ \t]+\\([^\r\n]*\\)"))
  3921. (match-string 1) "")))
  3922. (defun org-insert-heading-after-current ()
  3923. "Insert a new heading with same level as current, after current subtree."
  3924. (interactive)
  3925. (org-back-to-heading)
  3926. (org-insert-heading)
  3927. (org-move-subtree-down)
  3928. (end-of-line 1))
  3929. (defun org-insert-todo-heading (arg)
  3930. "Insert a new heading with the same level and TODO state as current heading.
  3931. If the heading has no TODO state, or if the state is DONE, use the first
  3932. state (TODO by default). Also with prefix arg, force first state."
  3933. (interactive "P")
  3934. (when (not (org-insert-item 'checkbox))
  3935. (org-insert-heading)
  3936. (save-excursion
  3937. (org-back-to-heading)
  3938. (outline-previous-heading)
  3939. (looking-at org-todo-line-regexp))
  3940. (if (or arg
  3941. (not (match-beginning 2))
  3942. (member (match-string 2) org-done-keywords))
  3943. (insert (car org-todo-keywords-1) " ")
  3944. (insert (match-string 2) " "))))
  3945. (defun org-insert-subheading (arg)
  3946. "Insert a new subheading and demote it.
  3947. Works for outline headings and for plain lists alike."
  3948. (interactive "P")
  3949. (org-insert-heading arg)
  3950. (cond
  3951. ((org-on-heading-p) (org-do-demote))
  3952. ((org-at-item-p) (org-indent-item 1))))
  3953. (defun org-insert-todo-subheading (arg)
  3954. "Insert a new subheading with TODO keyword or checkbox and demote it.
  3955. Works for outline headings and for plain lists alike."
  3956. (interactive "P")
  3957. (org-insert-todo-heading arg)
  3958. (cond
  3959. ((org-on-heading-p) (org-do-demote))
  3960. ((org-at-item-p) (org-indent-item 1))))
  3961. ;;; Promotion and Demotion
  3962. (defun org-promote-subtree ()
  3963. "Promote the entire subtree.
  3964. See also `org-promote'."
  3965. (interactive)
  3966. (save-excursion
  3967. (org-map-tree 'org-promote))
  3968. (org-fix-position-after-promote))
  3969. (defun org-demote-subtree ()
  3970. "Demote the entire subtree. See `org-demote'.
  3971. See also `org-promote'."
  3972. (interactive)
  3973. (save-excursion
  3974. (org-map-tree 'org-demote))
  3975. (org-fix-position-after-promote))
  3976. (defun org-do-promote ()
  3977. "Promote the current heading higher up the tree.
  3978. If the region is active in `transient-mark-mode', promote all headings
  3979. in the region."
  3980. (interactive)
  3981. (save-excursion
  3982. (if (org-region-active-p)
  3983. (org-map-region 'org-promote (region-beginning) (region-end))
  3984. (org-promote)))
  3985. (org-fix-position-after-promote))
  3986. (defun org-do-demote ()
  3987. "Demote the current heading lower down the tree.
  3988. If the region is active in `transient-mark-mode', demote all headings
  3989. in the region."
  3990. (interactive)
  3991. (save-excursion
  3992. (if (org-region-active-p)
  3993. (org-map-region 'org-demote (region-beginning) (region-end))
  3994. (org-demote)))
  3995. (org-fix-position-after-promote))
  3996. (defun org-fix-position-after-promote ()
  3997. "Make sure that after pro/demotion cursor position is right."
  3998. (let ((pos (point)))
  3999. (when (save-excursion
  4000. (beginning-of-line 1)
  4001. (looking-at org-todo-line-regexp)
  4002. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4003. (cond ((eobp) (insert " "))
  4004. ((eolp) (insert " "))
  4005. ((equal (char-after) ?\ ) (forward-char 1))))))
  4006. (defun org-reduced-level (l)
  4007. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4008. (defun org-get-valid-level (level &optional change)
  4009. "Rectify a level change under the influence of `org-odd-levels-only'
  4010. LEVEL is a current level, CHANGE is by how much the level should be
  4011. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4012. even level numbers will become the next higher odd number."
  4013. (if org-odd-levels-only
  4014. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4015. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4016. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4017. (max 1 (+ level change))))
  4018. (if (boundp 'define-obsolete-function-alias)
  4019. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4020. (define-obsolete-function-alias 'org-get-legal-level
  4021. 'org-get-valid-level)
  4022. (define-obsolete-function-alias 'org-get-legal-level
  4023. 'org-get-valid-level "23.1")))
  4024. (defun org-promote ()
  4025. "Promote the current heading higher up the tree.
  4026. If the region is active in `transient-mark-mode', promote all headings
  4027. in the region."
  4028. (org-back-to-heading t)
  4029. (let* ((level (save-match-data (funcall outline-level)))
  4030. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4031. (diff (abs (- level (length up-head) -1))))
  4032. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4033. (replace-match up-head nil t)
  4034. ;; Fixup tag positioning
  4035. (and org-auto-align-tags (org-set-tags nil t))
  4036. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4037. (defun org-demote ()
  4038. "Demote the current heading lower down the tree.
  4039. If the region is active in `transient-mark-mode', demote all headings
  4040. in the region."
  4041. (org-back-to-heading t)
  4042. (let* ((level (save-match-data (funcall outline-level)))
  4043. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4044. (diff (abs (- level (length down-head) -1))))
  4045. (replace-match down-head nil t)
  4046. ;; Fixup tag positioning
  4047. (and org-auto-align-tags (org-set-tags nil t))
  4048. (if org-adapt-indentation (org-fixup-indentation diff))))
  4049. (defun org-map-tree (fun)
  4050. "Call FUN for every heading underneath the current one."
  4051. (org-back-to-heading)
  4052. (let ((level (funcall outline-level)))
  4053. (save-excursion
  4054. (funcall fun)
  4055. (while (and (progn
  4056. (outline-next-heading)
  4057. (> (funcall outline-level) level))
  4058. (not (eobp)))
  4059. (funcall fun)))))
  4060. (defun org-map-region (fun beg end)
  4061. "Call FUN for every heading between BEG and END."
  4062. (let ((org-ignore-region t))
  4063. (save-excursion
  4064. (setq end (copy-marker end))
  4065. (goto-char beg)
  4066. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4067. (< (point) end))
  4068. (funcall fun))
  4069. (while (and (progn
  4070. (outline-next-heading)
  4071. (< (point) end))
  4072. (not (eobp)))
  4073. (funcall fun)))))
  4074. (defun org-fixup-indentation (diff)
  4075. "Change the indentation in the current entry by DIFF
  4076. However, if any line in the current entry has no indentation, or if it
  4077. would end up with no indentation after the change, nothing at all is done."
  4078. (save-excursion
  4079. (let ((end (save-excursion (outline-next-heading)
  4080. (point-marker)))
  4081. (prohibit (if (> diff 0)
  4082. "^\\S-"
  4083. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4084. col)
  4085. (unless (save-excursion (end-of-line 1)
  4086. (re-search-forward prohibit end t))
  4087. (while (and (< (point) end)
  4088. (re-search-forward "^[ \t]+" end t))
  4089. (goto-char (match-end 0))
  4090. (setq col (current-column))
  4091. (if (< diff 0) (replace-match ""))
  4092. (indent-to (+ diff col))))
  4093. (move-marker end nil))))
  4094. (defun org-convert-to-odd-levels ()
  4095. "Convert an org-mode file with all levels allowed to one with odd levels.
  4096. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4097. level 5 etc."
  4098. (interactive)
  4099. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4100. (let ((org-odd-levels-only nil) n)
  4101. (save-excursion
  4102. (goto-char (point-min))
  4103. (while (re-search-forward "^\\*\\*+ " nil t)
  4104. (setq n (- (length (match-string 0)) 2))
  4105. (while (>= (setq n (1- n)) 0)
  4106. (org-demote))
  4107. (end-of-line 1))))))
  4108. (defun org-convert-to-oddeven-levels ()
  4109. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4110. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4111. section with an even level, conversion would destroy the structure of the file. An error
  4112. is signaled in this case."
  4113. (interactive)
  4114. (goto-char (point-min))
  4115. ;; First check if there are no even levels
  4116. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4117. (org-show-context t)
  4118. (error "Not all levels are odd in this file. Conversion not possible."))
  4119. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4120. (let ((org-odd-levels-only nil) n)
  4121. (save-excursion
  4122. (goto-char (point-min))
  4123. (while (re-search-forward "^\\*\\*+ " nil t)
  4124. (setq n (/ (1- (length (match-string 0))) 2))
  4125. (while (>= (setq n (1- n)) 0)
  4126. (org-promote))
  4127. (end-of-line 1))))))
  4128. (defun org-tr-level (n)
  4129. "Make N odd if required."
  4130. (if org-odd-levels-only (1+ (/ n 2)) n))
  4131. ;;; Vertical tree motion, cutting and pasting of subtrees
  4132. (defun org-move-subtree-up (&optional arg)
  4133. "Move the current subtree up past ARG headlines of the same level."
  4134. (interactive "p")
  4135. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4136. (defun org-move-subtree-down (&optional arg)
  4137. "Move the current subtree down past ARG headlines of the same level."
  4138. (interactive "p")
  4139. (setq arg (prefix-numeric-value arg))
  4140. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4141. 'outline-get-last-sibling))
  4142. (ins-point (make-marker))
  4143. (cnt (abs arg))
  4144. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4145. ;; Select the tree
  4146. (org-back-to-heading)
  4147. (setq beg0 (point))
  4148. (save-excursion
  4149. (setq ne-beg (org-back-over-empty-lines))
  4150. (setq beg (point)))
  4151. (save-match-data
  4152. (save-excursion (outline-end-of-heading)
  4153. (setq folded (org-invisible-p)))
  4154. (outline-end-of-subtree))
  4155. (outline-next-heading)
  4156. (setq ne-end (org-back-over-empty-lines))
  4157. (setq end (point))
  4158. (goto-char beg0)
  4159. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4160. ;; include less whitespace
  4161. (save-excursion
  4162. (goto-char beg)
  4163. (forward-line (- ne-beg ne-end))
  4164. (setq beg (point))))
  4165. ;; Find insertion point, with error handling
  4166. (while (> cnt 0)
  4167. (or (and (funcall movfunc) (looking-at outline-regexp))
  4168. (progn (goto-char beg0)
  4169. (error "Cannot move past superior level or buffer limit")))
  4170. (setq cnt (1- cnt)))
  4171. (if (> arg 0)
  4172. ;; Moving forward - still need to move over subtree
  4173. (progn (org-end-of-subtree t t)
  4174. (save-excursion
  4175. (org-back-over-empty-lines)
  4176. (or (bolp) (newline)))))
  4177. (setq ne-ins (org-back-over-empty-lines))
  4178. (move-marker ins-point (point))
  4179. (setq txt (buffer-substring beg end))
  4180. (delete-region beg end)
  4181. (outline-flag-region (1- beg) beg nil)
  4182. (outline-flag-region (1- (point)) (point) nil)
  4183. (insert txt)
  4184. (or (bolp) (insert "\n"))
  4185. (setq ins-end (point))
  4186. (goto-char ins-point)
  4187. (org-skip-whitespace)
  4188. (when (and (< arg 0)
  4189. (org-first-sibling-p)
  4190. (> ne-ins ne-beg))
  4191. ;; Move whitespace back to beginning
  4192. (save-excursion
  4193. (goto-char ins-end)
  4194. (let ((kill-whole-line t))
  4195. (kill-line (- ne-ins ne-beg)) (point)))
  4196. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4197. (move-marker ins-point nil)
  4198. (org-compact-display-after-subtree-move)
  4199. (unless folded
  4200. (org-show-entry)
  4201. (show-children)
  4202. (org-cycle-hide-drawers 'children))))
  4203. (defvar org-subtree-clip ""
  4204. "Clipboard for cut and paste of subtrees.
  4205. This is actually only a copy of the kill, because we use the normal kill
  4206. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4207. (defvar org-subtree-clip-folded nil
  4208. "Was the last copied subtree folded?
  4209. This is used to fold the tree back after pasting.")
  4210. (defun org-cut-subtree (&optional n)
  4211. "Cut the current subtree into the clipboard.
  4212. With prefix arg N, cut this many sequential subtrees.
  4213. This is a short-hand for marking the subtree and then cutting it."
  4214. (interactive "p")
  4215. (org-copy-subtree n 'cut))
  4216. (defun org-copy-subtree (&optional n cut)
  4217. "Cut the current subtree into the clipboard.
  4218. With prefix arg N, cut this many sequential subtrees.
  4219. This is a short-hand for marking the subtree and then copying it.
  4220. If CUT is non-nil, actually cut the subtree."
  4221. (interactive "p")
  4222. (let (beg end folded (beg0 (point)))
  4223. (if (interactive-p)
  4224. (org-back-to-heading nil) ; take what looks like a subtree
  4225. (org-back-to-heading t)) ; take what is really there
  4226. (org-back-over-empty-lines)
  4227. (setq beg (point))
  4228. (skip-chars-forward " \t\r\n")
  4229. (save-match-data
  4230. (save-excursion (outline-end-of-heading)
  4231. (setq folded (org-invisible-p)))
  4232. (condition-case nil
  4233. (outline-forward-same-level (1- n))
  4234. (error nil))
  4235. (org-end-of-subtree t t))
  4236. (org-back-over-empty-lines)
  4237. (setq end (point))
  4238. (goto-char beg0)
  4239. (when (> end beg)
  4240. (setq org-subtree-clip-folded folded)
  4241. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4242. (setq org-subtree-clip (current-kill 0))
  4243. (message "%s: Subtree(s) with %d characters"
  4244. (if cut "Cut" "Copied")
  4245. (length org-subtree-clip)))))
  4246. (defun org-paste-subtree (&optional level tree)
  4247. "Paste the clipboard as a subtree, with modification of headline level.
  4248. The entire subtree is promoted or demoted in order to match a new headline
  4249. level. By default, the new level is derived from the visible headings
  4250. before and after the insertion point, and taken to be the inferior headline
  4251. level of the two. So if the previous visible heading is level 3 and the
  4252. next is level 4 (or vice versa), level 4 will be used for insertion.
  4253. This makes sure that the subtree remains an independent subtree and does
  4254. not swallow low level entries.
  4255. You can also force a different level, either by using a numeric prefix
  4256. argument, or by inserting the heading marker by hand. For example, if the
  4257. cursor is after \"*****\", then the tree will be shifted to level 5.
  4258. If you want to insert the tree as is, just use \\[yank].
  4259. If optional TREE is given, use this text instead of the kill ring."
  4260. (interactive "P")
  4261. (unless (org-kill-is-subtree-p tree)
  4262. (error "%s"
  4263. (substitute-command-keys
  4264. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4265. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4266. (^re (concat "^\\(" outline-regexp "\\)"))
  4267. (re (concat "\\(" outline-regexp "\\)"))
  4268. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4269. (old-level (if (string-match ^re txt)
  4270. (- (match-end 0) (match-beginning 0) 1)
  4271. -1))
  4272. (force-level (cond (level (prefix-numeric-value level))
  4273. ((string-match
  4274. ^re_ (buffer-substring (point-at-bol) (point)))
  4275. (- (match-end 1) (match-beginning 1)))
  4276. (t nil)))
  4277. (previous-level (save-excursion
  4278. (condition-case nil
  4279. (progn
  4280. (outline-previous-visible-heading 1)
  4281. (if (looking-at re)
  4282. (- (match-end 0) (match-beginning 0) 1)
  4283. 1))
  4284. (error 1))))
  4285. (next-level (save-excursion
  4286. (condition-case nil
  4287. (progn
  4288. (or (looking-at outline-regexp)
  4289. (outline-next-visible-heading 1))
  4290. (if (looking-at re)
  4291. (- (match-end 0) (match-beginning 0) 1)
  4292. 1))
  4293. (error 1))))
  4294. (new-level (or force-level (max previous-level next-level)))
  4295. (shift (if (or (= old-level -1)
  4296. (= new-level -1)
  4297. (= old-level new-level))
  4298. 0
  4299. (- new-level old-level)))
  4300. (delta (if (> shift 0) -1 1))
  4301. (func (if (> shift 0) 'org-demote 'org-promote))
  4302. (org-odd-levels-only nil)
  4303. beg end)
  4304. ;; Remove the forced level indicator
  4305. (if force-level
  4306. (delete-region (point-at-bol) (point)))
  4307. ;; Paste
  4308. (beginning-of-line 1)
  4309. (org-back-over-empty-lines)
  4310. (setq beg (point))
  4311. (insert-before-markers txt)
  4312. (unless (string-match "\n\\'" txt) (insert "\n"))
  4313. (setq end (point))
  4314. (goto-char beg)
  4315. (skip-chars-forward " \t\n\r")
  4316. (setq beg (point))
  4317. ;; Shift if necessary
  4318. (unless (= shift 0)
  4319. (save-restriction
  4320. (narrow-to-region beg end)
  4321. (while (not (= shift 0))
  4322. (org-map-region func (point-min) (point-max))
  4323. (setq shift (+ delta shift)))
  4324. (goto-char (point-min))))
  4325. (when (interactive-p)
  4326. (message "Clipboard pasted as level %d subtree" new-level))
  4327. (if (and kill-ring
  4328. (eq org-subtree-clip (current-kill 0))
  4329. org-subtree-clip-folded)
  4330. ;; The tree was folded before it was killed/copied
  4331. (hide-subtree))))
  4332. (defun org-kill-is-subtree-p (&optional txt)
  4333. "Check if the current kill is an outline subtree, or a set of trees.
  4334. Returns nil if kill does not start with a headline, or if the first
  4335. headline level is not the largest headline level in the tree.
  4336. So this will actually accept several entries of equal levels as well,
  4337. which is OK for `org-paste-subtree'.
  4338. If optional TXT is given, check this string instead of the current kill."
  4339. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4340. (start-level (and kill
  4341. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4342. org-outline-regexp "\\)")
  4343. kill)
  4344. (- (match-end 2) (match-beginning 2) 1)))
  4345. (re (concat "^" org-outline-regexp))
  4346. (start (1+ (match-beginning 2))))
  4347. (if (not start-level)
  4348. (progn
  4349. nil) ;; does not even start with a heading
  4350. (catch 'exit
  4351. (while (setq start (string-match re kill (1+ start)))
  4352. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4353. (throw 'exit nil)))
  4354. t))))
  4355. (defun org-narrow-to-subtree ()
  4356. "Narrow buffer to the current subtree."
  4357. (interactive)
  4358. (save-excursion
  4359. (save-match-data
  4360. (narrow-to-region
  4361. (progn (org-back-to-heading) (point))
  4362. (progn (org-end-of-subtree t t) (point))))))
  4363. ;;; Outline Sorting
  4364. (defun org-sort (with-case)
  4365. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4366. Optional argument WITH-CASE means sort case-sensitively."
  4367. (interactive "P")
  4368. (if (org-at-table-p)
  4369. (org-call-with-arg 'org-table-sort-lines with-case)
  4370. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4371. (defun org-sort-remove-invisible (s)
  4372. (remove-text-properties 0 (length s) org-rm-props s)
  4373. (while (string-match org-bracket-link-regexp s)
  4374. (setq s (replace-match (if (match-end 2)
  4375. (match-string 3 s)
  4376. (match-string 1 s)) t t s)))
  4377. s)
  4378. (defvar org-priority-regexp) ; defined later in the file
  4379. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4380. "Sort entries on a certain level of an outline tree.
  4381. If there is an active region, the entries in the region are sorted.
  4382. Else, if the cursor is before the first entry, sort the top-level items.
  4383. Else, the children of the entry at point are sorted.
  4384. Sorting can be alphabetically, numerically, and by date/time as given by
  4385. the first time stamp in the entry. The command prompts for the sorting
  4386. type unless it has been given to the function through the SORTING-TYPE
  4387. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4388. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4389. called with point at the beginning of the record. It must return either
  4390. a string or a number that should serve as the sorting key for that record.
  4391. Comparing entries ignores case by default. However, with an optional argument
  4392. WITH-CASE, the sorting considers case as well."
  4393. (interactive "P")
  4394. (let ((case-func (if with-case 'identity 'downcase))
  4395. start beg end stars re re2
  4396. txt what tmp plain-list-p)
  4397. ;; Find beginning and end of region to sort
  4398. (cond
  4399. ((org-region-active-p)
  4400. ;; we will sort the region
  4401. (setq end (region-end)
  4402. what "region")
  4403. (goto-char (region-beginning))
  4404. (if (not (org-on-heading-p)) (outline-next-heading))
  4405. (setq start (point)))
  4406. ((org-at-item-p)
  4407. ;; we will sort this plain list
  4408. (org-beginning-of-item-list) (setq start (point))
  4409. (org-end-of-item-list) (setq end (point))
  4410. (goto-char start)
  4411. (setq plain-list-p t
  4412. what "plain list"))
  4413. ((or (org-on-heading-p)
  4414. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4415. ;; we will sort the children of the current headline
  4416. (org-back-to-heading)
  4417. (setq start (point)
  4418. end (progn (org-end-of-subtree t t)
  4419. (org-back-over-empty-lines)
  4420. (point))
  4421. what "children")
  4422. (goto-char start)
  4423. (show-subtree)
  4424. (outline-next-heading))
  4425. (t
  4426. ;; we will sort the top-level entries in this file
  4427. (goto-char (point-min))
  4428. (or (org-on-heading-p) (outline-next-heading))
  4429. (setq start (point) end (point-max) what "top-level")
  4430. (goto-char start)
  4431. (show-all)))
  4432. (setq beg (point))
  4433. (if (>= beg end) (error "Nothing to sort"))
  4434. (unless plain-list-p
  4435. (looking-at "\\(\\*+\\)")
  4436. (setq stars (match-string 1)
  4437. re (concat "^" (regexp-quote stars) " +")
  4438. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4439. txt (buffer-substring beg end))
  4440. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4441. (if (and (not (equal stars "*")) (string-match re2 txt))
  4442. (error "Region to sort contains a level above the first entry")))
  4443. (unless sorting-type
  4444. (message
  4445. (if plain-list-p
  4446. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4447. "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:")
  4448. what)
  4449. (setq sorting-type (read-char-exclusive))
  4450. (and (= (downcase sorting-type) ?f)
  4451. (setq getkey-func
  4452. (completing-read "Sort using function: "
  4453. obarray 'fboundp t nil nil))
  4454. (setq getkey-func (intern getkey-func)))
  4455. (and (= (downcase sorting-type) ?r)
  4456. (setq property
  4457. (completing-read "Property: "
  4458. (mapcar 'list (org-buffer-property-keys t))
  4459. nil t))))
  4460. (message "Sorting entries...")
  4461. (save-restriction
  4462. (narrow-to-region start end)
  4463. (let ((dcst (downcase sorting-type))
  4464. (now (current-time)))
  4465. (sort-subr
  4466. (/= dcst sorting-type)
  4467. ;; This function moves to the beginning character of the "record" to
  4468. ;; be sorted.
  4469. (if plain-list-p
  4470. (lambda nil
  4471. (if (org-at-item-p) t (goto-char (point-max))))
  4472. (lambda nil
  4473. (if (re-search-forward re nil t)
  4474. (goto-char (match-beginning 0))
  4475. (goto-char (point-max)))))
  4476. ;; This function moves to the last character of the "record" being
  4477. ;; sorted.
  4478. (if plain-list-p
  4479. 'org-end-of-item
  4480. (lambda nil
  4481. (save-match-data
  4482. (condition-case nil
  4483. (outline-forward-same-level 1)
  4484. (error
  4485. (goto-char (point-max)))))))
  4486. ;; This function returns the value that gets sorted against.
  4487. (if plain-list-p
  4488. (lambda nil
  4489. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4490. (cond
  4491. ((= dcst ?n)
  4492. (string-to-number (buffer-substring (match-end 0)
  4493. (point-at-eol))))
  4494. ((= dcst ?a)
  4495. (buffer-substring (match-end 0) (point-at-eol)))
  4496. ((= dcst ?t)
  4497. (if (re-search-forward org-ts-regexp
  4498. (point-at-eol) t)
  4499. (org-time-string-to-time (match-string 0))
  4500. now))
  4501. ((= dcst ?f)
  4502. (if getkey-func
  4503. (progn
  4504. (setq tmp (funcall getkey-func))
  4505. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4506. tmp)
  4507. (error "Invalid key function `%s'" getkey-func)))
  4508. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4509. (lambda nil
  4510. (cond
  4511. ((= dcst ?n)
  4512. (if (looking-at outline-regexp)
  4513. (string-to-number (buffer-substring (match-end 0)
  4514. (point-at-eol)))
  4515. nil))
  4516. ((= dcst ?a)
  4517. (funcall case-func (buffer-substring (point-at-bol)
  4518. (point-at-eol))))
  4519. ((= dcst ?t)
  4520. (if (re-search-forward org-ts-regexp
  4521. (save-excursion
  4522. (forward-line 2)
  4523. (point)) t)
  4524. (org-time-string-to-time (match-string 0))
  4525. now))
  4526. ((= dcst ?p)
  4527. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4528. (string-to-char (match-string 2))
  4529. org-default-priority))
  4530. ((= dcst ?r)
  4531. (or (org-entry-get nil property) ""))
  4532. ((= dcst ?o)
  4533. (if (looking-at org-complex-heading-regexp)
  4534. (- 9999 (length (member (match-string 2)
  4535. org-todo-keywords-1)))))
  4536. ((= dcst ?f)
  4537. (if getkey-func
  4538. (progn
  4539. (setq tmp (funcall getkey-func))
  4540. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4541. tmp)
  4542. (error "Invalid key function `%s'" getkey-func)))
  4543. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4544. nil
  4545. (cond
  4546. ((= dcst ?a) 'string<)
  4547. ((= dcst ?t) 'time-less-p)
  4548. (t nil)))))
  4549. (message "Sorting entries...done")))
  4550. (defun org-do-sort (table what &optional with-case sorting-type)
  4551. "Sort TABLE of WHAT according to SORTING-TYPE.
  4552. The user will be prompted for the SORTING-TYPE if the call to this
  4553. function does not specify it. WHAT is only for the prompt, to indicate
  4554. what is being sorted. The sorting key will be extracted from
  4555. the car of the elements of the table.
  4556. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4557. (unless sorting-type
  4558. (message
  4559. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4560. what)
  4561. (setq sorting-type (read-char-exclusive)))
  4562. (let ((dcst (downcase sorting-type))
  4563. extractfun comparefun)
  4564. ;; Define the appropriate functions
  4565. (cond
  4566. ((= dcst ?n)
  4567. (setq extractfun 'string-to-number
  4568. comparefun (if (= dcst sorting-type) '< '>)))
  4569. ((= dcst ?a)
  4570. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4571. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4572. comparefun (if (= dcst sorting-type)
  4573. 'string<
  4574. (lambda (a b) (and (not (string< a b))
  4575. (not (string= a b)))))))
  4576. ((= dcst ?t)
  4577. (setq extractfun
  4578. (lambda (x)
  4579. (if (string-match org-ts-regexp x)
  4580. (time-to-seconds
  4581. (org-time-string-to-time (match-string 0 x)))
  4582. 0))
  4583. comparefun (if (= dcst sorting-type) '< '>)))
  4584. (t (error "Invalid sorting type `%c'" sorting-type)))
  4585. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4586. table)
  4587. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4588. ;;;; Plain list items, including checkboxes
  4589. ;;; Plain list items
  4590. (defun org-at-item-p ()
  4591. "Is point in a line starting a hand-formatted item?"
  4592. (let ((llt org-plain-list-ordered-item-terminator))
  4593. (save-excursion
  4594. (goto-char (point-at-bol))
  4595. (looking-at
  4596. (cond
  4597. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4598. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4599. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4600. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4601. (defun org-in-item-p ()
  4602. "It the cursor inside a plain list item.
  4603. Does not have to be the first line."
  4604. (save-excursion
  4605. (condition-case nil
  4606. (progn
  4607. (org-beginning-of-item)
  4608. (org-at-item-p)
  4609. t)
  4610. (error nil))))
  4611. (defun org-insert-item (&optional checkbox)
  4612. "Insert a new item at the current level.
  4613. Return t when things worked, nil when we are not in an item."
  4614. (when (save-excursion
  4615. (condition-case nil
  4616. (progn
  4617. (org-beginning-of-item)
  4618. (org-at-item-p)
  4619. (if (org-invisible-p) (error "Invisible item"))
  4620. t)
  4621. (error nil)))
  4622. (let* ((bul (match-string 0))
  4623. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4624. (match-end 0)))
  4625. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4626. pos)
  4627. (cond
  4628. ((and (org-at-item-p) (<= (point) eow))
  4629. ;; before the bullet
  4630. (beginning-of-line 1)
  4631. (open-line (if blank 2 1)))
  4632. ((<= (point) eow)
  4633. (beginning-of-line 1))
  4634. (t
  4635. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4636. (end-of-line 1)
  4637. (delete-horizontal-space))
  4638. (newline (if blank 2 1))))
  4639. (insert bul (if checkbox "[ ]" ""))
  4640. (just-one-space)
  4641. (setq pos (point))
  4642. (end-of-line 1)
  4643. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4644. (org-maybe-renumber-ordered-list)
  4645. (and checkbox (org-update-checkbox-count-maybe))
  4646. t))
  4647. ;;; Checkboxes
  4648. (defun org-at-item-checkbox-p ()
  4649. "Is point at a line starting a plain-list item with a checklet?"
  4650. (and (org-at-item-p)
  4651. (save-excursion
  4652. (goto-char (match-end 0))
  4653. (skip-chars-forward " \t")
  4654. (looking-at "\\[[- X]\\]"))))
  4655. (defun org-toggle-checkbox (&optional arg)
  4656. "Toggle the checkbox in the current line."
  4657. (interactive "P")
  4658. (catch 'exit
  4659. (let (beg end status (firstnew 'unknown))
  4660. (cond
  4661. ((org-region-active-p)
  4662. (setq beg (region-beginning) end (region-end)))
  4663. ((org-on-heading-p)
  4664. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4665. ((org-at-item-checkbox-p)
  4666. (let ((pos (point)))
  4667. (replace-match
  4668. (cond (arg "[-]")
  4669. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4670. (t "[ ]"))
  4671. t t)
  4672. (goto-char pos))
  4673. (throw 'exit t))
  4674. (t (error "Not at a checkbox or heading, and no active region")))
  4675. (save-excursion
  4676. (goto-char beg)
  4677. (while (< (point) end)
  4678. (when (org-at-item-checkbox-p)
  4679. (setq status (equal (match-string 0) "[X]"))
  4680. (when (eq firstnew 'unknown)
  4681. (setq firstnew (not status)))
  4682. (replace-match
  4683. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4684. (beginning-of-line 2)))))
  4685. (org-update-checkbox-count-maybe))
  4686. (defun org-update-checkbox-count-maybe ()
  4687. "Update checkbox statistics unless turned off by user."
  4688. (when org-provide-checkbox-statistics
  4689. (org-update-checkbox-count)))
  4690. (defun org-update-checkbox-count (&optional all)
  4691. "Update the checkbox statistics in the current section.
  4692. This will find all statistic cookies like [57%] and [6/12] and update them
  4693. with the current numbers. With optional prefix argument ALL, do this for
  4694. the whole buffer."
  4695. (interactive "P")
  4696. (save-excursion
  4697. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4698. (beg (condition-case nil
  4699. (progn (outline-back-to-heading) (point))
  4700. (error (point-min))))
  4701. (end (move-marker (make-marker)
  4702. (progn (outline-next-heading) (point))))
  4703. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4704. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4705. (re-find (concat re "\\|" re-box))
  4706. beg-cookie end-cookie is-percent c-on c-off lim
  4707. eline curr-ind next-ind continue-from startsearch
  4708. (cstat 0)
  4709. )
  4710. (when all
  4711. (goto-char (point-min))
  4712. (outline-next-heading)
  4713. (setq beg (point) end (point-max)))
  4714. (goto-char end)
  4715. ;; find each statistic cookie
  4716. (while (re-search-backward re-find beg t)
  4717. (setq beg-cookie (match-beginning 1)
  4718. end-cookie (match-end 1)
  4719. cstat (+ cstat (if end-cookie 1 0))
  4720. startsearch (point-at-eol)
  4721. continue-from (point-at-bol)
  4722. is-percent (match-beginning 2)
  4723. lim (cond
  4724. ((org-on-heading-p) (outline-next-heading) (point))
  4725. ((org-at-item-p) (org-end-of-item) (point))
  4726. (t nil))
  4727. c-on 0
  4728. c-off 0)
  4729. (when lim
  4730. ;; find first checkbox for this cookie and gather
  4731. ;; statistics from all that are at this indentation level
  4732. (goto-char startsearch)
  4733. (if (re-search-forward re-box lim t)
  4734. (progn
  4735. (org-beginning-of-item)
  4736. (setq curr-ind (org-get-indentation))
  4737. (setq next-ind curr-ind)
  4738. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4739. (save-excursion (end-of-line) (setq eline (point)))
  4740. (if (re-search-forward re-box eline t)
  4741. (if (member (match-string 2) '("[ ]" "[-]"))
  4742. (setq c-off (1+ c-off))
  4743. (setq c-on (1+ c-on))
  4744. )
  4745. )
  4746. (org-end-of-item)
  4747. (setq next-ind (org-get-indentation))
  4748. )))
  4749. (goto-char continue-from)
  4750. ;; update cookie
  4751. (when end-cookie
  4752. (delete-region beg-cookie end-cookie)
  4753. (goto-char beg-cookie)
  4754. (insert
  4755. (if is-percent
  4756. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4757. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4758. ;; update items checkbox if it has one
  4759. (when (org-at-item-p)
  4760. (org-beginning-of-item)
  4761. (when (and (> (+ c-on c-off) 0)
  4762. (re-search-forward re-box (point-at-eol) t))
  4763. (setq beg-cookie (match-beginning 2)
  4764. end-cookie (match-end 2))
  4765. (delete-region beg-cookie end-cookie)
  4766. (goto-char beg-cookie)
  4767. (cond ((= c-off 0) (insert "[X]"))
  4768. ((= c-on 0) (insert "[ ]"))
  4769. (t (insert "[-]")))
  4770. )))
  4771. (goto-char continue-from))
  4772. (when (interactive-p)
  4773. (message "Checkbox satistics updated %s (%d places)"
  4774. (if all "in entire file" "in current outline entry") cstat)))))
  4775. (defun org-get-checkbox-statistics-face ()
  4776. "Select the face for checkbox statistics.
  4777. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4778. it will be `org-todo'."
  4779. (if (match-end 1)
  4780. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4781. (if (and (> (match-end 2) (match-beginning 2))
  4782. (equal (match-string 2) (match-string 3)))
  4783. 'org-done
  4784. 'org-todo)))
  4785. (defun org-get-indentation (&optional line)
  4786. "Get the indentation of the current line, interpreting tabs.
  4787. When LINE is given, assume it represents a line and compute its indentation."
  4788. (if line
  4789. (if (string-match "^ *" (org-remove-tabs line))
  4790. (match-end 0))
  4791. (save-excursion
  4792. (beginning-of-line 1)
  4793. (skip-chars-forward " \t")
  4794. (current-column))))
  4795. (defun org-remove-tabs (s &optional width)
  4796. "Replace tabulators in S with spaces.
  4797. Assumes that s is a single line, starting in column 0."
  4798. (setq width (or width tab-width))
  4799. (while (string-match "\t" s)
  4800. (setq s (replace-match
  4801. (make-string
  4802. (- (* width (/ (+ (match-beginning 0) width) width))
  4803. (match-beginning 0)) ?\ )
  4804. t t s)))
  4805. s)
  4806. (defun org-fix-indentation (line ind)
  4807. "Fix indentation in LINE.
  4808. IND is a cons cell with target and minimum indentation.
  4809. If the current indenation in LINE is smaller than the minimum,
  4810. leave it alone. If it is larger than ind, set it to the target."
  4811. (let* ((l (org-remove-tabs line))
  4812. (i (org-get-indentation l))
  4813. (i1 (car ind)) (i2 (cdr ind)))
  4814. (if (>= i i2) (setq l (substring line i2)))
  4815. (if (> i1 0)
  4816. (concat (make-string i1 ?\ ) l)
  4817. l)))
  4818. (defun org-beginning-of-item ()
  4819. "Go to the beginning of the current hand-formatted item.
  4820. If the cursor is not in an item, throw an error."
  4821. (interactive)
  4822. (let ((pos (point))
  4823. (limit (save-excursion
  4824. (condition-case nil
  4825. (progn
  4826. (org-back-to-heading)
  4827. (beginning-of-line 2) (point))
  4828. (error (point-min)))))
  4829. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4830. ind ind1)
  4831. (if (org-at-item-p)
  4832. (beginning-of-line 1)
  4833. (beginning-of-line 1)
  4834. (skip-chars-forward " \t")
  4835. (setq ind (current-column))
  4836. (if (catch 'exit
  4837. (while t
  4838. (beginning-of-line 0)
  4839. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4840. (if (looking-at "[ \t]*$")
  4841. (setq ind1 ind-empty)
  4842. (skip-chars-forward " \t")
  4843. (setq ind1 (current-column)))
  4844. (if (< ind1 ind)
  4845. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4846. nil
  4847. (goto-char pos)
  4848. (error "Not in an item")))))
  4849. (defun org-end-of-item ()
  4850. "Go to the end of the current hand-formatted item.
  4851. If the cursor is not in an item, throw an error."
  4852. (interactive)
  4853. (let* ((pos (point))
  4854. ind1
  4855. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4856. (limit (save-excursion (outline-next-heading) (point)))
  4857. (ind (save-excursion
  4858. (org-beginning-of-item)
  4859. (skip-chars-forward " \t")
  4860. (current-column)))
  4861. (end (catch 'exit
  4862. (while t
  4863. (beginning-of-line 2)
  4864. (if (eobp) (throw 'exit (point)))
  4865. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  4866. (if (looking-at "[ \t]*$")
  4867. (setq ind1 ind-empty)
  4868. (skip-chars-forward " \t")
  4869. (setq ind1 (current-column)))
  4870. (if (<= ind1 ind)
  4871. (throw 'exit (point-at-bol)))))))
  4872. (if end
  4873. (goto-char end)
  4874. (goto-char pos)
  4875. (error "Not in an item"))))
  4876. (defun org-next-item ()
  4877. "Move to the beginning of the next item in the current plain list.
  4878. Error if not at a plain list, or if this is the last item in the list."
  4879. (interactive)
  4880. (let (ind ind1 (pos (point)))
  4881. (org-beginning-of-item)
  4882. (setq ind (org-get-indentation))
  4883. (org-end-of-item)
  4884. (setq ind1 (org-get-indentation))
  4885. (unless (and (org-at-item-p) (= ind ind1))
  4886. (goto-char pos)
  4887. (error "On last item"))))
  4888. (defun org-previous-item ()
  4889. "Move to the beginning of the previous item in the current plain list.
  4890. Error if not at a plain list, or if this is the first item in the list."
  4891. (interactive)
  4892. (let (beg ind ind1 (pos (point)))
  4893. (org-beginning-of-item)
  4894. (setq beg (point))
  4895. (setq ind (org-get-indentation))
  4896. (goto-char beg)
  4897. (catch 'exit
  4898. (while t
  4899. (beginning-of-line 0)
  4900. (if (looking-at "[ \t]*$")
  4901. nil
  4902. (if (<= (setq ind1 (org-get-indentation)) ind)
  4903. (throw 'exit t)))))
  4904. (condition-case nil
  4905. (if (or (not (org-at-item-p))
  4906. (< ind1 (1- ind)))
  4907. (error "")
  4908. (org-beginning-of-item))
  4909. (error (goto-char pos)
  4910. (error "On first item")))))
  4911. (defun org-first-list-item-p ()
  4912. "Is this heading the item in a plain list?"
  4913. (unless (org-at-item-p)
  4914. (error "Not at a plain list item"))
  4915. (org-beginning-of-item)
  4916. (= (point) (save-excursion (org-beginning-of-item-list))))
  4917. (defun org-move-item-down ()
  4918. "Move the plain list item at point down, i.e. swap with following item.
  4919. Subitems (items with larger indentation) are considered part of the item,
  4920. so this really moves item trees."
  4921. (interactive)
  4922. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  4923. (org-beginning-of-item)
  4924. (setq beg0 (point))
  4925. (save-excursion
  4926. (setq ne-beg (org-back-over-empty-lines))
  4927. (setq beg (point)))
  4928. (goto-char beg0)
  4929. (setq ind (org-get-indentation))
  4930. (org-end-of-item)
  4931. (setq end0 (point))
  4932. (setq ind1 (org-get-indentation))
  4933. (setq ne-end (org-back-over-empty-lines))
  4934. (setq end (point))
  4935. (goto-char beg0)
  4936. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  4937. ;; include less whitespace
  4938. (save-excursion
  4939. (goto-char beg)
  4940. (forward-line (- ne-beg ne-end))
  4941. (setq beg (point))))
  4942. (goto-char end0)
  4943. (if (and (org-at-item-p) (= ind ind1))
  4944. (progn
  4945. (org-end-of-item)
  4946. (org-back-over-empty-lines)
  4947. (setq txt (buffer-substring beg end))
  4948. (save-excursion
  4949. (delete-region beg end))
  4950. (setq pos (point))
  4951. (insert txt)
  4952. (goto-char pos) (org-skip-whitespace)
  4953. (org-maybe-renumber-ordered-list))
  4954. (goto-char pos)
  4955. (error "Cannot move this item further down"))))
  4956. (defun org-move-item-up (arg)
  4957. "Move the plain list item at point up, i.e. swap with previous item.
  4958. Subitems (items with larger indentation) are considered part of the item,
  4959. so this really moves item trees."
  4960. (interactive "p")
  4961. (let (beg beg0 end ind ind1 (pos (point)) txt
  4962. ne-beg ne-ins ins-end)
  4963. (org-beginning-of-item)
  4964. (setq beg0 (point))
  4965. (setq ind (org-get-indentation))
  4966. (save-excursion
  4967. (setq ne-beg (org-back-over-empty-lines))
  4968. (setq beg (point)))
  4969. (goto-char beg0)
  4970. (org-end-of-item)
  4971. (setq end (point))
  4972. (goto-char beg0)
  4973. (catch 'exit
  4974. (while t
  4975. (beginning-of-line 0)
  4976. (if (looking-at "[ \t]*$")
  4977. (if org-empty-line-terminates-plain-lists
  4978. (progn
  4979. (goto-char pos)
  4980. (error "Cannot move this item further up"))
  4981. nil)
  4982. (if (<= (setq ind1 (org-get-indentation)) ind)
  4983. (throw 'exit t)))))
  4984. (condition-case nil
  4985. (org-beginning-of-item)
  4986. (error (goto-char beg)
  4987. (error "Cannot move this item further up")))
  4988. (setq ind1 (org-get-indentation))
  4989. (if (and (org-at-item-p) (= ind ind1))
  4990. (progn
  4991. (setq ne-ins (org-back-over-empty-lines))
  4992. (setq txt (buffer-substring beg end))
  4993. (save-excursion
  4994. (delete-region beg end))
  4995. (setq pos (point))
  4996. (insert txt)
  4997. (setq ins-end (point))
  4998. (goto-char pos) (org-skip-whitespace)
  4999. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5000. ;; Move whitespace back to beginning
  5001. (save-excursion
  5002. (goto-char ins-end)
  5003. (let ((kill-whole-line t))
  5004. (kill-line (- ne-ins ne-beg)) (point)))
  5005. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5006. (org-maybe-renumber-ordered-list))
  5007. (goto-char pos)
  5008. (error "Cannot move this item further up"))))
  5009. (defun org-maybe-renumber-ordered-list ()
  5010. "Renumber the ordered list at point if setup allows it.
  5011. This tests the user option `org-auto-renumber-ordered-lists' before
  5012. doing the renumbering."
  5013. (interactive)
  5014. (when (and org-auto-renumber-ordered-lists
  5015. (org-at-item-p))
  5016. (if (match-beginning 3)
  5017. (org-renumber-ordered-list 1)
  5018. (org-fix-bullet-type))))
  5019. (defun org-maybe-renumber-ordered-list-safe ()
  5020. (condition-case nil
  5021. (save-excursion
  5022. (org-maybe-renumber-ordered-list))
  5023. (error nil)))
  5024. (defun org-cycle-list-bullet (&optional which)
  5025. "Cycle through the different itemize/enumerate bullets.
  5026. This cycle the entire list level through the sequence:
  5027. `-' -> `+' -> `*' -> `1.' -> `1)'
  5028. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5029. 0 meand `-', 1 means `+' etc."
  5030. (interactive "P")
  5031. (org-preserve-lc
  5032. (org-beginning-of-item-list)
  5033. (org-at-item-p)
  5034. (beginning-of-line 1)
  5035. (let ((current (match-string 0))
  5036. (prevp (eq which 'previous))
  5037. new)
  5038. (setq new (cond
  5039. ((and (numberp which)
  5040. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5041. ((string-match "-" current) (if prevp "1)" "+"))
  5042. ((string-match "\\+" current)
  5043. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5044. ((string-match "\\*" current) (if prevp "+" "1."))
  5045. ((string-match "\\." current) (if prevp "*" "1)"))
  5046. ((string-match ")" current) (if prevp "1." "-"))
  5047. (t (error "This should not happen"))))
  5048. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5049. (org-fix-bullet-type)
  5050. (org-maybe-renumber-ordered-list))))
  5051. (defun org-get-string-indentation (s)
  5052. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5053. (let ((n -1) (i 0) (w tab-width) c)
  5054. (catch 'exit
  5055. (while (< (setq n (1+ n)) (length s))
  5056. (setq c (aref s n))
  5057. (cond ((= c ?\ ) (setq i (1+ i)))
  5058. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5059. (t (throw 'exit t)))))
  5060. i))
  5061. (defun org-renumber-ordered-list (arg)
  5062. "Renumber an ordered plain list.
  5063. Cursor needs to be in the first line of an item, the line that starts
  5064. with something like \"1.\" or \"2)\"."
  5065. (interactive "p")
  5066. (unless (and (org-at-item-p)
  5067. (match-beginning 3))
  5068. (error "This is not an ordered list"))
  5069. (let ((line (org-current-line))
  5070. (col (current-column))
  5071. (ind (org-get-string-indentation
  5072. (buffer-substring (point-at-bol) (match-beginning 3))))
  5073. ;; (term (substring (match-string 3) -1))
  5074. ind1 (n (1- arg))
  5075. fmt)
  5076. ;; find where this list begins
  5077. (org-beginning-of-item-list)
  5078. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5079. (setq fmt (concat "%d" (match-string 1)))
  5080. (beginning-of-line 0)
  5081. ;; walk forward and replace these numbers
  5082. (catch 'exit
  5083. (while t
  5084. (catch 'next
  5085. (beginning-of-line 2)
  5086. (if (eobp) (throw 'exit nil))
  5087. (if (looking-at "[ \t]*$") (throw 'next nil))
  5088. (skip-chars-forward " \t") (setq ind1 (current-column))
  5089. (if (> ind1 ind) (throw 'next t))
  5090. (if (< ind1 ind) (throw 'exit t))
  5091. (if (not (org-at-item-p)) (throw 'exit nil))
  5092. (delete-region (match-beginning 2) (match-end 2))
  5093. (goto-char (match-beginning 2))
  5094. (insert (format fmt (setq n (1+ n)))))))
  5095. (goto-line line)
  5096. (move-to-column col)))
  5097. (defun org-fix-bullet-type ()
  5098. "Make sure all items in this list have the same bullet as the firsst item."
  5099. (interactive)
  5100. (unless (org-at-item-p) (error "This is not a list"))
  5101. (let ((line (org-current-line))
  5102. (col (current-column))
  5103. (ind (current-indentation))
  5104. ind1 bullet)
  5105. ;; find where this list begins
  5106. (org-beginning-of-item-list)
  5107. (beginning-of-line 1)
  5108. ;; find out what the bullet type is
  5109. (looking-at "[ \t]*\\(\\S-+\\)")
  5110. (setq bullet (match-string 1))
  5111. ;; walk forward and replace these numbers
  5112. (beginning-of-line 0)
  5113. (catch 'exit
  5114. (while t
  5115. (catch 'next
  5116. (beginning-of-line 2)
  5117. (if (eobp) (throw 'exit nil))
  5118. (if (looking-at "[ \t]*$") (throw 'next nil))
  5119. (skip-chars-forward " \t") (setq ind1 (current-column))
  5120. (if (> ind1 ind) (throw 'next t))
  5121. (if (< ind1 ind) (throw 'exit t))
  5122. (if (not (org-at-item-p)) (throw 'exit nil))
  5123. (skip-chars-forward " \t")
  5124. (looking-at "\\S-+")
  5125. (replace-match bullet))))
  5126. (goto-line line)
  5127. (move-to-column col)
  5128. (if (string-match "[0-9]" bullet)
  5129. (org-renumber-ordered-list 1))))
  5130. (defun org-beginning-of-item-list ()
  5131. "Go to the beginning of the current item list.
  5132. I.e. to the first item in this list."
  5133. (interactive)
  5134. (org-beginning-of-item)
  5135. (let ((pos (point-at-bol))
  5136. (ind (org-get-indentation))
  5137. ind1)
  5138. ;; find where this list begins
  5139. (catch 'exit
  5140. (while t
  5141. (catch 'next
  5142. (beginning-of-line 0)
  5143. (if (looking-at "[ \t]*$")
  5144. (throw (if (bobp) 'exit 'next) t))
  5145. (skip-chars-forward " \t") (setq ind1 (current-column))
  5146. (if (or (< ind1 ind)
  5147. (and (= ind1 ind)
  5148. (not (org-at-item-p)))
  5149. (bobp))
  5150. (throw 'exit t)
  5151. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5152. (goto-char pos)))
  5153. (defun org-end-of-item-list ()
  5154. "Go to the end of the current item list.
  5155. I.e. to the text after the last item."
  5156. (interactive)
  5157. (org-beginning-of-item)
  5158. (let ((pos (point-at-bol))
  5159. (ind (org-get-indentation))
  5160. ind1)
  5161. ;; find where this list begins
  5162. (catch 'exit
  5163. (while t
  5164. (catch 'next
  5165. (beginning-of-line 2)
  5166. (if (looking-at "[ \t]*$")
  5167. (throw (if (eobp) 'exit 'next) t))
  5168. (skip-chars-forward " \t") (setq ind1 (current-column))
  5169. (if (or (< ind1 ind)
  5170. (and (= ind1 ind)
  5171. (not (org-at-item-p)))
  5172. (eobp))
  5173. (progn
  5174. (setq pos (point-at-bol))
  5175. (throw 'exit t))))))
  5176. (goto-char pos)))
  5177. (defvar org-last-indent-begin-marker (make-marker))
  5178. (defvar org-last-indent-end-marker (make-marker))
  5179. (defun org-outdent-item (arg)
  5180. "Outdent a local list item."
  5181. (interactive "p")
  5182. (org-indent-item (- arg)))
  5183. (defun org-indent-item (arg)
  5184. "Indent a local list item."
  5185. (interactive "p")
  5186. (unless (org-at-item-p)
  5187. (error "Not on an item"))
  5188. (save-excursion
  5189. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5190. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5191. (setq beg org-last-indent-begin-marker
  5192. end org-last-indent-end-marker)
  5193. (org-beginning-of-item)
  5194. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5195. (org-end-of-item)
  5196. (setq end (move-marker org-last-indent-end-marker (point))))
  5197. (goto-char beg)
  5198. (setq tmp (org-item-indent-positions)
  5199. ind (car tmp)
  5200. ind-down (nth 2 tmp)
  5201. ind-up (nth 1 tmp)
  5202. delta (if (> arg 0)
  5203. (if ind-down (- ind-down ind) 2)
  5204. (if ind-up (- ind-up ind) -2)))
  5205. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5206. (while (< (point) end)
  5207. (beginning-of-line 1)
  5208. (skip-chars-forward " \t") (setq ind1 (current-column))
  5209. (delete-region (point-at-bol) (point))
  5210. (or (eolp) (indent-to-column (+ ind1 delta)))
  5211. (beginning-of-line 2))))
  5212. (org-fix-bullet-type)
  5213. (org-maybe-renumber-ordered-list-safe)
  5214. (save-excursion
  5215. (beginning-of-line 0)
  5216. (condition-case nil (org-beginning-of-item) (error nil))
  5217. (org-maybe-renumber-ordered-list-safe)))
  5218. (defun org-item-indent-positions ()
  5219. "Return indentation for plain list items.
  5220. This returns a list with three values: The current indentation, the
  5221. parent indentation and the indentation a child should habe.
  5222. Assumes cursor in item line."
  5223. (let* ((bolpos (point-at-bol))
  5224. (ind (org-get-indentation))
  5225. ind-down ind-up pos)
  5226. (save-excursion
  5227. (org-beginning-of-item-list)
  5228. (skip-chars-backward "\n\r \t")
  5229. (when (org-in-item-p)
  5230. (org-beginning-of-item)
  5231. (setq ind-up (org-get-indentation))))
  5232. (setq pos (point))
  5233. (save-excursion
  5234. (cond
  5235. ((and (condition-case nil (progn (org-previous-item) t)
  5236. (error nil))
  5237. (or (forward-char 1) t)
  5238. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5239. (setq ind-down (org-get-indentation)))
  5240. ((and (goto-char pos)
  5241. (org-at-item-p))
  5242. (goto-char (match-end 0))
  5243. (skip-chars-forward " \t")
  5244. (setq ind-down (current-column)))))
  5245. (list ind ind-up ind-down)))
  5246. ;;; The orgstruct minor mode
  5247. ;; Define a minor mode which can be used in other modes in order to
  5248. ;; integrate the org-mode structure editing commands.
  5249. ;; This is really a hack, because the org-mode structure commands use
  5250. ;; keys which normally belong to the major mode. Here is how it
  5251. ;; works: The minor mode defines all the keys necessary to operate the
  5252. ;; structure commands, but wraps the commands into a function which
  5253. ;; tests if the cursor is currently at a headline or a plain list
  5254. ;; item. If that is the case, the structure command is used,
  5255. ;; temporarily setting many Org-mode variables like regular
  5256. ;; expressions for filling etc. However, when any of those keys is
  5257. ;; used at a different location, function uses `key-binding' to look
  5258. ;; up if the key has an associated command in another currently active
  5259. ;; keymap (minor modes, major mode, global), and executes that
  5260. ;; command. There might be problems if any of the keys is otherwise
  5261. ;; used as a prefix key.
  5262. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5263. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5264. ;; addresses this by checking explicitly for both bindings.
  5265. (defvar orgstruct-mode-map (make-sparse-keymap)
  5266. "Keymap for the minor `orgstruct-mode'.")
  5267. (defvar org-local-vars nil
  5268. "List of local variables, for use by `orgstruct-mode'")
  5269. ;;;###autoload
  5270. (define-minor-mode orgstruct-mode
  5271. "Toggle the minor more `orgstruct-mode'.
  5272. This mode is for using Org-mode structure commands in other modes.
  5273. The following key behave as if Org-mode was active, if the cursor
  5274. is on a headline, or on a plain list item (both in the definition
  5275. of Org-mode).
  5276. M-up Move entry/item up
  5277. M-down Move entry/item down
  5278. M-left Promote
  5279. M-right Demote
  5280. M-S-up Move entry/item up
  5281. M-S-down Move entry/item down
  5282. M-S-left Promote subtree
  5283. M-S-right Demote subtree
  5284. M-q Fill paragraph and items like in Org-mode
  5285. C-c ^ Sort entries
  5286. C-c - Cycle list bullet
  5287. TAB Cycle item visibility
  5288. M-RET Insert new heading/item
  5289. S-M-RET Insert new TODO heading / Chekbox item
  5290. C-c C-c Set tags / toggle checkbox"
  5291. nil " OrgStruct" nil
  5292. (org-load-modules-maybe)
  5293. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5294. ;;;###autoload
  5295. (defun turn-on-orgstruct ()
  5296. "Unconditionally turn on `orgstruct-mode'."
  5297. (orgstruct-mode 1))
  5298. ;;;###autoload
  5299. (defun turn-on-orgstruct++ ()
  5300. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5301. In addition to setting orgstruct-mode, this also exports all indentation and
  5302. autofilling variables from org-mode into the buffer. Note that turning
  5303. off orgstruct-mode will *not* remove these additional settings."
  5304. (orgstruct-mode 1)
  5305. (let (var val)
  5306. (mapc
  5307. (lambda (x)
  5308. (when (string-match
  5309. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5310. (symbol-name (car x)))
  5311. (setq var (car x) val (nth 1 x))
  5312. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5313. org-local-vars)))
  5314. (defun orgstruct-error ()
  5315. "Error when there is no default binding for a structure key."
  5316. (interactive)
  5317. (error "This key has no function outside structure elements"))
  5318. (defun orgstruct-setup ()
  5319. "Setup orgstruct keymaps."
  5320. (let ((nfunc 0)
  5321. (bindings
  5322. (list
  5323. '([(meta up)] org-metaup)
  5324. '([(meta down)] org-metadown)
  5325. '([(meta left)] org-metaleft)
  5326. '([(meta right)] org-metaright)
  5327. '([(meta shift up)] org-shiftmetaup)
  5328. '([(meta shift down)] org-shiftmetadown)
  5329. '([(meta shift left)] org-shiftmetaleft)
  5330. '([(meta shift right)] org-shiftmetaright)
  5331. '([(shift up)] org-shiftup)
  5332. '([(shift down)] org-shiftdown)
  5333. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5334. '("\M-q" fill-paragraph)
  5335. '("\C-c^" org-sort)
  5336. '("\C-c-" org-cycle-list-bullet)))
  5337. elt key fun cmd)
  5338. (while (setq elt (pop bindings))
  5339. (setq nfunc (1+ nfunc))
  5340. (setq key (org-key (car elt))
  5341. fun (nth 1 elt)
  5342. cmd (orgstruct-make-binding fun nfunc key))
  5343. (org-defkey orgstruct-mode-map key cmd))
  5344. ;; Special treatment needed for TAB and RET
  5345. (org-defkey orgstruct-mode-map [(tab)]
  5346. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5347. (org-defkey orgstruct-mode-map "\C-i"
  5348. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5349. (org-defkey orgstruct-mode-map "\M-\C-m"
  5350. (orgstruct-make-binding 'org-insert-heading 105
  5351. "\M-\C-m" [(meta return)]))
  5352. (org-defkey orgstruct-mode-map [(meta return)]
  5353. (orgstruct-make-binding 'org-insert-heading 106
  5354. [(meta return)] "\M-\C-m"))
  5355. (org-defkey orgstruct-mode-map [(shift meta return)]
  5356. (orgstruct-make-binding 'org-insert-todo-heading 107
  5357. [(meta return)] "\M-\C-m"))
  5358. (unless org-local-vars
  5359. (setq org-local-vars (org-get-local-variables)))
  5360. t))
  5361. (defun orgstruct-make-binding (fun n &rest keys)
  5362. "Create a function for binding in the structure minor mode.
  5363. FUN is the command to call inside a table. N is used to create a unique
  5364. command name. KEYS are keys that should be checked in for a command
  5365. to execute outside of tables."
  5366. (eval
  5367. (list 'defun
  5368. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5369. '(arg)
  5370. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5371. "Outside of structure, run the binding of `"
  5372. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5373. "'.")
  5374. '(interactive "p")
  5375. (list 'if
  5376. '(org-context-p 'headline 'item)
  5377. (list 'org-run-like-in-org-mode (list 'quote fun))
  5378. (list 'let '(orgstruct-mode)
  5379. (list 'call-interactively
  5380. (append '(or)
  5381. (mapcar (lambda (k)
  5382. (list 'key-binding k))
  5383. keys)
  5384. '('orgstruct-error))))))))
  5385. (defun org-context-p (&rest contexts)
  5386. "Check if local context is and of CONTEXTS.
  5387. Possible values in the list of contexts are `table', `headline', and `item'."
  5388. (let ((pos (point)))
  5389. (goto-char (point-at-bol))
  5390. (prog1 (or (and (memq 'table contexts)
  5391. (looking-at "[ \t]*|"))
  5392. (and (memq 'headline contexts)
  5393. (looking-at "\\*+"))
  5394. (and (memq 'item contexts)
  5395. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5396. (goto-char pos))))
  5397. (defun org-get-local-variables ()
  5398. "Return a list of all local variables in an org-mode buffer."
  5399. (let (varlist)
  5400. (with-current-buffer (get-buffer-create "*Org tmp*")
  5401. (erase-buffer)
  5402. (org-mode)
  5403. (setq varlist (buffer-local-variables)))
  5404. (kill-buffer "*Org tmp*")
  5405. (delq nil
  5406. (mapcar
  5407. (lambda (x)
  5408. (setq x
  5409. (if (symbolp x)
  5410. (list x)
  5411. (list (car x) (list 'quote (cdr x)))))
  5412. (if (string-match
  5413. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5414. (symbol-name (car x)))
  5415. x nil))
  5416. varlist))))
  5417. ;;;###autoload
  5418. (defun org-run-like-in-org-mode (cmd)
  5419. (org-load-modules-maybe)
  5420. (unless org-local-vars
  5421. (setq org-local-vars (org-get-local-variables)))
  5422. (eval (list 'let org-local-vars
  5423. (list 'call-interactively (list 'quote cmd)))))
  5424. ;;;; Archiving
  5425. (defun org-get-category (&optional pos)
  5426. "Get the category applying to position POS."
  5427. (get-text-property (or pos (point)) 'org-category))
  5428. (defun org-refresh-category-properties ()
  5429. "Refresh category text properties in the buffer."
  5430. (let ((def-cat (cond
  5431. ((null org-category)
  5432. (if buffer-file-name
  5433. (file-name-sans-extension
  5434. (file-name-nondirectory buffer-file-name))
  5435. "???"))
  5436. ((symbolp org-category) (symbol-name org-category))
  5437. (t org-category)))
  5438. beg end cat pos optionp)
  5439. (org-unmodified
  5440. (save-excursion
  5441. (save-restriction
  5442. (widen)
  5443. (goto-char (point-min))
  5444. (put-text-property (point) (point-max) 'org-category def-cat)
  5445. (while (re-search-forward
  5446. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5447. (setq pos (match-end 0)
  5448. optionp (equal (char-after (match-beginning 0)) ?#)
  5449. cat (org-trim (match-string 2)))
  5450. (if optionp
  5451. (setq beg (point-at-bol) end (point-max))
  5452. (org-back-to-heading t)
  5453. (setq beg (point) end (org-end-of-subtree t t)))
  5454. (put-text-property beg end 'org-category cat)
  5455. (goto-char pos)))))))
  5456. ;;;; Link Stuff
  5457. ;;; Link abbreviations
  5458. (defun org-link-expand-abbrev (link)
  5459. "Apply replacements as defined in `org-link-abbrev-alist."
  5460. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5461. (let* ((key (match-string 1 link))
  5462. (as (or (assoc key org-link-abbrev-alist-local)
  5463. (assoc key org-link-abbrev-alist)))
  5464. (tag (and (match-end 2) (match-string 3 link)))
  5465. rpl)
  5466. (if (not as)
  5467. link
  5468. (setq rpl (cdr as))
  5469. (cond
  5470. ((symbolp rpl) (funcall rpl tag))
  5471. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5472. (t (concat rpl tag)))))
  5473. link))
  5474. ;;; Storing and inserting links
  5475. (defvar org-insert-link-history nil
  5476. "Minibuffer history for links inserted with `org-insert-link'.")
  5477. (defvar org-stored-links nil
  5478. "Contains the links stored with `org-store-link'.")
  5479. (defvar org-store-link-plist nil
  5480. "Plist with info about the most recently link created with `org-store-link'.")
  5481. (defvar org-link-protocols nil
  5482. "Link protocols added to Org-mode using `org-add-link-type'.")
  5483. (defvar org-store-link-functions nil
  5484. "List of functions that are called to create and store a link.
  5485. Each function will be called in turn until one returns a non-nil
  5486. value. Each function should check if it is responsible for creating
  5487. this link (for example by looking at the major mode).
  5488. If not, it must exit and return nil.
  5489. If yes, it should return a non-nil value after a calling
  5490. `org-store-link-props' with a list of properties and values.
  5491. Special properties are:
  5492. :type The link prefix. like \"http\". This must be given.
  5493. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5494. This is obligatory as well.
  5495. :description Optional default description for the second pair
  5496. of brackets in an Org-mode link. The user can still change
  5497. this when inserting this link into an Org-mode buffer.
  5498. In addition to these, any additional properties can be specified
  5499. and then used in remember templates.")
  5500. (defun org-add-link-type (type &optional follow export)
  5501. "Add TYPE to the list of `org-link-types'.
  5502. Re-compute all regular expressions depending on `org-link-types'
  5503. FOLLOW and EXPORT are two functions.
  5504. FOLLOW should take the link path as the single argument and do whatever
  5505. is necessary to follow the link, for example find a file or display
  5506. a mail message.
  5507. EXPORT should format the link path for export to one of the export formats.
  5508. It should be a function accepting three arguments:
  5509. path the path of the link, the text after the prefix (like \"http:\")
  5510. desc the description of the link, if any, nil if there was no descripton
  5511. format the export format, a symbol like `html' or `latex'.
  5512. The function may use the FORMAT information to return different values
  5513. depending on the format. The return value will be put literally into
  5514. the exported file.
  5515. Org-mode has a built-in default for exporting links. If you are happy with
  5516. this default, there is no need to define an export function for the link
  5517. type. For a simple example of an export function, see `org-bbdb.el'."
  5518. (add-to-list 'org-link-types type t)
  5519. (org-make-link-regexps)
  5520. (if (assoc type org-link-protocols)
  5521. (setcdr (assoc type org-link-protocols) (list follow export))
  5522. (push (list type follow export) org-link-protocols)))
  5523. ;;;###autoload
  5524. (defun org-store-link (arg)
  5525. "\\<org-mode-map>Store an org-link to the current location.
  5526. This link is added to `org-stored-links' and can later be inserted
  5527. into an org-buffer with \\[org-insert-link].
  5528. For some link types, a prefix arg is interpreted:
  5529. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5530. For file links, arg negates `org-context-in-file-links'."
  5531. (interactive "P")
  5532. (org-load-modules-maybe)
  5533. (setq org-store-link-plist nil) ; reset
  5534. (let (link cpltxt desc description search txt)
  5535. (cond
  5536. ((run-hook-with-args-until-success 'org-store-link-functions)
  5537. (setq link (plist-get org-store-link-plist :link)
  5538. desc (or (plist-get org-store-link-plist :description) link)))
  5539. ((eq major-mode 'calendar-mode)
  5540. (let ((cd (calendar-cursor-to-date)))
  5541. (setq link
  5542. (format-time-string
  5543. (car org-time-stamp-formats)
  5544. (apply 'encode-time
  5545. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5546. nil nil nil))))
  5547. (org-store-link-props :type "calendar" :date cd)))
  5548. ((eq major-mode 'w3-mode)
  5549. (setq cpltxt (url-view-url t)
  5550. link (org-make-link cpltxt))
  5551. (org-store-link-props :type "w3" :url (url-view-url t)))
  5552. ((eq major-mode 'w3m-mode)
  5553. (setq cpltxt (or w3m-current-title w3m-current-url)
  5554. link (org-make-link w3m-current-url))
  5555. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5556. ((setq search (run-hook-with-args-until-success
  5557. 'org-create-file-search-functions))
  5558. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5559. "::" search))
  5560. (setq cpltxt (or description link)))
  5561. ((eq major-mode 'image-mode)
  5562. (setq cpltxt (concat "file:"
  5563. (abbreviate-file-name buffer-file-name))
  5564. link (org-make-link cpltxt))
  5565. (org-store-link-props :type "image" :file buffer-file-name))
  5566. ((eq major-mode 'dired-mode)
  5567. ;; link to the file in the current line
  5568. (setq cpltxt (concat "file:"
  5569. (abbreviate-file-name
  5570. (expand-file-name
  5571. (dired-get-filename nil t))))
  5572. link (org-make-link cpltxt)))
  5573. ((and buffer-file-name (org-mode-p))
  5574. ;; Just link to current headline
  5575. (setq cpltxt (concat "file:"
  5576. (abbreviate-file-name buffer-file-name)))
  5577. ;; Add a context search string
  5578. (when (org-xor org-context-in-file-links arg)
  5579. ;; Check if we are on a target
  5580. (if (org-in-regexp "<<\\(.*?\\)>>")
  5581. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5582. (setq txt (cond
  5583. ((org-on-heading-p) nil)
  5584. ((org-region-active-p)
  5585. (buffer-substring (region-beginning) (region-end)))
  5586. (t nil)))
  5587. (when (or (null txt) (string-match "\\S-" txt))
  5588. (setq cpltxt
  5589. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5590. desc "NONE"))))
  5591. (if (string-match "::\\'" cpltxt)
  5592. (setq cpltxt (substring cpltxt 0 -2)))
  5593. (setq link (org-make-link cpltxt)))
  5594. ((buffer-file-name (buffer-base-buffer))
  5595. ;; Just link to this file here.
  5596. (setq cpltxt (concat "file:"
  5597. (abbreviate-file-name
  5598. (buffer-file-name (buffer-base-buffer)))))
  5599. ;; Add a context string
  5600. (when (org-xor org-context-in-file-links arg)
  5601. (setq txt (if (org-region-active-p)
  5602. (buffer-substring (region-beginning) (region-end))
  5603. (buffer-substring (point-at-bol) (point-at-eol))))
  5604. ;; Only use search option if there is some text.
  5605. (when (string-match "\\S-" txt)
  5606. (setq cpltxt
  5607. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5608. desc "NONE")))
  5609. (setq link (org-make-link cpltxt)))
  5610. ((interactive-p)
  5611. (error "Cannot link to a buffer which is not visiting a file"))
  5612. (t (setq link nil)))
  5613. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5614. (setq link (or link cpltxt)
  5615. desc (or desc cpltxt))
  5616. (if (equal desc "NONE") (setq desc nil))
  5617. (if (and (interactive-p) link)
  5618. (progn
  5619. (setq org-stored-links
  5620. (cons (list link desc) org-stored-links))
  5621. (message "Stored: %s" (or desc link)))
  5622. (and link (org-make-link-string link desc)))))
  5623. (defun org-store-link-props (&rest plist)
  5624. "Store link properties, extract names and addresses."
  5625. (let (x adr)
  5626. (when (setq x (plist-get plist :from))
  5627. (setq adr (mail-extract-address-components x))
  5628. (plist-put plist :fromname (car adr))
  5629. (plist-put plist :fromaddress (nth 1 adr)))
  5630. (when (setq x (plist-get plist :to))
  5631. (setq adr (mail-extract-address-components x))
  5632. (plist-put plist :toname (car adr))
  5633. (plist-put plist :toaddress (nth 1 adr))))
  5634. (let ((from (plist-get plist :from))
  5635. (to (plist-get plist :to)))
  5636. (when (and from to org-from-is-user-regexp)
  5637. (plist-put plist :fromto
  5638. (if (string-match org-from-is-user-regexp from)
  5639. (concat "to %t")
  5640. (concat "from %f")))))
  5641. (setq org-store-link-plist plist))
  5642. (defun org-add-link-props (&rest plist)
  5643. "Add these properties to the link property list."
  5644. (let (key value)
  5645. (while plist
  5646. (setq key (pop plist) value (pop plist))
  5647. (setq org-store-link-plist
  5648. (plist-put org-store-link-plist key value)))))
  5649. (defun org-email-link-description (&optional fmt)
  5650. "Return the description part of an email link.
  5651. This takes information from `org-store-link-plist' and formats it
  5652. according to FMT (default from `org-email-link-description-format')."
  5653. (setq fmt (or fmt org-email-link-description-format))
  5654. (let* ((p org-store-link-plist)
  5655. (to (plist-get p :toaddress))
  5656. (from (plist-get p :fromaddress))
  5657. (table
  5658. (list
  5659. (cons "%c" (plist-get p :fromto))
  5660. (cons "%F" (plist-get p :from))
  5661. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5662. (cons "%T" (plist-get p :to))
  5663. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5664. (cons "%s" (plist-get p :subject))
  5665. (cons "%m" (plist-get p :message-id)))))
  5666. (when (string-match "%c" fmt)
  5667. ;; Check if the user wrote this message
  5668. (if (and org-from-is-user-regexp from to
  5669. (save-match-data (string-match org-from-is-user-regexp from)))
  5670. (setq fmt (replace-match "to %t" t t fmt))
  5671. (setq fmt (replace-match "from %f" t t fmt))))
  5672. (org-replace-escapes fmt table)))
  5673. (defun org-make-org-heading-search-string (&optional string heading)
  5674. "Make search string for STRING or current headline."
  5675. (interactive)
  5676. (let ((s (or string (org-get-heading))))
  5677. (unless (and string (not heading))
  5678. ;; We are using a headline, clean up garbage in there.
  5679. (if (string-match org-todo-regexp s)
  5680. (setq s (replace-match "" t t s)))
  5681. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5682. (setq s (replace-match "" t t s)))
  5683. (setq s (org-trim s))
  5684. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5685. org-comment-string "\\)") s)
  5686. (setq s (replace-match "" t t s)))
  5687. (while (string-match org-ts-regexp s)
  5688. (setq s (replace-match "" t t s))))
  5689. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5690. (setq s (replace-match " " t t s)))
  5691. (or string (setq s (concat "*" s))) ; Add * for headlines
  5692. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5693. (defun org-make-link (&rest strings)
  5694. "Concatenate STRINGS."
  5695. (apply 'concat strings))
  5696. (defun org-make-link-string (link &optional description)
  5697. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5698. (unless (string-match "\\S-" link)
  5699. (error "Empty link"))
  5700. (when (stringp description)
  5701. ;; Remove brackets from the description, they are fatal.
  5702. (while (string-match "\\[" description)
  5703. (setq description (replace-match "{" t t description)))
  5704. (while (string-match "\\]" description)
  5705. (setq description (replace-match "}" t t description))))
  5706. (when (equal (org-link-escape link) description)
  5707. ;; No description needed, it is identical
  5708. (setq description nil))
  5709. (when (and (not description)
  5710. (not (equal link (org-link-escape link))))
  5711. (setq description link))
  5712. (concat "[[" (org-link-escape link) "]"
  5713. (if description (concat "[" description "]") "")
  5714. "]"))
  5715. (defconst org-link-escape-chars
  5716. '((?\ . "%20")
  5717. (?\[ . "%5B")
  5718. (?\] . "%5D")
  5719. (?\340 . "%E0") ; `a
  5720. (?\342 . "%E2") ; ^a
  5721. (?\347 . "%E7") ; ,c
  5722. (?\350 . "%E8") ; `e
  5723. (?\351 . "%E9") ; 'e
  5724. (?\352 . "%EA") ; ^e
  5725. (?\356 . "%EE") ; ^i
  5726. (?\364 . "%F4") ; ^o
  5727. (?\371 . "%F9") ; `u
  5728. (?\373 . "%FB") ; ^u
  5729. (?\; . "%3B")
  5730. (?? . "%3F")
  5731. (?= . "%3D")
  5732. (?+ . "%2B")
  5733. )
  5734. "Association list of escapes for some characters problematic in links.
  5735. This is the list that is used for internal purposes.")
  5736. (defconst org-link-escape-chars-browser
  5737. '((?\ . "%20")) ; 32 for the SPC char
  5738. "Association list of escapes for some characters problematic in links.
  5739. This is the list that is used before handing over to the browser.")
  5740. (defun org-link-escape (text &optional table)
  5741. "Escape charaters in TEXT that are problematic for links."
  5742. (setq table (or table org-link-escape-chars))
  5743. (when text
  5744. (let ((re (mapconcat (lambda (x) (regexp-quote
  5745. (char-to-string (car x))))
  5746. table "\\|")))
  5747. (while (string-match re text)
  5748. (setq text
  5749. (replace-match
  5750. (cdr (assoc (string-to-char (match-string 0 text))
  5751. table))
  5752. t t text)))
  5753. text)))
  5754. (defun org-link-unescape (text &optional table)
  5755. "Reverse the action of `org-link-escape'."
  5756. (setq table (or table org-link-escape-chars))
  5757. (when text
  5758. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5759. table "\\|")))
  5760. (while (string-match re text)
  5761. (setq text
  5762. (replace-match
  5763. (char-to-string (car (rassoc (match-string 0 text) table)))
  5764. t t text)))
  5765. text)))
  5766. (defun org-xor (a b)
  5767. "Exclusive or."
  5768. (if a (not b) b))
  5769. (defun org-get-header (header)
  5770. "Find a header field in the current buffer."
  5771. (save-excursion
  5772. (goto-char (point-min))
  5773. (let ((case-fold-search t) s)
  5774. (cond
  5775. ((eq header 'from)
  5776. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5777. (setq s (match-string 1)))
  5778. (while (string-match "\"" s)
  5779. (setq s (replace-match "" t t s)))
  5780. (if (string-match "[<(].*" s)
  5781. (setq s (replace-match "" t t s))))
  5782. ((eq header 'message-id)
  5783. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5784. (setq s (match-string 1))))
  5785. ((eq header 'subject)
  5786. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5787. (setq s (match-string 1)))))
  5788. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5789. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5790. s)))
  5791. (defun org-fixup-message-id-for-http (s)
  5792. "Replace special characters in a message id, so it can be used in an http query."
  5793. (while (string-match "<" s)
  5794. (setq s (replace-match "%3C" t t s)))
  5795. (while (string-match ">" s)
  5796. (setq s (replace-match "%3E" t t s)))
  5797. (while (string-match "@" s)
  5798. (setq s (replace-match "%40" t t s)))
  5799. s)
  5800. ;;;###autoload
  5801. (defun org-insert-link-global ()
  5802. "Insert a link like Org-mode does.
  5803. This command can be called in any mode to insert a link in Org-mode syntax."
  5804. (interactive)
  5805. (org-load-modules-maybe)
  5806. (org-run-like-in-org-mode 'org-insert-link))
  5807. (defun org-insert-link (&optional complete-file link-location)
  5808. "Insert a link. At the prompt, enter the link.
  5809. Completion can be used to select a link previously stored with
  5810. `org-store-link'. When the empty string is entered (i.e. if you just
  5811. press RET at the prompt), the link defaults to the most recently
  5812. stored link. As SPC triggers completion in the minibuffer, you need to
  5813. use M-SPC or C-q SPC to force the insertion of a space character.
  5814. You will also be prompted for a description, and if one is given, it will
  5815. be displayed in the buffer instead of the link.
  5816. If there is already a link at point, this command will allow you to edit link
  5817. and description parts.
  5818. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5819. be selected using completion. The path to the file will be relative to the
  5820. current directory if the file is in the current directory or a subdirectory.
  5821. Otherwise, the link will be the absolute path as completed in the minibuffer
  5822. \(i.e. normally ~/path/to/file).
  5823. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5824. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5825. of `org-keep-stored-link-after-insertion'.
  5826. If `org-make-link-description-function' is non-nil, this function will be
  5827. called with the link target, and the result will be the default
  5828. link description.
  5829. If the LINK-LOCATION parameter is non-nil, this value will be
  5830. used as the link location instead of reading one interactively."
  5831. (interactive "P")
  5832. (let* ((wcf (current-window-configuration))
  5833. (region (if (org-region-active-p)
  5834. (buffer-substring (region-beginning) (region-end))))
  5835. (remove (and region (list (region-beginning) (region-end))))
  5836. (desc region)
  5837. tmphist ; byte-compile incorrectly complains about this
  5838. (link link-location)
  5839. entry file)
  5840. (cond
  5841. (link-location) ; specified by arg, just use it.
  5842. ((org-in-regexp org-bracket-link-regexp 1)
  5843. ;; We do have a link at point, and we are going to edit it.
  5844. (setq remove (list (match-beginning 0) (match-end 0)))
  5845. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5846. (setq link (read-string "Link: "
  5847. (org-link-unescape
  5848. (org-match-string-no-properties 1)))))
  5849. ((or (org-in-regexp org-angle-link-re)
  5850. (org-in-regexp org-plain-link-re))
  5851. ;; Convert to bracket link
  5852. (setq remove (list (match-beginning 0) (match-end 0))
  5853. link (read-string "Link: "
  5854. (org-remove-angle-brackets (match-string 0)))))
  5855. ((equal complete-file '(4))
  5856. ;; Completing read for file names.
  5857. (setq file (read-file-name "File: "))
  5858. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5859. (pwd1 (file-name-as-directory (abbreviate-file-name
  5860. (expand-file-name ".")))))
  5861. (cond
  5862. ((equal complete-file '(16))
  5863. (setq link (org-make-link
  5864. "file:"
  5865. (abbreviate-file-name (expand-file-name file)))))
  5866. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5867. (setq link (org-make-link "file:" (match-string 1 file))))
  5868. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5869. (expand-file-name file))
  5870. (setq link (org-make-link
  5871. "file:" (match-string 1 (expand-file-name file)))))
  5872. (t (setq link (org-make-link "file:" file))))))
  5873. (t
  5874. ;; Read link, with completion for stored links.
  5875. (with-output-to-temp-buffer "*Org Links*"
  5876. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5877. (when org-stored-links
  5878. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5879. (princ (mapconcat
  5880. (lambda (x)
  5881. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5882. (reverse org-stored-links) "\n"))))
  5883. (let ((cw (selected-window)))
  5884. (select-window (get-buffer-window "*Org Links*"))
  5885. (shrink-window-if-larger-than-buffer)
  5886. (setq truncate-lines t)
  5887. (select-window cw))
  5888. ;; Fake a link history, containing the stored links.
  5889. (setq tmphist (append (mapcar 'car org-stored-links)
  5890. org-insert-link-history))
  5891. (unwind-protect
  5892. (setq link (org-completing-read
  5893. "Link: "
  5894. (append
  5895. (mapcar (lambda (x) (list (concat (car x) ":")))
  5896. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5897. (mapcar (lambda (x) (list (concat x ":")))
  5898. org-link-types))
  5899. nil nil nil
  5900. 'tmphist
  5901. (or (car (car org-stored-links)))))
  5902. (set-window-configuration wcf)
  5903. (kill-buffer "*Org Links*"))
  5904. (setq entry (assoc link org-stored-links))
  5905. (or entry (push link org-insert-link-history))
  5906. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5907. (not org-keep-stored-link-after-insertion))
  5908. (setq org-stored-links (delq (assoc link org-stored-links)
  5909. org-stored-links)))
  5910. (setq desc (or desc (nth 1 entry)))))
  5911. (if (string-match org-plain-link-re link)
  5912. ;; URL-like link, normalize the use of angular brackets.
  5913. (setq link (org-make-link (org-remove-angle-brackets link))))
  5914. ;; Check if we are linking to the current file with a search option
  5915. ;; If yes, simplify the link by using only the search option.
  5916. (when (and buffer-file-name
  5917. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  5918. (let* ((path (match-string 1 link))
  5919. (case-fold-search nil)
  5920. (search (match-string 2 link)))
  5921. (save-match-data
  5922. (if (equal (file-truename buffer-file-name) (file-truename path))
  5923. ;; We are linking to this same file, with a search option
  5924. (setq link search)))))
  5925. ;; Check if we can/should use a relative path. If yes, simplify the link
  5926. (when (string-match "\\<file:\\(.*\\)" link)
  5927. (let* ((path (match-string 1 link))
  5928. (origpath path)
  5929. (case-fold-search nil))
  5930. (cond
  5931. ((eq org-link-file-path-type 'absolute)
  5932. (setq path (abbreviate-file-name (expand-file-name path))))
  5933. ((eq org-link-file-path-type 'noabbrev)
  5934. (setq path (expand-file-name path)))
  5935. ((eq org-link-file-path-type 'relative)
  5936. (setq path (file-relative-name path)))
  5937. (t
  5938. (save-match-data
  5939. (if (string-match (concat "^" (regexp-quote
  5940. (file-name-as-directory
  5941. (expand-file-name "."))))
  5942. (expand-file-name path))
  5943. ;; We are linking a file with relative path name.
  5944. (setq path (substring (expand-file-name path)
  5945. (match-end 0)))))))
  5946. (setq link (concat "file:" path))
  5947. (if (equal desc origpath)
  5948. (setq desc path))))
  5949. (if org-make-link-description-function
  5950. (setq desc (funcall org-make-link-description-function link desc)))
  5951. (setq desc (read-string "Description: " desc))
  5952. (unless (string-match "\\S-" desc) (setq desc nil))
  5953. (if remove (apply 'delete-region remove))
  5954. (insert (org-make-link-string link desc))))
  5955. (defun org-completing-read (&rest args)
  5956. (let ((minibuffer-local-completion-map
  5957. (copy-keymap minibuffer-local-completion-map)))
  5958. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  5959. (apply 'completing-read args)))
  5960. ;;; Opening/following a link
  5961. (defvar org-link-search-failed nil)
  5962. (defun org-next-link ()
  5963. "Move forward to the next link.
  5964. If the link is in hidden text, expose it."
  5965. (interactive)
  5966. (when (and org-link-search-failed (eq this-command last-command))
  5967. (goto-char (point-min))
  5968. (message "Link search wrapped back to beginning of buffer"))
  5969. (setq org-link-search-failed nil)
  5970. (let* ((pos (point))
  5971. (ct (org-context))
  5972. (a (assoc :link ct)))
  5973. (if a (goto-char (nth 2 a)))
  5974. (if (re-search-forward org-any-link-re nil t)
  5975. (progn
  5976. (goto-char (match-beginning 0))
  5977. (if (org-invisible-p) (org-show-context)))
  5978. (goto-char pos)
  5979. (setq org-link-search-failed t)
  5980. (error "No further link found"))))
  5981. (defun org-previous-link ()
  5982. "Move backward to the previous link.
  5983. If the link is in hidden text, expose it."
  5984. (interactive)
  5985. (when (and org-link-search-failed (eq this-command last-command))
  5986. (goto-char (point-max))
  5987. (message "Link search wrapped back to end of buffer"))
  5988. (setq org-link-search-failed nil)
  5989. (let* ((pos (point))
  5990. (ct (org-context))
  5991. (a (assoc :link ct)))
  5992. (if a (goto-char (nth 1 a)))
  5993. (if (re-search-backward org-any-link-re nil t)
  5994. (progn
  5995. (goto-char (match-beginning 0))
  5996. (if (org-invisible-p) (org-show-context)))
  5997. (goto-char pos)
  5998. (setq org-link-search-failed t)
  5999. (error "No further link found"))))
  6000. (defun org-find-file-at-mouse (ev)
  6001. "Open file link or URL at mouse."
  6002. (interactive "e")
  6003. (mouse-set-point ev)
  6004. (org-open-at-point 'in-emacs))
  6005. (defun org-open-at-mouse (ev)
  6006. "Open file link or URL at mouse."
  6007. (interactive "e")
  6008. (mouse-set-point ev)
  6009. (org-open-at-point))
  6010. (defvar org-window-config-before-follow-link nil
  6011. "The window configuration before following a link.
  6012. This is saved in case the need arises to restore it.")
  6013. (defvar org-open-link-marker (make-marker)
  6014. "Marker pointing to the location where `org-open-at-point; was called.")
  6015. ;;;###autoload
  6016. (defun org-open-at-point-global ()
  6017. "Follow a link like Org-mode does.
  6018. This command can be called in any mode to follow a link that has
  6019. Org-mode syntax."
  6020. (interactive)
  6021. (org-run-like-in-org-mode 'org-open-at-point))
  6022. ;;;###autoload
  6023. (defun org-open-link-from-string (s &optional arg)
  6024. "Open a link in the string S, as if it was in Org-mode."
  6025. (interactive "sLink: \nP")
  6026. (with-temp-buffer
  6027. (let ((org-inhibit-startup t))
  6028. (org-mode)
  6029. (insert s)
  6030. (goto-char (point-min))
  6031. (org-open-at-point arg))))
  6032. (defun org-open-at-point (&optional in-emacs)
  6033. "Open link at or after point.
  6034. If there is no link at point, this function will search forward up to
  6035. the end of the current subtree.
  6036. Normally, files will be opened by an appropriate application. If the
  6037. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6038. (interactive "P")
  6039. (org-load-modules-maybe)
  6040. (move-marker org-open-link-marker (point))
  6041. (setq org-window-config-before-follow-link (current-window-configuration))
  6042. (org-remove-occur-highlights nil nil t)
  6043. (if (org-at-timestamp-p t)
  6044. (org-follow-timestamp-link)
  6045. (let (type path link line search (pos (point)))
  6046. (catch 'match
  6047. (save-excursion
  6048. (skip-chars-forward "^]\n\r")
  6049. (when (org-in-regexp org-bracket-link-regexp)
  6050. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6051. (while (string-match " *\n *" link)
  6052. (setq link (replace-match " " t t link)))
  6053. (setq link (org-link-expand-abbrev link))
  6054. (if (string-match org-link-re-with-space2 link)
  6055. (setq type (match-string 1 link) path (match-string 2 link))
  6056. (setq type "thisfile" path link))
  6057. (throw 'match t)))
  6058. (when (get-text-property (point) 'org-linked-text)
  6059. (setq type "thisfile"
  6060. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6061. (1+ (point)) (point))
  6062. path (buffer-substring
  6063. (previous-single-property-change pos 'org-linked-text)
  6064. (next-single-property-change pos 'org-linked-text)))
  6065. (throw 'match t))
  6066. (save-excursion
  6067. (when (or (org-in-regexp org-angle-link-re)
  6068. (org-in-regexp org-plain-link-re))
  6069. (setq type (match-string 1) path (match-string 2))
  6070. (throw 'match t)))
  6071. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6072. (setq type "tree-match"
  6073. path (match-string 1))
  6074. (throw 'match t))
  6075. (save-excursion
  6076. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6077. (setq type "tags"
  6078. path (match-string 1))
  6079. (while (string-match ":" path)
  6080. (setq path (replace-match "+" t t path)))
  6081. (throw 'match t))))
  6082. (unless path
  6083. (error "No link found"))
  6084. ;; Remove any trailing spaces in path
  6085. (if (string-match " +\\'" path)
  6086. (setq path (replace-match "" t t path)))
  6087. (cond
  6088. ((assoc type org-link-protocols)
  6089. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6090. ((equal type "mailto")
  6091. (let ((cmd (car org-link-mailto-program))
  6092. (args (cdr org-link-mailto-program)) args1
  6093. (address path) (subject "") a)
  6094. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6095. (setq address (match-string 1 path)
  6096. subject (org-link-escape (match-string 2 path))))
  6097. (while args
  6098. (cond
  6099. ((not (stringp (car args))) (push (pop args) args1))
  6100. (t (setq a (pop args))
  6101. (if (string-match "%a" a)
  6102. (setq a (replace-match address t t a)))
  6103. (if (string-match "%s" a)
  6104. (setq a (replace-match subject t t a)))
  6105. (push a args1))))
  6106. (apply cmd (nreverse args1))))
  6107. ((member type '("http" "https" "ftp" "news"))
  6108. (browse-url (concat type ":" (org-link-escape
  6109. path org-link-escape-chars-browser))))
  6110. ((member type '("message"))
  6111. (browse-url (concat type ":" path)))
  6112. ((string= type "tags")
  6113. (org-tags-view in-emacs path))
  6114. ((string= type "thisfile")
  6115. (if in-emacs
  6116. (switch-to-buffer-other-window
  6117. (org-get-buffer-for-internal-link (current-buffer)))
  6118. (org-mark-ring-push))
  6119. (let ((cmd `(org-link-search
  6120. ,path
  6121. ,(cond ((equal in-emacs '(4)) 'occur)
  6122. ((equal in-emacs '(16)) 'org-occur)
  6123. (t nil))
  6124. ,pos)))
  6125. (condition-case nil (eval cmd)
  6126. (error (progn (widen) (eval cmd))))))
  6127. ((string= type "tree-match")
  6128. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6129. ((string= type "file")
  6130. (if (string-match "::\\([0-9]+\\)\\'" path)
  6131. (setq line (string-to-number (match-string 1 path))
  6132. path (substring path 0 (match-beginning 0)))
  6133. (if (string-match "::\\(.+\\)\\'" path)
  6134. (setq search (match-string 1 path)
  6135. path (substring path 0 (match-beginning 0)))))
  6136. (if (string-match "[*?{]" (file-name-nondirectory path))
  6137. (dired path)
  6138. (org-open-file path in-emacs line search)))
  6139. ((string= type "news")
  6140. (require 'org-gnus)
  6141. (org-gnus-follow-link path))
  6142. ((string= type "shell")
  6143. (let ((cmd path))
  6144. (if (or (not org-confirm-shell-link-function)
  6145. (funcall org-confirm-shell-link-function
  6146. (format "Execute \"%s\" in shell? "
  6147. (org-add-props cmd nil
  6148. 'face 'org-warning))))
  6149. (progn
  6150. (message "Executing %s" cmd)
  6151. (shell-command cmd))
  6152. (error "Abort"))))
  6153. ((string= type "elisp")
  6154. (let ((cmd path))
  6155. (if (or (not org-confirm-elisp-link-function)
  6156. (funcall org-confirm-elisp-link-function
  6157. (format "Execute \"%s\" as elisp? "
  6158. (org-add-props cmd nil
  6159. 'face 'org-warning))))
  6160. (message "%s => %s" cmd (eval (read cmd)))
  6161. (error "Abort"))))
  6162. (t
  6163. (browse-url-at-point)))))
  6164. (move-marker org-open-link-marker nil)
  6165. (run-hook-with-args 'org-follow-link-hook))
  6166. ;;;; Time estimates
  6167. (defun org-get-effort (&optional pom)
  6168. "Get the effort estimate for the current entry."
  6169. (org-entry-get pom org-effort-property))
  6170. ;;; File search
  6171. (defvar org-create-file-search-functions nil
  6172. "List of functions to construct the right search string for a file link.
  6173. These functions are called in turn with point at the location to
  6174. which the link should point.
  6175. A function in the hook should first test if it would like to
  6176. handle this file type, for example by checking the major-mode or
  6177. the file extension. If it decides not to handle this file, it
  6178. should just return nil to give other functions a chance. If it
  6179. does handle the file, it must return the search string to be used
  6180. when following the link. The search string will be part of the
  6181. file link, given after a double colon, and `org-open-at-point'
  6182. will automatically search for it. If special measures must be
  6183. taken to make the search successful, another function should be
  6184. added to the companion hook `org-execute-file-search-functions',
  6185. which see.
  6186. A function in this hook may also use `setq' to set the variable
  6187. `description' to provide a suggestion for the descriptive text to
  6188. be used for this link when it gets inserted into an Org-mode
  6189. buffer with \\[org-insert-link].")
  6190. (defvar org-execute-file-search-functions nil
  6191. "List of functions to execute a file search triggered by a link.
  6192. Functions added to this hook must accept a single argument, the
  6193. search string that was part of the file link, the part after the
  6194. double colon. The function must first check if it would like to
  6195. handle this search, for example by checking the major-mode or the
  6196. file extension. If it decides not to handle this search, it
  6197. should just return nil to give other functions a chance. If it
  6198. does handle the search, it must return a non-nil value to keep
  6199. other functions from trying.
  6200. Each function can access the current prefix argument through the
  6201. variable `current-prefix-argument'. Note that a single prefix is
  6202. used to force opening a link in Emacs, so it may be good to only
  6203. use a numeric or double prefix to guide the search function.
  6204. In case this is needed, a function in this hook can also restore
  6205. the window configuration before `org-open-at-point' was called using:
  6206. (set-window-configuration org-window-config-before-follow-link)")
  6207. (defun org-link-search (s &optional type avoid-pos)
  6208. "Search for a link search option.
  6209. If S is surrounded by forward slashes, it is interpreted as a
  6210. regular expression. In org-mode files, this will create an `org-occur'
  6211. sparse tree. In ordinary files, `occur' will be used to list matches.
  6212. If the current buffer is in `dired-mode', grep will be used to search
  6213. in all files. If AVOID-POS is given, ignore matches near that position."
  6214. (let ((case-fold-search t)
  6215. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6216. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6217. (append '(("") (" ") ("\t") ("\n"))
  6218. org-emphasis-alist)
  6219. "\\|") "\\)"))
  6220. (pos (point))
  6221. (pre nil) (post nil)
  6222. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6223. (cond
  6224. ;; First check if there are any special
  6225. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6226. ;; Now try the builtin stuff
  6227. ((save-excursion
  6228. (goto-char (point-min))
  6229. (and
  6230. (re-search-forward
  6231. (concat "<<" (regexp-quote s0) ">>") nil t)
  6232. (setq type 'dedicated
  6233. pos (match-beginning 0))))
  6234. ;; There is an exact target for this
  6235. (goto-char pos))
  6236. ((string-match "^/\\(.*\\)/$" s)
  6237. ;; A regular expression
  6238. (cond
  6239. ((org-mode-p)
  6240. (org-occur (match-string 1 s)))
  6241. ;;((eq major-mode 'dired-mode)
  6242. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6243. (t (org-do-occur (match-string 1 s)))))
  6244. (t
  6245. ;; A normal search strings
  6246. (when (equal (string-to-char s) ?*)
  6247. ;; Anchor on headlines, post may include tags.
  6248. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6249. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6250. s (substring s 1)))
  6251. (remove-text-properties
  6252. 0 (length s)
  6253. '(face nil mouse-face nil keymap nil fontified nil) s)
  6254. ;; Make a series of regular expressions to find a match
  6255. (setq words (org-split-string s "[ \n\r\t]+")
  6256. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6257. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6258. "\\)" markers)
  6259. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6260. re2a (concat "[ \t\r\n]" re2a_)
  6261. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6262. re4 (concat "[^a-zA-Z_]" re4_)
  6263. re1 (concat pre re2 post)
  6264. re3 (concat pre (if pre re4_ re4) post)
  6265. re5 (concat pre ".*" re4)
  6266. re2 (concat pre re2)
  6267. re2a (concat pre (if pre re2a_ re2a))
  6268. re4 (concat pre (if pre re4_ re4))
  6269. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6270. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6271. re5 "\\)"
  6272. ))
  6273. (cond
  6274. ((eq type 'org-occur) (org-occur reall))
  6275. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6276. (t (goto-char (point-min))
  6277. (setq type 'fuzzy)
  6278. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6279. (org-search-not-self 1 re1 nil t)
  6280. (org-search-not-self 1 re2 nil t)
  6281. (org-search-not-self 1 re2a nil t)
  6282. (org-search-not-self 1 re3 nil t)
  6283. (org-search-not-self 1 re4 nil t)
  6284. (org-search-not-self 1 re5 nil t)
  6285. )
  6286. (goto-char (match-beginning 1))
  6287. (goto-char pos)
  6288. (error "No match")))))
  6289. (t
  6290. ;; Normal string-search
  6291. (goto-char (point-min))
  6292. (if (search-forward s nil t)
  6293. (goto-char (match-beginning 0))
  6294. (error "No match"))))
  6295. (and (org-mode-p) (org-show-context 'link-search))
  6296. type))
  6297. (defun org-search-not-self (group &rest args)
  6298. "Execute `re-search-forward', but only accept matches that do not
  6299. enclose the position of `org-open-link-marker'."
  6300. (let ((m org-open-link-marker))
  6301. (catch 'exit
  6302. (while (apply 're-search-forward args)
  6303. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6304. (goto-char (match-end group))
  6305. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6306. (> (match-beginning 0) (marker-position m))
  6307. (< (match-end 0) (marker-position m)))
  6308. (save-match-data
  6309. (or (not (org-in-regexp
  6310. org-bracket-link-analytic-regexp 1))
  6311. (not (match-end 4)) ; no description
  6312. (and (<= (match-beginning 4) (point))
  6313. (>= (match-end 4) (point))))))
  6314. (throw 'exit (point))))))))
  6315. (defun org-get-buffer-for-internal-link (buffer)
  6316. "Return a buffer to be used for displaying the link target of internal links."
  6317. (cond
  6318. ((not org-display-internal-link-with-indirect-buffer)
  6319. buffer)
  6320. ((string-match "(Clone)$" (buffer-name buffer))
  6321. (message "Buffer is already a clone, not making another one")
  6322. ;; we also do not modify visibility in this case
  6323. buffer)
  6324. (t ; make a new indirect buffer for displaying the link
  6325. (let* ((bn (buffer-name buffer))
  6326. (ibn (concat bn "(Clone)"))
  6327. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6328. (with-current-buffer ib (org-overview))
  6329. ib))))
  6330. (defun org-do-occur (regexp &optional cleanup)
  6331. "Call the Emacs command `occur'.
  6332. If CLEANUP is non-nil, remove the printout of the regular expression
  6333. in the *Occur* buffer. This is useful if the regex is long and not useful
  6334. to read."
  6335. (occur regexp)
  6336. (when cleanup
  6337. (let ((cwin (selected-window)) win beg end)
  6338. (when (setq win (get-buffer-window "*Occur*"))
  6339. (select-window win))
  6340. (goto-char (point-min))
  6341. (when (re-search-forward "match[a-z]+" nil t)
  6342. (setq beg (match-end 0))
  6343. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6344. (setq end (1- (match-beginning 0)))))
  6345. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6346. (goto-char (point-min))
  6347. (select-window cwin))))
  6348. ;;; The mark ring for links jumps
  6349. (defvar org-mark-ring nil
  6350. "Mark ring for positions before jumps in Org-mode.")
  6351. (defvar org-mark-ring-last-goto nil
  6352. "Last position in the mark ring used to go back.")
  6353. ;; Fill and close the ring
  6354. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6355. (loop for i from 1 to org-mark-ring-length do
  6356. (push (make-marker) org-mark-ring))
  6357. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6358. org-mark-ring)
  6359. (defun org-mark-ring-push (&optional pos buffer)
  6360. "Put the current position or POS into the mark ring and rotate it."
  6361. (interactive)
  6362. (setq pos (or pos (point)))
  6363. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6364. (move-marker (car org-mark-ring)
  6365. (or pos (point))
  6366. (or buffer (current-buffer)))
  6367. (message "%s"
  6368. (substitute-command-keys
  6369. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6370. (defun org-mark-ring-goto (&optional n)
  6371. "Jump to the previous position in the mark ring.
  6372. With prefix arg N, jump back that many stored positions. When
  6373. called several times in succession, walk through the entire ring.
  6374. Org-mode commands jumping to a different position in the current file,
  6375. or to another Org-mode file, automatically push the old position
  6376. onto the ring."
  6377. (interactive "p")
  6378. (let (p m)
  6379. (if (eq last-command this-command)
  6380. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6381. (setq p org-mark-ring))
  6382. (setq org-mark-ring-last-goto p)
  6383. (setq m (car p))
  6384. (switch-to-buffer (marker-buffer m))
  6385. (goto-char m)
  6386. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6387. (defun org-remove-angle-brackets (s)
  6388. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6389. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6390. s)
  6391. (defun org-add-angle-brackets (s)
  6392. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6393. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6394. s)
  6395. ;;; Following specific links
  6396. (defun org-follow-timestamp-link ()
  6397. (cond
  6398. ((org-at-date-range-p t)
  6399. (let ((org-agenda-start-on-weekday)
  6400. (t1 (match-string 1))
  6401. (t2 (match-string 2)))
  6402. (setq t1 (time-to-days (org-time-string-to-time t1))
  6403. t2 (time-to-days (org-time-string-to-time t2)))
  6404. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6405. ((org-at-timestamp-p t)
  6406. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6407. (substring (match-string 1) 0 10)))
  6408. 1))
  6409. (t (error "This should not happen"))))
  6410. ;;; Following file links
  6411. (defvar org-wait nil)
  6412. (defun org-open-file (path &optional in-emacs line search)
  6413. "Open the file at PATH.
  6414. First, this expands any special file name abbreviations. Then the
  6415. configuration variable `org-file-apps' is checked if it contains an
  6416. entry for this file type, and if yes, the corresponding command is launched.
  6417. If no application is found, Emacs simply visits the file.
  6418. With optional argument IN-EMACS, Emacs will visit the file.
  6419. Optional LINE specifies a line to go to, optional SEARCH a string to
  6420. search for. If LINE or SEARCH is given, the file will always be
  6421. opened in Emacs.
  6422. If the file does not exist, an error is thrown."
  6423. (setq in-emacs (or in-emacs line search))
  6424. (let* ((file (if (equal path "")
  6425. buffer-file-name
  6426. (substitute-in-file-name (expand-file-name path))))
  6427. (apps (append org-file-apps (org-default-apps)))
  6428. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6429. (dirp (if remp nil (file-directory-p file)))
  6430. (dfile (downcase file))
  6431. (old-buffer (current-buffer))
  6432. (old-pos (point))
  6433. (old-mode major-mode)
  6434. ext cmd)
  6435. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6436. (setq ext (match-string 1 dfile))
  6437. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6438. (setq ext (match-string 1 dfile))))
  6439. (if in-emacs
  6440. (setq cmd 'emacs)
  6441. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6442. (and dirp (cdr (assoc 'directory apps)))
  6443. (cdr (assoc ext apps))
  6444. (cdr (assoc t apps)))))
  6445. (when (eq cmd 'mailcap)
  6446. (require 'mailcap)
  6447. (mailcap-parse-mailcaps)
  6448. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6449. (command (mailcap-mime-info mime-type)))
  6450. (if (stringp command)
  6451. (setq cmd command)
  6452. (setq cmd 'emacs))))
  6453. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6454. (not (file-exists-p file))
  6455. (not org-open-non-existing-files))
  6456. (error "No such file: %s" file))
  6457. (cond
  6458. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6459. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6460. (while (string-match "['\"]%s['\"]" cmd)
  6461. (setq cmd (replace-match "%s" t t cmd)))
  6462. (while (string-match "%s" cmd)
  6463. (setq cmd (replace-match
  6464. (save-match-data (shell-quote-argument file))
  6465. t t cmd)))
  6466. (save-window-excursion
  6467. (start-process-shell-command cmd nil cmd)
  6468. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6469. ))
  6470. ((or (stringp cmd)
  6471. (eq cmd 'emacs))
  6472. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6473. (widen)
  6474. (if line (goto-line line)
  6475. (if search (org-link-search search))))
  6476. ((consp cmd)
  6477. (eval cmd))
  6478. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6479. (and (org-mode-p) (eq old-mode 'org-mode)
  6480. (or (not (equal old-buffer (current-buffer)))
  6481. (not (equal old-pos (point))))
  6482. (org-mark-ring-push old-pos old-buffer))))
  6483. (defun org-default-apps ()
  6484. "Return the default applications for this operating system."
  6485. (cond
  6486. ((eq system-type 'darwin)
  6487. org-file-apps-defaults-macosx)
  6488. ((eq system-type 'windows-nt)
  6489. org-file-apps-defaults-windowsnt)
  6490. (t org-file-apps-defaults-gnu)))
  6491. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6492. (defun org-file-remote-p (file)
  6493. "Test whether FILE specifies a location on a remote system.
  6494. Return non-nil if the location is indeed remote.
  6495. For example, the filename \"/user@host:/foo\" specifies a location
  6496. on the system \"/user@host:\"."
  6497. (cond ((fboundp 'file-remote-p)
  6498. (file-remote-p file))
  6499. ((fboundp 'tramp-handle-file-remote-p)
  6500. (tramp-handle-file-remote-p file))
  6501. ((and (boundp 'ange-ftp-name-format)
  6502. (string-match (car ange-ftp-name-format) file))
  6503. t)
  6504. (t nil)))
  6505. ;;;; Refiling
  6506. (defun org-get-org-file ()
  6507. "Read a filename, with default directory `org-directory'."
  6508. (let ((default (or org-default-notes-file remember-data-file)))
  6509. (read-file-name (format "File name [%s]: " default)
  6510. (file-name-as-directory org-directory)
  6511. default)))
  6512. (defun org-notes-order-reversed-p ()
  6513. "Check if the current file should receive notes in reversed order."
  6514. (cond
  6515. ((not org-reverse-note-order) nil)
  6516. ((eq t org-reverse-note-order) t)
  6517. ((not (listp org-reverse-note-order)) nil)
  6518. (t (catch 'exit
  6519. (let ((all org-reverse-note-order)
  6520. entry)
  6521. (while (setq entry (pop all))
  6522. (if (string-match (car entry) buffer-file-name)
  6523. (throw 'exit (cdr entry))))
  6524. nil)))))
  6525. (defvar org-refile-target-table nil
  6526. "The list of refile targets, created by `org-refile'.")
  6527. (defvar org-agenda-new-buffers nil
  6528. "Buffers created to visit agenda files.")
  6529. (defun org-get-refile-targets (&optional default-buffer)
  6530. "Produce a table with refile targets."
  6531. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6532. targets txt re files f desc descre)
  6533. (with-current-buffer (or default-buffer (current-buffer))
  6534. (while (setq entry (pop entries))
  6535. (setq files (car entry) desc (cdr entry))
  6536. (cond
  6537. ((null files) (setq files (list (current-buffer))))
  6538. ((eq files 'org-agenda-files)
  6539. (setq files (org-agenda-files 'unrestricted)))
  6540. ((and (symbolp files) (fboundp files))
  6541. (setq files (funcall files)))
  6542. ((and (symbolp files) (boundp files))
  6543. (setq files (symbol-value files))))
  6544. (if (stringp files) (setq files (list files)))
  6545. (cond
  6546. ((eq (car desc) :tag)
  6547. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6548. ((eq (car desc) :todo)
  6549. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6550. ((eq (car desc) :regexp)
  6551. (setq descre (cdr desc)))
  6552. ((eq (car desc) :level)
  6553. (setq descre (concat "^\\*\\{" (number-to-string
  6554. (if org-odd-levels-only
  6555. (1- (* 2 (cdr desc)))
  6556. (cdr desc)))
  6557. "\\}[ \t]")))
  6558. ((eq (car desc) :maxlevel)
  6559. (setq descre (concat "^\\*\\{1," (number-to-string
  6560. (if org-odd-levels-only
  6561. (1- (* 2 (cdr desc)))
  6562. (cdr desc)))
  6563. "\\}[ \t]")))
  6564. (t (error "Bad refiling target description %s" desc)))
  6565. (while (setq f (pop files))
  6566. (save-excursion
  6567. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6568. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6569. (save-excursion
  6570. (save-restriction
  6571. (widen)
  6572. (goto-char (point-min))
  6573. (while (re-search-forward descre nil t)
  6574. (goto-char (point-at-bol))
  6575. (when (looking-at org-complex-heading-regexp)
  6576. (setq txt (match-string 4)
  6577. re (concat "^" (regexp-quote
  6578. (buffer-substring (match-beginning 1)
  6579. (match-end 4)))))
  6580. (if (match-end 5) (setq re (concat re "[ \t]+"
  6581. (regexp-quote
  6582. (match-string 5)))))
  6583. (setq re (concat re "[ \t]*$"))
  6584. (when org-refile-use-outline-path
  6585. (setq txt (mapconcat 'identity
  6586. (append
  6587. (if (eq org-refile-use-outline-path 'file)
  6588. (list (file-name-nondirectory
  6589. (buffer-file-name (buffer-base-buffer))))
  6590. (if (eq org-refile-use-outline-path 'full-file-path)
  6591. (list (buffer-file-name (buffer-base-buffer)))))
  6592. (org-get-outline-path)
  6593. (list txt))
  6594. "/")))
  6595. (push (list txt f re (point)) targets))
  6596. (goto-char (point-at-eol))))))))
  6597. (nreverse targets))))
  6598. (defun org-get-outline-path ()
  6599. "Return the outline path to the current entry, as a list."
  6600. (let (rtn)
  6601. (save-excursion
  6602. (while (org-up-heading-safe)
  6603. (when (looking-at org-complex-heading-regexp)
  6604. (push (org-match-string-no-properties 4) rtn)))
  6605. rtn)))
  6606. (defvar org-refile-history nil
  6607. "History for refiling operations.")
  6608. (defun org-refile (&optional goto default-buffer)
  6609. "Move the entry at point to another heading.
  6610. The list of target headings is compiled using the information in
  6611. `org-refile-targets', which see. This list is created before each use
  6612. and will therefore always be up-to-date.
  6613. At the target location, the entry is filed as a subitem of the target heading.
  6614. Depending on `org-reverse-note-order', the new subitem will either be the
  6615. first of the last subitem.
  6616. With prefix arg GOTO, the command will only visit the target location,
  6617. not actually move anything.
  6618. With a double prefix `C-c C-c', go to the location where the last refiling
  6619. operation has put the subtree."
  6620. (interactive "P")
  6621. (let* ((cbuf (current-buffer))
  6622. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6623. pos it nbuf file re level reversed)
  6624. (if (equal goto '(16))
  6625. (org-refile-goto-last-stored)
  6626. (when (setq it (org-refile-get-location
  6627. (if goto "Goto: " "Refile to: ") default-buffer))
  6628. (setq file (nth 1 it)
  6629. re (nth 2 it)
  6630. pos (nth 3 it))
  6631. (setq nbuf (or (find-buffer-visiting file)
  6632. (find-file-noselect file)))
  6633. (if goto
  6634. (progn
  6635. (switch-to-buffer nbuf)
  6636. (goto-char pos)
  6637. (org-show-context 'org-goto))
  6638. (org-copy-special)
  6639. (save-excursion
  6640. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6641. (find-file-noselect file))))
  6642. (setq reversed (org-notes-order-reversed-p))
  6643. (save-excursion
  6644. (save-restriction
  6645. (widen)
  6646. (goto-char pos)
  6647. (looking-at outline-regexp)
  6648. (setq level (org-get-valid-level (funcall outline-level) 1))
  6649. (goto-char
  6650. (if reversed
  6651. (outline-next-heading)
  6652. (or (save-excursion (outline-get-next-sibling))
  6653. (org-end-of-subtree t t)
  6654. (point-max))))
  6655. (bookmark-set "org-refile-last-stored")
  6656. (org-paste-subtree level))))
  6657. (org-cut-special)
  6658. (message "Entry refiled to \"%s\"" (car it)))))))
  6659. (defun org-refile-goto-last-stored ()
  6660. "Go to the location where the last refile was stored."
  6661. (interactive)
  6662. (bookmark-jump "org-refile-last-stored")
  6663. (message "This is the location of the last refile"))
  6664. (defun org-refile-get-location (&optional prompt default-buffer)
  6665. "Prompt the user for a refile location, using PROMPT."
  6666. (let ((org-refile-targets org-refile-targets)
  6667. (org-refile-use-outline-path org-refile-use-outline-path))
  6668. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6669. (unless org-refile-target-table
  6670. (error "No refile targets"))
  6671. (let* ((cbuf (current-buffer))
  6672. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6673. (fname (and filename (file-truename filename)))
  6674. (tbl (mapcar
  6675. (lambda (x)
  6676. (if (not (equal fname (file-truename (nth 1 x))))
  6677. (cons (concat (car x) " (" (file-name-nondirectory
  6678. (nth 1 x)) ")")
  6679. (cdr x))
  6680. x))
  6681. org-refile-target-table))
  6682. (completion-ignore-case t))
  6683. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6684. tbl)))
  6685. ;;;; Dynamic blocks
  6686. (defun org-find-dblock (name)
  6687. "Find the first dynamic block with name NAME in the buffer.
  6688. If not found, stay at current position and return nil."
  6689. (let (pos)
  6690. (save-excursion
  6691. (goto-char (point-min))
  6692. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6693. nil t)
  6694. (match-beginning 0))))
  6695. (if pos (goto-char pos))
  6696. pos))
  6697. (defconst org-dblock-start-re
  6698. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6699. "Matches the startline of a dynamic block, with parameters.")
  6700. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6701. "Matches the end of a dyhamic block.")
  6702. (defun org-create-dblock (plist)
  6703. "Create a dynamic block section, with parameters taken from PLIST.
  6704. PLIST must containe a :name entry which is used as name of the block."
  6705. (unless (bolp) (newline))
  6706. (let ((name (plist-get plist :name)))
  6707. (insert "#+BEGIN: " name)
  6708. (while plist
  6709. (if (eq (car plist) :name)
  6710. (setq plist (cddr plist))
  6711. (insert " " (prin1-to-string (pop plist)))))
  6712. (insert "\n\n#+END:\n")
  6713. (beginning-of-line -2)))
  6714. (defun org-prepare-dblock ()
  6715. "Prepare dynamic block for refresh.
  6716. This empties the block, puts the cursor at the insert position and returns
  6717. the property list including an extra property :name with the block name."
  6718. (unless (looking-at org-dblock-start-re)
  6719. (error "Not at a dynamic block"))
  6720. (let* ((begdel (1+ (match-end 0)))
  6721. (name (org-no-properties (match-string 1)))
  6722. (params (append (list :name name)
  6723. (read (concat "(" (match-string 3) ")")))))
  6724. (unless (re-search-forward org-dblock-end-re nil t)
  6725. (error "Dynamic block not terminated"))
  6726. (setq params
  6727. (append params
  6728. (list :content (buffer-substring
  6729. begdel (match-beginning 0)))))
  6730. (delete-region begdel (match-beginning 0))
  6731. (goto-char begdel)
  6732. (open-line 1)
  6733. params))
  6734. (defun org-map-dblocks (&optional command)
  6735. "Apply COMMAND to all dynamic blocks in the current buffer.
  6736. If COMMAND is not given, use `org-update-dblock'."
  6737. (let ((cmd (or command 'org-update-dblock))
  6738. pos)
  6739. (save-excursion
  6740. (goto-char (point-min))
  6741. (while (re-search-forward org-dblock-start-re nil t)
  6742. (goto-char (setq pos (match-beginning 0)))
  6743. (condition-case nil
  6744. (funcall cmd)
  6745. (error (message "Error during update of dynamic block")))
  6746. (goto-char pos)
  6747. (unless (re-search-forward org-dblock-end-re nil t)
  6748. (error "Dynamic block not terminated"))))))
  6749. (defun org-dblock-update (&optional arg)
  6750. "User command for updating dynamic blocks.
  6751. Update the dynamic block at point. With prefix ARG, update all dynamic
  6752. blocks in the buffer."
  6753. (interactive "P")
  6754. (if arg
  6755. (org-update-all-dblocks)
  6756. (or (looking-at org-dblock-start-re)
  6757. (org-beginning-of-dblock))
  6758. (org-update-dblock)))
  6759. (defun org-update-dblock ()
  6760. "Update the dynamic block at point
  6761. This means to empty the block, parse for parameters and then call
  6762. the correct writing function."
  6763. (save-window-excursion
  6764. (let* ((pos (point))
  6765. (line (org-current-line))
  6766. (params (org-prepare-dblock))
  6767. (name (plist-get params :name))
  6768. (cmd (intern (concat "org-dblock-write:" name))))
  6769. (message "Updating dynamic block `%s' at line %d..." name line)
  6770. (funcall cmd params)
  6771. (message "Updating dynamic block `%s' at line %d...done" name line)
  6772. (goto-char pos))))
  6773. (defun org-beginning-of-dblock ()
  6774. "Find the beginning of the dynamic block at point.
  6775. Error if there is no scuh block at point."
  6776. (let ((pos (point))
  6777. beg)
  6778. (end-of-line 1)
  6779. (if (and (re-search-backward org-dblock-start-re nil t)
  6780. (setq beg (match-beginning 0))
  6781. (re-search-forward org-dblock-end-re nil t)
  6782. (> (match-end 0) pos))
  6783. (goto-char beg)
  6784. (goto-char pos)
  6785. (error "Not in a dynamic block"))))
  6786. (defun org-update-all-dblocks ()
  6787. "Update all dynamic blocks in the buffer.
  6788. This function can be used in a hook."
  6789. (when (org-mode-p)
  6790. (org-map-dblocks 'org-update-dblock)))
  6791. ;;;; Completion
  6792. (defconst org-additional-option-like-keywords
  6793. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6794. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6795. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6796. (defun org-complete (&optional arg)
  6797. "Perform completion on word at point.
  6798. At the beginning of a headline, this completes TODO keywords as given in
  6799. `org-todo-keywords'.
  6800. If the current word is preceded by a backslash, completes the TeX symbols
  6801. that are supported for HTML support.
  6802. If the current word is preceded by \"#+\", completes special words for
  6803. setting file options.
  6804. In the line after \"#+STARTUP:, complete valid keywords.\"
  6805. At all other locations, this simply calls the value of
  6806. `org-completion-fallback-command'."
  6807. (interactive "P")
  6808. (org-without-partial-completion
  6809. (catch 'exit
  6810. (let* ((end (point))
  6811. (beg1 (save-excursion
  6812. (skip-chars-backward (org-re "[:alnum:]_@"))
  6813. (point)))
  6814. (beg (save-excursion
  6815. (skip-chars-backward "a-zA-Z0-9_:$")
  6816. (point)))
  6817. (confirm (lambda (x) (stringp (car x))))
  6818. (searchhead (equal (char-before beg) ?*))
  6819. (tag (and (equal (char-before beg1) ?:)
  6820. (equal (char-after (point-at-bol)) ?*)))
  6821. (prop (and (equal (char-before beg1) ?:)
  6822. (not (equal (char-after (point-at-bol)) ?*))))
  6823. (texp (equal (char-before beg) ?\\))
  6824. (link (equal (char-before beg) ?\[))
  6825. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6826. beg)
  6827. "#+"))
  6828. (startup (string-match "^#\\+STARTUP:.*"
  6829. (buffer-substring (point-at-bol) (point))))
  6830. (completion-ignore-case opt)
  6831. (type nil)
  6832. (tbl nil)
  6833. (table (cond
  6834. (opt
  6835. (setq type :opt)
  6836. (require 'org-exp)
  6837. (append
  6838. (mapcar
  6839. (lambda (x)
  6840. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6841. (cons (match-string 2 x) (match-string 1 x)))
  6842. (org-split-string (org-get-current-options) "\n"))
  6843. (mapcar 'list org-additional-option-like-keywords)))
  6844. (startup
  6845. (setq type :startup)
  6846. org-startup-options)
  6847. (link (append org-link-abbrev-alist-local
  6848. org-link-abbrev-alist))
  6849. (texp
  6850. (setq type :tex)
  6851. org-html-entities)
  6852. ((string-match "\\`\\*+[ \t]+\\'"
  6853. (buffer-substring (point-at-bol) beg))
  6854. (setq type :todo)
  6855. (mapcar 'list org-todo-keywords-1))
  6856. (searchhead
  6857. (setq type :searchhead)
  6858. (save-excursion
  6859. (goto-char (point-min))
  6860. (while (re-search-forward org-todo-line-regexp nil t)
  6861. (push (list
  6862. (org-make-org-heading-search-string
  6863. (match-string 3) t))
  6864. tbl)))
  6865. tbl)
  6866. (tag (setq type :tag beg beg1)
  6867. (or org-tag-alist (org-get-buffer-tags)))
  6868. (prop (setq type :prop beg beg1)
  6869. (mapcar 'list (org-buffer-property-keys nil t t)))
  6870. (t (progn
  6871. (call-interactively org-completion-fallback-command)
  6872. (throw 'exit nil)))))
  6873. (pattern (buffer-substring-no-properties beg end))
  6874. (completion (try-completion pattern table confirm)))
  6875. (cond ((eq completion t)
  6876. (if (not (assoc (upcase pattern) table))
  6877. (message "Already complete")
  6878. (if (and (equal type :opt)
  6879. (not (member (car (assoc (upcase pattern) table))
  6880. org-additional-option-like-keywords)))
  6881. (insert (substring (cdr (assoc (upcase pattern) table))
  6882. (length pattern)))
  6883. (if (memq type '(:tag :prop)) (insert ":")))))
  6884. ((null completion)
  6885. (message "Can't find completion for \"%s\"" pattern)
  6886. (ding))
  6887. ((not (string= pattern completion))
  6888. (delete-region beg end)
  6889. (if (string-match " +$" completion)
  6890. (setq completion (replace-match "" t t completion)))
  6891. (insert completion)
  6892. (if (get-buffer-window "*Completions*")
  6893. (delete-window (get-buffer-window "*Completions*")))
  6894. (if (assoc completion table)
  6895. (if (eq type :todo) (insert " ")
  6896. (if (memq type '(:tag :prop)) (insert ":"))))
  6897. (if (and (equal type :opt) (assoc completion table))
  6898. (message "%s" (substitute-command-keys
  6899. "Press \\[org-complete] again to insert example settings"))))
  6900. (t
  6901. (message "Making completion list...")
  6902. (let ((list (sort (all-completions pattern table confirm)
  6903. 'string<)))
  6904. (with-output-to-temp-buffer "*Completions*"
  6905. (condition-case nil
  6906. ;; Protection needed for XEmacs and emacs 21
  6907. (display-completion-list list pattern)
  6908. (error (display-completion-list list)))))
  6909. (message "Making completion list...%s" "done")))))))
  6910. ;;;; TODO, DEADLINE, Comments
  6911. (defun org-toggle-comment ()
  6912. "Change the COMMENT state of an entry."
  6913. (interactive)
  6914. (save-excursion
  6915. (org-back-to-heading)
  6916. (let (case-fold-search)
  6917. (if (looking-at (concat outline-regexp
  6918. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  6919. (replace-match "" t t nil 1)
  6920. (if (looking-at outline-regexp)
  6921. (progn
  6922. (goto-char (match-end 0))
  6923. (insert org-comment-string " ")))))))
  6924. (defvar org-last-todo-state-is-todo nil
  6925. "This is non-nil when the last TODO state change led to a TODO state.
  6926. If the last change removed the TODO tag or switched to DONE, then
  6927. this is nil.")
  6928. (defvar org-setting-tags nil) ; dynamically skiped
  6929. (defun org-parse-local-options (string var)
  6930. "Parse STRING for startup setting relevant for variable VAR."
  6931. (let ((rtn (symbol-value var))
  6932. e opts)
  6933. (save-match-data
  6934. (if (or (not string) (not (string-match "\\S-" string)))
  6935. rtn
  6936. (setq opts (delq nil (mapcar (lambda (x)
  6937. (setq e (assoc x org-startup-options))
  6938. (if (eq (nth 1 e) var) e nil))
  6939. (org-split-string string "[ \t]+"))))
  6940. (if (not opts)
  6941. rtn
  6942. (setq rtn nil)
  6943. (while (setq e (pop opts))
  6944. (if (not (nth 3 e))
  6945. (setq rtn (nth 2 e))
  6946. (if (not (listp rtn)) (setq rtn nil))
  6947. (push (nth 2 e) rtn)))
  6948. rtn)))))
  6949. (defvar org-blocker-hook nil
  6950. "Hook for functions that are allowed to block a state change.
  6951. Each function gets as its single argument a property list, see
  6952. `org-trigger-hook' for more information about this list.
  6953. If any of the functions in this hook returns nil, the state change
  6954. is blocked.")
  6955. (defvar org-trigger-hook nil
  6956. "Hook for functions that are triggered by a state change.
  6957. Each function gets as its single argument a property list with at least
  6958. the following elements:
  6959. (:type type-of-change :position pos-at-entry-start
  6960. :from old-state :to new-state)
  6961. Depending on the type, more properties may be present.
  6962. This mechanism is currently implemented for:
  6963. TODO state changes
  6964. ------------------
  6965. :type todo-state-change
  6966. :from previous state (keyword as a string), or nil
  6967. :to new state (keyword as a string), or nil")
  6968. (defun org-todo (&optional arg)
  6969. "Change the TODO state of an item.
  6970. The state of an item is given by a keyword at the start of the heading,
  6971. like
  6972. *** TODO Write paper
  6973. *** DONE Call mom
  6974. The different keywords are specified in the variable `org-todo-keywords'.
  6975. By default the available states are \"TODO\" and \"DONE\".
  6976. So for this example: when the item starts with TODO, it is changed to DONE.
  6977. When it starts with DONE, the DONE is removed. And when neither TODO nor
  6978. DONE are present, add TODO at the beginning of the heading.
  6979. With C-u prefix arg, use completion to determine the new state.
  6980. With numeric prefix arg, switch to that state.
  6981. For calling through lisp, arg is also interpreted in the following way:
  6982. 'none -> empty state
  6983. \"\"(empty string) -> switch to empty state
  6984. 'done -> switch to DONE
  6985. 'nextset -> switch to the next set of keywords
  6986. 'previousset -> switch to the previous set of keywords
  6987. \"WAITING\" -> switch to the specified keyword, but only if it
  6988. really is a member of `org-todo-keywords'."
  6989. (interactive "P")
  6990. (save-excursion
  6991. (catch 'exit
  6992. (org-back-to-heading)
  6993. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  6994. (or (looking-at (concat " +" org-todo-regexp " *"))
  6995. (looking-at " *"))
  6996. (let* ((match-data (match-data))
  6997. (startpos (point-at-bol))
  6998. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  6999. (org-log-done org-log-done)
  7000. (org-log-repeat org-log-repeat)
  7001. (org-todo-log-states org-todo-log-states)
  7002. (this (match-string 1))
  7003. (hl-pos (match-beginning 0))
  7004. (head (org-get-todo-sequence-head this))
  7005. (ass (assoc head org-todo-kwd-alist))
  7006. (interpret (nth 1 ass))
  7007. (done-word (nth 3 ass))
  7008. (final-done-word (nth 4 ass))
  7009. (last-state (or this ""))
  7010. (completion-ignore-case t)
  7011. (member (member this org-todo-keywords-1))
  7012. (tail (cdr member))
  7013. (state (cond
  7014. ((and org-todo-key-trigger
  7015. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7016. (and (not arg) org-use-fast-todo-selection
  7017. (not (eq org-use-fast-todo-selection 'prefix)))))
  7018. ;; Use fast selection
  7019. (org-fast-todo-selection))
  7020. ((and (equal arg '(4))
  7021. (or (not org-use-fast-todo-selection)
  7022. (not org-todo-key-trigger)))
  7023. ;; Read a state with completion
  7024. (completing-read "State: " (mapcar (lambda(x) (list x))
  7025. org-todo-keywords-1)
  7026. nil t))
  7027. ((eq arg 'right)
  7028. (if this
  7029. (if tail (car tail) nil)
  7030. (car org-todo-keywords-1)))
  7031. ((eq arg 'left)
  7032. (if (equal member org-todo-keywords-1)
  7033. nil
  7034. (if this
  7035. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7036. org-todo-keywords-1)
  7037. (org-last org-todo-keywords-1))))
  7038. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7039. (setq arg nil))) ; hack to fall back to cycling
  7040. (arg
  7041. ;; user or caller requests a specific state
  7042. (cond
  7043. ((equal arg "") nil)
  7044. ((eq arg 'none) nil)
  7045. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7046. ((eq arg 'nextset)
  7047. (or (car (cdr (member head org-todo-heads)))
  7048. (car org-todo-heads)))
  7049. ((eq arg 'previousset)
  7050. (let ((org-todo-heads (reverse org-todo-heads)))
  7051. (or (car (cdr (member head org-todo-heads)))
  7052. (car org-todo-heads))))
  7053. ((car (member arg org-todo-keywords-1)))
  7054. ((nth (1- (prefix-numeric-value arg))
  7055. org-todo-keywords-1))))
  7056. ((null member) (or head (car org-todo-keywords-1)))
  7057. ((equal this final-done-word) nil) ;; -> make empty
  7058. ((null tail) nil) ;; -> first entry
  7059. ((eq interpret 'sequence)
  7060. (car tail))
  7061. ((memq interpret '(type priority))
  7062. (if (eq this-command last-command)
  7063. (car tail)
  7064. (if (> (length tail) 0)
  7065. (or done-word (car org-done-keywords))
  7066. nil)))
  7067. (t nil)))
  7068. (next (if state (concat " " state " ") " "))
  7069. (change-plist (list :type 'todo-state-change :from this :to state
  7070. :position startpos))
  7071. dolog now-done-p)
  7072. (when org-blocker-hook
  7073. (unless (save-excursion
  7074. (save-match-data
  7075. (run-hook-with-args-until-failure
  7076. 'org-blocker-hook change-plist)))
  7077. (if (interactive-p)
  7078. (error "TODO state change from %s to %s blocked" this state)
  7079. ;; fail silently
  7080. (message "TODO state change from %s to %s blocked" this state)
  7081. (throw 'exit nil))))
  7082. (store-match-data match-data)
  7083. (replace-match next t t)
  7084. (unless (pos-visible-in-window-p hl-pos)
  7085. (message "TODO state changed to %s" (org-trim next)))
  7086. (unless head
  7087. (setq head (org-get-todo-sequence-head state)
  7088. ass (assoc head org-todo-kwd-alist)
  7089. interpret (nth 1 ass)
  7090. done-word (nth 3 ass)
  7091. final-done-word (nth 4 ass)))
  7092. (when (memq arg '(nextset previousset))
  7093. (message "Keyword-Set %d/%d: %s"
  7094. (- (length org-todo-sets) -1
  7095. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7096. (length org-todo-sets)
  7097. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7098. (setq org-last-todo-state-is-todo
  7099. (not (member state org-done-keywords)))
  7100. (setq now-done-p (and (member state org-done-keywords)
  7101. (not (member this org-done-keywords))))
  7102. (and logging (org-local-logging logging))
  7103. (when (and (or org-todo-log-states org-log-done)
  7104. (not (memq arg '(nextset previousset))))
  7105. ;; we need to look at recording a time and note
  7106. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7107. (nth 2 (assoc this org-todo-log-states))))
  7108. (when (and state
  7109. (member state org-not-done-keywords)
  7110. (not (member this org-not-done-keywords)))
  7111. ;; This is now a todo state and was not one before
  7112. ;; If there was a CLOSED time stamp, get rid of it.
  7113. (org-add-planning-info nil nil 'closed))
  7114. (when (and now-done-p org-log-done)
  7115. ;; It is now done, and it was not done before
  7116. (org-add-planning-info 'closed (org-current-time))
  7117. (if (and (not dolog) (eq 'note org-log-done))
  7118. (org-add-log-setup 'done state 'findpos 'note)))
  7119. (when (and state dolog)
  7120. ;; This is a non-nil state, and we need to log it
  7121. (org-add-log-setup 'state state 'findpos dolog)))
  7122. ;; Fixup tag positioning
  7123. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7124. (run-hooks 'org-after-todo-state-change-hook)
  7125. (if (and arg (not (member state org-done-keywords)))
  7126. (setq head (org-get-todo-sequence-head state)))
  7127. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7128. ;; Do we need to trigger a repeat?
  7129. (when now-done-p (org-auto-repeat-maybe state))
  7130. ;; Fixup cursor location if close to the keyword
  7131. (if (and (outline-on-heading-p)
  7132. (not (bolp))
  7133. (save-excursion (beginning-of-line 1)
  7134. (looking-at org-todo-line-regexp))
  7135. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7136. (progn
  7137. (goto-char (or (match-end 2) (match-end 1)))
  7138. (just-one-space)))
  7139. (when org-trigger-hook
  7140. (save-excursion
  7141. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7142. (defun org-local-logging (value)
  7143. "Get logging settings from a property VALUE."
  7144. (let* (words w a)
  7145. ;; directly set the variables, they are already local.
  7146. (setq org-log-done nil
  7147. org-log-repeat nil
  7148. org-todo-log-states nil)
  7149. (setq words (org-split-string value))
  7150. (while (setq w (pop words))
  7151. (cond
  7152. ((setq a (assoc w org-startup-options))
  7153. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7154. (set (nth 1 a) (nth 2 a))))
  7155. ((setq a (org-extract-log-state-settings w))
  7156. (and (member (car a) org-todo-keywords-1)
  7157. (push a org-todo-log-states)))))))
  7158. (defun org-get-todo-sequence-head (kwd)
  7159. "Return the head of the TODO sequence to which KWD belongs.
  7160. If KWD is not set, check if there is a text property remembering the
  7161. right sequence."
  7162. (let (p)
  7163. (cond
  7164. ((not kwd)
  7165. (or (get-text-property (point-at-bol) 'org-todo-head)
  7166. (progn
  7167. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7168. nil (point-at-eol)))
  7169. (get-text-property p 'org-todo-head))))
  7170. ((not (member kwd org-todo-keywords-1))
  7171. (car org-todo-keywords-1))
  7172. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7173. (defun org-fast-todo-selection ()
  7174. "Fast TODO keyword selection with single keys.
  7175. Returns the new TODO keyword, or nil if no state change should occur."
  7176. (let* ((fulltable org-todo-key-alist)
  7177. (done-keywords org-done-keywords) ;; needed for the faces.
  7178. (maxlen (apply 'max (mapcar
  7179. (lambda (x)
  7180. (if (stringp (car x)) (string-width (car x)) 0))
  7181. fulltable)))
  7182. (expert nil)
  7183. (fwidth (+ maxlen 3 1 3))
  7184. (ncol (/ (- (window-width) 4) fwidth))
  7185. tg cnt e c tbl
  7186. groups ingroup)
  7187. (save-window-excursion
  7188. (if expert
  7189. (set-buffer (get-buffer-create " *Org todo*"))
  7190. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7191. (erase-buffer)
  7192. (org-set-local 'org-done-keywords done-keywords)
  7193. (setq tbl fulltable cnt 0)
  7194. (while (setq e (pop tbl))
  7195. (cond
  7196. ((equal e '(:startgroup))
  7197. (push '() groups) (setq ingroup t)
  7198. (when (not (= cnt 0))
  7199. (setq cnt 0)
  7200. (insert "\n"))
  7201. (insert "{ "))
  7202. ((equal e '(:endgroup))
  7203. (setq ingroup nil cnt 0)
  7204. (insert "}\n"))
  7205. (t
  7206. (setq tg (car e) c (cdr e))
  7207. (if ingroup (push tg (car groups)))
  7208. (setq tg (org-add-props tg nil 'face
  7209. (org-get-todo-face tg)))
  7210. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7211. (insert "[" c "] " tg (make-string
  7212. (- fwidth 4 (length tg)) ?\ ))
  7213. (when (= (setq cnt (1+ cnt)) ncol)
  7214. (insert "\n")
  7215. (if ingroup (insert " "))
  7216. (setq cnt 0)))))
  7217. (insert "\n")
  7218. (goto-char (point-min))
  7219. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7220. (fit-window-to-buffer))
  7221. (message "[a-z..]:Set [SPC]:clear")
  7222. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7223. (cond
  7224. ((or (= c ?\C-g)
  7225. (and (= c ?q) (not (rassoc c fulltable))))
  7226. (setq quit-flag t))
  7227. ((= c ?\ ) nil)
  7228. ((setq e (rassoc c fulltable) tg (car e))
  7229. tg)
  7230. (t (setq quit-flag t))))))
  7231. (defun org-entry-is-todo-p ()
  7232. (member (org-get-todo-state) org-not-done-keywords))
  7233. (defun org-entry-is-done-p ()
  7234. (member (org-get-todo-state) org-done-keywords))
  7235. (defun org-get-todo-state ()
  7236. (save-excursion
  7237. (org-back-to-heading t)
  7238. (and (looking-at org-todo-line-regexp)
  7239. (match-end 2)
  7240. (match-string 2))))
  7241. (defun org-at-date-range-p (&optional inactive-ok)
  7242. "Is the cursor inside a date range?"
  7243. (interactive)
  7244. (save-excursion
  7245. (catch 'exit
  7246. (let ((pos (point)))
  7247. (skip-chars-backward "^[<\r\n")
  7248. (skip-chars-backward "<[")
  7249. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7250. (>= (match-end 0) pos)
  7251. (throw 'exit t))
  7252. (skip-chars-backward "^<[\r\n")
  7253. (skip-chars-backward "<[")
  7254. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7255. (>= (match-end 0) pos)
  7256. (throw 'exit t)))
  7257. nil)))
  7258. (defun org-get-repeat ()
  7259. "Check if tere is a deadline/schedule with repeater in this entry."
  7260. (save-match-data
  7261. (save-excursion
  7262. (org-back-to-heading t)
  7263. (if (re-search-forward
  7264. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7265. (match-string 1)))))
  7266. (defvar org-last-changed-timestamp)
  7267. (defvar org-log-post-message)
  7268. (defvar org-log-note-purpose)
  7269. (defvar org-log-note-how)
  7270. (defun org-auto-repeat-maybe (done-word)
  7271. "Check if the current headline contains a repeated deadline/schedule.
  7272. If yes, set TODO state back to what it was and change the base date
  7273. of repeating deadline/scheduled time stamps to new date.
  7274. This function is run automatically after each state change to a DONE state."
  7275. ;; last-state is dynamically scoped into this function
  7276. (let* ((repeat (org-get-repeat))
  7277. (aa (assoc last-state org-todo-kwd-alist))
  7278. (interpret (nth 1 aa))
  7279. (head (nth 2 aa))
  7280. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7281. (msg "Entry repeats: ")
  7282. (org-log-done nil)
  7283. (org-todo-log-states nil)
  7284. (nshiftmax 10) (nshift 0)
  7285. re type n what ts mb0 time)
  7286. (when repeat
  7287. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7288. (org-todo (if (eq interpret 'type) last-state head))
  7289. (when org-log-repeat
  7290. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7291. (memq 'org-add-log-note post-command-hook))
  7292. ;; OK, we are already setup for some record
  7293. (if (eq org-log-repeat 'note)
  7294. ;; make sure we take a note, not only a time stamp
  7295. (setq org-log-note-how 'note))
  7296. ;; Set up for taking a record
  7297. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7298. 'findpos org-log-repeat)))
  7299. (org-back-to-heading t)
  7300. (org-add-planning-info nil nil 'closed)
  7301. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7302. org-deadline-time-regexp "\\)\\|\\("
  7303. org-ts-regexp "\\)"))
  7304. (while (re-search-forward
  7305. re (save-excursion (outline-next-heading) (point)) t)
  7306. (setq type (if (match-end 1) org-scheduled-string
  7307. (if (match-end 3) org-deadline-string "Plain:"))
  7308. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7309. mb0 (match-beginning 0))
  7310. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7311. (setq n (string-to-number (match-string 2 ts))
  7312. what (match-string 3 ts))
  7313. (if (equal what "w") (setq n (* n 7) what "d"))
  7314. ;; Preparation, see if we need to modify the start date for the change
  7315. (when (match-end 1)
  7316. (setq time (save-match-data (org-time-string-to-time ts)))
  7317. (cond
  7318. ((equal (match-string 1 ts) ".")
  7319. ;; Shift starting date to today
  7320. (org-timestamp-change
  7321. (- (time-to-days (current-time)) (time-to-days time))
  7322. 'day))
  7323. ((equal (match-string 1 ts) "+")
  7324. (while (or (= nshift 0)
  7325. (<= (time-to-days time) (time-to-days (current-time))))
  7326. (when (= (incf nshift) nshiftmax)
  7327. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7328. (error "Abort")))
  7329. (org-timestamp-change n (cdr (assoc what whata)))
  7330. (org-at-timestamp-p t)
  7331. (setq ts (match-string 1))
  7332. (setq time (save-match-data (org-time-string-to-time ts))))
  7333. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7334. ;; rematch, so that we have everything in place for the real shift
  7335. (org-at-timestamp-p t)
  7336. (setq ts (match-string 1))
  7337. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7338. (org-timestamp-change n (cdr (assoc what whata)))
  7339. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7340. (setq org-log-post-message msg)
  7341. (message "%s" msg))))
  7342. (defun org-show-todo-tree (arg)
  7343. "Make a compact tree which shows all headlines marked with TODO.
  7344. The tree will show the lines where the regexp matches, and all higher
  7345. headlines above the match.
  7346. With a \\[universal-argument] prefix, also show the DONE entries.
  7347. With a numeric prefix N, construct a sparse tree for the Nth element
  7348. of `org-todo-keywords-1'."
  7349. (interactive "P")
  7350. (let ((case-fold-search nil)
  7351. (kwd-re
  7352. (cond ((null arg) org-not-done-regexp)
  7353. ((equal arg '(4))
  7354. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7355. (mapcar 'list org-todo-keywords-1))))
  7356. (concat "\\("
  7357. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7358. "\\)\\>")))
  7359. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7360. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7361. org-todo-keywords-1)))
  7362. (t (error "Invalid prefix argument: %s" arg)))))
  7363. (message "%d TODO entries found"
  7364. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7365. (defun org-deadline (&optional remove)
  7366. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7367. With argument REMOVE, remove any deadline from the item."
  7368. (interactive "P")
  7369. (if remove
  7370. (progn
  7371. (org-remove-timestamp-with-keyword org-deadline-string)
  7372. (message "Item no longer has a deadline."))
  7373. (org-add-planning-info 'deadline nil 'closed)))
  7374. (defun org-schedule (&optional remove)
  7375. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7376. With argument REMOVE, remove any scheduling date from the item."
  7377. (interactive "P")
  7378. (if remove
  7379. (progn
  7380. (org-remove-timestamp-with-keyword org-scheduled-string)
  7381. (message "Item is no longer scheduled."))
  7382. (org-add-planning-info 'scheduled nil 'closed)))
  7383. (defun org-remove-timestamp-with-keyword (keyword)
  7384. "Remove all time stamps with KEYWORD in the current entry."
  7385. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7386. beg)
  7387. (save-excursion
  7388. (org-back-to-heading t)
  7389. (setq beg (point))
  7390. (org-end-of-subtree t t)
  7391. (while (re-search-backward re beg t)
  7392. (replace-match "")
  7393. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7394. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7395. (defun org-add-planning-info (what &optional time &rest remove)
  7396. "Insert new timestamp with keyword in the line directly after the headline.
  7397. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7398. If non is given, the user is prompted for a date.
  7399. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7400. be removed."
  7401. (interactive)
  7402. (let (org-time-was-given org-end-time-was-given ts
  7403. end default-time default-input)
  7404. (when (and (not time) (memq what '(scheduled deadline)))
  7405. ;; Try to get a default date/time from existing timestamp
  7406. (save-excursion
  7407. (org-back-to-heading t)
  7408. (setq end (save-excursion (outline-next-heading) (point)))
  7409. (when (re-search-forward (if (eq what 'scheduled)
  7410. org-scheduled-time-regexp
  7411. org-deadline-time-regexp)
  7412. end t)
  7413. (setq ts (match-string 1)
  7414. default-time
  7415. (apply 'encode-time (org-parse-time-string ts))
  7416. default-input (and ts (org-get-compact-tod ts))))))
  7417. (when what
  7418. ;; If necessary, get the time from the user
  7419. (setq time (or time (org-read-date nil 'to-time nil nil
  7420. default-time default-input))))
  7421. (when (and org-insert-labeled-timestamps-at-point
  7422. (member what '(scheduled deadline)))
  7423. (insert
  7424. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7425. (org-insert-time-stamp time org-time-was-given
  7426. nil nil nil (list org-end-time-was-given))
  7427. (setq what nil))
  7428. (save-excursion
  7429. (save-restriction
  7430. (let (col list elt ts buffer-invisibility-spec)
  7431. (org-back-to-heading t)
  7432. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7433. (goto-char (match-end 1))
  7434. (setq col (current-column))
  7435. (goto-char (match-end 0))
  7436. (if (eobp) (insert "\n") (forward-char 1))
  7437. (if (and (not (looking-at outline-regexp))
  7438. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7439. "[^\r\n]*"))
  7440. (not (equal (match-string 1) org-clock-string)))
  7441. (narrow-to-region (match-beginning 0) (match-end 0))
  7442. (insert-before-markers "\n")
  7443. (backward-char 1)
  7444. (narrow-to-region (point) (point))
  7445. (indent-to-column col))
  7446. ;; Check if we have to remove something.
  7447. (setq list (cons what remove))
  7448. (while list
  7449. (setq elt (pop list))
  7450. (goto-char (point-min))
  7451. (when (or (and (eq elt 'scheduled)
  7452. (re-search-forward org-scheduled-time-regexp nil t))
  7453. (and (eq elt 'deadline)
  7454. (re-search-forward org-deadline-time-regexp nil t))
  7455. (and (eq elt 'closed)
  7456. (re-search-forward org-closed-time-regexp nil t)))
  7457. (replace-match "")
  7458. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7459. (if (looking-at " +") (replace-match ""))))
  7460. (goto-char (point-max))
  7461. (when what
  7462. (insert
  7463. (if (not (equal (char-before) ?\ )) " " "")
  7464. (cond ((eq what 'scheduled) org-scheduled-string)
  7465. ((eq what 'deadline) org-deadline-string)
  7466. ((eq what 'closed) org-closed-string))
  7467. " ")
  7468. (setq ts (org-insert-time-stamp
  7469. time
  7470. (or org-time-was-given
  7471. (and (eq what 'closed) org-log-done-with-time))
  7472. (eq what 'closed)
  7473. nil nil (list org-end-time-was-given)))
  7474. (end-of-line 1))
  7475. (goto-char (point-min))
  7476. (widen)
  7477. (if (looking-at "[ \t]+\r?\n")
  7478. (replace-match ""))
  7479. ts)))))
  7480. (defvar org-log-note-marker (make-marker))
  7481. (defvar org-log-note-purpose nil)
  7482. (defvar org-log-note-state nil)
  7483. (defvar org-log-note-how nil)
  7484. (defvar org-log-note-window-configuration nil)
  7485. (defvar org-log-note-return-to (make-marker))
  7486. (defvar org-log-post-message nil
  7487. "Message to be displayed after a log note has been stored.
  7488. The auto-repeater uses this.")
  7489. (defun org-add-note ()
  7490. "Add a note to the current entry.
  7491. This is done in the same way as adding a state change note."
  7492. (interactive)
  7493. (org-add-log-setup 'note nil t nil))
  7494. (defun org-add-log-setup (&optional purpose state findpos how)
  7495. "Set up the post command hook to take a note.
  7496. If this is about to TODO state change, the new state is expected in STATE.
  7497. When FINDPOS is non-nil, find the correct position for the note in
  7498. the current entry. If not, assume that it can be inserted at point."
  7499. (save-excursion
  7500. (when findpos
  7501. (org-back-to-heading t)
  7502. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7503. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7504. "[^\r\n]*\\)?"))
  7505. (goto-char (match-end 0))
  7506. (unless org-log-states-order-reversed
  7507. (and (= (char-after) ?\n) (forward-char 1))
  7508. (org-skip-over-state-notes)
  7509. (skip-chars-backward " \t\n\r")))
  7510. (move-marker org-log-note-marker (point))
  7511. (setq org-log-note-purpose purpose
  7512. org-log-note-state state
  7513. org-log-note-how how)
  7514. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7515. (defun org-skip-over-state-notes ()
  7516. "Skip past the list of State notes in an entry."
  7517. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7518. (while (looking-at "[ \t]*- State")
  7519. (condition-case nil
  7520. (org-next-item)
  7521. (error (org-end-of-item)))))
  7522. (defun org-add-log-note (&optional purpose)
  7523. "Pop up a window for taking a note, and add this note later at point."
  7524. (remove-hook 'post-command-hook 'org-add-log-note)
  7525. (setq org-log-note-window-configuration (current-window-configuration))
  7526. (delete-other-windows)
  7527. (move-marker org-log-note-return-to (point))
  7528. (switch-to-buffer (marker-buffer org-log-note-marker))
  7529. (goto-char org-log-note-marker)
  7530. (org-switch-to-buffer-other-window "*Org Note*")
  7531. (erase-buffer)
  7532. (if (memq org-log-note-how '(time state))
  7533. (org-store-log-note)
  7534. (let ((org-inhibit-startup t)) (org-mode))
  7535. (insert (format "# Insert note for %s.
  7536. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7537. (cond
  7538. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7539. ((eq org-log-note-purpose 'done) "closed todo item")
  7540. ((eq org-log-note-purpose 'state)
  7541. (format "state change to \"%s\"" org-log-note-state))
  7542. ((eq org-log-note-purpose 'note)
  7543. "this entry")
  7544. (t (error "This should not happen")))))
  7545. (org-set-local 'org-finish-function 'org-store-log-note)))
  7546. (defvar org-note-abort nil) ; dynamically scoped
  7547. (defun org-store-log-note ()
  7548. "Finish taking a log note, and insert it to where it belongs."
  7549. (let ((txt (buffer-string))
  7550. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7551. lines ind)
  7552. (kill-buffer (current-buffer))
  7553. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7554. (setq txt (replace-match "" t t txt)))
  7555. (if (string-match "\\s-+\\'" txt)
  7556. (setq txt (replace-match "" t t txt)))
  7557. (setq lines (org-split-string txt "\n"))
  7558. (when (and note (string-match "\\S-" note))
  7559. (setq note
  7560. (org-replace-escapes
  7561. note
  7562. (list (cons "%u" (user-login-name))
  7563. (cons "%U" user-full-name)
  7564. (cons "%t" (format-time-string
  7565. (org-time-stamp-format 'long 'inactive)
  7566. (current-time)))
  7567. (cons "%s" (if org-log-note-state
  7568. (concat "\"" org-log-note-state "\"")
  7569. "")))))
  7570. (if lines (setq note (concat note " \\\\")))
  7571. (push note lines))
  7572. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7573. (when lines
  7574. (save-excursion
  7575. (set-buffer (marker-buffer org-log-note-marker))
  7576. (save-excursion
  7577. (goto-char org-log-note-marker)
  7578. (move-marker org-log-note-marker nil)
  7579. (end-of-line 1)
  7580. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7581. (indent-relative nil)
  7582. (insert "- " (pop lines))
  7583. (org-indent-line-function)
  7584. (beginning-of-line 1)
  7585. (looking-at "[ \t]*")
  7586. (setq ind (concat (match-string 0) " "))
  7587. (end-of-line 1)
  7588. (while lines (insert "\n" ind (pop lines)))))))
  7589. (set-window-configuration org-log-note-window-configuration)
  7590. (with-current-buffer (marker-buffer org-log-note-return-to)
  7591. (goto-char org-log-note-return-to))
  7592. (move-marker org-log-note-return-to nil)
  7593. (and org-log-post-message (message "%s" org-log-post-message)))
  7594. (defun org-sparse-tree (&optional arg)
  7595. "Create a sparse tree, prompt for the details.
  7596. This command can create sparse trees. You first need to select the type
  7597. of match used to create the tree:
  7598. t Show entries with a specific TODO keyword.
  7599. T Show entries selected by a tags match.
  7600. p Enter a property name and its value (both with completion on existing
  7601. names/values) and show entries with that property.
  7602. r Show entries matching a regular expression
  7603. d Show deadlines due within `org-deadline-warning-days'."
  7604. (interactive "P")
  7605. (let (ans kwd value)
  7606. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7607. (setq ans (read-char-exclusive))
  7608. (cond
  7609. ((equal ans ?d)
  7610. (call-interactively 'org-check-deadlines))
  7611. ((equal ans ?b)
  7612. (call-interactively 'org-check-before-date))
  7613. ((equal ans ?t)
  7614. (org-show-todo-tree '(4)))
  7615. ((equal ans ?T)
  7616. (call-interactively 'org-tags-sparse-tree))
  7617. ((member ans '(?p ?P))
  7618. (setq kwd (completing-read "Property: "
  7619. (mapcar 'list (org-buffer-property-keys))))
  7620. (setq value (completing-read "Value: "
  7621. (mapcar 'list (org-property-values kwd))))
  7622. (unless (string-match "\\`{.*}\\'" value)
  7623. (setq value (concat "\"" value "\"")))
  7624. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7625. ((member ans '(?r ?R ?/))
  7626. (call-interactively 'org-occur))
  7627. (t (error "No such sparse tree command \"%c\"" ans)))))
  7628. (defvar org-occur-highlights nil
  7629. "List of overlays used for occur matches.")
  7630. (make-variable-buffer-local 'org-occur-highlights)
  7631. (defvar org-occur-parameters nil
  7632. "Parameters of the active org-occur calls.
  7633. This is a list, each call to org-occur pushes as cons cell,
  7634. containing the regular expression and the callback, onto the list.
  7635. The list can contain several entries if `org-occur' has been called
  7636. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7637. will only contain one set of parameters. When the highlights are
  7638. removed (for example with `C-c C-c', or with the next edit (depending
  7639. on `org-remove-highlights-with-change'), this variable is emptied
  7640. as well.")
  7641. (make-variable-buffer-local 'org-occur-parameters)
  7642. (defun org-occur (regexp &optional keep-previous callback)
  7643. "Make a compact tree which shows all matches of REGEXP.
  7644. The tree will show the lines where the regexp matches, and all higher
  7645. headlines above the match. It will also show the heading after the match,
  7646. to make sure editing the matching entry is easy.
  7647. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7648. call to `org-occur' will be kept, to allow stacking of calls to this
  7649. command.
  7650. If CALLBACK is non-nil, it is a function which is called to confirm
  7651. that the match should indeed be shown."
  7652. (interactive "sRegexp: \nP")
  7653. (unless keep-previous
  7654. (org-remove-occur-highlights nil nil t))
  7655. (push (cons regexp callback) org-occur-parameters)
  7656. (let ((cnt 0))
  7657. (save-excursion
  7658. (goto-char (point-min))
  7659. (if (or (not keep-previous) ; do not want to keep
  7660. (not org-occur-highlights)) ; no previous matches
  7661. ;; hide everything
  7662. (org-overview))
  7663. (while (re-search-forward regexp nil t)
  7664. (when (or (not callback)
  7665. (save-match-data (funcall callback)))
  7666. (setq cnt (1+ cnt))
  7667. (when org-highlight-sparse-tree-matches
  7668. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7669. (org-show-context 'occur-tree))))
  7670. (when org-remove-highlights-with-change
  7671. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7672. nil 'local))
  7673. (unless org-sparse-tree-open-archived-trees
  7674. (org-hide-archived-subtrees (point-min) (point-max)))
  7675. (run-hooks 'org-occur-hook)
  7676. (if (interactive-p)
  7677. (message "%d match(es) for regexp %s" cnt regexp))
  7678. cnt))
  7679. (defun org-show-context (&optional key)
  7680. "Make sure point and context and visible.
  7681. How much context is shown depends upon the variables
  7682. `org-show-hierarchy-above', `org-show-following-heading'. and
  7683. `org-show-siblings'."
  7684. (let ((heading-p (org-on-heading-p t))
  7685. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7686. (following-p (org-get-alist-option org-show-following-heading key))
  7687. (entry-p (org-get-alist-option org-show-entry-below key))
  7688. (siblings-p (org-get-alist-option org-show-siblings key)))
  7689. (catch 'exit
  7690. ;; Show heading or entry text
  7691. (if (and heading-p (not entry-p))
  7692. (org-flag-heading nil) ; only show the heading
  7693. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7694. (org-show-hidden-entry))) ; show entire entry
  7695. (when following-p
  7696. ;; Show next sibling, or heading below text
  7697. (save-excursion
  7698. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7699. (org-flag-heading nil))))
  7700. (when siblings-p (org-show-siblings))
  7701. (when hierarchy-p
  7702. ;; show all higher headings, possibly with siblings
  7703. (save-excursion
  7704. (while (and (condition-case nil
  7705. (progn (org-up-heading-all 1) t)
  7706. (error nil))
  7707. (not (bobp)))
  7708. (org-flag-heading nil)
  7709. (when siblings-p (org-show-siblings))))))))
  7710. (defun org-reveal (&optional siblings)
  7711. "Show current entry, hierarchy above it, and the following headline.
  7712. This can be used to show a consistent set of context around locations
  7713. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7714. not t for the search context.
  7715. With optional argument SIBLINGS, on each level of the hierarchy all
  7716. siblings are shown. This repairs the tree structure to what it would
  7717. look like when opened with hierarchical calls to `org-cycle'."
  7718. (interactive "P")
  7719. (let ((org-show-hierarchy-above t)
  7720. (org-show-following-heading t)
  7721. (org-show-siblings (if siblings t org-show-siblings)))
  7722. (org-show-context nil)))
  7723. (defun org-highlight-new-match (beg end)
  7724. "Highlight from BEG to END and mark the highlight is an occur headline."
  7725. (let ((ov (org-make-overlay beg end)))
  7726. (org-overlay-put ov 'face 'secondary-selection)
  7727. (push ov org-occur-highlights)))
  7728. (defun org-remove-occur-highlights (&optional beg end noremove)
  7729. "Remove the occur highlights from the buffer.
  7730. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7731. from the `before-change-functions' in the current buffer."
  7732. (interactive)
  7733. (unless org-inhibit-highlight-removal
  7734. (mapc 'org-delete-overlay org-occur-highlights)
  7735. (setq org-occur-highlights nil)
  7736. (setq org-occur-parameters nil)
  7737. (unless noremove
  7738. (remove-hook 'before-change-functions
  7739. 'org-remove-occur-highlights 'local))))
  7740. ;;;; Priorities
  7741. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7742. "Regular expression matching the priority indicator.")
  7743. (defvar org-remove-priority-next-time nil)
  7744. (defun org-priority-up ()
  7745. "Increase the priority of the current item."
  7746. (interactive)
  7747. (org-priority 'up))
  7748. (defun org-priority-down ()
  7749. "Decrease the priority of the current item."
  7750. (interactive)
  7751. (org-priority 'down))
  7752. (defun org-priority (&optional action)
  7753. "Change the priority of an item by ARG.
  7754. ACTION can be `set', `up', `down', or a character."
  7755. (interactive)
  7756. (setq action (or action 'set))
  7757. (let (current new news have remove)
  7758. (save-excursion
  7759. (org-back-to-heading)
  7760. (if (looking-at org-priority-regexp)
  7761. (setq current (string-to-char (match-string 2))
  7762. have t)
  7763. (setq current org-default-priority))
  7764. (cond
  7765. ((or (eq action 'set)
  7766. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7767. (if (not (eq action 'set))
  7768. (setq new action)
  7769. (message "Priority %c-%c, SPC to remove: "
  7770. org-highest-priority org-lowest-priority)
  7771. (setq new (read-char-exclusive)))
  7772. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7773. (= (upcase org-lowest-priority) org-lowest-priority))
  7774. (setq new (upcase new)))
  7775. (cond ((equal new ?\ ) (setq remove t))
  7776. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7777. (error "Priority must be between `%c' and `%c'"
  7778. org-highest-priority org-lowest-priority))))
  7779. ((eq action 'up)
  7780. (if (and (not have) (eq last-command this-command))
  7781. (setq new org-lowest-priority)
  7782. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7783. org-default-priority (1- current)))))
  7784. ((eq action 'down)
  7785. (if (and (not have) (eq last-command this-command))
  7786. (setq new org-highest-priority)
  7787. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7788. org-default-priority (1+ current)))))
  7789. (t (error "Invalid action")))
  7790. (if (or (< (upcase new) org-highest-priority)
  7791. (> (upcase new) org-lowest-priority))
  7792. (setq remove t))
  7793. (setq news (format "%c" new))
  7794. (if have
  7795. (if remove
  7796. (replace-match "" t t nil 1)
  7797. (replace-match news t t nil 2))
  7798. (if remove
  7799. (error "No priority cookie found in line")
  7800. (looking-at org-todo-line-regexp)
  7801. (if (match-end 2)
  7802. (progn
  7803. (goto-char (match-end 2))
  7804. (insert " [#" news "]"))
  7805. (goto-char (match-beginning 3))
  7806. (insert "[#" news "] ")))))
  7807. (org-preserve-lc (org-set-tags nil 'align))
  7808. (if remove
  7809. (message "Priority removed")
  7810. (message "Priority of current item set to %s" news))))
  7811. (defun org-get-priority (s)
  7812. "Find priority cookie and return priority."
  7813. (save-match-data
  7814. (if (not (string-match org-priority-regexp s))
  7815. (* 1000 (- org-lowest-priority org-default-priority))
  7816. (* 1000 (- org-lowest-priority
  7817. (string-to-char (match-string 2 s)))))))
  7818. ;;;; Tags
  7819. (defun org-scan-tags (action matcher &optional todo-only)
  7820. "Scan headline tags with inheritance and produce output ACTION.
  7821. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7822. evaluated, testing if a given set of tags qualifies a headline for
  7823. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7824. are included in the output."
  7825. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7826. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7827. (org-re
  7828. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7829. (props (list 'face nil
  7830. 'done-face 'org-done
  7831. 'undone-face nil
  7832. 'mouse-face 'highlight
  7833. 'org-not-done-regexp org-not-done-regexp
  7834. 'org-todo-regexp org-todo-regexp
  7835. 'keymap org-agenda-keymap
  7836. 'help-echo
  7837. (format "mouse-2 or RET jump to org file %s"
  7838. (abbreviate-file-name
  7839. (or (buffer-file-name (buffer-base-buffer))
  7840. (buffer-name (buffer-base-buffer)))))))
  7841. (case-fold-search nil)
  7842. lspos
  7843. tags tags-list tags-alist (llast 0) rtn level category i txt
  7844. todo marker entry priority)
  7845. (save-excursion
  7846. (goto-char (point-min))
  7847. (when (eq action 'sparse-tree)
  7848. (org-overview)
  7849. (org-remove-occur-highlights))
  7850. (while (re-search-forward re nil t)
  7851. (catch :skip
  7852. (setq todo (if (match-end 1) (match-string 2))
  7853. tags (if (match-end 4) (match-string 4)))
  7854. (goto-char (setq lspos (1+ (match-beginning 0))))
  7855. (setq level (org-reduced-level (funcall outline-level))
  7856. category (org-get-category))
  7857. (setq i llast llast level)
  7858. ;; remove tag lists from same and sublevels
  7859. (while (>= i level)
  7860. (when (setq entry (assoc i tags-alist))
  7861. (setq tags-alist (delete entry tags-alist)))
  7862. (setq i (1- i)))
  7863. ;; add the next tags
  7864. (when tags
  7865. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  7866. tags-alist
  7867. (cons (cons level tags) tags-alist)))
  7868. ;; compile tags for current headline
  7869. (setq tags-list
  7870. (if org-use-tag-inheritance
  7871. (apply 'append (mapcar 'cdr tags-alist))
  7872. tags))
  7873. (when (and tags org-use-tag-inheritance
  7874. (not (eq t org-use-tag-inheritance)))
  7875. ;; selective inheritance, remove uninherited ones
  7876. (setcdr (car tags-alist)
  7877. (org-remove-uniherited-tags (cdar tags-alist))))
  7878. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  7879. (eval matcher)
  7880. (or (not org-agenda-skip-archived-trees)
  7881. (not (member org-archive-tag tags-list))))
  7882. (and (eq action 'agenda) (org-agenda-skip))
  7883. ;; list this headline
  7884. (if (eq action 'sparse-tree)
  7885. (progn
  7886. (and org-highlight-sparse-tree-matches
  7887. (org-get-heading) (match-end 0)
  7888. (org-highlight-new-match
  7889. (match-beginning 0) (match-beginning 1)))
  7890. (org-show-context 'tags-tree))
  7891. (setq txt (org-format-agenda-item
  7892. ""
  7893. (concat
  7894. (if org-tags-match-list-sublevels
  7895. (make-string (1- level) ?.) "")
  7896. (org-get-heading))
  7897. category tags-list)
  7898. priority (org-get-priority txt))
  7899. (goto-char lspos)
  7900. (setq marker (org-agenda-new-marker))
  7901. (org-add-props txt props
  7902. 'org-marker marker 'org-hd-marker marker 'org-category category
  7903. 'priority priority 'type "tagsmatch")
  7904. (push txt rtn))
  7905. ;; if we are to skip sublevels, jump to end of subtree
  7906. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  7907. (when (and (eq action 'sparse-tree)
  7908. (not org-sparse-tree-open-archived-trees))
  7909. (org-hide-archived-subtrees (point-min) (point-max)))
  7910. (nreverse rtn)))
  7911. (defun org-remove-uniherited-tags (tags)
  7912. "Remove all tags that are not inherited from the list TAGS."
  7913. (cond
  7914. ((eq org-use-tag-inheritance t) tags)
  7915. ((not org-use-tag-inheritance) nil)
  7916. ((stringp org-use-tag-inheritance)
  7917. (delq nil (mapcar
  7918. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  7919. tags)))
  7920. ((listp org-use-tag-inheritance)
  7921. (org-delete-all org-use-tag-inheritance tags))))
  7922. (defvar todo-only) ;; dynamically scoped
  7923. (defun org-tags-sparse-tree (&optional todo-only match)
  7924. "Create a sparse tree according to tags string MATCH.
  7925. MATCH can contain positive and negative selection of tags, like
  7926. \"+WORK+URGENT-WITHBOSS\".
  7927. If optional argument TODO_ONLY is non-nil, only select lines that are
  7928. also TODO lines."
  7929. (interactive "P")
  7930. (org-prepare-agenda-buffers (list (current-buffer)))
  7931. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  7932. (defvar org-cached-props nil)
  7933. (defun org-cached-entry-get (pom property)
  7934. (if (or (eq t org-use-property-inheritance)
  7935. (and (stringp org-use-property-inheritance)
  7936. (string-match org-use-property-inheritance property))
  7937. (and (listp org-use-property-inheritance)
  7938. (member property org-use-property-inheritance)))
  7939. ;; Caching is not possible, check it directly
  7940. (org-entry-get pom property 'inherit)
  7941. ;; Get all properties, so that we can do complicated checks easily
  7942. (cdr (assoc property (or org-cached-props
  7943. (setq org-cached-props
  7944. (org-entry-properties pom)))))))
  7945. (defun org-global-tags-completion-table (&optional files)
  7946. "Return the list of all tags in all agenda buffer/files."
  7947. (save-excursion
  7948. (org-uniquify
  7949. (delq nil
  7950. (apply 'append
  7951. (mapcar
  7952. (lambda (file)
  7953. (set-buffer (find-file-noselect file))
  7954. (append (org-get-buffer-tags)
  7955. (mapcar (lambda (x) (if (stringp (car-safe x))
  7956. (list (car-safe x)) nil))
  7957. org-tag-alist)))
  7958. (if (and files (car files))
  7959. files
  7960. (org-agenda-files))))))))
  7961. (defun org-make-tags-matcher (match)
  7962. "Create the TAGS//TODO matcher form for the selection string MATCH."
  7963. ;; todo-only is scoped dynamically into this function, and the function
  7964. ;; may change it it the matcher asksk for it.
  7965. (unless match
  7966. ;; Get a new match request, with completion
  7967. (let ((org-last-tags-completion-table
  7968. (org-global-tags-completion-table)))
  7969. (setq match (completing-read
  7970. "Match: " 'org-tags-completion-function nil nil nil
  7971. 'org-tags-history))))
  7972. ;; Parse the string and create a lisp form
  7973. (let ((match0 match)
  7974. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL=\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)=\\({[^}]+}\\|\"[^\"]*\"\\)\\|[[:alnum:]_@]+\\)"))
  7975. minus tag mm
  7976. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  7977. orterms term orlist re-p level-p prop-p pn pv cat-p gv)
  7978. (if (string-match "/+" match)
  7979. ;; match contains also a todo-matching request
  7980. (progn
  7981. (setq tagsmatch (substring match 0 (match-beginning 0))
  7982. todomatch (substring match (match-end 0)))
  7983. (if (string-match "^!" todomatch)
  7984. (setq todo-only t todomatch (substring todomatch 1)))
  7985. (if (string-match "^\\s-*$" todomatch)
  7986. (setq todomatch nil)))
  7987. ;; only matching tags
  7988. (setq tagsmatch match todomatch nil))
  7989. ;; Make the tags matcher
  7990. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  7991. (setq tagsmatcher t)
  7992. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  7993. (while (setq term (pop orterms))
  7994. (while (and (equal (substring term -1) "\\") orterms)
  7995. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  7996. (while (string-match re term)
  7997. (setq minus (and (match-end 1)
  7998. (equal (match-string 1 term) "-"))
  7999. tag (match-string 2 term)
  8000. re-p (equal (string-to-char tag) ?{)
  8001. level-p (match-end 3)
  8002. prop-p (match-end 4)
  8003. mm (cond
  8004. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8005. (level-p `(= level ,(string-to-number
  8006. (match-string 3 term))))
  8007. (prop-p
  8008. (setq pn (match-string 4 term)
  8009. pv (match-string 5 term)
  8010. cat-p (equal pn "CATEGORY")
  8011. re-p (equal (string-to-char pv) ?{)
  8012. pv (substring pv 1 -1))
  8013. (if (equal pn "CATEGORY")
  8014. (setq gv '(get-text-property (point) 'org-category))
  8015. (setq gv `(org-cached-entry-get nil ,pn)))
  8016. (if re-p
  8017. `(string-match ,pv (or ,gv ""))
  8018. `(equal ,pv (or ,gv ""))))
  8019. (t `(member ,(downcase tag) tags-list)))
  8020. mm (if minus (list 'not mm) mm)
  8021. term (substring term (match-end 0)))
  8022. (push mm tagsmatcher))
  8023. (push (if (> (length tagsmatcher) 1)
  8024. (cons 'and tagsmatcher)
  8025. (car tagsmatcher))
  8026. orlist)
  8027. (setq tagsmatcher nil))
  8028. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8029. (setq tagsmatcher
  8030. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8031. ;; Make the todo matcher
  8032. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8033. (setq todomatcher t)
  8034. (setq orterms (org-split-string todomatch "|") orlist nil)
  8035. (while (setq term (pop orterms))
  8036. (while (string-match re term)
  8037. (setq minus (and (match-end 1)
  8038. (equal (match-string 1 term) "-"))
  8039. kwd (match-string 2 term)
  8040. re-p (equal (string-to-char kwd) ?{)
  8041. term (substring term (match-end 0))
  8042. mm (if re-p
  8043. `(string-match ,(substring kwd 1 -1) todo)
  8044. (list 'equal 'todo kwd))
  8045. mm (if minus (list 'not mm) mm))
  8046. (push mm todomatcher))
  8047. (push (if (> (length todomatcher) 1)
  8048. (cons 'and todomatcher)
  8049. (car todomatcher))
  8050. orlist)
  8051. (setq todomatcher nil))
  8052. (setq todomatcher (if (> (length orlist) 1)
  8053. (cons 'or orlist) (car orlist))))
  8054. ;; Return the string and lisp forms of the matcher
  8055. (setq matcher (if todomatcher
  8056. (list 'and tagsmatcher todomatcher)
  8057. tagsmatcher))
  8058. (cons match0 matcher)))
  8059. (defun org-match-any-p (re list)
  8060. "Does re match any element of list?"
  8061. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8062. (delq nil list))
  8063. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8064. (defvar org-tags-overlay (org-make-overlay 1 1))
  8065. (org-detach-overlay org-tags-overlay)
  8066. (defun org-get-tags-at (&optional pos)
  8067. "Get a list of all headline tags applicable at POS.
  8068. POS defaults to point. If tags are inherited, the list contains
  8069. the targets in the same sequence as the headlines appear, i.e.
  8070. sthe tags of the current headline come last."
  8071. (interactive)
  8072. (let (tags ltags lastpos parent)
  8073. (save-excursion
  8074. (save-restriction
  8075. (widen)
  8076. (goto-char (or pos (point)))
  8077. (save-match-data
  8078. (condition-case nil
  8079. (progn
  8080. (org-back-to-heading t)
  8081. (while (not (equal lastpos (point)))
  8082. (setq lastpos (point))
  8083. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8084. (setq ltags (org-split-string
  8085. (org-match-string-no-properties 1) ":"))
  8086. (setq tags (append (org-remove-uniherited-tags ltags)
  8087. tags)))
  8088. (or org-use-tag-inheritance (error ""))
  8089. (org-up-heading-all 1)
  8090. (setq parent t)))
  8091. (error nil))))
  8092. tags)))
  8093. (defun org-toggle-tag (tag &optional onoff)
  8094. "Toggle the tag TAG for the current line.
  8095. If ONOFF is `on' or `off', don't toggle but set to this state."
  8096. (unless (org-on-heading-p t) (error "Not on headling"))
  8097. (let (res current)
  8098. (save-excursion
  8099. (beginning-of-line)
  8100. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8101. (point-at-eol) t)
  8102. (progn
  8103. (setq current (match-string 1))
  8104. (replace-match ""))
  8105. (setq current ""))
  8106. (setq current (nreverse (org-split-string current ":")))
  8107. (cond
  8108. ((eq onoff 'on)
  8109. (setq res t)
  8110. (or (member tag current) (push tag current)))
  8111. ((eq onoff 'off)
  8112. (or (not (member tag current)) (setq current (delete tag current))))
  8113. (t (if (member tag current)
  8114. (setq current (delete tag current))
  8115. (setq res t)
  8116. (push tag current))))
  8117. (end-of-line 1)
  8118. (if current
  8119. (progn
  8120. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8121. (org-set-tags nil t))
  8122. (delete-horizontal-space))
  8123. (run-hooks 'org-after-tags-change-hook))
  8124. res))
  8125. (defun org-align-tags-here (to-col)
  8126. ;; Assumes that this is a headline
  8127. (let ((pos (point)) (col (current-column)) tags)
  8128. (beginning-of-line 1)
  8129. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8130. (< pos (match-beginning 2)))
  8131. (progn
  8132. (setq tags (match-string 2))
  8133. (goto-char (match-beginning 1))
  8134. (insert " ")
  8135. (delete-region (point) (1+ (match-end 0)))
  8136. (backward-char 1)
  8137. (move-to-column
  8138. (max (1+ (current-column))
  8139. (1+ col)
  8140. (if (> to-col 0)
  8141. to-col
  8142. (- (abs to-col) (length tags))))
  8143. t)
  8144. (insert tags)
  8145. (move-to-column (min (current-column) col) t))
  8146. (goto-char pos))))
  8147. (defun org-set-tags (&optional arg just-align)
  8148. "Set the tags for the current headline.
  8149. With prefix ARG, realign all tags in headings in the current buffer."
  8150. (interactive "P")
  8151. (let* ((re (concat "^" outline-regexp))
  8152. (current (org-get-tags-string))
  8153. (col (current-column))
  8154. (org-setting-tags t)
  8155. table current-tags inherited-tags ; computed below when needed
  8156. tags p0 c0 c1 rpl)
  8157. (if arg
  8158. (save-excursion
  8159. (goto-char (point-min))
  8160. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8161. (while (re-search-forward re nil t)
  8162. (org-set-tags nil t)
  8163. (end-of-line 1)))
  8164. (message "All tags realigned to column %d" org-tags-column))
  8165. (if just-align
  8166. (setq tags current)
  8167. ;; Get a new set of tags from the user
  8168. (save-excursion
  8169. (setq table (or org-tag-alist (org-get-buffer-tags))
  8170. org-last-tags-completion-table table
  8171. current-tags (org-split-string current ":")
  8172. inherited-tags (nreverse
  8173. (nthcdr (length current-tags)
  8174. (nreverse (org-get-tags-at))))
  8175. tags
  8176. (if (or (eq t org-use-fast-tag-selection)
  8177. (and org-use-fast-tag-selection
  8178. (delq nil (mapcar 'cdr table))))
  8179. (org-fast-tag-selection
  8180. current-tags inherited-tags table
  8181. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8182. (let ((org-add-colon-after-tag-completion t))
  8183. (org-trim
  8184. (org-without-partial-completion
  8185. (completing-read "Tags: " 'org-tags-completion-function
  8186. nil nil current 'org-tags-history)))))))
  8187. (while (string-match "[-+&]+" tags)
  8188. ;; No boolean logic, just a list
  8189. (setq tags (replace-match ":" t t tags))))
  8190. (if (string-match "\\`[\t ]*\\'" tags)
  8191. (setq tags "")
  8192. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8193. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8194. ;; Insert new tags at the correct column
  8195. (beginning-of-line 1)
  8196. (cond
  8197. ((and (equal current "") (equal tags "")))
  8198. ((re-search-forward
  8199. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8200. (point-at-eol) t)
  8201. (if (equal tags "")
  8202. (setq rpl "")
  8203. (goto-char (match-beginning 0))
  8204. (setq c0 (current-column) p0 (point)
  8205. c1 (max (1+ c0) (if (> org-tags-column 0)
  8206. org-tags-column
  8207. (- (- org-tags-column) (length tags))))
  8208. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8209. (replace-match rpl t t)
  8210. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8211. tags)
  8212. (t (error "Tags alignment failed")))
  8213. (move-to-column col)
  8214. (unless just-align
  8215. (run-hooks 'org-after-tags-change-hook)))))
  8216. (defun org-change-tag-in-region (beg end tag off)
  8217. "Add or remove TAG for each entry in the region.
  8218. This works in the agenda, and also in an org-mode buffer."
  8219. (interactive
  8220. (list (region-beginning) (region-end)
  8221. (let ((org-last-tags-completion-table
  8222. (if (org-mode-p)
  8223. (org-get-buffer-tags)
  8224. (org-global-tags-completion-table))))
  8225. (completing-read
  8226. "Tag: " 'org-tags-completion-function nil nil nil
  8227. 'org-tags-history))
  8228. (progn
  8229. (message "[s]et or [r]emove? ")
  8230. (equal (read-char-exclusive) ?r))))
  8231. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8232. (let ((agendap (equal major-mode 'org-agenda-mode))
  8233. l1 l2 m buf pos newhead (cnt 0))
  8234. (goto-char end)
  8235. (setq l2 (1- (org-current-line)))
  8236. (goto-char beg)
  8237. (setq l1 (org-current-line))
  8238. (loop for l from l1 to l2 do
  8239. (goto-line l)
  8240. (setq m (get-text-property (point) 'org-hd-marker))
  8241. (when (or (and (org-mode-p) (org-on-heading-p))
  8242. (and agendap m))
  8243. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8244. pos (if agendap m (point)))
  8245. (with-current-buffer buf
  8246. (save-excursion
  8247. (save-restriction
  8248. (goto-char pos)
  8249. (setq cnt (1+ cnt))
  8250. (org-toggle-tag tag (if off 'off 'on))
  8251. (setq newhead (org-get-heading)))))
  8252. (and agendap (org-agenda-change-all-lines newhead m))))
  8253. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8254. (defun org-tags-completion-function (string predicate &optional flag)
  8255. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8256. (confirm (lambda (x) (stringp (car x)))))
  8257. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8258. (setq s1 (match-string 1 string)
  8259. s2 (match-string 2 string))
  8260. (setq s1 "" s2 string))
  8261. (cond
  8262. ((eq flag nil)
  8263. ;; try completion
  8264. (setq rtn (try-completion s2 ctable confirm))
  8265. (if (stringp rtn)
  8266. (setq rtn
  8267. (concat s1 s2 (substring rtn (length s2))
  8268. (if (and org-add-colon-after-tag-completion
  8269. (assoc rtn ctable))
  8270. ":" ""))))
  8271. rtn)
  8272. ((eq flag t)
  8273. ;; all-completions
  8274. (all-completions s2 ctable confirm)
  8275. )
  8276. ((eq flag 'lambda)
  8277. ;; exact match?
  8278. (assoc s2 ctable)))
  8279. ))
  8280. (defun org-fast-tag-insert (kwd tags face &optional end)
  8281. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8282. (insert (format "%-12s" (concat kwd ":"))
  8283. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8284. (or end "")))
  8285. (defun org-fast-tag-show-exit (flag)
  8286. (save-excursion
  8287. (goto-line 3)
  8288. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8289. (replace-match ""))
  8290. (when flag
  8291. (end-of-line 1)
  8292. (move-to-column (- (window-width) 19) t)
  8293. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8294. (defun org-set-current-tags-overlay (current prefix)
  8295. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8296. (if (featurep 'xemacs)
  8297. (org-overlay-display org-tags-overlay (concat prefix s)
  8298. 'secondary-selection)
  8299. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8300. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8301. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8302. "Fast tag selection with single keys.
  8303. CURRENT is the current list of tags in the headline, INHERITED is the
  8304. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8305. possibly with grouping information. TODO-TABLE is a similar table with
  8306. TODO keywords, should these have keys assigned to them.
  8307. If the keys are nil, a-z are automatically assigned.
  8308. Returns the new tags string, or nil to not change the current settings."
  8309. (let* ((fulltable (append table todo-table))
  8310. (maxlen (apply 'max (mapcar
  8311. (lambda (x)
  8312. (if (stringp (car x)) (string-width (car x)) 0))
  8313. fulltable)))
  8314. (buf (current-buffer))
  8315. (expert (eq org-fast-tag-selection-single-key 'expert))
  8316. (buffer-tags nil)
  8317. (fwidth (+ maxlen 3 1 3))
  8318. (ncol (/ (- (window-width) 4) fwidth))
  8319. (i-face 'org-done)
  8320. (c-face 'org-todo)
  8321. tg cnt e c char c1 c2 ntable tbl rtn
  8322. ov-start ov-end ov-prefix
  8323. (exit-after-next org-fast-tag-selection-single-key)
  8324. (done-keywords org-done-keywords)
  8325. groups ingroup)
  8326. (save-excursion
  8327. (beginning-of-line 1)
  8328. (if (looking-at
  8329. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8330. (setq ov-start (match-beginning 1)
  8331. ov-end (match-end 1)
  8332. ov-prefix "")
  8333. (setq ov-start (1- (point-at-eol))
  8334. ov-end (1+ ov-start))
  8335. (skip-chars-forward "^\n\r")
  8336. (setq ov-prefix
  8337. (concat
  8338. (buffer-substring (1- (point)) (point))
  8339. (if (> (current-column) org-tags-column)
  8340. " "
  8341. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8342. (org-move-overlay org-tags-overlay ov-start ov-end)
  8343. (save-window-excursion
  8344. (if expert
  8345. (set-buffer (get-buffer-create " *Org tags*"))
  8346. (delete-other-windows)
  8347. (split-window-vertically)
  8348. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8349. (erase-buffer)
  8350. (org-set-local 'org-done-keywords done-keywords)
  8351. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8352. (org-fast-tag-insert "Current" current c-face "\n\n")
  8353. (org-fast-tag-show-exit exit-after-next)
  8354. (org-set-current-tags-overlay current ov-prefix)
  8355. (setq tbl fulltable char ?a cnt 0)
  8356. (while (setq e (pop tbl))
  8357. (cond
  8358. ((equal e '(:startgroup))
  8359. (push '() groups) (setq ingroup t)
  8360. (when (not (= cnt 0))
  8361. (setq cnt 0)
  8362. (insert "\n"))
  8363. (insert "{ "))
  8364. ((equal e '(:endgroup))
  8365. (setq ingroup nil cnt 0)
  8366. (insert "}\n"))
  8367. (t
  8368. (setq tg (car e) c2 nil)
  8369. (if (cdr e)
  8370. (setq c (cdr e))
  8371. ;; automatically assign a character.
  8372. (setq c1 (string-to-char
  8373. (downcase (substring
  8374. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8375. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8376. (while (or (rassoc char ntable) (rassoc char table))
  8377. (setq char (1+ char)))
  8378. (setq c2 c1))
  8379. (setq c (or c2 char)))
  8380. (if ingroup (push tg (car groups)))
  8381. (setq tg (org-add-props tg nil 'face
  8382. (cond
  8383. ((not (assoc tg table))
  8384. (org-get-todo-face tg))
  8385. ((member tg current) c-face)
  8386. ((member tg inherited) i-face)
  8387. (t nil))))
  8388. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8389. (insert "[" c "] " tg (make-string
  8390. (- fwidth 4 (length tg)) ?\ ))
  8391. (push (cons tg c) ntable)
  8392. (when (= (setq cnt (1+ cnt)) ncol)
  8393. (insert "\n")
  8394. (if ingroup (insert " "))
  8395. (setq cnt 0)))))
  8396. (setq ntable (nreverse ntable))
  8397. (insert "\n")
  8398. (goto-char (point-min))
  8399. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8400. (fit-window-to-buffer))
  8401. (setq rtn
  8402. (catch 'exit
  8403. (while t
  8404. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8405. (if groups " [!] no groups" " [!]groups")
  8406. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8407. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8408. (cond
  8409. ((= c ?\r) (throw 'exit t))
  8410. ((= c ?!)
  8411. (setq groups (not groups))
  8412. (goto-char (point-min))
  8413. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8414. ((= c ?\C-c)
  8415. (if (not expert)
  8416. (org-fast-tag-show-exit
  8417. (setq exit-after-next (not exit-after-next)))
  8418. (setq expert nil)
  8419. (delete-other-windows)
  8420. (split-window-vertically)
  8421. (org-switch-to-buffer-other-window " *Org tags*")
  8422. (and (fboundp 'fit-window-to-buffer)
  8423. (fit-window-to-buffer))))
  8424. ((or (= c ?\C-g)
  8425. (and (= c ?q) (not (rassoc c ntable))))
  8426. (org-detach-overlay org-tags-overlay)
  8427. (setq quit-flag t))
  8428. ((= c ?\ )
  8429. (setq current nil)
  8430. (if exit-after-next (setq exit-after-next 'now)))
  8431. ((= c ?\t)
  8432. (condition-case nil
  8433. (setq tg (completing-read
  8434. "Tag: "
  8435. (or buffer-tags
  8436. (with-current-buffer buf
  8437. (org-get-buffer-tags)))))
  8438. (quit (setq tg "")))
  8439. (when (string-match "\\S-" tg)
  8440. (add-to-list 'buffer-tags (list tg))
  8441. (if (member tg current)
  8442. (setq current (delete tg current))
  8443. (push tg current)))
  8444. (if exit-after-next (setq exit-after-next 'now)))
  8445. ((setq e (rassoc c todo-table) tg (car e))
  8446. (with-current-buffer buf
  8447. (save-excursion (org-todo tg)))
  8448. (if exit-after-next (setq exit-after-next 'now)))
  8449. ((setq e (rassoc c ntable) tg (car e))
  8450. (if (member tg current)
  8451. (setq current (delete tg current))
  8452. (loop for g in groups do
  8453. (if (member tg g)
  8454. (mapc (lambda (x)
  8455. (setq current (delete x current)))
  8456. g)))
  8457. (push tg current))
  8458. (if exit-after-next (setq exit-after-next 'now))))
  8459. ;; Create a sorted list
  8460. (setq current
  8461. (sort current
  8462. (lambda (a b)
  8463. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8464. (if (eq exit-after-next 'now) (throw 'exit t))
  8465. (goto-char (point-min))
  8466. (beginning-of-line 2)
  8467. (delete-region (point) (point-at-eol))
  8468. (org-fast-tag-insert "Current" current c-face)
  8469. (org-set-current-tags-overlay current ov-prefix)
  8470. (while (re-search-forward
  8471. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8472. (setq tg (match-string 1))
  8473. (add-text-properties
  8474. (match-beginning 1) (match-end 1)
  8475. (list 'face
  8476. (cond
  8477. ((member tg current) c-face)
  8478. ((member tg inherited) i-face)
  8479. (t (get-text-property (match-beginning 1) 'face))))))
  8480. (goto-char (point-min)))))
  8481. (org-detach-overlay org-tags-overlay)
  8482. (if rtn
  8483. (mapconcat 'identity current ":")
  8484. nil))))
  8485. (defun org-get-tags-string ()
  8486. "Get the TAGS string in the current headline."
  8487. (unless (org-on-heading-p t)
  8488. (error "Not on a heading"))
  8489. (save-excursion
  8490. (beginning-of-line 1)
  8491. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8492. (org-match-string-no-properties 1)
  8493. "")))
  8494. (defun org-get-tags ()
  8495. "Get the list of tags specified in the current headline."
  8496. (org-split-string (org-get-tags-string) ":"))
  8497. (defun org-get-buffer-tags ()
  8498. "Get a table of all tags used in the buffer, for completion."
  8499. (let (tags)
  8500. (save-excursion
  8501. (goto-char (point-min))
  8502. (while (re-search-forward
  8503. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8504. (when (equal (char-after (point-at-bol 0)) ?*)
  8505. (mapc (lambda (x) (add-to-list 'tags x))
  8506. (org-split-string (org-match-string-no-properties 1) ":")))))
  8507. (mapcar 'list tags)))
  8508. ;;;; Properties
  8509. ;;; Setting and retrieving properties
  8510. (defconst org-special-properties
  8511. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8512. "TIMESTAMP" "TIMESTAMP_IA")
  8513. "The special properties valid in Org-mode.
  8514. These are properties that are not defined in the property drawer,
  8515. but in some other way.")
  8516. (defconst org-default-properties
  8517. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8518. "LOCATION" "LOGGING" "COLUMNS")
  8519. "Some properties that are used by Org-mode for various purposes.
  8520. Being in this list makes sure that they are offered for completion.")
  8521. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8522. "Regular expression matching the first line of a property drawer.")
  8523. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8524. "Regular expression matching the first line of a property drawer.")
  8525. (defun org-property-action ()
  8526. "Do an action on properties."
  8527. (interactive)
  8528. (let (c)
  8529. (org-at-property-p)
  8530. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8531. (setq c (read-char-exclusive))
  8532. (cond
  8533. ((equal c ?s)
  8534. (call-interactively 'org-set-property))
  8535. ((equal c ?d)
  8536. (call-interactively 'org-delete-property))
  8537. ((equal c ?D)
  8538. (call-interactively 'org-delete-property-globally))
  8539. ((equal c ?c)
  8540. (call-interactively 'org-compute-property-at-point))
  8541. (t (error "No such property action %c" c)))))
  8542. (defun org-at-property-p ()
  8543. "Is the cursor in a property line?"
  8544. ;; FIXME: Does not check if we are actually in the drawer.
  8545. ;; FIXME: also returns true on any drawers.....
  8546. ;; This is used by C-c C-c for property action.
  8547. (save-excursion
  8548. (beginning-of-line 1)
  8549. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8550. (defun org-get-property-block (&optional beg end force)
  8551. "Return the (beg . end) range of the body of the property drawer.
  8552. BEG and END can be beginning and end of subtree, if not given
  8553. they will be found.
  8554. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8555. (catch 'exit
  8556. (save-excursion
  8557. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8558. (end (or end (progn (outline-next-heading) (point)))))
  8559. (goto-char beg)
  8560. (if (re-search-forward org-property-start-re end t)
  8561. (setq beg (1+ (match-end 0)))
  8562. (if force
  8563. (save-excursion
  8564. (org-insert-property-drawer)
  8565. (setq end (progn (outline-next-heading) (point))))
  8566. (throw 'exit nil))
  8567. (goto-char beg)
  8568. (if (re-search-forward org-property-start-re end t)
  8569. (setq beg (1+ (match-end 0)))))
  8570. (if (re-search-forward org-property-end-re end t)
  8571. (setq end (match-beginning 0))
  8572. (or force (throw 'exit nil))
  8573. (goto-char beg)
  8574. (setq end beg)
  8575. (org-indent-line-function)
  8576. (insert ":END:\n"))
  8577. (cons beg end)))))
  8578. (defun org-entry-properties (&optional pom which)
  8579. "Get all properties of the entry at point-or-marker POM.
  8580. This includes the TODO keyword, the tags, time strings for deadline,
  8581. scheduled, and clocking, and any additional properties defined in the
  8582. entry. The return value is an alist, keys may occur multiple times
  8583. if the property key was used several times.
  8584. POM may also be nil, in which case the current entry is used.
  8585. If WHICH is nil or `all', get all properties. If WHICH is
  8586. `special' or `standard', only get that subclass."
  8587. (setq which (or which 'all))
  8588. (org-with-point-at pom
  8589. (let ((clockstr (substring org-clock-string 0 -1))
  8590. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8591. beg end range props sum-props key value string clocksum)
  8592. (save-excursion
  8593. (when (condition-case nil (org-back-to-heading t) (error nil))
  8594. (setq beg (point))
  8595. (setq sum-props (get-text-property (point) 'org-summaries))
  8596. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8597. (outline-next-heading)
  8598. (setq end (point))
  8599. (when (memq which '(all special))
  8600. ;; Get the special properties, like TODO and tags
  8601. (goto-char beg)
  8602. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8603. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8604. (when (looking-at org-priority-regexp)
  8605. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8606. (when (and (setq value (org-get-tags-string))
  8607. (string-match "\\S-" value))
  8608. (push (cons "TAGS" value) props))
  8609. (when (setq value (org-get-tags-at))
  8610. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8611. props))
  8612. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8613. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8614. string (if (equal key clockstr)
  8615. (org-no-properties
  8616. (org-trim
  8617. (buffer-substring
  8618. (match-beginning 3) (goto-char (point-at-eol)))))
  8619. (substring (org-match-string-no-properties 3) 1 -1)))
  8620. (unless key
  8621. (if (= (char-after (match-beginning 3)) ?\[)
  8622. (setq key "TIMESTAMP_IA")
  8623. (setq key "TIMESTAMP")))
  8624. (when (or (equal key clockstr) (not (assoc key props)))
  8625. (push (cons key string) props)))
  8626. )
  8627. (when (memq which '(all standard))
  8628. ;; Get the standard properties, like :PORP: ...
  8629. (setq range (org-get-property-block beg end))
  8630. (when range
  8631. (goto-char (car range))
  8632. (while (re-search-forward
  8633. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8634. (cdr range) t)
  8635. (setq key (org-match-string-no-properties 1)
  8636. value (org-trim (or (org-match-string-no-properties 2) "")))
  8637. (unless (member key excluded)
  8638. (push (cons key (or value "")) props)))))
  8639. (if clocksum
  8640. (push (cons "CLOCKSUM"
  8641. (org-columns-number-to-string (/ (float clocksum) 60.)
  8642. 'add_times))
  8643. props))
  8644. (append sum-props (nreverse props)))))))
  8645. (defun org-entry-get (pom property &optional inherit)
  8646. "Get value of PROPERTY for entry at point-or-marker POM.
  8647. If INHERIT is non-nil and the entry does not have the property,
  8648. then also check higher levels of the hierarchy.
  8649. If INHERIT is the symbol `selective', use inheritance only if the setting
  8650. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8651. If the property is present but empty, the return value is the empty string.
  8652. If the property is not present at all, nil is returned."
  8653. (org-with-point-at pom
  8654. (if (and inherit (if (eq inherit 'selective)
  8655. (org-property-inherit-p property)
  8656. t))
  8657. (org-entry-get-with-inheritance property)
  8658. (if (member property org-special-properties)
  8659. ;; We need a special property. Use brute force, get all properties.
  8660. (cdr (assoc property (org-entry-properties nil 'special)))
  8661. (let ((range (org-get-property-block)))
  8662. (if (and range
  8663. (goto-char (car range))
  8664. (re-search-forward
  8665. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8666. (cdr range) t))
  8667. ;; Found the property, return it.
  8668. (if (match-end 1)
  8669. (org-match-string-no-properties 1)
  8670. "")))))))
  8671. (defun org-property-or-variable-value (var &optional inherit)
  8672. "Check if there is a property fixing the value of VAR.
  8673. If yes, return this value. If not, return the current value of the variable."
  8674. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8675. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8676. (read prop)
  8677. (symbol-value var))))
  8678. (defun org-entry-delete (pom property)
  8679. "Delete the property PROPERTY from entry at point-or-marker POM."
  8680. (org-with-point-at pom
  8681. (if (member property org-special-properties)
  8682. nil ; cannot delete these properties.
  8683. (let ((range (org-get-property-block)))
  8684. (if (and range
  8685. (goto-char (car range))
  8686. (re-search-forward
  8687. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8688. (cdr range) t))
  8689. (progn
  8690. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8691. t)
  8692. nil)))))
  8693. ;; Multi-values properties are properties that contain multiple values
  8694. ;; These values are assumed to be single words, separated by whitespace.
  8695. (defun org-entry-add-to-multivalued-property (pom property value)
  8696. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8697. (let* ((old (org-entry-get pom property))
  8698. (values (and old (org-split-string old "[ \t]"))))
  8699. (unless (member value values)
  8700. (setq values (cons value values))
  8701. (org-entry-put pom property
  8702. (mapconcat 'identity values " ")))))
  8703. (defun org-entry-remove-from-multivalued-property (pom property value)
  8704. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8705. (let* ((old (org-entry-get pom property))
  8706. (values (and old (org-split-string old "[ \t]"))))
  8707. (when (member value values)
  8708. (setq values (delete value values))
  8709. (org-entry-put pom property
  8710. (mapconcat 'identity values " ")))))
  8711. (defun org-entry-member-in-multivalued-property (pom property value)
  8712. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8713. (let* ((old (org-entry-get pom property))
  8714. (values (and old (org-split-string old "[ \t]"))))
  8715. (member value values)))
  8716. (defvar org-entry-property-inherited-from (make-marker))
  8717. (defun org-entry-get-with-inheritance (property)
  8718. "Get entry property, and search higher levels if not present."
  8719. (let (tmp)
  8720. (save-excursion
  8721. (save-restriction
  8722. (widen)
  8723. (catch 'ex
  8724. (while t
  8725. (when (setq tmp (org-entry-get nil property))
  8726. (org-back-to-heading t)
  8727. (move-marker org-entry-property-inherited-from (point))
  8728. (throw 'ex tmp))
  8729. (or (org-up-heading-safe) (throw 'ex nil)))))
  8730. (or tmp (cdr (assoc property org-local-properties))
  8731. (cdr (assoc property org-global-properties))))))
  8732. (defun org-entry-put (pom property value)
  8733. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8734. (org-with-point-at pom
  8735. (org-back-to-heading t)
  8736. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8737. range)
  8738. (cond
  8739. ((equal property "TODO")
  8740. (when (and (stringp value) (string-match "\\S-" value)
  8741. (not (member value org-todo-keywords-1)))
  8742. (error "\"%s\" is not a valid TODO state" value))
  8743. (if (or (not value)
  8744. (not (string-match "\\S-" value)))
  8745. (setq value 'none))
  8746. (org-todo value)
  8747. (org-set-tags nil 'align))
  8748. ((equal property "PRIORITY")
  8749. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8750. (string-to-char value) ?\ ))
  8751. (org-set-tags nil 'align))
  8752. ((equal property "SCHEDULED")
  8753. (if (re-search-forward org-scheduled-time-regexp end t)
  8754. (cond
  8755. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8756. ((eq value 'later) (org-timestamp-change 1 'day))
  8757. (t (call-interactively 'org-schedule)))
  8758. (call-interactively 'org-schedule)))
  8759. ((equal property "DEADLINE")
  8760. (if (re-search-forward org-deadline-time-regexp end t)
  8761. (cond
  8762. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8763. ((eq value 'later) (org-timestamp-change 1 'day))
  8764. (t (call-interactively 'org-deadline)))
  8765. (call-interactively 'org-deadline)))
  8766. ((member property org-special-properties)
  8767. (error "The %s property can not yet be set with `org-entry-put'"
  8768. property))
  8769. (t ; a non-special property
  8770. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  8771. (setq range (org-get-property-block beg end 'force))
  8772. (goto-char (car range))
  8773. (if (re-search-forward
  8774. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  8775. (progn
  8776. (delete-region (match-beginning 1) (match-end 1))
  8777. (goto-char (match-beginning 1)))
  8778. (goto-char (cdr range))
  8779. (insert "\n")
  8780. (backward-char 1)
  8781. (org-indent-line-function)
  8782. (insert ":" property ":"))
  8783. (and value (insert " " value))
  8784. (org-indent-line-function)))))))
  8785. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  8786. "Get all property keys in the current buffer.
  8787. With INCLUDE-SPECIALS, also list the special properties that relect things
  8788. like tags and TODO state.
  8789. With INCLUDE-DEFAULTS, also include properties that has special meaning
  8790. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  8791. With INCLUDE-COLUMNS, also include property names given in COLUMN
  8792. formats in the current buffer."
  8793. (let (rtn range cfmt cols s p)
  8794. (save-excursion
  8795. (save-restriction
  8796. (widen)
  8797. (goto-char (point-min))
  8798. (while (re-search-forward org-property-start-re nil t)
  8799. (setq range (org-get-property-block))
  8800. (goto-char (car range))
  8801. (while (re-search-forward
  8802. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  8803. (cdr range) t)
  8804. (add-to-list 'rtn (org-match-string-no-properties 1)))
  8805. (outline-next-heading))))
  8806. (when include-specials
  8807. (setq rtn (append org-special-properties rtn)))
  8808. (when include-defaults
  8809. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  8810. (when include-columns
  8811. (save-excursion
  8812. (save-restriction
  8813. (widen)
  8814. (goto-char (point-min))
  8815. (while (re-search-forward
  8816. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  8817. nil t)
  8818. (setq cfmt (match-string 2) s 0)
  8819. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  8820. cfmt s)
  8821. (setq s (match-end 0)
  8822. p (match-string 1 cfmt))
  8823. (unless (or (equal p "ITEM")
  8824. (member p org-special-properties))
  8825. (add-to-list 'rtn (match-string 1 cfmt))))))))
  8826. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  8827. (defun org-property-values (key)
  8828. "Return a list of all values of property KEY."
  8829. (save-excursion
  8830. (save-restriction
  8831. (widen)
  8832. (goto-char (point-min))
  8833. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  8834. values)
  8835. (while (re-search-forward re nil t)
  8836. (add-to-list 'values (org-trim (match-string 1))))
  8837. (delete "" values)))))
  8838. (defun org-insert-property-drawer ()
  8839. "Insert a property drawer into the current entry."
  8840. (interactive)
  8841. (org-back-to-heading t)
  8842. (looking-at outline-regexp)
  8843. (let ((indent (- (match-end 0)(match-beginning 0)))
  8844. (beg (point))
  8845. (re (concat "^[ \t]*" org-keyword-time-regexp))
  8846. end hiddenp)
  8847. (outline-next-heading)
  8848. (setq end (point))
  8849. (goto-char beg)
  8850. (while (re-search-forward re end t))
  8851. (setq hiddenp (org-invisible-p))
  8852. (end-of-line 1)
  8853. (and (equal (char-after) ?\n) (forward-char 1))
  8854. (org-skip-over-state-notes)
  8855. (skip-chars-backward " \t\n\r")
  8856. (if (eq (char-before) ?*) (forward-char 1))
  8857. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  8858. (beginning-of-line 0)
  8859. (indent-to-column indent)
  8860. (beginning-of-line 2)
  8861. (indent-to-column indent)
  8862. (beginning-of-line 0)
  8863. (if hiddenp
  8864. (save-excursion
  8865. (org-back-to-heading t)
  8866. (hide-entry))
  8867. (org-flag-drawer t))))
  8868. (defun org-set-property (property value)
  8869. "In the current entry, set PROPERTY to VALUE.
  8870. When called interactively, this will prompt for a property name, offering
  8871. completion on existing and default properties. And then it will prompt
  8872. for a value, offering competion either on allowed values (via an inherited
  8873. xxx_ALL property) or on existing values in other instances of this property
  8874. in the current file."
  8875. (interactive
  8876. (let* ((prop (completing-read
  8877. "Property: " (mapcar 'list (org-buffer-property-keys nil t t))))
  8878. (cur (org-entry-get nil prop))
  8879. (allowed (org-property-get-allowed-values nil prop 'table))
  8880. (existing (mapcar 'list (org-property-values prop)))
  8881. (val (if allowed
  8882. (completing-read "Value: " allowed nil 'req-match)
  8883. (completing-read
  8884. (concat "Value" (if (and cur (string-match "\\S-" cur))
  8885. (concat "[" cur "]") "")
  8886. ": ")
  8887. existing nil nil "" nil cur))))
  8888. (list prop (if (equal val "") cur val))))
  8889. (unless (equal (org-entry-get nil property) value)
  8890. (org-entry-put nil property value)))
  8891. (defun org-delete-property (property)
  8892. "In the current entry, delete PROPERTY."
  8893. (interactive
  8894. (let* ((prop (completing-read
  8895. "Property: " (org-entry-properties nil 'standard))))
  8896. (list prop)))
  8897. (message "Property %s %s" property
  8898. (if (org-entry-delete nil property)
  8899. "deleted"
  8900. "was not present in the entry")))
  8901. (defun org-delete-property-globally (property)
  8902. "Remove PROPERTY globally, from all entries."
  8903. (interactive
  8904. (let* ((prop (completing-read
  8905. "Globally remove property: "
  8906. (mapcar 'list (org-buffer-property-keys)))))
  8907. (list prop)))
  8908. (save-excursion
  8909. (save-restriction
  8910. (widen)
  8911. (goto-char (point-min))
  8912. (let ((cnt 0))
  8913. (while (re-search-forward
  8914. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  8915. nil t)
  8916. (setq cnt (1+ cnt))
  8917. (replace-match ""))
  8918. (message "Property \"%s\" removed from %d entries" property cnt)))))
  8919. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  8920. (defun org-compute-property-at-point ()
  8921. "Compute the property at point.
  8922. This looks for an enclosing column format, extracts the operator and
  8923. then applies it to the proerty in the column format's scope."
  8924. (interactive)
  8925. (unless (org-at-property-p)
  8926. (error "Not at a property"))
  8927. (let ((prop (org-match-string-no-properties 2)))
  8928. (org-columns-get-format-and-top-level)
  8929. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  8930. (error "No operator defined for property %s" prop))
  8931. (org-columns-compute prop)))
  8932. (defun org-property-get-allowed-values (pom property &optional table)
  8933. "Get allowed values for the property PROPERTY.
  8934. When TABLE is non-nil, return an alist that can directly be used for
  8935. completion."
  8936. (let (vals)
  8937. (cond
  8938. ((equal property "TODO")
  8939. (setq vals (org-with-point-at pom
  8940. (append org-todo-keywords-1 '("")))))
  8941. ((equal property "PRIORITY")
  8942. (let ((n org-lowest-priority))
  8943. (while (>= n org-highest-priority)
  8944. (push (char-to-string n) vals)
  8945. (setq n (1- n)))))
  8946. ((member property org-special-properties))
  8947. (t
  8948. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  8949. (when (and vals (string-match "\\S-" vals))
  8950. (setq vals (car (read-from-string (concat "(" vals ")"))))
  8951. (setq vals (mapcar (lambda (x)
  8952. (cond ((stringp x) x)
  8953. ((numberp x) (number-to-string x))
  8954. ((symbolp x) (symbol-name x))
  8955. (t "???")))
  8956. vals)))))
  8957. (if table (mapcar 'list vals) vals)))
  8958. (defun org-property-previous-allowed-value (&optional previous)
  8959. "Switch to the next allowed value for this property."
  8960. (interactive)
  8961. (org-property-next-allowed-value t))
  8962. (defun org-property-next-allowed-value (&optional previous)
  8963. "Switch to the next allowed value for this property."
  8964. (interactive)
  8965. (unless (org-at-property-p)
  8966. (error "Not at a property"))
  8967. (let* ((key (match-string 2))
  8968. (value (match-string 3))
  8969. (allowed (or (org-property-get-allowed-values (point) key)
  8970. (and (member value '("[ ]" "[-]" "[X]"))
  8971. '("[ ]" "[X]"))))
  8972. nval)
  8973. (unless allowed
  8974. (error "Allowed values for this property have not been defined"))
  8975. (if previous (setq allowed (reverse allowed)))
  8976. (if (member value allowed)
  8977. (setq nval (car (cdr (member value allowed)))))
  8978. (setq nval (or nval (car allowed)))
  8979. (if (equal nval value)
  8980. (error "Only one allowed value for this property"))
  8981. (org-at-property-p)
  8982. (replace-match (concat " :" key ": " nval) t t)
  8983. (org-indent-line-function)
  8984. (beginning-of-line 1)
  8985. (skip-chars-forward " \t")))
  8986. (defun org-find-entry-with-id (ident)
  8987. "Locate the entry that contains the ID property with exact value IDENT.
  8988. IDENT can be a string, a symbol or a number, this function will search for
  8989. the string representation of it.
  8990. Return the position where this entry starts, or nil if there is no such entry."
  8991. (let ((id (cond
  8992. ((stringp ident) ident)
  8993. ((symbol-name ident) (symbol-name ident))
  8994. ((numberp ident) (number-to-string ident))
  8995. (t (error "IDENT %s must be a string, symbol or number" ident))))
  8996. (case-fold-search nil))
  8997. (save-excursion
  8998. (save-restriction
  8999. (widen)
  9000. (goto-char (point-min))
  9001. (when (re-search-forward
  9002. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9003. nil t)
  9004. (org-back-to-heading)
  9005. (point))))))
  9006. ;;;; Timestamps
  9007. (defvar org-last-changed-timestamp nil)
  9008. (defvar org-time-was-given) ; dynamically scoped parameter
  9009. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9010. (defvar org-ts-what) ; dynamically scoped parameter
  9011. (defun org-time-stamp (arg)
  9012. "Prompt for a date/time and insert a time stamp.
  9013. If the user specifies a time like HH:MM, or if this command is called
  9014. with a prefix argument, the time stamp will contain date and time.
  9015. Otherwise, only the date will be included. All parts of a date not
  9016. specified by the user will be filled in from the current date/time.
  9017. So if you press just return without typing anything, the time stamp
  9018. will represent the current date/time. If there is already a timestamp
  9019. at the cursor, it will be modified."
  9020. (interactive "P")
  9021. (let* ((ts nil)
  9022. (default-time
  9023. ;; Default time is either today, or, when entering a range,
  9024. ;; the range start.
  9025. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9026. (save-excursion
  9027. (re-search-backward
  9028. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9029. (- (point) 20) t)))
  9030. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9031. (current-time)))
  9032. (default-input (and ts (org-get-compact-tod ts)))
  9033. org-time-was-given org-end-time-was-given time)
  9034. (cond
  9035. ((and (org-at-timestamp-p)
  9036. (eq last-command 'org-time-stamp)
  9037. (eq this-command 'org-time-stamp))
  9038. (insert "--")
  9039. (setq time (let ((this-command this-command))
  9040. (org-read-date arg 'totime nil nil default-time default-input)))
  9041. (org-insert-time-stamp time (or org-time-was-given arg)))
  9042. ((org-at-timestamp-p)
  9043. (setq time (let ((this-command this-command))
  9044. (org-read-date arg 'totime nil nil default-time default-input)))
  9045. (when (org-at-timestamp-p) ; just to get the match data
  9046. (replace-match "")
  9047. (setq org-last-changed-timestamp
  9048. (org-insert-time-stamp
  9049. time (or org-time-was-given arg)
  9050. nil nil nil (list org-end-time-was-given))))
  9051. (message "Timestamp updated"))
  9052. (t
  9053. (setq time (let ((this-command this-command))
  9054. (org-read-date arg 'totime nil nil default-time default-input)))
  9055. (org-insert-time-stamp time (or org-time-was-given arg)
  9056. nil nil nil (list org-end-time-was-given))))))
  9057. ;; FIXME: can we use this for something else, like computing time differences?
  9058. (defun org-get-compact-tod (s)
  9059. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9060. (let* ((t1 (match-string 1 s))
  9061. (h1 (string-to-number (match-string 2 s)))
  9062. (m1 (string-to-number (match-string 3 s)))
  9063. (t2 (and (match-end 4) (match-string 5 s)))
  9064. (h2 (and t2 (string-to-number (match-string 6 s))))
  9065. (m2 (and t2 (string-to-number (match-string 7 s))))
  9066. dh dm)
  9067. (if (not t2)
  9068. t1
  9069. (setq dh (- h2 h1) dm (- m2 m1))
  9070. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9071. (concat t1 "+" (number-to-string dh)
  9072. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9073. (defun org-time-stamp-inactive (&optional arg)
  9074. "Insert an inactive time stamp.
  9075. An inactive time stamp is enclosed in square brackets instead of angle
  9076. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9077. does not link to the calendar and cannot be changed with the S-cursor keys.
  9078. So these are more for recording a certain time/date."
  9079. (interactive "P")
  9080. (let (org-time-was-given org-end-time-was-given time)
  9081. (setq time (org-read-date arg 'totime))
  9082. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9083. nil nil (list org-end-time-was-given))))
  9084. (defvar org-date-ovl (org-make-overlay 1 1))
  9085. (org-overlay-put org-date-ovl 'face 'org-warning)
  9086. (org-detach-overlay org-date-ovl)
  9087. (defvar org-ans1) ; dynamically scoped parameter
  9088. (defvar org-ans2) ; dynamically scoped parameter
  9089. (defvar org-plain-time-of-day-regexp) ; defined below
  9090. (defvar org-read-date-overlay nil)
  9091. (defvar org-dcst nil) ; dynamically scoped
  9092. (defun org-read-date (&optional with-time to-time from-string prompt
  9093. default-time default-input)
  9094. "Read a date, possibly a time, and make things smooth for the user.
  9095. The prompt will suggest to enter an ISO date, but you can also enter anything
  9096. which will at least partially be understood by `parse-time-string'.
  9097. Unrecognized parts of the date will default to the current day, month, year,
  9098. hour and minute. If this command is called to replace a timestamp at point,
  9099. of to enter the second timestamp of a range, the default time is taken from the
  9100. existing stamp. For example,
  9101. 3-2-5 --> 2003-02-05
  9102. feb 15 --> currentyear-02-15
  9103. sep 12 9 --> 2009-09-12
  9104. 12:45 --> today 12:45
  9105. 22 sept 0:34 --> currentyear-09-22 0:34
  9106. 12 --> currentyear-currentmonth-12
  9107. Fri --> nearest Friday (today or later)
  9108. etc.
  9109. Furthermore you can specify a relative date by giving, as the *first* thing
  9110. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9111. change in days weeks, months, years.
  9112. With a single plus or minus, the date is relative to today. With a double
  9113. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9114. +4d --> four days from today
  9115. +4 --> same as above
  9116. +2w --> two weeks from today
  9117. ++5 --> five days from default date
  9118. The function understands only English month and weekday abbreviations,
  9119. but this can be configured with the variables `parse-time-months' and
  9120. `parse-time-weekdays'.
  9121. While prompting, a calendar is popped up - you can also select the
  9122. date with the mouse (button 1). The calendar shows a period of three
  9123. months. To scroll it to other months, use the keys `>' and `<'.
  9124. If you don't like the calendar, turn it off with
  9125. \(setq org-read-date-popup-calendar nil)
  9126. With optional argument TO-TIME, the date will immediately be converted
  9127. to an internal time.
  9128. With an optional argument WITH-TIME, the prompt will suggest to also
  9129. insert a time. Note that when WITH-TIME is not set, you can still
  9130. enter a time, and this function will inform the calling routine about
  9131. this change. The calling routine may then choose to change the format
  9132. used to insert the time stamp into the buffer to include the time.
  9133. With optional argument FROM-STRING, read from this string instead from
  9134. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9135. the time/date that is used for everything that is not specified by the
  9136. user."
  9137. (require 'parse-time)
  9138. (let* ((org-time-stamp-rounding-minutes
  9139. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9140. (org-dcst org-display-custom-times)
  9141. (ct (org-current-time))
  9142. (def (or default-time ct))
  9143. (defdecode (decode-time def))
  9144. (dummy (progn
  9145. (when (< (nth 2 defdecode) org-extend-today-until)
  9146. (setcar (nthcdr 2 defdecode) -1)
  9147. (setcar (nthcdr 1 defdecode) 59)
  9148. (setq def (apply 'encode-time defdecode)
  9149. defdecode (decode-time def)))))
  9150. (calendar-move-hook nil)
  9151. (calendar-view-diary-initially-flag nil)
  9152. (view-diary-entries-initially nil)
  9153. (calendar-view-holidays-initially-flag nil)
  9154. (view-calendar-holidays-initially nil)
  9155. (timestr (format-time-string
  9156. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9157. (prompt (concat (if prompt (concat prompt " ") "")
  9158. (format "Date+time [%s]: " timestr)))
  9159. ans (org-ans0 "") org-ans1 org-ans2 final)
  9160. (cond
  9161. (from-string (setq ans from-string))
  9162. (org-read-date-popup-calendar
  9163. (save-excursion
  9164. (save-window-excursion
  9165. (calendar)
  9166. (calendar-forward-day (- (time-to-days def)
  9167. (calendar-absolute-from-gregorian
  9168. (calendar-current-date))))
  9169. (org-eval-in-calendar nil t)
  9170. (let* ((old-map (current-local-map))
  9171. (map (copy-keymap calendar-mode-map))
  9172. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9173. (org-defkey map (kbd "RET") 'org-calendar-select)
  9174. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9175. 'org-calendar-select-mouse)
  9176. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9177. 'org-calendar-select-mouse)
  9178. (org-defkey minibuffer-local-map [(meta shift left)]
  9179. (lambda () (interactive)
  9180. (org-eval-in-calendar '(calendar-backward-month 1))))
  9181. (org-defkey minibuffer-local-map [(meta shift right)]
  9182. (lambda () (interactive)
  9183. (org-eval-in-calendar '(calendar-forward-month 1))))
  9184. (org-defkey minibuffer-local-map [(meta shift up)]
  9185. (lambda () (interactive)
  9186. (org-eval-in-calendar '(calendar-backward-year 1))))
  9187. (org-defkey minibuffer-local-map [(meta shift down)]
  9188. (lambda () (interactive)
  9189. (org-eval-in-calendar '(calendar-forward-year 1))))
  9190. (org-defkey minibuffer-local-map [(shift up)]
  9191. (lambda () (interactive)
  9192. (org-eval-in-calendar '(calendar-backward-week 1))))
  9193. (org-defkey minibuffer-local-map [(shift down)]
  9194. (lambda () (interactive)
  9195. (org-eval-in-calendar '(calendar-forward-week 1))))
  9196. (org-defkey minibuffer-local-map [(shift left)]
  9197. (lambda () (interactive)
  9198. (org-eval-in-calendar '(calendar-backward-day 1))))
  9199. (org-defkey minibuffer-local-map [(shift right)]
  9200. (lambda () (interactive)
  9201. (org-eval-in-calendar '(calendar-forward-day 1))))
  9202. (org-defkey minibuffer-local-map ">"
  9203. (lambda () (interactive)
  9204. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9205. (org-defkey minibuffer-local-map "<"
  9206. (lambda () (interactive)
  9207. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9208. (unwind-protect
  9209. (progn
  9210. (use-local-map map)
  9211. (add-hook 'post-command-hook 'org-read-date-display)
  9212. (setq org-ans0 (read-string prompt default-input nil nil))
  9213. ;; org-ans0: from prompt
  9214. ;; org-ans1: from mouse click
  9215. ;; org-ans2: from calendar motion
  9216. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9217. (remove-hook 'post-command-hook 'org-read-date-display)
  9218. (use-local-map old-map)
  9219. (when org-read-date-overlay
  9220. (org-delete-overlay org-read-date-overlay)
  9221. (setq org-read-date-overlay nil)))))))
  9222. (t ; Naked prompt only
  9223. (unwind-protect
  9224. (setq ans (read-string prompt default-input nil timestr))
  9225. (when org-read-date-overlay
  9226. (org-delete-overlay org-read-date-overlay)
  9227. (setq org-read-date-overlay nil)))))
  9228. (setq final (org-read-date-analyze ans def defdecode))
  9229. (if to-time
  9230. (apply 'encode-time final)
  9231. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9232. (format "%04d-%02d-%02d %02d:%02d"
  9233. (nth 5 final) (nth 4 final) (nth 3 final)
  9234. (nth 2 final) (nth 1 final))
  9235. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9236. (defvar def)
  9237. (defvar defdecode)
  9238. (defvar with-time)
  9239. (defun org-read-date-display ()
  9240. "Display the currrent date prompt interpretation in the minibuffer."
  9241. (when org-read-date-display-live
  9242. (when org-read-date-overlay
  9243. (org-delete-overlay org-read-date-overlay))
  9244. (let ((p (point)))
  9245. (end-of-line 1)
  9246. (while (not (equal (buffer-substring
  9247. (max (point-min) (- (point) 4)) (point))
  9248. " "))
  9249. (insert " "))
  9250. (goto-char p))
  9251. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9252. " " (or org-ans1 org-ans2)))
  9253. (org-end-time-was-given nil)
  9254. (f (org-read-date-analyze ans def defdecode))
  9255. (fmts (if org-dcst
  9256. org-time-stamp-custom-formats
  9257. org-time-stamp-formats))
  9258. (fmt (if (or with-time
  9259. (and (boundp 'org-time-was-given) org-time-was-given))
  9260. (cdr fmts)
  9261. (car fmts)))
  9262. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9263. (when (and org-end-time-was-given
  9264. (string-match org-plain-time-of-day-regexp txt))
  9265. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9266. org-end-time-was-given
  9267. (substring txt (match-end 0)))))
  9268. (setq org-read-date-overlay
  9269. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9270. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9271. (defun org-read-date-analyze (ans def defdecode)
  9272. "Analyze the combined answer of the date prompt."
  9273. ;; FIXME: cleanup and comment
  9274. (let (delta deltan deltaw deltadef year month day
  9275. hour minute second wday pm h2 m2 tl wday1
  9276. iso-year iso-weekday iso-week iso-year iso-date)
  9277. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9278. (setq ans (replace-match "" t t ans)
  9279. deltan (car delta)
  9280. deltaw (nth 1 delta)
  9281. deltadef (nth 2 delta)))
  9282. ;; Check if there is an iso week date in there
  9283. ;; If yes, sore the info and ostpone interpreting it until the rest
  9284. ;; of the parsing is done
  9285. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9286. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9287. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9288. iso-week (string-to-number (match-string 2 ans)))
  9289. (setq ans (replace-match "" t t ans)))
  9290. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9291. (when (string-match
  9292. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9293. (setq year (if (match-end 2)
  9294. (string-to-number (match-string 2 ans))
  9295. (string-to-number (format-time-string "%Y")))
  9296. month (string-to-number (match-string 3 ans))
  9297. day (string-to-number (match-string 4 ans)))
  9298. (if (< year 100) (setq year (+ 2000 year)))
  9299. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9300. t nil ans)))
  9301. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9302. ;; If there is a time with am/pm, and *no* time without it, we convert
  9303. ;; so that matching will be successful.
  9304. (loop for i from 1 to 2 do ; twice, for end time as well
  9305. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9306. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9307. (setq hour (string-to-number (match-string 1 ans))
  9308. minute (if (match-end 3)
  9309. (string-to-number (match-string 3 ans))
  9310. 0)
  9311. pm (equal ?p
  9312. (string-to-char (downcase (match-string 4 ans)))))
  9313. (if (and (= hour 12) (not pm))
  9314. (setq hour 0)
  9315. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9316. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9317. t t ans))))
  9318. ;; Check if a time range is given as a duration
  9319. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9320. (setq hour (string-to-number (match-string 1 ans))
  9321. h2 (+ hour (string-to-number (match-string 3 ans)))
  9322. minute (string-to-number (match-string 2 ans))
  9323. m2 (+ minute (if (match-end 5) (string-to-number
  9324. (match-string 5 ans))0)))
  9325. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9326. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9327. t t ans)))
  9328. ;; Check if there is a time range
  9329. (when (boundp 'org-end-time-was-given)
  9330. (setq org-time-was-given nil)
  9331. (when (and (string-match org-plain-time-of-day-regexp ans)
  9332. (match-end 8))
  9333. (setq org-end-time-was-given (match-string 8 ans))
  9334. (setq ans (concat (substring ans 0 (match-beginning 7))
  9335. (substring ans (match-end 7))))))
  9336. (setq tl (parse-time-string ans)
  9337. day (or (nth 3 tl) (nth 3 defdecode))
  9338. month (or (nth 4 tl)
  9339. (if (and org-read-date-prefer-future
  9340. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9341. (1+ (nth 4 defdecode))
  9342. (nth 4 defdecode)))
  9343. year (or (nth 5 tl)
  9344. (if (and org-read-date-prefer-future
  9345. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9346. (1+ (nth 5 defdecode))
  9347. (nth 5 defdecode)))
  9348. hour (or (nth 2 tl) (nth 2 defdecode))
  9349. minute (or (nth 1 tl) (nth 1 defdecode))
  9350. second (or (nth 0 tl) 0)
  9351. wday (nth 6 tl))
  9352. ;; Special date definitions below
  9353. (cond
  9354. (iso-week
  9355. ;; There was an iso week
  9356. (setq year (or iso-year year)
  9357. day (or iso-weekday wday 1)
  9358. wday nil ; to make sure that the trigger below does not match
  9359. iso-date (calendar-gregorian-from-absolute
  9360. (calendar-absolute-from-iso
  9361. (list iso-week day year))))
  9362. ; FIXME: Should we also push ISO weeks into the future?
  9363. ; (when (and org-read-date-prefer-future
  9364. ; (not iso-year)
  9365. ; (< (calendar-absolute-from-gregorian iso-date)
  9366. ; (time-to-days (current-time))))
  9367. ; (setq year (1+ year)
  9368. ; iso-date (calendar-gregorian-from-absolute
  9369. ; (calendar-absolute-from-iso
  9370. ; (list iso-week day year)))))
  9371. (setq month (car iso-date)
  9372. year (nth 2 iso-date)
  9373. day (nth 1 iso-date)))
  9374. (deltan
  9375. (unless deltadef
  9376. (let ((now (decode-time (current-time))))
  9377. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9378. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9379. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9380. ((equal deltaw "m") (setq month (+ month deltan)))
  9381. ((equal deltaw "y") (setq year (+ year deltan)))))
  9382. ((and wday (not (nth 3 tl)))
  9383. ;; Weekday was given, but no day, so pick that day in the week
  9384. ;; on or after the derived date.
  9385. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9386. (unless (equal wday wday1)
  9387. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9388. (if (and (boundp 'org-time-was-given)
  9389. (nth 2 tl))
  9390. (setq org-time-was-given t))
  9391. (if (< year 100) (setq year (+ 2000 year)))
  9392. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9393. (list second minute hour day month year)))
  9394. (defvar parse-time-weekdays)
  9395. (defun org-read-date-get-relative (s today default)
  9396. "Check string S for special relative date string.
  9397. TODAY and DEFAULT are internal times, for today and for a default.
  9398. Return shift list (N what def-flag)
  9399. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9400. N is the number of WHATs to shift.
  9401. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9402. the DEFAULT date rather than TODAY."
  9403. (when (string-match
  9404. (concat
  9405. "\\`[ \t]*\\([-+]\\{1,2\\}\\)"
  9406. "\\([0-9]+\\)?"
  9407. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9408. "\\([ \t]\\|$\\)") s)
  9409. (let* ((dir (if (match-end 1)
  9410. (string-to-char (substring (match-string 1 s) -1))
  9411. ?+))
  9412. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9413. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9414. (what (if (match-end 3) (match-string 3 s) "d"))
  9415. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9416. (date (if rel default today))
  9417. (wday (nth 6 (decode-time date)))
  9418. delta)
  9419. (if wday1
  9420. (progn
  9421. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9422. (if (= dir ?-) (setq delta (- delta 7)))
  9423. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9424. (list delta "d" rel))
  9425. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9426. (defun org-eval-in-calendar (form &optional keepdate)
  9427. "Eval FORM in the calendar window and return to current window.
  9428. Also, store the cursor date in variable org-ans2."
  9429. (let ((sw (selected-window)))
  9430. (select-window (get-buffer-window "*Calendar*"))
  9431. (eval form)
  9432. (when (and (not keepdate) (calendar-cursor-to-date))
  9433. (let* ((date (calendar-cursor-to-date))
  9434. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9435. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9436. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9437. (select-window sw)))
  9438. ; ;; Update the prompt to show new default date
  9439. ; (save-excursion
  9440. ; (goto-char (point-min))
  9441. ; (when (and org-ans2
  9442. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9443. ; (get-text-property (match-end 0) 'field))
  9444. ; (let ((inhibit-read-only t))
  9445. ; (replace-match (concat "[" org-ans2 "]") t t)
  9446. ; (add-text-properties (point-min) (1+ (match-end 0))
  9447. ; (text-properties-at (1+ (point-min)))))))))
  9448. (defun org-calendar-select ()
  9449. "Return to `org-read-date' with the date currently selected.
  9450. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9451. (interactive)
  9452. (when (calendar-cursor-to-date)
  9453. (let* ((date (calendar-cursor-to-date))
  9454. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9455. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9456. (if (active-minibuffer-window) (exit-minibuffer))))
  9457. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9458. "Insert a date stamp for the date given by the internal TIME.
  9459. WITH-HM means, use the stamp format that includes the time of the day.
  9460. INACTIVE means use square brackets instead of angular ones, so that the
  9461. stamp will not contribute to the agenda.
  9462. PRE and POST are optional strings to be inserted before and after the
  9463. stamp.
  9464. The command returns the inserted time stamp."
  9465. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9466. stamp)
  9467. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9468. (insert-before-markers (or pre ""))
  9469. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9470. (when (listp extra)
  9471. (setq extra (car extra))
  9472. (if (and (stringp extra)
  9473. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9474. (setq extra (format "-%02d:%02d"
  9475. (string-to-number (match-string 1 extra))
  9476. (string-to-number (match-string 2 extra))))
  9477. (setq extra nil)))
  9478. (when extra
  9479. (backward-char 1)
  9480. (insert-before-markers extra)
  9481. (forward-char 1))
  9482. (insert-before-markers (or post ""))
  9483. stamp))
  9484. (defun org-toggle-time-stamp-overlays ()
  9485. "Toggle the use of custom time stamp formats."
  9486. (interactive)
  9487. (setq org-display-custom-times (not org-display-custom-times))
  9488. (unless org-display-custom-times
  9489. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9490. (while (setq p (next-single-property-change p 'display))
  9491. (if (and (get-text-property p 'display)
  9492. (eq (get-text-property p 'face) 'org-date))
  9493. (remove-text-properties
  9494. p (setq p (next-single-property-change p 'display))
  9495. '(display t))))
  9496. (set-buffer-modified-p bmp)))
  9497. (if (featurep 'xemacs)
  9498. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9499. (org-restart-font-lock)
  9500. (setq org-table-may-need-update t)
  9501. (if org-display-custom-times
  9502. (message "Time stamps are overlayed with custom format")
  9503. (message "Time stamp overlays removed")))
  9504. (defun org-display-custom-time (beg end)
  9505. "Overlay modified time stamp format over timestamp between BED and END."
  9506. (let* ((ts (buffer-substring beg end))
  9507. t1 w1 with-hm tf time str w2 (off 0))
  9508. (save-match-data
  9509. (setq t1 (org-parse-time-string ts t))
  9510. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9511. (setq off (- (match-end 0) (match-beginning 0)))))
  9512. (setq end (- end off))
  9513. (setq w1 (- end beg)
  9514. with-hm (and (nth 1 t1) (nth 2 t1))
  9515. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9516. time (org-fix-decoded-time t1)
  9517. str (org-add-props
  9518. (format-time-string
  9519. (substring tf 1 -1) (apply 'encode-time time))
  9520. nil 'mouse-face 'highlight)
  9521. w2 (length str))
  9522. (if (not (= w2 w1))
  9523. (add-text-properties (1+ beg) (+ 2 beg)
  9524. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9525. (if (featurep 'xemacs)
  9526. (progn
  9527. (put-text-property beg end 'invisible t)
  9528. (put-text-property beg end 'end-glyph (make-glyph str)))
  9529. (put-text-property beg end 'display str))))
  9530. (defun org-translate-time (string)
  9531. "Translate all timestamps in STRING to custom format.
  9532. But do this only if the variable `org-display-custom-times' is set."
  9533. (when org-display-custom-times
  9534. (save-match-data
  9535. (let* ((start 0)
  9536. (re org-ts-regexp-both)
  9537. t1 with-hm inactive tf time str beg end)
  9538. (while (setq start (string-match re string start))
  9539. (setq beg (match-beginning 0)
  9540. end (match-end 0)
  9541. t1 (save-match-data
  9542. (org-parse-time-string (substring string beg end) t))
  9543. with-hm (and (nth 1 t1) (nth 2 t1))
  9544. inactive (equal (substring string beg (1+ beg)) "[")
  9545. tf (funcall (if with-hm 'cdr 'car)
  9546. org-time-stamp-custom-formats)
  9547. time (org-fix-decoded-time t1)
  9548. str (format-time-string
  9549. (concat
  9550. (if inactive "[" "<") (substring tf 1 -1)
  9551. (if inactive "]" ">"))
  9552. (apply 'encode-time time))
  9553. string (replace-match str t t string)
  9554. start (+ start (length str)))))))
  9555. string)
  9556. (defun org-fix-decoded-time (time)
  9557. "Set 0 instead of nil for the first 6 elements of time.
  9558. Don't touch the rest."
  9559. (let ((n 0))
  9560. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9561. (defun org-days-to-time (timestamp-string)
  9562. "Difference between TIMESTAMP-STRING and now in days."
  9563. (- (time-to-days (org-time-string-to-time timestamp-string))
  9564. (time-to-days (current-time))))
  9565. (defun org-deadline-close (timestamp-string &optional ndays)
  9566. "Is the time in TIMESTAMP-STRING close to the current date?"
  9567. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9568. (and (< (org-days-to-time timestamp-string) ndays)
  9569. (not (org-entry-is-done-p))))
  9570. (defun org-get-wdays (ts)
  9571. "Get the deadline lead time appropriate for timestring TS."
  9572. (cond
  9573. ((<= org-deadline-warning-days 0)
  9574. ;; 0 or negative, enforce this value no matter what
  9575. (- org-deadline-warning-days))
  9576. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9577. ;; lead time is specified.
  9578. (floor (* (string-to-number (match-string 1 ts))
  9579. (cdr (assoc (match-string 2 ts)
  9580. '(("d" . 1) ("w" . 7)
  9581. ("m" . 30.4) ("y" . 365.25)))))))
  9582. ;; go for the default.
  9583. (t org-deadline-warning-days)))
  9584. (defun org-calendar-select-mouse (ev)
  9585. "Return to `org-read-date' with the date currently selected.
  9586. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9587. (interactive "e")
  9588. (mouse-set-point ev)
  9589. (when (calendar-cursor-to-date)
  9590. (let* ((date (calendar-cursor-to-date))
  9591. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9592. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9593. (if (active-minibuffer-window) (exit-minibuffer))))
  9594. (defun org-check-deadlines (ndays)
  9595. "Check if there are any deadlines due or past due.
  9596. A deadline is considered due if it happens within `org-deadline-warning-days'
  9597. days from today's date. If the deadline appears in an entry marked DONE,
  9598. it is not shown. The prefix arg NDAYS can be used to test that many
  9599. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9600. (interactive "P")
  9601. (let* ((org-warn-days
  9602. (cond
  9603. ((equal ndays '(4)) 100000)
  9604. (ndays (prefix-numeric-value ndays))
  9605. (t (abs org-deadline-warning-days))))
  9606. (case-fold-search nil)
  9607. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9608. (callback
  9609. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9610. (message "%d deadlines past-due or due within %d days"
  9611. (org-occur regexp nil callback)
  9612. org-warn-days)))
  9613. (defun org-check-before-date (date)
  9614. "Check if there are deadlines or scheduled entries before DATE."
  9615. (interactive (list (org-read-date)))
  9616. (let ((case-fold-search nil)
  9617. (regexp (concat "\\<\\(" org-deadline-string
  9618. "\\|" org-scheduled-string
  9619. "\\) *<\\([^>]+\\)>"))
  9620. (callback
  9621. (lambda () (time-less-p
  9622. (org-time-string-to-time (match-string 2))
  9623. (org-time-string-to-time date)))))
  9624. (message "%d entries before %s"
  9625. (org-occur regexp nil callback) date)))
  9626. (defun org-evaluate-time-range (&optional to-buffer)
  9627. "Evaluate a time range by computing the difference between start and end.
  9628. Normally the result is just printed in the echo area, but with prefix arg
  9629. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9630. If the time range is actually in a table, the result is inserted into the
  9631. next column.
  9632. For time difference computation, a year is assumed to be exactly 365
  9633. days in order to avoid rounding problems."
  9634. (interactive "P")
  9635. (or
  9636. (org-clock-update-time-maybe)
  9637. (save-excursion
  9638. (unless (org-at-date-range-p t)
  9639. (goto-char (point-at-bol))
  9640. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9641. (if (not (org-at-date-range-p t))
  9642. (error "Not at a time-stamp range, and none found in current line")))
  9643. (let* ((ts1 (match-string 1))
  9644. (ts2 (match-string 2))
  9645. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9646. (match-end (match-end 0))
  9647. (time1 (org-time-string-to-time ts1))
  9648. (time2 (org-time-string-to-time ts2))
  9649. (t1 (time-to-seconds time1))
  9650. (t2 (time-to-seconds time2))
  9651. (diff (abs (- t2 t1)))
  9652. (negative (< (- t2 t1) 0))
  9653. ;; (ys (floor (* 365 24 60 60)))
  9654. (ds (* 24 60 60))
  9655. (hs (* 60 60))
  9656. (fy "%dy %dd %02d:%02d")
  9657. (fy1 "%dy %dd")
  9658. (fd "%dd %02d:%02d")
  9659. (fd1 "%dd")
  9660. (fh "%02d:%02d")
  9661. y d h m align)
  9662. (if havetime
  9663. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9664. y 0
  9665. d (floor (/ diff ds)) diff (mod diff ds)
  9666. h (floor (/ diff hs)) diff (mod diff hs)
  9667. m (floor (/ diff 60)))
  9668. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9669. y 0
  9670. d (floor (+ (/ diff ds) 0.5))
  9671. h 0 m 0))
  9672. (if (not to-buffer)
  9673. (message "%s" (org-make-tdiff-string y d h m))
  9674. (if (org-at-table-p)
  9675. (progn
  9676. (goto-char match-end)
  9677. (setq align t)
  9678. (and (looking-at " *|") (goto-char (match-end 0))))
  9679. (goto-char match-end))
  9680. (if (looking-at
  9681. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9682. (replace-match ""))
  9683. (if negative (insert " -"))
  9684. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9685. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9686. (insert " " (format fh h m))))
  9687. (if align (org-table-align))
  9688. (message "Time difference inserted")))))
  9689. (defun org-make-tdiff-string (y d h m)
  9690. (let ((fmt "")
  9691. (l nil))
  9692. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9693. l (push y l)))
  9694. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9695. l (push d l)))
  9696. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9697. l (push h l)))
  9698. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9699. l (push m l)))
  9700. (apply 'format fmt (nreverse l))))
  9701. (defun org-time-string-to-time (s)
  9702. (apply 'encode-time (org-parse-time-string s)))
  9703. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9704. "Convert a time stamp to an absolute day number.
  9705. If there is a specifyer for a cyclic time stamp, get the closest date to
  9706. DAYNR.
  9707. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9708. (cond
  9709. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9710. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9711. daynr
  9712. (+ daynr 1000)))
  9713. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9714. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9715. (time-to-days (current-time))) (match-string 0 s)
  9716. prefer show-all))
  9717. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9718. (defun org-days-to-iso-week (days)
  9719. "Return the iso week number."
  9720. (require 'cal-iso)
  9721. (car (calendar-iso-from-absolute days)))
  9722. (defun org-small-year-to-year (year)
  9723. "Convert 2-digit years into 4-digit years.
  9724. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9725. The year 2000 cannot be abbreviated. Any year lager than 99
  9726. is retrned unchanged."
  9727. (if (< year 38)
  9728. (setq year (+ 2000 year))
  9729. (if (< year 100)
  9730. (setq year (+ 1900 year))))
  9731. year)
  9732. (defun org-time-from-absolute (d)
  9733. "Return the time corresponding to date D.
  9734. D may be an absolute day number, or a calendar-type list (month day year)."
  9735. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9736. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9737. (defun org-calendar-holiday ()
  9738. "List of holidays, for Diary display in Org-mode."
  9739. (require 'holidays)
  9740. (let ((hl (funcall
  9741. (if (fboundp 'calendar-check-holidays)
  9742. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9743. (if hl (mapconcat 'identity hl "; "))))
  9744. (defun org-diary-sexp-entry (sexp entry date)
  9745. "Process a SEXP diary ENTRY for DATE."
  9746. (require 'diary-lib)
  9747. (let ((result (if calendar-debug-sexp
  9748. (let ((stack-trace-on-error t))
  9749. (eval (car (read-from-string sexp))))
  9750. (condition-case nil
  9751. (eval (car (read-from-string sexp)))
  9752. (error
  9753. (beep)
  9754. (message "Bad sexp at line %d in %s: %s"
  9755. (org-current-line)
  9756. (buffer-file-name) sexp)
  9757. (sleep-for 2))))))
  9758. (cond ((stringp result) result)
  9759. ((and (consp result)
  9760. (stringp (cdr result))) (cdr result))
  9761. (result entry)
  9762. (t nil))))
  9763. (defun org-diary-to-ical-string (frombuf)
  9764. "Get iCalendar entries from diary entries in buffer FROMBUF.
  9765. This uses the icalendar.el library."
  9766. (let* ((tmpdir (if (featurep 'xemacs)
  9767. (temp-directory)
  9768. temporary-file-directory))
  9769. (tmpfile (make-temp-name
  9770. (expand-file-name "orgics" tmpdir)))
  9771. buf rtn b e)
  9772. (save-excursion
  9773. (set-buffer frombuf)
  9774. (icalendar-export-region (point-min) (point-max) tmpfile)
  9775. (setq buf (find-buffer-visiting tmpfile))
  9776. (set-buffer buf)
  9777. (goto-char (point-min))
  9778. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  9779. (setq b (match-beginning 0)))
  9780. (goto-char (point-max))
  9781. (if (re-search-backward "^END:VEVENT" nil t)
  9782. (setq e (match-end 0)))
  9783. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  9784. (kill-buffer buf)
  9785. (kill-buffer frombuf)
  9786. (delete-file tmpfile)
  9787. rtn))
  9788. (defun org-closest-date (start current change prefer show-all)
  9789. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  9790. When PREFER is `past' return a date that is either CURRENT or past.
  9791. When PREFER is `future', return a date that is either CURRENT or future.
  9792. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  9793. ;; Make the proper lists from the dates
  9794. (catch 'exit
  9795. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  9796. dn dw sday cday n1 n2
  9797. d m y y1 y2 date1 date2 nmonths nm ny m2)
  9798. (setq start (org-date-to-gregorian start)
  9799. current (org-date-to-gregorian
  9800. (if show-all
  9801. current
  9802. (time-to-days (current-time))))
  9803. sday (calendar-absolute-from-gregorian start)
  9804. cday (calendar-absolute-from-gregorian current))
  9805. (if (<= cday sday) (throw 'exit sday))
  9806. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  9807. (setq dn (string-to-number (match-string 1 change))
  9808. dw (cdr (assoc (match-string 2 change) a1)))
  9809. (error "Invalid change specifyer: %s" change))
  9810. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  9811. (cond
  9812. ((eq dw 'day)
  9813. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  9814. n2 (+ n1 dn)))
  9815. ((eq dw 'year)
  9816. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  9817. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  9818. (setq date1 (list m d y1)
  9819. n1 (calendar-absolute-from-gregorian date1)
  9820. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  9821. n2 (calendar-absolute-from-gregorian date2)))
  9822. ((eq dw 'month)
  9823. ;; approx number of month between the tow dates
  9824. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  9825. ;; How often does dn fit in there?
  9826. (setq d (nth 1 start) m (car start) y (nth 2 start)
  9827. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  9828. m (+ m nm)
  9829. ny (floor (/ m 12))
  9830. y (+ y ny)
  9831. m (- m (* ny 12)))
  9832. (while (> m 12) (setq m (- m 12) y (1+ y)))
  9833. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  9834. (setq m2 (+ m dn) y2 y)
  9835. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9836. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  9837. (while (< n2 cday)
  9838. (setq n1 n2 m m2 y y2)
  9839. (setq m2 (+ m dn) y2 y)
  9840. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  9841. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  9842. (if show-all
  9843. (cond
  9844. ((eq prefer 'past) n1)
  9845. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9846. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  9847. (cond
  9848. ((eq prefer 'past) n1)
  9849. ((eq prefer 'future) (if (= cday n1) n1 n2))
  9850. (t (if (= cday n1) n1 n2)))))))
  9851. (defun org-date-to-gregorian (date)
  9852. "Turn any specification of DATE into a gregorian date for the calendar."
  9853. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  9854. ((and (listp date) (= (length date) 3)) date)
  9855. ((stringp date)
  9856. (setq date (org-parse-time-string date))
  9857. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  9858. ((listp date)
  9859. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  9860. (defun org-parse-time-string (s &optional nodefault)
  9861. "Parse the standard Org-mode time string.
  9862. This should be a lot faster than the normal `parse-time-string'.
  9863. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  9864. hour and minute fields will be nil if not given."
  9865. (if (string-match org-ts-regexp0 s)
  9866. (list 0
  9867. (if (or (match-beginning 8) (not nodefault))
  9868. (string-to-number (or (match-string 8 s) "0")))
  9869. (if (or (match-beginning 7) (not nodefault))
  9870. (string-to-number (or (match-string 7 s) "0")))
  9871. (string-to-number (match-string 4 s))
  9872. (string-to-number (match-string 3 s))
  9873. (string-to-number (match-string 2 s))
  9874. nil nil nil)
  9875. (make-list 9 0)))
  9876. (defun org-timestamp-up (&optional arg)
  9877. "Increase the date item at the cursor by one.
  9878. If the cursor is on the year, change the year. If it is on the month or
  9879. the day, change that.
  9880. With prefix ARG, change by that many units."
  9881. (interactive "p")
  9882. (org-timestamp-change (prefix-numeric-value arg)))
  9883. (defun org-timestamp-down (&optional arg)
  9884. "Decrease the date item at the cursor by one.
  9885. If the cursor is on the year, change the year. If it is on the month or
  9886. the day, change that.
  9887. With prefix ARG, change by that many units."
  9888. (interactive "p")
  9889. (org-timestamp-change (- (prefix-numeric-value arg))))
  9890. (defun org-timestamp-up-day (&optional arg)
  9891. "Increase the date in the time stamp by one day.
  9892. With prefix ARG, change that many days."
  9893. (interactive "p")
  9894. (if (and (not (org-at-timestamp-p t))
  9895. (org-on-heading-p))
  9896. (org-todo 'up)
  9897. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  9898. (defun org-timestamp-down-day (&optional arg)
  9899. "Decrease the date in the time stamp by one day.
  9900. With prefix ARG, change that many days."
  9901. (interactive "p")
  9902. (if (and (not (org-at-timestamp-p t))
  9903. (org-on-heading-p))
  9904. (org-todo 'down)
  9905. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  9906. (defun org-at-timestamp-p (&optional inactive-ok)
  9907. "Determine if the cursor is in or at a timestamp."
  9908. (interactive)
  9909. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  9910. (pos (point))
  9911. (ans (or (looking-at tsr)
  9912. (save-excursion
  9913. (skip-chars-backward "^[<\n\r\t")
  9914. (if (> (point) (point-min)) (backward-char 1))
  9915. (and (looking-at tsr)
  9916. (> (- (match-end 0) pos) -1))))))
  9917. (and ans
  9918. (boundp 'org-ts-what)
  9919. (setq org-ts-what
  9920. (cond
  9921. ((= pos (match-beginning 0)) 'bracket)
  9922. ((= pos (1- (match-end 0))) 'bracket)
  9923. ((org-pos-in-match-range pos 2) 'year)
  9924. ((org-pos-in-match-range pos 3) 'month)
  9925. ((org-pos-in-match-range pos 7) 'hour)
  9926. ((org-pos-in-match-range pos 8) 'minute)
  9927. ((or (org-pos-in-match-range pos 4)
  9928. (org-pos-in-match-range pos 5)) 'day)
  9929. ((and (> pos (or (match-end 8) (match-end 5)))
  9930. (< pos (match-end 0)))
  9931. (- pos (or (match-end 8) (match-end 5))))
  9932. (t 'day))))
  9933. ans))
  9934. (defun org-toggle-timestamp-type ()
  9935. "Toggle the type (<active> or [inactive]) of a time stamp."
  9936. (interactive)
  9937. (when (org-at-timestamp-p t)
  9938. (save-excursion
  9939. (goto-char (match-beginning 0))
  9940. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  9941. (goto-char (1- (match-end 0)))
  9942. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  9943. (message "Timestamp is now %sactive"
  9944. (if (equal (char-before) ?>) "in" ""))))
  9945. (defun org-timestamp-change (n &optional what)
  9946. "Change the date in the time stamp at point.
  9947. The date will be changed by N times WHAT. WHAT can be `day', `month',
  9948. `year', `minute', `second'. If WHAT is not given, the cursor position
  9949. in the timestamp determines what will be changed."
  9950. (let ((pos (point))
  9951. with-hm inactive
  9952. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  9953. org-ts-what
  9954. extra rem
  9955. ts time time0)
  9956. (if (not (org-at-timestamp-p t))
  9957. (error "Not at a timestamp"))
  9958. (if (and (not what) (eq org-ts-what 'bracket))
  9959. (org-toggle-timestamp-type)
  9960. (if (and (not what) (not (eq org-ts-what 'day))
  9961. org-display-custom-times
  9962. (get-text-property (point) 'display)
  9963. (not (get-text-property (1- (point)) 'display)))
  9964. (setq org-ts-what 'day))
  9965. (setq org-ts-what (or what org-ts-what)
  9966. inactive (= (char-after (match-beginning 0)) ?\[)
  9967. ts (match-string 0))
  9968. (replace-match "")
  9969. (if (string-match
  9970. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  9971. ts)
  9972. (setq extra (match-string 1 ts)))
  9973. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  9974. (setq with-hm t))
  9975. (setq time0 (org-parse-time-string ts))
  9976. (when (and (eq org-ts-what 'minute)
  9977. (eq current-prefix-arg nil))
  9978. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  9979. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  9980. (setcar (cdr time0) (+ (nth 1 time0)
  9981. (if (> n 0) (- rem) (- dm rem))))))
  9982. (setq time
  9983. (encode-time (or (car time0) 0)
  9984. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  9985. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  9986. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  9987. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  9988. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  9989. (nthcdr 6 time0)))
  9990. (when (integerp org-ts-what)
  9991. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  9992. (if (eq what 'calendar)
  9993. (let ((cal-date (org-get-date-from-calendar)))
  9994. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  9995. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  9996. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  9997. (setcar time0 (or (car time0) 0))
  9998. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  9999. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10000. (setq time (apply 'encode-time time0))))
  10001. (setq org-last-changed-timestamp
  10002. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10003. (org-clock-update-time-maybe)
  10004. (goto-char pos)
  10005. ;; Try to recenter the calendar window, if any
  10006. (if (and org-calendar-follow-timestamp-change
  10007. (get-buffer-window "*Calendar*" t)
  10008. (memq org-ts-what '(day month year)))
  10009. (org-recenter-calendar (time-to-days time))))))
  10010. (defun org-modify-ts-extra (s pos n dm)
  10011. "Change the different parts of the lead-time and repeat fields in timestamp."
  10012. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10013. ng h m new rem)
  10014. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10015. (cond
  10016. ((or (org-pos-in-match-range pos 2)
  10017. (org-pos-in-match-range pos 3))
  10018. (setq m (string-to-number (match-string 3 s))
  10019. h (string-to-number (match-string 2 s)))
  10020. (if (org-pos-in-match-range pos 2)
  10021. (setq h (+ h n))
  10022. (setq n (* dm (org-no-warnings (signum n))))
  10023. (when (not (= 0 (setq rem (% m dm))))
  10024. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10025. (setq m (+ m n)))
  10026. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10027. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10028. (setq h (min 24 (max 0 h)))
  10029. (setq ng 1 new (format "-%02d:%02d" h m)))
  10030. ((org-pos-in-match-range pos 6)
  10031. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10032. ((org-pos-in-match-range pos 5)
  10033. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10034. ((org-pos-in-match-range pos 9)
  10035. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10036. ((org-pos-in-match-range pos 8)
  10037. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10038. (when ng
  10039. (setq s (concat
  10040. (substring s 0 (match-beginning ng))
  10041. new
  10042. (substring s (match-end ng))))))
  10043. s))
  10044. (defun org-recenter-calendar (date)
  10045. "If the calendar is visible, recenter it to DATE."
  10046. (let* ((win (selected-window))
  10047. (cwin (get-buffer-window "*Calendar*" t))
  10048. (calendar-move-hook nil))
  10049. (when cwin
  10050. (select-window cwin)
  10051. (calendar-goto-date (if (listp date) date
  10052. (calendar-gregorian-from-absolute date)))
  10053. (select-window win))))
  10054. (defun org-goto-calendar (&optional arg)
  10055. "Go to the Emacs calendar at the current date.
  10056. If there is a time stamp in the current line, go to that date.
  10057. A prefix ARG can be used to force the current date."
  10058. (interactive "P")
  10059. (let ((tsr org-ts-regexp) diff
  10060. (calendar-move-hook nil)
  10061. (calendar-view-holidays-initially-flag nil)
  10062. (view-calendar-holidays-initially nil)
  10063. (calendar-view-diary-initially-flag nil)
  10064. (view-diary-entries-initially nil))
  10065. (if (or (org-at-timestamp-p)
  10066. (save-excursion
  10067. (beginning-of-line 1)
  10068. (looking-at (concat ".*" tsr))))
  10069. (let ((d1 (time-to-days (current-time)))
  10070. (d2 (time-to-days
  10071. (org-time-string-to-time (match-string 1)))))
  10072. (setq diff (- d2 d1))))
  10073. (calendar)
  10074. (calendar-goto-today)
  10075. (if (and diff (not arg)) (calendar-forward-day diff))))
  10076. (defun org-get-date-from-calendar ()
  10077. "Return a list (month day year) of date at point in calendar."
  10078. (with-current-buffer "*Calendar*"
  10079. (save-match-data
  10080. (calendar-cursor-to-date))))
  10081. (defun org-date-from-calendar ()
  10082. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10083. If there is already a time stamp at the cursor position, update it."
  10084. (interactive)
  10085. (if (org-at-timestamp-p t)
  10086. (org-timestamp-change 0 'calendar)
  10087. (let ((cal-date (org-get-date-from-calendar)))
  10088. (org-insert-time-stamp
  10089. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10090. (defun org-minutes-to-hh:mm-string (m)
  10091. "Compute H:MM from a number of minutes."
  10092. (let ((h (/ m 60)))
  10093. (setq m (- m (* 60 h)))
  10094. (format "%d:%02d" h m)))
  10095. (defun org-hh:mm-string-to-minutes (s)
  10096. "Convert a string H:MM to a number of minutes."
  10097. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10098. (+ (* (string-to-number (match-string 1 s)) 60)
  10099. (string-to-number (match-string 2 s)))
  10100. 0))
  10101. ;;;; Agenda files
  10102. ;;;###autoload
  10103. (defun org-iswitchb (&optional arg)
  10104. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10105. With a prefix argument, restrict available to files.
  10106. With two prefix arguments, restrict available buffers to agenda files.
  10107. Due to some yet unresolved reason, global function
  10108. `iswitchb-mode' needs to be active for this function to work."
  10109. (interactive "P")
  10110. (require 'iswitchb)
  10111. (let ((enabled iswitchb-mode) blist)
  10112. (or enabled (iswitchb-mode 1))
  10113. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10114. ((equal arg '(16)) (org-buffer-list 'agenda))
  10115. (t (org-buffer-list))))
  10116. (unwind-protect
  10117. (let ((iswitchb-make-buflist-hook
  10118. (lambda ()
  10119. (setq iswitchb-temp-buflist
  10120. (mapcar 'buffer-name blist)))))
  10121. (switch-to-buffer
  10122. (iswitchb-read-buffer
  10123. "Switch-to: " nil t))
  10124. (or enabled (iswitchb-mode -1))))))
  10125. (defun org-buffer-list (&optional predicate tmp)
  10126. "Return a list of Org buffers.
  10127. PREDICATE can be either 'export, 'files or 'agenda.
  10128. 'export restrict the list to Export buffers.
  10129. 'files restrict the list to buffers visiting Org files.
  10130. 'agenda restrict the list to buffers visiting agenda files.
  10131. If TMP is non-nil, don't include temporary buffers."
  10132. (let (filter blist)
  10133. (setq filter
  10134. (cond ((eq predicate 'files) "\.org$")
  10135. ((eq predicate 'export) "\*Org .*Export")
  10136. (t "\*Org \\|\.org$")))
  10137. (setq blist
  10138. (mapcar
  10139. (lambda(b)
  10140. (let ((bname (buffer-name b))
  10141. (bfile (buffer-file-name b)))
  10142. (if (and (string-match filter bname)
  10143. (if (eq predicate 'agenda)
  10144. (member bfile
  10145. (mapcar (lambda(f) (file-truename f))
  10146. org-agenda-files)) t)
  10147. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10148. (buffer-list)))
  10149. (delete nil blist)))
  10150. (defun org-agenda-files (&optional unrestricted ext)
  10151. "Get the list of agenda files.
  10152. Optional UNRESTRICTED means return the full list even if a restriction
  10153. is currently in place.
  10154. When EXT is non-nil, try to add all files that are created by adding EXT
  10155. to the file nemes. Basically, this is a way to add the archive files
  10156. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10157. a string, \"_archive\" will be used."
  10158. (let ((files
  10159. (cond
  10160. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10161. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10162. ((listp org-agenda-files) org-agenda-files)
  10163. (t (error "Invalid value of `org-agenda-files'")))))
  10164. (setq files (apply 'append
  10165. (mapcar (lambda (f)
  10166. (if (file-directory-p f)
  10167. (directory-files
  10168. f t org-agenda-file-regexp)
  10169. (list f)))
  10170. files)))
  10171. (when org-agenda-skip-unavailable-files
  10172. (setq files (delq nil
  10173. (mapcar (function
  10174. (lambda (file)
  10175. (and (file-readable-p file) file)))
  10176. files))))
  10177. (when ext
  10178. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10179. ext "_archive"))
  10180. (setq files (apply 'append
  10181. (mapcar
  10182. (lambda (f)
  10183. (if (file-exists-p (concat f ext))
  10184. (list f (concat f ext))
  10185. (list f)))
  10186. files))))
  10187. files))
  10188. (defun org-edit-agenda-file-list ()
  10189. "Edit the list of agenda files.
  10190. Depending on setup, this either uses customize to edit the variable
  10191. `org-agenda-files', or it visits the file that is holding the list. In the
  10192. latter case, the buffer is set up in a way that saving it automatically kills
  10193. the buffer and restores the previous window configuration."
  10194. (interactive)
  10195. (if (stringp org-agenda-files)
  10196. (let ((cw (current-window-configuration)))
  10197. (find-file org-agenda-files)
  10198. (org-set-local 'org-window-configuration cw)
  10199. (org-add-hook 'after-save-hook
  10200. (lambda ()
  10201. (set-window-configuration
  10202. (prog1 org-window-configuration
  10203. (kill-buffer (current-buffer))))
  10204. (org-install-agenda-files-menu)
  10205. (message "New agenda file list installed"))
  10206. nil 'local)
  10207. (message "%s" (substitute-command-keys
  10208. "Edit list and finish with \\[save-buffer]")))
  10209. (customize-variable 'org-agenda-files)))
  10210. (defun org-store-new-agenda-file-list (list)
  10211. "Set new value for the agenda file list and save it correcly."
  10212. (if (stringp org-agenda-files)
  10213. (let ((f org-agenda-files) b)
  10214. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10215. (with-temp-file f
  10216. (insert (mapconcat 'identity list "\n") "\n")))
  10217. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10218. (setq org-agenda-files list)
  10219. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10220. (defun org-read-agenda-file-list ()
  10221. "Read the list of agenda files from a file."
  10222. (when (file-directory-p org-agenda-files)
  10223. (error "`org-agenda-files' cannot be a single directory"))
  10224. (when (stringp org-agenda-files)
  10225. (with-temp-buffer
  10226. (insert-file-contents org-agenda-files)
  10227. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10228. ;;;###autoload
  10229. (defun org-cycle-agenda-files ()
  10230. "Cycle through the files in `org-agenda-files'.
  10231. If the current buffer visits an agenda file, find the next one in the list.
  10232. If the current buffer does not, find the first agenda file."
  10233. (interactive)
  10234. (let* ((fs (org-agenda-files t))
  10235. (files (append fs (list (car fs))))
  10236. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10237. file)
  10238. (unless files (error "No agenda files"))
  10239. (catch 'exit
  10240. (while (setq file (pop files))
  10241. (if (equal (file-truename file) tcf)
  10242. (when (car files)
  10243. (find-file (car files))
  10244. (throw 'exit t))))
  10245. (find-file (car fs)))
  10246. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10247. (defun org-agenda-file-to-front (&optional to-end)
  10248. "Move/add the current file to the top of the agenda file list.
  10249. If the file is not present in the list, it is added to the front. If it is
  10250. present, it is moved there. With optional argument TO-END, add/move to the
  10251. end of the list."
  10252. (interactive "P")
  10253. (let ((org-agenda-skip-unavailable-files nil)
  10254. (file-alist (mapcar (lambda (x)
  10255. (cons (file-truename x) x))
  10256. (org-agenda-files t)))
  10257. (ctf (file-truename buffer-file-name))
  10258. x had)
  10259. (setq x (assoc ctf file-alist) had x)
  10260. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10261. (if to-end
  10262. (setq file-alist (append (delq x file-alist) (list x)))
  10263. (setq file-alist (cons x (delq x file-alist))))
  10264. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10265. (org-install-agenda-files-menu)
  10266. (message "File %s to %s of agenda file list"
  10267. (if had "moved" "added") (if to-end "end" "front"))))
  10268. (defun org-remove-file (&optional file)
  10269. "Remove current file from the list of files in variable `org-agenda-files'.
  10270. These are the files which are being checked for agenda entries.
  10271. Optional argument FILE means, use this file instead of the current."
  10272. (interactive)
  10273. (let* ((org-agenda-skip-unavailable-files nil)
  10274. (file (or file buffer-file-name))
  10275. (true-file (file-truename file))
  10276. (afile (abbreviate-file-name file))
  10277. (files (delq nil (mapcar
  10278. (lambda (x)
  10279. (if (equal true-file
  10280. (file-truename x))
  10281. nil x))
  10282. (org-agenda-files t)))))
  10283. (if (not (= (length files) (length (org-agenda-files t))))
  10284. (progn
  10285. (org-store-new-agenda-file-list files)
  10286. (org-install-agenda-files-menu)
  10287. (message "Removed file: %s" afile))
  10288. (message "File was not in list: %s (not removed)" afile))))
  10289. (defun org-file-menu-entry (file)
  10290. (vector file (list 'find-file file) t))
  10291. (defun org-check-agenda-file (file)
  10292. "Make sure FILE exists. If not, ask user what to do."
  10293. (when (not (file-exists-p file))
  10294. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10295. (abbreviate-file-name file))
  10296. (let ((r (downcase (read-char-exclusive))))
  10297. (cond
  10298. ((equal r ?r)
  10299. (org-remove-file file)
  10300. (throw 'nextfile t))
  10301. (t (error "Abort"))))))
  10302. (defun org-get-agenda-file-buffer (file)
  10303. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10304. it to the list of buffers which might be released later."
  10305. (let ((buf (org-find-base-buffer-visiting file)))
  10306. (if buf
  10307. buf ; just return it
  10308. ;; Make a new buffer and remember it
  10309. (setq buf (find-file-noselect file))
  10310. (if buf (push buf org-agenda-new-buffers))
  10311. buf)))
  10312. (defun org-release-buffers (blist)
  10313. "Release all buffers in list, asking the user for confirmation when needed.
  10314. When a buffer is unmodified, it is just killed. When modified, it is saved
  10315. \(if the user agrees) and then killed."
  10316. (let (buf file)
  10317. (while (setq buf (pop blist))
  10318. (setq file (buffer-file-name buf))
  10319. (when (and (buffer-modified-p buf)
  10320. file
  10321. (y-or-n-p (format "Save file %s? " file)))
  10322. (with-current-buffer buf (save-buffer)))
  10323. (kill-buffer buf))))
  10324. (defun org-prepare-agenda-buffers (files)
  10325. "Create buffers for all agenda files, protect archived trees and comments."
  10326. (interactive)
  10327. (let ((pa '(:org-archived t))
  10328. (pc '(:org-comment t))
  10329. (pall '(:org-archived t :org-comment t))
  10330. (inhibit-read-only t)
  10331. (rea (concat ":" org-archive-tag ":"))
  10332. bmp file re)
  10333. (save-excursion
  10334. (save-restriction
  10335. (while (setq file (pop files))
  10336. (if (bufferp file)
  10337. (set-buffer file)
  10338. (org-check-agenda-file file)
  10339. (set-buffer (org-get-agenda-file-buffer file)))
  10340. (widen)
  10341. (setq bmp (buffer-modified-p))
  10342. (org-refresh-category-properties)
  10343. (setq org-todo-keywords-for-agenda
  10344. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10345. (setq org-done-keywords-for-agenda
  10346. (append org-done-keywords-for-agenda org-done-keywords))
  10347. (save-excursion
  10348. (remove-text-properties (point-min) (point-max) pall)
  10349. (when org-agenda-skip-archived-trees
  10350. (goto-char (point-min))
  10351. (while (re-search-forward rea nil t)
  10352. (if (org-on-heading-p t)
  10353. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10354. (goto-char (point-min))
  10355. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10356. (while (re-search-forward re nil t)
  10357. (add-text-properties
  10358. (match-beginning 0) (org-end-of-subtree t) pc)))
  10359. (set-buffer-modified-p bmp))))))
  10360. ;;;; Embedded LaTeX
  10361. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10362. "Keymap for the minor `org-cdlatex-mode'.")
  10363. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10364. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10365. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10366. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10367. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10368. (defvar org-cdlatex-texmathp-advice-is-done nil
  10369. "Flag remembering if we have applied the advice to texmathp already.")
  10370. (define-minor-mode org-cdlatex-mode
  10371. "Toggle the minor `org-cdlatex-mode'.
  10372. This mode supports entering LaTeX environment and math in LaTeX fragments
  10373. in Org-mode.
  10374. \\{org-cdlatex-mode-map}"
  10375. nil " OCDL" nil
  10376. (when org-cdlatex-mode (require 'cdlatex))
  10377. (unless org-cdlatex-texmathp-advice-is-done
  10378. (setq org-cdlatex-texmathp-advice-is-done t)
  10379. (defadvice texmathp (around org-math-always-on activate)
  10380. "Always return t in org-mode buffers.
  10381. This is because we want to insert math symbols without dollars even outside
  10382. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10383. en embedded LaTeX fragement, let texmathp do its job.
  10384. \\[org-cdlatex-mode-map]"
  10385. (interactive)
  10386. (let (p)
  10387. (cond
  10388. ((not (org-mode-p)) ad-do-it)
  10389. ((eq this-command 'cdlatex-math-symbol)
  10390. (setq ad-return-value t
  10391. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10392. (t
  10393. (let ((p (org-inside-LaTeX-fragment-p)))
  10394. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10395. (setq ad-return-value t
  10396. texmathp-why '("Org-mode embedded math" . 0))
  10397. (if p ad-do-it)))))))))
  10398. (defun turn-on-org-cdlatex ()
  10399. "Unconditionally turn on `org-cdlatex-mode'."
  10400. (org-cdlatex-mode 1))
  10401. (defun org-inside-LaTeX-fragment-p ()
  10402. "Test if point is inside a LaTeX fragment.
  10403. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10404. sequence appearing also before point.
  10405. Even though the matchers for math are configurable, this function assumes
  10406. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10407. delimiters are skipped when they have been removed by customization.
  10408. The return value is nil, or a cons cell with the delimiter and
  10409. and the position of this delimiter.
  10410. This function does a reasonably good job, but can locally be fooled by
  10411. for example currency specifications. For example it will assume being in
  10412. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10413. fragments that are properly closed, but during editing, we have to live
  10414. with the uncertainty caused by missing closing delimiters. This function
  10415. looks only before point, not after."
  10416. (catch 'exit
  10417. (let ((pos (point))
  10418. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10419. (lim (progn
  10420. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10421. (point)))
  10422. dd-on str (start 0) m re)
  10423. (goto-char pos)
  10424. (when dodollar
  10425. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10426. re (nth 1 (assoc "$" org-latex-regexps)))
  10427. (while (string-match re str start)
  10428. (cond
  10429. ((= (match-end 0) (length str))
  10430. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10431. ((= (match-end 0) (- (length str) 5))
  10432. (throw 'exit nil))
  10433. (t (setq start (match-end 0))))))
  10434. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10435. (goto-char pos)
  10436. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10437. (and (match-beginning 2) (throw 'exit nil))
  10438. ;; count $$
  10439. (while (re-search-backward "\\$\\$" lim t)
  10440. (setq dd-on (not dd-on)))
  10441. (goto-char pos)
  10442. (if dd-on (cons "$$" m))))))
  10443. (defun org-try-cdlatex-tab ()
  10444. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10445. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10446. - inside a LaTeX fragment, or
  10447. - after the first word in a line, where an abbreviation expansion could
  10448. insert a LaTeX environment."
  10449. (when org-cdlatex-mode
  10450. (cond
  10451. ((save-excursion
  10452. (skip-chars-backward "a-zA-Z0-9*")
  10453. (skip-chars-backward " \t")
  10454. (bolp))
  10455. (cdlatex-tab) t)
  10456. ((org-inside-LaTeX-fragment-p)
  10457. (cdlatex-tab) t)
  10458. (t nil))))
  10459. (defun org-cdlatex-underscore-caret (&optional arg)
  10460. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10461. Revert to the normal definition outside of these fragments."
  10462. (interactive "P")
  10463. (if (org-inside-LaTeX-fragment-p)
  10464. (call-interactively 'cdlatex-sub-superscript)
  10465. (let (org-cdlatex-mode)
  10466. (call-interactively (key-binding (vector last-input-event))))))
  10467. (defun org-cdlatex-math-modify (&optional arg)
  10468. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10469. Revert to the normal definition outside of these fragments."
  10470. (interactive "P")
  10471. (if (org-inside-LaTeX-fragment-p)
  10472. (call-interactively 'cdlatex-math-modify)
  10473. (let (org-cdlatex-mode)
  10474. (call-interactively (key-binding (vector last-input-event))))))
  10475. (defvar org-latex-fragment-image-overlays nil
  10476. "List of overlays carrying the images of latex fragments.")
  10477. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10478. (defun org-remove-latex-fragment-image-overlays ()
  10479. "Remove all overlays with LaTeX fragment images in current buffer."
  10480. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10481. (setq org-latex-fragment-image-overlays nil))
  10482. (defun org-preview-latex-fragment (&optional subtree)
  10483. "Preview the LaTeX fragment at point, or all locally or globally.
  10484. If the cursor is in a LaTeX fragment, create the image and overlay
  10485. it over the source code. If there is no fragment at point, display
  10486. all fragments in the current text, from one headline to the next. With
  10487. prefix SUBTREE, display all fragments in the current subtree. With a
  10488. double prefix `C-u C-u', or when the cursor is before the first headline,
  10489. display all fragments in the buffer.
  10490. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10491. (interactive "P")
  10492. (org-remove-latex-fragment-image-overlays)
  10493. (save-excursion
  10494. (save-restriction
  10495. (let (beg end at msg)
  10496. (cond
  10497. ((or (equal subtree '(16))
  10498. (not (save-excursion
  10499. (re-search-backward (concat "^" outline-regexp) nil t))))
  10500. (setq beg (point-min) end (point-max)
  10501. msg "Creating images for buffer...%s"))
  10502. ((equal subtree '(4))
  10503. (org-back-to-heading)
  10504. (setq beg (point) end (org-end-of-subtree t)
  10505. msg "Creating images for subtree...%s"))
  10506. (t
  10507. (if (setq at (org-inside-LaTeX-fragment-p))
  10508. (goto-char (max (point-min) (- (cdr at) 2)))
  10509. (org-back-to-heading))
  10510. (setq beg (point) end (progn (outline-next-heading) (point))
  10511. msg (if at "Creating image...%s"
  10512. "Creating images for entry...%s"))))
  10513. (message msg "")
  10514. (narrow-to-region beg end)
  10515. (goto-char beg)
  10516. (org-format-latex
  10517. (concat "ltxpng/" (file-name-sans-extension
  10518. (file-name-nondirectory
  10519. buffer-file-name)))
  10520. default-directory 'overlays msg at 'forbuffer)
  10521. (message msg "done. Use `C-c C-c' to remove images.")))))
  10522. (defvar org-latex-regexps
  10523. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10524. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10525. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10526. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10527. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10528. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10529. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10530. "Regular expressions for matching embedded LaTeX.")
  10531. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10532. "Replace LaTeX fragments with links to an image, and produce images."
  10533. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10534. (let* ((prefixnodir (file-name-nondirectory prefix))
  10535. (absprefix (expand-file-name prefix dir))
  10536. (todir (file-name-directory absprefix))
  10537. (opt org-format-latex-options)
  10538. (matchers (plist-get opt :matchers))
  10539. (re-list org-latex-regexps)
  10540. (cnt 0) txt link beg end re e checkdir
  10541. m n block linkfile movefile ov)
  10542. ;; Check if there are old images files with this prefix, and remove them
  10543. (when (file-directory-p todir)
  10544. (mapc 'delete-file
  10545. (directory-files
  10546. todir 'full
  10547. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10548. ;; Check the different regular expressions
  10549. (while (setq e (pop re-list))
  10550. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10551. block (if (nth 3 e) "\n\n" ""))
  10552. (when (member m matchers)
  10553. (goto-char (point-min))
  10554. (while (re-search-forward re nil t)
  10555. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10556. (setq txt (match-string n)
  10557. beg (match-beginning n) end (match-end n)
  10558. cnt (1+ cnt)
  10559. linkfile (format "%s_%04d.png" prefix cnt)
  10560. movefile (format "%s_%04d.png" absprefix cnt)
  10561. link (concat block "[[file:" linkfile "]]" block))
  10562. (if msg (message msg cnt))
  10563. (goto-char beg)
  10564. (unless checkdir ; make sure the directory exists
  10565. (setq checkdir t)
  10566. (or (file-directory-p todir) (make-directory todir)))
  10567. (org-create-formula-image
  10568. txt movefile opt forbuffer)
  10569. (if overlays
  10570. (progn
  10571. (setq ov (org-make-overlay beg end))
  10572. (if (featurep 'xemacs)
  10573. (progn
  10574. (org-overlay-put ov 'invisible t)
  10575. (org-overlay-put
  10576. ov 'end-glyph
  10577. (make-glyph (vector 'png :file movefile))))
  10578. (org-overlay-put
  10579. ov 'display
  10580. (list 'image :type 'png :file movefile :ascent 'center)))
  10581. (push ov org-latex-fragment-image-overlays)
  10582. (goto-char end))
  10583. (delete-region beg end)
  10584. (insert link))))))))
  10585. ;; This function borrows from Ganesh Swami's latex2png.el
  10586. (defun org-create-formula-image (string tofile options buffer)
  10587. (let* ((tmpdir (if (featurep 'xemacs)
  10588. (temp-directory)
  10589. temporary-file-directory))
  10590. (texfilebase (make-temp-name
  10591. (expand-file-name "orgtex" tmpdir)))
  10592. (texfile (concat texfilebase ".tex"))
  10593. (dvifile (concat texfilebase ".dvi"))
  10594. (pngfile (concat texfilebase ".png"))
  10595. (fnh (if (featurep 'xemacs)
  10596. (font-height (get-face-font 'default))
  10597. (face-attribute 'default :height nil)))
  10598. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10599. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10600. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10601. "Black"))
  10602. (bg (or (plist-get options (if buffer :background :html-background))
  10603. "Transparent")))
  10604. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10605. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10606. (with-temp-file texfile
  10607. (insert org-format-latex-header
  10608. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10609. (let ((dir default-directory))
  10610. (condition-case nil
  10611. (progn
  10612. (cd tmpdir)
  10613. (call-process "latex" nil nil nil texfile))
  10614. (error nil))
  10615. (cd dir))
  10616. (if (not (file-exists-p dvifile))
  10617. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10618. (call-process "dvipng" nil nil nil
  10619. "-E" "-fg" fg "-bg" bg
  10620. "-D" dpi
  10621. ;;"-x" scale "-y" scale
  10622. "-T" "tight"
  10623. "-o" pngfile
  10624. dvifile)
  10625. (if (not (file-exists-p pngfile))
  10626. (progn (message "Failed to create png file from %s" texfile) nil)
  10627. ;; Use the requested file name and clean up
  10628. (copy-file pngfile tofile 'replace)
  10629. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10630. (delete-file (concat texfilebase e)))
  10631. pngfile))))
  10632. (defun org-dvipng-color (attr)
  10633. "Return an rgb color specification for dvipng."
  10634. (apply 'format "rgb %s %s %s"
  10635. (mapcar 'org-normalize-color
  10636. (color-values (face-attribute 'default attr nil)))))
  10637. (defun org-normalize-color (value)
  10638. "Return string to be used as color value for an RGB component."
  10639. (format "%g" (/ value 65535.0)))
  10640. ;;;; Key bindings
  10641. ;; Make `C-c C-x' a prefix key
  10642. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10643. ;; TAB key with modifiers
  10644. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10645. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10646. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10647. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10648. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10649. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10650. ;; The following line is necessary under Suse GNU/Linux
  10651. (unless (featurep 'xemacs)
  10652. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10653. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10654. (define-key org-mode-map [backtab] 'org-shifttab)
  10655. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10656. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10657. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10658. ;; Cursor keys with modifiers
  10659. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10660. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10661. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10662. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10663. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10664. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10665. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10666. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10667. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10668. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10669. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10670. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10671. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10672. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10673. ;;; Extra keys for tty access.
  10674. ;; We only set them when really needed because otherwise the
  10675. ;; menus don't show the simple keys
  10676. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10677. (not window-system))
  10678. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10679. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10680. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10681. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10682. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10683. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10684. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10685. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10686. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10687. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10688. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10689. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10690. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10691. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10692. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10693. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10694. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10695. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10696. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10697. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10698. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10699. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10700. ;; All the other keys
  10701. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10702. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10703. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10704. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10705. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10706. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10707. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10708. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10709. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10710. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10711. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10712. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10713. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10714. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10715. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10716. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10717. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10718. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10719. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10720. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10721. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10722. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10723. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10724. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10725. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10726. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10727. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10728. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10729. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10730. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10731. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10732. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10733. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10734. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10735. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10736. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10737. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10738. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10739. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10740. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10741. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10742. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10743. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10744. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10745. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10746. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10747. (org-defkey org-mode-map "\C-m" 'org-return)
  10748. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10749. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10750. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  10751. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  10752. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  10753. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  10754. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  10755. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  10756. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  10757. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  10758. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  10759. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  10760. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  10761. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  10762. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  10763. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  10764. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  10765. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  10766. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  10767. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  10768. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  10769. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  10770. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  10771. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  10772. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  10773. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  10774. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  10775. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  10776. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  10777. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  10778. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  10779. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  10780. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  10781. (when (featurep 'xemacs)
  10782. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  10783. (defvar org-table-auto-blank-field) ; defined in org-table.el
  10784. (defun org-self-insert-command (N)
  10785. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10786. If the cursor is in a table looking at whitespace, the whitespace is
  10787. overwritten, and the table is not marked as requiring realignment."
  10788. (interactive "p")
  10789. (if (and (org-table-p)
  10790. (progn
  10791. ;; check if we blank the field, and if that triggers align
  10792. (and (featurep 'org-table) org-table-auto-blank-field
  10793. (member last-command
  10794. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  10795. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  10796. ;; got extra space, this field does not determine column width
  10797. (let (org-table-may-need-update) (org-table-blank-field))
  10798. ;; no extra space, this field may determine column width
  10799. (org-table-blank-field)))
  10800. t)
  10801. (eq N 1)
  10802. (looking-at "[^|\n]* |"))
  10803. (let (org-table-may-need-update)
  10804. (goto-char (1- (match-end 0)))
  10805. (delete-backward-char 1)
  10806. (goto-char (match-beginning 0))
  10807. (self-insert-command N))
  10808. (setq org-table-may-need-update t)
  10809. (self-insert-command N)
  10810. (org-fix-tags-on-the-fly)))
  10811. (defun org-fix-tags-on-the-fly ()
  10812. (when (and (equal (char-after (point-at-bol)) ?*)
  10813. (org-on-heading-p))
  10814. (org-align-tags-here org-tags-column)))
  10815. (defun org-delete-backward-char (N)
  10816. "Like `delete-backward-char', insert whitespace at field end in tables.
  10817. When deleting backwards, in tables this function will insert whitespace in
  10818. front of the next \"|\" separator, to keep the table aligned. The table will
  10819. still be marked for re-alignment if the field did fill the entire column,
  10820. because, in this case the deletion might narrow the column."
  10821. (interactive "p")
  10822. (if (and (org-table-p)
  10823. (eq N 1)
  10824. (string-match "|" (buffer-substring (point-at-bol) (point)))
  10825. (looking-at ".*?|"))
  10826. (let ((pos (point))
  10827. (noalign (looking-at "[^|\n\r]* |"))
  10828. (c org-table-may-need-update))
  10829. (backward-delete-char N)
  10830. (skip-chars-forward "^|")
  10831. (insert " ")
  10832. (goto-char (1- pos))
  10833. ;; noalign: if there were two spaces at the end, this field
  10834. ;; does not determine the width of the column.
  10835. (if noalign (setq org-table-may-need-update c)))
  10836. (backward-delete-char N)
  10837. (org-fix-tags-on-the-fly)))
  10838. (defun org-delete-char (N)
  10839. "Like `delete-char', but insert whitespace at field end in tables.
  10840. When deleting characters, in tables this function will insert whitespace in
  10841. front of the next \"|\" separator, to keep the table aligned. The table will
  10842. still be marked for re-alignment if the field did fill the entire column,
  10843. because, in this case the deletion might narrow the column."
  10844. (interactive "p")
  10845. (if (and (org-table-p)
  10846. (not (bolp))
  10847. (not (= (char-after) ?|))
  10848. (eq N 1))
  10849. (if (looking-at ".*?|")
  10850. (let ((pos (point))
  10851. (noalign (looking-at "[^|\n\r]* |"))
  10852. (c org-table-may-need-update))
  10853. (replace-match (concat
  10854. (substring (match-string 0) 1 -1)
  10855. " |"))
  10856. (goto-char pos)
  10857. ;; noalign: if there were two spaces at the end, this field
  10858. ;; does not determine the width of the column.
  10859. (if noalign (setq org-table-may-need-update c)))
  10860. (delete-char N))
  10861. (delete-char N)
  10862. (org-fix-tags-on-the-fly)))
  10863. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  10864. (put 'org-self-insert-command 'delete-selection t)
  10865. (put 'orgtbl-self-insert-command 'delete-selection t)
  10866. (put 'org-delete-char 'delete-selection 'supersede)
  10867. (put 'org-delete-backward-char 'delete-selection 'supersede)
  10868. ;; Make `flyspell-mode' delay after some commands
  10869. (put 'org-self-insert-command 'flyspell-delayed t)
  10870. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  10871. (put 'org-delete-char 'flyspell-delayed t)
  10872. (put 'org-delete-backward-char 'flyspell-delayed t)
  10873. ;; Make pabbrev-mode expand after org-mode commands
  10874. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  10875. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  10876. ;; How to do this: Measure non-white length of current string
  10877. ;; If equal to column width, we should realign.
  10878. (defun org-remap (map &rest commands)
  10879. "In MAP, remap the functions given in COMMANDS.
  10880. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  10881. (let (new old)
  10882. (while commands
  10883. (setq old (pop commands) new (pop commands))
  10884. (if (fboundp 'command-remapping)
  10885. (org-defkey map (vector 'remap old) new)
  10886. (substitute-key-definition old new map global-map)))))
  10887. (when (eq org-enable-table-editor 'optimized)
  10888. ;; If the user wants maximum table support, we need to hijack
  10889. ;; some standard editing functions
  10890. (org-remap org-mode-map
  10891. 'self-insert-command 'org-self-insert-command
  10892. 'delete-char 'org-delete-char
  10893. 'delete-backward-char 'org-delete-backward-char)
  10894. (org-defkey org-mode-map "|" 'org-force-self-insert))
  10895. (defun org-shiftcursor-error ()
  10896. "Throw an error because Shift-Cursor command was applied in wrong context."
  10897. (error "This command is active in special context like tables, headlines or timestamps"))
  10898. (defun org-shifttab (&optional arg)
  10899. "Global visibility cycling or move to previous table field.
  10900. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  10901. on context.
  10902. See the individual commands for more information."
  10903. (interactive "P")
  10904. (cond
  10905. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  10906. (arg (message "Content view to level: ")
  10907. (org-content (prefix-numeric-value arg))
  10908. (setq org-cycle-global-status 'overview))
  10909. (t (call-interactively 'org-global-cycle))))
  10910. (defun org-shiftmetaleft ()
  10911. "Promote subtree or delete table column.
  10912. Calls `org-promote-subtree', `org-outdent-item',
  10913. or `org-table-delete-column', depending on context.
  10914. See the individual commands for more information."
  10915. (interactive)
  10916. (cond
  10917. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  10918. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  10919. ((org-at-item-p) (call-interactively 'org-outdent-item))
  10920. (t (org-shiftcursor-error))))
  10921. (defun org-shiftmetaright ()
  10922. "Demote subtree or insert table column.
  10923. Calls `org-demote-subtree', `org-indent-item',
  10924. or `org-table-insert-column', depending on context.
  10925. See the individual commands for more information."
  10926. (interactive)
  10927. (cond
  10928. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  10929. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  10930. ((org-at-item-p) (call-interactively 'org-indent-item))
  10931. (t (org-shiftcursor-error))))
  10932. (defun org-shiftmetaup (&optional arg)
  10933. "Move subtree up or kill table row.
  10934. Calls `org-move-subtree-up' or `org-table-kill-row' or
  10935. `org-move-item-up' depending on context. See the individual commands
  10936. for more information."
  10937. (interactive "P")
  10938. (cond
  10939. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  10940. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  10941. ((org-at-item-p) (call-interactively 'org-move-item-up))
  10942. (t (org-shiftcursor-error))))
  10943. (defun org-shiftmetadown (&optional arg)
  10944. "Move subtree down or insert table row.
  10945. Calls `org-move-subtree-down' or `org-table-insert-row' or
  10946. `org-move-item-down', depending on context. See the individual
  10947. commands for more information."
  10948. (interactive "P")
  10949. (cond
  10950. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  10951. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  10952. ((org-at-item-p) (call-interactively 'org-move-item-down))
  10953. (t (org-shiftcursor-error))))
  10954. (defun org-metaleft (&optional arg)
  10955. "Promote heading or move table column to left.
  10956. Calls `org-do-promote' or `org-table-move-column', depending on context.
  10957. With no specific context, calls the Emacs default `backward-word'.
  10958. See the individual commands for more information."
  10959. (interactive "P")
  10960. (cond
  10961. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  10962. ((or (org-on-heading-p) (org-region-active-p))
  10963. (call-interactively 'org-do-promote))
  10964. ((org-at-item-p) (call-interactively 'org-outdent-item))
  10965. (t (call-interactively 'backward-word))))
  10966. (defun org-metaright (&optional arg)
  10967. "Demote subtree or move table column to right.
  10968. Calls `org-do-demote' or `org-table-move-column', depending on context.
  10969. With no specific context, calls the Emacs default `forward-word'.
  10970. See the individual commands for more information."
  10971. (interactive "P")
  10972. (cond
  10973. ((org-at-table-p) (call-interactively 'org-table-move-column))
  10974. ((or (org-on-heading-p) (org-region-active-p))
  10975. (call-interactively 'org-do-demote))
  10976. ((org-at-item-p) (call-interactively 'org-indent-item))
  10977. (t (call-interactively 'forward-word))))
  10978. (defun org-metaup (&optional arg)
  10979. "Move subtree up or move table row up.
  10980. Calls `org-move-subtree-up' or `org-table-move-row' or
  10981. `org-move-item-up', depending on context. See the individual commands
  10982. for more information."
  10983. (interactive "P")
  10984. (cond
  10985. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  10986. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  10987. ((org-at-item-p) (call-interactively 'org-move-item-up))
  10988. (t (transpose-lines 1) (beginning-of-line -1))))
  10989. (defun org-metadown (&optional arg)
  10990. "Move subtree down or move table row down.
  10991. Calls `org-move-subtree-down' or `org-table-move-row' or
  10992. `org-move-item-down', depending on context. See the individual
  10993. commands for more information."
  10994. (interactive "P")
  10995. (cond
  10996. ((org-at-table-p) (call-interactively 'org-table-move-row))
  10997. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  10998. ((org-at-item-p) (call-interactively 'org-move-item-down))
  10999. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11000. (defun org-shiftup (&optional arg)
  11001. "Increase item in timestamp or increase priority of current headline.
  11002. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11003. depending on context. See the individual commands for more information."
  11004. (interactive "P")
  11005. (cond
  11006. ((org-at-timestamp-p t)
  11007. (call-interactively (if org-edit-timestamp-down-means-later
  11008. 'org-timestamp-down 'org-timestamp-up)))
  11009. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11010. ((org-at-item-p) (call-interactively 'org-previous-item))
  11011. ((org-clocktable-try-shift 'up arg))
  11012. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11013. (defun org-shiftdown (&optional arg)
  11014. "Decrease item in timestamp or decrease priority of current headline.
  11015. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11016. depending on context. See the individual commands for more information."
  11017. (interactive "P")
  11018. (cond
  11019. ((org-at-timestamp-p t)
  11020. (call-interactively (if org-edit-timestamp-down-means-later
  11021. 'org-timestamp-up 'org-timestamp-down)))
  11022. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11023. ((org-clocktable-try-shift 'down arg))
  11024. (t (call-interactively 'org-next-item))))
  11025. (defun org-shiftright (&optional arg)
  11026. "Next TODO keyword or timestamp one day later, depending on context."
  11027. (interactive "P")
  11028. (cond
  11029. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11030. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11031. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11032. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11033. ((org-clocktable-try-shift 'right arg))
  11034. (t (org-shiftcursor-error))))
  11035. (defun org-shiftleft (&optional arg)
  11036. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11037. (interactive "P")
  11038. (cond
  11039. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11040. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11041. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11042. ((org-at-property-p)
  11043. (call-interactively 'org-property-previous-allowed-value))
  11044. ((org-clocktable-try-shift 'left arg))
  11045. (t (org-shiftcursor-error))))
  11046. (defun org-shiftcontrolright ()
  11047. "Switch to next TODO set."
  11048. (interactive)
  11049. (cond
  11050. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11051. (t (org-shiftcursor-error))))
  11052. (defun org-shiftcontrolleft ()
  11053. "Switch to previous TODO set."
  11054. (interactive)
  11055. (cond
  11056. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11057. (t (org-shiftcursor-error))))
  11058. (defun org-ctrl-c-ret ()
  11059. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11060. (interactive)
  11061. (cond
  11062. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11063. (t (call-interactively 'org-insert-heading))))
  11064. (defun org-copy-special ()
  11065. "Copy region in table or copy current subtree.
  11066. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11067. See the individual commands for more information."
  11068. (interactive)
  11069. (call-interactively
  11070. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11071. (defun org-cut-special ()
  11072. "Cut region in table or cut current subtree.
  11073. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11074. See the individual commands for more information."
  11075. (interactive)
  11076. (call-interactively
  11077. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11078. (defun org-paste-special (arg)
  11079. "Paste rectangular region into table, or past subtree relative to level.
  11080. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11081. See the individual commands for more information."
  11082. (interactive "P")
  11083. (if (org-at-table-p)
  11084. (org-table-paste-rectangle)
  11085. (org-paste-subtree arg)))
  11086. (defun org-ctrl-c-ctrl-c (&optional arg)
  11087. "Set tags in headline, or update according to changed information at point.
  11088. This command does many different things, depending on context:
  11089. - If the cursor is in a headline, prompt for tags and insert them
  11090. into the current line, aligned to `org-tags-column'. When called
  11091. with prefix arg, realign all tags in the current buffer.
  11092. - If the cursor is in one of the special #+KEYWORD lines, this
  11093. triggers scanning the buffer for these lines and updating the
  11094. information.
  11095. - If the cursor is inside a table, realign the table. This command
  11096. works even if the automatic table editor has been turned off.
  11097. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11098. the entire table.
  11099. - If the cursor is a the beginning of a dynamic block, update it.
  11100. - If the cursor is inside a table created by the table.el package,
  11101. activate that table.
  11102. - If the current buffer is a remember buffer, close note and file it.
  11103. with a prefix argument, file it without further interaction to the default
  11104. location.
  11105. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11106. links in this buffer.
  11107. - If the cursor is on a numbered item in a plain list, renumber the
  11108. ordered list.
  11109. - If the cursor is on a checkbox, toggle it."
  11110. (interactive "P")
  11111. (let ((org-enable-table-editor t))
  11112. (cond
  11113. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11114. org-occur-highlights
  11115. org-latex-fragment-image-overlays)
  11116. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11117. (org-remove-occur-highlights)
  11118. (org-remove-latex-fragment-image-overlays)
  11119. (message "Temporary highlights/overlays removed from current buffer"))
  11120. ((and (local-variable-p 'org-finish-function (current-buffer))
  11121. (fboundp org-finish-function))
  11122. (funcall org-finish-function))
  11123. ((org-at-property-p)
  11124. (call-interactively 'org-property-action))
  11125. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11126. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11127. ((org-at-table.el-p)
  11128. (require 'table)
  11129. (beginning-of-line 1)
  11130. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11131. (call-interactively 'table-recognize-table))
  11132. ((org-at-table-p)
  11133. (org-table-maybe-eval-formula)
  11134. (if arg
  11135. (call-interactively 'org-table-recalculate)
  11136. (org-table-maybe-recalculate-line))
  11137. (call-interactively 'org-table-align))
  11138. ((org-at-item-checkbox-p)
  11139. (call-interactively 'org-toggle-checkbox))
  11140. ((org-at-item-p)
  11141. (call-interactively 'org-maybe-renumber-ordered-list))
  11142. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11143. ;; Dynamic block
  11144. (beginning-of-line 1)
  11145. (org-update-dblock))
  11146. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11147. (cond
  11148. ((equal (match-string 1) "TBLFM")
  11149. ;; Recalculate the table before this line
  11150. (save-excursion
  11151. (beginning-of-line 1)
  11152. (skip-chars-backward " \r\n\t")
  11153. (if (org-at-table-p)
  11154. (org-call-with-arg 'org-table-recalculate t))))
  11155. (t
  11156. (call-interactively 'org-mode-restart))))
  11157. (t (error "C-c C-c can do nothing useful at this location.")))))
  11158. (defun org-mode-restart ()
  11159. "Restart Org-mode, to scan again for special lines.
  11160. Also updates the keyword regular expressions."
  11161. (interactive)
  11162. (let ((org-inhibit-startup t)) (org-mode))
  11163. (message "Org-mode restarted to refresh keyword and special line setup"))
  11164. (defun org-kill-note-or-show-branches ()
  11165. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11166. (interactive)
  11167. (if (not org-finish-function)
  11168. (call-interactively 'show-branches)
  11169. (let ((org-note-abort t))
  11170. (funcall org-finish-function))))
  11171. (defun org-return (&optional indent)
  11172. "Goto next table row or insert a newline.
  11173. Calls `org-table-next-row' or `newline', depending on context.
  11174. See the individual commands for more information."
  11175. (interactive)
  11176. (cond
  11177. ((bobp) (if indent (newline-and-indent) (newline)))
  11178. ((and (org-at-heading-p)
  11179. (looking-at
  11180. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11181. (org-show-entry)
  11182. (end-of-line 1)
  11183. (newline))
  11184. ((org-at-table-p)
  11185. (org-table-justify-field-maybe)
  11186. (call-interactively 'org-table-next-row))
  11187. (t (if indent (newline-and-indent) (newline)))))
  11188. (defun org-return-indent ()
  11189. "Goto next table row or insert a newline and indent.
  11190. Calls `org-table-next-row' or `newline-and-indent', depending on
  11191. context. See the individual commands for more information."
  11192. (interactive)
  11193. (org-return t))
  11194. (defun org-ctrl-c-star ()
  11195. "Compute table, or change heading status of lines.
  11196. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11197. depending on context. This will also turn a plain list item or a normal
  11198. line into a subheading."
  11199. (interactive)
  11200. (cond
  11201. ((org-at-table-p)
  11202. (call-interactively 'org-table-recalculate))
  11203. ((org-region-active-p)
  11204. ;; Convert all lines in region to list items
  11205. (call-interactively 'org-toggle-region-headings))
  11206. ((org-on-heading-p)
  11207. (org-toggle-region-headings (point-at-bol)
  11208. (min (1+ (point-at-eol)) (point-max))))
  11209. ((org-at-item-p)
  11210. ;; Convert to heading
  11211. (let ((level (save-match-data
  11212. (save-excursion
  11213. (condition-case nil
  11214. (progn
  11215. (org-back-to-heading t)
  11216. (funcall outline-level))
  11217. (error 0))))))
  11218. (replace-match
  11219. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11220. (t (org-toggle-region-headings (point-at-bol)
  11221. (min (1+ (point-at-eol)) (point-max))))))
  11222. (defun org-ctrl-c-minus ()
  11223. "Insert separator line in table or modify bullet status of line.
  11224. Also turns a plain line or a region of lines into list items.
  11225. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11226. `org-cycle-list-bullet', depending on context."
  11227. (interactive)
  11228. (cond
  11229. ((org-at-table-p)
  11230. (call-interactively 'org-table-insert-hline))
  11231. ((org-on-heading-p)
  11232. ;; Convert to item
  11233. (save-excursion
  11234. (beginning-of-line 1)
  11235. (if (looking-at "\\*+ ")
  11236. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11237. ((org-region-active-p)
  11238. ;; Convert all lines in region to list items
  11239. (call-interactively 'org-toggle-region-items))
  11240. ((org-in-item-p)
  11241. (call-interactively 'org-cycle-list-bullet))
  11242. (t (org-toggle-region-items (point-at-bol)
  11243. (min (1+ (point-at-eol)) (point-max))))))
  11244. (defun org-toggle-region-items (beg end)
  11245. "Convert all lines in region to list items.
  11246. If the first line is already an item, convert all list items in the region
  11247. to normal lines."
  11248. (interactive "r")
  11249. (let (l2 l)
  11250. (save-excursion
  11251. (goto-char end)
  11252. (setq l2 (org-current-line))
  11253. (goto-char beg)
  11254. (beginning-of-line 1)
  11255. (setq l (1- (org-current-line)))
  11256. (if (org-at-item-p)
  11257. ;; We already have items, de-itemize
  11258. (while (< (setq l (1+ l)) l2)
  11259. (when (org-at-item-p)
  11260. (goto-char (match-beginning 2))
  11261. (delete-region (match-beginning 2) (match-end 2))
  11262. (and (looking-at "[ \t]+") (replace-match "")))
  11263. (beginning-of-line 2))
  11264. (while (< (setq l (1+ l)) l2)
  11265. (unless (org-at-item-p)
  11266. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11267. (replace-match "\\1- \\2")))
  11268. (beginning-of-line 2))))))
  11269. (defun org-toggle-region-headings (beg end)
  11270. "Convert all lines in region to list items.
  11271. If the first line is already an item, convert all list items in the region
  11272. to normal lines."
  11273. (interactive "r")
  11274. (let (l2 l)
  11275. (save-excursion
  11276. (goto-char end)
  11277. (setq l2 (org-current-line))
  11278. (goto-char beg)
  11279. (beginning-of-line 1)
  11280. (setq l (1- (org-current-line)))
  11281. (if (org-on-heading-p)
  11282. ;; We already have headlines, de-star them
  11283. (while (< (setq l (1+ l)) l2)
  11284. (when (org-on-heading-p t)
  11285. (and (looking-at outline-regexp) (replace-match "")))
  11286. (beginning-of-line 2))
  11287. (let* ((stars (save-excursion
  11288. (re-search-backward org-complex-heading-regexp nil t)
  11289. (or (match-string 1) "*")))
  11290. (add-stars (if org-odd-levels-only "**" "*"))
  11291. (rpl (concat stars add-stars " \\2")))
  11292. (while (< (setq l (1+ l)) l2)
  11293. (unless (org-on-heading-p)
  11294. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11295. (replace-match rpl)))
  11296. (beginning-of-line 2)))))))
  11297. (defun org-meta-return (&optional arg)
  11298. "Insert a new heading or wrap a region in a table.
  11299. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11300. See the individual commands for more information."
  11301. (interactive "P")
  11302. (cond
  11303. ((org-at-table-p)
  11304. (call-interactively 'org-table-wrap-region))
  11305. (t (call-interactively 'org-insert-heading))))
  11306. ;;; Menu entries
  11307. ;; Define the Org-mode menus
  11308. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11309. '("Tbl"
  11310. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11311. ["Next Field" org-cycle (org-at-table-p)]
  11312. ["Previous Field" org-shifttab (org-at-table-p)]
  11313. ["Next Row" org-return (org-at-table-p)]
  11314. "--"
  11315. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11316. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11317. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11318. "--"
  11319. ("Column"
  11320. ["Move Column Left" org-metaleft (org-at-table-p)]
  11321. ["Move Column Right" org-metaright (org-at-table-p)]
  11322. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11323. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11324. ("Row"
  11325. ["Move Row Up" org-metaup (org-at-table-p)]
  11326. ["Move Row Down" org-metadown (org-at-table-p)]
  11327. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11328. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11329. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11330. "--"
  11331. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11332. ("Rectangle"
  11333. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11334. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11335. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11336. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11337. "--"
  11338. ("Calculate"
  11339. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11340. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11341. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11342. "--"
  11343. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11344. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11345. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11346. "--"
  11347. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11348. "--"
  11349. ["Sum Column/Rectangle" org-table-sum
  11350. (or (org-at-table-p) (org-region-active-p))]
  11351. ["Which Column?" org-table-current-column (org-at-table-p)])
  11352. ["Debug Formulas"
  11353. org-table-toggle-formula-debugger
  11354. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11355. ["Show Col/Row Numbers"
  11356. org-table-toggle-coordinate-overlays
  11357. :style toggle
  11358. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11359. "--"
  11360. ["Create" org-table-create (and (not (org-at-table-p))
  11361. org-enable-table-editor)]
  11362. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11363. ["Import from File" org-table-import (not (org-at-table-p))]
  11364. ["Export to File" org-table-export (org-at-table-p)]
  11365. "--"
  11366. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11367. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11368. '("Org"
  11369. ("Show/Hide"
  11370. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11371. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11372. ["Sparse Tree..." org-sparse-tree t]
  11373. ["Reveal Context" org-reveal t]
  11374. ["Show All" show-all t]
  11375. "--"
  11376. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11377. "--"
  11378. ["New Heading" org-insert-heading t]
  11379. ("Navigate Headings"
  11380. ["Up" outline-up-heading t]
  11381. ["Next" outline-next-visible-heading t]
  11382. ["Previous" outline-previous-visible-heading t]
  11383. ["Next Same Level" outline-forward-same-level t]
  11384. ["Previous Same Level" outline-backward-same-level t]
  11385. "--"
  11386. ["Jump" org-goto t])
  11387. ("Edit Structure"
  11388. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11389. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11390. "--"
  11391. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11392. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11393. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11394. "--"
  11395. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11396. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11397. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11398. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11399. "--"
  11400. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11401. "--"
  11402. ["Convert to odd levels" org-convert-to-odd-levels t]
  11403. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11404. ("Editing"
  11405. ["Emphasis..." org-emphasize t])
  11406. ("Archive"
  11407. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11408. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11409. ; :active t :keys "C-u C-c C-x C-a"]
  11410. ["Sparse trees open ARCHIVE trees"
  11411. (setq org-sparse-tree-open-archived-trees
  11412. (not org-sparse-tree-open-archived-trees))
  11413. :style toggle :selected org-sparse-tree-open-archived-trees]
  11414. ["Cycling opens ARCHIVE trees"
  11415. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11416. :style toggle :selected org-cycle-open-archived-trees]
  11417. ["Agenda includes ARCHIVE trees"
  11418. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11419. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11420. "--"
  11421. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11422. ; ["Check and Move Children" (org-archive-subtree '(4))
  11423. ; :active t :keys "C-u C-c C-x C-s"]
  11424. )
  11425. "--"
  11426. ("TODO Lists"
  11427. ["TODO/DONE/-" org-todo t]
  11428. ("Select keyword"
  11429. ["Next keyword" org-shiftright (org-on-heading-p)]
  11430. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11431. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11432. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11433. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11434. ["Show TODO Tree" org-show-todo-tree t]
  11435. ["Global TODO list" org-todo-list t]
  11436. "--"
  11437. ["Set Priority" org-priority t]
  11438. ["Priority Up" org-shiftup t]
  11439. ["Priority Down" org-shiftdown t])
  11440. ("TAGS and Properties"
  11441. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11442. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11443. "--"
  11444. ["Set property" 'org-set-property t]
  11445. ["Column view of properties" org-columns t]
  11446. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11447. ("Dates and Scheduling"
  11448. ["Timestamp" org-time-stamp t]
  11449. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11450. ("Change Date"
  11451. ["1 Day Later" org-shiftright t]
  11452. ["1 Day Earlier" org-shiftleft t]
  11453. ["1 ... Later" org-shiftup t]
  11454. ["1 ... Earlier" org-shiftdown t])
  11455. ["Compute Time Range" org-evaluate-time-range t]
  11456. ["Schedule Item" org-schedule t]
  11457. ["Deadline" org-deadline t]
  11458. "--"
  11459. ["Custom time format" org-toggle-time-stamp-overlays
  11460. :style radio :selected org-display-custom-times]
  11461. "--"
  11462. ["Goto Calendar" org-goto-calendar t]
  11463. ["Date from Calendar" org-date-from-calendar t])
  11464. ("Logging work"
  11465. ["Clock in" org-clock-in t]
  11466. ["Clock out" org-clock-out t]
  11467. ["Clock cancel" org-clock-cancel t]
  11468. ["Goto running clock" org-clock-goto t]
  11469. ["Display times" org-clock-display t]
  11470. ["Create clock table" org-clock-report t]
  11471. "--"
  11472. ["Record DONE time"
  11473. (progn (setq org-log-done (not org-log-done))
  11474. (message "Switching to %s will %s record a timestamp"
  11475. (car org-done-keywords)
  11476. (if org-log-done "automatically" "not")))
  11477. :style toggle :selected org-log-done])
  11478. "--"
  11479. ["Agenda Command..." org-agenda t]
  11480. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11481. ("File List for Agenda")
  11482. ("Special views current file"
  11483. ["TODO Tree" org-show-todo-tree t]
  11484. ["Check Deadlines" org-check-deadlines t]
  11485. ["Timeline" org-timeline t]
  11486. ["Tags Tree" org-tags-sparse-tree t])
  11487. "--"
  11488. ("Hyperlinks"
  11489. ["Store Link (Global)" org-store-link t]
  11490. ["Insert Link" org-insert-link t]
  11491. ["Follow Link" org-open-at-point t]
  11492. "--"
  11493. ["Next link" org-next-link t]
  11494. ["Previous link" org-previous-link t]
  11495. "--"
  11496. ["Descriptive Links"
  11497. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11498. :style radio
  11499. :selected (member '(org-link) buffer-invisibility-spec)]
  11500. ["Literal Links"
  11501. (progn
  11502. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11503. :style radio
  11504. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11505. "--"
  11506. ["Export/Publish..." org-export t]
  11507. ("LaTeX"
  11508. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11509. :selected org-cdlatex-mode]
  11510. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11511. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11512. ["Modify math symbol" org-cdlatex-math-modify
  11513. (org-inside-LaTeX-fragment-p)]
  11514. ["Export LaTeX fragments as images"
  11515. (if (featurep 'org-exp)
  11516. (setq org-export-with-LaTeX-fragments
  11517. (not org-export-with-LaTeX-fragments))
  11518. (require 'org-exp))
  11519. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11520. org-export-with-LaTeX-fragments)])
  11521. "--"
  11522. ("Documentation"
  11523. ["Show Version" org-version t]
  11524. ["Info Documentation" org-info t])
  11525. ("Customize"
  11526. ["Browse Org Group" org-customize t]
  11527. "--"
  11528. ["Expand This Menu" org-create-customize-menu
  11529. (fboundp 'customize-menu-create)])
  11530. "--"
  11531. ["Refresh setup" org-mode-restart t]
  11532. ))
  11533. (defun org-info (&optional node)
  11534. "Read documentation for Org-mode in the info system.
  11535. With optional NODE, go directly to that node."
  11536. (interactive)
  11537. (info (format "(org)%s" (or node ""))))
  11538. (defun org-install-agenda-files-menu ()
  11539. (let ((bl (buffer-list)))
  11540. (save-excursion
  11541. (while bl
  11542. (set-buffer (pop bl))
  11543. (if (org-mode-p) (setq bl nil)))
  11544. (when (org-mode-p)
  11545. (easy-menu-change
  11546. '("Org") "File List for Agenda"
  11547. (append
  11548. (list
  11549. ["Edit File List" (org-edit-agenda-file-list) t]
  11550. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11551. ["Remove Current File from List" org-remove-file t]
  11552. ["Cycle through agenda files" org-cycle-agenda-files t]
  11553. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11554. "--")
  11555. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11556. ;;;; Documentation
  11557. (defun org-require-autoloaded-modules ()
  11558. (interactive)
  11559. (mapc 'require
  11560. '(org-agenda org-archive org-clock org-colview
  11561. org-exp org-export-latex org-publish
  11562. org-remember org-table)))
  11563. (defun org-customize ()
  11564. "Call the customize function with org as argument."
  11565. (interactive)
  11566. (org-load-modules-maybe)
  11567. (org-require-autoloaded-modules)
  11568. (customize-browse 'org))
  11569. (defun org-create-customize-menu ()
  11570. "Create a full customization menu for Org-mode, insert it into the menu."
  11571. (interactive)
  11572. (org-load-modules-maybe)
  11573. (org-require-autoloaded-modules)
  11574. (if (fboundp 'customize-menu-create)
  11575. (progn
  11576. (easy-menu-change
  11577. '("Org") "Customize"
  11578. `(["Browse Org group" org-customize t]
  11579. "--"
  11580. ,(customize-menu-create 'org)
  11581. ["Set" Custom-set t]
  11582. ["Save" Custom-save t]
  11583. ["Reset to Current" Custom-reset-current t]
  11584. ["Reset to Saved" Custom-reset-saved t]
  11585. ["Reset to Standard Settings" Custom-reset-standard t]))
  11586. (message "\"Org\"-menu now contains full customization menu"))
  11587. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11588. ;;;; Miscellaneous stuff
  11589. ;;; Generally useful functions
  11590. (defun org-plist-delete (plist property)
  11591. "Delete PROPERTY from PLIST.
  11592. This is in contrast to merely setting it to 0."
  11593. (let (p)
  11594. (while plist
  11595. (if (not (eq property (car plist)))
  11596. (setq p (plist-put p (car plist) (nth 1 plist))))
  11597. (setq plist (cddr plist)))
  11598. p))
  11599. (defun org-force-self-insert (N)
  11600. "Needed to enforce self-insert under remapping."
  11601. (interactive "p")
  11602. (self-insert-command N))
  11603. (defun org-string-width (s)
  11604. "Compute width of string, ignoring invisible characters.
  11605. This ignores character with invisibility property `org-link', and also
  11606. characters with property `org-cwidth', because these will become invisible
  11607. upon the next fontification round."
  11608. (let (b l)
  11609. (when (or (eq t buffer-invisibility-spec)
  11610. (assq 'org-link buffer-invisibility-spec))
  11611. (while (setq b (text-property-any 0 (length s)
  11612. 'invisible 'org-link s))
  11613. (setq s (concat (substring s 0 b)
  11614. (substring s (or (next-single-property-change
  11615. b 'invisible s) (length s)))))))
  11616. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11617. (setq s (concat (substring s 0 b)
  11618. (substring s (or (next-single-property-change
  11619. b 'org-cwidth s) (length s))))))
  11620. (setq l (string-width s) b -1)
  11621. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11622. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11623. l))
  11624. (defun org-trim (s)
  11625. "Remove whitespace at beginning and end of string."
  11626. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11627. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11628. s)
  11629. (defun org-wrap (string &optional width lines)
  11630. "Wrap string to either a number of lines, or a width in characters.
  11631. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11632. that costs. If there is a word longer than WIDTH, the text is actually
  11633. wrapped to the length of that word.
  11634. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11635. many lines, whatever width that takes.
  11636. The return value is a list of lines, without newlines at the end."
  11637. (let* ((words (org-split-string string "[ \t\n]+"))
  11638. (maxword (apply 'max (mapcar 'org-string-width words)))
  11639. w ll)
  11640. (cond (width
  11641. (org-do-wrap words (max maxword width)))
  11642. (lines
  11643. (setq w maxword)
  11644. (setq ll (org-do-wrap words maxword))
  11645. (if (<= (length ll) lines)
  11646. ll
  11647. (setq ll words)
  11648. (while (> (length ll) lines)
  11649. (setq w (1+ w))
  11650. (setq ll (org-do-wrap words w)))
  11651. ll))
  11652. (t (error "Cannot wrap this")))))
  11653. (defun org-do-wrap (words width)
  11654. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11655. (let (lines line)
  11656. (while words
  11657. (setq line (pop words))
  11658. (while (and words (< (+ (length line) (length (car words))) width))
  11659. (setq line (concat line " " (pop words))))
  11660. (setq lines (push line lines)))
  11661. (nreverse lines)))
  11662. (defun org-split-string (string &optional separators)
  11663. "Splits STRING into substrings at SEPARATORS.
  11664. No empty strings are returned if there are matches at the beginning
  11665. and end of string."
  11666. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11667. (start 0)
  11668. notfirst
  11669. (list nil))
  11670. (while (and (string-match rexp string
  11671. (if (and notfirst
  11672. (= start (match-beginning 0))
  11673. (< start (length string)))
  11674. (1+ start) start))
  11675. (< (match-beginning 0) (length string)))
  11676. (setq notfirst t)
  11677. (or (eq (match-beginning 0) 0)
  11678. (and (eq (match-beginning 0) (match-end 0))
  11679. (eq (match-beginning 0) start))
  11680. (setq list
  11681. (cons (substring string start (match-beginning 0))
  11682. list)))
  11683. (setq start (match-end 0)))
  11684. (or (eq start (length string))
  11685. (setq list
  11686. (cons (substring string start)
  11687. list)))
  11688. (nreverse list)))
  11689. (defun org-context ()
  11690. "Return a list of contexts of the current cursor position.
  11691. If several contexts apply, all are returned.
  11692. Each context entry is a list with a symbol naming the context, and
  11693. two positions indicating start and end of the context. Possible
  11694. contexts are:
  11695. :headline anywhere in a headline
  11696. :headline-stars on the leading stars in a headline
  11697. :todo-keyword on a TODO keyword (including DONE) in a headline
  11698. :tags on the TAGS in a headline
  11699. :priority on the priority cookie in a headline
  11700. :item on the first line of a plain list item
  11701. :item-bullet on the bullet/number of a plain list item
  11702. :checkbox on the checkbox in a plain list item
  11703. :table in an org-mode table
  11704. :table-special on a special filed in a table
  11705. :table-table in a table.el table
  11706. :link on a hyperlink
  11707. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11708. :target on a <<target>>
  11709. :radio-target on a <<<radio-target>>>
  11710. :latex-fragment on a LaTeX fragment
  11711. :latex-preview on a LaTeX fragment with overlayed preview image
  11712. This function expects the position to be visible because it uses font-lock
  11713. faces as a help to recognize the following contexts: :table-special, :link,
  11714. and :keyword."
  11715. (let* ((f (get-text-property (point) 'face))
  11716. (faces (if (listp f) f (list f)))
  11717. (p (point)) clist o)
  11718. ;; First the large context
  11719. (cond
  11720. ((org-on-heading-p t)
  11721. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  11722. (when (progn
  11723. (beginning-of-line 1)
  11724. (looking-at org-todo-line-tags-regexp))
  11725. (push (org-point-in-group p 1 :headline-stars) clist)
  11726. (push (org-point-in-group p 2 :todo-keyword) clist)
  11727. (push (org-point-in-group p 4 :tags) clist))
  11728. (goto-char p)
  11729. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  11730. (if (looking-at "\\[#[A-Z0-9]\\]")
  11731. (push (org-point-in-group p 0 :priority) clist)))
  11732. ((org-at-item-p)
  11733. (push (org-point-in-group p 2 :item-bullet) clist)
  11734. (push (list :item (point-at-bol)
  11735. (save-excursion (org-end-of-item) (point)))
  11736. clist)
  11737. (and (org-at-item-checkbox-p)
  11738. (push (org-point-in-group p 0 :checkbox) clist)))
  11739. ((org-at-table-p)
  11740. (push (list :table (org-table-begin) (org-table-end)) clist)
  11741. (if (memq 'org-formula faces)
  11742. (push (list :table-special
  11743. (previous-single-property-change p 'face)
  11744. (next-single-property-change p 'face)) clist)))
  11745. ((org-at-table-p 'any)
  11746. (push (list :table-table) clist)))
  11747. (goto-char p)
  11748. ;; Now the small context
  11749. (cond
  11750. ((org-at-timestamp-p)
  11751. (push (org-point-in-group p 0 :timestamp) clist))
  11752. ((memq 'org-link faces)
  11753. (push (list :link
  11754. (previous-single-property-change p 'face)
  11755. (next-single-property-change p 'face)) clist))
  11756. ((memq 'org-special-keyword faces)
  11757. (push (list :keyword
  11758. (previous-single-property-change p 'face)
  11759. (next-single-property-change p 'face)) clist))
  11760. ((org-on-target-p)
  11761. (push (org-point-in-group p 0 :target) clist)
  11762. (goto-char (1- (match-beginning 0)))
  11763. (if (looking-at org-radio-target-regexp)
  11764. (push (org-point-in-group p 0 :radio-target) clist))
  11765. (goto-char p))
  11766. ((setq o (car (delq nil
  11767. (mapcar
  11768. (lambda (x)
  11769. (if (memq x org-latex-fragment-image-overlays) x))
  11770. (org-overlays-at (point))))))
  11771. (push (list :latex-fragment
  11772. (org-overlay-start o) (org-overlay-end o)) clist)
  11773. (push (list :latex-preview
  11774. (org-overlay-start o) (org-overlay-end o)) clist))
  11775. ((org-inside-LaTeX-fragment-p)
  11776. ;; FIXME: positions wrong.
  11777. (push (list :latex-fragment (point) (point)) clist)))
  11778. (setq clist (nreverse (delq nil clist)))
  11779. clist))
  11780. ;; FIXME: Compare with at-regexp-p Do we need both?
  11781. (defun org-in-regexp (re &optional nlines visually)
  11782. "Check if point is inside a match of regexp.
  11783. Normally only the current line is checked, but you can include NLINES extra
  11784. lines both before and after point into the search.
  11785. If VISUALLY is set, require that the cursor is not after the match but
  11786. really on, so that the block visually is on the match."
  11787. (catch 'exit
  11788. (let ((pos (point))
  11789. (eol (point-at-eol (+ 1 (or nlines 0))))
  11790. (inc (if visually 1 0)))
  11791. (save-excursion
  11792. (beginning-of-line (- 1 (or nlines 0)))
  11793. (while (re-search-forward re eol t)
  11794. (if (and (<= (match-beginning 0) pos)
  11795. (>= (+ inc (match-end 0)) pos))
  11796. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  11797. (defun org-at-regexp-p (regexp)
  11798. "Is point inside a match of REGEXP in the current line?"
  11799. (catch 'exit
  11800. (save-excursion
  11801. (let ((pos (point)) (end (point-at-eol)))
  11802. (beginning-of-line 1)
  11803. (while (re-search-forward regexp end t)
  11804. (if (and (<= (match-beginning 0) pos)
  11805. (>= (match-end 0) pos))
  11806. (throw 'exit t)))
  11807. nil))))
  11808. (defun org-occur-in-agenda-files (regexp &optional nlines)
  11809. "Call `multi-occur' with buffers for all agenda files."
  11810. (interactive "sOrg-files matching: \np")
  11811. (let* ((files (org-agenda-files))
  11812. (tnames (mapcar 'file-truename files))
  11813. (extra org-agenda-text-search-extra-files)
  11814. f)
  11815. (when (eq (car extra) 'agenda-archives)
  11816. (setq extra (cdr extra))
  11817. (setq files (org-add-archive-files files)))
  11818. (while (setq f (pop extra))
  11819. (unless (member (file-truename f) tnames)
  11820. (add-to-list 'files f 'append)
  11821. (add-to-list 'tnames (file-truename f) 'append)))
  11822. (multi-occur
  11823. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  11824. regexp)))
  11825. (if (boundp 'occur-mode-find-occurrence-hook)
  11826. ;; Emacs 23
  11827. (add-hook 'occur-mode-find-occurrence-hook
  11828. (lambda ()
  11829. (when (org-mode-p)
  11830. (org-reveal))))
  11831. ;; Emacs 22
  11832. (defadvice occur-mode-goto-occurrence
  11833. (after org-occur-reveal activate)
  11834. (and (org-mode-p) (org-reveal)))
  11835. (defadvice occur-mode-goto-occurrence-other-window
  11836. (after org-occur-reveal activate)
  11837. (and (org-mode-p) (org-reveal)))
  11838. (defadvice occur-mode-display-occurrence
  11839. (after org-occur-reveal activate)
  11840. (when (org-mode-p)
  11841. (let ((pos (occur-mode-find-occurrence)))
  11842. (with-current-buffer (marker-buffer pos)
  11843. (save-excursion
  11844. (goto-char pos)
  11845. (org-reveal)))))))
  11846. (defun org-uniquify (list)
  11847. "Remove duplicate elements from LIST."
  11848. (let (res)
  11849. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  11850. res))
  11851. (defun org-delete-all (elts list)
  11852. "Remove all elements in ELTS from LIST."
  11853. (while elts
  11854. (setq list (delete (pop elts) list)))
  11855. list)
  11856. (defun org-back-over-empty-lines ()
  11857. "Move backwards over witespace, to the beginning of the first empty line.
  11858. Returns the number of empty lines passed."
  11859. (let ((pos (point)))
  11860. (skip-chars-backward " \t\n\r")
  11861. (beginning-of-line 2)
  11862. (goto-char (min (point) pos))
  11863. (count-lines (point) pos)))
  11864. (defun org-skip-whitespace ()
  11865. (skip-chars-forward " \t\n\r"))
  11866. (defun org-point-in-group (point group &optional context)
  11867. "Check if POINT is in match-group GROUP.
  11868. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  11869. match. If the match group does ot exist or point is not inside it,
  11870. return nil."
  11871. (and (match-beginning group)
  11872. (>= point (match-beginning group))
  11873. (<= point (match-end group))
  11874. (if context
  11875. (list context (match-beginning group) (match-end group))
  11876. t)))
  11877. (defun org-switch-to-buffer-other-window (&rest args)
  11878. "Switch to buffer in a second window on the current frame.
  11879. In particular, do not allow pop-up frames."
  11880. (let (pop-up-frames special-display-buffer-names special-display-regexps
  11881. special-display-function)
  11882. (apply 'switch-to-buffer-other-window args)))
  11883. (defun org-combine-plists (&rest plists)
  11884. "Create a single property list from all plists in PLISTS.
  11885. The process starts by copying the first list, and then setting properties
  11886. from the other lists. Settings in the last list are the most significant
  11887. ones and overrule settings in the other lists."
  11888. (let ((rtn (copy-sequence (pop plists)))
  11889. p v ls)
  11890. (while plists
  11891. (setq ls (pop plists))
  11892. (while ls
  11893. (setq p (pop ls) v (pop ls))
  11894. (setq rtn (plist-put rtn p v))))
  11895. rtn))
  11896. (defun org-move-line-down (arg)
  11897. "Move the current line down. With prefix argument, move it past ARG lines."
  11898. (interactive "p")
  11899. (let ((col (current-column))
  11900. beg end pos)
  11901. (beginning-of-line 1) (setq beg (point))
  11902. (beginning-of-line 2) (setq end (point))
  11903. (beginning-of-line (+ 1 arg))
  11904. (setq pos (move-marker (make-marker) (point)))
  11905. (insert (delete-and-extract-region beg end))
  11906. (goto-char pos)
  11907. (move-to-column col)))
  11908. (defun org-move-line-up (arg)
  11909. "Move the current line up. With prefix argument, move it past ARG lines."
  11910. (interactive "p")
  11911. (let ((col (current-column))
  11912. beg end pos)
  11913. (beginning-of-line 1) (setq beg (point))
  11914. (beginning-of-line 2) (setq end (point))
  11915. (beginning-of-line (- arg))
  11916. (setq pos (move-marker (make-marker) (point)))
  11917. (insert (delete-and-extract-region beg end))
  11918. (goto-char pos)
  11919. (move-to-column col)))
  11920. (defun org-replace-escapes (string table)
  11921. "Replace %-escapes in STRING with values in TABLE.
  11922. TABLE is an association list with keys like \"%a\" and string values.
  11923. The sequences in STRING may contain normal field width and padding information,
  11924. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  11925. so values can contain further %-escapes if they are define later in TABLE."
  11926. (let ((case-fold-search nil)
  11927. e re rpl)
  11928. (while (setq e (pop table))
  11929. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  11930. (while (string-match re string)
  11931. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  11932. (cdr e)))
  11933. (setq string (replace-match rpl t t string))))
  11934. string))
  11935. (defun org-sublist (list start end)
  11936. "Return a section of LIST, from START to END.
  11937. Counting starts at 1."
  11938. (let (rtn (c start))
  11939. (setq list (nthcdr (1- start) list))
  11940. (while (and list (<= c end))
  11941. (push (pop list) rtn)
  11942. (setq c (1+ c)))
  11943. (nreverse rtn)))
  11944. (defun org-find-base-buffer-visiting (file)
  11945. "Like `find-buffer-visiting' but alway return the base buffer and
  11946. not an indirect buffer."
  11947. (let ((buf (find-buffer-visiting file)))
  11948. (if buf
  11949. (or (buffer-base-buffer buf) buf)
  11950. nil)))
  11951. (defun org-image-file-name-regexp ()
  11952. "Return regexp matching the file names of images."
  11953. (if (fboundp 'image-file-name-regexp)
  11954. (image-file-name-regexp)
  11955. (let ((image-file-name-extensions
  11956. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  11957. "xbm" "xpm" "pbm" "pgm" "ppm")))
  11958. (concat "\\."
  11959. (regexp-opt (nconc (mapcar 'upcase
  11960. image-file-name-extensions)
  11961. image-file-name-extensions)
  11962. t)
  11963. "\\'"))))
  11964. (defun org-file-image-p (file)
  11965. "Return non-nil if FILE is an image."
  11966. (save-match-data
  11967. (string-match (org-image-file-name-regexp) file)))
  11968. ;;; Paragraph filling stuff.
  11969. ;; We want this to be just right, so use the full arsenal.
  11970. (defun org-indent-line-function ()
  11971. "Indent line like previous, but further if previous was headline or item."
  11972. (interactive)
  11973. (let* ((pos (point))
  11974. (itemp (org-at-item-p))
  11975. column bpos bcol tpos tcol bullet btype bullet-type)
  11976. ;; Find the previous relevant line
  11977. (beginning-of-line 1)
  11978. (cond
  11979. ((looking-at "#") (setq column 0))
  11980. ((looking-at "\\*+ ") (setq column 0))
  11981. (t
  11982. (beginning-of-line 0)
  11983. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  11984. (beginning-of-line 0))
  11985. (cond
  11986. ((looking-at "\\*+[ \t]+")
  11987. (goto-char (match-end 0))
  11988. (setq column (current-column)))
  11989. ((org-in-item-p)
  11990. (org-beginning-of-item)
  11991. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  11992. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  11993. (setq bpos (match-beginning 1) tpos (match-end 0)
  11994. bcol (progn (goto-char bpos) (current-column))
  11995. tcol (progn (goto-char tpos) (current-column))
  11996. bullet (match-string 1)
  11997. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  11998. (if (not itemp)
  11999. (setq column tcol)
  12000. (goto-char pos)
  12001. (beginning-of-line 1)
  12002. (if (looking-at "\\S-")
  12003. (progn
  12004. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12005. (setq bullet (match-string 1)
  12006. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12007. (setq column (if (equal btype bullet-type) bcol tcol)))
  12008. (setq column (org-get-indentation)))))
  12009. (t (setq column (org-get-indentation))))))
  12010. (goto-char pos)
  12011. (if (<= (current-column) (current-indentation))
  12012. (indent-line-to column)
  12013. (save-excursion (indent-line-to column)))
  12014. (setq column (current-column))
  12015. (beginning-of-line 1)
  12016. (if (looking-at
  12017. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12018. (replace-match (concat "\\1" (format org-property-format
  12019. (match-string 2) (match-string 3)))
  12020. t nil))
  12021. (move-to-column column)))
  12022. (defun org-set-autofill-regexps ()
  12023. (interactive)
  12024. ;; In the paragraph separator we include headlines, because filling
  12025. ;; text in a line directly attached to a headline would otherwise
  12026. ;; fill the headline as well.
  12027. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12028. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12029. ;; The paragraph starter includes hand-formatted lists.
  12030. (org-set-local 'paragraph-start
  12031. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12032. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12033. ;; But only if the user has not turned off tables or fixed-width regions
  12034. (org-set-local
  12035. 'auto-fill-inhibit-regexp
  12036. (concat "\\*+ \\|#\\+"
  12037. "\\|[ \t]*" org-keyword-time-regexp
  12038. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12039. (concat
  12040. "\\|[ \t]*["
  12041. (if org-enable-table-editor "|" "")
  12042. (if org-enable-fixed-width-editor ":" "")
  12043. "]"))))
  12044. ;; We use our own fill-paragraph function, to make sure that tables
  12045. ;; and fixed-width regions are not wrapped. That function will pass
  12046. ;; through to `fill-paragraph' when appropriate.
  12047. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12048. ; Adaptive filling: To get full control, first make sure that
  12049. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12050. (org-set-local 'adaptive-fill-regexp "\000")
  12051. (org-set-local 'adaptive-fill-function
  12052. 'org-adaptive-fill-function)
  12053. (org-set-local
  12054. 'align-mode-rules-list
  12055. '((org-in-buffer-settings
  12056. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12057. (modes . '(org-mode))))))
  12058. (defun org-fill-paragraph (&optional justify)
  12059. "Re-align a table, pass through to fill-paragraph if no table."
  12060. (let ((table-p (org-at-table-p))
  12061. (table.el-p (org-at-table.el-p)))
  12062. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12063. (save-excursion (goto-char (point-at-bol))
  12064. (looking-at outline-regexp)))
  12065. t) ; skip headlines
  12066. (table.el-p t) ; skip table.el tables
  12067. (table-p (org-table-align) t) ; align org-mode tables
  12068. (t nil)))) ; call paragraph-fill
  12069. ;; For reference, this is the default value of adaptive-fill-regexp
  12070. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12071. (defun org-adaptive-fill-function ()
  12072. "Return a fill prefix for org-mode files.
  12073. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12074. work correctly."
  12075. (cond ((looking-at "#[ \t]+")
  12076. (match-string 0))
  12077. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12078. (save-excursion
  12079. (goto-char (match-end 0))
  12080. (make-string (current-column) ?\ )))
  12081. (t nil)))
  12082. ;;; Other stuff.
  12083. (defun org-toggle-fixed-width-section (arg)
  12084. "Toggle the fixed-width export.
  12085. If there is no active region, the QUOTE keyword at the current headline is
  12086. inserted or removed. When present, it causes the text between this headline
  12087. and the next to be exported as fixed-width text, and unmodified.
  12088. If there is an active region, this command adds or removes a colon as the
  12089. first character of this line. If the first character of a line is a colon,
  12090. this line is also exported in fixed-width font."
  12091. (interactive "P")
  12092. (let* ((cc 0)
  12093. (regionp (org-region-active-p))
  12094. (beg (if regionp (region-beginning) (point)))
  12095. (end (if regionp (region-end)))
  12096. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12097. (case-fold-search nil)
  12098. (re "[ \t]*\\(:\\)")
  12099. off)
  12100. (if regionp
  12101. (save-excursion
  12102. (goto-char beg)
  12103. (setq cc (current-column))
  12104. (beginning-of-line 1)
  12105. (setq off (looking-at re))
  12106. (while (> nlines 0)
  12107. (setq nlines (1- nlines))
  12108. (beginning-of-line 1)
  12109. (cond
  12110. (arg
  12111. (move-to-column cc t)
  12112. (insert ":\n")
  12113. (forward-line -1))
  12114. ((and off (looking-at re))
  12115. (replace-match "" t t nil 1))
  12116. ((not off) (move-to-column cc t) (insert ":")))
  12117. (forward-line 1)))
  12118. (save-excursion
  12119. (org-back-to-heading)
  12120. (if (looking-at (concat outline-regexp
  12121. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12122. (replace-match "" t t nil 1)
  12123. (if (looking-at outline-regexp)
  12124. (progn
  12125. (goto-char (match-end 0))
  12126. (insert org-quote-string " "))))))))
  12127. ;;;; Functions extending outline functionality
  12128. (defun org-beginning-of-line (&optional arg)
  12129. "Go to the beginning of the current line. If that is invisible, continue
  12130. to a visible line beginning. This makes the function of C-a more intuitive.
  12131. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12132. first attempt, and only move to after the tags when the cursor is already
  12133. beyond the end of the headline."
  12134. (interactive "P")
  12135. (let ((pos (point)))
  12136. (beginning-of-line 1)
  12137. (if (bobp)
  12138. nil
  12139. (backward-char 1)
  12140. (if (org-invisible-p)
  12141. (while (and (not (bobp)) (org-invisible-p))
  12142. (backward-char 1)
  12143. (beginning-of-line 1))
  12144. (forward-char 1)))
  12145. (when org-special-ctrl-a/e
  12146. (cond
  12147. ((and (looking-at org-todo-line-regexp)
  12148. (= (char-after (match-end 1)) ?\ ))
  12149. (goto-char
  12150. (if (eq org-special-ctrl-a/e t)
  12151. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12152. ((= pos (point)) (match-beginning 3))
  12153. (t (point)))
  12154. (cond ((> pos (point)) (point))
  12155. ((not (eq last-command this-command)) (point))
  12156. (t (match-beginning 3))))))
  12157. ((org-at-item-p)
  12158. (goto-char
  12159. (if (eq org-special-ctrl-a/e t)
  12160. (cond ((> pos (match-end 4)) (match-end 4))
  12161. ((= pos (point)) (match-end 4))
  12162. (t (point)))
  12163. (cond ((> pos (point)) (point))
  12164. ((not (eq last-command this-command)) (point))
  12165. (t (match-end 4))))))))))
  12166. (defun org-end-of-line (&optional arg)
  12167. "Go to the end of the line.
  12168. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12169. first attempt, and only move to after the tags when the cursor is already
  12170. beyond the end of the headline."
  12171. (interactive "P")
  12172. (if (or (not org-special-ctrl-a/e)
  12173. (not (org-on-heading-p)))
  12174. (end-of-line arg)
  12175. (let ((pos (point)))
  12176. (beginning-of-line 1)
  12177. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12178. (if (eq org-special-ctrl-a/e t)
  12179. (if (or (< pos (match-beginning 1))
  12180. (= pos (match-end 0)))
  12181. (goto-char (match-beginning 1))
  12182. (goto-char (match-end 0)))
  12183. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12184. (goto-char (match-end 0))
  12185. (goto-char (match-beginning 1))))
  12186. (end-of-line arg)))))
  12187. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12188. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12189. (defun org-kill-line (&optional arg)
  12190. "Kill line, to tags or end of line."
  12191. (interactive "P")
  12192. (cond
  12193. ((or (not org-special-ctrl-k)
  12194. (bolp)
  12195. (not (org-on-heading-p)))
  12196. (call-interactively 'kill-line))
  12197. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12198. (kill-region (point) (match-beginning 1))
  12199. (org-set-tags nil t))
  12200. (t (kill-region (point) (point-at-eol)))))
  12201. (define-key org-mode-map "\C-k" 'org-kill-line)
  12202. (defun org-invisible-p ()
  12203. "Check if point is at a character currently not visible."
  12204. ;; Early versions of noutline don't have `outline-invisible-p'.
  12205. (if (fboundp 'outline-invisible-p)
  12206. (outline-invisible-p)
  12207. (get-char-property (point) 'invisible)))
  12208. (defun org-invisible-p2 ()
  12209. "Check if point is at a character currently not visible."
  12210. (save-excursion
  12211. (if (and (eolp) (not (bobp))) (backward-char 1))
  12212. ;; Early versions of noutline don't have `outline-invisible-p'.
  12213. (if (fboundp 'outline-invisible-p)
  12214. (outline-invisible-p)
  12215. (get-char-property (point) 'invisible))))
  12216. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12217. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12218. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12219. (defun org-at-heading-or-item-p ()
  12220. (or (org-on-heading-p) (org-at-item-p)))
  12221. (defun org-on-target-p ()
  12222. (or (org-in-regexp org-radio-target-regexp)
  12223. (org-in-regexp org-target-regexp)))
  12224. (defun org-up-heading-all (arg)
  12225. "Move to the heading line of which the present line is a subheading.
  12226. This function considers both visible and invisible heading lines.
  12227. With argument, move up ARG levels."
  12228. (if (fboundp 'outline-up-heading-all)
  12229. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12230. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12231. (defun org-up-heading-safe ()
  12232. "Move to the heading line of which the present line is a subheading.
  12233. This version will not throw an error. It will return the level of the
  12234. headline found, or nil if no higher level is found."
  12235. (let ((pos (point)) start-level level
  12236. (re (concat "^" outline-regexp)))
  12237. (catch 'exit
  12238. (outline-back-to-heading t)
  12239. (setq start-level (funcall outline-level))
  12240. (if (equal start-level 1) (throw 'exit nil))
  12241. (while (re-search-backward re nil t)
  12242. (setq level (funcall outline-level))
  12243. (if (< level start-level) (throw 'exit level)))
  12244. nil)))
  12245. (defun org-first-sibling-p ()
  12246. "Is this heading the first child of its parents?"
  12247. (interactive)
  12248. (let ((re (concat "^" outline-regexp))
  12249. level l)
  12250. (unless (org-at-heading-p t)
  12251. (error "Not at a heading"))
  12252. (setq level (funcall outline-level))
  12253. (save-excursion
  12254. (if (not (re-search-backward re nil t))
  12255. t
  12256. (setq l (funcall outline-level))
  12257. (< l level)))))
  12258. (defun org-goto-sibling (&optional previous)
  12259. "Goto the next sibling, even if it is invisible.
  12260. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12261. when a sibling was found. When none is found, return nil and don't
  12262. move point."
  12263. (let ((fun (if previous 're-search-backward 're-search-forward))
  12264. (pos (point))
  12265. (re (concat "^" outline-regexp))
  12266. level l)
  12267. (when (condition-case nil (org-back-to-heading t) (error nil))
  12268. (setq level (funcall outline-level))
  12269. (catch 'exit
  12270. (or previous (forward-char 1))
  12271. (while (funcall fun re nil t)
  12272. (setq l (funcall outline-level))
  12273. (when (< l level) (goto-char pos) (throw 'exit nil))
  12274. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12275. (goto-char pos)
  12276. nil))))
  12277. (defun org-show-siblings ()
  12278. "Show all siblings of the current headline."
  12279. (save-excursion
  12280. (while (org-goto-sibling) (org-flag-heading nil)))
  12281. (save-excursion
  12282. (while (org-goto-sibling 'previous)
  12283. (org-flag-heading nil))))
  12284. (defun org-show-hidden-entry ()
  12285. "Show an entry where even the heading is hidden."
  12286. (save-excursion
  12287. (org-show-entry)))
  12288. (defun org-flag-heading (flag &optional entry)
  12289. "Flag the current heading. FLAG non-nil means make invisible.
  12290. When ENTRY is non-nil, show the entire entry."
  12291. (save-excursion
  12292. (org-back-to-heading t)
  12293. ;; Check if we should show the entire entry
  12294. (if entry
  12295. (progn
  12296. (org-show-entry)
  12297. (save-excursion
  12298. (and (outline-next-heading)
  12299. (org-flag-heading nil))))
  12300. (outline-flag-region (max (point-min) (1- (point)))
  12301. (save-excursion (outline-end-of-heading) (point))
  12302. flag))))
  12303. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12304. ;; This is an exact copy of the original function, but it uses
  12305. ;; `org-back-to-heading', to make it work also in invisible
  12306. ;; trees. And is uses an invisible-OK argument.
  12307. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12308. (org-back-to-heading invisible-OK)
  12309. (let ((first t)
  12310. (level (funcall outline-level)))
  12311. (while (and (not (eobp))
  12312. (or first (> (funcall outline-level) level)))
  12313. (setq first nil)
  12314. (outline-next-heading))
  12315. (unless to-heading
  12316. (if (memq (preceding-char) '(?\n ?\^M))
  12317. (progn
  12318. ;; Go to end of line before heading
  12319. (forward-char -1)
  12320. (if (memq (preceding-char) '(?\n ?\^M))
  12321. ;; leave blank line before heading
  12322. (forward-char -1))))))
  12323. (point))
  12324. (defun org-show-subtree ()
  12325. "Show everything after this heading at deeper levels."
  12326. (outline-flag-region
  12327. (point)
  12328. (save-excursion
  12329. (outline-end-of-subtree) (outline-next-heading) (point))
  12330. nil))
  12331. (defun org-show-entry ()
  12332. "Show the body directly following this heading.
  12333. Show the heading too, if it is currently invisible."
  12334. (interactive)
  12335. (save-excursion
  12336. (condition-case nil
  12337. (progn
  12338. (org-back-to-heading t)
  12339. (outline-flag-region
  12340. (max (point-min) (1- (point)))
  12341. (save-excursion
  12342. (re-search-forward
  12343. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12344. (or (match-beginning 1) (point-max)))
  12345. nil))
  12346. (error nil))))
  12347. (defun org-make-options-regexp (kwds)
  12348. "Make a regular expression for keyword lines."
  12349. (concat
  12350. "^"
  12351. "#?[ \t]*\\+\\("
  12352. (mapconcat 'regexp-quote kwds "\\|")
  12353. "\\):[ \t]*"
  12354. "\\(.+\\)"))
  12355. ;; Make isearch reveal the necessary context
  12356. (defun org-isearch-end ()
  12357. "Reveal context after isearch exits."
  12358. (when isearch-success ; only if search was successful
  12359. (if (featurep 'xemacs)
  12360. ;; Under XEmacs, the hook is run in the correct place,
  12361. ;; we directly show the context.
  12362. (org-show-context 'isearch)
  12363. ;; In Emacs the hook runs *before* restoring the overlays.
  12364. ;; So we have to use a one-time post-command-hook to do this.
  12365. ;; (Emacs 22 has a special variable, see function `org-mode')
  12366. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12367. isearch-mode-end-hook-quit)
  12368. ;; Only when the isearch was not quitted.
  12369. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12370. 'append 'local)))))
  12371. (defun org-isearch-post-command ()
  12372. "Remove self from hook, and show context."
  12373. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12374. (org-show-context 'isearch))
  12375. ;;;; Integration with and fixes for other packages
  12376. ;;; Imenu support
  12377. (defvar org-imenu-markers nil
  12378. "All markers currently used by Imenu.")
  12379. (make-variable-buffer-local 'org-imenu-markers)
  12380. (defun org-imenu-new-marker (&optional pos)
  12381. "Return a new marker for use by Imenu, and remember the marker."
  12382. (let ((m (make-marker)))
  12383. (move-marker m (or pos (point)))
  12384. (push m org-imenu-markers)
  12385. m))
  12386. (defun org-imenu-get-tree ()
  12387. "Produce the index for Imenu."
  12388. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12389. (setq org-imenu-markers nil)
  12390. (let* ((n org-imenu-depth)
  12391. (re (concat "^" outline-regexp))
  12392. (subs (make-vector (1+ n) nil))
  12393. (last-level 0)
  12394. m tree level head)
  12395. (save-excursion
  12396. (save-restriction
  12397. (widen)
  12398. (goto-char (point-max))
  12399. (while (re-search-backward re nil t)
  12400. (setq level (org-reduced-level (funcall outline-level)))
  12401. (when (<= level n)
  12402. (looking-at org-complex-heading-regexp)
  12403. (setq head (org-match-string-no-properties 4)
  12404. m (org-imenu-new-marker))
  12405. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12406. (if (>= level last-level)
  12407. (push (cons head m) (aref subs level))
  12408. (push (cons head (aref subs (1+ level))) (aref subs level))
  12409. (loop for i from (1+ level) to n do (aset subs i nil)))
  12410. (setq last-level level)))))
  12411. (aref subs 1)))
  12412. (eval-after-load "imenu"
  12413. '(progn
  12414. (add-hook 'imenu-after-jump-hook
  12415. (lambda () (org-show-context 'org-goto)))))
  12416. ;; Speedbar support
  12417. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12418. "Overlay marking the agenda restriction line in speedbar.")
  12419. (org-overlay-put org-speedbar-restriction-lock-overlay
  12420. 'face 'org-agenda-restriction-lock)
  12421. (org-overlay-put org-speedbar-restriction-lock-overlay
  12422. 'help-echo "Agendas are currently limited to this item.")
  12423. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12424. (defun org-speedbar-set-agenda-restriction ()
  12425. "Restrict future agenda commands to the location at point in speedbar.
  12426. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12427. (interactive)
  12428. (require 'org-agenda)
  12429. (let (p m tp np dir txt w)
  12430. (cond
  12431. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12432. 'org-imenu t))
  12433. (setq m (get-text-property p 'org-imenu-marker))
  12434. (save-excursion
  12435. (save-restriction
  12436. (set-buffer (marker-buffer m))
  12437. (goto-char m)
  12438. (org-agenda-set-restriction-lock 'subtree))))
  12439. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12440. 'speedbar-function 'speedbar-find-file))
  12441. (setq tp (previous-single-property-change
  12442. (1+ p) 'speedbar-function)
  12443. np (next-single-property-change
  12444. tp 'speedbar-function)
  12445. dir (speedbar-line-directory)
  12446. txt (buffer-substring-no-properties (or tp (point-min))
  12447. (or np (point-max))))
  12448. (save-excursion
  12449. (save-restriction
  12450. (set-buffer (find-file-noselect
  12451. (let ((default-directory dir))
  12452. (expand-file-name txt))))
  12453. (unless (org-mode-p)
  12454. (error "Cannot restrict to non-Org-mode file"))
  12455. (org-agenda-set-restriction-lock 'file))))
  12456. (t (error "Don't know how to restrict Org-mode's agenda")))
  12457. (org-move-overlay org-speedbar-restriction-lock-overlay
  12458. (point-at-bol) (point-at-eol))
  12459. (setq current-prefix-arg nil)
  12460. (org-agenda-maybe-redo)))
  12461. (eval-after-load "speedbar"
  12462. '(progn
  12463. (speedbar-add-supported-extension ".org")
  12464. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12465. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12466. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12467. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12468. (add-hook 'speedbar-visiting-tag-hook
  12469. (lambda () (org-show-context 'org-goto)))))
  12470. ;;; Fixes and Hacks for problems with other packages
  12471. ;; Make flyspell not check words in links, to not mess up our keymap
  12472. (defun org-mode-flyspell-verify ()
  12473. "Don't let flyspell put overlays at active buttons."
  12474. (not (get-text-property (point) 'keymap)))
  12475. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12476. (eval-after-load "bookmark"
  12477. '(if (boundp 'bookmark-after-jump-hook)
  12478. ;; We can use the hook
  12479. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12480. ;; Hook not available, use advice
  12481. (defadvice bookmark-jump (after org-make-visible activate)
  12482. "Make the position visible."
  12483. (org-bookmark-jump-unhide))))
  12484. (defun org-bookmark-jump-unhide ()
  12485. "Unhide the current position, to show the bookmark location."
  12486. (and (org-mode-p)
  12487. (or (org-invisible-p)
  12488. (save-excursion (goto-char (max (point-min) (1- (point))))
  12489. (org-invisible-p)))
  12490. (org-show-context 'bookmark-jump)))
  12491. ;; Make session.el ignore our circular variable
  12492. (eval-after-load "session"
  12493. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12494. ;;;; Experimental code
  12495. (defun org-closed-in-range ()
  12496. "Sparse tree of items closed in a certain time range.
  12497. Still experimental, may disappear in the future."
  12498. (interactive)
  12499. ;; Get the time interval from the user.
  12500. (let* ((time1 (time-to-seconds
  12501. (org-read-date nil 'to-time nil "Starting date: ")))
  12502. (time2 (time-to-seconds
  12503. (org-read-date nil 'to-time nil "End date:")))
  12504. ;; callback function
  12505. (callback (lambda ()
  12506. (let ((time
  12507. (time-to-seconds
  12508. (apply 'encode-time
  12509. (org-parse-time-string
  12510. (match-string 1))))))
  12511. ;; check if time in interval
  12512. (and (>= time time1) (<= time time2))))))
  12513. ;; make tree, check each match with the callback
  12514. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12515. ;;;; Finish up
  12516. (provide 'org)
  12517. (run-hooks 'org-load-hook)
  12518. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12519. ;;; org.el ends here