org.el 509 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873
  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.02b
  9. ;;
  10. ;; This file is part of GNU Emacs.
  11. ;;
  12. ;; GNU Emacs is free software: you can redistribute it and/or modify
  13. ;; it under the terms of the GNU General Public License as published by
  14. ;; the Free Software Foundation, either version 3 of the License, or
  15. ;; (at your option) any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  22. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  23. ;;
  24. ;;; Commentary:
  25. ;;
  26. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  27. ;; project planning with a fast and effective plain-text system.
  28. ;;
  29. ;; Org-mode develops organizational tasks around NOTES files that contain
  30. ;; information about projects as plain text. Org-mode is implemented on
  31. ;; top of outline-mode, which makes it possible to keep the content of
  32. ;; large files well structured. Visibility cycling and structure editing
  33. ;; help to work with the tree. Tables are easily created with a built-in
  34. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  35. ;; and scheduling. It dynamically compiles entries into an agenda that
  36. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  37. ;; Plain text URL-like links connect to websites, emails, Usenet
  38. ;; messages, BBDB entries, and any files related to the projects. For
  39. ;; printing and sharing of notes, an Org-mode file can be exported as a
  40. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  41. ;; iCalendar file. It can also serve as a publishing tool for a set of
  42. ;; linked webpages.
  43. ;;
  44. ;; Installation and Activation
  45. ;; ---------------------------
  46. ;; See the corresponding sections in the manual at
  47. ;;
  48. ;; http://orgmode.org/org.html#Installation
  49. ;;
  50. ;; Documentation
  51. ;; -------------
  52. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  53. ;; distribution also contains a PDF version of it. At the homepage of
  54. ;; Org-mode, you can read the same text online as HTML. There is also an
  55. ;; excellent reference card made by Philip Rooke. This card can be found
  56. ;; in the etc/ directory of Emacs 22.
  57. ;;
  58. ;; A list of recent changes can be found at
  59. ;; http://orgmode.org/Changes.html
  60. ;;
  61. ;;; Code:
  62. (defvar org-inhibit-highlight-removal nil) ; dynamically scoped param
  63. (defvar org-table-formula-constants-local nil
  64. "Local version of `org-table-formula-constants'.")
  65. (make-variable-buffer-local 'org-table-formula-constants-local)
  66. ;;;; Require other packages
  67. (eval-when-compile
  68. (require 'cl)
  69. (require 'gnus-sum)
  70. (require 'calendar))
  71. ;; For XEmacs, noutline is not yet provided by outline.el, so arrange for
  72. ;; the file noutline.el being loaded.
  73. (if (featurep 'xemacs) (condition-case nil (require 'noutline)))
  74. ;; We require noutline, which might be provided in outline.el
  75. (require 'outline) (require 'noutline)
  76. ;; Other stuff we need.
  77. (require 'time-date)
  78. (unless (fboundp 'time-subtract) (defalias 'time-subtract 'subtract-time))
  79. (require 'easymenu)
  80. (require 'org-macs)
  81. (require 'org-compat)
  82. (require 'org-faces)
  83. ;;;; Customization variables
  84. ;;; Version
  85. (defconst org-version "6.02b"
  86. "The version number of the file org.el.")
  87. (defun org-version (&optional here)
  88. "Show the org-mode version in the echo area.
  89. With prefix arg HERE, insert it at point."
  90. (interactive "P")
  91. (let ((version (format "Org-mode version %s" org-version)))
  92. (message version)
  93. (if here
  94. (insert version))))
  95. ;;; Compatibility constants
  96. ;;; The custom variables
  97. (defgroup org nil
  98. "Outline-based notes management and organizer."
  99. :tag "Org"
  100. :group 'outlines
  101. :group 'hypermedia
  102. :group 'calendar)
  103. (defcustom org-load-hook nil
  104. "Hook that is run after org.el has been loaded."
  105. :group 'org
  106. :type 'hook)
  107. (defvar org-modules) ; defined below
  108. (defvar org-modules-loaded nil
  109. "Have the modules been loaded already?")
  110. (defun org-load-modules-maybe (&optional force)
  111. "Load all extensions listed in `org-default-extensions'."
  112. (when (or force (not org-modules-loaded))
  113. (mapc (lambda (ext)
  114. (condition-case nil (require ext)
  115. (error (message "Problems while trying to load feature `%s'" ext))))
  116. org-modules)
  117. (setq org-modules-loaded t)))
  118. (defun org-set-modules (var value)
  119. "Set VAR to VALUE and call `org-load-modules-maybe' with the force flag."
  120. (set var value)
  121. (when (featurep 'org)
  122. (org-load-modules-maybe 'force)))
  123. (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)
  124. "Modules that should always be loaded together with org.el.
  125. If a description starts with <C>, the file is not part of emacs
  126. and loading it will require that you have downloaded and properly installed
  127. the org-mode distribution.
  128. You can also use this system to load external packages (i.e. neither Org
  129. core modules, not modules from the CONTRIB directory). Just add symbols
  130. to the end of the list. If the package is called org-xyz.e, then you need
  131. to add the symbol `xyz', and the package must have a call to
  132. (provide 'org-xyz)"
  133. :group 'org
  134. :set 'org-set-modules
  135. :type
  136. '(set :greedy t
  137. (const :tag " bbdb: Links to BBDB entries" org-bbdb)
  138. (const :tag " bibtex: Links to BibTeX entries" org-bibtex)
  139. (const :tag " gnus: Links to GNUS folders/messages" org-gnus)
  140. (const :tag " info: Links to Info nodes" org-info)
  141. (const :tag " infojs: Set up Sebastian Rose's JavaScript org-info.js" org-infojs)
  142. (const :tag " irc: Links to IRC/ERC chat sessions" org-irc)
  143. (const :tag " mac-message: Links to messages in Apple Mail" org-mac-message)
  144. (const :tag " mew Links to Mew folders/messages" org-mew)
  145. (const :tag " mhe: Links to MHE folders/messages" org-mhe)
  146. (const :tag " rmail: Links to RMAIL folders/messages" org-rmail)
  147. (const :tag " vm: Links to VM folders/messages" org-vm)
  148. (const :tag " wl: Links to Wanderlust folders/messages" org-wl)
  149. (const :tag " mouse: Additional mouse support" org-mouse)
  150. (const :tag "C annotate-file: Annotate a file with org syntax" org-annotate-file)
  151. (const :tag "C bookmark: Org links to bookmarks" org-bookmark)
  152. (const :tag "C depend: TODO dependencies for Org-mode" org-depend)
  153. (const :tag "C elisp-symbol: Org links to emacs-lisp symbols" org-elisp-symbol)
  154. (const :tag "C eval: Include command output as text" org-eval)
  155. (const :tag "C expiry: Expiry mechanism for Org entries" org-expiry)
  156. (const :tag "C id: Global id's for identifying entries" org-id)
  157. (const :tag "C interactive-query: Interactive modification of tags query" org-interactive-query)
  158. (const :tag "C mairix: Hook mairix search into Org for different MUAs" org-mairix)
  159. (const :tag "C man: Support for links to manpages in Org-mode" org-man)
  160. (const :tag "C panel: Simple routines for us with bad memory" org-panel)
  161. (const :tag "C registry: A registry for Org links" org-registry)
  162. (const :tag "C org2rem: Convert org appointments into reminders" org2rem)
  163. (const :tag "C screen: Visit screen sessions through Org-mode links" org-screen)
  164. (const :tag "C toc: Table of contents for Org-mode buffer" org-toc)
  165. (const :tag "C sqlinsert: Convert Org-mode tables to SQL insertions" orgtbl-sqlinsert)
  166. (repeat :tag "External packages" :inline t (symbol :tag "Package"))))
  167. (defgroup org-startup nil
  168. "Options concerning startup of Org-mode."
  169. :tag "Org Startup"
  170. :group 'org)
  171. (defcustom org-startup-folded t
  172. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  173. This can also be configured on a per-file basis by adding one of
  174. the following lines anywhere in the buffer:
  175. #+STARTUP: fold
  176. #+STARTUP: nofold
  177. #+STARTUP: content"
  178. :group 'org-startup
  179. :type '(choice
  180. (const :tag "nofold: show all" nil)
  181. (const :tag "fold: overview" t)
  182. (const :tag "content: all headlines" content)))
  183. (defcustom org-startup-truncated t
  184. "Non-nil means, entering Org-mode will set `truncate-lines'.
  185. This is useful since some lines containing links can be very long and
  186. uninteresting. Also tables look terrible when wrapped."
  187. :group 'org-startup
  188. :type 'boolean)
  189. (defcustom org-startup-align-all-tables nil
  190. "Non-nil means, align all tables when visiting a file.
  191. This is useful when the column width in tables is forced with <N> cookies
  192. in table fields. Such tables will look correct only after the first re-align.
  193. This can also be configured on a per-file basis by adding one of
  194. the following lines anywhere in the buffer:
  195. #+STARTUP: align
  196. #+STARTUP: noalign"
  197. :group 'org-startup
  198. :type 'boolean)
  199. (defcustom org-insert-mode-line-in-empty-file nil
  200. "Non-nil means insert the first line setting Org-mode in empty files.
  201. When the function `org-mode' is called interactively in an empty file, this
  202. normally means that the file name does not automatically trigger Org-mode.
  203. To ensure that the file will always be in Org-mode in the future, a
  204. line enforcing Org-mode will be inserted into the buffer, if this option
  205. has been set."
  206. :group 'org-startup
  207. :type 'boolean)
  208. (defcustom org-replace-disputed-keys nil
  209. "Non-nil means use alternative key bindings for some keys.
  210. Org-mode uses S-<cursor> keys for changing timestamps and priorities.
  211. These keys are also used by other packages like `CUA-mode' or `windmove.el'.
  212. If you want to use Org-mode together with one of these other modes,
  213. or more generally if you would like to move some Org-mode commands to
  214. other keys, set this variable and configure the keys with the variable
  215. `org-disputed-keys'.
  216. This option is only relevant at load-time of Org-mode, and must be set
  217. *before* org.el is loaded. Changing it requires a restart of Emacs to
  218. become effective."
  219. :group 'org-startup
  220. :type 'boolean)
  221. (if (fboundp 'defvaralias)
  222. (defvaralias 'org-CUA-compatible 'org-replace-disputed-keys))
  223. (defcustom org-disputed-keys
  224. '(([(shift up)] . [(meta p)])
  225. ([(shift down)] . [(meta n)])
  226. ([(shift left)] . [(meta -)])
  227. ([(shift right)] . [(meta +)])
  228. ([(control shift right)] . [(meta shift +)])
  229. ([(control shift left)] . [(meta shift -)]))
  230. "Keys for which Org-mode and other modes compete.
  231. This is an alist, cars are the default keys, second element specifies
  232. the alternative to use when `org-replace-disputed-keys' is t.
  233. Keys can be specified in any syntax supported by `define-key'.
  234. The value of this option takes effect only at Org-mode's startup,
  235. therefore you'll have to restart Emacs to apply it after changing."
  236. :group 'org-startup
  237. :type 'alist)
  238. (defun org-key (key)
  239. "Select key according to `org-replace-disputed-keys' and `org-disputed-keys'.
  240. Or return the original if not disputed."
  241. (if org-replace-disputed-keys
  242. (let* ((nkey (key-description key))
  243. (x (org-find-if (lambda (x)
  244. (equal (key-description (car x)) nkey))
  245. org-disputed-keys)))
  246. (if x (cdr x) key))
  247. key))
  248. (defun org-find-if (predicate seq)
  249. (catch 'exit
  250. (while seq
  251. (if (funcall predicate (car seq))
  252. (throw 'exit (car seq))
  253. (pop seq)))))
  254. (defun org-defkey (keymap key def)
  255. "Define a key, possibly translated, as returned by `org-key'."
  256. (define-key keymap (org-key key) def))
  257. (defcustom org-ellipsis nil
  258. "The ellipsis to use in the Org-mode outline.
  259. When nil, just use the standard three dots. When a string, use that instead,
  260. When a face, use the standart 3 dots, but with the specified face.
  261. The change affects only Org-mode (which will then use its own display table).
  262. Changing this requires executing `M-x org-mode' in a buffer to become
  263. effective."
  264. :group 'org-startup
  265. :type '(choice (const :tag "Default" nil)
  266. (face :tag "Face" :value org-warning)
  267. (string :tag "String" :value "...#")))
  268. (defvar org-display-table nil
  269. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  270. (defgroup org-keywords nil
  271. "Keywords in Org-mode."
  272. :tag "Org Keywords"
  273. :group 'org)
  274. (defcustom org-deadline-string "DEADLINE:"
  275. "String to mark deadline entries.
  276. A deadline is this string, followed by a time stamp. Should be a word,
  277. terminated by a colon. You can insert a schedule keyword and
  278. a timestamp with \\[org-deadline].
  279. Changes become only effective after restarting Emacs."
  280. :group 'org-keywords
  281. :type 'string)
  282. (defcustom org-scheduled-string "SCHEDULED:"
  283. "String to mark scheduled TODO entries.
  284. A schedule is this string, followed by a time stamp. Should be a word,
  285. terminated by a colon. You can insert a schedule keyword and
  286. a timestamp with \\[org-schedule].
  287. Changes become only effective after restarting Emacs."
  288. :group 'org-keywords
  289. :type 'string)
  290. (defcustom org-closed-string "CLOSED:"
  291. "String used as the prefix for timestamps logging closing a TODO entry."
  292. :group 'org-keywords
  293. :type 'string)
  294. (defcustom org-clock-string "CLOCK:"
  295. "String used as prefix for timestamps clocking work hours on an item."
  296. :group 'org-keywords
  297. :type 'string)
  298. (defcustom org-comment-string "COMMENT"
  299. "Entries starting with this keyword will never be exported.
  300. An entry can be toggled between COMMENT and normal with
  301. \\[org-toggle-comment].
  302. Changes become only effective after restarting Emacs."
  303. :group 'org-keywords
  304. :type 'string)
  305. (defcustom org-quote-string "QUOTE"
  306. "Entries starting with this keyword will be exported in fixed-width font.
  307. Quoting applies only to the text in the entry following the headline, and does
  308. not extend beyond the next headline, even if that is lower level.
  309. An entry can be toggled between QUOTE and normal with
  310. \\[org-toggle-fixed-width-section]."
  311. :group 'org-keywords
  312. :type 'string)
  313. (defconst org-repeat-re
  314. "<[0-9]\\{4\\}-[0-9][0-9]-[0-9][0-9] [^>\n]*\\([.+]?\\+[0-9]+[dwmy]\\)"
  315. "Regular expression for specifying repeated events.
  316. After a match, group 1 contains the repeat expression.")
  317. (defgroup org-structure nil
  318. "Options concerning the general structure of Org-mode files."
  319. :tag "Org Structure"
  320. :group 'org)
  321. (defgroup org-reveal-location nil
  322. "Options about how to make context of a location visible."
  323. :tag "Org Reveal Location"
  324. :group 'org-structure)
  325. (defconst org-context-choice
  326. '(choice
  327. (const :tag "Always" t)
  328. (const :tag "Never" nil)
  329. (repeat :greedy t :tag "Individual contexts"
  330. (cons
  331. (choice :tag "Context"
  332. (const agenda)
  333. (const org-goto)
  334. (const occur-tree)
  335. (const tags-tree)
  336. (const link-search)
  337. (const mark-goto)
  338. (const bookmark-jump)
  339. (const isearch)
  340. (const default))
  341. (boolean))))
  342. "Contexts for the reveal options.")
  343. (defcustom org-show-hierarchy-above '((default . t))
  344. "Non-nil means, show full hierarchy when revealing a location.
  345. Org-mode often shows locations in an org-mode file which might have
  346. been invisible before. When this is set, the hierarchy of headings
  347. above the exposed location is shown.
  348. Turning this off for example for sparse trees makes them very compact.
  349. Instead of t, this can also be an alist specifying this option for different
  350. contexts. Valid contexts are
  351. agenda when exposing an entry from the agenda
  352. org-goto when using the command `org-goto' on key C-c C-j
  353. occur-tree when using the command `org-occur' on key C-c /
  354. tags-tree when constructing a sparse tree based on tags matches
  355. link-search when exposing search matches associated with a link
  356. mark-goto when exposing the jump goal of a mark
  357. bookmark-jump when exposing a bookmark location
  358. isearch when exiting from an incremental search
  359. default default for all contexts not set explicitly"
  360. :group 'org-reveal-location
  361. :type org-context-choice)
  362. (defcustom org-show-following-heading '((default . nil))
  363. "Non-nil means, show following heading when revealing a location.
  364. Org-mode often shows locations in an org-mode file which might have
  365. been invisible before. When this is set, the heading following the
  366. match is shown.
  367. Turning this off for example for sparse trees makes them very compact,
  368. but makes it harder to edit the location of the match. In such a case,
  369. use the command \\[org-reveal] to show more context.
  370. Instead of t, this can also be an alist specifying this option for different
  371. contexts. See `org-show-hierarchy-above' for valid contexts."
  372. :group 'org-reveal-location
  373. :type org-context-choice)
  374. (defcustom org-show-siblings '((default . nil) (isearch t))
  375. "Non-nil means, show all sibling heading when revealing a location.
  376. Org-mode often shows locations in an org-mode file which might have
  377. been invisible before. When this is set, the sibling of the current entry
  378. heading are all made visible. If `org-show-hierarchy-above' is t,
  379. the same happens on each level of the hierarchy above the current entry.
  380. By default this is on for the isearch context, off for all other contexts.
  381. Turning this off for example for sparse trees makes them very compact,
  382. but makes it harder to edit the location of the match. In such a case,
  383. use the command \\[org-reveal] to show more context.
  384. Instead of t, this can also be an alist specifying this option for different
  385. contexts. See `org-show-hierarchy-above' for valid contexts."
  386. :group 'org-reveal-location
  387. :type org-context-choice)
  388. (defcustom org-show-entry-below '((default . nil))
  389. "Non-nil means, show the entry below a headline when revealing a location.
  390. Org-mode often shows locations in an org-mode file which might have
  391. been invisible before. When this is set, the text below the headline that is
  392. exposed is also shown.
  393. By default this is off for all contexts.
  394. Instead of t, this can also be an alist specifying this option for different
  395. contexts. See `org-show-hierarchy-above' for valid contexts."
  396. :group 'org-reveal-location
  397. :type org-context-choice)
  398. (defcustom org-indirect-buffer-display 'other-window
  399. "How should indirect tree buffers be displayed?
  400. This applies to indirect buffers created with the commands
  401. \\[org-tree-to-indirect-buffer] and \\[org-agenda-tree-to-indirect-buffer].
  402. Valid values are:
  403. current-window Display in the current window
  404. other-window Just display in another window.
  405. dedicated-frame Create one new frame, and re-use it each time.
  406. new-frame Make a new frame each time. Note that in this case
  407. previously-made indirect buffers are kept, and you need to
  408. kill these buffers yourself."
  409. :group 'org-structure
  410. :group 'org-agenda-windows
  411. :type '(choice
  412. (const :tag "In current window" current-window)
  413. (const :tag "In current frame, other window" other-window)
  414. (const :tag "Each time a new frame" new-frame)
  415. (const :tag "One dedicated frame" dedicated-frame)))
  416. (defgroup org-cycle nil
  417. "Options concerning visibility cycling in Org-mode."
  418. :tag "Org Cycle"
  419. :group 'org-structure)
  420. (defcustom org-drawers '("PROPERTIES" "CLOCK")
  421. "Names of drawers. Drawers are not opened by cycling on the headline above.
  422. Drawers only open with a TAB on the drawer line itself. A drawer looks like
  423. this:
  424. :DRAWERNAME:
  425. .....
  426. :END:
  427. The drawer \"PROPERTIES\" is special for capturing properties through
  428. the property API.
  429. Drawers can be defined on the per-file basis with a line like:
  430. #+DRAWERS: HIDDEN STATE PROPERTIES"
  431. :group 'org-structure
  432. :type '(repeat (string :tag "Drawer Name")))
  433. (defcustom org-cycle-global-at-bob nil
  434. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  435. This makes it possible to do global cycling without having to use S-TAB or
  436. C-u TAB. For this special case to work, the first line of the buffer
  437. must not be a headline - it may be empty ot some other text. When used in
  438. this way, `org-cycle-hook' is disables temporarily, to make sure the
  439. cursor stays at the beginning of the buffer.
  440. When this option is nil, don't do anything special at the beginning
  441. of the buffer."
  442. :group 'org-cycle
  443. :type 'boolean)
  444. (defcustom org-cycle-emulate-tab t
  445. "Where should `org-cycle' emulate TAB.
  446. nil Never
  447. white Only in completely white lines
  448. whitestart Only at the beginning of lines, before the first non-white char
  449. t Everywhere except in headlines
  450. exc-hl-bol Everywhere except at the start of a headline
  451. If TAB is used in a place where it does not emulate TAB, the current subtree
  452. visibility is cycled."
  453. :group 'org-cycle
  454. :type '(choice (const :tag "Never" nil)
  455. (const :tag "Only in completely white lines" white)
  456. (const :tag "Before first char in a line" whitestart)
  457. (const :tag "Everywhere except in headlines" t)
  458. (const :tag "Everywhere except at bol in headlines" exc-hl-bol)
  459. ))
  460. (defcustom org-cycle-separator-lines 2
  461. "Number of empty lines needed to keep an empty line between collapsed trees.
  462. If you leave an empty line between the end of a subtree and the following
  463. headline, this empty line is hidden when the subtree is folded.
  464. Org-mode will leave (exactly) one empty line visible if the number of
  465. empty lines is equal or larger to the number given in this variable.
  466. So the default 2 means, at least 2 empty lines after the end of a subtree
  467. are needed to produce free space between a collapsed subtree and the
  468. following headline.
  469. Special case: when 0, never leave empty lines in collapsed view."
  470. :group 'org-cycle
  471. :type 'integer)
  472. (defcustom org-cycle-hook '(org-cycle-hide-archived-subtrees
  473. org-cycle-hide-drawers
  474. org-cycle-show-empty-lines
  475. org-optimize-window-after-visibility-change)
  476. "Hook that is run after `org-cycle' has changed the buffer visibility.
  477. The function(s) in this hook must accept a single argument which indicates
  478. the new state that was set by the most recent `org-cycle' command. The
  479. argument is a symbol. After a global state change, it can have the values
  480. `overview', `content', or `all'. After a local state change, it can have
  481. the values `folded', `children', or `subtree'."
  482. :group 'org-cycle
  483. :type 'hook)
  484. (defgroup org-edit-structure nil
  485. "Options concerning structure editing in Org-mode."
  486. :tag "Org Edit Structure"
  487. :group 'org-structure)
  488. (defcustom org-odd-levels-only nil
  489. "Non-nil means, skip even levels and only use odd levels for the outline.
  490. This has the effect that two stars are being added/taken away in
  491. promotion/demotion commands. It also influences how levels are
  492. handled by the exporters.
  493. Changing it requires restart of `font-lock-mode' to become effective
  494. for fontification also in regions already fontified.
  495. You may also set this on a per-file basis by adding one of the following
  496. lines to the buffer:
  497. #+STARTUP: odd
  498. #+STARTUP: oddeven"
  499. :group 'org-edit-structure
  500. :group 'org-font-lock
  501. :type 'boolean)
  502. (defcustom org-adapt-indentation t
  503. "Non-nil means, adapt indentation when promoting and demoting.
  504. When this is set and the *entire* text in an entry is indented, the
  505. indentation is increased by one space in a demotion command, and
  506. decreased by one in a promotion command. If any line in the entry
  507. body starts at column 0, indentation is not changed at all."
  508. :group 'org-edit-structure
  509. :type 'boolean)
  510. (defcustom org-special-ctrl-a/e nil
  511. "Non-nil means `C-a' and `C-e' behave specially in headlines and items.
  512. When t, `C-a' will bring back the cursor to the beginning of the
  513. headline text, i.e. after the stars and after a possible TODO keyword.
  514. In an item, this will be the position after the bullet.
  515. When the cursor is already at that position, another `C-a' will bring
  516. it to the beginning of the line.
  517. `C-e' will jump to the end of the headline, ignoring the presence of tags
  518. in the headline. A second `C-e' will then jump to the true end of the
  519. line, after any tags.
  520. When set to the symbol `reversed', the first `C-a' or `C-e' works normally,
  521. and only a directly following, identical keypress will bring the cursor
  522. to the special positions."
  523. :group 'org-edit-structure
  524. :type '(choice
  525. (const :tag "off" nil)
  526. (const :tag "after bullet first" t)
  527. (const :tag "border first" reversed)))
  528. (if (fboundp 'defvaralias)
  529. (defvaralias 'org-special-ctrl-a 'org-special-ctrl-a/e))
  530. (defcustom org-special-ctrl-k nil
  531. "Non-nil means `C-k' will behave specially in headlines.
  532. When nil, `C-k' will call the default `kill-line' command.
  533. When t, the following will happen while the cursor is in the headline:
  534. - When the cursor is at the beginning of a headline, kill the entire
  535. line and possible the folded subtree below the line.
  536. - When in the middle of the headline text, kill the headline up to the tags.
  537. - When after the headline text, kill the tags."
  538. :group 'org-edit-structure
  539. :type 'boolean)
  540. (defcustom org-M-RET-may-split-line '((default . t))
  541. "Non-nil means, M-RET will split the line at the cursor position.
  542. When nil, it will go to the end of the line before making a
  543. new line.
  544. You may also set this option in a different way for different
  545. contexts. Valid contexts are:
  546. headline when creating a new headline
  547. item when creating a new item
  548. table in a table field
  549. default the value to be used for all contexts not explicitly
  550. customized"
  551. :group 'org-structure
  552. :group 'org-table
  553. :type '(choice
  554. (const :tag "Always" t)
  555. (const :tag "Never" nil)
  556. (repeat :greedy t :tag "Individual contexts"
  557. (cons
  558. (choice :tag "Context"
  559. (const headline)
  560. (const item)
  561. (const table)
  562. (const default))
  563. (boolean)))))
  564. (defcustom org-blank-before-new-entry '((heading . nil)
  565. (plain-list-item . nil))
  566. "Should `org-insert-heading' leave a blank line before new heading/item?
  567. The value is an alist, with `heading' and `plain-list-item' as car,
  568. and a boolean flag as cdr."
  569. :group 'org-edit-structure
  570. :type '(list
  571. (cons (const heading) (boolean))
  572. (cons (const plain-list-item) (boolean))))
  573. (defcustom org-insert-heading-hook nil
  574. "Hook being run after inserting a new heading."
  575. :group 'org-edit-structure
  576. :type 'hook)
  577. (defcustom org-enable-fixed-width-editor t
  578. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  579. This currently only means, they are never auto-wrapped.
  580. When nil, such lines will be treated like ordinary lines.
  581. See also the QUOTE keyword."
  582. :group 'org-edit-structure
  583. :type 'boolean)
  584. (defcustom org-goto-auto-isearch t
  585. "Non-nil means, typing characters in org-goto starts incremental search."
  586. :group 'org-edit-structure
  587. :type 'boolean)
  588. (defgroup org-sparse-trees nil
  589. "Options concerning sparse trees in Org-mode."
  590. :tag "Org Sparse Trees"
  591. :group 'org-structure)
  592. (defcustom org-highlight-sparse-tree-matches t
  593. "Non-nil means, highlight all matches that define a sparse tree.
  594. The highlights will automatically disappear the next time the buffer is
  595. changed by an edit command."
  596. :group 'org-sparse-trees
  597. :type 'boolean)
  598. (defcustom org-remove-highlights-with-change t
  599. "Non-nil means, any change to the buffer will remove temporary highlights.
  600. Such highlights are created by `org-occur' and `org-clock-display'.
  601. When nil, `C-c C-c needs to be used to get rid of the highlights.
  602. The highlights created by `org-preview-latex-fragment' always need
  603. `C-c C-c' to be removed."
  604. :group 'org-sparse-trees
  605. :group 'org-time
  606. :type 'boolean)
  607. (defcustom org-occur-hook '(org-first-headline-recenter)
  608. "Hook that is run after `org-occur' has constructed a sparse tree.
  609. This can be used to recenter the window to show as much of the structure
  610. as possible."
  611. :group 'org-sparse-trees
  612. :type 'hook)
  613. (defgroup org-plain-lists nil
  614. "Options concerning plain lists in Org-mode."
  615. :tag "Org Plain lists"
  616. :group 'org-structure)
  617. (defcustom org-cycle-include-plain-lists nil
  618. "Non-nil means, include plain lists into visibility cycling.
  619. This means that during cycling, plain list items will *temporarily* be
  620. interpreted as outline headlines with a level given by 1000+i where i is the
  621. indentation of the bullet. In all other operations, plain list items are
  622. not seen as headlines. For example, you cannot assign a TODO keyword to
  623. such an item."
  624. :group 'org-plain-lists
  625. :type 'boolean)
  626. (defcustom org-plain-list-ordered-item-terminator t
  627. "The character that makes a line with leading number an ordered list item.
  628. Valid values are ?. and ?\). To get both terminators, use t. While
  629. ?. may look nicer, it creates the danger that a line with leading
  630. number may be incorrectly interpreted as an item. ?\) therefore is
  631. the safe choice."
  632. :group 'org-plain-lists
  633. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  634. (const :tag "paren like in \"2)\"" ?\))
  635. (const :tab "both" t)))
  636. (defcustom org-empty-line-terminates-plain-lists nil
  637. "Non-nil means, an empty line ends all plain list levels.
  638. When nil, empty lines are part of the preceeding item."
  639. :group 'org-plain-lists
  640. :type 'boolean)
  641. (defcustom org-auto-renumber-ordered-lists t
  642. "Non-nil means, automatically renumber ordered plain lists.
  643. Renumbering happens when the sequence have been changed with
  644. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  645. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  646. :group 'org-plain-lists
  647. :type 'boolean)
  648. (defcustom org-provide-checkbox-statistics t
  649. "Non-nil means, update checkbox statistics after insert and toggle.
  650. When this is set, checkbox statistics is updated each time you either insert
  651. a new checkbox with \\[org-insert-todo-heading] or toggle a checkbox
  652. with \\[org-ctrl-c-ctrl-c\\]."
  653. :group 'org-plain-lists
  654. :type 'boolean)
  655. (defgroup org-imenu-and-speedbar nil
  656. "Options concerning imenu and speedbar in Org-mode."
  657. :tag "Org Imenu and Speedbar"
  658. :group 'org-structure)
  659. (defcustom org-imenu-depth 2
  660. "The maximum level for Imenu access to Org-mode headlines.
  661. This also applied for speedbar access."
  662. :group 'org-imenu-and-speedbar
  663. :type 'number)
  664. (defgroup org-table nil
  665. "Options concerning tables in Org-mode."
  666. :tag "Org Table"
  667. :group 'org)
  668. (defcustom org-enable-table-editor 'optimized
  669. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  670. When nil, such lines will be treated like ordinary lines.
  671. When equal to the symbol `optimized', the table editor will be optimized to
  672. do the following:
  673. - Automatic overwrite mode in front of whitespace in table fields.
  674. This makes the structure of the table stay in tact as long as the edited
  675. field does not exceed the column width.
  676. - Minimize the number of realigns. Normally, the table is aligned each time
  677. TAB or RET are pressed to move to another field. With optimization this
  678. happens only if changes to a field might have changed the column width.
  679. Optimization requires replacing the functions `self-insert-command',
  680. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  681. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  682. very good at guessing when a re-align will be necessary, but you can always
  683. force one with \\[org-ctrl-c-ctrl-c].
  684. If you would like to use the optimized version in Org-mode, but the
  685. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  686. This variable can be used to turn on and off the table editor during a session,
  687. but in order to toggle optimization, a restart is required.
  688. See also the variable `org-table-auto-blank-field'."
  689. :group 'org-table
  690. :type '(choice
  691. (const :tag "off" nil)
  692. (const :tag "on" t)
  693. (const :tag "on, optimized" optimized)))
  694. (defcustom org-table-tab-recognizes-table.el t
  695. "Non-nil means, TAB will automatically notice a table.el table.
  696. When it sees such a table, it moves point into it and - if necessary -
  697. calls `table-recognize-table'."
  698. :group 'org-table-editing
  699. :type 'boolean)
  700. (defgroup org-link nil
  701. "Options concerning links in Org-mode."
  702. :tag "Org Link"
  703. :group 'org)
  704. (defvar org-link-abbrev-alist-local nil
  705. "Buffer-local version of `org-link-abbrev-alist', which see.
  706. The value of this is taken from the #+LINK lines.")
  707. (make-variable-buffer-local 'org-link-abbrev-alist-local)
  708. (defcustom org-link-abbrev-alist nil
  709. "Alist of link abbreviations.
  710. The car of each element is a string, to be replaced at the start of a link.
  711. The cdrs are replacement values, like (\"linkkey\" . REPLACE). Abbreviated
  712. links in Org-mode buffers can have an optional tag after a double colon, e.g.
  713. [[linkkey:tag][description]]
  714. If REPLACE is a string, the tag will simply be appended to create the link.
  715. If the string contains \"%s\", the tag will be inserted there.
  716. REPLACE may also be a function that will be called with the tag as the
  717. only argument to create the link, which should be returned as a string.
  718. See the manual for examples."
  719. :group 'org-link
  720. :type 'alist)
  721. (defcustom org-descriptive-links t
  722. "Non-nil means, hide link part and only show description of bracket links.
  723. Bracket links are like [[link][descritpion]]. This variable sets the initial
  724. state in new org-mode buffers. The setting can then be toggled on a
  725. per-buffer basis from the Org->Hyperlinks menu."
  726. :group 'org-link
  727. :type 'boolean)
  728. (defcustom org-link-file-path-type 'adaptive
  729. "How the path name in file links should be stored.
  730. Valid values are:
  731. relative Relative to the current directory, i.e. the directory of the file
  732. into which the link is being inserted.
  733. absolute Absolute path, if possible with ~ for home directory.
  734. noabbrev Absolute path, no abbreviation of home directory.
  735. adaptive Use relative path for files in the current directory and sub-
  736. directories of it. For other files, use an absolute path."
  737. :group 'org-link
  738. :type '(choice
  739. (const relative)
  740. (const absolute)
  741. (const noabbrev)
  742. (const adaptive)))
  743. (defcustom org-activate-links '(bracket angle plain radio tag date)
  744. "Types of links that should be activated in Org-mode files.
  745. This is a list of symbols, each leading to the activation of a certain link
  746. type. In principle, it does not hurt to turn on most link types - there may
  747. be a small gain when turning off unused link types. The types are:
  748. bracket The recommended [[link][description]] or [[link]] links with hiding.
  749. angular Links in angular brackes that may contain whitespace like
  750. <bbdb:Carsten Dominik>.
  751. plain Plain links in normal text, no whitespace, like http://google.com.
  752. radio Text that is matched by a radio target, see manual for details.
  753. tag Tag settings in a headline (link to tag search).
  754. date Time stamps (link to calendar).
  755. Changing this variable requires a restart of Emacs to become effective."
  756. :group 'org-link
  757. :type '(set (const :tag "Double bracket links (new style)" bracket)
  758. (const :tag "Angular bracket links (old style)" angular)
  759. (const :tag "Plain text links" plain)
  760. (const :tag "Radio target matches" radio)
  761. (const :tag "Tags" tag)
  762. (const :tag "Timestamps" date)))
  763. (defcustom org-make-link-description-function nil
  764. "Function to use to generate link descriptions from links. If
  765. nil the link location will be used. This function must take two
  766. parameters; the first is the link and the second the description
  767. org-insert-link has generated, and should return the description
  768. to use."
  769. :group 'org-link
  770. :type 'function)
  771. (defgroup org-link-store nil
  772. "Options concerning storing links in Org-mode."
  773. :tag "Org Store Link"
  774. :group 'org-link)
  775. (defcustom org-email-link-description-format "Email %c: %.30s"
  776. "Format of the description part of a link to an email or usenet message.
  777. The following %-excapes will be replaced by corresponding information:
  778. %F full \"From\" field
  779. %f name, taken from \"From\" field, address if no name
  780. %T full \"To\" field
  781. %t first name in \"To\" field, address if no name
  782. %c correspondent. Unually \"from NAME\", but if you sent it yourself, it
  783. will be \"to NAME\". See also the variable `org-from-is-user-regexp'.
  784. %s subject
  785. %m message-id.
  786. You may use normal field width specification between the % and the letter.
  787. This is for example useful to limit the length of the subject.
  788. Examples: \"%f on: %.30s\", \"Email from %f\", \"Email %c\""
  789. :group 'org-link-store
  790. :type 'string)
  791. (defcustom org-from-is-user-regexp
  792. (let (r1 r2)
  793. (when (and user-mail-address (not (string= user-mail-address "")))
  794. (setq r1 (concat "\\<" (regexp-quote user-mail-address) "\\>")))
  795. (when (and user-full-name (not (string= user-full-name "")))
  796. (setq r2 (concat "\\<" (regexp-quote user-full-name) "\\>")))
  797. (if (and r1 r2) (concat r1 "\\|" r2) (or r1 r2)))
  798. "Regexp mached against the \"From:\" header of an email or usenet message.
  799. It should match if the message is from the user him/herself."
  800. :group 'org-link-store
  801. :type 'regexp)
  802. (defcustom org-context-in-file-links t
  803. "Non-nil means, file links from `org-store-link' contain context.
  804. A search string will be added to the file name with :: as separator and
  805. used to find the context when the link is activated by the command
  806. `org-open-at-point'.
  807. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  808. negates this setting for the duration of the command."
  809. :group 'org-link-store
  810. :type 'boolean)
  811. (defcustom org-keep-stored-link-after-insertion nil
  812. "Non-nil means, keep link in list for entire session.
  813. The command `org-store-link' adds a link pointing to the current
  814. location to an internal list. These links accumulate during a session.
  815. The command `org-insert-link' can be used to insert links into any
  816. Org-mode file (offering completion for all stored links). When this
  817. option is nil, every link which has been inserted once using \\[org-insert-link]
  818. will be removed from the list, to make completing the unused links
  819. more efficient."
  820. :group 'org-link-store
  821. :type 'boolean)
  822. (defgroup org-link-follow nil
  823. "Options concerning following links in Org-mode."
  824. :tag "Org Follow Link"
  825. :group 'org-link)
  826. (defcustom org-follow-link-hook nil
  827. "Hook that is run after a link has been followed."
  828. :group 'org-link-follow
  829. :type 'hook)
  830. (defcustom org-tab-follows-link nil
  831. "Non-nil means, on links TAB will follow the link.
  832. Needs to be set before org.el is loaded."
  833. :group 'org-link-follow
  834. :type 'boolean)
  835. (defcustom org-return-follows-link nil
  836. "Non-nil means, on links RET will follow the link.
  837. Needs to be set before org.el is loaded."
  838. :group 'org-link-follow
  839. :type 'boolean)
  840. (defcustom org-mouse-1-follows-link
  841. (if (boundp 'mouse-1-click-follows-link) mouse-1-click-follows-link t)
  842. "Non-nil means, mouse-1 on a link will follow the link.
  843. A longer mouse click will still set point. Does not work on XEmacs.
  844. Needs to be set before org.el is loaded."
  845. :group 'org-link-follow
  846. :type 'boolean)
  847. (defcustom org-mark-ring-length 4
  848. "Number of different positions to be recorded in the ring
  849. Changing this requires a restart of Emacs to work correctly."
  850. :group 'org-link-follow
  851. :type 'interger)
  852. (defcustom org-link-frame-setup
  853. '((vm . vm-visit-folder-other-frame)
  854. (gnus . gnus-other-frame)
  855. (file . find-file-other-window))
  856. "Setup the frame configuration for following links.
  857. When following a link with Emacs, it may often be useful to display
  858. this link in another window or frame. This variable can be used to
  859. set this up for the different types of links.
  860. For VM, use any of
  861. `vm-visit-folder'
  862. `vm-visit-folder-other-frame'
  863. For Gnus, use any of
  864. `gnus'
  865. `gnus-other-frame'
  866. For FILE, use any of
  867. `find-file'
  868. `find-file-other-window'
  869. `find-file-other-frame'
  870. For the calendar, use the variable `calendar-setup'.
  871. For BBDB, it is currently only possible to display the matches in
  872. another window."
  873. :group 'org-link-follow
  874. :type '(list
  875. (cons (const vm)
  876. (choice
  877. (const vm-visit-folder)
  878. (const vm-visit-folder-other-window)
  879. (const vm-visit-folder-other-frame)))
  880. (cons (const gnus)
  881. (choice
  882. (const gnus)
  883. (const gnus-other-frame)))
  884. (cons (const file)
  885. (choice
  886. (const find-file)
  887. (const find-file-other-window)
  888. (const find-file-other-frame)))))
  889. (defcustom org-display-internal-link-with-indirect-buffer nil
  890. "Non-nil means, use indirect buffer to display infile links.
  891. Activating internal links (from one location in a file to another location
  892. in the same file) normally just jumps to the location. When the link is
  893. activated with a C-u prefix (or with mouse-3), the link is displayed in
  894. another window. When this option is set, the other window actually displays
  895. an indirect buffer clone of the current buffer, to avoid any visibility
  896. changes to the current buffer."
  897. :group 'org-link-follow
  898. :type 'boolean)
  899. (defcustom org-open-non-existing-files nil
  900. "Non-nil means, `org-open-file' will open non-existing files.
  901. When nil, an error will be generated."
  902. :group 'org-link-follow
  903. :type 'boolean)
  904. (defcustom org-link-mailto-program '(browse-url "mailto:%a?subject=%s")
  905. "Function and arguments to call for following mailto links.
  906. This is a list with the first element being a lisp function, and the
  907. remaining elements being arguments to the function. In string arguments,
  908. %a will be replaced by the address, and %s will be replaced by the subject
  909. if one was given like in <mailto:arthur@galaxy.org::this subject>."
  910. :group 'org-link-follow
  911. :type '(choice
  912. (const :tag "browse-url" (browse-url-mail "mailto:%a?subject=%s"))
  913. (const :tag "compose-mail" (compose-mail "%a" "%s"))
  914. (const :tag "message-mail" (message-mail "%a" "%s"))
  915. (cons :tag "other" (function) (repeat :tag "argument" sexp))))
  916. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  917. "Non-nil means, ask for confirmation before executing shell links.
  918. Shell links can be dangerous: just think about a link
  919. [[shell:rm -rf ~/*][Google Search]]
  920. This link would show up in your Org-mode document as \"Google Search\",
  921. but really it would remove your entire home directory.
  922. Therefore we advise against setting this variable to nil.
  923. Just change it to `y-or-n-p' of you want to confirm with a
  924. single keystroke rather than having to type \"yes\"."
  925. :group 'org-link-follow
  926. :type '(choice
  927. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  928. (const :tag "with y-or-n (faster)" y-or-n-p)
  929. (const :tag "no confirmation (dangerous)" nil)))
  930. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  931. "Non-nil means, ask for confirmation before executing Emacs Lisp links.
  932. Elisp links can be dangerous: just think about a link
  933. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  934. This link would show up in your Org-mode document as \"Google Search\",
  935. but really it would remove your entire home directory.
  936. Therefore we advise against setting this variable to nil.
  937. Just change it to `y-or-n-p' of you want to confirm with a
  938. single keystroke rather than having to type \"yes\"."
  939. :group 'org-link-follow
  940. :type '(choice
  941. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  942. (const :tag "with y-or-n (faster)" y-or-n-p)
  943. (const :tag "no confirmation (dangerous)" nil)))
  944. (defconst org-file-apps-defaults-gnu
  945. '((remote . emacs)
  946. (t . mailcap))
  947. "Default file applications on a UNIX or GNU/Linux system.
  948. See `org-file-apps'.")
  949. (defconst org-file-apps-defaults-macosx
  950. '((remote . emacs)
  951. (t . "open %s")
  952. ("ps" . "gv %s")
  953. ("ps.gz" . "gv %s")
  954. ("eps" . "gv %s")
  955. ("eps.gz" . "gv %s")
  956. ("dvi" . "xdvi %s")
  957. ("fig" . "xfig %s"))
  958. "Default file applications on a MacOS X system.
  959. The system \"open\" is known as a default, but we use X11 applications
  960. for some files for which the OS does not have a good default.
  961. See `org-file-apps'.")
  962. (defconst org-file-apps-defaults-windowsnt
  963. (list
  964. '(remote . emacs)
  965. (cons t
  966. (list (if (featurep 'xemacs)
  967. 'mswindows-shell-execute
  968. 'w32-shell-execute)
  969. "open" 'file)))
  970. "Default file applications on a Windows NT system.
  971. The system \"open\" is used for most files.
  972. See `org-file-apps'.")
  973. (defcustom org-file-apps
  974. '(
  975. ("txt" . emacs)
  976. ("tex" . emacs)
  977. ("ltx" . emacs)
  978. ("org" . emacs)
  979. ("el" . emacs)
  980. ("bib" . emacs)
  981. )
  982. "External applications for opening `file:path' items in a document.
  983. Org-mode uses system defaults for different file types, but
  984. you can use this variable to set the application for a given file
  985. extension. The entries in this list are cons cells where the car identifies
  986. files and the cdr the corresponding command. Possible values for the
  987. file identifier are
  988. \"ext\" A string identifying an extension
  989. `directory' Matches a directory
  990. `remote' Matches a remote file, accessible through tramp or efs.
  991. Remote files most likely should be visited through Emacs
  992. because external applications cannot handle such paths.
  993. t Default for all remaining files
  994. Possible values for the command are:
  995. `emacs' The file will be visited by the current Emacs process.
  996. `default' Use the default application for this file type.
  997. string A command to be executed by a shell; %s will be replaced
  998. by the path to the file.
  999. sexp A Lisp form which will be evaluated. The file path will
  1000. be available in the Lisp variable `file'.
  1001. For more examples, see the system specific constants
  1002. `org-file-apps-defaults-macosx'
  1003. `org-file-apps-defaults-windowsnt'
  1004. `org-file-apps-defaults-gnu'."
  1005. :group 'org-link-follow
  1006. :type '(repeat
  1007. (cons (choice :value ""
  1008. (string :tag "Extension")
  1009. (const :tag "Default for unrecognized files" t)
  1010. (const :tag "Remote file" remote)
  1011. (const :tag "Links to a directory" directory))
  1012. (choice :value ""
  1013. (const :tag "Visit with Emacs" emacs)
  1014. (const :tag "Use system default" default)
  1015. (string :tag "Command")
  1016. (sexp :tag "Lisp form")))))
  1017. (defgroup org-refile nil
  1018. "Options concerning refiling entries in Org-mode."
  1019. :tag "Org Remember"
  1020. :group 'org)
  1021. (defcustom org-directory "~/org"
  1022. "Directory with org files.
  1023. This directory will be used as default to prompt for org files.
  1024. Used by the hooks for remember.el."
  1025. :group 'org-refile
  1026. :group 'org-remember
  1027. :type 'directory)
  1028. (defcustom org-default-notes-file "~/.notes"
  1029. "Default target for storing notes.
  1030. Used by the hooks for remember.el. This can be a string, or nil to mean
  1031. the value of `remember-data-file'.
  1032. You can set this on a per-template basis with the variable
  1033. `org-remember-templates'."
  1034. :group 'org-refile
  1035. :group 'org-remember
  1036. :type '(choice
  1037. (const :tag "Default from remember-data-file" nil)
  1038. file))
  1039. (defcustom org-goto-interface 'outline
  1040. "The default interface to be used for `org-goto'.
  1041. Allowed vaues are:
  1042. outline The interface shows an outline of the relevant file
  1043. and the correct heading is found by moving through
  1044. the outline or by searching with incremental search.
  1045. outline-path-completion Headlines in the current buffer are offered via
  1046. completion."
  1047. :group 'org-refile
  1048. :type '(choice
  1049. (const :tag "Outline" outline)
  1050. (const :tag "Outline-path-completion" outline-path-completion)))
  1051. (defcustom org-reverse-note-order nil
  1052. "Non-nil means, store new notes at the beginning of a file or entry.
  1053. When nil, new notes will be filed to the end of a file or entry.
  1054. This can also be a list with cons cells of regular expressions that
  1055. are matched against file names, and values."
  1056. :group 'org-remember
  1057. :type '(choice
  1058. (const :tag "Reverse always" t)
  1059. (const :tag "Reverse never" nil)
  1060. (repeat :tag "By file name regexp"
  1061. (cons regexp boolean))))
  1062. (defcustom org-refile-targets nil
  1063. "Targets for refiling entries with \\[org-refile].
  1064. This is list of cons cells. Each cell contains:
  1065. - a specification of the files to be considered, either a list of files,
  1066. or a symbol whose function or variable value will be used to retrieve
  1067. a file name or a list of file names. Nil means, refile to a different
  1068. heading in the current buffer.
  1069. - A specification of how to find candidate refile targets. This may be
  1070. any of
  1071. - a cons cell (:tag . \"TAG\") to identify refile targets by a tag.
  1072. This tag has to be present in all target headlines, inheritance will
  1073. not be considered.
  1074. - a cons cell (:todo . \"KEYWORD\") to identify refile targets by
  1075. todo keyword.
  1076. - a cons cell (:regexp . \"REGEXP\") with a regular expression matching
  1077. headlines that are refiling targets.
  1078. - a cons cell (:level . N). Any headline of level N is considered a target.
  1079. - a cons cell (:maxlevel . N). Any headline with level <= N is a target."
  1080. :group 'org-remember
  1081. :type '(repeat
  1082. (cons
  1083. (choice :value org-agenda-files
  1084. (const :tag "All agenda files" org-agenda-files)
  1085. (const :tag "Current buffer" nil)
  1086. (function) (variable) (file))
  1087. (choice :tag "Identify target headline by"
  1088. (cons :tag "Specific tag" (const :tag) (string))
  1089. (cons :tag "TODO keyword" (const :todo) (string))
  1090. (cons :tag "Regular expression" (const :regexp) (regexp))
  1091. (cons :tag "Level number" (const :level) (integer))
  1092. (cons :tag "Max Level number" (const :maxlevel) (integer))))))
  1093. (defcustom org-refile-use-outline-path nil
  1094. "Non-nil means, provide refile targets as paths.
  1095. So a level 3 headline will be available as level1/level2/level3.
  1096. When the value is `file', also include the file name (without directory)
  1097. into the path. When `full-file-path', include the full file path."
  1098. :group 'org-remember
  1099. :type '(choice
  1100. (const :tag "Not" nil)
  1101. (const :tag "Yes" t)
  1102. (const :tag "Start with file name" file)
  1103. (const :tag "Start with full file path" full-file-path)))
  1104. (defgroup org-todo nil
  1105. "Options concerning TODO items in Org-mode."
  1106. :tag "Org TODO"
  1107. :group 'org)
  1108. (defgroup org-progress nil
  1109. "Options concerning Progress logging in Org-mode."
  1110. :tag "Org Progress"
  1111. :group 'org-time)
  1112. (defcustom org-todo-keywords '((sequence "TODO" "DONE"))
  1113. "List of TODO entry keyword sequences and their interpretation.
  1114. \\<org-mode-map>This is a list of sequences.
  1115. Each sequence starts with a symbol, either `sequence' or `type',
  1116. indicating if the keywords should be interpreted as a sequence of
  1117. action steps, or as different types of TODO items. The first
  1118. keywords are states requiring action - these states will select a headline
  1119. for inclusion into the global TODO list Org-mode produces. If one of
  1120. the \"keywords\" is the vertical bat \"|\" the remaining keywords
  1121. signify that no further action is necessary. If \"|\" is not found,
  1122. the last keyword is treated as the only DONE state of the sequence.
  1123. The command \\[org-todo] cycles an entry through these states, and one
  1124. additional state where no keyword is present. For details about this
  1125. cycling, see the manual.
  1126. TODO keywords and interpretation can also be set on a per-file basis with
  1127. the special #+SEQ_TODO and #+TYP_TODO lines.
  1128. Each keyword can optionally specify a character for fast state selection
  1129. \(in combination with the variable `org-use-fast-todo-selection')
  1130. and specifiers for state change logging, using the same syntax
  1131. that is used in the \"#+TODO:\" lines. For example, \"WAIT(w)\" says
  1132. that the WAIT state can be selected with the \"w\" key. \"WAIT(w!)\"
  1133. indicates to record a time stamp each time this state is selected.
  1134. Each keyword may also specify if a timestamp or a note should be
  1135. recorded when entering or leaving the state, by adding additional
  1136. characters in the parenthesis after the keyword. This looks like this:
  1137. \"WAIT(w@/!)\". \"@\" means to add a note (with time), \"!\" means to
  1138. record only the time of the state change. With X and Y being either
  1139. \"@\" or \"!\", \"X/Y\" means use X when entering the state, and use
  1140. Y when leaving the state if and only if the *target* state does not
  1141. define X. You may omit any of the fast-selection key or X or /Y,
  1142. so WAIT(w@), WAIT(w/@) and WAIT(@/@) are all valid.
  1143. For backward compatibility, this variable may also be just a list
  1144. of keywords - in this case the interptetation (sequence or type) will be
  1145. taken from the (otherwise obsolete) variable `org-todo-interpretation'."
  1146. :group 'org-todo
  1147. :group 'org-keywords
  1148. :type '(choice
  1149. (repeat :tag "Old syntax, just keywords"
  1150. (string :tag "Keyword"))
  1151. (repeat :tag "New syntax"
  1152. (cons
  1153. (choice
  1154. :tag "Interpretation"
  1155. (const :tag "Sequence (cycling hits every state)" sequence)
  1156. (const :tag "Type (cycling directly to DONE)" type))
  1157. (repeat
  1158. (string :tag "Keyword"))))))
  1159. (defvar org-todo-keywords-1 nil
  1160. "All TODO and DONE keywords active in a buffer.")
  1161. (make-variable-buffer-local 'org-todo-keywords-1)
  1162. (defvar org-todo-keywords-for-agenda nil)
  1163. (defvar org-done-keywords-for-agenda nil)
  1164. (defvar org-agenda-contributing-files nil)
  1165. (defvar org-not-done-keywords nil)
  1166. (make-variable-buffer-local 'org-not-done-keywords)
  1167. (defvar org-done-keywords nil)
  1168. (make-variable-buffer-local 'org-done-keywords)
  1169. (defvar org-todo-heads nil)
  1170. (make-variable-buffer-local 'org-todo-heads)
  1171. (defvar org-todo-sets nil)
  1172. (make-variable-buffer-local 'org-todo-sets)
  1173. (defvar org-todo-log-states nil)
  1174. (make-variable-buffer-local 'org-todo-log-states)
  1175. (defvar org-todo-kwd-alist nil)
  1176. (make-variable-buffer-local 'org-todo-kwd-alist)
  1177. (defvar org-todo-key-alist nil)
  1178. (make-variable-buffer-local 'org-todo-key-alist)
  1179. (defvar org-todo-key-trigger nil)
  1180. (make-variable-buffer-local 'org-todo-key-trigger)
  1181. (defcustom org-todo-interpretation 'sequence
  1182. "Controls how TODO keywords are interpreted.
  1183. This variable is in principle obsolete and is only used for
  1184. backward compatibility, if the interpretation of todo keywords is
  1185. not given already in `org-todo-keywords'. See that variable for
  1186. more information."
  1187. :group 'org-todo
  1188. :group 'org-keywords
  1189. :type '(choice (const sequence)
  1190. (const type)))
  1191. (defcustom org-use-fast-todo-selection 'prefix
  1192. "Non-nil means, use the fast todo selection scheme with C-c C-t.
  1193. This variable describes if and under what circumstances the cycling
  1194. mechanism for TODO keywords will be replaced by a single-key, direct
  1195. selection scheme.
  1196. When nil, fast selection is never used.
  1197. When the symbol `prefix', it will be used when `org-todo' is called with
  1198. a prefix argument, i.e. `C-u C-c C-t' in an Org-mode buffer, and `C-u t'
  1199. in an agenda buffer.
  1200. When t, fast selection is used by default. In this case, the prefix
  1201. argument forces cycling instead.
  1202. In all cases, the special interface is only used if access keys have actually
  1203. been assigned by the user, i.e. if keywords in the configuration are followed
  1204. by a letter in parenthesis, like TODO(t)."
  1205. :group 'org-todo
  1206. :type '(choice
  1207. (const :tag "Never" nil)
  1208. (const :tag "By default" t)
  1209. (const :tag "Only with C-u C-c C-t" prefix)))
  1210. (defcustom org-after-todo-state-change-hook nil
  1211. "Hook which is run after the state of a TODO item was changed.
  1212. The new state (a string with a TODO keyword, or nil) is available in the
  1213. Lisp variable `state'."
  1214. :group 'org-todo
  1215. :type 'hook)
  1216. (defcustom org-log-done nil
  1217. "Non-nil means, record a CLOSED timestamp when moving an entry to DONE.
  1218. When equal to the list (done), also prompt for a closing note.
  1219. This can also be configured on a per-file basis by adding one of
  1220. the following lines anywhere in the buffer:
  1221. #+STARTUP: logdone
  1222. #+STARTUP: lognotedone
  1223. #+STARTUP: nologdone"
  1224. :group 'org-todo
  1225. :group 'org-progress
  1226. :type '(choice
  1227. (const :tag "No logging" nil)
  1228. (const :tag "Record CLOSED timestamp" time)
  1229. (const :tag "Record CLOSED timestamp with closing note." note)))
  1230. ;; Normalize old uses of org-log-done.
  1231. (cond
  1232. ((eq org-log-done t) (setq org-log-done 'time))
  1233. ((and (listp org-log-done) (memq 'done org-log-done))
  1234. (setq org-log-done 'note)))
  1235. (defcustom org-log-note-clock-out nil
  1236. "Non-nil means, recored a note when clocking out of an item.
  1237. This can also be configured on a per-file basis by adding one of
  1238. the following lines anywhere in the buffer:
  1239. #+STARTUP: lognoteclock-out
  1240. #+STARTUP: nolognoteclock-out"
  1241. :group 'org-todo
  1242. :group 'org-progress
  1243. :type 'boolean)
  1244. (defcustom org-log-done-with-time t
  1245. "Non-nil means, the CLOSED time stamp will contain date and time.
  1246. When nil, only the date will be recorded."
  1247. :group 'org-progress
  1248. :type 'boolean)
  1249. (defcustom org-log-note-headings
  1250. '((done . "CLOSING NOTE %t")
  1251. (state . "State %-12s %t")
  1252. (note . "Note taken on %t")
  1253. (clock-out . ""))
  1254. "Headings for notes added to entries.
  1255. The value is an alist, with the car being a symbol indicating the note
  1256. context, and the cdr is the heading to be used. The heading may also be the
  1257. empty string.
  1258. %t in the heading will be replaced by a time stamp.
  1259. %s will be replaced by the new TODO state, in double quotes.
  1260. %u will be replaced by the user name.
  1261. %U will be replaced by the full user name."
  1262. :group 'org-todo
  1263. :group 'org-progress
  1264. :type '(list :greedy t
  1265. (cons (const :tag "Heading when closing an item" done) string)
  1266. (cons (const :tag
  1267. "Heading when changing todo state (todo sequence only)"
  1268. state) string)
  1269. (cons (const :tag "Heading when just taking a note" note) string)
  1270. (cons (const :tag "Heading when clocking out" clock-out) string)))
  1271. (unless (assq 'note org-log-note-headings)
  1272. (push '(note . "%t") org-log-note-headings))
  1273. (defcustom org-log-states-order-reversed t
  1274. "Non-nil means, the latest state change note will be directly after heading.
  1275. When nil, the notes will be orderer according to time."
  1276. :group 'org-todo
  1277. :group 'org-progress
  1278. :type 'boolean)
  1279. (defcustom org-log-repeat 'time
  1280. "Non-nil means, record moving through the DONE state when triggering repeat.
  1281. An auto-repeating tasks is immediately switched back to TODO when marked
  1282. done. If you are not logging state changes (by adding \"@\" or \"!\" to
  1283. the TODO keyword definition, or recording a closing note by setting
  1284. `org-log-done', there will be no record of the task moving through DONE.
  1285. This variable forces taking a note anyway. Possible values are:
  1286. nil Don't force a record
  1287. time Record a time stamp
  1288. note Record a note
  1289. This option can also be set with on a per-file-basis with
  1290. #+STARTUP: logrepeat
  1291. #+STARTUP: lognoterepeat
  1292. #+STARTUP: nologrepeat
  1293. You can have local logging settings for a subtree by setting the LOGGING
  1294. property to one or more of these keywords."
  1295. :group 'org-todo
  1296. :group 'org-progress
  1297. :type '(choice
  1298. (const :tag "Don't force a record" nil)
  1299. (const :tag "Force recording the DONE state" time)
  1300. (const :tag "Force recording a note with the DONE state" note)))
  1301. (defgroup org-priorities nil
  1302. "Priorities in Org-mode."
  1303. :tag "Org Priorities"
  1304. :group 'org-todo)
  1305. (defcustom org-highest-priority ?A
  1306. "The highest priority of TODO items. A character like ?A, ?B etc.
  1307. Must have a smaller ASCII number than `org-lowest-priority'."
  1308. :group 'org-priorities
  1309. :type 'character)
  1310. (defcustom org-lowest-priority ?C
  1311. "The lowest priority of TODO items. A character like ?A, ?B etc.
  1312. Must have a larger ASCII number than `org-highest-priority'."
  1313. :group 'org-priorities
  1314. :type 'character)
  1315. (defcustom org-default-priority ?B
  1316. "The default priority of TODO items.
  1317. This is the priority an item get if no explicit priority is given."
  1318. :group 'org-priorities
  1319. :type 'character)
  1320. (defcustom org-priority-start-cycle-with-default t
  1321. "Non-nil means, start with default priority when starting to cycle.
  1322. When this is nil, the first step in the cycle will be (depending on the
  1323. command used) one higher or lower that the default priority."
  1324. :group 'org-priorities
  1325. :type 'boolean)
  1326. (defgroup org-time nil
  1327. "Options concerning time stamps and deadlines in Org-mode."
  1328. :tag "Org Time"
  1329. :group 'org)
  1330. (defcustom org-insert-labeled-timestamps-at-point nil
  1331. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1332. When nil, these labeled time stamps are forces into the second line of an
  1333. entry, just after the headline. When scheduling from the global TODO list,
  1334. the time stamp will always be forced into the second line."
  1335. :group 'org-time
  1336. :type 'boolean)
  1337. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1338. "Formats for `format-time-string' which are used for time stamps.
  1339. It is not recommended to change this constant.")
  1340. (defcustom org-time-stamp-rounding-minutes '(0 5)
  1341. "Number of minutes to round time stamps to.
  1342. These are two values, the first applies when first creating a time stamp.
  1343. The second applies when changing it with the commands `S-up' and `S-down'.
  1344. When changing the time stamp, this means that it will change in steps
  1345. of N minutes, as given by the second value.
  1346. When a setting is 0 or 1, insert the time unmodified. Useful rounding
  1347. numbers should be factors of 60, so for example 5, 10, 15.
  1348. When this is larger than 1, you can still force an exact time-stamp by using
  1349. a double prefix argument to a time-stamp command like `C-c .' or `C-c !',
  1350. and by using a prefix arg to `S-up/down' to specify the exact number
  1351. of minutes to shift."
  1352. :group 'org-time
  1353. :get '(lambda (var) ; Make sure all entries have 5 elements
  1354. (if (integerp (default-value var))
  1355. (list (default-value var) 5)
  1356. (default-value var)))
  1357. :type '(list
  1358. (integer :tag "when inserting times")
  1359. (integer :tag "when modifying times")))
  1360. ;; Normalize old customizations of this variable.
  1361. (when (integerp org-time-stamp-rounding-minutes)
  1362. (setq org-time-stamp-rounding-minutes
  1363. (list org-time-stamp-rounding-minutes
  1364. org-time-stamp-rounding-minutes)))
  1365. (defcustom org-display-custom-times nil
  1366. "Non-nil means, overlay custom formats over all time stamps.
  1367. The formats are defined through the variable `org-time-stamp-custom-formats'.
  1368. To turn this on on a per-file basis, insert anywhere in the file:
  1369. #+STARTUP: customtime"
  1370. :group 'org-time
  1371. :set 'set-default
  1372. :type 'sexp)
  1373. (make-variable-buffer-local 'org-display-custom-times)
  1374. (defcustom org-time-stamp-custom-formats
  1375. '("<%m/%d/%y %a>" . "<%m/%d/%y %a %H:%M>") ; american
  1376. "Custom formats for time stamps. See `format-time-string' for the syntax.
  1377. These are overlayed over the default ISO format if the variable
  1378. `org-display-custom-times' is set. Time like %H:%M should be at the
  1379. end of the second format."
  1380. :group 'org-time
  1381. :type 'sexp)
  1382. (defun org-time-stamp-format (&optional long inactive)
  1383. "Get the right format for a time string."
  1384. (let ((f (if long (cdr org-time-stamp-formats)
  1385. (car org-time-stamp-formats))))
  1386. (if inactive
  1387. (concat "[" (substring f 1 -1) "]")
  1388. f)))
  1389. (defcustom org-deadline-warning-days 14
  1390. "No. of days before expiration during which a deadline becomes active.
  1391. This variable governs the display in sparse trees and in the agenda.
  1392. When 0 or negative, it means use this number (the absolute value of it)
  1393. even if a deadline has a different individual lead time specified."
  1394. :group 'org-time
  1395. :group 'org-agenda-daily/weekly
  1396. :type 'number)
  1397. (defcustom org-read-date-prefer-future t
  1398. "Non-nil means, assume future for incomplete date input from user.
  1399. This affects the following situations:
  1400. 1. The user gives a day, but no month.
  1401. For example, if today is the 15th, and you enter \"3\", Org-mode will
  1402. read this as the third of *next* month. However, if you enter \"17\",
  1403. it will be considered as *this* month.
  1404. 2. The user gives a month but not a year.
  1405. For example, if it is april and you enter \"feb 2\", this will be read
  1406. as feb 2, *next* year. \"May 5\", however, will be this year.
  1407. Currently this does not work for ISO week specifications.
  1408. When this option is nil, the current month and year will always be used
  1409. as defaults."
  1410. :group 'org-time
  1411. :type 'boolean)
  1412. (defcustom org-read-date-display-live t
  1413. "Non-nil means, display current interpretation of date prompt live.
  1414. This display will be in an overlay, in the minibuffer."
  1415. :group 'org-time
  1416. :type 'boolean)
  1417. (defcustom org-read-date-popup-calendar t
  1418. "Non-nil means, pop up a calendar when prompting for a date.
  1419. In the calendar, the date can be selected with mouse-1. However, the
  1420. minibuffer will also be active, and you can simply enter the date as well.
  1421. When nil, only the minibuffer will be available."
  1422. :group 'org-time
  1423. :type 'boolean)
  1424. (if (fboundp 'defvaralias)
  1425. (defvaralias 'org-popup-calendar-for-date-prompt
  1426. 'org-read-date-popup-calendar))
  1427. (defcustom org-extend-today-until 0
  1428. "The hour when your day really ends.
  1429. This has influence for the following applications:
  1430. - When switching the agenda to \"today\". It it is still earlier than
  1431. the time given here, the day recognized as TODAY is actually yesterday.
  1432. - When a date is read from the user and it is still before the time given
  1433. here, the current date and time will be assumed to be yesterday, 23:59.
  1434. FIXME:
  1435. IMPORTANT: This is still a very experimental feature, it may disappear
  1436. again or it may be extended to mean more things."
  1437. :group 'org-time
  1438. :type 'number)
  1439. (defcustom org-edit-timestamp-down-means-later nil
  1440. "Non-nil means, S-down will increase the time in a time stamp.
  1441. When nil, S-up will increase."
  1442. :group 'org-time
  1443. :type 'boolean)
  1444. (defcustom org-calendar-follow-timestamp-change t
  1445. "Non-nil means, make the calendar window follow timestamp changes.
  1446. When a timestamp is modified and the calendar window is visible, it will be
  1447. moved to the new date."
  1448. :group 'org-time
  1449. :type 'boolean)
  1450. (defgroup org-tags nil
  1451. "Options concerning tags in Org-mode."
  1452. :tag "Org Tags"
  1453. :group 'org)
  1454. (defcustom org-tag-alist nil
  1455. "List of tags allowed in Org-mode files.
  1456. When this list is nil, Org-mode will base TAG input on what is already in the
  1457. buffer.
  1458. The value of this variable is an alist, the car of each entry must be a
  1459. keyword as a string, the cdr may be a character that is used to select
  1460. that tag through the fast-tag-selection interface.
  1461. See the manual for details."
  1462. :group 'org-tags
  1463. :type '(repeat
  1464. (choice
  1465. (cons (string :tag "Tag name")
  1466. (character :tag "Access char"))
  1467. (const :tag "Start radio group" (:startgroup))
  1468. (const :tag "End radio group" (:endgroup)))))
  1469. (defcustom org-use-fast-tag-selection 'auto
  1470. "Non-nil means, use fast tag selection scheme.
  1471. This is a special interface to select and deselect tags with single keys.
  1472. When nil, fast selection is never used.
  1473. When the symbol `auto', fast selection is used if and only if selection
  1474. characters for tags have been configured, either through the variable
  1475. `org-tag-alist' or through a #+TAGS line in the buffer.
  1476. When t, fast selection is always used and selection keys are assigned
  1477. automatically if necessary."
  1478. :group 'org-tags
  1479. :type '(choice
  1480. (const :tag "Always" t)
  1481. (const :tag "Never" nil)
  1482. (const :tag "When selection characters are configured" 'auto)))
  1483. (defcustom org-fast-tag-selection-single-key nil
  1484. "Non-nil means, fast tag selection exits after first change.
  1485. When nil, you have to press RET to exit it.
  1486. During fast tag selection, you can toggle this flag with `C-c'.
  1487. This variable can also have the value `expert'. In this case, the window
  1488. displaying the tags menu is not even shown, until you press C-c again."
  1489. :group 'org-tags
  1490. :type '(choice
  1491. (const :tag "No" nil)
  1492. (const :tag "Yes" t)
  1493. (const :tag "Expert" expert)))
  1494. (defvar org-fast-tag-selection-include-todo nil
  1495. "Non-nil means, fast tags selection interface will also offer TODO states.
  1496. This is an undocumented feature, you should not rely on it.")
  1497. (defcustom org-tags-column (if (featurep 'xemacs) -79 -80)
  1498. "The column to which tags should be indented in a headline.
  1499. If this number is positive, it specifies the column. If it is negative,
  1500. it means that the tags should be flushright to that column. For example,
  1501. -80 works well for a normal 80 character screen."
  1502. :group 'org-tags
  1503. :type 'integer)
  1504. (defcustom org-auto-align-tags t
  1505. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1506. These operations change the length of a headline and therefore shift
  1507. the tags around. With this options turned on, after each such operation
  1508. the tags are again aligned to `org-tags-column'."
  1509. :group 'org-tags
  1510. :type 'boolean)
  1511. (defcustom org-use-tag-inheritance t
  1512. "Non-nil means, tags in levels apply also for sublevels.
  1513. When nil, only the tags directly given in a specific line apply there.
  1514. If you turn off this option, you very likely want to turn on the
  1515. companion option `org-tags-match-list-sublevels'.
  1516. This may also be a list of tags that should be inherited, or a regexp that
  1517. matches tags that should be inherited."
  1518. :group 'org-tags
  1519. :type '(choice
  1520. (const :tag "Not" nil)
  1521. (const :tag "Always" t)
  1522. (repeat :tag "Specific tags" (string :tag "Tag"))
  1523. (regexp :tag "Tags matched by regexp")))
  1524. (defun org-tag-inherit-p (tag)
  1525. "Check if TAG is one that should be inherited."
  1526. (cond
  1527. ((eq org-use-tag-inheritance t) t)
  1528. ((not org-use-tag-inheritance) nil)
  1529. ((stringp org-use-tag-inheritance)
  1530. (string-match org-use-tag-inheritance tag))
  1531. ((listp org-use-tag-inheritance)
  1532. (member tag org-use-tag-inheritance))
  1533. (t (error "Invalid setting of `org-use-tag-inheritance'"))))
  1534. (defcustom org-tags-match-list-sublevels nil
  1535. "Non-nil means list also sublevels of headlines matching tag search.
  1536. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1537. the sublevels of a headline matching a tag search often also match
  1538. the same search. Listing all of them can create very long lists.
  1539. Setting this variable to nil causes subtrees of a match to be skipped.
  1540. This option is off by default, because inheritance in on. If you turn
  1541. inheritance off, you very likely want to turn this option on.
  1542. As a special case, if the tag search is restricted to TODO items, the
  1543. value of this variable is ignored and sublevels are always checked, to
  1544. make sure all corresponding TODO items find their way into the list."
  1545. :group 'org-tags
  1546. :type 'boolean)
  1547. (defvar org-tags-history nil
  1548. "History of minibuffer reads for tags.")
  1549. (defvar org-last-tags-completion-table nil
  1550. "The last used completion table for tags.")
  1551. (defvar org-after-tags-change-hook nil
  1552. "Hook that is run after the tags in a line have changed.")
  1553. (defgroup org-properties nil
  1554. "Options concerning properties in Org-mode."
  1555. :tag "Org Properties"
  1556. :group 'org)
  1557. (defcustom org-property-format "%-10s %s"
  1558. "How property key/value pairs should be formatted by `indent-line'.
  1559. When `indent-line' hits a property definition, it will format the line
  1560. according to this format, mainly to make sure that the values are
  1561. lined-up with respect to each other."
  1562. :group 'org-properties
  1563. :type 'string)
  1564. (defcustom org-use-property-inheritance nil
  1565. "Non-nil means, properties apply also for sublevels.
  1566. This setting is chiefly used during property searches. Turning it on can
  1567. cause significant overhead when doing a search, which is why it is not
  1568. on by default.
  1569. When nil, only the properties directly given in the current entry count.
  1570. When t, every property is inherited. The value may also be a list of
  1571. properties that should have inheritance, or a regular expression matching
  1572. properties that should be inherited.
  1573. However, note that some special properties use inheritance under special
  1574. circumstances (not in searches). Examples are CATEGORY, ARCHIVE, COLUMNS,
  1575. and the properties ending in \"_ALL\" when they are used as descriptor
  1576. for valid values of a property.
  1577. Note for programmers:
  1578. When querying an entry with `org-entry-get', you can control if inheritance
  1579. should be used. By default, `org-entry-get' looks only at the local
  1580. properties. You can request inheritance by setting the inherit argument
  1581. to t (to force inheritance) or to `selective' (to respect the setting
  1582. in this variable)."
  1583. :group 'org-properties
  1584. :type '(choice
  1585. (const :tag "Not" nil)
  1586. (const :tag "Always" t)
  1587. (repeat :tag "Specific properties" (string :tag "Property"))
  1588. (regexp :tag "Properties matched by regexp")))
  1589. (defun org-property-inherit-p (property)
  1590. "Check if PROPERTY is one that should be inherited."
  1591. (cond
  1592. ((eq org-use-property-inheritance t) t)
  1593. ((not org-use-property-inheritance) nil)
  1594. ((stringp org-use-property-inheritance)
  1595. (string-match org-use-property-inheritance property))
  1596. ((listp org-use-property-inheritance)
  1597. (member property org-use-property-inheritance))
  1598. (t (error "Invalid setting of `org-use-property-inheritance'"))))
  1599. (defcustom org-columns-default-format "%25ITEM %TODO %3PRIORITY %TAGS"
  1600. "The default column format, if no other format has been defined.
  1601. This variable can be set on the per-file basis by inserting a line
  1602. #+COLUMNS: %25ITEM ....."
  1603. :group 'org-properties
  1604. :type 'string)
  1605. (defcustom org-effort-property "Effort"
  1606. "The property that is being used to keep track of effort estimates.
  1607. Effort estimates given in this property need to have the format H:MM."
  1608. :group 'org-properties
  1609. :group 'org-progress
  1610. :type '(string :tag "Property"))
  1611. (defcustom org-global-properties nil
  1612. "List of property/value pairs that can be inherited by any entry.
  1613. You can set buffer-local values for this by adding lines like
  1614. #+PROPERTY: NAME VALUE"
  1615. :group 'org-properties
  1616. :type '(repeat
  1617. (cons (string :tag "Property")
  1618. (string :tag "Value"))))
  1619. (defvar org-local-properties nil
  1620. "List of property/value pairs that can be inherited by any entry.
  1621. Valid for the current buffer.
  1622. This variable is populated from #+PROPERTY lines.")
  1623. (defgroup org-agenda nil
  1624. "Options concerning agenda views in Org-mode."
  1625. :tag "Org Agenda"
  1626. :group 'org)
  1627. (defvar org-category nil
  1628. "Variable used by org files to set a category for agenda display.
  1629. Such files should use a file variable to set it, for example
  1630. # -*- mode: org; org-category: \"ELisp\"
  1631. or contain a special line
  1632. #+CATEGORY: ELisp
  1633. If the file does not specify a category, then file's base name
  1634. is used instead.")
  1635. (make-variable-buffer-local 'org-category)
  1636. (defcustom org-agenda-files nil
  1637. "The files to be used for agenda display.
  1638. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1639. \\[org-remove-file]. You can also use customize to edit the list.
  1640. If an entry is a directory, all files in that directory that are matched by
  1641. `org-agenda-file-regexp' will be part of the file list.
  1642. If the value of the variable is not a list but a single file name, then
  1643. the list of agenda files is actually stored and maintained in that file, one
  1644. agenda file per line."
  1645. :group 'org-agenda
  1646. :type '(choice
  1647. (repeat :tag "List of files and directories" file)
  1648. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1649. (defcustom org-agenda-file-regexp "\\`[^.].*\\.org\\'"
  1650. "Regular expression to match files for `org-agenda-files'.
  1651. If any element in the list in that variable contains a directory instead
  1652. of a normal file, all files in that directory that are matched by this
  1653. regular expression will be included."
  1654. :group 'org-agenda
  1655. :type 'regexp)
  1656. (defcustom org-agenda-text-search-extra-files nil
  1657. "List of extra files to be searched by text search commands.
  1658. These files will be search in addition to the agenda files by the
  1659. commands `org-search-view' (`C-c a s') and `org-occur-in-agenda-files'.
  1660. Note that these files will only be searched for text search commands,
  1661. not for the other agenda views like todo lists, tag searches or the weekly
  1662. agenda. This variable is intended to list notes and possibly archive files
  1663. that should also be searched by these two commands.
  1664. In fact, if the first element in the list is the symbol `agenda-archives',
  1665. than all archive files of all agenda files will be added to the search
  1666. scope."
  1667. :group 'org-agenda
  1668. :type '(set :greedy t
  1669. (const :tag "Agenda Archives" agenda-archives)
  1670. (repeat :inline t (file))))
  1671. (if (fboundp 'defvaralias)
  1672. (defvaralias 'org-agenda-multi-occur-extra-files
  1673. 'org-agenda-text-search-extra-files))
  1674. (defcustom org-agenda-skip-unavailable-files nil
  1675. "t means to just skip non-reachable files in `org-agenda-files'.
  1676. Nil means to remove them, after a query, from the list."
  1677. :group 'org-agenda
  1678. :type 'boolean)
  1679. (defcustom org-calendar-to-agenda-key [?c]
  1680. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1681. The command `org-calendar-goto-agenda' will be bound to this key. The
  1682. default is the character `c' because then `c' can be used to switch back and
  1683. forth between agenda and calendar."
  1684. :group 'org-agenda
  1685. :type 'sexp)
  1686. (eval-after-load "calendar"
  1687. '(org-defkey calendar-mode-map org-calendar-to-agenda-key
  1688. 'org-calendar-goto-agenda))
  1689. (defgroup org-latex nil
  1690. "Options for embedding LaTeX code into Org-mode."
  1691. :tag "Org LaTeX"
  1692. :group 'org)
  1693. (defcustom org-format-latex-options
  1694. '(:foreground default :background default :scale 1.0
  1695. :html-foreground "Black" :html-background "Transparent" :html-scale 1.0
  1696. :matchers ("begin" "$" "$$" "\\(" "\\["))
  1697. "Options for creating images from LaTeX fragments.
  1698. This is a property list with the following properties:
  1699. :foreground the foreground color for images embedded in emacs, e.g. \"Black\".
  1700. `default' means use the forground of the default face.
  1701. :background the background color, or \"Transparent\".
  1702. `default' means use the background of the default face.
  1703. :scale a scaling factor for the size of the images
  1704. :html-foreground, :html-background, :html-scale
  1705. The same numbers for HTML export.
  1706. :matchers a list indicating which matchers should be used to
  1707. find LaTeX fragments. Valid members of this list are:
  1708. \"begin\" find environments
  1709. \"$\" find math expressions surrounded by $...$
  1710. \"$$\" find math expressions surrounded by $$....$$
  1711. \"\\(\" find math expressions surrounded by \\(...\\)
  1712. \"\\ [\" find math expressions surrounded by \\ [...\\]"
  1713. :group 'org-latex
  1714. :type 'plist)
  1715. (defcustom org-format-latex-header "\\documentclass{article}
  1716. \\usepackage{fullpage} % do not remove
  1717. \\usepackage{amssymb}
  1718. \\usepackage[usenames]{color}
  1719. \\usepackage{amsmath}
  1720. \\usepackage{latexsym}
  1721. \\usepackage[mathscr]{eucal}
  1722. \\pagestyle{empty} % do not remove"
  1723. "The document header used for processing LaTeX fragments."
  1724. :group 'org-latex
  1725. :type 'string)
  1726. (defgroup org-font-lock nil
  1727. "Font-lock settings for highlighting in Org-mode."
  1728. :tag "Org Font Lock"
  1729. :group 'org)
  1730. (defcustom org-level-color-stars-only nil
  1731. "Non-nil means fontify only the stars in each headline.
  1732. When nil, the entire headline is fontified.
  1733. Changing it requires restart of `font-lock-mode' to become effective
  1734. also in regions already fontified."
  1735. :group 'org-font-lock
  1736. :type 'boolean)
  1737. (defcustom org-hide-leading-stars nil
  1738. "Non-nil means, hide the first N-1 stars in a headline.
  1739. This works by using the face `org-hide' for these stars. This
  1740. face is white for a light background, and black for a dark
  1741. background. You may have to customize the face `org-hide' to
  1742. make this work.
  1743. Changing it requires restart of `font-lock-mode' to become effective
  1744. also in regions already fontified.
  1745. You may also set this on a per-file basis by adding one of the following
  1746. lines to the buffer:
  1747. #+STARTUP: hidestars
  1748. #+STARTUP: showstars"
  1749. :group 'org-font-lock
  1750. :type 'boolean)
  1751. (defcustom org-fontify-done-headline nil
  1752. "Non-nil means, change the face of a headline if it is marked DONE.
  1753. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1754. When this is non-nil, the headline after the keyword is set to the
  1755. `org-headline-done' as an additional indication."
  1756. :group 'org-font-lock
  1757. :type 'boolean)
  1758. (defcustom org-fontify-emphasized-text t
  1759. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1760. Changing this variable requires a restart of Emacs to take effect."
  1761. :group 'org-font-lock
  1762. :type 'boolean)
  1763. (defcustom org-highlight-latex-fragments-and-specials nil
  1764. "Non-nil means, fontify what is treated specially by the exporters."
  1765. :group 'org-font-lock
  1766. :type 'boolean)
  1767. (defcustom org-hide-emphasis-markers nil
  1768. "Non-nil mean font-lock should hide the emphasis marker characters."
  1769. :group 'org-font-lock
  1770. :type 'boolean)
  1771. (defvar org-emph-re nil
  1772. "Regular expression for matching emphasis.")
  1773. (defvar org-verbatim-re nil
  1774. "Regular expression for matching verbatim text.")
  1775. (defvar org-emphasis-regexp-components) ; defined just below
  1776. (defvar org-emphasis-alist) ; defined just below
  1777. (defun org-set-emph-re (var val)
  1778. "Set variable and compute the emphasis regular expression."
  1779. (set var val)
  1780. (when (and (boundp 'org-emphasis-alist)
  1781. (boundp 'org-emphasis-regexp-components)
  1782. org-emphasis-alist org-emphasis-regexp-components)
  1783. (let* ((e org-emphasis-regexp-components)
  1784. (pre (car e))
  1785. (post (nth 1 e))
  1786. (border (nth 2 e))
  1787. (body (nth 3 e))
  1788. (nl (nth 4 e))
  1789. (stacked (and nil (nth 5 e))) ; stacked is no longer allowed, forced to nil
  1790. (body1 (concat body "*?"))
  1791. (markers (mapconcat 'car org-emphasis-alist ""))
  1792. (vmarkers (mapconcat
  1793. (lambda (x) (if (eq (nth 4 x) 'verbatim) (car x) ""))
  1794. org-emphasis-alist "")))
  1795. ;; make sure special characters appear at the right position in the class
  1796. (if (string-match "\\^" markers)
  1797. (setq markers (concat (replace-match "" t t markers) "^")))
  1798. (if (string-match "-" markers)
  1799. (setq markers (concat (replace-match "" t t markers) "-")))
  1800. (if (string-match "\\^" vmarkers)
  1801. (setq vmarkers (concat (replace-match "" t t vmarkers) "^")))
  1802. (if (string-match "-" vmarkers)
  1803. (setq vmarkers (concat (replace-match "" t t vmarkers) "-")))
  1804. (if (> nl 0)
  1805. (setq body1 (concat body1 "\\(?:\n" body "*?\\)\\{0,"
  1806. (int-to-string nl) "\\}")))
  1807. ;; Make the regexp
  1808. (setq org-emph-re
  1809. (concat "\\([" pre (if (and nil stacked) markers) "]\\|^\\)"
  1810. "\\("
  1811. "\\([" markers "]\\)"
  1812. "\\("
  1813. "[^" border "]\\|"
  1814. "[^" border (if (and nil stacked) markers) "]"
  1815. body1
  1816. "[^" border (if (and nil stacked) markers) "]"
  1817. "\\)"
  1818. "\\3\\)"
  1819. "\\([" post (if (and nil stacked) markers) "]\\|$\\)"))
  1820. (setq org-verbatim-re
  1821. (concat "\\([" pre "]\\|^\\)"
  1822. "\\("
  1823. "\\([" vmarkers "]\\)"
  1824. "\\("
  1825. "[^" border "]\\|"
  1826. "[^" border "]"
  1827. body1
  1828. "[^" border "]"
  1829. "\\)"
  1830. "\\3\\)"
  1831. "\\([" post "]\\|$\\)")))))
  1832. (defcustom org-emphasis-regexp-components
  1833. '(" \t('\"" "- \t.,:?;'\")" " \t\r\n,\"'" "." 1)
  1834. "Components used to build the regular expression for emphasis.
  1835. This is a list with 6 entries. Terminology: In an emphasis string
  1836. like \" *strong word* \", we call the initial space PREMATCH, the final
  1837. space POSTMATCH, the stars MARKERS, \"s\" and \"d\" are BORDER characters
  1838. and \"trong wor\" is the body. The different components in this variable
  1839. specify what is allowed/forbidden in each part:
  1840. pre Chars allowed as prematch. Beginning of line will be allowed too.
  1841. post Chars allowed as postmatch. End of line will be allowed too.
  1842. border The chars *forbidden* as border characters.
  1843. body-regexp A regexp like \".\" to match a body character. Don't use
  1844. non-shy groups here, and don't allow newline here.
  1845. newline The maximum number of newlines allowed in an emphasis exp.
  1846. Use customize to modify this, or restart Emacs after changing it."
  1847. :group 'org-font-lock
  1848. :set 'org-set-emph-re
  1849. :type '(list
  1850. (sexp :tag "Allowed chars in pre ")
  1851. (sexp :tag "Allowed chars in post ")
  1852. (sexp :tag "Forbidden chars in border ")
  1853. (sexp :tag "Regexp for body ")
  1854. (integer :tag "number of newlines allowed")
  1855. (option (boolean :tag "Stacking (DISABLED) "))))
  1856. (defcustom org-emphasis-alist
  1857. `(("*" bold "<b>" "</b>")
  1858. ("/" italic "<i>" "</i>")
  1859. ("_" underline "<u>" "</u>")
  1860. ("=" org-code "<code>" "</code>" verbatim)
  1861. ("~" org-verbatim "" "" verbatim)
  1862. ("+" ,(if (featurep 'xemacs) 'org-table '(:strike-through t))
  1863. "<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" (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" (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 org-agenda-save-markers-for-cut-and-paste "org-agenda"
  1925. (beg end))
  1926. (declare-function parse-time-string "parse-time" (string))
  1927. (declare-function remember "remember" (&optional initial))
  1928. (declare-function remember-buffer-desc "remember" ())
  1929. (declare-function remember-finalize "remember" ())
  1930. (defvar remember-save-after-remembering)
  1931. (defvar remember-data-file)
  1932. (defvar remember-register)
  1933. (defvar remember-buffer)
  1934. (defvar remember-handler-functions)
  1935. (defvar remember-annotation-functions)
  1936. (defvar texmathp-why)
  1937. (declare-function speedbar-line-directory "speedbar" (&optional depth))
  1938. (declare-function table--at-cell-p "table" (position &optional object at-column))
  1939. (defvar w3m-current-url)
  1940. (defvar w3m-current-title)
  1941. (defvar org-latex-regexps)
  1942. ;;; Autoload and prepare some org modules
  1943. ;; Some table stuff that needs to be defined here, because it is used
  1944. ;; by the functions setting up org-mode or checking for table context.
  1945. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  1946. "Detects an org-type or table-type table.")
  1947. (defconst org-table-line-regexp "^[ \t]*|"
  1948. "Detects an org-type table line.")
  1949. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  1950. "Detects an org-type table line.")
  1951. (defconst org-table-hline-regexp "^[ \t]*|-"
  1952. "Detects an org-type table hline.")
  1953. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  1954. "Detects a table-type table hline.")
  1955. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  1956. "Searching from within a table (any type) this finds the first line
  1957. outside the table.")
  1958. ;; Autoload the functions in org-table.el that are needed by functions here.
  1959. (eval-and-compile
  1960. (org-autoload "org-table"
  1961. '(org-table-align org-table-begin org-table-blank-field
  1962. org-table-convert org-table-convert-region org-table-copy-down
  1963. org-table-copy-region org-table-create
  1964. org-table-create-or-convert-from-region
  1965. org-table-create-with-table.el org-table-current-dline
  1966. org-table-cut-region org-table-delete-column org-table-edit-field
  1967. org-table-edit-formulas org-table-end org-table-eval-formula
  1968. org-table-export org-table-field-info
  1969. org-table-get-stored-formulas org-table-goto-column
  1970. org-table-hline-and-move org-table-import org-table-insert-column
  1971. org-table-insert-hline org-table-insert-row org-table-iterate
  1972. org-table-justify-field-maybe org-table-kill-row
  1973. org-table-maybe-eval-formula org-table-maybe-recalculate-line
  1974. org-table-move-column org-table-move-column-left
  1975. org-table-move-column-right org-table-move-row
  1976. org-table-move-row-down org-table-move-row-up
  1977. org-table-next-field org-table-next-row org-table-paste-rectangle
  1978. org-table-previous-field org-table-recalculate
  1979. org-table-rotate-recalc-marks org-table-sort-lines org-table-sum
  1980. org-table-toggle-coordinate-overlays
  1981. org-table-toggle-formula-debugger org-table-wrap-region
  1982. orgtbl-mode turn-on-orgtbl)))
  1983. (defun org-at-table-p (&optional table-type)
  1984. "Return t if the cursor is inside an org-type table.
  1985. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  1986. (if org-enable-table-editor
  1987. (save-excursion
  1988. (beginning-of-line 1)
  1989. (looking-at (if table-type org-table-any-line-regexp
  1990. org-table-line-regexp)))
  1991. nil))
  1992. (defsubst org-table-p () (org-at-table-p))
  1993. (defun org-at-table.el-p ()
  1994. "Return t if and only if we are at a table.el table."
  1995. (and (org-at-table-p 'any)
  1996. (save-excursion
  1997. (goto-char (org-table-begin 'any))
  1998. (looking-at org-table1-hline-regexp))))
  1999. (defun org-table-recognize-table.el ()
  2000. "If there is a table.el table nearby, recognize it and move into it."
  2001. (if org-table-tab-recognizes-table.el
  2002. (if (org-at-table.el-p)
  2003. (progn
  2004. (beginning-of-line 1)
  2005. (if (looking-at org-table-dataline-regexp)
  2006. nil
  2007. (if (looking-at org-table1-hline-regexp)
  2008. (progn
  2009. (beginning-of-line 2)
  2010. (if (looking-at org-table-any-border-regexp)
  2011. (beginning-of-line -1)))))
  2012. (if (re-search-forward "|" (org-table-end t) t)
  2013. (progn
  2014. (require 'table)
  2015. (if (table--at-cell-p (point))
  2016. t
  2017. (message "recognizing table.el table...")
  2018. (table-recognize-table)
  2019. (message "recognizing table.el table...done")))
  2020. (error "This should not happen..."))
  2021. t)
  2022. nil)
  2023. nil))
  2024. (defun org-at-table-hline-p ()
  2025. "Return t if the cursor is inside a hline in a table."
  2026. (if org-enable-table-editor
  2027. (save-excursion
  2028. (beginning-of-line 1)
  2029. (looking-at org-table-hline-regexp))
  2030. nil))
  2031. (defvar org-table-clean-did-remove-column nil)
  2032. (defun org-table-map-tables (function)
  2033. "Apply FUNCTION to the start of all tables in the buffer."
  2034. (save-excursion
  2035. (save-restriction
  2036. (widen)
  2037. (goto-char (point-min))
  2038. (while (re-search-forward org-table-any-line-regexp nil t)
  2039. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  2040. (beginning-of-line 1)
  2041. (if (looking-at org-table-line-regexp)
  2042. (save-excursion (funcall function)))
  2043. (re-search-forward org-table-any-border-regexp nil 1))))
  2044. (message "Mapping tables: done"))
  2045. ;; Declare and autoload functions from org-exp.el
  2046. (declare-function org-default-export-plist "org-exp")
  2047. (declare-function org-infile-export-plist "org-exp")
  2048. (declare-function org-get-current-options "org-exp")
  2049. (eval-and-compile
  2050. (org-autoload "org-exp"
  2051. '(org-export org-export-as-ascii org-export-visible
  2052. org-insert-export-options-template org-export-as-html-and-open
  2053. org-export-as-html-batch org-export-as-html-to-buffer
  2054. org-replace-region-by-html org-export-region-as-html
  2055. org-export-as-html org-export-icalendar-this-file
  2056. org-export-icalendar-all-agenda-files
  2057. org-export-icalendar-combine-agenda-files org-export-as-xoxo)))
  2058. ;; Declare and autoload functions from org-exp.el
  2059. (eval-and-compile
  2060. (org-autoload "org-exp"
  2061. '(org-agenda org-agenda-list org-search-view
  2062. org-todo-list org-tags-view org-agenda-list-stuck-projects
  2063. org-diary org-agenda-to-appt)))
  2064. ;; Autoload org-remember
  2065. (eval-and-compile
  2066. (org-autoload "org-remember"
  2067. '(org-remember-insinuate org-remember-annotation
  2068. org-remember-apply-template org-remember org-remember-handler)))
  2069. ;; Autoload org-clock.el
  2070. (declare-function org-clock-save-markers-for-cut-and-paste "org-clock"
  2071. (beg end))
  2072. (defvar org-clock-marker (make-marker)
  2073. "Marker recording the last clock-in.")
  2074. (eval-and-compile
  2075. (org-autoload
  2076. "org-clock"
  2077. '(org-clock-in org-clock-out org-clock-cancel
  2078. org-clock-goto org-clock-sum org-clock-display
  2079. org-remove-clock-overlays org-clock-report
  2080. org-clocktable-shift org-dblock-write:clocktable
  2081. org-get-clocktable)))
  2082. (defun org-clock-update-time-maybe ()
  2083. "If this is a CLOCK line, update it and return t.
  2084. Otherwise, return nil."
  2085. (interactive)
  2086. (save-excursion
  2087. (beginning-of-line 1)
  2088. (skip-chars-forward " \t")
  2089. (when (looking-at org-clock-string)
  2090. (let ((re (concat "[ \t]*" org-clock-string
  2091. " *[[<]\\([^]>]+\\)[]>]-+[[<]\\([^]>]+\\)[]>]"
  2092. "\\([ \t]*=>.*\\)?"))
  2093. ts te h m s)
  2094. (if (not (looking-at re))
  2095. nil
  2096. (and (match-end 3) (delete-region (match-beginning 3) (match-end 3)))
  2097. (end-of-line 1)
  2098. (setq ts (match-string 1)
  2099. te (match-string 2))
  2100. (setq s (- (time-to-seconds
  2101. (apply 'encode-time (org-parse-time-string te)))
  2102. (time-to-seconds
  2103. (apply 'encode-time (org-parse-time-string ts))))
  2104. h (floor (/ s 3600))
  2105. s (- s (* 3600 h))
  2106. m (floor (/ s 60))
  2107. s (- s (* 60 s)))
  2108. (insert " => " (format "%2d:%02d" h m))
  2109. t)))))
  2110. (defun org-check-running-clock ()
  2111. "Check if the current buffer contains the running clock.
  2112. If yes, offer to stop it and to save the buffer with the changes."
  2113. (when (and (equal (marker-buffer org-clock-marker) (current-buffer))
  2114. (y-or-n-p (format "Clock-out in buffer %s before killing it? "
  2115. (buffer-name))))
  2116. (org-clock-out)
  2117. (when (y-or-n-p "Save changed buffer?")
  2118. (save-buffer))))
  2119. (defun org-clocktable-try-shift (dir n)
  2120. "Check if this line starts a clock table, if yes, shift the time block."
  2121. (when (org-match-line "#\\+BEGIN: clocktable\\>")
  2122. (org-clocktable-shift dir n)))
  2123. ;; Autoload archiving code
  2124. ;; The stuff that is needed for cycling and tags has to be defined here.
  2125. (defgroup org-archive nil
  2126. "Options concerning archiving in Org-mode."
  2127. :tag "Org Archive"
  2128. :group 'org-structure)
  2129. (defcustom org-archive-location "%s_archive::"
  2130. "The location where subtrees should be archived.
  2131. Otherwise, the value of this variable is a string, consisting of two
  2132. parts, separated by a double-colon.
  2133. The first part is a file name - when omitted, archiving happens in the same
  2134. file. %s will be replaced by the current file name (without directory part).
  2135. Archiving to a different file is useful to keep archived entries from
  2136. contributing to the Org-mode Agenda.
  2137. The part after the double colon is a headline. The archived entries will be
  2138. filed under that headline. When omitted, the subtrees are simply filed away
  2139. at the end of the file, as top-level entries.
  2140. Here are a few examples:
  2141. \"%s_archive::\"
  2142. If the current file is Projects.org, archive in file
  2143. Projects.org_archive, as top-level trees. This is the default.
  2144. \"::* Archived Tasks\"
  2145. Archive in the current file, under the top-level headline
  2146. \"* Archived Tasks\".
  2147. \"~/org/archive.org::\"
  2148. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  2149. \"basement::** Finished Tasks\"
  2150. Archive in file ./basement (relative path), as level 3 trees
  2151. below the level 2 heading \"** Finished Tasks\".
  2152. You may set this option on a per-file basis by adding to the buffer a
  2153. line like
  2154. #+ARCHIVE: basement::** Finished Tasks
  2155. You may also define it locally for a subtree by setting an ARCHIVE property
  2156. in the entry. If such a property is found in an entry, or anywhere up
  2157. the hierarchy, it will be used."
  2158. :group 'org-archive
  2159. :type 'string)
  2160. (defcustom org-archive-tag "ARCHIVE"
  2161. "The tag that marks a subtree as archived.
  2162. An archived subtree does not open during visibility cycling, and does
  2163. not contribute to the agenda listings.
  2164. After changing this, font-lock must be restarted in the relevant buffers to
  2165. get the proper fontification."
  2166. :group 'org-archive
  2167. :group 'org-keywords
  2168. :type 'string)
  2169. (defcustom org-agenda-skip-archived-trees t
  2170. "Non-nil means, the agenda will skip any items located in archived trees.
  2171. An archived tree is a tree marked with the tag ARCHIVE."
  2172. :group 'org-archive
  2173. :group 'org-agenda-skip
  2174. :type 'boolean)
  2175. (defcustom org-cycle-open-archived-trees nil
  2176. "Non-nil means, `org-cycle' will open archived trees.
  2177. An archived tree is a tree marked with the tag ARCHIVE.
  2178. When nil, archived trees will stay folded. You can still open them with
  2179. normal outline commands like `show-all', but not with the cycling commands."
  2180. :group 'org-archive
  2181. :group 'org-cycle
  2182. :type 'boolean)
  2183. (defcustom org-sparse-tree-open-archived-trees nil
  2184. "Non-nil means sparse tree construction shows matches in archived trees.
  2185. When nil, matches in these trees are highlighted, but the trees are kept in
  2186. collapsed state."
  2187. :group 'org-archive
  2188. :group 'org-sparse-trees
  2189. :type 'boolean)
  2190. (defun org-cycle-hide-archived-subtrees (state)
  2191. "Re-hide all archived subtrees after a visibility state change."
  2192. (when (and (not org-cycle-open-archived-trees)
  2193. (not (memq state '(overview folded))))
  2194. (save-excursion
  2195. (let* ((globalp (memq state '(contents all)))
  2196. (beg (if globalp (point-min) (point)))
  2197. (end (if globalp (point-max) (org-end-of-subtree t))))
  2198. (org-hide-archived-subtrees beg end)
  2199. (goto-char beg)
  2200. (if (looking-at (concat ".*:" org-archive-tag ":"))
  2201. (message "%s" (substitute-command-keys
  2202. "Subtree is archived and stays closed. Use \\[org-force-cycle-archived] to cycle it anyway.")))))))
  2203. (defun org-force-cycle-archived ()
  2204. "Cycle subtree even if it is archived."
  2205. (interactive)
  2206. (setq this-command 'org-cycle)
  2207. (let ((org-cycle-open-archived-trees t))
  2208. (call-interactively 'org-cycle)))
  2209. (defun org-hide-archived-subtrees (beg end)
  2210. "Re-hide all archived subtrees after a visibility state change."
  2211. (save-excursion
  2212. (let* ((re (concat ":" org-archive-tag ":")))
  2213. (goto-char beg)
  2214. (while (re-search-forward re end t)
  2215. (and (org-on-heading-p) (hide-subtree))
  2216. (org-end-of-subtree t)))))
  2217. (defalias 'org-advertized-archive-subtree 'org-archive-subtree)
  2218. (eval-and-compile
  2219. (org-autoload "org-archive"
  2220. '(org-add-archive-files org-archive-subtree
  2221. org-archive-to-archive-sibling org-toggle-archive-tag)))
  2222. ;; Autoload Column View Code
  2223. (declare-function org-columns-number-to-string "org-colview")
  2224. (declare-function org-columns-get-format-and-top-level "org-colview")
  2225. (declare-function org-columns-compute "org-colview")
  2226. (org-autoload (if (featurep 'xemacs) "org-colview-xemacs" "org-colview")
  2227. '(org-columns-number-to-string org-columns-get-format-and-top-level
  2228. org-columns-compute org-agenda-columns org-columns-remove-overlays
  2229. org-columns org-insert-columns-dblock))
  2230. ;;; Variables for pre-computed regular expressions, all buffer local
  2231. (defvar org-drawer-regexp nil
  2232. "Matches first line of a hidden block.")
  2233. (make-variable-buffer-local 'org-drawer-regexp)
  2234. (defvar org-todo-regexp nil
  2235. "Matches any of the TODO state keywords.")
  2236. (make-variable-buffer-local 'org-todo-regexp)
  2237. (defvar org-not-done-regexp nil
  2238. "Matches any of the TODO state keywords except the last one.")
  2239. (make-variable-buffer-local 'org-not-done-regexp)
  2240. (defvar org-todo-line-regexp nil
  2241. "Matches a headline and puts TODO state into group 2 if present.")
  2242. (make-variable-buffer-local 'org-todo-line-regexp)
  2243. (defvar org-complex-heading-regexp nil
  2244. "Matches a headline and puts everything into groups:
  2245. group 1: the stars
  2246. group 2: The todo keyword, maybe
  2247. group 3: Priority cookie
  2248. group 4: True headline
  2249. group 5: Tags")
  2250. (make-variable-buffer-local 'org-complex-heading-regexp)
  2251. (defvar org-todo-line-tags-regexp nil
  2252. "Matches a headline and puts TODO state into group 2 if present.
  2253. Also put tags into group 4 if tags are present.")
  2254. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2255. (defvar org-nl-done-regexp nil
  2256. "Matches newline followed by a headline with the DONE keyword.")
  2257. (make-variable-buffer-local 'org-nl-done-regexp)
  2258. (defvar org-looking-at-done-regexp nil
  2259. "Matches the DONE keyword a point.")
  2260. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2261. (defvar org-ds-keyword-length 12
  2262. "Maximum length of the Deadline and SCHEDULED keywords.")
  2263. (make-variable-buffer-local 'org-ds-keyword-length)
  2264. (defvar org-deadline-regexp nil
  2265. "Matches the DEADLINE keyword.")
  2266. (make-variable-buffer-local 'org-deadline-regexp)
  2267. (defvar org-deadline-time-regexp nil
  2268. "Matches the DEADLINE keyword together with a time stamp.")
  2269. (make-variable-buffer-local 'org-deadline-time-regexp)
  2270. (defvar org-deadline-line-regexp nil
  2271. "Matches the DEADLINE keyword and the rest of the line.")
  2272. (make-variable-buffer-local 'org-deadline-line-regexp)
  2273. (defvar org-scheduled-regexp nil
  2274. "Matches the SCHEDULED keyword.")
  2275. (make-variable-buffer-local 'org-scheduled-regexp)
  2276. (defvar org-scheduled-time-regexp nil
  2277. "Matches the SCHEDULED keyword together with a time stamp.")
  2278. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2279. (defvar org-closed-time-regexp nil
  2280. "Matches the CLOSED keyword together with a time stamp.")
  2281. (make-variable-buffer-local 'org-closed-time-regexp)
  2282. (defvar org-keyword-time-regexp nil
  2283. "Matches any of the 4 keywords, together with the time stamp.")
  2284. (make-variable-buffer-local 'org-keyword-time-regexp)
  2285. (defvar org-keyword-time-not-clock-regexp nil
  2286. "Matches any of the 3 keywords, together with the time stamp.")
  2287. (make-variable-buffer-local 'org-keyword-time-not-clock-regexp)
  2288. (defvar org-maybe-keyword-time-regexp nil
  2289. "Matches a timestamp, possibly preceeded by a keyword.")
  2290. (make-variable-buffer-local 'org-maybe-keyword-time-regexp)
  2291. (defvar org-planning-or-clock-line-re nil
  2292. "Matches a line with planning or clock info.")
  2293. (make-variable-buffer-local 'org-planning-or-clock-line-re)
  2294. (defconst org-plain-time-of-day-regexp
  2295. (concat
  2296. "\\(\\<[012]?[0-9]"
  2297. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2298. "\\(--?"
  2299. "\\(\\<[012]?[0-9]"
  2300. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2301. "\\)?")
  2302. "Regular expression to match a plain time or time range.
  2303. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2304. groups carry important information:
  2305. 0 the full match
  2306. 1 the first time, range or not
  2307. 8 the second time, if it is a range.")
  2308. (defconst org-plain-time-extension-regexp
  2309. (concat
  2310. "\\(\\<[012]?[0-9]"
  2311. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  2312. "\\+\\([0-9]+\\)\\(:\\([0-5][0-9]\\)\\)?")
  2313. "Regular expression to match a time range like 13:30+2:10 = 13:30-15:40.
  2314. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  2315. groups carry important information:
  2316. 0 the full match
  2317. 7 hours of duration
  2318. 9 minutes of duration")
  2319. (defconst org-stamp-time-of-day-regexp
  2320. (concat
  2321. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +\\sw+ +\\)"
  2322. "\\([012][0-9]:[0-5][0-9]\\(-\\([012][0-9]:[0-5][0-9]\\)\\)?[^\n\r>]*?\\)>"
  2323. "\\(--?"
  2324. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  2325. "Regular expression to match a timestamp time or time range.
  2326. After a match, the following groups carry important information:
  2327. 0 the full match
  2328. 1 date plus weekday, for backreferencing to make sure both times on same day
  2329. 2 the first time, range or not
  2330. 4 the second time, if it is a range.")
  2331. (defconst org-startup-options
  2332. '(("fold" org-startup-folded t)
  2333. ("overview" org-startup-folded t)
  2334. ("nofold" org-startup-folded nil)
  2335. ("showall" org-startup-folded nil)
  2336. ("content" org-startup-folded content)
  2337. ("hidestars" org-hide-leading-stars t)
  2338. ("showstars" org-hide-leading-stars nil)
  2339. ("odd" org-odd-levels-only t)
  2340. ("oddeven" org-odd-levels-only nil)
  2341. ("align" org-startup-align-all-tables t)
  2342. ("noalign" org-startup-align-all-tables nil)
  2343. ("customtime" org-display-custom-times t)
  2344. ("logdone" org-log-done time)
  2345. ("lognotedone" org-log-done note)
  2346. ("nologdone" org-log-done nil)
  2347. ("lognoteclock-out" org-log-note-clock-out t)
  2348. ("nolognoteclock-out" org-log-note-clock-out nil)
  2349. ("logrepeat" org-log-repeat state)
  2350. ("lognoterepeat" org-log-repeat note)
  2351. ("nologrepeat" org-log-repeat nil)
  2352. ("constcgs" constants-unit-system cgs)
  2353. ("constSI" constants-unit-system SI))
  2354. "Variable associated with STARTUP options for org-mode.
  2355. Each element is a list of three items: The startup options as written
  2356. in the #+STARTUP line, the corresponding variable, and the value to
  2357. set this variable to if the option is found. An optional forth element PUSH
  2358. means to push this value onto the list in the variable.")
  2359. (defun org-set-regexps-and-options ()
  2360. "Precompute regular expressions for current buffer."
  2361. (when (org-mode-p)
  2362. (org-set-local 'org-todo-kwd-alist nil)
  2363. (org-set-local 'org-todo-key-alist nil)
  2364. (org-set-local 'org-todo-key-trigger nil)
  2365. (org-set-local 'org-todo-keywords-1 nil)
  2366. (org-set-local 'org-done-keywords nil)
  2367. (org-set-local 'org-todo-heads nil)
  2368. (org-set-local 'org-todo-sets nil)
  2369. (org-set-local 'org-todo-log-states nil)
  2370. (let ((re (org-make-options-regexp
  2371. '("CATEGORY" "SEQ_TODO" "TYP_TODO" "TODO" "COLUMNS"
  2372. "STARTUP" "ARCHIVE" "TAGS" "LINK" "PRIORITIES"
  2373. "CONSTANTS" "PROPERTY" "DRAWERS" "SETUPFILE")))
  2374. (splitre "[ \t]+")
  2375. kwds kws0 kwsa key log value cat arch tags const links hw dws
  2376. tail sep kws1 prio props drawers
  2377. ext-setup-or-nil setup-contents (start 0))
  2378. (save-excursion
  2379. (save-restriction
  2380. (widen)
  2381. (goto-char (point-min))
  2382. (while (or (and ext-setup-or-nil
  2383. (string-match re ext-setup-or-nil start)
  2384. (setq start (match-end 0)))
  2385. (and (setq ext-setup-or-nil nil start 0)
  2386. (re-search-forward re nil t)))
  2387. (setq key (upcase (match-string 1 ext-setup-or-nil))
  2388. value (org-match-string-no-properties 2 ext-setup-or-nil))
  2389. (cond
  2390. ((equal key "CATEGORY")
  2391. (if (string-match "[ \t]+$" value)
  2392. (setq value (replace-match "" t t value)))
  2393. (setq cat value))
  2394. ((member key '("SEQ_TODO" "TODO"))
  2395. (push (cons 'sequence (org-split-string value splitre)) kwds))
  2396. ((equal key "TYP_TODO")
  2397. (push (cons 'type (org-split-string value splitre)) kwds))
  2398. ((equal key "TAGS")
  2399. (setq tags (append tags (org-split-string value splitre))))
  2400. ((equal key "COLUMNS")
  2401. (org-set-local 'org-columns-default-format value))
  2402. ((equal key "LINK")
  2403. (when (string-match "^\\(\\S-+\\)[ \t]+\\(.+\\)" value)
  2404. (push (cons (match-string 1 value)
  2405. (org-trim (match-string 2 value)))
  2406. links)))
  2407. ((equal key "PRIORITIES")
  2408. (setq prio (org-split-string value " +")))
  2409. ((equal key "PROPERTY")
  2410. (when (string-match "\\(\\S-+\\)\\s-+\\(.*\\)" value)
  2411. (push (cons (match-string 1 value) (match-string 2 value))
  2412. props)))
  2413. ((equal key "DRAWERS")
  2414. (setq drawers (org-split-string value splitre)))
  2415. ((equal key "CONSTANTS")
  2416. (setq const (append const (org-split-string value splitre))))
  2417. ((equal key "STARTUP")
  2418. (let ((opts (org-split-string value splitre))
  2419. l var val)
  2420. (while (setq l (pop opts))
  2421. (when (setq l (assoc l org-startup-options))
  2422. (setq var (nth 1 l) val (nth 2 l))
  2423. (if (not (nth 3 l))
  2424. (set (make-local-variable var) val)
  2425. (if (not (listp (symbol-value var)))
  2426. (set (make-local-variable var) nil))
  2427. (set (make-local-variable var) (symbol-value var))
  2428. (add-to-list var val))))))
  2429. ((equal key "ARCHIVE")
  2430. (string-match " *$" value)
  2431. (setq arch (replace-match "" t t value))
  2432. (remove-text-properties 0 (length arch)
  2433. '(face t fontified t) arch))
  2434. ((equal key "SETUPFILE")
  2435. (setq setup-contents (org-file-contents
  2436. (expand-file-name
  2437. (org-remove-double-quotes value))
  2438. 'noerror))
  2439. (if (not ext-setup-or-nil)
  2440. (setq ext-setup-or-nil setup-contents start 0)
  2441. (setq ext-setup-or-nil
  2442. (concat (substring ext-setup-or-nil 0 start)
  2443. "\n" setup-contents "\n"
  2444. (substring ext-setup-or-nil start)))))
  2445. ))))
  2446. (when cat
  2447. (org-set-local 'org-category (intern cat))
  2448. (push (cons "CATEGORY" cat) props))
  2449. (when prio
  2450. (if (< (length prio) 3) (setq prio '("A" "C" "B")))
  2451. (setq prio (mapcar 'string-to-char prio))
  2452. (org-set-local 'org-highest-priority (nth 0 prio))
  2453. (org-set-local 'org-lowest-priority (nth 1 prio))
  2454. (org-set-local 'org-default-priority (nth 2 prio)))
  2455. (and props (org-set-local 'org-local-properties (nreverse props)))
  2456. (and drawers (org-set-local 'org-drawers drawers))
  2457. (and arch (org-set-local 'org-archive-location arch))
  2458. (and links (setq org-link-abbrev-alist-local (nreverse links)))
  2459. ;; Process the TODO keywords
  2460. (unless kwds
  2461. ;; Use the global values as if they had been given locally.
  2462. (setq kwds (default-value 'org-todo-keywords))
  2463. (if (stringp (car kwds))
  2464. (setq kwds (list (cons org-todo-interpretation
  2465. (default-value 'org-todo-keywords)))))
  2466. (setq kwds (reverse kwds)))
  2467. (setq kwds (nreverse kwds))
  2468. (let (inter kws kw)
  2469. (while (setq kws (pop kwds))
  2470. (setq inter (pop kws) sep (member "|" kws)
  2471. kws0 (delete "|" (copy-sequence kws))
  2472. kwsa nil
  2473. kws1 (mapcar
  2474. (lambda (x)
  2475. ;; 1 2
  2476. (if (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$" x)
  2477. (progn
  2478. (setq kw (match-string 1 x)
  2479. key (and (match-end 2) (match-string 2 x))
  2480. log (org-extract-log-state-settings x))
  2481. (push (cons kw (and key (string-to-char key))) kwsa)
  2482. (and log (push log org-todo-log-states))
  2483. kw)
  2484. (error "Invalid TODO keyword %s" x)))
  2485. kws0)
  2486. kwsa (if kwsa (append '((:startgroup))
  2487. (nreverse kwsa)
  2488. '((:endgroup))))
  2489. hw (car kws1)
  2490. dws (if sep (org-remove-keyword-keys (cdr sep)) (last kws1))
  2491. tail (list inter hw (car dws) (org-last dws)))
  2492. (add-to-list 'org-todo-heads hw 'append)
  2493. (push kws1 org-todo-sets)
  2494. (setq org-done-keywords (append org-done-keywords dws nil))
  2495. (setq org-todo-key-alist (append org-todo-key-alist kwsa))
  2496. (mapc (lambda (x) (push (cons x tail) org-todo-kwd-alist)) kws1)
  2497. (setq org-todo-keywords-1 (append org-todo-keywords-1 kws1 nil)))
  2498. (setq org-todo-sets (nreverse org-todo-sets)
  2499. org-todo-kwd-alist (nreverse org-todo-kwd-alist)
  2500. org-todo-key-trigger (delq nil (mapcar 'cdr org-todo-key-alist))
  2501. org-todo-key-alist (org-assign-fast-keys org-todo-key-alist)))
  2502. ;; Process the constants
  2503. (when const
  2504. (let (e cst)
  2505. (while (setq e (pop const))
  2506. (if (string-match "^\\([a-zA-Z0][_a-zA-Z0-9]*\\)=\\(.*\\)" e)
  2507. (push (cons (match-string 1 e) (match-string 2 e)) cst)))
  2508. (setq org-table-formula-constants-local cst)))
  2509. ;; Process the tags.
  2510. (when tags
  2511. (let (e tgs)
  2512. (while (setq e (pop tags))
  2513. (cond
  2514. ((equal e "{") (push '(:startgroup) tgs))
  2515. ((equal e "}") (push '(:endgroup) tgs))
  2516. ((string-match (org-re "^\\([[:alnum:]_@]+\\)(\\(.\\))$") e)
  2517. (push (cons (match-string 1 e)
  2518. (string-to-char (match-string 2 e)))
  2519. tgs))
  2520. (t (push (list e) tgs))))
  2521. (org-set-local 'org-tag-alist nil)
  2522. (while (setq e (pop tgs))
  2523. (or (and (stringp (car e))
  2524. (assoc (car e) org-tag-alist))
  2525. (push e org-tag-alist))))))
  2526. ;; Compute the regular expressions and other local variables
  2527. (if (not org-done-keywords)
  2528. (setq org-done-keywords (list (org-last org-todo-keywords-1))))
  2529. (setq org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2530. (length org-scheduled-string)
  2531. (length org-clock-string)
  2532. (length org-closed-string)))
  2533. org-drawer-regexp
  2534. (concat "^[ \t]*:\\("
  2535. (mapconcat 'regexp-quote org-drawers "\\|")
  2536. "\\):[ \t]*$")
  2537. org-not-done-keywords
  2538. (org-delete-all org-done-keywords (copy-sequence org-todo-keywords-1))
  2539. org-todo-regexp
  2540. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords-1
  2541. "\\|") "\\)\\>")
  2542. org-not-done-regexp
  2543. (concat "\\<\\("
  2544. (mapconcat 'regexp-quote org-not-done-keywords "\\|")
  2545. "\\)\\>")
  2546. org-todo-line-regexp
  2547. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2548. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2549. "\\)\\>\\)?[ \t]*\\(.*\\)")
  2550. org-complex-heading-regexp
  2551. (concat "^\\(\\*+\\)\\(?:[ \t]+\\("
  2552. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2553. "\\)\\>\\)?\\(?:[ \t]*\\(\\[#.\\]\\)\\)?[ \t]*\\(.*?\\)"
  2554. "\\(?:[ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  2555. org-nl-done-regexp
  2556. (concat "\n\\*+[ \t]+"
  2557. "\\(?:" (mapconcat 'regexp-quote org-done-keywords "\\|")
  2558. "\\)" "\\>")
  2559. org-todo-line-tags-regexp
  2560. (concat "^\\(\\*+\\)[ \t]+\\(?:\\("
  2561. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  2562. (org-re
  2563. "\\)\\>\\)? *\\(.*?\\([ \t]:[[:alnum:]:_@]+:[ \t]*\\)?$\\)"))
  2564. org-looking-at-done-regexp
  2565. (concat "^" "\\(?:"
  2566. (mapconcat 'regexp-quote org-done-keywords "\\|") "\\)"
  2567. "\\>")
  2568. org-deadline-regexp (concat "\\<" org-deadline-string)
  2569. org-deadline-time-regexp
  2570. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2571. org-deadline-line-regexp
  2572. (concat "\\<\\(" org-deadline-string "\\).*")
  2573. org-scheduled-regexp
  2574. (concat "\\<" org-scheduled-string)
  2575. org-scheduled-time-regexp
  2576. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2577. org-closed-time-regexp
  2578. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2579. org-keyword-time-regexp
  2580. (concat "\\<\\(" org-scheduled-string
  2581. "\\|" org-deadline-string
  2582. "\\|" org-closed-string
  2583. "\\|" org-clock-string "\\)"
  2584. " *[[<]\\([^]>]+\\)[]>]")
  2585. org-keyword-time-not-clock-regexp
  2586. (concat "\\<\\(" org-scheduled-string
  2587. "\\|" org-deadline-string
  2588. "\\|" org-closed-string
  2589. "\\)"
  2590. " *[[<]\\([^]>]+\\)[]>]")
  2591. org-maybe-keyword-time-regexp
  2592. (concat "\\(\\<\\(" org-scheduled-string
  2593. "\\|" org-deadline-string
  2594. "\\|" org-closed-string
  2595. "\\|" org-clock-string "\\)\\)?"
  2596. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^]\r\n>]*?[]>]\\|<%%([^\r\n>]*>\\)")
  2597. org-planning-or-clock-line-re
  2598. (concat "\\(?:^[ \t]*\\(" org-scheduled-string
  2599. "\\|" org-deadline-string
  2600. "\\|" org-closed-string "\\|" org-clock-string
  2601. "\\)\\>\\)")
  2602. )
  2603. (org-compute-latex-and-specials-regexp)
  2604. (org-set-font-lock-defaults)))
  2605. (defun org-file-contents (file &optional noerror)
  2606. "Return the contents of FILE, as a string."
  2607. (if (or (not file)
  2608. (not (file-readable-p file)))
  2609. (if noerror
  2610. (progn
  2611. (message "Cannot read file %s" file)
  2612. (ding) (sit-for 2)
  2613. "")
  2614. (error "Cannot read file %s" file))
  2615. (with-temp-buffer
  2616. (insert-file-contents file)
  2617. (buffer-string))))
  2618. (defun org-extract-log-state-settings (x)
  2619. "Extract the log state setting from a TODO keyword string.
  2620. This will extract info from a string like \"WAIT(w@/!)\"."
  2621. (let (kw key log1 log2)
  2622. (when (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?\\([!@]\\)?\\(?:/\\([!@]\\)\\)?)\\)?$" x)
  2623. (setq kw (match-string 1 x)
  2624. key (and (match-end 2) (match-string 2 x))
  2625. log1 (and (match-end 3) (match-string 3 x))
  2626. log2 (and (match-end 4) (match-string 4 x)))
  2627. (and (or log1 log2)
  2628. (list kw
  2629. (and log1 (if (equal log1 "!") 'time 'note))
  2630. (and log2 (if (equal log2 "!") 'time 'note)))))))
  2631. (defun org-remove-keyword-keys (list)
  2632. "Remove a pair of parenthesis at the end of each string in LIST."
  2633. (mapcar (lambda (x)
  2634. (if (string-match "(.*)$" x)
  2635. (substring x 0 (match-beginning 0))
  2636. x))
  2637. list))
  2638. ;; FIXME: this could be done much better, using second characters etc.
  2639. (defun org-assign-fast-keys (alist)
  2640. "Assign fast keys to a keyword-key alist.
  2641. Respect keys that are already there."
  2642. (let (new e k c c1 c2 (char ?a))
  2643. (while (setq e (pop alist))
  2644. (cond
  2645. ((equal e '(:startgroup)) (push e new))
  2646. ((equal e '(:endgroup)) (push e new))
  2647. (t
  2648. (setq k (car e) c2 nil)
  2649. (if (cdr e)
  2650. (setq c (cdr e))
  2651. ;; automatically assign a character.
  2652. (setq c1 (string-to-char
  2653. (downcase (substring
  2654. k (if (= (string-to-char k) ?@) 1 0)))))
  2655. (if (or (rassoc c1 new) (rassoc c1 alist))
  2656. (while (or (rassoc char new) (rassoc char alist))
  2657. (setq char (1+ char)))
  2658. (setq c2 c1))
  2659. (setq c (or c2 char)))
  2660. (push (cons k c) new))))
  2661. (nreverse new)))
  2662. ;;; Some variables used in various places
  2663. (defvar org-window-configuration nil
  2664. "Used in various places to store a window configuration.")
  2665. (defvar org-finish-function nil
  2666. "Function to be called when `C-c C-c' is used.
  2667. This is for getting out of special buffers like remember.")
  2668. ;; FIXME: Occasionally check by commenting these, to make sure
  2669. ;; no other functions uses these, forgetting to let-bind them.
  2670. (defvar entry)
  2671. (defvar state)
  2672. (defvar last-state)
  2673. (defvar date)
  2674. (defvar description)
  2675. ;; Defined somewhere in this file, but used before definition.
  2676. (defvar org-html-entities)
  2677. (defvar org-struct-menu)
  2678. (defvar org-org-menu)
  2679. (defvar org-tbl-menu)
  2680. (defvar org-agenda-keymap)
  2681. ;;;; Define the Org-mode
  2682. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2683. (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."))
  2684. ;; We use a before-change function to check if a table might need
  2685. ;; an update.
  2686. (defvar org-table-may-need-update t
  2687. "Indicates that a table might need an update.
  2688. This variable is set by `org-before-change-function'.
  2689. `org-table-align' sets it back to nil.")
  2690. (defun org-before-change-function (beg end)
  2691. "Every change indicates that a table might need an update."
  2692. (setq org-table-may-need-update t))
  2693. (defvar org-mode-map)
  2694. (defvar org-mode-hook nil)
  2695. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2696. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2697. (defvar org-table-buffer-is-an nil)
  2698. (defconst org-outline-regexp "\\*+ ")
  2699. ;;;###autoload
  2700. (define-derived-mode org-mode outline-mode "Org"
  2701. "Outline-based notes management and organizer, alias
  2702. \"Carsten's outline-mode for keeping track of everything.\"
  2703. Org-mode develops organizational tasks around a NOTES file which
  2704. contains information about projects as plain text. Org-mode is
  2705. implemented on top of outline-mode, which is ideal to keep the content
  2706. of large files well structured. It supports ToDo items, deadlines and
  2707. time stamps, which magically appear in the diary listing of the Emacs
  2708. calendar. Tables are easily created with a built-in table editor.
  2709. Plain text URL-like links connect to websites, emails (VM), Usenet
  2710. messages (Gnus), BBDB entries, and any files related to the project.
  2711. For printing and sharing of notes, an Org-mode file (or a part of it)
  2712. can be exported as a structured ASCII or HTML file.
  2713. The following commands are available:
  2714. \\{org-mode-map}"
  2715. ;; Get rid of Outline menus, they are not needed
  2716. ;; Need to do this here because define-derived-mode sets up
  2717. ;; the keymap so late. Still, it is a waste to call this each time
  2718. ;; we switch another buffer into org-mode.
  2719. (if (featurep 'xemacs)
  2720. (when (boundp 'outline-mode-menu-heading)
  2721. ;; Assume this is Greg's port, it used easymenu
  2722. (easy-menu-remove outline-mode-menu-heading)
  2723. (easy-menu-remove outline-mode-menu-show)
  2724. (easy-menu-remove outline-mode-menu-hide))
  2725. (define-key org-mode-map [menu-bar headings] 'undefined)
  2726. (define-key org-mode-map [menu-bar hide] 'undefined)
  2727. (define-key org-mode-map [menu-bar show] 'undefined))
  2728. (org-load-modules-maybe)
  2729. (easy-menu-add org-org-menu)
  2730. (easy-menu-add org-tbl-menu)
  2731. (org-install-agenda-files-menu)
  2732. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2733. (org-add-to-invisibility-spec '(org-cwidth))
  2734. (when (featurep 'xemacs)
  2735. (org-set-local 'line-move-ignore-invisible t))
  2736. (org-set-local 'outline-regexp org-outline-regexp)
  2737. (org-set-local 'outline-level 'org-outline-level)
  2738. (when (and org-ellipsis
  2739. (fboundp 'set-display-table-slot) (boundp 'buffer-display-table)
  2740. (fboundp 'make-glyph-code))
  2741. (unless org-display-table
  2742. (setq org-display-table (make-display-table)))
  2743. (set-display-table-slot
  2744. org-display-table 4
  2745. (vconcat (mapcar
  2746. (lambda (c) (make-glyph-code c (and (not (stringp org-ellipsis))
  2747. org-ellipsis)))
  2748. (if (stringp org-ellipsis) org-ellipsis "..."))))
  2749. (setq buffer-display-table org-display-table))
  2750. (org-set-regexps-and-options)
  2751. ;; Calc embedded
  2752. (org-set-local 'calc-embedded-open-mode "# ")
  2753. (modify-syntax-entry ?# "<")
  2754. (modify-syntax-entry ?@ "w")
  2755. (if org-startup-truncated (setq truncate-lines t))
  2756. (org-set-local 'font-lock-unfontify-region-function
  2757. 'org-unfontify-region)
  2758. ;; Activate before-change-function
  2759. (org-set-local 'org-table-may-need-update t)
  2760. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2761. 'local)
  2762. ;; Check for running clock before killing a buffer
  2763. (org-add-hook 'kill-buffer-hook 'org-check-running-clock nil 'local)
  2764. ;; Paragraphs and auto-filling
  2765. (org-set-autofill-regexps)
  2766. (setq indent-line-function 'org-indent-line-function)
  2767. (org-update-radio-target-regexp)
  2768. ;; Comment characters
  2769. ; (org-set-local 'comment-start "#") ;; FIXME: this breaks wrapping
  2770. (org-set-local 'comment-padding " ")
  2771. ;; Align options lines
  2772. (org-set-local
  2773. 'align-mode-rules-list
  2774. '((org-in-buffer-settings
  2775. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  2776. (modes . '(org-mode)))))
  2777. ;; Imenu
  2778. (org-set-local 'imenu-create-index-function
  2779. 'org-imenu-get-tree)
  2780. ;; Make isearch reveal context
  2781. (if (or (featurep 'xemacs)
  2782. (not (boundp 'outline-isearch-open-invisible-function)))
  2783. ;; Emacs 21 and XEmacs make use of the hook
  2784. (org-add-hook 'isearch-mode-end-hook 'org-isearch-end 'append 'local)
  2785. ;; Emacs 22 deals with this through a special variable
  2786. (org-set-local 'outline-isearch-open-invisible-function
  2787. (lambda (&rest ignore) (org-show-context 'isearch))))
  2788. ;; If empty file that did not turn on org-mode automatically, make it to.
  2789. (if (and org-insert-mode-line-in-empty-file
  2790. (interactive-p)
  2791. (= (point-min) (point-max)))
  2792. (insert "# -*- mode: org -*-\n\n"))
  2793. (unless org-inhibit-startup
  2794. (when org-startup-align-all-tables
  2795. (let ((bmp (buffer-modified-p)))
  2796. (org-table-map-tables 'org-table-align)
  2797. (set-buffer-modified-p bmp)))
  2798. (org-cycle-hide-drawers 'all)
  2799. (cond
  2800. ((eq org-startup-folded t)
  2801. (org-cycle '(4)))
  2802. ((eq org-startup-folded 'content)
  2803. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2804. (org-cycle '(4)) (org-cycle '(4)))))))
  2805. (put 'org-mode 'flyspell-mode-predicate 'org-mode-flyspell-verify)
  2806. (defun org-current-time ()
  2807. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2808. (if (> (car org-time-stamp-rounding-minutes) 1)
  2809. (let ((r (car org-time-stamp-rounding-minutes))
  2810. (time (decode-time)))
  2811. (apply 'encode-time
  2812. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2813. (nthcdr 2 time))))
  2814. (current-time)))
  2815. ;;;; Font-Lock stuff, including the activators
  2816. (defvar org-mouse-map (make-sparse-keymap))
  2817. (org-defkey org-mouse-map
  2818. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2819. (org-defkey org-mouse-map
  2820. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2821. (when org-mouse-1-follows-link
  2822. (org-defkey org-mouse-map [follow-link] 'mouse-face))
  2823. (when org-tab-follows-link
  2824. (org-defkey org-mouse-map [(tab)] 'org-open-at-point)
  2825. (org-defkey org-mouse-map "\C-i" 'org-open-at-point))
  2826. (when org-return-follows-link
  2827. (org-defkey org-mouse-map [(return)] 'org-open-at-point)
  2828. (org-defkey org-mouse-map "\C-m" 'org-open-at-point))
  2829. (require 'font-lock)
  2830. (defconst org-non-link-chars "]\t\n\r<>")
  2831. (defvar org-link-types '("http" "https" "ftp" "mailto" "file" "news"
  2832. "shell" "elisp"))
  2833. (defvar org-link-types-re nil
  2834. "Matches a link that has a url-like prefix like \"http:\"")
  2835. (defvar org-link-re-with-space nil
  2836. "Matches a link with spaces, optional angular brackets around it.")
  2837. (defvar org-link-re-with-space2 nil
  2838. "Matches a link with spaces, optional angular brackets around it.")
  2839. (defvar org-angle-link-re nil
  2840. "Matches link with angular brackets, spaces are allowed.")
  2841. (defvar org-plain-link-re nil
  2842. "Matches plain link, without spaces.")
  2843. (defvar org-bracket-link-regexp nil
  2844. "Matches a link in double brackets.")
  2845. (defvar org-bracket-link-analytic-regexp nil
  2846. "Regular expression used to analyze links.
  2847. Here is what the match groups contain after a match:
  2848. 1: http:
  2849. 2: http
  2850. 3: path
  2851. 4: [desc]
  2852. 5: desc")
  2853. (defvar org-any-link-re nil
  2854. "Regular expression matching any link.")
  2855. (defun org-make-link-regexps ()
  2856. "Update the link regular expressions.
  2857. This should be called after the variable `org-link-types' has changed."
  2858. (setq org-link-types-re
  2859. (concat
  2860. "\\`\\(" (mapconcat 'identity org-link-types "\\|") "\\):")
  2861. org-link-re-with-space
  2862. (concat
  2863. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2864. "\\([^" org-non-link-chars " ]"
  2865. "[^" org-non-link-chars "]*"
  2866. "[^" org-non-link-chars " ]\\)>?")
  2867. org-link-re-with-space2
  2868. (concat
  2869. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2870. "\\([^" org-non-link-chars " ]"
  2871. "[^]\t\n\r]*"
  2872. "[^" org-non-link-chars " ]\\)>?")
  2873. org-angle-link-re
  2874. (concat
  2875. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2876. "\\([^" org-non-link-chars " ]"
  2877. "[^" org-non-link-chars "]*"
  2878. "\\)>")
  2879. org-plain-link-re
  2880. (concat
  2881. "\\<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2882. "\\([^]\t\n\r<>() ]+[^]\t\n\r<>,.;() ]\\)")
  2883. org-bracket-link-regexp
  2884. "\\[\\[\\([^][]+\\)\\]\\(\\[\\([^][]+\\)\\]\\)?\\]"
  2885. org-bracket-link-analytic-regexp
  2886. (concat
  2887. "\\[\\["
  2888. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2889. "\\([^]]+\\)"
  2890. "\\]"
  2891. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2892. "\\]")
  2893. org-any-link-re
  2894. (concat "\\(" org-bracket-link-regexp "\\)\\|\\("
  2895. org-angle-link-re "\\)\\|\\("
  2896. org-plain-link-re "\\)")))
  2897. (org-make-link-regexps)
  2898. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)>"
  2899. "Regular expression for fast time stamp matching.")
  2900. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} [^\r\n>]*?\\)[]>]"
  2901. "Regular expression for fast time stamp matching.")
  2902. (defconst org-ts-regexp0 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) *\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2903. "Regular expression matching time strings for analysis.
  2904. This one does not require the space after the date, so it can be used
  2905. on a string that terminates immediately after the date.")
  2906. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\) +\\([^]-+0-9>\r\n ]*\\)\\( \\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2907. "Regular expression matching time strings for analysis.")
  2908. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 "[^>\n]\\{0,16\\}>")
  2909. "Regular expression matching time stamps, with groups.")
  2910. (defconst org-ts-regexp3 (concat "[[<]" org-ts-regexp1 "[^]>\n]\\{0,16\\}[]>]")
  2911. "Regular expression matching time stamps (also [..]), with groups.")
  2912. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2913. "Regular expression matching a time stamp range.")
  2914. (defconst org-tr-regexp-both
  2915. (concat org-ts-regexp-both "--?-?" org-ts-regexp-both)
  2916. "Regular expression matching a time stamp range.")
  2917. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2918. org-ts-regexp "\\)?")
  2919. "Regular expression matching a time stamp or time stamp range.")
  2920. (defconst org-tsr-regexp-both (concat org-ts-regexp-both "\\(--?-?"
  2921. org-ts-regexp-both "\\)?")
  2922. "Regular expression matching a time stamp or time stamp range.
  2923. The time stamps may be either active or inactive.")
  2924. (defvar org-emph-face nil)
  2925. (defun org-do-emphasis-faces (limit)
  2926. "Run through the buffer and add overlays to links."
  2927. (let (rtn)
  2928. (while (and (not rtn) (re-search-forward org-emph-re limit t))
  2929. (if (not (= (char-after (match-beginning 3))
  2930. (char-after (match-beginning 4))))
  2931. (progn
  2932. (setq rtn t)
  2933. (font-lock-prepend-text-property (match-beginning 2) (match-end 2)
  2934. 'face
  2935. (nth 1 (assoc (match-string 3)
  2936. org-emphasis-alist)))
  2937. (add-text-properties (match-beginning 2) (match-end 2)
  2938. '(font-lock-multiline t))
  2939. (when org-hide-emphasis-markers
  2940. (add-text-properties (match-end 4) (match-beginning 5)
  2941. '(invisible org-link))
  2942. (add-text-properties (match-beginning 3) (match-end 3)
  2943. '(invisible org-link)))))
  2944. (backward-char 1))
  2945. rtn))
  2946. (defun org-emphasize (&optional char)
  2947. "Insert or change an emphasis, i.e. a font like bold or italic.
  2948. If there is an active region, change that region to a new emphasis.
  2949. If there is no region, just insert the marker characters and position
  2950. the cursor between them.
  2951. CHAR should be either the marker character, or the first character of the
  2952. HTML tag associated with that emphasis. If CHAR is a space, the means
  2953. to remove the emphasis of the selected region.
  2954. If char is not given (for example in an interactive call) it
  2955. will be prompted for."
  2956. (interactive)
  2957. (let ((eal org-emphasis-alist) e det
  2958. (erc org-emphasis-regexp-components)
  2959. (prompt "")
  2960. (string "") beg end move tag c s)
  2961. (if (org-region-active-p)
  2962. (setq beg (region-beginning) end (region-end)
  2963. string (buffer-substring beg end))
  2964. (setq move t))
  2965. (while (setq e (pop eal))
  2966. (setq tag (car (org-split-string (nth 2 e) "[ <>/]+"))
  2967. c (aref tag 0))
  2968. (push (cons c (string-to-char (car e))) det)
  2969. (setq prompt (concat prompt (format " [%s%c]%s" (car e) c
  2970. (substring tag 1)))))
  2971. (unless char
  2972. (message "%s" (concat "Emphasis marker or tag:" prompt))
  2973. (setq char (read-char-exclusive)))
  2974. (setq char (or (cdr (assoc char det)) char))
  2975. (if (equal char ?\ )
  2976. (setq s "" move nil)
  2977. (unless (assoc (char-to-string char) org-emphasis-alist)
  2978. (error "No such emphasis marker: \"%c\"" char))
  2979. (setq s (char-to-string char)))
  2980. (while (and (> (length string) 1)
  2981. (equal (substring string 0 1) (substring string -1))
  2982. (assoc (substring string 0 1) org-emphasis-alist))
  2983. (setq string (substring string 1 -1)))
  2984. (setq string (concat s string s))
  2985. (if beg (delete-region beg end))
  2986. (unless (or (bolp)
  2987. (string-match (concat "[" (nth 0 erc) "\n]")
  2988. (char-to-string (char-before (point)))))
  2989. (insert " "))
  2990. (unless (string-match (concat "[" (nth 1 erc) "\n]")
  2991. (char-to-string (char-after (point))))
  2992. (insert " ") (backward-char 1))
  2993. (insert string)
  2994. (and move (backward-char 1))))
  2995. (defconst org-nonsticky-props
  2996. '(mouse-face highlight keymap invisible intangible help-echo org-linked-text))
  2997. (defun org-activate-plain-links (limit)
  2998. "Run through the buffer and add overlays to links."
  2999. (catch 'exit
  3000. (let (f)
  3001. (while (re-search-forward org-plain-link-re limit t)
  3002. (setq f (get-text-property (match-beginning 0) 'face))
  3003. (if (or (eq f 'org-tag)
  3004. (and (listp f) (memq 'org-tag f)))
  3005. nil
  3006. (add-text-properties (match-beginning 0) (match-end 0)
  3007. (list 'mouse-face 'highlight
  3008. 'rear-nonsticky org-nonsticky-props
  3009. 'keymap org-mouse-map
  3010. ))
  3011. (throw 'exit t))))))
  3012. (defun org-activate-code (limit)
  3013. (if (re-search-forward "^[ \t]*\\(:.*\\)" limit t)
  3014. (unless (get-text-property (match-beginning 1) 'face)
  3015. (remove-text-properties (match-beginning 0) (match-end 0)
  3016. '(display t invisible t intangible t))
  3017. t)))
  3018. (defun org-activate-angle-links (limit)
  3019. "Run through the buffer and add overlays to links."
  3020. (if (re-search-forward org-angle-link-re limit t)
  3021. (progn
  3022. (add-text-properties (match-beginning 0) (match-end 0)
  3023. (list 'mouse-face 'highlight
  3024. 'rear-nonsticky org-nonsticky-props
  3025. 'keymap org-mouse-map
  3026. ))
  3027. t)))
  3028. (defun org-activate-bracket-links (limit)
  3029. "Run through the buffer and add overlays to bracketed links."
  3030. (if (re-search-forward org-bracket-link-regexp limit t)
  3031. (let* ((help (concat "LINK: "
  3032. (org-match-string-no-properties 1)))
  3033. ;; FIXME: above we should remove the escapes.
  3034. ;; but that requires another match, protecting match data,
  3035. ;; a lot of overhead for font-lock.
  3036. (ip (org-maybe-intangible
  3037. (list 'invisible 'org-link 'rear-nonsticky org-nonsticky-props
  3038. 'keymap org-mouse-map 'mouse-face 'highlight
  3039. 'font-lock-multiline t 'help-echo help)))
  3040. (vp (list 'rear-nonsticky org-nonsticky-props
  3041. 'keymap org-mouse-map 'mouse-face 'highlight
  3042. ' font-lock-multiline t 'help-echo help)))
  3043. ;; We need to remove the invisible property here. Table narrowing
  3044. ;; may have made some of this invisible.
  3045. (remove-text-properties (match-beginning 0) (match-end 0)
  3046. '(invisible nil))
  3047. (if (match-end 3)
  3048. (progn
  3049. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  3050. (add-text-properties (match-beginning 3) (match-end 3) vp)
  3051. (add-text-properties (match-end 3) (match-end 0) ip))
  3052. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  3053. (add-text-properties (match-beginning 1) (match-end 1) vp)
  3054. (add-text-properties (match-end 1) (match-end 0) ip))
  3055. t)))
  3056. (defun org-activate-dates (limit)
  3057. "Run through the buffer and add overlays to dates."
  3058. (if (re-search-forward org-tsr-regexp-both limit t)
  3059. (progn
  3060. (add-text-properties (match-beginning 0) (match-end 0)
  3061. (list 'mouse-face 'highlight
  3062. 'rear-nonsticky org-nonsticky-props
  3063. 'keymap org-mouse-map))
  3064. (when org-display-custom-times
  3065. (if (match-end 3)
  3066. (org-display-custom-time (match-beginning 3) (match-end 3)))
  3067. (org-display-custom-time (match-beginning 1) (match-end 1)))
  3068. t)))
  3069. (defvar org-target-link-regexp nil
  3070. "Regular expression matching radio targets in plain text.")
  3071. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  3072. "Regular expression matching a link target.")
  3073. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  3074. "Regular expression matching a radio target.")
  3075. (defvar org-any-target-regexp "<<<?\\([^<>\n\r]+\\)>>>?" ; FIXME, not exact, would match <<<aaa>> as a radio target.
  3076. "Regular expression matching any target.")
  3077. (defun org-activate-target-links (limit)
  3078. "Run through the buffer and add overlays to target matches."
  3079. (when org-target-link-regexp
  3080. (let ((case-fold-search t))
  3081. (if (re-search-forward org-target-link-regexp limit t)
  3082. (progn
  3083. (add-text-properties (match-beginning 0) (match-end 0)
  3084. (list 'mouse-face 'highlight
  3085. 'rear-nonsticky org-nonsticky-props
  3086. 'keymap org-mouse-map
  3087. 'help-echo "Radio target link"
  3088. 'org-linked-text t))
  3089. t)))))
  3090. (defun org-update-radio-target-regexp ()
  3091. "Find all radio targets in this file and update the regular expression."
  3092. (interactive)
  3093. (when (memq 'radio org-activate-links)
  3094. (setq org-target-link-regexp
  3095. (org-make-target-link-regexp (org-all-targets 'radio)))
  3096. (org-restart-font-lock)))
  3097. (defun org-hide-wide-columns (limit)
  3098. (let (s e)
  3099. (setq s (text-property-any (point) (or limit (point-max))
  3100. 'org-cwidth t))
  3101. (when s
  3102. (setq e (next-single-property-change s 'org-cwidth))
  3103. (add-text-properties s e (org-maybe-intangible '(invisible org-cwidth)))
  3104. (goto-char e)
  3105. t)))
  3106. (defvar org-latex-and-specials-regexp nil
  3107. "Regular expression for highlighting export special stuff.")
  3108. (defvar org-match-substring-regexp)
  3109. (defvar org-match-substring-with-braces-regexp)
  3110. (defvar org-export-html-special-string-regexps)
  3111. (defun org-compute-latex-and-specials-regexp ()
  3112. "Compute regular expression for stuff treated specially by exporters."
  3113. (if (not org-highlight-latex-fragments-and-specials)
  3114. (org-set-local 'org-latex-and-specials-regexp nil)
  3115. (require 'org-exp)
  3116. (let*
  3117. ((matchers (plist-get org-format-latex-options :matchers))
  3118. (latexs (delq nil (mapcar (lambda (x) (if (member (car x) matchers) x))
  3119. org-latex-regexps)))
  3120. (options (org-combine-plists (org-default-export-plist)
  3121. (org-infile-export-plist)))
  3122. (org-export-with-sub-superscripts (plist-get options :sub-superscript))
  3123. (org-export-with-LaTeX-fragments (plist-get options :LaTeX-fragments))
  3124. (org-export-with-TeX-macros (plist-get options :TeX-macros))
  3125. (org-export-html-expand (plist-get options :expand-quoted-html))
  3126. (org-export-with-special-strings (plist-get options :special-strings))
  3127. (re-sub
  3128. (cond
  3129. ((equal org-export-with-sub-superscripts '{})
  3130. (list org-match-substring-with-braces-regexp))
  3131. (org-export-with-sub-superscripts
  3132. (list org-match-substring-regexp))
  3133. (t nil)))
  3134. (re-latex
  3135. (if org-export-with-LaTeX-fragments
  3136. (mapcar (lambda (x) (nth 1 x)) latexs)))
  3137. (re-macros
  3138. (if org-export-with-TeX-macros
  3139. (list (concat "\\\\"
  3140. (regexp-opt
  3141. (append (mapcar 'car org-html-entities)
  3142. (if (boundp 'org-latex-entities)
  3143. org-latex-entities nil))
  3144. 'words))) ; FIXME
  3145. ))
  3146. ;; (list "\\\\\\(?:[a-zA-Z]+\\)")))
  3147. (re-special (if org-export-with-special-strings
  3148. (mapcar (lambda (x) (car x))
  3149. org-export-html-special-string-regexps)))
  3150. (re-rest
  3151. (delq nil
  3152. (list
  3153. (if org-export-html-expand "@<[^>\n]+>")
  3154. ))))
  3155. (org-set-local
  3156. 'org-latex-and-specials-regexp
  3157. (mapconcat 'identity (append re-latex re-sub re-macros re-special
  3158. re-rest) "\\|")))))
  3159. (defun org-do-latex-and-special-faces (limit)
  3160. "Run through the buffer and add overlays to links."
  3161. (when org-latex-and-specials-regexp
  3162. (let (rtn d)
  3163. (while (and (not rtn) (re-search-forward org-latex-and-specials-regexp
  3164. limit t))
  3165. (if (not (memq (car-safe (get-text-property (1+ (match-beginning 0))
  3166. 'face))
  3167. '(org-code org-verbatim underline)))
  3168. (progn
  3169. (setq rtn t
  3170. d (cond ((member (char-after (1+ (match-beginning 0)))
  3171. '(?_ ?^)) 1)
  3172. (t 0)))
  3173. (font-lock-prepend-text-property
  3174. (+ d (match-beginning 0)) (match-end 0)
  3175. 'face 'org-latex-and-export-specials)
  3176. (add-text-properties (+ d (match-beginning 0)) (match-end 0)
  3177. '(font-lock-multiline t)))))
  3178. rtn)))
  3179. (defun org-restart-font-lock ()
  3180. "Restart font-lock-mode, to force refontification."
  3181. (when (and (boundp 'font-lock-mode) font-lock-mode)
  3182. (font-lock-mode -1)
  3183. (font-lock-mode 1)))
  3184. (defun org-all-targets (&optional radio)
  3185. "Return a list of all targets in this file.
  3186. With optional argument RADIO, only find radio targets."
  3187. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  3188. rtn)
  3189. (save-excursion
  3190. (goto-char (point-min))
  3191. (while (re-search-forward re nil t)
  3192. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  3193. rtn)))
  3194. (defun org-make-target-link-regexp (targets)
  3195. "Make regular expression matching all strings in TARGETS.
  3196. The regular expression finds the targets also if there is a line break
  3197. between words."
  3198. (and targets
  3199. (concat
  3200. "\\<\\("
  3201. (mapconcat
  3202. (lambda (x)
  3203. (while (string-match " +" x)
  3204. (setq x (replace-match "\\s-+" t t x)))
  3205. x)
  3206. targets
  3207. "\\|")
  3208. "\\)\\>")))
  3209. (defun org-activate-tags (limit)
  3210. (if (re-search-forward (org-re "^\\*+.*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \r\n]") limit t)
  3211. (progn
  3212. (add-text-properties (match-beginning 1) (match-end 1)
  3213. (list 'mouse-face 'highlight
  3214. 'rear-nonsticky org-nonsticky-props
  3215. 'keymap org-mouse-map))
  3216. t)))
  3217. (defun org-outline-level ()
  3218. (save-excursion
  3219. (looking-at outline-regexp)
  3220. (if (match-beginning 1)
  3221. (+ (org-get-string-indentation (match-string 1)) 1000)
  3222. (1- (- (match-end 0) (match-beginning 0))))))
  3223. (defvar org-font-lock-keywords nil)
  3224. (defconst org-property-re (org-re "^[ \t]*\\(:\\([[:alnum:]_]+\\):\\)[ \t]*\\([^ \t\r\n].*\\)")
  3225. "Regular expression matching a property line.")
  3226. (defvar org-font-lock-hook nil
  3227. "Functions to be called for special font loch stuff.")
  3228. (defun org-font-lock-hook (limit)
  3229. (run-hook-with-args 'org-font-lock-hook limit))
  3230. (defun org-set-font-lock-defaults ()
  3231. (let* ((em org-fontify-emphasized-text)
  3232. (lk org-activate-links)
  3233. (org-font-lock-extra-keywords
  3234. (list
  3235. ;; Call the hook
  3236. '(org-font-lock-hook)
  3237. ;; Headlines
  3238. '("^\\(\\**\\)\\(\\* \\)\\(.*\\)" (1 (org-get-level-face 1))
  3239. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  3240. ;; Table lines
  3241. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  3242. (1 'org-table t))
  3243. ;; Table internals
  3244. '("^[ \t]*|\\(?:.*?|\\)? *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  3245. '("^[ \t]*| *\\([#*]\\) *|" (1 'org-formula t))
  3246. '("^[ \t]*|\\( *\\([$!_^/]\\) *|.*\\)|" (1 'org-formula t))
  3247. ;; Drawers
  3248. (list org-drawer-regexp '(0 'org-special-keyword t))
  3249. (list "^[ \t]*:END:" '(0 'org-special-keyword t))
  3250. ;; Properties
  3251. (list org-property-re
  3252. '(1 'org-special-keyword t)
  3253. '(3 'org-property-value t))
  3254. (if org-format-transports-properties-p
  3255. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  3256. ;; Links
  3257. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  3258. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  3259. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  3260. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  3261. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  3262. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  3263. '("^&?%%(.*\\|<%%([^>\n]*?>" (0 'org-sexp-date t))
  3264. '(org-hide-wide-columns (0 nil append))
  3265. ;; TODO lines
  3266. (list (concat "^\\*+[ \t]+" org-todo-regexp)
  3267. '(1 (org-get-todo-face 1) t))
  3268. ;; DONE
  3269. (if org-fontify-done-headline
  3270. (list (concat "^[*]+ +\\<\\("
  3271. (mapconcat 'regexp-quote org-done-keywords "\\|")
  3272. "\\)\\(.*\\)")
  3273. '(2 'org-headline-done t))
  3274. nil)
  3275. ;; Priorities
  3276. (list (concat "\\[#[A-Z0-9]\\]") '(0 'org-special-keyword t))
  3277. ;; Special keywords
  3278. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  3279. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  3280. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  3281. (list (concat "\\<" org-clock-string) '(0 'org-special-keyword t))
  3282. ;; Emphasis
  3283. (if em
  3284. (if (featurep 'xemacs)
  3285. '(org-do-emphasis-faces (0 nil append))
  3286. '(org-do-emphasis-faces)))
  3287. ;; Checkboxes
  3288. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)"
  3289. 2 'bold prepend)
  3290. (if org-provide-checkbox-statistics
  3291. '("\\[\\([0-9]*%\\)\\]\\|\\[\\([0-9]*\\)/\\([0-9]*\\)\\]"
  3292. (0 (org-get-checkbox-statistics-face) t)))
  3293. ;; Description list items
  3294. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(.*?\\) ::"
  3295. 2 'bold prepend)
  3296. (list (concat "^\\*+ \\(.*:" org-archive-tag ":.*\\)")
  3297. '(1 'org-archived prepend))
  3298. ;; Specials
  3299. '(org-do-latex-and-special-faces)
  3300. ;; Code
  3301. '(org-activate-code (1 'org-code t))
  3302. ;; COMMENT
  3303. (list (concat "^\\*+[ \t]+\\<\\(" org-comment-string
  3304. "\\|" org-quote-string "\\)\\>")
  3305. '(1 'org-special-keyword t))
  3306. '("^#.*" (0 'font-lock-comment-face t))
  3307. )))
  3308. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  3309. ;; Now set the full font-lock-keywords
  3310. (org-set-local 'org-font-lock-keywords org-font-lock-extra-keywords)
  3311. (org-set-local 'font-lock-defaults
  3312. '(org-font-lock-keywords t nil nil backward-paragraph))
  3313. (kill-local-variable 'font-lock-keywords) nil))
  3314. (defvar org-m nil)
  3315. (defvar org-l nil)
  3316. (defvar org-f nil)
  3317. (defun org-get-level-face (n)
  3318. "Get the right face for match N in font-lock matching of healdines."
  3319. (setq org-l (- (match-end 2) (match-beginning 1) 1))
  3320. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  3321. (setq org-f (nth (% (1- org-l) org-n-level-faces) org-level-faces))
  3322. (cond
  3323. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  3324. ((eq n 2) org-f)
  3325. (t (if org-level-color-stars-only nil org-f))))
  3326. (defun org-get-todo-face (kwd)
  3327. "Get the right face for a TODO keyword KWD.
  3328. If KWD is a number, get the corresponding match group."
  3329. (if (numberp kwd) (setq kwd (match-string kwd)))
  3330. (or (cdr (assoc kwd org-todo-keyword-faces))
  3331. (and (member kwd org-done-keywords) 'org-done)
  3332. 'org-todo))
  3333. (defun org-unfontify-region (beg end &optional maybe_loudly)
  3334. "Remove fontification and activation overlays from links."
  3335. (font-lock-default-unfontify-region beg end)
  3336. (let* ((buffer-undo-list t)
  3337. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  3338. (inhibit-modification-hooks t)
  3339. deactivate-mark buffer-file-name buffer-file-truename)
  3340. (remove-text-properties beg end
  3341. '(mouse-face t keymap t org-linked-text t
  3342. invisible t intangible t))))
  3343. ;;;; Visibility cycling, including org-goto and indirect buffer
  3344. ;;; Cycling
  3345. (defvar org-cycle-global-status nil)
  3346. (make-variable-buffer-local 'org-cycle-global-status)
  3347. (defvar org-cycle-subtree-status nil)
  3348. (make-variable-buffer-local 'org-cycle-subtree-status)
  3349. ;;;###autoload
  3350. (defun org-cycle (&optional arg)
  3351. "Visibility cycling for Org-mode.
  3352. - When this function is called with a prefix argument, rotate the entire
  3353. buffer through 3 states (global cycling)
  3354. 1. OVERVIEW: Show only top-level headlines.
  3355. 2. CONTENTS: Show all headlines of all levels, but no body text.
  3356. 3. SHOW ALL: Show everything.
  3357. - When point is at the beginning of a headline, rotate the subtree started
  3358. by this line through 3 different states (local cycling)
  3359. 1. FOLDED: Only the main headline is shown.
  3360. 2. CHILDREN: The main headline and the direct children are shown.
  3361. From this state, you can move to one of the children
  3362. and zoom in further.
  3363. 3. SUBTREE: Show the entire subtree, including body text.
  3364. - When there is a numeric prefix, go up to a heading with level ARG, do
  3365. a `show-subtree' and return to the previous cursor position. If ARG
  3366. is negative, go up that many levels.
  3367. - When point is not at the beginning of a headline, execute
  3368. `indent-relative', like TAB normally does. See the option
  3369. `org-cycle-emulate-tab' for details.
  3370. - Special case: if point is at the beginning of the buffer and there is
  3371. no headline in line 1, this function will act as if called with prefix arg.
  3372. But only if also the variable `org-cycle-global-at-bob' is t."
  3373. (interactive "P")
  3374. (org-load-modules-maybe)
  3375. (let* ((outline-regexp
  3376. (if (and (org-mode-p) org-cycle-include-plain-lists)
  3377. "\\(?:\\*+ \\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) \\)"
  3378. outline-regexp))
  3379. (bob-special (and org-cycle-global-at-bob (bobp)
  3380. (not (looking-at outline-regexp))))
  3381. (org-cycle-hook
  3382. (if bob-special
  3383. (delq 'org-optimize-window-after-visibility-change
  3384. (copy-sequence org-cycle-hook))
  3385. org-cycle-hook))
  3386. (pos (point)))
  3387. (if (or bob-special (equal arg '(4)))
  3388. ;; special case: use global cycling
  3389. (setq arg t))
  3390. (cond
  3391. ((org-at-table-p 'any)
  3392. ;; Enter the table or move to the next field in the table
  3393. (or (org-table-recognize-table.el)
  3394. (progn
  3395. (if arg (org-table-edit-field t)
  3396. (org-table-justify-field-maybe)
  3397. (call-interactively 'org-table-next-field)))))
  3398. ((eq arg t) ;; Global cycling
  3399. (cond
  3400. ((and (eq last-command this-command)
  3401. (eq org-cycle-global-status 'overview))
  3402. ;; We just created the overview - now do table of contents
  3403. ;; This can be slow in very large buffers, so indicate action
  3404. (message "CONTENTS...")
  3405. (org-content)
  3406. (message "CONTENTS...done")
  3407. (setq org-cycle-global-status 'contents)
  3408. (run-hook-with-args 'org-cycle-hook 'contents))
  3409. ((and (eq last-command this-command)
  3410. (eq org-cycle-global-status 'contents))
  3411. ;; We just showed the table of contents - now show everything
  3412. (show-all)
  3413. (message "SHOW ALL")
  3414. (setq org-cycle-global-status 'all)
  3415. (run-hook-with-args 'org-cycle-hook 'all))
  3416. (t
  3417. ;; Default action: go to overview
  3418. (org-overview)
  3419. (message "OVERVIEW")
  3420. (setq org-cycle-global-status 'overview)
  3421. (run-hook-with-args 'org-cycle-hook 'overview))))
  3422. ((and org-drawers org-drawer-regexp
  3423. (save-excursion
  3424. (beginning-of-line 1)
  3425. (looking-at org-drawer-regexp)))
  3426. ;; Toggle block visibility
  3427. (org-flag-drawer
  3428. (not (get-char-property (match-end 0) 'invisible))))
  3429. ((integerp arg)
  3430. ;; Show-subtree, ARG levels up from here.
  3431. (save-excursion
  3432. (org-back-to-heading)
  3433. (outline-up-heading (if (< arg 0) (- arg)
  3434. (- (funcall outline-level) arg)))
  3435. (org-show-subtree)))
  3436. ((and (save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  3437. (or (bolp) (not (eq org-cycle-emulate-tab 'exc-hl-bol))))
  3438. ;; At a heading: rotate between three different views
  3439. (org-back-to-heading)
  3440. (let ((goal-column 0) eoh eol eos)
  3441. ;; First, some boundaries
  3442. (save-excursion
  3443. (org-back-to-heading)
  3444. (save-excursion
  3445. (beginning-of-line 2)
  3446. (while (and (not (eobp)) ;; this is like `next-line'
  3447. (get-char-property (1- (point)) 'invisible))
  3448. (beginning-of-line 2)) (setq eol (point)))
  3449. (outline-end-of-heading) (setq eoh (point))
  3450. (org-end-of-subtree t)
  3451. (unless (eobp)
  3452. (skip-chars-forward " \t\n")
  3453. (beginning-of-line 1) ; in case this is an item
  3454. )
  3455. (setq eos (1- (point))))
  3456. ;; Find out what to do next and set `this-command'
  3457. (cond
  3458. ((= eos eoh)
  3459. ;; Nothing is hidden behind this heading
  3460. (message "EMPTY ENTRY")
  3461. (setq org-cycle-subtree-status nil)
  3462. (save-excursion
  3463. (goto-char eos)
  3464. (outline-next-heading)
  3465. (if (org-invisible-p) (org-flag-heading nil))))
  3466. ((or (>= eol eos)
  3467. (not (string-match "\\S-" (buffer-substring eol eos))))
  3468. ;; Entire subtree is hidden in one line: open it
  3469. (org-show-entry)
  3470. (show-children)
  3471. (message "CHILDREN")
  3472. (save-excursion
  3473. (goto-char eos)
  3474. (outline-next-heading)
  3475. (if (org-invisible-p) (org-flag-heading nil)))
  3476. (setq org-cycle-subtree-status 'children)
  3477. (run-hook-with-args 'org-cycle-hook 'children))
  3478. ((and (eq last-command this-command)
  3479. (eq org-cycle-subtree-status 'children))
  3480. ;; We just showed the children, now show everything.
  3481. (org-show-subtree)
  3482. (message "SUBTREE")
  3483. (setq org-cycle-subtree-status 'subtree)
  3484. (run-hook-with-args 'org-cycle-hook 'subtree))
  3485. (t
  3486. ;; Default action: hide the subtree.
  3487. (hide-subtree)
  3488. (message "FOLDED")
  3489. (setq org-cycle-subtree-status 'folded)
  3490. (run-hook-with-args 'org-cycle-hook 'folded)))))
  3491. ;; TAB emulation
  3492. (buffer-read-only (org-back-to-heading))
  3493. ((org-try-cdlatex-tab))
  3494. ((and (eq org-cycle-emulate-tab 'exc-hl-bol)
  3495. (or (not (bolp))
  3496. (not (looking-at outline-regexp))))
  3497. (call-interactively (global-key-binding "\t")))
  3498. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  3499. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  3500. (or (and (eq org-cycle-emulate-tab 'white)
  3501. (= (match-end 0) (point-at-eol)))
  3502. (and (eq org-cycle-emulate-tab 'whitestart)
  3503. (>= (match-end 0) pos))))
  3504. t
  3505. (eq org-cycle-emulate-tab t))
  3506. (call-interactively (global-key-binding "\t")))
  3507. (t (save-excursion
  3508. (org-back-to-heading)
  3509. (org-cycle))))))
  3510. ;;;###autoload
  3511. (defun org-global-cycle (&optional arg)
  3512. "Cycle the global visibility. For details see `org-cycle'."
  3513. (interactive "P")
  3514. (let ((org-cycle-include-plain-lists
  3515. (if (org-mode-p) org-cycle-include-plain-lists nil)))
  3516. (if (integerp arg)
  3517. (progn
  3518. (show-all)
  3519. (hide-sublevels arg)
  3520. (setq org-cycle-global-status 'contents))
  3521. (org-cycle '(4)))))
  3522. (defun org-overview ()
  3523. "Switch to overview mode, shoing only top-level headlines.
  3524. Really, this shows all headlines with level equal or greater than the level
  3525. of the first headline in the buffer. This is important, because if the
  3526. first headline is not level one, then (hide-sublevels 1) gives confusing
  3527. results."
  3528. (interactive)
  3529. (let ((level (save-excursion
  3530. (goto-char (point-min))
  3531. (if (re-search-forward (concat "^" outline-regexp) nil t)
  3532. (progn
  3533. (goto-char (match-beginning 0))
  3534. (funcall outline-level))))))
  3535. (and level (hide-sublevels level))))
  3536. (defun org-content (&optional arg)
  3537. "Show all headlines in the buffer, like a table of contents.
  3538. With numerical argument N, show content up to level N."
  3539. (interactive "P")
  3540. (save-excursion
  3541. ;; Visit all headings and show their offspring
  3542. (and (integerp arg) (org-overview))
  3543. (goto-char (point-max))
  3544. (catch 'exit
  3545. (while (and (progn (condition-case nil
  3546. (outline-previous-visible-heading 1)
  3547. (error (goto-char (point-min))))
  3548. t)
  3549. (looking-at outline-regexp))
  3550. (if (integerp arg)
  3551. (show-children (1- arg))
  3552. (show-branches))
  3553. (if (bobp) (throw 'exit nil))))))
  3554. (defun org-optimize-window-after-visibility-change (state)
  3555. "Adjust the window after a change in outline visibility.
  3556. This function is the default value of the hook `org-cycle-hook'."
  3557. (when (get-buffer-window (current-buffer))
  3558. (cond
  3559. ; ((eq state 'overview) (org-first-headline-recenter 1))
  3560. ; ((eq state 'overview) (org-beginning-of-line))
  3561. ((eq state 'content) nil)
  3562. ((eq state 'all) nil)
  3563. ((eq state 'folded) nil)
  3564. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  3565. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  3566. (defun org-compact-display-after-subtree-move ()
  3567. (let (beg end)
  3568. (save-excursion
  3569. (if (org-up-heading-safe)
  3570. (progn
  3571. (hide-subtree)
  3572. (show-entry)
  3573. (show-children)
  3574. (org-cycle-show-empty-lines 'children)
  3575. (org-cycle-hide-drawers 'children))
  3576. (org-overview)))))
  3577. (defun org-cycle-show-empty-lines (state)
  3578. "Show empty lines above all visible headlines.
  3579. The region to be covered depends on STATE when called through
  3580. `org-cycle-hook'. Lisp program can use t for STATE to get the
  3581. entire buffer covered. Note that an empty line is only shown if there
  3582. are at least `org-cycle-separator-lines' empty lines before the headeline."
  3583. (when (> org-cycle-separator-lines 0)
  3584. (save-excursion
  3585. (let* ((n org-cycle-separator-lines)
  3586. (re (cond
  3587. ((= n 1) "\\(\n[ \t]*\n\\*+\\) ")
  3588. ((= n 2) "^[ \t]*\\(\n[ \t]*\n\\*+\\) ")
  3589. (t (let ((ns (number-to-string (- n 2))))
  3590. (concat "^\\(?:[ \t]*\n\\)\\{" ns "," ns "\\}"
  3591. "[ \t]*\\(\n[ \t]*\n\\*+\\) ")))))
  3592. beg end)
  3593. (cond
  3594. ((memq state '(overview contents t))
  3595. (setq beg (point-min) end (point-max)))
  3596. ((memq state '(children folded))
  3597. (setq beg (point) end (progn (org-end-of-subtree t t)
  3598. (beginning-of-line 2)
  3599. (point)))))
  3600. (when beg
  3601. (goto-char beg)
  3602. (while (re-search-forward re end t)
  3603. (if (not (get-char-property (match-end 1) 'invisible))
  3604. (outline-flag-region
  3605. (match-beginning 1) (match-end 1) nil)))))))
  3606. ;; Never hide empty lines at the end of the file.
  3607. (save-excursion
  3608. (goto-char (point-max))
  3609. (outline-previous-heading)
  3610. (outline-end-of-heading)
  3611. (if (and (looking-at "[ \t\n]+")
  3612. (= (match-end 0) (point-max)))
  3613. (outline-flag-region (point) (match-end 0) nil))))
  3614. (defun org-cycle-hide-drawers (state)
  3615. "Re-hide all drawers after a visibility state change."
  3616. (when (and (org-mode-p)
  3617. (not (memq state '(overview folded))))
  3618. (save-excursion
  3619. (let* ((globalp (memq state '(contents all)))
  3620. (beg (if globalp (point-min) (point)))
  3621. (end (if globalp (point-max) (org-end-of-subtree t))))
  3622. (goto-char beg)
  3623. (while (re-search-forward org-drawer-regexp end t)
  3624. (org-flag-drawer t))))))
  3625. (defun org-flag-drawer (flag)
  3626. (save-excursion
  3627. (beginning-of-line 1)
  3628. (when (looking-at "^[ \t]*:[a-zA-Z][a-zA-Z0-9]*:")
  3629. (let ((b (match-end 0))
  3630. (outline-regexp org-outline-regexp))
  3631. (if (re-search-forward
  3632. "^[ \t]*:END:"
  3633. (save-excursion (outline-next-heading) (point)) t)
  3634. (outline-flag-region b (point-at-eol) flag)
  3635. (error ":END: line missing"))))))
  3636. (defun org-subtree-end-visible-p ()
  3637. "Is the end of the current subtree visible?"
  3638. (pos-visible-in-window-p
  3639. (save-excursion (org-end-of-subtree t) (point))))
  3640. (defun org-first-headline-recenter (&optional N)
  3641. "Move cursor to the first headline and recenter the headline.
  3642. Optional argument N means, put the headline into the Nth line of the window."
  3643. (goto-char (point-min))
  3644. (when (re-search-forward (concat "^\\(" outline-regexp "\\)") nil t)
  3645. (beginning-of-line)
  3646. (recenter (prefix-numeric-value N))))
  3647. ;;; Org-goto
  3648. (defvar org-goto-window-configuration nil)
  3649. (defvar org-goto-marker nil)
  3650. (defvar org-goto-map
  3651. (let ((map (make-sparse-keymap)))
  3652. (let ((cmds '(isearch-forward isearch-backward kill-ring-save set-mark-command mouse-drag-region universal-argument org-occur)) cmd)
  3653. (while (setq cmd (pop cmds))
  3654. (substitute-key-definition cmd cmd map global-map)))
  3655. (suppress-keymap map)
  3656. (org-defkey map "\C-m" 'org-goto-ret)
  3657. (org-defkey map [(return)] 'org-goto-ret)
  3658. (org-defkey map [(left)] 'org-goto-left)
  3659. (org-defkey map [(right)] 'org-goto-right)
  3660. (org-defkey map [(control ?g)] 'org-goto-quit)
  3661. (org-defkey map "\C-i" 'org-cycle)
  3662. (org-defkey map [(tab)] 'org-cycle)
  3663. (org-defkey map [(down)] 'outline-next-visible-heading)
  3664. (org-defkey map [(up)] 'outline-previous-visible-heading)
  3665. (if org-goto-auto-isearch
  3666. (if (fboundp 'define-key-after)
  3667. (define-key-after map [t] 'org-goto-local-auto-isearch)
  3668. nil)
  3669. (org-defkey map "q" 'org-goto-quit)
  3670. (org-defkey map "n" 'outline-next-visible-heading)
  3671. (org-defkey map "p" 'outline-previous-visible-heading)
  3672. (org-defkey map "f" 'outline-forward-same-level)
  3673. (org-defkey map "b" 'outline-backward-same-level)
  3674. (org-defkey map "u" 'outline-up-heading))
  3675. (org-defkey map "/" 'org-occur)
  3676. (org-defkey map "\C-c\C-n" 'outline-next-visible-heading)
  3677. (org-defkey map "\C-c\C-p" 'outline-previous-visible-heading)
  3678. (org-defkey map "\C-c\C-f" 'outline-forward-same-level)
  3679. (org-defkey map "\C-c\C-b" 'outline-backward-same-level)
  3680. (org-defkey map "\C-c\C-u" 'outline-up-heading)
  3681. map))
  3682. (defconst org-goto-help
  3683. "Browse buffer copy, to find location or copy text. Just type for auto-isearch.
  3684. RET=jump to location [Q]uit and return to previous location
  3685. \[Up]/[Down]=next/prev headline TAB=cycle visibility [/] org-occur")
  3686. (defvar org-goto-start-pos) ; dynamically scoped parameter
  3687. (defun org-goto (&optional alternative-interface)
  3688. "Look up a different location in the current file, keeping current visibility.
  3689. When you want look-up or go to a different location in a document, the
  3690. fastest way is often to fold the entire buffer and then dive into the tree.
  3691. This method has the disadvantage, that the previous location will be folded,
  3692. which may not be what you want.
  3693. This command works around this by showing a copy of the current buffer
  3694. in an indirect buffer, in overview mode. You can dive into the tree in
  3695. that copy, use org-occur and incremental search to find a location.
  3696. When pressing RET or `Q', the command returns to the original buffer in
  3697. which the visibility is still unchanged. After RET is will also jump to
  3698. the location selected in the indirect buffer and expose the
  3699. the headline hierarchy above."
  3700. (interactive "P")
  3701. (let* ((org-refile-targets '((nil . (:maxlevel . 10))))
  3702. (org-refile-use-outline-path t)
  3703. (interface
  3704. (if (not alternative-interface)
  3705. org-goto-interface
  3706. (if (eq org-goto-interface 'outline)
  3707. 'outline-path-completion
  3708. 'outline)))
  3709. (org-goto-start-pos (point))
  3710. (selected-point
  3711. (if (eq interface 'outline)
  3712. (car (org-get-location (current-buffer) org-goto-help))
  3713. (nth 3 (org-refile-get-location "Goto: ")))))
  3714. (if selected-point
  3715. (progn
  3716. (org-mark-ring-push org-goto-start-pos)
  3717. (goto-char selected-point)
  3718. (if (or (org-invisible-p) (org-invisible-p2))
  3719. (org-show-context 'org-goto)))
  3720. (message "Quit"))))
  3721. (defvar org-goto-selected-point nil) ; dynamically scoped parameter
  3722. (defvar org-goto-exit-command nil) ; dynamically scoped parameter
  3723. (defvar org-goto-local-auto-isearch-map) ; defined below
  3724. (defun org-get-location (buf help)
  3725. "Let the user select a location in the Org-mode buffer BUF.
  3726. This function uses a recursive edit. It returns the selected position
  3727. or nil."
  3728. (let ((isearch-mode-map org-goto-local-auto-isearch-map)
  3729. (isearch-hide-immediately nil)
  3730. (isearch-search-fun-function
  3731. (lambda () 'org-goto-local-search-forward-headings))
  3732. (org-goto-selected-point org-goto-exit-command))
  3733. (save-excursion
  3734. (save-window-excursion
  3735. (delete-other-windows)
  3736. (and (get-buffer "*org-goto*") (kill-buffer "*org-goto*"))
  3737. (switch-to-buffer
  3738. (condition-case nil
  3739. (make-indirect-buffer (current-buffer) "*org-goto*")
  3740. (error (make-indirect-buffer (current-buffer) "*org-goto*"))))
  3741. (with-output-to-temp-buffer "*Help*"
  3742. (princ help))
  3743. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3744. (setq buffer-read-only nil)
  3745. (let ((org-startup-truncated t)
  3746. (org-startup-folded nil)
  3747. (org-startup-align-all-tables nil))
  3748. (org-mode)
  3749. (org-overview))
  3750. (setq buffer-read-only t)
  3751. (if (and (boundp 'org-goto-start-pos)
  3752. (integer-or-marker-p org-goto-start-pos))
  3753. (let ((org-show-hierarchy-above t)
  3754. (org-show-siblings t)
  3755. (org-show-following-heading t))
  3756. (goto-char org-goto-start-pos)
  3757. (and (org-invisible-p) (org-show-context)))
  3758. (goto-char (point-min)))
  3759. (org-beginning-of-line)
  3760. (message "Select location and press RET")
  3761. (use-local-map org-goto-map)
  3762. (recursive-edit)
  3763. ))
  3764. (kill-buffer "*org-goto*")
  3765. (cons org-goto-selected-point org-goto-exit-command)))
  3766. (defvar org-goto-local-auto-isearch-map (make-sparse-keymap))
  3767. (set-keymap-parent org-goto-local-auto-isearch-map isearch-mode-map)
  3768. (define-key org-goto-local-auto-isearch-map "\C-i" 'isearch-other-control-char)
  3769. (define-key org-goto-local-auto-isearch-map "\C-m" 'isearch-other-control-char)
  3770. (defun org-goto-local-search-forward-headings (string bound noerror)
  3771. "Search and make sure that anu matches are in headlines."
  3772. (catch 'return
  3773. (while (search-forward string bound noerror)
  3774. (when (let ((context (mapcar 'car (save-match-data (org-context)))))
  3775. (and (member :headline context)
  3776. (not (member :tags context))))
  3777. (throw 'return (point))))))
  3778. (defun org-goto-local-auto-isearch ()
  3779. "Start isearch."
  3780. (interactive)
  3781. (goto-char (point-min))
  3782. (let ((keys (this-command-keys)))
  3783. (when (eq (lookup-key isearch-mode-map keys) 'isearch-printing-char)
  3784. (isearch-mode t)
  3785. (isearch-process-search-char (string-to-char keys)))))
  3786. (defun org-goto-ret (&optional arg)
  3787. "Finish `org-goto' by going to the new location."
  3788. (interactive "P")
  3789. (setq org-goto-selected-point (point)
  3790. org-goto-exit-command 'return)
  3791. (throw 'exit nil))
  3792. (defun org-goto-left ()
  3793. "Finish `org-goto' by going to the new location."
  3794. (interactive)
  3795. (if (org-on-heading-p)
  3796. (progn
  3797. (beginning-of-line 1)
  3798. (setq org-goto-selected-point (point)
  3799. org-goto-exit-command 'left)
  3800. (throw 'exit nil))
  3801. (error "Not on a heading")))
  3802. (defun org-goto-right ()
  3803. "Finish `org-goto' by going to the new location."
  3804. (interactive)
  3805. (if (org-on-heading-p)
  3806. (progn
  3807. (setq org-goto-selected-point (point)
  3808. org-goto-exit-command 'right)
  3809. (throw 'exit nil))
  3810. (error "Not on a heading")))
  3811. (defun org-goto-quit ()
  3812. "Finish `org-goto' without cursor motion."
  3813. (interactive)
  3814. (setq org-goto-selected-point nil)
  3815. (setq org-goto-exit-command 'quit)
  3816. (throw 'exit nil))
  3817. ;;; Indirect buffer display of subtrees
  3818. (defvar org-indirect-dedicated-frame nil
  3819. "This is the frame being used for indirect tree display.")
  3820. (defvar org-last-indirect-buffer nil)
  3821. (defun org-tree-to-indirect-buffer (&optional arg)
  3822. "Create indirect buffer and narrow it to current subtree.
  3823. With numerical prefix ARG, go up to this level and then take that tree.
  3824. If ARG is negative, go up that many levels.
  3825. If `org-indirect-buffer-display' is not `new-frame', the command removes the
  3826. indirect buffer previously made with this command, to avoid proliferation of
  3827. indirect buffers. However, when you call the command with a `C-u' prefix, or
  3828. when `org-indirect-buffer-display' is `new-frame', the last buffer
  3829. is kept so that you can work with several indirect buffers at the same time.
  3830. If `org-indirect-buffer-display' is `dedicated-frame', the C-u prefix also
  3831. requests that a new frame be made for the new buffer, so that the dedicated
  3832. frame is not changed."
  3833. (interactive "P")
  3834. (let ((cbuf (current-buffer))
  3835. (cwin (selected-window))
  3836. (pos (point))
  3837. beg end level heading ibuf)
  3838. (save-excursion
  3839. (org-back-to-heading t)
  3840. (when (numberp arg)
  3841. (setq level (org-outline-level))
  3842. (if (< arg 0) (setq arg (+ level arg)))
  3843. (while (> (setq level (org-outline-level)) arg)
  3844. (outline-up-heading 1 t)))
  3845. (setq beg (point)
  3846. heading (org-get-heading))
  3847. (org-end-of-subtree t) (setq end (point)))
  3848. (if (and (buffer-live-p org-last-indirect-buffer)
  3849. (not (eq org-indirect-buffer-display 'new-frame))
  3850. (not arg))
  3851. (kill-buffer org-last-indirect-buffer))
  3852. (setq ibuf (org-get-indirect-buffer cbuf)
  3853. org-last-indirect-buffer ibuf)
  3854. (cond
  3855. ((or (eq org-indirect-buffer-display 'new-frame)
  3856. (and arg (eq org-indirect-buffer-display 'dedicated-frame)))
  3857. (select-frame (make-frame))
  3858. (delete-other-windows)
  3859. (switch-to-buffer ibuf)
  3860. (org-set-frame-title heading))
  3861. ((eq org-indirect-buffer-display 'dedicated-frame)
  3862. (raise-frame
  3863. (select-frame (or (and org-indirect-dedicated-frame
  3864. (frame-live-p org-indirect-dedicated-frame)
  3865. org-indirect-dedicated-frame)
  3866. (setq org-indirect-dedicated-frame (make-frame)))))
  3867. (delete-other-windows)
  3868. (switch-to-buffer ibuf)
  3869. (org-set-frame-title (concat "Indirect: " heading)))
  3870. ((eq org-indirect-buffer-display 'current-window)
  3871. (switch-to-buffer ibuf))
  3872. ((eq org-indirect-buffer-display 'other-window)
  3873. (pop-to-buffer ibuf))
  3874. (t (error "Invalid value.")))
  3875. (if (featurep 'xemacs)
  3876. (save-excursion (org-mode) (turn-on-font-lock)))
  3877. (narrow-to-region beg end)
  3878. (show-all)
  3879. (goto-char pos)
  3880. (and (window-live-p cwin) (select-window cwin))))
  3881. (defun org-get-indirect-buffer (&optional buffer)
  3882. (setq buffer (or buffer (current-buffer)))
  3883. (let ((n 1) (base (buffer-name buffer)) bname)
  3884. (while (buffer-live-p
  3885. (get-buffer (setq bname (concat base "-" (number-to-string n)))))
  3886. (setq n (1+ n)))
  3887. (condition-case nil
  3888. (make-indirect-buffer buffer bname 'clone)
  3889. (error (make-indirect-buffer buffer bname)))))
  3890. (defun org-set-frame-title (title)
  3891. "Set the title of the current frame to the string TITLE."
  3892. ;; FIXME: how to name a single frame in XEmacs???
  3893. (unless (featurep 'xemacs)
  3894. (modify-frame-parameters (selected-frame) (list (cons 'name title)))))
  3895. ;;;; Structure editing
  3896. ;;; Inserting headlines
  3897. (defun org-insert-heading (&optional force-heading)
  3898. "Insert a new heading or item with same depth at point.
  3899. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3900. If point is at the beginning of a headline, insert a sibling before the
  3901. current headline. If point is not at the beginning, do not split the line,
  3902. but create the new hedline after the current line."
  3903. (interactive "P")
  3904. (if (= (buffer-size) 0)
  3905. (insert "\n* ")
  3906. (when (or force-heading (not (org-insert-item)))
  3907. (let* ((head (save-excursion
  3908. (condition-case nil
  3909. (progn
  3910. (org-back-to-heading)
  3911. (match-string 0))
  3912. (error "*"))))
  3913. (blank (cdr (assq 'heading org-blank-before-new-entry)))
  3914. pos)
  3915. (cond
  3916. ((and (org-on-heading-p) (bolp)
  3917. (or (bobp)
  3918. (save-excursion (backward-char 1) (not (org-invisible-p)))))
  3919. ;; insert before the current line
  3920. (open-line (if blank 2 1)))
  3921. ((and (bolp)
  3922. (or (bobp)
  3923. (save-excursion
  3924. (backward-char 1) (not (org-invisible-p)))))
  3925. ;; insert right here
  3926. nil)
  3927. (t
  3928. ;; in the middle of the line
  3929. (org-show-entry)
  3930. (let ((split
  3931. (org-get-alist-option org-M-RET-may-split-line 'headline))
  3932. tags pos)
  3933. (if (org-on-heading-p)
  3934. (progn
  3935. (looking-at ".*?\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)?[ \t]*$")
  3936. (setq tags (and (match-end 2) (match-string 2)))
  3937. (and (match-end 1)
  3938. (delete-region (match-beginning 1) (match-end 1)))
  3939. (setq pos (point-at-bol))
  3940. (or split (end-of-line 1))
  3941. (delete-horizontal-space)
  3942. (newline (if blank 2 1))
  3943. (when tags
  3944. (save-excursion
  3945. (goto-char pos)
  3946. (end-of-line 1)
  3947. (insert " " tags)
  3948. (org-set-tags nil 'align))))
  3949. (or split (end-of-line 1))
  3950. (newline (if blank 2 1))))))
  3951. (insert head) (just-one-space)
  3952. (setq pos (point))
  3953. (end-of-line 1)
  3954. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3955. (run-hooks 'org-insert-heading-hook)))))
  3956. (defun org-get-heading (&optional no-tags)
  3957. "Return the heading of the current entry, without the stars."
  3958. (save-excursion
  3959. (org-back-to-heading t)
  3960. (if (looking-at
  3961. (if no-tags
  3962. (org-re "\\*+[ \t]+\\([^\n\r]*?\\)\\([ \t]+:[[:alnum:]:_@]+:[ \t]*\\)?$")
  3963. "\\*+[ \t]+\\([^\r\n]*\\)"))
  3964. (match-string 1) "")))
  3965. (defun org-insert-heading-after-current ()
  3966. "Insert a new heading with same level as current, after current subtree."
  3967. (interactive)
  3968. (org-back-to-heading)
  3969. (org-insert-heading)
  3970. (org-move-subtree-down)
  3971. (end-of-line 1))
  3972. (defun org-insert-todo-heading (arg)
  3973. "Insert a new heading with the same level and TODO state as current heading.
  3974. If the heading has no TODO state, or if the state is DONE, use the first
  3975. state (TODO by default). Also with prefix arg, force first state."
  3976. (interactive "P")
  3977. (when (not (org-insert-item 'checkbox))
  3978. (org-insert-heading)
  3979. (save-excursion
  3980. (org-back-to-heading)
  3981. (outline-previous-heading)
  3982. (looking-at org-todo-line-regexp))
  3983. (if (or arg
  3984. (not (match-beginning 2))
  3985. (member (match-string 2) org-done-keywords))
  3986. (insert (car org-todo-keywords-1) " ")
  3987. (insert (match-string 2) " "))))
  3988. (defun org-insert-subheading (arg)
  3989. "Insert a new subheading and demote it.
  3990. Works for outline headings and for plain lists alike."
  3991. (interactive "P")
  3992. (org-insert-heading arg)
  3993. (cond
  3994. ((org-on-heading-p) (org-do-demote))
  3995. ((org-at-item-p) (org-indent-item 1))))
  3996. (defun org-insert-todo-subheading (arg)
  3997. "Insert a new subheading with TODO keyword or checkbox and demote it.
  3998. Works for outline headings and for plain lists alike."
  3999. (interactive "P")
  4000. (org-insert-todo-heading arg)
  4001. (cond
  4002. ((org-on-heading-p) (org-do-demote))
  4003. ((org-at-item-p) (org-indent-item 1))))
  4004. ;;; Promotion and Demotion
  4005. (defun org-promote-subtree ()
  4006. "Promote the entire subtree.
  4007. See also `org-promote'."
  4008. (interactive)
  4009. (save-excursion
  4010. (org-map-tree 'org-promote))
  4011. (org-fix-position-after-promote))
  4012. (defun org-demote-subtree ()
  4013. "Demote the entire subtree. See `org-demote'.
  4014. See also `org-promote'."
  4015. (interactive)
  4016. (save-excursion
  4017. (org-map-tree 'org-demote))
  4018. (org-fix-position-after-promote))
  4019. (defun org-do-promote ()
  4020. "Promote the current heading higher up the tree.
  4021. If the region is active in `transient-mark-mode', promote all headings
  4022. in the region."
  4023. (interactive)
  4024. (save-excursion
  4025. (if (org-region-active-p)
  4026. (org-map-region 'org-promote (region-beginning) (region-end))
  4027. (org-promote)))
  4028. (org-fix-position-after-promote))
  4029. (defun org-do-demote ()
  4030. "Demote the current heading lower down the tree.
  4031. If the region is active in `transient-mark-mode', demote all headings
  4032. in the region."
  4033. (interactive)
  4034. (save-excursion
  4035. (if (org-region-active-p)
  4036. (org-map-region 'org-demote (region-beginning) (region-end))
  4037. (org-demote)))
  4038. (org-fix-position-after-promote))
  4039. (defun org-fix-position-after-promote ()
  4040. "Make sure that after pro/demotion cursor position is right."
  4041. (let ((pos (point)))
  4042. (when (save-excursion
  4043. (beginning-of-line 1)
  4044. (looking-at org-todo-line-regexp)
  4045. (or (equal pos (match-end 1)) (equal pos (match-end 2))))
  4046. (cond ((eobp) (insert " "))
  4047. ((eolp) (insert " "))
  4048. ((equal (char-after) ?\ ) (forward-char 1))))))
  4049. (defun org-reduced-level (l)
  4050. (if org-odd-levels-only (1+ (floor (/ l 2))) l))
  4051. (defun org-get-valid-level (level &optional change)
  4052. "Rectify a level change under the influence of `org-odd-levels-only'
  4053. LEVEL is a current level, CHANGE is by how much the level should be
  4054. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  4055. even level numbers will become the next higher odd number."
  4056. (if org-odd-levels-only
  4057. (cond ((or (not change) (= 0 change)) (1+ (* 2 (/ level 2))))
  4058. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  4059. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  4060. (max 1 (+ level change))))
  4061. (if (boundp 'define-obsolete-function-alias)
  4062. (if (or (featurep 'xemacs) (< emacs-major-version 23))
  4063. (define-obsolete-function-alias 'org-get-legal-level
  4064. 'org-get-valid-level)
  4065. (define-obsolete-function-alias 'org-get-legal-level
  4066. 'org-get-valid-level "23.1")))
  4067. (defun org-promote ()
  4068. "Promote the current heading higher up the tree.
  4069. If the region is active in `transient-mark-mode', promote all headings
  4070. in the region."
  4071. (org-back-to-heading t)
  4072. (let* ((level (save-match-data (funcall outline-level)))
  4073. (up-head (concat (make-string (org-get-valid-level level -1) ?*) " "))
  4074. (diff (abs (- level (length up-head) -1))))
  4075. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover if necessary"))
  4076. (replace-match up-head nil t)
  4077. ;; Fixup tag positioning
  4078. (and org-auto-align-tags (org-set-tags nil t))
  4079. (if org-adapt-indentation (org-fixup-indentation (- diff)))))
  4080. (defun org-demote ()
  4081. "Demote the current heading lower down the tree.
  4082. If the region is active in `transient-mark-mode', demote all headings
  4083. in the region."
  4084. (org-back-to-heading t)
  4085. (let* ((level (save-match-data (funcall outline-level)))
  4086. (down-head (concat (make-string (org-get-valid-level level 1) ?*) " "))
  4087. (diff (abs (- level (length down-head) -1))))
  4088. (replace-match down-head nil t)
  4089. ;; Fixup tag positioning
  4090. (and org-auto-align-tags (org-set-tags nil t))
  4091. (if org-adapt-indentation (org-fixup-indentation diff))))
  4092. (defun org-map-tree (fun)
  4093. "Call FUN for every heading underneath the current one."
  4094. (org-back-to-heading)
  4095. (let ((level (funcall outline-level)))
  4096. (save-excursion
  4097. (funcall fun)
  4098. (while (and (progn
  4099. (outline-next-heading)
  4100. (> (funcall outline-level) level))
  4101. (not (eobp)))
  4102. (funcall fun)))))
  4103. (defun org-map-region (fun beg end)
  4104. "Call FUN for every heading between BEG and END."
  4105. (let ((org-ignore-region t))
  4106. (save-excursion
  4107. (setq end (copy-marker end))
  4108. (goto-char beg)
  4109. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  4110. (< (point) end))
  4111. (funcall fun))
  4112. (while (and (progn
  4113. (outline-next-heading)
  4114. (< (point) end))
  4115. (not (eobp)))
  4116. (funcall fun)))))
  4117. (defun org-fixup-indentation (diff)
  4118. "Change the indentation in the current entry by DIFF
  4119. However, if any line in the current entry has no indentation, or if it
  4120. would end up with no indentation after the change, nothing at all is done."
  4121. (save-excursion
  4122. (let ((end (save-excursion (outline-next-heading)
  4123. (point-marker)))
  4124. (prohibit (if (> diff 0)
  4125. "^\\S-"
  4126. (concat "^ \\{0," (int-to-string (- diff)) "\\}\\S-")))
  4127. col)
  4128. (unless (save-excursion (end-of-line 1)
  4129. (re-search-forward prohibit end t))
  4130. (while (and (< (point) end)
  4131. (re-search-forward "^[ \t]+" end t))
  4132. (goto-char (match-end 0))
  4133. (setq col (current-column))
  4134. (if (< diff 0) (replace-match ""))
  4135. (indent-to (+ diff col))))
  4136. (move-marker end nil))))
  4137. (defun org-convert-to-odd-levels ()
  4138. "Convert an org-mode file with all levels allowed to one with odd levels.
  4139. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  4140. level 5 etc."
  4141. (interactive)
  4142. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  4143. (let ((org-odd-levels-only nil) n)
  4144. (save-excursion
  4145. (goto-char (point-min))
  4146. (while (re-search-forward "^\\*\\*+ " nil t)
  4147. (setq n (- (length (match-string 0)) 2))
  4148. (while (>= (setq n (1- n)) 0)
  4149. (org-demote))
  4150. (end-of-line 1))))))
  4151. (defun org-convert-to-oddeven-levels ()
  4152. "Convert an org-mode file with only odd levels to one with odd and even levels.
  4153. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  4154. section with an even level, conversion would destroy the structure of the file. An error
  4155. is signaled in this case."
  4156. (interactive)
  4157. (goto-char (point-min))
  4158. ;; First check if there are no even levels
  4159. (when (re-search-forward "^\\(\\*\\*\\)+ " nil t)
  4160. (org-show-context t)
  4161. (error "Not all levels are odd in this file. Conversion not possible."))
  4162. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  4163. (let ((org-odd-levels-only nil) n)
  4164. (save-excursion
  4165. (goto-char (point-min))
  4166. (while (re-search-forward "^\\*\\*+ " nil t)
  4167. (setq n (/ (1- (length (match-string 0))) 2))
  4168. (while (>= (setq n (1- n)) 0)
  4169. (org-promote))
  4170. (end-of-line 1))))))
  4171. (defun org-tr-level (n)
  4172. "Make N odd if required."
  4173. (if org-odd-levels-only (1+ (/ n 2)) n))
  4174. ;;; Vertical tree motion, cutting and pasting of subtrees
  4175. (defun org-move-subtree-up (&optional arg)
  4176. "Move the current subtree up past ARG headlines of the same level."
  4177. (interactive "p")
  4178. (org-move-subtree-down (- (prefix-numeric-value arg))))
  4179. (defun org-move-subtree-down (&optional arg)
  4180. "Move the current subtree down past ARG headlines of the same level."
  4181. (interactive "p")
  4182. (setq arg (prefix-numeric-value arg))
  4183. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  4184. 'outline-get-last-sibling))
  4185. (ins-point (make-marker))
  4186. (cnt (abs arg))
  4187. beg beg0 end txt folded ne-beg ne-end ne-ins ins-end)
  4188. ;; Select the tree
  4189. (org-back-to-heading)
  4190. (setq beg0 (point))
  4191. (save-excursion
  4192. (setq ne-beg (org-back-over-empty-lines))
  4193. (setq beg (point)))
  4194. (save-match-data
  4195. (save-excursion (outline-end-of-heading)
  4196. (setq folded (org-invisible-p)))
  4197. (outline-end-of-subtree))
  4198. (outline-next-heading)
  4199. (setq ne-end (org-back-over-empty-lines))
  4200. (setq end (point))
  4201. (goto-char beg0)
  4202. (when (and (> arg 0) (org-first-sibling-p) (< ne-end ne-beg))
  4203. ;; include less whitespace
  4204. (save-excursion
  4205. (goto-char beg)
  4206. (forward-line (- ne-beg ne-end))
  4207. (setq beg (point))))
  4208. ;; Find insertion point, with error handling
  4209. (while (> cnt 0)
  4210. (or (and (funcall movfunc) (looking-at outline-regexp))
  4211. (progn (goto-char beg0)
  4212. (error "Cannot move past superior level or buffer limit")))
  4213. (setq cnt (1- cnt)))
  4214. (if (> arg 0)
  4215. ;; Moving forward - still need to move over subtree
  4216. (progn (org-end-of-subtree t t)
  4217. (save-excursion
  4218. (org-back-over-empty-lines)
  4219. (or (bolp) (newline)))))
  4220. (setq ne-ins (org-back-over-empty-lines))
  4221. (move-marker ins-point (point))
  4222. (setq txt (buffer-substring beg end))
  4223. (org-save-markers-in-region beg end)
  4224. (delete-region beg end)
  4225. (outline-flag-region (1- beg) beg nil)
  4226. (outline-flag-region (1- (point)) (point) nil)
  4227. (let ((bbb (point)))
  4228. (insert-before-markers txt)
  4229. (org-reinstall-markers-in-region bbb)
  4230. (move-marker ins-point bbb))
  4231. (or (bolp) (insert "\n"))
  4232. (setq ins-end (point))
  4233. (goto-char ins-point)
  4234. (org-skip-whitespace)
  4235. (when (and (< arg 0)
  4236. (org-first-sibling-p)
  4237. (> ne-ins ne-beg))
  4238. ;; Move whitespace back to beginning
  4239. (save-excursion
  4240. (goto-char ins-end)
  4241. (let ((kill-whole-line t))
  4242. (kill-line (- ne-ins ne-beg)) (point)))
  4243. (insert (make-string (- ne-ins ne-beg) ?\n)))
  4244. (move-marker ins-point nil)
  4245. (org-compact-display-after-subtree-move)
  4246. (unless folded
  4247. (org-show-entry)
  4248. (show-children)
  4249. (org-cycle-hide-drawers 'children))))
  4250. (defvar org-subtree-clip ""
  4251. "Clipboard for cut and paste of subtrees.
  4252. This is actually only a copy of the kill, because we use the normal kill
  4253. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  4254. (defvar org-subtree-clip-folded nil
  4255. "Was the last copied subtree folded?
  4256. This is used to fold the tree back after pasting.")
  4257. (defun org-cut-subtree (&optional n)
  4258. "Cut the current subtree into the clipboard.
  4259. With prefix arg N, cut this many sequential subtrees.
  4260. This is a short-hand for marking the subtree and then cutting it."
  4261. (interactive "p")
  4262. (org-copy-subtree n 'cut))
  4263. (defun org-copy-subtree (&optional n cut force-store-markers)
  4264. "Cut the current subtree into the clipboard.
  4265. With prefix arg N, cut this many sequential subtrees.
  4266. This is a short-hand for marking the subtree and then copying it.
  4267. If CUT is non-nil, actually cut the subtree.
  4268. If FORCE-STORE-MARKERS is non-nil, store the relative locations
  4269. of some markers in the region, even if CUT is non-nil. This is
  4270. useful if the caller implements cut-and-paste as copy-then-paste-then-cut."
  4271. (interactive "p")
  4272. (let (beg end folded (beg0 (point)))
  4273. (if (interactive-p)
  4274. (org-back-to-heading nil) ; take what looks like a subtree
  4275. (org-back-to-heading t)) ; take what is really there
  4276. (org-back-over-empty-lines)
  4277. (setq beg (point))
  4278. (skip-chars-forward " \t\r\n")
  4279. (save-match-data
  4280. (save-excursion (outline-end-of-heading)
  4281. (setq folded (org-invisible-p)))
  4282. (condition-case nil
  4283. (outline-forward-same-level (1- n))
  4284. (error nil))
  4285. (org-end-of-subtree t t))
  4286. (org-back-over-empty-lines)
  4287. (setq end (point))
  4288. (goto-char beg0)
  4289. (when (> end beg)
  4290. (setq org-subtree-clip-folded folded)
  4291. (when (or cut force-store-markers)
  4292. (org-save-markers-in-region beg end))
  4293. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  4294. (setq org-subtree-clip (current-kill 0))
  4295. (message "%s: Subtree(s) with %d characters"
  4296. (if cut "Cut" "Copied")
  4297. (length org-subtree-clip)))))
  4298. (defun org-paste-subtree (&optional level tree)
  4299. "Paste the clipboard as a subtree, with modification of headline level.
  4300. The entire subtree is promoted or demoted in order to match a new headline
  4301. level. By default, the new level is derived from the visible headings
  4302. before and after the insertion point, and taken to be the inferior headline
  4303. level of the two. So if the previous visible heading is level 3 and the
  4304. next is level 4 (or vice versa), level 4 will be used for insertion.
  4305. This makes sure that the subtree remains an independent subtree and does
  4306. not swallow low level entries.
  4307. You can also force a different level, either by using a numeric prefix
  4308. argument, or by inserting the heading marker by hand. For example, if the
  4309. cursor is after \"*****\", then the tree will be shifted to level 5.
  4310. If you want to insert the tree as is, just use \\[yank].
  4311. If optional TREE is given, use this text instead of the kill ring."
  4312. (interactive "P")
  4313. (unless (org-kill-is-subtree-p tree)
  4314. (error "%s"
  4315. (substitute-command-keys
  4316. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  4317. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  4318. (^re (concat "^\\(" outline-regexp "\\)"))
  4319. (re (concat "\\(" outline-regexp "\\)"))
  4320. (^re_ (concat "\\(\\*+\\)[ \t]*"))
  4321. (old-level (if (string-match ^re txt)
  4322. (- (match-end 0) (match-beginning 0) 1)
  4323. -1))
  4324. (force-level (cond (level (prefix-numeric-value level))
  4325. ((string-match
  4326. ^re_ (buffer-substring (point-at-bol) (point)))
  4327. (- (match-end 1) (match-beginning 1)))
  4328. (t nil)))
  4329. (previous-level (save-excursion
  4330. (condition-case nil
  4331. (progn
  4332. (outline-previous-visible-heading 1)
  4333. (if (looking-at re)
  4334. (- (match-end 0) (match-beginning 0) 1)
  4335. 1))
  4336. (error 1))))
  4337. (next-level (save-excursion
  4338. (condition-case nil
  4339. (progn
  4340. (or (looking-at outline-regexp)
  4341. (outline-next-visible-heading 1))
  4342. (if (looking-at re)
  4343. (- (match-end 0) (match-beginning 0) 1)
  4344. 1))
  4345. (error 1))))
  4346. (new-level (or force-level (max previous-level next-level)))
  4347. (shift (if (or (= old-level -1)
  4348. (= new-level -1)
  4349. (= old-level new-level))
  4350. 0
  4351. (- new-level old-level)))
  4352. (delta (if (> shift 0) -1 1))
  4353. (func (if (> shift 0) 'org-demote 'org-promote))
  4354. (org-odd-levels-only nil)
  4355. beg end)
  4356. ;; Remove the forced level indicator
  4357. (if force-level
  4358. (delete-region (point-at-bol) (point)))
  4359. ;; Paste
  4360. (beginning-of-line 1)
  4361. (org-back-over-empty-lines)
  4362. (setq beg (point))
  4363. (insert-before-markers txt)
  4364. (unless (string-match "\n\\'" txt) (insert "\n"))
  4365. (org-reinstall-markers-in-region beg)
  4366. (setq end (point))
  4367. (goto-char beg)
  4368. (skip-chars-forward " \t\n\r")
  4369. (setq beg (point))
  4370. ;; Shift if necessary
  4371. (unless (= shift 0)
  4372. (save-restriction
  4373. (narrow-to-region beg end)
  4374. (while (not (= shift 0))
  4375. (org-map-region func (point-min) (point-max))
  4376. (setq shift (+ delta shift)))
  4377. (goto-char (point-min))))
  4378. (when (interactive-p)
  4379. (message "Clipboard pasted as level %d subtree" new-level))
  4380. (if (and kill-ring
  4381. (eq org-subtree-clip (current-kill 0))
  4382. org-subtree-clip-folded)
  4383. ;; The tree was folded before it was killed/copied
  4384. (hide-subtree))))
  4385. (defun org-kill-is-subtree-p (&optional txt)
  4386. "Check if the current kill is an outline subtree, or a set of trees.
  4387. Returns nil if kill does not start with a headline, or if the first
  4388. headline level is not the largest headline level in the tree.
  4389. So this will actually accept several entries of equal levels as well,
  4390. which is OK for `org-paste-subtree'.
  4391. If optional TXT is given, check this string instead of the current kill."
  4392. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  4393. (start-level (and kill
  4394. (string-match (concat "\\`\\([ \t\n\r]*?\n\\)?\\("
  4395. org-outline-regexp "\\)")
  4396. kill)
  4397. (- (match-end 2) (match-beginning 2) 1)))
  4398. (re (concat "^" org-outline-regexp))
  4399. (start (1+ (match-beginning 2))))
  4400. (if (not start-level)
  4401. (progn
  4402. nil) ;; does not even start with a heading
  4403. (catch 'exit
  4404. (while (setq start (string-match re kill (1+ start)))
  4405. (when (< (- (match-end 0) (match-beginning 0) 1) start-level)
  4406. (throw 'exit nil)))
  4407. t))))
  4408. (defvar org-markers-to-move nil
  4409. "Markers that should be moved with a cut-and-paste operation.
  4410. Those markers are stored together with their positions relative to
  4411. the start of the region.")
  4412. (defun org-save-markers-in-region (beg end)
  4413. "Check markers in region.
  4414. If these markers are between BEG and END, record their position relative
  4415. to BEG, so that after moving the block of text, we can put the markers back
  4416. into place.
  4417. This function gets called just before an entry or tree gets cut from the
  4418. buffer. After re-insertion, `org-reinstall-markers-in-region' must be
  4419. called immediately, to move the markers with the entries."
  4420. (setq org-markers-to-move nil)
  4421. (when (featurep 'org-clock)
  4422. (org-clock-save-markers-for-cut-and-paste beg end))
  4423. (when (featurep 'org-agenda)
  4424. (org-agenda-save-markers-for-cut-and-paste beg end)))
  4425. (defun org-check-and-save-marker (marker beg end)
  4426. "Check if MARKER is between BEG and END.
  4427. If yes, remember the marker and the distance to BEG."
  4428. (when (and (marker-buffer marker)
  4429. (equal (marker-buffer marker) (current-buffer)))
  4430. (if (and (>= marker beg) (< marker end))
  4431. (push (cons marker (- marker beg)) org-markers-to-move))))
  4432. (defun org-reinstall-markers-in-region (beg)
  4433. "Move all remembered markers to their position relative to BEG."
  4434. (mapc (lambda (x)
  4435. (move-marker (car x) (+ beg (cdr x))))
  4436. org-markers-to-move)
  4437. (setq org-markers-to-move nil))
  4438. (defun org-narrow-to-subtree ()
  4439. "Narrow buffer to the current subtree."
  4440. (interactive)
  4441. (save-excursion
  4442. (save-match-data
  4443. (narrow-to-region
  4444. (progn (org-back-to-heading) (point))
  4445. (progn (org-end-of-subtree t t) (point))))))
  4446. ;;; Outline Sorting
  4447. (defun org-sort (with-case)
  4448. "Call `org-sort-entries-or-items' or `org-table-sort-lines'.
  4449. Optional argument WITH-CASE means sort case-sensitively."
  4450. (interactive "P")
  4451. (if (org-at-table-p)
  4452. (org-call-with-arg 'org-table-sort-lines with-case)
  4453. (org-call-with-arg 'org-sort-entries-or-items with-case)))
  4454. (defun org-sort-remove-invisible (s)
  4455. (remove-text-properties 0 (length s) org-rm-props s)
  4456. (while (string-match org-bracket-link-regexp s)
  4457. (setq s (replace-match (if (match-end 2)
  4458. (match-string 3 s)
  4459. (match-string 1 s)) t t s)))
  4460. s)
  4461. (defvar org-priority-regexp) ; defined later in the file
  4462. (defun org-sort-entries-or-items (&optional with-case sorting-type getkey-func property)
  4463. "Sort entries on a certain level of an outline tree.
  4464. If there is an active region, the entries in the region are sorted.
  4465. Else, if the cursor is before the first entry, sort the top-level items.
  4466. Else, the children of the entry at point are sorted.
  4467. Sorting can be alphabetically, numerically, and by date/time as given by
  4468. the first time stamp in the entry. The command prompts for the sorting
  4469. type unless it has been given to the function through the SORTING-TYPE
  4470. argument, which needs to a character, any of (?n ?N ?a ?A ?t ?T ?p ?P ?f ?F).
  4471. If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies a function to be
  4472. called with point at the beginning of the record. It must return either
  4473. a string or a number that should serve as the sorting key for that record.
  4474. Comparing entries ignores case by default. However, with an optional argument
  4475. WITH-CASE, the sorting considers case as well."
  4476. (interactive "P")
  4477. (let ((case-func (if with-case 'identity 'downcase))
  4478. start beg end stars re re2
  4479. txt what tmp plain-list-p)
  4480. ;; Find beginning and end of region to sort
  4481. (cond
  4482. ((org-region-active-p)
  4483. ;; we will sort the region
  4484. (setq end (region-end)
  4485. what "region")
  4486. (goto-char (region-beginning))
  4487. (if (not (org-on-heading-p)) (outline-next-heading))
  4488. (setq start (point)))
  4489. ((org-at-item-p)
  4490. ;; we will sort this plain list
  4491. (org-beginning-of-item-list) (setq start (point))
  4492. (org-end-of-item-list) (setq end (point))
  4493. (goto-char start)
  4494. (setq plain-list-p t
  4495. what "plain list"))
  4496. ((or (org-on-heading-p)
  4497. (condition-case nil (progn (org-back-to-heading) t) (error nil)))
  4498. ;; we will sort the children of the current headline
  4499. (org-back-to-heading)
  4500. (setq start (point)
  4501. end (progn (org-end-of-subtree t t)
  4502. (org-back-over-empty-lines)
  4503. (point))
  4504. what "children")
  4505. (goto-char start)
  4506. (show-subtree)
  4507. (outline-next-heading))
  4508. (t
  4509. ;; we will sort the top-level entries in this file
  4510. (goto-char (point-min))
  4511. (or (org-on-heading-p) (outline-next-heading))
  4512. (setq start (point) end (point-max) what "top-level")
  4513. (goto-char start)
  4514. (show-all)))
  4515. (setq beg (point))
  4516. (if (>= beg end) (error "Nothing to sort"))
  4517. (unless plain-list-p
  4518. (looking-at "\\(\\*+\\)")
  4519. (setq stars (match-string 1)
  4520. re (concat "^" (regexp-quote stars) " +")
  4521. re2 (concat "^" (regexp-quote (substring stars 0 -1)) "[^*]")
  4522. txt (buffer-substring beg end))
  4523. (if (not (equal (substring txt -1) "\n")) (setq txt (concat txt "\n")))
  4524. (if (and (not (equal stars "*")) (string-match re2 txt))
  4525. (error "Region to sort contains a level above the first entry")))
  4526. (unless sorting-type
  4527. (message
  4528. (if plain-list-p
  4529. "Sort %s: [a]lpha [n]umeric [t]ime [f]unc A/N/T/F means reversed:"
  4530. "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:")
  4531. what)
  4532. (setq sorting-type (read-char-exclusive))
  4533. (and (= (downcase sorting-type) ?f)
  4534. (setq getkey-func
  4535. (completing-read "Sort using function: "
  4536. obarray 'fboundp t nil nil))
  4537. (setq getkey-func (intern getkey-func)))
  4538. (and (= (downcase sorting-type) ?r)
  4539. (setq property
  4540. (completing-read "Property: "
  4541. (mapcar 'list (org-buffer-property-keys t))
  4542. nil t))))
  4543. (message "Sorting entries...")
  4544. (save-restriction
  4545. (narrow-to-region start end)
  4546. (let ((dcst (downcase sorting-type))
  4547. (now (current-time)))
  4548. (sort-subr
  4549. (/= dcst sorting-type)
  4550. ;; This function moves to the beginning character of the "record" to
  4551. ;; be sorted.
  4552. (if plain-list-p
  4553. (lambda nil
  4554. (if (org-at-item-p) t (goto-char (point-max))))
  4555. (lambda nil
  4556. (if (re-search-forward re nil t)
  4557. (goto-char (match-beginning 0))
  4558. (goto-char (point-max)))))
  4559. ;; This function moves to the last character of the "record" being
  4560. ;; sorted.
  4561. (if plain-list-p
  4562. 'org-end-of-item
  4563. (lambda nil
  4564. (save-match-data
  4565. (condition-case nil
  4566. (outline-forward-same-level 1)
  4567. (error
  4568. (goto-char (point-max)))))))
  4569. ;; This function returns the value that gets sorted against.
  4570. (if plain-list-p
  4571. (lambda nil
  4572. (when (looking-at "[ \t]*[-+*0-9.)]+[ \t]+")
  4573. (cond
  4574. ((= dcst ?n)
  4575. (string-to-number (buffer-substring (match-end 0)
  4576. (point-at-eol))))
  4577. ((= dcst ?a)
  4578. (buffer-substring (match-end 0) (point-at-eol)))
  4579. ((= dcst ?t)
  4580. (if (re-search-forward org-ts-regexp
  4581. (point-at-eol) t)
  4582. (org-time-string-to-time (match-string 0))
  4583. now))
  4584. ((= dcst ?f)
  4585. (if getkey-func
  4586. (progn
  4587. (setq tmp (funcall getkey-func))
  4588. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4589. tmp)
  4590. (error "Invalid key function `%s'" getkey-func)))
  4591. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4592. (lambda nil
  4593. (cond
  4594. ((= dcst ?n)
  4595. (if (looking-at outline-regexp)
  4596. (string-to-number (buffer-substring (match-end 0)
  4597. (point-at-eol)))
  4598. nil))
  4599. ((= dcst ?a)
  4600. (funcall case-func (buffer-substring (point-at-bol)
  4601. (point-at-eol))))
  4602. ((= dcst ?t)
  4603. (if (re-search-forward org-ts-regexp
  4604. (save-excursion
  4605. (forward-line 2)
  4606. (point)) t)
  4607. (org-time-string-to-time (match-string 0))
  4608. now))
  4609. ((= dcst ?p)
  4610. (if (re-search-forward org-priority-regexp (point-at-eol) t)
  4611. (string-to-char (match-string 2))
  4612. org-default-priority))
  4613. ((= dcst ?r)
  4614. (or (org-entry-get nil property) ""))
  4615. ((= dcst ?o)
  4616. (if (looking-at org-complex-heading-regexp)
  4617. (- 9999 (length (member (match-string 2)
  4618. org-todo-keywords-1)))))
  4619. ((= dcst ?f)
  4620. (if getkey-func
  4621. (progn
  4622. (setq tmp (funcall getkey-func))
  4623. (if (stringp tmp) (setq tmp (funcall case-func tmp)))
  4624. tmp)
  4625. (error "Invalid key function `%s'" getkey-func)))
  4626. (t (error "Invalid sorting type `%c'" sorting-type)))))
  4627. nil
  4628. (cond
  4629. ((= dcst ?a) 'string<)
  4630. ((= dcst ?t) 'time-less-p)
  4631. (t nil)))))
  4632. (message "Sorting entries...done")))
  4633. (defun org-do-sort (table what &optional with-case sorting-type)
  4634. "Sort TABLE of WHAT according to SORTING-TYPE.
  4635. The user will be prompted for the SORTING-TYPE if the call to this
  4636. function does not specify it. WHAT is only for the prompt, to indicate
  4637. what is being sorted. The sorting key will be extracted from
  4638. the car of the elements of the table.
  4639. If WITH-CASE is non-nil, the sorting will be case-sensitive."
  4640. (unless sorting-type
  4641. (message
  4642. "Sort %s: [a]lphabetic. [n]umeric. [t]ime. A/N/T means reversed:"
  4643. what)
  4644. (setq sorting-type (read-char-exclusive)))
  4645. (let ((dcst (downcase sorting-type))
  4646. extractfun comparefun)
  4647. ;; Define the appropriate functions
  4648. (cond
  4649. ((= dcst ?n)
  4650. (setq extractfun 'string-to-number
  4651. comparefun (if (= dcst sorting-type) '< '>)))
  4652. ((= dcst ?a)
  4653. (setq extractfun (if with-case (lambda(x) (org-sort-remove-invisible x))
  4654. (lambda(x) (downcase (org-sort-remove-invisible x))))
  4655. comparefun (if (= dcst sorting-type)
  4656. 'string<
  4657. (lambda (a b) (and (not (string< a b))
  4658. (not (string= a b)))))))
  4659. ((= dcst ?t)
  4660. (setq extractfun
  4661. (lambda (x)
  4662. (if (string-match org-ts-regexp x)
  4663. (time-to-seconds
  4664. (org-time-string-to-time (match-string 0 x)))
  4665. 0))
  4666. comparefun (if (= dcst sorting-type) '< '>)))
  4667. (t (error "Invalid sorting type `%c'" sorting-type)))
  4668. (sort (mapcar (lambda (x) (cons (funcall extractfun (car x)) (cdr x)))
  4669. table)
  4670. (lambda (a b) (funcall comparefun (car a) (car b))))))
  4671. ;;;; Plain list items, including checkboxes
  4672. ;;; Plain list items
  4673. (defun org-at-item-p ()
  4674. "Is point in a line starting a hand-formatted item?"
  4675. (let ((llt org-plain-list-ordered-item-terminator))
  4676. (save-excursion
  4677. (goto-char (point-at-bol))
  4678. (looking-at
  4679. (cond
  4680. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4681. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4682. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+))\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  4683. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  4684. (defun org-in-item-p ()
  4685. "It the cursor inside a plain list item.
  4686. Does not have to be the first line."
  4687. (save-excursion
  4688. (condition-case nil
  4689. (progn
  4690. (org-beginning-of-item)
  4691. (org-at-item-p)
  4692. t)
  4693. (error nil))))
  4694. (defun org-insert-item (&optional checkbox)
  4695. "Insert a new item at the current level.
  4696. Return t when things worked, nil when we are not in an item."
  4697. (when (save-excursion
  4698. (condition-case nil
  4699. (progn
  4700. (org-beginning-of-item)
  4701. (org-at-item-p)
  4702. (if (org-invisible-p) (error "Invisible item"))
  4703. t)
  4704. (error nil)))
  4705. (let* ((bul (match-string 0))
  4706. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  4707. (match-end 0)))
  4708. (blank (cdr (assq 'plain-list-item org-blank-before-new-entry)))
  4709. pos)
  4710. (cond
  4711. ((and (org-at-item-p) (<= (point) eow))
  4712. ;; before the bullet
  4713. (beginning-of-line 1)
  4714. (open-line (if blank 2 1)))
  4715. ((<= (point) eow)
  4716. (beginning-of-line 1))
  4717. (t
  4718. (unless (org-get-alist-option org-M-RET-may-split-line 'item)
  4719. (end-of-line 1)
  4720. (delete-horizontal-space))
  4721. (newline (if blank 2 1))))
  4722. (insert bul (if checkbox "[ ]" ""))
  4723. (just-one-space)
  4724. (setq pos (point))
  4725. (end-of-line 1)
  4726. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  4727. (org-maybe-renumber-ordered-list)
  4728. (and checkbox (org-update-checkbox-count-maybe))
  4729. t))
  4730. ;;; Checkboxes
  4731. (defun org-at-item-checkbox-p ()
  4732. "Is point at a line starting a plain-list item with a checklet?"
  4733. (and (org-at-item-p)
  4734. (save-excursion
  4735. (goto-char (match-end 0))
  4736. (skip-chars-forward " \t")
  4737. (looking-at "\\[[- X]\\]"))))
  4738. (defun org-toggle-checkbox (&optional arg)
  4739. "Toggle the checkbox in the current line."
  4740. (interactive "P")
  4741. (catch 'exit
  4742. (let (beg end status (firstnew 'unknown))
  4743. (cond
  4744. ((org-region-active-p)
  4745. (setq beg (region-beginning) end (region-end)))
  4746. ((org-on-heading-p)
  4747. (setq beg (point) end (save-excursion (outline-next-heading) (point))))
  4748. ((org-at-item-checkbox-p)
  4749. (let ((pos (point)))
  4750. (replace-match
  4751. (cond (arg "[-]")
  4752. ((member (match-string 0) '("[ ]" "[-]")) "[X]")
  4753. (t "[ ]"))
  4754. t t)
  4755. (goto-char pos))
  4756. (throw 'exit t))
  4757. (t (error "Not at a checkbox or heading, and no active region")))
  4758. (save-excursion
  4759. (goto-char beg)
  4760. (while (< (point) end)
  4761. (when (org-at-item-checkbox-p)
  4762. (setq status (equal (match-string 0) "[X]"))
  4763. (when (eq firstnew 'unknown)
  4764. (setq firstnew (not status)))
  4765. (replace-match
  4766. (if (if arg (not status) firstnew) "[X]" "[ ]") t t))
  4767. (beginning-of-line 2)))))
  4768. (org-update-checkbox-count-maybe))
  4769. (defun org-update-checkbox-count-maybe ()
  4770. "Update checkbox statistics unless turned off by user."
  4771. (when org-provide-checkbox-statistics
  4772. (org-update-checkbox-count)))
  4773. (defun org-update-checkbox-count (&optional all)
  4774. "Update the checkbox statistics in the current section.
  4775. This will find all statistic cookies like [57%] and [6/12] and update them
  4776. with the current numbers. With optional prefix argument ALL, do this for
  4777. the whole buffer."
  4778. (interactive "P")
  4779. (save-excursion
  4780. (let* ((buffer-invisibility-spec (org-inhibit-invisibility)) ; Emacs 21
  4781. (beg (condition-case nil
  4782. (progn (outline-back-to-heading) (point))
  4783. (error (point-min))))
  4784. (end (move-marker (make-marker)
  4785. (progn (outline-next-heading) (point))))
  4786. (re "\\(\\(\\[[0-9]*%\\]\\)\\|\\(\\[[0-9]*/[0-9]*\\]\\)\\)")
  4787. (re-box "^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[- X]\\]\\)")
  4788. (re-find (concat re "\\|" re-box))
  4789. beg-cookie end-cookie is-percent c-on c-off lim
  4790. eline curr-ind next-ind continue-from startsearch
  4791. (cstat 0)
  4792. )
  4793. (when all
  4794. (goto-char (point-min))
  4795. (outline-next-heading)
  4796. (setq beg (point) end (point-max)))
  4797. (goto-char end)
  4798. ;; find each statistic cookie
  4799. (while (re-search-backward re-find beg t)
  4800. (setq beg-cookie (match-beginning 1)
  4801. end-cookie (match-end 1)
  4802. cstat (+ cstat (if end-cookie 1 0))
  4803. startsearch (point-at-eol)
  4804. continue-from (point-at-bol)
  4805. is-percent (match-beginning 2)
  4806. lim (cond
  4807. ((org-on-heading-p) (outline-next-heading) (point))
  4808. ((org-at-item-p) (org-end-of-item) (point))
  4809. (t nil))
  4810. c-on 0
  4811. c-off 0)
  4812. (when lim
  4813. ;; find first checkbox for this cookie and gather
  4814. ;; statistics from all that are at this indentation level
  4815. (goto-char startsearch)
  4816. (if (re-search-forward re-box lim t)
  4817. (progn
  4818. (org-beginning-of-item)
  4819. (setq curr-ind (org-get-indentation))
  4820. (setq next-ind curr-ind)
  4821. (while (and (bolp) (org-at-item-p) (= curr-ind next-ind))
  4822. (save-excursion (end-of-line) (setq eline (point)))
  4823. (if (re-search-forward re-box eline t)
  4824. (if (member (match-string 2) '("[ ]" "[-]"))
  4825. (setq c-off (1+ c-off))
  4826. (setq c-on (1+ c-on))
  4827. )
  4828. )
  4829. (org-end-of-item)
  4830. (setq next-ind (org-get-indentation))
  4831. )))
  4832. (goto-char continue-from)
  4833. ;; update cookie
  4834. (when end-cookie
  4835. (delete-region beg-cookie end-cookie)
  4836. (goto-char beg-cookie)
  4837. (insert
  4838. (if is-percent
  4839. (format "[%d%%]" (/ (* 100 c-on) (max 1 (+ c-on c-off))))
  4840. (format "[%d/%d]" c-on (+ c-on c-off)))))
  4841. ;; update items checkbox if it has one
  4842. (when (org-at-item-p)
  4843. (org-beginning-of-item)
  4844. (when (and (> (+ c-on c-off) 0)
  4845. (re-search-forward re-box (point-at-eol) t))
  4846. (setq beg-cookie (match-beginning 2)
  4847. end-cookie (match-end 2))
  4848. (delete-region beg-cookie end-cookie)
  4849. (goto-char beg-cookie)
  4850. (cond ((= c-off 0) (insert "[X]"))
  4851. ((= c-on 0) (insert "[ ]"))
  4852. (t (insert "[-]")))
  4853. )))
  4854. (goto-char continue-from))
  4855. (when (interactive-p)
  4856. (message "Checkbox satistics updated %s (%d places)"
  4857. (if all "in entire file" "in current outline entry") cstat)))))
  4858. (defun org-get-checkbox-statistics-face ()
  4859. "Select the face for checkbox statistics.
  4860. The face will be `org-done' when all relevant boxes are checked. Otherwise
  4861. it will be `org-todo'."
  4862. (if (match-end 1)
  4863. (if (equal (match-string 1) "100%") 'org-done 'org-todo)
  4864. (if (and (> (match-end 2) (match-beginning 2))
  4865. (equal (match-string 2) (match-string 3)))
  4866. 'org-done
  4867. 'org-todo)))
  4868. (defun org-get-indentation (&optional line)
  4869. "Get the indentation of the current line, interpreting tabs.
  4870. When LINE is given, assume it represents a line and compute its indentation."
  4871. (if line
  4872. (if (string-match "^ *" (org-remove-tabs line))
  4873. (match-end 0))
  4874. (save-excursion
  4875. (beginning-of-line 1)
  4876. (skip-chars-forward " \t")
  4877. (current-column))))
  4878. (defun org-remove-tabs (s &optional width)
  4879. "Replace tabulators in S with spaces.
  4880. Assumes that s is a single line, starting in column 0."
  4881. (setq width (or width tab-width))
  4882. (while (string-match "\t" s)
  4883. (setq s (replace-match
  4884. (make-string
  4885. (- (* width (/ (+ (match-beginning 0) width) width))
  4886. (match-beginning 0)) ?\ )
  4887. t t s)))
  4888. s)
  4889. (defun org-fix-indentation (line ind)
  4890. "Fix indentation in LINE.
  4891. IND is a cons cell with target and minimum indentation.
  4892. If the current indenation in LINE is smaller than the minimum,
  4893. leave it alone. If it is larger than ind, set it to the target."
  4894. (let* ((l (org-remove-tabs line))
  4895. (i (org-get-indentation l))
  4896. (i1 (car ind)) (i2 (cdr ind)))
  4897. (if (>= i i2) (setq l (substring line i2)))
  4898. (if (> i1 0)
  4899. (concat (make-string i1 ?\ ) l)
  4900. l)))
  4901. (defun org-beginning-of-item ()
  4902. "Go to the beginning of the current hand-formatted item.
  4903. If the cursor is not in an item, throw an error."
  4904. (interactive)
  4905. (let ((pos (point))
  4906. (limit (save-excursion
  4907. (condition-case nil
  4908. (progn
  4909. (org-back-to-heading)
  4910. (beginning-of-line 2) (point))
  4911. (error (point-min)))))
  4912. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4913. ind ind1)
  4914. (if (org-at-item-p)
  4915. (beginning-of-line 1)
  4916. (beginning-of-line 1)
  4917. (skip-chars-forward " \t")
  4918. (setq ind (current-column))
  4919. (if (catch 'exit
  4920. (while t
  4921. (beginning-of-line 0)
  4922. (if (or (bobp) (< (point) limit)) (throw 'exit nil))
  4923. (if (looking-at "[ \t]*$")
  4924. (setq ind1 ind-empty)
  4925. (skip-chars-forward " \t")
  4926. (setq ind1 (current-column)))
  4927. (if (< ind1 ind)
  4928. (progn (beginning-of-line 1) (throw 'exit (org-at-item-p))))))
  4929. nil
  4930. (goto-char pos)
  4931. (error "Not in an item")))))
  4932. (defun org-end-of-item ()
  4933. "Go to the end of the current hand-formatted item.
  4934. If the cursor is not in an item, throw an error."
  4935. (interactive)
  4936. (let* ((pos (point))
  4937. ind1
  4938. (ind-empty (if org-empty-line-terminates-plain-lists 0 10000))
  4939. (limit (save-excursion (outline-next-heading) (point)))
  4940. (ind (save-excursion
  4941. (org-beginning-of-item)
  4942. (skip-chars-forward " \t")
  4943. (current-column)))
  4944. (end (catch 'exit
  4945. (while t
  4946. (beginning-of-line 2)
  4947. (if (eobp) (throw 'exit (point)))
  4948. (if (>= (point) limit) (throw 'exit (point-at-bol)))
  4949. (if (looking-at "[ \t]*$")
  4950. (setq ind1 ind-empty)
  4951. (skip-chars-forward " \t")
  4952. (setq ind1 (current-column)))
  4953. (if (<= ind1 ind)
  4954. (throw 'exit (point-at-bol)))))))
  4955. (if end
  4956. (goto-char end)
  4957. (goto-char pos)
  4958. (error "Not in an item"))))
  4959. (defun org-next-item ()
  4960. "Move to the beginning of the next item in the current plain list.
  4961. Error if not at a plain list, or if this is the last item in the list."
  4962. (interactive)
  4963. (let (ind ind1 (pos (point)))
  4964. (org-beginning-of-item)
  4965. (setq ind (org-get-indentation))
  4966. (org-end-of-item)
  4967. (setq ind1 (org-get-indentation))
  4968. (unless (and (org-at-item-p) (= ind ind1))
  4969. (goto-char pos)
  4970. (error "On last item"))))
  4971. (defun org-previous-item ()
  4972. "Move to the beginning of the previous item in the current plain list.
  4973. Error if not at a plain list, or if this is the first item in the list."
  4974. (interactive)
  4975. (let (beg ind ind1 (pos (point)))
  4976. (org-beginning-of-item)
  4977. (setq beg (point))
  4978. (setq ind (org-get-indentation))
  4979. (goto-char beg)
  4980. (catch 'exit
  4981. (while t
  4982. (beginning-of-line 0)
  4983. (if (looking-at "[ \t]*$")
  4984. nil
  4985. (if (<= (setq ind1 (org-get-indentation)) ind)
  4986. (throw 'exit t)))))
  4987. (condition-case nil
  4988. (if (or (not (org-at-item-p))
  4989. (< ind1 (1- ind)))
  4990. (error "")
  4991. (org-beginning-of-item))
  4992. (error (goto-char pos)
  4993. (error "On first item")))))
  4994. (defun org-first-list-item-p ()
  4995. "Is this heading the item in a plain list?"
  4996. (unless (org-at-item-p)
  4997. (error "Not at a plain list item"))
  4998. (org-beginning-of-item)
  4999. (= (point) (save-excursion (org-beginning-of-item-list))))
  5000. (defun org-move-item-down ()
  5001. "Move the plain list item at point down, i.e. swap with following item.
  5002. Subitems (items with larger indentation) are considered part of the item,
  5003. so this really moves item trees."
  5004. (interactive)
  5005. (let (beg beg0 end end0 ind ind1 (pos (point)) txt ne-end ne-beg)
  5006. (org-beginning-of-item)
  5007. (setq beg0 (point))
  5008. (save-excursion
  5009. (setq ne-beg (org-back-over-empty-lines))
  5010. (setq beg (point)))
  5011. (goto-char beg0)
  5012. (setq ind (org-get-indentation))
  5013. (org-end-of-item)
  5014. (setq end0 (point))
  5015. (setq ind1 (org-get-indentation))
  5016. (setq ne-end (org-back-over-empty-lines))
  5017. (setq end (point))
  5018. (goto-char beg0)
  5019. (when (and (org-first-list-item-p) (< ne-end ne-beg))
  5020. ;; include less whitespace
  5021. (save-excursion
  5022. (goto-char beg)
  5023. (forward-line (- ne-beg ne-end))
  5024. (setq beg (point))))
  5025. (goto-char end0)
  5026. (if (and (org-at-item-p) (= ind ind1))
  5027. (progn
  5028. (org-end-of-item)
  5029. (org-back-over-empty-lines)
  5030. (setq txt (buffer-substring beg end))
  5031. (save-excursion
  5032. (delete-region beg end))
  5033. (setq pos (point))
  5034. (insert txt)
  5035. (goto-char pos) (org-skip-whitespace)
  5036. (org-maybe-renumber-ordered-list))
  5037. (goto-char pos)
  5038. (error "Cannot move this item further down"))))
  5039. (defun org-move-item-up (arg)
  5040. "Move the plain list item at point up, i.e. swap with previous item.
  5041. Subitems (items with larger indentation) are considered part of the item,
  5042. so this really moves item trees."
  5043. (interactive "p")
  5044. (let (beg beg0 end ind ind1 (pos (point)) txt
  5045. ne-beg ne-ins ins-end)
  5046. (org-beginning-of-item)
  5047. (setq beg0 (point))
  5048. (setq ind (org-get-indentation))
  5049. (save-excursion
  5050. (setq ne-beg (org-back-over-empty-lines))
  5051. (setq beg (point)))
  5052. (goto-char beg0)
  5053. (org-end-of-item)
  5054. (setq end (point))
  5055. (goto-char beg0)
  5056. (catch 'exit
  5057. (while t
  5058. (beginning-of-line 0)
  5059. (if (looking-at "[ \t]*$")
  5060. (if org-empty-line-terminates-plain-lists
  5061. (progn
  5062. (goto-char pos)
  5063. (error "Cannot move this item further up"))
  5064. nil)
  5065. (if (<= (setq ind1 (org-get-indentation)) ind)
  5066. (throw 'exit t)))))
  5067. (condition-case nil
  5068. (org-beginning-of-item)
  5069. (error (goto-char beg)
  5070. (error "Cannot move this item further up")))
  5071. (setq ind1 (org-get-indentation))
  5072. (if (and (org-at-item-p) (= ind ind1))
  5073. (progn
  5074. (setq ne-ins (org-back-over-empty-lines))
  5075. (setq txt (buffer-substring beg end))
  5076. (save-excursion
  5077. (delete-region beg end))
  5078. (setq pos (point))
  5079. (insert txt)
  5080. (setq ins-end (point))
  5081. (goto-char pos) (org-skip-whitespace)
  5082. (when (and (org-first-list-item-p) (> ne-ins ne-beg))
  5083. ;; Move whitespace back to beginning
  5084. (save-excursion
  5085. (goto-char ins-end)
  5086. (let ((kill-whole-line t))
  5087. (kill-line (- ne-ins ne-beg)) (point)))
  5088. (insert (make-string (- ne-ins ne-beg) ?\n)))
  5089. (org-maybe-renumber-ordered-list))
  5090. (goto-char pos)
  5091. (error "Cannot move this item further up"))))
  5092. (defun org-maybe-renumber-ordered-list ()
  5093. "Renumber the ordered list at point if setup allows it.
  5094. This tests the user option `org-auto-renumber-ordered-lists' before
  5095. doing the renumbering."
  5096. (interactive)
  5097. (when (and org-auto-renumber-ordered-lists
  5098. (org-at-item-p))
  5099. (if (match-beginning 3)
  5100. (org-renumber-ordered-list 1)
  5101. (org-fix-bullet-type))))
  5102. (defun org-maybe-renumber-ordered-list-safe ()
  5103. (condition-case nil
  5104. (save-excursion
  5105. (org-maybe-renumber-ordered-list))
  5106. (error nil)))
  5107. (defun org-cycle-list-bullet (&optional which)
  5108. "Cycle through the different itemize/enumerate bullets.
  5109. This cycle the entire list level through the sequence:
  5110. `-' -> `+' -> `*' -> `1.' -> `1)'
  5111. If WHICH is a string, use that as the new bullet. If WHICH is an integer,
  5112. 0 meand `-', 1 means `+' etc."
  5113. (interactive "P")
  5114. (org-preserve-lc
  5115. (org-beginning-of-item-list)
  5116. (org-at-item-p)
  5117. (beginning-of-line 1)
  5118. (let ((current (match-string 0))
  5119. (prevp (eq which 'previous))
  5120. new)
  5121. (setq new (cond
  5122. ((and (numberp which)
  5123. (nth (1- which) '("-" "+" "*" "1." "1)"))))
  5124. ((string-match "-" current) (if prevp "1)" "+"))
  5125. ((string-match "\\+" current)
  5126. (if prevp "-" (if (looking-at "\\S-") "1." "*")))
  5127. ((string-match "\\*" current) (if prevp "+" "1."))
  5128. ((string-match "\\." current) (if prevp "*" "1)"))
  5129. ((string-match ")" current) (if prevp "1." "-"))
  5130. (t (error "This should not happen"))))
  5131. (and (looking-at "\\([ \t]*\\)\\S-+") (replace-match (concat "\\1" new)))
  5132. (org-fix-bullet-type)
  5133. (org-maybe-renumber-ordered-list))))
  5134. (defun org-get-string-indentation (s)
  5135. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  5136. (let ((n -1) (i 0) (w tab-width) c)
  5137. (catch 'exit
  5138. (while (< (setq n (1+ n)) (length s))
  5139. (setq c (aref s n))
  5140. (cond ((= c ?\ ) (setq i (1+ i)))
  5141. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  5142. (t (throw 'exit t)))))
  5143. i))
  5144. (defun org-renumber-ordered-list (arg)
  5145. "Renumber an ordered plain list.
  5146. Cursor needs to be in the first line of an item, the line that starts
  5147. with something like \"1.\" or \"2)\"."
  5148. (interactive "p")
  5149. (unless (and (org-at-item-p)
  5150. (match-beginning 3))
  5151. (error "This is not an ordered list"))
  5152. (let ((line (org-current-line))
  5153. (col (current-column))
  5154. (ind (org-get-string-indentation
  5155. (buffer-substring (point-at-bol) (match-beginning 3))))
  5156. ;; (term (substring (match-string 3) -1))
  5157. ind1 (n (1- arg))
  5158. fmt)
  5159. ;; find where this list begins
  5160. (org-beginning-of-item-list)
  5161. (looking-at "[ \t]*[0-9]+\\([.)]\\)")
  5162. (setq fmt (concat "%d" (match-string 1)))
  5163. (beginning-of-line 0)
  5164. ;; walk forward and replace these numbers
  5165. (catch 'exit
  5166. (while t
  5167. (catch 'next
  5168. (beginning-of-line 2)
  5169. (if (eobp) (throw 'exit nil))
  5170. (if (looking-at "[ \t]*$") (throw 'next nil))
  5171. (skip-chars-forward " \t") (setq ind1 (current-column))
  5172. (if (> ind1 ind) (throw 'next t))
  5173. (if (< ind1 ind) (throw 'exit t))
  5174. (if (not (org-at-item-p)) (throw 'exit nil))
  5175. (delete-region (match-beginning 2) (match-end 2))
  5176. (goto-char (match-beginning 2))
  5177. (insert (format fmt (setq n (1+ n)))))))
  5178. (goto-line line)
  5179. (org-move-to-column col)))
  5180. (defun org-fix-bullet-type ()
  5181. "Make sure all items in this list have the same bullet as the firsst item."
  5182. (interactive)
  5183. (unless (org-at-item-p) (error "This is not a list"))
  5184. (let ((line (org-current-line))
  5185. (col (current-column))
  5186. (ind (current-indentation))
  5187. ind1 bullet)
  5188. ;; find where this list begins
  5189. (org-beginning-of-item-list)
  5190. (beginning-of-line 1)
  5191. ;; find out what the bullet type is
  5192. (looking-at "[ \t]*\\(\\S-+\\)")
  5193. (setq bullet (match-string 1))
  5194. ;; walk forward and replace these numbers
  5195. (beginning-of-line 0)
  5196. (catch 'exit
  5197. (while t
  5198. (catch 'next
  5199. (beginning-of-line 2)
  5200. (if (eobp) (throw 'exit nil))
  5201. (if (looking-at "[ \t]*$") (throw 'next nil))
  5202. (skip-chars-forward " \t") (setq ind1 (current-column))
  5203. (if (> ind1 ind) (throw 'next t))
  5204. (if (< ind1 ind) (throw 'exit t))
  5205. (if (not (org-at-item-p)) (throw 'exit nil))
  5206. (skip-chars-forward " \t")
  5207. (looking-at "\\S-+")
  5208. (replace-match bullet))))
  5209. (goto-line line)
  5210. (org-move-to-column col)
  5211. (if (string-match "[0-9]" bullet)
  5212. (org-renumber-ordered-list 1))))
  5213. (defun org-beginning-of-item-list ()
  5214. "Go to the beginning of the current item list.
  5215. I.e. to the first item in this list."
  5216. (interactive)
  5217. (org-beginning-of-item)
  5218. (let ((pos (point-at-bol))
  5219. (ind (org-get-indentation))
  5220. ind1)
  5221. ;; find where this list begins
  5222. (catch 'exit
  5223. (while t
  5224. (catch 'next
  5225. (beginning-of-line 0)
  5226. (if (looking-at "[ \t]*$")
  5227. (throw (if (bobp) 'exit 'next) t))
  5228. (skip-chars-forward " \t") (setq ind1 (current-column))
  5229. (if (or (< ind1 ind)
  5230. (and (= ind1 ind)
  5231. (not (org-at-item-p)))
  5232. (bobp))
  5233. (throw 'exit t)
  5234. (when (org-at-item-p) (setq pos (point-at-bol)))))))
  5235. (goto-char pos)))
  5236. (defun org-end-of-item-list ()
  5237. "Go to the end of the current item list.
  5238. I.e. to the text after the last item."
  5239. (interactive)
  5240. (org-beginning-of-item)
  5241. (let ((pos (point-at-bol))
  5242. (ind (org-get-indentation))
  5243. ind1)
  5244. ;; find where this list begins
  5245. (catch 'exit
  5246. (while t
  5247. (catch 'next
  5248. (beginning-of-line 2)
  5249. (if (looking-at "[ \t]*$")
  5250. (throw (if (eobp) 'exit 'next) t))
  5251. (skip-chars-forward " \t") (setq ind1 (current-column))
  5252. (if (or (< ind1 ind)
  5253. (and (= ind1 ind)
  5254. (not (org-at-item-p)))
  5255. (eobp))
  5256. (progn
  5257. (setq pos (point-at-bol))
  5258. (throw 'exit t))))))
  5259. (goto-char pos)))
  5260. (defvar org-last-indent-begin-marker (make-marker))
  5261. (defvar org-last-indent-end-marker (make-marker))
  5262. (defun org-outdent-item (arg)
  5263. "Outdent a local list item."
  5264. (interactive "p")
  5265. (org-indent-item (- arg)))
  5266. (defun org-indent-item (arg)
  5267. "Indent a local list item."
  5268. (interactive "p")
  5269. (unless (org-at-item-p)
  5270. (error "Not on an item"))
  5271. (save-excursion
  5272. (let (beg end ind ind1 tmp delta ind-down ind-up)
  5273. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  5274. (setq beg org-last-indent-begin-marker
  5275. end org-last-indent-end-marker)
  5276. (org-beginning-of-item)
  5277. (setq beg (move-marker org-last-indent-begin-marker (point)))
  5278. (org-end-of-item)
  5279. (setq end (move-marker org-last-indent-end-marker (point))))
  5280. (goto-char beg)
  5281. (setq tmp (org-item-indent-positions)
  5282. ind (car tmp)
  5283. ind-down (nth 2 tmp)
  5284. ind-up (nth 1 tmp)
  5285. delta (if (> arg 0)
  5286. (if ind-down (- ind-down ind) 2)
  5287. (if ind-up (- ind-up ind) -2)))
  5288. (if (< (+ delta ind) 0) (error "Cannot outdent beyond margin"))
  5289. (while (< (point) end)
  5290. (beginning-of-line 1)
  5291. (skip-chars-forward " \t") (setq ind1 (current-column))
  5292. (delete-region (point-at-bol) (point))
  5293. (or (eolp) (org-indent-to-column (+ ind1 delta)))
  5294. (beginning-of-line 2))))
  5295. (org-fix-bullet-type)
  5296. (org-maybe-renumber-ordered-list-safe)
  5297. (save-excursion
  5298. (beginning-of-line 0)
  5299. (condition-case nil (org-beginning-of-item) (error nil))
  5300. (org-maybe-renumber-ordered-list-safe)))
  5301. (defun org-item-indent-positions ()
  5302. "Return indentation for plain list items.
  5303. This returns a list with three values: The current indentation, the
  5304. parent indentation and the indentation a child should habe.
  5305. Assumes cursor in item line."
  5306. (let* ((bolpos (point-at-bol))
  5307. (ind (org-get-indentation))
  5308. ind-down ind-up pos)
  5309. (save-excursion
  5310. (org-beginning-of-item-list)
  5311. (skip-chars-backward "\n\r \t")
  5312. (when (org-in-item-p)
  5313. (org-beginning-of-item)
  5314. (setq ind-up (org-get-indentation))))
  5315. (setq pos (point))
  5316. (save-excursion
  5317. (cond
  5318. ((and (condition-case nil (progn (org-previous-item) t)
  5319. (error nil))
  5320. (or (forward-char 1) t)
  5321. (re-search-forward "^\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)" bolpos t))
  5322. (setq ind-down (org-get-indentation)))
  5323. ((and (goto-char pos)
  5324. (org-at-item-p))
  5325. (goto-char (match-end 0))
  5326. (skip-chars-forward " \t")
  5327. (setq ind-down (current-column)))))
  5328. (list ind ind-up ind-down)))
  5329. ;;; The orgstruct minor mode
  5330. ;; Define a minor mode which can be used in other modes in order to
  5331. ;; integrate the org-mode structure editing commands.
  5332. ;; This is really a hack, because the org-mode structure commands use
  5333. ;; keys which normally belong to the major mode. Here is how it
  5334. ;; works: The minor mode defines all the keys necessary to operate the
  5335. ;; structure commands, but wraps the commands into a function which
  5336. ;; tests if the cursor is currently at a headline or a plain list
  5337. ;; item. If that is the case, the structure command is used,
  5338. ;; temporarily setting many Org-mode variables like regular
  5339. ;; expressions for filling etc. However, when any of those keys is
  5340. ;; used at a different location, function uses `key-binding' to look
  5341. ;; up if the key has an associated command in another currently active
  5342. ;; keymap (minor modes, major mode, global), and executes that
  5343. ;; command. There might be problems if any of the keys is otherwise
  5344. ;; used as a prefix key.
  5345. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  5346. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  5347. ;; addresses this by checking explicitly for both bindings.
  5348. (defvar orgstruct-mode-map (make-sparse-keymap)
  5349. "Keymap for the minor `orgstruct-mode'.")
  5350. (defvar org-local-vars nil
  5351. "List of local variables, for use by `orgstruct-mode'")
  5352. ;;;###autoload
  5353. (define-minor-mode orgstruct-mode
  5354. "Toggle the minor more `orgstruct-mode'.
  5355. This mode is for using Org-mode structure commands in other modes.
  5356. The following key behave as if Org-mode was active, if the cursor
  5357. is on a headline, or on a plain list item (both in the definition
  5358. of Org-mode).
  5359. M-up Move entry/item up
  5360. M-down Move entry/item down
  5361. M-left Promote
  5362. M-right Demote
  5363. M-S-up Move entry/item up
  5364. M-S-down Move entry/item down
  5365. M-S-left Promote subtree
  5366. M-S-right Demote subtree
  5367. M-q Fill paragraph and items like in Org-mode
  5368. C-c ^ Sort entries
  5369. C-c - Cycle list bullet
  5370. TAB Cycle item visibility
  5371. M-RET Insert new heading/item
  5372. S-M-RET Insert new TODO heading / Chekbox item
  5373. C-c C-c Set tags / toggle checkbox"
  5374. nil " OrgStruct" nil
  5375. (org-load-modules-maybe)
  5376. (and (orgstruct-setup) (defun orgstruct-setup () nil)))
  5377. ;;;###autoload
  5378. (defun turn-on-orgstruct ()
  5379. "Unconditionally turn on `orgstruct-mode'."
  5380. (orgstruct-mode 1))
  5381. ;;;###autoload
  5382. (defun turn-on-orgstruct++ ()
  5383. "Unconditionally turn on `orgstruct-mode', and force org-mode indentations.
  5384. In addition to setting orgstruct-mode, this also exports all indentation and
  5385. autofilling variables from org-mode into the buffer. Note that turning
  5386. off orgstruct-mode will *not* remove these additional settings."
  5387. (orgstruct-mode 1)
  5388. (let (var val)
  5389. (mapc
  5390. (lambda (x)
  5391. (when (string-match
  5392. "^\\(paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5393. (symbol-name (car x)))
  5394. (setq var (car x) val (nth 1 x))
  5395. (org-set-local var (if (eq (car-safe val) 'quote) (nth 1 val) val))))
  5396. org-local-vars)))
  5397. (defun orgstruct-error ()
  5398. "Error when there is no default binding for a structure key."
  5399. (interactive)
  5400. (error "This key has no function outside structure elements"))
  5401. (defun orgstruct-setup ()
  5402. "Setup orgstruct keymaps."
  5403. (let ((nfunc 0)
  5404. (bindings
  5405. (list
  5406. '([(meta up)] org-metaup)
  5407. '([(meta down)] org-metadown)
  5408. '([(meta left)] org-metaleft)
  5409. '([(meta right)] org-metaright)
  5410. '([(meta shift up)] org-shiftmetaup)
  5411. '([(meta shift down)] org-shiftmetadown)
  5412. '([(meta shift left)] org-shiftmetaleft)
  5413. '([(meta shift right)] org-shiftmetaright)
  5414. '([(shift up)] org-shiftup)
  5415. '([(shift down)] org-shiftdown)
  5416. '("\C-c\C-c" org-ctrl-c-ctrl-c)
  5417. '("\M-q" fill-paragraph)
  5418. '("\C-c^" org-sort)
  5419. '("\C-c-" org-cycle-list-bullet)))
  5420. elt key fun cmd)
  5421. (while (setq elt (pop bindings))
  5422. (setq nfunc (1+ nfunc))
  5423. (setq key (org-key (car elt))
  5424. fun (nth 1 elt)
  5425. cmd (orgstruct-make-binding fun nfunc key))
  5426. (org-defkey orgstruct-mode-map key cmd))
  5427. ;; Special treatment needed for TAB and RET
  5428. (org-defkey orgstruct-mode-map [(tab)]
  5429. (orgstruct-make-binding 'org-cycle 102 [(tab)] "\C-i"))
  5430. (org-defkey orgstruct-mode-map "\C-i"
  5431. (orgstruct-make-binding 'org-cycle 103 "\C-i" [(tab)]))
  5432. (org-defkey orgstruct-mode-map "\M-\C-m"
  5433. (orgstruct-make-binding 'org-insert-heading 105
  5434. "\M-\C-m" [(meta return)]))
  5435. (org-defkey orgstruct-mode-map [(meta return)]
  5436. (orgstruct-make-binding 'org-insert-heading 106
  5437. [(meta return)] "\M-\C-m"))
  5438. (org-defkey orgstruct-mode-map [(shift meta return)]
  5439. (orgstruct-make-binding 'org-insert-todo-heading 107
  5440. [(meta return)] "\M-\C-m"))
  5441. (unless org-local-vars
  5442. (setq org-local-vars (org-get-local-variables)))
  5443. t))
  5444. (defun orgstruct-make-binding (fun n &rest keys)
  5445. "Create a function for binding in the structure minor mode.
  5446. FUN is the command to call inside a table. N is used to create a unique
  5447. command name. KEYS are keys that should be checked in for a command
  5448. to execute outside of tables."
  5449. (eval
  5450. (list 'defun
  5451. (intern (concat "orgstruct-hijacker-command-" (int-to-string n)))
  5452. '(arg)
  5453. (concat "In Structure, run `" (symbol-name fun) "'.\n"
  5454. "Outside of structure, run the binding of `"
  5455. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  5456. "'.")
  5457. '(interactive "p")
  5458. (list 'if
  5459. '(org-context-p 'headline 'item)
  5460. (list 'org-run-like-in-org-mode (list 'quote fun))
  5461. (list 'let '(orgstruct-mode)
  5462. (list 'call-interactively
  5463. (append '(or)
  5464. (mapcar (lambda (k)
  5465. (list 'key-binding k))
  5466. keys)
  5467. '('orgstruct-error))))))))
  5468. (defun org-context-p (&rest contexts)
  5469. "Check if local context is and of CONTEXTS.
  5470. Possible values in the list of contexts are `table', `headline', and `item'."
  5471. (let ((pos (point)))
  5472. (goto-char (point-at-bol))
  5473. (prog1 (or (and (memq 'table contexts)
  5474. (looking-at "[ \t]*|"))
  5475. (and (memq 'headline contexts)
  5476. (looking-at "\\*+"))
  5477. (and (memq 'item contexts)
  5478. (looking-at "[ \t]*\\([-+*] \\|[0-9]+[.)] \\)")))
  5479. (goto-char pos))))
  5480. (defun org-get-local-variables ()
  5481. "Return a list of all local variables in an org-mode buffer."
  5482. (let (varlist)
  5483. (with-current-buffer (get-buffer-create "*Org tmp*")
  5484. (erase-buffer)
  5485. (org-mode)
  5486. (setq varlist (buffer-local-variables)))
  5487. (kill-buffer "*Org tmp*")
  5488. (delq nil
  5489. (mapcar
  5490. (lambda (x)
  5491. (setq x
  5492. (if (symbolp x)
  5493. (list x)
  5494. (list (car x) (list 'quote (cdr x)))))
  5495. (if (string-match
  5496. "^\\(org-\\|orgtbl-\\|outline-\\|comment-\\|paragraph-\\|auto-fill\\|fill-paragraph\\|adaptive-fill\\|indent-\\)"
  5497. (symbol-name (car x)))
  5498. x nil))
  5499. varlist))))
  5500. ;;;###autoload
  5501. (defun org-run-like-in-org-mode (cmd)
  5502. (org-load-modules-maybe)
  5503. (unless org-local-vars
  5504. (setq org-local-vars (org-get-local-variables)))
  5505. (eval (list 'let org-local-vars
  5506. (list 'call-interactively (list 'quote cmd)))))
  5507. ;;;; Archiving
  5508. (defun org-get-category (&optional pos)
  5509. "Get the category applying to position POS."
  5510. (get-text-property (or pos (point)) 'org-category))
  5511. (defun org-refresh-category-properties ()
  5512. "Refresh category text properties in the buffer."
  5513. (let ((def-cat (cond
  5514. ((null org-category)
  5515. (if buffer-file-name
  5516. (file-name-sans-extension
  5517. (file-name-nondirectory buffer-file-name))
  5518. "???"))
  5519. ((symbolp org-category) (symbol-name org-category))
  5520. (t org-category)))
  5521. beg end cat pos optionp)
  5522. (org-unmodified
  5523. (save-excursion
  5524. (save-restriction
  5525. (widen)
  5526. (goto-char (point-min))
  5527. (put-text-property (point) (point-max) 'org-category def-cat)
  5528. (while (re-search-forward
  5529. "^\\(#\\+CATEGORY:\\|[ \t]*:CATEGORY:\\)\\(.*\\)" nil t)
  5530. (setq pos (match-end 0)
  5531. optionp (equal (char-after (match-beginning 0)) ?#)
  5532. cat (org-trim (match-string 2)))
  5533. (if optionp
  5534. (setq beg (point-at-bol) end (point-max))
  5535. (org-back-to-heading t)
  5536. (setq beg (point) end (org-end-of-subtree t t)))
  5537. (put-text-property beg end 'org-category cat)
  5538. (goto-char pos)))))))
  5539. ;;;; Link Stuff
  5540. ;;; Link abbreviations
  5541. (defun org-link-expand-abbrev (link)
  5542. "Apply replacements as defined in `org-link-abbrev-alist."
  5543. (if (string-match "^\\([a-zA-Z][-_a-zA-Z0-9]*\\)\\(::?\\(.*\\)\\)?$" link)
  5544. (let* ((key (match-string 1 link))
  5545. (as (or (assoc key org-link-abbrev-alist-local)
  5546. (assoc key org-link-abbrev-alist)))
  5547. (tag (and (match-end 2) (match-string 3 link)))
  5548. rpl)
  5549. (if (not as)
  5550. link
  5551. (setq rpl (cdr as))
  5552. (cond
  5553. ((symbolp rpl) (funcall rpl tag))
  5554. ((string-match "%s" rpl) (replace-match (or tag "") t t rpl))
  5555. (t (concat rpl tag)))))
  5556. link))
  5557. ;;; Storing and inserting links
  5558. (defvar org-insert-link-history nil
  5559. "Minibuffer history for links inserted with `org-insert-link'.")
  5560. (defvar org-stored-links nil
  5561. "Contains the links stored with `org-store-link'.")
  5562. (defvar org-store-link-plist nil
  5563. "Plist with info about the most recently link created with `org-store-link'.")
  5564. (defvar org-link-protocols nil
  5565. "Link protocols added to Org-mode using `org-add-link-type'.")
  5566. (defvar org-store-link-functions nil
  5567. "List of functions that are called to create and store a link.
  5568. Each function will be called in turn until one returns a non-nil
  5569. value. Each function should check if it is responsible for creating
  5570. this link (for example by looking at the major mode).
  5571. If not, it must exit and return nil.
  5572. If yes, it should return a non-nil value after a calling
  5573. `org-store-link-props' with a list of properties and values.
  5574. Special properties are:
  5575. :type The link prefix. like \"http\". This must be given.
  5576. :link The link, like \"http://www.astro.uva.nl/~dominik\".
  5577. This is obligatory as well.
  5578. :description Optional default description for the second pair
  5579. of brackets in an Org-mode link. The user can still change
  5580. this when inserting this link into an Org-mode buffer.
  5581. In addition to these, any additional properties can be specified
  5582. and then used in remember templates.")
  5583. (defun org-add-link-type (type &optional follow export)
  5584. "Add TYPE to the list of `org-link-types'.
  5585. Re-compute all regular expressions depending on `org-link-types'
  5586. FOLLOW and EXPORT are two functions.
  5587. FOLLOW should take the link path as the single argument and do whatever
  5588. is necessary to follow the link, for example find a file or display
  5589. a mail message.
  5590. EXPORT should format the link path for export to one of the export formats.
  5591. It should be a function accepting three arguments:
  5592. path the path of the link, the text after the prefix (like \"http:\")
  5593. desc the description of the link, if any, nil if there was no descripton
  5594. format the export format, a symbol like `html' or `latex'.
  5595. The function may use the FORMAT information to return different values
  5596. depending on the format. The return value will be put literally into
  5597. the exported file.
  5598. Org-mode has a built-in default for exporting links. If you are happy with
  5599. this default, there is no need to define an export function for the link
  5600. type. For a simple example of an export function, see `org-bbdb.el'."
  5601. (add-to-list 'org-link-types type t)
  5602. (org-make-link-regexps)
  5603. (if (assoc type org-link-protocols)
  5604. (setcdr (assoc type org-link-protocols) (list follow export))
  5605. (push (list type follow export) org-link-protocols)))
  5606. ;;;###autoload
  5607. (defun org-store-link (arg)
  5608. "\\<org-mode-map>Store an org-link to the current location.
  5609. This link is added to `org-stored-links' and can later be inserted
  5610. into an org-buffer with \\[org-insert-link].
  5611. For some link types, a prefix arg is interpreted:
  5612. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  5613. For file links, arg negates `org-context-in-file-links'."
  5614. (interactive "P")
  5615. (org-load-modules-maybe)
  5616. (setq org-store-link-plist nil) ; reset
  5617. (let (link cpltxt desc description search txt)
  5618. (cond
  5619. ((run-hook-with-args-until-success 'org-store-link-functions)
  5620. (setq link (plist-get org-store-link-plist :link)
  5621. desc (or (plist-get org-store-link-plist :description) link)))
  5622. ((eq major-mode 'calendar-mode)
  5623. (let ((cd (calendar-cursor-to-date)))
  5624. (setq link
  5625. (format-time-string
  5626. (car org-time-stamp-formats)
  5627. (apply 'encode-time
  5628. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  5629. nil nil nil))))
  5630. (org-store-link-props :type "calendar" :date cd)))
  5631. ((eq major-mode 'w3-mode)
  5632. (setq cpltxt (url-view-url t)
  5633. link (org-make-link cpltxt))
  5634. (org-store-link-props :type "w3" :url (url-view-url t)))
  5635. ((eq major-mode 'w3m-mode)
  5636. (setq cpltxt (or w3m-current-title w3m-current-url)
  5637. link (org-make-link w3m-current-url))
  5638. (org-store-link-props :type "w3m" :url (url-view-url t)))
  5639. ((setq search (run-hook-with-args-until-success
  5640. 'org-create-file-search-functions))
  5641. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  5642. "::" search))
  5643. (setq cpltxt (or description link)))
  5644. ((eq major-mode 'image-mode)
  5645. (setq cpltxt (concat "file:"
  5646. (abbreviate-file-name buffer-file-name))
  5647. link (org-make-link cpltxt))
  5648. (org-store-link-props :type "image" :file buffer-file-name))
  5649. ((eq major-mode 'dired-mode)
  5650. ;; link to the file in the current line
  5651. (setq cpltxt (concat "file:"
  5652. (abbreviate-file-name
  5653. (expand-file-name
  5654. (dired-get-filename nil t))))
  5655. link (org-make-link cpltxt)))
  5656. ((and buffer-file-name (org-mode-p))
  5657. ;; Just link to current headline
  5658. (setq cpltxt (concat "file:"
  5659. (abbreviate-file-name buffer-file-name)))
  5660. ;; Add a context search string
  5661. (when (org-xor org-context-in-file-links arg)
  5662. ;; Check if we are on a target
  5663. (if (org-in-regexp "<<\\(.*?\\)>>")
  5664. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  5665. (setq txt (cond
  5666. ((org-on-heading-p) nil)
  5667. ((org-region-active-p)
  5668. (buffer-substring (region-beginning) (region-end)))
  5669. (t nil)))
  5670. (when (or (null txt) (string-match "\\S-" txt))
  5671. (setq cpltxt
  5672. (concat cpltxt "::"
  5673. (condition-case nil
  5674. (org-make-org-heading-search-string txt)
  5675. (error "")))
  5676. desc "NONE"))))
  5677. (if (string-match "::\\'" cpltxt)
  5678. (setq cpltxt (substring cpltxt 0 -2)))
  5679. (setq link (org-make-link cpltxt)))
  5680. ((buffer-file-name (buffer-base-buffer))
  5681. ;; Just link to this file here.
  5682. (setq cpltxt (concat "file:"
  5683. (abbreviate-file-name
  5684. (buffer-file-name (buffer-base-buffer)))))
  5685. ;; Add a context string
  5686. (when (org-xor org-context-in-file-links arg)
  5687. (setq txt (if (org-region-active-p)
  5688. (buffer-substring (region-beginning) (region-end))
  5689. (buffer-substring (point-at-bol) (point-at-eol))))
  5690. ;; Only use search option if there is some text.
  5691. (when (string-match "\\S-" txt)
  5692. (setq cpltxt
  5693. (concat cpltxt "::" (org-make-org-heading-search-string txt))
  5694. desc "NONE")))
  5695. (setq link (org-make-link cpltxt)))
  5696. ((interactive-p)
  5697. (error "Cannot link to a buffer which is not visiting a file"))
  5698. (t (setq link nil)))
  5699. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  5700. (setq link (or link cpltxt)
  5701. desc (or desc cpltxt))
  5702. (if (equal desc "NONE") (setq desc nil))
  5703. (if (and (interactive-p) link)
  5704. (progn
  5705. (setq org-stored-links
  5706. (cons (list link desc) org-stored-links))
  5707. (message "Stored: %s" (or desc link)))
  5708. (and link (org-make-link-string link desc)))))
  5709. (defun org-store-link-props (&rest plist)
  5710. "Store link properties, extract names and addresses."
  5711. (let (x adr)
  5712. (when (setq x (plist-get plist :from))
  5713. (setq adr (mail-extract-address-components x))
  5714. (plist-put plist :fromname (car adr))
  5715. (plist-put plist :fromaddress (nth 1 adr)))
  5716. (when (setq x (plist-get plist :to))
  5717. (setq adr (mail-extract-address-components x))
  5718. (plist-put plist :toname (car adr))
  5719. (plist-put plist :toaddress (nth 1 adr))))
  5720. (let ((from (plist-get plist :from))
  5721. (to (plist-get plist :to)))
  5722. (when (and from to org-from-is-user-regexp)
  5723. (plist-put plist :fromto
  5724. (if (string-match org-from-is-user-regexp from)
  5725. (concat "to %t")
  5726. (concat "from %f")))))
  5727. (setq org-store-link-plist plist))
  5728. (defun org-add-link-props (&rest plist)
  5729. "Add these properties to the link property list."
  5730. (let (key value)
  5731. (while plist
  5732. (setq key (pop plist) value (pop plist))
  5733. (setq org-store-link-plist
  5734. (plist-put org-store-link-plist key value)))))
  5735. (defun org-email-link-description (&optional fmt)
  5736. "Return the description part of an email link.
  5737. This takes information from `org-store-link-plist' and formats it
  5738. according to FMT (default from `org-email-link-description-format')."
  5739. (setq fmt (or fmt org-email-link-description-format))
  5740. (let* ((p org-store-link-plist)
  5741. (to (plist-get p :toaddress))
  5742. (from (plist-get p :fromaddress))
  5743. (table
  5744. (list
  5745. (cons "%c" (plist-get p :fromto))
  5746. (cons "%F" (plist-get p :from))
  5747. (cons "%f" (or (plist-get p :fromname) (plist-get p :fromaddress) "?"))
  5748. (cons "%T" (plist-get p :to))
  5749. (cons "%t" (or (plist-get p :toname) (plist-get p :toaddress) "?"))
  5750. (cons "%s" (plist-get p :subject))
  5751. (cons "%m" (plist-get p :message-id)))))
  5752. (when (string-match "%c" fmt)
  5753. ;; Check if the user wrote this message
  5754. (if (and org-from-is-user-regexp from to
  5755. (save-match-data (string-match org-from-is-user-regexp from)))
  5756. (setq fmt (replace-match "to %t" t t fmt))
  5757. (setq fmt (replace-match "from %f" t t fmt))))
  5758. (org-replace-escapes fmt table)))
  5759. (defun org-make-org-heading-search-string (&optional string heading)
  5760. "Make search string for STRING or current headline."
  5761. (interactive)
  5762. (let ((s (or string (org-get-heading))))
  5763. (unless (and string (not heading))
  5764. ;; We are using a headline, clean up garbage in there.
  5765. (if (string-match org-todo-regexp s)
  5766. (setq s (replace-match "" t t s)))
  5767. (if (string-match (org-re ":[[:alnum:]_@:]+:[ \t]*$") s)
  5768. (setq s (replace-match "" t t s)))
  5769. (setq s (org-trim s))
  5770. (if (string-match (concat "^\\(" org-quote-string "\\|"
  5771. org-comment-string "\\)") s)
  5772. (setq s (replace-match "" t t s)))
  5773. (while (string-match org-ts-regexp s)
  5774. (setq s (replace-match "" t t s))))
  5775. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  5776. (setq s (replace-match " " t t s)))
  5777. (or string (setq s (concat "*" s))) ; Add * for headlines
  5778. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  5779. (defun org-make-link (&rest strings)
  5780. "Concatenate STRINGS."
  5781. (apply 'concat strings))
  5782. (defun org-make-link-string (link &optional description)
  5783. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  5784. (unless (string-match "\\S-" link)
  5785. (error "Empty link"))
  5786. (when (stringp description)
  5787. ;; Remove brackets from the description, they are fatal.
  5788. (while (string-match "\\[" description)
  5789. (setq description (replace-match "{" t t description)))
  5790. (while (string-match "\\]" description)
  5791. (setq description (replace-match "}" t t description))))
  5792. (when (equal (org-link-escape link) description)
  5793. ;; No description needed, it is identical
  5794. (setq description nil))
  5795. (when (and (not description)
  5796. (not (equal link (org-link-escape link))))
  5797. (setq description link))
  5798. (concat "[[" (org-link-escape link) "]"
  5799. (if description (concat "[" description "]") "")
  5800. "]"))
  5801. (defconst org-link-escape-chars
  5802. '((?\ . "%20")
  5803. (?\[ . "%5B")
  5804. (?\] . "%5D")
  5805. (?\340 . "%E0") ; `a
  5806. (?\342 . "%E2") ; ^a
  5807. (?\347 . "%E7") ; ,c
  5808. (?\350 . "%E8") ; `e
  5809. (?\351 . "%E9") ; 'e
  5810. (?\352 . "%EA") ; ^e
  5811. (?\356 . "%EE") ; ^i
  5812. (?\364 . "%F4") ; ^o
  5813. (?\371 . "%F9") ; `u
  5814. (?\373 . "%FB") ; ^u
  5815. (?\; . "%3B")
  5816. (?? . "%3F")
  5817. (?= . "%3D")
  5818. (?+ . "%2B")
  5819. )
  5820. "Association list of escapes for some characters problematic in links.
  5821. This is the list that is used for internal purposes.")
  5822. (defconst org-link-escape-chars-browser
  5823. '((?\ . "%20")) ; 32 for the SPC char
  5824. "Association list of escapes for some characters problematic in links.
  5825. This is the list that is used before handing over to the browser.")
  5826. (defun org-link-escape (text &optional table)
  5827. "Escape charaters in TEXT that are problematic for links."
  5828. (setq table (or table org-link-escape-chars))
  5829. (when text
  5830. (let ((re (mapconcat (lambda (x) (regexp-quote
  5831. (char-to-string (car x))))
  5832. table "\\|")))
  5833. (while (string-match re text)
  5834. (setq text
  5835. (replace-match
  5836. (cdr (assoc (string-to-char (match-string 0 text))
  5837. table))
  5838. t t text)))
  5839. text)))
  5840. (defun org-link-unescape (text &optional table)
  5841. "Reverse the action of `org-link-escape'."
  5842. (setq table (or table org-link-escape-chars))
  5843. (when text
  5844. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  5845. table "\\|")))
  5846. (while (string-match re text)
  5847. (setq text
  5848. (replace-match
  5849. (char-to-string (car (rassoc (match-string 0 text) table)))
  5850. t t text)))
  5851. text)))
  5852. (defun org-xor (a b)
  5853. "Exclusive or."
  5854. (if a (not b) b))
  5855. (defun org-get-header (header)
  5856. "Find a header field in the current buffer."
  5857. (save-excursion
  5858. (goto-char (point-min))
  5859. (let ((case-fold-search t) s)
  5860. (cond
  5861. ((eq header 'from)
  5862. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  5863. (setq s (match-string 1)))
  5864. (while (string-match "\"" s)
  5865. (setq s (replace-match "" t t s)))
  5866. (if (string-match "[<(].*" s)
  5867. (setq s (replace-match "" t t s))))
  5868. ((eq header 'message-id)
  5869. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  5870. (setq s (match-string 1))))
  5871. ((eq header 'subject)
  5872. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  5873. (setq s (match-string 1)))))
  5874. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  5875. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  5876. s)))
  5877. (defun org-fixup-message-id-for-http (s)
  5878. "Replace special characters in a message id, so it can be used in an http query."
  5879. (while (string-match "<" s)
  5880. (setq s (replace-match "%3C" t t s)))
  5881. (while (string-match ">" s)
  5882. (setq s (replace-match "%3E" t t s)))
  5883. (while (string-match "@" s)
  5884. (setq s (replace-match "%40" t t s)))
  5885. s)
  5886. ;;;###autoload
  5887. (defun org-insert-link-global ()
  5888. "Insert a link like Org-mode does.
  5889. This command can be called in any mode to insert a link in Org-mode syntax."
  5890. (interactive)
  5891. (org-load-modules-maybe)
  5892. (org-run-like-in-org-mode 'org-insert-link))
  5893. (defun org-insert-link (&optional complete-file link-location)
  5894. "Insert a link. At the prompt, enter the link.
  5895. Completion can be used to select a link previously stored with
  5896. `org-store-link'. When the empty string is entered (i.e. if you just
  5897. press RET at the prompt), the link defaults to the most recently
  5898. stored link. As SPC triggers completion in the minibuffer, you need to
  5899. use M-SPC or C-q SPC to force the insertion of a space character.
  5900. You will also be prompted for a description, and if one is given, it will
  5901. be displayed in the buffer instead of the link.
  5902. If there is already a link at point, this command will allow you to edit link
  5903. and description parts.
  5904. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can
  5905. be selected using completion. The path to the file will be relative to the
  5906. current directory if the file is in the current directory or a subdirectory.
  5907. Otherwise, the link will be the absolute path as completed in the minibuffer
  5908. \(i.e. normally ~/path/to/file).
  5909. With two \\[universal-argument] prefixes, enforce an absolute path even if the file is in
  5910. the current directory or below. With three \\[universal-argument] prefixes, negate the meaning
  5911. of `org-keep-stored-link-after-insertion'.
  5912. If `org-make-link-description-function' is non-nil, this function will be
  5913. called with the link target, and the result will be the default
  5914. link description.
  5915. If the LINK-LOCATION parameter is non-nil, this value will be
  5916. used as the link location instead of reading one interactively."
  5917. (interactive "P")
  5918. (let* ((wcf (current-window-configuration))
  5919. (region (if (org-region-active-p)
  5920. (buffer-substring (region-beginning) (region-end))))
  5921. (remove (and region (list (region-beginning) (region-end))))
  5922. (desc region)
  5923. tmphist ; byte-compile incorrectly complains about this
  5924. (link link-location)
  5925. entry file)
  5926. (cond
  5927. (link-location) ; specified by arg, just use it.
  5928. ((org-in-regexp org-bracket-link-regexp 1)
  5929. ;; We do have a link at point, and we are going to edit it.
  5930. (setq remove (list (match-beginning 0) (match-end 0)))
  5931. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  5932. (setq link (read-string "Link: "
  5933. (org-link-unescape
  5934. (org-match-string-no-properties 1)))))
  5935. ((or (org-in-regexp org-angle-link-re)
  5936. (org-in-regexp org-plain-link-re))
  5937. ;; Convert to bracket link
  5938. (setq remove (list (match-beginning 0) (match-end 0))
  5939. link (read-string "Link: "
  5940. (org-remove-angle-brackets (match-string 0)))))
  5941. ((equal complete-file '(4))
  5942. ;; Completing read for file names.
  5943. (setq file (read-file-name "File: "))
  5944. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  5945. (pwd1 (file-name-as-directory (abbreviate-file-name
  5946. (expand-file-name ".")))))
  5947. (cond
  5948. ((equal complete-file '(16))
  5949. (setq link (org-make-link
  5950. "file:"
  5951. (abbreviate-file-name (expand-file-name file)))))
  5952. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  5953. (setq link (org-make-link "file:" (match-string 1 file))))
  5954. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  5955. (expand-file-name file))
  5956. (setq link (org-make-link
  5957. "file:" (match-string 1 (expand-file-name file)))))
  5958. (t (setq link (org-make-link "file:" file))))))
  5959. (t
  5960. ;; Read link, with completion for stored links.
  5961. (with-output-to-temp-buffer "*Org Links*"
  5962. (princ "Insert a link. Use TAB to complete valid link prefixes.\n")
  5963. (when org-stored-links
  5964. (princ "\nStored links are available with <up>/<down> or M-p/n (most recent with RET):\n\n")
  5965. (princ (mapconcat
  5966. (lambda (x)
  5967. (if (nth 1 x) (concat (car x) " (" (nth 1 x) ")") (car x)))
  5968. (reverse org-stored-links) "\n"))))
  5969. (let ((cw (selected-window)))
  5970. (select-window (get-buffer-window "*Org Links*"))
  5971. (shrink-window-if-larger-than-buffer)
  5972. (setq truncate-lines t)
  5973. (select-window cw))
  5974. ;; Fake a link history, containing the stored links.
  5975. (setq tmphist (append (mapcar 'car org-stored-links)
  5976. org-insert-link-history))
  5977. (unwind-protect
  5978. (setq link (org-completing-read
  5979. "Link: "
  5980. (append
  5981. (mapcar (lambda (x) (list (concat (car x) ":")))
  5982. (append org-link-abbrev-alist-local org-link-abbrev-alist))
  5983. (mapcar (lambda (x) (list (concat x ":")))
  5984. org-link-types))
  5985. nil nil nil
  5986. 'tmphist
  5987. (or (car (car org-stored-links)))))
  5988. (set-window-configuration wcf)
  5989. (kill-buffer "*Org Links*"))
  5990. (setq entry (assoc link org-stored-links))
  5991. (or entry (push link org-insert-link-history))
  5992. (if (funcall (if (equal complete-file '(64)) 'not 'identity)
  5993. (not org-keep-stored-link-after-insertion))
  5994. (setq org-stored-links (delq (assoc link org-stored-links)
  5995. org-stored-links)))
  5996. (setq desc (or desc (nth 1 entry)))))
  5997. (if (string-match org-plain-link-re link)
  5998. ;; URL-like link, normalize the use of angular brackets.
  5999. (setq link (org-make-link (org-remove-angle-brackets link))))
  6000. ;; Check if we are linking to the current file with a search option
  6001. ;; If yes, simplify the link by using only the search option.
  6002. (when (and buffer-file-name
  6003. (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link))
  6004. (let* ((path (match-string 1 link))
  6005. (case-fold-search nil)
  6006. (search (match-string 2 link)))
  6007. (save-match-data
  6008. (if (equal (file-truename buffer-file-name) (file-truename path))
  6009. ;; We are linking to this same file, with a search option
  6010. (setq link search)))))
  6011. ;; Check if we can/should use a relative path. If yes, simplify the link
  6012. (when (string-match "\\<file:\\(.*\\)" link)
  6013. (let* ((path (match-string 1 link))
  6014. (origpath path)
  6015. (case-fold-search nil))
  6016. (cond
  6017. ((eq org-link-file-path-type 'absolute)
  6018. (setq path (abbreviate-file-name (expand-file-name path))))
  6019. ((eq org-link-file-path-type 'noabbrev)
  6020. (setq path (expand-file-name path)))
  6021. ((eq org-link-file-path-type 'relative)
  6022. (setq path (file-relative-name path)))
  6023. (t
  6024. (save-match-data
  6025. (if (string-match (concat "^" (regexp-quote
  6026. (file-name-as-directory
  6027. (expand-file-name "."))))
  6028. (expand-file-name path))
  6029. ;; We are linking a file with relative path name.
  6030. (setq path (substring (expand-file-name path)
  6031. (match-end 0)))))))
  6032. (setq link (concat "file:" path))
  6033. (if (equal desc origpath)
  6034. (setq desc path))))
  6035. (if org-make-link-description-function
  6036. (setq desc (funcall org-make-link-description-function link desc)))
  6037. (setq desc (read-string "Description: " desc))
  6038. (unless (string-match "\\S-" desc) (setq desc nil))
  6039. (if remove (apply 'delete-region remove))
  6040. (insert (org-make-link-string link desc))))
  6041. (defun org-completing-read (&rest args)
  6042. (let ((minibuffer-local-completion-map
  6043. (copy-keymap minibuffer-local-completion-map)))
  6044. (org-defkey minibuffer-local-completion-map " " 'self-insert-command)
  6045. (apply 'completing-read args)))
  6046. ;;; Opening/following a link
  6047. (defvar org-link-search-failed nil)
  6048. (defun org-next-link ()
  6049. "Move forward to the next link.
  6050. If the link is in hidden text, expose it."
  6051. (interactive)
  6052. (when (and org-link-search-failed (eq this-command last-command))
  6053. (goto-char (point-min))
  6054. (message "Link search wrapped back to beginning of buffer"))
  6055. (setq org-link-search-failed nil)
  6056. (let* ((pos (point))
  6057. (ct (org-context))
  6058. (a (assoc :link ct)))
  6059. (if a (goto-char (nth 2 a)))
  6060. (if (re-search-forward org-any-link-re nil t)
  6061. (progn
  6062. (goto-char (match-beginning 0))
  6063. (if (org-invisible-p) (org-show-context)))
  6064. (goto-char pos)
  6065. (setq org-link-search-failed t)
  6066. (error "No further link found"))))
  6067. (defun org-previous-link ()
  6068. "Move backward to the previous link.
  6069. If the link is in hidden text, expose it."
  6070. (interactive)
  6071. (when (and org-link-search-failed (eq this-command last-command))
  6072. (goto-char (point-max))
  6073. (message "Link search wrapped back to end of buffer"))
  6074. (setq org-link-search-failed nil)
  6075. (let* ((pos (point))
  6076. (ct (org-context))
  6077. (a (assoc :link ct)))
  6078. (if a (goto-char (nth 1 a)))
  6079. (if (re-search-backward org-any-link-re nil t)
  6080. (progn
  6081. (goto-char (match-beginning 0))
  6082. (if (org-invisible-p) (org-show-context)))
  6083. (goto-char pos)
  6084. (setq org-link-search-failed t)
  6085. (error "No further link found"))))
  6086. (defun org-find-file-at-mouse (ev)
  6087. "Open file link or URL at mouse."
  6088. (interactive "e")
  6089. (mouse-set-point ev)
  6090. (org-open-at-point 'in-emacs))
  6091. (defun org-open-at-mouse (ev)
  6092. "Open file link or URL at mouse."
  6093. (interactive "e")
  6094. (mouse-set-point ev)
  6095. (org-open-at-point))
  6096. (defvar org-window-config-before-follow-link nil
  6097. "The window configuration before following a link.
  6098. This is saved in case the need arises to restore it.")
  6099. (defvar org-open-link-marker (make-marker)
  6100. "Marker pointing to the location where `org-open-at-point; was called.")
  6101. ;;;###autoload
  6102. (defun org-open-at-point-global ()
  6103. "Follow a link like Org-mode does.
  6104. This command can be called in any mode to follow a link that has
  6105. Org-mode syntax."
  6106. (interactive)
  6107. (org-run-like-in-org-mode 'org-open-at-point))
  6108. ;;;###autoload
  6109. (defun org-open-link-from-string (s &optional arg)
  6110. "Open a link in the string S, as if it was in Org-mode."
  6111. (interactive "sLink: \nP")
  6112. (with-temp-buffer
  6113. (let ((org-inhibit-startup t))
  6114. (org-mode)
  6115. (insert s)
  6116. (goto-char (point-min))
  6117. (org-open-at-point arg))))
  6118. (defun org-open-at-point (&optional in-emacs)
  6119. "Open link at or after point.
  6120. If there is no link at point, this function will search forward up to
  6121. the end of the current subtree.
  6122. Normally, files will be opened by an appropriate application. If the
  6123. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  6124. (interactive "P")
  6125. (org-load-modules-maybe)
  6126. (move-marker org-open-link-marker (point))
  6127. (setq org-window-config-before-follow-link (current-window-configuration))
  6128. (org-remove-occur-highlights nil nil t)
  6129. (if (org-at-timestamp-p t)
  6130. (org-follow-timestamp-link)
  6131. (let (type path link line search (pos (point)))
  6132. (catch 'match
  6133. (save-excursion
  6134. (skip-chars-forward "^]\n\r")
  6135. (when (org-in-regexp org-bracket-link-regexp)
  6136. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  6137. (while (string-match " *\n *" link)
  6138. (setq link (replace-match " " t t link)))
  6139. (setq link (org-link-expand-abbrev link))
  6140. (if (string-match org-link-re-with-space2 link)
  6141. (setq type (match-string 1 link) path (match-string 2 link))
  6142. (setq type "thisfile" path link))
  6143. (throw 'match t)))
  6144. (when (get-text-property (point) 'org-linked-text)
  6145. (setq type "thisfile"
  6146. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  6147. (1+ (point)) (point))
  6148. path (buffer-substring
  6149. (previous-single-property-change pos 'org-linked-text)
  6150. (next-single-property-change pos 'org-linked-text)))
  6151. (throw 'match t))
  6152. (save-excursion
  6153. (when (or (org-in-regexp org-angle-link-re)
  6154. (org-in-regexp org-plain-link-re))
  6155. (setq type (match-string 1) path (match-string 2))
  6156. (throw 'match t)))
  6157. (when (org-in-regexp "\\<\\([^><\n]+\\)\\>")
  6158. (setq type "tree-match"
  6159. path (match-string 1))
  6160. (throw 'match t))
  6161. (save-excursion
  6162. (when (org-in-regexp (org-re "\\(:[[:alnum:]_@:]+\\):[ \t]*$"))
  6163. (setq type "tags"
  6164. path (match-string 1))
  6165. (while (string-match ":" path)
  6166. (setq path (replace-match "+" t t path)))
  6167. (throw 'match t))))
  6168. (unless path
  6169. (error "No link found"))
  6170. ;; Remove any trailing spaces in path
  6171. (if (string-match " +\\'" path)
  6172. (setq path (replace-match "" t t path)))
  6173. (cond
  6174. ((assoc type org-link-protocols)
  6175. (funcall (nth 1 (assoc type org-link-protocols)) path))
  6176. ((equal type "mailto")
  6177. (let ((cmd (car org-link-mailto-program))
  6178. (args (cdr org-link-mailto-program)) args1
  6179. (address path) (subject "") a)
  6180. (if (string-match "\\(.*\\)::\\(.*\\)" path)
  6181. (setq address (match-string 1 path)
  6182. subject (org-link-escape (match-string 2 path))))
  6183. (while args
  6184. (cond
  6185. ((not (stringp (car args))) (push (pop args) args1))
  6186. (t (setq a (pop args))
  6187. (if (string-match "%a" a)
  6188. (setq a (replace-match address t t a)))
  6189. (if (string-match "%s" a)
  6190. (setq a (replace-match subject t t a)))
  6191. (push a args1))))
  6192. (apply cmd (nreverse args1))))
  6193. ((member type '("http" "https" "ftp" "news"))
  6194. (browse-url (concat type ":" (org-link-escape
  6195. path org-link-escape-chars-browser))))
  6196. ((member type '("message"))
  6197. (browse-url (concat type ":" path)))
  6198. ((string= type "tags")
  6199. (org-tags-view in-emacs path))
  6200. ((string= type "thisfile")
  6201. (if in-emacs
  6202. (switch-to-buffer-other-window
  6203. (org-get-buffer-for-internal-link (current-buffer)))
  6204. (org-mark-ring-push))
  6205. (let ((cmd `(org-link-search
  6206. ,path
  6207. ,(cond ((equal in-emacs '(4)) 'occur)
  6208. ((equal in-emacs '(16)) 'org-occur)
  6209. (t nil))
  6210. ,pos)))
  6211. (condition-case nil (eval cmd)
  6212. (error (progn (widen) (eval cmd))))))
  6213. ((string= type "tree-match")
  6214. (org-occur (concat "\\[" (regexp-quote path) "\\]")))
  6215. ((string= type "file")
  6216. (if (string-match "::\\([0-9]+\\)\\'" path)
  6217. (setq line (string-to-number (match-string 1 path))
  6218. path (substring path 0 (match-beginning 0)))
  6219. (if (string-match "::\\(.+\\)\\'" path)
  6220. (setq search (match-string 1 path)
  6221. path (substring path 0 (match-beginning 0)))))
  6222. (if (string-match "[*?{]" (file-name-nondirectory path))
  6223. (dired path)
  6224. (org-open-file path in-emacs line search)))
  6225. ((string= type "news")
  6226. (require 'org-gnus)
  6227. (org-gnus-follow-link path))
  6228. ((string= type "shell")
  6229. (let ((cmd path))
  6230. (if (or (not org-confirm-shell-link-function)
  6231. (funcall org-confirm-shell-link-function
  6232. (format "Execute \"%s\" in shell? "
  6233. (org-add-props cmd nil
  6234. 'face 'org-warning))))
  6235. (progn
  6236. (message "Executing %s" cmd)
  6237. (shell-command cmd))
  6238. (error "Abort"))))
  6239. ((string= type "elisp")
  6240. (let ((cmd path))
  6241. (if (or (not org-confirm-elisp-link-function)
  6242. (funcall org-confirm-elisp-link-function
  6243. (format "Execute \"%s\" as elisp? "
  6244. (org-add-props cmd nil
  6245. 'face 'org-warning))))
  6246. (message "%s => %s" cmd (eval (read cmd)))
  6247. (error "Abort"))))
  6248. (t
  6249. (browse-url-at-point)))))
  6250. (move-marker org-open-link-marker nil)
  6251. (run-hook-with-args 'org-follow-link-hook))
  6252. ;;;; Time estimates
  6253. (defun org-get-effort (&optional pom)
  6254. "Get the effort estimate for the current entry."
  6255. (org-entry-get pom org-effort-property))
  6256. ;;; File search
  6257. (defvar org-create-file-search-functions nil
  6258. "List of functions to construct the right search string for a file link.
  6259. These functions are called in turn with point at the location to
  6260. which the link should point.
  6261. A function in the hook should first test if it would like to
  6262. handle this file type, for example by checking the major-mode or
  6263. the file extension. If it decides not to handle this file, it
  6264. should just return nil to give other functions a chance. If it
  6265. does handle the file, it must return the search string to be used
  6266. when following the link. The search string will be part of the
  6267. file link, given after a double colon, and `org-open-at-point'
  6268. will automatically search for it. If special measures must be
  6269. taken to make the search successful, another function should be
  6270. added to the companion hook `org-execute-file-search-functions',
  6271. which see.
  6272. A function in this hook may also use `setq' to set the variable
  6273. `description' to provide a suggestion for the descriptive text to
  6274. be used for this link when it gets inserted into an Org-mode
  6275. buffer with \\[org-insert-link].")
  6276. (defvar org-execute-file-search-functions nil
  6277. "List of functions to execute a file search triggered by a link.
  6278. Functions added to this hook must accept a single argument, the
  6279. search string that was part of the file link, the part after the
  6280. double colon. The function must first check if it would like to
  6281. handle this search, for example by checking the major-mode or the
  6282. file extension. If it decides not to handle this search, it
  6283. should just return nil to give other functions a chance. If it
  6284. does handle the search, it must return a non-nil value to keep
  6285. other functions from trying.
  6286. Each function can access the current prefix argument through the
  6287. variable `current-prefix-argument'. Note that a single prefix is
  6288. used to force opening a link in Emacs, so it may be good to only
  6289. use a numeric or double prefix to guide the search function.
  6290. In case this is needed, a function in this hook can also restore
  6291. the window configuration before `org-open-at-point' was called using:
  6292. (set-window-configuration org-window-config-before-follow-link)")
  6293. (defun org-link-search (s &optional type avoid-pos)
  6294. "Search for a link search option.
  6295. If S is surrounded by forward slashes, it is interpreted as a
  6296. regular expression. In org-mode files, this will create an `org-occur'
  6297. sparse tree. In ordinary files, `occur' will be used to list matches.
  6298. If the current buffer is in `dired-mode', grep will be used to search
  6299. in all files. If AVOID-POS is given, ignore matches near that position."
  6300. (let ((case-fold-search t)
  6301. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  6302. (markers (concat "\\(?:" (mapconcat (lambda (x) (regexp-quote (car x)))
  6303. (append '(("") (" ") ("\t") ("\n"))
  6304. org-emphasis-alist)
  6305. "\\|") "\\)"))
  6306. (pos (point))
  6307. (pre nil) (post nil)
  6308. words re0 re1 re2 re3 re4_ re4 re5 re2a re2a_ reall)
  6309. (cond
  6310. ;; First check if there are any special
  6311. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  6312. ;; Now try the builtin stuff
  6313. ((save-excursion
  6314. (goto-char (point-min))
  6315. (and
  6316. (re-search-forward
  6317. (concat "<<" (regexp-quote s0) ">>") nil t)
  6318. (setq type 'dedicated
  6319. pos (match-beginning 0))))
  6320. ;; There is an exact target for this
  6321. (goto-char pos))
  6322. ((string-match "^/\\(.*\\)/$" s)
  6323. ;; A regular expression
  6324. (cond
  6325. ((org-mode-p)
  6326. (org-occur (match-string 1 s)))
  6327. ;;((eq major-mode 'dired-mode)
  6328. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  6329. (t (org-do-occur (match-string 1 s)))))
  6330. (t
  6331. ;; A normal search strings
  6332. (when (equal (string-to-char s) ?*)
  6333. ;; Anchor on headlines, post may include tags.
  6334. (setq pre "^\\*+[ \t]+\\(?:\\sw+\\)?[ \t]*"
  6335. post (org-re "[ \t]*\\(?:[ \t]+:[[:alnum:]_@:+]:[ \t]*\\)?$")
  6336. s (substring s 1)))
  6337. (remove-text-properties
  6338. 0 (length s)
  6339. '(face nil mouse-face nil keymap nil fontified nil) s)
  6340. ;; Make a series of regular expressions to find a match
  6341. (setq words (org-split-string s "[ \n\r\t]+")
  6342. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  6343. re2 (concat markers "\\(" (mapconcat 'downcase words "[ \t]+")
  6344. "\\)" markers)
  6345. re2a_ (concat "\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  6346. re2a (concat "[ \t\r\n]" re2a_)
  6347. re4_ (concat "\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  6348. re4 (concat "[^a-zA-Z_]" re4_)
  6349. re1 (concat pre re2 post)
  6350. re3 (concat pre (if pre re4_ re4) post)
  6351. re5 (concat pre ".*" re4)
  6352. re2 (concat pre re2)
  6353. re2a (concat pre (if pre re2a_ re2a))
  6354. re4 (concat pre (if pre re4_ re4))
  6355. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  6356. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  6357. re5 "\\)"
  6358. ))
  6359. (cond
  6360. ((eq type 'org-occur) (org-occur reall))
  6361. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  6362. (t (goto-char (point-min))
  6363. (setq type 'fuzzy)
  6364. (if (or (and (org-search-not-self 1 re0 nil t) (setq type 'dedicated))
  6365. (org-search-not-self 1 re1 nil t)
  6366. (org-search-not-self 1 re2 nil t)
  6367. (org-search-not-self 1 re2a nil t)
  6368. (org-search-not-self 1 re3 nil t)
  6369. (org-search-not-self 1 re4 nil t)
  6370. (org-search-not-self 1 re5 nil t)
  6371. )
  6372. (goto-char (match-beginning 1))
  6373. (goto-char pos)
  6374. (error "No match")))))
  6375. (t
  6376. ;; Normal string-search
  6377. (goto-char (point-min))
  6378. (if (search-forward s nil t)
  6379. (goto-char (match-beginning 0))
  6380. (error "No match"))))
  6381. (and (org-mode-p) (org-show-context 'link-search))
  6382. type))
  6383. (defun org-search-not-self (group &rest args)
  6384. "Execute `re-search-forward', but only accept matches that do not
  6385. enclose the position of `org-open-link-marker'."
  6386. (let ((m org-open-link-marker))
  6387. (catch 'exit
  6388. (while (apply 're-search-forward args)
  6389. (unless (get-text-property (match-end group) 'intangible) ; Emacs 21
  6390. (goto-char (match-end group))
  6391. (if (and (or (not (eq (marker-buffer m) (current-buffer)))
  6392. (> (match-beginning 0) (marker-position m))
  6393. (< (match-end 0) (marker-position m)))
  6394. (save-match-data
  6395. (or (not (org-in-regexp
  6396. org-bracket-link-analytic-regexp 1))
  6397. (not (match-end 4)) ; no description
  6398. (and (<= (match-beginning 4) (point))
  6399. (>= (match-end 4) (point))))))
  6400. (throw 'exit (point))))))))
  6401. (defun org-get-buffer-for-internal-link (buffer)
  6402. "Return a buffer to be used for displaying the link target of internal links."
  6403. (cond
  6404. ((not org-display-internal-link-with-indirect-buffer)
  6405. buffer)
  6406. ((string-match "(Clone)$" (buffer-name buffer))
  6407. (message "Buffer is already a clone, not making another one")
  6408. ;; we also do not modify visibility in this case
  6409. buffer)
  6410. (t ; make a new indirect buffer for displaying the link
  6411. (let* ((bn (buffer-name buffer))
  6412. (ibn (concat bn "(Clone)"))
  6413. (ib (or (get-buffer ibn) (make-indirect-buffer buffer ibn 'clone))))
  6414. (with-current-buffer ib (org-overview))
  6415. ib))))
  6416. (defun org-do-occur (regexp &optional cleanup)
  6417. "Call the Emacs command `occur'.
  6418. If CLEANUP is non-nil, remove the printout of the regular expression
  6419. in the *Occur* buffer. This is useful if the regex is long and not useful
  6420. to read."
  6421. (occur regexp)
  6422. (when cleanup
  6423. (let ((cwin (selected-window)) win beg end)
  6424. (when (setq win (get-buffer-window "*Occur*"))
  6425. (select-window win))
  6426. (goto-char (point-min))
  6427. (when (re-search-forward "match[a-z]+" nil t)
  6428. (setq beg (match-end 0))
  6429. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  6430. (setq end (1- (match-beginning 0)))))
  6431. (and beg end (let ((inhibit-read-only t)) (delete-region beg end)))
  6432. (goto-char (point-min))
  6433. (select-window cwin))))
  6434. ;;; The mark ring for links jumps
  6435. (defvar org-mark-ring nil
  6436. "Mark ring for positions before jumps in Org-mode.")
  6437. (defvar org-mark-ring-last-goto nil
  6438. "Last position in the mark ring used to go back.")
  6439. ;; Fill and close the ring
  6440. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  6441. (loop for i from 1 to org-mark-ring-length do
  6442. (push (make-marker) org-mark-ring))
  6443. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  6444. org-mark-ring)
  6445. (defun org-mark-ring-push (&optional pos buffer)
  6446. "Put the current position or POS into the mark ring and rotate it."
  6447. (interactive)
  6448. (setq pos (or pos (point)))
  6449. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  6450. (move-marker (car org-mark-ring)
  6451. (or pos (point))
  6452. (or buffer (current-buffer)))
  6453. (message "%s"
  6454. (substitute-command-keys
  6455. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  6456. (defun org-mark-ring-goto (&optional n)
  6457. "Jump to the previous position in the mark ring.
  6458. With prefix arg N, jump back that many stored positions. When
  6459. called several times in succession, walk through the entire ring.
  6460. Org-mode commands jumping to a different position in the current file,
  6461. or to another Org-mode file, automatically push the old position
  6462. onto the ring."
  6463. (interactive "p")
  6464. (let (p m)
  6465. (if (eq last-command this-command)
  6466. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  6467. (setq p org-mark-ring))
  6468. (setq org-mark-ring-last-goto p)
  6469. (setq m (car p))
  6470. (switch-to-buffer (marker-buffer m))
  6471. (goto-char m)
  6472. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-context 'mark-goto))))
  6473. (defun org-remove-angle-brackets (s)
  6474. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  6475. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  6476. s)
  6477. (defun org-add-angle-brackets (s)
  6478. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  6479. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  6480. s)
  6481. (defun org-remove-double-quotes (s)
  6482. (if (equal (substring s 0 1) "\"") (setq s (substring s 1)))
  6483. (if (equal (substring s -1) "\"") (setq s (substring s 0 -1)))
  6484. s)
  6485. ;;; Following specific links
  6486. (defun org-follow-timestamp-link ()
  6487. (cond
  6488. ((org-at-date-range-p t)
  6489. (let ((org-agenda-start-on-weekday)
  6490. (t1 (match-string 1))
  6491. (t2 (match-string 2)))
  6492. (setq t1 (time-to-days (org-time-string-to-time t1))
  6493. t2 (time-to-days (org-time-string-to-time t2)))
  6494. (org-agenda-list nil t1 (1+ (- t2 t1)))))
  6495. ((org-at-timestamp-p t)
  6496. (org-agenda-list nil (time-to-days (org-time-string-to-time
  6497. (substring (match-string 1) 0 10)))
  6498. 1))
  6499. (t (error "This should not happen"))))
  6500. ;;; Following file links
  6501. (defvar org-wait nil)
  6502. (defun org-open-file (path &optional in-emacs line search)
  6503. "Open the file at PATH.
  6504. First, this expands any special file name abbreviations. Then the
  6505. configuration variable `org-file-apps' is checked if it contains an
  6506. entry for this file type, and if yes, the corresponding command is launched.
  6507. If no application is found, Emacs simply visits the file.
  6508. With optional argument IN-EMACS, Emacs will visit the file.
  6509. Optional LINE specifies a line to go to, optional SEARCH a string to
  6510. search for. If LINE or SEARCH is given, the file will always be
  6511. opened in Emacs.
  6512. If the file does not exist, an error is thrown."
  6513. (setq in-emacs (or in-emacs line search))
  6514. (let* ((file (if (equal path "")
  6515. buffer-file-name
  6516. (substitute-in-file-name (expand-file-name path))))
  6517. (apps (append org-file-apps (org-default-apps)))
  6518. (remp (and (assq 'remote apps) (org-file-remote-p file)))
  6519. (dirp (if remp nil (file-directory-p file)))
  6520. (dfile (downcase file))
  6521. (old-buffer (current-buffer))
  6522. (old-pos (point))
  6523. (old-mode major-mode)
  6524. ext cmd)
  6525. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  6526. (setq ext (match-string 1 dfile))
  6527. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  6528. (setq ext (match-string 1 dfile))))
  6529. (if in-emacs
  6530. (setq cmd 'emacs)
  6531. (setq cmd (or (and remp (cdr (assoc 'remote apps)))
  6532. (and dirp (cdr (assoc 'directory apps)))
  6533. (cdr (assoc ext apps))
  6534. (cdr (assoc t apps)))))
  6535. (when (eq cmd 'mailcap)
  6536. (require 'mailcap)
  6537. (mailcap-parse-mailcaps)
  6538. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  6539. (command (mailcap-mime-info mime-type)))
  6540. (if (stringp command)
  6541. (setq cmd command)
  6542. (setq cmd 'emacs))))
  6543. (if (and (not (eq cmd 'emacs)) ; Emacs has no problems with non-ex files
  6544. (not (file-exists-p file))
  6545. (not org-open-non-existing-files))
  6546. (error "No such file: %s" file))
  6547. (cond
  6548. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  6549. ;; Remove quotes around the file name - we'll use shell-quote-argument.
  6550. (while (string-match "['\"]%s['\"]" cmd)
  6551. (setq cmd (replace-match "%s" t t cmd)))
  6552. (while (string-match "%s" cmd)
  6553. (setq cmd (replace-match
  6554. (save-match-data (shell-quote-argument file))
  6555. t t cmd)))
  6556. (save-window-excursion
  6557. (start-process-shell-command cmd nil cmd)
  6558. (and (boundp 'org-wait) (numberp org-wait) (sit-for org-wait))
  6559. ))
  6560. ((or (stringp cmd)
  6561. (eq cmd 'emacs))
  6562. (funcall (cdr (assq 'file org-link-frame-setup)) file)
  6563. (widen)
  6564. (if line (goto-line line)
  6565. (if search (org-link-search search))))
  6566. ((consp cmd)
  6567. (eval cmd))
  6568. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  6569. (and (org-mode-p) (eq old-mode 'org-mode)
  6570. (or (not (equal old-buffer (current-buffer)))
  6571. (not (equal old-pos (point))))
  6572. (org-mark-ring-push old-pos old-buffer))))
  6573. (defun org-default-apps ()
  6574. "Return the default applications for this operating system."
  6575. (cond
  6576. ((eq system-type 'darwin)
  6577. org-file-apps-defaults-macosx)
  6578. ((eq system-type 'windows-nt)
  6579. org-file-apps-defaults-windowsnt)
  6580. (t org-file-apps-defaults-gnu)))
  6581. (defvar ange-ftp-name-format) ; to silence the XEmacs compiler.
  6582. (defun org-file-remote-p (file)
  6583. "Test whether FILE specifies a location on a remote system.
  6584. Return non-nil if the location is indeed remote.
  6585. For example, the filename \"/user@host:/foo\" specifies a location
  6586. on the system \"/user@host:\"."
  6587. (cond ((fboundp 'file-remote-p)
  6588. (file-remote-p file))
  6589. ((fboundp 'tramp-handle-file-remote-p)
  6590. (tramp-handle-file-remote-p file))
  6591. ((and (boundp 'ange-ftp-name-format)
  6592. (string-match (car ange-ftp-name-format) file))
  6593. t)
  6594. (t nil)))
  6595. ;;;; Refiling
  6596. (defun org-get-org-file ()
  6597. "Read a filename, with default directory `org-directory'."
  6598. (let ((default (or org-default-notes-file remember-data-file)))
  6599. (read-file-name (format "File name [%s]: " default)
  6600. (file-name-as-directory org-directory)
  6601. default)))
  6602. (defun org-notes-order-reversed-p ()
  6603. "Check if the current file should receive notes in reversed order."
  6604. (cond
  6605. ((not org-reverse-note-order) nil)
  6606. ((eq t org-reverse-note-order) t)
  6607. ((not (listp org-reverse-note-order)) nil)
  6608. (t (catch 'exit
  6609. (let ((all org-reverse-note-order)
  6610. entry)
  6611. (while (setq entry (pop all))
  6612. (if (string-match (car entry) buffer-file-name)
  6613. (throw 'exit (cdr entry))))
  6614. nil)))))
  6615. (defvar org-refile-target-table nil
  6616. "The list of refile targets, created by `org-refile'.")
  6617. (defvar org-agenda-new-buffers nil
  6618. "Buffers created to visit agenda files.")
  6619. (defun org-get-refile-targets (&optional default-buffer)
  6620. "Produce a table with refile targets."
  6621. (let ((entries (or org-refile-targets '((nil . (:level . 1)))))
  6622. targets txt re files f desc descre)
  6623. (with-current-buffer (or default-buffer (current-buffer))
  6624. (while (setq entry (pop entries))
  6625. (setq files (car entry) desc (cdr entry))
  6626. (cond
  6627. ((null files) (setq files (list (current-buffer))))
  6628. ((eq files 'org-agenda-files)
  6629. (setq files (org-agenda-files 'unrestricted)))
  6630. ((and (symbolp files) (fboundp files))
  6631. (setq files (funcall files)))
  6632. ((and (symbolp files) (boundp files))
  6633. (setq files (symbol-value files))))
  6634. (if (stringp files) (setq files (list files)))
  6635. (cond
  6636. ((eq (car desc) :tag)
  6637. (setq descre (concat "^\\*+[ \t]+.*?:" (regexp-quote (cdr desc)) ":")))
  6638. ((eq (car desc) :todo)
  6639. (setq descre (concat "^\\*+[ \t]+" (regexp-quote (cdr desc)) "[ \t]")))
  6640. ((eq (car desc) :regexp)
  6641. (setq descre (cdr desc)))
  6642. ((eq (car desc) :level)
  6643. (setq descre (concat "^\\*\\{" (number-to-string
  6644. (if org-odd-levels-only
  6645. (1- (* 2 (cdr desc)))
  6646. (cdr desc)))
  6647. "\\}[ \t]")))
  6648. ((eq (car desc) :maxlevel)
  6649. (setq descre (concat "^\\*\\{1," (number-to-string
  6650. (if org-odd-levels-only
  6651. (1- (* 2 (cdr desc)))
  6652. (cdr desc)))
  6653. "\\}[ \t]")))
  6654. (t (error "Bad refiling target description %s" desc)))
  6655. (while (setq f (pop files))
  6656. (save-excursion
  6657. (set-buffer (if (bufferp f) f (org-get-agenda-file-buffer f)))
  6658. (if (bufferp f) (setq f (buffer-file-name (buffer-base-buffer f))))
  6659. (save-excursion
  6660. (save-restriction
  6661. (widen)
  6662. (goto-char (point-min))
  6663. (while (re-search-forward descre nil t)
  6664. (goto-char (point-at-bol))
  6665. (when (looking-at org-complex-heading-regexp)
  6666. (setq txt (match-string 4)
  6667. re (concat "^" (regexp-quote
  6668. (buffer-substring (match-beginning 1)
  6669. (match-end 4)))))
  6670. (if (match-end 5) (setq re (concat re "[ \t]+"
  6671. (regexp-quote
  6672. (match-string 5)))))
  6673. (setq re (concat re "[ \t]*$"))
  6674. (when org-refile-use-outline-path
  6675. (setq txt (mapconcat 'identity
  6676. (append
  6677. (if (eq org-refile-use-outline-path 'file)
  6678. (list (file-name-nondirectory
  6679. (buffer-file-name (buffer-base-buffer))))
  6680. (if (eq org-refile-use-outline-path 'full-file-path)
  6681. (list (buffer-file-name (buffer-base-buffer)))))
  6682. (org-get-outline-path)
  6683. (list txt))
  6684. "/")))
  6685. (push (list txt f re (point)) targets))
  6686. (goto-char (point-at-eol))))))))
  6687. (nreverse targets))))
  6688. (defun org-get-outline-path ()
  6689. "Return the outline path to the current entry, as a list."
  6690. (let (rtn)
  6691. (save-excursion
  6692. (while (org-up-heading-safe)
  6693. (when (looking-at org-complex-heading-regexp)
  6694. (push (org-match-string-no-properties 4) rtn)))
  6695. rtn)))
  6696. (defvar org-refile-history nil
  6697. "History for refiling operations.")
  6698. (defun org-refile (&optional goto default-buffer)
  6699. "Move the entry at point to another heading.
  6700. The list of target headings is compiled using the information in
  6701. `org-refile-targets', which see. This list is created before each use
  6702. and will therefore always be up-to-date.
  6703. At the target location, the entry is filed as a subitem of the target heading.
  6704. Depending on `org-reverse-note-order', the new subitem will either be the
  6705. first of the last subitem.
  6706. With prefix arg GOTO, the command will only visit the target location,
  6707. not actually move anything.
  6708. With a double prefix `C-c C-c', go to the location where the last refiling
  6709. operation has put the subtree."
  6710. (interactive "P")
  6711. (let* ((cbuf (current-buffer))
  6712. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6713. pos it nbuf file re level reversed)
  6714. (if (equal goto '(16))
  6715. (org-refile-goto-last-stored)
  6716. (when (setq it (org-refile-get-location
  6717. (if goto "Goto: " "Refile to: ") default-buffer))
  6718. (setq file (nth 1 it)
  6719. re (nth 2 it)
  6720. pos (nth 3 it))
  6721. (setq nbuf (or (find-buffer-visiting file)
  6722. (find-file-noselect file)))
  6723. (if goto
  6724. (progn
  6725. (switch-to-buffer nbuf)
  6726. (goto-char pos)
  6727. (org-show-context 'org-goto))
  6728. (org-copy-subtree 1 nil t)
  6729. (save-excursion
  6730. (set-buffer (setq nbuf (or (find-buffer-visiting file)
  6731. (find-file-noselect file))))
  6732. (setq reversed (org-notes-order-reversed-p))
  6733. (save-excursion
  6734. (save-restriction
  6735. (widen)
  6736. (goto-char pos)
  6737. (looking-at outline-regexp)
  6738. (setq level (org-get-valid-level (funcall outline-level) 1))
  6739. (goto-char
  6740. (if reversed
  6741. (outline-next-heading)
  6742. (or (save-excursion (outline-get-next-sibling))
  6743. (org-end-of-subtree t t)
  6744. (point-max))))
  6745. (bookmark-set "org-refile-last-stored")
  6746. (org-paste-subtree level))))
  6747. (org-cut-subtree)
  6748. (setq org-markers-to-move nil)
  6749. (message "Entry refiled to \"%s\"" (car it)))))))
  6750. (defun org-refile-goto-last-stored ()
  6751. "Go to the location where the last refile was stored."
  6752. (interactive)
  6753. (bookmark-jump "org-refile-last-stored")
  6754. (message "This is the location of the last refile"))
  6755. (defun org-refile-get-location (&optional prompt default-buffer)
  6756. "Prompt the user for a refile location, using PROMPT."
  6757. (let ((org-refile-targets org-refile-targets)
  6758. (org-refile-use-outline-path org-refile-use-outline-path))
  6759. (setq org-refile-target-table (org-get-refile-targets default-buffer)))
  6760. (unless org-refile-target-table
  6761. (error "No refile targets"))
  6762. (let* ((cbuf (current-buffer))
  6763. (filename (buffer-file-name (buffer-base-buffer cbuf)))
  6764. (fname (and filename (file-truename filename)))
  6765. (tbl (mapcar
  6766. (lambda (x)
  6767. (if (not (equal fname (file-truename (nth 1 x))))
  6768. (cons (concat (car x) " (" (file-name-nondirectory
  6769. (nth 1 x)) ")")
  6770. (cdr x))
  6771. x))
  6772. org-refile-target-table))
  6773. (completion-ignore-case t))
  6774. (assoc (completing-read prompt tbl nil t nil 'org-refile-history)
  6775. tbl)))
  6776. ;;;; Dynamic blocks
  6777. (defun org-find-dblock (name)
  6778. "Find the first dynamic block with name NAME in the buffer.
  6779. If not found, stay at current position and return nil."
  6780. (let (pos)
  6781. (save-excursion
  6782. (goto-char (point-min))
  6783. (setq pos (and (re-search-forward (concat "^#\\+BEGIN:[ \t]+" name "\\>")
  6784. nil t)
  6785. (match-beginning 0))))
  6786. (if pos (goto-char pos))
  6787. pos))
  6788. (defconst org-dblock-start-re
  6789. "^#\\+BEGIN:[ \t]+\\(\\S-+\\)\\([ \t]+\\(.*\\)\\)?"
  6790. "Matches the startline of a dynamic block, with parameters.")
  6791. (defconst org-dblock-end-re "^#\\+END\\([: \t\r\n]\\|$\\)"
  6792. "Matches the end of a dyhamic block.")
  6793. (defun org-create-dblock (plist)
  6794. "Create a dynamic block section, with parameters taken from PLIST.
  6795. PLIST must containe a :name entry which is used as name of the block."
  6796. (unless (bolp) (newline))
  6797. (let ((name (plist-get plist :name)))
  6798. (insert "#+BEGIN: " name)
  6799. (while plist
  6800. (if (eq (car plist) :name)
  6801. (setq plist (cddr plist))
  6802. (insert " " (prin1-to-string (pop plist)))))
  6803. (insert "\n\n#+END:\n")
  6804. (beginning-of-line -2)))
  6805. (defun org-prepare-dblock ()
  6806. "Prepare dynamic block for refresh.
  6807. This empties the block, puts the cursor at the insert position and returns
  6808. the property list including an extra property :name with the block name."
  6809. (unless (looking-at org-dblock-start-re)
  6810. (error "Not at a dynamic block"))
  6811. (let* ((begdel (1+ (match-end 0)))
  6812. (name (org-no-properties (match-string 1)))
  6813. (params (append (list :name name)
  6814. (read (concat "(" (match-string 3) ")")))))
  6815. (unless (re-search-forward org-dblock-end-re nil t)
  6816. (error "Dynamic block not terminated"))
  6817. (setq params
  6818. (append params
  6819. (list :content (buffer-substring
  6820. begdel (match-beginning 0)))))
  6821. (delete-region begdel (match-beginning 0))
  6822. (goto-char begdel)
  6823. (open-line 1)
  6824. params))
  6825. (defun org-map-dblocks (&optional command)
  6826. "Apply COMMAND to all dynamic blocks in the current buffer.
  6827. If COMMAND is not given, use `org-update-dblock'."
  6828. (let ((cmd (or command 'org-update-dblock))
  6829. pos)
  6830. (save-excursion
  6831. (goto-char (point-min))
  6832. (while (re-search-forward org-dblock-start-re nil t)
  6833. (goto-char (setq pos (match-beginning 0)))
  6834. (condition-case nil
  6835. (funcall cmd)
  6836. (error (message "Error during update of dynamic block")))
  6837. (goto-char pos)
  6838. (unless (re-search-forward org-dblock-end-re nil t)
  6839. (error "Dynamic block not terminated"))))))
  6840. (defun org-dblock-update (&optional arg)
  6841. "User command for updating dynamic blocks.
  6842. Update the dynamic block at point. With prefix ARG, update all dynamic
  6843. blocks in the buffer."
  6844. (interactive "P")
  6845. (if arg
  6846. (org-update-all-dblocks)
  6847. (or (looking-at org-dblock-start-re)
  6848. (org-beginning-of-dblock))
  6849. (org-update-dblock)))
  6850. (defun org-update-dblock ()
  6851. "Update the dynamic block at point
  6852. This means to empty the block, parse for parameters and then call
  6853. the correct writing function."
  6854. (save-window-excursion
  6855. (let* ((pos (point))
  6856. (line (org-current-line))
  6857. (params (org-prepare-dblock))
  6858. (name (plist-get params :name))
  6859. (cmd (intern (concat "org-dblock-write:" name))))
  6860. (message "Updating dynamic block `%s' at line %d..." name line)
  6861. (funcall cmd params)
  6862. (message "Updating dynamic block `%s' at line %d...done" name line)
  6863. (goto-char pos))))
  6864. (defun org-beginning-of-dblock ()
  6865. "Find the beginning of the dynamic block at point.
  6866. Error if there is no scuh block at point."
  6867. (let ((pos (point))
  6868. beg)
  6869. (end-of-line 1)
  6870. (if (and (re-search-backward org-dblock-start-re nil t)
  6871. (setq beg (match-beginning 0))
  6872. (re-search-forward org-dblock-end-re nil t)
  6873. (> (match-end 0) pos))
  6874. (goto-char beg)
  6875. (goto-char pos)
  6876. (error "Not in a dynamic block"))))
  6877. (defun org-update-all-dblocks ()
  6878. "Update all dynamic blocks in the buffer.
  6879. This function can be used in a hook."
  6880. (when (org-mode-p)
  6881. (org-map-dblocks 'org-update-dblock)))
  6882. ;;;; Completion
  6883. (defconst org-additional-option-like-keywords
  6884. '("BEGIN_HTML" "BEGIN_LaTeX" "END_HTML" "END_LaTeX"
  6885. "ORGTBL" "HTML:" "LaTeX:" "BEGIN:" "END:" "TBLFM"
  6886. "BEGIN_EXAMPLE" "END_EXAMPLE"))
  6887. (defcustom org-structure-template-alist
  6888. '(
  6889. ("s" "#+begin_src ?\n\n#+end_src")
  6890. ("e" "#+begin_example\n?\n#+end_example")
  6891. ("q" "#+begin_quote\n?\n#+end_quote")
  6892. ("v" "#+begin_verse\n?\n#+end_verse")
  6893. ("l" "#+begin_latex\n?\n#+end_latex")
  6894. ("L" "#+latex: ")
  6895. ("h" "#+begin_html\n?\n#+end_html")
  6896. ("H" "#+html: ")
  6897. ("a" "#+begin_ascii\n?\n#+end_ascii")
  6898. ("A" "#+ascii: ")
  6899. ("i" "#+include: %file ?")
  6900. )
  6901. "Structure completion elements.
  6902. This is a list of abbreviation keys and values. The value gets inserted
  6903. it you type @samp{.} followed by the key and then the completion key,
  6904. usually `M-TAB'. %file will be replaced by a file name after prompting
  6905. for the file uning completion.
  6906. This is an experimental feature, it is undecided if it is going to stay in."
  6907. :group 'org-completion
  6908. :type '(repeat
  6909. (string :tag "Key") (string :tag "Template")))
  6910. (defun org-complete-expand-structure-template (start cell)
  6911. "Expand a structure template."
  6912. (let ((rpl (nth 1 cell)))
  6913. (delete-region start (point))
  6914. (when (string-match "\\`#\\+" rpl)
  6915. (cond
  6916. ((bolp))
  6917. ((not (string-match "\\S-" (buffer-substring (point-at-bol) (point))))
  6918. (delete-region (point-at-bol) (point)))
  6919. (t (newline))))
  6920. (setq start (point))
  6921. (if (string-match "%file" rpl)
  6922. (setq rpl (replace-match
  6923. (concat
  6924. "\""
  6925. (save-match-data
  6926. (abbreviate-file-name (read-file-name "Include file: ")))
  6927. "\"")
  6928. t t rpl)))
  6929. (insert rpl)
  6930. (if (re-search-backward "\\?" start t) (delete-char 1))))
  6931. (defun org-complete (&optional arg)
  6932. "Perform completion on word at point.
  6933. At the beginning of a headline, this completes TODO keywords as given in
  6934. `org-todo-keywords'.
  6935. If the current word is preceded by a backslash, completes the TeX symbols
  6936. that are supported for HTML support.
  6937. If the current word is preceded by \"#+\", completes special words for
  6938. setting file options.
  6939. In the line after \"#+STARTUP:, complete valid keywords.\"
  6940. At all other locations, this simply calls the value of
  6941. `org-completion-fallback-command'."
  6942. (interactive "P")
  6943. (org-without-partial-completion
  6944. (catch 'exit
  6945. (let* ((a nil)
  6946. (end (point))
  6947. (beg1 (save-excursion
  6948. (skip-chars-backward (org-re "[:alnum:]_@"))
  6949. (point)))
  6950. (beg (save-excursion
  6951. (skip-chars-backward "a-zA-Z0-9_:$")
  6952. (point)))
  6953. (confirm (lambda (x) (stringp (car x))))
  6954. (searchhead (equal (char-before beg) ?*))
  6955. (struct
  6956. (when (and (equal (char-before beg1) ?.)
  6957. (setq a (assoc (buffer-substring beg1 (point))
  6958. org-structure-template-alist)))
  6959. (org-complete-expand-structure-template (1- beg1) a)
  6960. (throw 'exit t)))
  6961. (tag (and (equal (char-before beg1) ?:)
  6962. (equal (char-after (point-at-bol)) ?*)))
  6963. (prop (and (equal (char-before beg1) ?:)
  6964. (not (equal (char-after (point-at-bol)) ?*))))
  6965. (texp (equal (char-before beg) ?\\))
  6966. (link (equal (char-before beg) ?\[))
  6967. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  6968. beg)
  6969. "#+"))
  6970. (startup (string-match "^#\\+STARTUP:.*"
  6971. (buffer-substring (point-at-bol) (point))))
  6972. (completion-ignore-case opt)
  6973. (type nil)
  6974. (tbl nil)
  6975. (table (cond
  6976. (opt
  6977. (setq type :opt)
  6978. (require 'org-exp)
  6979. (append
  6980. (mapcar
  6981. (lambda (x)
  6982. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  6983. (cons (match-string 2 x) (match-string 1 x)))
  6984. (org-split-string (org-get-current-options) "\n"))
  6985. (mapcar 'list org-additional-option-like-keywords)))
  6986. (startup
  6987. (setq type :startup)
  6988. org-startup-options)
  6989. (link (append org-link-abbrev-alist-local
  6990. org-link-abbrev-alist))
  6991. (texp
  6992. (setq type :tex)
  6993. org-html-entities)
  6994. ((string-match "\\`\\*+[ \t]+\\'"
  6995. (buffer-substring (point-at-bol) beg))
  6996. (setq type :todo)
  6997. (mapcar 'list org-todo-keywords-1))
  6998. (searchhead
  6999. (setq type :searchhead)
  7000. (save-excursion
  7001. (goto-char (point-min))
  7002. (while (re-search-forward org-todo-line-regexp nil t)
  7003. (push (list
  7004. (org-make-org-heading-search-string
  7005. (match-string 3) t))
  7006. tbl)))
  7007. tbl)
  7008. (tag (setq type :tag beg beg1)
  7009. (or org-tag-alist (org-get-buffer-tags)))
  7010. (prop (setq type :prop beg beg1)
  7011. (mapcar 'list (org-buffer-property-keys nil t t)))
  7012. (t (progn
  7013. (call-interactively org-completion-fallback-command)
  7014. (throw 'exit nil)))))
  7015. (pattern (buffer-substring-no-properties beg end))
  7016. (completion (try-completion pattern table confirm)))
  7017. (cond ((eq completion t)
  7018. (if (not (assoc (upcase pattern) table))
  7019. (message "Already complete")
  7020. (if (and (equal type :opt)
  7021. (not (member (car (assoc (upcase pattern) table))
  7022. org-additional-option-like-keywords)))
  7023. (insert (substring (cdr (assoc (upcase pattern) table))
  7024. (length pattern)))
  7025. (if (memq type '(:tag :prop)) (insert ":")))))
  7026. ((null completion)
  7027. (message "Can't find completion for \"%s\"" pattern)
  7028. (ding))
  7029. ((not (string= pattern completion))
  7030. (delete-region beg end)
  7031. (if (string-match " +$" completion)
  7032. (setq completion (replace-match "" t t completion)))
  7033. (insert completion)
  7034. (if (get-buffer-window "*Completions*")
  7035. (delete-window (get-buffer-window "*Completions*")))
  7036. (if (assoc completion table)
  7037. (if (eq type :todo) (insert " ")
  7038. (if (memq type '(:tag :prop)) (insert ":"))))
  7039. (if (and (equal type :opt) (assoc completion table))
  7040. (message "%s" (substitute-command-keys
  7041. "Press \\[org-complete] again to insert example settings"))))
  7042. (t
  7043. (message "Making completion list...")
  7044. (let ((list (sort (all-completions pattern table confirm)
  7045. 'string<)))
  7046. (with-output-to-temp-buffer "*Completions*"
  7047. (condition-case nil
  7048. ;; Protection needed for XEmacs and emacs 21
  7049. (display-completion-list list pattern)
  7050. (error (display-completion-list list)))))
  7051. (message "Making completion list...%s" "done")))))))
  7052. ;;;; TODO, DEADLINE, Comments
  7053. (defun org-toggle-comment ()
  7054. "Change the COMMENT state of an entry."
  7055. (interactive)
  7056. (save-excursion
  7057. (org-back-to-heading)
  7058. (let (case-fold-search)
  7059. (if (looking-at (concat outline-regexp
  7060. "\\( *\\<" org-comment-string "\\>[ \t]*\\)"))
  7061. (replace-match "" t t nil 1)
  7062. (if (looking-at outline-regexp)
  7063. (progn
  7064. (goto-char (match-end 0))
  7065. (insert org-comment-string " ")))))))
  7066. (defvar org-last-todo-state-is-todo nil
  7067. "This is non-nil when the last TODO state change led to a TODO state.
  7068. If the last change removed the TODO tag or switched to DONE, then
  7069. this is nil.")
  7070. (defvar org-setting-tags nil) ; dynamically skiped
  7071. (defun org-parse-local-options (string var)
  7072. "Parse STRING for startup setting relevant for variable VAR."
  7073. (let ((rtn (symbol-value var))
  7074. e opts)
  7075. (save-match-data
  7076. (if (or (not string) (not (string-match "\\S-" string)))
  7077. rtn
  7078. (setq opts (delq nil (mapcar (lambda (x)
  7079. (setq e (assoc x org-startup-options))
  7080. (if (eq (nth 1 e) var) e nil))
  7081. (org-split-string string "[ \t]+"))))
  7082. (if (not opts)
  7083. rtn
  7084. (setq rtn nil)
  7085. (while (setq e (pop opts))
  7086. (if (not (nth 3 e))
  7087. (setq rtn (nth 2 e))
  7088. (if (not (listp rtn)) (setq rtn nil))
  7089. (push (nth 2 e) rtn)))
  7090. rtn)))))
  7091. (defvar org-blocker-hook nil
  7092. "Hook for functions that are allowed to block a state change.
  7093. Each function gets as its single argument a property list, see
  7094. `org-trigger-hook' for more information about this list.
  7095. If any of the functions in this hook returns nil, the state change
  7096. is blocked.")
  7097. (defvar org-trigger-hook nil
  7098. "Hook for functions that are triggered by a state change.
  7099. Each function gets as its single argument a property list with at least
  7100. the following elements:
  7101. (:type type-of-change :position pos-at-entry-start
  7102. :from old-state :to new-state)
  7103. Depending on the type, more properties may be present.
  7104. This mechanism is currently implemented for:
  7105. TODO state changes
  7106. ------------------
  7107. :type todo-state-change
  7108. :from previous state (keyword as a string), or nil
  7109. :to new state (keyword as a string), or nil")
  7110. (defun org-todo (&optional arg)
  7111. "Change the TODO state of an item.
  7112. The state of an item is given by a keyword at the start of the heading,
  7113. like
  7114. *** TODO Write paper
  7115. *** DONE Call mom
  7116. The different keywords are specified in the variable `org-todo-keywords'.
  7117. By default the available states are \"TODO\" and \"DONE\".
  7118. So for this example: when the item starts with TODO, it is changed to DONE.
  7119. When it starts with DONE, the DONE is removed. And when neither TODO nor
  7120. DONE are present, add TODO at the beginning of the heading.
  7121. With C-u prefix arg, use completion to determine the new state.
  7122. With numeric prefix arg, switch to that state.
  7123. For calling through lisp, arg is also interpreted in the following way:
  7124. 'none -> empty state
  7125. \"\"(empty string) -> switch to empty state
  7126. 'done -> switch to DONE
  7127. 'nextset -> switch to the next set of keywords
  7128. 'previousset -> switch to the previous set of keywords
  7129. \"WAITING\" -> switch to the specified keyword, but only if it
  7130. really is a member of `org-todo-keywords'."
  7131. (interactive "P")
  7132. (save-excursion
  7133. (catch 'exit
  7134. (org-back-to-heading)
  7135. (if (looking-at outline-regexp) (goto-char (1- (match-end 0))))
  7136. (or (looking-at (concat " +" org-todo-regexp " *"))
  7137. (looking-at " *"))
  7138. (let* ((match-data (match-data))
  7139. (startpos (point-at-bol))
  7140. (logging (save-match-data (org-entry-get nil "LOGGING" t)))
  7141. (org-log-done org-log-done)
  7142. (org-log-repeat org-log-repeat)
  7143. (org-todo-log-states org-todo-log-states)
  7144. (this (match-string 1))
  7145. (hl-pos (match-beginning 0))
  7146. (head (org-get-todo-sequence-head this))
  7147. (ass (assoc head org-todo-kwd-alist))
  7148. (interpret (nth 1 ass))
  7149. (done-word (nth 3 ass))
  7150. (final-done-word (nth 4 ass))
  7151. (last-state (or this ""))
  7152. (completion-ignore-case t)
  7153. (member (member this org-todo-keywords-1))
  7154. (tail (cdr member))
  7155. (state (cond
  7156. ((and org-todo-key-trigger
  7157. (or (and (equal arg '(4)) (eq org-use-fast-todo-selection 'prefix))
  7158. (and (not arg) org-use-fast-todo-selection
  7159. (not (eq org-use-fast-todo-selection 'prefix)))))
  7160. ;; Use fast selection
  7161. (org-fast-todo-selection))
  7162. ((and (equal arg '(4))
  7163. (or (not org-use-fast-todo-selection)
  7164. (not org-todo-key-trigger)))
  7165. ;; Read a state with completion
  7166. (completing-read "State: " (mapcar (lambda(x) (list x))
  7167. org-todo-keywords-1)
  7168. nil t))
  7169. ((eq arg 'right)
  7170. (if this
  7171. (if tail (car tail) nil)
  7172. (car org-todo-keywords-1)))
  7173. ((eq arg 'left)
  7174. (if (equal member org-todo-keywords-1)
  7175. nil
  7176. (if this
  7177. (nth (- (length org-todo-keywords-1) (length tail) 2)
  7178. org-todo-keywords-1)
  7179. (org-last org-todo-keywords-1))))
  7180. ((and (eq org-use-fast-todo-selection t) (equal arg '(4))
  7181. (setq arg nil))) ; hack to fall back to cycling
  7182. (arg
  7183. ;; user or caller requests a specific state
  7184. (cond
  7185. ((equal arg "") nil)
  7186. ((eq arg 'none) nil)
  7187. ((eq arg 'done) (or done-word (car org-done-keywords)))
  7188. ((eq arg 'nextset)
  7189. (or (car (cdr (member head org-todo-heads)))
  7190. (car org-todo-heads)))
  7191. ((eq arg 'previousset)
  7192. (let ((org-todo-heads (reverse org-todo-heads)))
  7193. (or (car (cdr (member head org-todo-heads)))
  7194. (car org-todo-heads))))
  7195. ((car (member arg org-todo-keywords-1)))
  7196. ((nth (1- (prefix-numeric-value arg))
  7197. org-todo-keywords-1))))
  7198. ((null member) (or head (car org-todo-keywords-1)))
  7199. ((equal this final-done-word) nil) ;; -> make empty
  7200. ((null tail) nil) ;; -> first entry
  7201. ((eq interpret 'sequence)
  7202. (car tail))
  7203. ((memq interpret '(type priority))
  7204. (if (eq this-command last-command)
  7205. (car tail)
  7206. (if (> (length tail) 0)
  7207. (or done-word (car org-done-keywords))
  7208. nil)))
  7209. (t nil)))
  7210. (next (if state (concat " " state " ") " "))
  7211. (change-plist (list :type 'todo-state-change :from this :to state
  7212. :position startpos))
  7213. dolog now-done-p)
  7214. (when org-blocker-hook
  7215. (unless (save-excursion
  7216. (save-match-data
  7217. (run-hook-with-args-until-failure
  7218. 'org-blocker-hook change-plist)))
  7219. (if (interactive-p)
  7220. (error "TODO state change from %s to %s blocked" this state)
  7221. ;; fail silently
  7222. (message "TODO state change from %s to %s blocked" this state)
  7223. (throw 'exit nil))))
  7224. (store-match-data match-data)
  7225. (replace-match next t t)
  7226. (unless (pos-visible-in-window-p hl-pos)
  7227. (message "TODO state changed to %s" (org-trim next)))
  7228. (unless head
  7229. (setq head (org-get-todo-sequence-head state)
  7230. ass (assoc head org-todo-kwd-alist)
  7231. interpret (nth 1 ass)
  7232. done-word (nth 3 ass)
  7233. final-done-word (nth 4 ass)))
  7234. (when (memq arg '(nextset previousset))
  7235. (message "Keyword-Set %d/%d: %s"
  7236. (- (length org-todo-sets) -1
  7237. (length (memq (assoc state org-todo-sets) org-todo-sets)))
  7238. (length org-todo-sets)
  7239. (mapconcat 'identity (assoc state org-todo-sets) " ")))
  7240. (setq org-last-todo-state-is-todo
  7241. (not (member state org-done-keywords)))
  7242. (setq now-done-p (and (member state org-done-keywords)
  7243. (not (member this org-done-keywords))))
  7244. (and logging (org-local-logging logging))
  7245. (when (and (or org-todo-log-states org-log-done)
  7246. (not (memq arg '(nextset previousset))))
  7247. ;; we need to look at recording a time and note
  7248. (setq dolog (or (nth 1 (assoc state org-todo-log-states))
  7249. (nth 2 (assoc this org-todo-log-states))))
  7250. (when (and state
  7251. (member state org-not-done-keywords)
  7252. (not (member this org-not-done-keywords)))
  7253. ;; This is now a todo state and was not one before
  7254. ;; If there was a CLOSED time stamp, get rid of it.
  7255. (org-add-planning-info nil nil 'closed))
  7256. (when (and now-done-p org-log-done)
  7257. ;; It is now done, and it was not done before
  7258. (org-add-planning-info 'closed (org-current-time))
  7259. (if (and (not dolog) (eq 'note org-log-done))
  7260. (org-add-log-setup 'done state 'findpos 'note)))
  7261. (when (and state dolog)
  7262. ;; This is a non-nil state, and we need to log it
  7263. (org-add-log-setup 'state state 'findpos dolog)))
  7264. ;; Fixup tag positioning
  7265. (and org-auto-align-tags (not org-setting-tags) (org-set-tags nil t))
  7266. (run-hooks 'org-after-todo-state-change-hook)
  7267. (if (and arg (not (member state org-done-keywords)))
  7268. (setq head (org-get-todo-sequence-head state)))
  7269. (put-text-property (point-at-bol) (point-at-eol) 'org-todo-head head)
  7270. ;; Do we need to trigger a repeat?
  7271. (when now-done-p (org-auto-repeat-maybe state))
  7272. ;; Fixup cursor location if close to the keyword
  7273. (if (and (outline-on-heading-p)
  7274. (not (bolp))
  7275. (save-excursion (beginning-of-line 1)
  7276. (looking-at org-todo-line-regexp))
  7277. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  7278. (progn
  7279. (goto-char (or (match-end 2) (match-end 1)))
  7280. (just-one-space)))
  7281. (when org-trigger-hook
  7282. (save-excursion
  7283. (run-hook-with-args 'org-trigger-hook change-plist)))))))
  7284. (defun org-local-logging (value)
  7285. "Get logging settings from a property VALUE."
  7286. (let* (words w a)
  7287. ;; directly set the variables, they are already local.
  7288. (setq org-log-done nil
  7289. org-log-repeat nil
  7290. org-todo-log-states nil)
  7291. (setq words (org-split-string value))
  7292. (while (setq w (pop words))
  7293. (cond
  7294. ((setq a (assoc w org-startup-options))
  7295. (and (member (nth 1 a) '(org-log-done org-log-repeat))
  7296. (set (nth 1 a) (nth 2 a))))
  7297. ((setq a (org-extract-log-state-settings w))
  7298. (and (member (car a) org-todo-keywords-1)
  7299. (push a org-todo-log-states)))))))
  7300. (defun org-get-todo-sequence-head (kwd)
  7301. "Return the head of the TODO sequence to which KWD belongs.
  7302. If KWD is not set, check if there is a text property remembering the
  7303. right sequence."
  7304. (let (p)
  7305. (cond
  7306. ((not kwd)
  7307. (or (get-text-property (point-at-bol) 'org-todo-head)
  7308. (progn
  7309. (setq p (next-single-property-change (point-at-bol) 'org-todo-head
  7310. nil (point-at-eol)))
  7311. (get-text-property p 'org-todo-head))))
  7312. ((not (member kwd org-todo-keywords-1))
  7313. (car org-todo-keywords-1))
  7314. (t (nth 2 (assoc kwd org-todo-kwd-alist))))))
  7315. (defun org-fast-todo-selection ()
  7316. "Fast TODO keyword selection with single keys.
  7317. Returns the new TODO keyword, or nil if no state change should occur."
  7318. (let* ((fulltable org-todo-key-alist)
  7319. (done-keywords org-done-keywords) ;; needed for the faces.
  7320. (maxlen (apply 'max (mapcar
  7321. (lambda (x)
  7322. (if (stringp (car x)) (string-width (car x)) 0))
  7323. fulltable)))
  7324. (expert nil)
  7325. (fwidth (+ maxlen 3 1 3))
  7326. (ncol (/ (- (window-width) 4) fwidth))
  7327. tg cnt e c tbl
  7328. groups ingroup)
  7329. (save-window-excursion
  7330. (if expert
  7331. (set-buffer (get-buffer-create " *Org todo*"))
  7332. (org-switch-to-buffer-other-window (get-buffer-create " *Org todo*")))
  7333. (erase-buffer)
  7334. (org-set-local 'org-done-keywords done-keywords)
  7335. (setq tbl fulltable cnt 0)
  7336. (while (setq e (pop tbl))
  7337. (cond
  7338. ((equal e '(:startgroup))
  7339. (push '() groups) (setq ingroup t)
  7340. (when (not (= cnt 0))
  7341. (setq cnt 0)
  7342. (insert "\n"))
  7343. (insert "{ "))
  7344. ((equal e '(:endgroup))
  7345. (setq ingroup nil cnt 0)
  7346. (insert "}\n"))
  7347. (t
  7348. (setq tg (car e) c (cdr e))
  7349. (if ingroup (push tg (car groups)))
  7350. (setq tg (org-add-props tg nil 'face
  7351. (org-get-todo-face tg)))
  7352. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7353. (insert "[" c "] " tg (make-string
  7354. (- fwidth 4 (length tg)) ?\ ))
  7355. (when (= (setq cnt (1+ cnt)) ncol)
  7356. (insert "\n")
  7357. (if ingroup (insert " "))
  7358. (setq cnt 0)))))
  7359. (insert "\n")
  7360. (goto-char (point-min))
  7361. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  7362. (fit-window-to-buffer))
  7363. (message "[a-z..]:Set [SPC]:clear")
  7364. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  7365. (cond
  7366. ((or (= c ?\C-g)
  7367. (and (= c ?q) (not (rassoc c fulltable))))
  7368. (setq quit-flag t))
  7369. ((= c ?\ ) nil)
  7370. ((setq e (rassoc c fulltable) tg (car e))
  7371. tg)
  7372. (t (setq quit-flag t))))))
  7373. (defun org-entry-is-todo-p ()
  7374. (member (org-get-todo-state) org-not-done-keywords))
  7375. (defun org-entry-is-done-p ()
  7376. (member (org-get-todo-state) org-done-keywords))
  7377. (defun org-get-todo-state ()
  7378. (save-excursion
  7379. (org-back-to-heading t)
  7380. (and (looking-at org-todo-line-regexp)
  7381. (match-end 2)
  7382. (match-string 2))))
  7383. (defun org-at-date-range-p (&optional inactive-ok)
  7384. "Is the cursor inside a date range?"
  7385. (interactive)
  7386. (save-excursion
  7387. (catch 'exit
  7388. (let ((pos (point)))
  7389. (skip-chars-backward "^[<\r\n")
  7390. (skip-chars-backward "<[")
  7391. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7392. (>= (match-end 0) pos)
  7393. (throw 'exit t))
  7394. (skip-chars-backward "^<[\r\n")
  7395. (skip-chars-backward "<[")
  7396. (and (looking-at (if inactive-ok org-tr-regexp-both org-tr-regexp))
  7397. (>= (match-end 0) pos)
  7398. (throw 'exit t)))
  7399. nil)))
  7400. (defun org-get-repeat ()
  7401. "Check if tere is a deadline/schedule with repeater in this entry."
  7402. (save-match-data
  7403. (save-excursion
  7404. (org-back-to-heading t)
  7405. (if (re-search-forward
  7406. org-repeat-re (save-excursion (outline-next-heading) (point)) t)
  7407. (match-string 1)))))
  7408. (defvar org-last-changed-timestamp)
  7409. (defvar org-log-post-message)
  7410. (defvar org-log-note-purpose)
  7411. (defvar org-log-note-how)
  7412. (defun org-auto-repeat-maybe (done-word)
  7413. "Check if the current headline contains a repeated deadline/schedule.
  7414. If yes, set TODO state back to what it was and change the base date
  7415. of repeating deadline/scheduled time stamps to new date.
  7416. This function is run automatically after each state change to a DONE state."
  7417. ;; last-state is dynamically scoped into this function
  7418. (let* ((repeat (org-get-repeat))
  7419. (aa (assoc last-state org-todo-kwd-alist))
  7420. (interpret (nth 1 aa))
  7421. (head (nth 2 aa))
  7422. (whata '(("d" . day) ("m" . month) ("y" . year)))
  7423. (msg "Entry repeats: ")
  7424. (org-log-done nil)
  7425. (org-todo-log-states nil)
  7426. (nshiftmax 10) (nshift 0)
  7427. re type n what ts mb0 time)
  7428. (when repeat
  7429. (if (eq org-log-repeat t) (setq org-log-repeat 'state))
  7430. (org-todo (if (eq interpret 'type) last-state head))
  7431. (when org-log-repeat
  7432. (if (or (memq 'org-add-log-note (default-value 'post-command-hook))
  7433. (memq 'org-add-log-note post-command-hook))
  7434. ;; OK, we are already setup for some record
  7435. (if (eq org-log-repeat 'note)
  7436. ;; make sure we take a note, not only a time stamp
  7437. (setq org-log-note-how 'note))
  7438. ;; Set up for taking a record
  7439. (org-add-log-setup 'state (or done-word (car org-done-keywords))
  7440. 'findpos org-log-repeat)))
  7441. (org-back-to-heading t)
  7442. (org-add-planning-info nil nil 'closed)
  7443. (setq re (concat "\\(" org-scheduled-time-regexp "\\)\\|\\("
  7444. org-deadline-time-regexp "\\)\\|\\("
  7445. org-ts-regexp "\\)"))
  7446. (while (re-search-forward
  7447. re (save-excursion (outline-next-heading) (point)) t)
  7448. (setq type (if (match-end 1) org-scheduled-string
  7449. (if (match-end 3) org-deadline-string "Plain:"))
  7450. ts (match-string (if (match-end 2) 2 (if (match-end 4) 4 0)))
  7451. mb0 (match-beginning 0))
  7452. (when (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts)
  7453. (setq n (string-to-number (match-string 2 ts))
  7454. what (match-string 3 ts))
  7455. (if (equal what "w") (setq n (* n 7) what "d"))
  7456. ;; Preparation, see if we need to modify the start date for the change
  7457. (when (match-end 1)
  7458. (setq time (save-match-data (org-time-string-to-time ts)))
  7459. (cond
  7460. ((equal (match-string 1 ts) ".")
  7461. ;; Shift starting date to today
  7462. (org-timestamp-change
  7463. (- (time-to-days (current-time)) (time-to-days time))
  7464. 'day))
  7465. ((equal (match-string 1 ts) "+")
  7466. (while (or (= nshift 0)
  7467. (<= (time-to-days time) (time-to-days (current-time))))
  7468. (when (= (incf nshift) nshiftmax)
  7469. (or (y-or-n-p (message "%d repeater intervals were not enough to shift date past today. Continue? " nshift))
  7470. (error "Abort")))
  7471. (org-timestamp-change n (cdr (assoc what whata)))
  7472. (org-at-timestamp-p t)
  7473. (setq ts (match-string 1))
  7474. (setq time (save-match-data (org-time-string-to-time ts))))
  7475. (org-timestamp-change (- n) (cdr (assoc what whata)))
  7476. ;; rematch, so that we have everything in place for the real shift
  7477. (org-at-timestamp-p t)
  7478. (setq ts (match-string 1))
  7479. (string-match "\\([.+]\\)?\\(\\+[0-9]+\\)\\([dwmy]\\)" ts))))
  7480. (org-timestamp-change n (cdr (assoc what whata)))
  7481. (setq msg (concat msg type org-last-changed-timestamp " "))))
  7482. (setq org-log-post-message msg)
  7483. (message "%s" msg))))
  7484. (defun org-show-todo-tree (arg)
  7485. "Make a compact tree which shows all headlines marked with TODO.
  7486. The tree will show the lines where the regexp matches, and all higher
  7487. headlines above the match.
  7488. With a \\[universal-argument] prefix, also show the DONE entries.
  7489. With a numeric prefix N, construct a sparse tree for the Nth element
  7490. of `org-todo-keywords-1'."
  7491. (interactive "P")
  7492. (let ((case-fold-search nil)
  7493. (kwd-re
  7494. (cond ((null arg) org-not-done-regexp)
  7495. ((equal arg '(4))
  7496. (let ((kwd (completing-read "Keyword (or KWD1|KWD2|...): "
  7497. (mapcar 'list org-todo-keywords-1))))
  7498. (concat "\\("
  7499. (mapconcat 'identity (org-split-string kwd "|") "\\|")
  7500. "\\)\\>")))
  7501. ((<= (prefix-numeric-value arg) (length org-todo-keywords-1))
  7502. (regexp-quote (nth (1- (prefix-numeric-value arg))
  7503. org-todo-keywords-1)))
  7504. (t (error "Invalid prefix argument: %s" arg)))))
  7505. (message "%d TODO entries found"
  7506. (org-occur (concat "^" outline-regexp " *" kwd-re )))))
  7507. (defun org-deadline (&optional remove)
  7508. "Insert the \"DEADLINE:\" string with a timestamp to make a deadline.
  7509. With argument REMOVE, remove any deadline from the item."
  7510. (interactive "P")
  7511. (if remove
  7512. (progn
  7513. (org-remove-timestamp-with-keyword org-deadline-string)
  7514. (message "Item no longer has a deadline."))
  7515. (org-add-planning-info 'deadline nil 'closed)))
  7516. (defun org-schedule (&optional remove)
  7517. "Insert the SCHEDULED: string with a timestamp to schedule a TODO item.
  7518. With argument REMOVE, remove any scheduling date from the item."
  7519. (interactive "P")
  7520. (if remove
  7521. (progn
  7522. (org-remove-timestamp-with-keyword org-scheduled-string)
  7523. (message "Item is no longer scheduled."))
  7524. (org-add-planning-info 'scheduled nil 'closed)))
  7525. (defun org-remove-timestamp-with-keyword (keyword)
  7526. "Remove all time stamps with KEYWORD in the current entry."
  7527. (let ((re (concat "\\<" (regexp-quote keyword) " +<[^>\n]+>[ \t]*"))
  7528. beg)
  7529. (save-excursion
  7530. (org-back-to-heading t)
  7531. (setq beg (point))
  7532. (org-end-of-subtree t t)
  7533. (while (re-search-backward re beg t)
  7534. (replace-match "")
  7535. (unless (string-match "\\S-" (buffer-substring (point-at-bol) (point)))
  7536. (delete-region (point-at-bol) (min (1+ (point)) (point-max))))))))
  7537. (defun org-add-planning-info (what &optional time &rest remove)
  7538. "Insert new timestamp with keyword in the line directly after the headline.
  7539. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  7540. If non is given, the user is prompted for a date.
  7541. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  7542. be removed."
  7543. (interactive)
  7544. (let (org-time-was-given org-end-time-was-given ts
  7545. end default-time default-input)
  7546. (when (and (not time) (memq what '(scheduled deadline)))
  7547. ;; Try to get a default date/time from existing timestamp
  7548. (save-excursion
  7549. (org-back-to-heading t)
  7550. (setq end (save-excursion (outline-next-heading) (point)))
  7551. (when (re-search-forward (if (eq what 'scheduled)
  7552. org-scheduled-time-regexp
  7553. org-deadline-time-regexp)
  7554. end t)
  7555. (setq ts (match-string 1)
  7556. default-time
  7557. (apply 'encode-time (org-parse-time-string ts))
  7558. default-input (and ts (org-get-compact-tod ts))))))
  7559. (when what
  7560. ;; If necessary, get the time from the user
  7561. (setq time (or time (org-read-date nil 'to-time nil nil
  7562. default-time default-input))))
  7563. (when (and org-insert-labeled-timestamps-at-point
  7564. (member what '(scheduled deadline)))
  7565. (insert
  7566. (if (eq what 'scheduled) org-scheduled-string org-deadline-string) " ")
  7567. (org-insert-time-stamp time org-time-was-given
  7568. nil nil nil (list org-end-time-was-given))
  7569. (setq what nil))
  7570. (save-excursion
  7571. (save-restriction
  7572. (let (col list elt ts buffer-invisibility-spec)
  7573. (org-back-to-heading t)
  7574. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  7575. (goto-char (match-end 1))
  7576. (setq col (current-column))
  7577. (goto-char (match-end 0))
  7578. (if (eobp) (insert "\n") (forward-char 1))
  7579. (if (and (not (looking-at outline-regexp))
  7580. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  7581. "[^\r\n]*"))
  7582. (not (equal (match-string 1) org-clock-string)))
  7583. (narrow-to-region (match-beginning 0) (match-end 0))
  7584. (insert-before-markers "\n")
  7585. (backward-char 1)
  7586. (narrow-to-region (point) (point))
  7587. (org-indent-to-column col))
  7588. ;; Check if we have to remove something.
  7589. (setq list (cons what remove))
  7590. (while list
  7591. (setq elt (pop list))
  7592. (goto-char (point-min))
  7593. (when (or (and (eq elt 'scheduled)
  7594. (re-search-forward org-scheduled-time-regexp nil t))
  7595. (and (eq elt 'deadline)
  7596. (re-search-forward org-deadline-time-regexp nil t))
  7597. (and (eq elt 'closed)
  7598. (re-search-forward org-closed-time-regexp nil t)))
  7599. (replace-match "")
  7600. (if (looking-at "--+<[^>]+>") (replace-match ""))
  7601. (if (looking-at " +") (replace-match ""))))
  7602. (goto-char (point-max))
  7603. (when what
  7604. (insert
  7605. (if (not (equal (char-before) ?\ )) " " "")
  7606. (cond ((eq what 'scheduled) org-scheduled-string)
  7607. ((eq what 'deadline) org-deadline-string)
  7608. ((eq what 'closed) org-closed-string))
  7609. " ")
  7610. (setq ts (org-insert-time-stamp
  7611. time
  7612. (or org-time-was-given
  7613. (and (eq what 'closed) org-log-done-with-time))
  7614. (eq what 'closed)
  7615. nil nil (list org-end-time-was-given)))
  7616. (end-of-line 1))
  7617. (goto-char (point-min))
  7618. (widen)
  7619. (if (and (looking-at "[ \t]+\n")
  7620. (equal (char-before) ?\n))
  7621. (backward-delete-char 1))
  7622. ts)))))
  7623. (defvar org-log-note-marker (make-marker))
  7624. (defvar org-log-note-purpose nil)
  7625. (defvar org-log-note-state nil)
  7626. (defvar org-log-note-how nil)
  7627. (defvar org-log-note-window-configuration nil)
  7628. (defvar org-log-note-return-to (make-marker))
  7629. (defvar org-log-post-message nil
  7630. "Message to be displayed after a log note has been stored.
  7631. The auto-repeater uses this.")
  7632. (defun org-add-note ()
  7633. "Add a note to the current entry.
  7634. This is done in the same way as adding a state change note."
  7635. (interactive)
  7636. (org-add-log-setup 'note nil t nil))
  7637. (defun org-add-log-setup (&optional purpose state findpos how)
  7638. "Set up the post command hook to take a note.
  7639. If this is about to TODO state change, the new state is expected in STATE.
  7640. When FINDPOS is non-nil, find the correct position for the note in
  7641. the current entry. If not, assume that it can be inserted at point."
  7642. (save-excursion
  7643. (when findpos
  7644. (org-back-to-heading t)
  7645. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"
  7646. "\\(\n[^\r\n]*?" org-keyword-time-not-clock-regexp
  7647. "[^\r\n]*\\)?"))
  7648. (goto-char (match-end 0))
  7649. (unless org-log-states-order-reversed
  7650. (and (= (char-after) ?\n) (forward-char 1))
  7651. (org-skip-over-state-notes)
  7652. (skip-chars-backward " \t\n\r")))
  7653. (move-marker org-log-note-marker (point))
  7654. (setq org-log-note-purpose purpose
  7655. org-log-note-state state
  7656. org-log-note-how how)
  7657. (add-hook 'post-command-hook 'org-add-log-note 'append)))
  7658. (defun org-skip-over-state-notes ()
  7659. "Skip past the list of State notes in an entry."
  7660. (if (looking-at "\n[ \t]*- State") (forward-char 1))
  7661. (while (looking-at "[ \t]*- State")
  7662. (condition-case nil
  7663. (org-next-item)
  7664. (error (org-end-of-item)))))
  7665. (defun org-add-log-note (&optional purpose)
  7666. "Pop up a window for taking a note, and add this note later at point."
  7667. (remove-hook 'post-command-hook 'org-add-log-note)
  7668. (setq org-log-note-window-configuration (current-window-configuration))
  7669. (delete-other-windows)
  7670. (move-marker org-log-note-return-to (point))
  7671. (switch-to-buffer (marker-buffer org-log-note-marker))
  7672. (goto-char org-log-note-marker)
  7673. (org-switch-to-buffer-other-window "*Org Note*")
  7674. (erase-buffer)
  7675. (if (memq org-log-note-how '(time state))
  7676. (org-store-log-note)
  7677. (let ((org-inhibit-startup t)) (org-mode))
  7678. (insert (format "# Insert note for %s.
  7679. # Finish with C-c C-c, or cancel with C-c C-k.\n\n"
  7680. (cond
  7681. ((eq org-log-note-purpose 'clock-out) "stopped clock")
  7682. ((eq org-log-note-purpose 'done) "closed todo item")
  7683. ((eq org-log-note-purpose 'state)
  7684. (format "state change to \"%s\"" org-log-note-state))
  7685. ((eq org-log-note-purpose 'note)
  7686. "this entry")
  7687. (t (error "This should not happen")))))
  7688. (org-set-local 'org-finish-function 'org-store-log-note)))
  7689. (defvar org-note-abort nil) ; dynamically scoped
  7690. (defun org-store-log-note ()
  7691. "Finish taking a log note, and insert it to where it belongs."
  7692. (let ((txt (buffer-string))
  7693. (note (cdr (assq org-log-note-purpose org-log-note-headings)))
  7694. lines ind)
  7695. (kill-buffer (current-buffer))
  7696. (while (string-match "\\`#.*\n[ \t\n]*" txt)
  7697. (setq txt (replace-match "" t t txt)))
  7698. (if (string-match "\\s-+\\'" txt)
  7699. (setq txt (replace-match "" t t txt)))
  7700. (setq lines (org-split-string txt "\n"))
  7701. (when (and note (string-match "\\S-" note))
  7702. (setq note
  7703. (org-replace-escapes
  7704. note
  7705. (list (cons "%u" (user-login-name))
  7706. (cons "%U" user-full-name)
  7707. (cons "%t" (format-time-string
  7708. (org-time-stamp-format 'long 'inactive)
  7709. (current-time)))
  7710. (cons "%s" (if org-log-note-state
  7711. (concat "\"" org-log-note-state "\"")
  7712. "")))))
  7713. (if lines (setq note (concat note " \\\\")))
  7714. (push note lines))
  7715. (when (or current-prefix-arg org-note-abort) (setq lines nil))
  7716. (when lines
  7717. (save-excursion
  7718. (set-buffer (marker-buffer org-log-note-marker))
  7719. (save-excursion
  7720. (goto-char org-log-note-marker)
  7721. (move-marker org-log-note-marker nil)
  7722. (end-of-line 1)
  7723. (if (not (bolp)) (let ((inhibit-read-only t)) (insert "\n")))
  7724. (indent-relative nil)
  7725. (insert "- " (pop lines))
  7726. (org-indent-line-function)
  7727. (beginning-of-line 1)
  7728. (looking-at "[ \t]*")
  7729. (setq ind (concat (match-string 0) " "))
  7730. (end-of-line 1)
  7731. (while lines (insert "\n" ind (pop lines)))))))
  7732. (set-window-configuration org-log-note-window-configuration)
  7733. (with-current-buffer (marker-buffer org-log-note-return-to)
  7734. (goto-char org-log-note-return-to))
  7735. (move-marker org-log-note-return-to nil)
  7736. (and org-log-post-message (message "%s" org-log-post-message)))
  7737. (defun org-sparse-tree (&optional arg)
  7738. "Create a sparse tree, prompt for the details.
  7739. This command can create sparse trees. You first need to select the type
  7740. of match used to create the tree:
  7741. t Show entries with a specific TODO keyword.
  7742. T Show entries selected by a tags match.
  7743. p Enter a property name and its value (both with completion on existing
  7744. names/values) and show entries with that property.
  7745. r Show entries matching a regular expression
  7746. d Show deadlines due within `org-deadline-warning-days'."
  7747. (interactive "P")
  7748. (let (ans kwd value)
  7749. (message "Sparse tree: [/]regexp [t]odo-kwd [T]ag [p]roperty [d]eadlines [b]efore-date")
  7750. (setq ans (read-char-exclusive))
  7751. (cond
  7752. ((equal ans ?d)
  7753. (call-interactively 'org-check-deadlines))
  7754. ((equal ans ?b)
  7755. (call-interactively 'org-check-before-date))
  7756. ((equal ans ?t)
  7757. (org-show-todo-tree '(4)))
  7758. ((equal ans ?T)
  7759. (call-interactively 'org-tags-sparse-tree))
  7760. ((member ans '(?p ?P))
  7761. (setq kwd (completing-read "Property: "
  7762. (mapcar 'list (org-buffer-property-keys))))
  7763. (setq value (completing-read "Value: "
  7764. (mapcar 'list (org-property-values kwd))))
  7765. (unless (string-match "\\`{.*}\\'" value)
  7766. (setq value (concat "\"" value "\"")))
  7767. (org-tags-sparse-tree arg (concat kwd "=" value)))
  7768. ((member ans '(?r ?R ?/))
  7769. (call-interactively 'org-occur))
  7770. (t (error "No such sparse tree command \"%c\"" ans)))))
  7771. (defvar org-occur-highlights nil
  7772. "List of overlays used for occur matches.")
  7773. (make-variable-buffer-local 'org-occur-highlights)
  7774. (defvar org-occur-parameters nil
  7775. "Parameters of the active org-occur calls.
  7776. This is a list, each call to org-occur pushes as cons cell,
  7777. containing the regular expression and the callback, onto the list.
  7778. The list can contain several entries if `org-occur' has been called
  7779. several time with the KEEP-PREVIOUS argument. Otherwise, this list
  7780. will only contain one set of parameters. When the highlights are
  7781. removed (for example with `C-c C-c', or with the next edit (depending
  7782. on `org-remove-highlights-with-change'), this variable is emptied
  7783. as well.")
  7784. (make-variable-buffer-local 'org-occur-parameters)
  7785. (defun org-occur (regexp &optional keep-previous callback)
  7786. "Make a compact tree which shows all matches of REGEXP.
  7787. The tree will show the lines where the regexp matches, and all higher
  7788. headlines above the match. It will also show the heading after the match,
  7789. to make sure editing the matching entry is easy.
  7790. If KEEP-PREVIOUS is non-nil, highlighting and exposing done by a previous
  7791. call to `org-occur' will be kept, to allow stacking of calls to this
  7792. command.
  7793. If CALLBACK is non-nil, it is a function which is called to confirm
  7794. that the match should indeed be shown."
  7795. (interactive "sRegexp: \nP")
  7796. (unless keep-previous
  7797. (org-remove-occur-highlights nil nil t))
  7798. (push (cons regexp callback) org-occur-parameters)
  7799. (let ((cnt 0))
  7800. (save-excursion
  7801. (goto-char (point-min))
  7802. (if (or (not keep-previous) ; do not want to keep
  7803. (not org-occur-highlights)) ; no previous matches
  7804. ;; hide everything
  7805. (org-overview))
  7806. (while (re-search-forward regexp nil t)
  7807. (when (or (not callback)
  7808. (save-match-data (funcall callback)))
  7809. (setq cnt (1+ cnt))
  7810. (when org-highlight-sparse-tree-matches
  7811. (org-highlight-new-match (match-beginning 0) (match-end 0)))
  7812. (org-show-context 'occur-tree))))
  7813. (when org-remove-highlights-with-change
  7814. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  7815. nil 'local))
  7816. (unless org-sparse-tree-open-archived-trees
  7817. (org-hide-archived-subtrees (point-min) (point-max)))
  7818. (run-hooks 'org-occur-hook)
  7819. (if (interactive-p)
  7820. (message "%d match(es) for regexp %s" cnt regexp))
  7821. cnt))
  7822. (defun org-show-context (&optional key)
  7823. "Make sure point and context and visible.
  7824. How much context is shown depends upon the variables
  7825. `org-show-hierarchy-above', `org-show-following-heading'. and
  7826. `org-show-siblings'."
  7827. (let ((heading-p (org-on-heading-p t))
  7828. (hierarchy-p (org-get-alist-option org-show-hierarchy-above key))
  7829. (following-p (org-get-alist-option org-show-following-heading key))
  7830. (entry-p (org-get-alist-option org-show-entry-below key))
  7831. (siblings-p (org-get-alist-option org-show-siblings key)))
  7832. (catch 'exit
  7833. ;; Show heading or entry text
  7834. (if (and heading-p (not entry-p))
  7835. (org-flag-heading nil) ; only show the heading
  7836. (and (or entry-p (org-invisible-p) (org-invisible-p2))
  7837. (org-show-hidden-entry))) ; show entire entry
  7838. (when following-p
  7839. ;; Show next sibling, or heading below text
  7840. (save-excursion
  7841. (and (if heading-p (org-goto-sibling) (outline-next-heading))
  7842. (org-flag-heading nil))))
  7843. (when siblings-p (org-show-siblings))
  7844. (when hierarchy-p
  7845. ;; show all higher headings, possibly with siblings
  7846. (save-excursion
  7847. (while (and (condition-case nil
  7848. (progn (org-up-heading-all 1) t)
  7849. (error nil))
  7850. (not (bobp)))
  7851. (org-flag-heading nil)
  7852. (when siblings-p (org-show-siblings))))))))
  7853. (defun org-reveal (&optional siblings)
  7854. "Show current entry, hierarchy above it, and the following headline.
  7855. This can be used to show a consistent set of context around locations
  7856. exposed with `org-show-hierarchy-above' or `org-show-following-heading'
  7857. not t for the search context.
  7858. With optional argument SIBLINGS, on each level of the hierarchy all
  7859. siblings are shown. This repairs the tree structure to what it would
  7860. look like when opened with hierarchical calls to `org-cycle'."
  7861. (interactive "P")
  7862. (let ((org-show-hierarchy-above t)
  7863. (org-show-following-heading t)
  7864. (org-show-siblings (if siblings t org-show-siblings)))
  7865. (org-show-context nil)))
  7866. (defun org-highlight-new-match (beg end)
  7867. "Highlight from BEG to END and mark the highlight is an occur headline."
  7868. (let ((ov (org-make-overlay beg end)))
  7869. (org-overlay-put ov 'face 'secondary-selection)
  7870. (push ov org-occur-highlights)))
  7871. (defun org-remove-occur-highlights (&optional beg end noremove)
  7872. "Remove the occur highlights from the buffer.
  7873. BEG and END are ignored. If NOREMOVE is nil, remove this function
  7874. from the `before-change-functions' in the current buffer."
  7875. (interactive)
  7876. (unless org-inhibit-highlight-removal
  7877. (mapc 'org-delete-overlay org-occur-highlights)
  7878. (setq org-occur-highlights nil)
  7879. (setq org-occur-parameters nil)
  7880. (unless noremove
  7881. (remove-hook 'before-change-functions
  7882. 'org-remove-occur-highlights 'local))))
  7883. ;;;; Priorities
  7884. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z0-9]\\)\\] ?\\)"
  7885. "Regular expression matching the priority indicator.")
  7886. (defvar org-remove-priority-next-time nil)
  7887. (defun org-priority-up ()
  7888. "Increase the priority of the current item."
  7889. (interactive)
  7890. (org-priority 'up))
  7891. (defun org-priority-down ()
  7892. "Decrease the priority of the current item."
  7893. (interactive)
  7894. (org-priority 'down))
  7895. (defun org-priority (&optional action)
  7896. "Change the priority of an item by ARG.
  7897. ACTION can be `set', `up', `down', or a character."
  7898. (interactive)
  7899. (setq action (or action 'set))
  7900. (let (current new news have remove)
  7901. (save-excursion
  7902. (org-back-to-heading)
  7903. (if (looking-at org-priority-regexp)
  7904. (setq current (string-to-char (match-string 2))
  7905. have t)
  7906. (setq current org-default-priority))
  7907. (cond
  7908. ((or (eq action 'set)
  7909. (if (featurep 'xemacs) (characterp action) (integerp action)))
  7910. (if (not (eq action 'set))
  7911. (setq new action)
  7912. (message "Priority %c-%c, SPC to remove: "
  7913. org-highest-priority org-lowest-priority)
  7914. (setq new (read-char-exclusive)))
  7915. (if (and (= (upcase org-highest-priority) org-highest-priority)
  7916. (= (upcase org-lowest-priority) org-lowest-priority))
  7917. (setq new (upcase new)))
  7918. (cond ((equal new ?\ ) (setq remove t))
  7919. ((or (< (upcase new) org-highest-priority) (> (upcase new) org-lowest-priority))
  7920. (error "Priority must be between `%c' and `%c'"
  7921. org-highest-priority org-lowest-priority))))
  7922. ((eq action 'up)
  7923. (if (and (not have) (eq last-command this-command))
  7924. (setq new org-lowest-priority)
  7925. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7926. org-default-priority (1- current)))))
  7927. ((eq action 'down)
  7928. (if (and (not have) (eq last-command this-command))
  7929. (setq new org-highest-priority)
  7930. (setq new (if (and org-priority-start-cycle-with-default (not have))
  7931. org-default-priority (1+ current)))))
  7932. (t (error "Invalid action")))
  7933. (if (or (< (upcase new) org-highest-priority)
  7934. (> (upcase new) org-lowest-priority))
  7935. (setq remove t))
  7936. (setq news (format "%c" new))
  7937. (if have
  7938. (if remove
  7939. (replace-match "" t t nil 1)
  7940. (replace-match news t t nil 2))
  7941. (if remove
  7942. (error "No priority cookie found in line")
  7943. (looking-at org-todo-line-regexp)
  7944. (if (match-end 2)
  7945. (progn
  7946. (goto-char (match-end 2))
  7947. (insert " [#" news "]"))
  7948. (goto-char (match-beginning 3))
  7949. (insert "[#" news "] ")))))
  7950. (org-preserve-lc (org-set-tags nil 'align))
  7951. (if remove
  7952. (message "Priority removed")
  7953. (message "Priority of current item set to %s" news))))
  7954. (defun org-get-priority (s)
  7955. "Find priority cookie and return priority."
  7956. (save-match-data
  7957. (if (not (string-match org-priority-regexp s))
  7958. (* 1000 (- org-lowest-priority org-default-priority))
  7959. (* 1000 (- org-lowest-priority
  7960. (string-to-char (match-string 2 s)))))))
  7961. ;;;; Tags
  7962. (defun org-scan-tags (action matcher &optional todo-only)
  7963. "Scan headline tags with inheritance and produce output ACTION.
  7964. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  7965. evaluated, testing if a given set of tags qualifies a headline for
  7966. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  7967. are included in the output."
  7968. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  7969. (mapconcat 'regexp-quote org-todo-keywords-1 "\\|")
  7970. (org-re
  7971. "\\>\\)\\)? *\\(.*?\\)\\(:[[:alnum:]_@:]+:\\)?[ \t]*$")))
  7972. (props (list 'face nil
  7973. 'done-face 'org-done
  7974. 'undone-face nil
  7975. 'mouse-face 'highlight
  7976. 'org-not-done-regexp org-not-done-regexp
  7977. 'org-todo-regexp org-todo-regexp
  7978. 'keymap org-agenda-keymap
  7979. 'help-echo
  7980. (format "mouse-2 or RET jump to org file %s"
  7981. (abbreviate-file-name
  7982. (or (buffer-file-name (buffer-base-buffer))
  7983. (buffer-name (buffer-base-buffer)))))))
  7984. (case-fold-search nil)
  7985. lspos
  7986. tags tags-list tags-alist (llast 0) rtn level category i txt
  7987. todo marker entry priority)
  7988. (save-excursion
  7989. (goto-char (point-min))
  7990. (when (eq action 'sparse-tree)
  7991. (org-overview)
  7992. (org-remove-occur-highlights))
  7993. (while (re-search-forward re nil t)
  7994. (catch :skip
  7995. (setq todo (if (match-end 1) (match-string 2))
  7996. tags (if (match-end 4) (match-string 4)))
  7997. (goto-char (setq lspos (1+ (match-beginning 0))))
  7998. (setq level (org-reduced-level (funcall outline-level))
  7999. category (org-get-category))
  8000. (setq i llast llast level)
  8001. ;; remove tag lists from same and sublevels
  8002. (while (>= i level)
  8003. (when (setq entry (assoc i tags-alist))
  8004. (setq tags-alist (delete entry tags-alist)))
  8005. (setq i (1- i)))
  8006. ;; add the next tags
  8007. (when tags
  8008. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  8009. tags-alist
  8010. (cons (cons level tags) tags-alist)))
  8011. ;; compile tags for current headline
  8012. (setq tags-list
  8013. (if org-use-tag-inheritance
  8014. (apply 'append (mapcar 'cdr tags-alist))
  8015. tags))
  8016. (when (and tags org-use-tag-inheritance
  8017. (not (eq t org-use-tag-inheritance)))
  8018. ;; selective inheritance, remove uninherited ones
  8019. (setcdr (car tags-alist)
  8020. (org-remove-uniherited-tags (cdar tags-alist))))
  8021. (when (and (or (not todo-only) (member todo org-not-done-keywords))
  8022. (eval matcher)
  8023. (or (not org-agenda-skip-archived-trees)
  8024. (not (member org-archive-tag tags-list))))
  8025. (and (eq action 'agenda) (org-agenda-skip))
  8026. ;; list this headline
  8027. (if (eq action 'sparse-tree)
  8028. (progn
  8029. (and org-highlight-sparse-tree-matches
  8030. (org-get-heading) (match-end 0)
  8031. (org-highlight-new-match
  8032. (match-beginning 0) (match-beginning 1)))
  8033. (org-show-context 'tags-tree))
  8034. (setq txt (org-format-agenda-item
  8035. ""
  8036. (concat
  8037. (if org-tags-match-list-sublevels
  8038. (make-string (1- level) ?.) "")
  8039. (org-get-heading))
  8040. category tags-list)
  8041. priority (org-get-priority txt))
  8042. (goto-char lspos)
  8043. (setq marker (org-agenda-new-marker))
  8044. (org-add-props txt props
  8045. 'org-marker marker 'org-hd-marker marker 'org-category category
  8046. 'priority priority 'type "tagsmatch")
  8047. (push txt rtn))
  8048. ;; if we are to skip sublevels, jump to end of subtree
  8049. (or org-tags-match-list-sublevels (org-end-of-subtree t))))))
  8050. (when (and (eq action 'sparse-tree)
  8051. (not org-sparse-tree-open-archived-trees))
  8052. (org-hide-archived-subtrees (point-min) (point-max)))
  8053. (nreverse rtn)))
  8054. (defun org-remove-uniherited-tags (tags)
  8055. "Remove all tags that are not inherited from the list TAGS."
  8056. (cond
  8057. ((eq org-use-tag-inheritance t) tags)
  8058. ((not org-use-tag-inheritance) nil)
  8059. ((stringp org-use-tag-inheritance)
  8060. (delq nil (mapcar
  8061. (lambda (x) (if (string-match org-use-tag-inheritance x) x nil))
  8062. tags)))
  8063. ((listp org-use-tag-inheritance)
  8064. (org-delete-all org-use-tag-inheritance tags))))
  8065. (defvar todo-only) ;; dynamically scoped
  8066. (defun org-tags-sparse-tree (&optional todo-only match)
  8067. "Create a sparse tree according to tags string MATCH.
  8068. MATCH can contain positive and negative selection of tags, like
  8069. \"+WORK+URGENT-WITHBOSS\".
  8070. If optional argument TODO_ONLY is non-nil, only select lines that are
  8071. also TODO lines."
  8072. (interactive "P")
  8073. (org-prepare-agenda-buffers (list (current-buffer)))
  8074. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)) todo-only))
  8075. (defvar org-cached-props nil)
  8076. (defun org-cached-entry-get (pom property)
  8077. (if (or (eq t org-use-property-inheritance)
  8078. (and (stringp org-use-property-inheritance)
  8079. (string-match org-use-property-inheritance property))
  8080. (and (listp org-use-property-inheritance)
  8081. (member property org-use-property-inheritance)))
  8082. ;; Caching is not possible, check it directly
  8083. (org-entry-get pom property 'inherit)
  8084. ;; Get all properties, so that we can do complicated checks easily
  8085. (cdr (assoc property (or org-cached-props
  8086. (setq org-cached-props
  8087. (org-entry-properties pom)))))))
  8088. (defun org-global-tags-completion-table (&optional files)
  8089. "Return the list of all tags in all agenda buffer/files."
  8090. (save-excursion
  8091. (org-uniquify
  8092. (delq nil
  8093. (apply 'append
  8094. (mapcar
  8095. (lambda (file)
  8096. (set-buffer (find-file-noselect file))
  8097. (append (org-get-buffer-tags)
  8098. (mapcar (lambda (x) (if (stringp (car-safe x))
  8099. (list (car-safe x)) nil))
  8100. org-tag-alist)))
  8101. (if (and files (car files))
  8102. files
  8103. (org-agenda-files))))))))
  8104. (defun org-make-tags-matcher (match)
  8105. "Create the TAGS//TODO matcher form for the selection string MATCH."
  8106. ;; todo-only is scoped dynamically into this function, and the function
  8107. ;; may change it it the matcher asksk for it.
  8108. (unless match
  8109. ;; Get a new match request, with completion
  8110. (let ((org-last-tags-completion-table
  8111. (org-global-tags-completion-table)))
  8112. (setq match (completing-read
  8113. "Match: " 'org-tags-completion-function nil nil nil
  8114. 'org-tags-history))))
  8115. ;; Parse the string and create a lisp form
  8116. (let ((match0 match)
  8117. (re (org-re "^&?\\([-+:]\\)?\\({[^}]+}\\|LEVEL\\([<=>]\\{1,2\\}\\)\\([0-9]+\\)\\|\\([[:alnum:]_]+\\)\\([<>=]\\{1,2\\}\\)\\({[^}]+}\\|\"[^\"]*\"\\|-?[.0-9]+\\(?:[eE][-+]?[0-9]+\\)?\\)\\|[[:alnum:]_@]+\\)"))
  8118. minus tag mm
  8119. tagsmatch todomatch tagsmatcher todomatcher kwd matcher
  8120. orterms term orlist re-p str-p level-p level-op
  8121. prop-p pn pv po cat-p gv)
  8122. (if (string-match "/+" match)
  8123. ;; match contains also a todo-matching request
  8124. (progn
  8125. (setq tagsmatch (substring match 0 (match-beginning 0))
  8126. todomatch (substring match (match-end 0)))
  8127. (if (string-match "^!" todomatch)
  8128. (setq todo-only t todomatch (substring todomatch 1)))
  8129. (if (string-match "^\\s-*$" todomatch)
  8130. (setq todomatch nil)))
  8131. ;; only matching tags
  8132. (setq tagsmatch match todomatch nil))
  8133. ;; Make the tags matcher
  8134. (if (or (not tagsmatch) (not (string-match "\\S-" tagsmatch)))
  8135. (setq tagsmatcher t)
  8136. (setq orterms (org-split-string tagsmatch "|") orlist nil)
  8137. (while (setq term (pop orterms))
  8138. (while (and (equal (substring term -1) "\\") orterms)
  8139. (setq term (concat term "|" (pop orterms)))) ; repair bad split
  8140. (while (string-match re term)
  8141. (setq minus (and (match-end 1)
  8142. (equal (match-string 1 term) "-"))
  8143. tag (match-string 2 term)
  8144. re-p (equal (string-to-char tag) ?{)
  8145. level-p (match-end 4)
  8146. prop-p (match-end 5)
  8147. mm (cond
  8148. (re-p `(org-match-any-p ,(substring tag 1 -1) tags-list))
  8149. (level-p
  8150. (setq level-op (org-op-to-function (match-string 3 term)))
  8151. `(,level-op level ,(string-to-number
  8152. (match-string 4 term))))
  8153. (prop-p
  8154. (setq pn (match-string 5 term)
  8155. po (match-string 6 term)
  8156. pv (match-string 7 term)
  8157. cat-p (equal pn "CATEGORY")
  8158. re-p (equal (string-to-char pv) ?{)
  8159. str-p (equal (string-to-char pv) ?\")
  8160. pv (if (or re-p str-p) (substring pv 1 -1) pv))
  8161. (setq po (org-op-to-function po str-p))
  8162. (if (equal pn "CATEGORY")
  8163. (setq gv '(get-text-property (point) 'org-category))
  8164. (setq gv `(org-cached-entry-get nil ,pn)))
  8165. (if re-p
  8166. (if (eq po 'org<>)
  8167. `(not (string-match ,pv (or ,gv "")))
  8168. `(string-match ,pv (or ,gv "")))
  8169. (if str-p
  8170. `(,po (or ,gv "") ,pv)
  8171. `(,po (string-to-number (or ,gv ""))
  8172. ,(string-to-number pv) ))))
  8173. (t `(member ,(downcase tag) tags-list)))
  8174. mm (if minus (list 'not mm) mm)
  8175. term (substring term (match-end 0)))
  8176. (push mm tagsmatcher))
  8177. (push (if (> (length tagsmatcher) 1)
  8178. (cons 'and tagsmatcher)
  8179. (car tagsmatcher))
  8180. orlist)
  8181. (setq tagsmatcher nil))
  8182. (setq tagsmatcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  8183. (setq tagsmatcher
  8184. (list 'progn '(setq org-cached-props nil) tagsmatcher)))
  8185. ;; Make the todo matcher
  8186. (if (or (not todomatch) (not (string-match "\\S-" todomatch)))
  8187. (setq todomatcher t)
  8188. (setq orterms (org-split-string todomatch "|") orlist nil)
  8189. (while (setq term (pop orterms))
  8190. (while (string-match re term)
  8191. (setq minus (and (match-end 1)
  8192. (equal (match-string 1 term) "-"))
  8193. kwd (match-string 2 term)
  8194. re-p (equal (string-to-char kwd) ?{)
  8195. term (substring term (match-end 0))
  8196. mm (if re-p
  8197. `(string-match ,(substring kwd 1 -1) todo)
  8198. (list 'equal 'todo kwd))
  8199. mm (if minus (list 'not mm) mm))
  8200. (push mm todomatcher))
  8201. (push (if (> (length todomatcher) 1)
  8202. (cons 'and todomatcher)
  8203. (car todomatcher))
  8204. orlist)
  8205. (setq todomatcher nil))
  8206. (setq todomatcher (if (> (length orlist) 1)
  8207. (cons 'or orlist) (car orlist))))
  8208. ;; Return the string and lisp forms of the matcher
  8209. (setq matcher (if todomatcher
  8210. (list 'and tagsmatcher todomatcher)
  8211. tagsmatcher))
  8212. (cons match0 matcher)))
  8213. (defun org-op-to-function (op &optional stringp)
  8214. (setq op
  8215. (cond
  8216. ((equal op "<" ) '(< string< ))
  8217. ((equal op ">" ) '(> org-string> ))
  8218. ((member op '("<=" "=<")) '(<= org-string<= ))
  8219. ((member op '(">=" "=>")) '(>= org-string>= ))
  8220. ((member op '("=" "==")) '(= string= ))
  8221. ((member op '("<>" "!=")) '(org<> org-string<> ))))
  8222. (nth (if stringp 1 0) op))
  8223. (defun org<> (a b) (not (= a b)))
  8224. (defun org-string<= (a b) (or (string= a b) (string< a b)))
  8225. (defun org-string>= (a b) (not (string< a b)))
  8226. (defun org-string> (a b) (and (not (string= a b)) (not (string< a b))))
  8227. (defun org-string<> (a b) (not (string= a b)))
  8228. (defun org-match-any-p (re list)
  8229. "Does re match any element of list?"
  8230. (setq list (mapcar (lambda (x) (string-match re x)) list))
  8231. (delq nil list))
  8232. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  8233. (defvar org-tags-overlay (org-make-overlay 1 1))
  8234. (org-detach-overlay org-tags-overlay)
  8235. (defun org-get-tags-at (&optional pos)
  8236. "Get a list of all headline tags applicable at POS.
  8237. POS defaults to point. If tags are inherited, the list contains
  8238. the targets in the same sequence as the headlines appear, i.e.
  8239. sthe tags of the current headline come last."
  8240. (interactive)
  8241. (let (tags ltags lastpos parent)
  8242. (save-excursion
  8243. (save-restriction
  8244. (widen)
  8245. (goto-char (or pos (point)))
  8246. (save-match-data
  8247. (condition-case nil
  8248. (progn
  8249. (org-back-to-heading t)
  8250. (while (not (equal lastpos (point)))
  8251. (setq lastpos (point))
  8252. (when (looking-at (org-re "[^\r\n]+?:\\([[:alnum:]_@:]+\\):[ \t]*$"))
  8253. (setq ltags (org-split-string
  8254. (org-match-string-no-properties 1) ":"))
  8255. (setq tags (append (org-remove-uniherited-tags ltags)
  8256. tags)))
  8257. (or org-use-tag-inheritance (error ""))
  8258. (org-up-heading-all 1)
  8259. (setq parent t)))
  8260. (error nil))))
  8261. tags)))
  8262. (defun org-toggle-tag (tag &optional onoff)
  8263. "Toggle the tag TAG for the current line.
  8264. If ONOFF is `on' or `off', don't toggle but set to this state."
  8265. (unless (org-on-heading-p t) (error "Not on headling"))
  8266. (let (res current)
  8267. (save-excursion
  8268. (beginning-of-line)
  8269. (if (re-search-forward (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t]*$")
  8270. (point-at-eol) t)
  8271. (progn
  8272. (setq current (match-string 1))
  8273. (replace-match ""))
  8274. (setq current ""))
  8275. (setq current (nreverse (org-split-string current ":")))
  8276. (cond
  8277. ((eq onoff 'on)
  8278. (setq res t)
  8279. (or (member tag current) (push tag current)))
  8280. ((eq onoff 'off)
  8281. (or (not (member tag current)) (setq current (delete tag current))))
  8282. (t (if (member tag current)
  8283. (setq current (delete tag current))
  8284. (setq res t)
  8285. (push tag current))))
  8286. (end-of-line 1)
  8287. (if current
  8288. (progn
  8289. (insert " :" (mapconcat 'identity (nreverse current) ":") ":")
  8290. (org-set-tags nil t))
  8291. (delete-horizontal-space))
  8292. (run-hooks 'org-after-tags-change-hook))
  8293. res))
  8294. (defun org-align-tags-here (to-col)
  8295. ;; Assumes that this is a headline
  8296. (let ((pos (point)) (col (current-column)) ncol tags-l p)
  8297. (beginning-of-line 1)
  8298. (if (and (looking-at (org-re ".*?\\([ \t]+\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8299. (< pos (match-beginning 2)))
  8300. (progn
  8301. (setq tags-l (- (match-end 2) (match-beginning 2)))
  8302. (goto-char (match-beginning 1))
  8303. (insert " ")
  8304. (delete-region (point) (1+ (match-beginning 2)))
  8305. (setq ncol (max (1+ (current-column))
  8306. (1+ col)
  8307. (if (> to-col 0)
  8308. to-col
  8309. (- (abs to-col) tags-l))))
  8310. (setq p (point))
  8311. (insert (make-string (- ncol (current-column)) ?\ ))
  8312. (setq ncol (current-column))
  8313. (tabify p (point-at-eol))
  8314. (org-move-to-column (min ncol col) t))
  8315. (goto-char pos))))
  8316. (defun org-set-tags (&optional arg just-align)
  8317. "Set the tags for the current headline.
  8318. With prefix ARG, realign all tags in headings in the current buffer."
  8319. (interactive "P")
  8320. (let* ((re (concat "^" outline-regexp))
  8321. (current (org-get-tags-string))
  8322. (col (current-column))
  8323. (org-setting-tags t)
  8324. table current-tags inherited-tags ; computed below when needed
  8325. tags p0 c0 c1 rpl)
  8326. (if arg
  8327. (save-excursion
  8328. (goto-char (point-min))
  8329. (let ((buffer-invisibility-spec (org-inhibit-invisibility)))
  8330. (while (re-search-forward re nil t)
  8331. (org-set-tags nil t)
  8332. (end-of-line 1)))
  8333. (message "All tags realigned to column %d" org-tags-column))
  8334. (if just-align
  8335. (setq tags current)
  8336. ;; Get a new set of tags from the user
  8337. (save-excursion
  8338. (setq table (or org-tag-alist (org-get-buffer-tags))
  8339. org-last-tags-completion-table table
  8340. current-tags (org-split-string current ":")
  8341. inherited-tags (nreverse
  8342. (nthcdr (length current-tags)
  8343. (nreverse (org-get-tags-at))))
  8344. tags
  8345. (if (or (eq t org-use-fast-tag-selection)
  8346. (and org-use-fast-tag-selection
  8347. (delq nil (mapcar 'cdr table))))
  8348. (org-fast-tag-selection
  8349. current-tags inherited-tags table
  8350. (if org-fast-tag-selection-include-todo org-todo-key-alist))
  8351. (let ((org-add-colon-after-tag-completion t))
  8352. (org-trim
  8353. (org-without-partial-completion
  8354. (completing-read "Tags: " 'org-tags-completion-function
  8355. nil nil current 'org-tags-history)))))))
  8356. (while (string-match "[-+&]+" tags)
  8357. ;; No boolean logic, just a list
  8358. (setq tags (replace-match ":" t t tags))))
  8359. (if (string-match "\\`[\t ]*\\'" tags)
  8360. (setq tags "")
  8361. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  8362. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  8363. ;; Insert new tags at the correct column
  8364. (beginning-of-line 1)
  8365. (cond
  8366. ((and (equal current "") (equal tags "")))
  8367. ((re-search-forward
  8368. (concat "\\([ \t]*" (regexp-quote current) "\\)[ \t]*$")
  8369. (point-at-eol) t)
  8370. (if (equal tags "")
  8371. (setq rpl "")
  8372. (goto-char (match-beginning 0))
  8373. (setq c0 (current-column) p0 (point)
  8374. c1 (max (1+ c0) (if (> org-tags-column 0)
  8375. org-tags-column
  8376. (- (- org-tags-column) (length tags))))
  8377. rpl (concat (make-string (max 0 (- c1 c0)) ?\ ) tags)))
  8378. (replace-match rpl t t)
  8379. (and (not (featurep 'xemacs)) c0 indent-tabs-mode (tabify p0 (point)))
  8380. tags)
  8381. (t (error "Tags alignment failed")))
  8382. (org-move-to-column col)
  8383. (unless just-align
  8384. (run-hooks 'org-after-tags-change-hook)))))
  8385. (defun org-change-tag-in-region (beg end tag off)
  8386. "Add or remove TAG for each entry in the region.
  8387. This works in the agenda, and also in an org-mode buffer."
  8388. (interactive
  8389. (list (region-beginning) (region-end)
  8390. (let ((org-last-tags-completion-table
  8391. (if (org-mode-p)
  8392. (org-get-buffer-tags)
  8393. (org-global-tags-completion-table))))
  8394. (completing-read
  8395. "Tag: " 'org-tags-completion-function nil nil nil
  8396. 'org-tags-history))
  8397. (progn
  8398. (message "[s]et or [r]emove? ")
  8399. (equal (read-char-exclusive) ?r))))
  8400. (if (fboundp 'deactivate-mark) (deactivate-mark))
  8401. (let ((agendap (equal major-mode 'org-agenda-mode))
  8402. l1 l2 m buf pos newhead (cnt 0))
  8403. (goto-char end)
  8404. (setq l2 (1- (org-current-line)))
  8405. (goto-char beg)
  8406. (setq l1 (org-current-line))
  8407. (loop for l from l1 to l2 do
  8408. (goto-line l)
  8409. (setq m (get-text-property (point) 'org-hd-marker))
  8410. (when (or (and (org-mode-p) (org-on-heading-p))
  8411. (and agendap m))
  8412. (setq buf (if agendap (marker-buffer m) (current-buffer))
  8413. pos (if agendap m (point)))
  8414. (with-current-buffer buf
  8415. (save-excursion
  8416. (save-restriction
  8417. (goto-char pos)
  8418. (setq cnt (1+ cnt))
  8419. (org-toggle-tag tag (if off 'off 'on))
  8420. (setq newhead (org-get-heading)))))
  8421. (and agendap (org-agenda-change-all-lines newhead m))))
  8422. (message "Tag :%s: %s in %d headings" tag (if off "removed" "set") cnt)))
  8423. (defun org-tags-completion-function (string predicate &optional flag)
  8424. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  8425. (confirm (lambda (x) (stringp (car x)))))
  8426. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  8427. (setq s1 (match-string 1 string)
  8428. s2 (match-string 2 string))
  8429. (setq s1 "" s2 string))
  8430. (cond
  8431. ((eq flag nil)
  8432. ;; try completion
  8433. (setq rtn (try-completion s2 ctable confirm))
  8434. (if (stringp rtn)
  8435. (setq rtn
  8436. (concat s1 s2 (substring rtn (length s2))
  8437. (if (and org-add-colon-after-tag-completion
  8438. (assoc rtn ctable))
  8439. ":" ""))))
  8440. rtn)
  8441. ((eq flag t)
  8442. ;; all-completions
  8443. (all-completions s2 ctable confirm)
  8444. )
  8445. ((eq flag 'lambda)
  8446. ;; exact match?
  8447. (assoc s2 ctable)))
  8448. ))
  8449. (defun org-fast-tag-insert (kwd tags face &optional end)
  8450. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  8451. (insert (format "%-12s" (concat kwd ":"))
  8452. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  8453. (or end "")))
  8454. (defun org-fast-tag-show-exit (flag)
  8455. (save-excursion
  8456. (goto-line 3)
  8457. (if (re-search-forward "[ \t]+Next change exits" (point-at-eol) t)
  8458. (replace-match ""))
  8459. (when flag
  8460. (end-of-line 1)
  8461. (org-move-to-column (- (window-width) 19) t)
  8462. (insert (org-add-props " Next change exits" nil 'face 'org-warning)))))
  8463. (defun org-set-current-tags-overlay (current prefix)
  8464. (let ((s (concat ":" (mapconcat 'identity current ":") ":")))
  8465. (if (featurep 'xemacs)
  8466. (org-overlay-display org-tags-overlay (concat prefix s)
  8467. 'secondary-selection)
  8468. (put-text-property 0 (length s) 'face '(secondary-selection org-tag) s)
  8469. (org-overlay-display org-tags-overlay (concat prefix s)))))
  8470. (defun org-fast-tag-selection (current inherited table &optional todo-table)
  8471. "Fast tag selection with single keys.
  8472. CURRENT is the current list of tags in the headline, INHERITED is the
  8473. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  8474. possibly with grouping information. TODO-TABLE is a similar table with
  8475. TODO keywords, should these have keys assigned to them.
  8476. If the keys are nil, a-z are automatically assigned.
  8477. Returns the new tags string, or nil to not change the current settings."
  8478. (let* ((fulltable (append table todo-table))
  8479. (maxlen (apply 'max (mapcar
  8480. (lambda (x)
  8481. (if (stringp (car x)) (string-width (car x)) 0))
  8482. fulltable)))
  8483. (buf (current-buffer))
  8484. (expert (eq org-fast-tag-selection-single-key 'expert))
  8485. (buffer-tags nil)
  8486. (fwidth (+ maxlen 3 1 3))
  8487. (ncol (/ (- (window-width) 4) fwidth))
  8488. (i-face 'org-done)
  8489. (c-face 'org-todo)
  8490. tg cnt e c char c1 c2 ntable tbl rtn
  8491. ov-start ov-end ov-prefix
  8492. (exit-after-next org-fast-tag-selection-single-key)
  8493. (done-keywords org-done-keywords)
  8494. groups ingroup)
  8495. (save-excursion
  8496. (beginning-of-line 1)
  8497. (if (looking-at
  8498. (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8499. (setq ov-start (match-beginning 1)
  8500. ov-end (match-end 1)
  8501. ov-prefix "")
  8502. (setq ov-start (1- (point-at-eol))
  8503. ov-end (1+ ov-start))
  8504. (skip-chars-forward "^\n\r")
  8505. (setq ov-prefix
  8506. (concat
  8507. (buffer-substring (1- (point)) (point))
  8508. (if (> (current-column) org-tags-column)
  8509. " "
  8510. (make-string (- org-tags-column (current-column)) ?\ ))))))
  8511. (org-move-overlay org-tags-overlay ov-start ov-end)
  8512. (save-window-excursion
  8513. (if expert
  8514. (set-buffer (get-buffer-create " *Org tags*"))
  8515. (delete-other-windows)
  8516. (split-window-vertically)
  8517. (org-switch-to-buffer-other-window (get-buffer-create " *Org tags*")))
  8518. (erase-buffer)
  8519. (org-set-local 'org-done-keywords done-keywords)
  8520. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  8521. (org-fast-tag-insert "Current" current c-face "\n\n")
  8522. (org-fast-tag-show-exit exit-after-next)
  8523. (org-set-current-tags-overlay current ov-prefix)
  8524. (setq tbl fulltable char ?a cnt 0)
  8525. (while (setq e (pop tbl))
  8526. (cond
  8527. ((equal e '(:startgroup))
  8528. (push '() groups) (setq ingroup t)
  8529. (when (not (= cnt 0))
  8530. (setq cnt 0)
  8531. (insert "\n"))
  8532. (insert "{ "))
  8533. ((equal e '(:endgroup))
  8534. (setq ingroup nil cnt 0)
  8535. (insert "}\n"))
  8536. (t
  8537. (setq tg (car e) c2 nil)
  8538. (if (cdr e)
  8539. (setq c (cdr e))
  8540. ;; automatically assign a character.
  8541. (setq c1 (string-to-char
  8542. (downcase (substring
  8543. tg (if (= (string-to-char tg) ?@) 1 0)))))
  8544. (if (or (rassoc c1 ntable) (rassoc c1 table))
  8545. (while (or (rassoc char ntable) (rassoc char table))
  8546. (setq char (1+ char)))
  8547. (setq c2 c1))
  8548. (setq c (or c2 char)))
  8549. (if ingroup (push tg (car groups)))
  8550. (setq tg (org-add-props tg nil 'face
  8551. (cond
  8552. ((not (assoc tg table))
  8553. (org-get-todo-face tg))
  8554. ((member tg current) c-face)
  8555. ((member tg inherited) i-face)
  8556. (t nil))))
  8557. (if (and (= cnt 0) (not ingroup)) (insert " "))
  8558. (insert "[" c "] " tg (make-string
  8559. (- fwidth 4 (length tg)) ?\ ))
  8560. (push (cons tg c) ntable)
  8561. (when (= (setq cnt (1+ cnt)) ncol)
  8562. (insert "\n")
  8563. (if ingroup (insert " "))
  8564. (setq cnt 0)))))
  8565. (setq ntable (nreverse ntable))
  8566. (insert "\n")
  8567. (goto-char (point-min))
  8568. (if (and (not expert) (fboundp 'fit-window-to-buffer))
  8569. (fit-window-to-buffer))
  8570. (setq rtn
  8571. (catch 'exit
  8572. (while t
  8573. (message "[a-z..]:Toggle [SPC]:clear [RET]:accept [TAB]:free%s%s"
  8574. (if groups " [!] no groups" " [!]groups")
  8575. (if expert " [C-c]:window" (if exit-after-next " [C-c]:single" " [C-c]:multi")))
  8576. (setq c (let ((inhibit-quit t)) (read-char-exclusive)))
  8577. (cond
  8578. ((= c ?\r) (throw 'exit t))
  8579. ((= c ?!)
  8580. (setq groups (not groups))
  8581. (goto-char (point-min))
  8582. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  8583. ((= c ?\C-c)
  8584. (if (not expert)
  8585. (org-fast-tag-show-exit
  8586. (setq exit-after-next (not exit-after-next)))
  8587. (setq expert nil)
  8588. (delete-other-windows)
  8589. (split-window-vertically)
  8590. (org-switch-to-buffer-other-window " *Org tags*")
  8591. (and (fboundp 'fit-window-to-buffer)
  8592. (fit-window-to-buffer))))
  8593. ((or (= c ?\C-g)
  8594. (and (= c ?q) (not (rassoc c ntable))))
  8595. (org-detach-overlay org-tags-overlay)
  8596. (setq quit-flag t))
  8597. ((= c ?\ )
  8598. (setq current nil)
  8599. (if exit-after-next (setq exit-after-next 'now)))
  8600. ((= c ?\t)
  8601. (condition-case nil
  8602. (setq tg (completing-read
  8603. "Tag: "
  8604. (or buffer-tags
  8605. (with-current-buffer buf
  8606. (org-get-buffer-tags)))))
  8607. (quit (setq tg "")))
  8608. (when (string-match "\\S-" tg)
  8609. (add-to-list 'buffer-tags (list tg))
  8610. (if (member tg current)
  8611. (setq current (delete tg current))
  8612. (push tg current)))
  8613. (if exit-after-next (setq exit-after-next 'now)))
  8614. ((setq e (rassoc c todo-table) tg (car e))
  8615. (with-current-buffer buf
  8616. (save-excursion (org-todo tg)))
  8617. (if exit-after-next (setq exit-after-next 'now)))
  8618. ((setq e (rassoc c ntable) tg (car e))
  8619. (if (member tg current)
  8620. (setq current (delete tg current))
  8621. (loop for g in groups do
  8622. (if (member tg g)
  8623. (mapc (lambda (x)
  8624. (setq current (delete x current)))
  8625. g)))
  8626. (push tg current))
  8627. (if exit-after-next (setq exit-after-next 'now))))
  8628. ;; Create a sorted list
  8629. (setq current
  8630. (sort current
  8631. (lambda (a b)
  8632. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  8633. (if (eq exit-after-next 'now) (throw 'exit t))
  8634. (goto-char (point-min))
  8635. (beginning-of-line 2)
  8636. (delete-region (point) (point-at-eol))
  8637. (org-fast-tag-insert "Current" current c-face)
  8638. (org-set-current-tags-overlay current ov-prefix)
  8639. (while (re-search-forward
  8640. (org-re "\\[.\\] \\([[:alnum:]_@]+\\)") nil t)
  8641. (setq tg (match-string 1))
  8642. (add-text-properties
  8643. (match-beginning 1) (match-end 1)
  8644. (list 'face
  8645. (cond
  8646. ((member tg current) c-face)
  8647. ((member tg inherited) i-face)
  8648. (t (get-text-property (match-beginning 1) 'face))))))
  8649. (goto-char (point-min)))))
  8650. (org-detach-overlay org-tags-overlay)
  8651. (if rtn
  8652. (mapconcat 'identity current ":")
  8653. nil))))
  8654. (defun org-get-tags-string ()
  8655. "Get the TAGS string in the current headline."
  8656. (unless (org-on-heading-p t)
  8657. (error "Not on a heading"))
  8658. (save-excursion
  8659. (beginning-of-line 1)
  8660. (if (looking-at (org-re ".*[ \t]\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  8661. (org-match-string-no-properties 1)
  8662. "")))
  8663. (defun org-get-tags ()
  8664. "Get the list of tags specified in the current headline."
  8665. (org-split-string (org-get-tags-string) ":"))
  8666. (defun org-get-buffer-tags ()
  8667. "Get a table of all tags used in the buffer, for completion."
  8668. (let (tags)
  8669. (save-excursion
  8670. (goto-char (point-min))
  8671. (while (re-search-forward
  8672. (org-re "[ \t]:\\([[:alnum:]_@:]+\\):[ \t\r\n]") nil t)
  8673. (when (equal (char-after (point-at-bol 0)) ?*)
  8674. (mapc (lambda (x) (add-to-list 'tags x))
  8675. (org-split-string (org-match-string-no-properties 1) ":")))))
  8676. (mapcar 'list tags)))
  8677. ;;;; Properties
  8678. ;;; Setting and retrieving properties
  8679. (defconst org-special-properties
  8680. '("TODO" "TAGS" "ALLTAGS" "DEADLINE" "SCHEDULED" "CLOCK" "PRIORITY"
  8681. "TIMESTAMP" "TIMESTAMP_IA")
  8682. "The special properties valid in Org-mode.
  8683. These are properties that are not defined in the property drawer,
  8684. but in some other way.")
  8685. (defconst org-default-properties
  8686. '("ARCHIVE" "CATEGORY" "SUMMARY" "DESCRIPTION"
  8687. "LOCATION" "LOGGING" "COLUMNS")
  8688. "Some properties that are used by Org-mode for various purposes.
  8689. Being in this list makes sure that they are offered for completion.")
  8690. (defconst org-property-start-re "^[ \t]*:PROPERTIES:[ \t]*$"
  8691. "Regular expression matching the first line of a property drawer.")
  8692. (defconst org-property-end-re "^[ \t]*:END:[ \t]*$"
  8693. "Regular expression matching the first line of a property drawer.")
  8694. (defun org-property-action ()
  8695. "Do an action on properties."
  8696. (interactive)
  8697. (let (c)
  8698. (org-at-property-p)
  8699. (message "Property Action: [s]et [d]elete [D]elete globally [c]ompute")
  8700. (setq c (read-char-exclusive))
  8701. (cond
  8702. ((equal c ?s)
  8703. (call-interactively 'org-set-property))
  8704. ((equal c ?d)
  8705. (call-interactively 'org-delete-property))
  8706. ((equal c ?D)
  8707. (call-interactively 'org-delete-property-globally))
  8708. ((equal c ?c)
  8709. (call-interactively 'org-compute-property-at-point))
  8710. (t (error "No such property action %c" c)))))
  8711. (defun org-at-property-p ()
  8712. "Is the cursor in a property line?"
  8713. ;; FIXME: Does not check if we are actually in the drawer.
  8714. ;; FIXME: also returns true on any drawers.....
  8715. ;; This is used by C-c C-c for property action.
  8716. (save-excursion
  8717. (beginning-of-line 1)
  8718. (looking-at (org-re "^[ \t]*\\(:\\([[:alpha:]][[:alnum:]_-]*\\):\\)[ \t]*\\(.*\\)"))))
  8719. (defun org-get-property-block (&optional beg end force)
  8720. "Return the (beg . end) range of the body of the property drawer.
  8721. BEG and END can be beginning and end of subtree, if not given
  8722. they will be found.
  8723. If the drawer does not exist and FORCE is non-nil, create the drawer."
  8724. (catch 'exit
  8725. (save-excursion
  8726. (let* ((beg (or beg (progn (org-back-to-heading t) (point))))
  8727. (end (or end (progn (outline-next-heading) (point)))))
  8728. (goto-char beg)
  8729. (if (re-search-forward org-property-start-re end t)
  8730. (setq beg (1+ (match-end 0)))
  8731. (if force
  8732. (save-excursion
  8733. (org-insert-property-drawer)
  8734. (setq end (progn (outline-next-heading) (point))))
  8735. (throw 'exit nil))
  8736. (goto-char beg)
  8737. (if (re-search-forward org-property-start-re end t)
  8738. (setq beg (1+ (match-end 0)))))
  8739. (if (re-search-forward org-property-end-re end t)
  8740. (setq end (match-beginning 0))
  8741. (or force (throw 'exit nil))
  8742. (goto-char beg)
  8743. (setq end beg)
  8744. (org-indent-line-function)
  8745. (insert ":END:\n"))
  8746. (cons beg end)))))
  8747. (defun org-entry-properties (&optional pom which)
  8748. "Get all properties of the entry at point-or-marker POM.
  8749. This includes the TODO keyword, the tags, time strings for deadline,
  8750. scheduled, and clocking, and any additional properties defined in the
  8751. entry. The return value is an alist, keys may occur multiple times
  8752. if the property key was used several times.
  8753. POM may also be nil, in which case the current entry is used.
  8754. If WHICH is nil or `all', get all properties. If WHICH is
  8755. `special' or `standard', only get that subclass."
  8756. (setq which (or which 'all))
  8757. (org-with-point-at pom
  8758. (let ((clockstr (substring org-clock-string 0 -1))
  8759. (excluded '("TODO" "TAGS" "ALLTAGS" "PRIORITY"))
  8760. beg end range props sum-props key value string clocksum)
  8761. (save-excursion
  8762. (when (condition-case nil (org-back-to-heading t) (error nil))
  8763. (setq beg (point))
  8764. (setq sum-props (get-text-property (point) 'org-summaries))
  8765. (setq clocksum (get-text-property (point) :org-clock-minutes))
  8766. (outline-next-heading)
  8767. (setq end (point))
  8768. (when (memq which '(all special))
  8769. ;; Get the special properties, like TODO and tags
  8770. (goto-char beg)
  8771. (when (and (looking-at org-todo-line-regexp) (match-end 2))
  8772. (push (cons "TODO" (org-match-string-no-properties 2)) props))
  8773. (when (looking-at org-priority-regexp)
  8774. (push (cons "PRIORITY" (org-match-string-no-properties 2)) props))
  8775. (when (and (setq value (org-get-tags-string))
  8776. (string-match "\\S-" value))
  8777. (push (cons "TAGS" value) props))
  8778. (when (setq value (org-get-tags-at))
  8779. (push (cons "ALLTAGS" (concat ":" (mapconcat 'identity value ":") ":"))
  8780. props))
  8781. (while (re-search-forward org-maybe-keyword-time-regexp end t)
  8782. (setq key (if (match-end 1) (substring (org-match-string-no-properties 1) 0 -1))
  8783. string (if (equal key clockstr)
  8784. (org-no-properties
  8785. (org-trim
  8786. (buffer-substring
  8787. (match-beginning 3) (goto-char (point-at-eol)))))
  8788. (substring (org-match-string-no-properties 3) 1 -1)))
  8789. (unless key
  8790. (if (= (char-after (match-beginning 3)) ?\[)
  8791. (setq key "TIMESTAMP_IA")
  8792. (setq key "TIMESTAMP")))
  8793. (when (or (equal key clockstr) (not (assoc key props)))
  8794. (push (cons key string) props)))
  8795. )
  8796. (when (memq which '(all standard))
  8797. ;; Get the standard properties, like :PORP: ...
  8798. (setq range (org-get-property-block beg end))
  8799. (when range
  8800. (goto-char (car range))
  8801. (while (re-search-forward
  8802. (org-re "^[ \t]*:\\([[:alpha:]][[:alnum:]_-]*\\):[ \t]*\\(\\S-.*\\)?")
  8803. (cdr range) t)
  8804. (setq key (org-match-string-no-properties 1)
  8805. value (org-trim (or (org-match-string-no-properties 2) "")))
  8806. (unless (member key excluded)
  8807. (push (cons key (or value "")) props)))))
  8808. (if clocksum
  8809. (push (cons "CLOCKSUM"
  8810. (org-columns-number-to-string (/ (float clocksum) 60.)
  8811. 'add_times))
  8812. props))
  8813. (append sum-props (nreverse props)))))))
  8814. (defun org-entry-get (pom property &optional inherit)
  8815. "Get value of PROPERTY for entry at point-or-marker POM.
  8816. If INHERIT is non-nil and the entry does not have the property,
  8817. then also check higher levels of the hierarchy.
  8818. If INHERIT is the symbol `selective', use inheritance only if the setting
  8819. in `org-use-property-inheritance' selects PROPERTY for inheritance.
  8820. If the property is present but empty, the return value is the empty string.
  8821. If the property is not present at all, nil is returned."
  8822. (org-with-point-at pom
  8823. (if (and inherit (if (eq inherit 'selective)
  8824. (org-property-inherit-p property)
  8825. t))
  8826. (org-entry-get-with-inheritance property)
  8827. (if (member property org-special-properties)
  8828. ;; We need a special property. Use brute force, get all properties.
  8829. (cdr (assoc property (org-entry-properties nil 'special)))
  8830. (let ((range (org-get-property-block)))
  8831. (if (and range
  8832. (goto-char (car range))
  8833. (re-search-forward
  8834. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)?")
  8835. (cdr range) t))
  8836. ;; Found the property, return it.
  8837. (if (match-end 1)
  8838. (org-match-string-no-properties 1)
  8839. "")))))))
  8840. (defun org-property-or-variable-value (var &optional inherit)
  8841. "Check if there is a property fixing the value of VAR.
  8842. If yes, return this value. If not, return the current value of the variable."
  8843. (let ((prop (org-entry-get nil (symbol-name var) inherit)))
  8844. (if (and prop (stringp prop) (string-match "\\S-" prop))
  8845. (read prop)
  8846. (symbol-value var))))
  8847. (defun org-entry-delete (pom property)
  8848. "Delete the property PROPERTY from entry at point-or-marker POM."
  8849. (org-with-point-at pom
  8850. (if (member property org-special-properties)
  8851. nil ; cannot delete these properties.
  8852. (let ((range (org-get-property-block)))
  8853. (if (and range
  8854. (goto-char (car range))
  8855. (re-search-forward
  8856. (concat "^[ \t]*:" property ":[ \t]*\\(.*\\S-\\)")
  8857. (cdr range) t))
  8858. (progn
  8859. (delete-region (match-beginning 0) (1+ (point-at-eol)))
  8860. t)
  8861. nil)))))
  8862. ;; Multi-values properties are properties that contain multiple values
  8863. ;; These values are assumed to be single words, separated by whitespace.
  8864. (defun org-entry-add-to-multivalued-property (pom property value)
  8865. "Add VALUE to the words in the PROPERTY in entry at point-or-marker POM."
  8866. (let* ((old (org-entry-get pom property))
  8867. (values (and old (org-split-string old "[ \t]"))))
  8868. (unless (member value values)
  8869. (setq values (cons value values))
  8870. (org-entry-put pom property
  8871. (mapconcat 'identity values " ")))))
  8872. (defun org-entry-remove-from-multivalued-property (pom property value)
  8873. "Remove VALUE from words in the PROPERTY in entry at point-or-marker POM."
  8874. (let* ((old (org-entry-get pom property))
  8875. (values (and old (org-split-string old "[ \t]"))))
  8876. (when (member value values)
  8877. (setq values (delete value values))
  8878. (org-entry-put pom property
  8879. (mapconcat 'identity values " ")))))
  8880. (defun org-entry-member-in-multivalued-property (pom property value)
  8881. "Is VALUE one of the words in the PROPERTY in entry at point-or-marker POM?"
  8882. (let* ((old (org-entry-get pom property))
  8883. (values (and old (org-split-string old "[ \t]"))))
  8884. (member value values)))
  8885. (defvar org-entry-property-inherited-from (make-marker))
  8886. (defun org-entry-get-with-inheritance (property)
  8887. "Get entry property, and search higher levels if not present."
  8888. (let (tmp)
  8889. (save-excursion
  8890. (save-restriction
  8891. (widen)
  8892. (catch 'ex
  8893. (while t
  8894. (when (setq tmp (org-entry-get nil property))
  8895. (org-back-to-heading t)
  8896. (move-marker org-entry-property-inherited-from (point))
  8897. (throw 'ex tmp))
  8898. (or (org-up-heading-safe) (throw 'ex nil)))))
  8899. (or tmp (cdr (assoc property org-local-properties))
  8900. (cdr (assoc property org-global-properties))))))
  8901. (defun org-entry-put (pom property value)
  8902. "Set PROPERTY to VALUE for entry at point-or-marker POM."
  8903. (org-with-point-at pom
  8904. (org-back-to-heading t)
  8905. (let ((beg (point)) (end (save-excursion (outline-next-heading) (point)))
  8906. range)
  8907. (cond
  8908. ((equal property "TODO")
  8909. (when (and (stringp value) (string-match "\\S-" value)
  8910. (not (member value org-todo-keywords-1)))
  8911. (error "\"%s\" is not a valid TODO state" value))
  8912. (if (or (not value)
  8913. (not (string-match "\\S-" value)))
  8914. (setq value 'none))
  8915. (org-todo value)
  8916. (org-set-tags nil 'align))
  8917. ((equal property "PRIORITY")
  8918. (org-priority (if (and value (stringp value) (string-match "\\S-" value))
  8919. (string-to-char value) ?\ ))
  8920. (org-set-tags nil 'align))
  8921. ((equal property "SCHEDULED")
  8922. (if (re-search-forward org-scheduled-time-regexp end t)
  8923. (cond
  8924. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8925. ((eq value 'later) (org-timestamp-change 1 'day))
  8926. (t (call-interactively 'org-schedule)))
  8927. (call-interactively 'org-schedule)))
  8928. ((equal property "DEADLINE")
  8929. (if (re-search-forward org-deadline-time-regexp end t)
  8930. (cond
  8931. ((eq value 'earlier) (org-timestamp-change -1 'day))
  8932. ((eq value 'later) (org-timestamp-change 1 'day))
  8933. (t (call-interactively 'org-deadline)))
  8934. (call-interactively 'org-deadline)))
  8935. ((member property org-special-properties)
  8936. (error "The %s property can not yet be set with `org-entry-put'"
  8937. property))
  8938. (t ; a non-special property
  8939. (let ((buffer-invisibility-spec (org-inhibit-invisibility))) ; Emacs 21
  8940. (setq range (org-get-property-block beg end 'force))
  8941. (goto-char (car range))
  8942. (if (re-search-forward
  8943. (concat "^[ \t]*:" property ":\\(.*\\)") (cdr range) t)
  8944. (progn
  8945. (delete-region (match-beginning 1) (match-end 1))
  8946. (goto-char (match-beginning 1)))
  8947. (goto-char (cdr range))
  8948. (insert "\n")
  8949. (backward-char 1)
  8950. (org-indent-line-function)
  8951. (insert ":" property ":"))
  8952. (and value (insert " " value))
  8953. (org-indent-line-function)))))))
  8954. (defun org-buffer-property-keys (&optional include-specials include-defaults include-columns)
  8955. "Get all property keys in the current buffer.
  8956. With INCLUDE-SPECIALS, also list the special properties that relect things
  8957. like tags and TODO state.
  8958. With INCLUDE-DEFAULTS, also include properties that has special meaning
  8959. internally: ARCHIVE, CATEGORY, SUMMARY, DESCRIPTION, LOCATION, and LOGGING.
  8960. With INCLUDE-COLUMNS, also include property names given in COLUMN
  8961. formats in the current buffer."
  8962. (let (rtn range cfmt cols s p)
  8963. (save-excursion
  8964. (save-restriction
  8965. (widen)
  8966. (goto-char (point-min))
  8967. (while (re-search-forward org-property-start-re nil t)
  8968. (setq range (org-get-property-block))
  8969. (goto-char (car range))
  8970. (while (re-search-forward
  8971. (org-re "^[ \t]*:\\([-[:alnum:]_]+\\):")
  8972. (cdr range) t)
  8973. (add-to-list 'rtn (org-match-string-no-properties 1)))
  8974. (outline-next-heading))))
  8975. (when include-specials
  8976. (setq rtn (append org-special-properties rtn)))
  8977. (when include-defaults
  8978. (mapc (lambda (x) (add-to-list 'rtn x)) org-default-properties))
  8979. (when include-columns
  8980. (save-excursion
  8981. (save-restriction
  8982. (widen)
  8983. (goto-char (point-min))
  8984. (while (re-search-forward
  8985. "^\\(#\\+COLUMNS:\\|[ \t]*:COLUMNS:\\)[ \t]*\\(.*\\)"
  8986. nil t)
  8987. (setq cfmt (match-string 2) s 0)
  8988. (while (string-match (org-re "%[0-9]*\\([-[:alnum:]_]+\\)")
  8989. cfmt s)
  8990. (setq s (match-end 0)
  8991. p (match-string 1 cfmt))
  8992. (unless (or (equal p "ITEM")
  8993. (member p org-special-properties))
  8994. (add-to-list 'rtn (match-string 1 cfmt))))))))
  8995. (sort rtn (lambda (a b) (string< (upcase a) (upcase b))))))
  8996. (defun org-property-values (key)
  8997. "Return a list of all values of property KEY."
  8998. (save-excursion
  8999. (save-restriction
  9000. (widen)
  9001. (goto-char (point-min))
  9002. (let ((re (concat "^[ \t]*:" key ":[ \t]*\\(\\S-.*\\)"))
  9003. values)
  9004. (while (re-search-forward re nil t)
  9005. (add-to-list 'values (org-trim (match-string 1))))
  9006. (delete "" values)))))
  9007. (defun org-insert-property-drawer ()
  9008. "Insert a property drawer into the current entry."
  9009. (interactive)
  9010. (org-back-to-heading t)
  9011. (looking-at outline-regexp)
  9012. (let ((indent (- (match-end 0)(match-beginning 0)))
  9013. (beg (point))
  9014. (re (concat "^[ \t]*" org-keyword-time-regexp))
  9015. end hiddenp)
  9016. (outline-next-heading)
  9017. (setq end (point))
  9018. (goto-char beg)
  9019. (while (re-search-forward re end t))
  9020. (setq hiddenp (org-invisible-p))
  9021. (end-of-line 1)
  9022. (and (equal (char-after) ?\n) (forward-char 1))
  9023. (while (looking-at "^[ \t]*\\(:CLOCK:\\|CLOCK\\|:END:\\)")
  9024. (beginning-of-line 2))
  9025. (org-skip-over-state-notes)
  9026. (skip-chars-backward " \t\n\r")
  9027. (if (eq (char-before) ?*) (forward-char 1))
  9028. (let ((inhibit-read-only t)) (insert "\n:PROPERTIES:\n:END:"))
  9029. (beginning-of-line 0)
  9030. (org-indent-to-column indent)
  9031. (beginning-of-line 2)
  9032. (org-indent-to-column indent)
  9033. (beginning-of-line 0)
  9034. (if hiddenp
  9035. (save-excursion
  9036. (org-back-to-heading t)
  9037. (hide-entry))
  9038. (org-flag-drawer t))))
  9039. (defun org-set-property (property value)
  9040. "In the current entry, set PROPERTY to VALUE.
  9041. When called interactively, this will prompt for a property name, offering
  9042. completion on existing and default properties. And then it will prompt
  9043. for a value, offering competion either on allowed values (via an inherited
  9044. xxx_ALL property) or on existing values in other instances of this property
  9045. in the current file."
  9046. (interactive
  9047. (let* ((completion-ignore-case t)
  9048. (keys (org-buffer-property-keys nil t t))
  9049. (prop0 (completing-read "Property: " (mapcar 'list keys)))
  9050. (prop (if (member prop0 keys)
  9051. prop0
  9052. (or (cdr (assoc (downcase prop0)
  9053. (mapcar (lambda (x) (cons (downcase x) x))
  9054. keys)))
  9055. prop0)))
  9056. (cur (org-entry-get nil prop))
  9057. (allowed (org-property-get-allowed-values nil prop 'table))
  9058. (existing (mapcar 'list (org-property-values prop)))
  9059. (val (if allowed
  9060. (org-completing-read "Value: " allowed nil 'req-match)
  9061. (org-completing-read
  9062. (concat "Value" (if (and cur (string-match "\\S-" cur))
  9063. (concat "[" cur "]") "")
  9064. ": ")
  9065. existing nil nil "" nil cur))))
  9066. (list prop (if (equal val "") cur val))))
  9067. (unless (equal (org-entry-get nil property) value)
  9068. (org-entry-put nil property value)))
  9069. (defun org-delete-property (property)
  9070. "In the current entry, delete PROPERTY."
  9071. (interactive
  9072. (let* ((completion-ignore-case t)
  9073. (prop (completing-read
  9074. "Property: " (org-entry-properties nil 'standard))))
  9075. (list prop)))
  9076. (message "Property %s %s" property
  9077. (if (org-entry-delete nil property)
  9078. "deleted"
  9079. "was not present in the entry")))
  9080. (defun org-delete-property-globally (property)
  9081. "Remove PROPERTY globally, from all entries."
  9082. (interactive
  9083. (let* ((completion-ignore-case t)
  9084. (prop (completing-read
  9085. "Globally remove property: "
  9086. (mapcar 'list (org-buffer-property-keys)))))
  9087. (list prop)))
  9088. (save-excursion
  9089. (save-restriction
  9090. (widen)
  9091. (goto-char (point-min))
  9092. (let ((cnt 0))
  9093. (while (re-search-forward
  9094. (concat "^[ \t]*:" (regexp-quote property) ":.*\n?")
  9095. nil t)
  9096. (setq cnt (1+ cnt))
  9097. (replace-match ""))
  9098. (message "Property \"%s\" removed from %d entries" property cnt)))))
  9099. (defvar org-columns-current-fmt-compiled) ; defined in org-colview.el
  9100. (defun org-compute-property-at-point ()
  9101. "Compute the property at point.
  9102. This looks for an enclosing column format, extracts the operator and
  9103. then applies it to the proerty in the column format's scope."
  9104. (interactive)
  9105. (unless (org-at-property-p)
  9106. (error "Not at a property"))
  9107. (let ((prop (org-match-string-no-properties 2)))
  9108. (org-columns-get-format-and-top-level)
  9109. (unless (nth 3 (assoc prop org-columns-current-fmt-compiled))
  9110. (error "No operator defined for property %s" prop))
  9111. (org-columns-compute prop)))
  9112. (defun org-property-get-allowed-values (pom property &optional table)
  9113. "Get allowed values for the property PROPERTY.
  9114. When TABLE is non-nil, return an alist that can directly be used for
  9115. completion."
  9116. (let (vals)
  9117. (cond
  9118. ((equal property "TODO")
  9119. (setq vals (org-with-point-at pom
  9120. (append org-todo-keywords-1 '("")))))
  9121. ((equal property "PRIORITY")
  9122. (let ((n org-lowest-priority))
  9123. (while (>= n org-highest-priority)
  9124. (push (char-to-string n) vals)
  9125. (setq n (1- n)))))
  9126. ((member property org-special-properties))
  9127. (t
  9128. (setq vals (org-entry-get pom (concat property "_ALL") 'inherit))
  9129. (when (and vals (string-match "\\S-" vals))
  9130. (setq vals (car (read-from-string (concat "(" vals ")"))))
  9131. (setq vals (mapcar (lambda (x)
  9132. (cond ((stringp x) x)
  9133. ((numberp x) (number-to-string x))
  9134. ((symbolp x) (symbol-name x))
  9135. (t "???")))
  9136. vals)))))
  9137. (if table (mapcar 'list vals) vals)))
  9138. (defun org-property-previous-allowed-value (&optional previous)
  9139. "Switch to the next allowed value for this property."
  9140. (interactive)
  9141. (org-property-next-allowed-value t))
  9142. (defun org-property-next-allowed-value (&optional previous)
  9143. "Switch to the next allowed value for this property."
  9144. (interactive)
  9145. (unless (org-at-property-p)
  9146. (error "Not at a property"))
  9147. (let* ((key (match-string 2))
  9148. (value (match-string 3))
  9149. (allowed (or (org-property-get-allowed-values (point) key)
  9150. (and (member value '("[ ]" "[-]" "[X]"))
  9151. '("[ ]" "[X]"))))
  9152. nval)
  9153. (unless allowed
  9154. (error "Allowed values for this property have not been defined"))
  9155. (if previous (setq allowed (reverse allowed)))
  9156. (if (member value allowed)
  9157. (setq nval (car (cdr (member value allowed)))))
  9158. (setq nval (or nval (car allowed)))
  9159. (if (equal nval value)
  9160. (error "Only one allowed value for this property"))
  9161. (org-at-property-p)
  9162. (replace-match (concat " :" key ": " nval) t t)
  9163. (org-indent-line-function)
  9164. (beginning-of-line 1)
  9165. (skip-chars-forward " \t")))
  9166. (defun org-find-entry-with-id (ident)
  9167. "Locate the entry that contains the ID property with exact value IDENT.
  9168. IDENT can be a string, a symbol or a number, this function will search for
  9169. the string representation of it.
  9170. Return the position where this entry starts, or nil if there is no such entry."
  9171. (let ((id (cond
  9172. ((stringp ident) ident)
  9173. ((symbol-name ident) (symbol-name ident))
  9174. ((numberp ident) (number-to-string ident))
  9175. (t (error "IDENT %s must be a string, symbol or number" ident))))
  9176. (case-fold-search nil))
  9177. (save-excursion
  9178. (save-restriction
  9179. (widen)
  9180. (goto-char (point-min))
  9181. (when (re-search-forward
  9182. (concat "^[ \t]*:ID:[ \t]+" (regexp-quote id) "[ \t]*$")
  9183. nil t)
  9184. (org-back-to-heading)
  9185. (point))))))
  9186. ;;;; Timestamps
  9187. (defvar org-last-changed-timestamp nil)
  9188. (defvar org-time-was-given) ; dynamically scoped parameter
  9189. (defvar org-end-time-was-given) ; dynamically scoped parameter
  9190. (defvar org-ts-what) ; dynamically scoped parameter
  9191. (defun org-time-stamp (arg)
  9192. "Prompt for a date/time and insert a time stamp.
  9193. If the user specifies a time like HH:MM, or if this command is called
  9194. with a prefix argument, the time stamp will contain date and time.
  9195. Otherwise, only the date will be included. All parts of a date not
  9196. specified by the user will be filled in from the current date/time.
  9197. So if you press just return without typing anything, the time stamp
  9198. will represent the current date/time. If there is already a timestamp
  9199. at the cursor, it will be modified."
  9200. (interactive "P")
  9201. (let* ((ts nil)
  9202. (default-time
  9203. ;; Default time is either today, or, when entering a range,
  9204. ;; the range start.
  9205. (if (or (and (org-at-timestamp-p t) (setq ts (match-string 0)))
  9206. (save-excursion
  9207. (re-search-backward
  9208. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  9209. (- (point) 20) t)))
  9210. (apply 'encode-time (org-parse-time-string (match-string 1)))
  9211. (current-time)))
  9212. (default-input (and ts (org-get-compact-tod ts)))
  9213. org-time-was-given org-end-time-was-given time)
  9214. (cond
  9215. ((and (org-at-timestamp-p)
  9216. (eq last-command 'org-time-stamp)
  9217. (eq this-command 'org-time-stamp))
  9218. (insert "--")
  9219. (setq time (let ((this-command this-command))
  9220. (org-read-date arg 'totime nil nil default-time default-input)))
  9221. (org-insert-time-stamp time (or org-time-was-given arg)))
  9222. ((org-at-timestamp-p)
  9223. (setq time (let ((this-command this-command))
  9224. (org-read-date arg 'totime nil nil default-time default-input)))
  9225. (when (org-at-timestamp-p) ; just to get the match data
  9226. (replace-match "")
  9227. (setq org-last-changed-timestamp
  9228. (org-insert-time-stamp
  9229. time (or org-time-was-given arg)
  9230. nil nil nil (list org-end-time-was-given))))
  9231. (message "Timestamp updated"))
  9232. (t
  9233. (setq time (let ((this-command this-command))
  9234. (org-read-date arg 'totime nil nil default-time default-input)))
  9235. (org-insert-time-stamp time (or org-time-was-given arg)
  9236. nil nil nil (list org-end-time-was-given))))))
  9237. ;; FIXME: can we use this for something else, like computing time differences?
  9238. (defun org-get-compact-tod (s)
  9239. (when (string-match "\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\(-\\(\\([012]?[0-9]\\):\\([0-5][0-9]\\)\\)\\)?" s)
  9240. (let* ((t1 (match-string 1 s))
  9241. (h1 (string-to-number (match-string 2 s)))
  9242. (m1 (string-to-number (match-string 3 s)))
  9243. (t2 (and (match-end 4) (match-string 5 s)))
  9244. (h2 (and t2 (string-to-number (match-string 6 s))))
  9245. (m2 (and t2 (string-to-number (match-string 7 s))))
  9246. dh dm)
  9247. (if (not t2)
  9248. t1
  9249. (setq dh (- h2 h1) dm (- m2 m1))
  9250. (if (< dm 0) (setq dm (+ dm 60) dh (1- dh)))
  9251. (concat t1 "+" (number-to-string dh)
  9252. (if (/= 0 dm) (concat ":" (number-to-string dm))))))))
  9253. (defun org-time-stamp-inactive (&optional arg)
  9254. "Insert an inactive time stamp.
  9255. An inactive time stamp is enclosed in square brackets instead of angle
  9256. brackets. It is inactive in the sense that it does not trigger agenda entries,
  9257. does not link to the calendar and cannot be changed with the S-cursor keys.
  9258. So these are more for recording a certain time/date."
  9259. (interactive "P")
  9260. (let (org-time-was-given org-end-time-was-given time)
  9261. (setq time (org-read-date arg 'totime))
  9262. (org-insert-time-stamp time (or org-time-was-given arg) 'inactive
  9263. nil nil (list org-end-time-was-given))))
  9264. (defvar org-date-ovl (org-make-overlay 1 1))
  9265. (org-overlay-put org-date-ovl 'face 'org-warning)
  9266. (org-detach-overlay org-date-ovl)
  9267. (defvar org-ans1) ; dynamically scoped parameter
  9268. (defvar org-ans2) ; dynamically scoped parameter
  9269. (defvar org-plain-time-of-day-regexp) ; defined below
  9270. (defvar org-read-date-overlay nil)
  9271. (defvar org-dcst nil) ; dynamically scoped
  9272. (defun org-read-date (&optional with-time to-time from-string prompt
  9273. default-time default-input)
  9274. "Read a date, possibly a time, and make things smooth for the user.
  9275. The prompt will suggest to enter an ISO date, but you can also enter anything
  9276. which will at least partially be understood by `parse-time-string'.
  9277. Unrecognized parts of the date will default to the current day, month, year,
  9278. hour and minute. If this command is called to replace a timestamp at point,
  9279. of to enter the second timestamp of a range, the default time is taken from the
  9280. existing stamp. For example,
  9281. 3-2-5 --> 2003-02-05
  9282. feb 15 --> currentyear-02-15
  9283. sep 12 9 --> 2009-09-12
  9284. 12:45 --> today 12:45
  9285. 22 sept 0:34 --> currentyear-09-22 0:34
  9286. 12 --> currentyear-currentmonth-12
  9287. Fri --> nearest Friday (today or later)
  9288. etc.
  9289. Furthermore you can specify a relative date by giving, as the *first* thing
  9290. in the input: a plus/minus sign, a number and a letter [dwmy] to indicate
  9291. change in days weeks, months, years.
  9292. With a single plus or minus, the date is relative to today. With a double
  9293. plus or minus, it is relative to the date in DEFAULT-TIME. E.g.
  9294. +4d --> four days from today
  9295. +4 --> same as above
  9296. +2w --> two weeks from today
  9297. ++5 --> five days from default date
  9298. The function understands only English month and weekday abbreviations,
  9299. but this can be configured with the variables `parse-time-months' and
  9300. `parse-time-weekdays'.
  9301. While prompting, a calendar is popped up - you can also select the
  9302. date with the mouse (button 1). The calendar shows a period of three
  9303. months. To scroll it to other months, use the keys `>' and `<'.
  9304. If you don't like the calendar, turn it off with
  9305. \(setq org-read-date-popup-calendar nil)
  9306. With optional argument TO-TIME, the date will immediately be converted
  9307. to an internal time.
  9308. With an optional argument WITH-TIME, the prompt will suggest to also
  9309. insert a time. Note that when WITH-TIME is not set, you can still
  9310. enter a time, and this function will inform the calling routine about
  9311. this change. The calling routine may then choose to change the format
  9312. used to insert the time stamp into the buffer to include the time.
  9313. With optional argument FROM-STRING, read from this string instead from
  9314. the user. PROMPT can overwrite the default prompt. DEFAULT-TIME is
  9315. the time/date that is used for everything that is not specified by the
  9316. user."
  9317. (require 'parse-time)
  9318. (let* ((org-time-stamp-rounding-minutes
  9319. (if (equal with-time '(16)) '(0 0) org-time-stamp-rounding-minutes))
  9320. (org-dcst org-display-custom-times)
  9321. (ct (org-current-time))
  9322. (def (or default-time ct))
  9323. (defdecode (decode-time def))
  9324. (dummy (progn
  9325. (when (< (nth 2 defdecode) org-extend-today-until)
  9326. (setcar (nthcdr 2 defdecode) -1)
  9327. (setcar (nthcdr 1 defdecode) 59)
  9328. (setq def (apply 'encode-time defdecode)
  9329. defdecode (decode-time def)))))
  9330. (calendar-move-hook nil)
  9331. (calendar-view-diary-initially-flag nil)
  9332. (view-diary-entries-initially nil)
  9333. (calendar-view-holidays-initially-flag nil)
  9334. (view-calendar-holidays-initially nil)
  9335. (timestr (format-time-string
  9336. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") def))
  9337. (prompt (concat (if prompt (concat prompt " ") "")
  9338. (format "Date+time [%s]: " timestr)))
  9339. ans (org-ans0 "") org-ans1 org-ans2 final)
  9340. (cond
  9341. (from-string (setq ans from-string))
  9342. (org-read-date-popup-calendar
  9343. (save-excursion
  9344. (save-window-excursion
  9345. (calendar)
  9346. (calendar-forward-day (- (time-to-days def)
  9347. (calendar-absolute-from-gregorian
  9348. (calendar-current-date))))
  9349. (org-eval-in-calendar nil t)
  9350. (let* ((old-map (current-local-map))
  9351. (map (copy-keymap calendar-mode-map))
  9352. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  9353. (org-defkey map (kbd "RET") 'org-calendar-select)
  9354. (org-defkey map (if (featurep 'xemacs) [button1] [mouse-1])
  9355. 'org-calendar-select-mouse)
  9356. (org-defkey map (if (featurep 'xemacs) [button2] [mouse-2])
  9357. 'org-calendar-select-mouse)
  9358. (org-defkey minibuffer-local-map [(meta shift left)]
  9359. (lambda () (interactive)
  9360. (org-eval-in-calendar '(calendar-backward-month 1))))
  9361. (org-defkey minibuffer-local-map [(meta shift right)]
  9362. (lambda () (interactive)
  9363. (org-eval-in-calendar '(calendar-forward-month 1))))
  9364. (org-defkey minibuffer-local-map [(meta shift up)]
  9365. (lambda () (interactive)
  9366. (org-eval-in-calendar '(calendar-backward-year 1))))
  9367. (org-defkey minibuffer-local-map [(meta shift down)]
  9368. (lambda () (interactive)
  9369. (org-eval-in-calendar '(calendar-forward-year 1))))
  9370. (org-defkey minibuffer-local-map [(shift up)]
  9371. (lambda () (interactive)
  9372. (org-eval-in-calendar '(calendar-backward-week 1))))
  9373. (org-defkey minibuffer-local-map [(shift down)]
  9374. (lambda () (interactive)
  9375. (org-eval-in-calendar '(calendar-forward-week 1))))
  9376. (org-defkey minibuffer-local-map [(shift left)]
  9377. (lambda () (interactive)
  9378. (org-eval-in-calendar '(calendar-backward-day 1))))
  9379. (org-defkey minibuffer-local-map [(shift right)]
  9380. (lambda () (interactive)
  9381. (org-eval-in-calendar '(calendar-forward-day 1))))
  9382. (org-defkey minibuffer-local-map ">"
  9383. (lambda () (interactive)
  9384. (org-eval-in-calendar '(scroll-calendar-left 1))))
  9385. (org-defkey minibuffer-local-map "<"
  9386. (lambda () (interactive)
  9387. (org-eval-in-calendar '(scroll-calendar-right 1))))
  9388. (unwind-protect
  9389. (progn
  9390. (use-local-map map)
  9391. (add-hook 'post-command-hook 'org-read-date-display)
  9392. (setq org-ans0 (read-string prompt default-input nil nil))
  9393. ;; org-ans0: from prompt
  9394. ;; org-ans1: from mouse click
  9395. ;; org-ans2: from calendar motion
  9396. (setq ans (concat org-ans0 " " (or org-ans1 org-ans2))))
  9397. (remove-hook 'post-command-hook 'org-read-date-display)
  9398. (use-local-map old-map)
  9399. (when org-read-date-overlay
  9400. (org-delete-overlay org-read-date-overlay)
  9401. (setq org-read-date-overlay nil)))))))
  9402. (t ; Naked prompt only
  9403. (unwind-protect
  9404. (setq ans (read-string prompt default-input nil timestr))
  9405. (when org-read-date-overlay
  9406. (org-delete-overlay org-read-date-overlay)
  9407. (setq org-read-date-overlay nil)))))
  9408. (setq final (org-read-date-analyze ans def defdecode))
  9409. (if to-time
  9410. (apply 'encode-time final)
  9411. (if (and (boundp 'org-time-was-given) org-time-was-given)
  9412. (format "%04d-%02d-%02d %02d:%02d"
  9413. (nth 5 final) (nth 4 final) (nth 3 final)
  9414. (nth 2 final) (nth 1 final))
  9415. (format "%04d-%02d-%02d" (nth 5 final) (nth 4 final) (nth 3 final))))))
  9416. (defvar def)
  9417. (defvar defdecode)
  9418. (defvar with-time)
  9419. (defun org-read-date-display ()
  9420. "Display the currrent date prompt interpretation in the minibuffer."
  9421. (when org-read-date-display-live
  9422. (when org-read-date-overlay
  9423. (org-delete-overlay org-read-date-overlay))
  9424. (let ((p (point)))
  9425. (end-of-line 1)
  9426. (while (not (equal (buffer-substring
  9427. (max (point-min) (- (point) 4)) (point))
  9428. " "))
  9429. (insert " "))
  9430. (goto-char p))
  9431. (let* ((ans (concat (buffer-substring (point-at-bol) (point-max))
  9432. " " (or org-ans1 org-ans2)))
  9433. (org-end-time-was-given nil)
  9434. (f (org-read-date-analyze ans def defdecode))
  9435. (fmts (if org-dcst
  9436. org-time-stamp-custom-formats
  9437. org-time-stamp-formats))
  9438. (fmt (if (or with-time
  9439. (and (boundp 'org-time-was-given) org-time-was-given))
  9440. (cdr fmts)
  9441. (car fmts)))
  9442. (txt (concat "=> " (format-time-string fmt (apply 'encode-time f)))))
  9443. (when (and org-end-time-was-given
  9444. (string-match org-plain-time-of-day-regexp txt))
  9445. (setq txt (concat (substring txt 0 (match-end 0)) "-"
  9446. org-end-time-was-given
  9447. (substring txt (match-end 0)))))
  9448. (setq org-read-date-overlay
  9449. (make-overlay (1- (point-at-eol)) (point-at-eol)))
  9450. (org-overlay-display org-read-date-overlay txt 'secondary-selection))))
  9451. (defun org-read-date-analyze (ans def defdecode)
  9452. "Analyze the combined answer of the date prompt."
  9453. ;; FIXME: cleanup and comment
  9454. (let (delta deltan deltaw deltadef year month day
  9455. hour minute second wday pm h2 m2 tl wday1
  9456. iso-year iso-weekday iso-week iso-year iso-date)
  9457. (when (string-match "\\`[ \t]*\\.[ \t]*\\'" ans)
  9458. (setq ans "+0"))
  9459. (when (setq delta (org-read-date-get-relative ans (current-time) def))
  9460. (setq ans (replace-match "" t t ans)
  9461. deltan (car delta)
  9462. deltaw (nth 1 delta)
  9463. deltadef (nth 2 delta)))
  9464. ;; Check if there is an iso week date in there
  9465. ;; If yes, sore the info and ostpone interpreting it until the rest
  9466. ;; of the parsing is done
  9467. (when (string-match "\\<\\(?:\\([0-9]+\\)-\\)?[wW]\\([0-9]\\{1,2\\}\\)\\(?:-\\([0-6]\\)\\)?\\([ \t]\\|$\\)" ans)
  9468. (setq iso-year (if (match-end 1) (org-small-year-to-year (string-to-number (match-string 1 ans))))
  9469. iso-weekday (if (match-end 3) (string-to-number (match-string 3 ans)))
  9470. iso-week (string-to-number (match-string 2 ans)))
  9471. (setq ans (replace-match "" t t ans)))
  9472. ;; Help matching ISO dates with single digit month ot day, like 2006-8-11.
  9473. (when (string-match
  9474. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  9475. (setq year (if (match-end 2)
  9476. (string-to-number (match-string 2 ans))
  9477. (string-to-number (format-time-string "%Y")))
  9478. month (string-to-number (match-string 3 ans))
  9479. day (string-to-number (match-string 4 ans)))
  9480. (if (< year 100) (setq year (+ 2000 year)))
  9481. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  9482. t nil ans)))
  9483. ;; Help matching am/pm times, because `parse-time-string' does not do that.
  9484. ;; If there is a time with am/pm, and *no* time without it, we convert
  9485. ;; so that matching will be successful.
  9486. (loop for i from 1 to 2 do ; twice, for end time as well
  9487. (when (and (not (string-match "\\(\\`\\|[^+]\\)[012]?[0-9]:[0-9][0-9]\\([ \t\n]\\|$\\)" ans))
  9488. (string-match "\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\(am\\|AM\\|pm\\|PM\\)\\>" ans))
  9489. (setq hour (string-to-number (match-string 1 ans))
  9490. minute (if (match-end 3)
  9491. (string-to-number (match-string 3 ans))
  9492. 0)
  9493. pm (equal ?p
  9494. (string-to-char (downcase (match-string 4 ans)))))
  9495. (if (and (= hour 12) (not pm))
  9496. (setq hour 0)
  9497. (if (and pm (< hour 12)) (setq hour (+ 12 hour))))
  9498. (setq ans (replace-match (format "%02d:%02d" hour minute)
  9499. t t ans))))
  9500. ;; Check if a time range is given as a duration
  9501. (when (string-match "\\([012]?[0-9]\\):\\([0-6][0-9]\\)\\+\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?" ans)
  9502. (setq hour (string-to-number (match-string 1 ans))
  9503. h2 (+ hour (string-to-number (match-string 3 ans)))
  9504. minute (string-to-number (match-string 2 ans))
  9505. m2 (+ minute (if (match-end 5) (string-to-number
  9506. (match-string 5 ans))0)))
  9507. (if (>= m2 60) (setq h2 (1+ h2) m2 (- m2 60)))
  9508. (setq ans (replace-match (format "%02d:%02d-%02d:%02d" hour minute h2 m2)
  9509. t t ans)))
  9510. ;; Check if there is a time range
  9511. (when (boundp 'org-end-time-was-given)
  9512. (setq org-time-was-given nil)
  9513. (when (and (string-match org-plain-time-of-day-regexp ans)
  9514. (match-end 8))
  9515. (setq org-end-time-was-given (match-string 8 ans))
  9516. (setq ans (concat (substring ans 0 (match-beginning 7))
  9517. (substring ans (match-end 7))))))
  9518. (setq tl (parse-time-string ans)
  9519. day (or (nth 3 tl) (nth 3 defdecode))
  9520. month (or (nth 4 tl)
  9521. (if (and org-read-date-prefer-future
  9522. (nth 3 tl) (< (nth 3 tl) (nth 3 defdecode)))
  9523. (1+ (nth 4 defdecode))
  9524. (nth 4 defdecode)))
  9525. year (or (nth 5 tl)
  9526. (if (and org-read-date-prefer-future
  9527. (nth 4 tl) (< (nth 4 tl) (nth 4 defdecode)))
  9528. (1+ (nth 5 defdecode))
  9529. (nth 5 defdecode)))
  9530. hour (or (nth 2 tl) (nth 2 defdecode))
  9531. minute (or (nth 1 tl) (nth 1 defdecode))
  9532. second (or (nth 0 tl) 0)
  9533. wday (nth 6 tl))
  9534. ;; Special date definitions below
  9535. (cond
  9536. (iso-week
  9537. ;; There was an iso week
  9538. (setq year (or iso-year year)
  9539. day (or iso-weekday wday 1)
  9540. wday nil ; to make sure that the trigger below does not match
  9541. iso-date (calendar-gregorian-from-absolute
  9542. (calendar-absolute-from-iso
  9543. (list iso-week day year))))
  9544. ; FIXME: Should we also push ISO weeks into the future?
  9545. ; (when (and org-read-date-prefer-future
  9546. ; (not iso-year)
  9547. ; (< (calendar-absolute-from-gregorian iso-date)
  9548. ; (time-to-days (current-time))))
  9549. ; (setq year (1+ year)
  9550. ; iso-date (calendar-gregorian-from-absolute
  9551. ; (calendar-absolute-from-iso
  9552. ; (list iso-week day year)))))
  9553. (setq month (car iso-date)
  9554. year (nth 2 iso-date)
  9555. day (nth 1 iso-date)))
  9556. (deltan
  9557. (unless deltadef
  9558. (let ((now (decode-time (current-time))))
  9559. (setq day (nth 3 now) month (nth 4 now) year (nth 5 now))))
  9560. (cond ((member deltaw '("d" "")) (setq day (+ day deltan)))
  9561. ((equal deltaw "w") (setq day (+ day (* 7 deltan))))
  9562. ((equal deltaw "m") (setq month (+ month deltan)))
  9563. ((equal deltaw "y") (setq year (+ year deltan)))))
  9564. ((and wday (not (nth 3 tl)))
  9565. ;; Weekday was given, but no day, so pick that day in the week
  9566. ;; on or after the derived date.
  9567. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  9568. (unless (equal wday wday1)
  9569. (setq day (+ day (% (- wday wday1 -7) 7))))))
  9570. (if (and (boundp 'org-time-was-given)
  9571. (nth 2 tl))
  9572. (setq org-time-was-given t))
  9573. (if (< year 100) (setq year (+ 2000 year)))
  9574. (if (< year 1970) (setq year (nth 5 defdecode))) ; not representable
  9575. (list second minute hour day month year)))
  9576. (defvar parse-time-weekdays)
  9577. (defun org-read-date-get-relative (s today default)
  9578. "Check string S for special relative date string.
  9579. TODAY and DEFAULT are internal times, for today and for a default.
  9580. Return shift list (N what def-flag)
  9581. WHAT is \"d\", \"w\", \"m\", or \"y\" for day, week, month, year.
  9582. N is the number of WHATs to shift.
  9583. DEF-FLAG is t when a double ++ or -- indicates shift relative to
  9584. the DEFAULT date rather than TODAY."
  9585. (when (and
  9586. (string-match
  9587. (concat
  9588. "\\`[ \t]*\\([-+]\\{0,2\\}\\)"
  9589. "\\([0-9]+\\)?"
  9590. "\\([dwmy]\\|\\(" (mapconcat 'car parse-time-weekdays "\\|") "\\)\\)?"
  9591. "\\([ \t]\\|$\\)") s)
  9592. (or (> (match-end 1) (match-beginning 1)) (match-end 4)))
  9593. (let* ((dir (if (> (match-end 1) (match-beginning 1))
  9594. (string-to-char (substring (match-string 1 s) -1))
  9595. ?+))
  9596. (rel (and (match-end 1) (= 2 (- (match-end 1) (match-beginning 1)))))
  9597. (n (if (match-end 2) (string-to-number (match-string 2 s)) 1))
  9598. (what (if (match-end 3) (match-string 3 s) "d"))
  9599. (wday1 (cdr (assoc (downcase what) parse-time-weekdays)))
  9600. (date (if rel default today))
  9601. (wday (nth 6 (decode-time date)))
  9602. delta)
  9603. (if wday1
  9604. (progn
  9605. (setq delta (mod (+ 7 (- wday1 wday)) 7))
  9606. (if (= dir ?-) (setq delta (- delta 7)))
  9607. (if (> n 1) (setq delta (+ delta (* (1- n) (if (= dir ?-) -7 7)))))
  9608. (list delta "d" rel))
  9609. (list (* n (if (= dir ?-) -1 1)) what rel)))))
  9610. (defun org-eval-in-calendar (form &optional keepdate)
  9611. "Eval FORM in the calendar window and return to current window.
  9612. Also, store the cursor date in variable org-ans2."
  9613. (let ((sw (selected-window)))
  9614. (select-window (get-buffer-window "*Calendar*"))
  9615. (eval form)
  9616. (when (and (not keepdate) (calendar-cursor-to-date))
  9617. (let* ((date (calendar-cursor-to-date))
  9618. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9619. (setq org-ans2 (format-time-string "%Y-%m-%d" time))))
  9620. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  9621. (select-window sw)))
  9622. ; ;; Update the prompt to show new default date
  9623. ; (save-excursion
  9624. ; (goto-char (point-min))
  9625. ; (when (and org-ans2
  9626. ; (re-search-forward "\\[[-0-9]+\\]" nil t)
  9627. ; (get-text-property (match-end 0) 'field))
  9628. ; (let ((inhibit-read-only t))
  9629. ; (replace-match (concat "[" org-ans2 "]") t t)
  9630. ; (add-text-properties (point-min) (1+ (match-end 0))
  9631. ; (text-properties-at (1+ (point-min)))))))))
  9632. (defun org-calendar-select ()
  9633. "Return to `org-read-date' with the date currently selected.
  9634. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9635. (interactive)
  9636. (when (calendar-cursor-to-date)
  9637. (let* ((date (calendar-cursor-to-date))
  9638. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9639. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9640. (if (active-minibuffer-window) (exit-minibuffer))))
  9641. (defun org-insert-time-stamp (time &optional with-hm inactive pre post extra)
  9642. "Insert a date stamp for the date given by the internal TIME.
  9643. WITH-HM means, use the stamp format that includes the time of the day.
  9644. INACTIVE means use square brackets instead of angular ones, so that the
  9645. stamp will not contribute to the agenda.
  9646. PRE and POST are optional strings to be inserted before and after the
  9647. stamp.
  9648. The command returns the inserted time stamp."
  9649. (let ((fmt (funcall (if with-hm 'cdr 'car) org-time-stamp-formats))
  9650. stamp)
  9651. (if inactive (setq fmt (concat "[" (substring fmt 1 -1) "]")))
  9652. (insert-before-markers (or pre ""))
  9653. (insert-before-markers (setq stamp (format-time-string fmt time)))
  9654. (when (listp extra)
  9655. (setq extra (car extra))
  9656. (if (and (stringp extra)
  9657. (string-match "\\([0-9]+\\):\\([0-9]+\\)" extra))
  9658. (setq extra (format "-%02d:%02d"
  9659. (string-to-number (match-string 1 extra))
  9660. (string-to-number (match-string 2 extra))))
  9661. (setq extra nil)))
  9662. (when extra
  9663. (backward-char 1)
  9664. (insert-before-markers extra)
  9665. (forward-char 1))
  9666. (insert-before-markers (or post ""))
  9667. stamp))
  9668. (defun org-toggle-time-stamp-overlays ()
  9669. "Toggle the use of custom time stamp formats."
  9670. (interactive)
  9671. (setq org-display-custom-times (not org-display-custom-times))
  9672. (unless org-display-custom-times
  9673. (let ((p (point-min)) (bmp (buffer-modified-p)))
  9674. (while (setq p (next-single-property-change p 'display))
  9675. (if (and (get-text-property p 'display)
  9676. (eq (get-text-property p 'face) 'org-date))
  9677. (remove-text-properties
  9678. p (setq p (next-single-property-change p 'display))
  9679. '(display t))))
  9680. (set-buffer-modified-p bmp)))
  9681. (if (featurep 'xemacs)
  9682. (remove-text-properties (point-min) (point-max) '(end-glyph t)))
  9683. (org-restart-font-lock)
  9684. (setq org-table-may-need-update t)
  9685. (if org-display-custom-times
  9686. (message "Time stamps are overlayed with custom format")
  9687. (message "Time stamp overlays removed")))
  9688. (defun org-display-custom-time (beg end)
  9689. "Overlay modified time stamp format over timestamp between BEG and END."
  9690. (let* ((ts (buffer-substring beg end))
  9691. t1 w1 with-hm tf time str w2 (off 0))
  9692. (save-match-data
  9693. (setq t1 (org-parse-time-string ts t))
  9694. (if (string-match "\\(-[0-9]+:[0-9]+\\)?\\( [.+]?\\+[0-9]+[dwmy]\\)?\\'" ts)
  9695. (setq off (- (match-end 0) (match-beginning 0)))))
  9696. (setq end (- end off))
  9697. (setq w1 (- end beg)
  9698. with-hm (and (nth 1 t1) (nth 2 t1))
  9699. tf (funcall (if with-hm 'cdr 'car) org-time-stamp-custom-formats)
  9700. time (org-fix-decoded-time t1)
  9701. str (org-add-props
  9702. (format-time-string
  9703. (substring tf 1 -1) (apply 'encode-time time))
  9704. nil 'mouse-face 'highlight)
  9705. w2 (length str))
  9706. (if (not (= w2 w1))
  9707. (add-text-properties (1+ beg) (+ 2 beg)
  9708. (list 'org-dwidth t 'org-dwidth-n (- w1 w2))))
  9709. (if (featurep 'xemacs)
  9710. (progn
  9711. (put-text-property beg end 'invisible t)
  9712. (put-text-property beg end 'end-glyph (make-glyph str)))
  9713. (put-text-property beg end 'display str))))
  9714. (defun org-translate-time (string)
  9715. "Translate all timestamps in STRING to custom format.
  9716. But do this only if the variable `org-display-custom-times' is set."
  9717. (when org-display-custom-times
  9718. (save-match-data
  9719. (let* ((start 0)
  9720. (re org-ts-regexp-both)
  9721. t1 with-hm inactive tf time str beg end)
  9722. (while (setq start (string-match re string start))
  9723. (setq beg (match-beginning 0)
  9724. end (match-end 0)
  9725. t1 (save-match-data
  9726. (org-parse-time-string (substring string beg end) t))
  9727. with-hm (and (nth 1 t1) (nth 2 t1))
  9728. inactive (equal (substring string beg (1+ beg)) "[")
  9729. tf (funcall (if with-hm 'cdr 'car)
  9730. org-time-stamp-custom-formats)
  9731. time (org-fix-decoded-time t1)
  9732. str (format-time-string
  9733. (concat
  9734. (if inactive "[" "<") (substring tf 1 -1)
  9735. (if inactive "]" ">"))
  9736. (apply 'encode-time time))
  9737. string (replace-match str t t string)
  9738. start (+ start (length str)))))))
  9739. string)
  9740. (defun org-fix-decoded-time (time)
  9741. "Set 0 instead of nil for the first 6 elements of time.
  9742. Don't touch the rest."
  9743. (let ((n 0))
  9744. (mapcar (lambda (x) (if (< (setq n (1+ n)) 7) (or x 0) x)) time)))
  9745. (defun org-days-to-time (timestamp-string)
  9746. "Difference between TIMESTAMP-STRING and now in days."
  9747. (- (time-to-days (org-time-string-to-time timestamp-string))
  9748. (time-to-days (current-time))))
  9749. (defun org-deadline-close (timestamp-string &optional ndays)
  9750. "Is the time in TIMESTAMP-STRING close to the current date?"
  9751. (setq ndays (or ndays (org-get-wdays timestamp-string)))
  9752. (and (< (org-days-to-time timestamp-string) ndays)
  9753. (not (org-entry-is-done-p))))
  9754. (defun org-get-wdays (ts)
  9755. "Get the deadline lead time appropriate for timestring TS."
  9756. (cond
  9757. ((<= org-deadline-warning-days 0)
  9758. ;; 0 or negative, enforce this value no matter what
  9759. (- org-deadline-warning-days))
  9760. ((string-match "-\\([0-9]+\\)\\([dwmy]\\)\\(\\'\\|>\\)" ts)
  9761. ;; lead time is specified.
  9762. (floor (* (string-to-number (match-string 1 ts))
  9763. (cdr (assoc (match-string 2 ts)
  9764. '(("d" . 1) ("w" . 7)
  9765. ("m" . 30.4) ("y" . 365.25)))))))
  9766. ;; go for the default.
  9767. (t org-deadline-warning-days)))
  9768. (defun org-calendar-select-mouse (ev)
  9769. "Return to `org-read-date' with the date currently selected.
  9770. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  9771. (interactive "e")
  9772. (mouse-set-point ev)
  9773. (when (calendar-cursor-to-date)
  9774. (let* ((date (calendar-cursor-to-date))
  9775. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  9776. (setq org-ans1 (format-time-string "%Y-%m-%d" time)))
  9777. (if (active-minibuffer-window) (exit-minibuffer))))
  9778. (defun org-check-deadlines (ndays)
  9779. "Check if there are any deadlines due or past due.
  9780. A deadline is considered due if it happens within `org-deadline-warning-days'
  9781. days from today's date. If the deadline appears in an entry marked DONE,
  9782. it is not shown. The prefix arg NDAYS can be used to test that many
  9783. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  9784. (interactive "P")
  9785. (let* ((org-warn-days
  9786. (cond
  9787. ((equal ndays '(4)) 100000)
  9788. (ndays (prefix-numeric-value ndays))
  9789. (t (abs org-deadline-warning-days))))
  9790. (case-fold-search nil)
  9791. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  9792. (callback
  9793. (lambda () (org-deadline-close (match-string 1) org-warn-days))))
  9794. (message "%d deadlines past-due or due within %d days"
  9795. (org-occur regexp nil callback)
  9796. org-warn-days)))
  9797. (defun org-check-before-date (date)
  9798. "Check if there are deadlines or scheduled entries before DATE."
  9799. (interactive (list (org-read-date)))
  9800. (let ((case-fold-search nil)
  9801. (regexp (concat "\\<\\(" org-deadline-string
  9802. "\\|" org-scheduled-string
  9803. "\\) *<\\([^>]+\\)>"))
  9804. (callback
  9805. (lambda () (time-less-p
  9806. (org-time-string-to-time (match-string 2))
  9807. (org-time-string-to-time date)))))
  9808. (message "%d entries before %s"
  9809. (org-occur regexp nil callback) date)))
  9810. (defun org-evaluate-time-range (&optional to-buffer)
  9811. "Evaluate a time range by computing the difference between start and end.
  9812. Normally the result is just printed in the echo area, but with prefix arg
  9813. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  9814. If the time range is actually in a table, the result is inserted into the
  9815. next column.
  9816. For time difference computation, a year is assumed to be exactly 365
  9817. days in order to avoid rounding problems."
  9818. (interactive "P")
  9819. (or
  9820. (org-clock-update-time-maybe)
  9821. (save-excursion
  9822. (unless (org-at-date-range-p t)
  9823. (goto-char (point-at-bol))
  9824. (re-search-forward org-tr-regexp-both (point-at-eol) t))
  9825. (if (not (org-at-date-range-p t))
  9826. (error "Not at a time-stamp range, and none found in current line")))
  9827. (let* ((ts1 (match-string 1))
  9828. (ts2 (match-string 2))
  9829. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  9830. (match-end (match-end 0))
  9831. (time1 (org-time-string-to-time ts1))
  9832. (time2 (org-time-string-to-time ts2))
  9833. (t1 (time-to-seconds time1))
  9834. (t2 (time-to-seconds time2))
  9835. (diff (abs (- t2 t1)))
  9836. (negative (< (- t2 t1) 0))
  9837. ;; (ys (floor (* 365 24 60 60)))
  9838. (ds (* 24 60 60))
  9839. (hs (* 60 60))
  9840. (fy "%dy %dd %02d:%02d")
  9841. (fy1 "%dy %dd")
  9842. (fd "%dd %02d:%02d")
  9843. (fd1 "%dd")
  9844. (fh "%02d:%02d")
  9845. y d h m align)
  9846. (if havetime
  9847. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9848. y 0
  9849. d (floor (/ diff ds)) diff (mod diff ds)
  9850. h (floor (/ diff hs)) diff (mod diff hs)
  9851. m (floor (/ diff 60)))
  9852. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  9853. y 0
  9854. d (floor (+ (/ diff ds) 0.5))
  9855. h 0 m 0))
  9856. (if (not to-buffer)
  9857. (message "%s" (org-make-tdiff-string y d h m))
  9858. (if (org-at-table-p)
  9859. (progn
  9860. (goto-char match-end)
  9861. (setq align t)
  9862. (and (looking-at " *|") (goto-char (match-end 0))))
  9863. (goto-char match-end))
  9864. (if (looking-at
  9865. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  9866. (replace-match ""))
  9867. (if negative (insert " -"))
  9868. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  9869. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  9870. (insert " " (format fh h m))))
  9871. (if align (org-table-align))
  9872. (message "Time difference inserted")))))
  9873. (defun org-make-tdiff-string (y d h m)
  9874. (let ((fmt "")
  9875. (l nil))
  9876. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  9877. l (push y l)))
  9878. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  9879. l (push d l)))
  9880. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  9881. l (push h l)))
  9882. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  9883. l (push m l)))
  9884. (apply 'format fmt (nreverse l))))
  9885. (defun org-time-string-to-time (s)
  9886. (apply 'encode-time (org-parse-time-string s)))
  9887. (defun org-time-string-to-absolute (s &optional daynr prefer show-all)
  9888. "Convert a time stamp to an absolute day number.
  9889. If there is a specifyer for a cyclic time stamp, get the closest date to
  9890. DAYNR.
  9891. PREFER and SHOW_ALL are passed through to `org-closest-date'."
  9892. (cond
  9893. ((and daynr (string-match "\\`%%\\((.*)\\)" s))
  9894. (if (org-diary-sexp-entry (match-string 1 s) "" date)
  9895. daynr
  9896. (+ daynr 1000)))
  9897. ((and daynr (string-match "\\+[0-9]+[dwmy]" s))
  9898. (org-closest-date s (if (and (boundp 'daynr) (integerp daynr)) daynr
  9899. (time-to-days (current-time))) (match-string 0 s)
  9900. prefer show-all))
  9901. (t (time-to-days (apply 'encode-time (org-parse-time-string s))))))
  9902. (defun org-days-to-iso-week (days)
  9903. "Return the iso week number."
  9904. (require 'cal-iso)
  9905. (car (calendar-iso-from-absolute days)))
  9906. (defun org-small-year-to-year (year)
  9907. "Convert 2-digit years into 4-digit years.
  9908. 38-99 are mapped into 1938-1999. 1-37 are mapped into 2001-2007.
  9909. The year 2000 cannot be abbreviated. Any year lager than 99
  9910. is retrned unchanged."
  9911. (if (< year 38)
  9912. (setq year (+ 2000 year))
  9913. (if (< year 100)
  9914. (setq year (+ 1900 year))))
  9915. year)
  9916. (defun org-time-from-absolute (d)
  9917. "Return the time corresponding to date D.
  9918. D may be an absolute day number, or a calendar-type list (month day year)."
  9919. (if (numberp d) (setq d (calendar-gregorian-from-absolute d)))
  9920. (encode-time 0 0 0 (nth 1 d) (car d) (nth 2 d)))
  9921. (defun org-calendar-holiday ()
  9922. "List of holidays, for Diary display in Org-mode."
  9923. (require 'holidays)
  9924. (let ((hl (funcall
  9925. (if (fboundp 'calendar-check-holidays)
  9926. 'calendar-check-holidays 'check-calendar-holidays) date)))
  9927. (if hl (mapconcat 'identity hl "; "))))
  9928. (defun org-diary-sexp-entry (sexp entry date)
  9929. "Process a SEXP diary ENTRY for DATE."
  9930. (require 'diary-lib)
  9931. (let ((result (if calendar-debug-sexp
  9932. (let ((stack-trace-on-error t))
  9933. (eval (car (read-from-string sexp))))
  9934. (condition-case nil
  9935. (eval (car (read-from-string sexp)))
  9936. (error
  9937. (beep)
  9938. (message "Bad sexp at line %d in %s: %s"
  9939. (org-current-line)
  9940. (buffer-file-name) sexp)
  9941. (sleep-for 2))))))
  9942. (cond ((stringp result) result)
  9943. ((and (consp result)
  9944. (stringp (cdr result))) (cdr result))
  9945. (result entry)
  9946. (t nil))))
  9947. (defun org-diary-to-ical-string (frombuf)
  9948. "Get iCalendar entries from diary entries in buffer FROMBUF.
  9949. This uses the icalendar.el library."
  9950. (let* ((tmpdir (if (featurep 'xemacs)
  9951. (temp-directory)
  9952. temporary-file-directory))
  9953. (tmpfile (make-temp-name
  9954. (expand-file-name "orgics" tmpdir)))
  9955. buf rtn b e)
  9956. (save-excursion
  9957. (set-buffer frombuf)
  9958. (icalendar-export-region (point-min) (point-max) tmpfile)
  9959. (setq buf (find-buffer-visiting tmpfile))
  9960. (set-buffer buf)
  9961. (goto-char (point-min))
  9962. (if (re-search-forward "^BEGIN:VEVENT" nil t)
  9963. (setq b (match-beginning 0)))
  9964. (goto-char (point-max))
  9965. (if (re-search-backward "^END:VEVENT" nil t)
  9966. (setq e (match-end 0)))
  9967. (setq rtn (if (and b e) (concat (buffer-substring b e) "\n") "")))
  9968. (kill-buffer buf)
  9969. (kill-buffer frombuf)
  9970. (delete-file tmpfile)
  9971. rtn))
  9972. (defun org-closest-date (start current change prefer show-all)
  9973. "Find the date closest to CURRENT that is consistent with START and CHANGE.
  9974. When PREFER is `past' return a date that is either CURRENT or past.
  9975. When PREFER is `future', return a date that is either CURRENT or future.
  9976. When SHOW-ALL is nil, only return the current occurence of a time stamp."
  9977. ;; Make the proper lists from the dates
  9978. (catch 'exit
  9979. (let ((a1 '(("d" . day) ("w" . week) ("m" . month) ("y" . year)))
  9980. dn dw sday cday n1 n2
  9981. d m y y1 y2 date1 date2 nmonths nm ny m2)
  9982. (setq start (org-date-to-gregorian start)
  9983. current (org-date-to-gregorian
  9984. (if show-all
  9985. current
  9986. (time-to-days (current-time))))
  9987. sday (calendar-absolute-from-gregorian start)
  9988. cday (calendar-absolute-from-gregorian current))
  9989. (if (<= cday sday) (throw 'exit sday))
  9990. (if (string-match "\\(\\+[0-9]+\\)\\([dwmy]\\)" change)
  9991. (setq dn (string-to-number (match-string 1 change))
  9992. dw (cdr (assoc (match-string 2 change) a1)))
  9993. (error "Invalid change specifyer: %s" change))
  9994. (if (eq dw 'week) (setq dw 'day dn (* 7 dn)))
  9995. (cond
  9996. ((eq dw 'day)
  9997. (setq n1 (+ sday (* dn (floor (/ (- cday sday) dn))))
  9998. n2 (+ n1 dn)))
  9999. ((eq dw 'year)
  10000. (setq d (nth 1 start) m (car start) y1 (nth 2 start) y2 (nth 2 current))
  10001. (setq y1 (+ (* (floor (/ (- y2 y1) dn)) dn) y1))
  10002. (setq date1 (list m d y1)
  10003. n1 (calendar-absolute-from-gregorian date1)
  10004. date2 (list m d (+ y1 (* (if (< n1 cday) 1 -1) dn)))
  10005. n2 (calendar-absolute-from-gregorian date2)))
  10006. ((eq dw 'month)
  10007. ;; approx number of month between the tow dates
  10008. (setq nmonths (floor (/ (- cday sday) 30.436875)))
  10009. ;; How often does dn fit in there?
  10010. (setq d (nth 1 start) m (car start) y (nth 2 start)
  10011. nm (* dn (max 0 (1- (floor (/ nmonths dn)))))
  10012. m (+ m nm)
  10013. ny (floor (/ m 12))
  10014. y (+ y ny)
  10015. m (- m (* ny 12)))
  10016. (while (> m 12) (setq m (- m 12) y (1+ y)))
  10017. (setq n1 (calendar-absolute-from-gregorian (list m d y)))
  10018. (setq m2 (+ m dn) y2 y)
  10019. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10020. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2)))
  10021. (while (< n2 cday)
  10022. (setq n1 n2 m m2 y y2)
  10023. (setq m2 (+ m dn) y2 y)
  10024. (if (> m2 12) (setq y2 (1+ y2) m2 (- m2 12)))
  10025. (setq n2 (calendar-absolute-from-gregorian (list m2 d y2))))))
  10026. (if show-all
  10027. (cond
  10028. ((eq prefer 'past) n1)
  10029. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10030. (t (if (> (abs (- cday n1)) (abs (- cday n2))) n2 n1)))
  10031. (cond
  10032. ((eq prefer 'past) n1)
  10033. ((eq prefer 'future) (if (= cday n1) n1 n2))
  10034. (t (if (= cday n1) n1 n2)))))))
  10035. (defun org-date-to-gregorian (date)
  10036. "Turn any specification of DATE into a gregorian date for the calendar."
  10037. (cond ((integerp date) (calendar-gregorian-from-absolute date))
  10038. ((and (listp date) (= (length date) 3)) date)
  10039. ((stringp date)
  10040. (setq date (org-parse-time-string date))
  10041. (list (nth 4 date) (nth 3 date) (nth 5 date)))
  10042. ((listp date)
  10043. (list (nth 4 date) (nth 3 date) (nth 5 date)))))
  10044. (defun org-parse-time-string (s &optional nodefault)
  10045. "Parse the standard Org-mode time string.
  10046. This should be a lot faster than the normal `parse-time-string'.
  10047. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  10048. hour and minute fields will be nil if not given."
  10049. (if (string-match org-ts-regexp0 s)
  10050. (list 0
  10051. (if (or (match-beginning 8) (not nodefault))
  10052. (string-to-number (or (match-string 8 s) "0")))
  10053. (if (or (match-beginning 7) (not nodefault))
  10054. (string-to-number (or (match-string 7 s) "0")))
  10055. (string-to-number (match-string 4 s))
  10056. (string-to-number (match-string 3 s))
  10057. (string-to-number (match-string 2 s))
  10058. nil nil nil)
  10059. (make-list 9 0)))
  10060. (defun org-timestamp-up (&optional arg)
  10061. "Increase the date item at the cursor by one.
  10062. If the cursor is on the year, change the year. If it is on the month or
  10063. the day, change that.
  10064. With prefix ARG, change by that many units."
  10065. (interactive "p")
  10066. (org-timestamp-change (prefix-numeric-value arg)))
  10067. (defun org-timestamp-down (&optional arg)
  10068. "Decrease the date item at the cursor by one.
  10069. If the cursor is on the year, change the year. If it is on the month or
  10070. the day, change that.
  10071. With prefix ARG, change by that many units."
  10072. (interactive "p")
  10073. (org-timestamp-change (- (prefix-numeric-value arg))))
  10074. (defun org-timestamp-up-day (&optional arg)
  10075. "Increase the date in the time stamp by one day.
  10076. With prefix ARG, change that many days."
  10077. (interactive "p")
  10078. (if (and (not (org-at-timestamp-p t))
  10079. (org-on-heading-p))
  10080. (org-todo 'up)
  10081. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  10082. (defun org-timestamp-down-day (&optional arg)
  10083. "Decrease the date in the time stamp by one day.
  10084. With prefix ARG, change that many days."
  10085. (interactive "p")
  10086. (if (and (not (org-at-timestamp-p t))
  10087. (org-on-heading-p))
  10088. (org-todo 'down)
  10089. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  10090. (defun org-at-timestamp-p (&optional inactive-ok)
  10091. "Determine if the cursor is in or at a timestamp."
  10092. (interactive)
  10093. (let* ((tsr (if inactive-ok org-ts-regexp3 org-ts-regexp2))
  10094. (pos (point))
  10095. (ans (or (looking-at tsr)
  10096. (save-excursion
  10097. (skip-chars-backward "^[<\n\r\t")
  10098. (if (> (point) (point-min)) (backward-char 1))
  10099. (and (looking-at tsr)
  10100. (> (- (match-end 0) pos) -1))))))
  10101. (and ans
  10102. (boundp 'org-ts-what)
  10103. (setq org-ts-what
  10104. (cond
  10105. ((= pos (match-beginning 0)) 'bracket)
  10106. ((= pos (1- (match-end 0))) 'bracket)
  10107. ((org-pos-in-match-range pos 2) 'year)
  10108. ((org-pos-in-match-range pos 3) 'month)
  10109. ((org-pos-in-match-range pos 7) 'hour)
  10110. ((org-pos-in-match-range pos 8) 'minute)
  10111. ((or (org-pos-in-match-range pos 4)
  10112. (org-pos-in-match-range pos 5)) 'day)
  10113. ((and (> pos (or (match-end 8) (match-end 5)))
  10114. (< pos (match-end 0)))
  10115. (- pos (or (match-end 8) (match-end 5))))
  10116. (t 'day))))
  10117. ans))
  10118. (defun org-toggle-timestamp-type ()
  10119. "Toggle the type (<active> or [inactive]) of a time stamp."
  10120. (interactive)
  10121. (when (org-at-timestamp-p t)
  10122. (save-excursion
  10123. (goto-char (match-beginning 0))
  10124. (insert (if (equal (char-after) ?<) "[" "<")) (delete-char 1)
  10125. (goto-char (1- (match-end 0)))
  10126. (insert (if (equal (char-after) ?>) "]" ">")) (delete-char 1))
  10127. (message "Timestamp is now %sactive"
  10128. (if (equal (char-before) ?>) "in" ""))))
  10129. (defun org-timestamp-change (n &optional what)
  10130. "Change the date in the time stamp at point.
  10131. The date will be changed by N times WHAT. WHAT can be `day', `month',
  10132. `year', `minute', `second'. If WHAT is not given, the cursor position
  10133. in the timestamp determines what will be changed."
  10134. (let ((pos (point))
  10135. with-hm inactive
  10136. (dm (max (nth 1 org-time-stamp-rounding-minutes) 1))
  10137. org-ts-what
  10138. extra rem
  10139. ts time time0)
  10140. (if (not (org-at-timestamp-p t))
  10141. (error "Not at a timestamp"))
  10142. (if (and (not what) (eq org-ts-what 'bracket))
  10143. (org-toggle-timestamp-type)
  10144. (if (and (not what) (not (eq org-ts-what 'day))
  10145. org-display-custom-times
  10146. (get-text-property (point) 'display)
  10147. (not (get-text-property (1- (point)) 'display)))
  10148. (setq org-ts-what 'day))
  10149. (setq org-ts-what (or what org-ts-what)
  10150. inactive (= (char-after (match-beginning 0)) ?\[)
  10151. ts (match-string 0))
  10152. (replace-match "")
  10153. (if (string-match
  10154. "\\(\\(-[012][0-9]:[0-5][0-9]\\)?\\( +[.+]?[-+][0-9]+[dwmy]\\)*\\)[]>]"
  10155. ts)
  10156. (setq extra (match-string 1 ts)))
  10157. (if (string-match "^.\\{10\\}.*?[0-9]+:[0-9][0-9]" ts)
  10158. (setq with-hm t))
  10159. (setq time0 (org-parse-time-string ts))
  10160. (when (and (eq org-ts-what 'minute)
  10161. (eq current-prefix-arg nil))
  10162. (setq n (* dm (cond ((> n 0) 1) ((< n 0) -1) (t 0))))
  10163. (when (not (= 0 (setq rem (% (nth 1 time0) dm))))
  10164. (setcar (cdr time0) (+ (nth 1 time0)
  10165. (if (> n 0) (- rem) (- dm rem))))))
  10166. (setq time
  10167. (encode-time (or (car time0) 0)
  10168. (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0))
  10169. (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0))
  10170. (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0))
  10171. (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0))
  10172. (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0))
  10173. (nthcdr 6 time0)))
  10174. (when (integerp org-ts-what)
  10175. (setq extra (org-modify-ts-extra extra org-ts-what n dm)))
  10176. (if (eq what 'calendar)
  10177. (let ((cal-date (org-get-date-from-calendar)))
  10178. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  10179. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  10180. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  10181. (setcar time0 (or (car time0) 0))
  10182. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  10183. (setcar (nthcdr 2 time0) (or (nth 2 time0) 0))
  10184. (setq time (apply 'encode-time time0))))
  10185. (setq org-last-changed-timestamp
  10186. (org-insert-time-stamp time with-hm inactive nil nil extra))
  10187. (org-clock-update-time-maybe)
  10188. (goto-char pos)
  10189. ;; Try to recenter the calendar window, if any
  10190. (if (and org-calendar-follow-timestamp-change
  10191. (get-buffer-window "*Calendar*" t)
  10192. (memq org-ts-what '(day month year)))
  10193. (org-recenter-calendar (time-to-days time))))))
  10194. (defun org-modify-ts-extra (s pos n dm)
  10195. "Change the different parts of the lead-time and repeat fields in timestamp."
  10196. (let ((idx '(("d" . 0) ("w" . 1) ("m" . 2) ("y" . 3) ("d" . -1) ("y" . 4)))
  10197. ng h m new rem)
  10198. (when (string-match "\\(-\\([012][0-9]\\):\\([0-5][0-9]\\)\\)?\\( +\\+\\([0-9]+\\)\\([dmwy]\\)\\)?\\( +-\\([0-9]+\\)\\([dmwy]\\)\\)?" s)
  10199. (cond
  10200. ((or (org-pos-in-match-range pos 2)
  10201. (org-pos-in-match-range pos 3))
  10202. (setq m (string-to-number (match-string 3 s))
  10203. h (string-to-number (match-string 2 s)))
  10204. (if (org-pos-in-match-range pos 2)
  10205. (setq h (+ h n))
  10206. (setq n (* dm (org-no-warnings (signum n))))
  10207. (when (not (= 0 (setq rem (% m dm))))
  10208. (setq m (+ m (if (> n 0) (- rem) (- dm rem)))))
  10209. (setq m (+ m n)))
  10210. (if (< m 0) (setq m (+ m 60) h (1- h)))
  10211. (if (> m 59) (setq m (- m 60) h (1+ h)))
  10212. (setq h (min 24 (max 0 h)))
  10213. (setq ng 1 new (format "-%02d:%02d" h m)))
  10214. ((org-pos-in-match-range pos 6)
  10215. (setq ng 6 new (car (rassoc (+ n (cdr (assoc (match-string 6 s) idx))) idx))))
  10216. ((org-pos-in-match-range pos 5)
  10217. (setq ng 5 new (format "%d" (max 1 (+ n (string-to-number (match-string 5 s)))))))
  10218. ((org-pos-in-match-range pos 9)
  10219. (setq ng 9 new (car (rassoc (+ n (cdr (assoc (match-string 9 s) idx))) idx))))
  10220. ((org-pos-in-match-range pos 8)
  10221. (setq ng 8 new (format "%d" (max 0 (+ n (string-to-number (match-string 8 s))))))))
  10222. (when ng
  10223. (setq s (concat
  10224. (substring s 0 (match-beginning ng))
  10225. new
  10226. (substring s (match-end ng))))))
  10227. s))
  10228. (defun org-recenter-calendar (date)
  10229. "If the calendar is visible, recenter it to DATE."
  10230. (let* ((win (selected-window))
  10231. (cwin (get-buffer-window "*Calendar*" t))
  10232. (calendar-move-hook nil))
  10233. (when cwin
  10234. (select-window cwin)
  10235. (calendar-goto-date (if (listp date) date
  10236. (calendar-gregorian-from-absolute date)))
  10237. (select-window win))))
  10238. (defun org-goto-calendar (&optional arg)
  10239. "Go to the Emacs calendar at the current date.
  10240. If there is a time stamp in the current line, go to that date.
  10241. A prefix ARG can be used to force the current date."
  10242. (interactive "P")
  10243. (let ((tsr org-ts-regexp) diff
  10244. (calendar-move-hook nil)
  10245. (calendar-view-holidays-initially-flag nil)
  10246. (view-calendar-holidays-initially nil)
  10247. (calendar-view-diary-initially-flag nil)
  10248. (view-diary-entries-initially nil))
  10249. (if (or (org-at-timestamp-p)
  10250. (save-excursion
  10251. (beginning-of-line 1)
  10252. (looking-at (concat ".*" tsr))))
  10253. (let ((d1 (time-to-days (current-time)))
  10254. (d2 (time-to-days
  10255. (org-time-string-to-time (match-string 1)))))
  10256. (setq diff (- d2 d1))))
  10257. (calendar)
  10258. (calendar-goto-today)
  10259. (if (and diff (not arg)) (calendar-forward-day diff))))
  10260. (defun org-get-date-from-calendar ()
  10261. "Return a list (month day year) of date at point in calendar."
  10262. (with-current-buffer "*Calendar*"
  10263. (save-match-data
  10264. (calendar-cursor-to-date))))
  10265. (defun org-date-from-calendar ()
  10266. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  10267. If there is already a time stamp at the cursor position, update it."
  10268. (interactive)
  10269. (if (org-at-timestamp-p t)
  10270. (org-timestamp-change 0 'calendar)
  10271. (let ((cal-date (org-get-date-from-calendar)))
  10272. (org-insert-time-stamp
  10273. (encode-time 0 0 0 (nth 1 cal-date) (car cal-date) (nth 2 cal-date))))))
  10274. (defun org-minutes-to-hh:mm-string (m)
  10275. "Compute H:MM from a number of minutes."
  10276. (let ((h (/ m 60)))
  10277. (setq m (- m (* 60 h)))
  10278. (format "%d:%02d" h m)))
  10279. (defun org-hh:mm-string-to-minutes (s)
  10280. "Convert a string H:MM to a number of minutes."
  10281. (if (string-match "\\([0-9]+\\):\\([0-9]+\\)" s)
  10282. (+ (* (string-to-number (match-string 1 s)) 60)
  10283. (string-to-number (match-string 2 s)))
  10284. 0))
  10285. ;;;; Agenda files
  10286. ;;;###autoload
  10287. (defun org-iswitchb (&optional arg)
  10288. "Use `iswitchb-read-buffer' to prompt for an Org buffer to switch to.
  10289. With a prefix argument, restrict available to files.
  10290. With two prefix arguments, restrict available buffers to agenda files.
  10291. Due to some yet unresolved reason, global function
  10292. `iswitchb-mode' needs to be active for this function to work."
  10293. (interactive "P")
  10294. (require 'iswitchb)
  10295. (let ((enabled iswitchb-mode) blist)
  10296. (or enabled (iswitchb-mode 1))
  10297. (setq blist (cond ((equal arg '(4)) (org-buffer-list 'files))
  10298. ((equal arg '(16)) (org-buffer-list 'agenda))
  10299. (t (org-buffer-list))))
  10300. (unwind-protect
  10301. (let ((iswitchb-make-buflist-hook
  10302. (lambda ()
  10303. (setq iswitchb-temp-buflist
  10304. (mapcar 'buffer-name blist)))))
  10305. (switch-to-buffer
  10306. (iswitchb-read-buffer
  10307. "Switch-to: " nil t))
  10308. (or enabled (iswitchb-mode -1))))))
  10309. (defun org-buffer-list (&optional predicate tmp)
  10310. "Return a list of Org buffers.
  10311. PREDICATE can be either 'export, 'files or 'agenda.
  10312. 'export restrict the list to Export buffers.
  10313. 'files restrict the list to buffers visiting Org files.
  10314. 'agenda restrict the list to buffers visiting agenda files.
  10315. If TMP is non-nil, don't include temporary buffers."
  10316. (let (filter blist)
  10317. (setq filter
  10318. (cond ((eq predicate 'files) "\.org$")
  10319. ((eq predicate 'export) "\*Org .*Export")
  10320. (t "\*Org \\|\.org$")))
  10321. (setq blist
  10322. (mapcar
  10323. (lambda(b)
  10324. (let ((bname (buffer-name b))
  10325. (bfile (buffer-file-name b)))
  10326. (if (and (string-match filter bname)
  10327. (if (eq predicate 'agenda)
  10328. (member bfile
  10329. (mapcar (lambda(f) (file-truename f))
  10330. org-agenda-files)) t)
  10331. (if tmp (not (string-match "tmp" bname)) t)) b)))
  10332. (buffer-list)))
  10333. (delete nil blist)))
  10334. (defun org-agenda-files (&optional unrestricted ext)
  10335. "Get the list of agenda files.
  10336. Optional UNRESTRICTED means return the full list even if a restriction
  10337. is currently in place.
  10338. When EXT is non-nil, try to add all files that are created by adding EXT
  10339. to the file nemes. Basically, this is a way to add the archive files
  10340. to the list, by setting EXT to \"_archive\" If EXT is non-nil, but not
  10341. a string, \"_archive\" will be used."
  10342. (let ((files
  10343. (cond
  10344. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  10345. ((stringp org-agenda-files) (org-read-agenda-file-list))
  10346. ((listp org-agenda-files) org-agenda-files)
  10347. (t (error "Invalid value of `org-agenda-files'")))))
  10348. (setq files (apply 'append
  10349. (mapcar (lambda (f)
  10350. (if (file-directory-p f)
  10351. (directory-files
  10352. f t org-agenda-file-regexp)
  10353. (list f)))
  10354. files)))
  10355. (when org-agenda-skip-unavailable-files
  10356. (setq files (delq nil
  10357. (mapcar (function
  10358. (lambda (file)
  10359. (and (file-readable-p file) file)))
  10360. files))))
  10361. (when ext
  10362. (setq ext (if (and (stringp ext) (string-match "\\S-" ext))
  10363. ext "_archive"))
  10364. (setq files (apply 'append
  10365. (mapcar
  10366. (lambda (f)
  10367. (if (file-exists-p (concat f ext))
  10368. (list f (concat f ext))
  10369. (list f)))
  10370. files))))
  10371. files))
  10372. (defun org-edit-agenda-file-list ()
  10373. "Edit the list of agenda files.
  10374. Depending on setup, this either uses customize to edit the variable
  10375. `org-agenda-files', or it visits the file that is holding the list. In the
  10376. latter case, the buffer is set up in a way that saving it automatically kills
  10377. the buffer and restores the previous window configuration."
  10378. (interactive)
  10379. (if (stringp org-agenda-files)
  10380. (let ((cw (current-window-configuration)))
  10381. (find-file org-agenda-files)
  10382. (org-set-local 'org-window-configuration cw)
  10383. (org-add-hook 'after-save-hook
  10384. (lambda ()
  10385. (set-window-configuration
  10386. (prog1 org-window-configuration
  10387. (kill-buffer (current-buffer))))
  10388. (org-install-agenda-files-menu)
  10389. (message "New agenda file list installed"))
  10390. nil 'local)
  10391. (message "%s" (substitute-command-keys
  10392. "Edit list and finish with \\[save-buffer]")))
  10393. (customize-variable 'org-agenda-files)))
  10394. (defun org-store-new-agenda-file-list (list)
  10395. "Set new value for the agenda file list and save it correcly."
  10396. (if (stringp org-agenda-files)
  10397. (let ((f org-agenda-files) b)
  10398. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  10399. (with-temp-file f
  10400. (insert (mapconcat 'identity list "\n") "\n")))
  10401. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  10402. (setq org-agenda-files list)
  10403. (customize-save-variable 'org-agenda-files org-agenda-files))))
  10404. (defun org-read-agenda-file-list ()
  10405. "Read the list of agenda files from a file."
  10406. (when (file-directory-p org-agenda-files)
  10407. (error "`org-agenda-files' cannot be a single directory"))
  10408. (when (stringp org-agenda-files)
  10409. (with-temp-buffer
  10410. (insert-file-contents org-agenda-files)
  10411. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  10412. ;;;###autoload
  10413. (defun org-cycle-agenda-files ()
  10414. "Cycle through the files in `org-agenda-files'.
  10415. If the current buffer visits an agenda file, find the next one in the list.
  10416. If the current buffer does not, find the first agenda file."
  10417. (interactive)
  10418. (let* ((fs (org-agenda-files t))
  10419. (files (append fs (list (car fs))))
  10420. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  10421. file)
  10422. (unless files (error "No agenda files"))
  10423. (catch 'exit
  10424. (while (setq file (pop files))
  10425. (if (equal (file-truename file) tcf)
  10426. (when (car files)
  10427. (find-file (car files))
  10428. (throw 'exit t))))
  10429. (find-file (car fs)))
  10430. (if (buffer-base-buffer) (switch-to-buffer (buffer-base-buffer)))))
  10431. (defun org-agenda-file-to-front (&optional to-end)
  10432. "Move/add the current file to the top of the agenda file list.
  10433. If the file is not present in the list, it is added to the front. If it is
  10434. present, it is moved there. With optional argument TO-END, add/move to the
  10435. end of the list."
  10436. (interactive "P")
  10437. (let ((org-agenda-skip-unavailable-files nil)
  10438. (file-alist (mapcar (lambda (x)
  10439. (cons (file-truename x) x))
  10440. (org-agenda-files t)))
  10441. (ctf (file-truename buffer-file-name))
  10442. x had)
  10443. (setq x (assoc ctf file-alist) had x)
  10444. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  10445. (if to-end
  10446. (setq file-alist (append (delq x file-alist) (list x)))
  10447. (setq file-alist (cons x (delq x file-alist))))
  10448. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  10449. (org-install-agenda-files-menu)
  10450. (message "File %s to %s of agenda file list"
  10451. (if had "moved" "added") (if to-end "end" "front"))))
  10452. (defun org-remove-file (&optional file)
  10453. "Remove current file from the list of files in variable `org-agenda-files'.
  10454. These are the files which are being checked for agenda entries.
  10455. Optional argument FILE means, use this file instead of the current."
  10456. (interactive)
  10457. (let* ((org-agenda-skip-unavailable-files nil)
  10458. (file (or file buffer-file-name))
  10459. (true-file (file-truename file))
  10460. (afile (abbreviate-file-name file))
  10461. (files (delq nil (mapcar
  10462. (lambda (x)
  10463. (if (equal true-file
  10464. (file-truename x))
  10465. nil x))
  10466. (org-agenda-files t)))))
  10467. (if (not (= (length files) (length (org-agenda-files t))))
  10468. (progn
  10469. (org-store-new-agenda-file-list files)
  10470. (org-install-agenda-files-menu)
  10471. (message "Removed file: %s" afile))
  10472. (message "File was not in list: %s (not removed)" afile))))
  10473. (defun org-file-menu-entry (file)
  10474. (vector file (list 'find-file file) t))
  10475. (defun org-check-agenda-file (file)
  10476. "Make sure FILE exists. If not, ask user what to do."
  10477. (when (not (file-exists-p file))
  10478. (message "non-existent file %s. [R]emove from list or [A]bort?"
  10479. (abbreviate-file-name file))
  10480. (let ((r (downcase (read-char-exclusive))))
  10481. (cond
  10482. ((equal r ?r)
  10483. (org-remove-file file)
  10484. (throw 'nextfile t))
  10485. (t (error "Abort"))))))
  10486. (defun org-get-agenda-file-buffer (file)
  10487. "Get a buffer visiting FILE. If the buffer needs to be created, add
  10488. it to the list of buffers which might be released later."
  10489. (let ((buf (org-find-base-buffer-visiting file)))
  10490. (if buf
  10491. buf ; just return it
  10492. ;; Make a new buffer and remember it
  10493. (setq buf (find-file-noselect file))
  10494. (if buf (push buf org-agenda-new-buffers))
  10495. buf)))
  10496. (defun org-release-buffers (blist)
  10497. "Release all buffers in list, asking the user for confirmation when needed.
  10498. When a buffer is unmodified, it is just killed. When modified, it is saved
  10499. \(if the user agrees) and then killed."
  10500. (let (buf file)
  10501. (while (setq buf (pop blist))
  10502. (setq file (buffer-file-name buf))
  10503. (when (and (buffer-modified-p buf)
  10504. file
  10505. (y-or-n-p (format "Save file %s? " file)))
  10506. (with-current-buffer buf (save-buffer)))
  10507. (kill-buffer buf))))
  10508. (defun org-prepare-agenda-buffers (files)
  10509. "Create buffers for all agenda files, protect archived trees and comments."
  10510. (interactive)
  10511. (let ((pa '(:org-archived t))
  10512. (pc '(:org-comment t))
  10513. (pall '(:org-archived t :org-comment t))
  10514. (inhibit-read-only t)
  10515. (rea (concat ":" org-archive-tag ":"))
  10516. bmp file re)
  10517. (save-excursion
  10518. (save-restriction
  10519. (while (setq file (pop files))
  10520. (if (bufferp file)
  10521. (set-buffer file)
  10522. (org-check-agenda-file file)
  10523. (set-buffer (org-get-agenda-file-buffer file)))
  10524. (widen)
  10525. (setq bmp (buffer-modified-p))
  10526. (org-refresh-category-properties)
  10527. (setq org-todo-keywords-for-agenda
  10528. (append org-todo-keywords-for-agenda org-todo-keywords-1))
  10529. (setq org-done-keywords-for-agenda
  10530. (append org-done-keywords-for-agenda org-done-keywords))
  10531. (save-excursion
  10532. (remove-text-properties (point-min) (point-max) pall)
  10533. (when org-agenda-skip-archived-trees
  10534. (goto-char (point-min))
  10535. (while (re-search-forward rea nil t)
  10536. (if (org-on-heading-p t)
  10537. (add-text-properties (point-at-bol) (org-end-of-subtree t) pa))))
  10538. (goto-char (point-min))
  10539. (setq re (concat "^\\*+ +" org-comment-string "\\>"))
  10540. (while (re-search-forward re nil t)
  10541. (add-text-properties
  10542. (match-beginning 0) (org-end-of-subtree t) pc)))
  10543. (set-buffer-modified-p bmp))))))
  10544. ;;;; Embedded LaTeX
  10545. (defvar org-cdlatex-mode-map (make-sparse-keymap)
  10546. "Keymap for the minor `org-cdlatex-mode'.")
  10547. (org-defkey org-cdlatex-mode-map "_" 'org-cdlatex-underscore-caret)
  10548. (org-defkey org-cdlatex-mode-map "^" 'org-cdlatex-underscore-caret)
  10549. (org-defkey org-cdlatex-mode-map "`" 'cdlatex-math-symbol)
  10550. (org-defkey org-cdlatex-mode-map "'" 'org-cdlatex-math-modify)
  10551. (org-defkey org-cdlatex-mode-map "\C-c{" 'cdlatex-environment)
  10552. (defvar org-cdlatex-texmathp-advice-is-done nil
  10553. "Flag remembering if we have applied the advice to texmathp already.")
  10554. (define-minor-mode org-cdlatex-mode
  10555. "Toggle the minor `org-cdlatex-mode'.
  10556. This mode supports entering LaTeX environment and math in LaTeX fragments
  10557. in Org-mode.
  10558. \\{org-cdlatex-mode-map}"
  10559. nil " OCDL" nil
  10560. (when org-cdlatex-mode (require 'cdlatex))
  10561. (unless org-cdlatex-texmathp-advice-is-done
  10562. (setq org-cdlatex-texmathp-advice-is-done t)
  10563. (defadvice texmathp (around org-math-always-on activate)
  10564. "Always return t in org-mode buffers.
  10565. This is because we want to insert math symbols without dollars even outside
  10566. the LaTeX math segments. If Orgmode thinks that point is actually inside
  10567. en embedded LaTeX fragement, let texmathp do its job.
  10568. \\[org-cdlatex-mode-map]"
  10569. (interactive)
  10570. (let (p)
  10571. (cond
  10572. ((not (org-mode-p)) ad-do-it)
  10573. ((eq this-command 'cdlatex-math-symbol)
  10574. (setq ad-return-value t
  10575. texmathp-why '("cdlatex-math-symbol in org-mode" . 0)))
  10576. (t
  10577. (let ((p (org-inside-LaTeX-fragment-p)))
  10578. (if (and p (member (car p) (plist-get org-format-latex-options :matchers)))
  10579. (setq ad-return-value t
  10580. texmathp-why '("Org-mode embedded math" . 0))
  10581. (if p ad-do-it)))))))))
  10582. (defun turn-on-org-cdlatex ()
  10583. "Unconditionally turn on `org-cdlatex-mode'."
  10584. (org-cdlatex-mode 1))
  10585. (defun org-inside-LaTeX-fragment-p ()
  10586. "Test if point is inside a LaTeX fragment.
  10587. I.e. after a \\begin, \\(, \\[, $, or $$, without the corresponding closing
  10588. sequence appearing also before point.
  10589. Even though the matchers for math are configurable, this function assumes
  10590. that \\begin, \\(, \\[, and $$ are always used. Only the single dollar
  10591. delimiters are skipped when they have been removed by customization.
  10592. The return value is nil, or a cons cell with the delimiter and
  10593. and the position of this delimiter.
  10594. This function does a reasonably good job, but can locally be fooled by
  10595. for example currency specifications. For example it will assume being in
  10596. inline math after \"$22.34\". The LaTeX fragment formatter will only format
  10597. fragments that are properly closed, but during editing, we have to live
  10598. with the uncertainty caused by missing closing delimiters. This function
  10599. looks only before point, not after."
  10600. (catch 'exit
  10601. (let ((pos (point))
  10602. (dodollar (member "$" (plist-get org-format-latex-options :matchers)))
  10603. (lim (progn
  10604. (re-search-backward (concat "^\\(" paragraph-start "\\)") nil t)
  10605. (point)))
  10606. dd-on str (start 0) m re)
  10607. (goto-char pos)
  10608. (when dodollar
  10609. (setq str (concat (buffer-substring lim (point)) "\000 X$.")
  10610. re (nth 1 (assoc "$" org-latex-regexps)))
  10611. (while (string-match re str start)
  10612. (cond
  10613. ((= (match-end 0) (length str))
  10614. (throw 'exit (cons "$" (+ lim (match-beginning 0) 1))))
  10615. ((= (match-end 0) (- (length str) 5))
  10616. (throw 'exit nil))
  10617. (t (setq start (match-end 0))))))
  10618. (when (setq m (re-search-backward "\\(\\\\begin{[^}]*}\\|\\\\(\\|\\\\\\[\\)\\|\\(\\\\end{[^}]*}\\|\\\\)\\|\\\\\\]\\)\\|\\(\\$\\$\\)" lim t))
  10619. (goto-char pos)
  10620. (and (match-beginning 1) (throw 'exit (cons (match-string 1) m)))
  10621. (and (match-beginning 2) (throw 'exit nil))
  10622. ;; count $$
  10623. (while (re-search-backward "\\$\\$" lim t)
  10624. (setq dd-on (not dd-on)))
  10625. (goto-char pos)
  10626. (if dd-on (cons "$$" m))))))
  10627. (defun org-try-cdlatex-tab ()
  10628. "Check if it makes sense to execute `cdlatex-tab', and do it if yes.
  10629. It makes sense to do so if `org-cdlatex-mode' is active and if the cursor is
  10630. - inside a LaTeX fragment, or
  10631. - after the first word in a line, where an abbreviation expansion could
  10632. insert a LaTeX environment."
  10633. (when org-cdlatex-mode
  10634. (cond
  10635. ((save-excursion
  10636. (skip-chars-backward "a-zA-Z0-9*")
  10637. (skip-chars-backward " \t")
  10638. (bolp))
  10639. (cdlatex-tab) t)
  10640. ((org-inside-LaTeX-fragment-p)
  10641. (cdlatex-tab) t)
  10642. (t nil))))
  10643. (defun org-cdlatex-underscore-caret (&optional arg)
  10644. "Execute `cdlatex-sub-superscript' in LaTeX fragments.
  10645. Revert to the normal definition outside of these fragments."
  10646. (interactive "P")
  10647. (if (org-inside-LaTeX-fragment-p)
  10648. (call-interactively 'cdlatex-sub-superscript)
  10649. (let (org-cdlatex-mode)
  10650. (call-interactively (key-binding (vector last-input-event))))))
  10651. (defun org-cdlatex-math-modify (&optional arg)
  10652. "Execute `cdlatex-math-modify' in LaTeX fragments.
  10653. Revert to the normal definition outside of these fragments."
  10654. (interactive "P")
  10655. (if (org-inside-LaTeX-fragment-p)
  10656. (call-interactively 'cdlatex-math-modify)
  10657. (let (org-cdlatex-mode)
  10658. (call-interactively (key-binding (vector last-input-event))))))
  10659. (defvar org-latex-fragment-image-overlays nil
  10660. "List of overlays carrying the images of latex fragments.")
  10661. (make-variable-buffer-local 'org-latex-fragment-image-overlays)
  10662. (defun org-remove-latex-fragment-image-overlays ()
  10663. "Remove all overlays with LaTeX fragment images in current buffer."
  10664. (mapc 'org-delete-overlay org-latex-fragment-image-overlays)
  10665. (setq org-latex-fragment-image-overlays nil))
  10666. (defun org-preview-latex-fragment (&optional subtree)
  10667. "Preview the LaTeX fragment at point, or all locally or globally.
  10668. If the cursor is in a LaTeX fragment, create the image and overlay
  10669. it over the source code. If there is no fragment at point, display
  10670. all fragments in the current text, from one headline to the next. With
  10671. prefix SUBTREE, display all fragments in the current subtree. With a
  10672. double prefix `C-u C-u', or when the cursor is before the first headline,
  10673. display all fragments in the buffer.
  10674. The images can be removed again with \\[org-ctrl-c-ctrl-c]."
  10675. (interactive "P")
  10676. (org-remove-latex-fragment-image-overlays)
  10677. (save-excursion
  10678. (save-restriction
  10679. (let (beg end at msg)
  10680. (cond
  10681. ((or (equal subtree '(16))
  10682. (not (save-excursion
  10683. (re-search-backward (concat "^" outline-regexp) nil t))))
  10684. (setq beg (point-min) end (point-max)
  10685. msg "Creating images for buffer...%s"))
  10686. ((equal subtree '(4))
  10687. (org-back-to-heading)
  10688. (setq beg (point) end (org-end-of-subtree t)
  10689. msg "Creating images for subtree...%s"))
  10690. (t
  10691. (if (setq at (org-inside-LaTeX-fragment-p))
  10692. (goto-char (max (point-min) (- (cdr at) 2)))
  10693. (org-back-to-heading))
  10694. (setq beg (point) end (progn (outline-next-heading) (point))
  10695. msg (if at "Creating image...%s"
  10696. "Creating images for entry...%s"))))
  10697. (message msg "")
  10698. (narrow-to-region beg end)
  10699. (goto-char beg)
  10700. (org-format-latex
  10701. (concat "ltxpng/" (file-name-sans-extension
  10702. (file-name-nondirectory
  10703. buffer-file-name)))
  10704. default-directory 'overlays msg at 'forbuffer)
  10705. (message msg "done. Use `C-c C-c' to remove images.")))))
  10706. (defvar org-latex-regexps
  10707. '(("begin" "^[ \t]*\\(\\\\begin{\\([a-zA-Z0-9\\*]+\\)[^\000]+?\\\\end{\\2}\\)" 1 t)
  10708. ;; ("$" "\\([ (]\\|^\\)\\(\\(\\([$]\\)\\([^ \r\n,.$].*?\\(\n.*?\\)\\{0,5\\}[^ \r\n,.$]\\)\\4\\)\\)\\([ .,?;:'\")]\\|$\\)" 2 nil)
  10709. ;; \000 in the following regex is needed for org-inside-LaTeX-fragment-p
  10710. ("$" "\\([^$]\\)\\(\\(\\$\\([^ \r\n,;.$][^$\n\r]*?\\(\n[^$\n\r]*?\\)\\{0,2\\}[^ \r\n,.$]\\)\\$\\)\\)\\([ .,?;:'\")\000]\\|$\\)" 2 nil)
  10711. ("\\(" "\\\\([^\000]*?\\\\)" 0 nil)
  10712. ("\\[" "\\\\\\[[^\000]*?\\\\\\]" 0 t)
  10713. ("$$" "\\$\\$[^\000]*?\\$\\$" 0 t))
  10714. "Regular expressions for matching embedded LaTeX.")
  10715. (defun org-format-latex (prefix &optional dir overlays msg at forbuffer)
  10716. "Replace LaTeX fragments with links to an image, and produce images."
  10717. (if (and overlays (fboundp 'clear-image-cache)) (clear-image-cache))
  10718. (let* ((prefixnodir (file-name-nondirectory prefix))
  10719. (absprefix (expand-file-name prefix dir))
  10720. (todir (file-name-directory absprefix))
  10721. (opt org-format-latex-options)
  10722. (matchers (plist-get opt :matchers))
  10723. (re-list org-latex-regexps)
  10724. (cnt 0) txt link beg end re e checkdir
  10725. m n block linkfile movefile ov)
  10726. ;; Check if there are old images files with this prefix, and remove them
  10727. (when (file-directory-p todir)
  10728. (mapc 'delete-file
  10729. (directory-files
  10730. todir 'full
  10731. (concat (regexp-quote prefixnodir) "_[0-9]+\\.png$"))))
  10732. ;; Check the different regular expressions
  10733. (while (setq e (pop re-list))
  10734. (setq m (car e) re (nth 1 e) n (nth 2 e)
  10735. block (if (nth 3 e) "\n\n" ""))
  10736. (when (member m matchers)
  10737. (goto-char (point-min))
  10738. (while (re-search-forward re nil t)
  10739. (when (or (not at) (equal (cdr at) (match-beginning n)))
  10740. (setq txt (match-string n)
  10741. beg (match-beginning n) end (match-end n)
  10742. cnt (1+ cnt)
  10743. linkfile (format "%s_%04d.png" prefix cnt)
  10744. movefile (format "%s_%04d.png" absprefix cnt)
  10745. link (concat block "[[file:" linkfile "]]" block))
  10746. (if msg (message msg cnt))
  10747. (goto-char beg)
  10748. (unless checkdir ; make sure the directory exists
  10749. (setq checkdir t)
  10750. (or (file-directory-p todir) (make-directory todir)))
  10751. (org-create-formula-image
  10752. txt movefile opt forbuffer)
  10753. (if overlays
  10754. (progn
  10755. (setq ov (org-make-overlay beg end))
  10756. (if (featurep 'xemacs)
  10757. (progn
  10758. (org-overlay-put ov 'invisible t)
  10759. (org-overlay-put
  10760. ov 'end-glyph
  10761. (make-glyph (vector 'png :file movefile))))
  10762. (org-overlay-put
  10763. ov 'display
  10764. (list 'image :type 'png :file movefile :ascent 'center)))
  10765. (push ov org-latex-fragment-image-overlays)
  10766. (goto-char end))
  10767. (delete-region beg end)
  10768. (insert link))))))))
  10769. ;; This function borrows from Ganesh Swami's latex2png.el
  10770. (defun org-create-formula-image (string tofile options buffer)
  10771. (let* ((tmpdir (if (featurep 'xemacs)
  10772. (temp-directory)
  10773. temporary-file-directory))
  10774. (texfilebase (make-temp-name
  10775. (expand-file-name "orgtex" tmpdir)))
  10776. (texfile (concat texfilebase ".tex"))
  10777. (dvifile (concat texfilebase ".dvi"))
  10778. (pngfile (concat texfilebase ".png"))
  10779. (fnh (if (featurep 'xemacs)
  10780. (font-height (get-face-font 'default))
  10781. (face-attribute 'default :height nil)))
  10782. (scale (or (plist-get options (if buffer :scale :html-scale)) 1.0))
  10783. (dpi (number-to-string (* scale (floor (* 0.9 (if buffer fnh 140.))))))
  10784. (fg (or (plist-get options (if buffer :foreground :html-foreground))
  10785. "Black"))
  10786. (bg (or (plist-get options (if buffer :background :html-background))
  10787. "Transparent")))
  10788. (if (eq fg 'default) (setq fg (org-dvipng-color :foreground)))
  10789. (if (eq bg 'default) (setq bg (org-dvipng-color :background)))
  10790. (with-temp-file texfile
  10791. (insert org-format-latex-header
  10792. "\n\\begin{document}\n" string "\n\\end{document}\n"))
  10793. (let ((dir default-directory))
  10794. (condition-case nil
  10795. (progn
  10796. (cd tmpdir)
  10797. (call-process "latex" nil nil nil texfile))
  10798. (error nil))
  10799. (cd dir))
  10800. (if (not (file-exists-p dvifile))
  10801. (progn (message "Failed to create dvi file from %s" texfile) nil)
  10802. (call-process "dvipng" nil nil nil
  10803. "-E" "-fg" fg "-bg" bg
  10804. "-D" dpi
  10805. ;;"-x" scale "-y" scale
  10806. "-T" "tight"
  10807. "-o" pngfile
  10808. dvifile)
  10809. (if (not (file-exists-p pngfile))
  10810. (progn (message "Failed to create png file from %s" texfile) nil)
  10811. ;; Use the requested file name and clean up
  10812. (copy-file pngfile tofile 'replace)
  10813. (loop for e in '(".dvi" ".tex" ".aux" ".log" ".png") do
  10814. (delete-file (concat texfilebase e)))
  10815. pngfile))))
  10816. (defun org-dvipng-color (attr)
  10817. "Return an rgb color specification for dvipng."
  10818. (apply 'format "rgb %s %s %s"
  10819. (mapcar 'org-normalize-color
  10820. (color-values (face-attribute 'default attr nil)))))
  10821. (defun org-normalize-color (value)
  10822. "Return string to be used as color value for an RGB component."
  10823. (format "%g" (/ value 65535.0)))
  10824. ;;;; Key bindings
  10825. ;; Make `C-c C-x' a prefix key
  10826. (org-defkey org-mode-map "\C-c\C-x" (make-sparse-keymap))
  10827. ;; TAB key with modifiers
  10828. (org-defkey org-mode-map "\C-i" 'org-cycle)
  10829. (org-defkey org-mode-map [(tab)] 'org-cycle)
  10830. (org-defkey org-mode-map [(control tab)] 'org-force-cycle-archived)
  10831. (org-defkey org-mode-map [(meta tab)] 'org-complete)
  10832. (org-defkey org-mode-map "\M-\t" 'org-complete)
  10833. (org-defkey org-mode-map "\M-\C-i" 'org-complete)
  10834. ;; The following line is necessary under Suse GNU/Linux
  10835. (unless (featurep 'xemacs)
  10836. (org-defkey org-mode-map [S-iso-lefttab] 'org-shifttab))
  10837. (org-defkey org-mode-map [(shift tab)] 'org-shifttab)
  10838. (define-key org-mode-map [backtab] 'org-shifttab)
  10839. (org-defkey org-mode-map [(shift return)] 'org-table-copy-down)
  10840. (org-defkey org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  10841. (org-defkey org-mode-map [(meta return)] 'org-meta-return)
  10842. ;; Cursor keys with modifiers
  10843. (org-defkey org-mode-map [(meta left)] 'org-metaleft)
  10844. (org-defkey org-mode-map [(meta right)] 'org-metaright)
  10845. (org-defkey org-mode-map [(meta up)] 'org-metaup)
  10846. (org-defkey org-mode-map [(meta down)] 'org-metadown)
  10847. (org-defkey org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  10848. (org-defkey org-mode-map [(meta shift right)] 'org-shiftmetaright)
  10849. (org-defkey org-mode-map [(meta shift up)] 'org-shiftmetaup)
  10850. (org-defkey org-mode-map [(meta shift down)] 'org-shiftmetadown)
  10851. (org-defkey org-mode-map [(shift up)] 'org-shiftup)
  10852. (org-defkey org-mode-map [(shift down)] 'org-shiftdown)
  10853. (org-defkey org-mode-map [(shift left)] 'org-shiftleft)
  10854. (org-defkey org-mode-map [(shift right)] 'org-shiftright)
  10855. (org-defkey org-mode-map [(control shift right)] 'org-shiftcontrolright)
  10856. (org-defkey org-mode-map [(control shift left)] 'org-shiftcontrolleft)
  10857. ;;; Extra keys for tty access.
  10858. ;; We only set them when really needed because otherwise the
  10859. ;; menus don't show the simple keys
  10860. (when (or (featurep 'xemacs) ;; because XEmacs supports multi-device stuff
  10861. (not window-system))
  10862. (org-defkey org-mode-map "\C-c\C-xc" 'org-table-copy-down)
  10863. (org-defkey org-mode-map "\C-c\C-xM" 'org-insert-todo-heading)
  10864. (org-defkey org-mode-map "\C-c\C-xm" 'org-meta-return)
  10865. (org-defkey org-mode-map [?\e (return)] 'org-meta-return)
  10866. (org-defkey org-mode-map [?\e (left)] 'org-metaleft)
  10867. (org-defkey org-mode-map "\C-c\C-xl" 'org-metaleft)
  10868. (org-defkey org-mode-map [?\e (right)] 'org-metaright)
  10869. (org-defkey org-mode-map "\C-c\C-xr" 'org-metaright)
  10870. (org-defkey org-mode-map [?\e (up)] 'org-metaup)
  10871. (org-defkey org-mode-map "\C-c\C-xu" 'org-metaup)
  10872. (org-defkey org-mode-map [?\e (down)] 'org-metadown)
  10873. (org-defkey org-mode-map "\C-c\C-xd" 'org-metadown)
  10874. (org-defkey org-mode-map "\C-c\C-xL" 'org-shiftmetaleft)
  10875. (org-defkey org-mode-map "\C-c\C-xR" 'org-shiftmetaright)
  10876. (org-defkey org-mode-map "\C-c\C-xU" 'org-shiftmetaup)
  10877. (org-defkey org-mode-map "\C-c\C-xD" 'org-shiftmetadown)
  10878. (org-defkey org-mode-map [?\C-c (up)] 'org-shiftup)
  10879. (org-defkey org-mode-map [?\C-c (down)] 'org-shiftdown)
  10880. (org-defkey org-mode-map [?\C-c (left)] 'org-shiftleft)
  10881. (org-defkey org-mode-map [?\C-c (right)] 'org-shiftright)
  10882. (org-defkey org-mode-map [?\C-c ?\C-x (right)] 'org-shiftcontrolright)
  10883. (org-defkey org-mode-map [?\C-c ?\C-x (left)] 'org-shiftcontrolleft))
  10884. ;; All the other keys
  10885. (org-defkey org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  10886. (org-defkey org-mode-map "\C-c\C-r" 'org-reveal)
  10887. (org-defkey org-mode-map "\C-xns" 'org-narrow-to-subtree)
  10888. (org-defkey org-mode-map "\C-c$" 'org-archive-subtree)
  10889. (org-defkey org-mode-map "\C-c\C-x\C-s" 'org-advertized-archive-subtree)
  10890. (org-defkey org-mode-map "\C-c\C-x\C-a" 'org-toggle-archive-tag)
  10891. (org-defkey org-mode-map "\C-c\C-xa" 'org-toggle-archive-tag)
  10892. (org-defkey org-mode-map "\C-c\C-xA" 'org-archive-to-archive-sibling)
  10893. (org-defkey org-mode-map "\C-c\C-xb" 'org-tree-to-indirect-buffer)
  10894. (org-defkey org-mode-map "\C-c\C-j" 'org-goto)
  10895. (org-defkey org-mode-map "\C-c\C-t" 'org-todo)
  10896. (org-defkey org-mode-map "\C-c\C-s" 'org-schedule)
  10897. (org-defkey org-mode-map "\C-c\C-d" 'org-deadline)
  10898. (org-defkey org-mode-map "\C-c;" 'org-toggle-comment)
  10899. (org-defkey org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  10900. (org-defkey org-mode-map "\C-c\C-w" 'org-refile)
  10901. (org-defkey org-mode-map "\C-c/" 'org-sparse-tree) ; Minor-mode reserved
  10902. (org-defkey org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  10903. (org-defkey org-mode-map "\C-c\C-m" 'org-ctrl-c-ret)
  10904. (org-defkey org-mode-map "\M-\C-m" 'org-insert-heading)
  10905. (org-defkey org-mode-map [(control return)] 'org-insert-heading-after-current)
  10906. (org-defkey org-mode-map "\C-c\C-x\C-n" 'org-next-link)
  10907. (org-defkey org-mode-map "\C-c\C-x\C-p" 'org-previous-link)
  10908. (org-defkey org-mode-map "\C-c\C-l" 'org-insert-link)
  10909. (org-defkey org-mode-map "\C-c\C-o" 'org-open-at-point)
  10910. (org-defkey org-mode-map "\C-c%" 'org-mark-ring-push)
  10911. (org-defkey org-mode-map "\C-c&" 'org-mark-ring-goto)
  10912. (org-defkey org-mode-map "\C-c\C-z" 'org-add-note) ; Alternative binding
  10913. (org-defkey org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  10914. (org-defkey org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  10915. (org-defkey org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  10916. (org-defkey org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  10917. (org-defkey org-mode-map "\C-c>" 'org-goto-calendar)
  10918. (org-defkey org-mode-map "\C-c<" 'org-date-from-calendar)
  10919. (org-defkey org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  10920. (org-defkey org-mode-map [(control ?\')] 'org-cycle-agenda-files)
  10921. (org-defkey org-mode-map "\C-c[" 'org-agenda-file-to-front)
  10922. (org-defkey org-mode-map "\C-c]" 'org-remove-file)
  10923. (org-defkey org-mode-map "\C-c\C-x<" 'org-agenda-set-restriction-lock)
  10924. (org-defkey org-mode-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  10925. (org-defkey org-mode-map "\C-c-" 'org-ctrl-c-minus)
  10926. (org-defkey org-mode-map "\C-c*" 'org-ctrl-c-star)
  10927. (org-defkey org-mode-map "\C-c^" 'org-sort)
  10928. (org-defkey org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  10929. (org-defkey org-mode-map "\C-c\C-k" 'org-kill-note-or-show-branches)
  10930. (org-defkey org-mode-map "\C-c#" 'org-update-checkbox-count)
  10931. (org-defkey org-mode-map "\C-m" 'org-return)
  10932. (org-defkey org-mode-map "\C-j" 'org-return-indent)
  10933. (org-defkey org-mode-map "\C-c?" 'org-table-field-info)
  10934. (org-defkey org-mode-map "\C-c " 'org-table-blank-field)
  10935. (org-defkey org-mode-map "\C-c+" 'org-table-sum)
  10936. (org-defkey org-mode-map "\C-c=" 'org-table-eval-formula)
  10937. (org-defkey org-mode-map "\C-c'" 'org-table-edit-formulas)
  10938. (org-defkey org-mode-map "\C-c`" 'org-table-edit-field)
  10939. (org-defkey org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  10940. (org-defkey org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  10941. (org-defkey org-mode-map "\C-c~" 'org-table-create-with-table.el)
  10942. (org-defkey org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  10943. (org-defkey org-mode-map "\C-c}" 'org-table-toggle-coordinate-overlays)
  10944. (org-defkey org-mode-map "\C-c{" 'org-table-toggle-formula-debugger)
  10945. (org-defkey org-mode-map "\C-c\C-e" 'org-export)
  10946. (org-defkey org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  10947. (org-defkey org-mode-map "\C-c\C-x\C-f" 'org-emphasize)
  10948. (org-defkey org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  10949. (org-defkey org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  10950. (org-defkey org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  10951. (org-defkey org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  10952. (org-defkey org-mode-map "\C-c\C-x\C-t" 'org-toggle-time-stamp-overlays)
  10953. (org-defkey org-mode-map "\C-c\C-x\C-i" 'org-clock-in)
  10954. (org-defkey org-mode-map "\C-c\C-x\C-o" 'org-clock-out)
  10955. (org-defkey org-mode-map "\C-c\C-x\C-j" 'org-clock-goto)
  10956. (org-defkey org-mode-map "\C-c\C-x\C-x" 'org-clock-cancel)
  10957. (org-defkey org-mode-map "\C-c\C-x\C-d" 'org-clock-display)
  10958. (org-defkey org-mode-map "\C-c\C-x\C-r" 'org-clock-report)
  10959. (org-defkey org-mode-map "\C-c\C-x\C-u" 'org-dblock-update)
  10960. (org-defkey org-mode-map "\C-c\C-x\C-l" 'org-preview-latex-fragment)
  10961. (org-defkey org-mode-map "\C-c\C-x\C-b" 'org-toggle-checkbox)
  10962. (org-defkey org-mode-map "\C-c\C-xp" 'org-set-property)
  10963. (org-defkey org-mode-map "\C-c\C-xr" 'org-insert-columns-dblock)
  10964. (define-key org-mode-map "\C-c\C-x\C-c" 'org-columns)
  10965. (when (featurep 'xemacs)
  10966. (org-defkey org-mode-map 'button3 'popup-mode-menu))
  10967. (defvar org-table-auto-blank-field) ; defined in org-table.el
  10968. (defun org-self-insert-command (N)
  10969. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10970. If the cursor is in a table looking at whitespace, the whitespace is
  10971. overwritten, and the table is not marked as requiring realignment."
  10972. (interactive "p")
  10973. (if (and (org-table-p)
  10974. (progn
  10975. ;; check if we blank the field, and if that triggers align
  10976. (and (featurep 'org-table) org-table-auto-blank-field
  10977. (member last-command
  10978. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  10979. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  10980. ;; got extra space, this field does not determine column width
  10981. (let (org-table-may-need-update) (org-table-blank-field))
  10982. ;; no extra space, this field may determine column width
  10983. (org-table-blank-field)))
  10984. t)
  10985. (eq N 1)
  10986. (looking-at "[^|\n]* |"))
  10987. (let (org-table-may-need-update)
  10988. (goto-char (1- (match-end 0)))
  10989. (delete-backward-char 1)
  10990. (goto-char (match-beginning 0))
  10991. (self-insert-command N))
  10992. (setq org-table-may-need-update t)
  10993. (self-insert-command N)
  10994. (org-fix-tags-on-the-fly)))
  10995. (defun org-fix-tags-on-the-fly ()
  10996. (when (and (equal (char-after (point-at-bol)) ?*)
  10997. (org-on-heading-p))
  10998. (org-align-tags-here org-tags-column)))
  10999. (defun org-delete-backward-char (N)
  11000. "Like `delete-backward-char', insert whitespace at field end in tables.
  11001. When deleting backwards, in tables this function will insert whitespace in
  11002. front of the next \"|\" separator, to keep the table aligned. The table will
  11003. still be marked for re-alignment if the field did fill the entire column,
  11004. because, in this case the deletion might narrow the column."
  11005. (interactive "p")
  11006. (if (and (org-table-p)
  11007. (eq N 1)
  11008. (string-match "|" (buffer-substring (point-at-bol) (point)))
  11009. (looking-at ".*?|"))
  11010. (let ((pos (point))
  11011. (noalign (looking-at "[^|\n\r]* |"))
  11012. (c org-table-may-need-update))
  11013. (backward-delete-char N)
  11014. (skip-chars-forward "^|")
  11015. (insert " ")
  11016. (goto-char (1- pos))
  11017. ;; noalign: if there were two spaces at the end, this field
  11018. ;; does not determine the width of the column.
  11019. (if noalign (setq org-table-may-need-update c)))
  11020. (backward-delete-char N)
  11021. (org-fix-tags-on-the-fly)))
  11022. (defun org-delete-char (N)
  11023. "Like `delete-char', but insert whitespace at field end in tables.
  11024. When deleting characters, in tables this function will insert whitespace in
  11025. front of the next \"|\" separator, to keep the table aligned. The table will
  11026. still be marked for re-alignment if the field did fill the entire column,
  11027. because, in this case the deletion might narrow the column."
  11028. (interactive "p")
  11029. (if (and (org-table-p)
  11030. (not (bolp))
  11031. (not (= (char-after) ?|))
  11032. (eq N 1))
  11033. (if (looking-at ".*?|")
  11034. (let ((pos (point))
  11035. (noalign (looking-at "[^|\n\r]* |"))
  11036. (c org-table-may-need-update))
  11037. (replace-match (concat
  11038. (substring (match-string 0) 1 -1)
  11039. " |"))
  11040. (goto-char pos)
  11041. ;; noalign: if there were two spaces at the end, this field
  11042. ;; does not determine the width of the column.
  11043. (if noalign (setq org-table-may-need-update c)))
  11044. (delete-char N))
  11045. (delete-char N)
  11046. (org-fix-tags-on-the-fly)))
  11047. ;; Make `delete-selection-mode' work with org-mode and orgtbl-mode
  11048. (put 'org-self-insert-command 'delete-selection t)
  11049. (put 'orgtbl-self-insert-command 'delete-selection t)
  11050. (put 'org-delete-char 'delete-selection 'supersede)
  11051. (put 'org-delete-backward-char 'delete-selection 'supersede)
  11052. ;; Make `flyspell-mode' delay after some commands
  11053. (put 'org-self-insert-command 'flyspell-delayed t)
  11054. (put 'orgtbl-self-insert-command 'flyspell-delayed t)
  11055. (put 'org-delete-char 'flyspell-delayed t)
  11056. (put 'org-delete-backward-char 'flyspell-delayed t)
  11057. ;; Make pabbrev-mode expand after org-mode commands
  11058. (put 'org-self-insert-command 'pabbrev-expand-after-command t)
  11059. (put 'orgybl-self-insert-command 'pabbrev-expand-after-command t)
  11060. ;; How to do this: Measure non-white length of current string
  11061. ;; If equal to column width, we should realign.
  11062. (defun org-remap (map &rest commands)
  11063. "In MAP, remap the functions given in COMMANDS.
  11064. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  11065. (let (new old)
  11066. (while commands
  11067. (setq old (pop commands) new (pop commands))
  11068. (if (fboundp 'command-remapping)
  11069. (org-defkey map (vector 'remap old) new)
  11070. (substitute-key-definition old new map global-map)))))
  11071. (when (eq org-enable-table-editor 'optimized)
  11072. ;; If the user wants maximum table support, we need to hijack
  11073. ;; some standard editing functions
  11074. (org-remap org-mode-map
  11075. 'self-insert-command 'org-self-insert-command
  11076. 'delete-char 'org-delete-char
  11077. 'delete-backward-char 'org-delete-backward-char)
  11078. (org-defkey org-mode-map "|" 'org-force-self-insert))
  11079. (defun org-shiftcursor-error ()
  11080. "Throw an error because Shift-Cursor command was applied in wrong context."
  11081. (error "This command is active in special context like tables, headlines or timestamps"))
  11082. (defun org-shifttab (&optional arg)
  11083. "Global visibility cycling or move to previous table field.
  11084. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  11085. on context.
  11086. See the individual commands for more information."
  11087. (interactive "P")
  11088. (cond
  11089. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  11090. (arg (message "Content view to level: ")
  11091. (org-content (prefix-numeric-value arg))
  11092. (setq org-cycle-global-status 'overview))
  11093. (t (call-interactively 'org-global-cycle))))
  11094. (defun org-shiftmetaleft ()
  11095. "Promote subtree or delete table column.
  11096. Calls `org-promote-subtree', `org-outdent-item',
  11097. or `org-table-delete-column', depending on context.
  11098. See the individual commands for more information."
  11099. (interactive)
  11100. (cond
  11101. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  11102. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  11103. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11104. (t (org-shiftcursor-error))))
  11105. (defun org-shiftmetaright ()
  11106. "Demote subtree or insert table column.
  11107. Calls `org-demote-subtree', `org-indent-item',
  11108. or `org-table-insert-column', depending on context.
  11109. See the individual commands for more information."
  11110. (interactive)
  11111. (cond
  11112. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  11113. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  11114. ((org-at-item-p) (call-interactively 'org-indent-item))
  11115. (t (org-shiftcursor-error))))
  11116. (defun org-shiftmetaup (&optional arg)
  11117. "Move subtree up or kill table row.
  11118. Calls `org-move-subtree-up' or `org-table-kill-row' or
  11119. `org-move-item-up' depending on context. See the individual commands
  11120. for more information."
  11121. (interactive "P")
  11122. (cond
  11123. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  11124. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11125. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11126. (t (org-shiftcursor-error))))
  11127. (defun org-shiftmetadown (&optional arg)
  11128. "Move subtree down or insert table row.
  11129. Calls `org-move-subtree-down' or `org-table-insert-row' or
  11130. `org-move-item-down', depending on context. See the individual
  11131. commands for more information."
  11132. (interactive "P")
  11133. (cond
  11134. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  11135. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11136. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11137. (t (org-shiftcursor-error))))
  11138. (defun org-metaleft (&optional arg)
  11139. "Promote heading or move table column to left.
  11140. Calls `org-do-promote' or `org-table-move-column', depending on context.
  11141. With no specific context, calls the Emacs default `backward-word'.
  11142. See the individual commands for more information."
  11143. (interactive "P")
  11144. (cond
  11145. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  11146. ((or (org-on-heading-p) (org-region-active-p))
  11147. (call-interactively 'org-do-promote))
  11148. ((org-at-item-p) (call-interactively 'org-outdent-item))
  11149. (t (call-interactively 'backward-word))))
  11150. (defun org-metaright (&optional arg)
  11151. "Demote subtree or move table column to right.
  11152. Calls `org-do-demote' or `org-table-move-column', depending on context.
  11153. With no specific context, calls the Emacs default `forward-word'.
  11154. See the individual commands for more information."
  11155. (interactive "P")
  11156. (cond
  11157. ((org-at-table-p) (call-interactively 'org-table-move-column))
  11158. ((or (org-on-heading-p) (org-region-active-p))
  11159. (call-interactively 'org-do-demote))
  11160. ((org-at-item-p) (call-interactively 'org-indent-item))
  11161. (t (call-interactively 'forward-word))))
  11162. (defun org-metaup (&optional arg)
  11163. "Move subtree up or move table row up.
  11164. Calls `org-move-subtree-up' or `org-table-move-row' or
  11165. `org-move-item-up', depending on context. See the individual commands
  11166. for more information."
  11167. (interactive "P")
  11168. (cond
  11169. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  11170. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  11171. ((org-at-item-p) (call-interactively 'org-move-item-up))
  11172. (t (transpose-lines 1) (beginning-of-line -1))))
  11173. (defun org-metadown (&optional arg)
  11174. "Move subtree down or move table row down.
  11175. Calls `org-move-subtree-down' or `org-table-move-row' or
  11176. `org-move-item-down', depending on context. See the individual
  11177. commands for more information."
  11178. (interactive "P")
  11179. (cond
  11180. ((org-at-table-p) (call-interactively 'org-table-move-row))
  11181. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  11182. ((org-at-item-p) (call-interactively 'org-move-item-down))
  11183. (t (beginning-of-line 2) (transpose-lines 1) (beginning-of-line 0))))
  11184. (defun org-shiftup (&optional arg)
  11185. "Increase item in timestamp or increase priority of current headline.
  11186. Calls `org-timestamp-up' or `org-priority-up', or `org-previous-item',
  11187. depending on context. See the individual commands for more information."
  11188. (interactive "P")
  11189. (cond
  11190. ((org-at-timestamp-p t)
  11191. (call-interactively (if org-edit-timestamp-down-means-later
  11192. 'org-timestamp-down 'org-timestamp-up)))
  11193. ((org-on-heading-p) (call-interactively 'org-priority-up))
  11194. ((org-at-item-p) (call-interactively 'org-previous-item))
  11195. ((org-clocktable-try-shift 'up arg))
  11196. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  11197. (defun org-shiftdown (&optional arg)
  11198. "Decrease item in timestamp or decrease priority of current headline.
  11199. Calls `org-timestamp-down' or `org-priority-down', or `org-next-item'
  11200. depending on context. See the individual commands for more information."
  11201. (interactive "P")
  11202. (cond
  11203. ((org-at-timestamp-p t)
  11204. (call-interactively (if org-edit-timestamp-down-means-later
  11205. 'org-timestamp-up 'org-timestamp-down)))
  11206. ((org-on-heading-p) (call-interactively 'org-priority-down))
  11207. ((org-clocktable-try-shift 'down arg))
  11208. (t (call-interactively 'org-next-item))))
  11209. (defun org-shiftright (&optional arg)
  11210. "Next TODO keyword or timestamp one day later, depending on context."
  11211. (interactive "P")
  11212. (cond
  11213. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-up-day))
  11214. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  11215. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet nil))
  11216. ((org-at-property-p) (call-interactively 'org-property-next-allowed-value))
  11217. ((org-clocktable-try-shift 'right arg))
  11218. (t (org-shiftcursor-error))))
  11219. (defun org-shiftleft (&optional arg)
  11220. "Previous TODO keyword or timestamp one day earlier, depending on context."
  11221. (interactive "P")
  11222. (cond
  11223. ((org-at-timestamp-p t) (call-interactively 'org-timestamp-down-day))
  11224. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  11225. ((org-at-item-p) (org-call-with-arg 'org-cycle-list-bullet 'previous))
  11226. ((org-at-property-p)
  11227. (call-interactively 'org-property-previous-allowed-value))
  11228. ((org-clocktable-try-shift 'left arg))
  11229. (t (org-shiftcursor-error))))
  11230. (defun org-shiftcontrolright ()
  11231. "Switch to next TODO set."
  11232. (interactive)
  11233. (cond
  11234. ((org-on-heading-p) (org-call-with-arg 'org-todo 'nextset))
  11235. (t (org-shiftcursor-error))))
  11236. (defun org-shiftcontrolleft ()
  11237. "Switch to previous TODO set."
  11238. (interactive)
  11239. (cond
  11240. ((org-on-heading-p) (org-call-with-arg 'org-todo 'previousset))
  11241. (t (org-shiftcursor-error))))
  11242. (defun org-ctrl-c-ret ()
  11243. "Call `org-table-hline-and-move' or `org-insert-heading' dep. on context."
  11244. (interactive)
  11245. (cond
  11246. ((org-at-table-p) (call-interactively 'org-table-hline-and-move))
  11247. (t (call-interactively 'org-insert-heading))))
  11248. (defun org-copy-special ()
  11249. "Copy region in table or copy current subtree.
  11250. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  11251. See the individual commands for more information."
  11252. (interactive)
  11253. (call-interactively
  11254. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  11255. (defun org-cut-special ()
  11256. "Cut region in table or cut current subtree.
  11257. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  11258. See the individual commands for more information."
  11259. (interactive)
  11260. (call-interactively
  11261. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  11262. (defun org-paste-special (arg)
  11263. "Paste rectangular region into table, or past subtree relative to level.
  11264. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  11265. See the individual commands for more information."
  11266. (interactive "P")
  11267. (if (org-at-table-p)
  11268. (org-table-paste-rectangle)
  11269. (org-paste-subtree arg)))
  11270. (defun org-ctrl-c-ctrl-c (&optional arg)
  11271. "Set tags in headline, or update according to changed information at point.
  11272. This command does many different things, depending on context:
  11273. - If the cursor is in a headline, prompt for tags and insert them
  11274. into the current line, aligned to `org-tags-column'. When called
  11275. with prefix arg, realign all tags in the current buffer.
  11276. - If the cursor is in one of the special #+KEYWORD lines, this
  11277. triggers scanning the buffer for these lines and updating the
  11278. information.
  11279. - If the cursor is inside a table, realign the table. This command
  11280. works even if the automatic table editor has been turned off.
  11281. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  11282. the entire table.
  11283. - If the cursor is a the beginning of a dynamic block, update it.
  11284. - If the cursor is inside a table created by the table.el package,
  11285. activate that table.
  11286. - If the current buffer is a remember buffer, close note and file it.
  11287. with a prefix argument, file it without further interaction to the default
  11288. location.
  11289. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  11290. links in this buffer.
  11291. - If the cursor is on a numbered item in a plain list, renumber the
  11292. ordered list.
  11293. - If the cursor is on a checkbox, toggle it."
  11294. (interactive "P")
  11295. (let ((org-enable-table-editor t))
  11296. (cond
  11297. ((or (and (boundp 'org-clock-overlays) org-clock-overlays)
  11298. org-occur-highlights
  11299. org-latex-fragment-image-overlays)
  11300. (and (boundp 'org-clock-overlays) (org-remove-clock-overlays))
  11301. (org-remove-occur-highlights)
  11302. (org-remove-latex-fragment-image-overlays)
  11303. (message "Temporary highlights/overlays removed from current buffer"))
  11304. ((and (local-variable-p 'org-finish-function (current-buffer))
  11305. (fboundp org-finish-function))
  11306. (funcall org-finish-function))
  11307. ((org-at-property-p)
  11308. (call-interactively 'org-property-action))
  11309. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  11310. ((org-on-heading-p) (call-interactively 'org-set-tags))
  11311. ((org-at-table.el-p)
  11312. (require 'table)
  11313. (beginning-of-line 1)
  11314. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  11315. (call-interactively 'table-recognize-table))
  11316. ((org-at-table-p)
  11317. (org-table-maybe-eval-formula)
  11318. (if arg
  11319. (call-interactively 'org-table-recalculate)
  11320. (org-table-maybe-recalculate-line))
  11321. (call-interactively 'org-table-align))
  11322. ((org-at-item-checkbox-p)
  11323. (call-interactively 'org-toggle-checkbox))
  11324. ((org-at-item-p)
  11325. (call-interactively 'org-maybe-renumber-ordered-list))
  11326. ((save-excursion (beginning-of-line 1) (looking-at "#\\+BEGIN:"))
  11327. ;; Dynamic block
  11328. (beginning-of-line 1)
  11329. (org-update-dblock))
  11330. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  11331. (cond
  11332. ((equal (match-string 1) "TBLFM")
  11333. ;; Recalculate the table before this line
  11334. (save-excursion
  11335. (beginning-of-line 1)
  11336. (skip-chars-backward " \r\n\t")
  11337. (if (org-at-table-p)
  11338. (org-call-with-arg 'org-table-recalculate t))))
  11339. (t
  11340. (call-interactively 'org-mode-restart))))
  11341. (t (error "C-c C-c can do nothing useful at this location.")))))
  11342. (defun org-mode-restart ()
  11343. "Restart Org-mode, to scan again for special lines.
  11344. Also updates the keyword regular expressions."
  11345. (interactive)
  11346. (let ((org-inhibit-startup t)) (org-mode))
  11347. (message "Org-mode restarted to refresh keyword and special line setup"))
  11348. (defun org-kill-note-or-show-branches ()
  11349. "If this is a Note buffer, abort storing the note. Else call `show-branches'."
  11350. (interactive)
  11351. (if (not org-finish-function)
  11352. (call-interactively 'show-branches)
  11353. (let ((org-note-abort t))
  11354. (funcall org-finish-function))))
  11355. (defun org-return (&optional indent)
  11356. "Goto next table row or insert a newline.
  11357. Calls `org-table-next-row' or `newline', depending on context.
  11358. See the individual commands for more information."
  11359. (interactive)
  11360. (cond
  11361. ((bobp) (if indent (newline-and-indent) (newline)))
  11362. ((and (org-at-heading-p)
  11363. (looking-at
  11364. (org-re "\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$")))
  11365. (org-show-entry)
  11366. (end-of-line 1)
  11367. (newline))
  11368. ((org-at-table-p)
  11369. (org-table-justify-field-maybe)
  11370. (call-interactively 'org-table-next-row))
  11371. (t (if indent (newline-and-indent) (newline)))))
  11372. (defun org-return-indent ()
  11373. "Goto next table row or insert a newline and indent.
  11374. Calls `org-table-next-row' or `newline-and-indent', depending on
  11375. context. See the individual commands for more information."
  11376. (interactive)
  11377. (org-return t))
  11378. (defun org-ctrl-c-star ()
  11379. "Compute table, or change heading status of lines.
  11380. Calls `org-table-recalculate' or `org-toggle-region-headlines',
  11381. depending on context. This will also turn a plain list item or a normal
  11382. line into a subheading."
  11383. (interactive)
  11384. (cond
  11385. ((org-at-table-p)
  11386. (call-interactively 'org-table-recalculate))
  11387. ((org-region-active-p)
  11388. ;; Convert all lines in region to list items
  11389. (call-interactively 'org-toggle-region-headings))
  11390. ((org-on-heading-p)
  11391. (org-toggle-region-headings (point-at-bol)
  11392. (min (1+ (point-at-eol)) (point-max))))
  11393. ((org-at-item-p)
  11394. ;; Convert to heading
  11395. (let ((level (save-match-data
  11396. (save-excursion
  11397. (condition-case nil
  11398. (progn
  11399. (org-back-to-heading t)
  11400. (funcall outline-level))
  11401. (error 0))))))
  11402. (replace-match
  11403. (concat (make-string (org-get-valid-level level 1) ?*) " ") t t)))
  11404. (t (org-toggle-region-headings (point-at-bol)
  11405. (min (1+ (point-at-eol)) (point-max))))))
  11406. (defun org-ctrl-c-minus ()
  11407. "Insert separator line in table or modify bullet status of line.
  11408. Also turns a plain line or a region of lines into list items.
  11409. Calls `org-table-insert-hline', `org-toggle-region-items', or
  11410. `org-cycle-list-bullet', depending on context."
  11411. (interactive)
  11412. (cond
  11413. ((org-at-table-p)
  11414. (call-interactively 'org-table-insert-hline))
  11415. ((org-on-heading-p)
  11416. ;; Convert to item
  11417. (save-excursion
  11418. (beginning-of-line 1)
  11419. (if (looking-at "\\*+ ")
  11420. (replace-match (concat (make-string (- (match-end 0) (point) 1) ?\ ) "- ")))))
  11421. ((org-region-active-p)
  11422. ;; Convert all lines in region to list items
  11423. (call-interactively 'org-toggle-region-items))
  11424. ((org-in-item-p)
  11425. (call-interactively 'org-cycle-list-bullet))
  11426. (t (org-toggle-region-items (point-at-bol)
  11427. (min (1+ (point-at-eol)) (point-max))))))
  11428. (defun org-toggle-region-items (beg end)
  11429. "Convert all lines in region to list items.
  11430. If the first line is already an item, convert all list items in the region
  11431. to normal lines."
  11432. (interactive "r")
  11433. (let (l2 l)
  11434. (save-excursion
  11435. (goto-char end)
  11436. (setq l2 (org-current-line))
  11437. (goto-char beg)
  11438. (beginning-of-line 1)
  11439. (setq l (1- (org-current-line)))
  11440. (if (org-at-item-p)
  11441. ;; We already have items, de-itemize
  11442. (while (< (setq l (1+ l)) l2)
  11443. (when (org-at-item-p)
  11444. (goto-char (match-beginning 2))
  11445. (delete-region (match-beginning 2) (match-end 2))
  11446. (and (looking-at "[ \t]+") (replace-match "")))
  11447. (beginning-of-line 2))
  11448. (while (< (setq l (1+ l)) l2)
  11449. (unless (org-at-item-p)
  11450. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11451. (replace-match "\\1- \\2")))
  11452. (beginning-of-line 2))))))
  11453. (defun org-toggle-region-headings (beg end)
  11454. "Convert all lines in region to list items.
  11455. If the first line is already an item, convert all list items in the region
  11456. to normal lines."
  11457. (interactive "r")
  11458. (let (l2 l)
  11459. (save-excursion
  11460. (goto-char end)
  11461. (setq l2 (org-current-line))
  11462. (goto-char beg)
  11463. (beginning-of-line 1)
  11464. (setq l (1- (org-current-line)))
  11465. (if (org-on-heading-p)
  11466. ;; We already have headlines, de-star them
  11467. (while (< (setq l (1+ l)) l2)
  11468. (when (org-on-heading-p t)
  11469. (and (looking-at outline-regexp) (replace-match "")))
  11470. (beginning-of-line 2))
  11471. (let* ((stars (save-excursion
  11472. (re-search-backward org-complex-heading-regexp nil t)
  11473. (or (match-string 1) "*")))
  11474. (add-stars (if org-odd-levels-only "**" "*"))
  11475. (rpl (concat stars add-stars " \\2")))
  11476. (while (< (setq l (1+ l)) l2)
  11477. (unless (org-on-heading-p)
  11478. (if (looking-at "\\([ \t]*\\)\\(\\S-\\)")
  11479. (replace-match rpl)))
  11480. (beginning-of-line 2)))))))
  11481. (defun org-meta-return (&optional arg)
  11482. "Insert a new heading or wrap a region in a table.
  11483. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  11484. See the individual commands for more information."
  11485. (interactive "P")
  11486. (cond
  11487. ((org-at-table-p)
  11488. (call-interactively 'org-table-wrap-region))
  11489. (t (call-interactively 'org-insert-heading))))
  11490. ;;; Menu entries
  11491. ;; Define the Org-mode menus
  11492. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  11493. '("Tbl"
  11494. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p)]
  11495. ["Next Field" org-cycle (org-at-table-p)]
  11496. ["Previous Field" org-shifttab (org-at-table-p)]
  11497. ["Next Row" org-return (org-at-table-p)]
  11498. "--"
  11499. ["Blank Field" org-table-blank-field (org-at-table-p)]
  11500. ["Edit Field" org-table-edit-field (org-at-table-p)]
  11501. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  11502. "--"
  11503. ("Column"
  11504. ["Move Column Left" org-metaleft (org-at-table-p)]
  11505. ["Move Column Right" org-metaright (org-at-table-p)]
  11506. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  11507. ["Insert Column" org-shiftmetaright (org-at-table-p)])
  11508. ("Row"
  11509. ["Move Row Up" org-metaup (org-at-table-p)]
  11510. ["Move Row Down" org-metadown (org-at-table-p)]
  11511. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  11512. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  11513. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  11514. "--"
  11515. ["Insert Hline" org-ctrl-c-minus (org-at-table-p)])
  11516. ("Rectangle"
  11517. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  11518. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  11519. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  11520. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  11521. "--"
  11522. ("Calculate"
  11523. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  11524. ["Set Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  11525. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  11526. "--"
  11527. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  11528. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  11529. ["Iterate all" (lambda () (interactive) (org-table-recalculate '(16))) :active (org-at-table-p) :keys "C-u C-u C-c *"]
  11530. "--"
  11531. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  11532. "--"
  11533. ["Sum Column/Rectangle" org-table-sum
  11534. (or (org-at-table-p) (org-region-active-p))]
  11535. ["Which Column?" org-table-current-column (org-at-table-p)])
  11536. ["Debug Formulas"
  11537. org-table-toggle-formula-debugger
  11538. :style toggle :selected (org-bound-and-true-p org-table-formula-debug)]
  11539. ["Show Col/Row Numbers"
  11540. org-table-toggle-coordinate-overlays
  11541. :style toggle
  11542. :selected (org-bound-and-true-p org-table-overlay-coordinates)]
  11543. "--"
  11544. ["Create" org-table-create (and (not (org-at-table-p))
  11545. org-enable-table-editor)]
  11546. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  11547. ["Import from File" org-table-import (not (org-at-table-p))]
  11548. ["Export to File" org-table-export (org-at-table-p)]
  11549. "--"
  11550. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  11551. (easy-menu-define org-org-menu org-mode-map "Org menu"
  11552. '("Org"
  11553. ("Show/Hide"
  11554. ["Cycle Visibility" org-cycle :active (or (bobp) (outline-on-heading-p))]
  11555. ["Cycle Global Visibility" org-shifttab :active (not (org-at-table-p))]
  11556. ["Sparse Tree..." org-sparse-tree t]
  11557. ["Reveal Context" org-reveal t]
  11558. ["Show All" show-all t]
  11559. "--"
  11560. ["Subtree to indirect buffer" org-tree-to-indirect-buffer t])
  11561. "--"
  11562. ["New Heading" org-insert-heading t]
  11563. ("Navigate Headings"
  11564. ["Up" outline-up-heading t]
  11565. ["Next" outline-next-visible-heading t]
  11566. ["Previous" outline-previous-visible-heading t]
  11567. ["Next Same Level" outline-forward-same-level t]
  11568. ["Previous Same Level" outline-backward-same-level t]
  11569. "--"
  11570. ["Jump" org-goto t])
  11571. ("Edit Structure"
  11572. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  11573. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  11574. "--"
  11575. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  11576. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  11577. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  11578. "--"
  11579. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  11580. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  11581. ["Demote Heading" org-metaright (not (org-at-table-p))]
  11582. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  11583. "--"
  11584. ["Sort Region/Children" org-sort (not (org-at-table-p))]
  11585. "--"
  11586. ["Convert to odd levels" org-convert-to-odd-levels t]
  11587. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  11588. ("Editing"
  11589. ["Emphasis..." org-emphasize t])
  11590. ("Archive"
  11591. ["Toggle ARCHIVE tag" org-toggle-archive-tag t]
  11592. ; ["Check and Tag Children" (org-toggle-archive-tag (4))
  11593. ; :active t :keys "C-u C-c C-x C-a"]
  11594. ["Sparse trees open ARCHIVE trees"
  11595. (setq org-sparse-tree-open-archived-trees
  11596. (not org-sparse-tree-open-archived-trees))
  11597. :style toggle :selected org-sparse-tree-open-archived-trees]
  11598. ["Cycling opens ARCHIVE trees"
  11599. (setq org-cycle-open-archived-trees (not org-cycle-open-archived-trees))
  11600. :style toggle :selected org-cycle-open-archived-trees]
  11601. ["Agenda includes ARCHIVE trees"
  11602. (setq org-agenda-skip-archived-trees (not org-agenda-skip-archived-trees))
  11603. :style toggle :selected (not org-agenda-skip-archived-trees)]
  11604. "--"
  11605. ["Move Subtree to Archive" org-advertized-archive-subtree t]
  11606. ; ["Check and Move Children" (org-archive-subtree '(4))
  11607. ; :active t :keys "C-u C-c C-x C-s"]
  11608. )
  11609. "--"
  11610. ("TODO Lists"
  11611. ["TODO/DONE/-" org-todo t]
  11612. ("Select keyword"
  11613. ["Next keyword" org-shiftright (org-on-heading-p)]
  11614. ["Previous keyword" org-shiftleft (org-on-heading-p)]
  11615. ["Complete Keyword" org-complete (assq :todo-keyword (org-context))]
  11616. ["Next keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))]
  11617. ["Previous keyword set" org-shiftcontrolright (and (> (length org-todo-sets) 1) (org-on-heading-p))])
  11618. ["Show TODO Tree" org-show-todo-tree t]
  11619. ["Global TODO list" org-todo-list t]
  11620. "--"
  11621. ["Set Priority" org-priority t]
  11622. ["Priority Up" org-shiftup t]
  11623. ["Priority Down" org-shiftdown t])
  11624. ("TAGS and Properties"
  11625. ["Set Tags" 'org-ctrl-c-ctrl-c (org-at-heading-p)]
  11626. ["Change tag in region" 'org-change-tag-in-region (org-region-active-p)]
  11627. "--"
  11628. ["Set property" 'org-set-property t]
  11629. ["Column view of properties" org-columns t]
  11630. ["Insert Column View DBlock" org-insert-columns-dblock t])
  11631. ("Dates and Scheduling"
  11632. ["Timestamp" org-time-stamp t]
  11633. ["Timestamp (inactive)" org-time-stamp-inactive t]
  11634. ("Change Date"
  11635. ["1 Day Later" org-shiftright t]
  11636. ["1 Day Earlier" org-shiftleft t]
  11637. ["1 ... Later" org-shiftup t]
  11638. ["1 ... Earlier" org-shiftdown t])
  11639. ["Compute Time Range" org-evaluate-time-range t]
  11640. ["Schedule Item" org-schedule t]
  11641. ["Deadline" org-deadline t]
  11642. "--"
  11643. ["Custom time format" org-toggle-time-stamp-overlays
  11644. :style radio :selected org-display-custom-times]
  11645. "--"
  11646. ["Goto Calendar" org-goto-calendar t]
  11647. ["Date from Calendar" org-date-from-calendar t])
  11648. ("Logging work"
  11649. ["Clock in" org-clock-in t]
  11650. ["Clock out" org-clock-out t]
  11651. ["Clock cancel" org-clock-cancel t]
  11652. ["Goto running clock" org-clock-goto t]
  11653. ["Display times" org-clock-display t]
  11654. ["Create clock table" org-clock-report t]
  11655. "--"
  11656. ["Record DONE time"
  11657. (progn (setq org-log-done (not org-log-done))
  11658. (message "Switching to %s will %s record a timestamp"
  11659. (car org-done-keywords)
  11660. (if org-log-done "automatically" "not")))
  11661. :style toggle :selected org-log-done])
  11662. "--"
  11663. ["Agenda Command..." org-agenda t]
  11664. ["Set Restriction Lock" org-agenda-set-restriction-lock t]
  11665. ("File List for Agenda")
  11666. ("Special views current file"
  11667. ["TODO Tree" org-show-todo-tree t]
  11668. ["Check Deadlines" org-check-deadlines t]
  11669. ["Timeline" org-timeline t]
  11670. ["Tags Tree" org-tags-sparse-tree t])
  11671. "--"
  11672. ("Hyperlinks"
  11673. ["Store Link (Global)" org-store-link t]
  11674. ["Insert Link" org-insert-link t]
  11675. ["Follow Link" org-open-at-point t]
  11676. "--"
  11677. ["Next link" org-next-link t]
  11678. ["Previous link" org-previous-link t]
  11679. "--"
  11680. ["Descriptive Links"
  11681. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  11682. :style radio
  11683. :selected (member '(org-link) buffer-invisibility-spec)]
  11684. ["Literal Links"
  11685. (progn
  11686. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  11687. :style radio
  11688. :selected (not (member '(org-link) buffer-invisibility-spec))])
  11689. "--"
  11690. ["Export/Publish..." org-export t]
  11691. ("LaTeX"
  11692. ["Org CDLaTeX mode" org-cdlatex-mode :style toggle
  11693. :selected org-cdlatex-mode]
  11694. ["Insert Environment" cdlatex-environment (fboundp 'cdlatex-environment)]
  11695. ["Insert math symbol" cdlatex-math-symbol (fboundp 'cdlatex-math-symbol)]
  11696. ["Modify math symbol" org-cdlatex-math-modify
  11697. (org-inside-LaTeX-fragment-p)]
  11698. ["Export LaTeX fragments as images"
  11699. (if (featurep 'org-exp)
  11700. (setq org-export-with-LaTeX-fragments
  11701. (not org-export-with-LaTeX-fragments))
  11702. (require 'org-exp))
  11703. :style toggle :selected (and (boundp 'org-export-with-LaTeX-fragments)
  11704. org-export-with-LaTeX-fragments)])
  11705. "--"
  11706. ("Documentation"
  11707. ["Show Version" org-version t]
  11708. ["Info Documentation" org-info t])
  11709. ("Customize"
  11710. ["Browse Org Group" org-customize t]
  11711. "--"
  11712. ["Expand This Menu" org-create-customize-menu
  11713. (fboundp 'customize-menu-create)])
  11714. "--"
  11715. ["Refresh setup" org-mode-restart t]
  11716. ))
  11717. (defun org-info (&optional node)
  11718. "Read documentation for Org-mode in the info system.
  11719. With optional NODE, go directly to that node."
  11720. (interactive)
  11721. (info (format "(org)%s" (or node ""))))
  11722. (defun org-install-agenda-files-menu ()
  11723. (let ((bl (buffer-list)))
  11724. (save-excursion
  11725. (while bl
  11726. (set-buffer (pop bl))
  11727. (if (org-mode-p) (setq bl nil)))
  11728. (when (org-mode-p)
  11729. (easy-menu-change
  11730. '("Org") "File List for Agenda"
  11731. (append
  11732. (list
  11733. ["Edit File List" (org-edit-agenda-file-list) t]
  11734. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  11735. ["Remove Current File from List" org-remove-file t]
  11736. ["Cycle through agenda files" org-cycle-agenda-files t]
  11737. ["Occur in all agenda files" org-occur-in-agenda-files t]
  11738. "--")
  11739. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  11740. ;;;; Documentation
  11741. (defun org-require-autoloaded-modules ()
  11742. (interactive)
  11743. (mapc 'require
  11744. '(org-agenda org-archive org-clock org-colview
  11745. org-exp org-export-latex org-publish
  11746. org-remember org-table)))
  11747. (defun org-customize ()
  11748. "Call the customize function with org as argument."
  11749. (interactive)
  11750. (org-load-modules-maybe)
  11751. (org-require-autoloaded-modules)
  11752. (customize-browse 'org))
  11753. (defun org-create-customize-menu ()
  11754. "Create a full customization menu for Org-mode, insert it into the menu."
  11755. (interactive)
  11756. (org-load-modules-maybe)
  11757. (org-require-autoloaded-modules)
  11758. (if (fboundp 'customize-menu-create)
  11759. (progn
  11760. (easy-menu-change
  11761. '("Org") "Customize"
  11762. `(["Browse Org group" org-customize t]
  11763. "--"
  11764. ,(customize-menu-create 'org)
  11765. ["Set" Custom-set t]
  11766. ["Save" Custom-save t]
  11767. ["Reset to Current" Custom-reset-current t]
  11768. ["Reset to Saved" Custom-reset-saved t]
  11769. ["Reset to Standard Settings" Custom-reset-standard t]))
  11770. (message "\"Org\"-menu now contains full customization menu"))
  11771. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  11772. ;;;; Miscellaneous stuff
  11773. ;;; Generally useful functions
  11774. (defun org-display-warning (message) ;; Copied from Emacs-Muse
  11775. "Display the given MESSAGE as a warning."
  11776. (if (fboundp 'display-warning)
  11777. (display-warning 'org message
  11778. (if (featurep 'xemacs)
  11779. 'warning
  11780. :warning))
  11781. (let ((buf (get-buffer-create "*Org warnings*")))
  11782. (with-current-buffer buf
  11783. (goto-char (point-max))
  11784. (insert "Warning (Org): " message)
  11785. (unless (bolp)
  11786. (newline)))
  11787. (display-buffer buf)
  11788. (sit-for 0))))
  11789. (defun org-plist-delete (plist property)
  11790. "Delete PROPERTY from PLIST.
  11791. This is in contrast to merely setting it to 0."
  11792. (let (p)
  11793. (while plist
  11794. (if (not (eq property (car plist)))
  11795. (setq p (plist-put p (car plist) (nth 1 plist))))
  11796. (setq plist (cddr plist)))
  11797. p))
  11798. (defun org-force-self-insert (N)
  11799. "Needed to enforce self-insert under remapping."
  11800. (interactive "p")
  11801. (self-insert-command N))
  11802. (defun org-string-width (s)
  11803. "Compute width of string, ignoring invisible characters.
  11804. This ignores character with invisibility property `org-link', and also
  11805. characters with property `org-cwidth', because these will become invisible
  11806. upon the next fontification round."
  11807. (let (b l)
  11808. (when (or (eq t buffer-invisibility-spec)
  11809. (assq 'org-link buffer-invisibility-spec))
  11810. (while (setq b (text-property-any 0 (length s)
  11811. 'invisible 'org-link s))
  11812. (setq s (concat (substring s 0 b)
  11813. (substring s (or (next-single-property-change
  11814. b 'invisible s) (length s)))))))
  11815. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  11816. (setq s (concat (substring s 0 b)
  11817. (substring s (or (next-single-property-change
  11818. b 'org-cwidth s) (length s))))))
  11819. (setq l (string-width s) b -1)
  11820. (while (setq b (text-property-any (1+ b) (length s) 'org-dwidth t s))
  11821. (setq l (- l (get-text-property b 'org-dwidth-n s))))
  11822. l))
  11823. (defun org-base-buffer (buffer)
  11824. "Return the base buffer of BUFFER, if it has one. Else return the buffer."
  11825. (if (not buffer)
  11826. buffer
  11827. (or (buffer-base-buffer buffer)
  11828. buffer)))
  11829. (defun org-trim (s)
  11830. "Remove whitespace at beginning and end of string."
  11831. (if (string-match "\\`[ \t\n\r]+" s) (setq s (replace-match "" t t s)))
  11832. (if (string-match "[ \t\n\r]+\\'" s) (setq s (replace-match "" t t s)))
  11833. s)
  11834. (defun org-wrap (string &optional width lines)
  11835. "Wrap string to either a number of lines, or a width in characters.
  11836. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  11837. that costs. If there is a word longer than WIDTH, the text is actually
  11838. wrapped to the length of that word.
  11839. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  11840. many lines, whatever width that takes.
  11841. The return value is a list of lines, without newlines at the end."
  11842. (let* ((words (org-split-string string "[ \t\n]+"))
  11843. (maxword (apply 'max (mapcar 'org-string-width words)))
  11844. w ll)
  11845. (cond (width
  11846. (org-do-wrap words (max maxword width)))
  11847. (lines
  11848. (setq w maxword)
  11849. (setq ll (org-do-wrap words maxword))
  11850. (if (<= (length ll) lines)
  11851. ll
  11852. (setq ll words)
  11853. (while (> (length ll) lines)
  11854. (setq w (1+ w))
  11855. (setq ll (org-do-wrap words w)))
  11856. ll))
  11857. (t (error "Cannot wrap this")))))
  11858. (defun org-do-wrap (words width)
  11859. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  11860. (let (lines line)
  11861. (while words
  11862. (setq line (pop words))
  11863. (while (and words (< (+ (length line) (length (car words))) width))
  11864. (setq line (concat line " " (pop words))))
  11865. (setq lines (push line lines)))
  11866. (nreverse lines)))
  11867. (defun org-split-string (string &optional separators)
  11868. "Splits STRING into substrings at SEPARATORS.
  11869. No empty strings are returned if there are matches at the beginning
  11870. and end of string."
  11871. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  11872. (start 0)
  11873. notfirst
  11874. (list nil))
  11875. (while (and (string-match rexp string
  11876. (if (and notfirst
  11877. (= start (match-beginning 0))
  11878. (< start (length string)))
  11879. (1+ start) start))
  11880. (< (match-beginning 0) (length string)))
  11881. (setq notfirst t)
  11882. (or (eq (match-beginning 0) 0)
  11883. (and (eq (match-beginning 0) (match-end 0))
  11884. (eq (match-beginning 0) start))
  11885. (setq list
  11886. (cons (substring string start (match-beginning 0))
  11887. list)))
  11888. (setq start (match-end 0)))
  11889. (or (eq start (length string))
  11890. (setq list
  11891. (cons (substring string start)
  11892. list)))
  11893. (nreverse list)))
  11894. (defun org-context ()
  11895. "Return a list of contexts of the current cursor position.
  11896. If several contexts apply, all are returned.
  11897. Each context entry is a list with a symbol naming the context, and
  11898. two positions indicating start and end of the context. Possible
  11899. contexts are:
  11900. :headline anywhere in a headline
  11901. :headline-stars on the leading stars in a headline
  11902. :todo-keyword on a TODO keyword (including DONE) in a headline
  11903. :tags on the TAGS in a headline
  11904. :priority on the priority cookie in a headline
  11905. :item on the first line of a plain list item
  11906. :item-bullet on the bullet/number of a plain list item
  11907. :checkbox on the checkbox in a plain list item
  11908. :table in an org-mode table
  11909. :table-special on a special filed in a table
  11910. :table-table in a table.el table
  11911. :link on a hyperlink
  11912. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  11913. :target on a <<target>>
  11914. :radio-target on a <<<radio-target>>>
  11915. :latex-fragment on a LaTeX fragment
  11916. :latex-preview on a LaTeX fragment with overlayed preview image
  11917. This function expects the position to be visible because it uses font-lock
  11918. faces as a help to recognize the following contexts: :table-special, :link,
  11919. and :keyword."
  11920. (let* ((f (get-text-property (point) 'face))
  11921. (faces (if (listp f) f (list f)))
  11922. (p (point)) clist o)
  11923. ;; First the large context
  11924. (cond
  11925. ((org-on-heading-p t)
  11926. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  11927. (when (progn
  11928. (beginning-of-line 1)
  11929. (looking-at org-todo-line-tags-regexp))
  11930. (push (org-point-in-group p 1 :headline-stars) clist)
  11931. (push (org-point-in-group p 2 :todo-keyword) clist)
  11932. (push (org-point-in-group p 4 :tags) clist))
  11933. (goto-char p)
  11934. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  11935. (if (looking-at "\\[#[A-Z0-9]\\]")
  11936. (push (org-point-in-group p 0 :priority) clist)))
  11937. ((org-at-item-p)
  11938. (push (org-point-in-group p 2 :item-bullet) clist)
  11939. (push (list :item (point-at-bol)
  11940. (save-excursion (org-end-of-item) (point)))
  11941. clist)
  11942. (and (org-at-item-checkbox-p)
  11943. (push (org-point-in-group p 0 :checkbox) clist)))
  11944. ((org-at-table-p)
  11945. (push (list :table (org-table-begin) (org-table-end)) clist)
  11946. (if (memq 'org-formula faces)
  11947. (push (list :table-special
  11948. (previous-single-property-change p 'face)
  11949. (next-single-property-change p 'face)) clist)))
  11950. ((org-at-table-p 'any)
  11951. (push (list :table-table) clist)))
  11952. (goto-char p)
  11953. ;; Now the small context
  11954. (cond
  11955. ((org-at-timestamp-p)
  11956. (push (org-point-in-group p 0 :timestamp) clist))
  11957. ((memq 'org-link faces)
  11958. (push (list :link
  11959. (previous-single-property-change p 'face)
  11960. (next-single-property-change p 'face)) clist))
  11961. ((memq 'org-special-keyword faces)
  11962. (push (list :keyword
  11963. (previous-single-property-change p 'face)
  11964. (next-single-property-change p 'face)) clist))
  11965. ((org-on-target-p)
  11966. (push (org-point-in-group p 0 :target) clist)
  11967. (goto-char (1- (match-beginning 0)))
  11968. (if (looking-at org-radio-target-regexp)
  11969. (push (org-point-in-group p 0 :radio-target) clist))
  11970. (goto-char p))
  11971. ((setq o (car (delq nil
  11972. (mapcar
  11973. (lambda (x)
  11974. (if (memq x org-latex-fragment-image-overlays) x))
  11975. (org-overlays-at (point))))))
  11976. (push (list :latex-fragment
  11977. (org-overlay-start o) (org-overlay-end o)) clist)
  11978. (push (list :latex-preview
  11979. (org-overlay-start o) (org-overlay-end o)) clist))
  11980. ((org-inside-LaTeX-fragment-p)
  11981. ;; FIXME: positions wrong.
  11982. (push (list :latex-fragment (point) (point)) clist)))
  11983. (setq clist (nreverse (delq nil clist)))
  11984. clist))
  11985. ;; FIXME: Compare with at-regexp-p Do we need both?
  11986. (defun org-in-regexp (re &optional nlines visually)
  11987. "Check if point is inside a match of regexp.
  11988. Normally only the current line is checked, but you can include NLINES extra
  11989. lines both before and after point into the search.
  11990. If VISUALLY is set, require that the cursor is not after the match but
  11991. really on, so that the block visually is on the match."
  11992. (catch 'exit
  11993. (let ((pos (point))
  11994. (eol (point-at-eol (+ 1 (or nlines 0))))
  11995. (inc (if visually 1 0)))
  11996. (save-excursion
  11997. (beginning-of-line (- 1 (or nlines 0)))
  11998. (while (re-search-forward re eol t)
  11999. (if (and (<= (match-beginning 0) pos)
  12000. (>= (+ inc (match-end 0)) pos))
  12001. (throw 'exit (cons (match-beginning 0) (match-end 0)))))))))
  12002. (defun org-at-regexp-p (regexp)
  12003. "Is point inside a match of REGEXP in the current line?"
  12004. (catch 'exit
  12005. (save-excursion
  12006. (let ((pos (point)) (end (point-at-eol)))
  12007. (beginning-of-line 1)
  12008. (while (re-search-forward regexp end t)
  12009. (if (and (<= (match-beginning 0) pos)
  12010. (>= (match-end 0) pos))
  12011. (throw 'exit t)))
  12012. nil))))
  12013. (defun org-occur-in-agenda-files (regexp &optional nlines)
  12014. "Call `multi-occur' with buffers for all agenda files."
  12015. (interactive "sOrg-files matching: \np")
  12016. (let* ((files (org-agenda-files))
  12017. (tnames (mapcar 'file-truename files))
  12018. (extra org-agenda-text-search-extra-files)
  12019. f)
  12020. (when (eq (car extra) 'agenda-archives)
  12021. (setq extra (cdr extra))
  12022. (setq files (org-add-archive-files files)))
  12023. (while (setq f (pop extra))
  12024. (unless (member (file-truename f) tnames)
  12025. (add-to-list 'files f 'append)
  12026. (add-to-list 'tnames (file-truename f) 'append)))
  12027. (multi-occur
  12028. (mapcar (lambda (x) (or (get-file-buffer x) (find-file-noselect x))) files)
  12029. regexp)))
  12030. (if (boundp 'occur-mode-find-occurrence-hook)
  12031. ;; Emacs 23
  12032. (add-hook 'occur-mode-find-occurrence-hook
  12033. (lambda ()
  12034. (when (org-mode-p)
  12035. (org-reveal))))
  12036. ;; Emacs 22
  12037. (defadvice occur-mode-goto-occurrence
  12038. (after org-occur-reveal activate)
  12039. (and (org-mode-p) (org-reveal)))
  12040. (defadvice occur-mode-goto-occurrence-other-window
  12041. (after org-occur-reveal activate)
  12042. (and (org-mode-p) (org-reveal)))
  12043. (defadvice occur-mode-display-occurrence
  12044. (after org-occur-reveal activate)
  12045. (when (org-mode-p)
  12046. (let ((pos (occur-mode-find-occurrence)))
  12047. (with-current-buffer (marker-buffer pos)
  12048. (save-excursion
  12049. (goto-char pos)
  12050. (org-reveal)))))))
  12051. (defun org-uniquify (list)
  12052. "Remove duplicate elements from LIST."
  12053. (let (res)
  12054. (mapc (lambda (x) (add-to-list 'res x 'append)) list)
  12055. res))
  12056. (defun org-delete-all (elts list)
  12057. "Remove all elements in ELTS from LIST."
  12058. (while elts
  12059. (setq list (delete (pop elts) list)))
  12060. list)
  12061. (defun org-back-over-empty-lines ()
  12062. "Move backwards over witespace, to the beginning of the first empty line.
  12063. Returns the number of empty lines passed."
  12064. (let ((pos (point)))
  12065. (skip-chars-backward " \t\n\r")
  12066. (beginning-of-line 2)
  12067. (goto-char (min (point) pos))
  12068. (count-lines (point) pos)))
  12069. (defun org-skip-whitespace ()
  12070. (skip-chars-forward " \t\n\r"))
  12071. (defun org-point-in-group (point group &optional context)
  12072. "Check if POINT is in match-group GROUP.
  12073. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  12074. match. If the match group does ot exist or point is not inside it,
  12075. return nil."
  12076. (and (match-beginning group)
  12077. (>= point (match-beginning group))
  12078. (<= point (match-end group))
  12079. (if context
  12080. (list context (match-beginning group) (match-end group))
  12081. t)))
  12082. (defun org-switch-to-buffer-other-window (&rest args)
  12083. "Switch to buffer in a second window on the current frame.
  12084. In particular, do not allow pop-up frames."
  12085. (let (pop-up-frames special-display-buffer-names special-display-regexps
  12086. special-display-function)
  12087. (apply 'switch-to-buffer-other-window args)))
  12088. (defun org-combine-plists (&rest plists)
  12089. "Create a single property list from all plists in PLISTS.
  12090. The process starts by copying the first list, and then setting properties
  12091. from the other lists. Settings in the last list are the most significant
  12092. ones and overrule settings in the other lists."
  12093. (let ((rtn (copy-sequence (pop plists)))
  12094. p v ls)
  12095. (while plists
  12096. (setq ls (pop plists))
  12097. (while ls
  12098. (setq p (pop ls) v (pop ls))
  12099. (setq rtn (plist-put rtn p v))))
  12100. rtn))
  12101. (defun org-move-line-down (arg)
  12102. "Move the current line down. With prefix argument, move it past ARG lines."
  12103. (interactive "p")
  12104. (let ((col (current-column))
  12105. beg end pos)
  12106. (beginning-of-line 1) (setq beg (point))
  12107. (beginning-of-line 2) (setq end (point))
  12108. (beginning-of-line (+ 1 arg))
  12109. (setq pos (move-marker (make-marker) (point)))
  12110. (insert (delete-and-extract-region beg end))
  12111. (goto-char pos)
  12112. (org-move-to-column col)))
  12113. (defun org-move-line-up (arg)
  12114. "Move the current line up. With prefix argument, move it past ARG lines."
  12115. (interactive "p")
  12116. (let ((col (current-column))
  12117. beg end pos)
  12118. (beginning-of-line 1) (setq beg (point))
  12119. (beginning-of-line 2) (setq end (point))
  12120. (beginning-of-line (- arg))
  12121. (setq pos (move-marker (make-marker) (point)))
  12122. (insert (delete-and-extract-region beg end))
  12123. (goto-char pos)
  12124. (org-move-to-column col)))
  12125. (defun org-replace-escapes (string table)
  12126. "Replace %-escapes in STRING with values in TABLE.
  12127. TABLE is an association list with keys like \"%a\" and string values.
  12128. The sequences in STRING may contain normal field width and padding information,
  12129. for example \"%-5s\". Replacements happen in the sequence given by TABLE,
  12130. so values can contain further %-escapes if they are define later in TABLE."
  12131. (let ((case-fold-search nil)
  12132. e re rpl)
  12133. (while (setq e (pop table))
  12134. (setq re (concat "%-?[0-9.]*" (substring (car e) 1)))
  12135. (while (string-match re string)
  12136. (setq rpl (format (concat (substring (match-string 0 string) 0 -1) "s")
  12137. (cdr e)))
  12138. (setq string (replace-match rpl t t string))))
  12139. string))
  12140. (defun org-sublist (list start end)
  12141. "Return a section of LIST, from START to END.
  12142. Counting starts at 1."
  12143. (let (rtn (c start))
  12144. (setq list (nthcdr (1- start) list))
  12145. (while (and list (<= c end))
  12146. (push (pop list) rtn)
  12147. (setq c (1+ c)))
  12148. (nreverse rtn)))
  12149. (defun org-find-base-buffer-visiting (file)
  12150. "Like `find-buffer-visiting' but alway return the base buffer and
  12151. not an indirect buffer."
  12152. (let ((buf (find-buffer-visiting file)))
  12153. (if buf
  12154. (or (buffer-base-buffer buf) buf)
  12155. nil)))
  12156. (defun org-image-file-name-regexp ()
  12157. "Return regexp matching the file names of images."
  12158. (if (fboundp 'image-file-name-regexp)
  12159. (image-file-name-regexp)
  12160. (let ((image-file-name-extensions
  12161. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  12162. "xbm" "xpm" "pbm" "pgm" "ppm")))
  12163. (concat "\\."
  12164. (regexp-opt (nconc (mapcar 'upcase
  12165. image-file-name-extensions)
  12166. image-file-name-extensions)
  12167. t)
  12168. "\\'"))))
  12169. (defun org-file-image-p (file)
  12170. "Return non-nil if FILE is an image."
  12171. (save-match-data
  12172. (string-match (org-image-file-name-regexp) file)))
  12173. ;;; Paragraph filling stuff.
  12174. ;; We want this to be just right, so use the full arsenal.
  12175. (defun org-indent-line-function ()
  12176. "Indent line like previous, but further if previous was headline or item."
  12177. (interactive)
  12178. (let* ((pos (point))
  12179. (itemp (org-at-item-p))
  12180. column bpos bcol tpos tcol bullet btype bullet-type)
  12181. ;; Find the previous relevant line
  12182. (beginning-of-line 1)
  12183. (cond
  12184. ((looking-at "#") (setq column 0))
  12185. ((looking-at "\\*+ ") (setq column 0))
  12186. (t
  12187. (beginning-of-line 0)
  12188. (while (and (not (bobp)) (looking-at "[ \t]*[\n:#|]"))
  12189. (beginning-of-line 0))
  12190. (cond
  12191. ((looking-at "\\*+[ \t]+")
  12192. (goto-char (match-end 0))
  12193. (setq column (current-column)))
  12194. ((org-in-item-p)
  12195. (org-beginning-of-item)
  12196. ; (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12197. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*\\(\\[[- X]\\][ \t]*\\)?")
  12198. (setq bpos (match-beginning 1) tpos (match-end 0)
  12199. bcol (progn (goto-char bpos) (current-column))
  12200. tcol (progn (goto-char tpos) (current-column))
  12201. bullet (match-string 1)
  12202. bullet-type (if (string-match "[0-9]" bullet) "n" bullet))
  12203. (if (not itemp)
  12204. (setq column tcol)
  12205. (goto-char pos)
  12206. (beginning-of-line 1)
  12207. (if (looking-at "\\S-")
  12208. (progn
  12209. (looking-at "[ \t]*\\(\\S-+\\)[ \t]*")
  12210. (setq bullet (match-string 1)
  12211. btype (if (string-match "[0-9]" bullet) "n" bullet))
  12212. (setq column (if (equal btype bullet-type) bcol tcol)))
  12213. (setq column (org-get-indentation)))))
  12214. (t (setq column (org-get-indentation))))))
  12215. (goto-char pos)
  12216. (if (<= (current-column) (current-indentation))
  12217. (org-indent-line-to column)
  12218. (save-excursion (org-indent-line-to column)))
  12219. (setq column (current-column))
  12220. (beginning-of-line 1)
  12221. (if (looking-at
  12222. "\\([ \t]+\\)\\(:[-_0-9a-zA-Z]+:\\)[ \t]*\\(\\S-.*\\(\\S-\\|$\\)\\)")
  12223. (replace-match (concat "\\1" (format org-property-format
  12224. (match-string 2) (match-string 3)))
  12225. t nil))
  12226. (org-move-to-column column)))
  12227. (defun org-set-autofill-regexps ()
  12228. (interactive)
  12229. ;; In the paragraph separator we include headlines, because filling
  12230. ;; text in a line directly attached to a headline would otherwise
  12231. ;; fill the headline as well.
  12232. (org-set-local 'comment-start-skip "^#+[ \t]*")
  12233. (org-set-local 'paragraph-separate "\f\\|\\*+ \\|[ ]*$\\|[ \t]*[:|]")
  12234. ;; The paragraph starter includes hand-formatted lists.
  12235. (org-set-local 'paragraph-start
  12236. "\f\\|[ ]*$\\|\\*+ \\|\f\\|[ \t]*\\([-+*][ \t]+\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  12237. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  12238. ;; But only if the user has not turned off tables or fixed-width regions
  12239. (org-set-local
  12240. 'auto-fill-inhibit-regexp
  12241. (concat "\\*+ \\|#\\+"
  12242. "\\|[ \t]*" org-keyword-time-regexp
  12243. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  12244. (concat
  12245. "\\|[ \t]*["
  12246. (if org-enable-table-editor "|" "")
  12247. (if org-enable-fixed-width-editor ":" "")
  12248. "]"))))
  12249. ;; We use our own fill-paragraph function, to make sure that tables
  12250. ;; and fixed-width regions are not wrapped. That function will pass
  12251. ;; through to `fill-paragraph' when appropriate.
  12252. (org-set-local 'fill-paragraph-function 'org-fill-paragraph)
  12253. ; Adaptive filling: To get full control, first make sure that
  12254. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  12255. (org-set-local 'adaptive-fill-regexp "\000")
  12256. (org-set-local 'adaptive-fill-function
  12257. 'org-adaptive-fill-function)
  12258. (org-set-local
  12259. 'align-mode-rules-list
  12260. '((org-in-buffer-settings
  12261. (regexp . "^#\\+[A-Z_]+:\\(\\s-*\\)\\S-+")
  12262. (modes . '(org-mode))))))
  12263. (defun org-fill-paragraph (&optional justify)
  12264. "Re-align a table, pass through to fill-paragraph if no table."
  12265. (let ((table-p (org-at-table-p))
  12266. (table.el-p (org-at-table.el-p)))
  12267. (cond ((and (equal (char-after (point-at-bol)) ?*)
  12268. (save-excursion (goto-char (point-at-bol))
  12269. (looking-at outline-regexp)))
  12270. t) ; skip headlines
  12271. (table.el-p t) ; skip table.el tables
  12272. (table-p (org-table-align) t) ; align org-mode tables
  12273. (t nil)))) ; call paragraph-fill
  12274. ;; For reference, this is the default value of adaptive-fill-regexp
  12275. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  12276. (defun org-adaptive-fill-function ()
  12277. "Return a fill prefix for org-mode files.
  12278. In particular, this makes sure hanging paragraphs for hand-formatted lists
  12279. work correctly."
  12280. (cond ((looking-at "#[ \t]+")
  12281. (match-string 0))
  12282. ((looking-at "[ \t]*\\([-*+] \\|[0-9]+[.)] \\)?")
  12283. (save-excursion
  12284. (goto-char (match-end 0))
  12285. (make-string (current-column) ?\ )))
  12286. (t nil)))
  12287. ;;; Other stuff.
  12288. (defun org-toggle-fixed-width-section (arg)
  12289. "Toggle the fixed-width export.
  12290. If there is no active region, the QUOTE keyword at the current headline is
  12291. inserted or removed. When present, it causes the text between this headline
  12292. and the next to be exported as fixed-width text, and unmodified.
  12293. If there is an active region, this command adds or removes a colon as the
  12294. first character of this line. If the first character of a line is a colon,
  12295. this line is also exported in fixed-width font."
  12296. (interactive "P")
  12297. (let* ((cc 0)
  12298. (regionp (org-region-active-p))
  12299. (beg (if regionp (region-beginning) (point)))
  12300. (end (if regionp (region-end)))
  12301. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  12302. (case-fold-search nil)
  12303. (re "[ \t]*\\(:\\)")
  12304. off)
  12305. (if regionp
  12306. (save-excursion
  12307. (goto-char beg)
  12308. (setq cc (current-column))
  12309. (beginning-of-line 1)
  12310. (setq off (looking-at re))
  12311. (while (> nlines 0)
  12312. (setq nlines (1- nlines))
  12313. (beginning-of-line 1)
  12314. (cond
  12315. (arg
  12316. (org-move-to-column cc t)
  12317. (insert ":\n")
  12318. (forward-line -1))
  12319. ((and off (looking-at re))
  12320. (replace-match "" t t nil 1))
  12321. ((not off) (org-move-to-column cc t) (insert ":")))
  12322. (forward-line 1)))
  12323. (save-excursion
  12324. (org-back-to-heading)
  12325. (if (looking-at (concat outline-regexp
  12326. "\\( *\\<" org-quote-string "\\>[ \t]*\\)"))
  12327. (replace-match "" t t nil 1)
  12328. (if (looking-at outline-regexp)
  12329. (progn
  12330. (goto-char (match-end 0))
  12331. (insert org-quote-string " "))))))))
  12332. ;;;; Functions extending outline functionality
  12333. (defun org-beginning-of-line (&optional arg)
  12334. "Go to the beginning of the current line. If that is invisible, continue
  12335. to a visible line beginning. This makes the function of C-a more intuitive.
  12336. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12337. first attempt, and only move to after the tags when the cursor is already
  12338. beyond the end of the headline."
  12339. (interactive "P")
  12340. (let ((pos (point)))
  12341. (beginning-of-line 1)
  12342. (if (bobp)
  12343. nil
  12344. (backward-char 1)
  12345. (if (org-invisible-p)
  12346. (while (and (not (bobp)) (org-invisible-p))
  12347. (backward-char 1)
  12348. (beginning-of-line 1))
  12349. (forward-char 1)))
  12350. (when org-special-ctrl-a/e
  12351. (cond
  12352. ((and (looking-at org-todo-line-regexp)
  12353. (= (char-after (match-end 1)) ?\ ))
  12354. (goto-char
  12355. (if (eq org-special-ctrl-a/e t)
  12356. (cond ((> pos (match-beginning 3)) (match-beginning 3))
  12357. ((= pos (point)) (match-beginning 3))
  12358. (t (point)))
  12359. (cond ((> pos (point)) (point))
  12360. ((not (eq last-command this-command)) (point))
  12361. (t (match-beginning 3))))))
  12362. ((org-at-item-p)
  12363. (goto-char
  12364. (if (eq org-special-ctrl-a/e t)
  12365. (cond ((> pos (match-end 4)) (match-end 4))
  12366. ((= pos (point)) (match-end 4))
  12367. (t (point)))
  12368. (cond ((> pos (point)) (point))
  12369. ((not (eq last-command this-command)) (point))
  12370. (t (match-end 4))))))))))
  12371. (defun org-end-of-line (&optional arg)
  12372. "Go to the end of the line.
  12373. If this is a headline, and `org-special-ctrl-a/e' is set, ignore tags on the
  12374. first attempt, and only move to after the tags when the cursor is already
  12375. beyond the end of the headline."
  12376. (interactive "P")
  12377. (if (or (not org-special-ctrl-a/e)
  12378. (not (org-on-heading-p)))
  12379. (end-of-line arg)
  12380. (let ((pos (point)))
  12381. (beginning-of-line 1)
  12382. (if (looking-at (org-re ".*?\\([ \t]*\\)\\(:[[:alnum:]_@:]+:\\)[ \t]*$"))
  12383. (if (eq org-special-ctrl-a/e t)
  12384. (if (or (< pos (match-beginning 1))
  12385. (= pos (match-end 0)))
  12386. (goto-char (match-beginning 1))
  12387. (goto-char (match-end 0)))
  12388. (if (or (< pos (match-end 0)) (not (eq this-command last-command)))
  12389. (goto-char (match-end 0))
  12390. (goto-char (match-beginning 1))))
  12391. (end-of-line arg)))))
  12392. (define-key org-mode-map "\C-a" 'org-beginning-of-line)
  12393. (define-key org-mode-map "\C-e" 'org-end-of-line)
  12394. (defun org-kill-line (&optional arg)
  12395. "Kill line, to tags or end of line."
  12396. (interactive "P")
  12397. (cond
  12398. ((or (not org-special-ctrl-k)
  12399. (bolp)
  12400. (not (org-on-heading-p)))
  12401. (call-interactively 'kill-line))
  12402. ((looking-at (org-re ".*?\\S-\\([ \t]+\\(:[[:alnum:]_@:]+:\\)\\)[ \t]*$"))
  12403. (kill-region (point) (match-beginning 1))
  12404. (org-set-tags nil t))
  12405. (t (kill-region (point) (point-at-eol)))))
  12406. (define-key org-mode-map "\C-k" 'org-kill-line)
  12407. (defun org-invisible-p ()
  12408. "Check if point is at a character currently not visible."
  12409. ;; Early versions of noutline don't have `outline-invisible-p'.
  12410. (if (fboundp 'outline-invisible-p)
  12411. (outline-invisible-p)
  12412. (get-char-property (point) 'invisible)))
  12413. (defun org-invisible-p2 ()
  12414. "Check if point is at a character currently not visible."
  12415. (save-excursion
  12416. (if (and (eolp) (not (bobp))) (backward-char 1))
  12417. ;; Early versions of noutline don't have `outline-invisible-p'.
  12418. (if (fboundp 'outline-invisible-p)
  12419. (outline-invisible-p)
  12420. (get-char-property (point) 'invisible))))
  12421. (defalias 'org-back-to-heading 'outline-back-to-heading)
  12422. (defalias 'org-on-heading-p 'outline-on-heading-p)
  12423. (defalias 'org-at-heading-p 'outline-on-heading-p)
  12424. (defun org-at-heading-or-item-p ()
  12425. (or (org-on-heading-p) (org-at-item-p)))
  12426. (defun org-on-target-p ()
  12427. (or (org-in-regexp org-radio-target-regexp)
  12428. (org-in-regexp org-target-regexp)))
  12429. (defun org-up-heading-all (arg)
  12430. "Move to the heading line of which the present line is a subheading.
  12431. This function considers both visible and invisible heading lines.
  12432. With argument, move up ARG levels."
  12433. (if (fboundp 'outline-up-heading-all)
  12434. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  12435. (outline-up-heading arg t))) ; emacs 22 version of outline.el
  12436. (defun org-up-heading-safe ()
  12437. "Move to the heading line of which the present line is a subheading.
  12438. This version will not throw an error. It will return the level of the
  12439. headline found, or nil if no higher level is found."
  12440. (let ((pos (point)) start-level level
  12441. (re (concat "^" outline-regexp)))
  12442. (catch 'exit
  12443. (outline-back-to-heading t)
  12444. (setq start-level (funcall outline-level))
  12445. (if (equal start-level 1) (throw 'exit nil))
  12446. (while (re-search-backward re nil t)
  12447. (setq level (funcall outline-level))
  12448. (if (< level start-level) (throw 'exit level)))
  12449. nil)))
  12450. (defun org-first-sibling-p ()
  12451. "Is this heading the first child of its parents?"
  12452. (interactive)
  12453. (let ((re (concat "^" outline-regexp))
  12454. level l)
  12455. (unless (org-at-heading-p t)
  12456. (error "Not at a heading"))
  12457. (setq level (funcall outline-level))
  12458. (save-excursion
  12459. (if (not (re-search-backward re nil t))
  12460. t
  12461. (setq l (funcall outline-level))
  12462. (< l level)))))
  12463. (defun org-goto-sibling (&optional previous)
  12464. "Goto the next sibling, even if it is invisible.
  12465. When PREVIOUS is set, go to the previous sibling instead. Returns t
  12466. when a sibling was found. When none is found, return nil and don't
  12467. move point."
  12468. (let ((fun (if previous 're-search-backward 're-search-forward))
  12469. (pos (point))
  12470. (re (concat "^" outline-regexp))
  12471. level l)
  12472. (when (condition-case nil (org-back-to-heading t) (error nil))
  12473. (setq level (funcall outline-level))
  12474. (catch 'exit
  12475. (or previous (forward-char 1))
  12476. (while (funcall fun re nil t)
  12477. (setq l (funcall outline-level))
  12478. (when (< l level) (goto-char pos) (throw 'exit nil))
  12479. (when (= l level) (goto-char (match-beginning 0)) (throw 'exit t)))
  12480. (goto-char pos)
  12481. nil))))
  12482. (defun org-show-siblings ()
  12483. "Show all siblings of the current headline."
  12484. (save-excursion
  12485. (while (org-goto-sibling) (org-flag-heading nil)))
  12486. (save-excursion
  12487. (while (org-goto-sibling 'previous)
  12488. (org-flag-heading nil))))
  12489. (defun org-show-hidden-entry ()
  12490. "Show an entry where even the heading is hidden."
  12491. (save-excursion
  12492. (org-show-entry)))
  12493. (defun org-flag-heading (flag &optional entry)
  12494. "Flag the current heading. FLAG non-nil means make invisible.
  12495. When ENTRY is non-nil, show the entire entry."
  12496. (save-excursion
  12497. (org-back-to-heading t)
  12498. ;; Check if we should show the entire entry
  12499. (if entry
  12500. (progn
  12501. (org-show-entry)
  12502. (save-excursion
  12503. (and (outline-next-heading)
  12504. (org-flag-heading nil))))
  12505. (outline-flag-region (max (point-min) (1- (point)))
  12506. (save-excursion (outline-end-of-heading) (point))
  12507. flag))))
  12508. (defun org-end-of-subtree (&optional invisible-OK to-heading)
  12509. ;; This is an exact copy of the original function, but it uses
  12510. ;; `org-back-to-heading', to make it work also in invisible
  12511. ;; trees. And is uses an invisible-OK argument.
  12512. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  12513. (org-back-to-heading invisible-OK)
  12514. (let ((first t)
  12515. (level (funcall outline-level)))
  12516. (while (and (not (eobp))
  12517. (or first (> (funcall outline-level) level)))
  12518. (setq first nil)
  12519. (outline-next-heading))
  12520. (unless to-heading
  12521. (if (memq (preceding-char) '(?\n ?\^M))
  12522. (progn
  12523. ;; Go to end of line before heading
  12524. (forward-char -1)
  12525. (if (memq (preceding-char) '(?\n ?\^M))
  12526. ;; leave blank line before heading
  12527. (forward-char -1))))))
  12528. (point))
  12529. (defun org-show-subtree ()
  12530. "Show everything after this heading at deeper levels."
  12531. (outline-flag-region
  12532. (point)
  12533. (save-excursion
  12534. (outline-end-of-subtree) (outline-next-heading) (point))
  12535. nil))
  12536. (defun org-show-entry ()
  12537. "Show the body directly following this heading.
  12538. Show the heading too, if it is currently invisible."
  12539. (interactive)
  12540. (save-excursion
  12541. (condition-case nil
  12542. (progn
  12543. (org-back-to-heading t)
  12544. (outline-flag-region
  12545. (max (point-min) (1- (point)))
  12546. (save-excursion
  12547. (re-search-forward
  12548. (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  12549. (or (match-beginning 1) (point-max)))
  12550. nil))
  12551. (error nil))))
  12552. (defun org-make-options-regexp (kwds)
  12553. "Make a regular expression for keyword lines."
  12554. (concat
  12555. "^"
  12556. "#?[ \t]*\\+\\("
  12557. (mapconcat 'regexp-quote kwds "\\|")
  12558. "\\):[ \t]*"
  12559. "\\(.+\\)"))
  12560. ;; Make isearch reveal the necessary context
  12561. (defun org-isearch-end ()
  12562. "Reveal context after isearch exits."
  12563. (when isearch-success ; only if search was successful
  12564. (if (featurep 'xemacs)
  12565. ;; Under XEmacs, the hook is run in the correct place,
  12566. ;; we directly show the context.
  12567. (org-show-context 'isearch)
  12568. ;; In Emacs the hook runs *before* restoring the overlays.
  12569. ;; So we have to use a one-time post-command-hook to do this.
  12570. ;; (Emacs 22 has a special variable, see function `org-mode')
  12571. (unless (and (boundp 'isearch-mode-end-hook-quit)
  12572. isearch-mode-end-hook-quit)
  12573. ;; Only when the isearch was not quitted.
  12574. (org-add-hook 'post-command-hook 'org-isearch-post-command
  12575. 'append 'local)))))
  12576. (defun org-isearch-post-command ()
  12577. "Remove self from hook, and show context."
  12578. (remove-hook 'post-command-hook 'org-isearch-post-command 'local)
  12579. (org-show-context 'isearch))
  12580. ;;;; Integration with and fixes for other packages
  12581. ;;; Imenu support
  12582. (defvar org-imenu-markers nil
  12583. "All markers currently used by Imenu.")
  12584. (make-variable-buffer-local 'org-imenu-markers)
  12585. (defun org-imenu-new-marker (&optional pos)
  12586. "Return a new marker for use by Imenu, and remember the marker."
  12587. (let ((m (make-marker)))
  12588. (move-marker m (or pos (point)))
  12589. (push m org-imenu-markers)
  12590. m))
  12591. (defun org-imenu-get-tree ()
  12592. "Produce the index for Imenu."
  12593. (mapc (lambda (x) (move-marker x nil)) org-imenu-markers)
  12594. (setq org-imenu-markers nil)
  12595. (let* ((n org-imenu-depth)
  12596. (re (concat "^" outline-regexp))
  12597. (subs (make-vector (1+ n) nil))
  12598. (last-level 0)
  12599. m tree level head)
  12600. (save-excursion
  12601. (save-restriction
  12602. (widen)
  12603. (goto-char (point-max))
  12604. (while (re-search-backward re nil t)
  12605. (setq level (org-reduced-level (funcall outline-level)))
  12606. (when (<= level n)
  12607. (looking-at org-complex-heading-regexp)
  12608. (setq head (org-match-string-no-properties 4)
  12609. m (org-imenu-new-marker))
  12610. (org-add-props head nil 'org-imenu-marker m 'org-imenu t)
  12611. (if (>= level last-level)
  12612. (push (cons head m) (aref subs level))
  12613. (push (cons head (aref subs (1+ level))) (aref subs level))
  12614. (loop for i from (1+ level) to n do (aset subs i nil)))
  12615. (setq last-level level)))))
  12616. (aref subs 1)))
  12617. (eval-after-load "imenu"
  12618. '(progn
  12619. (add-hook 'imenu-after-jump-hook
  12620. (lambda () (org-show-context 'org-goto)))))
  12621. ;; Speedbar support
  12622. (defvar org-speedbar-restriction-lock-overlay (org-make-overlay 1 1)
  12623. "Overlay marking the agenda restriction line in speedbar.")
  12624. (org-overlay-put org-speedbar-restriction-lock-overlay
  12625. 'face 'org-agenda-restriction-lock)
  12626. (org-overlay-put org-speedbar-restriction-lock-overlay
  12627. 'help-echo "Agendas are currently limited to this item.")
  12628. (org-detach-overlay org-speedbar-restriction-lock-overlay)
  12629. (defun org-speedbar-set-agenda-restriction ()
  12630. "Restrict future agenda commands to the location at point in speedbar.
  12631. To get rid of the restriction, use \\[org-agenda-remove-restriction-lock]."
  12632. (interactive)
  12633. (require 'org-agenda)
  12634. (let (p m tp np dir txt w)
  12635. (cond
  12636. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12637. 'org-imenu t))
  12638. (setq m (get-text-property p 'org-imenu-marker))
  12639. (save-excursion
  12640. (save-restriction
  12641. (set-buffer (marker-buffer m))
  12642. (goto-char m)
  12643. (org-agenda-set-restriction-lock 'subtree))))
  12644. ((setq p (text-property-any (point-at-bol) (point-at-eol)
  12645. 'speedbar-function 'speedbar-find-file))
  12646. (setq tp (previous-single-property-change
  12647. (1+ p) 'speedbar-function)
  12648. np (next-single-property-change
  12649. tp 'speedbar-function)
  12650. dir (speedbar-line-directory)
  12651. txt (buffer-substring-no-properties (or tp (point-min))
  12652. (or np (point-max))))
  12653. (save-excursion
  12654. (save-restriction
  12655. (set-buffer (find-file-noselect
  12656. (let ((default-directory dir))
  12657. (expand-file-name txt))))
  12658. (unless (org-mode-p)
  12659. (error "Cannot restrict to non-Org-mode file"))
  12660. (org-agenda-set-restriction-lock 'file))))
  12661. (t (error "Don't know how to restrict Org-mode's agenda")))
  12662. (org-move-overlay org-speedbar-restriction-lock-overlay
  12663. (point-at-bol) (point-at-eol))
  12664. (setq current-prefix-arg nil)
  12665. (org-agenda-maybe-redo)))
  12666. (eval-after-load "speedbar"
  12667. '(progn
  12668. (speedbar-add-supported-extension ".org")
  12669. (define-key speedbar-file-key-map "<" 'org-speedbar-set-agenda-restriction)
  12670. (define-key speedbar-file-key-map "\C-c\C-x<" 'org-speedbar-set-agenda-restriction)
  12671. (define-key speedbar-file-key-map ">" 'org-agenda-remove-restriction-lock)
  12672. (define-key speedbar-file-key-map "\C-c\C-x>" 'org-agenda-remove-restriction-lock)
  12673. (add-hook 'speedbar-visiting-tag-hook
  12674. (lambda () (org-show-context 'org-goto)))))
  12675. ;;; Fixes and Hacks for problems with other packages
  12676. ;; Make flyspell not check words in links, to not mess up our keymap
  12677. (defun org-mode-flyspell-verify ()
  12678. "Don't let flyspell put overlays at active buttons."
  12679. (not (get-text-property (point) 'keymap)))
  12680. ;; Make `bookmark-jump' show the jump location if it was hidden.
  12681. (eval-after-load "bookmark"
  12682. '(if (boundp 'bookmark-after-jump-hook)
  12683. ;; We can use the hook
  12684. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  12685. ;; Hook not available, use advice
  12686. (defadvice bookmark-jump (after org-make-visible activate)
  12687. "Make the position visible."
  12688. (org-bookmark-jump-unhide))))
  12689. (defun org-bookmark-jump-unhide ()
  12690. "Unhide the current position, to show the bookmark location."
  12691. (and (org-mode-p)
  12692. (or (org-invisible-p)
  12693. (save-excursion (goto-char (max (point-min) (1- (point))))
  12694. (org-invisible-p)))
  12695. (org-show-context 'bookmark-jump)))
  12696. ;; Make session.el ignore our circular variable
  12697. (eval-after-load "session"
  12698. '(add-to-list 'session-globals-exclude 'org-mark-ring))
  12699. ;;;; Experimental code
  12700. (defun org-closed-in-range ()
  12701. "Sparse tree of items closed in a certain time range.
  12702. Still experimental, may disappear in the future."
  12703. (interactive)
  12704. ;; Get the time interval from the user.
  12705. (let* ((time1 (time-to-seconds
  12706. (org-read-date nil 'to-time nil "Starting date: ")))
  12707. (time2 (time-to-seconds
  12708. (org-read-date nil 'to-time nil "End date:")))
  12709. ;; callback function
  12710. (callback (lambda ()
  12711. (let ((time
  12712. (time-to-seconds
  12713. (apply 'encode-time
  12714. (org-parse-time-string
  12715. (match-string 1))))))
  12716. ;; check if time in interval
  12717. (and (>= time time1) (<= time time2))))))
  12718. ;; make tree, check each match with the callback
  12719. (org-occur "CLOSED: +\\[\\(.*?\\)\\]" nil callback)))
  12720. ;;;; Finish up
  12721. (provide 'org)
  12722. (run-hooks 'org-load-hook)
  12723. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  12724. ;;; org.el ends here