org.el 551 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511751275137514751575167517751875197520752175227523752475257526752775287529753075317532753375347535753675377538753975407541754275437544754575467547754875497550755175527553755475557556755775587559756075617562756375647565756675677568756975707571757275737574757575767577757875797580758175827583758475857586758775887589759075917592759375947595759675977598759976007601760276037604760576067607760876097610761176127613761476157616761776187619762076217622762376247625762676277628762976307631763276337634763576367637763876397640764176427643764476457646764776487649765076517652765376547655765676577658765976607661766276637664766576667667766876697670767176727673767476757676767776787679768076817682768376847685768676877688768976907691769276937694769576967697769876997700770177027703770477057706770777087709771077117712771377147715771677177718771977207721772277237724772577267727772877297730773177327733773477357736773777387739774077417742774377447745774677477748774977507751775277537754775577567757775877597760776177627763776477657766776777687769777077717772777377747775777677777778777977807781778277837784778577867787778877897790779177927793779477957796779777987799780078017802780378047805780678077808780978107811781278137814781578167817781878197820782178227823782478257826782778287829783078317832783378347835783678377838783978407841784278437844784578467847784878497850785178527853785478557856785778587859786078617862786378647865786678677868786978707871787278737874787578767877787878797880788178827883788478857886788778887889789078917892789378947895789678977898789979007901790279037904790579067907790879097910791179127913791479157916791779187919792079217922792379247925792679277928792979307931793279337934793579367937793879397940794179427943794479457946794779487949795079517952795379547955795679577958795979607961796279637964796579667967796879697970797179727973797479757976797779787979798079817982798379847985798679877988798979907991799279937994799579967997799879998000800180028003800480058006800780088009801080118012801380148015801680178018801980208021802280238024802580268027802880298030803180328033803480358036803780388039804080418042804380448045804680478048804980508051805280538054805580568057805880598060806180628063806480658066806780688069807080718072807380748075807680778078807980808081808280838084808580868087808880898090809180928093809480958096809780988099810081018102810381048105810681078108810981108111811281138114811581168117811881198120812181228123812481258126812781288129813081318132813381348135813681378138813981408141814281438144814581468147814881498150815181528153815481558156815781588159816081618162816381648165816681678168816981708171817281738174817581768177817881798180818181828183818481858186818781888189819081918192819381948195819681978198819982008201820282038204820582068207820882098210821182128213821482158216821782188219822082218222822382248225822682278228822982308231823282338234823582368237823882398240824182428243824482458246824782488249825082518252825382548255825682578258825982608261826282638264826582668267826882698270827182728273827482758276827782788279828082818282828382848285828682878288828982908291829282938294829582968297829882998300830183028303830483058306830783088309831083118312831383148315831683178318831983208321832283238324832583268327832883298330833183328333833483358336833783388339834083418342834383448345834683478348834983508351835283538354835583568357835883598360836183628363836483658366836783688369837083718372837383748375837683778378837983808381838283838384838583868387838883898390839183928393839483958396839783988399840084018402840384048405840684078408840984108411841284138414841584168417841884198420842184228423842484258426842784288429843084318432843384348435843684378438843984408441844284438444844584468447844884498450845184528453845484558456845784588459846084618462846384648465846684678468846984708471847284738474847584768477847884798480848184828483848484858486848784888489849084918492849384948495849684978498849985008501850285038504850585068507850885098510851185128513851485158516851785188519852085218522852385248525852685278528852985308531853285338534853585368537853885398540854185428543854485458546854785488549855085518552855385548555855685578558855985608561856285638564856585668567856885698570857185728573857485758576857785788579858085818582858385848585858685878588858985908591859285938594859585968597859885998600860186028603860486058606860786088609861086118612861386148615861686178618861986208621862286238624862586268627862886298630863186328633863486358636863786388639864086418642864386448645864686478648864986508651865286538654865586568657865886598660866186628663866486658666866786688669867086718672867386748675867686778678867986808681868286838684868586868687868886898690869186928693869486958696869786988699870087018702870387048705870687078708870987108711871287138714871587168717871887198720872187228723872487258726872787288729873087318732873387348735873687378738873987408741874287438744874587468747874887498750875187528753875487558756875787588759876087618762876387648765876687678768876987708771877287738774877587768777877887798780878187828783878487858786878787888789879087918792879387948795879687978798879988008801880288038804880588068807880888098810881188128813881488158816881788188819882088218822882388248825882688278828882988308831883288338834883588368837883888398840884188428843884488458846884788488849885088518852885388548855885688578858885988608861886288638864886588668867886888698870887188728873887488758876887788788879888088818882888388848885888688878888888988908891889288938894889588968897889888998900890189028903890489058906890789088909891089118912891389148915891689178918891989208921892289238924892589268927892889298930893189328933893489358936893789388939894089418942894389448945894689478948894989508951895289538954895589568957895889598960896189628963896489658966896789688969897089718972897389748975897689778978897989808981898289838984898589868987898889898990899189928993899489958996899789988999900090019002900390049005900690079008900990109011901290139014901590169017901890199020902190229023902490259026902790289029903090319032903390349035903690379038903990409041904290439044904590469047904890499050905190529053905490559056905790589059906090619062906390649065906690679068906990709071907290739074907590769077907890799080908190829083908490859086908790889089909090919092909390949095909690979098909991009101910291039104910591069107910891099110911191129113911491159116911791189119912091219122912391249125912691279128912991309131913291339134913591369137913891399140914191429143914491459146914791489149915091519152915391549155915691579158915991609161916291639164916591669167916891699170917191729173917491759176917791789179918091819182918391849185918691879188918991909191919291939194919591969197919891999200920192029203920492059206920792089209921092119212921392149215921692179218921992209221922292239224922592269227922892299230923192329233923492359236923792389239924092419242924392449245924692479248924992509251925292539254925592569257925892599260926192629263926492659266926792689269927092719272927392749275927692779278927992809281928292839284928592869287928892899290929192929293929492959296929792989299930093019302930393049305930693079308930993109311931293139314931593169317931893199320932193229323932493259326932793289329933093319332933393349335933693379338933993409341934293439344934593469347934893499350935193529353935493559356935793589359936093619362936393649365936693679368936993709371937293739374937593769377937893799380938193829383938493859386938793889389939093919392939393949395939693979398939994009401940294039404940594069407940894099410941194129413941494159416941794189419942094219422942394249425942694279428942994309431943294339434943594369437943894399440944194429443944494459446944794489449945094519452945394549455945694579458945994609461946294639464946594669467946894699470947194729473947494759476947794789479948094819482948394849485948694879488948994909491949294939494949594969497949894999500950195029503950495059506950795089509951095119512951395149515951695179518951995209521952295239524952595269527952895299530953195329533953495359536953795389539954095419542954395449545954695479548954995509551955295539554955595569557955895599560956195629563956495659566956795689569957095719572957395749575957695779578957995809581958295839584958595869587958895899590959195929593959495959596959795989599960096019602960396049605960696079608960996109611961296139614961596169617961896199620962196229623962496259626962796289629963096319632963396349635963696379638963996409641964296439644964596469647964896499650965196529653965496559656965796589659966096619662966396649665966696679668966996709671967296739674967596769677967896799680968196829683968496859686968796889689969096919692969396949695969696979698969997009701970297039704970597069707970897099710971197129713971497159716971797189719972097219722972397249725972697279728972997309731973297339734973597369737973897399740974197429743974497459746974797489749975097519752975397549755975697579758975997609761976297639764976597669767976897699770977197729773977497759776977797789779978097819782978397849785978697879788978997909791979297939794979597969797979897999800980198029803980498059806980798089809981098119812981398149815981698179818981998209821982298239824982598269827982898299830983198329833983498359836983798389839984098419842984398449845984698479848984998509851985298539854985598569857985898599860986198629863986498659866986798689869987098719872987398749875987698779878987998809881988298839884988598869887988898899890989198929893989498959896989798989899990099019902990399049905990699079908990999109911991299139914991599169917991899199920992199229923992499259926992799289929993099319932993399349935993699379938993999409941994299439944994599469947994899499950995199529953995499559956995799589959996099619962996399649965996699679968996999709971997299739974997599769977997899799980998199829983998499859986998799889989999099919992999399949995999699979998999910000100011000210003100041000510006100071000810009100101001110012100131001410015100161001710018100191002010021100221002310024100251002610027100281002910030100311003210033100341003510036100371003810039100401004110042100431004410045100461004710048100491005010051100521005310054100551005610057100581005910060100611006210063100641006510066100671006810069100701007110072100731007410075100761007710078100791008010081100821008310084100851008610087100881008910090100911009210093100941009510096100971009810099101001010110102101031010410105101061010710108101091011010111101121011310114101151011610117101181011910120101211012210123101241012510126101271012810129101301013110132101331013410135101361013710138101391014010141101421014310144101451014610147101481014910150101511015210153101541015510156101571015810159101601016110162101631016410165101661016710168101691017010171101721017310174101751017610177101781017910180101811018210183101841018510186101871018810189101901019110192101931019410195101961019710198101991020010201102021020310204102051020610207102081020910210102111021210213102141021510216102171021810219102201022110222102231022410225102261022710228102291023010231102321023310234102351023610237102381023910240102411024210243102441024510246102471024810249102501025110252102531025410255102561025710258102591026010261102621026310264102651026610267102681026910270102711027210273102741027510276102771027810279102801028110282102831028410285102861028710288102891029010291102921029310294102951029610297102981029910300103011030210303103041030510306103071030810309103101031110312103131031410315103161031710318103191032010321103221032310324103251032610327103281032910330103311033210333103341033510336103371033810339103401034110342103431034410345103461034710348103491035010351103521035310354103551035610357103581035910360103611036210363103641036510366103671036810369103701037110372103731037410375103761037710378103791038010381103821038310384103851038610387103881038910390103911039210393103941039510396103971039810399104001040110402104031040410405104061040710408104091041010411104121041310414104151041610417104181041910420104211042210423104241042510426104271042810429104301043110432104331043410435104361043710438104391044010441104421044310444104451044610447104481044910450104511045210453104541045510456104571045810459104601046110462104631046410465104661046710468104691047010471104721047310474104751047610477104781047910480104811048210483104841048510486104871048810489104901049110492104931049410495104961049710498104991050010501105021050310504105051050610507105081050910510105111051210513105141051510516105171051810519105201052110522105231052410525105261052710528105291053010531105321053310534105351053610537105381053910540105411054210543105441054510546105471054810549105501055110552105531055410555105561055710558105591056010561105621056310564105651056610567105681056910570105711057210573105741057510576105771057810579105801058110582105831058410585105861058710588105891059010591105921059310594105951059610597105981059910600106011060210603106041060510606106071060810609106101061110612106131061410615106161061710618106191062010621106221062310624106251062610627106281062910630106311063210633106341063510636106371063810639106401064110642106431064410645106461064710648106491065010651106521065310654106551065610657106581065910660106611066210663106641066510666106671066810669106701067110672106731067410675106761067710678106791068010681106821068310684106851068610687106881068910690106911069210693106941069510696106971069810699107001070110702107031070410705107061070710708107091071010711107121071310714107151071610717107181071910720107211072210723107241072510726107271072810729107301073110732107331073410735107361073710738107391074010741107421074310744107451074610747107481074910750107511075210753107541075510756107571075810759107601076110762107631076410765107661076710768107691077010771107721077310774107751077610777107781077910780107811078210783107841078510786107871078810789107901079110792107931079410795107961079710798107991080010801108021080310804108051080610807108081080910810108111081210813108141081510816108171081810819108201082110822108231082410825108261082710828108291083010831108321083310834108351083610837108381083910840108411084210843108441084510846108471084810849108501085110852108531085410855108561085710858108591086010861108621086310864108651086610867108681086910870108711087210873108741087510876108771087810879108801088110882108831088410885108861088710888108891089010891108921089310894108951089610897108981089910900109011090210903109041090510906109071090810909109101091110912109131091410915109161091710918109191092010921109221092310924109251092610927109281092910930109311093210933109341093510936109371093810939109401094110942109431094410945109461094710948109491095010951109521095310954109551095610957109581095910960109611096210963109641096510966109671096810969109701097110972109731097410975109761097710978109791098010981109821098310984109851098610987109881098910990109911099210993109941099510996109971099810999110001100111002110031100411005110061100711008110091101011011110121101311014110151101611017110181101911020110211102211023110241102511026110271102811029110301103111032110331103411035110361103711038110391104011041110421104311044110451104611047110481104911050110511105211053110541105511056110571105811059110601106111062110631106411065110661106711068110691107011071110721107311074110751107611077110781107911080110811108211083110841108511086110871108811089110901109111092110931109411095110961109711098110991110011101111021110311104111051110611107111081110911110111111111211113111141111511116111171111811119111201112111122111231112411125111261112711128111291113011131111321113311134111351113611137111381113911140111411114211143111441114511146111471114811149111501115111152111531115411155111561115711158111591116011161111621116311164111651116611167111681116911170111711117211173111741117511176111771117811179111801118111182111831118411185111861118711188111891119011191111921119311194111951119611197111981119911200112011120211203112041120511206112071120811209112101121111212112131121411215112161121711218112191122011221112221122311224112251122611227112281122911230112311123211233112341123511236112371123811239112401124111242112431124411245112461124711248112491125011251112521125311254112551125611257112581125911260112611126211263112641126511266112671126811269112701127111272112731127411275112761127711278112791128011281112821128311284112851128611287112881128911290112911129211293112941129511296112971129811299113001130111302113031130411305113061130711308113091131011311113121131311314113151131611317113181131911320113211132211323113241132511326113271132811329113301133111332113331133411335113361133711338113391134011341113421134311344113451134611347113481134911350113511135211353113541135511356113571135811359113601136111362113631136411365113661136711368113691137011371113721137311374113751137611377113781137911380113811138211383113841138511386113871138811389113901139111392113931139411395113961139711398113991140011401114021140311404114051140611407114081140911410114111141211413114141141511416114171141811419114201142111422114231142411425114261142711428114291143011431114321143311434114351143611437114381143911440114411144211443114441144511446114471144811449114501145111452114531145411455114561145711458114591146011461114621146311464114651146611467114681146911470114711147211473114741147511476114771147811479114801148111482114831148411485114861148711488114891149011491114921149311494114951149611497114981149911500115011150211503115041150511506115071150811509115101151111512115131151411515115161151711518115191152011521115221152311524115251152611527115281152911530115311153211533115341153511536115371153811539115401154111542115431154411545115461154711548115491155011551115521155311554115551155611557115581155911560115611156211563115641156511566115671156811569115701157111572115731157411575115761157711578115791158011581115821158311584115851158611587115881158911590115911159211593115941159511596115971159811599116001160111602116031160411605116061160711608116091161011611116121161311614116151161611617116181161911620116211162211623116241162511626116271162811629116301163111632116331163411635116361163711638116391164011641116421164311644116451164611647116481164911650116511165211653116541165511656116571165811659116601166111662116631166411665116661166711668116691167011671116721167311674116751167611677116781167911680116811168211683116841168511686116871168811689116901169111692116931169411695116961169711698116991170011701117021170311704117051170611707117081170911710117111171211713117141171511716117171171811719117201172111722117231172411725117261172711728117291173011731117321173311734117351173611737117381173911740117411174211743117441174511746117471174811749117501175111752117531175411755117561175711758117591176011761117621176311764117651176611767117681176911770117711177211773117741177511776117771177811779117801178111782117831178411785117861178711788117891179011791117921179311794117951179611797117981179911800118011180211803118041180511806118071180811809118101181111812118131181411815118161181711818118191182011821118221182311824118251182611827118281182911830118311183211833118341183511836118371183811839118401184111842118431184411845118461184711848118491185011851118521185311854118551185611857118581185911860118611186211863118641186511866118671186811869118701187111872118731187411875118761187711878118791188011881118821188311884118851188611887118881188911890118911189211893118941189511896118971189811899119001190111902119031190411905119061190711908119091191011911119121191311914119151191611917119181191911920119211192211923119241192511926119271192811929119301193111932119331193411935119361193711938119391194011941119421194311944119451194611947119481194911950119511195211953119541195511956119571195811959119601196111962119631196411965119661196711968119691197011971119721197311974119751197611977119781197911980119811198211983119841198511986119871198811989119901199111992119931199411995119961199711998119991200012001120021200312004120051200612007120081200912010120111201212013120141201512016120171201812019120201202112022120231202412025120261202712028120291203012031120321203312034120351203612037120381203912040120411204212043120441204512046120471204812049120501205112052120531205412055120561205712058120591206012061120621206312064120651206612067120681206912070120711207212073120741207512076120771207812079120801208112082120831208412085120861208712088120891209012091120921209312094120951209612097120981209912100121011210212103121041210512106121071210812109121101211112112121131211412115121161211712118121191212012121121221212312124121251212612127121281212912130121311213212133121341213512136121371213812139121401214112142121431214412145121461214712148121491215012151121521215312154121551215612157121581215912160121611216212163121641216512166121671216812169121701217112172121731217412175121761217712178121791218012181121821218312184121851218612187121881218912190121911219212193121941219512196121971219812199122001220112202122031220412205122061220712208122091221012211122121221312214122151221612217122181221912220122211222212223122241222512226122271222812229122301223112232122331223412235122361223712238122391224012241122421224312244122451224612247122481224912250122511225212253122541225512256122571225812259122601226112262122631226412265122661226712268122691227012271122721227312274122751227612277122781227912280122811228212283122841228512286122871228812289122901229112292122931229412295122961229712298122991230012301123021230312304123051230612307123081230912310123111231212313123141231512316123171231812319123201232112322123231232412325123261232712328123291233012331123321233312334123351233612337123381233912340123411234212343123441234512346123471234812349123501235112352123531235412355123561235712358123591236012361123621236312364123651236612367123681236912370123711237212373123741237512376123771237812379123801238112382123831238412385123861238712388123891239012391123921239312394123951239612397123981239912400124011240212403124041240512406124071240812409124101241112412124131241412415124161241712418124191242012421124221242312424124251242612427124281242912430124311243212433124341243512436124371243812439124401244112442124431244412445124461244712448124491245012451124521245312454124551245612457124581245912460124611246212463124641246512466124671246812469124701247112472124731247412475124761247712478124791248012481124821248312484124851248612487124881248912490124911249212493124941249512496124971249812499125001250112502125031250412505125061250712508125091251012511125121251312514125151251612517125181251912520125211252212523125241252512526125271252812529125301253112532125331253412535125361253712538125391254012541125421254312544125451254612547125481254912550125511255212553125541255512556125571255812559125601256112562125631256412565125661256712568125691257012571125721257312574125751257612577125781257912580125811258212583125841258512586125871258812589125901259112592125931259412595125961259712598125991260012601126021260312604126051260612607126081260912610126111261212613126141261512616126171261812619126201262112622126231262412625126261262712628126291263012631126321263312634126351263612637126381263912640126411264212643126441264512646126471264812649126501265112652126531265412655126561265712658126591266012661126621266312664126651266612667126681266912670126711267212673126741267512676126771267812679126801268112682126831268412685126861268712688126891269012691126921269312694126951269612697126981269912700127011270212703127041270512706127071270812709127101271112712127131271412715127161271712718127191272012721127221272312724127251272612727127281272912730127311273212733127341273512736127371273812739127401274112742127431274412745127461274712748127491275012751127521275312754127551275612757127581275912760127611276212763127641276512766127671276812769127701277112772127731277412775127761277712778127791278012781127821278312784127851278612787127881278912790127911279212793127941279512796127971279812799128001280112802128031280412805128061280712808128091281012811128121281312814128151281612817128181281912820128211282212823128241282512826128271282812829128301283112832128331283412835128361283712838128391284012841128421284312844128451284612847128481284912850128511285212853128541285512856128571285812859128601286112862128631286412865128661286712868128691287012871128721287312874128751287612877128781287912880128811288212883128841288512886128871288812889128901289112892128931289412895128961289712898128991290012901129021290312904129051290612907129081290912910129111291212913129141291512916129171291812919129201292112922129231292412925129261292712928129291293012931129321293312934129351293612937129381293912940129411294212943129441294512946129471294812949129501295112952129531295412955129561295712958129591296012961129621296312964129651296612967129681296912970129711297212973129741297512976129771297812979129801298112982129831298412985129861298712988129891299012991129921299312994129951299612997129981299913000130011300213003130041300513006130071300813009130101301113012130131301413015130161301713018130191302013021130221302313024130251302613027130281302913030130311303213033130341303513036130371303813039130401304113042130431304413045130461304713048130491305013051130521305313054130551305613057130581305913060130611306213063130641306513066130671306813069130701307113072130731307413075130761307713078130791308013081130821308313084130851308613087130881308913090130911309213093130941309513096130971309813099131001310113102131031310413105131061310713108131091311013111131121311313114131151311613117131181311913120131211312213123131241312513126131271312813129131301313113132131331313413135131361313713138131391314013141131421314313144131451314613147131481314913150131511315213153131541315513156131571315813159131601316113162131631316413165131661316713168131691317013171131721317313174131751317613177131781317913180131811318213183131841318513186131871318813189131901319113192131931319413195131961319713198131991320013201132021320313204132051320613207132081320913210132111321213213132141321513216132171321813219132201322113222132231322413225132261322713228132291323013231132321323313234132351323613237132381323913240132411324213243132441324513246132471324813249132501325113252132531325413255132561325713258132591326013261132621326313264132651326613267132681326913270132711327213273132741327513276132771327813279132801328113282132831328413285132861328713288132891329013291132921329313294132951329613297132981329913300133011330213303133041330513306133071330813309133101331113312133131331413315133161331713318133191332013321133221332313324133251332613327133281332913330133311333213333133341333513336133371333813339133401334113342133431334413345133461334713348133491335013351133521335313354133551335613357133581335913360133611336213363133641336513366133671336813369133701337113372133731337413375133761337713378133791338013381133821338313384133851338613387133881338913390133911339213393133941339513396133971339813399134001340113402134031340413405134061340713408134091341013411134121341313414134151341613417134181341913420134211342213423134241342513426134271342813429134301343113432134331343413435134361343713438134391344013441134421344313444134451344613447134481344913450134511345213453134541345513456134571345813459134601346113462134631346413465134661346713468134691347013471134721347313474134751347613477134781347913480134811348213483134841348513486134871348813489134901349113492134931349413495134961349713498134991350013501135021350313504135051350613507135081350913510135111351213513135141351513516135171351813519135201352113522135231352413525135261352713528135291353013531135321353313534135351353613537135381353913540135411354213543135441354513546135471354813549135501355113552135531355413555135561355713558135591356013561135621356313564135651356613567135681356913570135711357213573135741357513576135771357813579135801358113582135831358413585135861358713588135891359013591135921359313594135951359613597135981359913600136011360213603136041360513606136071360813609136101361113612136131361413615136161361713618136191362013621136221362313624136251362613627136281362913630136311363213633136341363513636136371363813639136401364113642136431364413645136461364713648136491365013651136521365313654136551365613657136581365913660136611366213663136641366513666136671366813669136701367113672136731367413675136761367713678136791368013681136821368313684136851368613687136881368913690136911369213693136941369513696136971369813699137001370113702137031370413705137061370713708137091371013711137121371313714137151371613717137181371913720137211372213723137241372513726137271372813729137301373113732137331373413735137361373713738137391374013741137421374313744137451374613747137481374913750137511375213753137541375513756137571375813759137601376113762137631376413765137661376713768137691377013771137721377313774137751377613777137781377913780137811378213783137841378513786137871378813789137901379113792137931379413795137961379713798137991380013801138021380313804138051380613807138081380913810138111381213813138141381513816138171381813819138201382113822138231382413825138261382713828138291383013831138321383313834138351383613837138381383913840138411384213843138441384513846138471384813849138501385113852138531385413855138561385713858138591386013861138621386313864138651386613867138681386913870138711387213873138741387513876138771387813879138801388113882138831388413885138861388713888138891389013891138921389313894138951389613897138981389913900139011390213903139041390513906139071390813909139101391113912139131391413915139161391713918139191392013921139221392313924139251392613927139281392913930139311393213933139341393513936139371393813939139401394113942139431394413945139461394713948139491395013951139521395313954139551395613957139581395913960139611396213963139641396513966139671396813969139701397113972139731397413975139761397713978139791398013981139821398313984139851398613987139881398913990139911399213993139941399513996139971399813999140001400114002140031400414005140061400714008140091401014011140121401314014140151401614017140181401914020140211402214023140241402514026140271402814029140301403114032140331403414035140361403714038140391404014041140421404314044140451404614047140481404914050140511405214053140541405514056140571405814059140601406114062140631406414065140661406714068140691407014071140721407314074140751407614077140781407914080140811408214083140841408514086140871408814089140901409114092140931409414095140961409714098140991410014101141021410314104141051410614107141081410914110141111411214113141141411514116141171411814119141201412114122141231412414125141261412714128141291413014131141321413314134141351413614137141381413914140141411414214143141441414514146141471414814149141501415114152141531415414155141561415714158141591416014161141621416314164141651416614167141681416914170141711417214173141741417514176141771417814179141801418114182141831418414185141861418714188141891419014191141921419314194141951419614197141981419914200142011420214203142041420514206142071420814209142101421114212142131421414215142161421714218142191422014221142221422314224142251422614227142281422914230142311423214233142341423514236142371423814239142401424114242142431424414245142461424714248142491425014251142521425314254142551425614257142581425914260142611426214263142641426514266142671426814269142701427114272142731427414275142761427714278142791428014281142821428314284142851428614287142881428914290142911429214293142941429514296142971429814299143001430114302143031430414305143061430714308143091431014311143121431314314143151431614317143181431914320143211432214323143241432514326143271432814329143301433114332143331433414335143361433714338143391434014341143421434314344143451434614347143481434914350143511435214353143541435514356143571435814359143601436114362143631436414365143661436714368143691437014371143721437314374143751437614377143781437914380143811438214383143841438514386143871438814389143901439114392143931439414395143961439714398143991440014401144021440314404144051440614407144081440914410144111441214413144141441514416144171441814419144201442114422144231442414425144261442714428144291443014431144321443314434144351443614437144381443914440144411444214443144441444514446144471444814449144501445114452144531445414455144561445714458144591446014461144621446314464144651446614467144681446914470144711447214473144741447514476144771447814479144801448114482144831448414485144861448714488144891449014491144921449314494144951449614497144981449914500145011450214503145041450514506145071450814509145101451114512145131451414515145161451714518145191452014521145221452314524145251452614527145281452914530145311453214533145341453514536145371453814539145401454114542145431454414545145461454714548145491455014551145521455314554145551455614557145581455914560145611456214563145641456514566145671456814569145701457114572145731457414575145761457714578145791458014581145821458314584145851458614587145881458914590145911459214593145941459514596145971459814599146001460114602146031460414605146061460714608146091461014611146121461314614146151461614617146181461914620146211462214623146241462514626146271462814629146301463114632146331463414635146361463714638146391464014641146421464314644146451464614647146481464914650146511465214653146541465514656146571465814659146601466114662146631466414665146661466714668146691467014671146721467314674146751467614677146781467914680146811468214683146841468514686146871468814689146901469114692146931469414695146961469714698146991470014701147021470314704147051470614707147081470914710147111471214713147141471514716147171471814719147201472114722147231472414725147261472714728147291473014731147321473314734147351473614737147381473914740147411474214743147441474514746147471474814749147501475114752147531475414755147561475714758147591476014761147621476314764147651476614767147681476914770147711477214773147741477514776147771477814779147801478114782147831478414785147861478714788147891479014791147921479314794147951479614797147981479914800148011480214803148041480514806148071480814809148101481114812148131481414815148161481714818148191482014821148221482314824148251482614827148281482914830148311483214833148341483514836148371483814839148401484114842148431484414845148461484714848148491485014851148521485314854148551485614857148581485914860148611486214863148641486514866148671486814869148701487114872148731487414875148761487714878148791488014881148821488314884148851488614887148881488914890148911489214893148941489514896148971489814899149001490114902149031490414905149061490714908149091491014911149121491314914149151491614917149181491914920149211492214923149241492514926149271492814929149301493114932149331493414935149361493714938149391494014941149421494314944149451494614947149481494914950149511495214953149541495514956149571495814959149601496114962149631496414965149661496714968149691497014971149721497314974149751497614977
  1. ;;; org.el --- Outline-based notes management and organize
  2. ;; Carstens outline-mode for keeping track of everything.
  3. ;; Copyright (C) 2004, 2005, 2006 Free Software Foundation, Inc.
  4. ;;
  5. ;; Author: Carsten Dominik <dominik at science dot uva dot nl>
  6. ;; Keywords: outlines, hypermedia, calendar, wp
  7. ;; Homepage: http://www.astro.uva.nl/~dominik/Tools/org/
  8. ;; Version: 4.36
  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 2, or (at your option)
  15. ;; any later version.
  16. ;; GNU Emacs is distributed in the hope that it will be useful,
  17. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. ;; GNU General Public License for more details.
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  22. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  23. ;; Boston, MA 02110-1301, USA.
  24. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  25. ;;
  26. ;;; Commentary:
  27. ;;
  28. ;; Org-mode is a mode for keeping notes, maintaining ToDo lists, and doing
  29. ;; project planning with a fast and effective plain-text system.
  30. ;;
  31. ;; Org-mode develops organizational tasks around NOTES files that contain
  32. ;; information about projects as plain text. Org-mode is implemented on
  33. ;; top of outline-mode, which makes it possible to keep the content of
  34. ;; large files well structured. Visibility cycling and structure editing
  35. ;; help to work with the tree. Tables are easily created with a built-in
  36. ;; table editor. Org-mode supports ToDo items, deadlines, time stamps,
  37. ;; and scheduling. It dynamically compiles entries into an agenda that
  38. ;; utilizes and smoothly integrates much of the Emacs calendar and diary.
  39. ;; Plain text URL-like links connect to websites, emails, Usenet
  40. ;; messages, BBDB entries, and any files related to the projects. For
  41. ;; printing and sharing of notes, an Org-mode file can be exported as a
  42. ;; structured ASCII file, as HTML, or (todo and agenda items only) as an
  43. ;; iCalendar file. It can also serve as a publishing tool for a set of
  44. ;; linked webpages.
  45. ;;
  46. ;;
  47. ;; Installation
  48. ;; ------------
  49. ;; If Org-mode is part of the Emacs distribution or an XEmacs package, you
  50. ;; only need to copy the following lines to your .emacs file. The last two
  51. ;; lines define *global* keys for the commands `org-store-link' and
  52. ;; `org-agenda' - please choose suitable keys yourself.
  53. ;;
  54. ;; (add-to-list 'auto-mode-alist '("\\.org$" . org-mode))
  55. ;; (define-key global-map "\C-cl" 'org-store-link)
  56. ;; (define-key global-map "\C-ca" 'org-agenda)
  57. ;;
  58. ;; Furthermore you need to activate font-lock-mode in org-mode buffers.
  59. ;; either of the following two lins will do the trick:
  60. ;;
  61. ;; (global-font-lock-mode 1) ; for all buffers
  62. ;; (add-hook 'org-mode-hook 'turn-on-font-lock) ; org-mode buffers only
  63. ;;
  64. ;; If you have downloaded Org-mode from the Web, you have to take additional
  65. ;; action: Byte-compile org.el and org-publish.el and put them together with
  66. ;; org-install.el on your load path. Then also add to your .emacs file:
  67. ;;
  68. ;; (require 'org-install)
  69. ;;
  70. ;;
  71. ;; Activation
  72. ;; ----------
  73. ;; The setup above will put all files with extension ".org" into Org-mode.
  74. ;; As an alternative, make the first line of a file look like this:
  75. ;;
  76. ;; MY PROJECTS -*- mode: org; -*-
  77. ;;
  78. ;; which will select Org-mode for this buffer no matter what the file's
  79. ;; name is.
  80. ;;
  81. ;; Documentation
  82. ;; -------------
  83. ;; The documentation of Org-mode can be found in the TeXInfo file. The
  84. ;; distribution also contains a PDF version of it. At the homepage of
  85. ;; Org-mode, you can read the same text online as HTML. There is also an
  86. ;; excellent reference card made by Philip Rooke. This card can be found
  87. ;; in the etc/ directory of Emacs 22.
  88. ;;
  89. ;; Recent changes
  90. ;; --------------
  91. ;; Version 4.36
  92. ;; - Improved indentation of ASCII export, when headlines become items.
  93. ;; - Handling of 12am and 12pm fixed. Times beyond 24:00 can be used
  94. ;; and will not lead to conflicts.
  95. ;; - Support for mutually exclusive TAGS with the fast tags interface.
  96. ;; - Bug fixes.
  97. ;;
  98. ;; Version 4.35
  99. ;; - HTML export is now valid XHTML.
  100. ;; - Timeline can also show dates without entries. See new option
  101. ;; `org-timeline-show-empty-dates'.
  102. ;; - The bullets created by the ASCII exporter can now be configured.
  103. ;; See the new option `org-export-ascii-bullets'.
  104. ;; - New face `org-upcoming-deadline' (was `org-scheduled-previously').
  105. ;; - New function `org-context' to allow testing for local context.
  106. ;;
  107. ;; Version 4.34
  108. ;; - Bug fixes.
  109. ;;
  110. ;; Version 4.33
  111. ;; - New commands to move through plain lists: S-up and S-down.
  112. ;; - Bug fixes and documentation update.
  113. ;;
  114. ;; Version 4.32
  115. ;; - Fast (single-key-per-tag) interface for setting TAGS.
  116. ;; - The list of legal tags can be configured globally and locally.
  117. ;; - Elisp and Info links (thanks to Todd Neal).
  118. ;; - `org-export-publishing-directory' can be an alist, with different
  119. ;; directories for different export types.
  120. ;; - All context-sensitive commands use `call-interactively' to dispatch.
  121. ;; - `org-confirm-shell-links' renamed to `org-confirm-shell-link-function'.
  122. ;; - Bug fixes.
  123. ;;
  124. ;; Version 4.31
  125. ;; - Bug fixes.
  126. ;;
  127. ;; Version 4.30
  128. ;; - Modified installation: Autoloads have been collected in org-install.el.
  129. ;; - Logging (org-log-done) is now a #+STARTUP option.
  130. ;; - Checkboxes in plain list items, following up on Frank Ruell's idea.
  131. ;; - File links inserted with C-c C-l will use relative paths if the linked
  132. ;; file is in the current directory or a subdirectory of it.
  133. ;; - New variable `org-link-file-path-type' to specify preference for
  134. ;; relative and absolute paths.
  135. ;; - New CSS classes for tags, timestamps, timestamp keywords.
  136. ;; - Bug and typo fixes.
  137. ;;
  138. ;; Version 4.29
  139. ;; - Inlining images in HTML export now depends on wheather the link
  140. ;; contains a description or not.
  141. ;; - TODO items can be scheduled from the global TODO list using C-c C-s.
  142. ;; - TODO items already scheduled can be made to disappear from the global
  143. ;; todo list, see `org-agenda-todo-ignore-scheduled'.
  144. ;; - In Tables, formulas may also be Lisp forms.
  145. ;; - Exporting the visible part of an outline with `C-c C-x v' works now
  146. ;; for all available exporters.
  147. ;; - Bug fixes, lots of them :-(
  148. ;;
  149. ;; Version 4.28
  150. ;; - Bug fixes.
  151. ;;
  152. ;; Version 4.27
  153. ;; - HTML exporter generalized to receive external options.
  154. ;; As part of the process, author, email and date have been moved to the
  155. ;; end of the HTML file.
  156. ;; - Support for customizable file search in file links.
  157. ;; - BibTeX database links as first application of the above.
  158. ;; - New option `org-agenda-todo-list-sublevels' to turn off listing TODO
  159. ;; entries that are sublevels of another TODO entry.
  160. ;;
  161. ;;
  162. ;;; Code:
  163. (eval-when-compile
  164. (require 'cl)
  165. (require 'calendar))
  166. (require 'outline)
  167. (require 'time-date)
  168. (require 'easymenu)
  169. ;;; Customization variables
  170. (defvar org-version "4.36"
  171. "The version number of the file org.el.")
  172. (defun org-version ()
  173. (interactive)
  174. (message "Org-mode version %s" org-version))
  175. ;; The following constant is for compatibility with different versions
  176. ;; of outline.el.
  177. (defconst org-noutline-p (featurep 'noutline)
  178. "Are we using the new outline mode?")
  179. (defconst org-xemacs-p (featurep 'xemacs)) ; not used by org.el itself
  180. (defconst org-format-transports-properties-p
  181. (let ((x "a"))
  182. (add-text-properties 0 1 '(test t) x)
  183. (get-text-property 0 'test (format "%s" x)))
  184. "Does format transport text properties?")
  185. (defgroup org nil
  186. "Outline-based notes management and organizer."
  187. :tag "Org"
  188. :group 'outlines
  189. :group 'hypermedia
  190. :group 'calendar)
  191. (defgroup org-startup nil
  192. "Options concerning startup of Org-mode."
  193. :tag "Org Startup"
  194. :group 'org)
  195. (defcustom org-startup-folded t
  196. "Non-nil means, entering Org-mode will switch to OVERVIEW.
  197. This can also be configured on a per-file basis by adding one of
  198. the following lines anywhere in the buffer:
  199. #+STARTUP: fold
  200. #+STARTUP: nofold
  201. #+STARTUP: content"
  202. :group 'org-startup
  203. :type '(choice
  204. (const :tag "nofold: show all" nil)
  205. (const :tag "fold: overview" t)
  206. (const :tag "content: all headlines" content)))
  207. (defcustom org-startup-truncated t
  208. "Non-nil means, entering Org-mode will set `truncate-lines'.
  209. This is useful since some lines containing links can be very long and
  210. uninteresting. Also tables look terrible when wrapped."
  211. :group 'org-startup
  212. :type 'boolean)
  213. (defcustom org-startup-align-all-tables nil
  214. "Non-nil means, align all tables when visiting a file.
  215. This is useful when the column width in tables is forced with <N> cookies
  216. in table fields. Such tables will look correct only after the first re-align.
  217. This can also be configured on a per-file basis by adding one of
  218. the following lines anywhere in the buffer:
  219. #+STARTUP: align
  220. #+STARTUP: noalign"
  221. :group 'org-startup
  222. :type 'boolean)
  223. (defcustom org-startup-with-deadline-check nil
  224. "Non-nil means, entering Org-mode will run the deadline check.
  225. This means, if you start editing an org file, you will get an
  226. immediate reminder of any due deadlines.
  227. This can also be configured on a per-file basis by adding one of
  228. the following lines anywhere in the buffer:
  229. #+STARTUP: dlcheck
  230. #+STARTUP: nodlcheck"
  231. :group 'org-startup
  232. :type 'boolean)
  233. (defcustom org-insert-mode-line-in-empty-file nil
  234. "Non-nil means insert the first line setting Org-mode in empty files.
  235. When the function `org-mode' is called interactively in an empty file, this
  236. normally means that the file name does not automatically trigger Org-mode.
  237. To ensure that the file will always be in Org-mode in the future, a
  238. line enforcing Org-mode will be inserted into the buffer, if this option
  239. has been set."
  240. :group 'org-startup
  241. :type 'boolean)
  242. (defcustom org-CUA-compatible nil
  243. "Non-nil means use alternative key bindings for S-<cursor movement>.
  244. Org-mode used S-<cursor movement> for changing timestamps and priorities.
  245. S-<cursor movement> is also used for example by `CUA-mode' to select text.
  246. If you want to use Org-mode together with `CUA-mode', Org-mode needs to use
  247. alternative bindings. Setting this variable to t will replace the following
  248. keys both in Org-mode and in the Org-agenda buffer.
  249. S-RET -> C-S-RET
  250. S-up -> M-p
  251. S-down -> M-n
  252. S-left -> M--
  253. S-right -> M-+
  254. If you do not like the alternative keys, take a look at the variable
  255. `org-disputed-keys'.
  256. This option is only relevant at load-time of Org-mode. Changing it requires
  257. a restart of Emacs to become effective."
  258. :group 'org-startup
  259. :type 'boolean)
  260. (defvar org-disputed-keys
  261. '((S-up [(shift up)] [(meta ?p)])
  262. (S-down [(shift down)] [(meta ?n)])
  263. (S-left [(shift left)] [(meta ?-)])
  264. (S-right [(shift right)] [(meta ?+)])
  265. (S-return [(shift return)] [(control shift return)]))
  266. "Keys for which Org-mode and other modes compete.
  267. This is an alist, cars are symbols for lookup, 1st element is the default key,
  268. second element will be used when `org-CUA-compatible' is t.")
  269. (defun org-key (key)
  270. "Select a key according to `org-CUA-compatible'."
  271. (nth (if org-CUA-compatible 2 1)
  272. (or (assq key org-disputed-keys)
  273. (error "Invalid Key %s in `org-key'" key))))
  274. (defcustom org-ellipsis nil
  275. "The ellipsis to use in the Org-mode outline.
  276. When nil, just use the standard three dots. When a string, use that instead,
  277. and just in Org-mode (which will then use its own display table).
  278. Changing this requires executing `M-x org-mode' in a buffer to become
  279. effective."
  280. :group 'org-startup
  281. :type '(choice (const :tag "Default" nil)
  282. (string :tag "String" :value "...#")))
  283. (defvar org-display-table nil
  284. "The display table for org-mode, in case `org-ellipsis' is non-nil.")
  285. (defgroup org-keywords nil
  286. "Keywords in Org-mode."
  287. :tag "Org Keywords"
  288. :group 'org)
  289. (defcustom org-deadline-string "DEADLINE:"
  290. "String to mark deadline entries.
  291. A deadline is this string, followed by a time stamp. Should be a word,
  292. terminated by a colon. You can insert a schedule keyword and
  293. a timestamp with \\[org-deadline].
  294. Changes become only effective after restarting Emacs."
  295. :group 'org-keywords
  296. :type 'string)
  297. (defcustom org-scheduled-string "SCHEDULED:"
  298. "String to mark scheduled TODO entries.
  299. A schedule is this string, followed by a time stamp. Should be a word,
  300. terminated by a colon. You can insert a schedule keyword and
  301. a timestamp with \\[org-schedule].
  302. Changes become only effective after restarting Emacs."
  303. :group 'org-keywords
  304. :type 'string)
  305. (defcustom org-closed-string "CLOSED:"
  306. "String used as the prefix for timestamps logging closing a TODO entry."
  307. :group 'org-keywords
  308. :type 'string)
  309. (defcustom org-comment-string "COMMENT"
  310. "Entries starting with this keyword will never be exported.
  311. An entry can be toggled between COMMENT and normal with
  312. \\[org-toggle-comment].
  313. Changes become only effective after restarting Emacs."
  314. :group 'org-keywords
  315. :type 'string)
  316. (defcustom org-quote-string "QUOTE"
  317. "Entries starting with this keyword will be exported in fixed-width font.
  318. Quoting applies only to the text in the entry following the headline, and does
  319. not extend beyond the next headline, even if that is lower level.
  320. An entry can be toggled between QUOTE and normal with
  321. \\[org-toggle-fixed-width-section]."
  322. :group 'org-keywords
  323. :type 'string)
  324. (defgroup org-structure nil
  325. "Options concerning the general structure of Org-mode files."
  326. :tag "Org Structure"
  327. :group 'org)
  328. (defgroup org-cycle nil
  329. "Options concerning visibility cycling in Org-mode."
  330. :tag "Org Cycle"
  331. :group 'org-structure)
  332. (defcustom org-cycle-global-at-bob t
  333. "Cycle globally if cursor is at beginning of buffer and not at a headline.
  334. This makes it possible to do global cycling without having to use S-TAB or
  335. C-u TAB. For this special case to work, the first line of the buffer
  336. must not be a headline - it may be empty ot some other text. When used in
  337. this way, `org-cycle-hook' is disables temporarily, to make sure the
  338. cursor stays at the beginning of the buffer.
  339. When this option is nil, don't do anything special at the beginning
  340. of the buffer."
  341. :group 'org-cycle
  342. :type 'boolean)
  343. (defcustom org-cycle-emulate-tab t
  344. "Where should `org-cycle' emulate TAB.
  345. nil Never
  346. white Only in completely white lines
  347. whitestart Only at the beginning of lines, before the first non-white char.
  348. t Everywhere except in headlines
  349. If TAB is used in a place where it does not emulate TAB, the current subtree
  350. visibility is cycled."
  351. :group 'org-cycle
  352. :type '(choice (const :tag "Never" nil)
  353. (const :tag "Only in completely white lines" white)
  354. (const :tag "Before first char in a line" whitestart)
  355. (const :tag "Everywhere except in headlines" t)
  356. ))
  357. (defcustom org-cycle-hook '(org-optimize-window-after-visibility-change)
  358. "Hook that is run after `org-cycle' has changed the buffer visibility.
  359. The function(s) in this hook must accept a single argument which indicates
  360. the new state that was set by the most recent `org-cycle' command. The
  361. argument is a symbol. After a global state change, it can have the values
  362. `overview', `content', or `all'. After a local state change, it can have
  363. the values `folded', `children', or `subtree'."
  364. :group 'org-cycle
  365. :type 'hook)
  366. (defgroup org-edit-structure nil
  367. "Options concerning structure editing in Org-mode."
  368. :tag "Org Edit Structure"
  369. :group 'org-structure)
  370. (defcustom org-odd-levels-only nil
  371. "Non-nil means, skip even levels and only use odd levels for the outline.
  372. This has the effect that two stars are being added/taken away in
  373. promotion/demotion commands. It also influences how levels are
  374. handled by the exporters.
  375. Changing it requires restart of `font-lock-mode' to become effective
  376. for fontification also in regions already fontified.
  377. You may also set this on a per-file basis by adding one of the following
  378. lines to the buffer:
  379. #+STARTUP: odd
  380. #+STARTUP: oddeven"
  381. :group 'org-edit-structure
  382. :group 'org-font-lock
  383. :type 'boolean)
  384. (defcustom org-adapt-indentation t
  385. "Non-nil means, adapt indentation when promoting and demoting.
  386. When this is set and the *entire* text in an entry is indented, the
  387. indentation is increased by one space in a demotion command, and
  388. decreased by one in a promotion command. If any line in the entry
  389. body starts at column 0, indentation is not changed at all."
  390. :group 'org-edit-structure
  391. :type 'boolean)
  392. (defcustom org-insert-heading-hook nil
  393. "Hook being run after inserting a new heading."
  394. :group 'org-edit-structure
  395. :type 'boolean)
  396. (defcustom org-enable-fixed-width-editor t
  397. "Non-nil means, lines starting with \":\" are treated as fixed-width.
  398. This currently only means, they are never auto-wrapped.
  399. When nil, such lines will be treated like ordinary lines.
  400. See also the QUOTE keyword."
  401. :group 'org-edit-structure
  402. :type 'boolean)
  403. (defgroup org-sparse-trees nil
  404. "Options concerning sparse trees in Org-mode."
  405. :tag "Org Sparse Trees"
  406. :group 'org-structure)
  407. (defcustom org-highlight-sparse-tree-matches t
  408. "Non-nil means, highlight all matches that define a sparse tree.
  409. The highlights will automatically disappear the next time the buffer is
  410. changed by an edit command."
  411. :group 'org-sparse-trees
  412. :type 'boolean)
  413. (defcustom org-show-hierarchy-above t
  414. "Non-nil means, show full hierarchy when showing a spot in the tree.
  415. Turning this off makes sparse trees more compact, but also less clear."
  416. :group 'org-sparse-trees
  417. :type 'boolean)
  418. (defcustom org-show-following-heading t
  419. "Non-nil means, show heading following match in `org-occur'.
  420. When doing an `org-occur' it is useful to show the headline which
  421. follows the match, even if they do not match the regexp. This makes it
  422. easier to edit directly inside the sparse tree. However, if you use
  423. `org-occur' mainly as an overview, the following headlines are
  424. unnecessary clutter."
  425. :group 'org-sparse-trees
  426. :type 'boolean)
  427. (defcustom org-occur-hook '(org-first-headline-recenter)
  428. "Hook that is run after `org-occur' has constructed a sparse tree.
  429. This can be used to recenter the window to show as much of the structure
  430. as possible."
  431. :group 'org-sparse-trees
  432. :type 'hook)
  433. (defgroup org-plain-lists nil
  434. "Options concerning plain lists in Org-mode."
  435. :tag "Org Plain lists"
  436. :group 'org-structure)
  437. (defcustom org-cycle-include-plain-lists nil
  438. "Non-nil means, include plain lists into visibility cycling.
  439. This means that during cycling, plain list items will *temporarily* be
  440. interpreted as outline headlines with a level given by 1000+i where i is the
  441. indentation of the bullet. In all other operations, plain list items are
  442. not seen as headlines. For example, you cannot assign a TODO keyword to
  443. such an item."
  444. :group 'org-plain-lists
  445. :type 'boolean)
  446. (defcustom org-plain-list-ordered-item-terminator t
  447. "The character that makes a line with leading number an ordered list item.
  448. Valid values are ?. and ?\). To get both terminators, use t. While
  449. ?. may look nicer, it creates the danger that a line with leading
  450. number may be incorrectly interpreted as an item. ?\) therefore is
  451. the safe choice."
  452. :group 'org-plain-lists
  453. :type '(choice (const :tag "dot like in \"2.\"" ?.)
  454. (const :tag "paren like in \"2)\"" ?\))
  455. (const :tab "both" t)))
  456. (defcustom org-auto-renumber-ordered-lists t
  457. "Non-nil means, automatically renumber ordered plain lists.
  458. Renumbering happens when the sequence have been changed with
  459. \\[org-shiftmetaup] or \\[org-shiftmetadown]. After other editing commands,
  460. use \\[org-ctrl-c-ctrl-c] to trigger renumbering."
  461. :group 'org-plain-lists
  462. :type 'boolean)
  463. (defgroup org-archive nil
  464. "Options concerning archiving in Org-mode."
  465. :tag "Org Archive"
  466. :group 'org-structure)
  467. (defcustom org-archive-location "%s_archive::"
  468. "The location where subtrees should be archived.
  469. This string consists of two parts, separated by a double-colon.
  470. The first part is a file name - when omitted, archiving happens in the same
  471. file. %s will be replaced by the current file name (without directory part).
  472. Archiving to a different file is useful to keep archived entries from
  473. contributing to the Org-mode Agenda.
  474. The part after the double colon is a headline. The archived entries will be
  475. filed under that headline. When omitted, the subtrees are simply filed away
  476. at the end of the file, as top-level entries.
  477. Here are a few examples:
  478. \"%s_archive::\"
  479. If the current file is Projects.org, archive in file
  480. Projects.org_archive, as top-level trees. This is the default.
  481. \"::* Archived Tasks\"
  482. Archive in the current file, under the top-level headline
  483. \"* Archived Tasks\".
  484. \"~/org/archive.org::\"
  485. Archive in file ~/org/archive.org (absolute path), as top-level trees.
  486. \"basement::** Finished Tasks\"
  487. Archive in file ./basement (relative path), as level 3 trees
  488. below the level 2 heading \"** Finished Tasks\".
  489. You may set this option on a per-file basis by adding to the buffer a
  490. line like
  491. #+ARCHIVE: basement::** Finished Tasks"
  492. :group 'org-archive
  493. :type 'string)
  494. (defcustom org-archive-mark-done t
  495. "Non-nil means, mark archived entries as DONE."
  496. :group 'org-archive
  497. :type 'boolean)
  498. (defcustom org-archive-stamp-time t
  499. "Non-nil means, add a time stamp to archived entries.
  500. The time stamp will be added directly after the TODO state keyword in the
  501. first line, so it is probably best to use this in combinations with
  502. `org-archive-mark-done'."
  503. :group 'org-archive
  504. :type 'boolean)
  505. (defgroup org-table nil
  506. "Options concerning tables in Org-mode."
  507. :tag "Org Table"
  508. :group 'org)
  509. (defcustom org-enable-table-editor 'optimized
  510. "Non-nil means, lines starting with \"|\" are handled by the table editor.
  511. When nil, such lines will be treated like ordinary lines.
  512. When equal to the symbol `optimized', the table editor will be optimized to
  513. do the following:
  514. - Use automatic overwrite mode in front of whitespace in table fields.
  515. This make the structure of the table stay in tact as long as the edited
  516. field does not exceed the column width.
  517. - Minimize the number of realigns. Normally, the table is aligned each time
  518. TAB or RET are pressed to move to another field. With optimization this
  519. happens only if changes to a field might have changed the column width.
  520. Optimization requires replacing the functions `self-insert-command',
  521. `delete-char', and `backward-delete-char' in Org-mode buffers, with a
  522. slight (in fact: unnoticeable) speed impact for normal typing. Org-mode is
  523. very good at guessing when a re-align will be necessary, but you can always
  524. force one with \\[org-ctrl-c-ctrl-c].
  525. If you would like to use the optimized version in Org-mode, but the
  526. un-optimized version in OrgTbl-mode, see the variable `orgtbl-optimized'.
  527. This variable can be used to turn on and off the table editor during a session,
  528. but in order to toggle optimization, a restart is required.
  529. See also the variable `org-table-auto-blank-field'."
  530. :group 'org-table
  531. :type '(choice
  532. (const :tag "off" nil)
  533. (const :tag "on" t)
  534. (const :tag "on, optimized" optimized)))
  535. (defcustom orgtbl-optimized (eq org-enable-table-editor 'optimized)
  536. "Non-nil means, use the optimized table editor version for `orgtbl-mode'.
  537. In the optimized version, the table editor takes over all simple keys that
  538. normally just insert a character. In tables, the characters are inserted
  539. in a way to minimize disturbing the table structure (i.e. in overwrite mode
  540. for empty fields). Outside tables, the correct binding of the keys is
  541. restored.
  542. The default for this option is t if the optimized version is also used in
  543. Org-mode. See the variable `org-enable-table-editor' for details. Changing
  544. this variable requires a restart of Emacs to become effective."
  545. :group 'org-table
  546. :type 'boolean)
  547. (defgroup org-table-settings nil
  548. "Settings for tables in Org-mode."
  549. :tag "Org Table Settings"
  550. :group 'org-table)
  551. (defcustom org-table-default-size "5x2"
  552. "The default size for newly created tables, Columns x Rows."
  553. :group 'org-table-settings
  554. :type 'string)
  555. (defcustom org-table-number-regexp "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$"
  556. "Regular expression for recognizing numbers in table columns.
  557. If a table column contains mostly numbers, it will be aligned to the
  558. right. If not, it will be aligned to the left.
  559. The default value of this option is a regular expression which allows
  560. anything which looks remotely like a number as used in scientific
  561. context. For example, all of the following will be considered a
  562. number:
  563. 12 12.2 2.4e-08 2x10^12 4.034+-0.02 2.7(10) >3.5
  564. Other options offered by the customize interface are more restrictive."
  565. :group 'org-table-settings
  566. :type '(choice
  567. (const :tag "Positive Integers"
  568. "^[0-9]+$")
  569. (const :tag "Integers"
  570. "^[-+]?[0-9]+$")
  571. (const :tag "Floating Point Numbers"
  572. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.[0-9]*\\)$")
  573. (const :tag "Floating Point Number or Integer"
  574. "^[-+]?\\([0-9]*\\.[0-9]+\\|[0-9]+\\.?[0-9]*\\)$")
  575. (const :tag "Exponential, Floating point, Integer"
  576. "^[-+]?[0-9.]+\\([eEdD][-+0-9]+\\)?$")
  577. (const :tag "Very General Number-Like"
  578. "^[<>]?[-+^.0-9]*[0-9][-+^.0-9eEdDx()%]*$")
  579. (string :tag "Regexp:")))
  580. (defcustom org-table-number-fraction 0.5
  581. "Fraction of numbers in a column required to make the column align right.
  582. In a column all non-white fields are considered. If at least this
  583. fraction of fields is matched by `org-table-number-fraction',
  584. alignment to the right border applies."
  585. :group 'org-table-settings
  586. :type 'number)
  587. (defgroup org-table-editing nil
  588. "Bahavior of tables during editing in Org-mode."
  589. :tag "Org Table Editing"
  590. :group 'org-table)
  591. (defcustom org-table-automatic-realign t
  592. "Non-nil means, automatically re-align table when pressing TAB or RETURN.
  593. When nil, aligning is only done with \\[org-table-align], or after column
  594. removal/insertion."
  595. :group 'org-table-editing
  596. :type 'boolean)
  597. (defcustom org-table-limit-column-width t ;kw
  598. "Non-nil means, allow to limit the width of table columns with <N> fields."
  599. :group 'org-table-editing
  600. :type 'boolean)
  601. (defcustom org-table-auto-blank-field t
  602. "Non-nil means, automatically blank table field when starting to type into it.
  603. This only happens when typing immediately after a field motion
  604. command (TAB, S-TAB or RET).
  605. Only relevant when `org-enable-table-editor' is equal to `optimized'."
  606. :group 'org-table-editing
  607. :type 'boolean)
  608. (defcustom org-table-tab-jumps-over-hlines t
  609. "Non-nil means, tab in the last column of a table with jump over a hline.
  610. If a horizontal separator line is following the current line,
  611. `org-table-next-field' can either create a new row before that line, or jump
  612. over the line. When this option is nil, a new line will be created before
  613. this line."
  614. :group 'org-table-editing
  615. :type 'boolean)
  616. (defcustom org-table-tab-recognizes-table.el t
  617. "Non-nil means, TAB will automatically notice a table.el table.
  618. When it sees such a table, it moves point into it and - if necessary -
  619. calls `table-recognize-table'."
  620. :group 'org-table-editing
  621. :type 'boolean)
  622. (defgroup org-table-calculation nil
  623. "Options concerning tables in Org-mode."
  624. :tag "Org Table Calculation"
  625. :group 'org-table)
  626. (defcustom org-table-copy-increment t
  627. "Non-nil means, increment when copying current field with \\[org-table-copy-down]."
  628. :group 'org-table-calculation
  629. :type 'boolean)
  630. (defcustom org-calc-default-modes
  631. '(calc-internal-prec 12
  632. calc-float-format (float 5)
  633. calc-angle-mode deg
  634. calc-prefer-frac nil
  635. calc-symbolic-mode nil
  636. calc-date-format (YYYY "-" MM "-" DD " " Www (" " HH ":" mm))
  637. calc-display-working-message t
  638. )
  639. "List with Calc mode settings for use in calc-eval for table formulas.
  640. The list must contain alternating symbols (Calc modes variables and values).
  641. Don't remove any of the default settings, just change the values. Org-mode
  642. relies on the variables to be present in the list."
  643. :group 'org-table-calculation
  644. :type 'plist)
  645. (defcustom org-table-formula-evaluate-inline t
  646. "Non-nil means, TAB and RET evaluate a formula in current table field.
  647. If the current field starts with an equal sign, it is assumed to be a formula
  648. which should be evaluated as described in the manual and in the documentation
  649. string of the command `org-table-eval-formula'. This feature requires the
  650. Emacs calc package.
  651. When this variable is nil, formula calculation is only available through
  652. the command \\[org-table-eval-formula]."
  653. :group 'org-table-calculation
  654. :type 'boolean)
  655. (defcustom org-table-formula-use-constants t
  656. "Non-nil means, interpret constants in formulas in tables.
  657. A constant looks like `$c' or `$Grav' and will be replaced before evaluation
  658. by the value given in `org-table-formula-constants', or by a value obtained
  659. from the `constants.el' package."
  660. :group 'org-table-calculation
  661. :type 'boolean)
  662. (defcustom org-table-formula-constants nil
  663. "Alist with constant names and values, for use in table formulas.
  664. The car of each element is a name of a constant, without the `$' before it.
  665. The cdr is the value as a string. For example, if you'd like to use the
  666. speed of light in a formula, you would configure
  667. (setq org-table-formula-constants '((\"c\" . \"299792458.\")))
  668. and then use it in an equation like `$1*$c'."
  669. :group 'org-table-calculation
  670. :type '(repeat
  671. (cons (string :tag "name")
  672. (string :tag "value"))))
  673. (defcustom org-table-formula-numbers-only nil
  674. "Non-nil means, calculate only with numbers in table formulas.
  675. Then all input fields will be converted to a number, and the result
  676. must also be a number. When nil, calc's full potential is available
  677. in table calculations, including symbolics etc."
  678. :group 'org-table-calculation
  679. :type 'boolean)
  680. (defcustom org-table-allow-automatic-line-recalculation t
  681. "Non-nil means, lines marked with |#| or |*| will be recomputed automatically.
  682. Automatically means, when TAB or RET or C-c C-c are pressed in the line."
  683. :group 'org-table-calculation
  684. :type 'boolean)
  685. (defgroup org-link nil
  686. "Options concerning links in Org-mode."
  687. :tag "Org Link"
  688. :group 'org)
  689. (defcustom org-descriptive-links t
  690. "Non-nil means, hide link part and only show description of bracket links.
  691. Bracket links are like [[link][descritpion]]. This variable sets the initial
  692. state in new org-mode buffers. The setting can then be toggled on a
  693. per-buffer basis from the Org->Hyperlinks menu."
  694. :group 'org-link
  695. :type 'boolean)
  696. (defcustom org-link-style 'bracket
  697. "The style of links to be inserted with \\[org-insert-link].
  698. Possible values are:
  699. bracket [[link][description]]. This is recommended
  700. plain Description \\n link. The old way, no longer recommended."
  701. :group 'org-link
  702. :type '(choice
  703. (const :tag "Bracket (recommended)" bracket)
  704. (const :tag "Plain (no longer recommended)" plain)))
  705. (defcustom org-link-format "%s"
  706. "Default format for external, URL-like linkes in the buffer.
  707. This is a format string for printf, %s will be replaced by the link text.
  708. The recommended value is just \"%s\", since links will be protected by
  709. enclosing them in double brackets. If you prefer plain links (see variable
  710. `org-link-style'), \"<%s>\" is useful. Some people also recommend an
  711. additional URL: prefix, so the format would be \"<URL:%s>\"."
  712. :group 'org-link
  713. :type '(choice
  714. (const :tag "\"%s\" (e.g. http://www.there.com)" "%s")
  715. (const :tag "\"<%s>\" (e.g. <http://www.there.com>)" "<%s>")
  716. (const :tag "\"<URL:%s>\" (e.g. <URL:http://www.there.com>)" "<URL:%s>")
  717. (string :tag "Other" :value "<%s>")))
  718. (defcustom org-link-file-path-type 'adaptive
  719. "How the path name in file links should be stored.
  720. Valid values are:
  721. relative relative to the current directory, i.e. the directory of the file
  722. into which the link is being inserted.
  723. absolute absolute path, if possible with ~ for home directory.
  724. noabbrev absolute path, no abbreviation of home directory.
  725. adaptive Use relative path for files in the current directory and sub-
  726. directories of it. For other files, use an absolute path."
  727. :group 'org-link
  728. :type '(choice
  729. (const relative)
  730. (const absolute)
  731. (const noabbrev)
  732. (const adaptive)))
  733. (defcustom org-activate-links '(bracket angle plain radio tag date)
  734. "Types of links that should be activated in Org-mode files.
  735. This is a list of symbols, each leading to the activation of a certain link
  736. type. In principle, it does not hurt to turn on most link types - there may
  737. be a small gain when turning off unused link types. The types are:
  738. bracket The recommended [[link][description]] or [[link]] links with hiding.
  739. angular Links in angular brackes that may contain whitespace like
  740. <bbdb:Carsten Dominik>.
  741. plain Plain links in normal text, no whitespace, like http://google.com.
  742. radio Text that is matched by a radio target, see manual for details.
  743. tag Tag settings in a headline (link to tag search).
  744. date Time stamps (link to calendar).
  745. camel CamelCase words defining text searches.
  746. Changing this variable requires a restart of Emacs to become effective."
  747. :group 'org-link
  748. :type '(set (const :tag "Double bracket links (new style)" bracket)
  749. (const :tag "Angular bracket links (old style)" angular)
  750. (const :tag "plain text links" plain)
  751. (const :tag "Radio target matches" radio)
  752. (const :tag "Tags" tag)
  753. (const :tag "Timestamps" date)
  754. (const :tag "CamelCase words" camel)))
  755. (defgroup org-link-store nil
  756. "Options concerning storing links in Org-mode"
  757. :tag "Org Store Link"
  758. :group 'org-link)
  759. (defcustom org-context-in-file-links t
  760. "Non-nil means, file links from `org-store-link' contain context.
  761. A search string will be added to the file name with :: as separator and
  762. used to find the context when the link is activated by the command
  763. `org-open-at-point'.
  764. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  765. negates this setting for the duration of the command."
  766. :group 'org-link-store
  767. :type 'boolean)
  768. (defcustom org-file-link-context-use-camel-case nil
  769. "Non-nil means, use CamelCase to store a search context in a file link.
  770. When nil, the search string simply consists of the words of the string.
  771. CamelCase is deprecated, and support for it may be dropped in the future."
  772. :group 'org-link-store
  773. :type 'boolean)
  774. (defcustom org-keep-stored-link-after-insertion nil
  775. "Non-nil means, keep link in list for entire session.
  776. The command `org-store-link' adds a link pointing to the current
  777. location to an internal list. These links accumulate during a session.
  778. The command `org-insert-link' can be used to insert links into any
  779. Org-mode file (offering completion for all stored links). When this
  780. option is nil, every link which has been inserted once using \\[org-insert-link]
  781. will be removed from the list, to make completing the unused links
  782. more efficient."
  783. :group 'org-link-store
  784. :type 'boolean)
  785. (defcustom org-usenet-links-prefer-google nil
  786. "Non-nil means, `org-store-link' will create web links to Google groups.
  787. When nil, Gnus will be used for such links.
  788. Using a prefix arg to the command \\[org-store-link] (`org-store-link')
  789. negates this setting for the duration of the command."
  790. :group 'org-link-store
  791. :type 'boolean)
  792. (defgroup org-link-follow nil
  793. "Options concerning following links in Org-mode"
  794. :tag "Org Follow Link"
  795. :group 'org-link)
  796. (defcustom org-tab-follows-link nil
  797. "Non-nil means, on links TAB will follow the link.
  798. Needs to be set before org.el is loaded."
  799. :group 'org-link-follow
  800. :type 'boolean)
  801. (defcustom org-return-follows-link nil
  802. "Non-nil means, on links RET will follow the link.
  803. Needs to be set before org.el is loaded."
  804. :group 'org-link-follow
  805. :type 'boolean)
  806. (defcustom org-mouse-1-follows-link t
  807. "Non-nil means, mouse-1 on a link will follow the link.
  808. A longer mouse click will still set point. Does not wortk on XEmacs.
  809. Needs to be set before org.el is loaded."
  810. :group 'org-link-follow
  811. :type 'boolean)
  812. (defcustom org-mark-ring-length 4
  813. "Number of different positions to be recorded in the ring
  814. Changing this requires a restart of Emacs to work correctly."
  815. :group 'org-link-follow
  816. :type 'interger)
  817. (defcustom org-link-frame-setup
  818. '((vm . vm-visit-folder-other-frame)
  819. (gnus . gnus-other-frame)
  820. (file . find-file-other-window))
  821. "Setup the frame configuration for following links.
  822. When following a link with Emacs, it may often be useful to display
  823. this link in another window or frame. This variable can be used to
  824. set this up for the different types of links.
  825. For VM, use any of
  826. `vm-visit-folder'
  827. `vm-visit-folder-other-frame'
  828. For Gnus, use any of
  829. `gnus'
  830. `gnus-other-frame'
  831. For FILE, use any of
  832. `find-file'
  833. `find-file-other-window'
  834. `find-file-other-frame'
  835. For the calendar, use the variable `calendar-setup'.
  836. For BBDB, it is currently only possible to display the matches in
  837. another window."
  838. :group 'org-link-follow
  839. :type '(list
  840. (cons (const vm)
  841. (choice
  842. (const vm-visit-folder)
  843. (const vm-visit-folder-other-window)
  844. (const vm-visit-folder-other-frame)))
  845. (cons (const gnus)
  846. (choice
  847. (const gnus)
  848. (const gnus-other-frame)))
  849. (cons (const file)
  850. (choice
  851. (const find-file)
  852. (const find-file-other-window)
  853. (const find-file-other-frame)))))
  854. (defcustom org-open-non-existing-files nil
  855. "Non-nil means, `org-open-file' will open non-existing file.
  856. When nil, an error will be generated."
  857. :group 'org-link-follow
  858. :type 'boolean)
  859. (defcustom org-confirm-shell-link-function 'yes-or-no-p
  860. "Non-nil means, ask for confirmation before executing shell links.
  861. Shell links can be dangerous, just thing about a link
  862. [[shell:rm -rf ~/*][Google Search]]
  863. This link would show up in your Org-mode document as \"Google Search\"
  864. but really it would remove your entire home directory.
  865. Therefore I *definitely* advise against setting this variable to nil.
  866. Just change it to `y-or-n-p' of you want to confirm with a single key press
  867. rather than having to type \"yes\"."
  868. :group 'org-link-follow
  869. :type '(choice
  870. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  871. (const :tag "with y-or-n (faster)" y-or-n-p)
  872. (const :tag "no confirmation (dangerous)" nil)))
  873. (defcustom org-confirm-elisp-link-function 'yes-or-no-p
  874. "Non-nil means, ask for confirmation before executing elisp links.
  875. Elisp links can be dangerous, just thing about a link
  876. [[elisp:(shell-command \"rm -rf ~/*\")][Google Search]]
  877. This link would show up in your Org-mode document as \"Google Search\"
  878. but really it would remove your entire home directory.
  879. Therefore I *definitely* advise against setting this variable to nil.
  880. Just change it to `y-or-n-p' of you want to confirm with a single key press
  881. rather than having to type \"yes\"."
  882. :group 'org-link-follow
  883. :type '(choice
  884. (const :tag "with yes-or-no (safer)" yes-or-no-p)
  885. (const :tag "with y-or-n (faster)" y-or-n-p)
  886. (const :tag "no confirmation (dangerous)" nil)))
  887. (defconst org-file-apps-defaults-gnu
  888. '((t . mailcap))
  889. "Default file applications on a UNIX or GNU/Linux system.
  890. See `org-file-apps'.")
  891. (defconst org-file-apps-defaults-macosx
  892. '((t . "open %s")
  893. ("ps" . "gv %s")
  894. ("ps.gz" . "gv %s")
  895. ("eps" . "gv %s")
  896. ("eps.gz" . "gv %s")
  897. ("dvi" . "xdvi %s")
  898. ("fig" . "xfig %s"))
  899. "Default file applications on a MacOS X system.
  900. The system \"open\" is known as a default, but we use X11 applications
  901. for some files for which the OS does not have a good default.
  902. See `org-file-apps'.")
  903. (defconst org-file-apps-defaults-windowsnt
  904. (list (cons t
  905. (list (if (featurep 'xemacs)
  906. 'mswindows-shell-execute
  907. 'w32-shell-execute)
  908. "open" 'file)))
  909. "Default file applications on a Windows NT system.
  910. The system \"open\" is used for most files.
  911. See `org-file-apps'.")
  912. (defcustom org-file-apps
  913. '(
  914. ("txt" . emacs)
  915. ("tex" . emacs)
  916. ("ltx" . emacs)
  917. ("org" . emacs)
  918. ("el" . emacs)
  919. ("bib" . emacs)
  920. )
  921. "External applications for opening `file:path' items in a document.
  922. Org-mode uses system defaults for different file types, but
  923. you can use this variable to set the application for a given file
  924. extension. The entries in this list are cons cells where the car identifies
  925. files and the cdr the corresponding command. Possible values for the
  926. file identifier are
  927. \"ext\" A string identifying an extension
  928. `directory' Matches a directory
  929. t Default for all remaining files
  930. Possible values for the command are:
  931. `emacs' The file will be visited by the current Emacs process.
  932. `default' Use the default application for this file type.
  933. string A command to be executed by a shell; %s will be replaced
  934. by the path to the file.
  935. sexp A Lisp form which will be evaluated. The file path will
  936. be available in the Lisp variable `file'.
  937. For more examples, see the system specific constants
  938. `org-file-apps-defaults-macosx'
  939. `org-file-apps-defaults-windowsnt'
  940. `org-file-apps-defaults-gnu'."
  941. :group 'org-link-follow
  942. :type '(repeat
  943. (cons (choice :value ""
  944. (string :tag "Extension")
  945. (const :tag "Default for unrecognized files" t)
  946. (const :tag "Links to a directory" directory))
  947. (choice :value ""
  948. (const :tag "Visit with Emacs" emacs)
  949. (const :tag "Use system default" default)
  950. (string :tag "Command")
  951. (sexp :tag "Lisp form")))))
  952. (defcustom org-mhe-search-all-folders nil
  953. "Non-nil means, that the search for the mh-message will be extended to
  954. all folders if the message cannot be found in the folder given in the link.
  955. Searching all folders is very effective with one of the search engines
  956. supported by MH-E, but will be slow with pick."
  957. :group 'org-link-follow
  958. :type 'boolean)
  959. (defgroup org-remember nil
  960. "Options concerning interaction with remember.el."
  961. :tag "Org Remember"
  962. :group 'org)
  963. (defcustom org-directory "~/org"
  964. "Directory with org files.
  965. This directory will be used as default to prompt for org files.
  966. Used by the hooks for remember.el."
  967. :group 'org-remember
  968. :type 'directory)
  969. (defcustom org-default-notes-file "~/.notes"
  970. "Default target for storing notes.
  971. Used by the hooks for remember.el. This can be a string, or nil to mean
  972. the value of `remember-data-file'."
  973. :group 'org-remember
  974. :type '(choice
  975. (const :tag "Default from remember-data-file" nil)
  976. file))
  977. (defcustom org-remember-templates nil
  978. "Templates for the creation of remember buffers.
  979. When nil, just let remember make the buffer.
  980. When not nil, this is a list of 3-element lists. In each entry, the first
  981. element is a character, a unique key to select this template.
  982. The second element is the template. The third element is optional and can
  983. specify a destination file for remember items created with this template.
  984. The default file is given by `org-default-notes-file'.
  985. The template specifies the structure of the remember buffer. It should have
  986. a first line starting with a star, to act as the org-mode headline.
  987. Furthermore, the following %-escapes will be replaced with content:
  988. %t time stamp, date only
  989. %T time stamp with date and time
  990. %u inactive time stamp, date only
  991. %U inactive time stamp with date and time
  992. %n user name
  993. %a annotation, normally the link created with org-store-link
  994. %i initial content, the region when remember is called with C-u.
  995. If %i is indented, the entire inserted text will be indented as well.
  996. %? This will be removed, and the cursor placed at this position."
  997. :group 'org-remember
  998. :type '(repeat :tag "enabled"
  999. (list :value (?a "\n" nil)
  1000. (character :tag "Selection Key")
  1001. (string :tag "Template")
  1002. (file :tag "Destination file (optional)"))))
  1003. (defcustom org-reverse-note-order nil
  1004. "Non-nil means, store new notes at the beginning of a file or entry.
  1005. When nil, new notes will be filed to the end of a file or entry."
  1006. :group 'org-remember
  1007. :type '(choice
  1008. (const :tag "Reverse always" t)
  1009. (const :tag "Reverse never" nil)
  1010. (repeat :tag "By file name regexp"
  1011. (cons regexp boolean))))
  1012. (defgroup org-todo nil
  1013. "Options concerning TODO items in Org-mode."
  1014. :tag "Org TODO"
  1015. :group 'org)
  1016. (defcustom org-todo-keywords '("TODO" "DONE")
  1017. "List of TODO entry keywords.
  1018. \\<org-mode-map>By default, this is '(\"TODO\" \"DONE\"). The last entry in the list is
  1019. considered to mean that the entry is \"done\". All the other mean that
  1020. action is required, and will make the entry show up in todo lists, diaries
  1021. etc.
  1022. The command \\[org-todo] cycles an entry through these states, and an
  1023. additional state where no keyword is present. For details about this
  1024. cycling, see also the variable `org-todo-interpretation'
  1025. Changes become only effective after restarting Emacs."
  1026. :group 'org-todo
  1027. :group 'org-keywords
  1028. :type '(repeat (string :tag "Keyword")))
  1029. (defcustom org-todo-interpretation 'sequence
  1030. "Controls how TODO keywords are interpreted.
  1031. This variable is only relevant if `org-todo-keywords' contains more than two
  1032. states. \\<org-mode-map>Possible values are `sequence' and `type'.
  1033. When `sequence', \\[org-todo] will always switch to the next state in the
  1034. `org-todo-keywords' list. When `type', \\[org-todo] only cycles from state
  1035. to state when executed several times in direct succession. Otherwise, it
  1036. switches directly to DONE from any state.
  1037. See the manual for more information."
  1038. :group 'org-todo
  1039. :group 'org-keywords
  1040. :type '(choice (const sequence)
  1041. (const type)))
  1042. (defcustom org-after-todo-state-change-hook nil
  1043. "Hook which is run after the state of a TODO item was changed.
  1044. The new state (a string with a TODO keyword, or nil) is available in the
  1045. Lisp variable `state'."
  1046. :group 'org-todo
  1047. :type 'hook)
  1048. (defcustom org-log-done nil
  1049. "When set, insert a (non-active) time stamp when TODO entry is marked DONE.
  1050. When the state of an entry is changed from nothing to TODO, remove a previous
  1051. closing date.
  1052. This can also be configured on a per-file basis by adding one of
  1053. the following lines anywhere in the buffer:
  1054. #+STARTUP: logging
  1055. #+STARTUP: nologging"
  1056. :group 'org-todo
  1057. :type 'boolean)
  1058. (defgroup org-priorities nil
  1059. "Priorities in Org-mode."
  1060. :tag "Org Priorities"
  1061. :group 'org-todo)
  1062. (defcustom org-default-priority ?B
  1063. "The default priority of TODO items.
  1064. This is the priority an item get if no explicit priority is given."
  1065. :group 'org-priorities
  1066. :type 'character)
  1067. (defcustom org-lowest-priority ?C
  1068. "The lowest priority of TODO items. A character like ?A, ?B etc."
  1069. :group 'org-priorities
  1070. :type 'character)
  1071. (defgroup org-time nil
  1072. "Options concerning time stamps and deadlines in Org-mode."
  1073. :tag "Org Time"
  1074. :group 'org)
  1075. (defcustom org-insert-labeled-timestamps-at-point nil
  1076. "Non-nil means, SCHEDULED and DEADLINE timestamps are inserted at point.
  1077. When nil, these labeled time stamps are forces into the second line of an
  1078. entry, just after the headline. When scheduling from the global TODO list,
  1079. the time stamp will always be forced into the second line."
  1080. :group 'org-time
  1081. :type 'boolean)
  1082. (defconst org-time-stamp-formats '("<%Y-%m-%d %a>" . "<%Y-%m-%d %a %H:%M>")
  1083. "Formats for `format-time-string' which are used for time stamps.
  1084. It is not recommended to change this constant.")
  1085. (defcustom org-time-stamp-rounding-minutes 0
  1086. "Number of minutes to round time stamps to upon insertion.
  1087. When zero, insert the time unmodified. Useful rounding numbers
  1088. should be factors of 60, so for example 5, 10, 15.
  1089. When this is not zero, you can still force an exact time-stamp by using
  1090. a double prefix argument to a time-stamp command like `C-c .' or `C-c !'."
  1091. :group 'org-time
  1092. :type 'integer)
  1093. (defcustom org-deadline-warning-days 30
  1094. "No. of days before expiration during which a deadline becomes active.
  1095. This variable governs the display in the org file."
  1096. :group 'org-time
  1097. :type 'number)
  1098. (defcustom org-popup-calendar-for-date-prompt t
  1099. "Non-nil means, pop up a calendar when prompting for a date.
  1100. In the calendar, the date can be selected with mouse-1. However, the
  1101. minibuffer will also be active, and you can simply enter the date as well.
  1102. When nil, only the minibuffer will be available."
  1103. :group 'org-time
  1104. :type 'boolean)
  1105. (defcustom org-calendar-follow-timestamp-change t
  1106. "Non-nil means, make the calendar window follow timestamp changes.
  1107. When a timestamp is modified and the calendar window is visible, it will be
  1108. moved to the new date."
  1109. :group 'org-time
  1110. :type 'boolean)
  1111. (defgroup org-tags nil
  1112. "Options concerning tags in Org-mode."
  1113. :tag "Org Tags"
  1114. :group 'org)
  1115. (defcustom org-tag-alist nil
  1116. "List of tags allowed in Org-mode files.
  1117. When this list is nil, Org-mode will base TAG input on what is already in the
  1118. buffer.
  1119. The value of this variable is an alist, the car may be (and should) be a
  1120. character that is used to select that tag through the fast-tag-selection
  1121. interface. See the manual for details."
  1122. :group 'org-tags
  1123. :type '(repeat
  1124. (choice
  1125. (cons (string :tag "Tag name")
  1126. (character :tag "Access char"))
  1127. (const :tag "Start radio group" (:startgroup))
  1128. (const :tag "End radio group" (:endgroup)))))
  1129. (defcustom org-use-fast-tag-selection 'auto
  1130. "Non-nil means, use fast tag selection scheme.
  1131. This is a special interface to select and deselect tags with single keys.
  1132. When nil, fast selection is never used.
  1133. When the symbol `auto', fast selection is used if and only if selection
  1134. characters for tags have been configured, either through the variable
  1135. `org-tag-alist' or through a #+TAGS line in the buffer.
  1136. When t, fast selection is always used and selection keys are assigned
  1137. automatically if necessary."
  1138. :group 'org-tags
  1139. :type '(choice
  1140. (const :tag "Always" t)
  1141. (const :tag "Never" nil)
  1142. (const :tag "When selection characters are configured" 'auto)))
  1143. (defcustom org-tags-column 48
  1144. "The column to which tags should be indented in a headline.
  1145. If this number is positive, it specifies the column. If it is negative,
  1146. it means that the tags should be flushright to that column. For example,
  1147. -79 works well for a normal 80 character screen."
  1148. :group 'org-tags
  1149. :type 'integer)
  1150. (defcustom org-auto-align-tags t
  1151. "Non-nil means, realign tags after pro/demotion of TODO state change.
  1152. These operations change the length of a headline and therefore shift
  1153. the tags around. With this options turned on, after each such operation
  1154. the tags are again aligned to `org-tags-column'."
  1155. :group 'org-tags
  1156. :type 'boolean)
  1157. (defcustom org-use-tag-inheritance t
  1158. "Non-nil means, tags in levels apply also for sublevels.
  1159. When nil, only the tags directly given in a specific line apply there.
  1160. If you turn off this option, you very likely want to turn on the
  1161. companion option `org-tags-match-list-sublevels'."
  1162. :group 'org-tags
  1163. :type 'boolean)
  1164. (defcustom org-tags-match-list-sublevels nil
  1165. "Non-nil means list also sublevels of headlines matching tag search.
  1166. Because of tag inheritance (see variable `org-use-tag-inheritance'),
  1167. the sublevels of a headline matching a tag search often also match
  1168. the same search. Listing all of them can create very long lists.
  1169. Setting this variable to nil causes subtrees of a match to be skipped.
  1170. This option is off by default, because inheritance in on. If you turn
  1171. inheritance off, you very likely want to turn this option on.
  1172. As a special case, if the tag search is restricted to TODO items, the
  1173. value of this variable is ignored and sublevels are always checked, to
  1174. make sure all corresponding TODO items find their way into the list."
  1175. :group 'org-tags
  1176. :type 'boolean)
  1177. (defvar org-tags-history nil
  1178. "History of minibuffer reads for tags.")
  1179. (defvar org-last-tags-completion-table nil
  1180. "The last used completion table for tags.")
  1181. (defgroup org-agenda nil
  1182. "Options concerning agenda display Org-mode."
  1183. :tag "Org Agenda"
  1184. :group 'org)
  1185. (defvar org-category nil
  1186. "Variable used by org files to set a category for agenda display.
  1187. Such files should use a file variable to set it, for example
  1188. -*- mode: org; org-category: \"ELisp\"
  1189. or contain a special line
  1190. #+CATEGORY: ELisp
  1191. If the file does not specify a category, then file's base name
  1192. is used instead.")
  1193. (make-variable-buffer-local 'org-category)
  1194. (defcustom org-agenda-files nil
  1195. "The files to be used for agenda display.
  1196. Entries may be added to this list with \\[org-agenda-file-to-front] and removed with
  1197. \\[org-remove-file]. You can also use customize to edit the list.
  1198. If the value of the variable is not a list but a single file name, then
  1199. the list of agenda files is actually stored and maintained in that file, one
  1200. agenda file per line."
  1201. :group 'org-agenda
  1202. :type '(choice
  1203. (repeat :tag "List of files" file)
  1204. (file :tag "Store list in a file\n" :value "~/.agenda_files")))
  1205. (defcustom org-agenda-custom-commands '(("w" todo "WAITING"))
  1206. "Custom commands for the agenda.
  1207. These commands will be offered on the splash screen displayed by the
  1208. agenda dispatcher \\[org-agenda]. Each entry is a list of 3 items:
  1209. key The key (a single char as a string) to be associated with the command.
  1210. type The command type, any of the following symbols:
  1211. todo Entries with a specific TODO keyword, in all agenda files.
  1212. tags Tags match in all agenda files.
  1213. tags-todo Tags match in all agenda files, TODO entries only.
  1214. todo-tree Sparse tree of specific TODO keyword in *current* file.
  1215. tags-tree Sparse tree with all tags matches in *current* file.
  1216. occur-tree Occur sparse tree for current file.
  1217. match What to search for:
  1218. - a single keyword for TODO keyword searches
  1219. - a tags match expression for tags searches
  1220. - a regular expression for occur searches"
  1221. :group 'org-agenda
  1222. :type '(repeat
  1223. (list (string :tag "Key")
  1224. (choice :tag "Type"
  1225. (const :tag "Tags search in all agenda files" tags)
  1226. (const :tag "Tags search of TODO entries, all agenda files" tags-todo)
  1227. (const :tag "TODO keyword search in all agenda files" todo)
  1228. (const :tag "Tags sparse tree in current buffer" tags-tree)
  1229. (const :tag "TODO keyword tree in current buffer" todo-tree)
  1230. (const :tag "Occur tree in current buffer" occur-tree))
  1231. (string :tag "Match"))))
  1232. (defcustom org-agenda-todo-list-sublevels t
  1233. "Non-nil means, check also the sublevels of a TODO entry for TODO entries.
  1234. When nil, the sublevels of a TODO entry are not checked, resulting in
  1235. potentially much shorter TODO lists."
  1236. :group 'org-agenda
  1237. :group 'org-todo
  1238. :type 'boolean)
  1239. (defcustom org-agenda-todo-ignore-scheduled nil
  1240. "Non-nil means, don't show scheduled entries in the global todo list.
  1241. The idea behind this is that by scheduling it, you have already taken care
  1242. of this item."
  1243. :group 'org-agenda
  1244. :group 'org-todo
  1245. :type 'boolean)
  1246. (defcustom org-agenda-include-all-todo nil
  1247. "Non-nil means, the agenda will always contain all TODO entries.
  1248. When nil, date-less entries will only be shown if `org-agenda' is called
  1249. with a prefix argument.
  1250. When non-nil, the TODO entries will be listed at the top of the agenda, before
  1251. the entries for specific days."
  1252. :group 'org-agenda
  1253. :type 'boolean)
  1254. (defcustom org-agenda-include-diary nil
  1255. "If non-nil, include in the agenda entries from the Emacs Calendar's diary."
  1256. :group 'org-agenda
  1257. :type 'boolean)
  1258. (defcustom org-calendar-to-agenda-key [?c]
  1259. "The key to be installed in `calendar-mode-map' for switching to the agenda.
  1260. The command `org-calendar-goto-agenda' will be bound to this key. The
  1261. default is the character `c' because then `c' can be used to switch back and
  1262. forth between agenda and calendar."
  1263. :group 'org-agenda
  1264. :type 'sexp)
  1265. (defgroup org-agenda-setup nil
  1266. "Options concerning setting up the Agenda window in Org Mode."
  1267. :tag "Org Agenda Window Setup"
  1268. :group 'org-agenda)
  1269. (defcustom org-agenda-mouse-1-follows-link nil
  1270. "Non-nil means, mouse-1 on a link will follow the link in the agenda.
  1271. A longer mouse click will still set point. Does not wortk on XEmacs.
  1272. Needs to be set before org.el is loaded."
  1273. :group 'org-agenda-setup
  1274. :type 'boolean)
  1275. (defcustom org-agenda-start-with-follow-mode nil
  1276. "The initial value of follwo-mode in a newly created agenda window."
  1277. :group 'org-agenda-setup
  1278. :type 'boolean)
  1279. (defcustom org-select-agenda-window t
  1280. "Non-nil means, after creating an agenda, move cursor into Agenda window.
  1281. When nil, cursor will remain in the current window."
  1282. :group 'org-agenda-setup
  1283. :type 'boolean)
  1284. (defcustom org-fit-agenda-window t
  1285. "Non-nil means, change window size of agenda to fit content."
  1286. :group 'org-agenda-setup
  1287. :type 'boolean)
  1288. (defgroup org-agenda-display nil
  1289. "Options concerning what to display initially in Agenda."
  1290. :tag "Org Agenda Display"
  1291. :group 'org-agenda)
  1292. (defcustom org-agenda-show-all-dates t
  1293. "Non-nil means, `org-agenda' shows every day in the selected range.
  1294. When nil, only the days which actually have entries are shown."
  1295. :group 'org-agenda-display
  1296. :type 'boolean)
  1297. (defcustom org-agenda-start-on-weekday 1
  1298. "Non-nil means, start the overview always on the specified weekday.
  1299. 0 denotes Sunday, 1 denotes Monday etc.
  1300. When nil, always start on the current day."
  1301. :group 'org-agenda-display
  1302. :type '(choice (const :tag "Today" nil)
  1303. (number :tag "Weekday No.")))
  1304. (defcustom org-agenda-ndays 7
  1305. "Number of days to include in overview display.
  1306. Should be 1 or 7."
  1307. :group 'org-agenda-display
  1308. :type 'number)
  1309. (defcustom org-agenda-use-time-grid t
  1310. "Non-nil means, show a time grid in the agenda schedule.
  1311. A time grid is a set of lines for specific times (like every two hours between
  1312. 8:00 and 20:00). The items scheduled for a day at specific times are
  1313. sorted in between these lines.
  1314. For details about when the grid will be shown, and what it will look like, see
  1315. the variable `org-agenda-time-grid'."
  1316. :group 'org-agenda-display
  1317. :type 'boolean)
  1318. (defcustom org-agenda-time-grid
  1319. '((daily today require-timed)
  1320. "----------------"
  1321. (800 1000 1200 1400 1600 1800 2000))
  1322. "The settings for time grid for agenda display.
  1323. This is a list of three items. The first item is again a list. It contains
  1324. symbols specifying conditions when the grid should be displayed:
  1325. daily if the agenda shows a single day
  1326. weekly if the agenda shows an entire week
  1327. today show grid on current date, independent of daily/weekly display
  1328. require-timed show grid only if at least on item has a time specification
  1329. The second item is a string which will be places behing the grid time.
  1330. The third item is a list of integers, indicating the times that should have
  1331. a grid line."
  1332. :group 'org-agenda-display
  1333. :type
  1334. '(list
  1335. (set :greedy t :tag "Grid Display Options"
  1336. (const :tag "Show grid in single day agenda display" daily)
  1337. (const :tag "Show grid in weekly agenda display" weekly)
  1338. (const :tag "Always show grid for today" today)
  1339. (const :tag "Show grid only if any timed entries are present"
  1340. require-timed)
  1341. (const :tag "Skip grid times already present in an entry"
  1342. remove-match))
  1343. (string :tag "Grid String")
  1344. (repeat :tag "Grid Times" (integer :tag "Time"))))
  1345. (defcustom org-agenda-sorting-strategy '(time-up category-keep priority-down)
  1346. "Sorting structure for the agenda items of a single day.
  1347. This is a list of symbols which will be used in sequence to determine
  1348. if an entry should be listed before another entry. The following
  1349. symbols are recognized:
  1350. time-up Put entries with time-of-day indications first, early first
  1351. time-down Put entries with time-of-day indications first, late first
  1352. category-keep Keep the default order of categories, corresponding to the
  1353. sequence in `org-agenda-files'.
  1354. category-up Sort alphabetically by category, A-Z.
  1355. category-down Sort alphabetically by category, Z-A.
  1356. priority-up Sort numerically by priority, high priority last.
  1357. priority-down Sort numerically by priority, high priority first.
  1358. The different possibilities will be tried in sequence, and testing stops
  1359. if one comparison returns a \"not-equal\". For example, the default
  1360. '(time-up category-keep priority-down)
  1361. means: Pull out all entries having a specified time of day and sort them,
  1362. in order to make a time schedule for the current day the first thing in the
  1363. agenda listing for the day. Of the entries without a time indication, keep
  1364. the grouped in categories, don't sort the categories, but keep them in
  1365. the sequence given in `org-agenda-files'. Within each category sort by
  1366. priority.
  1367. Leaving out `category-keep' would mean that items will be sorted across
  1368. categories by priority."
  1369. :group 'org-agenda-display
  1370. :type '(repeat
  1371. (choice
  1372. (const time-up)
  1373. (const time-down)
  1374. (const category-keep)
  1375. (const category-up)
  1376. (const category-down)
  1377. (const priority-up)
  1378. (const priority-down))))
  1379. (defcustom org-sort-agenda-notime-is-late t
  1380. "Non-nil means, items without time are considered late.
  1381. This is only relevant for sorting. When t, items which have no explicit
  1382. time like 15:30 will be considered as 99:01, i.e. later than any items which
  1383. do have a time. When nil, the default time is before 0:00. You can use this
  1384. option to decide if the schedule for today should come before or after timeless
  1385. agenda entries."
  1386. :group 'org-agenda-display
  1387. :type 'boolean)
  1388. (defgroup org-agenda-prefix nil
  1389. "Options concerning the entry prefix in the Org-mode agenda display."
  1390. :tag "Org Agenda Prefix"
  1391. :group 'org-agenda)
  1392. (defcustom org-agenda-prefix-format " %-12:c%?-12t% s"
  1393. "Format specification for the prefix of items in the agenda buffer.
  1394. This format works similar to a printf format, with the following meaning:
  1395. %c the category of the item, \"Diary\" for entries from the diary, or
  1396. as given by the CATEGORY keyword or derived from the file name.
  1397. %T the first tag of the item.
  1398. %t the time-of-day specification if one applies to the entry, in the
  1399. format HH:MM
  1400. %s Scheduling/Deadline information, a short string
  1401. All specifiers work basically like the standard `%s' of printf, but may
  1402. contain two additional characters: A question mark just after the `%' and
  1403. a whitespace/punctuation character just before the final letter.
  1404. If the first character after `%' is a question mark, the entire field
  1405. will only be included if the corresponding value applies to the
  1406. current entry. This is useful for fields which should have fixed
  1407. width when present, but zero width when absent. For example,
  1408. \"%?-12t\" will result in a 12 character time field if a time of the
  1409. day is specified, but will completely disappear in entries which do
  1410. not contain a time.
  1411. If there is punctuation or whitespace character just before the final
  1412. format letter, this character will be appended to the field value if
  1413. the value is not empty. For example, the format \"%-12:c\" leads to
  1414. \"Diary: \" if the category is \"Diary\". If the category were be
  1415. empty, no additional colon would be interted.
  1416. The default value of this option is \" %-12:c%?-12t% s\", meaning:
  1417. - Indent the line with two space characters
  1418. - Give the category in a 12 chars wide field, padded with whitespace on
  1419. the right (because of `-'). Append a colon if there is a category
  1420. (because of `:').
  1421. - If there is a time-of-day, put it into a 12 chars wide field. If no
  1422. time, don't put in an empty field, just skip it (because of '?').
  1423. - Finally, put the scheduling information and append a whitespace.
  1424. As another example, if you don't want the time-of-day of entries in
  1425. the prefix, you could use:
  1426. (setq org-agenda-prefix-format \" %-11:c% s\")
  1427. See also the variables `org-agenda-remove-times-when-in-prefix' and
  1428. `org-agenda-remove-tags-when-in-prefix'."
  1429. :type 'string
  1430. :group 'org-agenda-prefix)
  1431. (defvar org-prefix-format-compiled nil
  1432. "The compiled version of the most recently used prefix format.
  1433. Depending on which command was used last, this may be the compiled version
  1434. of `org-agenda-prefix-format' or `org-timeline-prefix-format'.")
  1435. (defcustom org-agenda-remove-times-when-in-prefix t
  1436. "Non-nil means, remove duplicate time specifications in agenda items.
  1437. When the format `org-agenda-prefix-format' contains a `%t' specifier, a
  1438. time-of-day specification in a headline or diary entry is extracted and
  1439. placed into the prefix. If this option is non-nil, the original specification
  1440. \(a timestamp or -range, or just a plain time(range) specification like
  1441. 11:30-4pm) will be removed for agenda display. This makes the agenda less
  1442. cluttered.
  1443. The option can be t or nil. It may also be the symbol `beg', indicating
  1444. that the time should only be removed what it is located at the beginning of
  1445. the headline/diary entry."
  1446. :group 'org-agenda-prefix
  1447. :type '(choice
  1448. (const :tag "Always" t)
  1449. (const :tag "Never" nil)
  1450. (const :tag "When at beginning of entry" beg)))
  1451. (defcustom org-agenda-remove-tags-when-in-prefix nil
  1452. "Non-nil means, remove the tags from the headline copy in the agenda.
  1453. When this is the symbol `prefix', only remove tags when
  1454. `org-agenda-prefix-format' contains a `%T' specifier."
  1455. :group 'org-agenda-prefix
  1456. :type '(choice
  1457. (const :tag "Always" t)
  1458. (const :tag "Never" nil)
  1459. (const :tag "When prefix format contains %T" prefix)))
  1460. (defgroup org-agenda-timeline nil
  1461. "Options concerning the timeline buffer in Org Mode."
  1462. :tag "Org Agenda Timeline"
  1463. :group 'org-agenda)
  1464. (defcustom org-timeline-prefix-format " % s"
  1465. "Like `org-agenda-prefix-format', but for the timeline of a single file."
  1466. :type 'string
  1467. :group 'org-agenda-timeline)
  1468. (defcustom org-select-timeline-window t
  1469. "Non-nil means, after creating a timeline, move cursor into Timeline window.
  1470. When nil, cursor will remain in the current window."
  1471. :group 'org-agenda-timeline
  1472. :type 'boolean)
  1473. (defcustom org-timeline-show-empty-dates 3
  1474. "Non-nil means, `org-timeline' also shows dates without an entry.
  1475. When nil, only the days which actually have entries are shown.
  1476. When t, all days between the first and the last date are shown.
  1477. When an integer, show also empty dates, but if there is a gap of more than
  1478. N days, just insert a special line indicating the size of the gap."
  1479. :group 'org-agenda-timeline
  1480. :type '(choice
  1481. (const :tag "None" nil)
  1482. (const :tag "All" t)
  1483. (number :tag "at most")))
  1484. (defgroup org-export nil
  1485. "Options for exporting org-listings."
  1486. :tag "Org Export"
  1487. :group 'org)
  1488. (defgroup org-export-general nil
  1489. "General options for exporting Org-mode files."
  1490. :tag "Org Export General"
  1491. :group 'org-export)
  1492. (defcustom org-export-publishing-directory "."
  1493. "Path to the location where exported files should be located.
  1494. This path may be relative to the directory where the Org-mode file lives.
  1495. The default is to put them into the same directory as the Org-mode file.
  1496. The variable may also be an alist with export types `:html', `:ascii',
  1497. `:ical', or `:xoxo' and the corresponding directories. If a direcoty path
  1498. is relative, it is interpreted relative to the directory where the exported
  1499. Org-mode files lives."
  1500. :group 'org-export-general
  1501. :type '(choice
  1502. (directory)
  1503. (repeat
  1504. (cons
  1505. (choice :tag "Type"
  1506. (const :html) (const :ascii) (const :ical) (const :xoxo))
  1507. (directory)))))
  1508. (defcustom org-export-language-setup
  1509. '(("en" "Author" "Date" "Table of Contents")
  1510. ("da" "Ophavsmand" "Dato" "Indhold")
  1511. ("de" "Autor" "Datum" "Inhaltsverzeichnis")
  1512. ("es" "Autor" "Fecha" "\xccndice")
  1513. ("fr" "Auteur" "Date" "Table des Mati\xe8res")
  1514. ("it" "Autore" "Data" "Indice")
  1515. ("nl" "Auteur" "Datum" "Inhoudsopgave")
  1516. ("nn" "Forfattar" "Dato" "Innhold") ;; nn = Norsk (nynorsk)
  1517. ("sv" "F\xf6rfattarens" "Datum" "Inneh\xe5ll"))
  1518. "Terms used in export text, translated to different languages.
  1519. Use the variable `org-export-default-language' to set the language,
  1520. or use the +OPTION lines for a per-file setting."
  1521. :group 'org-export-general
  1522. :type '(repeat
  1523. (list
  1524. (string :tag "HTML language tag")
  1525. (string :tag "Author")
  1526. (string :tag "Date")
  1527. (string :tag "Table of Contents"))))
  1528. (defcustom org-export-default-language "en"
  1529. "The default language of HTML export, as a string.
  1530. This should have an association in `org-export-language-setup'."
  1531. :group 'org-export-general
  1532. :type 'string)
  1533. (defcustom org-export-headline-levels 3
  1534. "The last level which is still exported as a headline.
  1535. Inferior levels will produce itemize lists when exported.
  1536. Note that a numeric prefix argument to an exporter function overrides
  1537. this setting.
  1538. This option can also be set with the +OPTIONS line, e.g. \"H:2\"."
  1539. :group 'org-export-general
  1540. :type 'number)
  1541. (defcustom org-export-with-section-numbers t
  1542. "Non-nil means, add section numbers to headlines when exporting.
  1543. This option can also be set with the +OPTIONS line, e.g. \"num:t\"."
  1544. :group 'org-export-general
  1545. :type 'boolean)
  1546. (defcustom org-export-with-toc t
  1547. "Non-nil means, create a table of contents in exported files.
  1548. The TOC contains headlines with levels up to`org-export-headline-levels'.
  1549. Headlines which contain any TODO items will be marked with \"(*)\" in
  1550. ASCII export, and with red color in HTML output.
  1551. In HTML output, the TOC will be clickable.
  1552. This option can also be set with the +OPTIONS line, e.g. \"toc:nil\"."
  1553. :group 'org-export-general
  1554. :type 'boolean)
  1555. (defcustom org-export-mark-todo-in-toc nil
  1556. "Non-nil means, mark TOC lines that contain any open TODO items."
  1557. :group 'org-export-general
  1558. :type 'boolean)
  1559. (defcustom org-export-preserve-breaks nil
  1560. "Non-nil means, preserve all line breaks when exporting.
  1561. Normally, in HTML output paragraphs will be reformatted. In ASCII
  1562. export, line breaks will always be preserved, regardless of this variable.
  1563. This option can also be set with the +OPTIONS line, e.g. \"\\n:t\"."
  1564. :group 'org-export-general
  1565. :type 'boolean)
  1566. (defcustom org-export-with-timestamps t
  1567. "Nil means, do not export time stamps and associated keywords."
  1568. :group 'org-export
  1569. :type 'boolean)
  1570. (defcustom org-export-with-tags t
  1571. "Nil means, do not export tags, just remove them from headlines."
  1572. :group 'org-export-general
  1573. :type 'boolean)
  1574. (defcustom org-export-with-timestamps t
  1575. "Nil means, do not export timestamps and associated keywords."
  1576. :group 'org-export-general
  1577. :type 'boolean)
  1578. (defgroup org-export-translation nil
  1579. "Options for translating special ascii sequences for the export backends."
  1580. :tag "Org Export Translation"
  1581. :group 'org-export)
  1582. (defcustom org-export-with-emphasize t
  1583. "Non-nil means, interpret *word*, /word/, and _word_ as emphasized text.
  1584. If the export target supports emphasizing text, the word will be
  1585. typeset in bold, italic, or underlined, respectively. Works only for
  1586. single words, but you can say: I *really* *mean* *this*.
  1587. Not all export backends support this.
  1588. This option can also be set with the +OPTIONS line, e.g. \"*:nil\"."
  1589. :group 'org-export-translation
  1590. :type 'boolean)
  1591. (defcustom org-export-with-sub-superscripts t
  1592. "Non-nil means, interpret \"_\" and \"^\" for export.
  1593. When this option is turned on, you can use TeX-like syntax for sub- and
  1594. superscripts. Several characters after \"_\" or \"^\" will be
  1595. considered as a single item - so grouping with {} is normally not
  1596. needed. For example, the following things will be parsed as single
  1597. sub- or superscripts.
  1598. 10^24 or 10^tau several digits will be considered 1 item.
  1599. 10^-12 or 10^-tau a leading sign with digits or a word
  1600. x^2-y^3 will be read as x^2 - y^3, because items are
  1601. terminated by almost any nonword/nondigit char.
  1602. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  1603. Still, ambiguity is possible - so when in doubt use {} to enclose the
  1604. sub/superscript.
  1605. Not all export backends support this, but HTML does.
  1606. This option can also be set with the +OPTIONS line, e.g. \"^:nil\"."
  1607. :group 'org-export-translation
  1608. :type 'boolean)
  1609. (defcustom org-export-with-TeX-macros t
  1610. "Non-nil means, interpret simple TeX-like macros when exporting.
  1611. For example, HTML export converts \\alpha to &alpha; and \\AA to &Aring;.
  1612. No only real TeX macros will work here, but the standard HTML entities
  1613. for math can be used as macro names as well. For a list of supported
  1614. names in HTML export, see the constant `org-html-entities'.
  1615. Not all export backends support this.
  1616. This option can also be set with the +OPTIONS line, e.g. \"TeX:nil\"."
  1617. :group 'org-export-translation
  1618. :type 'boolean)
  1619. (defcustom org-export-with-fixed-width t
  1620. "Non-nil means, lines starting with \":\" will be in fixed width font.
  1621. This can be used to have pre-formatted text, fragments of code etc. For
  1622. example:
  1623. : ;; Some Lisp examples
  1624. : (while (defc cnt)
  1625. : (ding))
  1626. will be looking just like this in also HTML. See also the QUOTE keyword.
  1627. Not all export backends support this.
  1628. This option can also be set with the +OPTIONS line, e.g. \"::nil\"."
  1629. :group 'org-export-translation
  1630. :type 'boolean)
  1631. (defcustom org-match-sexp-depth 3
  1632. "Number of stacked braces for sub/superscript matching.
  1633. This has to be set before loading org.el to be effective."
  1634. :group 'org-export-translation
  1635. :type 'integer)
  1636. (defgroup org-export-tables nil
  1637. "Options for exporting tables in Org-mode."
  1638. :tag "Org Export Tables"
  1639. :group 'org-export)
  1640. (defcustom org-export-with-tables t
  1641. "If non-nil, lines starting with \"|\" define a table.
  1642. For example:
  1643. | Name | Address | Birthday |
  1644. |-------------+----------+-----------|
  1645. | Arthur Dent | England | 29.2.2100 |
  1646. Not all export backends support this.
  1647. This option can also be set with the +OPTIONS line, e.g. \"|:nil\"."
  1648. :group 'org-export-tables
  1649. :type 'boolean)
  1650. (defcustom org-export-highlight-first-table-line t
  1651. "Non-nil means, highlight the first table line.
  1652. In HTML export, this means use <th> instead of <td>.
  1653. In tables created with table.el, this applies to the first table line.
  1654. In Org-mode tables, all lines before the first horizontal separator
  1655. line will be formatted with <th> tags."
  1656. :group 'org-export-tables
  1657. :type 'boolean)
  1658. (defcustom org-export-table-remove-special-lines t
  1659. "Remove special lines and marking characters in calculating tables.
  1660. This removes the special marking character column from tables that are set
  1661. up for spreadsheet calculations. It also removes the entire lines
  1662. marked with `!', `_', or `^'. The lines with `$' are kept, because
  1663. the values of constants may be useful to have."
  1664. :group 'org-export-tables
  1665. :type 'boolean)
  1666. (defcustom org-export-prefer-native-exporter-for-tables nil
  1667. "Non-nil means, always export tables created with table.el natively.
  1668. Natively means, use the HTML code generator in table.el.
  1669. When nil, Org-mode's own HTML generator is used when possible (i.e. if
  1670. the table does not use row- or column-spanning). This has the
  1671. advantage, that the automatic HTML conversions for math symbols and
  1672. sub/superscripts can be applied. Org-mode's HTML generator is also
  1673. much faster."
  1674. :group 'org-export-tables
  1675. :type 'boolean)
  1676. (defgroup org-export-ascii nil
  1677. "Options specific for ASCII export of Org-mode files."
  1678. :tag "Org Export ASCII"
  1679. :group 'org-export)
  1680. (defcustom org-export-ascii-underline '(?\$ ?\# ?^ ?\~ ?\= ?\-)
  1681. "Characters for underlining headings in ASCII export.
  1682. In the given sequence, these characters will be used for level 1, 2, ..."
  1683. :group 'org-export-ascii
  1684. :type '(repeat character))
  1685. (defcustom org-export-ascii-bullets '(?* ?+ ?-)
  1686. "Bullet characters for headlines converted to lists in ASCII export.
  1687. The first character is is used for the first lest level generated in this
  1688. way, and so on. If there are more levels than characters given here,
  1689. the list will be repeated.
  1690. Note that plain lists will keep the same bullets as the have in the
  1691. Org-mode file."
  1692. :group 'org-export-ascii
  1693. :type '(repeat character))
  1694. (defcustom org-export-ascii-show-new-buffer t
  1695. "Non-nil means, popup buffer containing the exported ASCII text.
  1696. Otherwise the buffer will just be saved to a file and stay hidden."
  1697. :group 'org-export-ascii
  1698. :type 'boolean)
  1699. (defgroup org-export-xml nil
  1700. "Options specific for XML export of Org-mode files."
  1701. :tag "Org Export XML"
  1702. :group 'org-export)
  1703. (defgroup org-export-html nil
  1704. "Options specific for HTML export of Org-mode files."
  1705. :tag "Org Export HTML"
  1706. :group 'org-export)
  1707. (defcustom org-export-html-style
  1708. "<style type=\"text/css\">
  1709. html {
  1710. font-family: Times, serif;
  1711. font-size: 12pt;
  1712. }
  1713. .title { text-align: center; }
  1714. .todo { color: red; }
  1715. .done { color: green; }
  1716. .timestamp { color: grey }
  1717. .timestamp-kwd { color: CadetBlue }
  1718. .tag { background-color:lightblue; font-weight:normal }
  1719. .target { background-color: lavender; }
  1720. pre {
  1721. border: 1pt solid #AEBDCC;
  1722. background-color: #F3F5F7;
  1723. padding: 5pt;
  1724. font-family: courier, monospace;
  1725. }
  1726. table { border-collapse: collapse; }
  1727. td, th {
  1728. vertical-align: top;
  1729. border: 1pt solid #ADB9CC;
  1730. }
  1731. </style>"
  1732. "The default style specification for exported HTML files.
  1733. Since there are different ways of setting style information, this variable
  1734. needs to contain the full HTML structure to provide a style, including the
  1735. surrounding HTML tags. The style specifications should include definitions
  1736. for new classes todo, done, title, and deadline. For example, legal values
  1737. would be:
  1738. <style type=\"text/css\">
  1739. p { font-weight: normal; color: gray; }
  1740. h1 { color: black; }
  1741. .title { text-align: center; }
  1742. .todo, .deadline { color: red; }
  1743. .done { color: green; }
  1744. </style>
  1745. or, if you want to keep the style in a file,
  1746. <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\">
  1747. As the value of this option simply gets inserted into the HTML <head> header,
  1748. you can \"misuse\" it to add arbitrary text to the header."
  1749. :group 'org-export-html
  1750. :type 'string)
  1751. (defcustom org-export-html-link-org-files-as-html t
  1752. "Non-nil means, make file links to `file.org' point to `file.html'.
  1753. When org-mode is exporting an org-mode file to HTML, links to
  1754. non-html files are directly put into a href tag in HTML.
  1755. However, links to other Org-mode files (recognized by the
  1756. extension `.org.) should become links to the corresponding html
  1757. file, assuming that the linked org-mode file will also be
  1758. converted to HTML.
  1759. When nil, the links still point to the plain `.org' file."
  1760. :group 'org-export-html
  1761. :type 'boolean)
  1762. (defcustom org-export-html-inline-images 'maybe
  1763. "Non-nil means, inline images into exported HTML pages.
  1764. This is done using an <img> tag. When nil, an anchor with href is used to
  1765. link to the image. If this option is `maybe', then images in links with
  1766. an empty description will be inlined, while images with a description will
  1767. be linked only."
  1768. :group 'org-export-html
  1769. :type '(choice (const :tag "Never" nil)
  1770. (const :tag "Always" t)
  1771. (const :tag "When there is no description" maybe)))
  1772. (defcustom org-export-html-expand t
  1773. "Non-nil means, for HTML export, treat @<...> as HTML tag.
  1774. When nil, these tags will be exported as plain text and therefore
  1775. not be interpreted by a browser.
  1776. This option can also be set with the +OPTIONS line, e.g. \"@:nil\"."
  1777. :group 'org-export-html
  1778. :type 'boolean)
  1779. (defcustom org-export-html-table-tag
  1780. "<table border=\"1\" cellspacing=\"0\" cellpadding=\"6\">"
  1781. "The HTML tag used to start a table.
  1782. This must be a <table> tag, but you may change the options like
  1783. borders and spacing."
  1784. :group 'org-export-html
  1785. :type 'string)
  1786. (defcustom org-export-html-with-timestamp nil
  1787. "If non-nil, write `org-export-html-html-helper-timestamp'
  1788. into the exported HTML text. Otherwise, the buffer will just be saved
  1789. to a file."
  1790. :group 'org-export-html
  1791. :type 'boolean)
  1792. (defcustom org-export-html-html-helper-timestamp
  1793. "<br/><br/><hr><p><!-- hhmts start --> <!-- hhmts end --></p>\n"
  1794. "The HTML tag used as timestamp delimiter for HTML-helper-mode."
  1795. :group 'org-export-html
  1796. :type 'string)
  1797. (defcustom org-export-html-show-new-buffer nil
  1798. "Non-nil means, popup buffer containing the exported html text.
  1799. Otherwise, the buffer will just be saved to a file and stay hidden."
  1800. :group 'org-export-html
  1801. :type 'boolean)
  1802. (defgroup org-export-icalendar nil
  1803. "Options specific for iCalendar export of Org-mode files."
  1804. :tag "Org Export iCalendar"
  1805. :group 'org-export)
  1806. (defcustom org-combined-agenda-icalendar-file "~/org.ics"
  1807. "The file name for the iCalendar file covering all agenda files.
  1808. This file is created with the command \\[org-export-icalendar-all-agenda-files].
  1809. The file name should be absolute."
  1810. :group 'org-export-icalendar
  1811. :type 'file)
  1812. (defcustom org-icalendar-include-todo nil
  1813. "Non-nil means, export to iCalendar files should also cover TODO items."
  1814. :group 'org-export-icalendar
  1815. :type 'boolean)
  1816. (defcustom org-icalendar-combined-name "OrgMode"
  1817. "Calendar name for the combined iCalendar representing all agenda files."
  1818. :group 'org-export-icalendar
  1819. :type 'string)
  1820. (defgroup org-font-lock nil
  1821. "Font-lock settings for highlighting in Org-mode."
  1822. :tag "Org Font Lock"
  1823. :group 'org)
  1824. (defcustom org-level-color-stars-only nil
  1825. "Non-nil means fontify only the stars in each headline.
  1826. When nil, the entire headline is fontified.
  1827. Changing it requires restart of `font-lock-mode' to become effective
  1828. also in regions already fontified."
  1829. :group 'org-font-lock
  1830. :type 'boolean)
  1831. (defcustom org-hide-leading-stars nil
  1832. "Non-nil means, hide the first N-1 stars in a headline.
  1833. This works by using the face `org-hide' for these stars. This
  1834. face is white for a light background, and black for a dark
  1835. background. You may have to customize the face `org-hide' to
  1836. make this work.
  1837. Changing it requires restart of `font-lock-mode' to become effective
  1838. also in regions already fontified.
  1839. You may also set this on a per-file basis by adding one of the following
  1840. lines to the buffer:
  1841. #+STARTUP: hidestars
  1842. #+STARTUP: showstars"
  1843. :group 'org-font-lock
  1844. :type 'boolean)
  1845. (defcustom org-fontify-done-headline nil
  1846. "Non-nil means, change the face of a headline if it is marked DONE.
  1847. Normally, only the TODO/DONE keyword indicates the state of a headline.
  1848. When this is non-nil, the headline after the keyword is set to the
  1849. `org-headline-done' as an additional indication."
  1850. :group 'org-font-lock
  1851. :type 'boolean)
  1852. (defcustom org-fontify-emphasized-text t
  1853. "Non-nil means fontify *bold*, /italic/ and _underlined_ text.
  1854. Changing this variable requires a restart of Emacs to take effect."
  1855. :group 'org-font-lock
  1856. :type 'boolean)
  1857. (defgroup org-faces nil
  1858. "Faces in Org-mode."
  1859. :tag "Org Faces"
  1860. :group 'org-font-lock)
  1861. (defun org-compatible-face (specs)
  1862. "Make a compatible face specification.
  1863. XEmacs and Emacs 21 do not know about the `min-colors' attribute.
  1864. For them we convert a (min-colors 8) entry to a `tty' entry and move it
  1865. to the top of the list. The `min-colors' attribute will be removed from
  1866. any other entries, and any resulting duplicates will be removed entirely."
  1867. (if (or (featurep 'xemacs) (< emacs-major-version 22))
  1868. (let (r e a)
  1869. (while (setq e (pop specs))
  1870. (cond
  1871. ((memq (car e) '(t default)) (push e r))
  1872. ((setq a (member '(min-colors 8) (car e)))
  1873. (nconc r (list (cons (cons '(type tty) (delq (car a) (car e)))
  1874. (cdr e)))))
  1875. ((setq a (assq 'min-colors (car e)))
  1876. (setq e (cons (delq a (car e)) (cdr e)))
  1877. (or (assoc (car e) r) (push e r)))
  1878. (t (or (assoc (car e) r) (push e r)))))
  1879. (nreverse r))
  1880. specs))
  1881. (defface org-hide
  1882. '((((background light)) (:foreground "white"))
  1883. (((background dark)) (:foreground "black")))
  1884. "Face used to hide leading stars in headlines.
  1885. The forground color of this face should be equal to the background
  1886. color of the frame."
  1887. :group 'org-faces)
  1888. (defface org-level-1 ;; font-lock-function-name-face
  1889. (org-compatible-face
  1890. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  1891. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  1892. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  1893. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  1894. (((class color) (min-colors 8)) (:foreground "blue" :bold t))
  1895. (t (:bold t))))
  1896. "Face used for level 1 headlines."
  1897. :group 'org-faces)
  1898. (defface org-level-2 ;; font-lock-variable-name-face
  1899. (org-compatible-face
  1900. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  1901. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  1902. (((class color) (min-colors 8) (background light)) (:foreground "yellow"))
  1903. (((class color) (min-colors 8) (background dark)) (:foreground "yellow" :bold t))
  1904. (t (:bold t))))
  1905. "Face used for level 2 headlines."
  1906. :group 'org-faces)
  1907. (defface org-level-3 ;; font-lock-keyword-face
  1908. (org-compatible-face
  1909. '((((class color) (min-colors 88) (background light)) (:foreground "Purple"))
  1910. (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1"))
  1911. (((class color) (min-colors 16) (background light)) (:foreground "Purple"))
  1912. (((class color) (min-colors 16) (background dark)) (:foreground "Cyan"))
  1913. (((class color) (min-colors 8) (background light)) (:foreground "purple" :bold t))
  1914. (((class color) (min-colors 8) (background dark)) (:foreground "cyan" :bold t))
  1915. (t (:bold t))))
  1916. "Face used for level 3 headlines."
  1917. :group 'org-faces)
  1918. (defface org-level-4 ;; font-lock-comment-face
  1919. (org-compatible-face
  1920. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  1921. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  1922. (((class color) (min-colors 16) (background light)) (:foreground "red"))
  1923. (((class color) (min-colors 16) (background dark)) (:foreground "red1"))
  1924. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  1925. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  1926. (t (:bold t))))
  1927. "Face used for level 4 headlines."
  1928. :group 'org-faces)
  1929. (defface org-level-5 ;; font-lock-type-face
  1930. (org-compatible-face
  1931. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  1932. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  1933. (((class color) (min-colors 8)) (:foreground "green"))))
  1934. "Face used for level 5 headlines."
  1935. :group 'org-faces)
  1936. (defface org-level-6 ;; font-lock-constant-face
  1937. (org-compatible-face
  1938. '((((class color) (min-colors 16) (background light)) (:foreground "CadetBlue"))
  1939. (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine"))
  1940. (((class color) (min-colors 8)) (:foreground "magenta"))))
  1941. "Face used for level 6 headlines."
  1942. :group 'org-faces)
  1943. (defface org-level-7 ;; font-lock-builtin-face
  1944. (org-compatible-face
  1945. '((((class color) (min-colors 16) (background light)) (:foreground "Orchid"))
  1946. (((class color) (min-colors 16) (background dark)) (:foreground "LightSteelBlue"))
  1947. (((class color) (min-colors 8)) (:foreground "blue"))))
  1948. "Face used for level 7 headlines."
  1949. :group 'org-faces)
  1950. (defface org-level-8 ;; font-lock-string-face
  1951. (org-compatible-face
  1952. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1953. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1954. (((class color) (min-colors 8)) (:foreground "green"))))
  1955. "Face used for level 8 headlines."
  1956. :group 'org-faces)
  1957. (defface org-special-keyword ;; font-lock-string-face
  1958. (org-compatible-face
  1959. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1960. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1961. (t (:italic t))))
  1962. "Face used for special keywords."
  1963. :group 'org-faces)
  1964. (defface org-warning ;; font-lock-warning-face
  1965. (org-compatible-face
  1966. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  1967. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  1968. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  1969. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  1970. (t (:bold t))))
  1971. "Face for deadlines and TODO keywords."
  1972. :group 'org-faces)
  1973. (defface org-headline-done ;; font-lock-string-face
  1974. (org-compatible-face
  1975. '((((class color) (min-colors 16) (background light)) (:foreground "RosyBrown"))
  1976. (((class color) (min-colors 16) (background dark)) (:foreground "LightSalmon"))
  1977. (((class color) (min-colors 8) (background light)) (:bold nil))))
  1978. "Face used to indicate that a headline is DONE.
  1979. This face is only used if `org-fontify-done-headline' is set."
  1980. :group 'org-faces)
  1981. (defface org-link
  1982. '((((class color) (background light)) (:foreground "Purple" :underline t))
  1983. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  1984. (t (:underline t)))
  1985. "Face for links."
  1986. :group 'org-faces)
  1987. (defface org-date
  1988. '((((class color) (background light)) (:foreground "Purple" :underline t))
  1989. (((class color) (background dark)) (:foreground "Cyan" :underline t))
  1990. (t (:underline t)))
  1991. "Face for links."
  1992. :group 'org-faces)
  1993. (defface org-tag
  1994. '((t (:bold t)))
  1995. "Face for tags."
  1996. :group 'org-faces)
  1997. (defface org-todo ;; font-lock-warning-face
  1998. (org-compatible-face
  1999. '((((class color) (min-colors 16) (background light)) (:foreground "Red1" :bold t))
  2000. (((class color) (min-colors 16) (background dark)) (:foreground "Pink" :bold t))
  2001. (((class color) (min-colors 8) (background light)) (:foreground "red" :bold t))
  2002. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2003. (t (:inverse-video t :bold t))))
  2004. "Face for TODO keywords."
  2005. :group 'org-faces)
  2006. (defface org-done ;; font-lock-type-face
  2007. (org-compatible-face
  2008. '((((class color) (min-colors 16) (background light)) (:foreground "ForestGreen"))
  2009. (((class color) (min-colors 16) (background dark)) (:foreground "PaleGreen"))
  2010. (((class color) (min-colors 8)) (:foreground "green"))
  2011. (t (:bold t))))
  2012. "Face used for DONE."
  2013. :group 'org-faces)
  2014. (defface org-table ;; font-lock-function-name-face
  2015. (org-compatible-face
  2016. '((((class color) (min-colors 88) (background light)) (:foreground "Blue1"))
  2017. (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue"))
  2018. (((class color) (min-colors 16) (background light)) (:foreground "Blue"))
  2019. (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue"))
  2020. (((class color) (min-colors 8) (background light)) (:foreground "blue"))
  2021. (((class color) (min-colors 8) (background dark)))))
  2022. "Face used for tables."
  2023. :group 'org-faces)
  2024. (defface org-formula
  2025. (org-compatible-face
  2026. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2027. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2028. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2029. (((class color) (min-colors 8) (background dark)) (:foreground "red"))
  2030. (t (:bold t :italic t))))
  2031. "Face for formulas."
  2032. :group 'org-faces)
  2033. (defface org-scheduled-today
  2034. (org-compatible-face
  2035. '((((class color) (min-colors 88) (background light)) (:foreground "DarkGreen"))
  2036. (((class color) (min-colors 88) (background dark)) (:foreground "PaleGreen"))
  2037. (((class color) (min-colors 8)) (:foreground "green"))
  2038. (t (:bold t :italic t))))
  2039. "Face for items scheduled for a certain day."
  2040. :group 'org-faces)
  2041. (defface org-scheduled-previously
  2042. (org-compatible-face
  2043. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2044. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2045. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2046. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2047. (t (:bold t))))
  2048. "Face for items scheduled previously, and not yet done."
  2049. :group 'org-faces)
  2050. (defface org-upcoming-deadline
  2051. (org-compatible-face
  2052. '((((class color) (min-colors 88) (background light)) (:foreground "Firebrick"))
  2053. (((class color) (min-colors 88) (background dark)) (:foreground "chocolate1"))
  2054. (((class color) (min-colors 8) (background light)) (:foreground "red"))
  2055. (((class color) (min-colors 8) (background dark)) (:foreground "red" :bold t))
  2056. (t (:bold t))))
  2057. "Face for items scheduled previously, and not yet done."
  2058. :group 'org-faces)
  2059. (defface org-time-grid ;; font-lock-variable-name-face
  2060. (org-compatible-face
  2061. '((((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod"))
  2062. (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod"))
  2063. (((class color) (min-colors 8)) (:foreground "yellow" :weight light))))
  2064. "Face used for time grids."
  2065. :group 'org-faces)
  2066. (defconst org-level-faces
  2067. '(org-level-1 org-level-2 org-level-3 org-level-4
  2068. org-level-5 org-level-6 org-level-7 org-level-8
  2069. ))
  2070. (defconst org-n-levels (length org-level-faces))
  2071. (defconst org-bold-re
  2072. (if (featurep 'xemacs)
  2073. "\\([ ]\\|^\\)\\(\\*\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)\\*\\)\\([ ,.]\\|$\\)"
  2074. "\\([ ]\\|^\\)\\(\\*\\(\\w[[:word:] -_]*?\\w\\)\\*\\)\\([ ,.]\\|$\\)")
  2075. "Regular expression for bold emphasis.")
  2076. (defconst org-italic-re
  2077. (if (featurep 'xemacs)
  2078. "\\([ ]\\|^\\)\\(/\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)/\\)\\([ ,.]\\|$\\)"
  2079. "\\([ ]\\|^\\)\\(/\\(\\w[[:word:] -_]*?\\w\\)/\\)\\([ ,.]\\|$\\)")
  2080. "Regular expression for italic emphasis.")
  2081. (defconst org-underline-re
  2082. (if (featurep 'xemacs)
  2083. "\\([ ]\\|^\\)\\(_\\(\\w[a-zA-Z0-9-_ ]*?\\w\\)_\\)\\([ ,.]\\|$\\)"
  2084. "\\([ ]\\|^\\)\\(_\\(\\w[[:word:] -_]*?\\w\\)_\\)\\([ ,.]\\|$\\)")
  2085. "Regular expression for underline emphasis.")
  2086. ;; Variables for pre-computed regular expressions, all buffer local
  2087. (defvar org-done-string nil
  2088. "The last string in `org-todo-keywords', indicating an item is DONE.")
  2089. (make-variable-buffer-local 'org-done-string)
  2090. (defvar org-todo-regexp nil
  2091. "Matches any of the TODO state keywords.")
  2092. (make-variable-buffer-local 'org-todo-regexp)
  2093. (defvar org-not-done-regexp nil
  2094. "Matches any of the TODO state keywords except the last one.")
  2095. (make-variable-buffer-local 'org-not-done-regexp)
  2096. (defvar org-todo-line-regexp nil
  2097. "Matches a headline and puts TODO state into group 2 if present.")
  2098. (make-variable-buffer-local 'org-todo-line-regexp)
  2099. (defvar org-todo-line-tags-regexp nil
  2100. "Matches a headline and puts TODO state into group 2 if present.
  2101. Also put tags into group 4 if tags are present.")
  2102. (make-variable-buffer-local 'org-todo-line-tags-regexp)
  2103. (defvar org-nl-done-regexp nil
  2104. "Matches newline followed by a headline with the DONE keyword.")
  2105. (make-variable-buffer-local 'org-nl-done-regexp)
  2106. (defvar org-looking-at-done-regexp nil
  2107. "Matches the DONE keyword a point.")
  2108. (make-variable-buffer-local 'org-looking-at-done-regexp)
  2109. (defvar org-todo-kwd-priority-p nil
  2110. "Do TODO items have priorities?")
  2111. (make-variable-buffer-local 'org-todo-kwd-priority-p)
  2112. (defvar org-todo-kwd-max-priority nil
  2113. "Maximum priority of TODO items.")
  2114. (make-variable-buffer-local 'org-todo-kwd-max-priority)
  2115. (defvar org-ds-keyword-length 12
  2116. "Maximum length of the Deadline and SCHEDULED keywords.")
  2117. (make-variable-buffer-local 'org-ds-keyword-length)
  2118. (defvar org-deadline-regexp nil
  2119. "Matches the DEADLINE keyword.")
  2120. (make-variable-buffer-local 'org-deadline-regexp)
  2121. (defvar org-deadline-time-regexp nil
  2122. "Matches the DEADLINE keyword together with a time stamp.")
  2123. (make-variable-buffer-local 'org-deadline-time-regexp)
  2124. (defvar org-deadline-line-regexp nil
  2125. "Matches the DEADLINE keyword and the rest of the line.")
  2126. (make-variable-buffer-local 'org-deadline-line-regexp)
  2127. (defvar org-scheduled-regexp nil
  2128. "Matches the SCHEDULED keyword.")
  2129. (make-variable-buffer-local 'org-scheduled-regexp)
  2130. (defvar org-scheduled-time-regexp nil
  2131. "Matches the SCHEDULED keyword together with a time stamp.")
  2132. (make-variable-buffer-local 'org-scheduled-time-regexp)
  2133. (defvar org-closed-time-regexp nil
  2134. "Matches the CLOSED keyword together with a time stamp.")
  2135. (make-variable-buffer-local 'org-closed-time-regexp)
  2136. (defvar org-keyword-time-regexp nil
  2137. "Matches any of the 3 keywords, together with the time stamp.")
  2138. (make-variable-buffer-local 'org-keyword-time-regexp)
  2139. (defvar org-maybe-keyword-time-regexp nil
  2140. "Matches a timestamp, possibly preceeded by a keyword.")
  2141. (make-variable-buffer-local 'org-keyword-time-regexp)
  2142. (defconst org-rm-props '(invisible t face t keymap t intangible t mouse-face t
  2143. mouse-map t)
  2144. "Properties to remove when a string without properties is wanted.")
  2145. (defsubst org-match-string-no-properties (num &optional string)
  2146. (if (featurep 'xemacs)
  2147. (let ((s (match-string num string)))
  2148. (remove-text-properties 0 (length s) org-rm-props s)
  2149. s)
  2150. (match-string-no-properties num string)))
  2151. (defsubst org-no-properties (s)
  2152. (remove-text-properties 0 (length s) org-rm-props s)
  2153. s)
  2154. (defun org-set-regexps-and-options ()
  2155. "Precompute regular expressions for current buffer."
  2156. (when (eq major-mode 'org-mode)
  2157. (let ((re (org-make-options-regexp
  2158. '("CATEGORY" "SEQ_TODO" "PRI_TODO" "TYP_TODO"
  2159. "STARTUP" "ARCHIVE" "TAGS")))
  2160. (splitre "[ \t]+")
  2161. kwds int key value cat arch tags)
  2162. (save-excursion
  2163. (save-restriction
  2164. (widen)
  2165. (goto-char (point-min))
  2166. (while (re-search-forward re nil t)
  2167. (setq key (match-string 1) value (org-match-string-no-properties 2))
  2168. (cond
  2169. ((equal key "CATEGORY")
  2170. (if (string-match "[ \t]+$" value)
  2171. (setq value (replace-match "" t t value)))
  2172. (setq cat (intern value)))
  2173. ((equal key "SEQ_TODO")
  2174. (setq int 'sequence
  2175. kwds (append kwds (org-split-string value splitre))))
  2176. ((equal key "PRI_TODO")
  2177. (setq int 'priority
  2178. kwds (append kwds (org-split-string value splitre))))
  2179. ((equal key "TYP_TODO")
  2180. (setq int 'type
  2181. kwds (append kwds (org-split-string value splitre))))
  2182. ((equal key "TAGS")
  2183. (setq tags (append tags (org-split-string value splitre))))
  2184. ((equal key "STARTUP")
  2185. (let ((opts (org-split-string value splitre))
  2186. (set '(("fold" org-startup-folded t)
  2187. ("overview" org-startup-folded t)
  2188. ("nofold" org-startup-folded nil)
  2189. ("showall" org-startup-folded nil)
  2190. ("content" org-startup-folded content)
  2191. ("hidestars" org-hide-leading-stars t)
  2192. ("showstars" org-hide-leading-stars nil)
  2193. ("odd" org-odd-levels-only t)
  2194. ("oddeven" org-odd-levels-only nil)
  2195. ("align" org-startup-align-all-tables t)
  2196. ("noalign" org-startup-align-all-tables nil)
  2197. ("logging" org-log-done t)
  2198. ("nologging" org-log-done nil)
  2199. ("dlcheck" org-startup-with-deadline-check t)
  2200. ("nodlcheck" org-startup-with-deadline-check nil)))
  2201. l var val)
  2202. (while (setq l (assoc (pop opts) set))
  2203. (setq var (nth 1 l) val (nth 2 l))
  2204. (set (make-local-variable var) val))))
  2205. ((equal key "ARCHIVE")
  2206. (string-match " *$" value)
  2207. (setq arch (replace-match "" t t value))
  2208. (remove-text-properties 0 (length arch)
  2209. '(face t fontified t) arch)))
  2210. )))
  2211. (and cat (set (make-local-variable 'org-category) cat))
  2212. (and kwds (set (make-local-variable 'org-todo-keywords) kwds))
  2213. (and arch (set (make-local-variable 'org-archive-location) arch))
  2214. (and int (set (make-local-variable 'org-todo-interpretation) int))
  2215. (when tags
  2216. (let (e tg c tgs)
  2217. (while (setq e (pop tags))
  2218. (cond
  2219. ((equal e "{") (push '(:startgroup) tgs))
  2220. ((equal e "}") (push '(:endgroup) tgs))
  2221. ((string-match "^\\([0-9a-zA-Z_@]+\\)(\\(.\\))$" e)
  2222. (push (cons (match-string 1 e)
  2223. (string-to-char (match-string 2 e)))
  2224. tgs))
  2225. (t (push (list e) tgs))))
  2226. (set (make-local-variable 'org-tag-alist) nil)
  2227. (while (setq e (pop tgs))
  2228. (or (and (stringp (car e))
  2229. (assoc (car e) org-tag-alist))
  2230. (push e org-tag-alist))))))
  2231. ;; Compute the regular expressions and other local variables
  2232. (setq org-todo-kwd-priority-p (equal org-todo-interpretation 'priority)
  2233. org-todo-kwd-max-priority (1- (length org-todo-keywords))
  2234. org-ds-keyword-length (+ 2 (max (length org-deadline-string)
  2235. (length org-scheduled-string)))
  2236. org-done-string
  2237. (nth (1- (length org-todo-keywords)) org-todo-keywords)
  2238. org-todo-regexp
  2239. (concat "\\<\\(" (mapconcat 'regexp-quote org-todo-keywords
  2240. "\\|") "\\)\\>")
  2241. org-not-done-regexp
  2242. (concat "\\<\\("
  2243. (mapconcat 'regexp-quote
  2244. (nreverse (cdr (reverse org-todo-keywords)))
  2245. "\\|")
  2246. "\\)\\>")
  2247. org-todo-line-regexp
  2248. (concat "^\\(\\*+\\)[ \t]*\\("
  2249. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2250. "\\)? *\\(.*\\)")
  2251. org-nl-done-regexp
  2252. (concat "[\r\n]\\*+[ \t]+" org-done-string "\\>")
  2253. org-todo-line-tags-regexp
  2254. (concat "^\\(\\*+\\)[ \t]*\\("
  2255. (mapconcat 'regexp-quote org-todo-keywords "\\|")
  2256. "\\)? *\\(.*?\\([ \t]:[a-zA-Z0-9:_@]+:[ \t]*\\)?$\\)")
  2257. org-looking-at-done-regexp (concat "^" org-done-string "\\>")
  2258. org-deadline-regexp (concat "\\<" org-deadline-string)
  2259. org-deadline-time-regexp
  2260. (concat "\\<" org-deadline-string " *<\\([^>]+\\)>")
  2261. org-deadline-line-regexp
  2262. (concat "\\<\\(" org-deadline-string "\\).*")
  2263. org-scheduled-regexp
  2264. (concat "\\<" org-scheduled-string)
  2265. org-scheduled-time-regexp
  2266. (concat "\\<" org-scheduled-string " *<\\([^>]+\\)>")
  2267. org-closed-time-regexp
  2268. (concat "\\<" org-closed-string " *\\[\\([^]]+\\)\\]")
  2269. org-keyword-time-regexp
  2270. (concat "\\<\\(" org-scheduled-string
  2271. "\\|" org-deadline-string
  2272. "\\|" org-closed-string "\\)"
  2273. " *[[<]\\([^]>]+\\)[]>]")
  2274. org-maybe-keyword-time-regexp
  2275. (concat "\\(\\<\\(" org-scheduled-string
  2276. "\\|" org-deadline-string
  2277. "\\|" org-closed-string "\\)\\)?"
  2278. " *\\([[<][0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^]\r\n>]*?[]>]\\)"))
  2279. (org-set-font-lock-defaults)))
  2280. ;; Tell the compiler about dynamically scoped variables,
  2281. ;; and variables from other packages
  2282. (defvar calc-embedded-close-formula) ; defined by the calc package
  2283. (defvar calc-embedded-open-formula) ; defined by the calc package
  2284. (defvar font-lock-unfontify-region-function) ; defined by font-lock.el
  2285. (defvar zmacs-regions) ; XEmacs regions
  2286. (defvar original-date) ; dynamically scoped in calendar
  2287. (defvar org-old-auto-fill-inhibit-regexp) ; local variable used by `orgtbl-mode'
  2288. (defvar orgtbl-mode-menu) ; defined when orgtbl mode get initialized
  2289. (defvar org-html-entities) ; defined later in this file
  2290. (defvar org-goto-start-pos) ; dynamically scoped parameter
  2291. (defvar org-time-was-given) ; dynamically scoped parameter
  2292. (defvar org-ts-what) ; dynamically scoped parameter
  2293. (defvar mark-active) ; Emacs only, not available in XEmacs.
  2294. (defvar timecnt) ; dynamically scoped parameter
  2295. (defvar levels-open) ; dynamically scoped parameter
  2296. (defvar entry) ; dynamically scoped parameter
  2297. (defvar date) ; dynamically scoped parameter
  2298. (defvar description) ; dynamically scoped parameter
  2299. (defvar ans1) ; dynamically scoped parameter
  2300. (defvar ans2) ; dynamically scoped parameter
  2301. (defvar starting-day) ; local variable
  2302. (defvar include-all-loc) ; local variable
  2303. (defvar vm-message-pointer) ; from vm
  2304. (defvar vm-folder-directory) ; from vm
  2305. (defvar wl-summary-buffer-elmo-folder) ; from wanderlust
  2306. (defvar wl-summary-buffer-folder-name) ; from wanderlust
  2307. (defvar gnus-group-name) ; from gnus
  2308. (defvar gnus-article-current) ; from gnus
  2309. (defvar w3m-current-url) ; from w3m
  2310. (defvar mh-progs) ; from MH-E
  2311. (defvar mh-current-folder) ; from MH-E
  2312. (defvar mh-show-folder-buffer) ; from MH-E
  2313. (defvar mh-index-folder) ; from MH-E
  2314. (defvar mh-searcher) ; from MH-E
  2315. (defvar org-selected-point) ; dynamically scoped parameter
  2316. (defvar calendar-mode-map) ; from calendar.el
  2317. (defvar last-arg) ; local variable
  2318. (defvar remember-save-after-remembering) ; from remember.el
  2319. (defvar remember-data-file) ; from remember.el
  2320. (defvar annotation) ; from remember.el, dynamically scoped in `remember-mode'
  2321. (defvar initial) ; from remember.el, dynamically scoped in `remember-mode'
  2322. (defvar orgtbl-mode) ; defined later in this file
  2323. (defvar Info-current-file) ; from info.el
  2324. (defvar Info-current-node) ; from info.el
  2325. ;;; Define the mode
  2326. (defvar org-mode-map
  2327. (if (and (not (keymapp outline-mode-map)) (featurep 'allout))
  2328. (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.")
  2329. (copy-keymap outline-mode-map))
  2330. "Keymap for Org-mode.")
  2331. (defvar org-struct-menu) ; defined later in this file
  2332. (defvar org-org-menu) ; defined later in this file
  2333. (defvar org-tbl-menu) ; defined later in this file
  2334. ;; We use a before-change function to check if a table might need
  2335. ;; an update.
  2336. (defvar org-table-may-need-update t
  2337. "Indicates that a table might need an update.
  2338. This variable is set by `org-before-change-function'.
  2339. `org-table-align' sets it back to nil.")
  2340. (defvar org-mode-hook nil)
  2341. (defvar org-inhibit-startup nil) ; Dynamically-scoped param.
  2342. (defvar org-agenda-keep-modes nil) ; Dynamically-scoped param.
  2343. ;;;###autoload
  2344. (define-derived-mode org-mode outline-mode "Org"
  2345. "Outline-based notes management and organizer, alias
  2346. \"Carsten's outline-mode for keeping track of everything.\"
  2347. Org-mode develops organizational tasks around a NOTES file which
  2348. contains information about projects as plain text. Org-mode is
  2349. implemented on top of outline-mode, which is ideal to keep the content
  2350. of large files well structured. It supports ToDo items, deadlines and
  2351. time stamps, which magically appear in the diary listing of the Emacs
  2352. calendar. Tables are easily created with a built-in table editor.
  2353. Plain text URL-like links connect to websites, emails (VM), Usenet
  2354. messages (Gnus), BBDB entries, and any files related to the project.
  2355. For printing and sharing of notes, an Org-mode file (or a part of it)
  2356. can be exported as a structured ASCII or HTML file.
  2357. The following commands are available:
  2358. \\{org-mode-map}"
  2359. ;; Get rid of Outline menus, they are not needed
  2360. ;; Need to do this here because define-derived-mode sets up
  2361. ;; the keymap so late.
  2362. (if (featurep 'xemacs)
  2363. (if org-noutline-p
  2364. (progn
  2365. (easy-menu-remove outline-mode-menu-heading)
  2366. (easy-menu-remove outline-mode-menu-show)
  2367. (easy-menu-remove outline-mode-menu-hide))
  2368. (delete-menu-item '("Headings"))
  2369. (delete-menu-item '("Show"))
  2370. (delete-menu-item '("Hide"))
  2371. (set-menubar-dirty-flag))
  2372. (define-key org-mode-map [menu-bar headings] 'undefined)
  2373. (define-key org-mode-map [menu-bar hide] 'undefined)
  2374. (define-key org-mode-map [menu-bar show] 'undefined))
  2375. (easy-menu-add org-org-menu)
  2376. (easy-menu-add org-tbl-menu)
  2377. (org-install-agenda-files-menu)
  2378. (if org-descriptive-links (org-add-to-invisibility-spec '(org-link)))
  2379. (org-add-to-invisibility-spec '(org-cwidth))
  2380. (when (featurep 'xemacs)
  2381. (set (make-local-variable 'line-move-ignore-invisible) t))
  2382. (setq outline-regexp "\\*+")
  2383. ;;(setq outline-regexp "\\(?:\\*+\\|[ \t]*\\(?:[-+*]\\|[0-9]+[.)]\\) \\)")
  2384. (setq outline-level 'org-outline-level)
  2385. (when (and org-ellipsis (stringp org-ellipsis))
  2386. (unless org-display-table
  2387. (setq org-display-table (make-display-table)))
  2388. (set-display-table-slot org-display-table
  2389. 4 (string-to-vector org-ellipsis))
  2390. (setq buffer-display-table org-display-table))
  2391. (org-set-regexps-and-options)
  2392. (if org-startup-truncated (setq truncate-lines t))
  2393. (set (make-local-variable 'font-lock-unfontify-region-function)
  2394. 'org-unfontify-region)
  2395. ;; Activate before-change-function
  2396. (set (make-local-variable 'org-table-may-need-update) t)
  2397. (org-add-hook 'before-change-functions 'org-before-change-function nil
  2398. 'local)
  2399. ;; Paragraphs and auto-filling
  2400. (org-set-autofill-regexps)
  2401. (org-update-radio-target-regexp)
  2402. ;; Settings for Calc embedded mode
  2403. (set (make-local-variable 'calc-embedded-open-formula) "|\\|\n")
  2404. (set (make-local-variable 'calc-embedded-close-formula) "|\\|\n")
  2405. (if (and org-insert-mode-line-in-empty-file
  2406. (interactive-p)
  2407. (= (point-min) (point-max)))
  2408. (insert " -*- mode: org -*-\n\n"))
  2409. (unless org-inhibit-startup
  2410. (if org-startup-align-all-tables
  2411. (org-table-map-tables 'org-table-align))
  2412. (if org-startup-with-deadline-check
  2413. (call-interactively 'org-check-deadlines)
  2414. (cond
  2415. ((eq org-startup-folded t)
  2416. (org-cycle '(4)))
  2417. ((eq org-startup-folded 'content)
  2418. (let ((this-command 'org-cycle) (last-command 'org-cycle))
  2419. (org-cycle '(4)) (org-cycle '(4))))))))
  2420. (defsubst org-call-with-arg (command arg)
  2421. "Call COMMAND interactively, but pretend prefix are was ARG."
  2422. (let ((current-prefix-arg arg)) (call-interactively command)))
  2423. (defsubst org-current-line (&optional pos)
  2424. (+ (if (bolp) 1 0) (count-lines (point-min) (or pos (point)))))
  2425. (defun org-current-time ()
  2426. "Current time, possibly rounded to `org-time-stamp-rounding-minutes'."
  2427. (if (> org-time-stamp-rounding-minutes 0)
  2428. (let ((r org-time-stamp-rounding-minutes)
  2429. (time (decode-time)))
  2430. (apply 'encode-time
  2431. (append (list 0 (* r (floor (+ .5 (/ (float (nth 1 time)) r)))))
  2432. (nthcdr 2 time))))
  2433. (current-time)))
  2434. (defun org-add-props (string plist &rest props)
  2435. "Add text properties to entire string, from beginning to end.
  2436. PLIST may be a list of properties, PROPS are individual properties and values
  2437. that will be added to PLIST. Returns the string that was modified."
  2438. (add-text-properties
  2439. 0 (length string) (if props (append plist props) plist) string)
  2440. string)
  2441. (put 'org-add-props 'lisp-indent-function 2)
  2442. ;;; Font-Lock stuff
  2443. (defvar org-mouse-map (make-sparse-keymap))
  2444. (define-key org-mouse-map
  2445. (if (featurep 'xemacs) [button2] [mouse-2]) 'org-open-at-mouse)
  2446. (define-key org-mouse-map
  2447. (if (featurep 'xemacs) [button3] [mouse-3]) 'org-find-file-at-mouse)
  2448. (when org-mouse-1-follows-link
  2449. (define-key org-mouse-map [follow-link] 'mouse-face))
  2450. (when org-tab-follows-link
  2451. (define-key org-mouse-map [(tab)] 'org-open-at-point)
  2452. (define-key org-mouse-map "\C-i" 'org-open-at-point))
  2453. (when org-return-follows-link
  2454. (define-key org-mouse-map [(return)] 'org-open-at-point)
  2455. (define-key org-mouse-map "\C-m" 'org-open-at-point))
  2456. (require 'font-lock)
  2457. (defconst org-non-link-chars "]\t\n\r<>")
  2458. (defconst org-link-types '("https?" "ftp" "mailto" "file" "news" "bbdb" "vm"
  2459. "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  2460. (defconst org-link-re-with-space
  2461. (concat
  2462. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2463. "\\([^" org-non-link-chars " ]"
  2464. "[^" org-non-link-chars "]*"
  2465. "[^" org-non-link-chars " ]\\)>?")
  2466. "Matches a link with spaces, optional angular brackets around it.")
  2467. (defconst org-link-re-with-space2
  2468. (concat
  2469. "<?\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2470. "\\([^" org-non-link-chars " ]"
  2471. "[^]\t\n\r]*"
  2472. "[^" org-non-link-chars " ]\\)>?")
  2473. "Matches a link with spaces, optional angular brackets around it.")
  2474. (defconst org-angle-link-re
  2475. (concat
  2476. "<\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2477. "\\([^" org-non-link-chars " ]"
  2478. "[^" org-non-link-chars "]*"
  2479. "\\)>")
  2480. "Matches link with angular brackets, spaces are allowed.")
  2481. (defconst org-plain-link-re
  2482. (concat
  2483. "\\(" (mapconcat 'identity org-link-types "\\|") "\\):"
  2484. "\\([^]\t\n\r<>,;() ]+\\)")
  2485. "Matches plain link, without spaces.")
  2486. (defconst org-bracket-link-regexp
  2487. "\\[\\[\\([^]]+\\)\\]\\(\\[\\([^]]+\\)\\]\\)?\\]"
  2488. "Matches a link in double brackets.")
  2489. (defconst org-bracket-link-analytic-regexp
  2490. (concat
  2491. "\\[\\["
  2492. "\\(\\(" (mapconcat 'identity org-link-types "\\|") "\\):\\)?"
  2493. "\\([^]]+\\)"
  2494. "\\]"
  2495. "\\(\\[" "\\([^]]+\\)" "\\]\\)?"
  2496. "\\]"))
  2497. ; 1: http:
  2498. ; 2: http
  2499. ; 3: path
  2500. ; 4: [desc]
  2501. ; 5: desc
  2502. (defconst org-ts-lengths
  2503. (cons (length (format-time-string (car org-time-stamp-formats)))
  2504. (length (format-time-string (cdr org-time-stamp-formats))))
  2505. "This holds the lengths of the two different time formats.")
  2506. (defconst org-ts-regexp "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)>"
  2507. "Regular expression for fast time stamp matching.")
  2508. (defconst org-ts-regexp-both "[[<]\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}[^\r\n>]*?\\)[]>]"
  2509. "Regular expression for fast time stamp matching.")
  2510. (defconst org-ts-regexp1 "\\(\\([0-9]\\{4\\}\\)-\\([0-9]\\{2\\}\\)-\\([0-9]\\{2\\}\\)\\([^0-9>\r\n]*\\)\\(\\([0-9]\\{2\\}\\):\\([0-9]\\{2\\}\\)\\)?\\)"
  2511. "Regular expression matching time strings for analysis.")
  2512. (defconst org-ts-regexp2 (concat "<" org-ts-regexp1 ">")
  2513. "Regular expression matching time stamps, with groups.")
  2514. (defconst org-tr-regexp (concat org-ts-regexp "--?-?" org-ts-regexp)
  2515. "Regular expression matching a time stamp range.")
  2516. (defconst org-tsr-regexp (concat org-ts-regexp "\\(--?-?"
  2517. org-ts-regexp "\\)?")
  2518. "Regular expression matching a time stamp or time stamp range.")
  2519. (defun org-activate-plain-links (limit)
  2520. "Run through the buffer and add overlays to links."
  2521. (if (re-search-forward org-plain-link-re limit t)
  2522. (progn
  2523. (add-text-properties (match-beginning 0) (match-end 0)
  2524. (list 'mouse-face 'highlight
  2525. 'keymap org-mouse-map
  2526. ))
  2527. t)))
  2528. (defun org-activate-angle-links (limit)
  2529. "Run through the buffer and add overlays to links."
  2530. (if (re-search-forward org-angle-link-re limit t)
  2531. (progn
  2532. (add-text-properties (match-beginning 0) (match-end 0)
  2533. (list 'mouse-face 'highlight
  2534. 'keymap org-mouse-map
  2535. ))
  2536. t)))
  2537. (defun org-activate-bracket-links (limit)
  2538. "Run through the buffer and add overlays to bracketed links."
  2539. (if (re-search-forward org-bracket-link-regexp limit t)
  2540. (let* ((help (concat "LINK: "
  2541. (org-match-string-no-properties 1)))
  2542. ;; FIXME: above we should remove the escapes.
  2543. ;; but that requires another match, protecting match data,
  2544. ;; a lot of overhead for font-lock.
  2545. (ip (list 'invisible 'org-link 'intangible t 'rear-nonsticky t
  2546. 'keymap org-mouse-map 'mouse-face 'highlight
  2547. 'help-echo help))
  2548. (vp (list 'rear-nonsticky t
  2549. 'keymap org-mouse-map 'mouse-face 'highlight
  2550. 'help-echo help)))
  2551. ;; We need to remove the invisible property here. Table narrowing
  2552. ;; may have made some of this invisible.
  2553. (remove-text-properties (match-beginning 0) (match-end 0)
  2554. '(invisible nil))
  2555. (if (match-end 3)
  2556. (progn
  2557. (add-text-properties (match-beginning 0) (match-beginning 3) ip)
  2558. (add-text-properties (match-beginning 3) (match-end 3) vp)
  2559. (add-text-properties (match-end 3) (match-end 0) ip))
  2560. (add-text-properties (match-beginning 0) (match-beginning 1) ip)
  2561. (add-text-properties (match-beginning 1) (match-end 1) vp)
  2562. (add-text-properties (match-end 1) (match-end 0) ip))
  2563. t)))
  2564. (defun org-activate-dates (limit)
  2565. "Run through the buffer and add overlays to dates."
  2566. (if (re-search-forward org-tsr-regexp limit t)
  2567. (progn
  2568. (add-text-properties (match-beginning 0) (match-end 0)
  2569. (list 'mouse-face 'highlight
  2570. 'keymap org-mouse-map))
  2571. t)))
  2572. (defvar org-target-link-regexp nil
  2573. "Regular expression matching radio targets in plain text.")
  2574. (defvar org-target-regexp "<<\\([^<>\n\r]+\\)>>"
  2575. "Regular expression matching a link target.")
  2576. (defvar org-radio-target-regexp "<<<\\([^<>\n\r]+\\)>>>"
  2577. "Regular expression matching a link target.")
  2578. (defun org-activate-target-links (limit)
  2579. "Run through the buffer and add overlays to target matches."
  2580. (when org-target-link-regexp
  2581. (let ((case-fold-search t))
  2582. (if (re-search-forward org-target-link-regexp limit t)
  2583. (progn
  2584. (add-text-properties (match-beginning 0) (match-end 0)
  2585. (list 'mouse-face 'highlight
  2586. 'keymap org-mouse-map
  2587. 'help-echo "Radio target link"
  2588. 'org-linked-text t))
  2589. t)))))
  2590. (defun org-update-radio-target-regexp ()
  2591. "Find all radio targets in this file and update the regular expression."
  2592. (interactive)
  2593. (when (memq 'radio org-activate-links)
  2594. (setq org-target-link-regexp
  2595. (org-make-target-link-regexp (org-all-targets 'radio)))
  2596. (org-restart-font-lock)))
  2597. (defun org-hide-wide-columns (limit)
  2598. (let (s e)
  2599. (setq s (text-property-any (point) (or limit (point-max))
  2600. 'org-cwidth t))
  2601. (when s
  2602. (setq e (next-single-property-change s 'org-cwidth))
  2603. (add-text-properties s e '(invisible org-cwidth intangible t))
  2604. (goto-char e)
  2605. t)))
  2606. (defun org-restart-font-lock ()
  2607. "Restart font-lock-mode, to force refontification."
  2608. (when (and (boundp 'font-lock-mode) font-lock-mode)
  2609. (font-lock-mode -1)
  2610. (font-lock-mode 1)))
  2611. (defun org-all-targets (&optional radio)
  2612. "Return a list of all targets in this file.
  2613. With optional argument RADIO, only find radio targets."
  2614. (let ((re (if radio org-radio-target-regexp org-target-regexp))
  2615. rtn)
  2616. (save-excursion
  2617. (goto-char (point-min))
  2618. (while (re-search-forward re nil t)
  2619. (add-to-list 'rtn (downcase (org-match-string-no-properties 1))))
  2620. rtn)))
  2621. (defun org-make-target-link-regexp (targets)
  2622. "Make regular expression matching all strings in TARGETS.
  2623. The regular expression finds the targets also if there is a line break
  2624. between words."
  2625. (and targets
  2626. (concat
  2627. "\\<\\("
  2628. (mapconcat
  2629. (lambda (x)
  2630. (while (string-match " +" x)
  2631. (setq x (replace-match "\\s-+" t t x)))
  2632. x)
  2633. targets
  2634. "\\|")
  2635. "\\)\\>")))
  2636. (defvar org-camel-regexp "\\*?\\<[A-Z]+[a-z]+[A-Z][a-zA-Z]*\\>"
  2637. "Matches CamelCase words, possibly with a star before it.")
  2638. (defun org-activate-camels (limit)
  2639. "Run through the buffer and add overlays to dates."
  2640. (if (re-search-forward org-camel-regexp limit t)
  2641. (progn
  2642. (add-text-properties (match-beginning 0) (match-end 0)
  2643. (list 'mouse-face 'highlight
  2644. 'keymap org-mouse-map))
  2645. t)))
  2646. (defun org-activate-tags (limit)
  2647. (if (re-search-forward "[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \r\n]" limit t)
  2648. (progn
  2649. (add-text-properties (match-beginning 1) (match-end 1)
  2650. (list 'mouse-face 'highlight
  2651. 'keymap org-mouse-map))
  2652. t)))
  2653. (defun org-font-lock-level ()
  2654. (save-excursion
  2655. (org-back-to-heading t)
  2656. (- (match-end 0) (match-beginning 0))))
  2657. (defun org-outline-level ()
  2658. (save-excursion
  2659. (looking-at outline-regexp)
  2660. (if (match-beginning 1)
  2661. (+ (org-get-string-indentation (match-string 1)) 1000)
  2662. (- (match-end 0) (match-beginning 0)))))
  2663. (defvar org-font-lock-keywords nil)
  2664. (defun org-set-font-lock-defaults ()
  2665. (let* ((em org-fontify-emphasized-text)
  2666. (lk org-activate-links)
  2667. (org-font-lock-extra-keywords
  2668. ;; Headlines
  2669. (list
  2670. '("^\\(\\**\\)\\(\\*\\)\\(.*\\)" (1 (org-get-level-face 1))
  2671. (2 (org-get-level-face 2)) (3 (org-get-level-face 3)))
  2672. '("^[ \t]*\\(\\(|\\|\\+-[-+]\\).*\\S-\\)"
  2673. (1 'org-table))
  2674. ;; Links
  2675. (if (memq 'angle lk) '(org-activate-angle-links (0 'org-link t)))
  2676. (if (memq 'plain lk) '(org-activate-plain-links (0 'org-link t)))
  2677. (if (memq 'bracket lk) '(org-activate-bracket-links (0 'org-link t)))
  2678. (if (memq 'radio lk) '(org-activate-target-links (0 'org-link t)))
  2679. (if (memq 'date lk) '(org-activate-dates (0 'org-date t)))
  2680. (if (memq 'camel lk) '(org-activate-camels (0 'org-link t)))
  2681. (if (memq 'tag lk) '(org-activate-tags (1 'org-tag prepend)))
  2682. (if org-table-limit-column-width
  2683. '(org-hide-wide-columns (0 nil append)))
  2684. ;; TODO lines
  2685. (list (concat "^\\*+[ \t]*" org-not-done-regexp)
  2686. '(1 'org-todo t))
  2687. ;; Priorities
  2688. (list (concat "\\[#[A-Z]\\]") '(0 'org-special-keyword t))
  2689. ;; Special keywords
  2690. (list (concat "\\<" org-deadline-string) '(0 'org-special-keyword t))
  2691. (list (concat "\\<" org-scheduled-string) '(0 'org-special-keyword t))
  2692. (list (concat "\\<" org-closed-string) '(0 'org-special-keyword t))
  2693. ;; Emphasis
  2694. (if em (list org-bold-re 2 ''bold 'prepend))
  2695. (if em (list org-italic-re 2 ''italic 'prepend))
  2696. (if em (list org-underline-re 2 ''underline 'prepend))
  2697. ;; Checkboxes, similar to Frank Ruell's org-checklet.el
  2698. '("^[ \t]*\\([-+*]\\|[0-9]+[.)]\\) +\\(\\[[ X]\\]\\)"
  2699. 2 'bold prepend)
  2700. ;; COMMENT
  2701. (list (concat "^\\*+[ \t]*\\<\\(" org-comment-string
  2702. "\\|" org-quote-string "\\)\\>")
  2703. '(1 'org-special-keyword t))
  2704. '("^#.*" (0 'font-lock-comment-face t))
  2705. ;; DONE
  2706. (if org-fontify-done-headline
  2707. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\(.*\\)\\>")
  2708. '(1 'org-done t) '(2 'org-headline-done t))
  2709. (list (concat "^[*]+ +\\<\\(" org-done-string "\\)\\>")
  2710. '(1 'org-done t)))
  2711. ;; Table stuff
  2712. '("^[ \t]*\\(:.*\\)" (1 'org-table t))
  2713. '("| *\\(:?=[^|\n]*\\)" (1 'org-formula t))
  2714. '("^[ \t]*| *\\([#!$*_^]\\) *|" (1 'org-formula t))
  2715. (if org-format-transports-properties-p
  2716. '("| *\\(<[0-9]+>\\) *" (1 'org-formula t)))
  2717. )))
  2718. (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
  2719. ;; Now set the full font-lock-keywords
  2720. (set (make-local-variable 'org-font-lock-keywords)
  2721. org-font-lock-extra-keywords)
  2722. (set (make-local-variable 'font-lock-defaults)
  2723. '(org-font-lock-keywords t nil nil backward-paragraph))
  2724. (kill-local-variable 'font-lock-keywords) nil))
  2725. (defvar org-m nil)
  2726. (defvar org-l nil)
  2727. (defvar org-f nil)
  2728. (defun org-get-level-face (n)
  2729. "Get the right face for match N in font-lock matching of healdines."
  2730. (setq org-l (- (match-end 2) (match-beginning 1)))
  2731. (if org-odd-levels-only (setq org-l (1+ (/ org-l 2))))
  2732. ; (setq org-f (nth (1- (% org-l org-n-levels)) org-level-faces))
  2733. (setq org-f (nth (% (1- org-l) org-n-levels) org-level-faces))
  2734. (cond
  2735. ((eq n 1) (if org-hide-leading-stars 'org-hide org-f))
  2736. ((eq n 2) org-f)
  2737. (t (if org-level-color-stars-only nil org-f))))
  2738. (defun org-unfontify-region (beg end &optional maybe_loudly)
  2739. "Remove fontification and activation overlays from links."
  2740. (font-lock-default-unfontify-region beg end)
  2741. (let* ((buffer-undo-list t)
  2742. (inhibit-read-only t) (inhibit-point-motion-hooks t)
  2743. (inhibit-modification-hooks t)
  2744. deactivate-mark buffer-file-name buffer-file-truename)
  2745. (remove-text-properties beg end
  2746. '(mouse-face nil keymap nil org-linked-text nil
  2747. invisible nil intangible nil))))
  2748. ;;; Visibility cycling
  2749. (defvar org-cycle-global-status nil)
  2750. (make-variable-buffer-local 'org-cycle-global-status)
  2751. (defvar org-cycle-subtree-status nil)
  2752. (make-variable-buffer-local 'org-cycle-subtree-status)
  2753. ;;;###autoload
  2754. (defun org-cycle (&optional arg)
  2755. "Visibility cycling for Org-mode.
  2756. - When this function is called with a prefix argument, rotate the entire
  2757. buffer through 3 states (global cycling)
  2758. 1. OVERVIEW: Show only top-level headlines.
  2759. 2. CONTENTS: Show all headlines of all levels, but no body text.
  2760. 3. SHOW ALL: Show everything.
  2761. - When point is at the beginning of a headline, rotate the subtree started
  2762. by this line through 3 different states (local cycling)
  2763. 1. FOLDED: Only the main headline is shown.
  2764. 2. CHILDREN: The main headline and the direct children are shown.
  2765. From this state, you can move to one of the children
  2766. and zoom in further.
  2767. 3. SUBTREE: Show the entire subtree, including body text.
  2768. - When there is a numeric prefix, go up to a heading with level ARG, do
  2769. a `show-subtree' and return to the previous cursor position. If ARG
  2770. is negative, go up that many levels.
  2771. - When point is not at the beginning of a headline, execute
  2772. `indent-relative', like TAB normally does. See the option
  2773. `org-cycle-emulate-tab' for details.
  2774. - Special case: if point is the the beginning of the buffer and there is
  2775. no headline in line 1, this function will act as if called with prefix arg."
  2776. (interactive "P")
  2777. (let* ((outline-regexp
  2778. (if org-cycle-include-plain-lists
  2779. "\\*+\\|\\([ \t]*\\)\\([-+*]\\|[0-9]+[.)]\\) "
  2780. outline-regexp))
  2781. (bob-special (and org-cycle-global-at-bob (bobp)
  2782. (not (looking-at outline-regexp))))
  2783. (org-cycle-hook (if bob-special nil org-cycle-hook))
  2784. (pos (point)))
  2785. (if (or bob-special (equal arg '(4)))
  2786. ;; special case: use global cycling
  2787. (setq arg t))
  2788. (cond
  2789. ((org-at-table-p 'any)
  2790. ;; Enter the table or move to the next field in the table
  2791. (or (org-table-recognize-table.el)
  2792. (progn
  2793. (if arg (org-table-edit-field t)
  2794. (org-table-justify-field-maybe)
  2795. (call-interactively 'org-table-next-field)))))
  2796. ((eq arg t) ;; Global cycling
  2797. (cond
  2798. ((and (eq last-command this-command)
  2799. (eq org-cycle-global-status 'overview))
  2800. ;; We just created the overview - now do table of contents
  2801. ;; This can be slow in very large buffers, so indicate action
  2802. (message "CONTENTS...")
  2803. (org-content)
  2804. (message "CONTENTS...done")
  2805. (setq org-cycle-global-status 'contents)
  2806. (run-hook-with-args 'org-cycle-hook 'contents))
  2807. ((and (eq last-command this-command)
  2808. (eq org-cycle-global-status 'contents))
  2809. ;; We just showed the table of contents - now show everything
  2810. (show-all)
  2811. (message "SHOW ALL")
  2812. (setq org-cycle-global-status 'all)
  2813. (run-hook-with-args 'org-cycle-hook 'all))
  2814. (t
  2815. ;; Default action: go to overview
  2816. (org-overview)
  2817. (message "OVERVIEW")
  2818. (setq org-cycle-global-status 'overview)
  2819. (run-hook-with-args 'org-cycle-hook 'overview))))
  2820. ((integerp arg)
  2821. ;; Show-subtree, ARG levels up from here.
  2822. (save-excursion
  2823. (org-back-to-heading)
  2824. (outline-up-heading (if (< arg 0) (- arg)
  2825. (- (funcall outline-level) arg)))
  2826. (org-show-subtree)))
  2827. ((save-excursion (beginning-of-line 1) (looking-at outline-regexp))
  2828. ;; At a heading: rotate between three different views
  2829. (org-back-to-heading)
  2830. (let ((goal-column 0) eoh eol eos)
  2831. ;; First, some boundaries
  2832. (save-excursion
  2833. (org-back-to-heading)
  2834. (save-excursion
  2835. (beginning-of-line 2)
  2836. (while (and (not (eobp)) ;; this is like `next-line'
  2837. (get-char-property (1- (point)) 'invisible))
  2838. (beginning-of-line 2)) (setq eol (point)))
  2839. (outline-end-of-heading) (setq eoh (point))
  2840. (org-end-of-subtree t) (setq eos (point))
  2841. (outline-next-heading))
  2842. ;; Find out what to do next and set `this-command'
  2843. (cond
  2844. ((and (= eos eoh)
  2845. ;; Nothing is hidden behind this heading
  2846. (message "EMPTY ENTRY")
  2847. (setq org-cycle-subtree-status nil)))
  2848. ((>= eol eos)
  2849. ;; Entire subtree is hidden in one line: open it
  2850. (org-show-entry)
  2851. (show-children)
  2852. (message "CHILDREN")
  2853. (setq org-cycle-subtree-status 'children)
  2854. (run-hook-with-args 'org-cycle-hook 'children))
  2855. ((and (eq last-command this-command)
  2856. (eq org-cycle-subtree-status 'children))
  2857. ;; We just showed the children, now show everything.
  2858. (org-show-subtree)
  2859. (message "SUBTREE")
  2860. (setq org-cycle-subtree-status 'subtree)
  2861. (run-hook-with-args 'org-cycle-hook 'subtree))
  2862. (t
  2863. ;; Default action: hide the subtree.
  2864. (hide-subtree)
  2865. (message "FOLDED")
  2866. (setq org-cycle-subtree-status 'folded)
  2867. (run-hook-with-args 'org-cycle-hook 'folded)))))
  2868. ;; TAB emulation
  2869. (buffer-read-only (org-back-to-heading))
  2870. ((if (and (memq org-cycle-emulate-tab '(white whitestart))
  2871. (save-excursion (beginning-of-line 1) (looking-at "[ \t]*"))
  2872. (or (and (eq org-cycle-emulate-tab 'white)
  2873. (= (match-end 0) (point-at-eol)))
  2874. (and (eq org-cycle-emulate-tab 'whitestart)
  2875. (>= (match-end 0) pos))))
  2876. t
  2877. (eq org-cycle-emulate-tab t))
  2878. (if (and (looking-at "[ \n\r\t]")
  2879. (string-match "^[ \t]*$" (buffer-substring
  2880. (point-at-bol) (point))))
  2881. (progn
  2882. (beginning-of-line 1)
  2883. (and (looking-at "[ \t]+") (replace-match ""))))
  2884. (indent-relative))
  2885. (t (save-excursion
  2886. (org-back-to-heading)
  2887. (org-cycle))))))
  2888. ;;;###autoload
  2889. (defun org-global-cycle ()
  2890. "Cycle the global visibility. For details see `org-cycle'."
  2891. (interactive)
  2892. (org-cycle '(4)))
  2893. (defun org-overview ()
  2894. "Switch to overview mode, shoing only top-level headlines.
  2895. Really, this shows all headlines with level equal or greater than the level
  2896. of the first headline in the buffer. This is important, because if the
  2897. first headline is not level one, then (hide-sublevels 1) gives confusing
  2898. results."
  2899. (interactive)
  2900. (hide-sublevels (save-excursion
  2901. (goto-char (point-min))
  2902. (if (re-search-forward (concat "^" outline-regexp) nil t)
  2903. (progn
  2904. (goto-char (match-beginning 0))
  2905. (funcall outline-level))
  2906. 1))))
  2907. ;; FIXME: allow an argument to give a limiting level for this.
  2908. (defun org-content ()
  2909. "Show all headlines in the buffer, like a table of contents"
  2910. (interactive)
  2911. (save-excursion
  2912. ;; Visit all headings and show their offspring
  2913. (goto-char (point-max))
  2914. (catch 'exit
  2915. (while (and (progn (condition-case nil
  2916. (outline-previous-visible-heading 1)
  2917. (error (goto-char (point-min))))
  2918. t)
  2919. (looking-at outline-regexp))
  2920. (show-branches)
  2921. (if (bobp) (throw 'exit nil))))))
  2922. (defun org-optimize-window-after-visibility-change (state)
  2923. "Adjust the window after a change in outline visibility.
  2924. This function is the default value of the hook `org-cycle-hook'."
  2925. (when (get-buffer-window (current-buffer))
  2926. (cond
  2927. ((eq state 'overview) (org-first-headline-recenter 1))
  2928. ((eq state 'content) nil)
  2929. ((eq state 'all) nil)
  2930. ((eq state 'folded) nil)
  2931. ((eq state 'children) (or (org-subtree-end-visible-p) (recenter 1)))
  2932. ((eq state 'subtree) (or (org-subtree-end-visible-p) (recenter 1))))))
  2933. (defun org-subtree-end-visible-p ()
  2934. "Is the end of the current subtree visible?"
  2935. (pos-visible-in-window-p
  2936. (save-excursion (org-end-of-subtree t) (point))))
  2937. (defun org-first-headline-recenter (&optional N)
  2938. "Move cursor to the first headline and recenter the headline.
  2939. Optional argument N means, put the headline into the Nth line of the window."
  2940. (goto-char (point-min))
  2941. (when (re-search-forward (concat "^" outline-regexp) nil t)
  2942. (beginning-of-line)
  2943. (recenter (prefix-numeric-value N))))
  2944. (defvar org-goto-window-configuration nil)
  2945. (defvar org-goto-marker nil)
  2946. (defvar org-goto-map (make-sparse-keymap))
  2947. (let ((cmds '(isearch-forward isearch-backward)) cmd)
  2948. (while (setq cmd (pop cmds))
  2949. (substitute-key-definition cmd cmd org-goto-map global-map)))
  2950. (define-key org-goto-map "\C-m" 'org-goto-ret)
  2951. (define-key org-goto-map [(left)] 'org-goto-left)
  2952. (define-key org-goto-map [(right)] 'org-goto-right)
  2953. (define-key org-goto-map [(?q)] 'org-goto-quit)
  2954. (define-key org-goto-map [(control ?g)] 'org-goto-quit)
  2955. (define-key org-goto-map "\C-i" 'org-cycle)
  2956. (define-key org-goto-map [(tab)] 'org-cycle)
  2957. (define-key org-goto-map [(down)] 'outline-next-visible-heading)
  2958. (define-key org-goto-map [(up)] 'outline-previous-visible-heading)
  2959. (define-key org-goto-map "n" 'outline-next-visible-heading)
  2960. (define-key org-goto-map "p" 'outline-previous-visible-heading)
  2961. (define-key org-goto-map "f" 'outline-forward-same-level)
  2962. (define-key org-goto-map "b" 'outline-backward-same-level)
  2963. (define-key org-goto-map "u" 'outline-up-heading)
  2964. (define-key org-goto-map "\C-c\C-n" 'outline-next-visible-heading)
  2965. (define-key org-goto-map "\C-c\C-p" 'outline-previous-visible-heading)
  2966. (define-key org-goto-map "\C-c\C-f" 'outline-forward-same-level)
  2967. (define-key org-goto-map "\C-c\C-b" 'outline-backward-same-level)
  2968. (define-key org-goto-map "\C-c\C-u" 'outline-up-heading)
  2969. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  2970. (while l (define-key org-goto-map (int-to-string (pop l)) 'digit-argument)))
  2971. (defconst org-goto-help
  2972. "Select a location to jump to, press RET
  2973. \[Up]/[Down]=next/prev headline TAB=cycle visibility RET=select [Q]uit")
  2974. (defun org-goto ()
  2975. "Go to a different location of the document, keeping current visibility.
  2976. When you want to go to a different location in a document, the fastest way
  2977. is often to fold the entire buffer and then dive into the tree. This
  2978. method has the disadvantage, that the previous location will be folded,
  2979. which may not be what you want.
  2980. This command works around this by showing a copy of the current buffer in
  2981. overview mode. You can dive into the tree in that copy, to find the
  2982. location you want to reach. When pressing RET, the command returns to the
  2983. original buffer in which the visibility is still unchanged. It then jumps
  2984. to the new location, making it and the headline hierarchy above it visible."
  2985. (interactive)
  2986. (let* ((org-goto-start-pos (point))
  2987. (selected-point
  2988. (org-get-location (current-buffer) org-goto-help)))
  2989. (if selected-point
  2990. (progn
  2991. (org-mark-ring-push org-goto-start-pos)
  2992. (goto-char selected-point)
  2993. (if (or (org-invisible-p) (org-invisible-p2))
  2994. (org-show-hierarchy-above)))
  2995. (error "Quit"))))
  2996. (defun org-get-location (buf help)
  2997. "Let the user select a location in the Org-mode buffer BUF.
  2998. This function uses a recursive edit. It returns the selected position
  2999. or nil."
  3000. (let (org-selected-point)
  3001. (save-excursion
  3002. (save-window-excursion
  3003. (delete-other-windows)
  3004. (switch-to-buffer (get-buffer-create "*org-goto*"))
  3005. (with-output-to-temp-buffer "*Help*"
  3006. (princ help))
  3007. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  3008. (setq buffer-read-only nil)
  3009. (erase-buffer)
  3010. (insert-buffer-substring buf)
  3011. (let ((org-startup-truncated t)
  3012. (org-startup-folded t)
  3013. (org-startup-align-all-tables nil)
  3014. (org-startup-with-deadline-check nil))
  3015. (org-mode))
  3016. (setq buffer-read-only t)
  3017. (if (boundp 'org-goto-start-pos)
  3018. (goto-char org-goto-start-pos)
  3019. (goto-char (point-min)))
  3020. (org-beginning-of-line)
  3021. (message "Select location and press RET")
  3022. ;; now we make sure that during selection, ony very few keys work
  3023. ;; and that it is impossible to switch to another window.
  3024. (let ((gm (current-global-map))
  3025. (overriding-local-map org-goto-map))
  3026. (unwind-protect
  3027. (progn
  3028. (use-global-map org-goto-map)
  3029. (recursive-edit))
  3030. (use-global-map gm)))))
  3031. (kill-buffer "*org-goto*")
  3032. org-selected-point))
  3033. (defun org-goto-ret (&optional arg)
  3034. "Finish `org-goto' by going to the new location."
  3035. (interactive "P")
  3036. (setq org-selected-point (point)
  3037. current-prefix-arg arg)
  3038. (throw 'exit nil))
  3039. (defun org-goto-left ()
  3040. "Finish `org-goto' by going to the new location."
  3041. (interactive)
  3042. (if (org-on-heading-p)
  3043. (progn
  3044. (beginning-of-line 1)
  3045. (setq org-selected-point (point)
  3046. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3047. (throw 'exit nil))
  3048. (error "Not on a heading")))
  3049. (defun org-goto-right ()
  3050. "Finish `org-goto' by going to the new location."
  3051. (interactive)
  3052. (if (org-on-heading-p)
  3053. (progn
  3054. (outline-end-of-subtree)
  3055. (or (eobp) (forward-char 1))
  3056. (setq org-selected-point (point)
  3057. current-prefix-arg (- (match-end 0) (match-beginning 0)))
  3058. (throw 'exit nil))
  3059. (error "Not on a heading")))
  3060. (defun org-goto-quit ()
  3061. "Finish `org-goto' without cursor motion."
  3062. (interactive)
  3063. (setq org-selected-point nil)
  3064. (throw 'exit nil))
  3065. ;;; Promotion, Demotion, Inserting new headlines
  3066. (defvar org-ignore-region nil
  3067. "To temporarily disable the active region.")
  3068. (defun org-insert-heading (&optional force-heading)
  3069. "Insert a new heading or item with same depth at point.
  3070. If point is in a plain list and FORCE-HEADING is nil, create a new list item.
  3071. If point is at the beginning of a headline, insert a sibling before the
  3072. current headline. If point is in the middle of a headline, split the headline
  3073. at that position and make the rest of the headline part of the sibling below
  3074. the current headline."
  3075. (interactive "P")
  3076. (if (= (buffer-size) 0)
  3077. (insert "\n* ")
  3078. (when (or force-heading (not (org-insert-item)))
  3079. (let* ((head (save-excursion
  3080. (condition-case nil
  3081. (progn
  3082. (org-back-to-heading)
  3083. (match-string 0))
  3084. (error "*"))))
  3085. pos)
  3086. (cond
  3087. ((and (org-on-heading-p) (bolp)
  3088. (save-excursion (backward-char 1) (not (org-invisible-p))))
  3089. (open-line 1))
  3090. ((bolp) nil)
  3091. (t (newline)))
  3092. (insert head) (just-one-space)
  3093. (setq pos (point))
  3094. (end-of-line 1)
  3095. (unless (= (point) pos) (just-one-space) (backward-delete-char 1))
  3096. (run-hooks 'org-insert-heading-hook)))))
  3097. (defun org-insert-item (&optional checkbox)
  3098. "Insert a new item at the current level.
  3099. Return t when things worked, nil when we are not in an item."
  3100. (when (save-excursion
  3101. (condition-case nil
  3102. (progn
  3103. (org-beginning-of-item)
  3104. (org-at-item-p)
  3105. t)
  3106. (error nil)))
  3107. (let* ((bul (match-string 0))
  3108. (end (match-end 0))
  3109. (eow (save-excursion (beginning-of-line 1) (looking-at "[ \t]*")
  3110. (match-end 0)))
  3111. (eowcol (save-excursion (goto-char eow) (current-column)))
  3112. pos)
  3113. (cond
  3114. ((and (org-at-item-p) (<= (point) eow))
  3115. ;; before the bullet
  3116. (beginning-of-line 1)
  3117. (open-line 1))
  3118. ((<= (point) eow)
  3119. (beginning-of-line 1))
  3120. (t (newline)))
  3121. (insert bul (if checkbox "[ ]" ""))
  3122. (just-one-space)
  3123. (setq pos (point))
  3124. (end-of-line 1)
  3125. (unless (= (point) pos) (just-one-space) (backward-delete-char 1)))
  3126. (org-maybe-renumber-ordered-list)
  3127. t))
  3128. (defun org-insert-todo-heading (arg)
  3129. "Insert a new heading with the same level and TODO state as current heading.
  3130. If the heading has no TODO state, or if the state is DONE, use the first
  3131. state (TODO by default). Also with prefix arg, force first state."
  3132. (interactive "P")
  3133. (when (not (org-insert-item 'checkbox))
  3134. (org-insert-heading)
  3135. (save-excursion
  3136. (org-back-to-heading)
  3137. (if org-noutline-p
  3138. (outline-previous-heading)
  3139. (outline-previous-visible-heading t))
  3140. (looking-at org-todo-line-regexp))
  3141. (if (or arg
  3142. (not (match-beginning 2))
  3143. (equal (match-string 2) org-done-string))
  3144. (insert (car org-todo-keywords) " ")
  3145. (insert (match-string 2) " "))))
  3146. (defun org-promote-subtree ()
  3147. "Promote the entire subtree.
  3148. See also `org-promote'."
  3149. (interactive)
  3150. (save-excursion
  3151. (org-map-tree 'org-promote)))
  3152. (defun org-demote-subtree ()
  3153. "Demote the entire subtree. See `org-demote'.
  3154. See also `org-promote'."
  3155. (interactive)
  3156. (save-excursion
  3157. (org-map-tree 'org-demote)))
  3158. (defun org-do-promote ()
  3159. "Promote the current heading higher up the tree.
  3160. If the region is active in `transient-mark-mode', promote all headings
  3161. in the region."
  3162. (interactive)
  3163. (save-excursion
  3164. (if (org-region-active-p)
  3165. (org-map-region 'org-promote (region-beginning) (region-end))
  3166. (org-promote)))
  3167. (org-fix-position-after-promote))
  3168. (defun org-do-demote ()
  3169. "Demote the current heading lower down the tree.
  3170. If the region is active in `transient-mark-mode', demote all headings
  3171. in the region."
  3172. (interactive)
  3173. (save-excursion
  3174. (if (org-region-active-p)
  3175. (org-map-region 'org-demote (region-beginning) (region-end))
  3176. (org-demote)))
  3177. (org-fix-position-after-promote))
  3178. (defun org-fix-position-after-promote ()
  3179. "Make sure that after pro/demotion cursor position is right."
  3180. (and (equal (char-after) ?\ )
  3181. (equal (char-before) ?*)
  3182. (forward-char 1)))
  3183. (defun org-get-legal-level (level change)
  3184. "Rectify a level change under the influence of `org-odd-levels-only'
  3185. LEVEL is a current level, CHANGE is by how much the level should be
  3186. modified. Even if CHANGE is nil, LEVEL may be returned modified because
  3187. even level numbers will become the next higher odd number."
  3188. (if org-odd-levels-only
  3189. (cond ((not change) (1+ (* 2 (/ level 2))))
  3190. ((> change 0) (1+ (* 2 (/ (+ level (* 2 change)) 2))))
  3191. ((< change 0) (max 1 (1+ (* 2 (/ (+ level (* 2 change)) 2))))))
  3192. (max 1 (+ level change))))
  3193. (defun org-promote ()
  3194. "Promote the current heading higher up the tree.
  3195. If the region is active in `transient-mark-mode', promote all headings
  3196. in the region."
  3197. (org-back-to-heading t)
  3198. (let* ((level (save-match-data (funcall outline-level)))
  3199. (up-head (make-string (org-get-legal-level level -1) ?*))
  3200. (diff (abs (- level (length up-head)))))
  3201. (if (= level 1) (error "Cannot promote to level 0. UNDO to recover"))
  3202. (replace-match up-head nil t)
  3203. ;; Fixup tag positioning
  3204. (and org-auto-align-tags (org-set-tags nil t))
  3205. (if org-adapt-indentation
  3206. (org-fixup-indentation (if (> diff 1) "^ " "^ ") ""
  3207. (if (> diff 1) "^ ? ?\\S-" "^ ?\\S-")))))
  3208. (defun org-demote ()
  3209. "Demote the current heading lower down the tree.
  3210. If the region is active in `transient-mark-mode', demote all headings
  3211. in the region."
  3212. (org-back-to-heading t)
  3213. (let* ((level (save-match-data (funcall outline-level)))
  3214. (down-head (make-string (org-get-legal-level level 1) ?*))
  3215. (diff (abs (- level (length down-head)))))
  3216. (replace-match down-head nil t)
  3217. ;; Fixup tag positioning
  3218. (and org-auto-align-tags (org-set-tags nil t))
  3219. (if org-adapt-indentation
  3220. (org-fixup-indentation "^ " (if (> diff 1) " " " ") "^\\S-"))))
  3221. (defun org-map-tree (fun)
  3222. "Call FUN for every heading underneath the current one."
  3223. (org-back-to-heading)
  3224. (let ((level (funcall outline-level)))
  3225. (save-excursion
  3226. (funcall fun)
  3227. (while (and (progn
  3228. (outline-next-heading)
  3229. (> (funcall outline-level) level))
  3230. (not (eobp)))
  3231. (funcall fun)))))
  3232. (defun org-map-region (fun beg end)
  3233. "Call FUN for every heading between BEG and END."
  3234. (let ((org-ignore-region t))
  3235. (save-excursion
  3236. (setq end (copy-marker end))
  3237. (goto-char beg)
  3238. (if (and (re-search-forward (concat "^" outline-regexp) nil t)
  3239. (< (point) end))
  3240. (funcall fun))
  3241. (while (and (progn
  3242. (outline-next-heading)
  3243. (< (point) end))
  3244. (not (eobp)))
  3245. (funcall fun)))))
  3246. (defun org-fixup-indentation (from to prohibit)
  3247. "Change the indentation in the current entry by re-replacing FROM with TO.
  3248. However, if the regexp PROHIBIT matches at all, don't do anything.
  3249. This is being used to change indentation along with the length of the
  3250. heading marker. But if there are any lines which are not indented, nothing
  3251. is changed at all."
  3252. (save-excursion
  3253. (let ((end (save-excursion (outline-next-heading)
  3254. (point-marker))))
  3255. (unless (save-excursion (re-search-forward prohibit end t))
  3256. (while (re-search-forward from end t)
  3257. (replace-match to)
  3258. (beginning-of-line 2)))
  3259. (move-marker end nil))))
  3260. ;;; Vertical tree motion, cutting and pasting of subtrees
  3261. (defun org-move-subtree-up (&optional arg)
  3262. "Move the current subtree up past ARG headlines of the same level."
  3263. (interactive "p")
  3264. (org-move-subtree-down (- (prefix-numeric-value arg))))
  3265. (defun org-move-subtree-down (&optional arg)
  3266. "Move the current subtree down past ARG headlines of the same level."
  3267. (interactive "p")
  3268. (setq arg (prefix-numeric-value arg))
  3269. (let ((movfunc (if (> arg 0) 'outline-get-next-sibling
  3270. 'outline-get-last-sibling))
  3271. (ins-point (make-marker))
  3272. (cnt (abs arg))
  3273. beg end txt folded)
  3274. ;; Select the tree
  3275. (org-back-to-heading)
  3276. (setq beg (point))
  3277. (save-match-data
  3278. (save-excursion (outline-end-of-heading)
  3279. (setq folded (org-invisible-p)))
  3280. (outline-end-of-subtree))
  3281. (outline-next-heading)
  3282. (setq end (point))
  3283. ;; Find insertion point, with error handling
  3284. (goto-char beg)
  3285. (while (> cnt 0)
  3286. (or (and (funcall movfunc) (looking-at outline-regexp))
  3287. (progn (goto-char beg)
  3288. (error "Cannot move past superior level or buffer limit")))
  3289. (setq cnt (1- cnt)))
  3290. (if (> arg 0)
  3291. ;; Moving forward - still need to move over subtree
  3292. (progn (outline-end-of-subtree)
  3293. (outline-next-heading)
  3294. (if (not (or (looking-at (concat "^" outline-regexp))
  3295. (bolp)))
  3296. (newline))))
  3297. (move-marker ins-point (point))
  3298. (setq txt (buffer-substring beg end))
  3299. (delete-region beg end)
  3300. (insert txt)
  3301. (goto-char ins-point)
  3302. (if folded (hide-subtree))
  3303. (move-marker ins-point nil)))
  3304. (defvar org-subtree-clip ""
  3305. "Clipboard for cut and paste of subtrees.
  3306. This is actually only a copy of the kill, because we use the normal kill
  3307. ring. We need it to check if the kill was created by `org-copy-subtree'.")
  3308. (defvar org-subtree-clip-folded nil
  3309. "Was the last copied subtree folded?
  3310. This is used to fold the tree back after pasting.")
  3311. (defun org-cut-subtree ()
  3312. "Cut the current subtree into the clipboard.
  3313. This is a short-hand for marking the subtree and then cutting it."
  3314. (interactive)
  3315. (org-copy-subtree 'cut))
  3316. (defun org-copy-subtree (&optional cut)
  3317. "Cut the current subtree into the clipboard.
  3318. This is a short-hand for marking the subtree and then copying it.
  3319. If CUT is non nil, actually cut the subtree."
  3320. (interactive)
  3321. (let (beg end folded)
  3322. (org-back-to-heading)
  3323. (setq beg (point))
  3324. (save-match-data
  3325. (save-excursion (outline-end-of-heading)
  3326. (setq folded (org-invisible-p)))
  3327. (outline-end-of-subtree))
  3328. (if (equal (char-after) ?\n) (forward-char 1))
  3329. (setq end (point))
  3330. (goto-char beg)
  3331. (when (> end beg)
  3332. (setq org-subtree-clip-folded folded)
  3333. (if cut (kill-region beg end) (copy-region-as-kill beg end))
  3334. (setq org-subtree-clip (current-kill 0))
  3335. (message "%s: Subtree with %d characters"
  3336. (if cut "Cut" "Copied")
  3337. (length org-subtree-clip)))))
  3338. (defun org-paste-subtree (&optional level tree)
  3339. "Paste the clipboard as a subtree, with modification of headline level.
  3340. The entire subtree is promoted or demoted in order to match a new headline
  3341. level. By default, the new level is derived from the visible headings
  3342. before and after the insertion point, and taken to be the inferior headline
  3343. level of the two. So if the previous visible heading is level 3 and the
  3344. next is level 4 (or vice versa), level 4 will be used for insertion.
  3345. This makes sure that the subtree remains an independent subtree and does
  3346. not swallow low level entries.
  3347. You can also force a different level, either by using a numeric prefix
  3348. argument, or by inserting the heading marker by hand. For example, if the
  3349. cursor is after \"*****\", then the tree will be shifted to level 5.
  3350. If you want to insert the tree as is, just use \\[yank].
  3351. If optional TREE is given, use this text instead of the kill ring."
  3352. (interactive "P")
  3353. (unless (org-kill-is-subtree-p tree)
  3354. (error
  3355. (substitute-command-keys
  3356. "The kill is not a (set of) tree(s) - please use \\[yank] to yank anyway")))
  3357. (let* ((txt (or tree (and kill-ring (current-kill 0))))
  3358. (^re (concat "^\\(" outline-regexp "\\)"))
  3359. (re (concat "\\(" outline-regexp "\\)"))
  3360. (^re_ (concat "\\(" outline-regexp "\\)[ \t]*"))
  3361. (old-level (if (string-match ^re txt)
  3362. (- (match-end 0) (match-beginning 0))
  3363. -1))
  3364. (force-level (cond (level (prefix-numeric-value level))
  3365. ((string-match
  3366. ^re_ (buffer-substring (point-at-bol) (point)))
  3367. (- (match-end 0) (match-beginning 0)))
  3368. (t nil)))
  3369. (previous-level (save-excursion
  3370. (condition-case nil
  3371. (progn
  3372. (outline-previous-visible-heading 1)
  3373. (if (looking-at re)
  3374. (- (match-end 0) (match-beginning 0))
  3375. 1))
  3376. (error 1))))
  3377. (next-level (save-excursion
  3378. (condition-case nil
  3379. (progn
  3380. (outline-next-visible-heading 1)
  3381. (if (looking-at re)
  3382. (- (match-end 0) (match-beginning 0))
  3383. 1))
  3384. (error 1))))
  3385. (new-level (or force-level (max previous-level next-level)))
  3386. (shift (if (or (= old-level -1)
  3387. (= new-level -1)
  3388. (= old-level new-level))
  3389. 0
  3390. (- new-level old-level)))
  3391. (shift1 shift)
  3392. (delta (if (> shift 0) -1 1))
  3393. (func (if (> shift 0) 'org-demote 'org-promote))
  3394. (org-odd-levels-only nil)
  3395. beg end)
  3396. ;; Remove the forces level indicator
  3397. (if force-level
  3398. (delete-region (point-at-bol) (point)))
  3399. ;; Make sure we start at the beginning of an empty line
  3400. (if (not (bolp)) (insert "\n"))
  3401. (if (not (looking-at "[ \t]*$"))
  3402. (progn (insert "\n") (backward-char 1)))
  3403. ;; Paste
  3404. (setq beg (point))
  3405. (if (string-match "[ \t\r\n]+\\'" txt)
  3406. (setq txt (replace-match "\n" t t txt)))
  3407. (insert txt)
  3408. (setq end (point))
  3409. (if (looking-at "[ \t\r\n]+")
  3410. (replace-match "\n"))
  3411. (goto-char beg)
  3412. ;; Shift if necessary
  3413. (if (= shift 0)
  3414. (message "Pasted at level %d, without shift" new-level)
  3415. (save-restriction
  3416. (narrow-to-region beg end)
  3417. (while (not (= shift 0))
  3418. (org-map-region func (point-min) (point-max))
  3419. (setq shift (+ delta shift)))
  3420. (goto-char (point-min))
  3421. (message "Pasted at level %d, with shift by %d levels"
  3422. new-level shift1)))
  3423. (if (and kill-ring
  3424. (eq org-subtree-clip (current-kill 0))
  3425. org-subtree-clip-folded)
  3426. ;; The tree was folded before it was killed/copied
  3427. (hide-subtree))))
  3428. (defun org-kill-is-subtree-p (&optional txt)
  3429. "Check if the current kill is an outline subtree, or a set of trees.
  3430. Returns nil if kill does not start with a headline, or if the first
  3431. headline level is not the largest headline level in the tree.
  3432. So this will actually accept several entries of equal levels as well,
  3433. which is OK for `org-paste-subtree'.
  3434. If optional TXT is given, check this string instead of the current kill."
  3435. (let* ((kill (or txt (and kill-ring (current-kill 0)) ""))
  3436. (start-level (and kill
  3437. (string-match (concat "\\`" outline-regexp) kill)
  3438. (- (match-end 0) (match-beginning 0))))
  3439. (re (concat "^" outline-regexp))
  3440. (start 1))
  3441. (if (not start-level)
  3442. nil ;; does not even start with a heading
  3443. (catch 'exit
  3444. (while (setq start (string-match re kill (1+ start)))
  3445. (if (< (- (match-end 0) (match-beginning 0)) start-level)
  3446. (throw 'exit nil)))
  3447. t))))
  3448. ;;; Plain list items
  3449. (defun org-at-item-p ()
  3450. "Is point in a line starting a hand-formatted item?"
  3451. (let ((llt org-plain-list-ordered-item-terminator))
  3452. (save-excursion
  3453. (goto-char (point-at-bol))
  3454. (looking-at
  3455. (cond
  3456. ((eq llt t) "\\([ \t]*\\([-+]\\|\\([0-9]+[.)]\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3457. ((= llt ?.) "\\([ \t]*\\([-+]\\|\\([0-9]+\\.\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3458. ((= llt ?\)) "\\([ \t]*\\([-+]\\|\\([0-9]+)\\)\\)\\|[ \t]+\\*\\)\\( \\|$\\)")
  3459. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))))))
  3460. (defun org-at-item-checkbox-p ()
  3461. "Is point at a line starting a plain-list item with a checklet?"
  3462. (and (org-at-item-p)
  3463. (save-excursion
  3464. (goto-char (match-end 0))
  3465. (skip-chars-forward " \t")
  3466. (looking-at "\\[[ X]\\]"))))
  3467. (defun org-toggle-checkbox ()
  3468. "Toggle the checkbox in the current line."
  3469. (interactive)
  3470. (save-excursion
  3471. (if (org-at-item-checkbox-p)
  3472. (replace-match (if (equal (match-string 0) "[ ]") "[X]" "[ ]") t t))))
  3473. (defun org-get-indentation (&optional line)
  3474. "Get the indentation of the current line, interpreting tabs.
  3475. When LINE is given, assume it represents a line and compute its indentation."
  3476. (if line
  3477. (if (string-match "^ *" (org-remove-tabs line))
  3478. (match-end 0))
  3479. (save-excursion
  3480. (beginning-of-line 1)
  3481. (skip-chars-forward " \t")
  3482. (current-column))))
  3483. (defun org-remove-tabs (s &optional width)
  3484. "Replace tabulators in S with spaces.
  3485. Assumes that s is a single line, starting in column 0."
  3486. (setq width (or width tab-width))
  3487. (while (string-match "\t" s)
  3488. (setq s (replace-match
  3489. (make-string
  3490. (- (* width (/ (+ (match-beginning 0) width) width))
  3491. (match-beginning 0)) ?\ )
  3492. t t s)))
  3493. s)
  3494. ;; FIXME: document properly.
  3495. (defun org-fix-indentation (line ind)
  3496. "If the current indenation is smaller than ind1, leave it alone.
  3497. If it is larger than ind, reduce it by ind."
  3498. (let* ((l (org-remove-tabs line))
  3499. (i (org-get-indentation l))
  3500. (i1 (car ind)) (i2 (cdr ind)))
  3501. (if (>= i i2) (setq l (substring line i2)))
  3502. (if (> i1 0)
  3503. (concat (make-string i1 ?\ ) l)
  3504. l)))
  3505. (defun org-beginning-of-item ()
  3506. "Go to the beginning of the current hand-formatted item.
  3507. If the cursor is not in an item, throw an error."
  3508. (interactive)
  3509. (let ((pos (point))
  3510. (limit (save-excursion (org-back-to-heading)
  3511. (beginning-of-line 2) (point)))
  3512. ind ind1)
  3513. (if (org-at-item-p)
  3514. (beginning-of-line 1)
  3515. (beginning-of-line 1)
  3516. (skip-chars-forward " \t")
  3517. (setq ind (current-column))
  3518. (if (catch 'exit
  3519. (while t
  3520. (beginning-of-line 0)
  3521. (if (< (point) limit) (throw 'exit nil))
  3522. (unless (looking-at " \t]*$")
  3523. (skip-chars-forward " \t")
  3524. (setq ind1 (current-column))
  3525. (if (< ind1 ind)
  3526. (throw 'exit (org-at-item-p))))))
  3527. nil
  3528. (goto-char pos)
  3529. (error "Not in an item")))))
  3530. (defun org-end-of-item ()
  3531. "Go to the end of the current hand-formatted item.
  3532. If the cursor is not in an item, throw an error."
  3533. (interactive)
  3534. (let ((pos (point))
  3535. (limit (save-excursion (outline-next-heading) (point)))
  3536. (ind (save-excursion
  3537. (org-beginning-of-item)
  3538. (skip-chars-forward " \t")
  3539. (current-column)))
  3540. ind1)
  3541. (if (catch 'exit
  3542. (while t
  3543. (beginning-of-line 2)
  3544. (if (>= (point) limit) (throw 'exit t))
  3545. (unless (looking-at "[ \t]*$")
  3546. (skip-chars-forward " \t")
  3547. (setq ind1 (current-column))
  3548. (if (<= ind1 ind) (throw 'exit t)))))
  3549. (beginning-of-line 1)
  3550. (goto-char pos)
  3551. (error "Not in an item"))))
  3552. (defun org-next-item ()
  3553. "Move to the beginning of the next item in the current plain list.
  3554. Error if not at a plain list, or if this is the last item in the list."
  3555. (interactive)
  3556. (let (beg end ind ind1 (pos (point)) txt)
  3557. (org-beginning-of-item)
  3558. (setq beg (point))
  3559. (setq ind (org-get-indentation))
  3560. (org-end-of-item)
  3561. (setq end (point))
  3562. (setq ind1 (org-get-indentation))
  3563. (unless (and (org-at-item-p) (= ind ind1))
  3564. (goto-char pos)
  3565. (error "On last item"))))
  3566. (defun org-previous-item ()
  3567. "Move to the beginning of the previous item in the current plain list.
  3568. Error if not at a plain list, or if this is the last item in the list."
  3569. (interactive)
  3570. (let (beg end ind ind1 (pos (point)) txt)
  3571. (org-beginning-of-item)
  3572. (setq beg (point))
  3573. (setq ind (org-get-indentation))
  3574. (goto-char beg)
  3575. (catch 'exit
  3576. (while t
  3577. (beginning-of-line 0)
  3578. (if (looking-at "[ \t]*$")
  3579. nil
  3580. (if (<= (setq ind1 (org-get-indentation)) ind)
  3581. (throw 'exit t)))))
  3582. (condition-case nil
  3583. (org-beginning-of-item)
  3584. (error (goto-char pos)
  3585. (error "On first item")))))
  3586. (defun org-move-item-down ()
  3587. "Move the plain list item at point down, i.e. swap with following item.
  3588. Subitems (items with larger indentation) are considered part of the item,
  3589. so this really moves item trees."
  3590. (interactive)
  3591. (let (beg end ind ind1 (pos (point)) txt)
  3592. (org-beginning-of-item)
  3593. (setq beg (point))
  3594. (setq ind (org-get-indentation))
  3595. (org-end-of-item)
  3596. (setq end (point))
  3597. (setq ind1 (org-get-indentation))
  3598. (if (and (org-at-item-p) (= ind ind1))
  3599. (progn
  3600. (org-end-of-item)
  3601. (setq txt (buffer-substring beg end))
  3602. (save-excursion
  3603. (delete-region beg end))
  3604. (setq pos (point))
  3605. (insert txt)
  3606. (goto-char pos)
  3607. (org-maybe-renumber-ordered-list))
  3608. (goto-char pos)
  3609. (error "Cannot move this item further down"))))
  3610. (defun org-move-item-up (arg)
  3611. "Move the plain list item at point up, i.e. swap with previous item.
  3612. Subitems (items with larger indentation) are considered part of the item,
  3613. so this really moves item trees."
  3614. (interactive "p")
  3615. (let (beg end ind ind1 (pos (point)) txt)
  3616. (org-beginning-of-item)
  3617. (setq beg (point))
  3618. (setq ind (org-get-indentation))
  3619. (org-end-of-item)
  3620. (setq end (point))
  3621. (goto-char beg)
  3622. (catch 'exit
  3623. (while t
  3624. (beginning-of-line 0)
  3625. (if (looking-at "[ \t]*$")
  3626. nil
  3627. (if (<= (setq ind1 (org-get-indentation)) ind)
  3628. (throw 'exit t)))))
  3629. (condition-case nil
  3630. (org-beginning-of-item)
  3631. (error (goto-char beg)
  3632. (error "Cannot move this item further up")))
  3633. (setq ind1 (org-get-indentation))
  3634. (if (and (org-at-item-p) (= ind ind1))
  3635. (progn
  3636. (setq txt (buffer-substring beg end))
  3637. (save-excursion
  3638. (delete-region beg end))
  3639. (setq pos (point))
  3640. (insert txt)
  3641. (goto-char pos)
  3642. (org-maybe-renumber-ordered-list))
  3643. (goto-char pos)
  3644. (error "Cannot move this item further up"))))
  3645. (defun org-maybe-renumber-ordered-list ()
  3646. "Renumber the ordered list at point if setup allows it.
  3647. This tests the user option `org-auto-renumber-ordered-lists' before
  3648. doing the renumbering."
  3649. (and org-auto-renumber-ordered-lists
  3650. (org-at-item-p)
  3651. (match-beginning 3)
  3652. (org-renumber-ordered-list 1)))
  3653. (defun org-get-string-indentation (s)
  3654. "What indentation has S due to SPACE and TAB at the beginning of the string?"
  3655. (let ((n -1) (i 0) (w tab-width) c)
  3656. (catch 'exit
  3657. (while (< (setq n (1+ n)) (length s))
  3658. (setq c (aref s n))
  3659. (cond ((= c ?\ ) (setq i (1+ i)))
  3660. ((= c ?\t) (setq i (* (/ (+ w i) w) w)))
  3661. (t (throw 'exit t)))))
  3662. i))
  3663. (defun org-renumber-ordered-list (arg)
  3664. "Renumber an ordered plain list.
  3665. Cursor needs to be in the first line of an item, the line that starts
  3666. with something like \"1.\" or \"2)\"."
  3667. (interactive "p")
  3668. (unless (and (org-at-item-p)
  3669. (match-beginning 3))
  3670. (error "This is not an ordered list"))
  3671. (let ((line (org-current-line))
  3672. (col (current-column))
  3673. (ind (org-get-string-indentation
  3674. (buffer-substring (point-at-bol) (match-beginning 3))))
  3675. ;; (term (substring (match-string 3) -1))
  3676. ind1 (n (1- arg)))
  3677. ;; find where this list begins
  3678. (catch 'exit
  3679. (while t
  3680. (catch 'next
  3681. (beginning-of-line 0)
  3682. (if (looking-at "[ \t]*$") (throw 'next t))
  3683. (skip-chars-forward " \t") (setq ind1 (current-column))
  3684. (if (or (< ind1 ind)
  3685. (and (= ind1 ind)
  3686. (not (org-at-item-p))))
  3687. (throw 'exit t)))))
  3688. ;; Walk forward and replace these numbers
  3689. (catch 'exit
  3690. (while t
  3691. (catch 'next
  3692. (beginning-of-line 2)
  3693. (if (eobp) (throw 'exit nil))
  3694. (if (looking-at "[ \t]*$") (throw 'next nil))
  3695. (skip-chars-forward " \t") (setq ind1 (current-column))
  3696. (if (> ind1 ind) (throw 'next t))
  3697. (if (< ind1 ind) (throw 'exit t))
  3698. (if (not (org-at-item-p)) (throw 'exit nil))
  3699. (if (not (match-beginning 3))
  3700. (error "unordered bullet in ordered list. Press \\[undo] to recover"))
  3701. (delete-region (match-beginning 3) (1- (match-end 3)))
  3702. (goto-char (match-beginning 3))
  3703. (insert (format "%d" (setq n (1+ n)))))))
  3704. (goto-line line)
  3705. (move-to-column col)))
  3706. (defvar org-last-indent-begin-marker (make-marker))
  3707. (defvar org-last-indent-end-marker (make-marker))
  3708. (defun org-outdent-item (arg)
  3709. "Outdent a local list item."
  3710. (interactive "p")
  3711. (org-indent-item (- arg)))
  3712. (defun org-indent-item (arg)
  3713. "Indent a local list item."
  3714. (interactive "p")
  3715. (unless (org-at-item-p)
  3716. (error "Not on an item"))
  3717. (save-excursion
  3718. (let (beg end ind ind1)
  3719. (if (memq last-command '(org-shiftmetaright org-shiftmetaleft))
  3720. (setq beg org-last-indent-begin-marker
  3721. end org-last-indent-end-marker)
  3722. (org-beginning-of-item)
  3723. (setq beg (move-marker org-last-indent-begin-marker (point)))
  3724. (org-end-of-item)
  3725. (setq end (move-marker org-last-indent-end-marker (point))))
  3726. (goto-char beg)
  3727. (skip-chars-forward " \t") (setq ind (current-column))
  3728. (if (< (+ arg ind) 0) (error "Cannot outdent beyond margin"))
  3729. (while (< (point) end)
  3730. (beginning-of-line 1)
  3731. (skip-chars-forward " \t") (setq ind1 (current-column))
  3732. (delete-region (point-at-bol) (point))
  3733. (indent-to-column (+ ind1 arg))
  3734. (beginning-of-line 2)))))
  3735. ;;; Archiving
  3736. (defun org-archive-subtree ()
  3737. "Move the current subtree to the archive.
  3738. The archive can be a certain top-level heading in the current file, or in
  3739. a different file. The tree will be moved to that location, the subtree
  3740. heading be marked DONE, and the current time will be added."
  3741. (interactive)
  3742. ;; Save all relevant TODO keyword-relatex variables
  3743. (let ((tr-org-todo-line-regexp org-todo-line-regexp) ; keep despite compiler
  3744. (tr-org-todo-keywords org-todo-keywords)
  3745. (tr-org-todo-interpretation org-todo-interpretation)
  3746. (tr-org-done-string org-done-string)
  3747. (tr-org-todo-regexp org-todo-regexp)
  3748. (tr-org-todo-line-regexp org-todo-line-regexp)
  3749. (this-buffer (current-buffer))
  3750. file heading buffer level newfile-p)
  3751. (if (string-match "\\(.*\\)::\\(.*\\)" org-archive-location)
  3752. (progn
  3753. (setq file (format (match-string 1 org-archive-location)
  3754. (file-name-nondirectory buffer-file-name))
  3755. heading (match-string 2 org-archive-location)))
  3756. (error "Invalid `org-archive-location'"))
  3757. (if (> (length file) 0)
  3758. (setq newfile-p (not (file-exists-p file))
  3759. buffer (find-file-noselect file))
  3760. (setq buffer (current-buffer)))
  3761. (unless buffer
  3762. (error "Cannot access file \"%s\"" file))
  3763. (if (and (> (length heading) 0)
  3764. (string-match "^\\*+" heading))
  3765. (setq level (match-end 0))
  3766. (setq heading nil level 0))
  3767. (save-excursion
  3768. ;; We first only copy, in case something goes wrong
  3769. ;; we need to protect this-command, to avoid kill-region sets it,
  3770. ;; which would lead to duplication of subtrees
  3771. (let (this-command) (org-copy-subtree))
  3772. (set-buffer buffer)
  3773. ;; Enforce org-mode for the archive buffer
  3774. (if (not (eq major-mode 'org-mode))
  3775. ;; Force the mode for future visits.
  3776. (let ((org-insert-mode-line-in-empty-file t))
  3777. (call-interactively 'org-mode)))
  3778. (when newfile-p
  3779. (goto-char (point-max))
  3780. (insert (format "\nArchived entries from file %s\n\n"
  3781. (buffer-file-name this-buffer))))
  3782. ;; Force the TODO keywords of the original buffer
  3783. (let ((org-todo-line-regexp tr-org-todo-line-regexp)
  3784. (org-todo-keywords tr-org-todo-keywords)
  3785. (org-todo-interpretation tr-org-todo-interpretation)
  3786. (org-done-string tr-org-done-string)
  3787. (org-todo-regexp tr-org-todo-regexp)
  3788. (org-todo-line-regexp tr-org-todo-line-regexp))
  3789. (goto-char (point-min))
  3790. (if heading
  3791. (progn
  3792. (if (re-search-forward
  3793. (concat "\\(^\\|\r\\)"
  3794. (regexp-quote heading) "[ \t]*\\($\\|\r\\)")
  3795. nil t)
  3796. (goto-char (match-end 0))
  3797. ;; Heading not found, just insert it at the end
  3798. (goto-char (point-max))
  3799. (or (bolp) (insert "\n"))
  3800. (insert "\n" heading "\n")
  3801. (end-of-line 0))
  3802. ;; Make the subtree visible
  3803. (show-subtree)
  3804. (org-end-of-subtree t)
  3805. (skip-chars-backward " \t\r\n]")
  3806. (and (looking-at "[ \t\r\n]*")
  3807. (replace-match "\n\n")))
  3808. ;; No specific heading, just go to end of file.
  3809. (goto-char (point-max)) (insert "\n"))
  3810. ;; Paste
  3811. (org-paste-subtree (1+ level))
  3812. ;; Mark the entry as done, i.e. set to last work in org-todo-keywords
  3813. (if org-archive-mark-done
  3814. (org-todo (length org-todo-keywords)))
  3815. ;; Move cursor to right after the TODO keyword
  3816. (when org-archive-stamp-time
  3817. (beginning-of-line 1)
  3818. (looking-at org-todo-line-regexp)
  3819. (goto-char (or (match-end 2) (match-beginning 3)))
  3820. (insert "(" (format-time-string (cdr org-time-stamp-formats)
  3821. (org-current-time))
  3822. ")"))
  3823. ;; Save the buffer, if it is not the same buffer.
  3824. (if (not (eq this-buffer buffer)) (save-buffer))))
  3825. ;; Here we are back in the original buffer. Everything seems to have
  3826. ;; worked. So now cut the tree and finish up.
  3827. (let (this-command) (org-cut-subtree))
  3828. (if (and (not (eobp)) (looking-at "[ \t]*$")) (kill-line))
  3829. (message "Subtree archived %s"
  3830. (if (eq this-buffer buffer)
  3831. (concat "under heading: " heading)
  3832. (concat "in file: " (abbreviate-file-name file))))))
  3833. ;;; Completion
  3834. (defun org-complete (&optional arg)
  3835. "Perform completion on word at point.
  3836. At the beginning of a headline, this completes TODO keywords as given in
  3837. `org-todo-keywords'.
  3838. If the current word is preceded by a backslash, completes the TeX symbols
  3839. that are supported for HTML support.
  3840. If the current word is preceded by \"#+\", completes special words for
  3841. setting file options.
  3842. At all other locations, this simply calls `ispell-complete-word'."
  3843. (interactive "P")
  3844. (catch 'exit
  3845. (let* ((end (point))
  3846. (beg1 (save-excursion
  3847. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3848. (skip-chars-backward "a-zA-Z_@0-9")
  3849. (point)))
  3850. (beg (save-excursion
  3851. (if (equal (char-before (point)) ?\ ) (backward-char 1))
  3852. (skip-chars-backward "a-zA-Z0-9_:$")
  3853. (point)))
  3854. (confirm (lambda (x) (stringp (car x))))
  3855. (camel (equal (char-before beg) ?*))
  3856. (tag (equal (char-before beg1) ?:))
  3857. (texp (equal (char-before beg) ?\\))
  3858. (opt (equal (buffer-substring (max (point-at-bol) (- beg 2))
  3859. beg)
  3860. "#+"))
  3861. (completion-ignore-case opt)
  3862. (type nil)
  3863. (tbl nil)
  3864. (table (cond
  3865. (opt
  3866. (setq type :opt)
  3867. (mapcar (lambda (x)
  3868. (string-match "^#\\+\\(\\([A-Z_]+:?\\).*\\)" x)
  3869. (cons (match-string 2 x) (match-string 1 x)))
  3870. (org-split-string (org-get-current-options) "\n")))
  3871. (texp
  3872. (setq type :tex)
  3873. org-html-entities)
  3874. ((string-match "\\`\\*+[ \t]*\\'"
  3875. (buffer-substring (point-at-bol) beg))
  3876. (setq type :todo)
  3877. (mapcar 'list org-todo-keywords))
  3878. (camel
  3879. (setq type :camel)
  3880. (save-excursion
  3881. (goto-char (point-min))
  3882. (while (re-search-forward org-todo-line-regexp nil t)
  3883. (push (list
  3884. (if org-file-link-context-use-camel-case
  3885. (org-make-org-heading-camel (match-string 3) t)
  3886. (org-make-org-heading-search-string
  3887. (match-string 3) t)))
  3888. tbl)))
  3889. tbl)
  3890. (tag (setq type :tag beg beg1)
  3891. (or org-tag-alist (org-get-buffer-tags)))
  3892. (t (progn (ispell-complete-word arg) (throw 'exit nil)))))
  3893. (pattern (buffer-substring-no-properties beg end))
  3894. (completion (try-completion pattern table confirm)))
  3895. (cond ((eq completion t)
  3896. (if (equal type :opt)
  3897. (insert (substring (cdr (assoc (upcase pattern) table))
  3898. (length pattern)))))
  3899. ((null completion)
  3900. (message "Can't find completion for \"%s\"" pattern)
  3901. (ding))
  3902. ((not (string= pattern completion))
  3903. (delete-region beg end)
  3904. (if (string-match " +$" completion)
  3905. (setq completion (replace-match "" t t completion)))
  3906. (insert completion)
  3907. (if (get-buffer-window "*Completions*")
  3908. (delete-window (get-buffer-window "*Completions*")))
  3909. (if (assoc completion table)
  3910. (if (eq type :todo) (insert " ")
  3911. (if (eq type :tag) (insert ":"))))
  3912. (if (and (equal type :opt) (assoc completion table))
  3913. (message "%s" (substitute-command-keys
  3914. "Press \\[org-complete] again to insert example settings"))))
  3915. (t
  3916. (message "Making completion list...")
  3917. (let ((list (sort (all-completions pattern table confirm)
  3918. 'string<)))
  3919. (with-output-to-temp-buffer "*Completions*"
  3920. (condition-case nil
  3921. ;; Protection needed for XEmacs and emacs 21
  3922. (display-completion-list list pattern)
  3923. (error (display-completion-list list)))))
  3924. (message "Making completion list...%s" "done"))))))
  3925. ;;; Comments, TODO and DEADLINE
  3926. (defun org-toggle-comment ()
  3927. "Change the COMMENT state of an entry."
  3928. (interactive)
  3929. (save-excursion
  3930. (org-back-to-heading)
  3931. (if (looking-at (concat outline-regexp
  3932. "\\( +\\<" org-comment-string "\\>\\)"))
  3933. (replace-match "" t t nil 1)
  3934. (if (looking-at outline-regexp)
  3935. (progn
  3936. (goto-char (match-end 0))
  3937. (insert " " org-comment-string))))))
  3938. (defvar org-last-todo-state-is-todo nil
  3939. "This is non-nil when the last TODO state change led to a TODO state.
  3940. If the last change removed the TODO tag or switched to DONE, then
  3941. this is nil.")
  3942. (defun org-todo (&optional arg)
  3943. "Change the TODO state of an item.
  3944. The state of an item is given by a keyword at the start of the heading,
  3945. like
  3946. *** TODO Write paper
  3947. *** DONE Call mom
  3948. The different keywords are specified in the variable `org-todo-keywords'.
  3949. By default the available states are \"TODO\" and \"DONE\".
  3950. So for this example: when the item starts with TODO, it is changed to DONE.
  3951. When it starts with DONE, the DONE is removed. And when neither TODO nor
  3952. DONE are present, add TODO at the beginning of the heading.
  3953. With prefix arg, use completion to determine the new state. With numeric
  3954. prefix arg, switch to that state."
  3955. (interactive "P")
  3956. (save-excursion
  3957. (org-back-to-heading)
  3958. (if (looking-at outline-regexp) (goto-char (match-end 0)))
  3959. (or (looking-at (concat " +" org-todo-regexp " *"))
  3960. (looking-at " *"))
  3961. (let* ((this (match-string 1))
  3962. (completion-ignore-case t)
  3963. (member (member this org-todo-keywords))
  3964. (tail (cdr member))
  3965. (state (cond
  3966. ((equal arg '(4))
  3967. ;; Read a state with completion
  3968. (completing-read "State: " (mapcar (lambda(x) (list x))
  3969. org-todo-keywords)
  3970. nil t))
  3971. ((eq arg 'right)
  3972. (if this
  3973. (if tail (car tail) nil)
  3974. (car org-todo-keywords)))
  3975. ((eq arg 'left)
  3976. (if (equal member org-todo-keywords)
  3977. nil
  3978. (if this
  3979. (nth (- (length org-todo-keywords) (length tail) 2)
  3980. org-todo-keywords)
  3981. org-done-string)))
  3982. (arg
  3983. ;; user requests a specific state
  3984. (nth (1- (prefix-numeric-value arg))
  3985. org-todo-keywords))
  3986. ((null member) (car org-todo-keywords))
  3987. ((null tail) nil) ;; -> first entry
  3988. ((eq org-todo-interpretation 'sequence)
  3989. (car tail))
  3990. ((memq org-todo-interpretation '(type priority))
  3991. (if (eq this-command last-command)
  3992. (car tail)
  3993. (if (> (length tail) 0) org-done-string nil)))
  3994. (t nil)))
  3995. (next (if state (concat " " state " ") " ")))
  3996. (replace-match next t t)
  3997. (setq org-last-todo-state-is-todo
  3998. (not (equal state org-done-string)))
  3999. (when org-log-done
  4000. (if (equal state org-done-string)
  4001. (org-add-planning-info 'closed (current-time) 'scheduled)
  4002. (if (not this)
  4003. (org-add-planning-info nil nil 'closed))))
  4004. ;; Fixup tag positioning
  4005. (and org-auto-align-tags (org-set-tags nil t))
  4006. (run-hooks 'org-after-todo-state-change-hook)))
  4007. ;; Fixup cursor location if close to the keyword
  4008. (if (and (outline-on-heading-p)
  4009. (not (bolp))
  4010. (save-excursion (beginning-of-line 1)
  4011. (looking-at org-todo-line-regexp))
  4012. (< (point) (+ 2 (or (match-end 2) (match-end 1)))))
  4013. (progn
  4014. (goto-char (or (match-end 2) (match-end 1)))
  4015. (just-one-space))))
  4016. (defun org-log-done (&optional undone)
  4017. "Add a time stamp logging that a TODO entry has been closed.
  4018. When UNDONE is non-nil, remove such a time stamp again."
  4019. (interactive)
  4020. (let (beg end col)
  4021. (save-excursion
  4022. (org-back-to-heading t)
  4023. (setq beg (point))
  4024. (looking-at (concat outline-regexp " *"))
  4025. (goto-char (match-end 0))
  4026. (setq col (current-column))
  4027. (outline-next-heading)
  4028. (setq end (point))
  4029. (goto-char beg)
  4030. (when (re-search-forward (concat
  4031. "[\r\n]\\([ \t]*"
  4032. (regexp-quote org-closed-string)
  4033. " *\\[.*?\\][^\n\r]*[\n\r]?\\)") end t)
  4034. (delete-region (match-beginning 1) (match-end 1)))
  4035. (unless undone
  4036. (org-back-to-heading t)
  4037. (skip-chars-forward "^\n\r")
  4038. (goto-char (min (1+ (point)) (point-max)))
  4039. (when (not (member (char-before) '(?\r ?\n)))
  4040. (insert "\n"))
  4041. (indent-to col)
  4042. (insert org-closed-string " "
  4043. (format-time-string
  4044. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4045. (org-current-time))
  4046. "\n")))))
  4047. (defun org-show-todo-tree (arg)
  4048. "Make a compact tree which shows all headlines marked with TODO.
  4049. The tree will show the lines where the regexp matches, and all higher
  4050. headlines above the match.
  4051. With \\[universal-argument] prefix, also show the DONE entries.
  4052. With a numeric prefix N, construct a sparse tree for the Nth element
  4053. of `org-todo-keywords'."
  4054. (interactive "P")
  4055. (let ((case-fold-search nil)
  4056. (kwd-re
  4057. (cond ((null arg) org-not-done-regexp)
  4058. ((equal arg '(4)) org-todo-regexp)
  4059. ((<= (prefix-numeric-value arg) (length org-todo-keywords))
  4060. (regexp-quote (nth (1- (prefix-numeric-value arg))
  4061. org-todo-keywords)))
  4062. (t (error "Invalid prefix argument: %s" arg)))))
  4063. (message "%d TODO entries found"
  4064. (org-occur (concat "^" outline-regexp " +" kwd-re )))))
  4065. (defun org-deadline ()
  4066. "Insert the DEADLINE: string to make a deadline.
  4067. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4068. to modify it to the correct date."
  4069. (interactive)
  4070. (org-add-planning-info 'deadline nil 'closed))
  4071. (defun org-schedule ()
  4072. "Insert the SCHEDULED: string to schedule a TODO item.
  4073. A timestamp is also inserted - use \\[org-timestamp-up] and \\[org-timestamp-down]
  4074. to modify it to the correct date."
  4075. (interactive)
  4076. (org-add-planning-info 'scheduled nil 'closed))
  4077. (defun org-add-planning-info (what &optional time &rest remove)
  4078. "Insert new timestamp with keyword in the line directly after the headline.
  4079. WHAT indicates what kind of time stamp to add. TIME indicated the time to use.
  4080. If non is given, the user is prompted for a date.
  4081. REMOVE indicates what kind of entries to remove. An old WHAT entry will also
  4082. be removed."
  4083. (interactive)
  4084. (when what (setq time (or time (org-read-date nil 'to-time))))
  4085. (when (and org-insert-labeled-timestamps-at-point
  4086. (member what '(scheduled deadline)))
  4087. (insert
  4088. (if (eq what 'scheduled) org-scheduled-string org-deadline-string)
  4089. " "
  4090. (format-time-string (car org-time-stamp-formats) time))
  4091. (setq what nil))
  4092. (save-excursion
  4093. (let (beg end col list elt (buffer-invisibility-spec nil) ts)
  4094. (org-back-to-heading t)
  4095. (setq beg (point))
  4096. (looking-at (concat outline-regexp "\\( *\\)[^\r\n]*"))
  4097. (goto-char (match-end 1))
  4098. (setq col (current-column))
  4099. (goto-char (1+ (match-end 0)))
  4100. (if (and (not (looking-at outline-regexp))
  4101. (looking-at (concat "[^\r\n]*?" org-keyword-time-regexp
  4102. "[^\r\n]*")))
  4103. (narrow-to-region (match-beginning 0) (match-end 0))
  4104. (insert "\n")
  4105. (backward-char 1)
  4106. (narrow-to-region (point) (point))
  4107. (indent-to-column col))
  4108. ;; Check if we have to remove something.
  4109. (setq list (cons what remove))
  4110. (while list
  4111. (setq elt (pop list))
  4112. (goto-char (point-min))
  4113. (when (or (and (eq elt 'scheduled)
  4114. (re-search-forward org-scheduled-time-regexp nil t))
  4115. (and (eq elt 'deadline)
  4116. (re-search-forward org-deadline-time-regexp nil t))
  4117. (and (eq elt 'closed)
  4118. (re-search-forward org-closed-time-regexp nil t)))
  4119. (replace-match "")
  4120. (if (looking-at " +") (replace-match ""))))
  4121. (goto-char (point-max))
  4122. (when what
  4123. (insert
  4124. (if (not (equal (char-before) ?\ )) " " "")
  4125. (cond ((eq what 'scheduled) org-scheduled-string)
  4126. ((eq what 'deadline) org-deadline-string)
  4127. ((eq what 'closed) org-closed-string))
  4128. " ")
  4129. (insert
  4130. (setq ts
  4131. (format-time-string
  4132. (if (eq what 'closed)
  4133. (concat "[" (substring (cdr org-time-stamp-formats) 1 -1) "]")
  4134. (car org-time-stamp-formats))
  4135. time))))
  4136. (goto-char (point-min))
  4137. (widen)
  4138. (if (looking-at "[ \t]+\r?\n")
  4139. (replace-match ""))
  4140. ts)))
  4141. (defun org-occur (regexp &optional callback)
  4142. "Make a compact tree which shows all matches of REGEXP.
  4143. The tree will show the lines where the regexp matches, and all higher
  4144. headlines above the match. It will also show the heading after the match,
  4145. to make sure editing the matching entry is easy.
  4146. If CALLBACK is non-nil, it is a function which is called to confirm
  4147. that the match should indeed be shown."
  4148. (interactive "sRegexp: ")
  4149. (org-remove-occur-highlights nil nil t)
  4150. (setq regexp (org-check-occur-regexp regexp))
  4151. (let ((cnt 0))
  4152. (save-excursion
  4153. (goto-char (point-min))
  4154. (org-overview)
  4155. (while (re-search-forward regexp nil t)
  4156. (when (or (not callback)
  4157. (save-match-data (funcall callback)))
  4158. (setq cnt (1+ cnt))
  4159. (org-highlight-new-match (match-beginning 0) (match-end 0))
  4160. (org-show-hierarchy-above))))
  4161. (org-add-hook 'before-change-functions 'org-remove-occur-highlights
  4162. nil 'local)
  4163. (run-hooks 'org-occur-hook)
  4164. (if (interactive-p)
  4165. (message "%d match(es) for regexp %s" cnt regexp))
  4166. cnt))
  4167. (defun org-show-hierarchy-above ()
  4168. "Make sure point and the headings hierarchy above is visible."
  4169. (catch 'exit
  4170. (if (org-on-heading-p t)
  4171. (org-flag-heading nil) ; only show the heading
  4172. (and (or (org-invisible-p) (org-invisible-p2))
  4173. (org-show-hidden-entry))) ; show entire entry
  4174. (save-excursion
  4175. (and org-show-following-heading
  4176. (outline-next-heading)
  4177. (org-flag-heading nil))) ; show the next heading
  4178. (when org-show-hierarchy-above
  4179. (save-excursion ; show all higher headings
  4180. (while (and (condition-case nil
  4181. (progn (org-up-heading-all 1) t)
  4182. (error nil))
  4183. (not (bobp)))
  4184. (org-flag-heading nil))))))
  4185. ;; Overlay compatibility functions
  4186. (defun org-make-overlay (beg end &optional buffer)
  4187. (if (featurep 'xemacs)
  4188. (make-extent beg end buffer)
  4189. (make-overlay beg end buffer)))
  4190. (defun org-delete-overlay (ovl)
  4191. (if (featurep 'xemacs) (delete-extent ovl) (delete-overlay ovl)))
  4192. (defun org-detatch-overlay (ovl)
  4193. (if (featurep 'xemacs) (detach-extent ovl) (delete-overlay ovl)))
  4194. (defun org-move-overlay (ovl beg end &optional buffer)
  4195. (if (featurep 'xemacs)
  4196. (set-extent-endpoints ovl beg end buffer)
  4197. (move-overlay ovl beg end buffer)))
  4198. (defun org-overlay-put (ovl prop value)
  4199. (if (featurep 'xemacs)
  4200. (set-extent-property ovl prop value)
  4201. (overlay-put ovl prop value)))
  4202. (defvar org-occur-highlights nil)
  4203. (defun org-highlight-new-match (beg end)
  4204. "Highlight from BEG to END and mark the highlight is an occur headline."
  4205. (let ((ov (org-make-overlay beg end)))
  4206. (org-overlay-put ov 'face 'secondary-selection)
  4207. (push ov org-occur-highlights)))
  4208. (defun org-remove-occur-highlights (&optional beg end noremove)
  4209. "Remove the occur highlights from the buffer.
  4210. BEG and END are ignored. If NOREMOVE is nil, remove this function
  4211. from the `before-change-functions' in the current buffer."
  4212. (interactive)
  4213. (mapc 'org-delete-overlay org-occur-highlights)
  4214. (setq org-occur-highlights nil)
  4215. (unless noremove
  4216. (remove-hook 'before-change-functions
  4217. 'org-remove-occur-highlights 'local)))
  4218. ;;; Priorities
  4219. (defvar org-priority-regexp ".*?\\(\\[#\\([A-Z]\\)\\] ?\\)"
  4220. "Regular expression matching the priority indicator.")
  4221. (defvar org-remove-priority-next-time nil)
  4222. (defun org-priority-up ()
  4223. "Increase the priority of the current item."
  4224. (interactive)
  4225. (org-priority 'up))
  4226. (defun org-priority-down ()
  4227. "Decrease the priority of the current item."
  4228. (interactive)
  4229. (org-priority 'down))
  4230. (defun org-priority (&optional action)
  4231. "Change the priority of an item by ARG.
  4232. ACTION can be set, up, or down."
  4233. (interactive)
  4234. (setq action (or action 'set))
  4235. (let (current new news have remove)
  4236. (save-excursion
  4237. (org-back-to-heading)
  4238. (if (looking-at org-priority-regexp)
  4239. (setq current (string-to-char (match-string 2))
  4240. have t)
  4241. (setq current org-default-priority))
  4242. (cond
  4243. ((eq action 'set)
  4244. (message "Priority A-%c, SPC to remove: " org-lowest-priority)
  4245. (setq new (read-char-exclusive))
  4246. (cond ((equal new ?\ ) (setq remove t))
  4247. ((or (< (upcase new) ?A) (> (upcase new) org-lowest-priority))
  4248. (error "Priority must be between `%c' and `%c'"
  4249. ?A org-lowest-priority))))
  4250. ((eq action 'up)
  4251. (setq new (1- current)))
  4252. ((eq action 'down)
  4253. (setq new (1+ current)))
  4254. (t (error "Invalid action")))
  4255. (setq new (min (max ?A (upcase new)) org-lowest-priority))
  4256. (setq news (format "%c" new))
  4257. (if have
  4258. (if remove
  4259. (replace-match "" t t nil 1)
  4260. (replace-match news t t nil 2))
  4261. (if remove
  4262. (error "No priority cookie found in line")
  4263. (looking-at org-todo-line-regexp)
  4264. (if (match-end 2)
  4265. (progn
  4266. (goto-char (match-end 2))
  4267. (insert " [#" news "]"))
  4268. (goto-char (match-beginning 3))
  4269. (insert "[#" news "] ")))))
  4270. (if remove
  4271. (message "Priority removed")
  4272. (message "Priority of current item set to %s" news))))
  4273. (defun org-get-priority (s)
  4274. "Find priority cookie and return priority."
  4275. (save-match-data
  4276. (if (not (string-match org-priority-regexp s))
  4277. (* 1000 (- org-lowest-priority org-default-priority))
  4278. (* 1000 (- org-lowest-priority
  4279. (string-to-char (match-string 2 s)))))))
  4280. ;;; Timestamps
  4281. (defvar org-last-changed-timestamp nil)
  4282. (defun org-time-stamp (arg)
  4283. "Prompt for a date/time and insert a time stamp.
  4284. If the user specifies a time like HH:MM, or if this command is called
  4285. with a prefix argument, the time stamp will contain date and time.
  4286. Otherwise, only the date will be included. All parts of a date not
  4287. specified by the user will be filled in from the current date/time.
  4288. So if you press just return without typing anything, the time stamp
  4289. will represent the current date/time. If there is already a timestamp
  4290. at the cursor, it will be modified."
  4291. (interactive "P")
  4292. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4293. (car org-time-stamp-formats)))
  4294. (org-time-was-given nil)
  4295. time)
  4296. (cond
  4297. ((and (org-at-timestamp-p)
  4298. (eq last-command 'org-time-stamp)
  4299. (eq this-command 'org-time-stamp))
  4300. (insert "--")
  4301. (setq time (let ((this-command this-command))
  4302. (org-read-date arg 'totime)))
  4303. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4304. (insert (format-time-string fmt time)))
  4305. ((org-at-timestamp-p)
  4306. (setq time (let ((this-command this-command))
  4307. (org-read-date arg 'totime)))
  4308. (and (org-at-timestamp-p) (replace-match
  4309. (setq org-last-changed-timestamp
  4310. (format-time-string fmt time))
  4311. t t))
  4312. (message "Timestamp updated"))
  4313. (t
  4314. (setq time (let ((this-command this-command))
  4315. (org-read-date arg 'totime)))
  4316. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4317. (insert (format-time-string fmt time))))))
  4318. (defun org-time-stamp-inactive (&optional arg)
  4319. "Insert an inactive time stamp.
  4320. An inactive time stamp is enclosed in square brackets instead of angle
  4321. brackets. It is inactive in the sense that it does not trigger agenda entries,
  4322. does not link to the calendar and cannot be changed with the S-cursor keys.
  4323. So these are more for recording a certain time/date."
  4324. (interactive "P")
  4325. (let ((fmt (if arg (cdr org-time-stamp-formats)
  4326. (car org-time-stamp-formats)))
  4327. (org-time-was-given nil)
  4328. time)
  4329. (setq time (org-read-date arg 'totime))
  4330. (if org-time-was-given (setq fmt (cdr org-time-stamp-formats)))
  4331. (setq fmt (concat "[" (substring fmt 1 -1) "]"))
  4332. (insert (format-time-string fmt time))))
  4333. (defvar org-date-ovl (org-make-overlay 1 1))
  4334. (org-overlay-put org-date-ovl 'face 'org-warning)
  4335. (org-detatch-overlay org-date-ovl)
  4336. (defun org-read-date (&optional with-time to-time)
  4337. "Read a date and make things smooth for the user.
  4338. The prompt will suggest to enter an ISO date, but you can also enter anything
  4339. which will at least partially be understood by `parse-time-string'.
  4340. Unrecognized parts of the date will default to the current day, month, year,
  4341. hour and minute. For example,
  4342. 3-2-5 --> 2003-02-05
  4343. feb 15 --> currentyear-02-15
  4344. sep 12 9 --> 2009-09-12
  4345. 12:45 --> today 12:45
  4346. 22 sept 0:34 --> currentyear-09-22 0:34
  4347. 12 --> currentyear-currentmonth-12
  4348. Fri --> nearest Friday (today or later)
  4349. etc.
  4350. The function understands only English month and weekday abbreviations,
  4351. but this can be configured with the variables `parse-time-months' and
  4352. `parse-time-weekdays'.
  4353. While prompting, a calendar is popped up - you can also select the
  4354. date with the mouse (button 1). The calendar shows a period of three
  4355. months. To scroll it to other months, use the keys `>' and `<'.
  4356. If you don't like the calendar, turn it off with
  4357. \(setq org-popup-calendar-for-date-prompt nil)
  4358. With optional argument TO-TIME, the date will immediately be converted
  4359. to an internal time.
  4360. With an optional argument WITH-TIME, the prompt will suggest to also
  4361. insert a time. Note that when WITH-TIME is not set, you can still
  4362. enter a time, and this function will inform the calling routine about
  4363. this change. The calling routine may then choose to change the format
  4364. used to insert the time stamp into the buffer to include the time."
  4365. (require 'parse-time)
  4366. (let* ((org-time-stamp-rounding-minutes
  4367. (if (equal with-time '(16)) 0 org-time-stamp-rounding-minutes))
  4368. (ct (org-current-time))
  4369. (default-time
  4370. ;; Default time is either today, or, when entering a range,
  4371. ;; the range start.
  4372. (if (save-excursion
  4373. (re-search-backward
  4374. (concat org-ts-regexp "--?-?\\=") ; 1-3 minuses
  4375. (- (point) 20) t))
  4376. (apply
  4377. 'encode-time
  4378. (mapcar (lambda(x) (or x 0))
  4379. (parse-time-string (match-string 1))))
  4380. ct))
  4381. (calendar-move-hook nil)
  4382. (view-diary-entries-initially nil)
  4383. (view-calendar-holidays-initially nil)
  4384. (timestr (format-time-string
  4385. (if with-time "%Y-%m-%d %H:%M" "%Y-%m-%d") default-time))
  4386. (prompt (format "YYYY-MM-DD [%s]: " timestr))
  4387. ans ans1 ans2
  4388. second minute hour day month year tl wday wday1)
  4389. (if org-popup-calendar-for-date-prompt
  4390. (save-excursion
  4391. (save-window-excursion
  4392. (calendar)
  4393. (calendar-forward-day (- (time-to-days default-time)
  4394. (calendar-absolute-from-gregorian
  4395. (calendar-current-date))))
  4396. (org-eval-in-calendar nil)
  4397. (let* ((old-map (current-local-map))
  4398. (map (copy-keymap calendar-mode-map))
  4399. (minibuffer-local-map (copy-keymap minibuffer-local-map)))
  4400. (define-key map (kbd "RET") 'org-calendar-select)
  4401. (define-key map (if (featurep 'xemacs) [button1] [mouse-1])
  4402. 'org-calendar-select-mouse)
  4403. (define-key map (if (featurep 'xemacs) [button2] [mouse-2])
  4404. 'org-calendar-select-mouse)
  4405. (define-key minibuffer-local-map [(meta shift left)]
  4406. (lambda () (interactive)
  4407. (org-eval-in-calendar '(calendar-backward-month 1))))
  4408. (define-key minibuffer-local-map [(meta shift right)]
  4409. (lambda () (interactive)
  4410. (org-eval-in-calendar '(calendar-forward-month 1))))
  4411. (define-key minibuffer-local-map [(shift up)]
  4412. (lambda () (interactive)
  4413. (org-eval-in-calendar '(calendar-backward-week 1))))
  4414. (define-key minibuffer-local-map [(shift down)]
  4415. (lambda () (interactive)
  4416. (org-eval-in-calendar '(calendar-forward-week 1))))
  4417. (define-key minibuffer-local-map [(shift left)]
  4418. (lambda () (interactive)
  4419. (org-eval-in-calendar '(calendar-backward-day 1))))
  4420. (define-key minibuffer-local-map [(shift right)]
  4421. (lambda () (interactive)
  4422. (org-eval-in-calendar '(calendar-forward-day 1))))
  4423. (define-key minibuffer-local-map ">"
  4424. (lambda () (interactive)
  4425. (org-eval-in-calendar '(scroll-calendar-left 1))))
  4426. (define-key minibuffer-local-map "<"
  4427. (lambda () (interactive)
  4428. (org-eval-in-calendar '(scroll-calendar-right 1))))
  4429. (unwind-protect
  4430. (progn
  4431. (use-local-map map)
  4432. (setq ans (read-string prompt "" nil nil))
  4433. (if (not (string-match "\\S-" ans)) (setq ans nil))
  4434. (setq ans (or ans1 ans ans2)))
  4435. (use-local-map old-map)))))
  4436. ;; Naked prompt only
  4437. (setq ans (read-string prompt "" nil timestr)))
  4438. (org-detatch-overlay org-date-ovl)
  4439. (if (string-match
  4440. "^ *\\(\\([0-9]+\\)-\\)?\\([0-1]?[0-9]\\)-\\([0-3]?[0-9]\\)\\([^-0-9]\\|$\\)" ans)
  4441. (progn
  4442. (setq year (if (match-end 2)
  4443. (string-to-number (match-string 2 ans))
  4444. (string-to-number (format-time-string "%Y")))
  4445. month (string-to-number (match-string 3 ans))
  4446. day (string-to-number (match-string 4 ans)))
  4447. (if (< year 100) (setq year (+ 2000 year)))
  4448. (setq ans (replace-match (format "%04d-%02d-%02d\\5" year month day)
  4449. t nil ans))))
  4450. (setq tl (parse-time-string ans)
  4451. year (or (nth 5 tl) (string-to-number (format-time-string "%Y" ct)))
  4452. month (or (nth 4 tl) (string-to-number (format-time-string "%m" ct)))
  4453. day (or (nth 3 tl) (string-to-number (format-time-string "%d" ct)))
  4454. hour (or (nth 2 tl) (string-to-number (format-time-string "%H" ct)))
  4455. minute (or (nth 1 tl) (string-to-number (format-time-string "%M" ct)))
  4456. second (or (nth 0 tl) 0)
  4457. wday (nth 6 tl))
  4458. (when (and wday (not (nth 3 tl)))
  4459. ;; Weekday was given, but no day, so pick that day in the week
  4460. ;; on or after the derived date.
  4461. (setq wday1 (nth 6 (decode-time (encode-time 0 0 0 day month year))))
  4462. (unless (equal wday wday1)
  4463. (setq day (+ day (% (- wday wday1 -7) 7)))))
  4464. (if (and (boundp 'org-time-was-given)
  4465. (nth 2 tl))
  4466. (setq org-time-was-given t))
  4467. (if (< year 100) (setq year (+ 2000 year)))
  4468. (if to-time
  4469. (encode-time second minute hour day month year)
  4470. (if (or (nth 1 tl) (nth 2 tl))
  4471. (format "%04d-%02d-%02d %02d:%02d" year month day hour minute)
  4472. (format "%04d-%02d-%02d" year month day)))))
  4473. (defun org-eval-in-calendar (form)
  4474. "Eval FORM in the calendar window and return to current window.
  4475. Also, store the cursor date in variable ans2."
  4476. (let ((sw (selected-window)))
  4477. (select-window (get-buffer-window "*Calendar*"))
  4478. (eval form)
  4479. (when (calendar-cursor-to-date)
  4480. (let* ((date (calendar-cursor-to-date))
  4481. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4482. (setq ans2 (format-time-string "%Y-%m-%d" time))))
  4483. (org-move-overlay org-date-ovl (1- (point)) (1+ (point)) (current-buffer))
  4484. (select-window sw)))
  4485. (defun org-calendar-select ()
  4486. "Return to `org-read-date' with the date currently selected.
  4487. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  4488. (interactive)
  4489. (when (calendar-cursor-to-date)
  4490. (let* ((date (calendar-cursor-to-date))
  4491. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4492. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  4493. (if (active-minibuffer-window) (exit-minibuffer))))
  4494. (defun org-calendar-select-mouse (ev)
  4495. "Return to `org-read-date' with the date currently selected.
  4496. This is used by `org-read-date' in a temporary keymap for the calendar buffer."
  4497. (interactive "e")
  4498. (mouse-set-point ev)
  4499. (when (calendar-cursor-to-date)
  4500. (let* ((date (calendar-cursor-to-date))
  4501. (time (encode-time 0 0 0 (nth 1 date) (nth 0 date) (nth 2 date))))
  4502. (setq ans1 (format-time-string "%Y-%m-%d" time)))
  4503. (if (active-minibuffer-window) (exit-minibuffer))))
  4504. (defun org-check-deadlines (ndays)
  4505. "Check if there are any deadlines due or past due.
  4506. A deadline is considered due if it happens within `org-deadline-warning-days'
  4507. days from today's date. If the deadline appears in an entry marked DONE,
  4508. it is not shown. The prefix arg NDAYS can be used to test that many
  4509. days. If the prefix is a raw \\[universal-argument] prefix, all deadlines are shown."
  4510. (interactive "P")
  4511. (let* ((org-warn-days
  4512. (cond
  4513. ((equal ndays '(4)) 100000)
  4514. (ndays (prefix-numeric-value ndays))
  4515. (t org-deadline-warning-days)))
  4516. (case-fold-search nil)
  4517. (regexp (concat "\\<" org-deadline-string " *<\\([^>]+\\)>"))
  4518. (callback
  4519. (lambda ()
  4520. (and (let ((d1 (time-to-days (current-time)))
  4521. (d2 (time-to-days
  4522. (org-time-string-to-time (match-string 1)))))
  4523. (< (- d2 d1) org-warn-days))
  4524. (not (org-entry-is-done-p))))))
  4525. (message "%d deadlines past-due or due within %d days"
  4526. (org-occur regexp callback)
  4527. org-warn-days)))
  4528. (defun org-evaluate-time-range (&optional to-buffer)
  4529. "Evaluate a time range by computing the difference between start and end.
  4530. Normally the result is just printed in the echo area, but with prefix arg
  4531. TO-BUFFER, the result is inserted just after the date stamp into the buffer.
  4532. If the time range is actually in a table, the result is inserted into the
  4533. next column.
  4534. For time difference computation, a year is assumed to be exactly 365
  4535. days in order to avoid rounding problems."
  4536. (interactive "P")
  4537. (save-excursion
  4538. (unless (org-at-date-range-p)
  4539. (goto-char (point-at-bol))
  4540. (re-search-forward org-tr-regexp (point-at-eol) t))
  4541. (if (not (org-at-date-range-p))
  4542. (error "Not at a time-stamp range, and none found in current line")))
  4543. (let* ((ts1 (match-string 1))
  4544. (ts2 (match-string 2))
  4545. (havetime (or (> (length ts1) 15) (> (length ts2) 15)))
  4546. (match-end (match-end 0))
  4547. (time1 (org-time-string-to-time ts1))
  4548. (time2 (org-time-string-to-time ts2))
  4549. (t1 (time-to-seconds time1))
  4550. (t2 (time-to-seconds time2))
  4551. (diff (abs (- t2 t1)))
  4552. (negative (< (- t2 t1) 0))
  4553. ;; (ys (floor (* 365 24 60 60)))
  4554. (ds (* 24 60 60))
  4555. (hs (* 60 60))
  4556. (fy "%dy %dd %02d:%02d")
  4557. (fy1 "%dy %dd")
  4558. (fd "%dd %02d:%02d")
  4559. (fd1 "%dd")
  4560. (fh "%02d:%02d")
  4561. y d h m align)
  4562. (if havetime
  4563. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4564. y 0
  4565. d (floor (/ diff ds)) diff (mod diff ds)
  4566. h (floor (/ diff hs)) diff (mod diff hs)
  4567. m (floor (/ diff 60)))
  4568. (setq ; y (floor (/ diff ys)) diff (mod diff ys)
  4569. y 0
  4570. d (floor (+ (/ diff ds) 0.5))
  4571. h 0 m 0))
  4572. (if (not to-buffer)
  4573. (message (org-make-tdiff-string y d h m))
  4574. (when (org-at-table-p)
  4575. (goto-char match-end)
  4576. (setq align t)
  4577. (and (looking-at " *|") (goto-char (match-end 0))))
  4578. (if (looking-at
  4579. "\\( *-? *[0-9]+y\\)?\\( *[0-9]+d\\)? *[0-9][0-9]:[0-9][0-9]")
  4580. (replace-match ""))
  4581. (if negative (insert " -"))
  4582. (if (> y 0) (insert " " (format (if havetime fy fy1) y d h m))
  4583. (if (> d 0) (insert " " (format (if havetime fd fd1) d h m))
  4584. (insert " " (format fh h m))))
  4585. (if align (org-table-align))
  4586. (message "Time difference inserted"))))
  4587. (defun org-make-tdiff-string (y d h m)
  4588. (let ((fmt "")
  4589. (l nil))
  4590. (if (> y 0) (setq fmt (concat fmt "%d year" (if (> y 1) "s" "") " ")
  4591. l (push y l)))
  4592. (if (> d 0) (setq fmt (concat fmt "%d day" (if (> d 1) "s" "") " ")
  4593. l (push d l)))
  4594. (if (> h 0) (setq fmt (concat fmt "%d hour" (if (> h 1) "s" "") " ")
  4595. l (push h l)))
  4596. (if (> m 0) (setq fmt (concat fmt "%d minute" (if (> m 1) "s" "") " ")
  4597. l (push m l)))
  4598. (apply 'format fmt (nreverse l))))
  4599. (defun org-time-string-to-time (s)
  4600. (apply 'encode-time (org-parse-time-string s)))
  4601. (defun org-parse-time-string (s &optional nodefault)
  4602. "Parse the standard Org-mode time string.
  4603. This should be a lot faster than the normal `parse-time-string'.
  4604. If time is not given, defaults to 0:00. However, with optional NODEFAULT,
  4605. hour and minute fields will be nil if not given."
  4606. (if (string-match org-ts-regexp1 s)
  4607. (list 0
  4608. (if (or (match-beginning 8) (not nodefault))
  4609. (string-to-number (or (match-string 8 s) "0")))
  4610. (if (or (match-beginning 7) (not nodefault))
  4611. (string-to-number (or (match-string 7 s) "0")))
  4612. (string-to-number (match-string 4 s))
  4613. (string-to-number (match-string 3 s))
  4614. (string-to-number (match-string 2 s))
  4615. nil nil nil)
  4616. (make-list 9 0)))
  4617. (defun org-timestamp-up (&optional arg)
  4618. "Increase the date item at the cursor by one.
  4619. If the cursor is on the year, change the year. If it is on the month or
  4620. the day, change that.
  4621. With prefix ARG, change by that many units."
  4622. (interactive "p")
  4623. (org-timestamp-change (prefix-numeric-value arg)))
  4624. (defun org-timestamp-down (&optional arg)
  4625. "Decrease the date item at the cursor by one.
  4626. If the cursor is on the year, change the year. If it is on the month or
  4627. the day, change that.
  4628. With prefix ARG, change by that many units."
  4629. (interactive "p")
  4630. (org-timestamp-change (- (prefix-numeric-value arg))))
  4631. (defun org-timestamp-up-day (&optional arg)
  4632. "Increase the date in the time stamp by one day.
  4633. With prefix ARG, change that many days."
  4634. (interactive "p")
  4635. (if (and (not (org-at-timestamp-p))
  4636. (org-on-heading-p))
  4637. (org-todo 'up)
  4638. (org-timestamp-change (prefix-numeric-value arg) 'day)))
  4639. (defun org-timestamp-down-day (&optional arg)
  4640. "Decrease the date in the time stamp by one day.
  4641. With prefix ARG, change that many days."
  4642. (interactive "p")
  4643. (if (and (not (org-at-timestamp-p))
  4644. (org-on-heading-p))
  4645. (org-todo 'down)
  4646. (org-timestamp-change (- (prefix-numeric-value arg)) 'day)))
  4647. (defsubst org-pos-in-match-range (pos n)
  4648. (and (match-beginning n)
  4649. (<= (match-beginning n) pos)
  4650. (>= (match-end n) pos)))
  4651. (defun org-at-timestamp-p ()
  4652. "Determine if the cursor is in or at a timestamp."
  4653. (interactive)
  4654. (let* ((tsr org-ts-regexp2)
  4655. (pos (point))
  4656. (ans (or (looking-at tsr)
  4657. (save-excursion
  4658. (skip-chars-backward "^<\n\r\t")
  4659. (if (> (point) 1) (backward-char 1))
  4660. (and (looking-at tsr)
  4661. (> (- (match-end 0) pos) -1))))))
  4662. (and (boundp 'org-ts-what)
  4663. (setq org-ts-what
  4664. (cond
  4665. ((org-pos-in-match-range pos 2) 'year)
  4666. ((org-pos-in-match-range pos 3) 'month)
  4667. ((org-pos-in-match-range pos 7) 'hour)
  4668. ((org-pos-in-match-range pos 8) 'minute)
  4669. ((or (org-pos-in-match-range pos 4)
  4670. (org-pos-in-match-range pos 5)) 'day)
  4671. (t 'day))))
  4672. ans))
  4673. (defun org-timestamp-change (n &optional what)
  4674. "Change the date in the time stamp at point.
  4675. The date will be changed by N times WHAT. WHAT can be `day', `month',
  4676. `year', `minute', `second'. If WHAT is not given, the cursor position
  4677. in the timestamp determines what will be changed."
  4678. (let ((fmt (car org-time-stamp-formats))
  4679. org-ts-what
  4680. (pos (point))
  4681. ts time time0)
  4682. (if (not (org-at-timestamp-p))
  4683. (error "Not at a timestamp"))
  4684. (setq org-ts-what (or what org-ts-what))
  4685. (setq fmt (if (<= (abs (- (cdr org-ts-lengths)
  4686. (- (match-end 0) (match-beginning 0))))
  4687. 1)
  4688. (cdr org-time-stamp-formats)
  4689. (car org-time-stamp-formats)))
  4690. (setq ts (match-string 0))
  4691. (replace-match "")
  4692. (setq time0 (org-parse-time-string ts))
  4693. (setq time
  4694. (apply 'encode-time
  4695. (append
  4696. (list (or (car time0) 0))
  4697. (list (+ (if (eq org-ts-what 'minute) n 0) (nth 1 time0)))
  4698. (list (+ (if (eq org-ts-what 'hour) n 0) (nth 2 time0)))
  4699. (list (+ (if (eq org-ts-what 'day) n 0) (nth 3 time0)))
  4700. (list (+ (if (eq org-ts-what 'month) n 0) (nth 4 time0)))
  4701. (list (+ (if (eq org-ts-what 'year) n 0) (nth 5 time0)))
  4702. (nthcdr 6 time0))))
  4703. (if (eq what 'calendar)
  4704. (let ((cal-date
  4705. (save-excursion
  4706. (save-match-data
  4707. (set-buffer "*Calendar*")
  4708. (calendar-cursor-to-date)))))
  4709. (setcar (nthcdr 4 time0) (nth 0 cal-date)) ; month
  4710. (setcar (nthcdr 3 time0) (nth 1 cal-date)) ; day
  4711. (setcar (nthcdr 5 time0) (nth 2 cal-date)) ; year
  4712. (setcar time0 (or (car time0) 0))
  4713. (setcar (nthcdr 1 time0) (or (nth 1 time0) 0))
  4714. (setcar (nthcdr 2 time0) (or (nth 1 time0) 0))
  4715. (setq time (apply 'encode-time time0))))
  4716. (insert (setq org-last-changed-timestamp (format-time-string fmt time)))
  4717. (goto-char pos)
  4718. ;; Try to recenter the calendar window, if any
  4719. (if (and org-calendar-follow-timestamp-change
  4720. (get-buffer-window "*Calendar*" t)
  4721. (memq org-ts-what '(day month year)))
  4722. (org-recenter-calendar (time-to-days time)))))
  4723. (defun org-recenter-calendar (date)
  4724. "If the calendar is visible, recenter it to DATE."
  4725. (let* ((win (selected-window))
  4726. (cwin (get-buffer-window "*Calendar*" t))
  4727. (calendar-move-hook nil))
  4728. (when cwin
  4729. (select-window cwin)
  4730. (calendar-goto-date (if (listp date) date
  4731. (calendar-gregorian-from-absolute date)))
  4732. (select-window win))))
  4733. (defun org-goto-calendar (&optional arg)
  4734. "Go to the Emacs calendar at the current date.
  4735. If there is a time stamp in the current line, go to that date.
  4736. A prefix ARG can be used to force the current date."
  4737. (interactive "P")
  4738. (let ((tsr org-ts-regexp) diff
  4739. (calendar-move-hook nil)
  4740. (view-calendar-holidays-initially nil)
  4741. (view-diary-entries-initially nil))
  4742. (if (or (org-at-timestamp-p)
  4743. (save-excursion
  4744. (beginning-of-line 1)
  4745. (looking-at (concat ".*" tsr))))
  4746. (let ((d1 (time-to-days (current-time)))
  4747. (d2 (time-to-days
  4748. (org-time-string-to-time (match-string 1)))))
  4749. (setq diff (- d2 d1))))
  4750. (calendar)
  4751. (calendar-goto-today)
  4752. (if (and diff (not arg)) (calendar-forward-day diff))))
  4753. (defun org-date-from-calendar ()
  4754. "Insert time stamp corresponding to cursor date in *Calendar* buffer.
  4755. If there is already a time stamp at the cursor position, update it."
  4756. (interactive)
  4757. (org-timestamp-change 0 'calendar))
  4758. ;;; Agenda, and Diary Integration
  4759. ;;; Define the mode
  4760. (defvar org-agenda-mode-map (make-sparse-keymap)
  4761. "Keymap for `org-agenda-mode'.")
  4762. (defvar org-agenda-menu) ; defined later in this file.
  4763. (defvar org-agenda-follow-mode nil)
  4764. (defvar org-agenda-show-log nil)
  4765. (defvar org-agenda-buffer-name "*Org Agenda*")
  4766. (defvar org-agenda-redo-command nil)
  4767. (defvar org-agenda-mode-hook nil)
  4768. (defvar org-agenda-type nil)
  4769. (defvar org-agenda-force-single-file nil)
  4770. (defun org-agenda-mode ()
  4771. "Mode for time-sorted view on action items in Org-mode files.
  4772. The following commands are available:
  4773. \\{org-agenda-mode-map}"
  4774. (interactive)
  4775. (kill-all-local-variables)
  4776. (setq major-mode 'org-agenda-mode)
  4777. (setq mode-name "Org-Agenda")
  4778. (use-local-map org-agenda-mode-map)
  4779. (easy-menu-add org-agenda-menu)
  4780. (if org-startup-truncated (setq truncate-lines t))
  4781. (org-add-hook 'post-command-hook 'org-agenda-post-command-hook nil 'local)
  4782. (org-add-hook 'pre-command-hook 'org-unhighlight nil 'local)
  4783. (unless org-agenda-keep-modes
  4784. (setq org-agenda-follow-mode org-agenda-start-with-follow-mode
  4785. org-agenda-show-log nil))
  4786. (easy-menu-change
  4787. '("Agenda") "Agenda Files"
  4788. (append
  4789. (list
  4790. (vector
  4791. (if (get 'org-agenda-files 'org-restrict)
  4792. "Restricted to single file"
  4793. "Edit File List")
  4794. '(org-edit-agenda-file-list)
  4795. (not (get 'org-agenda-files 'org-restrict)))
  4796. "--")
  4797. (mapcar 'org-file-menu-entry (org-agenda-files))))
  4798. (org-agenda-set-mode-name)
  4799. (apply
  4800. (if (fboundp 'run-mode-hooks) 'run-mode-hooks 'run-hooks)
  4801. (list 'org-agenda-mode-hook)))
  4802. (define-key org-agenda-mode-map "\C-i" 'org-agenda-goto)
  4803. (define-key org-agenda-mode-map [(tab)] 'org-agenda-goto)
  4804. (define-key org-agenda-mode-map "\C-m" 'org-agenda-switch-to)
  4805. (define-key org-agenda-mode-map " " 'org-agenda-show)
  4806. (define-key org-agenda-mode-map "\C-c\C-t" 'org-agenda-todo)
  4807. (define-key org-agenda-mode-map "o" 'delete-other-windows)
  4808. (define-key org-agenda-mode-map "L" 'org-agenda-recenter)
  4809. (define-key org-agenda-mode-map "t" 'org-agenda-todo)
  4810. (define-key org-agenda-mode-map ":" 'org-agenda-set-tags)
  4811. (define-key org-agenda-mode-map "." 'org-agenda-goto-today)
  4812. (define-key org-agenda-mode-map "d" 'org-agenda-day-view)
  4813. (define-key org-agenda-mode-map "w" 'org-agenda-week-view)
  4814. (define-key org-agenda-mode-map (org-key 'S-right) 'org-agenda-date-later)
  4815. (define-key org-agenda-mode-map (org-key 'S-left) 'org-agenda-date-earlier)
  4816. (define-key org-agenda-mode-map [?\C-c ?\C-x (right)] 'org-agenda-date-later)
  4817. (define-key org-agenda-mode-map [?\C-c ?\C-x (left)] 'org-agenda-date-earlier)
  4818. (define-key org-agenda-mode-map ">" 'org-agenda-date-prompt)
  4819. (define-key org-agenda-mode-map "\C-c\C-s" 'org-agenda-schedule)
  4820. (define-key org-agenda-mode-map "\C-c\C-d" 'org-agenda-deadline)
  4821. (let ((l '(1 2 3 4 5 6 7 8 9 0)))
  4822. (while l (define-key org-agenda-mode-map
  4823. (int-to-string (pop l)) 'digit-argument)))
  4824. (define-key org-agenda-mode-map "f" 'org-agenda-follow-mode)
  4825. (define-key org-agenda-mode-map "l" 'org-agenda-log-mode)
  4826. (define-key org-agenda-mode-map "D" 'org-agenda-toggle-diary)
  4827. (define-key org-agenda-mode-map "g" 'org-agenda-toggle-time-grid)
  4828. (define-key org-agenda-mode-map "r" 'org-agenda-redo)
  4829. (define-key org-agenda-mode-map "q" 'org-agenda-quit)
  4830. (define-key org-agenda-mode-map "x" 'org-agenda-exit)
  4831. (define-key org-agenda-mode-map "P" 'org-agenda-show-priority)
  4832. (define-key org-agenda-mode-map "T" 'org-agenda-show-tags)
  4833. (define-key org-agenda-mode-map "n" 'next-line)
  4834. (define-key org-agenda-mode-map "p" 'previous-line)
  4835. (define-key org-agenda-mode-map "\C-n" 'org-agenda-next-date-line)
  4836. (define-key org-agenda-mode-map "\C-p" 'org-agenda-previous-date-line)
  4837. (define-key org-agenda-mode-map "," 'org-agenda-priority)
  4838. (define-key org-agenda-mode-map "\C-c," 'org-agenda-priority)
  4839. (define-key org-agenda-mode-map "i" 'org-agenda-diary-entry)
  4840. (define-key org-agenda-mode-map "c" 'org-agenda-goto-calendar)
  4841. (eval-after-load "calendar"
  4842. '(define-key calendar-mode-map org-calendar-to-agenda-key
  4843. 'org-calendar-goto-agenda))
  4844. (define-key org-agenda-mode-map "C" 'org-agenda-convert-date)
  4845. (define-key org-agenda-mode-map "m" 'org-agenda-phases-of-moon)
  4846. (define-key org-agenda-mode-map "M" 'org-agenda-phases-of-moon)
  4847. (define-key org-agenda-mode-map "s" 'org-agenda-sunrise-sunset)
  4848. (define-key org-agenda-mode-map "S" 'org-agenda-sunrise-sunset)
  4849. (define-key org-agenda-mode-map "h" 'org-agenda-holidays)
  4850. (define-key org-agenda-mode-map "H" 'org-agenda-holidays)
  4851. (define-key org-agenda-mode-map "+" 'org-agenda-priority-up)
  4852. (define-key org-agenda-mode-map "-" 'org-agenda-priority-down)
  4853. (define-key org-agenda-mode-map (org-key 'S-up) 'org-agenda-priority-up)
  4854. (define-key org-agenda-mode-map (org-key 'S-down) 'org-agenda-priority-down)
  4855. (define-key org-agenda-mode-map [?\C-c ?\C-x (up)] 'org-agenda-priority-up)
  4856. (define-key org-agenda-mode-map [?\C-c ?\C-x (down)] 'org-agenda-priority-down)
  4857. (define-key org-agenda-mode-map [(right)] 'org-agenda-later)
  4858. (define-key org-agenda-mode-map [(left)] 'org-agenda-earlier)
  4859. (define-key org-agenda-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  4860. (defvar org-agenda-keymap (copy-keymap org-agenda-mode-map)
  4861. "Local keymap for agenda entries from Org-mode.")
  4862. (define-key org-agenda-keymap
  4863. (if (featurep 'xemacs) [(button2)] [(mouse-2)]) 'org-agenda-goto-mouse)
  4864. (define-key org-agenda-keymap
  4865. (if (featurep 'xemacs) [(button3)] [(mouse-3)]) 'org-agenda-show-mouse)
  4866. (when org-agenda-mouse-1-follows-link
  4867. (define-key org-agenda-keymap [follow-link] 'mouse-face))
  4868. (easy-menu-define org-agenda-menu org-agenda-mode-map "Agenda menu"
  4869. '("Agenda"
  4870. ("Agenda Files")
  4871. "--"
  4872. ["Show" org-agenda-show t]
  4873. ["Go To (other window)" org-agenda-goto t]
  4874. ["Go To (one window)" org-agenda-switch-to t]
  4875. ["Follow Mode" org-agenda-follow-mode
  4876. :style toggle :selected org-agenda-follow-mode :active t]
  4877. "--"
  4878. ["Cycle TODO" org-agenda-todo t]
  4879. ("Tags"
  4880. ["Show all Tags" org-agenda-show-tags t]
  4881. ["Set Tags" org-agenda-set-tags t])
  4882. ("Schedule"
  4883. ["Schedule" org-agenda-schedule t]
  4884. ["Set Deadline" org-agenda-deadline t]
  4885. "--"
  4886. ["Reschedule +1 day" org-agenda-date-later (org-agenda-check-type nil 'agenda 'timeline)]
  4887. ["Reschedule -1 day" org-agenda-date-earlier (org-agenda-check-type nil 'agenda 'timeline)]
  4888. ["Reschedule to ..." org-agenda-date-prompt (org-agenda-check-type nil 'agenda 'timeline)])
  4889. ("Priority"
  4890. ["Set Priority" org-agenda-priority t]
  4891. ["Increase Priority" org-agenda-priority-up t]
  4892. ["Decrease Priority" org-agenda-priority-down t]
  4893. ["Show Priority" org-agenda-show-priority t])
  4894. "--"
  4895. ;; ["New agenda command" org-agenda t]
  4896. ["Rebuild buffer" org-agenda-redo t]
  4897. "--"
  4898. ["Goto Today" org-agenda-goto-today (org-agenda-check-type nil 'agenda 'timeline)]
  4899. ["Next Dates" org-agenda-later (org-agenda-check-type nil 'agenda)]
  4900. ["Previous Dates" org-agenda-earlier (org-agenda-check-type nil 'agenda)]
  4901. "--"
  4902. ["Day View" org-agenda-day-view :active (org-agenda-check-type nil 'agenda)
  4903. :style radio :selected (equal org-agenda-ndays 1)]
  4904. ["Week View" org-agenda-week-view :active (org-agenda-check-type nil 'agenda)
  4905. :style radio :selected (equal org-agenda-ndays 7)]
  4906. "--"
  4907. ["Show Logbook entries" org-agenda-log-mode
  4908. :style toggle :selected org-agenda-show-log :active (org-agenda-check-type nil 'agenda 'timeline)]
  4909. ["Include Diary" org-agenda-toggle-diary
  4910. :style toggle :selected org-agenda-include-diary :active (org-agenda-check-type nil 'agenda)]
  4911. ["Use Time Grid" org-agenda-toggle-time-grid
  4912. :style toggle :selected org-agenda-use-time-grid :active (org-agenda-check-type nil 'agenda)]
  4913. "--"
  4914. ["New Diary Entry" org-agenda-diary-entry (org-agenda-check-type nil 'agenda 'timeline)]
  4915. ("Calendar Commands"
  4916. ["Goto Calendar" org-agenda-goto-calendar (org-agenda-check-type nil 'agenda 'timeline)]
  4917. ["Phases of the Moon" org-agenda-phases-of-moon (org-agenda-check-type nil 'agenda 'timeline)]
  4918. ["Sunrise/Sunset" org-agenda-sunrise-sunset (org-agenda-check-type nil 'agenda 'timeline)]
  4919. ["Holidays" org-agenda-holidays (org-agenda-check-type nil 'agenda 'timeline)]
  4920. ["Convert" org-agenda-convert-date (org-agenda-check-type nil 'agenda 'timeline)])
  4921. ["Create iCalendar file" org-export-icalendar-combine-agenda-files t]
  4922. "--"
  4923. ["Quit" org-agenda-quit t]
  4924. ["Exit and Release Buffers" org-agenda-exit t]
  4925. ))
  4926. ;;;###autoload
  4927. (defun org-agenda (arg)
  4928. "Dispatch agenda commands to collect entries to the agenda buffer.
  4929. Prompts for a character to select a command. Any prefix arg will be passed
  4930. on to the selected command. The default selections are:
  4931. a Call `org-agenda' to display the agenda for the current day or week.
  4932. t Call `org-todo-list' to display the global todo list.
  4933. T Call `org-todo-list' to display the global todo list, select only
  4934. entries with a specific TODO keyword (the user gets a prompt).
  4935. m Call `org-tags-view' to display headlines with tags matching
  4936. a condition (the user is prompted for the condition).
  4937. M Like `m', but select only TODO entries, no ordinary headlines.
  4938. More commands can be added by configuring the variable
  4939. `org-agenda-custom-commands'. In particular, specific tags and TODO keyword
  4940. searches can be pre-defined in this way.
  4941. If the current buffer is in Org-mode and visiting a file, you can also
  4942. first press `1' to indicate that the agenda should be temporarily (until the
  4943. next use of \\[org-agenda]) restricted to the current file."
  4944. (interactive "P")
  4945. (catch 'exit
  4946. (let ((restrict-ok (and buffer-file-name (eq major-mode 'org-mode)))
  4947. (bfn buffer-file-name)
  4948. (custom org-agenda-custom-commands)
  4949. c entry key type string)
  4950. (put 'org-agenda-files 'org-restrict nil)
  4951. (save-window-excursion
  4952. (delete-other-windows)
  4953. (switch-to-buffer-other-window " *Agenda Commands*")
  4954. (erase-buffer)
  4955. (insert
  4956. "Press key for an agenda command:
  4957. --------------------------------
  4958. a Agenda for current week or day
  4959. t List of all TODO entries T Entries with special TODO kwd
  4960. m Match a TAGS query M Like m, but only TODO entries
  4961. C Configure your own agenda commands")
  4962. (while (setq entry (pop custom))
  4963. (setq key (car entry) type (nth 1 entry) string (nth 2 entry))
  4964. (insert (format "\n%-4s%-14s: %s"
  4965. key
  4966. (cond
  4967. ((eq type 'tags) "Tags query")
  4968. ((eq type 'todo) "TODO keyword")
  4969. ((eq type 'tags-tree) "Tags tree")
  4970. ((eq type 'todo-tree) "TODO kwd tree")
  4971. ((eq type 'occur-tree) "Occur tree")
  4972. (t "???"))
  4973. (org-add-props string nil 'face 'org-warning))))
  4974. (goto-char (point-min))
  4975. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  4976. (message "Press key for agenda command%s"
  4977. (if restrict-ok ", or [1] to restrict to current file" ""))
  4978. (setq c (read-char-exclusive))
  4979. (message "")
  4980. (when (equal c ?1)
  4981. (if restrict-ok
  4982. (put 'org-agenda-files 'org-restrict (list bfn))
  4983. (error "Cannot restrict agenda to current buffer"))
  4984. (message "Press key for agenda command%s"
  4985. (if restrict-ok " (restricted to current file)" ""))
  4986. (setq c (read-char-exclusive))
  4987. (message "")))
  4988. (require 'calendar) ; FIXME: can we avoid this for some commands?
  4989. ;; For example the todo list should not need it (but does...)
  4990. (cond
  4991. ((equal c ?C) (customize-variable 'org-agenda-custom-commands))
  4992. ((equal c ?a) (call-interactively 'org-agenda-list))
  4993. ((equal c ?t) (call-interactively 'org-todo-list))
  4994. ((equal c ?T) (org-call-with-arg 'org-todo-list (or arg '(4))))
  4995. ((equal c ?m) (call-interactively 'org-tags-view))
  4996. ((equal c ?M) (org-call-with-arg 'org-tags-view (or arg '(4))))
  4997. ((setq entry (assoc (char-to-string c) org-agenda-custom-commands))
  4998. (setq type (nth 1 entry) string (nth 2 entry))
  4999. (cond
  5000. ((eq type 'tags)
  5001. (org-tags-view current-prefix-arg string))
  5002. ((eq type 'tags-todo)
  5003. (org-tags-view '(4) string))
  5004. ((eq type 'todo)
  5005. (org-todo-list string))
  5006. ((eq type 'tags-tree)
  5007. (org-check-for-org-mode)
  5008. (org-tags-sparse-tree current-prefix-arg string))
  5009. ((eq type 'todo-tree)
  5010. (org-check-for-org-mode)
  5011. (org-occur (concat "^" outline-regexp "[ \t]*"
  5012. (regexp-quote string) "\\>")))
  5013. ((eq type 'occur-tree)
  5014. (org-check-for-org-mode)
  5015. (org-occur string))
  5016. (t (error "Invalid custom agenda command type %s" type))))
  5017. (t (error "Invalid key"))))))
  5018. (defun org-check-for-org-mode ()
  5019. "Make sure current buffer is in org-mode. Error if not."
  5020. (or (eq major-mode 'org-mode)
  5021. (error "Cannot execute org-mode agenda command on buffer in %s."
  5022. major-mode)))
  5023. (defun org-fit-agenda-window ()
  5024. "Fit the window to the buffer size."
  5025. (and org-fit-agenda-window
  5026. (fboundp 'fit-window-to-buffer)
  5027. (fit-window-to-buffer nil (/ (* (frame-height) 3) 4)
  5028. (/ (frame-height) 2))))
  5029. (defun org-agenda-files (&optional unrestricted)
  5030. "Get the list of agenda files.
  5031. Optional UNRESTRICTED means return the full list even if a restriction
  5032. is currently in place."
  5033. (cond
  5034. ((and (not unrestricted) (get 'org-agenda-files 'org-restrict)))
  5035. ((stringp org-agenda-files) (org-read-agenda-file-list))
  5036. ((listp org-agenda-files) org-agenda-files)
  5037. (t (error "Invalid value of `org-agenda-files'"))))
  5038. (defvar org-window-configuration)
  5039. (defun org-edit-agenda-file-list ()
  5040. "Edit the list of agenda files.
  5041. Depending on setup, this either uses customize to edit the variable
  5042. `org-agenda-files', or it visits the file that is holding the list. In the
  5043. latter case, the buffer is set up in a way that saving it automatically kills
  5044. the buffer and restores the previous window configuration."
  5045. (interactive)
  5046. (if (stringp org-agenda-files)
  5047. (let ((cw (current-window-configuration)))
  5048. (find-file org-agenda-files)
  5049. (set (make-local-variable 'org-window-configuration) cw)
  5050. (org-add-hook 'after-save-hook
  5051. (lambda ()
  5052. (set-window-configuration
  5053. (prog1 org-window-configuration
  5054. (kill-buffer (current-buffer))))
  5055. (org-install-agenda-files-menu)
  5056. (message "New agenda file list installed"))
  5057. nil 'local)
  5058. (message (substitute-command-keys
  5059. "Edit list and finish with \\[save-buffer]")))
  5060. (customize-variable 'org-agenda-files)))
  5061. (defun org-store-new-agenda-file-list (list)
  5062. "Set new value for the agenda file list and save it correcly."
  5063. (if (stringp org-agenda-files)
  5064. (let ((f org-agenda-files) b)
  5065. (while (setq b (find-buffer-visiting f)) (kill-buffer b))
  5066. (with-temp-file f
  5067. (insert (mapconcat 'identity list "\n") "\n")))
  5068. (let ((org-mode-hook nil) (default-major-mode 'fundamental-mode))
  5069. (setq org-agenda-files list)
  5070. (customize-save-variable 'org-agenda-files org-agenda-files))))
  5071. (defun org-read-agenda-file-list ()
  5072. "Read the list of agenda files from a file."
  5073. (when (stringp org-agenda-files)
  5074. (with-temp-buffer
  5075. (insert-file-contents org-agenda-files)
  5076. (org-split-string (buffer-string) "[ \t\r\n]*?[\r\n][ \t\r\n]*"))))
  5077. (defvar org-agenda-markers nil
  5078. "List of all currently active markers created by `org-agenda'.")
  5079. (defvar org-agenda-last-marker-time (time-to-seconds (current-time))
  5080. "Creation time of the last agenda marker.")
  5081. (defun org-agenda-new-marker (&optional pos)
  5082. "Return a new agenda marker.
  5083. Org-mode keeps a list of these markers and resets them when they are
  5084. no longer in use."
  5085. (let ((m (copy-marker (or pos (point)))))
  5086. (setq org-agenda-last-marker-time (time-to-seconds (current-time)))
  5087. (push m org-agenda-markers)
  5088. m))
  5089. (defun org-agenda-maybe-reset-markers (&optional force)
  5090. "Reset markers created by `org-agenda'. But only if they are old enough."
  5091. (if (or force
  5092. (> (- (time-to-seconds (current-time))
  5093. org-agenda-last-marker-time)
  5094. 5))
  5095. (while org-agenda-markers
  5096. (move-marker (pop org-agenda-markers) nil))))
  5097. (defvar org-agenda-new-buffers nil
  5098. "Buffers created to visit agenda files.")
  5099. (defun org-get-agenda-file-buffer (file)
  5100. "Get a buffer visiting FILE. If the buffer needs to be created, add
  5101. it to the list of buffers which might be released later."
  5102. (let ((buf (find-buffer-visiting file)))
  5103. (if buf
  5104. buf ; just return it
  5105. ;; Make a new buffer and remember it
  5106. (setq buf (find-file-noselect file))
  5107. (if buf (push buf org-agenda-new-buffers))
  5108. buf)))
  5109. (defun org-release-buffers (blist)
  5110. "Release all buffers in list, asking the user for confirmation when needed.
  5111. When a buffer is unmodified, it is just killed. When modified, it is saved
  5112. \(if the user agrees) and then killed."
  5113. (let (buf file)
  5114. (while (setq buf (pop blist))
  5115. (setq file (buffer-file-name buf))
  5116. (when (and (buffer-modified-p buf)
  5117. file
  5118. (y-or-n-p (format "Save file %s? " file)))
  5119. (with-current-buffer buf (save-buffer)))
  5120. (kill-buffer buf))))
  5121. (defvar org-respect-restriction nil) ; Dynamically-scoped param.
  5122. (defun org-timeline (&optional include-all keep-modes)
  5123. "Show a time-sorted view of the entries in the current org file.
  5124. Only entries with a time stamp of today or later will be listed. With
  5125. \\[universal-argument] prefix, all unfinished TODO items will also be shown,
  5126. under the current date.
  5127. If the buffer contains an active region, only check the region for
  5128. dates."
  5129. (interactive "P")
  5130. (require 'calendar)
  5131. (org-agenda-maybe-reset-markers 'force)
  5132. (org-compile-prefix-format org-timeline-prefix-format)
  5133. (let* ((dopast t)
  5134. (dotodo include-all)
  5135. (doclosed org-agenda-show-log)
  5136. (org-agenda-keep-modes keep-modes)
  5137. (entry buffer-file-name)
  5138. (org-agenda-files (list buffer-file-name))
  5139. (date (calendar-current-date))
  5140. (win (selected-window))
  5141. (pos1 (point))
  5142. (beg (if (org-region-active-p) (region-beginning) (point-min)))
  5143. (end (if (org-region-active-p) (region-end) (point-max)))
  5144. (day-numbers (org-get-all-dates beg end 'no-ranges
  5145. t doclosed ; always include today
  5146. org-timeline-show-empty-dates))
  5147. (today (time-to-days (current-time)))
  5148. (org-respect-restriction t)
  5149. (past t)
  5150. args
  5151. s e rtn d emptyp)
  5152. (setq org-agenda-redo-command
  5153. (list 'progn
  5154. (list 'switch-to-buffer-other-window (current-buffer))
  5155. (list 'org-timeline (list 'quote include-all) t)))
  5156. (if (not dopast)
  5157. ;; Remove past dates from the list of dates.
  5158. (setq day-numbers (delq nil (mapcar (lambda(x)
  5159. (if (>= x today) x nil))
  5160. day-numbers))))
  5161. (switch-to-buffer-other-window
  5162. (get-buffer-create org-agenda-buffer-name))
  5163. (setq buffer-read-only nil)
  5164. (erase-buffer)
  5165. (org-agenda-mode) (setq buffer-read-only nil)
  5166. (set (make-local-variable 'org-agenda-type) 'timeline)
  5167. (if doclosed (push :closed args))
  5168. (push :timestamp args)
  5169. (if dotodo (push :todo args))
  5170. (while (setq d (pop day-numbers))
  5171. (if (and (listp d) (eq (car d) :omitted))
  5172. (progn
  5173. (setq s (point))
  5174. (insert (format "\n[... %d empty days omitted]\n\n" (cdr d)))
  5175. (put-text-property s (1- (point)) 'face 'org-level-3))
  5176. (if (listp d) (setq d (car d) emptyp t) (setq emptyp nil))
  5177. (if (and (>= d today)
  5178. dopast
  5179. past)
  5180. (progn
  5181. (setq past nil)
  5182. (insert (make-string 79 ?-) "\n")))
  5183. (setq date (calendar-gregorian-from-absolute d))
  5184. (setq s (point))
  5185. (setq rtn (and (not emptyp)
  5186. (apply 'org-agenda-get-day-entries
  5187. entry date args)))
  5188. (if (or rtn (equal d today) org-timeline-show-empty-dates)
  5189. (progn
  5190. (insert (calendar-day-name date) " "
  5191. (number-to-string (extract-calendar-day date)) " "
  5192. (calendar-month-name (extract-calendar-month date)) " "
  5193. (number-to-string (extract-calendar-year date)) "\n")
  5194. (put-text-property s (1- (point)) 'face
  5195. 'org-level-3)
  5196. (if (equal d today)
  5197. (put-text-property s (1- (point)) 'org-today t))
  5198. (and rtn (insert (org-finalize-agenda-entries rtn) "\n"))
  5199. (put-text-property s (1- (point)) 'day d)))))
  5200. (goto-char (point-min))
  5201. (setq buffer-read-only t)
  5202. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  5203. (point-min)))
  5204. (when (not org-select-timeline-window)
  5205. (select-window win)
  5206. (goto-char pos1))))
  5207. ;;;###autoload
  5208. (defun org-agenda-list (&optional include-all start-day ndays keep-modes)
  5209. "Produce a weekly view from all files in variable `org-agenda-files'.
  5210. The view will be for the current week, but from the overview buffer you
  5211. will be able to go to other weeks.
  5212. With one \\[universal-argument] prefix argument INCLUDE-ALL, all unfinished TODO items will
  5213. also be shown, under the current date.
  5214. With two \\[universal-argument] prefix argument INCLUDE-ALL, all TODO entries marked DONE
  5215. on the days are also shown. See the variable `org-log-done' for how
  5216. to turn on logging.
  5217. START-DAY defaults to TODAY, or to the most recent match for the weekday
  5218. given in `org-agenda-start-on-weekday'.
  5219. NDAYS defaults to `org-agenda-ndays'."
  5220. (interactive "P")
  5221. (org-agenda-maybe-reset-markers 'force)
  5222. (org-compile-prefix-format org-agenda-prefix-format)
  5223. (require 'calendar)
  5224. (let* ((org-agenda-start-on-weekday
  5225. (if (or (equal ndays 1)
  5226. (and (null ndays) (equal 1 org-agenda-ndays)))
  5227. nil org-agenda-start-on-weekday))
  5228. (org-agenda-keep-modes keep-modes)
  5229. (thefiles (org-agenda-files))
  5230. (files thefiles)
  5231. (win (selected-window))
  5232. (today (time-to-days (current-time)))
  5233. (sd (or start-day today))
  5234. (start (if (or (null org-agenda-start-on-weekday)
  5235. (< org-agenda-ndays 7))
  5236. sd
  5237. (let* ((nt (calendar-day-of-week
  5238. (calendar-gregorian-from-absolute sd)))
  5239. (n1 org-agenda-start-on-weekday)
  5240. (d (- nt n1)))
  5241. (- sd (+ (if (< d 0) 7 0) d)))))
  5242. (day-numbers (list start))
  5243. (inhibit-redisplay t)
  5244. s e rtn rtnall file date d start-pos end-pos todayp nd)
  5245. (setq org-agenda-redo-command
  5246. (list 'org-agenda-list (list 'quote include-all) start-day ndays t))
  5247. ;; Make the list of days
  5248. (setq ndays (or ndays org-agenda-ndays)
  5249. nd ndays)
  5250. (while (> ndays 1)
  5251. (push (1+ (car day-numbers)) day-numbers)
  5252. (setq ndays (1- ndays)))
  5253. (setq day-numbers (nreverse day-numbers))
  5254. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  5255. (progn
  5256. (delete-other-windows)
  5257. (switch-to-buffer-other-window
  5258. (get-buffer-create org-agenda-buffer-name))))
  5259. (setq buffer-read-only nil)
  5260. (erase-buffer)
  5261. (org-agenda-mode) (setq buffer-read-only nil)
  5262. (set (make-local-variable 'org-agenda-type) 'agenda)
  5263. (set (make-local-variable 'starting-day) (car day-numbers))
  5264. (set (make-local-variable 'include-all-loc) include-all)
  5265. (when (and (or include-all org-agenda-include-all-todo)
  5266. (member today day-numbers))
  5267. (setq files thefiles
  5268. rtnall nil)
  5269. (while (setq file (pop files))
  5270. (catch 'nextfile
  5271. (org-check-agenda-file file)
  5272. (setq date (calendar-gregorian-from-absolute today)
  5273. rtn (org-agenda-get-day-entries
  5274. file date :todo))
  5275. (setq rtnall (append rtnall rtn))))
  5276. (when rtnall
  5277. (insert "ALL CURRENTLY OPEN TODO ITEMS:\n")
  5278. (add-text-properties (point-min) (1- (point))
  5279. (list 'face 'org-level-3))
  5280. (insert (org-finalize-agenda-entries rtnall) "\n")))
  5281. (while (setq d (pop day-numbers))
  5282. (setq date (calendar-gregorian-from-absolute d)
  5283. s (point))
  5284. (if (or (setq todayp (= d today))
  5285. (and (not start-pos) (= d sd)))
  5286. (setq start-pos (point))
  5287. (if (and start-pos (not end-pos))
  5288. (setq end-pos (point))))
  5289. (setq files thefiles
  5290. rtnall nil)
  5291. (while (setq file (pop files))
  5292. (catch 'nextfile
  5293. (org-check-agenda-file file)
  5294. (if org-agenda-show-log
  5295. (setq rtn (org-agenda-get-day-entries
  5296. file date
  5297. :deadline :scheduled :timestamp :closed))
  5298. (setq rtn (org-agenda-get-day-entries
  5299. file date
  5300. :deadline :scheduled :timestamp)))
  5301. (setq rtnall (append rtnall rtn))))
  5302. (if org-agenda-include-diary
  5303. (progn
  5304. (require 'diary-lib)
  5305. (setq rtn (org-get-entries-from-diary date))
  5306. (setq rtnall (append rtnall rtn))))
  5307. (if (or rtnall org-agenda-show-all-dates)
  5308. (progn
  5309. (insert (format "%-9s %2d %s %4d\n"
  5310. (calendar-day-name date)
  5311. (extract-calendar-day date)
  5312. (calendar-month-name (extract-calendar-month date))
  5313. (extract-calendar-year date)))
  5314. (put-text-property s (1- (point)) 'face
  5315. 'org-level-3)
  5316. (if rtnall (insert
  5317. (org-finalize-agenda-entries
  5318. (org-agenda-add-time-grid-maybe
  5319. rtnall nd todayp))
  5320. "\n"))
  5321. (put-text-property s (1- (point)) 'day d))))
  5322. (goto-char (point-min))
  5323. (setq buffer-read-only t)
  5324. (org-fit-agenda-window)
  5325. (unless (and (pos-visible-in-window-p (point-min))
  5326. (pos-visible-in-window-p (point-max)))
  5327. (goto-char (1- (point-max)))
  5328. (recenter -1)
  5329. (if (not (pos-visible-in-window-p (or start-pos 1)))
  5330. (progn
  5331. (goto-char (or start-pos 1))
  5332. (recenter 1))))
  5333. (goto-char (or start-pos 1))
  5334. (if (not org-select-agenda-window) (select-window win))
  5335. (message "")))
  5336. (defvar org-select-this-todo-keyword nil)
  5337. ;;;###autoload
  5338. (defun org-todo-list (arg &optional keep-modes)
  5339. "Show all TODO entries from all agenda file in a single list.
  5340. The prefix arg can be used to select a specific TODO keyword and limit
  5341. the list to these. When using \\[universal-argument], you will be prompted
  5342. for a keyword. A numeric prefix directly selects the Nth keyword in
  5343. `org-todo-keywords'."
  5344. (interactive "P")
  5345. (org-agenda-maybe-reset-markers 'force)
  5346. (org-compile-prefix-format org-agenda-prefix-format)
  5347. (let* ((org-agenda-keep-modes keep-modes)
  5348. (today (time-to-days (current-time)))
  5349. (date (calendar-gregorian-from-absolute today))
  5350. (win (selected-window))
  5351. (kwds org-todo-keywords)
  5352. (completion-ignore-case t)
  5353. (org-select-this-todo-keyword
  5354. (if (stringp arg) arg
  5355. (and arg (integerp arg) (> arg 0)
  5356. (nth (1- arg) org-todo-keywords))))
  5357. rtn rtnall files file pos)
  5358. (when (equal arg '(4))
  5359. (setq org-select-this-todo-keyword
  5360. (completing-read "Keyword: " (mapcar 'list org-todo-keywords)
  5361. nil t)))
  5362. (and (equal 0 arg) (setq org-select-this-todo-keyword nil))
  5363. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  5364. (progn
  5365. (delete-other-windows)
  5366. (switch-to-buffer-other-window
  5367. (get-buffer-create org-agenda-buffer-name))))
  5368. (setq buffer-read-only nil)
  5369. (erase-buffer)
  5370. (org-agenda-mode) (setq buffer-read-only nil)
  5371. (set (make-local-variable 'org-agenda-type) 'todo)
  5372. (set (make-local-variable 'last-arg) arg)
  5373. (set (make-local-variable 'org-todo-keywords) kwds)
  5374. (set (make-local-variable 'org-agenda-redo-command)
  5375. '(org-todo-list (or current-prefix-arg last-arg) t))
  5376. (setq files (org-agenda-files)
  5377. rtnall nil)
  5378. (while (setq file (pop files))
  5379. (catch 'nextfile
  5380. (org-check-agenda-file file)
  5381. (setq rtn (org-agenda-get-day-entries file date :todo))
  5382. (setq rtnall (append rtnall rtn))))
  5383. (insert "Global list of TODO items of type: ")
  5384. (add-text-properties (point-min) (1- (point))
  5385. (list 'face 'org-level-3))
  5386. (setq pos (point))
  5387. (insert (or org-select-this-todo-keyword "ALL") "\n")
  5388. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  5389. (setq pos (point))
  5390. (insert
  5391. "Available with `N r': (0)ALL "
  5392. (let ((n 0))
  5393. (mapconcat (lambda (x)
  5394. (format "(%d)%s" (setq n (1+ n)) x))
  5395. org-todo-keywords " "))
  5396. "\n")
  5397. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  5398. (when rtnall
  5399. (insert (org-finalize-agenda-entries rtnall) "\n"))
  5400. (goto-char (point-min))
  5401. (setq buffer-read-only t)
  5402. (org-fit-agenda-window)
  5403. (if (not org-select-agenda-window) (select-window win))))
  5404. (defun org-check-agenda-file (file)
  5405. "Make sure FILE exists. If not, ask user what to do."
  5406. (when (not (file-exists-p file))
  5407. (message "non-existent file %s. [R]emove from list or [A]bort?"
  5408. (abbreviate-file-name file))
  5409. (let ((r (downcase (read-char-exclusive))))
  5410. (cond
  5411. ((equal r ?r)
  5412. (org-remove-file file)
  5413. (throw 'nextfile t))
  5414. (t (error "Abort"))))))
  5415. (defun org-agenda-check-type (error &rest types)
  5416. "Check if agenda buffer is of allowed type.
  5417. If ERROR is non-nil, throw an error, otherwise just return nil."
  5418. (if (memq org-agenda-type types)
  5419. t
  5420. (if error
  5421. (error "Not allowed in %s-type agenda buffers" org-agenda-type)
  5422. nil)))
  5423. (defun org-agenda-quit ()
  5424. "Exit agenda by removing the window or the buffer."
  5425. (interactive)
  5426. (let ((buf (current-buffer)))
  5427. (if (not (one-window-p)) (delete-window))
  5428. (kill-buffer buf)
  5429. (org-agenda-maybe-reset-markers 'force)))
  5430. (defun org-agenda-exit ()
  5431. "Exit agenda by removing the window or the buffer.
  5432. Also kill all Org-mode buffers which have been loaded by `org-agenda'.
  5433. Org-mode buffers visited directly by the user will not be touched."
  5434. (interactive)
  5435. (org-release-buffers org-agenda-new-buffers)
  5436. (setq org-agenda-new-buffers nil)
  5437. (org-agenda-quit))
  5438. (defun org-agenda-redo ()
  5439. "Rebuild Agenda.
  5440. When this is the global TODO list, a prefix argument will be interpreted."
  5441. (interactive)
  5442. (message "Rebuilding agenda buffer...")
  5443. (eval org-agenda-redo-command)
  5444. (message "Rebuilding agenda buffer...done"))
  5445. (defun org-agenda-goto-today ()
  5446. "Go to today."
  5447. (interactive)
  5448. (org-agenda-check-type t 'timeline 'agenda)
  5449. (if (boundp 'starting-day)
  5450. (let ((cmd (car org-agenda-redo-command))
  5451. (iall (nth 1 org-agenda-redo-command))
  5452. (nday (nth 3 org-agenda-redo-command))
  5453. (keep (nth 4 org-agenda-redo-command)))
  5454. (eval (list cmd iall nil nday keep)))
  5455. (goto-char (or (text-property-any (point-min) (point-max) 'org-today t)
  5456. (point-min)))))
  5457. (defun org-agenda-later (arg)
  5458. "Go forward in time by `org-agenda-ndays' days.
  5459. With prefix ARG, go forward that many times `org-agenda-ndays'."
  5460. (interactive "p")
  5461. (org-agenda-check-type t 'agenda)
  5462. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  5463. (+ starting-day (* arg org-agenda-ndays)) nil t))
  5464. (defun org-agenda-earlier (arg)
  5465. "Go back in time by `org-agenda-ndays' days.
  5466. With prefix ARG, go back that many times `org-agenda-ndays'."
  5467. (interactive "p")
  5468. (org-agenda-check-type t 'agenda)
  5469. (org-agenda-list (if (boundp 'include-all-loc) include-all-loc nil)
  5470. (- starting-day (* arg org-agenda-ndays)) nil t))
  5471. (defun org-agenda-week-view ()
  5472. "Switch to weekly view for agenda."
  5473. (interactive)
  5474. (org-agenda-check-type t 'agenda)
  5475. (setq org-agenda-ndays 7)
  5476. (org-agenda-list include-all-loc
  5477. (or (get-text-property (point) 'day)
  5478. starting-day)
  5479. nil t)
  5480. (org-agenda-set-mode-name)
  5481. (message "Switched to week view"))
  5482. (defun org-agenda-day-view ()
  5483. "Switch to daily view for agenda."
  5484. (interactive)
  5485. (org-agenda-check-type t 'agenda)
  5486. (setq org-agenda-ndays 1)
  5487. (org-agenda-list include-all-loc
  5488. (or (get-text-property (point) 'day)
  5489. starting-day)
  5490. nil t)
  5491. (org-agenda-set-mode-name)
  5492. (message "Switched to day view"))
  5493. (defun org-agenda-next-date-line (&optional arg)
  5494. "Jump to the next line indicating a date in agenda buffer."
  5495. (interactive "p")
  5496. (org-agenda-check-type t 'agenda 'timeline)
  5497. (beginning-of-line 1)
  5498. (if (looking-at "^\\S-") (forward-char 1))
  5499. (if (not (re-search-forward "^\\S-" nil t arg))
  5500. (progn
  5501. (backward-char 1)
  5502. (error "No next date after this line in this buffer")))
  5503. (goto-char (match-beginning 0)))
  5504. (defun org-agenda-previous-date-line (&optional arg)
  5505. "Jump to the previous line indicating a date in agenda buffer."
  5506. (interactive "p")
  5507. (org-agenda-check-type t 'agenda 'timeline)
  5508. (beginning-of-line 1)
  5509. (if (not (re-search-backward "^\\S-" nil t arg))
  5510. (error "No previous date before this line in this buffer")))
  5511. ;; Initialize the highlight
  5512. (defvar org-hl (org-make-overlay 1 1))
  5513. (org-overlay-put org-hl 'face 'highlight)
  5514. (defun org-highlight (begin end &optional buffer)
  5515. "Highlight a region with overlay."
  5516. (funcall (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay)
  5517. org-hl begin end (or buffer (current-buffer))))
  5518. (defun org-unhighlight ()
  5519. "Detach overlay INDEX."
  5520. (funcall (if (featurep 'xemacs) 'detach-extent 'delete-overlay) org-hl))
  5521. (defun org-agenda-follow-mode ()
  5522. "Toggle follow mode in an agenda buffer."
  5523. (interactive)
  5524. (setq org-agenda-follow-mode (not org-agenda-follow-mode))
  5525. (org-agenda-set-mode-name)
  5526. (message "Follow mode is %s"
  5527. (if org-agenda-follow-mode "on" "off")))
  5528. (defun org-agenda-log-mode ()
  5529. "Toggle log mode in an agenda buffer."
  5530. (interactive)
  5531. (org-agenda-check-type t 'agenda 'timeline)
  5532. (setq org-agenda-show-log (not org-agenda-show-log))
  5533. (org-agenda-set-mode-name)
  5534. (org-agenda-redo)
  5535. (message "Log mode is %s"
  5536. (if org-agenda-show-log "on" "off")))
  5537. (defun org-agenda-toggle-diary ()
  5538. "Toggle diary inclusion in an agenda buffer."
  5539. (interactive)
  5540. (org-agenda-check-type t 'agenda)
  5541. (setq org-agenda-include-diary (not org-agenda-include-diary))
  5542. (org-agenda-redo)
  5543. (org-agenda-set-mode-name)
  5544. (message "Diary inclusion turned %s"
  5545. (if org-agenda-include-diary "on" "off")))
  5546. (defun org-agenda-toggle-time-grid ()
  5547. "Toggle time grid in an agenda buffer."
  5548. (interactive)
  5549. (org-agenda-check-type t 'agenda)
  5550. (setq org-agenda-use-time-grid (not org-agenda-use-time-grid))
  5551. (org-agenda-redo)
  5552. (org-agenda-set-mode-name)
  5553. (message "Time-grid turned %s"
  5554. (if org-agenda-use-time-grid "on" "off")))
  5555. (defun org-agenda-set-mode-name ()
  5556. "Set the mode name to indicate all the small mode settings."
  5557. (setq mode-name
  5558. (concat "Org-Agenda"
  5559. (if (equal org-agenda-ndays 1) " Day" "")
  5560. (if (equal org-agenda-ndays 7) " Week" "")
  5561. (if org-agenda-follow-mode " Follow" "")
  5562. (if org-agenda-include-diary " Diary" "")
  5563. (if org-agenda-use-time-grid " Grid" "")
  5564. (if org-agenda-show-log " Log" "")))
  5565. (force-mode-line-update))
  5566. (defun org-agenda-post-command-hook ()
  5567. (and (eolp) (not (bolp)) (backward-char 1))
  5568. (if (and org-agenda-follow-mode
  5569. (get-text-property (point) 'org-marker))
  5570. (org-agenda-show)))
  5571. (defvar org-disable-agenda-to-diary nil) ;Dynamically-scoped param.
  5572. (defun org-get-entries-from-diary (date)
  5573. "Get the (Emacs Calendar) diary entries for DATE."
  5574. (let* ((fancy-diary-buffer "*temporary-fancy-diary-buffer*")
  5575. (diary-display-hook '(fancy-diary-display))
  5576. (list-diary-entries-hook
  5577. (cons 'org-diary-default-entry list-diary-entries-hook))
  5578. (diary-file-name-prefix-function nil) ; turn this feature off
  5579. (diary-modify-entry-list-string-function 'org-modify-diary-entry-string)
  5580. entries
  5581. (org-disable-agenda-to-diary t))
  5582. (save-excursion
  5583. (save-window-excursion
  5584. (list-diary-entries date 1))) ;; Keep this name for now, compatibility
  5585. (if (not (get-buffer fancy-diary-buffer))
  5586. (setq entries nil)
  5587. (with-current-buffer fancy-diary-buffer
  5588. (setq buffer-read-only nil)
  5589. (if (= (point-max) 1)
  5590. ;; No entries
  5591. (setq entries nil)
  5592. ;; Omit the date and other unnecessary stuff
  5593. (org-agenda-cleanup-fancy-diary)
  5594. ;; Add prefix to each line and extend the text properties
  5595. (if (= (point-max) 1)
  5596. (setq entries nil)
  5597. (setq entries (buffer-substring (point-min) (- (point-max) 1)))))
  5598. (set-buffer-modified-p nil)
  5599. (kill-buffer fancy-diary-buffer)))
  5600. (when entries
  5601. (setq entries (org-split-string entries "\n"))
  5602. (setq entries
  5603. (mapcar
  5604. (lambda (x)
  5605. (setq x (org-format-agenda-item "" x "Diary" nil 'time))
  5606. ;; Extend the text properties to the beginning of the line
  5607. (org-add-props x (text-properties-at (1- (length x)) x)))
  5608. entries)))))
  5609. (defun org-agenda-cleanup-fancy-diary ()
  5610. "Remove unwanted stuff in buffer created by `fancy-diary-display'.
  5611. This gets rid of the date, the underline under the date, and
  5612. the dummy entry installed by `org-mode' to ensure non-empty diary for each
  5613. date. It also removes lines that contain only whitespace."
  5614. (goto-char (point-min))
  5615. (if (looking-at ".*?:[ \t]*")
  5616. (progn
  5617. (replace-match "")
  5618. (re-search-forward "\n=+$" nil t)
  5619. (replace-match "")
  5620. (while (re-search-backward "^ +\n?" nil t) (replace-match "")))
  5621. (re-search-forward "\n=+$" nil t)
  5622. (delete-region (point-min) (min (point-max) (1+ (match-end 0)))))
  5623. (goto-char (point-min))
  5624. (while (re-search-forward "^ +\n" nil t)
  5625. (replace-match ""))
  5626. (goto-char (point-min))
  5627. (if (re-search-forward "^Org-mode dummy\n?" nil t)
  5628. (replace-match "")))
  5629. ;; Make sure entries from the diary have the right text properties.
  5630. (eval-after-load "diary-lib"
  5631. '(if (boundp 'diary-modify-entry-list-string-function)
  5632. ;; We can rely on the hook, nothing to do
  5633. nil
  5634. ;; Hook not avaiable, must use advice to make this work
  5635. (defadvice add-to-diary-list (before org-mark-diary-entry activate)
  5636. "Make the position visible."
  5637. (if (and org-disable-agenda-to-diary ;; called from org-agenda
  5638. (stringp string)
  5639. buffer-file-name)
  5640. (setq string (org-modify-diary-entry-string string))))))
  5641. (defun org-modify-diary-entry-string (string)
  5642. "Add text properties to string, allowing org-mode to act on it."
  5643. (org-add-props string nil
  5644. 'mouse-face 'highlight
  5645. 'keymap org-agenda-keymap
  5646. 'help-echo (format "mouse-2 or RET jump to diary file %s"
  5647. (abbreviate-file-name buffer-file-name))
  5648. 'org-agenda-diary-link t
  5649. 'org-marker (org-agenda-new-marker (point-at-bol))))
  5650. (defun org-diary-default-entry ()
  5651. "Add a dummy entry to the diary.
  5652. Needed to avoid empty dates which mess up holiday display."
  5653. ;; Catch the error if dealing with the new add-to-diary-alist
  5654. (when org-disable-agenda-to-diary
  5655. (condition-case nil
  5656. (add-to-diary-list original-date "Org-mode dummy" "")
  5657. (error
  5658. (add-to-diary-list original-date "Org-mode dummy" "" nil)))))
  5659. (defun org-cycle-agenda-files ()
  5660. "Cycle through the files in `org-agenda-files'.
  5661. If the current buffer visits an agenda file, find the next one in the list.
  5662. If the current buffer does not, find the first agenda file."
  5663. (interactive)
  5664. (let* ((fs (org-agenda-files t))
  5665. (files (append fs (list (car fs))))
  5666. (tcf (if buffer-file-name (file-truename buffer-file-name)))
  5667. file)
  5668. (unless files (error "No agenda files"))
  5669. (catch 'exit
  5670. (while (setq file (pop files))
  5671. (if (equal (file-truename file) tcf)
  5672. (when (car files)
  5673. (find-file (car files))
  5674. (throw 'exit t))))
  5675. (find-file (car fs)))))
  5676. (defun org-agenda-file-to-end ()
  5677. "Move/add the current file to the end of the agenda file list.
  5678. If the file is not present in the list, it is appended to the list. If it is
  5679. present, it is moved there."
  5680. (interactive)
  5681. (org-agenda-file-to-front 'to-end))
  5682. (defun org-agenda-file-to-front (&optional to-end)
  5683. "Move/add the current file to the top of the agenda file list.
  5684. If the file is not present in the list, it is added to the front. If it is
  5685. present, it is moved there. With optional argument TO-END, add/move to the
  5686. end of the list."
  5687. (interactive "P")
  5688. (let ((file-alist (mapcar (lambda (x)
  5689. (cons (file-truename x) x))
  5690. (org-agenda-files t)))
  5691. (ctf (file-truename buffer-file-name))
  5692. x had)
  5693. (setq x (assoc ctf file-alist) had x)
  5694. (if (not x) (setq x (cons ctf (abbreviate-file-name buffer-file-name))))
  5695. (if to-end
  5696. (setq file-alist (append (delq x file-alist) (list x)))
  5697. (setq file-alist (cons x (delq x file-alist))))
  5698. (org-store-new-agenda-file-list (mapcar 'cdr file-alist))
  5699. (org-install-agenda-files-menu)
  5700. (message "File %s to %s of agenda file list"
  5701. (if had "moved" "added") (if to-end "end" "front"))))
  5702. (defun org-remove-file (&optional file)
  5703. "Remove current file from the list of files in variable `org-agenda-files'.
  5704. These are the files which are being checked for agenda entries.
  5705. Optional argument FILE means, use this file instead of the current."
  5706. (interactive)
  5707. (let* ((file (or file buffer-file-name))
  5708. (true-file (file-truename file))
  5709. (afile (abbreviate-file-name file))
  5710. (files (delq nil (mapcar
  5711. (lambda (x)
  5712. (if (equal true-file
  5713. (file-truename x))
  5714. nil x))
  5715. (org-agenda-files t)))))
  5716. (if (not (= (length files) (length (org-agenda-files t))))
  5717. (progn
  5718. (org-store-new-agenda-file-list files)
  5719. (org-install-agenda-files-menu)
  5720. (message "Removed file: %s" afile))
  5721. (message "File was not in list: %s" afile))))
  5722. (defun org-file-menu-entry (file)
  5723. (vector file (list 'find-file file) t))
  5724. (defun org-get-all-dates (beg end &optional no-ranges force-today inactive empty)
  5725. "Return a list of all relevant day numbers from BEG to END buffer positions.
  5726. If NO-RANGES is non-nil, include only the start and end dates of a range,
  5727. not every single day in the range. If FORCE-TODAY is non-nil, make
  5728. sure that TODAY is included in the list. If INACTIVE is non-nil, also
  5729. inactive time stamps (those in square brackets) are included.
  5730. When EMPTY is non-nil, also include days without any entries."
  5731. (let ((re (if inactive org-ts-regexp-both org-ts-regexp))
  5732. dates dates1 date day day1 day2 ts1 ts2)
  5733. (if force-today
  5734. (setq dates (list (time-to-days (current-time)))))
  5735. (save-excursion
  5736. (goto-char beg)
  5737. (while (re-search-forward re end t)
  5738. (setq day (time-to-days (org-time-string-to-time
  5739. (substring (match-string 1) 0 10))))
  5740. (or (memq day dates) (push day dates)))
  5741. (unless no-ranges
  5742. (goto-char beg)
  5743. (while (re-search-forward org-tr-regexp end t)
  5744. (setq ts1 (substring (match-string 1) 0 10)
  5745. ts2 (substring (match-string 2) 0 10)
  5746. day1 (time-to-days (org-time-string-to-time ts1))
  5747. day2 (time-to-days (org-time-string-to-time ts2)))
  5748. (while (< (setq day1 (1+ day1)) day2)
  5749. (or (memq day1 dates) (push day1 dates)))))
  5750. (setq dates (sort dates '<))
  5751. (when empty
  5752. (while (setq day (pop dates))
  5753. (setq day2 (car dates))
  5754. (push day dates1)
  5755. (when (and day2 empty)
  5756. (if (or (eq empty t)
  5757. (and (numberp empty) (<= (- day2 day) empty)))
  5758. (while (< (setq day (1+ day)) day2)
  5759. (push (list day) dates1))
  5760. (push (cons :omitted (- day2 day)) dates1))))
  5761. (setq dates (nreverse dates1)))
  5762. dates)))
  5763. ;;;###autoload
  5764. (defun org-diary (&rest args)
  5765. "Return diary information from org-files.
  5766. This function can be used in a \"sexp\" diary entry in the Emacs calendar.
  5767. It accesses org files and extracts information from those files to be
  5768. listed in the diary. The function accepts arguments specifying what
  5769. items should be listed. The following arguments are allowed:
  5770. :timestamp List the headlines of items containing a date stamp or
  5771. date range matching the selected date. Deadlines will
  5772. also be listed, on the expiration day.
  5773. :deadline List any deadlines past due, or due within
  5774. `org-deadline-warning-days'. The listing occurs only
  5775. in the diary for *today*, not at any other date. If
  5776. an entry is marked DONE, it is no longer listed.
  5777. :scheduled List all items which are scheduled for the given date.
  5778. The diary for *today* also contains items which were
  5779. scheduled earlier and are not yet marked DONE.
  5780. :todo List all TODO items from the org-file. This may be a
  5781. long list - so this is not turned on by default.
  5782. Like deadlines, these entries only show up in the
  5783. diary for *today*, not at any other date.
  5784. The call in the diary file should look like this:
  5785. &%%(org-diary) ~/path/to/some/orgfile.org
  5786. Use a separate line for each org file to check. Or, if you omit the file name,
  5787. all files listed in `org-agenda-files' will be checked automatically:
  5788. &%%(org-diary)
  5789. If you don't give any arguments (as in the example above), the default
  5790. arguments (:deadline :scheduled :timestamp) are used. So the example above may
  5791. also be written as
  5792. &%%(org-diary :deadline :timestamp :scheduled)
  5793. The function expects the lisp variables `entry' and `date' to be provided
  5794. by the caller, because this is how the calendar works. Don't use this
  5795. function from a program - use `org-agenda-get-day-entries' instead."
  5796. (org-agenda-maybe-reset-markers)
  5797. (org-compile-prefix-format org-agenda-prefix-format)
  5798. (setq args (or args '(:deadline :scheduled :timestamp)))
  5799. (let* ((files (if (and entry (stringp entry) (string-match "\\S-" entry))
  5800. (list entry)
  5801. (org-agenda-files t)))
  5802. file rtn results)
  5803. ;; If this is called during org-agenda, don't return any entries to
  5804. ;; the calendar. Org Agenda will list these entries itself.
  5805. (if org-disable-agenda-to-diary (setq files nil))
  5806. (while (setq file (pop files))
  5807. (setq rtn (apply 'org-agenda-get-day-entries file date args))
  5808. (setq results (append results rtn)))
  5809. (if results
  5810. (concat (org-finalize-agenda-entries results) "\n"))))
  5811. (defvar org-category-table nil)
  5812. (defun org-get-category-table ()
  5813. "Get the table of categories and positions in current buffer."
  5814. (let (tbl)
  5815. (save-excursion
  5816. (goto-char (point-min))
  5817. (while (re-search-forward "\\(^\\|\r\\)#\\+CATEGORY:[ \t]*\\(.*\\)" nil t)
  5818. (push (cons (point) (org-trim (match-string 2))) tbl)))
  5819. tbl))
  5820. (defun org-get-category (&optional pos)
  5821. "Get the category applying to position POS."
  5822. (if (not org-category-table)
  5823. (cond
  5824. ((null org-category)
  5825. (setq org-category
  5826. (if buffer-file-name
  5827. (file-name-sans-extension
  5828. (file-name-nondirectory buffer-file-name))
  5829. "???")))
  5830. ((symbolp org-category) (symbol-name org-category))
  5831. (t org-category))
  5832. (let ((tbl org-category-table)
  5833. (pos (or pos (point))))
  5834. (while (and tbl (> (caar tbl) pos))
  5835. (pop tbl))
  5836. (or (cdar tbl) (cdr (nth (1- (length org-category-table))
  5837. org-category-table))))))
  5838. (defun org-agenda-get-day-entries (file date &rest args)
  5839. "Does the work for `org-diary' and `org-agenda'.
  5840. FILE is the path to a file to be checked for entries. DATE is date like
  5841. the one returned by `calendar-current-date'. ARGS are symbols indicating
  5842. which kind of entries should be extracted. For details about these, see
  5843. the documentation of `org-diary'."
  5844. (setq args (or args '(:deadline :scheduled :timestamp)))
  5845. (let* ((org-startup-with-deadline-check nil)
  5846. (org-startup-folded nil)
  5847. (org-startup-align-all-tables nil)
  5848. (buffer (if (file-exists-p file)
  5849. (org-get-agenda-file-buffer file)
  5850. (error "No such file %s" file)))
  5851. arg results rtn)
  5852. (if (not buffer)
  5853. ;; If file does not exist, make sure an error message ends up in diary
  5854. (list (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  5855. (with-current-buffer buffer
  5856. (unless (eq major-mode 'org-mode)
  5857. (error "Agenda file %s is not in `org-mode'" file))
  5858. (setq org-category-table (org-get-category-table))
  5859. (let ((case-fold-search nil))
  5860. (save-excursion
  5861. (save-restriction
  5862. (if org-respect-restriction
  5863. (if (org-region-active-p)
  5864. ;; Respect a region to restrict search
  5865. (narrow-to-region (region-beginning) (region-end)))
  5866. ;; If we work for the calendar or many files,
  5867. ;; get rid of any restriction
  5868. (widen))
  5869. ;; The way we repeatedly append to `results' makes it O(n^2) :-(
  5870. (while (setq arg (pop args))
  5871. (cond
  5872. ((and (eq arg :todo)
  5873. (equal date (calendar-current-date)))
  5874. (setq rtn (org-agenda-get-todos))
  5875. (setq results (append results rtn)))
  5876. ((eq arg :timestamp)
  5877. (setq rtn (org-agenda-get-blocks))
  5878. (setq results (append results rtn))
  5879. (setq rtn (org-agenda-get-timestamps))
  5880. (setq results (append results rtn)))
  5881. ((eq arg :scheduled)
  5882. (setq rtn (org-agenda-get-scheduled))
  5883. (setq results (append results rtn)))
  5884. ((eq arg :closed)
  5885. (setq rtn (org-agenda-get-closed))
  5886. (setq results (append results rtn)))
  5887. ((and (eq arg :deadline)
  5888. (equal date (calendar-current-date)))
  5889. (setq rtn (org-agenda-get-deadlines))
  5890. (setq results (append results rtn))))))))
  5891. results))))
  5892. (defun org-entry-is-done-p ()
  5893. "Is the current entry marked DONE?"
  5894. (save-excursion
  5895. (and (re-search-backward "[\r\n]\\*" nil t)
  5896. (looking-at org-nl-done-regexp))))
  5897. (defun org-at-date-range-p ()
  5898. "Is the cursor inside a date range?"
  5899. (interactive)
  5900. (save-excursion
  5901. (catch 'exit
  5902. (let ((pos (point)))
  5903. (skip-chars-backward "^<\r\n")
  5904. (skip-chars-backward "<")
  5905. (and (looking-at org-tr-regexp)
  5906. (>= (match-end 0) pos)
  5907. (throw 'exit t))
  5908. (skip-chars-backward "^<\r\n")
  5909. (skip-chars-backward "<")
  5910. (and (looking-at org-tr-regexp)
  5911. (>= (match-end 0) pos)
  5912. (throw 'exit t)))
  5913. nil)))
  5914. (defun org-agenda-get-todos ()
  5915. "Return the TODO information for agenda display."
  5916. (let* ((props (list 'face nil
  5917. 'done-face 'org-done
  5918. 'org-not-done-regexp org-not-done-regexp
  5919. 'mouse-face 'highlight
  5920. 'keymap org-agenda-keymap
  5921. 'help-echo
  5922. (format "mouse-2 or RET jump to org file %s"
  5923. (abbreviate-file-name buffer-file-name))))
  5924. (regexp (concat "[\n\r]\\*+ *\\("
  5925. (if org-select-this-todo-keyword
  5926. (concat "\\<\\(" org-select-this-todo-keyword
  5927. "\\)\\>")
  5928. org-not-done-regexp)
  5929. "[^\n\r]*\\)"))
  5930. (sched-re (concat ".*\n?.*?" org-scheduled-time-regexp))
  5931. marker priority category tags
  5932. ee txt)
  5933. (goto-char (point-min))
  5934. (while (re-search-forward regexp nil t)
  5935. (when (not (and org-agenda-todo-ignore-scheduled
  5936. (save-match-data (looking-at sched-re))))
  5937. (goto-char (match-beginning 1))
  5938. (setq marker (org-agenda-new-marker (1+ (match-beginning 0)))
  5939. category (org-get-category)
  5940. tags (org-get-tags-at (point))
  5941. txt (org-format-agenda-item "" (match-string 1) category tags)
  5942. priority
  5943. (+ (org-get-priority txt)
  5944. (if org-todo-kwd-priority-p
  5945. (- org-todo-kwd-max-priority -2
  5946. (length
  5947. (member (match-string 2) org-todo-keywords)))
  5948. 1)))
  5949. (org-add-props txt props
  5950. 'org-marker marker 'org-hd-marker marker
  5951. 'priority priority 'category category)
  5952. (push txt ee))
  5953. (if org-agenda-todo-list-sublevels
  5954. (goto-char (match-end 1))
  5955. (org-end-of-subtree 'invisible)))
  5956. (nreverse ee)))
  5957. (defconst org-agenda-no-heading-message
  5958. "No heading for this item in buffer or region.")
  5959. (defun org-agenda-get-timestamps ()
  5960. "Return the date stamp information for agenda display."
  5961. (let* ((props (list 'face nil
  5962. 'org-not-done-regexp org-not-done-regexp
  5963. 'mouse-face 'highlight
  5964. 'keymap org-agenda-keymap
  5965. 'help-echo
  5966. (format "mouse-2 or RET jump to org file %s"
  5967. (abbreviate-file-name buffer-file-name))))
  5968. (regexp (regexp-quote
  5969. (substring
  5970. (format-time-string
  5971. (car org-time-stamp-formats)
  5972. (apply 'encode-time ; DATE bound by calendar
  5973. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  5974. 0 11)))
  5975. marker hdmarker deadlinep scheduledp donep tmp priority category
  5976. ee txt timestr tags)
  5977. (goto-char (point-min))
  5978. (while (re-search-forward regexp nil t)
  5979. (if (not (save-match-data (org-at-date-range-p)))
  5980. (progn
  5981. (setq marker (org-agenda-new-marker (match-beginning 0))
  5982. category (org-get-category (match-beginning 0))
  5983. tmp (buffer-substring (max (point-min)
  5984. (- (match-beginning 0)
  5985. org-ds-keyword-length))
  5986. (match-beginning 0))
  5987. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  5988. deadlinep (string-match org-deadline-regexp tmp)
  5989. scheduledp (string-match org-scheduled-regexp tmp)
  5990. donep (org-entry-is-done-p))
  5991. (if (string-match ">" timestr)
  5992. ;; substring should only run to end of time stamp
  5993. (setq timestr (substring timestr 0 (match-end 0))))
  5994. (save-excursion
  5995. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  5996. (progn
  5997. (goto-char (match-end 1))
  5998. (setq hdmarker (org-agenda-new-marker)
  5999. tags (org-get-tags-at))
  6000. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6001. (setq txt (org-format-agenda-item
  6002. (format "%s%s"
  6003. (if deadlinep "Deadline: " "")
  6004. (if scheduledp "Scheduled: " ""))
  6005. (match-string 1) category tags timestr)))
  6006. (setq txt org-agenda-no-heading-message))
  6007. (setq priority (org-get-priority txt))
  6008. (org-add-props txt props
  6009. 'org-marker marker 'org-hd-marker hdmarker)
  6010. (if deadlinep
  6011. (org-add-props txt nil
  6012. 'face (if donep 'org-done 'org-warning)
  6013. 'undone-face 'org-warning 'done-face 'org-done
  6014. 'category category 'priority (+ 100 priority))
  6015. (if scheduledp
  6016. (org-add-props txt nil
  6017. 'face 'org-scheduled-today
  6018. 'undone-face 'org-scheduled-today 'done-face 'org-done
  6019. 'category category 'priority (+ 99 priority))
  6020. (org-add-props txt nil 'priority priority 'category category)))
  6021. (push txt ee))
  6022. (outline-next-heading))))
  6023. (nreverse ee)))
  6024. (defun org-agenda-get-closed ()
  6025. "Return the logged TODO entries for agenda display."
  6026. (let* ((props (list 'mouse-face 'highlight
  6027. 'org-not-done-regexp org-not-done-regexp
  6028. 'keymap org-agenda-keymap
  6029. 'help-echo
  6030. (format "mouse-2 or RET jump to org file %s"
  6031. (abbreviate-file-name buffer-file-name))))
  6032. (regexp (concat
  6033. "\\<" org-closed-string " *\\["
  6034. (regexp-quote
  6035. (substring
  6036. (format-time-string
  6037. (car org-time-stamp-formats)
  6038. (apply 'encode-time ; DATE bound by calendar
  6039. (list 0 0 0 (nth 1 date) (car date) (nth 2 date))))
  6040. 1 11))))
  6041. marker hdmarker priority category tags
  6042. ee txt timestr)
  6043. (goto-char (point-min))
  6044. (while (re-search-forward regexp nil t)
  6045. (if (not (save-match-data (org-at-date-range-p)))
  6046. (progn
  6047. (setq marker (org-agenda-new-marker (match-beginning 0))
  6048. category (org-get-category (match-beginning 0))
  6049. timestr (buffer-substring (match-beginning 0) (point-at-eol))
  6050. ;; donep (org-entry-is-done-p)
  6051. )
  6052. (if (string-match "\\]" timestr)
  6053. ;; substring should only run to end of time stamp
  6054. (setq timestr (substring timestr 0 (match-end 0))))
  6055. (save-excursion
  6056. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  6057. (progn
  6058. (goto-char (match-end 1))
  6059. (setq hdmarker (org-agenda-new-marker)
  6060. tags (org-get-tags-at))
  6061. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6062. (setq txt (org-format-agenda-item
  6063. "Closed: "
  6064. (match-string 1) category tags timestr)))
  6065. (setq txt org-agenda-no-heading-message))
  6066. (setq priority 100000)
  6067. (org-add-props txt props
  6068. 'org-marker marker 'org-hd-marker hdmarker 'face 'org-done
  6069. 'priority priority 'category category
  6070. 'undone-face 'org-warning 'done-face 'org-done)
  6071. (push txt ee))
  6072. (outline-next-heading))))
  6073. (nreverse ee)))
  6074. (defun org-agenda-get-deadlines ()
  6075. "Return the deadline information for agenda display."
  6076. (let* ((wdays org-deadline-warning-days)
  6077. (props (list 'mouse-face 'highlight
  6078. 'org-not-done-regexp org-not-done-regexp
  6079. 'keymap org-agenda-keymap
  6080. 'help-echo
  6081. (format "mouse-2 or RET jump to org file %s"
  6082. (abbreviate-file-name buffer-file-name))))
  6083. (regexp org-deadline-time-regexp)
  6084. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  6085. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  6086. d2 diff pos pos1 category tags
  6087. ee txt head face)
  6088. (goto-char (point-min))
  6089. (while (re-search-forward regexp nil t)
  6090. (setq pos (1- (match-beginning 1))
  6091. d2 (time-to-days
  6092. (org-time-string-to-time (match-string 1)))
  6093. diff (- d2 d1))
  6094. ;; When to show a deadline in the calendar:
  6095. ;; If the expiration is within wdays warning time.
  6096. ;; Past-due deadlines are only shown on the current date
  6097. (if (and (< diff wdays) todayp (not (= diff 0)))
  6098. (save-excursion
  6099. (setq category (org-get-category))
  6100. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  6101. (progn
  6102. (goto-char (match-end 0))
  6103. (setq pos1 (match-end 1))
  6104. (setq tags (org-get-tags-at pos1))
  6105. (setq head (buffer-substring-no-properties
  6106. (point)
  6107. (progn (skip-chars-forward "^\r\n")
  6108. (point))))
  6109. (if (string-match org-looking-at-done-regexp head)
  6110. (setq txt nil)
  6111. (setq txt (org-format-agenda-item
  6112. (format "In %3d d.: " diff) head category tags))))
  6113. (setq txt org-agenda-no-heading-message))
  6114. (when txt
  6115. (setq face (cond ((<= diff 0) 'org-warning)
  6116. ((<= diff 5) 'org-upcoming-deadline)
  6117. (t nil)))
  6118. (org-add-props txt props
  6119. 'org-marker (org-agenda-new-marker pos)
  6120. 'org-hd-marker (org-agenda-new-marker pos1)
  6121. 'priority (+ (- 10 diff) (org-get-priority txt))
  6122. 'category category
  6123. 'face face 'undone-face face 'done-face 'org-done)
  6124. (push txt ee)))))
  6125. ee))
  6126. (defun org-agenda-get-scheduled ()
  6127. "Return the scheduled information for agenda display."
  6128. (let* ((props (list 'face 'org-scheduled-previously
  6129. 'org-not-done-regexp org-not-done-regexp
  6130. 'undone-face 'org-scheduled-previously
  6131. 'done-face 'org-done
  6132. 'mouse-face 'highlight
  6133. 'keymap org-agenda-keymap
  6134. 'help-echo
  6135. (format "mouse-2 or RET jump to org file %s"
  6136. (abbreviate-file-name buffer-file-name))))
  6137. (regexp org-scheduled-time-regexp)
  6138. (todayp (equal date (calendar-current-date))) ; DATE bound by calendar
  6139. (d1 (calendar-absolute-from-gregorian date)) ; DATE bound by calendar
  6140. d2 diff pos pos1 category tags
  6141. ee txt head)
  6142. (goto-char (point-min))
  6143. (while (re-search-forward regexp nil t)
  6144. (setq pos (1- (match-beginning 1))
  6145. d2 (time-to-days
  6146. (org-time-string-to-time (match-string 1)))
  6147. diff (- d2 d1))
  6148. ;; When to show a scheduled item in the calendar:
  6149. ;; If it is on or past the date.
  6150. (if (and (< diff 0) todayp)
  6151. (save-excursion
  6152. (setq category (org-get-category))
  6153. (if (re-search-backward "\\(^\\|\r\\)\\*+[ \t]*" nil t)
  6154. (progn
  6155. (goto-char (match-end 0))
  6156. (setq pos1 (match-end 1))
  6157. (setq tags (org-get-tags-at))
  6158. (setq head (buffer-substring-no-properties
  6159. (point)
  6160. (progn (skip-chars-forward "^\r\n") (point))))
  6161. (if (string-match org-looking-at-done-regexp head)
  6162. (setq txt nil)
  6163. (setq txt (org-format-agenda-item
  6164. (format "Sched.%2dx: " (- 1 diff)) head
  6165. category tags))))
  6166. (setq txt org-agenda-no-heading-message))
  6167. (when txt
  6168. (org-add-props txt props
  6169. 'org-marker (org-agenda-new-marker pos)
  6170. 'org-hd-marker (org-agenda-new-marker pos1)
  6171. 'priority (+ (- 5 diff) (org-get-priority txt))
  6172. 'category category)
  6173. (push txt ee)))))
  6174. ee))
  6175. (defun org-agenda-get-blocks ()
  6176. "Return the date-range information for agenda display."
  6177. (let* ((props (list 'face nil
  6178. 'org-not-done-regexp org-not-done-regexp
  6179. 'mouse-face 'highlight
  6180. 'keymap org-agenda-keymap
  6181. 'help-echo
  6182. (format "mouse-2 or RET jump to org file %s"
  6183. (abbreviate-file-name buffer-file-name))))
  6184. (regexp org-tr-regexp)
  6185. (d0 (calendar-absolute-from-gregorian date))
  6186. marker hdmarker ee txt d1 d2 s1 s2 timestr category tags)
  6187. (goto-char (point-min))
  6188. (while (re-search-forward regexp nil t)
  6189. (setq timestr (match-string 0)
  6190. s1 (match-string 1)
  6191. s2 (match-string 2)
  6192. d1 (time-to-days (org-time-string-to-time s1))
  6193. d2 (time-to-days (org-time-string-to-time s2)))
  6194. (if (and (> (- d0 d1) -1) (> (- d2 d0) -1))
  6195. ;; Only allow days between the limits, because the normal
  6196. ;; date stamps will catch the limits.
  6197. (save-excursion
  6198. (setq marker (org-agenda-new-marker (point)))
  6199. (setq category (org-get-category))
  6200. (if (re-search-backward "\\(^\\|\r\\)\\*+" nil t)
  6201. (progn
  6202. (setq hdmarker (org-agenda-new-marker (match-end 1)))
  6203. (goto-char (match-end 1))
  6204. (setq tags (org-get-tags-at))
  6205. (looking-at "\\*+[ \t]*\\([^\r\n]+\\)")
  6206. (setq txt (org-format-agenda-item
  6207. (format (if (= d1 d2) "" "(%d/%d): ")
  6208. (1+ (- d0 d1)) (1+ (- d2 d1)))
  6209. (match-string 1) category tags
  6210. (if (= d0 d1) timestr))))
  6211. (setq txt org-agenda-no-heading-message))
  6212. (org-add-props txt props
  6213. 'org-marker marker 'org-hd-marker hdmarker
  6214. 'priority (org-get-priority txt) 'category category)
  6215. (push txt ee)))
  6216. (outline-next-heading))
  6217. ;; Sort the entries by expiration date.
  6218. (nreverse ee)))
  6219. (defconst org-plain-time-of-day-regexp
  6220. (concat
  6221. "\\(\\<[012]?[0-9]"
  6222. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  6223. "\\(--?"
  6224. "\\(\\<[012]?[0-9]"
  6225. "\\(\\(:\\([0-5][0-9]\\([AaPp][Mm]\\)?\\)\\)\\|\\([AaPp][Mm]\\)\\)\\>\\)"
  6226. "\\)?")
  6227. "Regular expression to match a plain time or time range.
  6228. Examples: 11:45 or 8am-13:15 or 2:45-2:45pm. After a match, the following
  6229. groups carry important information:
  6230. 0 the full match
  6231. 1 the first time, range or not
  6232. 8 the second time, if it is a range.")
  6233. (defconst org-stamp-time-of-day-regexp
  6234. (concat
  6235. "<\\([0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\} +[a-zA-Z]+ +\\)"
  6236. "\\([012][0-9]:[0-5][0-9]\\)>"
  6237. "\\(--?"
  6238. "<\\1\\([012][0-9]:[0-5][0-9]\\)>\\)?")
  6239. "Regular expression to match a timestamp time or time range.
  6240. After a match, the following groups carry important information:
  6241. 0 the full match
  6242. 1 date plus weekday, for backreferencing to make sure both times on same day
  6243. 2 the first time, range or not
  6244. 4 the second time, if it is a range.")
  6245. (defvar org-prefix-has-time nil
  6246. "A flag, set by `org-compile-prefix-format'.
  6247. The flag is set if the currently compiled format contains a `%t'.")
  6248. (defvar org-prefix-has-tag nil
  6249. "A flag, set by `org-compile-prefix-format'.
  6250. The flag is set if the currently compiled format contains a `%T'.")
  6251. (defun org-format-agenda-item (extra txt &optional category tags dotime noprefix)
  6252. "Format TXT to be inserted into the agenda buffer.
  6253. In particular, it adds the prefix and corresponding text properties. EXTRA
  6254. must be a string and replaces the `%s' specifier in the prefix format.
  6255. CATEGORY (string, symbol or nil) may be used to overrule the default
  6256. category taken from local variable or file name. It will replace the `%c'
  6257. specifier in the format. DOTIME, when non-nil, indicates that a
  6258. time-of-day should be extracted from TXT for sorting of this entry, and for
  6259. the `%t' specifier in the format. When DOTIME is a string, this string is
  6260. searched for a time before TXT is. NOPREFIX is a flag and indicates that
  6261. only the correctly processes TXT should be returned - this is used by
  6262. `org-agenda-change-all-lines'. TAG can be the tag of the headline."
  6263. (save-match-data
  6264. ;; Diary entries sometimes have extra whitespace at the beginning
  6265. (if (string-match "^ +" txt) (setq txt (replace-match "" nil nil txt)))
  6266. (let* ((category (or category
  6267. org-category
  6268. (if buffer-file-name
  6269. (file-name-sans-extension
  6270. (file-name-nondirectory buffer-file-name))
  6271. "")))
  6272. (tag (if tags (nth (1- (length tags)) tags) ""))
  6273. time ;; needed for the eval of the prefix format
  6274. (ts (if dotime (concat (if (stringp dotime) dotime "") txt)))
  6275. (time-of-day (and dotime (org-get-time-of-day ts)))
  6276. stamp plain s0 s1 s2 rtn)
  6277. (when (and dotime time-of-day org-prefix-has-time)
  6278. ;; Extract starting and ending time and move them to prefix
  6279. (when (or (setq stamp (string-match org-stamp-time-of-day-regexp ts))
  6280. (setq plain (string-match org-plain-time-of-day-regexp ts)))
  6281. (setq s0 (match-string 0 ts)
  6282. s1 (match-string (if plain 1 2) ts)
  6283. s2 (match-string (if plain 8 4) ts))
  6284. ;; If the times are in TXT (not in DOTIMES), and the prefix will list
  6285. ;; them, we might want to remove them there to avoid duplication.
  6286. ;; The user can turn this off with a variable.
  6287. (if (and org-agenda-remove-times-when-in-prefix (or stamp plain)
  6288. (string-match (concat (regexp-quote s0) " *") txt)
  6289. (if (eq org-agenda-remove-times-when-in-prefix 'beg)
  6290. (= (match-beginning 0) 0)
  6291. t))
  6292. (setq txt (replace-match "" nil nil txt))))
  6293. ;; Normalize the time(s) to 24 hour
  6294. (if s1 (setq s1 (org-get-time-of-day s1 'string t)))
  6295. (if s2 (setq s2 (org-get-time-of-day s2 'string t))))
  6296. (when (string-match "\\([ \t]+\\)\\(:[a-zA-Z_@0-9:]+:\\)[ \t]*$" txt)
  6297. ;; Tags are in the string
  6298. (if (or (eq org-agenda-remove-tags-when-in-prefix t)
  6299. (and org-agenda-remove-tags-when-in-prefix
  6300. org-prefix-has-tag))
  6301. (setq txt (replace-match "" t t txt))
  6302. (setq txt (replace-match
  6303. (concat (make-string (max (- 50 (length txt)) 1) ?\ )
  6304. (match-string 2 txt))
  6305. t t txt))))
  6306. ;; Create the final string
  6307. (if noprefix
  6308. (setq rtn txt)
  6309. ;; Prepare the variables needed in the eval of the compiled format
  6310. (setq time (cond (s2 (concat s1 "-" s2))
  6311. (s1 (concat s1 "......"))
  6312. (t ""))
  6313. extra (or extra "")
  6314. category (if (symbolp category) (symbol-name category) category))
  6315. ;; Evaluate the compiled format
  6316. (setq rtn (concat (eval org-prefix-format-compiled) txt)))
  6317. ;; And finally add the text properties
  6318. (org-add-props rtn nil
  6319. 'category (downcase category) 'tags tags
  6320. 'prefix-length (- (length rtn) (length txt))
  6321. 'time-of-day time-of-day
  6322. 'dotime dotime))))
  6323. (defun org-agenda-add-time-grid-maybe (list ndays todayp)
  6324. (catch 'exit
  6325. (cond ((not org-agenda-use-time-grid) (throw 'exit list))
  6326. ((and todayp (member 'today (car org-agenda-time-grid))))
  6327. ((and (= ndays 1) (member 'daily (car org-agenda-time-grid))))
  6328. ((member 'weekly (car org-agenda-time-grid)))
  6329. (t (throw 'exit list)))
  6330. (let* ((have (delq nil (mapcar
  6331. (lambda (x) (get-text-property 1 'time-of-day x))
  6332. list)))
  6333. (string (nth 1 org-agenda-time-grid))
  6334. (gridtimes (nth 2 org-agenda-time-grid))
  6335. (req (car org-agenda-time-grid))
  6336. (remove (member 'remove-match req))
  6337. new time)
  6338. (if (and (member 'require-timed req) (not have))
  6339. ;; don't show empty grid
  6340. (throw 'exit list))
  6341. (while (setq time (pop gridtimes))
  6342. (unless (and remove (member time have))
  6343. (setq time (int-to-string time))
  6344. (push (org-format-agenda-item
  6345. nil string "" nil
  6346. (concat (substring time 0 -2) ":" (substring time -2)))
  6347. new)
  6348. (put-text-property
  6349. 1 (length (car new)) 'face 'org-time-grid (car new))))
  6350. (if (member 'time-up org-agenda-sorting-strategy)
  6351. (append new list)
  6352. (append list new)))))
  6353. (defun org-compile-prefix-format (format)
  6354. "Compile the prefix format into a Lisp form that can be evaluated.
  6355. The resulting form is returned and stored in the variable
  6356. `org-prefix-format-compiled'."
  6357. (setq org-prefix-has-time nil org-prefix-has-tag nil)
  6358. (let ((start 0) varform vars var (s format)e c f opt)
  6359. (while (string-match "%\\(\\?\\)?\\([-+]?[0-9.]*\\)\\([ .;,:!?=|/<>]?\\)\\([cts]\\)"
  6360. s start)
  6361. (setq var (cdr (assoc (match-string 4 s)
  6362. '(("c" . category) ("t" . time) ("s" . extra)
  6363. ("T" . tag))))
  6364. c (or (match-string 3 s) "")
  6365. opt (match-beginning 1)
  6366. start (1+ (match-beginning 0)))
  6367. (if (equal var 'time) (setq org-prefix-has-time t))
  6368. (if (equal var 'tag) (setq org-prefix-has-tag t))
  6369. (setq f (concat "%" (match-string 2 s) "s"))
  6370. (if opt
  6371. (setq varform
  6372. `(if (equal "" ,var)
  6373. ""
  6374. (format ,f (if (equal "" ,var) "" (concat ,var ,c)))))
  6375. (setq varform `(format ,f (if (equal ,var "") "" (concat ,var ,c)))))
  6376. (setq s (replace-match "%s" t nil s))
  6377. (push varform vars))
  6378. (setq vars (nreverse vars))
  6379. (setq org-prefix-format-compiled `(format ,s ,@vars))))
  6380. (defun org-get-time-of-day (s &optional string mod24)
  6381. "Check string S for a time of day.
  6382. If found, return it as a military time number between 0 and 2400.
  6383. If not found, return nil.
  6384. The optional STRING argument forces conversion into a 5 character wide string
  6385. HH:MM."
  6386. (save-match-data
  6387. (when
  6388. (or
  6389. (string-match
  6390. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)\\([AaPp][Mm]\\)?\\> *" s)
  6391. (string-match
  6392. "\\<\\([012]?[0-9]\\)\\(:\\([0-5][0-9]\\)\\)?\\([AaPp][Mm]\\)\\> *" s))
  6393. (let* ((h (string-to-number (match-string 1 s)))
  6394. (m (if (match-end 3) (string-to-number (match-string 3 s)) 0))
  6395. (ampm (if (match-end 4) (downcase (match-string 4 s))))
  6396. (am-p (equal ampm "am"))
  6397. (h1 (cond ((not ampm) h)
  6398. ((= h 12) (if am-p 0 12))
  6399. (t (+ h (if am-p 0 12)))))
  6400. (h2 (if (and string mod24 (not (and (= m 0) (= h1 24))))
  6401. (mod h1 24) h1))
  6402. (t0 (+ (* 100 h2) m))
  6403. (t1 (concat (if (>= h1 24) "+" " ")
  6404. (if (< t0 100) "0" "")
  6405. (if (< t0 10) "0" "")
  6406. (int-to-string t0))))
  6407. (if string (concat (substring t1 -4 -2) ":" (substring t1 -2)) t0)))))
  6408. (defun org-finalize-agenda-entries (list)
  6409. "Sort and concatenate the agenda items."
  6410. (setq list (mapcar 'org-agenda-highlight-todo list))
  6411. (mapconcat 'identity (sort list 'org-entries-lessp) "\n"))
  6412. (defun org-agenda-highlight-todo (x)
  6413. (let (re pl)
  6414. (if (eq x 'line)
  6415. (save-excursion
  6416. (beginning-of-line 1)
  6417. (setq re (get-text-property (point) 'org-not-done-regexp))
  6418. (goto-char (+ (point) (get-text-property (point) 'prefix-length)))
  6419. (and (looking-at (concat "[ \t]*" re))
  6420. (add-text-properties (match-beginning 0) (match-end 0)
  6421. '(face org-todo))))
  6422. (setq re (get-text-property 0 'org-not-done-regexp x)
  6423. pl (get-text-property 0 'prefix-length x))
  6424. (and re (equal (string-match re x pl) pl)
  6425. (add-text-properties (match-beginning 0) (match-end 0)
  6426. '(face org-todo) x))
  6427. x)))
  6428. (defsubst org-cmp-priority (a b)
  6429. "Compare the priorities of string A and B."
  6430. (let ((pa (or (get-text-property 1 'priority a) 0))
  6431. (pb (or (get-text-property 1 'priority b) 0)))
  6432. (cond ((> pa pb) +1)
  6433. ((< pa pb) -1)
  6434. (t nil))))
  6435. (defsubst org-cmp-category (a b)
  6436. "Compare the string values of categories of strings A and B."
  6437. (let ((ca (or (get-text-property 1 'category a) ""))
  6438. (cb (or (get-text-property 1 'category b) "")))
  6439. (cond ((string-lessp ca cb) -1)
  6440. ((string-lessp cb ca) +1)
  6441. (t nil))))
  6442. (defsubst org-cmp-time (a b)
  6443. "Compare the time-of-day values of strings A and B."
  6444. (let* ((def (if org-sort-agenda-notime-is-late 9901 -1))
  6445. (ta (or (get-text-property 1 'time-of-day a) def))
  6446. (tb (or (get-text-property 1 'time-of-day b) def)))
  6447. (cond ((< ta tb) -1)
  6448. ((< tb ta) +1)
  6449. (t nil))))
  6450. (defun org-entries-lessp (a b)
  6451. "Predicate for sorting agenda entries."
  6452. ;; The following variables will be used when the form is evaluated.
  6453. (let* ((time-up (org-cmp-time a b))
  6454. (time-down (if time-up (- time-up) nil))
  6455. (priority-up (org-cmp-priority a b))
  6456. (priority-down (if priority-up (- priority-up) nil))
  6457. (category-up (org-cmp-category a b))
  6458. (category-down (if category-up (- category-up) nil))
  6459. (category-keep (if category-up +1 nil)))
  6460. (cdr (assoc
  6461. (eval (cons 'or org-agenda-sorting-strategy))
  6462. '((-1 . t) (1 . nil) (nil . nil))))))
  6463. (defun org-agenda-show-priority ()
  6464. "Show the priority of the current item.
  6465. This priority is composed of the main priority given with the [#A] cookies,
  6466. and by additional input from the age of a schedules or deadline entry."
  6467. (interactive)
  6468. (let* ((pri (get-text-property (point-at-bol) 'priority)))
  6469. (message "Priority is %d" (if pri pri -1000))))
  6470. (defun org-agenda-show-tags ()
  6471. "Show the tags applicable to the current item."
  6472. (interactive)
  6473. (let* ((tags (get-text-property (point-at-bol) 'tags)))
  6474. (if tags
  6475. (message "Tags are :%s:"
  6476. (org-no-properties (mapconcat 'identity tags ":")))
  6477. (message "No tags associated with this line"))))
  6478. (defun org-agenda-goto (&optional highlight)
  6479. "Go to the Org-mode file which contains the item at point."
  6480. (interactive)
  6481. (let* ((marker (or (get-text-property (point) 'org-marker)
  6482. (org-agenda-error)))
  6483. (buffer (marker-buffer marker))
  6484. (pos (marker-position marker)))
  6485. (switch-to-buffer-other-window buffer)
  6486. (widen)
  6487. (goto-char pos)
  6488. (when (eq major-mode 'org-mode)
  6489. (org-show-hidden-entry)
  6490. (save-excursion
  6491. (and (outline-next-heading)
  6492. (org-flag-heading nil)))) ; show the next heading
  6493. (and highlight (org-highlight (point-at-bol) (point-at-eol)))))
  6494. (defun org-agenda-switch-to ()
  6495. "Go to the Org-mode file which contains the item at point."
  6496. (interactive)
  6497. (let* ((marker (or (get-text-property (point) 'org-marker)
  6498. (org-agenda-error)))
  6499. (buffer (marker-buffer marker))
  6500. (pos (marker-position marker)))
  6501. (switch-to-buffer buffer)
  6502. (delete-other-windows)
  6503. (widen)
  6504. (goto-char pos)
  6505. (when (eq major-mode 'org-mode)
  6506. (org-show-hidden-entry)
  6507. (save-excursion
  6508. (and (outline-next-heading)
  6509. (org-flag-heading nil)))))) ; show the next heading
  6510. (defun org-agenda-goto-mouse (ev)
  6511. "Go to the Org-mode file which contains the item at the mouse click."
  6512. (interactive "e")
  6513. (mouse-set-point ev)
  6514. (org-agenda-goto))
  6515. (defun org-agenda-show ()
  6516. "Display the Org-mode file which contains the item at point."
  6517. (interactive)
  6518. (let ((win (selected-window)))
  6519. (org-agenda-goto t)
  6520. (select-window win)))
  6521. (defun org-agenda-recenter (arg)
  6522. "Display the Org-mode file which contains the item at point and recenter."
  6523. (interactive "P")
  6524. (let ((win (selected-window)))
  6525. (org-agenda-goto t)
  6526. (recenter arg)
  6527. (select-window win)))
  6528. (defun org-agenda-show-mouse (ev)
  6529. "Display the Org-mode file which contains the item at the mouse click."
  6530. (interactive "e")
  6531. (mouse-set-point ev)
  6532. (org-agenda-show))
  6533. (defun org-agenda-check-no-diary ()
  6534. "Check if the entry is a diary link and abort if yes."
  6535. (if (get-text-property (point) 'org-agenda-diary-link)
  6536. (org-agenda-error)))
  6537. (defun org-agenda-error ()
  6538. (error "Command not allowed in this line"))
  6539. (defvar org-last-heading-marker (make-marker)
  6540. "Marker pointing to the headline that last changed its TODO state
  6541. by a remote command from the agenda.")
  6542. (defun org-agenda-todo (&optional arg)
  6543. "Cycle TODO state of line at point, also in Org-mode file.
  6544. This changes the line at point, all other lines in the agenda referring to
  6545. the same tree node, and the headline of the tree node in the Org-mode file."
  6546. (interactive "P")
  6547. (org-agenda-check-no-diary)
  6548. (let* ((col (current-column))
  6549. (marker (or (get-text-property (point) 'org-marker)
  6550. (org-agenda-error)))
  6551. (buffer (marker-buffer marker))
  6552. (pos (marker-position marker))
  6553. (hdmarker (get-text-property (point) 'org-hd-marker))
  6554. (buffer-read-only nil)
  6555. newhead)
  6556. (with-current-buffer buffer
  6557. (widen)
  6558. (goto-char pos)
  6559. (org-show-hidden-entry)
  6560. (save-excursion
  6561. (and (outline-next-heading)
  6562. (org-flag-heading nil))) ; show the next heading
  6563. (org-todo arg)
  6564. (and (bolp) (forward-char 1))
  6565. (setq newhead (org-get-heading))
  6566. (save-excursion
  6567. (org-back-to-heading)
  6568. (move-marker org-last-heading-marker (point))))
  6569. (beginning-of-line 1)
  6570. (save-excursion
  6571. (org-agenda-change-all-lines newhead hdmarker 'fixface))
  6572. (move-to-column col)))
  6573. (defun org-agenda-change-all-lines (newhead hdmarker &optional fixface)
  6574. "Change all lines in the agenda buffer which match HDMARKER.
  6575. The new content of the line will be NEWHEAD (as modified by
  6576. `org-format-agenda-item'). HDMARKER is checked with
  6577. `equal' against all `org-hd-marker' text properties in the file.
  6578. If FIXFACE is non-nil, the face of each item is modified acording to
  6579. the new TODO state."
  6580. (let* (props m pl undone-face done-face finish new dotime cat tags)
  6581. ; (setq newhead (org-format-agenda-item "x" newhead "x" nil 'noprefix))
  6582. (save-excursion
  6583. (goto-char (point-max))
  6584. (beginning-of-line 1)
  6585. (while (not finish)
  6586. (setq finish (bobp))
  6587. (when (and (setq m (get-text-property (point) 'org-hd-marker))
  6588. (equal m hdmarker))
  6589. (setq props (text-properties-at (point))
  6590. dotime (get-text-property (point) 'dotime)
  6591. cat (get-text-property (point) 'category)
  6592. tags (get-text-property (point) 'tags)
  6593. new (org-format-agenda-item "x" newhead cat tags dotime 'noprefix)
  6594. pl (get-text-property (point) 'prefix-length)
  6595. undone-face (get-text-property (point) 'undone-face)
  6596. done-face (get-text-property (point) 'done-face))
  6597. (move-to-column pl)
  6598. (if (looking-at ".*")
  6599. (progn
  6600. (replace-match new t t)
  6601. (beginning-of-line 1)
  6602. (add-text-properties (point-at-bol) (point-at-eol) props)
  6603. (when fixface
  6604. (add-text-properties
  6605. (point-at-bol) (point-at-eol)
  6606. (list 'face
  6607. (if org-last-todo-state-is-todo
  6608. undone-face done-face)))
  6609. (org-agenda-highlight-todo 'line))
  6610. (beginning-of-line 1))
  6611. (error "Line update did not work")))
  6612. (beginning-of-line 0)))))
  6613. (defun org-agenda-priority-up ()
  6614. "Increase the priority of line at point, also in Org-mode file."
  6615. (interactive)
  6616. (org-agenda-priority 'up))
  6617. (defun org-agenda-priority-down ()
  6618. "Decrease the priority of line at point, also in Org-mode file."
  6619. (interactive)
  6620. (org-agenda-priority 'down))
  6621. (defun org-agenda-priority (&optional force-direction)
  6622. "Set the priority of line at point, also in Org-mode file.
  6623. This changes the line at point, all other lines in the agenda referring to
  6624. the same tree node, and the headline of the tree node in the Org-mode file."
  6625. (interactive)
  6626. (org-agenda-check-no-diary)
  6627. (let* ((marker (or (get-text-property (point) 'org-marker)
  6628. (org-agenda-error)))
  6629. (buffer (marker-buffer marker))
  6630. (pos (marker-position marker))
  6631. (hdmarker (get-text-property (point) 'org-hd-marker))
  6632. (buffer-read-only nil)
  6633. newhead)
  6634. (with-current-buffer buffer
  6635. (widen)
  6636. (goto-char pos)
  6637. (org-show-hidden-entry)
  6638. (save-excursion
  6639. (and (outline-next-heading)
  6640. (org-flag-heading nil))) ; show the next heading
  6641. (funcall 'org-priority force-direction)
  6642. (end-of-line 1)
  6643. (setq newhead (org-get-heading)))
  6644. (org-agenda-change-all-lines newhead hdmarker)
  6645. (beginning-of-line 1)))
  6646. (defun org-get-tags-at (&optional pos)
  6647. "Get a list of all headline tags applicable at POS.
  6648. POS defaults to point. If tags are inherited, the list contains
  6649. the targets in the same sequence as the headlines appear, i.e.
  6650. the tags of the current headline come last."
  6651. (interactive)
  6652. (let (tags)
  6653. (save-excursion
  6654. (goto-char (or pos (point)))
  6655. (save-match-data
  6656. (org-back-to-heading t)
  6657. (condition-case nil
  6658. (while t
  6659. (if (looking-at "[^\r\n]+?:\\([a-zA-Z_@0-9:]+\\):[ \t]*\\([\n\r]\\|\\'\\)")
  6660. (setq tags (append (org-split-string
  6661. (org-match-string-no-properties 1) ":")
  6662. tags)))
  6663. (or org-use-tag-inheritance (error ""))
  6664. (org-up-heading-all 1))
  6665. (error nil))))
  6666. (message "%s" tags)
  6667. tags))
  6668. (defun org-agenda-set-tags ()
  6669. "Set tags for the current headline."
  6670. (interactive)
  6671. (org-agenda-check-no-diary)
  6672. (org-agenda-show) ;;; FIXME This is a stupid hack and should not be needed
  6673. (let* ((hdmarker (or (get-text-property (point) 'org-hd-marker)
  6674. (org-agenda-error)))
  6675. (buffer (marker-buffer hdmarker))
  6676. (pos (marker-position hdmarker))
  6677. (buffer-read-only nil)
  6678. newhead)
  6679. (with-current-buffer buffer
  6680. (widen)
  6681. (goto-char pos)
  6682. (org-show-hidden-entry)
  6683. (save-excursion
  6684. (and (outline-next-heading)
  6685. (org-flag-heading nil))) ; show the next heading
  6686. (call-interactively 'org-set-tags)
  6687. (end-of-line 1)
  6688. (setq newhead (org-get-heading)))
  6689. (org-agenda-change-all-lines newhead hdmarker)
  6690. (beginning-of-line 1)))
  6691. (defun org-agenda-date-later (arg &optional what)
  6692. "Change the date of this item to one day later."
  6693. (interactive "p")
  6694. (org-agenda-check-type t 'agenda 'timeline)
  6695. (org-agenda-check-no-diary)
  6696. (let* ((marker (or (get-text-property (point) 'org-marker)
  6697. (org-agenda-error)))
  6698. (buffer (marker-buffer marker))
  6699. (pos (marker-position marker)))
  6700. (with-current-buffer buffer
  6701. (widen)
  6702. (goto-char pos)
  6703. (if (not (org-at-timestamp-p))
  6704. (error "Cannot find time stamp"))
  6705. (org-timestamp-change arg (or what 'day))
  6706. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6707. (defun org-agenda-date-earlier (arg &optional what)
  6708. "Change the date of this item to one day earlier."
  6709. (interactive "p")
  6710. (org-agenda-date-later (- arg) what))
  6711. (defun org-agenda-date-prompt (arg)
  6712. "Change the date of this item. Date is prompted for, with default today.
  6713. The prefix ARG is passed to the `org-time-stamp' command and can therefore
  6714. be used to request time specification in the time stamp."
  6715. (interactive "P")
  6716. (org-agenda-check-type t 'agenda 'timeline)
  6717. (org-agenda-check-no-diary)
  6718. (let* ((marker (or (get-text-property (point) 'org-marker)
  6719. (org-agenda-error)))
  6720. (buffer (marker-buffer marker))
  6721. (pos (marker-position marker)))
  6722. (with-current-buffer buffer
  6723. (widen)
  6724. (goto-char pos)
  6725. (if (not (org-at-timestamp-p))
  6726. (error "Cannot find time stamp"))
  6727. (org-time-stamp arg)
  6728. (message "Time stamp changed to %s" org-last-changed-timestamp))))
  6729. (defun org-agenda-schedule (arg)
  6730. "Schedule the item at point."
  6731. (interactive "P")
  6732. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  6733. (org-agenda-check-no-diary)
  6734. (let* ((marker (or (get-text-property (point) 'org-marker)
  6735. (org-agenda-error)))
  6736. (buffer (marker-buffer marker))
  6737. (pos (marker-position marker))
  6738. (org-insert-labeled-timestamps-at-point nil)
  6739. ts)
  6740. (with-current-buffer buffer
  6741. (widen)
  6742. (goto-char pos)
  6743. (setq ts (org-schedule))
  6744. (message "Item scheduled for %s" ts))))
  6745. (defun org-agenda-deadline (arg)
  6746. "Schedule the item at point."
  6747. (interactive "P")
  6748. (org-agenda-check-type t 'agenda 'timeline 'todo 'tags)
  6749. (org-agenda-check-no-diary)
  6750. (let* ((marker (or (get-text-property (point) 'org-marker)
  6751. (org-agenda-error)))
  6752. (buffer (marker-buffer marker))
  6753. (pos (marker-position marker))
  6754. (org-insert-labeled-timestamps-at-point nil)
  6755. ts)
  6756. (with-current-buffer buffer
  6757. (widen)
  6758. (goto-char pos)
  6759. (setq ts (org-deadline))
  6760. (message "Deadline for this item set to %s" ts))))
  6761. (defun org-get-heading ()
  6762. "Return the heading of the current entry, without the stars."
  6763. (save-excursion
  6764. (and (memq (char-before) '(?\n ?\r)) (skip-chars-forward "^\n\r"))
  6765. (if (and (re-search-backward "[\r\n]\\*" nil t)
  6766. (looking-at "[\r\n]\\*+[ \t]+\\([^\r\n]*\\)"))
  6767. (match-string 1)
  6768. "")))
  6769. (defun org-agenda-diary-entry ()
  6770. "Make a diary entry, like the `i' command from the calendar.
  6771. All the standard commands work: block, weekly etc."
  6772. (interactive)
  6773. (org-agenda-check-type t 'agenda 'timeline)
  6774. (require 'diary-lib)
  6775. (let* ((char (progn
  6776. (message "Diary entry: [d]ay [w]eekly [m]onthly [y]early [a]nniversary [b]lock [c]yclic")
  6777. (read-char-exclusive)))
  6778. (cmd (cdr (assoc char
  6779. '((?d . insert-diary-entry)
  6780. (?w . insert-weekly-diary-entry)
  6781. (?m . insert-monthly-diary-entry)
  6782. (?y . insert-yearly-diary-entry)
  6783. (?a . insert-anniversary-diary-entry)
  6784. (?b . insert-block-diary-entry)
  6785. (?c . insert-cyclic-diary-entry)))))
  6786. (oldf (symbol-function 'calendar-cursor-to-date))
  6787. (point (point))
  6788. (mark (or (mark t) (point))))
  6789. (unless cmd
  6790. (error "No command associated with <%c>" char))
  6791. (unless (and (get-text-property point 'day)
  6792. (or (not (equal ?b char))
  6793. (get-text-property mark 'day)))
  6794. (error "Don't know which date to use for diary entry"))
  6795. ;; We implement this by hacking the `calendar-cursor-to-date' function
  6796. ;; and the `calendar-mark-ring' variable. Saves a lot of code.
  6797. (let ((calendar-mark-ring
  6798. (list (calendar-gregorian-from-absolute
  6799. (or (get-text-property mark 'day)
  6800. (get-text-property point 'day))))))
  6801. (unwind-protect
  6802. (progn
  6803. (fset 'calendar-cursor-to-date
  6804. (lambda (&optional error)
  6805. (calendar-gregorian-from-absolute
  6806. (get-text-property point 'day))))
  6807. (call-interactively cmd))
  6808. (fset 'calendar-cursor-to-date oldf)))))
  6809. (defun org-agenda-execute-calendar-command (cmd)
  6810. "Execute a calendar command from the agenda, with the date associated to
  6811. the cursor position."
  6812. (org-agenda-check-type t 'agenda 'timeline)
  6813. (require 'diary-lib)
  6814. (unless (get-text-property (point) 'day)
  6815. (error "Don't know which date to use for calendar command"))
  6816. (let* ((oldf (symbol-function 'calendar-cursor-to-date))
  6817. (point (point))
  6818. (date (calendar-gregorian-from-absolute
  6819. (get-text-property point 'day)))
  6820. (displayed-day (extract-calendar-day date))
  6821. (displayed-month (extract-calendar-month date))
  6822. (displayed-year (extract-calendar-year date)))
  6823. (unwind-protect
  6824. (progn
  6825. (fset 'calendar-cursor-to-date
  6826. (lambda (&optional error)
  6827. (calendar-gregorian-from-absolute
  6828. (get-text-property point 'day))))
  6829. (call-interactively cmd))
  6830. (fset 'calendar-cursor-to-date oldf))))
  6831. (defun org-agenda-phases-of-moon ()
  6832. "Display the phases of the moon for the 3 months around the cursor date."
  6833. (interactive)
  6834. (org-agenda-execute-calendar-command 'calendar-phases-of-moon))
  6835. (defun org-agenda-holidays ()
  6836. "Display the holidays for the 3 months around the cursor date."
  6837. (interactive)
  6838. (org-agenda-execute-calendar-command 'list-calendar-holidays))
  6839. (defun org-agenda-sunrise-sunset (arg)
  6840. "Display sunrise and sunset for the cursor date.
  6841. Latitude and longitude can be specified with the variables
  6842. `calendar-latitude' and `calendar-longitude'. When called with prefix
  6843. argument, latitude and longitude will be prompted for."
  6844. (interactive "P")
  6845. (let ((calendar-longitude (if arg nil calendar-longitude))
  6846. (calendar-latitude (if arg nil calendar-latitude))
  6847. (calendar-location-name
  6848. (if arg "the given coordinates" calendar-location-name)))
  6849. (org-agenda-execute-calendar-command 'calendar-sunrise-sunset)))
  6850. (defun org-agenda-goto-calendar ()
  6851. "Open the Emacs calendar with the date at the cursor."
  6852. (interactive)
  6853. (org-agenda-check-type t 'agenda 'timeline)
  6854. (let* ((day (or (get-text-property (point) 'day)
  6855. (error "Don't know which date to open in calendar")))
  6856. (date (calendar-gregorian-from-absolute day))
  6857. (calendar-move-hook nil)
  6858. (view-calendar-holidays-initially nil)
  6859. (view-diary-entries-initially nil))
  6860. (calendar)
  6861. (calendar-goto-date date)))
  6862. (defun org-calendar-goto-agenda ()
  6863. "Compute the Org-mode agenda for the calendar date displayed at the cursor.
  6864. This is a command that has to be installed in `calendar-mode-map'."
  6865. (interactive)
  6866. (org-agenda-list nil (calendar-absolute-from-gregorian
  6867. (calendar-cursor-to-date))
  6868. nil t))
  6869. (defun org-agenda-convert-date ()
  6870. (interactive)
  6871. (org-agenda-check-type t 'agenda 'timeline)
  6872. (let ((day (get-text-property (point) 'day))
  6873. date s)
  6874. (unless day
  6875. (error "Don't know which date to convert"))
  6876. (setq date (calendar-gregorian-from-absolute day))
  6877. (setq s (concat
  6878. "Gregorian: " (calendar-date-string date) "\n"
  6879. "ISO: " (calendar-iso-date-string date) "\n"
  6880. "Day of Yr: " (calendar-day-of-year-string date) "\n"
  6881. "Julian: " (calendar-julian-date-string date) "\n"
  6882. "Astron. JD: " (calendar-astro-date-string date)
  6883. " (Julian date number at noon UTC)\n"
  6884. "Hebrew: " (calendar-hebrew-date-string date) " (until sunset)\n"
  6885. "Islamic: " (calendar-islamic-date-string date) " (until sunset)\n"
  6886. "French: " (calendar-french-date-string date) "\n"
  6887. "Mayan: " (calendar-mayan-date-string date) "\n"
  6888. "Coptic: " (calendar-coptic-date-string date) "\n"
  6889. "Ethiopic: " (calendar-ethiopic-date-string date) "\n"
  6890. "Persian: " (calendar-persian-date-string date) "\n"
  6891. "Chinese: " (calendar-chinese-date-string date) "\n"))
  6892. (with-output-to-temp-buffer "*Dates*"
  6893. (princ s))
  6894. (if (fboundp 'fit-window-to-buffer)
  6895. (fit-window-to-buffer (get-buffer-window "*Dates*")))))
  6896. ;;; Tags
  6897. (defun org-scan-tags (action matcher &optional todo-only)
  6898. "Scan headline tags with inheritance and produce output ACTION.
  6899. ACTION can be `sparse-tree' or `agenda'. MATCHER is a Lisp form to be
  6900. evaluated, testing if a given set of tags qualifies a headline for
  6901. inclusion. When TODO-ONLY is non-nil, only lines with a TODO keyword
  6902. are included in the output."
  6903. (let* ((re (concat "[\n\r]" outline-regexp " *\\(\\<\\("
  6904. (mapconcat 'regexp-quote
  6905. (nreverse (cdr (reverse org-todo-keywords)))
  6906. "\\|")
  6907. "\\>\\)\\)? *\\(.*?\\)\\(:[A-Za-z_@0-9:]+:\\)?[ \t]*[\n\r]"))
  6908. (props (list 'face nil
  6909. 'done-face 'org-done
  6910. 'undone-face nil
  6911. 'mouse-face 'highlight
  6912. 'keymap org-agenda-keymap
  6913. 'help-echo
  6914. (format "mouse-2 or RET jump to org file %s"
  6915. (abbreviate-file-name buffer-file-name))))
  6916. lspos
  6917. tags tags-list tags-alist (llast 0) rtn level category i txt
  6918. todo marker)
  6919. (save-excursion
  6920. (goto-char (point-min))
  6921. (when (eq action 'sparse-tree) (org-overview))
  6922. (while (re-search-forward re nil t)
  6923. (setq todo (if (match-end 1) (match-string 2))
  6924. tags (if (match-end 4) (match-string 4)))
  6925. (goto-char (setq lspos (1+ (match-beginning 0))))
  6926. (setq level (funcall outline-level)
  6927. category (org-get-category))
  6928. (setq i llast llast level)
  6929. ;; remove tag lists from same and sublevels
  6930. (while (>= i level)
  6931. (when (setq entry (assoc i tags-alist))
  6932. (setq tags-alist (delete entry tags-alist)))
  6933. (setq i (1- i)))
  6934. ;; add the nex tags
  6935. (when tags
  6936. (setq tags (mapcar 'downcase (org-split-string tags ":"))
  6937. tags-alist
  6938. (cons (cons level tags) tags-alist)))
  6939. ;; compile tags for current headline
  6940. (setq tags-list
  6941. (if org-use-tag-inheritance
  6942. (apply 'append (mapcar 'cdr tags-alist))
  6943. tags))
  6944. (when (and (or (not todo-only) todo)
  6945. (eval matcher))
  6946. ;; list this headline
  6947. (if (eq action 'sparse-tree)
  6948. (progn
  6949. (org-show-hierarchy-above))
  6950. (setq txt (org-format-agenda-item
  6951. ""
  6952. (concat
  6953. (if org-tags-match-list-sublevels
  6954. (make-string (1- level) ?.) "")
  6955. (org-get-heading))
  6956. category tags-list))
  6957. (goto-char lspos)
  6958. (setq marker (org-agenda-new-marker))
  6959. (org-add-props txt props
  6960. 'org-marker marker 'org-hd-marker marker 'category category)
  6961. (push txt rtn))
  6962. ;; if we are to skip sublevels, jump to end of subtree
  6963. (point)
  6964. (or org-tags-match-list-sublevels (org-end-of-subtree)))))
  6965. (nreverse rtn)))
  6966. (defun org-tags-sparse-tree (&optional arg match)
  6967. "Create a sparse tree according to tags search string MATCH.
  6968. MATCH can contain positive and negative selection of tags, like
  6969. \"+WORK+URGENT-WITHBOSS\"."
  6970. (interactive "P")
  6971. (let ((org-show-following-heading nil)
  6972. (org-show-hierarchy-above nil))
  6973. (org-scan-tags 'sparse-tree (cdr (org-make-tags-matcher match)))))
  6974. (defun org-make-tags-matcher (match)
  6975. "Create the TAGS matcher form for the tags-selecting string MATCH."
  6976. (unless match
  6977. ;; Get a new match request, with completion
  6978. (setq org-last-tags-completion-table
  6979. (or (org-get-buffer-tags)
  6980. org-last-tags-completion-table))
  6981. (setq match (completing-read
  6982. "Tags: " 'org-tags-completion-function nil nil nil
  6983. 'org-tags-history)))
  6984. ;; parse the string and create a lisp form
  6985. (let ((match0 match) minus tag mm matcher orterms term orlist)
  6986. (setq orterms (org-split-string match "|"))
  6987. (while (setq term (pop orterms))
  6988. (while (string-match "^&?\\([-+:]\\)?\\([A-Za-z_@0-9]+\\)" term)
  6989. (setq minus (and (match-end 1)
  6990. (equal (match-string 1 term) "-"))
  6991. tag (match-string 2 term)
  6992. term (substring term (match-end 0))
  6993. mm (list 'member (downcase tag) 'tags-list)
  6994. mm (if minus (list 'not mm) mm))
  6995. (push mm matcher))
  6996. (push (if (> (length matcher) 1) (cons 'and matcher) (car matcher))
  6997. orlist)
  6998. (setq matcher nil))
  6999. (setq matcher (if (> (length orlist) 1) (cons 'or orlist) (car orlist)))
  7000. ;; Return the string and lisp forms of the matcher
  7001. (cons match0 matcher)))
  7002. ;;;###autoload
  7003. (defun org-tags-view (&optional todo-only match keep-modes)
  7004. "Show all headlines for all `org-agenda-files' matching a TAGS criterion.
  7005. The prefix arg TODO-ONLY limits the search to TODO entries."
  7006. (interactive "P")
  7007. (org-agenda-maybe-reset-markers 'force)
  7008. (org-compile-prefix-format org-agenda-prefix-format)
  7009. (let* ((org-agenda-keep-modes keep-modes)
  7010. (org-tags-match-list-sublevels
  7011. (if todo-only t org-tags-match-list-sublevels))
  7012. (win (selected-window))
  7013. (completion-ignore-case t)
  7014. rtn rtnall files file pos matcher
  7015. buffer)
  7016. (setq matcher (org-make-tags-matcher match)
  7017. match (car matcher) matcher (cdr matcher))
  7018. (if (not (equal (current-buffer) (get-buffer org-agenda-buffer-name)))
  7019. (progn
  7020. (delete-other-windows)
  7021. (switch-to-buffer-other-window
  7022. (get-buffer-create org-agenda-buffer-name))))
  7023. (setq buffer-read-only nil)
  7024. (erase-buffer)
  7025. (org-agenda-mode) (setq buffer-read-only nil)
  7026. (set (make-local-variable 'org-agenda-type) 'tags)
  7027. (set (make-local-variable 'org-agenda-redo-command)
  7028. (list 'org-tags-view (list 'quote todo-only)
  7029. (list 'if 'current-prefix-arg nil match) t))
  7030. (setq files (org-agenda-files)
  7031. rtnall nil)
  7032. (while (setq file (pop files))
  7033. (catch 'nextfile
  7034. (org-check-agenda-file file)
  7035. (setq buffer (if (file-exists-p file)
  7036. (org-get-agenda-file-buffer file)
  7037. (error "No such file %s" file)))
  7038. (if (not buffer)
  7039. ;; If file does not exist, merror message to agenda
  7040. (setq rtn (list
  7041. (format "ORG-AGENDA-ERROR: No such org-file %s" file))
  7042. rtnall (append rtnall rtn))
  7043. (with-current-buffer buffer
  7044. (unless (eq major-mode 'org-mode)
  7045. (error "Agenda file %s is not in `org-mode'" file))
  7046. (setq org-category-table (org-get-category-table))
  7047. (save-excursion
  7048. (save-restriction
  7049. (if org-respect-restriction
  7050. (if (org-region-active-p)
  7051. ;; Respect a region to restrict search
  7052. (narrow-to-region (region-beginning) (region-end)))
  7053. ;; If we work for the calendar or many files,
  7054. ;; get rid of any restriction
  7055. (widen))
  7056. (setq rtn (org-scan-tags 'agenda matcher todo-only))
  7057. (setq rtnall (append rtnall rtn))))))))
  7058. (insert "Headlines with TAGS match: ")
  7059. (add-text-properties (point-min) (1- (point))
  7060. (list 'face 'org-level-3))
  7061. (setq pos (point))
  7062. (insert match "\n")
  7063. (add-text-properties pos (1- (point)) (list 'face 'org-warning))
  7064. (setq pos (point))
  7065. (insert "Press `C-u r' to search again with new search string\n")
  7066. (add-text-properties pos (1- (point)) (list 'face 'org-level-3))
  7067. (when rtnall
  7068. (insert (mapconcat 'identity rtnall "\n")))
  7069. (goto-char (point-min))
  7070. (setq buffer-read-only t)
  7071. (org-fit-agenda-window)
  7072. (if (not org-select-agenda-window) (select-window win))))
  7073. (defvar org-add-colon-after-tag-completion nil) ;; dynamically skoped param
  7074. (defun org-set-tags (&optional arg just-align)
  7075. "Set the tags for the current headline.
  7076. With prefix ARG, realign all tags in headings in the current buffer."
  7077. (interactive "P")
  7078. (let* ((re (concat "^" outline-regexp))
  7079. (col (current-column))
  7080. (current (org-get-tags))
  7081. table current-tags inherited-tags ; computed below when needed
  7082. tags hd empty invis)
  7083. (if arg
  7084. (save-excursion
  7085. (goto-char (point-min))
  7086. (while (re-search-forward re nil t)
  7087. (org-set-tags nil t))
  7088. (message "All tags realigned to column %d" org-tags-column))
  7089. (if just-align
  7090. (setq tags current)
  7091. (setq table (or org-tag-alist (org-get-buffer-tags))
  7092. org-last-tags-completion-table table
  7093. current-tags (org-split-string current ":")
  7094. inherited-tags (nreverse
  7095. (nthcdr (length current-tags)
  7096. (nreverse (org-get-tags-at))))
  7097. tags
  7098. (if (or (eq t org-use-fast-tag-selection)
  7099. (and org-use-fast-tag-selection
  7100. (delq nil (mapcar 'cdr table))))
  7101. (org-fast-tag-selection current-tags inherited-tags table)
  7102. (let ((org-add-colon-after-tag-completion t))
  7103. (completing-read "Tags: " 'org-tags-completion-function
  7104. nil nil current 'org-tags-history))))
  7105. (while (string-match "[-+&]+" tags)
  7106. (setq tags (replace-match ":" t t tags))))
  7107. (unless (setq empty (string-match "\\`[\t ]*\\'" tags))
  7108. (unless (string-match ":$" tags) (setq tags (concat tags ":")))
  7109. (unless (string-match "^:" tags) (setq tags (concat ":" tags))))
  7110. (if (equal current "")
  7111. (progn
  7112. (end-of-line 1)
  7113. (or empty (insert-before-markers " ")))
  7114. (beginning-of-line 1)
  7115. (setq invis (org-invisible-p))
  7116. (looking-at (concat "\\(.*\\)\\(" (regexp-quote current) "\\)[ \t]*"))
  7117. (setq hd (match-string 1))
  7118. (delete-region (match-beginning 0) (match-end 0))
  7119. (insert-before-markers (org-trim hd) (if empty "" " ")))
  7120. (unless (equal tags "")
  7121. (move-to-column (max (current-column)
  7122. (if (> org-tags-column 0)
  7123. org-tags-column
  7124. (- (- org-tags-column) (length tags))))
  7125. t)
  7126. (insert-before-markers tags)
  7127. (if (and (not invis) (org-invisible-p))
  7128. (outline-flag-region (point-at-bol) (point) nil)))
  7129. (move-to-column col))))
  7130. (defun org-tags-completion-function (string predicate &optional flag)
  7131. (let (s1 s2 rtn (ctable org-last-tags-completion-table)
  7132. (confirm (lambda (x) (stringp (car x)))))
  7133. (if (string-match "^\\(.*[-+:&|]\\)\\([^-+:&|]*\\)$" string)
  7134. (setq s1 (match-string 1 string)
  7135. s2 (match-string 2 string))
  7136. (setq s1 "" s2 string))
  7137. (cond
  7138. ((eq flag nil)
  7139. ;; try completion
  7140. (setq rtn (try-completion s2 ctable confirm))
  7141. (if (stringp rtn)
  7142. (concat s1 s2 (substring rtn (length s2))
  7143. (if (and org-add-colon-after-tag-completion
  7144. (assoc rtn ctable))
  7145. ":" "")))
  7146. )
  7147. ((eq flag t)
  7148. ;; all-completions
  7149. (all-completions s2 ctable confirm)
  7150. )
  7151. ((eq flag 'lambda)
  7152. ;; exact match?
  7153. (assoc s2 ctable)))
  7154. ))
  7155. (defun org-fast-tag-insert (kwd tags face &optional end)
  7156. "Insert KDW, and the TAGS, the latter with face FACE. Also inser END."
  7157. (insert (format "%-12s" (concat kwd ":"))
  7158. (org-add-props (mapconcat 'identity tags " ") nil 'face face)
  7159. (or end "")))
  7160. (defun org-fast-tag-selection (current inherited table)
  7161. "Fast tag selection with single keys.
  7162. CURRENT is the current list of tags in the headline, INHERITED is the
  7163. list of inherited tags, and TABLE is an alist of tags and corresponding keys,
  7164. possibly with grouping information.
  7165. If the keys are nil, a-z are automatically assigned.
  7166. Returns the new tags string, or nil to not change the current settings."
  7167. (let* ((maxlen (apply 'max (mapcar
  7168. (lambda (x)
  7169. (if (stringp (car x)) (string-width (car x)) 0))
  7170. table)))
  7171. (fwidth (+ maxlen 3 1 3))
  7172. (ncol (/ (- (window-width) 4) fwidth))
  7173. (i-face 'org-done)
  7174. (c-face 'org-tag)
  7175. tg cnt e c char c1 c2 ntable tbl rtn
  7176. groups ingroup)
  7177. (save-window-excursion
  7178. (delete-other-windows)
  7179. (split-window-vertically)
  7180. (switch-to-buffer-other-window (get-buffer-create " *Org tags*"))
  7181. (erase-buffer)
  7182. (org-fast-tag-insert "Inherited" inherited i-face "\n")
  7183. (org-fast-tag-insert "Current" current c-face "\n\n")
  7184. (setq tbl table char ?a cnt 0)
  7185. (while (setq e (pop tbl))
  7186. (cond
  7187. ((equal e '(:startgroup))
  7188. (push '() groups) (setq ingroup t)
  7189. (when (not (= cnt 0))
  7190. (setq cnt 0)
  7191. (insert "\n"))
  7192. (insert "{ "))
  7193. ((equal e '(:endgroup))
  7194. (setq ingroup nil cnt 0)
  7195. (insert "}\n"))
  7196. (t
  7197. (setq tg (car e) c2 nil)
  7198. (if (cdr e)
  7199. (setq c (cdr e))
  7200. ;; automatically assign a character.
  7201. (setq c1 (string-to-char
  7202. (downcase (substring
  7203. tg (if (= (string-to-char tg) ?@) 1 0)))))
  7204. (if (or (rassoc c1 ntable) (rassoc c1 table))
  7205. (while (or (rassoc char ntable) (rassoc char table))
  7206. (setq char (1+ char)))
  7207. (setq c2 c1))
  7208. (setq c (or c2 char)))
  7209. (if ingroup (push tg (car groups)))
  7210. (setq tg (org-add-props tg nil 'face
  7211. (cond
  7212. ((member tg current) c-face)
  7213. ((member tg inherited) i-face)
  7214. (t nil))))
  7215. (if (and (= cnt 0) (not ingroup)) (insert " "))
  7216. (insert "[" c "] " tg (make-string
  7217. (- fwidth 4 (length tg)) ?\ ))
  7218. (push (cons tg c) ntable)
  7219. (when (= (setq cnt (1+ cnt)) ncol)
  7220. (insert "\n")
  7221. (if ingroup (insert " "))
  7222. (setq cnt 0)))))
  7223. (setq ntable (nreverse ntable))
  7224. (insert "\n")
  7225. (goto-char (point-min))
  7226. (if (fboundp 'fit-window-to-buffer) (fit-window-to-buffer))
  7227. (setq rtn
  7228. (catch 'exit
  7229. (while t
  7230. (message "[key]:Toggle SPC: clear current RET accept%s"
  7231. (if groups " [!] ignore goups" ""))
  7232. (setq c (read-char-exclusive))
  7233. (cond
  7234. ((= c ?\r) (throw 'exit t))
  7235. ((= c ?!)
  7236. (setq groups nil)
  7237. (goto-char (point-min))
  7238. (while (re-search-forward "[{}]" nil t) (replace-match " ")))
  7239. ((or (= c ?\C-g)
  7240. (and (= c ?q) (not (rassoc c ntable))))
  7241. (setq quit-flag t))
  7242. ((= c ?\ ) (setq current nil))
  7243. ((setq e (rassoc c ntable) tg (car e))
  7244. (if (member tg current)
  7245. (setq current (delete tg current))
  7246. (loop for g in groups do
  7247. (if (member tg g)
  7248. (mapcar (lambda (x)
  7249. (setq current (delete x current)))
  7250. g)))
  7251. (setq current (cons tg current)))))
  7252. ;; Create a sorted list
  7253. (setq current
  7254. (sort current
  7255. (lambda (a b)
  7256. (assoc b (cdr (memq (assoc a ntable) ntable))))))
  7257. (goto-char (point-min))
  7258. (beginning-of-line 2)
  7259. (delete-region (point) (point-at-eol))
  7260. (org-fast-tag-insert "Current" current c-face)
  7261. (while (re-search-forward "\\[.\\] \\([a-zA-Z0-9_@]+\\)" nil t)
  7262. (setq tg (match-string 1))
  7263. (add-text-properties (match-beginning 1) (match-end 1)
  7264. (list 'face
  7265. (cond
  7266. ((member tg current) c-face)
  7267. ((member tg inherited) i-face)
  7268. (t nil)))))
  7269. (goto-char (point-min)))))
  7270. (if rtn
  7271. (mapconcat 'identity current ":")
  7272. nil))))
  7273. (defun org-get-tags ()
  7274. "Get the TAGS string in the current headline."
  7275. (unless (org-on-heading-p)
  7276. (error "Not on a heading"))
  7277. (save-excursion
  7278. (beginning-of-line 1)
  7279. (if (looking-at ".*[ \t]\\(:[A-Za-z_@0-9:]+:\\)[ \t]*\\(\r\\|$\\)")
  7280. (org-match-string-no-properties 1)
  7281. "")))
  7282. (defun org-get-buffer-tags ()
  7283. "Get a table of all tags used in the buffer, for completion."
  7284. (let (tags)
  7285. (save-excursion
  7286. (goto-char (point-min))
  7287. (while (re-search-forward "[ \t]:\\([A-Za-z_@0-9:]+\\):[ \t\r\n]" nil t)
  7288. (mapc (lambda (x) (add-to-list 'tags x))
  7289. (org-split-string (org-match-string-no-properties 1) ":"))))
  7290. (mapcar 'list tags)))
  7291. ;;; Link Stuff
  7292. (defvar org-create-file-search-functions nil
  7293. "List of functions to construct the right search string for a file link.
  7294. These functions are called in turn with point at the location to
  7295. which the link should point.
  7296. A function in the hook should first test if it would like to
  7297. handle this file type, for example by checking the major-mode or
  7298. the file extension. If it decides not to handle this file, it
  7299. should just return nil to give other functions a chance. If it
  7300. does handle the file, it must return the search string to be used
  7301. when following the link. The search string will be part of the
  7302. file link, given after a double colon, and `org-open-at-point'
  7303. will automatically search for it. If special measures must be
  7304. taken to make the search successful, another function should be
  7305. added to the companion hook `org-execute-file-search-functions',
  7306. which see.
  7307. A function in this hook may also use `setq' to set the variable
  7308. `description' to provide a suggestion for the descriptive text to
  7309. be used for this link when it gets inserted into an Org-mode
  7310. buffer with \\[org-insert-link].")
  7311. (defvar org-execute-file-search-functions nil
  7312. "List of functions to execute a file search triggered by a link.
  7313. Functions added to this hook must accept a single argument, the
  7314. search string that was part of the file link, the part after the
  7315. double colon. The function must first check if it would like to
  7316. handle this search, for example by checking the major-mode or the
  7317. file extension. If it decides not to handle this search, it
  7318. should just return nil to give other functions a chance. If it
  7319. does handle the search, it must return a non-nil value to keep
  7320. other functions from trying.
  7321. Each function can access the current prefix argument through the
  7322. variable `current-prefix-argument'. Note that a single prefix is
  7323. used to force opening a link in Emacs, so it may be good to only
  7324. use a numeric or double prefix to guide the search function.
  7325. In case this is needed, a function in this hook can also restore
  7326. the window configuration before `org-open-at-point' was called using:
  7327. (set-window-configuration org-window-config-before-follow-link)")
  7328. (defun org-find-file-at-mouse (ev)
  7329. "Open file link or URL at mouse."
  7330. (interactive "e")
  7331. (mouse-set-point ev)
  7332. (org-open-at-point 'in-emacs))
  7333. (defun org-open-at-mouse (ev)
  7334. "Open file link or URL at mouse."
  7335. (interactive "e")
  7336. (mouse-set-point ev)
  7337. (org-open-at-point))
  7338. (defvar org-window-config-before-follow-link nil
  7339. "The window configuration before following a link.
  7340. This is saved in case the need arises to restore it.")
  7341. (defun org-open-at-point (&optional in-emacs)
  7342. "Open link at or after point.
  7343. If there is no link at point, this function will search forward up to
  7344. the end of the current subtree.
  7345. Normally, files will be opened by an appropriate application. If the
  7346. optional argument IN-EMACS is non-nil, Emacs will visit the file."
  7347. (interactive "P")
  7348. (setq org-window-config-before-follow-link (current-window-configuration))
  7349. (org-remove-occur-highlights nil nil t)
  7350. (if (org-at-timestamp-p)
  7351. (org-agenda-list nil (time-to-days (org-time-string-to-time
  7352. (substring (match-string 1) 0 10)))
  7353. 1)
  7354. (let (type path link line search (pos (point)))
  7355. (catch 'match
  7356. (save-excursion
  7357. (skip-chars-forward "^]\n\r")
  7358. (when (and (re-search-backward "\\[\\[" nil t)
  7359. (looking-at org-bracket-link-regexp)
  7360. (<= (match-beginning 0) pos)
  7361. (>= (match-end 0) pos))
  7362. (setq link (org-link-unescape (org-match-string-no-properties 1)))
  7363. (while (string-match " *\n *" link)
  7364. (setq link (replace-match " " t t link)))
  7365. (if (string-match org-link-re-with-space2 link)
  7366. (setq type (match-string 1 link)
  7367. path (match-string 2 link))
  7368. (setq type "thisfile"
  7369. path link))
  7370. (throw 'match t)))
  7371. (when (get-text-property (point) 'org-linked-text)
  7372. (setq type "thisfile"
  7373. pos (if (get-text-property (1+ (point)) 'org-linked-text)
  7374. (1+ (point)) (point))
  7375. path (buffer-substring
  7376. (previous-single-property-change pos 'org-linked-text)
  7377. (next-single-property-change pos 'org-linked-text)))
  7378. (throw 'match t))
  7379. (save-excursion
  7380. (skip-chars-backward (concat "^[]" org-non-link-chars " "))
  7381. (if (equal (char-before) ?<) (backward-char 1))
  7382. (when (or (looking-at org-angle-link-re)
  7383. (looking-at org-plain-link-re)
  7384. (and (or (re-search-forward org-angle-link-re (point-at-eol) t)
  7385. (re-search-forward org-plain-link-re (point-at-eol) t))
  7386. (<= (match-beginning 0) pos)
  7387. (>= (match-end 0) pos)))
  7388. (setq type (match-string 1)
  7389. path (match-string 2))
  7390. (throw 'match t)))
  7391. (save-excursion
  7392. (skip-chars-backward "^ \t\n\r")
  7393. (when (looking-at "\\(:[A-Za-z_@0-9:]+\\):[ \t\r\n]")
  7394. (setq type "tags"
  7395. path (match-string 1))
  7396. (while (string-match ":" path)
  7397. (setq path (replace-match "+" t t path)))
  7398. (throw 'match t)))
  7399. (save-excursion
  7400. (skip-chars-backward "a-zA-Z_")
  7401. (when (and (memq 'camel org-activate-links)
  7402. (looking-at org-camel-regexp))
  7403. (setq type "camel" path (match-string 0))
  7404. (if (equal (char-before) ?*)
  7405. (setq path (concat "*" path))))
  7406. (throw 'match t)))
  7407. (unless path
  7408. (error "No link found"))
  7409. ;; Remove any trailing spaces in path
  7410. (if (string-match " +\\'" path)
  7411. (setq path (replace-match "" t t path)))
  7412. (cond
  7413. ((member type '("http" "https" "ftp" "mailto" "news"))
  7414. (browse-url (concat type ":" path)))
  7415. ((string= type "tags")
  7416. (org-tags-view in-emacs path))
  7417. ((or (string= type "camel")
  7418. (string= type "thisfile"))
  7419. (org-mark-ring-push)
  7420. (org-link-search
  7421. path
  7422. (cond ((equal in-emacs '(4)) 'occur)
  7423. ((equal in-emacs '(16)) 'org-occur)
  7424. (t nil))))
  7425. ((string= type "file")
  7426. (if (string-match "::\\([0-9]+\\)\\'" path)
  7427. (setq line (string-to-number (match-string 1 path))
  7428. path (substring path 0 (match-beginning 0)))
  7429. (if (string-match "::\\(.+\\)\\'" path)
  7430. (setq search (match-string 1 path)
  7431. path (substring path 0 (match-beginning 0)))))
  7432. (org-open-file path in-emacs line search))
  7433. ((string= type "news")
  7434. (org-follow-gnus-link path))
  7435. ((string= type "bbdb")
  7436. (org-follow-bbdb-link path))
  7437. ((string= type "info")
  7438. (org-follow-info-link path))
  7439. ((string= type "gnus")
  7440. (let (group article)
  7441. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7442. (error "Error in Gnus link"))
  7443. (setq group (match-string 1 path)
  7444. article (match-string 3 path))
  7445. (org-follow-gnus-link group article)))
  7446. ((string= type "vm")
  7447. (let (folder article)
  7448. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7449. (error "Error in VM link"))
  7450. (setq folder (match-string 1 path)
  7451. article (match-string 3 path))
  7452. ;; in-emacs is the prefix arg, will be interpreted as read-only
  7453. (org-follow-vm-link folder article in-emacs)))
  7454. ((string= type "wl")
  7455. (let (folder article)
  7456. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7457. (error "Error in Wanderlust link"))
  7458. (setq folder (match-string 1 path)
  7459. article (match-string 3 path))
  7460. (org-follow-wl-link folder article)))
  7461. ((string= type "mhe")
  7462. (let (folder article)
  7463. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7464. (error "Error in MHE link"))
  7465. (setq folder (match-string 1 path)
  7466. article (match-string 3 path))
  7467. (org-follow-mhe-link folder article)))
  7468. ((string= type "rmail")
  7469. (let (folder article)
  7470. (if (not (string-match "\\`\\([^#]+\\)\\(#\\(.*\\)\\)?" path))
  7471. (error "Error in RMAIL link"))
  7472. (setq folder (match-string 1 path)
  7473. article (match-string 3 path))
  7474. (org-follow-rmail-link folder article)))
  7475. ((string= type "shell")
  7476. (let ((cmd path))
  7477. (while (string-match "@{" cmd)
  7478. (setq cmd (replace-match "<" t t cmd)))
  7479. (while (string-match "@}" cmd)
  7480. (setq cmd (replace-match ">" t t cmd)))
  7481. (if (or (not org-confirm-shell-link-function)
  7482. (funcall org-confirm-shell-link-function
  7483. (format "Execute \"%s\" in shell? "
  7484. (org-add-props cmd nil
  7485. 'face 'org-warning))))
  7486. (progn
  7487. (message "Executing %s" cmd)
  7488. (shell-command cmd))
  7489. (error "Abort"))))
  7490. ((string= type "elisp")
  7491. (let ((cmd path))
  7492. (if (or (not org-confirm-elisp-link-function)
  7493. (funcall org-confirm-elisp-link-function
  7494. (format "Execute \"%s\" as elisp? "
  7495. (org-add-props cmd nil
  7496. 'face 'org-warning))))
  7497. (message "%s => %s" cmd (eval (read cmd)))
  7498. (error "Abort"))))
  7499. (t
  7500. (browse-url-at-point))))))
  7501. (defun org-link-search (s &optional type)
  7502. "Search for a link search option.
  7503. When S is a CamelCaseWord, search for a target, or for a sentence containing
  7504. the words. If S is surrounded by forward slashes, it is interpreted as a
  7505. regular expression. In org-mode files, this will create an `org-occur'
  7506. sparse tree. In ordinary files, `occur' will be used to list matches.
  7507. If the current buffer is in `dired-mode', grep will be used to search
  7508. in all files."
  7509. (let ((case-fold-search t)
  7510. (s0 (mapconcat 'identity (org-split-string s "[ \t\r\n]+") " "))
  7511. (pos (point))
  7512. (pre "") (post "")
  7513. words re0 re1 re2 re3 re4 re5 re2a reall camel)
  7514. (cond
  7515. ;; First check if there are any special
  7516. ((run-hook-with-args-until-success 'org-execute-file-search-functions s))
  7517. ;; Now try the builtin stuff
  7518. ((save-excursion
  7519. (goto-char (point-min))
  7520. (and
  7521. (re-search-forward
  7522. (concat "<<" (regexp-quote s0) ">>") nil t)
  7523. (setq pos (match-beginning 0))))
  7524. ;; There is an exact target for this
  7525. (goto-char pos))
  7526. ((string-match "^/\\(.*\\)/$" s)
  7527. ;; A regular expression
  7528. (cond
  7529. ((eq major-mode 'org-mode)
  7530. (org-occur (match-string 1 s)))
  7531. ;;((eq major-mode 'dired-mode)
  7532. ;; (grep (concat "grep -n -e '" (match-string 1 s) "' *")))
  7533. (t (org-do-occur (match-string 1 s)))))
  7534. ((or (setq camel (string-match (concat "^" org-camel-regexp "$") s))
  7535. t)
  7536. ;; A camel or a normal search string
  7537. (when (equal (string-to-char s) ?*)
  7538. ;; Anchor on headlines, post may include tags.
  7539. (setq pre "^\\*+[ \t]*\\(?:\\sw+\\)?[ \t]*"
  7540. post "[ \t]*\\(?:[ \t]+:[a-zA-Z_@0-9:+]:[ \t]*\\)?$"
  7541. s (substring s 1)))
  7542. (remove-text-properties
  7543. 0 (length s)
  7544. '(face nil mouse-face nil keymap nil fontified nil) s)
  7545. ;; Make a series of regular expressions to find a match
  7546. (setq words
  7547. (if camel
  7548. (org-camel-to-words s)
  7549. (org-split-string s "[ \n\r\t]+"))
  7550. re0 (concat "\\(<<" (regexp-quote s0) ">>\\)")
  7551. re2 (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t]+") "\\)[ \t\r\n]")
  7552. re2a (concat "[ \t\r\n]\\(" (mapconcat 'downcase words "[ \t\r\n]+") "\\)[ \t\r\n]")
  7553. re4 (concat "[^a-zA-Z_]\\(" (mapconcat 'downcase words "[^a-zA-Z_\r\n]+") "\\)[^a-zA-Z_]")
  7554. re1 (concat pre re2 post)
  7555. re3 (concat pre re4 post)
  7556. re5 (concat pre ".*" re4)
  7557. re2 (concat pre re2)
  7558. re2a (concat pre re2a)
  7559. re4 (concat pre re4)
  7560. reall (concat "\\(" re0 "\\)\\|\\(" re1 "\\)\\|\\(" re2
  7561. "\\)\\|\\(" re3 "\\)\\|\\(" re4 "\\)\\|\\("
  7562. re5 "\\)"
  7563. ))
  7564. (cond
  7565. ((eq type 'org-occur) (org-occur reall))
  7566. ((eq type 'occur) (org-do-occur (downcase reall) 'cleanup))
  7567. (t (goto-char (point-min))
  7568. (if (or (org-search-not-link re0 nil t)
  7569. (org-search-not-link re1 nil t)
  7570. (org-search-not-link re2 nil t)
  7571. (org-search-not-link re2a nil t)
  7572. (org-search-not-link re3 nil t)
  7573. (org-search-not-link re4 nil t)
  7574. (org-search-not-link re5 nil t)
  7575. )
  7576. (goto-char (match-beginning 1))
  7577. (goto-char pos)
  7578. (error "No match")))))
  7579. (t
  7580. ;; Normal string-search
  7581. (goto-char (point-min))
  7582. (if (search-forward s nil t)
  7583. (goto-char (match-beginning 0))
  7584. (error "No match"))))
  7585. (and (eq major-mode 'org-mode) (org-show-hierarchy-above))))
  7586. (defun org-search-not-link (&rest args)
  7587. "Execute `re-search-forward', but only accept matches that are not a link."
  7588. (catch 'exit
  7589. (let ((pos (point)) p1)
  7590. (while (apply 're-search-forward args)
  7591. (setq p1 (point))
  7592. (if (not (save-match-data
  7593. (and (re-search-backward "\\[\\[" nil t)
  7594. (looking-at org-bracket-link-regexp)
  7595. (<= (match-beginning 0) p1)
  7596. (>= (match-end 0) p1))))
  7597. (progn (goto-char (match-end 0))
  7598. (throw 'exit (point)))
  7599. (goto-char (match-end 0)))))))
  7600. (defun org-do-occur (regexp &optional cleanup)
  7601. "Call the Emacs command `occur'.
  7602. If CLEANUP is non-nil, remove the printout of the regular expression
  7603. in the *Occur* buffer. This is useful if the regex is long and not useful
  7604. to read."
  7605. (occur regexp)
  7606. (when cleanup
  7607. (let ((cwin (selected-window)) win beg end)
  7608. (when (setq win (get-buffer-window "*Occur*"))
  7609. (select-window win))
  7610. (goto-char (point-min))
  7611. (when (re-search-forward "match[a-z]+" nil t)
  7612. (setq beg (match-end 0))
  7613. (if (re-search-forward "^[ \t]*[0-9]+" nil t)
  7614. (setq end (1- (match-beginning 0)))))
  7615. (and beg end (let ((buffer-read-only)) (delete-region beg end)))
  7616. (goto-char (point-min))
  7617. (select-window cwin))))
  7618. (defvar org-mark-ring nil
  7619. "Mark ring for positions before jumps in Org-mode.")
  7620. (defvar org-mark-ring-last-goto nil
  7621. "Last position in the mark ring used to go back.")
  7622. ;; Fill and close the ring
  7623. (setq org-mark-ring nil org-mark-ring-last-goto nil) ;; in case file is reloaded
  7624. (loop for i from 1 to org-mark-ring-length do
  7625. (push (make-marker) org-mark-ring))
  7626. (setcdr (nthcdr (1- org-mark-ring-length) org-mark-ring)
  7627. org-mark-ring)
  7628. (defun org-mark-ring-push (&optional pos buffer)
  7629. "Put the current position or POS into the mark ring and rotate it."
  7630. (interactive)
  7631. (setq pos (or pos (point)))
  7632. (setq org-mark-ring (nthcdr (1- org-mark-ring-length) org-mark-ring))
  7633. (move-marker (car org-mark-ring)
  7634. (or pos (point))
  7635. (or buffer (current-buffer)))
  7636. (message
  7637. (substitute-command-keys
  7638. "Position saved to mark ring, go back with \\[org-mark-ring-goto].")))
  7639. (defun org-mark-ring-goto (&optional n)
  7640. "Jump to the previous position in the mark ring.
  7641. With prefix arg N, jump back that many stored positions. When
  7642. called several times in succession, walk through the entire ring.
  7643. Org-mode commands jumping to a different position in the current file,
  7644. or to another Org-mode file, automatically push the old position
  7645. onto the ring."
  7646. (interactive "p")
  7647. (let (p m)
  7648. (if (eq last-command this-command)
  7649. (setq p (nthcdr n (or org-mark-ring-last-goto org-mark-ring)))
  7650. (setq p org-mark-ring))
  7651. (setq org-mark-ring-last-goto p)
  7652. (setq m (car p))
  7653. (switch-to-buffer (marker-buffer m))
  7654. (goto-char m)
  7655. (if (or (org-invisible-p) (org-invisible-p2)) (org-show-hierarchy-above))))
  7656. (defun org-camel-to-words (s)
  7657. "Split \"CamelCaseWords\" to (\"Camel\" \"Case\" \"Words\")."
  7658. (let ((case-fold-search nil)
  7659. words)
  7660. (while (string-match "[a-z][A-Z]" s)
  7661. (push (substring s 0 (1+ (match-beginning 0))) words)
  7662. (setq s (substring s (1+ (match-beginning 0)))))
  7663. (nreverse (cons s words))))
  7664. (defun org-remove-angle-brackets (s)
  7665. (if (equal (substring s 0 1) "<") (setq s (substring s 1)))
  7666. (if (equal (substring s -1) ">") (setq s (substring s 0 -1)))
  7667. s)
  7668. (defun org-add-angle-brackets (s)
  7669. (if (equal (substring s 0 1) "<") nil (setq s (concat "<" s)))
  7670. (if (equal (substring s -1) ">") nil (setq s (concat s ">")))
  7671. s)
  7672. (defun org-follow-bbdb-link (name)
  7673. "Follow a BBDB link to NAME."
  7674. (require 'bbdb)
  7675. (let ((inhibit-redisplay t)
  7676. (bbdb-electric-p nil))
  7677. (catch 'exit
  7678. ;; Exact match on name
  7679. (bbdb-name (concat "\\`" name "\\'") nil)
  7680. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7681. ;; Exact match on name
  7682. (bbdb-company (concat "\\`" name "\\'") nil)
  7683. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7684. ;; Partial match on name
  7685. (bbdb-name name nil)
  7686. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7687. ;; Partial match on company
  7688. (bbdb-company name nil)
  7689. (if (< 0 (buffer-size (get-buffer "*BBDB*"))) (throw 'exit nil))
  7690. ;; General match including network address and notes
  7691. (bbdb name nil)
  7692. (when (= 0 (buffer-size (get-buffer "*BBDB*")))
  7693. (delete-window (get-buffer-window "*BBDB*"))
  7694. (error "No matching BBDB record")))))
  7695. (defun org-follow-info-link (name)
  7696. "Follow an info file & node link to NAME."
  7697. (if (or (string-match "\\(.*\\)::?\\(.*\\)" name)
  7698. (string-match "\\(.*\\)" name))
  7699. (progn
  7700. (require 'info)
  7701. (if (match-string 2 name) ; If there isn't a node, choose "Top"
  7702. (Info-find-node (match-string 1 name) (match-string 2 name))
  7703. (Info-find-node (match-string 1 name) "Top")))
  7704. (message (concat "Could not open: " name))))
  7705. (defun org-follow-gnus-link (&optional group article)
  7706. "Follow a Gnus link to GROUP and ARTICLE."
  7707. (require 'gnus)
  7708. (funcall (cdr (assq 'gnus org-link-frame-setup)))
  7709. (if group (gnus-fetch-group group))
  7710. (if article
  7711. (or (gnus-summary-goto-article article nil 'force)
  7712. (if (fboundp 'gnus-summary-insert-cached-articles)
  7713. (progn
  7714. (gnus-summary-insert-cached-articles)
  7715. (gnus-summary-goto-article article nil 'force))
  7716. (message "Message could not be found.")))))
  7717. (defun org-follow-vm-link (&optional folder article readonly)
  7718. "Follow a VM link to FOLDER and ARTICLE."
  7719. (require 'vm)
  7720. (setq article (org-add-angle-brackets article))
  7721. (if (string-match "^//\\([a-zA-Z]+@\\)?\\([^:]+\\):\\(.*\\)" folder)
  7722. ;; ange-ftp or efs or tramp access
  7723. (let ((user (or (match-string 1 folder) (user-login-name)))
  7724. (host (match-string 2 folder))
  7725. (file (match-string 3 folder)))
  7726. (cond
  7727. ((featurep 'tramp)
  7728. ;; use tramp to access the file
  7729. (if (featurep 'xemacs)
  7730. (setq folder (format "[%s@%s]%s" user host file))
  7731. (setq folder (format "/%s@%s:%s" user host file))))
  7732. (t
  7733. ;; use ange-ftp or efs
  7734. (require (if (featurep 'xemacs) 'efs 'ange-ftp))
  7735. (setq folder (format "/%s@%s:%s" user host file))))))
  7736. (when folder
  7737. (funcall (cdr (assq 'vm org-link-frame-setup)) folder readonly)
  7738. (sit-for 0.1)
  7739. (when article
  7740. (vm-select-folder-buffer)
  7741. (widen)
  7742. (let ((case-fold-search t))
  7743. (goto-char (point-min))
  7744. (if (not (re-search-forward
  7745. (concat "^" "message-id: *" (regexp-quote article))))
  7746. (error "Could not find the specified message in this folder"))
  7747. (vm-isearch-update)
  7748. (vm-isearch-narrow)
  7749. (vm-beginning-of-message)
  7750. (vm-summarize)))))
  7751. (defun org-follow-wl-link (folder article)
  7752. "Follow a Wanderlust link to FOLDER and ARTICLE."
  7753. (setq article (org-add-angle-brackets article))
  7754. (wl-summary-goto-folder-subr folder 'no-sync t nil t)
  7755. (if article (wl-summary-jump-to-msg-by-message-id article ">"))
  7756. (wl-summary-redisplay))
  7757. (defun org-follow-rmail-link (folder article)
  7758. "Follow an RMAIL link to FOLDER and ARTICLE."
  7759. (setq article (org-add-angle-brackets article))
  7760. (let (message-number)
  7761. (save-excursion
  7762. (save-window-excursion
  7763. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  7764. (setq message-number
  7765. (save-restriction
  7766. (widen)
  7767. (goto-char (point-max))
  7768. (if (re-search-backward
  7769. (concat "^Message-ID:\\s-+" (regexp-quote
  7770. (or article "")))
  7771. nil t)
  7772. (rmail-what-message))))))
  7773. (if message-number
  7774. (progn
  7775. (rmail (if (string= folder "RMAIL") rmail-file-name folder))
  7776. (rmail-show-message message-number)
  7777. message-number)
  7778. (error "Message not found"))))
  7779. ;; mh-e integration based on planner-mode
  7780. (defun org-mhe-get-message-real-folder ()
  7781. "Return the name of the current message real folder, so if you use
  7782. sequences, it will now work."
  7783. (save-excursion
  7784. (let* ((folder
  7785. (if (equal major-mode 'mh-folder-mode)
  7786. mh-current-folder
  7787. ;; Refer to the show buffer
  7788. mh-show-folder-buffer))
  7789. (end-index
  7790. (if (boundp 'mh-index-folder)
  7791. (min (length mh-index-folder) (length folder))))
  7792. )
  7793. ;; a simple test on mh-index-data does not work, because
  7794. ;; mh-index-data is always nil in a show buffer.
  7795. (if (and (boundp 'mh-index-folder)
  7796. (string= mh-index-folder (substring folder 0 end-index)))
  7797. (if (equal major-mode 'mh-show-mode)
  7798. (save-window-excursion
  7799. (when (buffer-live-p (get-buffer folder))
  7800. (progn
  7801. (pop-to-buffer folder)
  7802. (org-mhe-get-message-folder-from-index)
  7803. )
  7804. ))
  7805. (org-mhe-get-message-folder-from-index)
  7806. )
  7807. folder
  7808. )
  7809. )))
  7810. (defun org-mhe-get-message-folder-from-index ()
  7811. "Returns the name of the message folder in a index folder buffer."
  7812. (save-excursion
  7813. (mh-index-previous-folder)
  7814. (re-search-forward "^\\(+.*\\)$" nil t)
  7815. (message (match-string 1))))
  7816. (defun org-mhe-get-message-folder ()
  7817. "Return the name of the current message folder. Be careful if you
  7818. use sequences."
  7819. (save-excursion
  7820. (if (equal major-mode 'mh-folder-mode)
  7821. mh-current-folder
  7822. ;; Refer to the show buffer
  7823. mh-show-folder-buffer)))
  7824. (defun org-mhe-get-message-num ()
  7825. "Return the number of the current message. Be careful if you
  7826. use sequences."
  7827. (save-excursion
  7828. (if (equal major-mode 'mh-folder-mode)
  7829. (mh-get-msg-num nil)
  7830. ;; Refer to the show buffer
  7831. (mh-show-buffer-message-number))))
  7832. (defun org-mhe-get-header (header)
  7833. "Return a header of the message in folder mode. This will create a
  7834. show buffer for the corresponding message. If you have a more clever
  7835. idea..."
  7836. (let* ((folder (org-mhe-get-message-folder))
  7837. (num (org-mhe-get-message-num))
  7838. (buffer (get-buffer-create (concat "show-" folder)))
  7839. (header-field))
  7840. (with-current-buffer buffer
  7841. (mh-display-msg num folder)
  7842. (if (equal major-mode 'mh-folder-mode)
  7843. (mh-header-display)
  7844. (mh-show-header-display))
  7845. (set-buffer buffer)
  7846. (setq header-field (mh-get-header-field header))
  7847. (if (equal major-mode 'mh-folder-mode)
  7848. (mh-show)
  7849. (mh-show-show))
  7850. header-field)))
  7851. (defun org-follow-mhe-link (folder article)
  7852. "Follow an MHE link to FOLDER and ARTICLE.
  7853. If ARTICLE is nil FOLDER is shown. If the configuration variable
  7854. `org-mhe-search-all-folders' is t and `mh-searcher' is pick,
  7855. ARTICLE is searched in all folders. Indexed searches (swish++,
  7856. namazu, and others supported by MH-E) will always search in all
  7857. folders."
  7858. (require 'mh-e)
  7859. (require 'mh-search)
  7860. (require 'mh-utils)
  7861. (mh-find-path)
  7862. (if (not article)
  7863. (mh-visit-folder (mh-normalize-folder-name folder))
  7864. (setq article (org-add-angle-brackets article))
  7865. (mh-search-choose)
  7866. (if (equal mh-searcher 'pick)
  7867. (progn
  7868. (mh-search folder (list "--message-id" article))
  7869. (when (and org-mhe-search-all-folders
  7870. (not (org-mhe-get-message-real-folder)))
  7871. (kill-this-buffer)
  7872. (mh-search "+" (list "--message-id" article))))
  7873. (mh-search "+" article))
  7874. (if (org-mhe-get-message-real-folder)
  7875. (mh-show-msg 1)
  7876. (kill-this-buffer)
  7877. (error "Message not found"))))
  7878. ;; BibTeX links
  7879. ;; Use the custom search meachnism to construct and use search strings for
  7880. ;; file links to BibTeX database entries.
  7881. (defun org-create-file-search-in-bibtex ()
  7882. "Create the search string and description for a BibTeX database entry."
  7883. (when (eq major-mode 'bibtex-mode)
  7884. ;; yes, we want to construct this search string.
  7885. ;; Make a good description for this entry, using names, year and the title
  7886. ;; Put it into the `description' variable which is dynamically scoped.
  7887. (let ((bibtex-autokey-names 1)
  7888. (bibtex-autokey-names-stretch 1)
  7889. (bibtex-autokey-name-case-convert-function 'identity)
  7890. (bibtex-autokey-name-separator " & ")
  7891. (bibtex-autokey-additional-names " et al.")
  7892. (bibtex-autokey-year-length 4)
  7893. (bibtex-autokey-name-year-separator " ")
  7894. (bibtex-autokey-titlewords 3)
  7895. (bibtex-autokey-titleword-separator " ")
  7896. (bibtex-autokey-titleword-case-convert-function 'identity)
  7897. (bibtex-autokey-titleword-length 'infty)
  7898. (bibtex-autokey-year-title-separator ": "))
  7899. (setq description (bibtex-generate-autokey)))
  7900. ;; Now parse the entry, get the key and return it.
  7901. (save-excursion
  7902. (bibtex-beginning-of-entry)
  7903. (cdr (assoc "=key=" (bibtex-parse-entry))))))
  7904. (defun org-execute-file-search-in-bibtex (s)
  7905. "Find the link search string S as a key for a database entry."
  7906. (when (eq major-mode 'bibtex-mode)
  7907. ;; Yes, we want to do the search in this file.
  7908. ;; We construct a regexp that searches for "@entrytype{" followed by the key
  7909. (goto-char (point-min))
  7910. (and (re-search-forward (concat "@[a-zA-Z]+[ \t\n]*{[ \t\n]*"
  7911. (regexp-quote s) "[ \t\n]*,") nil t)
  7912. (goto-char (match-beginning 0)))
  7913. (if (and (match-beginning 0) (equal current-prefix-arg '(16)))
  7914. ;; Use double prefix to indicate that any web link should be browsed
  7915. (let ((b (current-buffer)) (p (point)))
  7916. ;; Restore the window configuration because we just use the web link
  7917. (set-window-configuration org-window-config-before-follow-link)
  7918. (save-excursion (set-buffer b) (goto-char p)
  7919. (bibtex-url)))
  7920. (recenter 0)) ; Move entry start to beginning of window
  7921. ;; return t to indicate that the search is done.
  7922. t))
  7923. ;; Finally add the functions to the right hooks.
  7924. (add-hook 'org-create-file-search-functions 'org-create-file-search-in-bibtex)
  7925. (add-hook 'org-execute-file-search-functions 'org-execute-file-search-in-bibtex)
  7926. ;; end of Bibtex link setup
  7927. (defun org-upgrade-old-links (&optional query-description)
  7928. "Transfer old <...> style links to new [[...]] style links.
  7929. With arg query-description, ask at each match for a description text to use
  7930. for this link."
  7931. (interactive (list (y-or-n-p "Would you like to be queried for a description at each link?")))
  7932. (save-excursion
  7933. (goto-char (point-min))
  7934. (let ((re (concat "\\([^[]\\)<\\("
  7935. "\\(" (mapconcat 'identity org-link-types "\\|")
  7936. "\\):"
  7937. "[^" org-non-link-chars "]+\\)>"))
  7938. l1 l2 (cnt 0))
  7939. (while (re-search-forward re nil t)
  7940. (setq cnt (1+ cnt)
  7941. l1 (org-match-string-no-properties 2)
  7942. l2 (save-match-data (org-link-escape l1)))
  7943. (when query-description (setq l1 (read-string "Desc: " l1)))
  7944. (if (equal l1 l2)
  7945. (replace-match (concat (match-string 1) "[[" l1 "]]") t t)
  7946. (replace-match (concat (match-string 1) "[[" l2 "][" l1 "]]") t t)))
  7947. (message "%d matches have beed treated" cnt))))
  7948. (defun org-open-file (path &optional in-emacs line search)
  7949. "Open the file at PATH.
  7950. First, this expands any special file name abbreviations. Then the
  7951. configuration variable `org-file-apps' is checked if it contains an
  7952. entry for this file type, and if yes, the corresponding command is launched.
  7953. If no application is found, Emacs simply visits the file.
  7954. With optional argument IN-EMACS, Emacs will visit the file.
  7955. Optional LINE specifies a line to go to, optional SEARCH a string to
  7956. search for. If LINE or SEARCH is given, the file will always be
  7957. opened in Emacs.
  7958. If the file does not exist, an error is thrown."
  7959. (setq in-emacs (or in-emacs line search))
  7960. (let* ((file (if (equal path "")
  7961. buffer-file-name
  7962. (convert-standard-filename (org-expand-file-name path))))
  7963. (dirp (file-directory-p file))
  7964. (dfile (downcase file))
  7965. (old-buffer (current-buffer))
  7966. (old-pos (point))
  7967. (old-mode major-mode)
  7968. ext cmd apps)
  7969. (if (and (not (file-exists-p file))
  7970. (not org-open-non-existing-files))
  7971. (error "No such file: %s" file))
  7972. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\.gz\\)$" dfile)
  7973. (setq ext (match-string 1 dfile))
  7974. (if (string-match "^.*\\.\\([a-zA-Z0-9]+\\)$" dfile)
  7975. (setq ext (match-string 1 dfile))))
  7976. (setq apps (append org-file-apps (org-default-apps)))
  7977. (if in-emacs
  7978. (setq cmd 'emacs)
  7979. (setq cmd (or (and dirp (cdr (assoc 'directory apps)))
  7980. (cdr (assoc ext apps))
  7981. (cdr (assoc t apps)))))
  7982. (when (eq cmd 'mailcap)
  7983. (require 'mailcap)
  7984. (mailcap-parse-mailcaps)
  7985. (let* ((mime-type (mailcap-extension-to-mime (or ext "")))
  7986. (command (mailcap-mime-info mime-type)))
  7987. (if (stringp command)
  7988. (setq cmd command)
  7989. (setq cmd 'emacs))))
  7990. (cond
  7991. ((and (stringp cmd) (not (string-match "^\\s-*$" cmd)))
  7992. ;; Normalize use of quote, this can vary.
  7993. (if (string-match "['\"]%s['\"]" cmd)
  7994. (setq cmd (replace-match "'%s'" t t cmd)))
  7995. (setq cmd (format cmd file))
  7996. (save-window-excursion
  7997. (shell-command (concat cmd " &"))))
  7998. ((or (stringp cmd)
  7999. (eq cmd 'emacs))
  8000. (unless (equal (file-truename file) (file-truename (or buffer-file-name "")))
  8001. (funcall (cdr (assq 'file org-link-frame-setup)) file))
  8002. (if line (goto-line line)
  8003. (if search (org-link-search search))))
  8004. ((consp cmd)
  8005. (eval cmd))
  8006. (t (funcall (cdr (assq 'file org-link-frame-setup)) file)))
  8007. (and (eq major-mode 'org-mode) (eq old-mode 'org-mode)
  8008. (or (not (equal old-buffer (current-buffer)))
  8009. (not (equal old-pos (point))))
  8010. (org-mark-ring-push old-pos old-buffer))))
  8011. (defun org-default-apps ()
  8012. "Return the default applications for this operating system."
  8013. (cond
  8014. ((eq system-type 'darwin)
  8015. org-file-apps-defaults-macosx)
  8016. ((eq system-type 'windows-nt)
  8017. org-file-apps-defaults-windowsnt)
  8018. (t org-file-apps-defaults-gnu)))
  8019. (defun org-expand-file-name (path)
  8020. "Replace special path abbreviations and expand the file name."
  8021. (expand-file-name path))
  8022. (defvar org-insert-link-history nil
  8023. "Minibuffer history for links inserted with `org-insert-link'.")
  8024. (defvar org-stored-links nil
  8025. "Contains the links stored with `org-store-link'.")
  8026. ;;;###autoload
  8027. (defun org-store-link (arg)
  8028. "\\<org-mode-map>Store an org-link to the current location.
  8029. This link can later be inserted into an org-buffer with
  8030. \\[org-insert-link].
  8031. For some link types, a prefix arg is interpreted:
  8032. For links to usenet articles, arg negates `org-usenet-links-prefer-google'.
  8033. For file links, arg negates `org-context-in-file-links'."
  8034. (interactive "P")
  8035. (let (link cpltxt desc description search txt (pos (point)))
  8036. (cond
  8037. ((eq major-mode 'bbdb-mode)
  8038. (setq cpltxt (concat
  8039. "bbdb:"
  8040. (or (bbdb-record-name (bbdb-current-record))
  8041. (bbdb-record-company (bbdb-current-record))))
  8042. link (org-make-link cpltxt)))
  8043. ((eq major-mode 'Info-mode)
  8044. (setq link (org-make-link "info:"
  8045. (file-name-nondirectory Info-current-file)
  8046. ":" Info-current-node))
  8047. (setq cpltxt (concat (file-name-nondirectory Info-current-file)
  8048. ":" Info-current-node)))
  8049. ((eq major-mode 'calendar-mode)
  8050. (let ((cd (calendar-cursor-to-date)))
  8051. (setq link
  8052. (format-time-string
  8053. (car org-time-stamp-formats)
  8054. (apply 'encode-time
  8055. (list 0 0 0 (nth 1 cd) (nth 0 cd) (nth 2 cd)
  8056. nil nil nil))))))
  8057. ((or (eq major-mode 'vm-summary-mode)
  8058. (eq major-mode 'vm-presentation-mode))
  8059. (and (eq major-mode 'vm-presentation-mode) (vm-summarize))
  8060. (vm-follow-summary-cursor)
  8061. (save-excursion
  8062. (vm-select-folder-buffer)
  8063. (let* ((message (car vm-message-pointer))
  8064. (folder buffer-file-name)
  8065. (subject (vm-su-subject message))
  8066. (author (vm-su-full-name message))
  8067. (message-id (vm-su-message-id message)))
  8068. (setq message-id (org-remove-angle-brackets message-id))
  8069. (setq folder (abbreviate-file-name folder))
  8070. (if (string-match (concat "^" (regexp-quote vm-folder-directory))
  8071. folder)
  8072. (setq folder (replace-match "" t t folder)))
  8073. (setq cpltxt (concat author " on: " subject))
  8074. (setq link (org-make-link "vm:" folder "#" message-id)))))
  8075. ((eq major-mode 'wl-summary-mode)
  8076. (let* ((msgnum (wl-summary-message-number))
  8077. (message-id (elmo-message-field wl-summary-buffer-elmo-folder
  8078. msgnum 'message-id))
  8079. (wl-message-entity (elmo-msgdb-overview-get-entity
  8080. msgnum (wl-summary-buffer-msgdb)))
  8081. (author (wl-summary-line-from)) ; FIXME: correct?
  8082. (subject "???")) ; FIXME:
  8083. (setq message-id (org-remove-angle-brackets message-id))
  8084. (setq cpltxt (concat author " on: " subject))
  8085. (setq link (org-make-link "wl:" wl-summary-buffer-folder-name
  8086. "#" message-id))))
  8087. ((or (equal major-mode 'mh-folder-mode)
  8088. (equal major-mode 'mh-show-mode))
  8089. (let ((from-header (org-mhe-get-header "From:"))
  8090. (to-header (org-mhe-get-header "To:"))
  8091. (subject (org-mhe-get-header "Subject:")))
  8092. (setq cpltxt (concat from-header " on: " subject))
  8093. (setq link (org-make-link "mhe:" (org-mhe-get-message-real-folder) "#"
  8094. (org-remove-angle-brackets
  8095. (org-mhe-get-header "Message-Id:"))))))
  8096. ((eq major-mode 'rmail-mode)
  8097. (save-excursion
  8098. (save-restriction
  8099. (rmail-narrow-to-non-pruned-header)
  8100. (let ((folder buffer-file-name)
  8101. (message-id (mail-fetch-field "message-id"))
  8102. (author (mail-fetch-field "from"))
  8103. (subject (mail-fetch-field "subject")))
  8104. (setq message-id (org-remove-angle-brackets message-id))
  8105. (setq cpltxt (concat author " on: " subject))
  8106. (setq link (org-make-link "rmail:" folder "#" message-id))))))
  8107. ((eq major-mode 'gnus-group-mode)
  8108. (let ((group (cond ((fboundp 'gnus-group-group-name) ; depending on Gnus
  8109. (gnus-group-group-name)) ; version
  8110. ((fboundp 'gnus-group-name)
  8111. (gnus-group-name))
  8112. (t "???"))))
  8113. (setq cpltxt (concat
  8114. (if (org-xor arg org-usenet-links-prefer-google)
  8115. "http://groups.google.com/groups?group="
  8116. "gnus:")
  8117. group)
  8118. link (org-make-link cpltxt))))
  8119. ((memq major-mode '(gnus-summary-mode gnus-article-mode))
  8120. (and (eq major-mode 'gnus-article-mode) (gnus-article-show-summary))
  8121. (gnus-summary-beginning-of-article)
  8122. (let* ((group (car gnus-article-current))
  8123. (article (cdr gnus-article-current))
  8124. (header (gnus-summary-article-header article))
  8125. (author (mail-header-from header))
  8126. (message-id (mail-header-id header))
  8127. (date (mail-header-date header))
  8128. (subject (gnus-summary-subject-string)))
  8129. (setq cpltxt (concat author " on: " subject))
  8130. (if (org-xor arg org-usenet-links-prefer-google)
  8131. (setq link
  8132. (concat
  8133. cpltxt "\n "
  8134. (format "http://groups.google.com/groups?as_umsgid=%s"
  8135. (org-fixup-message-id-for-http message-id))))
  8136. (setq link (org-make-link "gnus:" group
  8137. "#" (number-to-string article))))))
  8138. ((eq major-mode 'w3-mode)
  8139. (setq cpltxt (url-view-url t)
  8140. link (org-make-link cpltxt)))
  8141. ((eq major-mode 'w3m-mode)
  8142. (setq cpltxt w3m-current-url
  8143. link (org-make-link cpltxt)))
  8144. ((setq search (run-hook-with-args-until-success
  8145. 'org-create-file-search-functions))
  8146. (setq link (concat "file:" (abbreviate-file-name buffer-file-name)
  8147. "::" search))
  8148. (setq cpltxt (or description link)))
  8149. ((eq major-mode 'org-mode)
  8150. ;; Just link to current headline
  8151. (setq cpltxt (concat "file:"
  8152. (abbreviate-file-name buffer-file-name)))
  8153. ;; Add a context search string
  8154. (when (org-xor org-context-in-file-links arg)
  8155. ;; Check if we are on a target
  8156. (if (save-excursion
  8157. (skip-chars-forward "^>\n\r")
  8158. (and (re-search-backward "<<" nil t)
  8159. (looking-at "<<\\(.*?\\)>>")
  8160. (<= (match-beginning 0) pos)
  8161. (>= (match-end 0) pos)))
  8162. (setq cpltxt (concat cpltxt "::" (match-string 1)))
  8163. (setq txt (cond
  8164. ((org-on-heading-p) nil)
  8165. ((org-region-active-p)
  8166. (buffer-substring (region-beginning) (region-end)))
  8167. (t (buffer-substring (point-at-bol) (point-at-eol)))))
  8168. (when (or (null txt) (string-match "\\S-" txt))
  8169. (setq cpltxt
  8170. (concat cpltxt "::"
  8171. (if org-file-link-context-use-camel-case
  8172. (org-make-org-heading-camel txt)
  8173. (org-make-org-heading-search-string txt)))
  8174. desc "NONE"))))
  8175. (if (string-match "::\\'" cpltxt)
  8176. (setq cpltxt (substring cpltxt 0 -2)))
  8177. (setq link (org-make-link cpltxt)))
  8178. (buffer-file-name
  8179. ;; Just link to this file here.
  8180. (setq cpltxt (concat "file:"
  8181. (abbreviate-file-name buffer-file-name)))
  8182. ;; Add a context string
  8183. (when (org-xor org-context-in-file-links arg)
  8184. (setq txt (if (org-region-active-p)
  8185. (buffer-substring (region-beginning) (region-end))
  8186. (buffer-substring (point-at-bol) (point-at-eol))))
  8187. ;; Only use search option if there is some text.
  8188. (when (string-match "\\S-" txt)
  8189. (setq cpltxt
  8190. (concat cpltxt "::"
  8191. (if org-file-link-context-use-camel-case
  8192. (org-make-org-heading-camel txt)
  8193. (org-make-org-heading-search-string txt)))
  8194. desc "NONE")))
  8195. (setq link (org-make-link cpltxt)))
  8196. ((interactive-p)
  8197. (error "Cannot link to a buffer which is not visiting a file"))
  8198. (t (setq link nil)))
  8199. (if (consp link) (setq cpltxt (car link) link (cdr link)))
  8200. (setq link (or link cpltxt)
  8201. desc (or desc cpltxt))
  8202. (if (equal desc "NONE") (setq desc nil))
  8203. (if (and (interactive-p) link)
  8204. (progn
  8205. (setq org-stored-links
  8206. (cons (list cpltxt link desc) org-stored-links))
  8207. (message "Stored: %s" (or cpltxt link)))
  8208. (org-make-link-string link desc))))
  8209. (defun org-make-org-heading-search-string (&optional string heading)
  8210. "Make search string for STRING or current headline."
  8211. (interactive)
  8212. (let ((s (or string (org-get-heading))))
  8213. (unless (and string (not heading))
  8214. ;; We are using a headline, clean up garbage in there.
  8215. (if (string-match org-todo-regexp s)
  8216. (setq s (replace-match "" t t s)))
  8217. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8218. (setq s (replace-match "" t t s)))
  8219. (setq s (org-trim s))
  8220. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8221. org-comment-string "\\)") s)
  8222. (setq s (replace-match "" t t s)))
  8223. (while (string-match org-ts-regexp s)
  8224. (setq s (replace-match "" t t s))))
  8225. (while (string-match "[^a-zA-Z_0-9 \t]+" s)
  8226. (setq s (replace-match " " t t s)))
  8227. (or string (setq s (concat "*" s))) ; Add * for headlines
  8228. (mapconcat 'identity (org-split-string s "[ \t]+") " ")))
  8229. (defun org-make-org-heading-camel (&optional string heading)
  8230. "Make a CamelCase string for STRING or the current headline."
  8231. (interactive)
  8232. (let ((s (or string (org-get-heading))))
  8233. (unless (and string (not heading))
  8234. ;; We are using a headline, clean up garbage in there.
  8235. (if (string-match org-todo-regexp s)
  8236. (setq s (replace-match "" t t s)))
  8237. (if (string-match ":[a-zA-Z_@0-9:]+:[ \t]*$" s)
  8238. (setq s (replace-match "" t t s)))
  8239. (setq s (org-trim s))
  8240. (if (string-match (concat "^\\(" org-quote-string "\\|"
  8241. org-comment-string "\\)") s)
  8242. (setq s (replace-match "" t t s)))
  8243. (while (string-match org-ts-regexp s)
  8244. (setq s (replace-match "" t t s))))
  8245. (while (string-match "[^a-zA-Z_ \t]+" s)
  8246. (setq s (replace-match " " t t s)))
  8247. (or string (setq s (concat "*" s))) ; Add * for headlines
  8248. (mapconcat 'capitalize (org-split-string s "[ \t]+") "")))
  8249. (defun org-make-link (&rest strings)
  8250. "Concatenate STRINGS, format resulting string with `org-link-format'."
  8251. (format org-link-format (apply 'concat strings)))
  8252. (defun org-make-link-string (link &optional description)
  8253. "Make a link with brackets, consisting of LINK and DESCRIPTION."
  8254. (if (eq org-link-style 'plain)
  8255. (if (equal description link)
  8256. link
  8257. (concat description "\n" link))
  8258. (when (stringp description)
  8259. ;; Remove brackets from the description, they are fatal.
  8260. (while (string-match "\\[\\|\\]" description)
  8261. (setq description (replace-match "" t t description))))
  8262. (when (equal (org-link-escape link) description)
  8263. ;; No description needed, it is identical
  8264. (setq description nil))
  8265. (when (and (not description)
  8266. (not (equal link (org-link-escape link))))
  8267. (setq description link))
  8268. (concat "[[" (org-link-escape link) "]"
  8269. (if description (concat "[" description "]") "")
  8270. "]")))
  8271. (defconst org-link-escape-chars '(("[" . "%5B") ("]" . "%5D") (" " . "%20"))
  8272. "Association list of escapes for some characters problematic in links.")
  8273. (defun org-link-escape (text)
  8274. "Escape charaters in TEXT that are problematic for links."
  8275. (when text
  8276. (let ((re (mapconcat (lambda (x) (regexp-quote (car x)))
  8277. org-link-escape-chars "\\|")))
  8278. (while (string-match re text)
  8279. (setq text
  8280. (replace-match
  8281. (cdr (assoc (match-string 0 text) org-link-escape-chars))
  8282. t t text)))
  8283. text)))
  8284. (defun org-link-unescape (text)
  8285. "Reverse the action of `org-link-escape'."
  8286. (when text
  8287. (let ((re (mapconcat (lambda (x) (regexp-quote (cdr x)))
  8288. org-link-escape-chars "\\|")))
  8289. (while (string-match re text)
  8290. (setq text
  8291. (replace-match
  8292. (car (rassoc (match-string 0 text) org-link-escape-chars))
  8293. t t text)))
  8294. text)))
  8295. (defun org-xor (a b)
  8296. "Exclusive or."
  8297. (if a (not b) b))
  8298. (defun org-get-header (header)
  8299. "Find a header field in the current buffer."
  8300. (save-excursion
  8301. (goto-char (point-min))
  8302. (let ((case-fold-search t) s)
  8303. (cond
  8304. ((eq header 'from)
  8305. (if (re-search-forward "^From:\\s-+\\(.*\\)" nil t)
  8306. (setq s (match-string 1)))
  8307. (while (string-match "\"" s)
  8308. (setq s (replace-match "" t t s)))
  8309. (if (string-match "[<(].*" s)
  8310. (setq s (replace-match "" t t s))))
  8311. ((eq header 'message-id)
  8312. (if (re-search-forward "^message-id:\\s-+\\(.*\\)" nil t)
  8313. (setq s (match-string 1))))
  8314. ((eq header 'subject)
  8315. (if (re-search-forward "^subject:\\s-+\\(.*\\)" nil t)
  8316. (setq s (match-string 1)))))
  8317. (if (string-match "\\`[ \t\]+" s) (setq s (replace-match "" t t s)))
  8318. (if (string-match "[ \t\]+\\'" s) (setq s (replace-match "" t t s)))
  8319. s)))
  8320. (defun org-fixup-message-id-for-http (s)
  8321. "Replace special characters in a message id, so it can be used in an http query."
  8322. (while (string-match "<" s)
  8323. (setq s (replace-match "%3C" t t s)))
  8324. (while (string-match ">" s)
  8325. (setq s (replace-match "%3E" t t s)))
  8326. (while (string-match "@" s)
  8327. (setq s (replace-match "%40" t t s)))
  8328. s)
  8329. (defun org-insert-link (&optional complete-file)
  8330. "Insert a link. At the prompt, enter the link.
  8331. Completion can be used to select a link previously stored with
  8332. `org-store-link'. When the empty string is entered (i.e. if you just
  8333. press RET at the prompt), the link defaults to the most recently
  8334. stored link. As SPC triggers completion in the minibuffer, you need to
  8335. use M-SPC or C-q SPC to force the insertion of a space character.
  8336. You will also be prompted for a description, and if one is given, it will
  8337. be displayed in the buffer instead of the link.
  8338. If there is already a link at point, this command will allow you to edit link
  8339. and description parts.
  8340. With a \\[universal-argument] prefix, prompts for a file to link to. The file name can be
  8341. selected using completion. The path to the file will be relative to
  8342. the current directory if the file is in the current directory or a
  8343. subdirectory. Otherwise, the link will be the absolute path as
  8344. completed in the minibuffer (i.e. normally ~/path/to/file).
  8345. With two \\[universal-argument] prefixes, enforce an absolute path even if the file
  8346. is in the current directory or below."
  8347. (interactive "P")
  8348. (let (link desc entry remove file (pos (point)))
  8349. (cond
  8350. ((save-excursion
  8351. (skip-chars-forward "^]\n\r")
  8352. (and (re-search-backward "\\[\\[" nil t)
  8353. (looking-at org-bracket-link-regexp)
  8354. (<= (match-beginning 0) pos)
  8355. (>= (match-end 0) pos)))
  8356. ;; We do have a link at point, and we are going to edit it.
  8357. (setq remove (list (match-beginning 0) (match-end 0)))
  8358. (setq desc (if (match-end 3) (org-match-string-no-properties 3)))
  8359. (setq link (read-string "Link: "
  8360. (org-link-unescape
  8361. (org-match-string-no-properties 1)))))
  8362. (complete-file
  8363. ;; Completing read for file names.
  8364. (setq file (read-file-name "File: "))
  8365. (let ((pwd (file-name-as-directory (expand-file-name ".")))
  8366. (pwd1 (file-name-as-directory (abbreviate-file-name
  8367. (expand-file-name ".")))))
  8368. (cond
  8369. ((equal complete-file '(16))
  8370. (setq link (org-make-link
  8371. "file:"
  8372. (abbreviate-file-name (expand-file-name file)))))
  8373. ((string-match (concat "^" (regexp-quote pwd1) "\\(.+\\)") file)
  8374. (setq link (org-make-link "file:" (match-string 1 file))))
  8375. ((string-match (concat "^" (regexp-quote pwd) "\\(.+\\)")
  8376. (expand-file-name file))
  8377. (setq link (org-make-link
  8378. "file:" (match-string 1 (expand-file-name file)))))
  8379. (t (setq link (org-make-link "file:" file))))))
  8380. (t
  8381. ;; Read link, with completion for stored links.
  8382. (setq link (org-completing-read
  8383. "Link: " org-stored-links nil nil nil
  8384. org-insert-link-history
  8385. (or (car (car org-stored-links)))))
  8386. (setq entry (assoc link org-stored-links))
  8387. (if (not org-keep-stored-link-after-insertion)
  8388. (setq org-stored-links (delq (assoc link org-stored-links)
  8389. org-stored-links)))
  8390. (setq link (if entry (nth 1 entry) link)
  8391. desc (or desc (nth 2 entry)))))
  8392. (if (string-match org-plain-link-re link)
  8393. ;; URL-like link, normalize the use of angular brackets.
  8394. (setq link (org-make-link (org-remove-angle-brackets link))))
  8395. ;; Check if we are linking to the current file with a search option
  8396. ;; If yes, simplify the link by using only the search option.
  8397. (when (string-match "\\<file:\\(.+?\\)::\\([^>]+\\)" link)
  8398. (let* ((path (match-string 1 link))
  8399. (case-fold-search nil)
  8400. (search (match-string 2 link)))
  8401. (save-match-data
  8402. (if (equal (file-truename buffer-file-name) (file-truename path))
  8403. ;; We are linking to this same file, with a search option
  8404. (setq link search)))))
  8405. ;; Check if we can/should use a relative path. If yes, simplify the link
  8406. (when (string-match "\\<file:\\(.*\\)" link)
  8407. (let* ((path (match-string 1 link))
  8408. (case-fold-search nil))
  8409. (cond
  8410. ((eq org-link-file-path-type 'absolute)
  8411. (setq path (abbreviate-file-name (expand-file-name path))))
  8412. ((eq org-link-file-path-type 'noabbrev)
  8413. (setq path (expand-file-name path)))
  8414. ((eq org-link-file-path-type 'relative)
  8415. (setq path (file-relative-name path)))
  8416. (t
  8417. (save-match-data
  8418. (if (string-match (concat "^" (regexp-quote
  8419. (file-name-as-directory
  8420. (expand-file-name "."))))
  8421. (expand-file-name path))
  8422. ;; We are linking a file with relative path name.
  8423. (setq path (substring (expand-file-name path)
  8424. (match-end 0)))))))
  8425. (setq link (concat "file:" path))))
  8426. (setq desc (read-string "Description: " desc))
  8427. (unless (string-match "\\S-" desc) (setq desc nil))
  8428. (if remove (apply 'delete-region remove))
  8429. (insert (org-make-link-string link desc))))
  8430. (defun org-completing-read (&rest args)
  8431. (let ((minibuffer-local-completion-map
  8432. (copy-keymap minibuffer-local-completion-map)))
  8433. (define-key minibuffer-local-completion-map " " 'self-insert-command)
  8434. (apply 'completing-read args)))
  8435. ;;; Hooks for remember.el
  8436. (defvar org-finish-function nil)
  8437. ;;;###autoload
  8438. (defun org-remember-annotation ()
  8439. "Return a link to the current location as an annotation for remember.el.
  8440. If you are using Org-mode files as target for data storage with
  8441. remember.el, then the annotations should include a link compatible with the
  8442. conventions in Org-mode. This function returns such a link."
  8443. (org-store-link nil))
  8444. (defconst org-remember-help
  8445. "Select a destination location for the note.
  8446. UP/DOWN=headline TAB=cycle visibility [Q]uit RET/<left>/<right>=Store
  8447. RET at beg-of-buf -> Append to file as level 2 headline
  8448. RET on headline -> Store as sublevel entry to current headline
  8449. <left>/<right> -> before/after current headline, same headings level")
  8450. ;;;###autoload
  8451. (defun org-remember-apply-template ()
  8452. "Initialize *remember* buffer with template, invoke `org-mode'.
  8453. This function should be placed into `remember-mode-hook' and in fact requires
  8454. to be run from that hook to fucntion properly."
  8455. (if org-remember-templates
  8456. (let* ((entry (if (= (length org-remember-templates) 1)
  8457. (cdar org-remember-templates)
  8458. (message "Select template: %s"
  8459. (mapconcat
  8460. (lambda (x) (char-to-string (car x)))
  8461. org-remember-templates " "))
  8462. (cdr (assoc (read-char-exclusive) org-remember-templates))))
  8463. (tpl (car entry))
  8464. (file (if (consp (cdr entry)) (nth 1 entry)))
  8465. (v-t (format-time-string (car org-time-stamp-formats) (org-current-time)))
  8466. (v-T (format-time-string (cdr org-time-stamp-formats) (org-current-time)))
  8467. (v-u (concat "[" (substring v-t 1 -1) "]"))
  8468. (v-U (concat "[" (substring v-T 1 -1) "]"))
  8469. (v-a annotation) ; defined in `remember-mode'
  8470. (v-i initial) ; defined in `remember-mode'
  8471. (v-n user-full-name)
  8472. )
  8473. (unless tpl (setq tpl "") (message "No template") (ding))
  8474. (insert tpl) (goto-char (point-min))
  8475. (while (re-search-forward "%\\([tTuTai]\\)" nil t)
  8476. (when (and initial (equal (match-string 0) "%i"))
  8477. (save-match-data
  8478. (let* ((lead (buffer-substring
  8479. (point-at-bol) (match-beginning 0))))
  8480. (setq v-i (mapconcat 'identity
  8481. (org-split-string initial "\n")
  8482. (concat "\n" lead))))))
  8483. (replace-match
  8484. (or (eval (intern (concat "v-" (match-string 1)))) "")
  8485. t t))
  8486. (let ((org-startup-folded nil)
  8487. (org-startup-with-deadline-check nil))
  8488. (org-mode))
  8489. (if (and file (string-match "\\S-" file) (not (file-directory-p file)))
  8490. (set (make-local-variable 'org-default-notes-file) file))
  8491. (goto-char (point-min))
  8492. (if (re-search-forward "%\\?" nil t) (replace-match "")))
  8493. (let ((org-startup-folded nil)
  8494. (org-startup-with-deadline-check nil))
  8495. (org-mode)))
  8496. (set (make-local-variable 'org-finish-function) 'remember-buffer))
  8497. ;;;###autoload
  8498. (defun org-remember-handler ()
  8499. "Store stuff from remember.el into an org file.
  8500. First prompts for an org file. If the user just presses return, the value
  8501. of `org-default-notes-file' is used.
  8502. Then the command offers the headings tree of the selected file in order to
  8503. file the text at a specific location.
  8504. You can either immediately press RET to get the note appended to the
  8505. file, or you can use vertical cursor motion and visibility cycling (TAB) to
  8506. find a better place. Then press RET or <left> or <right> in insert the note.
  8507. Key Cursor position Note gets inserted
  8508. -----------------------------------------------------------------------------
  8509. RET buffer-start as level 2 heading at end of file
  8510. RET on headline as sublevel of the heading at cursor
  8511. RET no heading at cursor position, level taken from context.
  8512. Or use prefix arg to specify level manually.
  8513. <left> on headline as same level, before current heading
  8514. <right> on headline as same level, after current heading
  8515. So the fastest way to store the note is to press RET RET to append it to
  8516. the default file. This way your current train of thought is not
  8517. interrupted, in accordance with the principles of remember.el. But with
  8518. little extra effort, you can push it directly to the correct location.
  8519. Before being stored away, the function ensures that the text has a
  8520. headline, i.e. a first line that starts with a \"*\". If not, a headline
  8521. is constructed from the current date and some additional data.
  8522. If the variable `org-adapt-indentation' is non-nil, the entire text is
  8523. also indented so that it starts in the same column as the headline
  8524. \(i.e. after the stars).
  8525. See also the variable `org-reverse-note-order'."
  8526. (catch 'quit
  8527. (let* ((txt (buffer-substring (point-min) (point-max)))
  8528. (fastp current-prefix-arg)
  8529. (file (if fastp org-default-notes-file (org-get-org-file)))
  8530. (visiting (find-buffer-visiting file))
  8531. (org-startup-with-deadline-check nil)
  8532. (org-startup-folded nil)
  8533. (org-startup-align-all-tables nil)
  8534. spos level indent reversed)
  8535. ;; Modify text so that it becomes a nice subtree which can be inserted
  8536. ;; into an org tree.
  8537. (let* ((lines (split-string txt "\n"))
  8538. first)
  8539. ;; remove empty lines at the beginning
  8540. (while (and lines (string-match "^[ \t]*\n" (car lines)))
  8541. (setq lines (cdr lines)))
  8542. (setq first (car lines) lines (cdr lines))
  8543. (if (string-match "^\\*+" first)
  8544. ;; Is already a headline
  8545. (setq indent nil)
  8546. ;; We need to add a headline: Use time and first buffer line
  8547. (setq lines (cons first lines)
  8548. first (concat "* " (current-time-string)
  8549. " (" (remember-buffer-desc) ")")
  8550. indent " "))
  8551. (if (and org-adapt-indentation indent)
  8552. (setq lines (mapcar (lambda (x) (concat indent x)) lines)))
  8553. (setq txt (concat first "\n"
  8554. (mapconcat 'identity lines "\n"))))
  8555. ;; Find the file
  8556. (if (not visiting)
  8557. (find-file-noselect file))
  8558. (with-current-buffer (get-file-buffer file)
  8559. (save-excursion (and (goto-char (point-min))
  8560. (not (re-search-forward "^\\* " nil t))
  8561. (insert "\n* Notes\n")))
  8562. (setq reversed (org-notes-order-reversed-p))
  8563. (save-excursion
  8564. (save-restriction
  8565. (widen)
  8566. ;; Ask the User for a location
  8567. (setq spos (if fastp 1 (org-get-location
  8568. (current-buffer)
  8569. org-remember-help)))
  8570. (if (not spos) (throw 'quit nil)) ; return nil to show we did
  8571. ; not handle this note
  8572. (goto-char spos)
  8573. (cond ((bobp)
  8574. ;; Put it at the start or end, as level 2
  8575. (save-restriction
  8576. (widen)
  8577. (goto-char (if reversed (point-min) (point-max)))
  8578. (if (not (bolp)) (newline))
  8579. (org-paste-subtree 2 txt)))
  8580. ((and (org-on-heading-p nil) (not current-prefix-arg))
  8581. ;; Put it below this entry, at the beg/end of the subtree
  8582. (org-back-to-heading)
  8583. (setq level (funcall outline-level))
  8584. (if reversed
  8585. (outline-end-of-heading)
  8586. (outline-end-of-subtree))
  8587. (if (not (bolp)) (newline))
  8588. (beginning-of-line 1)
  8589. (org-paste-subtree (1+ level) txt))
  8590. (t
  8591. ;; Put it right there, with automatic level determined by
  8592. ;; org-paste-subtree or from prefix arg
  8593. (org-paste-subtree current-prefix-arg txt)))
  8594. (when remember-save-after-remembering
  8595. (save-buffer)
  8596. (if (not visiting) (kill-buffer (current-buffer)))))))))
  8597. t) ;; return t to indicate that we took care of this note.
  8598. (defun org-get-org-file ()
  8599. "Read a filename, with default directory `org-directory'."
  8600. (let ((default (or org-default-notes-file remember-data-file)))
  8601. (read-file-name (format "File name [%s]: " default)
  8602. (file-name-as-directory org-directory)
  8603. default)))
  8604. (defun org-notes-order-reversed-p ()
  8605. "Check if the current file should receive notes in reversed order."
  8606. (cond
  8607. ((not org-reverse-note-order) nil)
  8608. ((eq t org-reverse-note-order) t)
  8609. ((not (listp org-reverse-note-order)) nil)
  8610. (t (catch 'exit
  8611. (let ((all org-reverse-note-order)
  8612. entry)
  8613. (while (setq entry (pop all))
  8614. (if (string-match (car entry) buffer-file-name)
  8615. (throw 'exit (cdr entry))))
  8616. nil)))))
  8617. ;;; Tables
  8618. ;; Watch out: Here we are talking about two different kind of tables.
  8619. ;; Most of the code is for the tables created with the Org-mode table editor.
  8620. ;; Sometimes, we talk about tables created and edited with the table.el
  8621. ;; Emacs package. We call the former org-type tables, and the latter
  8622. ;; table.el-type tables.
  8623. (defun org-before-change-function (beg end)
  8624. "Every change indicates that a table might need an update."
  8625. (setq org-table-may-need-update t))
  8626. (defconst org-table-line-regexp "^[ \t]*|"
  8627. "Detects an org-type table line.")
  8628. (defconst org-table-dataline-regexp "^[ \t]*|[^-]"
  8629. "Detects an org-type table line.")
  8630. (defconst org-table-auto-recalculate-regexp "^[ \t]*| *# *\\(|\\|$\\)"
  8631. "Detects a table line marked for automatic recalculation.")
  8632. (defconst org-table-recalculate-regexp "^[ \t]*| *[#*] *\\(|\\|$\\)"
  8633. "Detects a table line marked for automatic recalculation.")
  8634. (defconst org-table-calculate-mark-regexp "^[ \t]*| *[!$^_#*] *\\(|\\|$\\)"
  8635. "Detects a table line marked for automatic recalculation.")
  8636. (defconst org-table-hline-regexp "^[ \t]*|-"
  8637. "Detects an org-type table hline.")
  8638. (defconst org-table1-hline-regexp "^[ \t]*\\+-[-+]"
  8639. "Detects a table-type table hline.")
  8640. (defconst org-table-any-line-regexp "^[ \t]*\\(|\\|\\+-[-+]\\)"
  8641. "Detects an org-type or table-type table.")
  8642. (defconst org-table-border-regexp "^[ \t]*[^| \t]"
  8643. "Searching from within a table (any type) this finds the first line
  8644. outside the table.")
  8645. (defconst org-table-any-border-regexp "^[ \t]*[^|+ \t]"
  8646. "Searching from within a table (any type) this finds the first line
  8647. outside the table.")
  8648. (defun org-table-create-with-table.el ()
  8649. "Use the table.el package to insert a new table.
  8650. If there is already a table at point, convert between Org-mode tables
  8651. and table.el tables."
  8652. (interactive)
  8653. (require 'table)
  8654. (cond
  8655. ((org-at-table.el-p)
  8656. (if (y-or-n-p "Convert table to Org-mode table? ")
  8657. (org-table-convert)))
  8658. ((org-at-table-p)
  8659. (if (y-or-n-p "Convert table to table.el table? ")
  8660. (org-table-convert)))
  8661. (t (call-interactively 'table-insert))))
  8662. (defun org-table-create-or-convert-from-region (arg)
  8663. "Convert region to table, or create an empty table.
  8664. If there is an active region, convert it to a table. If there is no such
  8665. region, create an empty table."
  8666. (interactive "P")
  8667. (if (org-region-active-p)
  8668. (org-table-convert-region (region-beginning) (region-end) arg)
  8669. (org-table-create arg)))
  8670. (defun org-table-create (&optional size)
  8671. "Query for a size and insert a table skeleton.
  8672. SIZE is a string Columns x Rows like for example \"3x2\"."
  8673. (interactive "P")
  8674. (unless size
  8675. (setq size (read-string
  8676. (concat "Table size Columns x Rows [e.g. "
  8677. org-table-default-size "]: ")
  8678. "" nil org-table-default-size)))
  8679. (let* ((pos (point))
  8680. (indent (make-string (current-column) ?\ ))
  8681. (split (org-split-string size " *x *"))
  8682. (rows (string-to-number (nth 1 split)))
  8683. (columns (string-to-number (car split)))
  8684. (line (concat (apply 'concat indent "|" (make-list columns " |"))
  8685. "\n")))
  8686. (if (string-match "^[ \t]*$" (buffer-substring-no-properties
  8687. (point-at-bol) (point)))
  8688. (beginning-of-line 1)
  8689. (newline))
  8690. ;; (mapcar (lambda (x) (insert line)) (make-list rows t))
  8691. (dotimes (i rows) (insert line))
  8692. (goto-char pos)
  8693. (if (> rows 1)
  8694. ;; Insert a hline after the first row.
  8695. (progn
  8696. (end-of-line 1)
  8697. (insert "\n|-")
  8698. (goto-char pos)))
  8699. (org-table-align)))
  8700. (defun org-table-convert-region (beg0 end0 &optional nspace)
  8701. "Convert region to a table.
  8702. The region goes from BEG0 to END0, but these borders will be moved
  8703. slightly, to make sure a beginning of line in the first line is included.
  8704. When NSPACE is non-nil, it indicates the minimum number of spaces that
  8705. separate columns (default: just one space)."
  8706. (interactive "rP")
  8707. (let* ((beg (min beg0 end0))
  8708. (end (max beg0 end0))
  8709. (tabsep t)
  8710. re)
  8711. (goto-char beg)
  8712. (beginning-of-line 1)
  8713. (setq beg (move-marker (make-marker) (point)))
  8714. (goto-char end)
  8715. (if (bolp) (backward-char 1) (end-of-line 1))
  8716. (setq end (move-marker (make-marker) (point)))
  8717. ;; Lets see if this is tab-separated material. If every nonempty line
  8718. ;; contains a tab, we will assume that it is tab-separated material
  8719. (if nspace
  8720. (setq tabsep nil)
  8721. (goto-char beg)
  8722. (and (re-search-forward "^[^\n\t]+$" end t) (setq tabsep nil)))
  8723. (if nspace (setq tabsep nil))
  8724. (if tabsep
  8725. (setq re "^\\|\t")
  8726. (setq re (format "^ *\\| *\t *\\| \\{%d,\\}"
  8727. (max 1 (prefix-numeric-value nspace)))))
  8728. (goto-char beg)
  8729. (while (re-search-forward re end t)
  8730. (replace-match "|" t t))
  8731. (goto-char beg)
  8732. (insert " ")
  8733. (org-table-align)))
  8734. (defun org-table-import (file arg)
  8735. "Import FILE as a table.
  8736. The file is assumed to be tab-separated. Such files can be produced by most
  8737. spreadsheet and database applications. If no tabs (at least one per line)
  8738. are found, lines will be split on whitespace into fields."
  8739. (interactive "f\nP")
  8740. (or (bolp) (newline))
  8741. (let ((beg (point))
  8742. (pm (point-max)))
  8743. (insert-file-contents file)
  8744. (org-table-convert-region beg (+ (point) (- (point-max) pm)) arg)))
  8745. (defun org-table-export ()
  8746. "Export table as a tab-separated file.
  8747. Such a file can be imported into a spreadsheet program like Excel."
  8748. (interactive)
  8749. (let* ((beg (org-table-begin))
  8750. (end (org-table-end))
  8751. (table (buffer-substring beg end))
  8752. (file (read-file-name "Export table to: "))
  8753. buf)
  8754. (unless (or (not (file-exists-p file))
  8755. (y-or-n-p (format "Overwrite file %s? " file)))
  8756. (error "Abort"))
  8757. (with-current-buffer (find-file-noselect file)
  8758. (setq buf (current-buffer))
  8759. (erase-buffer)
  8760. (fundamental-mode)
  8761. (insert table)
  8762. (goto-char (point-min))
  8763. (while (re-search-forward "^[ \t]*|[ \t]*" nil t)
  8764. (replace-match "" t t)
  8765. (end-of-line 1))
  8766. (goto-char (point-min))
  8767. (while (re-search-forward "[ \t]*|[ \t]*$" nil t)
  8768. (replace-match "" t t)
  8769. (goto-char (min (1+ (point)) (point-max))))
  8770. (goto-char (point-min))
  8771. (while (re-search-forward "^-[-+]*$" nil t)
  8772. (replace-match "")
  8773. (if (looking-at "\n")
  8774. (delete-char 1)))
  8775. (goto-char (point-min))
  8776. (while (re-search-forward "[ \t]*|[ \t]*" nil t)
  8777. (replace-match "\t" t t))
  8778. (save-buffer))
  8779. (kill-buffer buf)))
  8780. (defvar org-table-aligned-begin-marker (make-marker)
  8781. "Marker at the beginning of the table last aligned.
  8782. Used to check if cursor still is in that table, to minimize realignment.")
  8783. (defvar org-table-aligned-end-marker (make-marker)
  8784. "Marker at the end of the table last aligned.
  8785. Used to check if cursor still is in that table, to minimize realignment.")
  8786. (defvar org-table-last-alignment nil
  8787. "List of flags for flushright alignment, from the last re-alignment.
  8788. This is being used to correctly align a single field after TAB or RET.")
  8789. (defvar org-table-last-column-widths nil
  8790. "List of max width of fields in each column.
  8791. This is being used to correctly align a single field after TAB or RET.")
  8792. (defvar org-last-recalc-line nil)
  8793. (defconst org-narrow-column-arrow "=>"
  8794. "Used as display property in narrowed table columns.")
  8795. (defun org-table-align ()
  8796. "Align the table at point by aligning all vertical bars."
  8797. (interactive)
  8798. (let* (
  8799. ;; Limits of table
  8800. (beg (org-table-begin))
  8801. (end (org-table-end))
  8802. ;; Current cursor position
  8803. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  8804. (colpos (org-table-current-column))
  8805. (winstart (window-start))
  8806. text lines (new "") lengths l typenums ty fields maxfields i
  8807. column
  8808. (indent "") cnt frac
  8809. rfmt hfmt
  8810. (spaces '(1 . 1))
  8811. (sp1 (car spaces))
  8812. (sp2 (cdr spaces))
  8813. (rfmt1 (concat
  8814. (make-string sp2 ?\ ) "%%%s%ds" (make-string sp1 ?\ ) "|"))
  8815. (hfmt1 (concat
  8816. (make-string sp2 ?-) "%s" (make-string sp1 ?-) "+"))
  8817. emptystrings xx links narrow fmax fmin f1 len c e)
  8818. (untabify beg end)
  8819. (remove-text-properties beg end '(org-cwidth t display t))
  8820. ;; Check if we have links
  8821. (goto-char beg)
  8822. (setq links (re-search-forward org-bracket-link-regexp end t))
  8823. ;; Make sure the link properties are right
  8824. (when links (goto-char beg) (while (org-activate-bracket-links end)))
  8825. ;; Check if we are narrowing any columns
  8826. (goto-char beg)
  8827. (setq narrow (and org-format-transports-properties-p
  8828. (re-search-forward "<[0-9]+>" end t)))
  8829. ;; Get the rows
  8830. (setq lines (org-split-string
  8831. (buffer-substring beg end) "\n"))
  8832. ;; Store the indentation of the first line
  8833. (if (string-match "^ *" (car lines))
  8834. (setq indent (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  8835. ;; Mark the hlines by setting the corresponding element to nil
  8836. ;; At the same time, we remove trailing space.
  8837. (setq lines (mapcar (lambda (l)
  8838. (if (string-match "^ *|-" l)
  8839. nil
  8840. (if (string-match "[ \t]+$" l)
  8841. (substring l 0 (match-beginning 0))
  8842. l)))
  8843. lines))
  8844. ;; Get the data fields by splitting the lines.
  8845. (setq fields (mapcar
  8846. (lambda (l)
  8847. (org-split-string l " *| *"))
  8848. (delq nil (copy-sequence lines))))
  8849. ;; How many fields in the longest line?
  8850. (condition-case nil
  8851. (setq maxfields (apply 'max (mapcar 'length fields)))
  8852. (error
  8853. (kill-region beg end)
  8854. (org-table-create org-table-default-size)
  8855. (error "Empty table - created default table")))
  8856. ;; A list of empty string to fill any short rows on output
  8857. (setq emptystrings (make-list maxfields ""))
  8858. ;; Check for special formatting.
  8859. (setq i -1)
  8860. (while (< (setq i (1+ i)) maxfields) ;; Loop over all columns
  8861. (setq column (mapcar (lambda (x) (or (nth i x) "")) fields))
  8862. ;; Check if there is an explicit width specified
  8863. (when (and org-table-limit-column-width narrow)
  8864. (setq c column fmax nil)
  8865. (while c
  8866. (setq e (pop c))
  8867. (if (and (stringp e) (string-match "^<\\([0-9]+\\)>$" e))
  8868. (setq fmax (string-to-number (match-string 1 e)) c nil)))
  8869. ;; Find fields that are wider than fmax, and shorten them
  8870. (when fmax
  8871. (loop for xx in column do
  8872. (when (and (stringp xx)
  8873. (> (org-string-width xx) fmax))
  8874. (org-add-props xx nil
  8875. 'help-echo
  8876. (concat "Clipped table field, use C-c ` to edit. Full value is:\n" (org-no-properties (copy-sequence xx))))
  8877. (setq f1 (min fmax (or (string-match org-bracket-link-regexp xx) fmax)))
  8878. (unless (> f1 1)
  8879. (error "Cannot narrow field starting with wide link \"%s\""
  8880. (match-string 0 xx)))
  8881. (add-text-properties f1 (length xx) (list 'org-cwidth t) xx)
  8882. (add-text-properties (- f1 2) f1
  8883. (list 'display org-narrow-column-arrow)
  8884. xx)))))
  8885. ;; Get the maximum width for each column
  8886. (push (apply 'max 1 (mapcar 'org-string-width column)) lengths)
  8887. ;; Get the fraction of numbers, to decide about alignment of the column
  8888. (setq cnt 0 frac 0.0)
  8889. (loop for x in column do
  8890. (if (equal x "")
  8891. nil
  8892. (setq frac ( / (+ (* frac cnt)
  8893. (if (string-match org-table-number-regexp x) 1 0))
  8894. (setq cnt (1+ cnt))))))
  8895. (push (>= frac org-table-number-fraction) typenums))
  8896. (setq lengths (nreverse lengths) typenums (nreverse typenums))
  8897. ;; Store the alignment of this table, for later editing of single fields
  8898. (setq org-table-last-alignment typenums
  8899. org-table-last-column-widths lengths)
  8900. ;; With invisible characters, `format' does not get the field width right
  8901. ;; So we need to make these fields wide by hand.
  8902. (when links
  8903. (loop for i from 0 upto (1- maxfields) do
  8904. (setq len (nth i lengths))
  8905. (loop for j from 0 upto (1- (length fields)) do
  8906. (setq c (nthcdr i (car (nthcdr j fields))))
  8907. (if (and (stringp (car c))
  8908. (string-match org-bracket-link-regexp (car c))
  8909. (< (org-string-width (car c)) len))
  8910. (setcar c (concat (car c) (make-string (- len (org-string-width (car c))) ?\ )))))))
  8911. ;; Compute the formats needed for output of the table
  8912. (setq rfmt (concat indent "|") hfmt (concat indent "|"))
  8913. (while (setq l (pop lengths))
  8914. (setq ty (if (pop typenums) "" "-")) ; number types flushright
  8915. (setq rfmt (concat rfmt (format rfmt1 ty l))
  8916. hfmt (concat hfmt (format hfmt1 (make-string l ?-)))))
  8917. (setq rfmt (concat rfmt "\n")
  8918. hfmt (concat (substring hfmt 0 -1) "|\n"))
  8919. (setq new (mapconcat
  8920. (lambda (l)
  8921. (if l (apply 'format rfmt
  8922. (append (pop fields) emptystrings))
  8923. hfmt))
  8924. lines ""))
  8925. ;; Replace the old one
  8926. (delete-region beg end)
  8927. (move-marker end nil)
  8928. (move-marker org-table-aligned-begin-marker (point))
  8929. (insert new)
  8930. (move-marker org-table-aligned-end-marker (point))
  8931. (when (and orgtbl-mode (not (eq major-mode 'org-mode)))
  8932. (goto-char org-table-aligned-begin-marker)
  8933. (while (org-hide-wide-columns org-table-aligned-end-marker)))
  8934. ;; Try to move to the old location (approximately)
  8935. (goto-line linepos)
  8936. (set-window-start (selected-window) winstart 'noforce)
  8937. (org-table-goto-column colpos)
  8938. (setq org-table-may-need-update nil)
  8939. ))
  8940. (defun org-string-width (s)
  8941. "Compute width of string, ignoring invisible characters.
  8942. This ignores character with invisibility property `org-link', and also
  8943. characters with property `org-cwidth', because these will become invisible
  8944. upon the next fontification round."
  8945. (let (b)
  8946. (when (or (eq t buffer-invisibility-spec)
  8947. (assq 'org-link buffer-invisibility-spec))
  8948. (while (setq b (text-property-any 0 (length s)
  8949. 'invisible 'org-link s))
  8950. (setq s (concat (substring s 0 b)
  8951. (substring s (or (next-single-property-change
  8952. b 'invisible s) (length s)))))))
  8953. (while (setq b (text-property-any 0 (length s) 'org-cwidth t s))
  8954. (setq s (concat (substring s 0 b)
  8955. (substring s (or (next-single-property-change
  8956. b 'org-cwidth s) (length s))))))
  8957. (string-width s)))
  8958. (defun org-table-begin (&optional table-type)
  8959. "Find the beginning of the table and return its position.
  8960. With argument TABLE-TYPE, go to the beginning of a table.el-type table."
  8961. (save-excursion
  8962. (if (not (re-search-backward
  8963. (if table-type org-table-any-border-regexp
  8964. org-table-border-regexp)
  8965. nil t))
  8966. (progn (goto-char (point-min)) (point))
  8967. (goto-char (match-beginning 0))
  8968. (beginning-of-line 2)
  8969. (point))))
  8970. (defun org-table-end (&optional table-type)
  8971. "Find the end of the table and return its position.
  8972. With argument TABLE-TYPE, go to the end of a table.el-type table."
  8973. (save-excursion
  8974. (if (not (re-search-forward
  8975. (if table-type org-table-any-border-regexp
  8976. org-table-border-regexp)
  8977. nil t))
  8978. (goto-char (point-max))
  8979. (goto-char (match-beginning 0)))
  8980. (point-marker)))
  8981. (defun org-table-justify-field-maybe (&optional new)
  8982. "Justify the current field, text to left, number to right.
  8983. Optional argument NEW may specify text to replace the current field content."
  8984. (cond
  8985. ((and (not new) org-table-may-need-update)) ; Realignment will happen anyway
  8986. ((org-at-table-hline-p))
  8987. ((and (not new)
  8988. (or (not (equal (marker-buffer org-table-aligned-begin-marker)
  8989. (current-buffer)))
  8990. (< (point) org-table-aligned-begin-marker)
  8991. (>= (point) org-table-aligned-end-marker)))
  8992. ;; This is not the same table, force a full re-align
  8993. (setq org-table-may-need-update t))
  8994. (t ;; realign the current field, based on previous full realign
  8995. (let* ((pos (point)) s
  8996. (col (org-table-current-column))
  8997. (num (if (> col 0) (nth (1- col) org-table-last-alignment)))
  8998. l f n o e)
  8999. (when (> col 0)
  9000. (skip-chars-backward "^|\n")
  9001. (if (looking-at " *\\([^|\n]*?\\) *\\(|\\|$\\)")
  9002. (progn
  9003. (setq s (match-string 1)
  9004. o (match-string 0)
  9005. l (max 1 (- (match-end 0) (match-beginning 0) 3))
  9006. e (not (= (match-beginning 2) (match-end 2))))
  9007. (setq f (format (if num " %%%ds %s" " %%-%ds %s")
  9008. l (if e "|" (setq org-table-may-need-update t) ""))
  9009. n (format f s))
  9010. (if new
  9011. (if (<= (length new) l) ;; FIXME: length -> str-width?
  9012. (setq n (format f new))
  9013. (setq n (concat new "|") org-table-may-need-update t)))
  9014. (or (equal n o)
  9015. (let (org-table-may-need-update)
  9016. (replace-match n))))
  9017. (setq org-table-may-need-update t))
  9018. (goto-char pos))))))
  9019. (defun org-table-next-field ()
  9020. "Go to the next field in the current table, creating new lines as needed.
  9021. Before doing so, re-align the table if necessary."
  9022. (interactive)
  9023. (org-table-maybe-eval-formula)
  9024. (org-table-maybe-recalculate-line)
  9025. (if (and org-table-automatic-realign
  9026. org-table-may-need-update)
  9027. (org-table-align))
  9028. (let ((end (org-table-end)))
  9029. (if (org-at-table-hline-p)
  9030. (end-of-line 1))
  9031. (condition-case nil
  9032. (progn
  9033. (re-search-forward "|" end)
  9034. (if (looking-at "[ \t]*$")
  9035. (re-search-forward "|" end))
  9036. (if (and (looking-at "-")
  9037. org-table-tab-jumps-over-hlines
  9038. (re-search-forward "^[ \t]*|\\([^-]\\)" end t))
  9039. (goto-char (match-beginning 1)))
  9040. (if (looking-at "-")
  9041. (progn
  9042. (beginning-of-line 0)
  9043. (org-table-insert-row 'below))
  9044. (if (looking-at " ") (forward-char 1))))
  9045. (error
  9046. (org-table-insert-row 'below)))))
  9047. (defun org-table-previous-field ()
  9048. "Go to the previous field in the table.
  9049. Before doing so, re-align the table if necessary."
  9050. (interactive)
  9051. (org-table-justify-field-maybe)
  9052. (org-table-maybe-recalculate-line)
  9053. (if (and org-table-automatic-realign
  9054. org-table-may-need-update)
  9055. (org-table-align))
  9056. (if (org-at-table-hline-p)
  9057. (end-of-line 1))
  9058. (re-search-backward "|" (org-table-begin))
  9059. (re-search-backward "|" (org-table-begin))
  9060. (while (looking-at "|\\(-\\|[ \t]*$\\)")
  9061. (re-search-backward "|" (org-table-begin)))
  9062. (if (looking-at "| ?")
  9063. (goto-char (match-end 0))))
  9064. (defun org-table-next-row ()
  9065. "Go to the next row (same column) in the current table.
  9066. Before doing so, re-align the table if necessary."
  9067. (interactive)
  9068. (org-table-maybe-eval-formula)
  9069. (org-table-maybe-recalculate-line)
  9070. (if (or (looking-at "[ \t]*$")
  9071. (save-excursion (skip-chars-backward " \t") (bolp)))
  9072. (newline)
  9073. (if (and org-table-automatic-realign
  9074. org-table-may-need-update)
  9075. (org-table-align))
  9076. (let ((col (org-table-current-column)))
  9077. (beginning-of-line 2)
  9078. (if (or (not (org-at-table-p))
  9079. (org-at-table-hline-p))
  9080. (progn
  9081. (beginning-of-line 0)
  9082. (org-table-insert-row 'below)))
  9083. (org-table-goto-column col)
  9084. (skip-chars-backward "^|\n\r")
  9085. (if (looking-at " ") (forward-char 1)))))
  9086. (defun org-table-copy-down (n)
  9087. "Copy a field down in the current column.
  9088. If the field at the cursor is empty, copy into it the content of the nearest
  9089. non-empty field above. With argument N, use the Nth non-empty field.
  9090. If the current field is not empty, it is copied down to the next row, and
  9091. the cursor is moved with it. Therefore, repeating this command causes the
  9092. column to be filled row-by-row.
  9093. If the variable `org-table-copy-increment' is non-nil and the field is an
  9094. integer, it will be incremented while copying."
  9095. (interactive "p")
  9096. (let* ((colpos (org-table-current-column))
  9097. (field (org-table-get-field))
  9098. (non-empty (string-match "[^ \t]" field))
  9099. (beg (org-table-begin))
  9100. txt)
  9101. (org-table-check-inside-data-field)
  9102. (if non-empty
  9103. (progn
  9104. (setq txt (org-trim field))
  9105. (org-table-next-row)
  9106. (org-table-blank-field))
  9107. (save-excursion
  9108. (setq txt
  9109. (catch 'exit
  9110. (while (progn (beginning-of-line 1)
  9111. (re-search-backward org-table-dataline-regexp
  9112. beg t))
  9113. (org-table-goto-column colpos t)
  9114. (if (and (looking-at
  9115. "|[ \t]*\\([^| \t][^|]*?\\)[ \t]*|")
  9116. (= (setq n (1- n)) 0))
  9117. (throw 'exit (match-string 1))))))))
  9118. (if txt
  9119. (progn
  9120. (if (and org-table-copy-increment
  9121. (string-match "^[0-9]+$" txt))
  9122. (setq txt (format "%d" (+ (string-to-number txt) 1))))
  9123. (insert txt)
  9124. (org-table-maybe-recalculate-line)
  9125. (org-table-align))
  9126. (error "No non-empty field found"))))
  9127. (defun org-table-check-inside-data-field ()
  9128. "Is point inside a table data field?
  9129. I.e. not on a hline or before the first or after the last column?
  9130. This actually throws an error, so it aborts the current command."
  9131. (if (or (not (org-at-table-p))
  9132. (= (org-table-current-column) 0)
  9133. (org-at-table-hline-p)
  9134. (looking-at "[ \t]*$"))
  9135. (error "Not in table data field")))
  9136. (defvar org-table-clip nil
  9137. "Clipboard for table regions.")
  9138. (defun org-table-blank-field ()
  9139. "Blank the current table field or active region."
  9140. (interactive)
  9141. (org-table-check-inside-data-field)
  9142. (if (and (interactive-p) (org-region-active-p))
  9143. (let (org-table-clip)
  9144. (org-table-cut-region (region-beginning) (region-end)))
  9145. (skip-chars-backward "^|")
  9146. (backward-char 1)
  9147. (if (looking-at "|[^|\n]+")
  9148. (let* ((pos (match-beginning 0))
  9149. (match (match-string 0))
  9150. (len (org-string-width match)))
  9151. (replace-match (concat "|" (make-string (1- len) ?\ )))
  9152. (goto-char (+ 2 pos))
  9153. (substring match 1)))))
  9154. (defun org-table-get-field (&optional n replace)
  9155. "Return the value of the field in column N of current row.
  9156. N defaults to current field.
  9157. If REPLACE is a string, replace field with this value. The return value
  9158. is always the old value."
  9159. (and n (org-table-goto-column n))
  9160. (skip-chars-backward "^|\n")
  9161. (backward-char 1)
  9162. (if (looking-at "|[^|\r\n]*")
  9163. (let* ((pos (match-beginning 0))
  9164. (val (buffer-substring (1+ pos) (match-end 0))))
  9165. (if replace
  9166. (replace-match (concat "|" replace)))
  9167. (goto-char (min (point-at-eol) (+ 2 pos)))
  9168. val)
  9169. (forward-char 1) ""))
  9170. (defun org-table-current-column ()
  9171. "Find out which column we are in.
  9172. When called interactively, column is also displayed in echo area."
  9173. (interactive)
  9174. (if (interactive-p) (org-table-check-inside-data-field))
  9175. (save-excursion
  9176. (let ((cnt 0) (pos (point)))
  9177. (beginning-of-line 1)
  9178. (while (search-forward "|" pos t)
  9179. (setq cnt (1+ cnt)))
  9180. (if (interactive-p) (message "This is table column %d" cnt))
  9181. cnt)))
  9182. (defun org-table-goto-column (n &optional on-delim force)
  9183. "Move the cursor to the Nth column in the current table line.
  9184. With optional argument ON-DELIM, stop with point before the left delimiter
  9185. of the field.
  9186. If there are less than N fields, just go to after the last delimiter.
  9187. However, when FORCE is non-nil, create new columns if necessary."
  9188. (interactive "p")
  9189. (let ((pos (point-at-eol)))
  9190. (beginning-of-line 1)
  9191. (when (> n 0)
  9192. (while (and (> (setq n (1- n)) -1)
  9193. (or (search-forward "|" pos t)
  9194. (and force
  9195. (progn (end-of-line 1)
  9196. (skip-chars-backward "^|")
  9197. (insert " | "))))))
  9198. ; (backward-char 2) t)))))
  9199. (when (and force (not (looking-at ".*|")))
  9200. (save-excursion (end-of-line 1) (insert " | ")))
  9201. (if on-delim
  9202. (backward-char 1)
  9203. (if (looking-at " ") (forward-char 1))))))
  9204. (defun org-at-table-p (&optional table-type)
  9205. "Return t if the cursor is inside an org-type table.
  9206. If TABLE-TYPE is non-nil, also check for table.el-type tables."
  9207. (if org-enable-table-editor
  9208. (save-excursion
  9209. (beginning-of-line 1)
  9210. (looking-at (if table-type org-table-any-line-regexp
  9211. org-table-line-regexp)))
  9212. nil))
  9213. (defun org-at-table.el-p ()
  9214. "Return t if and only if we are at a table.el table."
  9215. (and (org-at-table-p 'any)
  9216. (save-excursion
  9217. (goto-char (org-table-begin 'any))
  9218. (looking-at org-table1-hline-regexp))))
  9219. (defun org-table-recognize-table.el ()
  9220. "If there is a table.el table nearby, recognize it and move into it."
  9221. (if org-table-tab-recognizes-table.el
  9222. (if (org-at-table.el-p)
  9223. (progn
  9224. (beginning-of-line 1)
  9225. (if (looking-at org-table-dataline-regexp)
  9226. nil
  9227. (if (looking-at org-table1-hline-regexp)
  9228. (progn
  9229. (beginning-of-line 2)
  9230. (if (looking-at org-table-any-border-regexp)
  9231. (beginning-of-line -1)))))
  9232. (if (re-search-forward "|" (org-table-end t) t)
  9233. (progn
  9234. (require 'table)
  9235. (if (table--at-cell-p (point))
  9236. t
  9237. (message "recognizing table.el table...")
  9238. (table-recognize-table)
  9239. (message "recognizing table.el table...done")))
  9240. (error "This should not happen..."))
  9241. t)
  9242. nil)
  9243. nil))
  9244. (defun org-at-table-hline-p ()
  9245. "Return t if the cursor is inside a hline in a table."
  9246. (if org-enable-table-editor
  9247. (save-excursion
  9248. (beginning-of-line 1)
  9249. (looking-at org-table-hline-regexp))
  9250. nil))
  9251. (defun org-table-insert-column ()
  9252. "Insert a new column into the table."
  9253. (interactive)
  9254. (if (not (org-at-table-p))
  9255. (error "Not at a table"))
  9256. (org-table-find-dataline)
  9257. (let* ((col (max 1 (org-table-current-column)))
  9258. (beg (org-table-begin))
  9259. (end (org-table-end))
  9260. ;; Current cursor position
  9261. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9262. (colpos col))
  9263. (goto-char beg)
  9264. (while (< (point) end)
  9265. (if (org-at-table-hline-p)
  9266. nil
  9267. (org-table-goto-column col t)
  9268. (insert "| "))
  9269. (beginning-of-line 2))
  9270. (move-marker end nil)
  9271. (goto-line linepos)
  9272. (org-table-goto-column colpos)
  9273. (org-table-align)
  9274. (org-table-modify-formulas 'insert col)))
  9275. (defun org-table-find-dataline ()
  9276. "Find a dataline in the current table, which is needed for column commands."
  9277. (if (and (org-at-table-p)
  9278. (not (org-at-table-hline-p)))
  9279. t
  9280. (let ((col (current-column))
  9281. (end (org-table-end)))
  9282. (move-to-column col)
  9283. (while (and (< (point) end)
  9284. (or (not (= (current-column) col))
  9285. (org-at-table-hline-p)))
  9286. (beginning-of-line 2)
  9287. (move-to-column col))
  9288. (if (and (org-at-table-p)
  9289. (not (org-at-table-hline-p)))
  9290. t
  9291. (error
  9292. "Please position cursor in a data line for column operations")))))
  9293. (defun org-table-delete-column ()
  9294. "Delete a column from the table."
  9295. (interactive)
  9296. (if (not (org-at-table-p))
  9297. (error "Not at a table"))
  9298. (org-table-find-dataline)
  9299. (org-table-check-inside-data-field)
  9300. (let* ((col (org-table-current-column))
  9301. (beg (org-table-begin))
  9302. (end (org-table-end))
  9303. ;; Current cursor position
  9304. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9305. (colpos col))
  9306. (goto-char beg)
  9307. (while (< (point) end)
  9308. (if (org-at-table-hline-p)
  9309. nil
  9310. (org-table-goto-column col t)
  9311. (and (looking-at "|[^|\n]+|")
  9312. (replace-match "|")))
  9313. (beginning-of-line 2))
  9314. (move-marker end nil)
  9315. (goto-line linepos)
  9316. (org-table-goto-column colpos)
  9317. (org-table-align)
  9318. (org-table-modify-formulas 'remove col)))
  9319. (defun org-table-move-column-right ()
  9320. "Move column to the right."
  9321. (interactive)
  9322. (org-table-move-column nil))
  9323. (defun org-table-move-column-left ()
  9324. "Move column to the left."
  9325. (interactive)
  9326. (org-table-move-column 'left))
  9327. (defun org-table-move-column (&optional left)
  9328. "Move the current column to the right. With arg LEFT, move to the left."
  9329. (interactive "P")
  9330. (if (not (org-at-table-p))
  9331. (error "Not at a table"))
  9332. (org-table-find-dataline)
  9333. (org-table-check-inside-data-field)
  9334. (let* ((col (org-table-current-column))
  9335. (col1 (if left (1- col) col))
  9336. (beg (org-table-begin))
  9337. (end (org-table-end))
  9338. ;; Current cursor position
  9339. (linepos (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  9340. (colpos (if left (1- col) (1+ col))))
  9341. (if (and left (= col 1))
  9342. (error "Cannot move column further left"))
  9343. (if (and (not left) (looking-at "[^|\n]*|[^|\n]*$"))
  9344. (error "Cannot move column further right"))
  9345. (goto-char beg)
  9346. (while (< (point) end)
  9347. (if (org-at-table-hline-p)
  9348. nil
  9349. (org-table-goto-column col1 t)
  9350. (and (looking-at "|\\([^|\n]+\\)|\\([^|\n]+\\)|")
  9351. (replace-match "|\\2|\\1|")))
  9352. (beginning-of-line 2))
  9353. (move-marker end nil)
  9354. (goto-line linepos)
  9355. (org-table-goto-column colpos)
  9356. (org-table-align)
  9357. (org-table-modify-formulas 'swap col (if left (1- col) (1+ col)))))
  9358. (defun org-table-move-row-down ()
  9359. "Move table row down."
  9360. (interactive)
  9361. (org-table-move-row nil))
  9362. (defun org-table-move-row-up ()
  9363. "Move table row up."
  9364. (interactive)
  9365. (org-table-move-row 'up))
  9366. (defun org-table-move-row (&optional up)
  9367. "Move the current table line down. With arg UP, move it up."
  9368. (interactive "P")
  9369. (let ((col (current-column))
  9370. (pos (point))
  9371. (tonew (if up 0 2))
  9372. txt)
  9373. (beginning-of-line tonew)
  9374. (if (not (org-at-table-p))
  9375. (progn
  9376. (goto-char pos)
  9377. (error "Cannot move row further")))
  9378. (goto-char pos)
  9379. (beginning-of-line 1)
  9380. (setq pos (point))
  9381. (setq txt (buffer-substring (point) (1+ (point-at-eol))))
  9382. (delete-region (point) (1+ (point-at-eol)))
  9383. (beginning-of-line tonew)
  9384. (insert txt)
  9385. (beginning-of-line 0)
  9386. (move-to-column col)))
  9387. (defun org-table-insert-row (&optional arg)
  9388. "Insert a new row above the current line into the table.
  9389. With prefix ARG, insert below the current line."
  9390. (interactive "P")
  9391. (if (not (org-at-table-p))
  9392. (error "Not at a table"))
  9393. (let* ((line (buffer-substring (point-at-bol) (point-at-eol)))
  9394. (new (org-table-clean-line line)))
  9395. ;; Fix the first field if necessary
  9396. (if (string-match "^[ \t]*| *[#$] *|" line)
  9397. (setq new (replace-match (match-string 0 line) t t new)))
  9398. (beginning-of-line (if arg 2 1))
  9399. (let (org-table-may-need-update) (insert-before-markers new "\n"))
  9400. (beginning-of-line 0)
  9401. (re-search-forward "| ?" (point-at-eol) t)
  9402. (and org-table-may-need-update (org-table-align))))
  9403. (defun org-table-insert-hline (&optional arg)
  9404. "Insert a horizontal-line below the current line into the table.
  9405. With prefix ARG, insert above the current line."
  9406. (interactive "P")
  9407. (if (not (org-at-table-p))
  9408. (error "Not at a table"))
  9409. (let ((line (org-table-clean-line
  9410. (buffer-substring (point-at-bol) (point-at-eol))))
  9411. (col (current-column)))
  9412. (while (string-match "|\\( +\\)|" line)
  9413. (setq line (replace-match
  9414. (concat "+" (make-string (- (match-end 1) (match-beginning 1))
  9415. ?-) "|") t t line)))
  9416. (and (string-match "\\+" line) (setq line (replace-match "|" t t line)))
  9417. (beginning-of-line (if arg 1 2))
  9418. (insert line "\n")
  9419. (beginning-of-line (if arg 1 -1))
  9420. (move-to-column col)))
  9421. (defun org-table-clean-line (s)
  9422. "Convert a table line S into a string with only \"|\" and space.
  9423. In particular, this does handle wide and invisible characters."
  9424. (if (string-match "^[ \t]*|-" s)
  9425. ;; It's a hline, just map the characters
  9426. (setq s (mapconcat (lambda (x) (if (member x '(?| ?+)) "|" " ")) s ""))
  9427. (while (string-match "|\\([ \t]*?[^ \t\r\n|][^\r\n|]*\\)|" s)
  9428. (setq s (replace-match
  9429. (concat "|" (make-string (org-string-width (match-string 1 s))
  9430. ?\ ) "|")
  9431. t t s)))
  9432. s))
  9433. (defun org-table-kill-row ()
  9434. "Delete the current row or horizontal line from the table."
  9435. (interactive)
  9436. (if (not (org-at-table-p))
  9437. (error "Not at a table"))
  9438. (let ((col (current-column)))
  9439. (kill-region (point-at-bol) (min (1+ (point-at-eol)) (point-max)))
  9440. (if (not (org-at-table-p)) (beginning-of-line 0))
  9441. (move-to-column col)))
  9442. (defun org-table-sort-lines (beg end numericp)
  9443. "Sort table lines in region.
  9444. Point and mark define the first and last line to include. Both point and
  9445. mark should be in the column that is used for sorting. For example, to
  9446. sort according to column 3, put the mark in the first line to sort, in
  9447. table column 3. Put point into the last line to be included in the sorting,
  9448. also in table column 3. The command will prompt for the sorting method
  9449. \(n for numerical, a for alphanumeric)."
  9450. (interactive "r\nsSorting method: [n]=numeric [a]=alpha: ")
  9451. (setq numericp (string-match "[nN]" numericp))
  9452. (org-table-align) ;; Just to be safe
  9453. (let* (bcol ecol cmp column lns)
  9454. (goto-char beg)
  9455. (org-table-check-inside-data-field)
  9456. (setq column (org-table-current-column)
  9457. beg (move-marker (make-marker) (point-at-bol)))
  9458. (goto-char end)
  9459. (org-table-check-inside-data-field)
  9460. (setq end (move-marker (make-marker) (1+ (point-at-eol))))
  9461. (untabify beg end)
  9462. (goto-char beg)
  9463. (org-table-goto-column column)
  9464. (skip-chars-backward "^|")
  9465. (setq bcol (current-column))
  9466. (org-table-goto-column (1+ column))
  9467. (skip-chars-backward "^|")
  9468. (setq ecol (1- (current-column)))
  9469. (setq cmp (if numericp
  9470. (lambda (a b) (< (car a) (car b)))
  9471. (lambda (a b) (string< (car a) (car b)))))
  9472. (setq lns (mapcar (lambda(x) (cons (org-trim (substring x bcol ecol)) x))
  9473. (org-split-string (buffer-substring beg end) "\n")))
  9474. (if numericp
  9475. (setq lns (mapcar (lambda(x)
  9476. (cons (string-to-number (car x)) (cdr x)))
  9477. lns)))
  9478. (delete-region beg end)
  9479. (move-marker beg nil)
  9480. (move-marker end nil)
  9481. (insert (mapconcat 'cdr (setq lns (sort lns cmp)) "\n") "\n")
  9482. (message "%d lines sorted %s based on column %d"
  9483. (length lns)
  9484. (if numericp "numerically" "alphabetically") column)))
  9485. (defun org-table-cut-region (beg end)
  9486. "Copy region in table to the clipboard and blank all relevant fields."
  9487. (interactive "r")
  9488. (org-table-copy-region beg end 'cut))
  9489. (defun org-table-copy-region (beg end &optional cut)
  9490. "Copy rectangular region in table to clipboard.
  9491. A special clipboard is used which can only be accessed
  9492. with `org-table-paste-rectangle'."
  9493. (interactive "rP")
  9494. (let* (l01 c01 l02 c02 l1 c1 l2 c2 ic1 ic2
  9495. region cols
  9496. (rpl (if cut " " nil)))
  9497. (goto-char beg)
  9498. (org-table-check-inside-data-field)
  9499. (setq l01 (count-lines (point-min) (point))
  9500. c01 (org-table-current-column))
  9501. (goto-char end)
  9502. (org-table-check-inside-data-field)
  9503. (setq l02 (count-lines (point-min) (point))
  9504. c02 (org-table-current-column))
  9505. (setq l1 (min l01 l02) l2 (max l01 l02)
  9506. c1 (min c01 c02) c2 (max c01 c02))
  9507. (catch 'exit
  9508. (while t
  9509. (catch 'nextline
  9510. (if (> l1 l2) (throw 'exit t))
  9511. (goto-line l1)
  9512. (if (org-at-table-hline-p) (throw 'nextline (setq l1 (1+ l1))))
  9513. (setq cols nil ic1 c1 ic2 c2)
  9514. (while (< ic1 (1+ ic2))
  9515. (push (org-table-get-field ic1 rpl) cols)
  9516. (setq ic1 (1+ ic1)))
  9517. (push (nreverse cols) region)
  9518. (setq l1 (1+ l1)))))
  9519. (setq org-table-clip (nreverse region))
  9520. (if cut (org-table-align))
  9521. org-table-clip))
  9522. (defun org-table-paste-rectangle ()
  9523. "Paste a rectangular region into a table.
  9524. The upper right corner ends up in the current field. All involved fields
  9525. will be overwritten. If the rectangle does not fit into the present table,
  9526. the table is enlarged as needed. The process ignores horizontal separator
  9527. lines."
  9528. (interactive)
  9529. (unless (and org-table-clip (listp org-table-clip))
  9530. (error "First cut/copy a region to paste!"))
  9531. (org-table-check-inside-data-field)
  9532. (let* ((clip org-table-clip)
  9533. (line (count-lines (point-min) (point)))
  9534. (col (org-table-current-column))
  9535. (org-enable-table-editor t)
  9536. (org-table-automatic-realign nil)
  9537. c cols field)
  9538. (while (setq cols (pop clip))
  9539. (while (org-at-table-hline-p) (beginning-of-line 2))
  9540. (if (not (org-at-table-p))
  9541. (progn (end-of-line 0) (org-table-next-field)))
  9542. (setq c col)
  9543. (while (setq field (pop cols))
  9544. (org-table-goto-column c nil 'force)
  9545. (org-table-get-field nil field)
  9546. (setq c (1+ c)))
  9547. (beginning-of-line 2))
  9548. (goto-line line)
  9549. (org-table-goto-column col)
  9550. (org-table-align)))
  9551. (defun org-table-convert ()
  9552. "Convert from `org-mode' table to table.el and back.
  9553. Obviously, this only works within limits. When an Org-mode table is
  9554. converted to table.el, all horizontal separator lines get lost, because
  9555. table.el uses these as cell boundaries and has no notion of horizontal lines.
  9556. A table.el table can be converted to an Org-mode table only if it does not
  9557. do row or column spanning. Multiline cells will become multiple cells.
  9558. Beware, Org-mode does not test if the table can be successfully converted - it
  9559. blindly applies a recipe that works for simple tables."
  9560. (interactive)
  9561. (require 'table)
  9562. (if (org-at-table.el-p)
  9563. ;; convert to Org-mode table
  9564. (let ((beg (move-marker (make-marker) (org-table-begin t)))
  9565. (end (move-marker (make-marker) (org-table-end t))))
  9566. (table-unrecognize-region beg end)
  9567. (goto-char beg)
  9568. (while (re-search-forward "^\\([ \t]*\\)\\+-.*\n" end t)
  9569. (replace-match ""))
  9570. (goto-char beg))
  9571. (if (org-at-table-p)
  9572. ;; convert to table.el table
  9573. (let ((beg (move-marker (make-marker) (org-table-begin)))
  9574. (end (move-marker (make-marker) (org-table-end))))
  9575. ;; first, get rid of all horizontal lines
  9576. (goto-char beg)
  9577. (while (re-search-forward "^\\([ \t]*\\)|-.*\n" end t)
  9578. (replace-match ""))
  9579. ;; insert a hline before first
  9580. (goto-char beg)
  9581. (org-table-insert-hline 'above)
  9582. (beginning-of-line -1)
  9583. ;; insert a hline after each line
  9584. (while (progn (beginning-of-line 3) (< (point) end))
  9585. (org-table-insert-hline))
  9586. (goto-char beg)
  9587. (setq end (move-marker end (org-table-end)))
  9588. ;; replace "+" at beginning and ending of hlines
  9589. (while (re-search-forward "^\\([ \t]*\\)|-" end t)
  9590. (replace-match "\\1+-"))
  9591. (goto-char beg)
  9592. (while (re-search-forward "-|[ \t]*$" end t)
  9593. (replace-match "-+"))
  9594. (goto-char beg)))))
  9595. (defun org-table-wrap-region (arg)
  9596. "Wrap several fields in a column like a paragraph.
  9597. This is useful if you'd like to spread the contents of a field over several
  9598. lines, in order to keep the table compact.
  9599. If there is an active region, and both point and mark are in the same column,
  9600. the text in the column is wrapped to minimum width for the given number of
  9601. lines. Generally, this makes the table more compact. A prefix ARG may be
  9602. used to change the number of desired lines. For example, `C-2 \\[org-table-wrap]'
  9603. formats the selected text to two lines. If the region was longer than two
  9604. lines, the remaining lines remain empty. A negative prefix argument reduces
  9605. the current number of lines by that amount. The wrapped text is pasted back
  9606. into the table. If you formatted it to more lines than it was before, fields
  9607. further down in the table get overwritten - so you might need to make space in
  9608. the table first.
  9609. If there is no region, the current field is split at the cursor position and
  9610. the text fragment to the right of the cursor is prepended to the field one
  9611. line down.
  9612. If there is no region, but you specify a prefix ARG, the current field gets
  9613. blank, and the content is appended to the field above."
  9614. (interactive "P")
  9615. (org-table-check-inside-data-field)
  9616. (if (org-region-active-p)
  9617. ;; There is a region: fill as a paragraph
  9618. (let ((beg (region-beginning))
  9619. nlines)
  9620. (org-table-cut-region (region-beginning) (region-end))
  9621. (if (> (length (car org-table-clip)) 1)
  9622. (error "Region must be limited to single column"))
  9623. (setq nlines (if arg
  9624. (if (< arg 1)
  9625. (+ (length org-table-clip) arg)
  9626. arg)
  9627. (length org-table-clip)))
  9628. (setq org-table-clip
  9629. (mapcar 'list (org-wrap (mapconcat 'car org-table-clip " ")
  9630. nil nlines)))
  9631. (goto-char beg)
  9632. (org-table-paste-rectangle))
  9633. ;; No region, split the current field at point
  9634. (if arg
  9635. ;; combine with field above
  9636. (let ((s (org-table-blank-field))
  9637. (col (org-table-current-column)))
  9638. (beginning-of-line 0)
  9639. (while (org-at-table-hline-p) (beginning-of-line 0))
  9640. (org-table-goto-column col)
  9641. (skip-chars-forward "^|")
  9642. (skip-chars-backward " ")
  9643. (insert " " (org-trim s))
  9644. (org-table-align))
  9645. ;; split field
  9646. (when (looking-at "\\([^|]+\\)+|")
  9647. (let ((s (match-string 1)))
  9648. (replace-match " |")
  9649. (goto-char (match-beginning 0))
  9650. (org-table-next-row)
  9651. (insert (org-trim s) " ")
  9652. (org-table-align))))))
  9653. (defvar org-field-marker nil)
  9654. (defun org-table-edit-field (arg)
  9655. "Edit table field in a different window.
  9656. This is mainly useful for fields that contain hidden parts.
  9657. When called with a \\[universal-argument] prefix, just make the full field visible so that
  9658. it can be edited in place."
  9659. (interactive "P")
  9660. (if arg
  9661. (let ((b (save-excursion (skip-chars-backward "^|") (point)))
  9662. (e (save-excursion (skip-chars-forward "^|\r\n") (point))))
  9663. (remove-text-properties b e '(org-cwidth t invisible t
  9664. display t intangible t))
  9665. (if (and (boundp 'font-lock-mode) font-lock-mode)
  9666. (font-lock-fontify-block)))
  9667. (let ((pos (move-marker (make-marker) (point)))
  9668. (field (org-table-get-field))
  9669. (cw (current-window-configuration))
  9670. p)
  9671. (switch-to-buffer-other-window "*Org tmp*")
  9672. (erase-buffer)
  9673. (insert "#\n# Edit field and finish with C-c C-c\n#\n")
  9674. (org-mode)
  9675. (goto-char (setq p (point-max)))
  9676. (insert (org-trim field))
  9677. (remove-text-properties p (point-max)
  9678. '(invisible t org-cwidth t display t
  9679. intangible t))
  9680. (goto-char p)
  9681. (set (make-local-variable 'org-finish-function)
  9682. 'org-table-finish-edit-field)
  9683. (set (make-local-variable 'org-window-configuration) cw)
  9684. (set (make-local-variable 'org-field-marker) pos)
  9685. (message "Edit and finish with C-c C-c"))))
  9686. (defun org-table-finish-edit-field ()
  9687. "Finish editing a table data field.
  9688. Remove all newline characters, insert the result into the table, realign
  9689. the table and kill the editing buffer."
  9690. (let ((pos org-field-marker)
  9691. (cw org-window-configuration)
  9692. (cb (current-buffer))
  9693. text)
  9694. (goto-char (point-min))
  9695. (while (re-search-forward "^#.*\n?" nil t) (replace-match ""))
  9696. (while (re-search-forward "\\([ \t]*\n[ \t]*\\)+" nil t)
  9697. (replace-match " "))
  9698. (setq text (org-trim (buffer-string)))
  9699. (set-window-configuration cw)
  9700. (kill-buffer cb)
  9701. (select-window (get-buffer-window (marker-buffer pos)))
  9702. (goto-char pos)
  9703. (move-marker pos nil)
  9704. (org-table-check-inside-data-field)
  9705. (org-table-get-field nil text)
  9706. (org-table-align)
  9707. (message "New field value inserted")))
  9708. (defun org-trim (s)
  9709. "Remove whitespace at beginning and end of string."
  9710. (if (string-match "^[ \t]+" s) (setq s (replace-match "" t t s)))
  9711. (if (string-match "[ \t]+$" s) (setq s (replace-match "" t t s)))
  9712. s)
  9713. (defun org-wrap (string &optional width lines)
  9714. "Wrap string to either a number of lines, or a width in characters.
  9715. If WIDTH is non-nil, the string is wrapped to that width, however many lines
  9716. that costs. If there is a word longer than WIDTH, the text is actually
  9717. wrapped to the length of that word.
  9718. IF WIDTH is nil and LINES is non-nil, the string is forced into at most that
  9719. many lines, whatever width that takes.
  9720. The return value is a list of lines, without newlines at the end."
  9721. (let* ((words (org-split-string string "[ \t\n]+"))
  9722. (maxword (apply 'max (mapcar 'org-string-width words)))
  9723. w ll)
  9724. (cond (width
  9725. (org-do-wrap words (max maxword width)))
  9726. (lines
  9727. (setq w maxword)
  9728. (setq ll (org-do-wrap words maxword))
  9729. (if (<= (length ll) lines)
  9730. ll
  9731. (setq ll words)
  9732. (while (> (length ll) lines)
  9733. (setq w (1+ w))
  9734. (setq ll (org-do-wrap words w)))
  9735. ll))
  9736. (t (error "Cannot wrap this")))))
  9737. (defun org-do-wrap (words width)
  9738. "Create lines of maximum width WIDTH (in characters) from word list WORDS."
  9739. (let (lines line)
  9740. (while words
  9741. (setq line (pop words))
  9742. (while (and words (< (+ (length line) (length (car words))) width))
  9743. (setq line (concat line " " (pop words))))
  9744. (setq lines (push line lines)))
  9745. (nreverse lines)))
  9746. (defun org-split-string (string &optional separators)
  9747. "Splits STRING into substrings at SEPARATORS.
  9748. No empty strings are returned if there are matches at the beginning
  9749. and end of string."
  9750. (let ((rexp (or separators "[ \f\t\n\r\v]+"))
  9751. (start 0)
  9752. notfirst
  9753. (list nil))
  9754. (while (and (string-match rexp string
  9755. (if (and notfirst
  9756. (= start (match-beginning 0))
  9757. (< start (length string)))
  9758. (1+ start) start))
  9759. (< (match-beginning 0) (length string)))
  9760. (setq notfirst t)
  9761. (or (eq (match-beginning 0) 0)
  9762. (and (eq (match-beginning 0) (match-end 0))
  9763. (eq (match-beginning 0) start))
  9764. (setq list
  9765. (cons (substring string start (match-beginning 0))
  9766. list)))
  9767. (setq start (match-end 0)))
  9768. (or (eq start (length string))
  9769. (setq list
  9770. (cons (substring string start)
  9771. list)))
  9772. (nreverse list)))
  9773. (defun org-table-map-tables (function)
  9774. "Apply FUNCTION to the start of all tables in the buffer."
  9775. (save-excursion
  9776. (save-restriction
  9777. (widen)
  9778. (goto-char (point-min))
  9779. (while (re-search-forward org-table-any-line-regexp nil t)
  9780. (message "Mapping tables: %d%%" (/ (* 100.0 (point)) (buffer-size)))
  9781. (beginning-of-line 1)
  9782. (if (looking-at org-table-line-regexp)
  9783. (save-excursion (funcall function)))
  9784. (re-search-forward org-table-any-border-regexp nil 1))))
  9785. (message "Mapping tables: done"))
  9786. (defun org-table-sum (&optional beg end nlast)
  9787. "Sum numbers in region of current table column.
  9788. The result will be displayed in the echo area, and will be available
  9789. as kill to be inserted with \\[yank].
  9790. If there is an active region, it is interpreted as a rectangle and all
  9791. numbers in that rectangle will be summed. If there is no active
  9792. region and point is located in a table column, sum all numbers in that
  9793. column.
  9794. If at least one number looks like a time HH:MM or HH:MM:SS, all other
  9795. numbers are assumed to be times as well (in decimal hours) and the
  9796. numbers are added as such.
  9797. If NLAST is a number, only the NLAST fields will actually be summed."
  9798. (interactive)
  9799. (save-excursion
  9800. (let (col (timecnt 0) diff h m s org-table-clip)
  9801. (cond
  9802. ((and beg end)) ; beg and end given explicitly
  9803. ((org-region-active-p)
  9804. (setq beg (region-beginning) end (region-end)))
  9805. (t
  9806. (setq col (org-table-current-column))
  9807. (goto-char (org-table-begin))
  9808. (unless (re-search-forward "^[ \t]*|[^-]" nil t)
  9809. (error "No table data"))
  9810. (org-table-goto-column col)
  9811. ;not needed? (skip-chars-backward "^|")
  9812. (setq beg (point))
  9813. (goto-char (org-table-end))
  9814. (unless (re-search-backward "^[ \t]*|[^-]" nil t)
  9815. (error "No table data"))
  9816. (org-table-goto-column col)
  9817. ;not needed? (skip-chars-forward "^|")
  9818. (setq end (point))))
  9819. (let* ((items (apply 'append (org-table-copy-region beg end)))
  9820. (items1 (cond ((not nlast) items)
  9821. ((>= nlast (length items)) items)
  9822. (t (setq items (reverse items))
  9823. (setcdr (nthcdr (1- nlast) items) nil)
  9824. (nreverse items))))
  9825. (numbers (delq nil (mapcar 'org-table-get-number-for-summing
  9826. items1)))
  9827. (res (apply '+ numbers))
  9828. (sres (if (= timecnt 0)
  9829. (format "%g" res)
  9830. (setq diff (* 3600 res)
  9831. h (floor (/ diff 3600)) diff (mod diff 3600)
  9832. m (floor (/ diff 60)) diff (mod diff 60)
  9833. s diff)
  9834. (format "%d:%02d:%02d" h m s))))
  9835. (kill-new sres)
  9836. (if (interactive-p)
  9837. (message "%s"
  9838. (substitute-command-keys
  9839. (format "Sum of %d items: %-20s (\\[yank] will insert result into buffer)"
  9840. (length numbers) sres))))
  9841. sres))))
  9842. (defun org-table-get-number-for-summing (s)
  9843. (let (n)
  9844. (if (string-match "^ *|? *" s)
  9845. (setq s (replace-match "" nil nil s)))
  9846. (if (string-match " *|? *$" s)
  9847. (setq s (replace-match "" nil nil s)))
  9848. (setq n (string-to-number s))
  9849. (cond
  9850. ((and (string-match "0" s)
  9851. (string-match "\\`[-+ \t0.edED]+\\'" s)) 0)
  9852. ((string-match "\\`[ \t]+\\'" s) nil)
  9853. ((string-match "\\`\\([0-9]+\\):\\([0-9]+\\)\\(:\\([0-9]+\\)\\)?\\'" s)
  9854. (let ((h (string-to-number (or (match-string 1 s) "0")))
  9855. (m (string-to-number (or (match-string 2 s) "0")))
  9856. (s (string-to-number (or (match-string 4 s) "0"))))
  9857. (if (boundp 'timecnt) (setq timecnt (1+ timecnt)))
  9858. (* 1.0 (+ h (/ m 60.0) (/ s 3600.0)))))
  9859. ((equal n 0) nil)
  9860. (t n))))
  9861. (defun org-table-get-vertical-vector (desc &optional tbeg col)
  9862. "Get a calc vector from a column, accorting to descriptor DESC.
  9863. Optional arguments TBEG and COL can give the beginning of the table and
  9864. the current column, to avoid unnecessary parsing."
  9865. (save-excursion
  9866. (or tbeg (setq tbeg (org-table-begin)))
  9867. (or col (setq col (org-table-current-column)))
  9868. (let (beg end nn n n1 n2 l (thisline (org-current-line)) hline-list)
  9869. (cond
  9870. ((string-match "\\(I+\\)\\(-\\(I+\\)\\)?" desc)
  9871. (setq n1 (- (match-end 1) (match-beginning 1)))
  9872. (if (match-beginning 3)
  9873. (setq n2 (- (match-end 2) (match-beginning 3))))
  9874. (setq n (if n2 (max n1 n2) n1))
  9875. (setq n1 (if n2 (min n1 n2)))
  9876. (setq nn n)
  9877. (while (and (> nn 0)
  9878. (re-search-backward org-table-hline-regexp tbeg t))
  9879. (push (org-current-line) hline-list)
  9880. (setq nn (1- nn)))
  9881. (setq hline-list (nreverse hline-list))
  9882. (goto-line (nth (1- n) hline-list))
  9883. (when (re-search-forward org-table-dataline-regexp)
  9884. (org-table-goto-column col)
  9885. (setq beg (point)))
  9886. (goto-line (if n1 (nth (1- n1) hline-list) thisline))
  9887. (when (re-search-backward org-table-dataline-regexp)
  9888. (org-table-goto-column col)
  9889. (setq end (point)))
  9890. (setq l (apply 'append (org-table-copy-region beg end)))
  9891. (concat "[" (mapconcat (lambda (x) (setq x (org-trim x))
  9892. (if (equal x "") "0" x))
  9893. l ",") "]"))
  9894. ((string-match "\\([0-9]+\\)-\\([0-9]+\\)" desc)
  9895. (setq n1 (string-to-number (match-string 1 desc))
  9896. n2 (string-to-number (match-string 2 desc)))
  9897. (beginning-of-line 1)
  9898. (save-excursion
  9899. (when (re-search-backward org-table-dataline-regexp tbeg t n1)
  9900. (org-table-goto-column col)
  9901. (setq beg (point))))
  9902. (when (re-search-backward org-table-dataline-regexp tbeg t n2)
  9903. (org-table-goto-column col)
  9904. (setq end (point)))
  9905. (setq l (apply 'append (org-table-copy-region beg end)))
  9906. (concat "[" (mapconcat
  9907. (lambda (x) (setq x (org-trim x))
  9908. (if (equal x "") "0" x))
  9909. l ",") "]"))
  9910. ((string-match "\\([0-9]+\\)" desc)
  9911. (beginning-of-line 1)
  9912. (when (re-search-backward org-table-dataline-regexp tbeg t
  9913. (string-to-number (match-string 0 desc)))
  9914. (org-table-goto-column col)
  9915. (org-trim (org-table-get-field))))))))
  9916. (defvar org-table-formula-history nil)
  9917. (defvar org-table-column-names nil
  9918. "Alist with column names, derived from the `!' line.")
  9919. (defvar org-table-column-name-regexp nil
  9920. "Regular expression matching the current column names.")
  9921. (defvar org-table-local-parameters nil
  9922. "Alist with parameter names, derived from the `$' line.")
  9923. (defvar org-table-named-field-locations nil
  9924. "Alist with locations of named fields.")
  9925. (defun org-table-get-formula (&optional equation named)
  9926. "Read a formula from the minibuffer, offer stored formula as default."
  9927. (let* ((name (car (rassoc (list (org-current-line)
  9928. (org-table-current-column))
  9929. org-table-named-field-locations)))
  9930. (scol (if named
  9931. (if name name
  9932. (error "Not in a named field"))
  9933. (int-to-string (org-table-current-column))))
  9934. (dummy (and name (not named)
  9935. (not (y-or-n-p "Replace named-field formula with column equation? " ))
  9936. (error "Abort")))
  9937. (org-table-may-need-update nil)
  9938. (stored-list (org-table-get-stored-formulas))
  9939. (stored (cdr (assoc scol stored-list)))
  9940. (eq (cond
  9941. ((and stored equation (string-match "^ *=? *$" equation))
  9942. stored)
  9943. ((stringp equation)
  9944. equation)
  9945. (t (read-string
  9946. (format "%s formula $%s=" (if named "Field" "Column") scol)
  9947. (or stored "") 'org-table-formula-history
  9948. ;stored
  9949. ))))
  9950. mustsave)
  9951. (when (not (string-match "\\S-" eq))
  9952. ;; remove formula
  9953. (setq stored-list (delq (assoc scol stored-list) stored-list))
  9954. (org-table-store-formulas stored-list)
  9955. (error "Formula removed"))
  9956. (if (string-match "^ *=?" eq) (setq eq (replace-match "" t t eq)))
  9957. (if (string-match " *$" eq) (setq eq (replace-match "" t t eq)))
  9958. (if (and name (not named))
  9959. ;; We set the column equation, delete the named one.
  9960. (setq stored-list (delq (assoc name stored-list) stored-list)
  9961. mustsave t))
  9962. (if stored
  9963. (setcdr (assoc scol stored-list) eq)
  9964. (setq stored-list (cons (cons scol eq) stored-list)))
  9965. (if (or mustsave (not (equal stored eq)))
  9966. (org-table-store-formulas stored-list))
  9967. eq))
  9968. (defun org-table-store-formulas (alist)
  9969. "Store the list of formulas below the current table."
  9970. (setq alist (sort alist (lambda (a b) (string< (car a) (car b)))))
  9971. (save-excursion
  9972. (goto-char (org-table-end))
  9973. (if (looking-at "\\([ \t]*\n\\)*#\\+TBLFM:.*\n?")
  9974. (delete-region (point) (match-end 0)))
  9975. (insert "#+TBLFM: "
  9976. (mapconcat (lambda (x)
  9977. (concat "$" (car x) "=" (cdr x)))
  9978. alist "::")
  9979. "\n")))
  9980. (defun org-table-get-stored-formulas ()
  9981. "Return an alist with the stored formulas directly after current table."
  9982. (interactive)
  9983. (let (scol eq eq-alist strings string seen)
  9984. (save-excursion
  9985. (goto-char (org-table-end))
  9986. (when (looking-at "\\([ \t]*\n\\)*#\\+TBLFM: *\\(.*\\)")
  9987. (setq strings (org-split-string (match-string 2) " *:: *"))
  9988. (while (setq string (pop strings))
  9989. (when (string-match "\\$\\([a-zA-Z0-9]+\\) *= *\\(.*[^ \t]\\)" string)
  9990. (setq scol (match-string 1 string)
  9991. eq (match-string 2 string)
  9992. eq-alist (cons (cons scol eq) eq-alist))
  9993. (if (member scol seen)
  9994. (error "Double definition `$%s=' in TBLFM line, please fix by hand" scol)
  9995. (push scol seen))))))
  9996. (nreverse eq-alist)))
  9997. (defun org-table-modify-formulas (action &rest columns)
  9998. "Modify the formulas stored below the current table.
  9999. ACTION can be `remove', `insert', `swap'. For `swap', two column numbers are
  10000. expected, for the other actions only a single column number is needed."
  10001. (let ((list (org-table-get-stored-formulas))
  10002. (nmax (length (org-split-string
  10003. (buffer-substring (point-at-bol) (point-at-eol))
  10004. "|")))
  10005. col col1 col2 scol si sc1 sc2)
  10006. (cond
  10007. ((null list)) ; No action needed if there are no stored formulas
  10008. ((eq action 'remove)
  10009. (setq col (car columns)
  10010. scol (int-to-string col))
  10011. (org-table-replace-in-formulas list scol "INVALID")
  10012. (if (assoc scol list) (setq list (delq (assoc scol list) list)))
  10013. (loop for i from (1+ col) upto nmax by 1 do
  10014. (setq si (int-to-string i))
  10015. (org-table-replace-in-formulas list si (int-to-string (1- i)))
  10016. (if (assoc si list) (setcar (assoc si list)
  10017. (int-to-string (1- i))))))
  10018. ((eq action 'insert)
  10019. (setq col (car columns))
  10020. (loop for i from nmax downto col by 1 do
  10021. (setq si (int-to-string i))
  10022. (org-table-replace-in-formulas list si (int-to-string (1+ i)))
  10023. (if (assoc si list) (setcar (assoc si list)
  10024. (int-to-string (1+ i))))))
  10025. ((eq action 'swap)
  10026. (setq col1 (car columns) col2 (nth 1 columns)
  10027. sc1 (int-to-string col1) sc2 (int-to-string col2))
  10028. ;; Hopefully, ZqZtZ will never be a name in a table
  10029. (org-table-replace-in-formulas list sc1 "ZqZtZ")
  10030. (org-table-replace-in-formulas list sc2 sc1)
  10031. (org-table-replace-in-formulas list "ZqZtZ" sc2)
  10032. (if (assoc sc1 list) (setcar (assoc sc1 list) "ZqZtZ"))
  10033. (if (assoc sc2 list) (setcar (assoc sc2 list) sc1))
  10034. (if (assoc "ZqZtZ" list) (setcar (assoc "ZqZtZ" list) sc2)))
  10035. (t (error "Invalid action in `org-table-modify-formulas'")))
  10036. (if list (org-table-store-formulas list))))
  10037. (defun org-table-replace-in-formulas (list s1 s2)
  10038. (let (elt re s)
  10039. (setq s1 (concat "$" (if (integerp s1) (int-to-string s1) s1))
  10040. s2 (concat "$" (if (integerp s2) (int-to-string s2) s2))
  10041. re (concat (regexp-quote s1) "\\>"))
  10042. (while (setq elt (pop list))
  10043. (setq s (cdr elt))
  10044. (while (string-match re s)
  10045. (setq s (replace-match s2 t t s)))
  10046. (setcdr elt s))))
  10047. (defun org-table-get-specials ()
  10048. "Get the column names and local parameters for this table."
  10049. (save-excursion
  10050. (let ((beg (org-table-begin)) (end (org-table-end))
  10051. names name fields fields1 field cnt c v line col)
  10052. (setq org-table-column-names nil
  10053. org-table-local-parameters nil
  10054. org-table-named-field-locations nil)
  10055. (goto-char beg)
  10056. (when (re-search-forward "^[ \t]*| *! *\\(|.*\\)" end t)
  10057. (setq names (org-split-string (match-string 1) " *| *")
  10058. cnt 1)
  10059. (while (setq name (pop names))
  10060. (setq cnt (1+ cnt))
  10061. (if (string-match "^[a-zA-Z][a-zA-Z0-9]*$" name)
  10062. (push (cons name (int-to-string cnt)) org-table-column-names))))
  10063. (setq org-table-column-names (nreverse org-table-column-names))
  10064. (setq org-table-column-name-regexp
  10065. (concat "\\$\\(" (mapconcat 'car org-table-column-names "\\|") "\\)\\>"))
  10066. (goto-char beg)
  10067. (while (re-search-forward "^[ \t]*| *\\$ *\\(|.*\\)" end t)
  10068. (setq fields (org-split-string (match-string 1) " *| *"))
  10069. (while (setq field (pop fields))
  10070. (if (string-match "^\\([a-zA-Z][a-zA-Z0-9]*\\|%\\) *= *\\(.*\\)" field)
  10071. (push (cons (match-string 1 field) (match-string 2 field))
  10072. org-table-local-parameters))))
  10073. (goto-char beg)
  10074. (while (re-search-forward "^[ \t]*| *\\([_^]\\) *\\(|.*\\)" end t)
  10075. (setq c (match-string 1)
  10076. fields (org-split-string (match-string 2) " *| *"))
  10077. (save-excursion
  10078. (beginning-of-line (if (equal c "_") 2 0))
  10079. (setq line (org-current-line) col 1)
  10080. (and (looking-at "^[ \t]*|[^|]*\\(|.*\\)")
  10081. (setq fields1 (org-split-string (match-string 1) " *| *"))))
  10082. (while (and fields1 (setq field (pop fields)))
  10083. (setq v (pop fields1) col (1+ col))
  10084. (when (and (stringp field) (stringp v)
  10085. (string-match "^[a-zA-Z][a-zA-Z0-9]*$" field))
  10086. (push (cons field v) org-table-local-parameters)
  10087. (push (list field line col) org-table-named-field-locations)))))))
  10088. (defun org-this-word ()
  10089. ;; Get the current word
  10090. (save-excursion
  10091. (let ((beg (progn (skip-chars-backward "^ \t\n") (point)))
  10092. (end (progn (skip-chars-forward "^ \t\n") (point))))
  10093. (buffer-substring-no-properties beg end))))
  10094. (defun org-table-maybe-eval-formula ()
  10095. "Check if the current field starts with \"=\" or \":=\".
  10096. If yes, store the formula and apply it."
  10097. ;; We already know we are in a table. Get field will only return a formula
  10098. ;; when appropriate. It might return a separator line, but no problem.
  10099. (when org-table-formula-evaluate-inline
  10100. (let* ((field (org-trim (or (org-table-get-field) "")))
  10101. named eq)
  10102. (when (string-match "^:?=\\(.*\\)" field)
  10103. (setq named (equal (string-to-char field) ?:)
  10104. eq (match-string 1 field))
  10105. (if (fboundp 'calc-eval)
  10106. (org-table-eval-formula (if named '(4) nil) eq))))))
  10107. (defvar org-recalc-commands nil
  10108. "List of commands triggering the recalculation of a line.
  10109. Will be filled automatically during use.")
  10110. (defvar org-recalc-marks
  10111. '((" " . "Unmarked: no special line, no automatic recalculation")
  10112. ("#" . "Automatically recalculate this line upon TAB, RET, and C-c C-c in the line")
  10113. ("*" . "Recalculate only when entire table is recalculated with `C-u C-c *'")
  10114. ("!" . "Column name definition line. Reference in formula as $name.")
  10115. ("$" . "Parameter definition line name=value. Reference in formula as $name.")
  10116. ("_" . "Names for values in row below this one.")
  10117. ("^" . "Names for values in row above this one.")))
  10118. (defun org-table-rotate-recalc-marks (&optional newchar)
  10119. "Rotate the recalculation mark in the first column.
  10120. If in any row, the first field is not consistent with a mark,
  10121. insert a new column for the markers.
  10122. When there is an active region, change all the lines in the region,
  10123. after prompting for the marking character.
  10124. After each change, a message will be displayed indicating the meaning
  10125. of the new mark."
  10126. (interactive)
  10127. (unless (org-at-table-p) (error "Not at a table"))
  10128. (let* ((marks (append (mapcar 'car org-recalc-marks) '(" ")))
  10129. (beg (org-table-begin))
  10130. (end (org-table-end))
  10131. (l (org-current-line))
  10132. (l1 (if (org-region-active-p) (org-current-line (region-beginning))))
  10133. (l2 (if (org-region-active-p) (org-current-line (region-end))))
  10134. (have-col
  10135. (save-excursion
  10136. (goto-char beg)
  10137. (not (re-search-forward "^[ \t]*|[^-|][^|]*[^#!$*_^| \t][^|]*|" end t))))
  10138. (col (org-table-current-column))
  10139. (forcenew (car (assoc newchar org-recalc-marks)))
  10140. epos new)
  10141. (when l1
  10142. (message "Change region to what mark? Type # * ! $ or SPC: ")
  10143. (setq newchar (char-to-string (read-char-exclusive))
  10144. forcenew (car (assoc newchar org-recalc-marks))))
  10145. (if (and newchar (not forcenew))
  10146. (error "Invalid NEWCHAR `%s' in `org-table-rotate-recalc-marks'"
  10147. newchar))
  10148. (if l1 (goto-line l1))
  10149. (save-excursion
  10150. (beginning-of-line 1)
  10151. (unless (looking-at org-table-dataline-regexp)
  10152. (error "Not at a table data line")))
  10153. (unless have-col
  10154. (org-table-goto-column 1)
  10155. (org-table-insert-column)
  10156. (org-table-goto-column (1+ col)))
  10157. (setq epos (point-at-eol))
  10158. (save-excursion
  10159. (beginning-of-line 1)
  10160. (org-table-get-field
  10161. 1 (if (looking-at "^[ \t]*| *\\([#!$*^_ ]\\) *|")
  10162. (concat " "
  10163. (setq new (or forcenew
  10164. (cadr (member (match-string 1) marks))))
  10165. " ")
  10166. " # ")))
  10167. (if (and l1 l2)
  10168. (progn
  10169. (goto-line l1)
  10170. (while (progn (beginning-of-line 2) (not (= (org-current-line) l2)))
  10171. (and (looking-at org-table-dataline-regexp)
  10172. (org-table-get-field 1 (concat " " new " "))))
  10173. (goto-line l1)))
  10174. (if (not (= epos (point-at-eol))) (org-table-align))
  10175. (goto-line l)
  10176. (and (interactive-p) (message (cdr (assoc new org-recalc-marks))))))
  10177. (defun org-table-maybe-recalculate-line ()
  10178. "Recompute the current line if marked for it, and if we haven't just done it."
  10179. (interactive)
  10180. (and org-table-allow-automatic-line-recalculation
  10181. (not (and (memq last-command org-recalc-commands)
  10182. (equal org-last-recalc-line (org-current-line))))
  10183. (save-excursion (beginning-of-line 1)
  10184. (looking-at org-table-auto-recalculate-regexp))
  10185. (fboundp 'calc-eval)
  10186. (org-table-recalculate) t))
  10187. (defvar org-table-formula-debug nil
  10188. "Non-nil means, debug table formulas.
  10189. When nil, simply write \"#ERROR\" in corrupted fields.")
  10190. (defvar modes)
  10191. (defsubst org-set-calc-mode (var &optional value)
  10192. (if (stringp var)
  10193. (setq var (assoc var '(("D" calc-angle-mode deg)
  10194. ("R" calc-angle-mode rad)
  10195. ("F" calc-prefer-frac t)
  10196. ("S" calc-symbolic-mode t)))
  10197. value (nth 2 var) var (nth 1 var)))
  10198. (if (memq var modes)
  10199. (setcar (cdr (memq var modes)) value)
  10200. (cons var (cons value modes)))
  10201. modes)
  10202. (defun org-table-eval-formula (&optional arg equation
  10203. suppress-align suppress-const
  10204. suppress-store)
  10205. "Replace the table field value at the cursor by the result of a calculation.
  10206. This function makes use of Dave Gillespie's Calc package, in my view the
  10207. most exciting program ever written for GNU Emacs. So you need to have Calc
  10208. installed in order to use this function.
  10209. In a table, this command replaces the value in the current field with the
  10210. result of a formula. It also installs the formula as the \"current\" column
  10211. formula, by storing it in a special line below the table. When called
  10212. with a `C-u' prefix, the current field must ba a named field, and the
  10213. formula is installed as valid in only this specific field.
  10214. When called, the command first prompts for a formula, which is read in
  10215. the minibuffer. Previously entered formulas are available through the
  10216. history list, and the last used formula is offered as a default.
  10217. These stored formulas are adapted correctly when moving, inserting, or
  10218. deleting columns with the corresponding commands.
  10219. The formula can be any algebraic expression understood by the Calc package.
  10220. For details, see the Org-mode manual.
  10221. This function can also be called from Lisp programs and offers
  10222. additional arguments: EQUATION can be the formula to apply. If this
  10223. argument is given, the user will not be prompted. SUPPRESS-ALIGN is
  10224. used to speed-up recursive calls by by-passing unnecessary aligns.
  10225. SUPPRESS-CONST suppresses the interpretation of constants in the
  10226. formula, assuming that this has been done already outside the function.
  10227. SUPPRESS-STORE means the formula should not be stored, either because
  10228. it is already stored, or because it is a modified equation that should
  10229. not overwrite the stored one."
  10230. (interactive "P")
  10231. (require 'calc)
  10232. (org-table-check-inside-data-field)
  10233. (org-table-get-specials)
  10234. (let* (fields
  10235. (ndown (if (integerp arg) arg 1))
  10236. (org-table-automatic-realign nil)
  10237. (case-fold-search nil)
  10238. (down (> ndown 1))
  10239. (formula (if (and equation suppress-store)
  10240. equation
  10241. (org-table-get-formula equation (equal arg '(4)))))
  10242. (n0 (org-table-current-column))
  10243. (modes (copy-sequence org-calc-default-modes))
  10244. n form fmt x ev orig c lispp)
  10245. ;; Parse the format string. Since we have a lot of modes, this is
  10246. ;; a lot of work. However, I think calc still uses most of the time.
  10247. (if (string-match ";" formula)
  10248. (let ((tmp (org-split-string formula ";")))
  10249. (setq formula (car tmp)
  10250. fmt (concat (cdr (assoc "%" org-table-local-parameters))
  10251. (nth 1 tmp)))
  10252. (while (string-match "[pnfse]\\(-?[0-9]+\\)" fmt)
  10253. (setq c (string-to-char (match-string 1 fmt))
  10254. n (string-to-number (or (match-string 1 fmt) "")))
  10255. (if (= c ?p) (setq modes (org-set-calc-mode 'calc-internal-prec n))
  10256. (setq modes (org-set-calc-mode
  10257. 'calc-float-format
  10258. (list (cdr (assoc c '((?n . float) (?f . fix)
  10259. (?s . sci) (?e . eng))))
  10260. n))))
  10261. (setq fmt (replace-match "" t t fmt)))
  10262. (while (string-match "[DRFS]" fmt)
  10263. (setq modes (org-set-calc-mode (match-string 0 fmt)))
  10264. (setq fmt (replace-match "" t t fmt)))
  10265. (unless (string-match "\\S-" fmt)
  10266. (setq fmt nil))))
  10267. (if (and (not suppress-const) org-table-formula-use-constants)
  10268. (setq formula (org-table-formula-substitute-names formula)))
  10269. (setq orig (or (get-text-property 1 :orig-formula formula) "?"))
  10270. (while (> ndown 0)
  10271. (setq fields (org-split-string
  10272. (buffer-substring
  10273. (point-at-bol) (point-at-eol)) " *| *"))
  10274. (if org-table-formula-numbers-only
  10275. (setq fields (mapcar
  10276. (lambda (x) (number-to-string (string-to-number x)))
  10277. fields)))
  10278. (setq ndown (1- ndown))
  10279. (setq form (copy-sequence formula)
  10280. lispp (equal (substring form 0 2) "'("))
  10281. ;; Insert the references to fields in same row
  10282. (while (string-match "\\$\\([0-9]+\\)?" form)
  10283. (setq n (if (match-beginning 1)
  10284. (string-to-number (match-string 1 form))
  10285. n0)
  10286. x (nth (1- n) fields))
  10287. (unless x (error "Invalid field specifier \"%s\""
  10288. (match-string 0 form)))
  10289. (if (equal x "") (setq x "0"))
  10290. (setq form (replace-match
  10291. (if lispp x (concat "(" x ")"))
  10292. t t form)))
  10293. ;; Insert ranges in current column
  10294. (while (string-match "\\&[-I0-9]+" form)
  10295. (setq form (replace-match
  10296. (save-match-data
  10297. (org-table-get-vertical-vector (match-string 0 form)
  10298. nil n0))
  10299. t t form)))
  10300. (if lispp
  10301. (setq ev (eval (eval (read form)))
  10302. ev (if (numberp ev) (number-to-string ev) ev))
  10303. (setq ev (calc-eval (cons form modes)
  10304. (if org-table-formula-numbers-only 'num))))
  10305. (when org-table-formula-debug
  10306. (with-output-to-temp-buffer "*Help*"
  10307. (princ (format "Substitution history of formula
  10308. Orig: %s
  10309. $xyz-> %s
  10310. $1-> %s\n" orig formula form))
  10311. (if (listp ev)
  10312. (princ (format " %s^\nError: %s"
  10313. (make-string (car ev) ?\-) (nth 1 ev)))
  10314. (princ (format "Result: %s\nFormat: %s\nFinal: %s"
  10315. ev (or fmt "NONE")
  10316. (if fmt (format fmt (string-to-number ev)) ev)))))
  10317. (shrink-window-if-larger-than-buffer (get-buffer-window "*Help*"))
  10318. (unless (and (interactive-p) (not ndown))
  10319. (unless (let (inhibit-redisplay)
  10320. (y-or-n-p "Debugging Formula. Continue to next? "))
  10321. (org-table-align)
  10322. (error "Abort"))
  10323. (delete-window (get-buffer-window "*Help*"))
  10324. (message "")))
  10325. (if (listp ev) (setq fmt nil ev "#ERROR"))
  10326. (org-table-justify-field-maybe
  10327. (if fmt (format fmt (string-to-number ev)) ev))
  10328. (if (and down (> ndown 0) (looking-at ".*\n[ \t]*|[^-]"))
  10329. (call-interactively 'org-return)
  10330. (setq ndown 0)))
  10331. (and down (org-table-maybe-recalculate-line))
  10332. (or suppress-align (and org-table-may-need-update
  10333. (org-table-align)))))
  10334. (defun org-table-recalculate (&optional all noalign)
  10335. "Recalculate the current table line by applying all stored formulas.
  10336. With prefix arg ALL, do this for all lines in the table."
  10337. (interactive "P")
  10338. (or (memq this-command org-recalc-commands)
  10339. (setq org-recalc-commands (cons this-command org-recalc-commands)))
  10340. (unless (org-at-table-p) (error "Not at a table"))
  10341. (org-table-get-specials)
  10342. (let* ((eqlist (sort (org-table-get-stored-formulas)
  10343. (lambda (a b) (string< (car a) (car b)))))
  10344. (inhibit-redisplay t)
  10345. (line-re org-table-dataline-regexp)
  10346. (thisline (+ (if (bolp) 1 0) (count-lines (point-min) (point))))
  10347. (thiscol (org-table-current-column))
  10348. beg end entry eqlnum eqlname eql (cnt 0) eq a name)
  10349. ;; Insert constants in all formulas
  10350. (setq eqlist
  10351. (mapcar (lambda (x)
  10352. (setcdr x (org-table-formula-substitute-names (cdr x)))
  10353. x)
  10354. eqlist))
  10355. ;; Split the equation list
  10356. (while (setq eq (pop eqlist))
  10357. (if (<= (string-to-char (car eq)) ?9)
  10358. (push eq eqlnum)
  10359. (push eq eqlname)))
  10360. (setq eqlnum (nreverse eqlnum) eqlname (nreverse eqlname))
  10361. (if all
  10362. (progn
  10363. (setq end (move-marker (make-marker) (1+ (org-table-end))))
  10364. (goto-char (setq beg (org-table-begin)))
  10365. (if (re-search-forward org-table-calculate-mark-regexp end t)
  10366. ;; This is a table with marked lines, only compute selected lines
  10367. (setq line-re org-table-recalculate-regexp)
  10368. ;; Move forward to the first non-header line
  10369. (if (and (re-search-forward org-table-dataline-regexp end t)
  10370. (re-search-forward org-table-hline-regexp end t)
  10371. (re-search-forward org-table-dataline-regexp end t))
  10372. (setq beg (match-beginning 0))
  10373. nil))) ;; just leave beg where it is
  10374. (setq beg (point-at-bol)
  10375. end (move-marker (make-marker) (1+ (point-at-eol)))))
  10376. (goto-char beg)
  10377. (and all (message "Re-applying formulas to full table..."))
  10378. (while (re-search-forward line-re end t)
  10379. (unless (string-match "^ *[_^!$] *$" (org-table-get-field 1))
  10380. ;; Unprotected line, recalculate
  10381. (and all (message "Re-applying formulas to full table...(line %d)"
  10382. (setq cnt (1+ cnt))))
  10383. (setq org-last-recalc-line (org-current-line))
  10384. (setq eql eqlnum)
  10385. (while (setq entry (pop eql))
  10386. (goto-line org-last-recalc-line)
  10387. (org-table-goto-column (string-to-number (car entry)) nil 'force)
  10388. (org-table-eval-formula nil (cdr entry) 'noalign 'nocst 'nostore))))
  10389. (goto-line thisline)
  10390. (org-table-goto-column thiscol)
  10391. (or noalign (and org-table-may-need-update (org-table-align))
  10392. (and all (message "Re-applying formulas to %d lines...done" cnt)))
  10393. ;; Now do the names fields
  10394. (while (setq eq (pop eqlname))
  10395. (setq name (car eq)
  10396. a (assoc name org-table-named-field-locations))
  10397. (when a
  10398. (message "Re-applying formula to named field: %s" name)
  10399. (goto-line (nth 1 a))
  10400. (org-table-goto-column (nth 2 a))
  10401. (org-table-eval-formula nil (cdr eq) 'noalign 'nocst 'nostore)))
  10402. ;; back to initial position
  10403. (goto-line thisline)
  10404. (org-table-goto-column thiscol)
  10405. (or noalign (and org-table-may-need-update (org-table-align))
  10406. (and all (message "Re-applying formulas...done")))))
  10407. (defun org-table-formula-substitute-names (f)
  10408. "Replace $const with values in string F."
  10409. (let ((start 0) a n1 n2 nn1 nn2 s (f1 f))
  10410. ;; First, check for column names
  10411. (while (setq start (string-match org-table-column-name-regexp f start))
  10412. (setq start (1+ start))
  10413. (setq a (assoc (match-string 1 f) org-table-column-names))
  10414. (setq f (replace-match (concat "$" (cdr a)) t t f)))
  10415. ;; Expand ranges to vectors
  10416. (while (string-match "\\$\\([0-9]+\\)\\.\\.\\.?\\$\\([0-9]+\\)" f)
  10417. (setq n1 (string-to-number (match-string 1 f))
  10418. n2 (string-to-number (match-string 2 f))
  10419. nn1 (1+ (min n1 n2)) nn2 (max n1 n2)
  10420. s (concat "[($" (number-to-string (1- nn1)) ")"))
  10421. (loop for i from nn1 upto nn2 do
  10422. (setq s (concat s ",($" (int-to-string i) ")")))
  10423. (setq s (concat s "]"))
  10424. (if (< n2 n1) (setq s (concat "rev(" s ")")))
  10425. (setq f (replace-match s t t f)))
  10426. ;; Parameters and constants
  10427. (setq start 0)
  10428. (while (setq start (string-match "\\$\\([a-zA-Z][a-zA-Z0-9]*\\)" f start))
  10429. (setq start (1+ start))
  10430. (if (setq a (save-match-data
  10431. (org-table-get-constant (match-string 1 f))))
  10432. (setq f (replace-match (concat "(" a ")") t t f))))
  10433. (if org-table-formula-debug
  10434. (put-text-property 0 (length f) :orig-formula f1 f))
  10435. f))
  10436. (defun org-table-get-constant (const)
  10437. "Find the value for a parameter or constant in a formula.
  10438. Parameters get priority."
  10439. (or (cdr (assoc const org-table-local-parameters))
  10440. (cdr (assoc const org-table-formula-constants))
  10441. (and (fboundp 'constants-get) (constants-get const))
  10442. "#UNDEFINED_NAME"))
  10443. (defvar org-edit-formulas-map (make-sparse-keymap))
  10444. (define-key org-edit-formulas-map "\C-c\C-c" 'org-finish-edit-formulas)
  10445. (define-key org-edit-formulas-map "\C-c\C-q" 'org-abort-edit-formulas)
  10446. (define-key org-edit-formulas-map "\C-c?" 'org-show-variable)
  10447. (defvar org-pos)
  10448. (defun org-table-edit-formulas ()
  10449. "Edit the formulas of the current table in a separate buffer."
  10450. (interactive)
  10451. (unless (org-at-table-p)
  10452. (error "Not at a table"))
  10453. (org-table-get-specials)
  10454. (let ((eql (org-table-get-stored-formulas))
  10455. (pos (move-marker (make-marker) (point)))
  10456. (wc (current-window-configuration))
  10457. entry loc s)
  10458. (switch-to-buffer-other-window "*Edit Formulas*")
  10459. (erase-buffer)
  10460. (fundamental-mode)
  10461. (set (make-local-variable 'org-pos) pos)
  10462. (set (make-local-variable 'org-window-configuration) wc)
  10463. (use-local-map org-edit-formulas-map)
  10464. (setq s "# Edit formulas and finish with `C-c C-c'.
  10465. # Use `C-u C-c C-c' to also appy them immediately to the entire table.
  10466. # Use `C-c ?' to get information about $name at point.
  10467. # To cancel editing, press `C-c C-q'.\n")
  10468. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  10469. (insert s)
  10470. (while (setq entry (pop eql))
  10471. (when (setq loc (assoc (car entry) org-table-named-field-locations))
  10472. (setq s (format "# Named formula, referring to column %d in line %d\n"
  10473. (nth 2 loc) (nth 1 loc)))
  10474. (put-text-property 0 (length s) 'face 'font-lock-comment-face s)
  10475. (insert s))
  10476. (setq s (concat "$" (car entry) "=" (cdr entry) "\n"))
  10477. (remove-text-properties 0 (length s) '(face nil) s)
  10478. (insert s))
  10479. (goto-char (point-min))
  10480. (message "Edit formulas and finish with `C-c C-c'.")))
  10481. (defun org-show-variable ()
  10482. "Show the location/value of the $ expression at point."
  10483. (interactive)
  10484. (let (var (pos org-pos) (win (selected-window)) e)
  10485. (save-excursion
  10486. (or (looking-at "\\$") (skip-chars-backward "$a-zA-Z0-9"))
  10487. (if (looking-at "\\$\\([a-zA-Z0-9]+\\)")
  10488. (setq var (match-string 1))
  10489. (error "No variable at point")))
  10490. (cond
  10491. ((setq e (assoc var org-table-named-field-locations))
  10492. (switch-to-buffer-other-window (marker-buffer pos))
  10493. (goto-line (nth 1 e))
  10494. (org-table-goto-column (nth 2 e))
  10495. (select-window win)
  10496. (message "Named field, column %d of line %d" (nth 2 e) (nth 1 e)))
  10497. ((setq e (assoc var org-table-column-names))
  10498. (switch-to-buffer-other-window (marker-buffer pos))
  10499. (goto-char pos)
  10500. (goto-char (org-table-begin))
  10501. (if (re-search-forward (concat "^[ \t]*| *! *.*?| *\\(" var "\\) *|")
  10502. (org-table-end) t)
  10503. (progn
  10504. (goto-char (match-beginning 1))
  10505. (message "Named column (column %s)" (cdr e)))
  10506. (error "Column name not found"))
  10507. (select-window win))
  10508. ((string-match "^[0-9]$" var)
  10509. ;; column number
  10510. (switch-to-buffer-other-window (marker-buffer pos))
  10511. (goto-char pos)
  10512. (goto-char (org-table-begin))
  10513. (recenter 1)
  10514. (if (re-search-forward org-table-dataline-regexp
  10515. (org-table-end) t)
  10516. (progn
  10517. (goto-char (match-beginning 0))
  10518. (org-table-goto-column (string-to-number var))
  10519. (message "Column %s" var))
  10520. (error "Column name not found"))
  10521. (select-window win))
  10522. ((setq e (assoc var org-table-local-parameters))
  10523. (switch-to-buffer-other-window (marker-buffer pos))
  10524. (goto-char pos)
  10525. (goto-char (org-table-begin))
  10526. (if (re-search-forward (concat "^[ \t]*| *\\$ *.*?| *\\(" var "=\\)") nil t)
  10527. (progn
  10528. (goto-char (match-beginning 1))
  10529. (message "Local parameter."))
  10530. (error "Parameter not found"))
  10531. (select-window win))
  10532. (t
  10533. (cond
  10534. ((setq e (assoc var org-table-formula-constants))
  10535. (message "Constant: $%s=%s in `org-table-formula-constants'." var (cdr e)))
  10536. ((setq e (and (fboundp 'constants-get) (constants-get var)))
  10537. (message "Constant: $%s=%s, retrieved from `constants.el'." var e))
  10538. (t (error "Undefined name $%s" var)))))))
  10539. (defun org-finish-edit-formulas (&optional arg)
  10540. "Parse the buffer for formula definitions and install them.
  10541. With prefix ARG, apply the new formulas to the table."
  10542. (interactive "P")
  10543. (let ((pos org-pos) eql)
  10544. (goto-char (point-min))
  10545. (while (re-search-forward "^\\$\\([a-zA-Z0-9]+\\) *= *\\(.*?\\) *$" nil t)
  10546. (push (cons (match-string 1) (match-string 2)) eql))
  10547. (set-window-configuration org-window-configuration)
  10548. (select-window (get-buffer-window (marker-buffer pos)))
  10549. (goto-char pos)
  10550. (unless (org-at-table-p)
  10551. (error "Lost table position - cannot install formulae"))
  10552. (org-table-store-formulas eql)
  10553. (move-marker pos nil)
  10554. (kill-buffer "*Edit Formulas*")
  10555. (if arg
  10556. (org-table-recalculate 'all)
  10557. (message "New formulas installed - press C-u C-c C-c to apply."))))
  10558. (defun org-abort-edit-formulas ()
  10559. "Abort editing formulas, without installing the changes."
  10560. (interactive)
  10561. (let ((pos org-pos))
  10562. (set-window-configuration org-window-configuration)
  10563. (select-window (get-buffer-window (marker-buffer pos)))
  10564. (goto-char pos)
  10565. (message "Formula editing aborted without installing changes")))
  10566. ;;; The orgtbl minor mode
  10567. ;; Define a minor mode which can be used in other modes in order to
  10568. ;; integrate the org-mode table editor.
  10569. ;; This is really a hack, because the org-mode table editor uses several
  10570. ;; keys which normally belong to the major mode, for example the TAB and
  10571. ;; RET keys. Here is how it works: The minor mode defines all the keys
  10572. ;; necessary to operate the table editor, but wraps the commands into a
  10573. ;; function which tests if the cursor is currently inside a table. If that
  10574. ;; is the case, the table editor command is executed. However, when any of
  10575. ;; those keys is used outside a table, the function uses `key-binding' to
  10576. ;; look up if the key has an associated command in another currently active
  10577. ;; keymap (minor modes, major mode, global), and executes that command.
  10578. ;; There might be problems if any of the keys used by the table editor is
  10579. ;; otherwise used as a prefix key.
  10580. ;; Another challenge is that the key binding for TAB can be tab or \C-i,
  10581. ;; likewise the binding for RET can be return or \C-m. Orgtbl-mode
  10582. ;; addresses this by checking explicitly for both bindings.
  10583. ;; The optimized version (see variable `orgtbl-optimized') takes over
  10584. ;; all keys which are bound to `self-insert-command' in the *global map*.
  10585. ;; Some modes bind other commands to simple characters, for example
  10586. ;; AUCTeX binds the double quote to `Tex-insert-quote'. With orgtbl-mode
  10587. ;; active, this binding is ignored inside tables and replaced with a
  10588. ;; modified self-insert.
  10589. (defvar orgtbl-mode nil
  10590. "Variable controlling `orgtbl-mode', a minor mode enabling the `org-mode'
  10591. table editor in arbitrary modes.")
  10592. (make-variable-buffer-local 'orgtbl-mode)
  10593. (defvar orgtbl-mode-map (make-keymap)
  10594. "Keymap for `orgtbl-mode'.")
  10595. ;;;###autoload
  10596. (defun turn-on-orgtbl ()
  10597. "Unconditionally turn on `orgtbl-mode'."
  10598. (orgtbl-mode 1))
  10599. ;;;###autoload
  10600. (defun orgtbl-mode (&optional arg)
  10601. "The `org-mode' table editor as a minor mode for use in other modes."
  10602. (interactive)
  10603. (if (eq major-mode 'org-mode)
  10604. ;; Exit without error, in case some hook functions calls this
  10605. ;; by accident in org-mode.
  10606. (message "Orgtbl-mode is not useful in org-mode, command ignored")
  10607. (setq orgtbl-mode
  10608. (if arg (> (prefix-numeric-value arg) 0) (not orgtbl-mode)))
  10609. (if orgtbl-mode
  10610. (progn
  10611. (and (orgtbl-setup) (defun orgtbl-setup () nil))
  10612. ;; Make sure we are first in minor-mode-map-alist
  10613. (let ((c (assq 'orgtbl-mode minor-mode-map-alist)))
  10614. (and c (setq minor-mode-map-alist
  10615. (cons c (delq c minor-mode-map-alist)))))
  10616. (set (make-local-variable (quote org-table-may-need-update)) t)
  10617. (org-add-hook 'before-change-functions 'org-before-change-function
  10618. nil 'local)
  10619. (set (make-local-variable 'org-old-auto-fill-inhibit-regexp)
  10620. auto-fill-inhibit-regexp)
  10621. (set (make-local-variable 'auto-fill-inhibit-regexp)
  10622. (if auto-fill-inhibit-regexp
  10623. (concat "\\([ \t]*|\\|" auto-fill-inhibit-regexp)
  10624. "[ \t]*|"))
  10625. (org-add-to-invisibility-spec '(org-cwidth))
  10626. (easy-menu-add orgtbl-mode-menu)
  10627. (run-hooks 'orgtbl-mode-hook))
  10628. (setq auto-fill-inhibit-regexp org-old-auto-fill-inhibit-regexp)
  10629. (org-cleanup-narrow-column-properties)
  10630. (org-remove-from-invisibility-spec '(org-cwidth))
  10631. (remove-hook 'before-change-functions 'org-before-change-function t)
  10632. (easy-menu-remove orgtbl-mode-menu)
  10633. (force-mode-line-update 'all))))
  10634. (defun org-cleanup-narrow-column-properties ()
  10635. "Remove all properties related to narrow-column invisibility."
  10636. (let ((s 1))
  10637. (while (setq s (text-property-any s (point-max)
  10638. 'display org-narrow-column-arrow))
  10639. (remove-text-properties s (1+ s) '(display t)))
  10640. (setq s 1)
  10641. (while (setq s (text-property-any s (point-max) 'org-cwidth 1))
  10642. (remove-text-properties s (1+ s) '(org-cwidth t)))
  10643. (setq s 1)
  10644. (while (setq s (text-property-any s (point-max) 'invisible 'org-cwidth))
  10645. (remove-text-properties s (1+ s) '(invisible t)))))
  10646. ;; Install it as a minor mode.
  10647. (put 'orgtbl-mode :included t)
  10648. (put 'orgtbl-mode :menu-tag "Org Table Mode")
  10649. (add-minor-mode 'orgtbl-mode " OrgTbl" orgtbl-mode-map)
  10650. (defun orgtbl-make-binding (fun n &rest keys)
  10651. "Create a function for binding in the table minor mode.
  10652. FUN is the command to call inside a table. N is used to create a unique
  10653. command name. KEYS are keys that should be checked in for a command
  10654. to execute outside of tables."
  10655. (eval
  10656. (list 'defun
  10657. (intern (concat "orgtbl-hijacker-command-" (int-to-string n)))
  10658. '(arg)
  10659. (concat "In tables, run `" (symbol-name fun) "'.\n"
  10660. "Outside of tables, run the binding of `"
  10661. (mapconcat (lambda (x) (format "%s" x)) keys "' or `")
  10662. "'.")
  10663. '(interactive "p")
  10664. (list 'if
  10665. '(org-at-table-p)
  10666. (list 'call-interactively (list 'quote fun))
  10667. (list 'let '(orgtbl-mode)
  10668. (list 'call-interactively
  10669. (append '(or)
  10670. (mapcar (lambda (k)
  10671. (list 'key-binding k))
  10672. keys)
  10673. '('orgtbl-error))))))))
  10674. (defun orgtbl-error ()
  10675. "Error when there is no default binding for a table key."
  10676. (interactive)
  10677. (error "This key is has no function outside tables"))
  10678. (defun orgtbl-setup ()
  10679. "Setup orgtbl keymaps."
  10680. (let ((nfunc 0)
  10681. (bindings
  10682. (list
  10683. '([(meta shift left)] org-table-delete-column)
  10684. '([(meta left)] org-table-move-column-left)
  10685. '([(meta right)] org-table-move-column-right)
  10686. '([(meta shift right)] org-table-insert-column)
  10687. '([(meta shift up)] org-table-kill-row)
  10688. '([(meta shift down)] org-table-insert-row)
  10689. '([(meta up)] org-table-move-row-up)
  10690. '([(meta down)] org-table-move-row-down)
  10691. '("\C-c\C-w" org-table-cut-region)
  10692. '("\C-c\M-w" org-table-copy-region)
  10693. '("\C-c\C-y" org-table-paste-rectangle)
  10694. '("\C-c-" org-table-insert-hline)
  10695. ; '([(shift tab)] org-table-previous-field)
  10696. '("\C-m" org-table-next-row)
  10697. (list (org-key 'S-return) 'org-table-copy-down)
  10698. '([(meta return)] org-table-wrap-region)
  10699. '("\C-c\C-q" org-table-wrap-region)
  10700. '("\C-c?" org-table-current-column)
  10701. '("\C-c " org-table-blank-field)
  10702. '("\C-c+" org-table-sum)
  10703. '("\C-c=" org-table-eval-formula)
  10704. '("\C-c'" org-table-edit-formulas)
  10705. '("\C-c`" org-table-edit-field)
  10706. '("\C-c*" org-table-recalculate)
  10707. '("\C-c|" org-table-create-or-convert-from-region)
  10708. '("\C-c^" org-table-sort-lines)
  10709. '([(control ?#)] org-table-rotate-recalc-marks)))
  10710. elt key fun cmd)
  10711. (while (setq elt (pop bindings))
  10712. (setq nfunc (1+ nfunc))
  10713. (setq key (car elt)
  10714. fun (nth 1 elt)
  10715. cmd (orgtbl-make-binding fun nfunc key))
  10716. (define-key orgtbl-mode-map key cmd))
  10717. ;; Special treatment needed for TAB and RET
  10718. (define-key orgtbl-mode-map [(return)]
  10719. (orgtbl-make-binding 'orgtbl-ret 100 [(return)] "\C-m"))
  10720. (define-key orgtbl-mode-map "\C-m"
  10721. (orgtbl-make-binding 'orgtbl-ret 101 "\C-m" [(return)]))
  10722. (define-key orgtbl-mode-map [(tab)]
  10723. (orgtbl-make-binding 'orgtbl-tab 102 [(tab)] "\C-i"))
  10724. (define-key orgtbl-mode-map "\C-i"
  10725. (orgtbl-make-binding 'orgtbl-tab 103 "\C-i" [(tab)])))
  10726. (define-key orgtbl-mode-map "\C-i"
  10727. (orgtbl-make-binding 'orgtbl-tab 104 [(shift tab)]))
  10728. (define-key orgtbl-mode-map "\C-c\C-c"
  10729. (orgtbl-make-binding 'org-ctrl-c-ctrl-c 105 "\C-c\C-c"))
  10730. (when orgtbl-optimized
  10731. ;; If the user wants maximum table support, we need to hijack
  10732. ;; some standard editing functions
  10733. (org-remap orgtbl-mode-map
  10734. 'self-insert-command 'orgtbl-self-insert-command
  10735. 'delete-char 'org-delete-char
  10736. 'delete-backward-char 'org-delete-backward-char)
  10737. (define-key orgtbl-mode-map "|" 'org-force-self-insert))
  10738. (easy-menu-define orgtbl-mode-menu orgtbl-mode-map "OrgTbl menu"
  10739. '("OrgTbl"
  10740. ["Align" org-ctrl-c-ctrl-c :active (org-at-table-p) :keys "C-c C-c"]
  10741. ["Next Field" org-cycle :active (org-at-table-p) :keys "TAB"]
  10742. ["Previous Field" org-shifttab :active (org-at-table-p) :keys "S-TAB"]
  10743. ["Next Row" org-return :active (org-at-table-p) :keys "RET"]
  10744. "--"
  10745. ["Blank Field" org-table-blank-field :active (org-at-table-p) :keys "C-c SPC"]
  10746. ["Edit Field" org-table-edit-field :active (org-at-table-p) :keys "C-c ` "]
  10747. ["Copy Field from Above"
  10748. org-table-copy-down :active (org-at-table-p) :keys "S-RET"]
  10749. "--"
  10750. ("Column"
  10751. ["Move Column Left" org-metaleft :active (org-at-table-p) :keys "M-<left>"]
  10752. ["Move Column Right" org-metaright :active (org-at-table-p) :keys "M-<right>"]
  10753. ["Delete Column" org-shiftmetaleft :active (org-at-table-p) :keys "M-S-<left>"]
  10754. ["Insert Column" org-shiftmetaright :active (org-at-table-p) :keys "M-S-<right>"]
  10755. "--"
  10756. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  10757. ("Row"
  10758. ["Move Row Up" org-metaup :active (org-at-table-p) :keys "M-<up>"]
  10759. ["Move Row Down" org-metadown :active (org-at-table-p) :keys "M-<down>"]
  10760. ["Delete Row" org-shiftmetaup :active (org-at-table-p) :keys "M-S-<up>"]
  10761. ["Insert Row" org-shiftmetadown :active (org-at-table-p) :keys "M-S-<down>"]
  10762. ["Sort lines in region" org-table-sort-lines (org-at-table-p) :keys "C-c ^"]
  10763. "--"
  10764. ["Insert Hline" org-table-insert-hline :active (org-at-table-p) :keys "C-c -"])
  10765. ("Rectangle"
  10766. ["Copy Rectangle" org-copy-special :active (org-at-table-p)]
  10767. ["Cut Rectangle" org-cut-special :active (org-at-table-p)]
  10768. ["Paste Rectangle" org-paste-special :active (org-at-table-p)]
  10769. ["Fill Rectangle" org-table-wrap-region :active (org-at-table-p)])
  10770. "--"
  10771. ["Set Column Formula" org-table-eval-formula :active (org-at-table-p) :keys "C-c ="]
  10772. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  10773. ["Edit Formulas" org-table-edit-formulas :active (org-at-table-p) :keys "C-c '"]
  10774. ["Recalculate line" org-table-recalculate :active (org-at-table-p) :keys "C-c *"]
  10775. ["Recalculate all" (org-table-recalculate '(4)) :active (org-at-table-p) :keys "C-u C-c *"]
  10776. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks :active (org-at-table-p) :keys "C-c #"]
  10777. ["Sum Column/Rectangle" org-table-sum
  10778. :active (or (org-at-table-p) (org-region-active-p)) :keys "C-c +"]
  10779. ["Which Column?" org-table-current-column :active (org-at-table-p) :keys "C-c ?"]
  10780. ["Debug Formulas"
  10781. (setq org-table-formula-debug (not org-table-formula-debug))
  10782. :style toggle :selected org-table-formula-debug]
  10783. ))
  10784. t)
  10785. (defun orgtbl-tab (arg)
  10786. "Justification and field motion for `orgtbl-mode'."
  10787. (interactive "P")
  10788. (if arg (org-table-edit-field t)
  10789. (org-table-justify-field-maybe)
  10790. (org-table-next-field)))
  10791. (defun orgtbl-ret ()
  10792. "Justification and field motion for `orgtbl-mode'."
  10793. (interactive)
  10794. (org-table-justify-field-maybe)
  10795. (org-table-next-row))
  10796. (defun orgtbl-self-insert-command (N)
  10797. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  10798. If the cursor is in a table looking at whitespace, the whitespace is
  10799. overwritten, and the table is not marked as requiring realignment."
  10800. (interactive "p")
  10801. (if (and (org-at-table-p)
  10802. (or
  10803. (and org-table-auto-blank-field
  10804. (member last-command
  10805. '(orgtbl-hijacker-command-100
  10806. orgtbl-hijacker-command-101
  10807. orgtbl-hijacker-command-102
  10808. orgtbl-hijacker-command-103
  10809. orgtbl-hijacker-command-104
  10810. orgtbl-hijacker-command-105))
  10811. (org-table-blank-field))
  10812. t)
  10813. (eq N 1)
  10814. (looking-at "[^|\n]* +|"))
  10815. (let (org-table-may-need-update)
  10816. (goto-char (1- (match-end 0)))
  10817. (delete-backward-char 1)
  10818. (goto-char (match-beginning 0))
  10819. (self-insert-command N))
  10820. (setq org-table-may-need-update t)
  10821. (let (orgtbl-mode)
  10822. (call-interactively (key-binding (vector last-input-event))))))
  10823. (defun org-force-self-insert (N)
  10824. "Needed to enforce self-insert under remapping."
  10825. (interactive "p")
  10826. (self-insert-command N))
  10827. ;;; Exporting
  10828. (defconst org-level-max 20)
  10829. (defvar org-export-html-preamble nil
  10830. "Preamble, to be inserted just after <body>. Set by publishing functions.")
  10831. (defvar org-export-html-postamble nil
  10832. "Preamble, to be inserted just before </body>. Set by publishing functions.")
  10833. (defvar org-export-html-auto-preamble t
  10834. "Should default preamble be inserted? Set by publishing functions.")
  10835. (defvar org-export-html-auto-postamble t
  10836. "Should default postamble be inserted? Set by publishing functions.")
  10837. (defconst org-export-plist-vars
  10838. '((:language . org-export-default-language)
  10839. (:headline-levels . org-export-headline-levels)
  10840. (:section-numbers . org-export-with-section-numbers)
  10841. (:table-of-contents . org-export-with-toc)
  10842. (:emphasize . org-export-with-emphasize)
  10843. (:sub-superscript . org-export-with-sub-superscripts)
  10844. (:TeX-macros . org-export-with-TeX-macros)
  10845. (:fixed-width . org-export-with-fixed-width)
  10846. (:timestamps . org-export-with-timestamps)
  10847. (:tables . org-export-with-tables)
  10848. (:table-auto-headline . org-export-highlight-first-table-line)
  10849. (:style . org-export-html-style)
  10850. (:convert-org-links . org-export-html-link-org-files-as-html)
  10851. (:inline-images . org-export-html-inline-images)
  10852. (:expand-quoted-html . org-export-html-expand)
  10853. (:timestamp . org-export-html-with-timestamp)
  10854. (:publishing-directory . org-export-publishing-directory)
  10855. (:preamble . org-export-html-preamble)
  10856. (:postamble . org-export-html-postamble)
  10857. (:auto-preamble . org-export-html-auto-preamble)
  10858. (:auto-postamble . org-export-html-auto-postamble)
  10859. (:author . user-full-name)
  10860. (:email . user-mail-address)))
  10861. (defun org-default-export-plist ()
  10862. "Return the property list with default settings for the export variables."
  10863. (let ((l org-export-plist-vars) rtn e)
  10864. (while (setq e (pop l))
  10865. (setq rtn (cons (car e) (cons (symbol-value (cdr e)) rtn))))
  10866. rtn))
  10867. (defun org-infile-export-plist ()
  10868. "Return the property list with file-local settings for export."
  10869. (save-excursion
  10870. (goto-char 0)
  10871. (let ((re (org-make-options-regexp
  10872. '("TITLE" "AUTHOR" "EMAIL" "TEXT" "OPTIONS" "LANGUAGE")))
  10873. (text nil)
  10874. p key val text options)
  10875. (while (re-search-forward re nil t)
  10876. (setq key (org-match-string-no-properties 1)
  10877. val (org-match-string-no-properties 2))
  10878. (cond
  10879. ((string-equal key "TITLE") (setq p (plist-put p :title val)))
  10880. ((string-equal key "AUTHOR")(setq p (plist-put p :author val)))
  10881. ((string-equal key "EMAIL") (setq p (plist-put p :email val)))
  10882. ((string-equal key "LANGUAGE") (setq p (plist-put p :language val)))
  10883. ((string-equal key "TEXT")
  10884. (setq text (if text (concat text "\n" val) val)))
  10885. ((string-equal key "OPTIONS") (setq options val))))
  10886. (setq p (plist-put p :text text))
  10887. (when options
  10888. (let ((op '(("H" . :headline-levels)
  10889. ("num" . :section-numbers)
  10890. ("toc" . :table-of-contents)
  10891. ("\\n" . :preserve-breaks)
  10892. ("@" . :expand-quoted-html)
  10893. (":" . :fixed-width)
  10894. ("|" . :tables)
  10895. ("^" . :sub-superscript)
  10896. ("*" . :emphasize)
  10897. ("TeX" . :TeX-macros)))
  10898. o)
  10899. (while (setq o (pop op))
  10900. (if (string-match (concat (regexp-quote (car o))
  10901. ":\\([^ \t\n\r;,.]*\\)")
  10902. options)
  10903. (setq p (plist-put p (cdr o)
  10904. (car (read-from-string
  10905. (match-string 1 options)))))))))
  10906. p)))
  10907. (defun org-combine-plists (&rest plists)
  10908. "Create a single property list from all plists in PLISTS.
  10909. The process starts by copying the last list, and then setting properties
  10910. from the other lists. Settings in the first list are the most significant
  10911. ones and overrule settings in the other lists."
  10912. (let ((rtn (copy-sequence (pop plists)))
  10913. p v ls)
  10914. (while plists
  10915. (setq ls (pop plists))
  10916. (while ls
  10917. (setq p (pop ls) v (pop ls))
  10918. (setq rtn (plist-put rtn p v))))
  10919. rtn))
  10920. (defun org-export-directory (type plist)
  10921. (let* ((val (plist-get plist :publishing-directory))
  10922. (dir (if (listp val)
  10923. (or (cdr (assoc type val)) ".")
  10924. val)))
  10925. dir))
  10926. (defun org-export-find-first-heading-line (list)
  10927. "Remove all lines from LIST which are before the first headline."
  10928. (let ((orig-list list)
  10929. (re (concat "^" outline-regexp)))
  10930. (while (and list
  10931. (not (string-match re (car list))))
  10932. (pop list))
  10933. (or list orig-list)))
  10934. (defun org-skip-comments (lines)
  10935. "Skip lines starting with \"#\" and subtrees starting with COMMENT."
  10936. (let ((re1 (concat "^\\(\\*+\\)[ \t]+" org-comment-string))
  10937. (re2 "^\\(\\*+\\)[ \t\n\r]")
  10938. rtn line level)
  10939. (while (setq line (pop lines))
  10940. (cond
  10941. ((and (string-match re1 line)
  10942. (setq level (- (match-end 1) (match-beginning 1))))
  10943. ;; Beginning of a COMMENT subtree. Skip it.
  10944. (while (and (setq line (pop lines))
  10945. (or (not (string-match re2 line))
  10946. (> (- (match-end 1) (match-beginning 1)) level))))
  10947. (setq lines (cons line lines)))
  10948. ((string-match "^#" line)
  10949. ;; an ordinary comment line
  10950. )
  10951. ((and org-export-table-remove-special-lines
  10952. (string-match "^[ \t]*|" line)
  10953. (or (string-match "^[ \t]*| *[!_^] *|" line)
  10954. (and (string-match "| *<[0-9]+> *|" line)
  10955. (not (string-match "| *[^ <|]" line)))))
  10956. ;; a special table line that should be removed
  10957. )
  10958. (t (setq rtn (cons line rtn)))))
  10959. (nreverse rtn)))
  10960. ;; ASCII
  10961. (defconst org-html-entities
  10962. '(("nbsp")
  10963. ("iexcl")
  10964. ("cent")
  10965. ("pound")
  10966. ("curren")
  10967. ("yen")
  10968. ("brvbar")
  10969. ("sect")
  10970. ("uml")
  10971. ("copy")
  10972. ("ordf")
  10973. ("laquo")
  10974. ("not")
  10975. ("shy")
  10976. ("reg")
  10977. ("macr")
  10978. ("deg")
  10979. ("plusmn")
  10980. ("sup2")
  10981. ("sup3")
  10982. ("acute")
  10983. ("micro")
  10984. ("para")
  10985. ("middot")
  10986. ("odot"."o")
  10987. ("star"."*")
  10988. ("cedil")
  10989. ("sup1")
  10990. ("ordm")
  10991. ("raquo")
  10992. ("frac14")
  10993. ("frac12")
  10994. ("frac34")
  10995. ("iquest")
  10996. ("Agrave")
  10997. ("Aacute")
  10998. ("Acirc")
  10999. ("Atilde")
  11000. ("Auml")
  11001. ("Aring") ("AA"."&Aring;")
  11002. ("AElig")
  11003. ("Ccedil")
  11004. ("Egrave")
  11005. ("Eacute")
  11006. ("Ecirc")
  11007. ("Euml")
  11008. ("Igrave")
  11009. ("Iacute")
  11010. ("Icirc")
  11011. ("Iuml")
  11012. ("ETH")
  11013. ("Ntilde")
  11014. ("Ograve")
  11015. ("Oacute")
  11016. ("Ocirc")
  11017. ("Otilde")
  11018. ("Ouml")
  11019. ("times")
  11020. ("Oslash")
  11021. ("Ugrave")
  11022. ("Uacute")
  11023. ("Ucirc")
  11024. ("Uuml")
  11025. ("Yacute")
  11026. ("THORN")
  11027. ("szlig")
  11028. ("agrave")
  11029. ("aacute")
  11030. ("acirc")
  11031. ("atilde")
  11032. ("auml")
  11033. ("aring")
  11034. ("aelig")
  11035. ("ccedil")
  11036. ("egrave")
  11037. ("eacute")
  11038. ("ecirc")
  11039. ("euml")
  11040. ("igrave")
  11041. ("iacute")
  11042. ("icirc")
  11043. ("iuml")
  11044. ("eth")
  11045. ("ntilde")
  11046. ("ograve")
  11047. ("oacute")
  11048. ("ocirc")
  11049. ("otilde")
  11050. ("ouml")
  11051. ("divide")
  11052. ("oslash")
  11053. ("ugrave")
  11054. ("uacute")
  11055. ("ucirc")
  11056. ("uuml")
  11057. ("yacute")
  11058. ("thorn")
  11059. ("yuml")
  11060. ("fnof")
  11061. ("Alpha")
  11062. ("Beta")
  11063. ("Gamma")
  11064. ("Delta")
  11065. ("Epsilon")
  11066. ("Zeta")
  11067. ("Eta")
  11068. ("Theta")
  11069. ("Iota")
  11070. ("Kappa")
  11071. ("Lambda")
  11072. ("Mu")
  11073. ("Nu")
  11074. ("Xi")
  11075. ("Omicron")
  11076. ("Pi")
  11077. ("Rho")
  11078. ("Sigma")
  11079. ("Tau")
  11080. ("Upsilon")
  11081. ("Phi")
  11082. ("Chi")
  11083. ("Psi")
  11084. ("Omega")
  11085. ("alpha")
  11086. ("beta")
  11087. ("gamma")
  11088. ("delta")
  11089. ("epsilon")
  11090. ("varepsilon"."&epsilon;")
  11091. ("zeta")
  11092. ("eta")
  11093. ("theta")
  11094. ("iota")
  11095. ("kappa")
  11096. ("lambda")
  11097. ("mu")
  11098. ("nu")
  11099. ("xi")
  11100. ("omicron")
  11101. ("pi")
  11102. ("rho")
  11103. ("sigmaf") ("varsigma"."&sigmaf;")
  11104. ("sigma")
  11105. ("tau")
  11106. ("upsilon")
  11107. ("phi")
  11108. ("chi")
  11109. ("psi")
  11110. ("omega")
  11111. ("thetasym") ("vartheta"."&thetasym;")
  11112. ("upsih")
  11113. ("piv")
  11114. ("bull") ("bullet"."&bull;")
  11115. ("hellip") ("dots"."&hellip;")
  11116. ("prime")
  11117. ("Prime")
  11118. ("oline")
  11119. ("frasl")
  11120. ("weierp")
  11121. ("image")
  11122. ("real")
  11123. ("trade")
  11124. ("alefsym")
  11125. ("larr") ("leftarrow"."&larr;") ("gets"."&larr;")
  11126. ("uarr") ("uparrow"."&uarr;")
  11127. ("rarr") ("to"."&rarr;") ("rightarrow"."&rarr;")
  11128. ("darr")("downarrow"."&darr;")
  11129. ("harr") ("leftrightarrow"."&harr;")
  11130. ("crarr") ("hookleftarrow"."&crarr;") ; has round hook, not quite CR
  11131. ("lArr") ("Leftarrow"."&lArr;")
  11132. ("uArr") ("Uparrow"."&uArr;")
  11133. ("rArr") ("Rightarrow"."&rArr;")
  11134. ("dArr") ("Downarrow"."&dArr;")
  11135. ("hArr") ("Leftrightarrow"."&hArr;")
  11136. ("forall")
  11137. ("part") ("partial"."&part;")
  11138. ("exist") ("exists"."&exist;")
  11139. ("empty") ("emptyset"."&empty;")
  11140. ("nabla")
  11141. ("isin") ("in"."&isin;")
  11142. ("notin")
  11143. ("ni")
  11144. ("prod")
  11145. ("sum")
  11146. ("minus")
  11147. ("lowast") ("ast"."&lowast;")
  11148. ("radic")
  11149. ("prop") ("proptp"."&prop;")
  11150. ("infin") ("infty"."&infin;")
  11151. ("ang") ("angle"."&ang;")
  11152. ("and") ("vee"."&and;")
  11153. ("or") ("wedge"."&or;")
  11154. ("cap")
  11155. ("cup")
  11156. ("int")
  11157. ("there4")
  11158. ("sim")
  11159. ("cong") ("simeq"."&cong;")
  11160. ("asymp")("approx"."&asymp;")
  11161. ("ne") ("neq"."&ne;")
  11162. ("equiv")
  11163. ("le")
  11164. ("ge")
  11165. ("sub") ("subset"."&sub;")
  11166. ("sup") ("supset"."&sup;")
  11167. ("nsub")
  11168. ("sube")
  11169. ("supe")
  11170. ("oplus")
  11171. ("otimes")
  11172. ("perp")
  11173. ("sdot") ("cdot"."&sdot;")
  11174. ("lceil")
  11175. ("rceil")
  11176. ("lfloor")
  11177. ("rfloor")
  11178. ("lang")
  11179. ("rang")
  11180. ("loz") ("Diamond"."&loz;")
  11181. ("spades") ("spadesuit"."&spades;")
  11182. ("clubs") ("clubsuit"."&clubs;")
  11183. ("hearts") ("diamondsuit"."&hearts;")
  11184. ("diams") ("diamondsuit"."&diams;")
  11185. ("quot")
  11186. ("amp")
  11187. ("lt")
  11188. ("gt")
  11189. ("OElig")
  11190. ("oelig")
  11191. ("Scaron")
  11192. ("scaron")
  11193. ("Yuml")
  11194. ("circ")
  11195. ("tilde")
  11196. ("ensp")
  11197. ("emsp")
  11198. ("thinsp")
  11199. ("zwnj")
  11200. ("zwj")
  11201. ("lrm")
  11202. ("rlm")
  11203. ("ndash")
  11204. ("mdash")
  11205. ("lsquo")
  11206. ("rsquo")
  11207. ("sbquo")
  11208. ("ldquo")
  11209. ("rdquo")
  11210. ("bdquo")
  11211. ("dagger")
  11212. ("Dagger")
  11213. ("permil")
  11214. ("lsaquo")
  11215. ("rsaquo")
  11216. ("euro")
  11217. ("arccos"."arccos")
  11218. ("arcsin"."arcsin")
  11219. ("arctan"."arctan")
  11220. ("arg"."arg")
  11221. ("cos"."cos")
  11222. ("cosh"."cosh")
  11223. ("cot"."cot")
  11224. ("coth"."coth")
  11225. ("csc"."csc")
  11226. ("deg"."deg")
  11227. ("det"."det")
  11228. ("dim"."dim")
  11229. ("exp"."exp")
  11230. ("gcd"."gcd")
  11231. ("hom"."hom")
  11232. ("inf"."inf")
  11233. ("ker"."ker")
  11234. ("lg"."lg")
  11235. ("lim"."lim")
  11236. ("liminf"."liminf")
  11237. ("limsup"."limsup")
  11238. ("ln"."ln")
  11239. ("log"."log")
  11240. ("max"."max")
  11241. ("min"."min")
  11242. ("Pr"."Pr")
  11243. ("sec"."sec")
  11244. ("sin"."sin")
  11245. ("sinh"."sinh")
  11246. ("sup"."sup")
  11247. ("tan"."tan")
  11248. ("tanh"."tanh")
  11249. )
  11250. "Entities for TeX->HTML translation.
  11251. Entries can be like (\"ent\"), in which case \"\\ent\" will be translated to
  11252. \"&ent;\". An entry can also be a dotted pair like (\"ent\".\"&other;\").
  11253. In that case, \"\\ent\" will be translated to \"&other;\".
  11254. The list contains HTML entities for Latin-1, Greek and other symbols.
  11255. It is supplemented by a number of commonly used TeX macros with appropriate
  11256. translations. There is currently no way for users to extend this.")
  11257. (defun org-cleaned-string-for-export (string)
  11258. "Cleanup a buffer substring so that links can be created safely."
  11259. (interactive)
  11260. (let* ((cb (current-buffer))
  11261. (re-radio (and org-target-link-regexp
  11262. (concat "\\([^<]\\)\\(" org-target-link-regexp "\\)")))
  11263. (re-plain-link (concat "\\([^[<]\\)" org-plain-link-re))
  11264. (re-angle-link (concat "\\([^[]\\)" org-angle-link-re))
  11265. rtn)
  11266. (save-excursion
  11267. (set-buffer (get-buffer-create " org-mode-tmp"))
  11268. (erase-buffer)
  11269. (insert string)
  11270. (org-mode)
  11271. ;; Find targets in comments and move them out of comments,
  11272. ;; but mark them as targets that should be invisible
  11273. (goto-char (point-min))
  11274. (while (re-search-forward "^#.*?\\(<<<?[^>\r\n]+>>>?\\).*" nil t)
  11275. (replace-match "\\1(INVISIBLE)"))
  11276. ;; Find matches for radio targets and turn them into internal links
  11277. (goto-char (point-min))
  11278. (when re-radio
  11279. (while (re-search-forward re-radio nil t)
  11280. (replace-match "\\1[[\\2]]")))
  11281. ;; Find all links that contain a newline and put them into a single line
  11282. (goto-char (point-min))
  11283. (while (re-search-forward "\\(\\(\\[\\|\\]\\)\\[[^]]*?\\)[ \t]*\n[ \t]*\\([^]]*\\]\\(\\[\\|\\]\\)\\)" nil t)
  11284. (replace-match "\\1 \\3")
  11285. (goto-char (match-beginning 0)))
  11286. ;; Normalize links: Convert angle and plain links into bracket links
  11287. (goto-char (point-min))
  11288. (while (re-search-forward re-plain-link nil t)
  11289. (replace-match
  11290. (concat
  11291. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  11292. t t))
  11293. (goto-char (point-min))
  11294. (while (re-search-forward re-angle-link nil t)
  11295. (replace-match
  11296. (concat
  11297. (match-string 1) "[[" (match-string 2) ":" (match-string 3) "]]")
  11298. t t))
  11299. ;; Remove comments
  11300. (goto-char (point-min))
  11301. (while (re-search-forward "^#.*\n?" nil t)
  11302. (replace-match ""))
  11303. (setq rtn (buffer-string)))
  11304. (kill-buffer " org-mode-tmp")
  11305. rtn))
  11306. (defun org-solidify-link-text (s &optional alist)
  11307. "Take link text and make a safe target out of it."
  11308. (save-match-data
  11309. (let* ((rtn
  11310. (mapconcat
  11311. 'identity
  11312. (org-split-string s "[ \t\r\n]+") "--"))
  11313. (a (assoc rtn alist)))
  11314. (or (cdr a) rtn))))
  11315. (defun org-convert-to-odd-levels ()
  11316. "Convert an org-mode file with all levels allowed to one with odd levels.
  11317. This will leave level 1 alone, convert level 2 to level 3, level 3 to
  11318. level 5 etc."
  11319. (interactive)
  11320. (when (yes-or-no-p "Are you sure you want to globally change levels to odd? ")
  11321. (let ((org-odd-levels-only nil) n)
  11322. (save-excursion
  11323. (goto-char (point-min))
  11324. (while (re-search-forward "^\\*\\*+" nil t)
  11325. (setq n (1- (length (match-string 0))))
  11326. (while (>= (setq n (1- n)) 0)
  11327. (org-demote))
  11328. (end-of-line 1))))))
  11329. (defun org-convert-to-oddeven-levels ()
  11330. "Convert an org-mode file with only odd levels to one with odd and even levels.
  11331. This promotes level 3 to level 2, level 5 to level 3 etc. If the file contains a
  11332. section with an even level, conversion would destroy the structure of the file. An error
  11333. is signaled in this case."
  11334. (interactive)
  11335. (goto-char (point-min))
  11336. ;; First check if there are no even levels
  11337. (when (re-search-forward "^\\(\\*\\*\\)+[^*]" nil t)
  11338. (org-show-hierarchy-above)
  11339. (error "Not all levels are odd in this file. Conversion not possible."))
  11340. (when (yes-or-no-p "Are you sure you want to globally change levels to odd-even? ")
  11341. (let ((org-odd-levels-only nil) n)
  11342. (save-excursion
  11343. (goto-char (point-min))
  11344. (while (re-search-forward "^\\*\\*+" nil t)
  11345. (setq n (/ (length (match-string 0)) 2))
  11346. (while (>= (setq n (1- n)) 0)
  11347. (org-promote))
  11348. (end-of-line 1))))))
  11349. (defun org-tr-level (n)
  11350. "Make N odd if required."
  11351. (if org-odd-levels-only (1+ (/ n 2)) n))
  11352. (defvar org-last-level nil) ; dynamically scoped variable
  11353. (defvar org-ascii-current-indentation nil) ; For communication
  11354. (defun org-export-as-ascii (arg)
  11355. "Export the outline as a pretty ASCII file.
  11356. If there is an active region, export only the region.
  11357. The prefix ARG specifies how many levels of the outline should become
  11358. underlined headlines. The default is 3."
  11359. (interactive "P")
  11360. (setq-default org-todo-line-regexp org-todo-line-regexp)
  11361. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  11362. (org-infile-export-plist)))
  11363. (region
  11364. (buffer-substring
  11365. (if (org-region-active-p) (region-beginning) (point-min))
  11366. (if (org-region-active-p) (region-end) (point-max))))
  11367. (lines (org-export-find-first-heading-line
  11368. (org-skip-comments
  11369. (org-split-string
  11370. (org-cleaned-string-for-export region)
  11371. "[\r\n]"))))
  11372. (org-ascii-current-indentation '(0 . 0))
  11373. (org-startup-with-deadline-check nil)
  11374. (level 0) line txt
  11375. (umax nil)
  11376. (case-fold-search nil)
  11377. (filename (concat (file-name-as-directory
  11378. (org-export-directory :ascii opt-plist))
  11379. (file-name-sans-extension
  11380. (file-name-nondirectory buffer-file-name))
  11381. ".txt"))
  11382. (buffer (find-file-noselect filename))
  11383. (levels-open (make-vector org-level-max nil))
  11384. (odd org-odd-levels-only)
  11385. (date (format-time-string "%Y/%m/%d" (current-time)))
  11386. (time (format-time-string "%X" (org-current-time)))
  11387. (author (plist-get opt-plist :author))
  11388. (title (or (plist-get opt-plist :title)
  11389. (file-name-sans-extension
  11390. (file-name-nondirectory buffer-file-name))))
  11391. (options nil)
  11392. (email (plist-get opt-plist :email))
  11393. (language (plist-get opt-plist :language))
  11394. (text nil)
  11395. (todo nil)
  11396. (lang-words nil))
  11397. (setq org-last-level 1)
  11398. (org-init-section-numbers)
  11399. (find-file-noselect filename)
  11400. (setq lang-words (or (assoc language org-export-language-setup)
  11401. (assoc "en" org-export-language-setup)))
  11402. (if org-export-ascii-show-new-buffer
  11403. (switch-to-buffer-other-window buffer)
  11404. (set-buffer buffer))
  11405. (erase-buffer)
  11406. (fundamental-mode)
  11407. ;; create local variables for all options, to make sure all called
  11408. ;; functions get the correct information
  11409. (mapcar (lambda (x)
  11410. (set (make-local-variable (cdr x))
  11411. (plist-get opt-plist (car x))))
  11412. org-export-plist-vars)
  11413. (set (make-local-variable 'org-odd-levels-only) odd)
  11414. (setq umax (if arg (prefix-numeric-value arg)
  11415. org-export-headline-levels))
  11416. ;; File header
  11417. (if title (org-insert-centered title ?=))
  11418. (insert "\n")
  11419. (if (or author email)
  11420. (insert (concat (nth 1 lang-words) ": " (or author "")
  11421. (if email (concat " <" email ">") "")
  11422. "\n")))
  11423. (if (and date time)
  11424. (insert (concat (nth 2 lang-words) ": " date " " time "\n")))
  11425. (if text (insert (concat (org-html-expand-for-ascii text) "\n\n")))
  11426. (insert "\n\n")
  11427. (if org-export-with-toc
  11428. (progn
  11429. (insert (nth 3 lang-words) "\n"
  11430. (make-string (length (nth 3 lang-words)) ?=) "\n")
  11431. (mapcar '(lambda (line)
  11432. (if (string-match org-todo-line-regexp
  11433. line)
  11434. ;; This is a headline
  11435. (progn
  11436. (setq level (- (match-end 1) (match-beginning 1))
  11437. level (org-tr-level level)
  11438. txt (match-string 3 line)
  11439. todo
  11440. (or (and org-export-mark-todo-in-toc
  11441. (match-beginning 2)
  11442. (not (equal (match-string 2 line)
  11443. org-done-string)))
  11444. ; TODO, not DONE
  11445. (and org-export-mark-todo-in-toc
  11446. (= level umax)
  11447. (org-search-todo-below
  11448. line lines level))))
  11449. (setq txt (org-html-expand-for-ascii txt))
  11450. (if org-export-with-section-numbers
  11451. (setq txt (concat (org-section-number level)
  11452. " " txt)))
  11453. (if (<= level umax)
  11454. (progn
  11455. (insert
  11456. (make-string (* (1- level) 4) ?\ )
  11457. (format (if todo "%s (*)\n" "%s\n") txt))
  11458. (setq org-last-level level))
  11459. ))))
  11460. lines)))
  11461. (org-init-section-numbers)
  11462. (while (setq line (pop lines))
  11463. ;; Remove the quoted HTML tags.
  11464. (setq line (org-html-expand-for-ascii line))
  11465. ;; Remove targets
  11466. (while (string-match "<<<?[^<>]*>>>?[ \t]*\n?" line)
  11467. (setq line (replace-match "" t t line)))
  11468. ;; Replace internal links
  11469. (while (string-match org-bracket-link-regexp line)
  11470. (setq line (replace-match
  11471. (if (match-end 3) "[\\3]" "[\\1]")
  11472. t nil line)))
  11473. (cond
  11474. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  11475. ;; a Headline
  11476. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  11477. txt (match-string 2 line))
  11478. (org-ascii-level-start level txt umax lines))
  11479. (t
  11480. (insert (org-fix-indentation line org-ascii-current-indentation) "\n"))))
  11481. (normal-mode)
  11482. (save-buffer)
  11483. ;; remove display and invisible chars
  11484. (let (beg end s)
  11485. (goto-char (point-min))
  11486. (while (setq beg (next-single-property-change (point) 'display))
  11487. (setq end (next-single-property-change beg 'display))
  11488. (delete-region beg end)
  11489. (goto-char beg)
  11490. (insert "=>"))
  11491. (goto-char (point-min))
  11492. (while (setq beg (next-single-property-change (point) 'org-cwidth))
  11493. (setq end (next-single-property-change beg 'org-cwidth))
  11494. (delete-region beg end)
  11495. (goto-char beg)))
  11496. (goto-char (point-min))))
  11497. (defun org-search-todo-below (line lines level)
  11498. "Search the subtree below LINE for any TODO entries."
  11499. (let ((rest (cdr (memq line lines)))
  11500. (re org-todo-line-regexp)
  11501. line lv todo)
  11502. (catch 'exit
  11503. (while (setq line (pop rest))
  11504. (if (string-match re line)
  11505. (progn
  11506. (setq lv (- (match-end 1) (match-beginning 1))
  11507. todo (and (match-beginning 2)
  11508. (not (equal (match-string 2 line)
  11509. org-done-string))))
  11510. ; TODO, not DONE
  11511. (if (<= lv level) (throw 'exit nil))
  11512. (if todo (throw 'exit t))))))))
  11513. (defun org-html-expand-for-ascii (line)
  11514. "Handle quoted HTML for ASCII export."
  11515. (if org-export-html-expand
  11516. (while (string-match "@<[^<>\n]*>" line)
  11517. ;; We just remove the tags for now.
  11518. (setq line (replace-match "" nil nil line))))
  11519. line)
  11520. (defun org-insert-centered (s &optional underline)
  11521. "Insert the string S centered and underline it with character UNDERLINE."
  11522. (let ((ind (max (/ (- 80 (string-width s)) 2) 0)))
  11523. (insert (make-string ind ?\ ) s "\n")
  11524. (if underline
  11525. (insert (make-string ind ?\ )
  11526. (make-string (string-width s) underline)
  11527. "\n"))))
  11528. (defun org-ascii-level-start (level title umax &optional lines)
  11529. "Insert a new level in ASCII export."
  11530. (let (char (n (- level umax 1)) (ind 0))
  11531. (if (> level umax)
  11532. (progn
  11533. (insert (make-string (* 2 n) ?\ )
  11534. (char-to-string (nth (% n (length org-export-ascii-bullets))
  11535. org-export-ascii-bullets))
  11536. " " title "\n")
  11537. ;; find the indentation of the next non-empty line
  11538. (catch 'stop
  11539. (while lines
  11540. (if (string-match "^\\*" (car lines)) (throw 'stop nil))
  11541. (if (string-match "^\\([ \t]*\\)\\S-" (car lines))
  11542. (throw 'stop (setq ind (org-get-indentation (car lines)))))
  11543. (pop lines)))
  11544. (setq org-ascii-current-indentation (cons (* 2 (1+ n)) ind)))
  11545. (if (or (not (equal (char-before) ?\n))
  11546. (not (equal (char-before (1- (point))) ?\n)))
  11547. (insert "\n"))
  11548. (setq char (nth (- umax level) (reverse org-export-ascii-underline)))
  11549. (if org-export-with-section-numbers
  11550. (setq title (concat (org-section-number level) " " title)))
  11551. (insert title "\n" (make-string (string-width title) char) "\n")
  11552. (setq org-ascii-current-indentation '(0 . 0)))))
  11553. (defun org-export-visible (type arg)
  11554. "Create a copy of the visible part of the current buffer, and export it.
  11555. The copy is created in a temporary buffer and removed after use.
  11556. TYPE is the final key (as a string) of the `C-c C-x' key sequence that will
  11557. run the export command - in interactive use, the command prompts for this
  11558. key. As a special case, if the you type SPC at the prompt, the temporary
  11559. org-mode file will not be removed but presented to you so that you can
  11560. continue to use it. The prefix arg ARG is passed through to the exporting
  11561. command."
  11562. (interactive
  11563. (list (progn
  11564. (message "Export visible: [a]SCII [h]tml [b]rowse HTML [x]OXO [ ]keep buffer")
  11565. (char-to-string (read-char-exclusive)))
  11566. current-prefix-arg))
  11567. (if (not (member type '("a" "\C-a" "b" "\C-b" "h" "x" " ")))
  11568. (error "Invalid export key"))
  11569. (let* ((binding (key-binding (concat "\C-c\C-x" type)))
  11570. (keepp (equal type " "))
  11571. (file buffer-file-name)
  11572. (buffer (get-buffer-create "*Org Export Visible*"))
  11573. s e)
  11574. (with-current-buffer buffer (erase-buffer))
  11575. (save-excursion
  11576. (setq s (goto-char (point-min)))
  11577. (while (not (= (point) (point-max)))
  11578. (goto-char (org-find-invisible))
  11579. (append-to-buffer buffer s (point))
  11580. (setq s (goto-char (org-find-visible))))
  11581. (goto-char (point-min))
  11582. (unless keepp
  11583. ;; Copy all comment lines to the end, to make sure #+ settings are
  11584. ;; still available for the second export step. Kind of a hack, but
  11585. ;; does do the trick.
  11586. (if (looking-at "#[^\r\n]*")
  11587. (append-to-buffer buffer (match-beginning 0) (1+ (match-end 0))))
  11588. (while (re-search-forward "[\n\r]#[^\n\r]*" nil t)
  11589. (append-to-buffer buffer (1+ (match-beginning 0))
  11590. (min (point-max) (1+ (match-end 0))))))
  11591. (set-buffer buffer)
  11592. (let ((buffer-file-name file)
  11593. (org-inhibit-startup t))
  11594. (org-mode)
  11595. (show-all)
  11596. (unless keepp (funcall binding arg))))
  11597. (if (not keepp)
  11598. (kill-buffer buffer)
  11599. (switch-to-buffer-other-window buffer)
  11600. (goto-char (point-min)))))
  11601. (defun org-find-visible ()
  11602. (if (featurep 'noutline)
  11603. (let ((s (point)))
  11604. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  11605. (get-char-property s 'invisible)))
  11606. s)
  11607. (skip-chars-forward "^\n")
  11608. (point)))
  11609. (defun org-find-invisible ()
  11610. (if (featurep 'noutline)
  11611. (let ((s (point)))
  11612. (while (and (not (= (point-max) (setq s (next-overlay-change s))))
  11613. (not (get-char-property s 'invisible))))
  11614. s)
  11615. (skip-chars-forward "^\r")
  11616. (point)))
  11617. ;; HTML
  11618. (defun org-get-current-options ()
  11619. "Return a string with current options as keyword options.
  11620. Does include HTML export options as well as TODO and CATEGORY stuff."
  11621. (format
  11622. "#+TITLE: %s
  11623. #+AUTHOR: %s
  11624. #+EMAIL: %s
  11625. #+LANGUAGE: %s
  11626. #+TEXT: Some descriptive text to be emitted. Several lines OK.
  11627. #+OPTIONS: H:%d num:%s toc:%s \\n:%s @:%s ::%s |:%s ^:%s *:%s TeX:%s
  11628. #+CATEGORY: %s
  11629. #+SEQ_TODO: %s
  11630. #+TYP_TODO: %s
  11631. #+STARTUP: %s %s %s %s %s %s
  11632. #+TAGS: %s
  11633. #+ARCHIVE: %s
  11634. "
  11635. (buffer-name) (user-full-name) user-mail-address org-export-default-language
  11636. org-export-headline-levels
  11637. org-export-with-section-numbers
  11638. org-export-with-toc
  11639. org-export-preserve-breaks
  11640. org-export-html-expand
  11641. org-export-with-fixed-width
  11642. org-export-with-tables
  11643. org-export-with-sub-superscripts
  11644. org-export-with-emphasize
  11645. org-export-with-TeX-macros
  11646. (file-name-nondirectory buffer-file-name)
  11647. (if (equal org-todo-interpretation 'sequence)
  11648. (mapconcat 'identity org-todo-keywords " ")
  11649. "TODO FEEDBACK VERIFY DONE")
  11650. (if (equal org-todo-interpretation 'type)
  11651. (mapconcat 'identity org-todo-keywords " ")
  11652. "Me Jason Marie DONE")
  11653. (cdr (assoc org-startup-folded
  11654. '((nil . "showall") (t . "overview") (content . "content"))))
  11655. (if org-startup-with-deadline-check "dlcheck" "nodlcheck")
  11656. (if org-odd-levels-only "odd" "oddeven")
  11657. (if org-hide-leading-stars "hidestars" "showstars")
  11658. (if org-startup-align-all-tables "align" "noalign")
  11659. (if org-log-done "logging" "nologging")
  11660. (if org-tag-alist (mapconcat 'car org-tag-alist " ") "")
  11661. org-archive-location
  11662. ))
  11663. (defun org-insert-export-options-template ()
  11664. "Insert into the buffer a template with information for exporting."
  11665. (interactive)
  11666. (if (not (bolp)) (newline))
  11667. (let ((s (org-get-current-options)))
  11668. (and (string-match "#\\+CATEGORY" s)
  11669. (setq s (substring s 0 (match-beginning 0))))
  11670. (insert s)))
  11671. (defun org-toggle-fixed-width-section (arg)
  11672. "Toggle the fixed-width export.
  11673. If there is no active region, the QUOTE keyword at the current headline is
  11674. inserted or removed. When present, it causes the text between this headline
  11675. and the next to be exported as fixed-width text, and unmodified.
  11676. If there is an active region, this command adds or removes a colon as the
  11677. first character of this line. If the first character of a line is a colon,
  11678. this line is also exported in fixed-width font."
  11679. (interactive "P")
  11680. (let* ((cc 0)
  11681. (regionp (org-region-active-p))
  11682. (beg (if regionp (region-beginning) (point)))
  11683. (end (if regionp (region-end)))
  11684. (nlines (or arg (if (and beg end) (count-lines beg end) 1)))
  11685. (re "[ \t]*\\(:\\)")
  11686. off)
  11687. (if regionp
  11688. (save-excursion
  11689. (goto-char beg)
  11690. (setq cc (current-column))
  11691. (beginning-of-line 1)
  11692. (setq off (looking-at re))
  11693. (while (> nlines 0)
  11694. (setq nlines (1- nlines))
  11695. (beginning-of-line 1)
  11696. (cond
  11697. (arg
  11698. (move-to-column cc t)
  11699. (insert ":\n")
  11700. (forward-line -1))
  11701. ((and off (looking-at re))
  11702. (replace-match "" t t nil 1))
  11703. ((not off) (move-to-column cc t) (insert ":")))
  11704. (forward-line 1)))
  11705. (save-excursion
  11706. (org-back-to-heading)
  11707. (if (looking-at (concat outline-regexp
  11708. "\\( +\\<" org-quote-string "\\>\\)"))
  11709. (replace-match "" t t nil 1)
  11710. (if (looking-at outline-regexp)
  11711. (progn
  11712. (goto-char (match-end 0))
  11713. (insert " " org-quote-string))))))))
  11714. (defun org-export-as-html-and-open (arg)
  11715. "Export the outline as HTML and immediately open it with a browser.
  11716. If there is an active region, export only the region.
  11717. The prefix ARG specifies how many levels of the outline should become
  11718. headlines. The default is 3. Lower levels will become bulleted lists."
  11719. (interactive "P")
  11720. (org-export-as-html arg 'hidden)
  11721. (org-open-file buffer-file-name))
  11722. (defun org-export-as-html-batch ()
  11723. "Call `org-export-as-html', may be used in batch processing as
  11724. emacs --batch
  11725. --load=$HOME/lib/emacs/org.el
  11726. --eval \"(setq org-export-headline-levels 2)\"
  11727. --visit=MyFile --funcall org-export-as-html-batch"
  11728. (org-export-as-html org-export-headline-levels 'hidden))
  11729. (defun org-export-as-html (arg &optional hidden ext-plist)
  11730. "Export the outline as a pretty HTML file.
  11731. If there is an active region, export only the region.
  11732. The prefix ARG specifies how many levels of the outline should become
  11733. headlines. The default is 3. Lower levels will become bulleted lists.
  11734. When HIDDEN is non-nil, don't display the HTML buffer.
  11735. EXT-PLIST is a property list with external parameters overriding
  11736. org-mode's default settings, but still inferior to file-local settings."
  11737. (interactive "P")
  11738. (setq-default org-todo-line-regexp org-todo-line-regexp)
  11739. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  11740. (setq-default org-done-string org-done-string)
  11741. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  11742. ext-plist
  11743. (org-infile-export-plist)))
  11744. (style (plist-get opt-plist :style))
  11745. (odd org-odd-levels-only)
  11746. (region-p (org-region-active-p))
  11747. (region
  11748. (buffer-substring
  11749. (if region-p (region-beginning) (point-min))
  11750. (if region-p (region-end) (point-max))))
  11751. (all_lines
  11752. (org-skip-comments (org-split-string
  11753. (org-cleaned-string-for-export region)
  11754. "[\r\n]")))
  11755. (lines (org-export-find-first-heading-line all_lines))
  11756. (level 0) (line "") (origline "") txt todo
  11757. (umax nil)
  11758. (filename (concat (file-name-as-directory
  11759. (org-export-directory :html opt-plist))
  11760. (file-name-sans-extension
  11761. (file-name-nondirectory buffer-file-name))
  11762. ".html"))
  11763. (buffer (find-file-noselect filename))
  11764. (levels-open (make-vector org-level-max nil))
  11765. (date (format-time-string "%Y/%m/%d" (current-time)))
  11766. (time (format-time-string "%X" (org-current-time)))
  11767. (author (plist-get opt-plist :author))
  11768. (title (or (plist-get opt-plist :title)
  11769. (file-name-sans-extension
  11770. (file-name-nondirectory buffer-file-name))))
  11771. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  11772. (quote-re (concat "^\\(\\*+\\)\\([ \t]*" org-quote-string "\\>\\)"))
  11773. (inquote nil)
  11774. (infixed nil)
  11775. (in-local-list nil)
  11776. (local-list-num nil)
  11777. (local-list-indent nil)
  11778. (llt org-plain-list-ordered-item-terminator)
  11779. (email (plist-get opt-plist :email))
  11780. (language (plist-get opt-plist :language))
  11781. (text (plist-get opt-plist :text))
  11782. (lang-words nil)
  11783. (target-alist nil) tg
  11784. (head-count 0) cnt
  11785. (start 0)
  11786. (coding-system (and (fboundp 'coding-system-get)
  11787. (boundp 'buffer-file-coding-system)
  11788. buffer-file-coding-system))
  11789. (coding-system-for-write (or coding-system coding-system-for-write))
  11790. (save-buffer-coding-system (or coding-system save-buffer-coding-system))
  11791. (charset (and coding-system
  11792. (coding-system-get coding-system 'mime-charset)))
  11793. table-open type
  11794. table-buffer table-orig-buffer
  11795. ind start-is-num starter
  11796. rpl path desc descp desc1 desc2 link
  11797. )
  11798. (message "Exporting...")
  11799. (setq org-last-level 1)
  11800. (org-init-section-numbers)
  11801. ;; Get the language-dependent settings
  11802. (setq lang-words (or (assoc language org-export-language-setup)
  11803. (assoc "en" org-export-language-setup)))
  11804. ;; Switch to the output buffer
  11805. (if (or hidden (not org-export-html-show-new-buffer))
  11806. (set-buffer buffer)
  11807. (switch-to-buffer-other-window buffer))
  11808. (erase-buffer)
  11809. (fundamental-mode)
  11810. (let ((case-fold-search nil)
  11811. (org-odd-levels-only odd))
  11812. ;; create local variables for all options, to make sure all called
  11813. ;; functions get the correct information
  11814. (mapcar (lambda (x)
  11815. (set (make-local-variable (cdr x))
  11816. (plist-get opt-plist (car x))))
  11817. org-export-plist-vars)
  11818. (setq umax (if arg (prefix-numeric-value arg)
  11819. org-export-headline-levels))
  11820. ;; File header
  11821. (insert (format
  11822. "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
  11823. \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">
  11824. <html xmlns=\"http://www.w3.org/1999/xhtml\"
  11825. lang=\"%s\" xml:lang=\"%s\">
  11826. <head>
  11827. <title>%s</title>
  11828. <meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>
  11829. <meta name=\"generator\" content=\"Org-mode\"/>
  11830. <meta name=\"generated\" content=\"%s %s\"/>
  11831. <meta name=\"author\" content=\"%s\"/>
  11832. %s
  11833. </head><body>
  11834. "
  11835. language language (org-html-expand title) (or charset "iso-8859-1")
  11836. date time author style))
  11837. (insert (or (plist-get opt-plist :preamble) ""))
  11838. (when (plist-get opt-plist :auto-preamble)
  11839. (if title (insert (concat "<h1 class=\"title\">"
  11840. (org-html-expand title) "</h1>\n")))
  11841. (if text (insert "<p>\n" (org-html-expand text) "</p>")))
  11842. (if org-export-with-toc
  11843. (progn
  11844. (insert (format "<h2>%s</h2>\n" (nth 3 lang-words)))
  11845. (insert "<ul>\n<li>")
  11846. (setq lines
  11847. (mapcar '(lambda (line)
  11848. (if (string-match org-todo-line-regexp line)
  11849. ;; This is a headline
  11850. (progn
  11851. (setq level (- (match-end 1) (match-beginning 1))
  11852. level (org-tr-level level)
  11853. txt (save-match-data
  11854. (org-html-expand
  11855. (org-html-cleanup-toc-line
  11856. (match-string 3 line))))
  11857. todo
  11858. (or (and org-export-mark-todo-in-toc
  11859. (match-beginning 2)
  11860. (not (equal (match-string 2 line)
  11861. org-done-string)))
  11862. ; TODO, not DONE
  11863. (and org-export-mark-todo-in-toc
  11864. (= level umax)
  11865. (org-search-todo-below
  11866. line lines level))))
  11867. (if org-export-with-section-numbers
  11868. (setq txt (concat (org-section-number level)
  11869. " " txt)))
  11870. (if (<= level umax)
  11871. (progn
  11872. (setq head-count (+ head-count 1))
  11873. (if (> level org-last-level)
  11874. (progn
  11875. (setq cnt (- level org-last-level))
  11876. (while (>= (setq cnt (1- cnt)) 0)
  11877. (insert "\n<ul>\n<li>"))
  11878. (insert "\n")))
  11879. (if (< level org-last-level)
  11880. (progn
  11881. (setq cnt (- org-last-level level))
  11882. (while (>= (setq cnt (1- cnt)) 0)
  11883. (insert "</li>\n</ul>"))
  11884. (insert "\n")))
  11885. ;; Check for targets
  11886. (while (string-match org-target-regexp line)
  11887. (setq tg (match-string 1 line)
  11888. line (replace-match
  11889. (concat "@<span class=\"target\">" tg "@</span> ")
  11890. t t line))
  11891. (push (cons (org-solidify-link-text tg)
  11892. (format "sec-%d" head-count))
  11893. target-alist))
  11894. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  11895. (setq txt (replace-match "" t t txt)))
  11896. (insert
  11897. (format
  11898. (if todo
  11899. "</li>\n<li><a href=\"#sec-%d\"><span class=\"todo\">%s</span></a>"
  11900. "</li>\n<li><a href=\"#sec-%d\">%s</a>")
  11901. head-count txt))
  11902. (setq org-last-level level))
  11903. )))
  11904. line)
  11905. lines))
  11906. (while (> org-last-level 0)
  11907. (setq org-last-level (1- org-last-level))
  11908. (insert "</li>\n</ul>\n"))
  11909. ))
  11910. (setq head-count 0)
  11911. (org-init-section-numbers)
  11912. (while (setq line (pop lines) origline line)
  11913. (catch 'nextline
  11914. ;; end of quote section?
  11915. (when (and inquote (string-match "^\\*+" line))
  11916. (insert "</pre>\n")
  11917. (setq inquote nil))
  11918. ;; inside a quote section?
  11919. (when inquote
  11920. (insert (org-html-protect line) "\n")
  11921. (throw 'nextline nil))
  11922. ;; verbatim lines
  11923. (when (and org-export-with-fixed-width
  11924. (string-match "^[ \t]*:\\(.*\\)" line))
  11925. (when (not infixed)
  11926. (setq infixed t)
  11927. (insert "<pre>\n"))
  11928. (insert (org-html-protect (match-string 1 line)) "\n")
  11929. (when (and lines
  11930. (not (string-match "^[ \t]*\\(:.*\\)"
  11931. (car lines))))
  11932. (setq infixed nil)
  11933. (insert "</pre>\n"))
  11934. (throw 'nextline nil))
  11935. ;; make targets to anchors
  11936. (while (string-match "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line)
  11937. (cond
  11938. ((match-end 2)
  11939. (setq line (replace-match
  11940. (concat "@<a name=\""
  11941. (org-solidify-link-text (match-string 1 line))
  11942. "\">\\nbsp@</a>")
  11943. t t line)))
  11944. ((and org-export-with-toc (equal (string-to-char line) ?*))
  11945. (setq line (replace-match
  11946. (concat "@<span class=\"target\">" (match-string 1 line) "@</span> ")
  11947. ; (concat "@<i>" (match-string 1 line) "@</i> ")
  11948. t t line)))
  11949. (t
  11950. (setq line (replace-match
  11951. (concat "@<a name=\""
  11952. (org-solidify-link-text (match-string 1 line))
  11953. "\" class=\"target\">" (match-string 1 line) "@</a> ")
  11954. t t line)))))
  11955. (setq line (org-html-handle-time-stamps line))
  11956. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  11957. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  11958. ;; Also handle sub_superscripts and checkboxes
  11959. (setq line (org-html-expand line))
  11960. ;; Format the links
  11961. (setq start 0)
  11962. (while (string-match org-bracket-link-analytic-regexp line start)
  11963. (setq start (match-beginning 0))
  11964. (setq type (if (match-end 2) (match-string 2 line) "internal"))
  11965. (setq path (match-string 3 line))
  11966. (setq desc1 (if (match-end 5) (match-string 5 line))
  11967. desc2 (if (match-end 2) (concat type ":" path) path)
  11968. descp (and desc1 (not (equal desc1 desc2)))
  11969. desc (or desc1 desc2))
  11970. ;; FIXME: do we need to unescape here somewhere?
  11971. (cond
  11972. ((equal type "internal")
  11973. (setq rpl
  11974. (concat
  11975. "<a href=\"#"
  11976. (org-solidify-link-text path target-alist)
  11977. "\">" desc "</a>")))
  11978. ((member type '("http" "https" "ftp" "mailto" "news"))
  11979. ;; standard URL
  11980. (setq link (concat type ":" path))
  11981. (setq rpl (concat "<a href=\"" link "\">" desc "</a>")))
  11982. ((string= type "file")
  11983. ;; FILE link
  11984. (let* ((filename path)
  11985. (abs-p (file-name-absolute-p filename))
  11986. thefile file-is-image-p search)
  11987. (save-match-data
  11988. (if (string-match "::\\(.*\\)" filename)
  11989. (setq search (match-string 1 filename)
  11990. filename (replace-match "" t nil filename)))
  11991. (setq file-is-image-p
  11992. (string-match (org-image-file-name-regexp) filename))
  11993. (setq thefile (if abs-p (expand-file-name filename) filename))
  11994. (when (and org-export-html-link-org-files-as-html
  11995. (string-match "\\.org$" thefile))
  11996. (setq thefile (concat (substring thefile 0
  11997. (match-beginning 0))
  11998. ".html"))
  11999. (if (and search
  12000. ;; make sure this is can be used as target search
  12001. (not (string-match "^[0-9]*$" search))
  12002. (not (string-match "^\\*" search))
  12003. (not (string-match "^/.*/$" search)))
  12004. (setq thefile (concat thefile "#"
  12005. (org-solidify-link-text
  12006. (org-link-unescape search)))))
  12007. (when (string-match "^file:" desc)
  12008. (setq desc (replace-match "" t t desc))
  12009. (if (string-match "\\.org$" desc)
  12010. (setq desc (replace-match "" t t desc))))))
  12011. (setq rpl (if (and file-is-image-p
  12012. (or (eq t org-export-html-inline-images)
  12013. (and org-export-html-inline-images
  12014. (not descp))))
  12015. (concat "<img src=\"" thefile "\"/>")
  12016. (concat "<a href=\"" thefile "\">" desc "</a>")))))
  12017. ((member type '("bbdb" "vm" "wl" "mhe" "rmail" "gnus" "shell" "info" "elisp"))
  12018. (setq rpl (concat "<i>&lt;" type ":"
  12019. (save-match-data (org-link-unescape path))
  12020. "&gt;</i>"))))
  12021. (setq line (replace-match rpl t t line)
  12022. start (+ start (length rpl))))
  12023. ;; TODO items
  12024. (if (and (string-match org-todo-line-regexp line)
  12025. (match-beginning 2))
  12026. (if (equal (match-string 2 line) org-done-string)
  12027. (setq line (replace-match
  12028. "<span class=\"done\">\\2</span>"
  12029. t nil line 2))
  12030. (setq line (replace-match "<span class=\"todo\">\\2</span>"
  12031. t nil line 2))))
  12032. (cond
  12033. ((string-match "^\\(\\*+\\)[ \t]*\\(.*\\)" line)
  12034. ;; This is a headline
  12035. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)))
  12036. txt (match-string 2 line))
  12037. (if (string-match quote-re0 txt)
  12038. (setq txt (replace-match "" t t txt)))
  12039. (if (<= level umax) (setq head-count (+ head-count 1)))
  12040. (when in-local-list
  12041. ;; Close any local lists before inserting a new header line
  12042. (while local-list-num
  12043. (org-close-li)
  12044. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  12045. (pop local-list-num))
  12046. (setq local-list-indent nil
  12047. in-local-list nil))
  12048. (org-html-level-start level txt umax
  12049. (and org-export-with-toc (<= level umax))
  12050. head-count)
  12051. ;; QUOTES
  12052. (when (string-match quote-re line)
  12053. (insert "<pre>")
  12054. (setq inquote t)))
  12055. ((and org-export-with-tables
  12056. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  12057. (if (not table-open)
  12058. ;; New table starts
  12059. (setq table-open t table-buffer nil table-orig-buffer nil))
  12060. ;; Accumulate lines
  12061. (setq table-buffer (cons line table-buffer)
  12062. table-orig-buffer (cons origline table-orig-buffer))
  12063. (when (or (not lines)
  12064. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  12065. (car lines))))
  12066. (setq table-open nil
  12067. table-buffer (nreverse table-buffer)
  12068. table-orig-buffer (nreverse table-orig-buffer))
  12069. (org-close-par-maybe)
  12070. (insert (org-format-table-html table-buffer table-orig-buffer))))
  12071. (t
  12072. ;; Normal lines
  12073. (when (string-match
  12074. (cond
  12075. ((eq llt t) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+[.)]\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  12076. ((= llt ?.) "^\\([ \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+\\.\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  12077. ((= llt ?\)) "^\\( \t]*\\)\\(\\([-+*]\\)\\|\\([0-9]+)\\) \\)?\\( *[^ \t\n\r]\\|[ \t]*$\\)")
  12078. (t (error "Invalid value of `org-plain-list-ordered-item-terminator'")))
  12079. line)
  12080. (setq ind (org-get-string-indentation line)
  12081. start-is-num (match-beginning 4)
  12082. starter (if (match-beginning 2)
  12083. (substring (match-string 2 line) 0 -1))
  12084. line (substring line (match-beginning 5)))
  12085. (unless (string-match "[^ \t]" line)
  12086. ;; empty line. Pretend indentation is large.
  12087. (setq ind (1+ (or (car local-list-indent) 1))))
  12088. (while (and in-local-list
  12089. (or (and (= ind (car local-list-indent))
  12090. (not starter))
  12091. (< ind (car local-list-indent))))
  12092. (org-close-li)
  12093. (insert (if (car local-list-num) "</ol>\n" "</ul>"))
  12094. (pop local-list-num) (pop local-list-indent)
  12095. (setq in-local-list local-list-indent))
  12096. (cond
  12097. ((and starter
  12098. (or (not in-local-list)
  12099. (> ind (car local-list-indent))))
  12100. ;; Start new (level of ) list
  12101. (org-close-par-maybe)
  12102. (insert (if start-is-num "<ol>\n<li>\n" "<ul>\n<li>\n"))
  12103. (push start-is-num local-list-num)
  12104. (push ind local-list-indent)
  12105. (setq in-local-list t))
  12106. (starter
  12107. ;; continue current list
  12108. (org-close-li)
  12109. (insert "<li>\n")))
  12110. (if (string-match "^[ \t]*\\[\\([X ]\\)\\]" line)
  12111. (setq line
  12112. (replace-match
  12113. (if (equal (match-string 1 line) "X")
  12114. "<b>[X]</b>"
  12115. "<b>[<span style=\"visibility:hidden;\">X</span>]</b>")
  12116. t t line))))
  12117. ;; Empty lines start a new paragraph. If hand-formatted lists
  12118. ;; are not fully interpreted, lines starting with "-", "+", "*"
  12119. ;; also start a new paragraph.
  12120. (if (string-match "^ [-+*]-\\|^[ \t]*$" line) (org-open-par))
  12121. ;; Check if the line break needs to be conserved
  12122. (cond
  12123. ((string-match "\\\\\\\\[ \t]*$" line)
  12124. (setq line (replace-match "<br/>" t t line)))
  12125. (org-export-preserve-breaks
  12126. (setq line (concat line "<br/>"))))
  12127. (insert line "\n")))))
  12128. ;; Properly close all local lists and other lists
  12129. (when inquote (insert "</pre>\n"))
  12130. (when in-local-list
  12131. ;; Close any local lists before inserting a new header line
  12132. (while local-list-num
  12133. (org-close-li)
  12134. (insert (if (car local-list-num) "</ol>\n" "</ul>\n"))
  12135. (pop local-list-num))
  12136. (setq local-list-indent nil
  12137. in-local-list nil))
  12138. (org-html-level-start 1 nil umax
  12139. (and org-export-with-toc (<= level umax))
  12140. head-count)
  12141. (when (plist-get opt-plist :auto-postamble)
  12142. (when author
  12143. (insert "<p class=\"author\"> "
  12144. (nth 1 lang-words) ": " author "\n")
  12145. (when email
  12146. (insert "<a href=\"mailto:" email "\">&lt;"
  12147. email "&gt;</a>\n"))
  12148. (insert "</p>\n"))
  12149. (when (and date time)
  12150. (insert "<p class=\"date\"> "
  12151. (nth 2 lang-words) ": "
  12152. date " " time "</p>\n")))
  12153. (if org-export-html-with-timestamp
  12154. (insert org-export-html-html-helper-timestamp))
  12155. (insert (or (plist-get opt-plist :postamble) ""))
  12156. (insert "</body>\n</html>\n")
  12157. (normal-mode)
  12158. ;; remove empty paragraphs and lists
  12159. (goto-char (point-min))
  12160. (while (re-search-forward "<p>[ \r\n\t]*</p>" nil t)
  12161. (replace-match ""))
  12162. (goto-char (point-min))
  12163. (while (re-search-forward "<li>[ \r\n\t]*</li>\n?" nil t)
  12164. (replace-match ""))
  12165. (save-buffer)
  12166. (goto-char (point-min)))))
  12167. (defun org-format-table-html (lines olines)
  12168. "Find out which HTML converter to use and return the HTML code."
  12169. (if (string-match "^[ \t]*|" (car lines))
  12170. ;; A normal org table
  12171. (org-format-org-table-html lines)
  12172. ;; Table made by table.el - test for spanning
  12173. (let* ((hlines (delq nil (mapcar
  12174. (lambda (x)
  12175. (if (string-match "^[ \t]*\\+-" x) x
  12176. nil))
  12177. lines)))
  12178. (first (car hlines))
  12179. (ll (and (string-match "\\S-+" first)
  12180. (match-string 0 first)))
  12181. (re (concat "^[ \t]*" (regexp-quote ll)))
  12182. (spanning (delq nil (mapcar (lambda (x) (not (string-match re x)))
  12183. hlines))))
  12184. (if (and (not spanning)
  12185. (not org-export-prefer-native-exporter-for-tables))
  12186. ;; We can use my own converter with HTML conversions
  12187. (org-format-table-table-html lines)
  12188. ;; Need to use the code generator in table.el, with the original text.
  12189. (org-format-table-table-html-using-table-generate-source olines)))))
  12190. (defun org-format-org-table-html (lines)
  12191. "Format a table into HTML."
  12192. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  12193. (setq lines (nreverse lines))
  12194. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  12195. (setq lines (nreverse lines))
  12196. (when org-export-table-remove-special-lines
  12197. ;; Check if the table has a marking column. If yes remove the
  12198. ;; column and the special lines
  12199. (let* ((special
  12200. (not
  12201. (memq nil
  12202. (mapcar
  12203. (lambda (x)
  12204. (or (string-match "^[ \t]*|-" x)
  12205. (string-match "^[ \t]*| *\\([#!$*_^ ]\\) *|" x)))
  12206. lines)))))
  12207. (if special
  12208. (setq lines
  12209. (delq nil
  12210. (mapcar
  12211. (lambda (x)
  12212. (if (string-match "^[ \t]*| *[!_^] *|" x)
  12213. nil ; ignore this line
  12214. (and (or (string-match "^[ \t]*|-+\\+" x)
  12215. (string-match "^[ \t]*|[^|]*|" x))
  12216. (replace-match "|" t t x))))
  12217. lines))))))
  12218. (let ((head (and org-export-highlight-first-table-line
  12219. (delq nil (mapcar
  12220. (lambda (x) (string-match "^[ \t]*|-" x))
  12221. (cdr lines)))))
  12222. line fields html)
  12223. (setq html (concat org-export-html-table-tag "\n"))
  12224. (while (setq line (pop lines))
  12225. (catch 'next-line
  12226. (if (string-match "^[ \t]*|-" line)
  12227. (progn
  12228. (setq head nil) ;; head ends here, first time around
  12229. ;; ignore this line
  12230. (throw 'next-line t)))
  12231. ;; Break the line into fields
  12232. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  12233. (setq html (concat
  12234. html
  12235. "<tr>"
  12236. (mapconcat (lambda (x)
  12237. (if head
  12238. (concat "<th>" x "</th>")
  12239. (concat "<td>" x "</td>")))
  12240. fields "")
  12241. "</tr>\n"))))
  12242. (setq html (concat html "</table>\n"))
  12243. html))
  12244. (defun org-fake-empty-table-line (line)
  12245. "Replace everything except \"|\" with spaces."
  12246. (let ((i (length line))
  12247. (newstr (copy-sequence line)))
  12248. (while (> i 0)
  12249. (setq i (1- i))
  12250. (if (not (eq (aref newstr i) ?|))
  12251. (aset newstr i ?\ )))
  12252. newstr))
  12253. (defun org-format-table-table-html (lines)
  12254. "Format a table generated by table.el into HTML.
  12255. This conversion does *not* use `table-generate-source' from table.el.
  12256. This has the advantage that Org-mode's HTML conversions can be used.
  12257. But it has the disadvantage, that no cell- or row-spanning is allowed."
  12258. (let (line field-buffer
  12259. (head org-export-highlight-first-table-line)
  12260. fields html empty)
  12261. (setq html (concat org-export-html-table-tag "\n"))
  12262. (while (setq line (pop lines))
  12263. (setq empty "&nbsp;")
  12264. (catch 'next-line
  12265. (if (string-match "^[ \t]*\\+-" line)
  12266. (progn
  12267. (if field-buffer
  12268. (progn
  12269. (setq html (concat
  12270. html
  12271. "<tr>"
  12272. (mapconcat
  12273. (lambda (x)
  12274. (if (equal x "") (setq x empty))
  12275. (if head
  12276. (concat "<th>" x "</th>\n")
  12277. (concat "<td>" x "</td>\n")))
  12278. field-buffer "\n")
  12279. "</tr>\n"))
  12280. (setq head nil)
  12281. (setq field-buffer nil)))
  12282. ;; Ignore this line
  12283. (throw 'next-line t)))
  12284. ;; Break the line into fields and store the fields
  12285. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  12286. (if field-buffer
  12287. (setq field-buffer (mapcar
  12288. (lambda (x)
  12289. (concat x "<br/>" (pop fields)))
  12290. field-buffer))
  12291. (setq field-buffer fields))))
  12292. (setq html (concat html "</table>\n"))
  12293. html))
  12294. (defun org-format-table-table-html-using-table-generate-source (lines)
  12295. "Format a table into html, using `table-generate-source' from table.el.
  12296. This has the advantage that cell- or row-spanning is allowed.
  12297. But it has the disadvantage, that Org-mode's HTML conversions cannot be used."
  12298. (require 'table)
  12299. (with-current-buffer (get-buffer-create " org-tmp1 ")
  12300. (erase-buffer)
  12301. (insert (mapconcat 'identity lines "\n"))
  12302. (goto-char (point-min))
  12303. (if (not (re-search-forward "|[^+]" nil t))
  12304. (error "Error processing table"))
  12305. (table-recognize-table)
  12306. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  12307. (table-generate-source 'html " org-tmp2 ")
  12308. (set-buffer " org-tmp2 ")
  12309. (buffer-substring (point-min) (point-max))))
  12310. (defun org-html-handle-time-stamps (s)
  12311. "Format time stamps in string S, or remove them."
  12312. (let (r b)
  12313. (while (string-match org-maybe-keyword-time-regexp s)
  12314. (or b (setq b (substring s 0 (match-beginning 0))))
  12315. (if (not org-export-with-timestamps)
  12316. (setq r (concat r (substring s 0 (match-beginning 0)))
  12317. s (substring s (match-end 0)))
  12318. (setq r (concat
  12319. r (substring s 0 (match-beginning 0))
  12320. (if (match-end 1)
  12321. (format "@<span class=\"timestamp-kwd\">%s @</span>"
  12322. (match-string 1 s)))
  12323. (format " @<span class=\"timestamp\">%s@</span>"
  12324. (substring (match-string 3 s) 1 -1)))
  12325. s (substring s (match-end 0)))))
  12326. ;; Line break of line started and ended with time stamp stuff
  12327. (if (not r)
  12328. s
  12329. (setq r (concat r s))
  12330. (unless (string-match "\\S-" (concat b s))
  12331. (setq r (concat r "@<br/>")))
  12332. r)))
  12333. (defun org-html-protect (s)
  12334. ;; convert & to &amp;, < to &lt; and > to &gt;
  12335. (let ((start 0))
  12336. (while (string-match "&" s start)
  12337. (setq s (replace-match "&amp;" t t s)
  12338. start (1+ (match-beginning 0))))
  12339. (while (string-match "<" s)
  12340. (setq s (replace-match "&lt;" t t s)))
  12341. (while (string-match ">" s)
  12342. (setq s (replace-match "&gt;" t t s))))
  12343. s)
  12344. (defun org-html-cleanup-toc-line (s)
  12345. "Remove tags and time staps from lines going into the toc."
  12346. (if (string-match " +:[a-zA-Z0-9_@:]+: *$" s)
  12347. (setq s (replace-match "" t t s)))
  12348. (while (string-match org-maybe-keyword-time-regexp s)
  12349. (setq s (replace-match "" t t s)))
  12350. s)
  12351. (defun org-html-expand (string)
  12352. "Prepare STRING for HTML export. Applies all active conversions.
  12353. If there are links in the string, don't modify these."
  12354. (let* (m s l res)
  12355. (while (setq m (string-match org-bracket-link-regexp string))
  12356. (setq s (substring string 0 m)
  12357. l (match-string 0 string)
  12358. string (substring string (match-end 0)))
  12359. (push (org-html-do-expand s) res)
  12360. (push l res))
  12361. (push (org-html-do-expand string) res)
  12362. (apply 'concat (nreverse res))))
  12363. (defun org-html-do-expand (s)
  12364. "Apply all active conversions to translate special ASCII to HTML."
  12365. (setq s (org-html-protect s))
  12366. (if org-export-html-expand
  12367. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  12368. (setq s (replace-match "<\\1>" t nil s))))
  12369. (if org-export-with-emphasize
  12370. (setq s (org-export-html-convert-emphasize s)))
  12371. (if org-export-with-sub-superscripts
  12372. (setq s (org-export-html-convert-sub-super s)))
  12373. (if org-export-with-TeX-macros
  12374. (let ((start 0) wd ass)
  12375. (while (setq start (string-match "\\\\\\([a-zA-Z]+\\)" s start))
  12376. (setq wd (match-string 1 s))
  12377. (if (setq ass (assoc wd org-html-entities))
  12378. (setq s (replace-match (or (cdr ass)
  12379. (concat "&" (car ass) ";"))
  12380. t t s))
  12381. (setq start (+ start (length wd)))))))
  12382. s)
  12383. (defun org-create-multibrace-regexp (left right n)
  12384. "Create a regular expression which will match a balanced sexp.
  12385. Opening delimiter is LEFT, and closing delimiter is RIGHT, both given
  12386. as single character strings.
  12387. The regexp returned will match the entire expression including the
  12388. delimiters. It will also define a single group which contains the
  12389. match except for the outermost delimiters. The maximum depth of
  12390. stacked delimiters is N. Escaping delimiters is not possible."
  12391. (let* ((nothing (concat "[^" "\\" left "\\" right "]*?"))
  12392. (or "\\|")
  12393. (re nothing)
  12394. (next (concat "\\(?:" nothing left nothing right "\\)+" nothing)))
  12395. (while (> n 1)
  12396. (setq n (1- n)
  12397. re (concat re or next)
  12398. next (concat "\\(?:" nothing left next right "\\)+" nothing)))
  12399. (concat left "\\(" re "\\)" right)))
  12400. (defvar org-match-substring-regexp
  12401. (concat
  12402. "\\([^\\]\\)\\([_^]\\)\\("
  12403. "\\(" (org-create-multibrace-regexp "{" "}" org-match-sexp-depth) "\\)"
  12404. "\\|"
  12405. "\\(" (org-create-multibrace-regexp "(" ")" org-match-sexp-depth) "\\)"
  12406. "\\|"
  12407. "\\(\\(?:\\*\\|[-+]?[^-+*!@#$%^_ \t\r\n,:\"?<>~;./{}=()]+\\)\\)\\)")
  12408. "The regular expression matching a sub- or superscript.")
  12409. (defun org-export-html-convert-sub-super (string)
  12410. "Convert sub- and superscripts in STRING to HTML."
  12411. (let (key c)
  12412. (while (string-match org-match-substring-regexp string)
  12413. (setq key (if (string= (match-string 2 string) "_") "sub" "sup"))
  12414. (setq c (or (match-string 8 string)
  12415. (match-string 6 string)
  12416. (match-string 5 string)))
  12417. (setq string (replace-match
  12418. (concat (match-string 1 string)
  12419. "<" key ">" c "</" key ">")
  12420. t t string)))
  12421. (while (string-match "\\\\\\([_^]\\)" string)
  12422. (setq string (replace-match (match-string 1 string) t t string))))
  12423. string)
  12424. (defun org-export-html-convert-emphasize (string)
  12425. (while (string-match org-italic-re string)
  12426. (setq string (replace-match "\\1<i>\\3</i>\\4" t nil string)))
  12427. (while (string-match org-bold-re string)
  12428. (setq string (replace-match "\\1<b>\\3</b>\\4" t nil string)))
  12429. (while (string-match org-underline-re string)
  12430. (setq string (replace-match "\\1<u>\\3</u>\\4" t nil string)))
  12431. string)
  12432. (defvar org-par-open nil)
  12433. (defun org-open-par ()
  12434. "Insert <p>, but first close previous paragraph if any."
  12435. (org-close-par-maybe)
  12436. (insert "\n<p>")
  12437. (setq org-par-open t))
  12438. (defun org-close-par-maybe ()
  12439. "Close paragraph if there is one open."
  12440. (when org-par-open
  12441. (insert "</p>")
  12442. (setq org-par-open nil)))
  12443. (defun org-close-li ()
  12444. "Close <li> if necessary."
  12445. (org-close-par-maybe)
  12446. (insert "</li>\n"))
  12447. ; (when (save-excursion
  12448. ; (re-search-backward "</?\\(ul\\|ol\\|li\\|[hH][0-9]\\)>" nil t))
  12449. ; (if (member (match-string 0) '("</ul>" "</ol>" "<li>"))
  12450. ; (insert "</li>"))))
  12451. (defun org-html-level-start (level title umax with-toc head-count)
  12452. "Insert a new level in HTML export.
  12453. When TITLE is nil, just close all open levels."
  12454. (org-close-par-maybe)
  12455. (let ((l (1+ (max level umax))))
  12456. (while (<= l org-level-max)
  12457. (if (aref levels-open (1- l))
  12458. (progn
  12459. (org-html-level-close l)
  12460. (aset levels-open (1- l) nil)))
  12461. (setq l (1+ l)))
  12462. (when title
  12463. ;; If title is nil, this means this function is called to close
  12464. ;; all levels, so the rest is done only if title is given
  12465. (when (string-match "\\(:[a-zA-Z0-9_@:]+:\\)[ \t]*$" title)
  12466. (setq title (replace-match
  12467. (if org-export-with-tags
  12468. (save-match-data
  12469. (concat
  12470. "&nbsp;&nbsp;&nbsp;<span class=\"tag\">"
  12471. (mapconcat 'identity (org-split-string
  12472. (match-string 1 title) ":")
  12473. "&nbsp;")
  12474. "</span>"))
  12475. "")
  12476. t t title)))
  12477. (if (> level umax)
  12478. (progn
  12479. (if (aref levels-open (1- level))
  12480. (progn
  12481. (org-close-li)
  12482. (insert "<li>" title "<br/>\n"))
  12483. (aset levels-open (1- level) t)
  12484. (org-close-par-maybe)
  12485. (insert "<ul>\n<li>" title "<br/>\n")))
  12486. (if org-export-with-section-numbers
  12487. (setq title (concat (org-section-number level) " " title)))
  12488. (setq level (+ level 1))
  12489. (if with-toc
  12490. (insert (format "\n<h%d><a name=\"sec-%d\">%s</a></h%d>\n"
  12491. level head-count title level))
  12492. (insert (format "\n<h%d>%s</h%d>\n" level title level)))
  12493. (org-open-par)))))
  12494. (defun org-html-level-close (&rest args)
  12495. "Terminate one level in HTML export."
  12496. (org-close-li)
  12497. (insert "</ul>"))
  12498. ;; Variable holding the vector with section numbers
  12499. (defvar org-section-numbers (make-vector org-level-max 0))
  12500. (defun org-init-section-numbers ()
  12501. "Initialize the vector for the section numbers."
  12502. (let* ((level -1)
  12503. (numbers (nreverse (org-split-string "" "\\.")))
  12504. (depth (1- (length org-section-numbers)))
  12505. (i depth) number-string)
  12506. (while (>= i 0)
  12507. (if (> i level)
  12508. (aset org-section-numbers i 0)
  12509. (setq number-string (or (car numbers) "0"))
  12510. (if (string-match "\\`[A-Z]\\'" number-string)
  12511. (aset org-section-numbers i
  12512. (- (string-to-char number-string) ?A -1))
  12513. (aset org-section-numbers i (string-to-number number-string)))
  12514. (pop numbers))
  12515. (setq i (1- i)))))
  12516. (defun org-section-number (&optional level)
  12517. "Return a string with the current section number.
  12518. When LEVEL is non-nil, increase section numbers on that level."
  12519. (let* ((depth (1- (length org-section-numbers))) idx n (string ""))
  12520. (when level
  12521. (when (> level -1)
  12522. (aset org-section-numbers
  12523. level (1+ (aref org-section-numbers level))))
  12524. (setq idx (1+ level))
  12525. (while (<= idx depth)
  12526. (if (not (= idx 1))
  12527. (aset org-section-numbers idx 0))
  12528. (setq idx (1+ idx))))
  12529. (setq idx 0)
  12530. (while (<= idx depth)
  12531. (setq n (aref org-section-numbers idx))
  12532. (setq string (concat string (if (not (string= string "")) "." "")
  12533. (int-to-string n)))
  12534. (setq idx (1+ idx)))
  12535. (save-match-data
  12536. (if (string-match "\\`\\([@0]\\.\\)+" string)
  12537. (setq string (replace-match "" t nil string)))
  12538. (if (string-match "\\(\\.0\\)+\\'" string)
  12539. (setq string (replace-match "" t nil string))))
  12540. string))
  12541. (defun org-export-icalendar-this-file ()
  12542. "Export current file as an iCalendar file.
  12543. The iCalendar file will be located in the same directory as the Org-mode
  12544. file, but with extension `.ics'."
  12545. (interactive)
  12546. (org-export-icalendar nil buffer-file-name))
  12547. (defun org-export-as-xoxo-insert-into (buffer &rest output)
  12548. (with-current-buffer buffer
  12549. (apply 'insert output)))
  12550. (defun org-export-as-xoxo (&optional buffer)
  12551. "Export the org buffer as XOXO.
  12552. The XOXO buffer is named *xoxo-<source buffer name>*"
  12553. (interactive (list (current-buffer)))
  12554. ;; A quickie abstraction
  12555. ;; Output everything as XOXO
  12556. (with-current-buffer (get-buffer buffer)
  12557. (goto-char (point-min)) ;; CD: beginning-of-buffer is not allowed.
  12558. (let* ((opt-plist (org-combine-plists (org-default-export-plist)
  12559. (org-infile-export-plist)))
  12560. (filename (concat (file-name-as-directory
  12561. (org-export-directory :xoxo opt-plist))
  12562. (file-name-sans-extension
  12563. (file-name-nondirectory buffer-file-name))
  12564. ".html"))
  12565. (out (find-file-noselect filename))
  12566. (last-level 1)
  12567. (hanging-li nil))
  12568. ;; Check the output buffer is empty.
  12569. (with-current-buffer out (erase-buffer))
  12570. ;; Kick off the output
  12571. (org-export-as-xoxo-insert-into out "<ol class='xoxo'>\n")
  12572. (while (re-search-forward "^\\(\\*+\\) \\(.+\\)" (point-max) 't)
  12573. (let* ((hd (match-string-no-properties 1))
  12574. (level (length hd))
  12575. (text (concat
  12576. (match-string-no-properties 2)
  12577. (save-excursion
  12578. (goto-char (match-end 0))
  12579. (let ((str ""))
  12580. (catch 'loop
  12581. (while 't
  12582. (forward-line)
  12583. (if (looking-at "^[ \t]\\(.*\\)")
  12584. (setq str (concat str (match-string-no-properties 1)))
  12585. (throw 'loop str)))))))))
  12586. ;; Handle level rendering
  12587. (cond
  12588. ((> level last-level)
  12589. (org-export-as-xoxo-insert-into out "\n<ol>\n"))
  12590. ((< level last-level)
  12591. (dotimes (- (- last-level level) 1)
  12592. (if hanging-li
  12593. (org-export-as-xoxo-insert-into out "</li>\n"))
  12594. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12595. (when hanging-li
  12596. (org-export-as-xoxo-insert-into out "</li>\n")
  12597. (setq hanging-li nil)))
  12598. ((equal level last-level)
  12599. (if hanging-li
  12600. (org-export-as-xoxo-insert-into out "</li>\n")))
  12601. )
  12602. (setq last-level level)
  12603. ;; And output the new li
  12604. (setq hanging-li 't)
  12605. (if (equal ?+ (elt text 0))
  12606. (org-export-as-xoxo-insert-into out "<li class='" (substring text 1) "'>")
  12607. (org-export-as-xoxo-insert-into out "<li>" text))))
  12608. ;; Finally finish off the ol
  12609. (dotimes (- last-level 1)
  12610. (if hanging-li
  12611. (org-export-as-xoxo-insert-into out "</li>\n"))
  12612. (org-export-as-xoxo-insert-into out "</ol>\n"))
  12613. ;; Finish the buffer off and clean it up.
  12614. (switch-to-buffer-other-window out)
  12615. (indent-region (point-min) (point-max) nil)
  12616. (save-buffer)
  12617. (goto-char (point-min))
  12618. )))
  12619. ;;;###autoload
  12620. (defun org-export-icalendar-all-agenda-files ()
  12621. "Export all files in `org-agenda-files' to iCalendar .ics files.
  12622. Each iCalendar file will be located in the same directory as the Org-mode
  12623. file, but with extension `.ics'."
  12624. (interactive)
  12625. (apply 'org-export-icalendar nil (org-agenda-files t)))
  12626. ;;;###autoload
  12627. (defun org-export-icalendar-combine-agenda-files ()
  12628. "Export all files in `org-agenda-files' to a single combined iCalendar file.
  12629. The file is stored under the name `org-combined-agenda-icalendar-file'."
  12630. (interactive)
  12631. (apply 'org-export-icalendar t (org-agenda-files t)))
  12632. (defun org-export-icalendar (combine &rest files)
  12633. "Create iCalendar files for all elements of FILES.
  12634. If COMBINE is non-nil, combine all calendar entries into a single large
  12635. file and store it under the name `org-combined-agenda-icalendar-file'."
  12636. (save-excursion
  12637. (let* ((dir (org-export-directory
  12638. :ical (list :publishing-directory
  12639. org-export-publishing-directory)))
  12640. file ical-file ical-buffer category started org-agenda-new-buffers)
  12641. (when combine
  12642. (setq ical-file
  12643. (if (file-name-absolute-p org-combined-agenda-icalendar-file)
  12644. org-combined-agenda-icalendar-file
  12645. (expand-file-name org-combined-agenda-icalendar-file dir))
  12646. ical-buffer (org-get-agenda-file-buffer ical-file))
  12647. (set-buffer ical-buffer) (erase-buffer))
  12648. (while (setq file (pop files))
  12649. (catch 'nextfile
  12650. (org-check-agenda-file file)
  12651. (set-buffer (org-get-agenda-file-buffer file))
  12652. (unless combine
  12653. (setq ical-file (concat (file-name-as-directory dir)
  12654. (file-name-sans-extension
  12655. (file-name-nondirectory buffer-file-name))
  12656. ".ics"))
  12657. (setq ical-buffer (org-get-agenda-file-buffer ical-file))
  12658. (with-current-buffer ical-buffer (erase-buffer)))
  12659. (setq category (or org-category
  12660. (file-name-sans-extension
  12661. (file-name-nondirectory buffer-file-name))))
  12662. (if (symbolp category) (setq category (symbol-name category)))
  12663. (let ((standard-output ical-buffer))
  12664. (if combine
  12665. (and (not started) (setq started t)
  12666. (org-start-icalendar-file org-icalendar-combined-name))
  12667. (org-start-icalendar-file category))
  12668. (org-print-icalendar-entries combine category)
  12669. (when (or (and combine (not files)) (not combine))
  12670. (org-finish-icalendar-file)
  12671. (set-buffer ical-buffer)
  12672. (save-buffer)
  12673. (run-hooks 'org-after-save-iCalendar-file-hook)))))
  12674. (org-release-buffers org-agenda-new-buffers))))
  12675. (defvar org-after-save-iCalendar-file-hook nil
  12676. "Hook run after an iCalendar file has been saved.
  12677. The iCalendar buffer is still current when this hook is run.
  12678. A good way to use this is to tell a desktop calenndar application to re-read
  12679. the iCalendar file.")
  12680. (defun org-print-icalendar-entries (&optional combine category)
  12681. "Print iCalendar entries for the current Org-mode file to `standard-output'.
  12682. When COMBINE is non nil, add the category to each line."
  12683. (let ((re2 (concat "--?-?\\(" org-ts-regexp "\\)"))
  12684. (dts (org-ical-ts-to-string
  12685. (format-time-string (cdr org-time-stamp-formats) (current-time))
  12686. "DTSTART"))
  12687. hd ts ts2 state (inc t) pos scheduledp deadlinep tmp pri)
  12688. (save-excursion
  12689. (goto-char (point-min))
  12690. (while (re-search-forward org-ts-regexp nil t)
  12691. (setq pos (match-beginning 0)
  12692. ts (match-string 0)
  12693. inc t
  12694. hd (org-get-heading))
  12695. (if (looking-at re2)
  12696. (progn
  12697. (goto-char (match-end 0))
  12698. (setq ts2 (match-string 1) inc nil))
  12699. (setq ts2 ts
  12700. tmp (buffer-substring (max (point-min)
  12701. (- pos org-ds-keyword-length))
  12702. pos)
  12703. deadlinep (string-match org-deadline-regexp tmp)
  12704. scheduledp (string-match org-scheduled-regexp tmp)
  12705. ;; donep (org-entry-is-done-p)
  12706. ))
  12707. (if (or (string-match org-tr-regexp hd)
  12708. (string-match org-ts-regexp hd))
  12709. (setq hd (replace-match "" t t hd)))
  12710. (if combine
  12711. (setq hd (concat hd " (category " category ")")))
  12712. (if deadlinep (setq hd (concat "DL: " hd " This is a deadline")))
  12713. (if scheduledp (setq hd (concat "S: " hd " Scheduled for this date")))
  12714. (princ (format "BEGIN:VEVENT
  12715. %s
  12716. %s
  12717. SUMMARY:%s
  12718. END:VEVENT\n"
  12719. (org-ical-ts-to-string ts "DTSTART")
  12720. (org-ical-ts-to-string ts2 "DTEND" inc)
  12721. hd)))
  12722. (when org-icalendar-include-todo
  12723. (goto-char (point-min))
  12724. (while (re-search-forward org-todo-line-regexp nil t)
  12725. (setq state (match-string 1))
  12726. (unless (equal state org-done-string)
  12727. (setq hd (match-string 3))
  12728. (if (string-match org-priority-regexp hd)
  12729. (setq pri (string-to-char (match-string 2 hd))
  12730. hd (concat (substring hd 0 (match-beginning 1))
  12731. (substring hd (- (match-end 1)))))
  12732. (setq pri org-default-priority))
  12733. (setq pri (floor (1+ (* 8. (/ (float (- org-lowest-priority pri))
  12734. (- org-lowest-priority ?A))))))
  12735. (princ (format "BEGIN:VTODO
  12736. %s
  12737. SUMMARY:%s
  12738. SEQUENCE:1
  12739. PRIORITY:%d
  12740. END:VTODO\n"
  12741. dts hd pri))))))))
  12742. (defun org-start-icalendar-file (name)
  12743. "Start an iCalendar file by inserting the header."
  12744. (let ((user user-full-name)
  12745. (name (or name "unknown"))
  12746. (timezone (cadr (current-time-zone))))
  12747. (princ
  12748. (format "BEGIN:VCALENDAR
  12749. VERSION:2.0
  12750. X-WR-CALNAME:%s
  12751. PRODID:-//%s//Emacs with Org-mode//EN
  12752. X-WR-TIMEZONE:%s
  12753. CALSCALE:GREGORIAN\n" name user timezone))))
  12754. (defun org-finish-icalendar-file ()
  12755. "Finish an iCalendar file by inserting the END statement."
  12756. (princ "END:VCALENDAR\n"))
  12757. (defun org-ical-ts-to-string (s keyword &optional inc)
  12758. "Take a time string S and convert it to iCalendar format.
  12759. KEYWORD is added in front, to make a complete line like DTSTART....
  12760. When INC is non-nil, increase the hour by two (if time string contains
  12761. a time), or the day by one (if it does not contain a time)."
  12762. (let ((t1 (org-parse-time-string s 'nodefault))
  12763. t2 fmt have-time time)
  12764. (if (and (car t1) (nth 1 t1) (nth 2 t1))
  12765. (setq t2 t1 have-time t)
  12766. (setq t2 (org-parse-time-string s)))
  12767. (let ((s (car t2)) (mi (nth 1 t2)) (h (nth 2 t2))
  12768. (d (nth 3 t2)) (m (nth 4 t2)) (y (nth 5 t2)))
  12769. (when inc
  12770. (if have-time (setq h (+ 2 h)) (setq d (1+ d))))
  12771. (setq time (encode-time s mi h d m y)))
  12772. (setq fmt (if have-time ":%Y%m%dT%H%M%S" ";VALUE=DATE:%Y%m%d"))
  12773. (concat keyword (format-time-string fmt time))))
  12774. ;;; Key bindings
  12775. ;; - Bindings in Org-mode map are currently
  12776. ;; 0123456789abcdefghijklmnopqrstuvwxyz!?@#$%^&-+*/=()_{}[]:;"|,.<>~`'\t the alphabet
  12777. ;; abcd fgh j lmnopqrstuvwxyz!? #$ ^ -+*/= [] ; |,.<>~ '\t necessary bindings
  12778. ;; e (?) useful from outline-mode
  12779. ;; i k @ expendable from outline-mode
  12780. ;; 0123456789 % & ()_{} " ` free
  12781. ;; Make `C-c C-x' a prefix key
  12782. (define-key org-mode-map "\C-c\C-x" (make-sparse-keymap))
  12783. (define-key org-mode-map "\C-c\C-e" (make-sparse-keymap))
  12784. ;; TAB key with modifiers
  12785. (define-key org-mode-map "\C-i" 'org-cycle)
  12786. (define-key org-mode-map [(tab)] 'org-cycle)
  12787. (define-key org-mode-map [(meta tab)] 'org-complete)
  12788. (define-key org-mode-map "\M-\C-i" 'org-complete) ; for tty emacs
  12789. ;; The following line is necessary under Suse GNU/Linux
  12790. (unless (featurep 'xemacs)
  12791. (define-key org-mode-map [S-iso-lefttab] 'org-shifttab))
  12792. (define-key org-mode-map [(shift tab)] 'org-shifttab)
  12793. (define-key org-mode-map (org-key 'S-return) 'org-table-copy-down)
  12794. (define-key org-mode-map "\C-c\C-xc" 'org-table-copy-down) ; tty
  12795. (define-key org-mode-map [(meta shift return)] 'org-insert-todo-heading)
  12796. (define-key org-mode-map "\C-c\C-xM" 'org-insert-todo-heading) ; tty
  12797. (define-key org-mode-map [(meta return)] 'org-meta-return)
  12798. (define-key org-mode-map "\C-c\C-xm" 'org-meta-return) ; tty emacs
  12799. (define-key org-mode-map [?\e (return)] 'org-meta-return) ; tty emacs
  12800. ;; Cursor keys with modifiers
  12801. (define-key org-mode-map [(meta left)] 'org-metaleft)
  12802. (define-key org-mode-map [?\e (left)] 'org-metaleft) ; for tty emacs
  12803. (define-key org-mode-map "\C-c\C-xl" 'org-metaleft) ; for tty emacs
  12804. (define-key org-mode-map [(meta right)] 'org-metaright)
  12805. (define-key org-mode-map [?\e (right)] 'org-metaright) ; for tty emacs
  12806. (define-key org-mode-map "\C-c\C-xr" 'org-metaright) ; for tty emacs
  12807. (define-key org-mode-map [(meta up)] 'org-metaup)
  12808. (define-key org-mode-map [?\e (up)] 'org-metaup) ; for tty emacs
  12809. (define-key org-mode-map "\C-c\C-xu" 'org-metaup) ; for tty emacs
  12810. (define-key org-mode-map [(meta down)] 'org-metadown)
  12811. (define-key org-mode-map [?\e (down)] 'org-metadown) ; for tty emacs
  12812. (define-key org-mode-map "\C-c\C-xd" 'org-metadown) ; for tty emacs
  12813. (define-key org-mode-map [(meta shift left)] 'org-shiftmetaleft)
  12814. (define-key org-mode-map "\C-c\C-xL" 'org-shiftmetaleft) ; tty
  12815. (define-key org-mode-map [(meta shift right)] 'org-shiftmetaright)
  12816. (define-key org-mode-map "\C-c\C-xR" 'org-shiftmetaright) ; tty
  12817. (define-key org-mode-map [(meta shift up)] 'org-shiftmetaup)
  12818. (define-key org-mode-map "\C-c\C-xU" 'org-shiftmetaup) ; tty
  12819. (define-key org-mode-map [(meta shift down)] 'org-shiftmetadown)
  12820. (define-key org-mode-map "\C-c\C-xD" 'org-shiftmetadown) ; tty
  12821. (define-key org-mode-map (org-key 'S-up) 'org-shiftup)
  12822. (define-key org-mode-map [?\C-c ?\C-x (up)] 'org-shiftup)
  12823. (define-key org-mode-map (org-key 'S-down) 'org-shiftdown)
  12824. (define-key org-mode-map [?\C-c ?\C-x (down)] 'org-shiftdown)
  12825. (define-key org-mode-map (org-key 'S-left) 'org-shiftleft)
  12826. (define-key org-mode-map [?\C-c ?\C-x (left)] 'org-shiftleft)
  12827. (define-key org-mode-map (org-key 'S-right) 'org-shiftright)
  12828. (define-key org-mode-map [?\C-c ?\C-x (right)] 'org-shiftright)
  12829. ;; All the other keys
  12830. (define-key org-mode-map "\C-c\C-a" 'show-all) ; in case allout messed up.
  12831. (define-key org-mode-map "\C-c$" 'org-archive-subtree)
  12832. (define-key org-mode-map "\C-c\C-j" 'org-goto)
  12833. (define-key org-mode-map "\C-c\C-t" 'org-todo)
  12834. (define-key org-mode-map "\C-c\C-s" 'org-schedule)
  12835. (define-key org-mode-map "\C-c\C-d" 'org-deadline)
  12836. (define-key org-mode-map "\C-c;" 'org-toggle-comment)
  12837. (define-key org-mode-map "\C-c\C-v" 'org-show-todo-tree)
  12838. (define-key org-mode-map "\C-c\C-w" 'org-check-deadlines)
  12839. (define-key org-mode-map "\C-c/" 'org-occur) ; Minor-mode reserved
  12840. (define-key org-mode-map "\C-c\\" 'org-tags-sparse-tree) ; Minor-mode res.
  12841. (define-key org-mode-map "\C-c\C-m" 'org-insert-heading)
  12842. (define-key org-mode-map "\M-\C-m" 'org-insert-heading)
  12843. (define-key org-mode-map "\C-c\C-l" 'org-insert-link)
  12844. (define-key org-mode-map "\C-c\C-o" 'org-open-at-point)
  12845. (define-key org-mode-map "\C-c%" 'org-mark-ring-push)
  12846. (define-key org-mode-map "\C-c&" 'org-mark-ring-goto)
  12847. (define-key org-mode-map "\C-c\C-z" 'org-time-stamp) ; Alternative binding
  12848. (define-key org-mode-map "\C-c." 'org-time-stamp) ; Minor-mode reserved
  12849. (define-key org-mode-map "\C-c!" 'org-time-stamp-inactive) ; Minor-mode r.
  12850. (define-key org-mode-map "\C-c," 'org-priority) ; Minor-mode reserved
  12851. (define-key org-mode-map "\C-c\C-y" 'org-evaluate-time-range)
  12852. (define-key org-mode-map "\C-c>" 'org-goto-calendar)
  12853. (define-key org-mode-map "\C-c<" 'org-date-from-calendar)
  12854. (define-key org-mode-map [(control ?,)] 'org-cycle-agenda-files)
  12855. (define-key org-mode-map "\C-c[" 'org-agenda-file-to-front)
  12856. (define-key org-mode-map "\C-c]" 'org-remove-file)
  12857. (define-key org-mode-map "\C-c\C-r" 'org-timeline)
  12858. (define-key org-mode-map "\C-c-" 'org-table-insert-hline)
  12859. (define-key org-mode-map "\C-c^" 'org-table-sort-lines)
  12860. (define-key org-mode-map "\C-c\C-c" 'org-ctrl-c-ctrl-c)
  12861. (define-key org-mode-map "\C-m" 'org-return)
  12862. (define-key org-mode-map "\C-c?" 'org-table-current-column)
  12863. (define-key org-mode-map "\C-c " 'org-table-blank-field)
  12864. (define-key org-mode-map "\C-c+" 'org-table-sum)
  12865. (define-key org-mode-map "\C-c=" 'org-table-eval-formula)
  12866. (define-key org-mode-map "\C-c'" 'org-table-edit-formulas)
  12867. (define-key org-mode-map "\C-c`" 'org-table-edit-field)
  12868. (define-key org-mode-map "\C-c|" 'org-table-create-or-convert-from-region)
  12869. (define-key org-mode-map "\C-c*" 'org-table-recalculate)
  12870. (define-key org-mode-map [(control ?#)] 'org-table-rotate-recalc-marks)
  12871. (define-key org-mode-map "\C-c~" 'org-table-create-with-table.el)
  12872. (define-key org-mode-map "\C-c\C-q" 'org-table-wrap-region)
  12873. (define-key org-mode-map "\C-c\C-xa" 'org-export-as-ascii)
  12874. (define-key org-mode-map "\C-c\C-x\C-a" 'org-export-as-ascii)
  12875. (define-key org-mode-map "\C-c\C-xv" 'org-export-visible)
  12876. (define-key org-mode-map "\C-c\C-x\C-v" 'org-export-visible)
  12877. ;; OPML support is only an option for the future
  12878. ;(define-key org-mode-map "\C-c\C-xo" 'org-export-as-opml)
  12879. ;(define-key org-mode-map "\C-c\C-x\C-o" 'org-export-as-opml)
  12880. (define-key org-mode-map "\C-c\C-xi" 'org-export-icalendar-this-file)
  12881. (define-key org-mode-map "\C-c\C-x\C-i" 'org-export-icalendar-all-agenda-files)
  12882. (define-key org-mode-map "\C-c\C-xc" 'org-export-icalendar-combine-agenda-files)
  12883. (define-key org-mode-map "\C-c\C-x\C-c" 'org-export-icalendar-combine-agenda-files)
  12884. (define-key org-mode-map "\C-c\C-xt" 'org-insert-export-options-template)
  12885. (define-key org-mode-map "\C-c:" 'org-toggle-fixed-width-section)
  12886. (define-key org-mode-map "\C-c\C-xh" 'org-export-as-html)
  12887. (define-key org-mode-map "\C-c\C-xx" 'org-export-as-xoxo)
  12888. (define-key org-mode-map "\C-c\C-x\C-x" 'org-export-as-xoxo)
  12889. (define-key org-mode-map "\C-c\C-xb" 'org-export-as-html-and-open)
  12890. (define-key org-mode-map "\C-c\C-x\C-b" 'org-export-as-html-and-open)
  12891. (define-key org-mode-map "\C-c\C-x\C-k" 'org-cut-special)
  12892. (define-key org-mode-map "\C-c\C-x\C-w" 'org-cut-special)
  12893. (define-key org-mode-map "\C-c\C-x\M-w" 'org-copy-special)
  12894. (define-key org-mode-map "\C-c\C-x\C-y" 'org-paste-special)
  12895. (define-key org-mode-map "\C-c\C-ef" 'org-publish-current-file)
  12896. (define-key org-mode-map "\C-c\C-ep" 'org-publish-current-project)
  12897. (define-key org-mode-map "\C-c\C-ec" 'org-publish)
  12898. (define-key org-mode-map "\C-c\C-ea" 'org-publish-all)
  12899. (define-key org-mode-map "\C-c\C-e\C-f" 'org-publish-current-file)
  12900. (define-key org-mode-map "\C-c\C-e\C-p" 'org-publish-current-project)
  12901. (define-key org-mode-map "\C-c\C-e\C-c" 'org-publish)
  12902. (define-key org-mode-map "\C-c\C-e\C-a" 'org-publish-all)
  12903. (when (featurep 'xemacs)
  12904. (define-key org-mode-map 'button3 'popup-mode-menu))
  12905. (defsubst org-table-p () (org-at-table-p))
  12906. (defun org-self-insert-command (N)
  12907. "Like `self-insert-command', use overwrite-mode for whitespace in tables.
  12908. If the cursor is in a table looking at whitespace, the whitespace is
  12909. overwritten, and the table is not marked as requiring realignment."
  12910. (interactive "p")
  12911. (if (and (org-table-p)
  12912. (progn
  12913. ;; check if we blank the field, and if that triggers align
  12914. (and org-table-auto-blank-field
  12915. (member last-command
  12916. '(org-cycle org-return org-shifttab org-ctrl-c-ctrl-c))
  12917. (if (or (equal (char-after) ?\ ) (looking-at "[^|\n]* |"))
  12918. ;; got extra space, this field does not determine column width
  12919. (let (org-table-may-need-update) (org-table-blank-field))
  12920. ;; no extra space, this field may determine column width
  12921. (org-table-blank-field)))
  12922. t)
  12923. (eq N 1)
  12924. (looking-at "[^|\n]* |"))
  12925. (let (org-table-may-need-update)
  12926. (goto-char (1- (match-end 0)))
  12927. (delete-backward-char 1)
  12928. (goto-char (match-beginning 0))
  12929. (self-insert-command N))
  12930. (setq org-table-may-need-update t)
  12931. (self-insert-command N)))
  12932. (defun org-delete-backward-char (N)
  12933. "Like `delete-backward-char', insert whitespace at field end in tables.
  12934. When deleting backwards, in tables this function will insert whitespace in
  12935. front of the next \"|\" separator, to keep the table aligned. The table will
  12936. still be marked for re-alignment if the field did fill the entire column,
  12937. because, in this case the deletion might narrow the column."
  12938. (interactive "p")
  12939. (if (and (org-table-p)
  12940. (eq N 1)
  12941. (string-match "|" (buffer-substring (point-at-bol) (point)))
  12942. (looking-at ".*?|"))
  12943. (let ((pos (point))
  12944. (noalign (looking-at "[^|\n\r]* |"))
  12945. (c org-table-may-need-update))
  12946. (backward-delete-char N)
  12947. (skip-chars-forward "^|")
  12948. (insert " ")
  12949. (goto-char (1- pos))
  12950. ;; noalign: if there were two spaces at the end, this field
  12951. ;; does not determine the width of the column.
  12952. (if noalign (setq org-table-may-need-update c)))
  12953. (backward-delete-char N)))
  12954. (defun org-delete-char (N)
  12955. "Like `delete-char', but insert whitespace at field end in tables.
  12956. When deleting characters, in tables this function will insert whitespace in
  12957. front of the next \"|\" separator, to keep the table aligned. The table will
  12958. still be marked for re-alignment if the field did fill the entire column,
  12959. because, in this case the deletion might narrow the column."
  12960. (interactive "p")
  12961. (if (and (org-table-p)
  12962. (not (bolp))
  12963. (not (= (char-after) ?|))
  12964. (eq N 1))
  12965. (if (looking-at ".*?|")
  12966. (let ((pos (point))
  12967. (noalign (looking-at "[^|\n\r]* |"))
  12968. (c org-table-may-need-update))
  12969. (replace-match (concat
  12970. (substring (match-string 0) 1 -1)
  12971. " |"))
  12972. (goto-char pos)
  12973. ;; noalign: if there were two spaces at the end, this field
  12974. ;; does not determine the width of the column.
  12975. (if noalign (setq org-table-may-need-update c)))
  12976. (delete-char N))
  12977. (delete-char N)))
  12978. ;; How to do this: Measure non-white length of current string
  12979. ;; If equal to column width, we should realign.
  12980. (defun org-remap (map &rest commands)
  12981. "In MAP, remap the functions given in COMMANDS.
  12982. COMMANDS is a list of alternating OLDDEF NEWDEF command names."
  12983. (let (new old)
  12984. (while commands
  12985. (setq old (pop commands) new (pop commands))
  12986. (if (fboundp 'command-remapping)
  12987. (define-key map (vector 'remap old) new)
  12988. (substitute-key-definition old new map global-map)))))
  12989. (when (eq org-enable-table-editor 'optimized)
  12990. ;; If the user wants maximum table support, we need to hijack
  12991. ;; some standard editing functions
  12992. (org-remap org-mode-map
  12993. 'self-insert-command 'org-self-insert-command
  12994. 'delete-char 'org-delete-char
  12995. 'delete-backward-char 'org-delete-backward-char)
  12996. (define-key org-mode-map "|" 'org-force-self-insert))
  12997. (defun org-shiftcursor-error ()
  12998. "Throw an error because Shift-Cursor command was applied in wrong context."
  12999. (error "This command is active in special context like tables, headlines or timestamps"))
  13000. (defun org-shifttab ()
  13001. "Global visibility cycling or move to previous table field.
  13002. Calls `org-cycle' with argument t, or `org-table-previous-field', depending
  13003. on context.
  13004. See the individual commands for more information."
  13005. (interactive)
  13006. (cond
  13007. ((org-at-table-p) (call-interactively 'org-table-previous-field))
  13008. (t (call-interactively 'org-global-cycle))))
  13009. (defun org-shiftmetaleft ()
  13010. "Promote subtree or delete table column.
  13011. Calls `org-promote-subtree' or `org-table-delete-column', depending on context.
  13012. See the individual commands for more information."
  13013. (interactive)
  13014. (cond
  13015. ((org-at-table-p) (call-interactively 'org-table-delete-column))
  13016. ((org-on-heading-p) (call-interactively 'org-promote-subtree))
  13017. ((org-at-item-p) (call-interactively 'org-outdent-item))
  13018. (t (org-shiftcursor-error))))
  13019. (defun org-shiftmetaright ()
  13020. "Demote subtree or insert table column.
  13021. Calls `org-demote-subtree' or `org-table-insert-column', depending on context.
  13022. See the individual commands for more information."
  13023. (interactive)
  13024. (cond
  13025. ((org-at-table-p) (call-interactively 'org-table-insert-column))
  13026. ((org-on-heading-p) (call-interactively 'org-demote-subtree))
  13027. ((org-at-item-p) (call-interactively 'org-indent-item))
  13028. (t (org-shiftcursor-error))))
  13029. (defun org-shiftmetaup (&optional arg)
  13030. "Move subtree up or kill table row.
  13031. Calls `org-move-subtree-up' or `org-table-kill-row' or
  13032. `org-move-item-up' depending on context. See the individual commands
  13033. for more information."
  13034. (interactive "P")
  13035. (cond
  13036. ((org-at-table-p) (call-interactively 'org-table-kill-row))
  13037. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13038. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13039. (t (org-shiftcursor-error))))
  13040. (defun org-shiftmetadown (&optional arg)
  13041. "Move subtree down or insert table row.
  13042. Calls `org-move-subtree-down' or `org-table-insert-row' or
  13043. `org-move-item-down', depending on context. See the individual
  13044. commands for more information."
  13045. (interactive "P")
  13046. (cond
  13047. ((org-at-table-p) (call-interactively 'org-table-insert-row))
  13048. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13049. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13050. (t (org-shiftcursor-error))))
  13051. (defun org-metaleft (&optional arg)
  13052. "Promote heading or move table column to left.
  13053. Calls `org-do-promote' or `org-table-move-column', depending on context.
  13054. With no specific context, calls the Emacs default `backward-word'.
  13055. See the individual commands for more information."
  13056. (interactive "P")
  13057. (cond
  13058. ((org-at-table-p) (org-call-with-arg 'org-table-move-column 'left))
  13059. ((or (org-on-heading-p) (org-region-active-p))
  13060. (call-interactively 'org-do-promote))
  13061. (t (call-interactively 'backward-word))))
  13062. (defun org-metaright (&optional arg)
  13063. "Demote subtree or move table column to right.
  13064. Calls `org-do-demote' or `org-table-move-column', depending on context.
  13065. With no specific context, calls the Emacs default `forward-word'.
  13066. See the individual commands for more information."
  13067. (interactive "P")
  13068. (cond
  13069. ((org-at-table-p) (call-interactively 'org-table-move-column))
  13070. ((or (org-on-heading-p) (org-region-active-p))
  13071. (call-interactively 'org-do-demote))
  13072. (t (call-interactively 'forward-word))))
  13073. (defun org-metaup (&optional arg)
  13074. "Move subtree up or move table row up.
  13075. Calls `org-move-subtree-up' or `org-table-move-row' or
  13076. `org-move-item-up', depending on context. See the individual commands
  13077. for more information."
  13078. (interactive "P")
  13079. (cond
  13080. ((org-at-table-p) (org-call-with-arg 'org-table-move-row 'up))
  13081. ((org-on-heading-p) (call-interactively 'org-move-subtree-up))
  13082. ((org-at-item-p) (call-interactively 'org-move-item-up))
  13083. (t (org-shiftcursor-error))))
  13084. (defun org-metadown (&optional arg)
  13085. "Move subtree down or move table row down.
  13086. Calls `org-move-subtree-down' or `org-table-move-row' or
  13087. `org-move-item-down', depending on context. See the individual
  13088. commands for more information."
  13089. (interactive "P")
  13090. (cond
  13091. ((org-at-table-p) (call-interactively 'org-table-move-row))
  13092. ((org-on-heading-p) (call-interactively 'org-move-subtree-down))
  13093. ((org-at-item-p) (call-interactively 'org-move-item-down))
  13094. (t (org-shiftcursor-error))))
  13095. (defun org-shiftup (&optional arg)
  13096. "Increase item in timestamp or increase priority of current headline.
  13097. Calls `org-timestamp-up' or `org-priority-up', depending on context.
  13098. See the individual commands for more information."
  13099. (interactive "P")
  13100. (cond
  13101. ((org-at-timestamp-p) (call-interactively 'org-timestamp-up))
  13102. ((org-on-heading-p) (call-interactively 'org-priority-up))
  13103. ((org-at-item-p) (call-interactively 'org-previous-item))
  13104. (t (call-interactively 'org-beginning-of-item) (beginning-of-line 1))))
  13105. (defun org-shiftdown (&optional arg)
  13106. "Decrease item in timestamp or decrease priority of current headline.
  13107. Calls `org-timestamp-down' or `org-priority-down', depending on context.
  13108. See the individual commands for more information."
  13109. (interactive "P")
  13110. (cond
  13111. ((org-at-timestamp-p) (call-interactively 'org-timestamp-down))
  13112. ((org-on-heading-p) (call-interactively 'org-priority-down))
  13113. (t (call-interactively 'org-next-item))))
  13114. (defun org-shiftright ()
  13115. "Next TODO keyword or timestamp one day later, depending on context."
  13116. (interactive)
  13117. (cond
  13118. ((org-at-timestamp-p) (call-interactively 'org-timestamp-up-day))
  13119. ((org-on-heading-p) (org-call-with-arg 'org-todo 'right))
  13120. (t (org-shiftcursor-error))))
  13121. (defun org-shiftleft ()
  13122. "Previous TODO keyword or timestamp one day earlier, depending on context."
  13123. (interactive)
  13124. (cond
  13125. ((org-at-timestamp-p) (call-interactively 'org-timestamp-down-day))
  13126. ((org-on-heading-p) (org-call-with-arg 'org-todo 'left))
  13127. (t (org-shiftcursor-error))))
  13128. (defun org-copy-special ()
  13129. "Copy region in table or copy current subtree.
  13130. Calls `org-table-copy' or `org-copy-subtree', depending on context.
  13131. See the individual commands for more information."
  13132. (interactive)
  13133. (call-interactively
  13134. (if (org-at-table-p) 'org-table-copy-region 'org-copy-subtree)))
  13135. (defun org-cut-special ()
  13136. "Cut region in table or cut current subtree.
  13137. Calls `org-table-copy' or `org-cut-subtree', depending on context.
  13138. See the individual commands for more information."
  13139. (interactive)
  13140. (call-interactively
  13141. (if (org-at-table-p) 'org-table-cut-region 'org-cut-subtree)))
  13142. (defun org-paste-special (arg)
  13143. "Paste rectangular region into table, or past subtree relative to level.
  13144. Calls `org-table-paste-rectangle' or `org-paste-subtree', depending on context.
  13145. See the individual commands for more information."
  13146. (interactive "P")
  13147. (if (org-at-table-p)
  13148. (org-table-paste-rectangle)
  13149. (org-paste-subtree arg)))
  13150. (defun org-ctrl-c-ctrl-c (&optional arg)
  13151. "Set tags in headline, or update according to changed information at point.
  13152. This command does many different things, depending on context:
  13153. - If the cursor is in a headline, prompt for tags and insert them
  13154. into the current line, aligned to `org-tags-column'. When called
  13155. with prefix arg, realign all tags in the current buffer.
  13156. - If the cursor is in one of the special #+KEYWORD lines, this
  13157. triggers scanning the buffer for these lines and updating the
  13158. information.
  13159. - If the cursor is inside a table, realign the table. This command
  13160. works even if the automatic table editor has been turned off.
  13161. - If the cursor is on a #+TBLFM line, re-apply the formulas to
  13162. the entire table.
  13163. - If the cursor is inside a table created by the table.el package,
  13164. activate that table.
  13165. - If the current buffer is a remember buffer, close note and file it.
  13166. with a prefix argument, file it without further interaction to the default
  13167. location.
  13168. - If the cursor is on a <<<target>>>, update radio targets and corresponding
  13169. links in this buffer.
  13170. - If the cursor is on a numbered item in a plain list, renumber the
  13171. ordered list."
  13172. (interactive "P")
  13173. (let ((org-enable-table-editor t))
  13174. (cond
  13175. ((and (local-variable-p 'org-finish-function (current-buffer))
  13176. (fboundp org-finish-function))
  13177. (funcall org-finish-function))
  13178. ((org-on-target-p) (call-interactively 'org-update-radio-target-regexp))
  13179. ((org-on-heading-p) (call-interactively 'org-set-tags))
  13180. ((org-at-table.el-p)
  13181. (require 'table)
  13182. (beginning-of-line 1)
  13183. (re-search-forward "|" (save-excursion (end-of-line 2) (point)))
  13184. (call-interactively 'table-recognize-table))
  13185. ((org-at-table-p)
  13186. (org-table-maybe-eval-formula)
  13187. (if arg
  13188. (call-interactively 'org-table-recalculate)
  13189. (org-table-maybe-recalculate-line))
  13190. (call-interactively 'org-table-align))
  13191. ((org-at-item-checkbox-p)
  13192. (call-interactively 'org-toggle-checkbox))
  13193. ((org-at-item-p)
  13194. (call-interactively 'org-renumber-ordered-list))
  13195. ((save-excursion (beginning-of-line 1) (looking-at "#\\+\\([A-Z]+\\)"))
  13196. (cond
  13197. ((equal (match-string 1) "TBLFM")
  13198. ;; Recalculate the table before this line
  13199. (save-excursion
  13200. (beginning-of-line 1)
  13201. (skip-chars-backward " \r\n\t")
  13202. (if (org-at-table-p)
  13203. (org-call-with-arg 'org-table-recalculate t))))
  13204. (t
  13205. (call-interactively 'org-mode-restart))))
  13206. (t (error "C-c C-c can do nothing useful at this location.")))))
  13207. (defun org-mode-restart ()
  13208. "Restart Org-mode, to scan again for special lines.
  13209. Also updates the keyword regular expressions."
  13210. (interactive)
  13211. (let ((org-inhibit-startup t)) (org-mode))
  13212. (message "Org-mode restarted to refresh keyword and special line setup"))
  13213. (defun org-return ()
  13214. "Goto next table row or insert a newline.
  13215. Calls `org-table-next-row' or `newline', depending on context.
  13216. See the individual commands for more information."
  13217. (interactive)
  13218. (cond
  13219. ((org-at-table-p)
  13220. (org-table-justify-field-maybe)
  13221. (call-interactively 'org-table-next-row))
  13222. (t (newline))))
  13223. (defun org-meta-return (&optional arg)
  13224. "Insert a new heading or wrap a region in a table.
  13225. Calls `org-insert-heading' or `org-table-wrap-region', depending on context.
  13226. See the individual commands for more information."
  13227. (interactive "P")
  13228. (cond
  13229. ((org-at-table-p)
  13230. (call-interactively 'org-table-wrap-region))
  13231. (t (call-interactively 'org-insert-heading))))
  13232. ;;; Menu entries
  13233. ;; Define the Org-mode menus
  13234. (easy-menu-define org-tbl-menu org-mode-map "Tbl menu"
  13235. '("Tbl"
  13236. ["Align" org-ctrl-c-ctrl-c (org-at-table-p)]
  13237. ["Next Field" org-cycle (org-at-table-p)]
  13238. ["Previous Field" org-shifttab (org-at-table-p)]
  13239. ["Next Row" org-return (org-at-table-p)]
  13240. "--"
  13241. ["Blank Field" org-table-blank-field (org-at-table-p)]
  13242. ["Edit Field" org-table-edit-field (org-at-table-p)]
  13243. ["Copy Field from Above" org-table-copy-down (org-at-table-p)]
  13244. "--"
  13245. ("Column"
  13246. ["Move Column Left" org-metaleft (org-at-table-p)]
  13247. ["Move Column Right" org-metaright (org-at-table-p)]
  13248. ["Delete Column" org-shiftmetaleft (org-at-table-p)]
  13249. ["Insert Column" org-shiftmetaright (org-at-table-p)]
  13250. "--"
  13251. ["Enable Narrowing" (setq org-table-limit-column-width (not org-table-limit-column-width)) :active (org-at-table-p) :selected org-table-limit-column-width :style toggle])
  13252. ("Row"
  13253. ["Move Row Up" org-metaup (org-at-table-p)]
  13254. ["Move Row Down" org-metadown (org-at-table-p)]
  13255. ["Delete Row" org-shiftmetaup (org-at-table-p)]
  13256. ["Insert Row" org-shiftmetadown (org-at-table-p)]
  13257. ["Sort lines in region" org-table-sort-lines (org-at-table-p)]
  13258. "--"
  13259. ["Insert Hline" org-table-insert-hline (org-at-table-p)])
  13260. ("Rectangle"
  13261. ["Copy Rectangle" org-copy-special (org-at-table-p)]
  13262. ["Cut Rectangle" org-cut-special (org-at-table-p)]
  13263. ["Paste Rectangle" org-paste-special (org-at-table-p)]
  13264. ["Fill Rectangle" org-table-wrap-region (org-at-table-p)])
  13265. "--"
  13266. ("Calculate"
  13267. ["Set Column Formula" org-table-eval-formula (org-at-table-p)]
  13268. ["Set Named Field Formula" (org-table-eval-formula '(4)) :active (org-at-table-p) :keys "C-u C-c ="]
  13269. ["Edit Formulas" org-table-edit-formulas (org-at-table-p)]
  13270. "--"
  13271. ["Recalculate line" org-table-recalculate (org-at-table-p)]
  13272. ["Recalculate all" (lambda () (interactive) (org-table-recalculate '(4))) :active (org-at-table-p) :keys "C-u C-c *"]
  13273. ["Toggle Recalculate Mark" org-table-rotate-recalc-marks (org-at-table-p)]
  13274. "--"
  13275. ["Sum Column/Rectangle" org-table-sum
  13276. (or (org-at-table-p) (org-region-active-p))]
  13277. ["Which Column?" org-table-current-column (org-at-table-p)])
  13278. ["Debug Formulas"
  13279. (setq org-table-formula-debug (not org-table-formula-debug))
  13280. :style toggle :selected org-table-formula-debug]
  13281. "--"
  13282. ["Create" org-table-create (and (not (org-at-table-p))
  13283. org-enable-table-editor)]
  13284. ["Convert Region" org-table-convert-region (not (org-at-table-p 'any))]
  13285. ["Import from File" org-table-import (not (org-at-table-p))]
  13286. ["Export to File" org-table-export (org-at-table-p)]
  13287. "--"
  13288. ["Create/Convert from/to table.el" org-table-create-with-table.el t]))
  13289. (easy-menu-define org-org-menu org-mode-map "Org menu"
  13290. '("Org"
  13291. ["Cycle Visibility" org-cycle (or (bobp) (outline-on-heading-p))]
  13292. ["Cycle Global Visibility" org-shifttab (not (org-at-table-p))]
  13293. ["Sparse Tree" org-occur t]
  13294. ["Show All" show-all t]
  13295. "--"
  13296. ["New Heading" org-insert-heading t]
  13297. ("Navigate Headings"
  13298. ["Up" outline-up-heading t]
  13299. ["Next" outline-next-visible-heading t]
  13300. ["Previous" outline-previous-visible-heading t]
  13301. ["Next Same Level" outline-forward-same-level t]
  13302. ["Previous Same Level" outline-backward-same-level t]
  13303. "--"
  13304. ["Jump" org-goto t])
  13305. ("Edit Structure"
  13306. ["Move Subtree Up" org-shiftmetaup (not (org-at-table-p))]
  13307. ["Move Subtree Down" org-shiftmetadown (not (org-at-table-p))]
  13308. "--"
  13309. ["Copy Subtree" org-copy-special (not (org-at-table-p))]
  13310. ["Cut Subtree" org-cut-special (not (org-at-table-p))]
  13311. ["Paste Subtree" org-paste-special (not (org-at-table-p))]
  13312. "--"
  13313. ["Promote Heading" org-metaleft (not (org-at-table-p))]
  13314. ["Promote Subtree" org-shiftmetaleft (not (org-at-table-p))]
  13315. ["Demote Heading" org-metaright (not (org-at-table-p))]
  13316. ["Demote Subtree" org-shiftmetaright (not (org-at-table-p))]
  13317. "--"
  13318. ["Archive Subtree" org-archive-subtree t]
  13319. "--"
  13320. ["Convert to odd levels" org-convert-to-odd-levels t]
  13321. ["Convert to odd/even levels" org-convert-to-oddeven-levels t])
  13322. "--"
  13323. ("TODO Lists"
  13324. ["TODO/DONE/-" org-todo t]
  13325. ["Show TODO Tree" org-show-todo-tree t]
  13326. ["Global TODO list" org-todo-list t]
  13327. "--"
  13328. ["Set Priority" org-priority t]
  13329. ["Priority Up" org-shiftup t]
  13330. ["Priority Down" org-shiftdown t])
  13331. ("Dates and Scheduling"
  13332. ["Timestamp" org-time-stamp t]
  13333. ["Timestamp (inactive)" org-time-stamp-inactive t]
  13334. ("Change Date"
  13335. ["1 Day Later" org-timestamp-up-day t]
  13336. ["1 Day Earlier" org-timestamp-down-day t]
  13337. ["1 ... Later" org-shiftup t]
  13338. ["1 ... Earlier" org-shiftdown t])
  13339. ["Compute Time Range" org-evaluate-time-range t]
  13340. ["Schedule Item" org-schedule t]
  13341. ["Deadline" org-deadline t]
  13342. "--"
  13343. ["Goto Calendar" org-goto-calendar t]
  13344. ["Date from Calendar" org-date-from-calendar t])
  13345. "--"
  13346. ["Agenda Command" org-agenda t]
  13347. ("File List for Agenda")
  13348. ("Special views current file"
  13349. ["TODO Tree" org-show-todo-tree t]
  13350. ["Check Deadlines" org-check-deadlines t]
  13351. ["Timeline" org-timeline t]
  13352. ["Tags Tree" org-tags-sparse-tree t])
  13353. "--"
  13354. ("Hyperlinks"
  13355. ["Store Link (Global)" org-store-link t]
  13356. ["Insert Link" org-insert-link t]
  13357. ["Follow Link" org-open-at-point t]
  13358. "--"
  13359. ["Descriptive Links"
  13360. (progn (org-add-to-invisibility-spec '(org-link)) (org-restart-font-lock))
  13361. :style radio :selected (member '(org-link) buffer-invisibility-spec)]
  13362. ["Literal Links"
  13363. (progn
  13364. (org-remove-from-invisibility-spec '(org-link)) (org-restart-font-lock))
  13365. :style radio :selected (not (member '(org-link) buffer-invisibility-spec))]
  13366. "--"
  13367. ["Upgrade all <link> to [[link][desc]]" org-upgrade-old-links
  13368. (save-excursion (goto-char (point-min))
  13369. (re-search-forward "<[a-z]+:" nil t))])
  13370. "--"
  13371. ("Export"
  13372. ["ASCII" org-export-as-ascii t]
  13373. ["Export visible part..." org-export-visible t]
  13374. ["HTML" org-export-as-html t]
  13375. ["HTML and Open" org-export-as-html-and-open t]
  13376. ["XOXO" org-export-as-xoxo t]
  13377. "--"
  13378. ["iCalendar this file" org-export-icalendar-this-file t]
  13379. ["iCalendar all agenda files" org-export-icalendar-all-agenda-files
  13380. :active t :keys "C-c C-x C-i"]
  13381. ["iCalendar combined" org-export-icalendar-combine-agenda-files t]
  13382. "--"
  13383. ["Option Template" org-insert-export-options-template t]
  13384. ["Toggle Fixed Width" org-toggle-fixed-width-section t])
  13385. ("Publish"
  13386. ["Current File" org-publish-current-file t]
  13387. ["Current Project" org-publish-current-project t]
  13388. ["Project..." org-publish t]
  13389. ["All Projects" org-publish-all t])
  13390. "--"
  13391. ("Documentation"
  13392. ["Show Version" org-version t]
  13393. ["Info Documentation" org-info t])
  13394. ("Customize"
  13395. ["Browse Org Group" org-customize t]
  13396. "--"
  13397. ["Expand This Menu" org-create-customize-menu
  13398. (fboundp 'customize-menu-create)])
  13399. "--"
  13400. ["Refresh setup" org-mode-restart t]
  13401. ))
  13402. (defun org-info (&optional node)
  13403. "Read documentation for Org-mode in the info system.
  13404. With optional NODE, go directly to that node."
  13405. (interactive)
  13406. (require 'info)
  13407. (Info-goto-node (format "(org)%s" (or node ""))))
  13408. (defun org-install-agenda-files-menu ()
  13409. (let ((bl (buffer-list)))
  13410. (save-excursion
  13411. (while bl
  13412. (set-buffer (pop bl))
  13413. (if (eq major-mode 'org-mode) (setq bl nil)))
  13414. (when (eq major-mode 'org-mode)
  13415. (easy-menu-change
  13416. '("Org") "File List for Agenda"
  13417. (append
  13418. (list
  13419. ["Edit File List" (org-edit-agenda-file-list) t]
  13420. ["Add/Move Current File to Front of List" org-agenda-file-to-front t]
  13421. ["Remove Current File from List" org-remove-file t]
  13422. ["Cycle through agenda files" org-cycle-agenda-files t]
  13423. "--")
  13424. (mapcar 'org-file-menu-entry (org-agenda-files t))))))))
  13425. ;;; Documentation
  13426. (defun org-customize ()
  13427. "Call the customize function with org as argument."
  13428. (interactive)
  13429. (customize-browse 'org))
  13430. (defun org-create-customize-menu ()
  13431. "Create a full customization menu for Org-mode, insert it into the menu."
  13432. (interactive)
  13433. (if (fboundp 'customize-menu-create)
  13434. (progn
  13435. (easy-menu-change
  13436. '("Org") "Customize"
  13437. `(["Browse Org group" org-customize t]
  13438. "--"
  13439. ,(customize-menu-create 'org)
  13440. ["Set" Custom-set t]
  13441. ["Save" Custom-save t]
  13442. ["Reset to Current" Custom-reset-current t]
  13443. ["Reset to Saved" Custom-reset-saved t]
  13444. ["Reset to Standard Settings" Custom-reset-standard t]))
  13445. (message "\"Org\"-menu now contains full customization menu"))
  13446. (error "Cannot expand menu (outdated version of cus-edit.el)")))
  13447. ;;; Miscellaneous stuff
  13448. (defun org-context ()
  13449. "Return a list of contexts of the current cursor position.
  13450. If several contexts apply, all are returned.
  13451. Each context entry is a list with a symbol naming the context, and
  13452. two positions indicating start and end of the context. Possible
  13453. contexts are:
  13454. :headline anywhere in a headline
  13455. :headline-stars on the leading stars in a headline
  13456. :todo-keyword on a TODO keyword (including DONE) in a headline
  13457. :tags on the TAGS in a headline
  13458. :priority on the priority cookie in a headline
  13459. :item on the first line of a plain list item
  13460. :checkbox on the checkbox in a plain list item
  13461. :table in an org-mode table
  13462. :table-special on a special filed in a table
  13463. :table-table in a table.el table
  13464. :link on a hyperline
  13465. :keyword on a keyword: SCHEDULED, DEADLINE, CLOSE,COMMENT, QUOTE.
  13466. :target on a <<target>>
  13467. :radio-target on a <<<radio-target>>>
  13468. This function expects the position to be visible because it uses font-lock
  13469. faces as a help to recognize the following contexts: :table-special, :link,
  13470. and :keyword."
  13471. (let* ((f (get-text-property (point) 'face))
  13472. (faces (if (listp f) f (list f)))
  13473. (p (point)) clist)
  13474. ;; First the large context
  13475. (cond
  13476. ((org-on-heading-p)
  13477. (push (list :headline (point-at-bol) (point-at-eol)) clist)
  13478. (when (progn
  13479. (beginning-of-line 1)
  13480. (looking-at org-todo-line-tags-regexp))
  13481. (push (org-point-in-group p 1 :headline-stars) clist)
  13482. (push (org-point-in-group p 2 :todo-keyword) clist)
  13483. (push (org-point-in-group p 4 :tags) clist))
  13484. (goto-char p)
  13485. (skip-chars-backward "^[\n\r \t") (or (eobp) (backward-char 1))
  13486. (if (looking-at "\\[#[A-Z]\\]")
  13487. (push (org-point-in-group p 0 :priority) clist)))
  13488. ((org-at-item-p)
  13489. (push (list :item (point-at-bol)
  13490. (save-excursion (org-end-of-item) (point)))
  13491. clist)
  13492. (and (org-at-item-checkbox-p)
  13493. (push (org-point-in-group p 0 :checkbox) clist)))
  13494. ((org-at-table-p)
  13495. (push (list :table (org-table-begin) (org-table-end)) clist)
  13496. (if (memq 'org-formula faces)
  13497. (push (list :table-special
  13498. (previous-single-property-change p 'face)
  13499. (next-single-property-change p 'face)) clist)))
  13500. ((org-at-table-p 'any)
  13501. (push (list :table-table) clist)))
  13502. (goto-char p)
  13503. ;; Now the small context
  13504. (cond
  13505. ((org-at-timestamp-p)
  13506. (push (org-point-in-group p 0 :timestamp) clist))
  13507. ((memq 'org-link faces)
  13508. (push (list :link
  13509. (previous-single-property-change p 'face)
  13510. (next-single-property-change p 'face)) clist))
  13511. ((memq 'org-special-keyword faces)
  13512. (push (list :keyword
  13513. (previous-single-property-change p 'face)
  13514. (next-single-property-change p 'face)) clist))
  13515. ((org-on-target-p)
  13516. (push (org-point-in-group p 0 :target) clist)
  13517. (goto-char (1- (match-beginning 0)))
  13518. (if (looking-at org-radio-target-regexp)
  13519. (push (org-point-in-group p 0 :radio-target) clist))
  13520. (goto-char p)))
  13521. (setq clist (nreverse (delq nil clist)))
  13522. clist))
  13523. (defun org-point-in-group (point group &optional context)
  13524. "Check if POINT is in match-group GROUP.
  13525. If CONTEXT is non-nil, return a list with CONTEXT and the boundaries of the
  13526. match. If the match group does ot exist or point is not inside it,
  13527. return nil."
  13528. (and (match-beginning group)
  13529. (>= point (match-beginning group))
  13530. (<= point (match-end group))
  13531. (if context
  13532. (list context (match-beginning group) (match-end group))
  13533. t)))
  13534. (defun org-move-line-down (arg)
  13535. "Move the current line down. With prefix argument, move it past ARG lines."
  13536. (interactive "p")
  13537. (let ((col (current-column))
  13538. beg end pos)
  13539. (beginning-of-line 1) (setq beg (point))
  13540. (beginning-of-line 2) (setq end (point))
  13541. (beginning-of-line (+ 1 arg))
  13542. (setq pos (move-marker (make-marker) (point)))
  13543. (insert (delete-and-extract-region beg end))
  13544. (goto-char pos)
  13545. (move-to-column col)))
  13546. (defun org-move-line-up (arg)
  13547. "Move the current line up. With prefix argument, move it past ARG lines."
  13548. (interactive "p")
  13549. (let ((col (current-column))
  13550. beg end pos)
  13551. (beginning-of-line 1) (setq beg (point))
  13552. (beginning-of-line 2) (setq end (point))
  13553. (beginning-of-line (- arg))
  13554. (setq pos (move-marker (make-marker) (point)))
  13555. (insert (delete-and-extract-region beg end))
  13556. (goto-char pos)
  13557. (move-to-column col)))
  13558. ;; Paragraph filling stuff.
  13559. ;; We want this to be just right, so use the full arsenal.
  13560. (defun org-set-autofill-regexps ()
  13561. (interactive)
  13562. ;; In the paragraph separator we include headlines, because filling
  13563. ;; text in a line directly attached to a headline would otherwise
  13564. ;; fill the headline as well.
  13565. (set (make-local-variable 'paragraph-separate) "\f\\|\\*\\|[ ]*$\\|[ \t]*[:|]")
  13566. ;; The paragraph starter includes hand-formatted lists.
  13567. (set (make-local-variable 'paragraph-start)
  13568. "\f\\|[ ]*$\\|\\([*\f]+\\)\\|[ \t]*\\([-+*]\\|[0-9]+[.)][ \t]+\\)\\|[ \t]*[:|]")
  13569. ;; Inhibit auto-fill for headers, tables and fixed-width lines.
  13570. ;; But only if the user has not turned off tables or fixed-width regions
  13571. (set (make-local-variable 'auto-fill-inhibit-regexp)
  13572. (concat "\\*\\|#"
  13573. (if (or org-enable-table-editor org-enable-fixed-width-editor)
  13574. (concat
  13575. "\\|[ \t]*["
  13576. (if org-enable-table-editor "|" "")
  13577. (if org-enable-fixed-width-editor ":" "")
  13578. "]"))))
  13579. ;; We use our own fill-paragraph function, to make sure that tables
  13580. ;; and fixed-width regions are not wrapped. That function will pass
  13581. ;; through to `fill-paragraph' when appropriate.
  13582. (set (make-local-variable 'fill-paragraph-function) 'org-fill-paragraph)
  13583. ;; Adaptive filling: To get full control, first make sure that
  13584. ;; `adaptive-fill-regexp' never matches. Then install our own matcher.
  13585. (set (make-local-variable 'adaptive-fill-regexp) "\000")
  13586. (set (make-local-variable 'adaptive-fill-function)
  13587. 'org-adaptive-fill-function))
  13588. (defun org-fill-paragraph (&optional justify)
  13589. "Re-align a table, pass through to fill-paragraph if no table."
  13590. (let ((table-p (org-at-table-p))
  13591. (table.el-p (org-at-table.el-p)))
  13592. (cond ((equal (char-after (point-at-bol)) ?*) t) ; skip headlines
  13593. (table.el-p t) ; skip table.el tables
  13594. (table-p (org-table-align) t) ; align org-mode tables
  13595. (t nil)))) ; call paragraph-fill
  13596. ;; For reference, this is the default value of adaptive-fill-regexp
  13597. ;; "[ \t]*\\([-|#;>*]+[ \t]*\\|(?[0-9]+[.)][ \t]*\\)*"
  13598. (defun org-adaptive-fill-function ()
  13599. "Return a fill prefix for org-mode files.
  13600. In particular, this makes sure hanging paragraphs for hand-formatted lists
  13601. work correctly."
  13602. (if (looking-at " *\\([-*+] \\|[0-9]+[.)] \\)?")
  13603. (make-string (- (match-end 0) (match-beginning 0)) ?\ )))
  13604. ;; Functions needed for Emacs/XEmacs region compatibility
  13605. (defun org-add-hook (hook function &optional append local)
  13606. "Add-hook, compatible with both Emacsen."
  13607. (if (and local (featurep 'xemacs))
  13608. (add-local-hook hook function append)
  13609. (add-hook hook function append local)))
  13610. (defun org-region-active-p ()
  13611. "Is `transient-mark-mode' on and the region active?
  13612. Works on both Emacs and XEmacs."
  13613. (if org-ignore-region
  13614. nil
  13615. (if (featurep 'xemacs)
  13616. (and zmacs-regions (region-active-p))
  13617. (and transient-mark-mode mark-active))))
  13618. (defun org-add-to-invisibility-spec (arg)
  13619. "Add elements to `buffer-invisibility-spec'.
  13620. See documentation for `buffer-invisibility-spec' for the kind of elements
  13621. that can be added."
  13622. (cond
  13623. ((fboundp 'add-to-invisibility-spec)
  13624. (add-to-invisibility-spec arg))
  13625. ((or (null buffer-invisibility-spec) (eq buffer-invisibility-spec t))
  13626. (setq buffer-invisibility-spec (list arg)))
  13627. (t
  13628. (setq buffer-invisibility-spec
  13629. (cons arg buffer-invisibility-spec)))))
  13630. (defun org-remove-from-invisibility-spec (arg)
  13631. "Remove elements from `buffer-invisibility-spec'."
  13632. (if (fboundp 'remove-from-invisibility-spec)
  13633. (remove-from-invisibility-spec arg)
  13634. (if (consp buffer-invisibility-spec)
  13635. (setq buffer-invisibility-spec
  13636. (delete arg buffer-invisibility-spec)))))
  13637. (defun org-in-invisibility-spec-p (arg)
  13638. "Is ARG a member of `buffer-invisibility-spec'?"
  13639. (if (consp buffer-invisibility-spec)
  13640. (member arg buffer-invisibility-spec)
  13641. nil))
  13642. (defun org-image-file-name-regexp ()
  13643. "Return regexp matching the file names of images."
  13644. (if (fboundp 'image-file-name-regexp)
  13645. (image-file-name-regexp)
  13646. (let ((image-file-name-extensions
  13647. '("png" "jpeg" "jpg" "gif" "tiff" "tif"
  13648. "xbm" "xpm" "pbm" "pgm" "ppm")))
  13649. (concat "\\."
  13650. (regexp-opt (nconc (mapcar 'upcase
  13651. image-file-name-extensions)
  13652. image-file-name-extensions)
  13653. t)
  13654. "\\'"))))
  13655. ;; Functions needed for compatibility with old outline.el.
  13656. ;; Programming for the old outline.el (that uses selective display
  13657. ;; instead of `invisible' text properties) is a nightmare, mostly
  13658. ;; because regular expressions can no longer be anchored at
  13659. ;; beginning/end of line. Therefore a number of function need special
  13660. ;; treatment when the old outline.el is being used.
  13661. ;; The following functions capture almost the entire compatibility code
  13662. ;; between the different versions of outline-mode. The only other
  13663. ;; places where this is important are the font-lock-keywords, and in
  13664. ;; `org-export-visible'. Search for `org-noutline-p' to find them.
  13665. ;; C-a should go to the beginning of a *visible* line, also in the
  13666. ;; new outline.el. I guess this should be patched into Emacs?
  13667. (defun org-beginning-of-line ()
  13668. "Go to the beginning of the current line. If that is invisible, continue
  13669. to a visible line beginning. This makes the function of C-a more intuitive."
  13670. (interactive)
  13671. (beginning-of-line 1)
  13672. (if (bobp)
  13673. nil
  13674. (backward-char 1)
  13675. (if (org-invisible-p)
  13676. (while (and (not (bobp)) (org-invisible-p))
  13677. (backward-char 1)
  13678. (beginning-of-line 1))
  13679. (forward-char 1))))
  13680. (when org-noutline-p
  13681. (define-key org-mode-map "\C-a" 'org-beginning-of-line))
  13682. (defun org-invisible-p ()
  13683. "Check if point is at a character currently not visible."
  13684. (if org-noutline-p
  13685. ;; Early versions of noutline don't have `outline-invisible-p'.
  13686. (if (fboundp 'outline-invisible-p)
  13687. (outline-invisible-p)
  13688. (get-char-property (point) 'invisible))
  13689. (save-excursion
  13690. (skip-chars-backward "^\r\n")
  13691. (equal (char-before) ?\r))))
  13692. (defun org-invisible-p2 ()
  13693. "Check if point is at a character currently not visible."
  13694. (save-excursion
  13695. (if org-noutline-p
  13696. (progn
  13697. (if (and (eolp) (not (bobp))) (backward-char 1))
  13698. ;; Early versions of noutline don't have `outline-invisible-p'.
  13699. (if (fboundp 'outline-invisible-p)
  13700. (outline-invisible-p)
  13701. (get-char-property (point) 'invisible)))
  13702. (skip-chars-backward "^\r\n")
  13703. (equal (char-before) ?\r))))
  13704. (defun org-back-to-heading (&optional invisible-ok)
  13705. "Move to previous heading line, or beg of this line if it's a heading.
  13706. Only visible heading lines are considered, unless INVISIBLE-OK is non-nil."
  13707. (if org-noutline-p
  13708. (outline-back-to-heading invisible-ok)
  13709. (if (and (or (bobp) (memq (char-before) '(?\n ?\r)))
  13710. (looking-at outline-regexp))
  13711. t
  13712. (if (re-search-backward (concat (if invisible-ok "\\([\r\n]\\|^\\)" "^")
  13713. outline-regexp)
  13714. nil t)
  13715. (if invisible-ok
  13716. (progn (goto-char (or (match-end 1) (match-beginning 0)))
  13717. (looking-at outline-regexp)))
  13718. (error "Before first heading")))))
  13719. (defun org-on-heading-p (&optional invisible-ok)
  13720. "Return t if point is on a (visible) heading line.
  13721. If INVISIBLE-OK is non-nil, an invisible heading line is ok too."
  13722. (if org-noutline-p
  13723. (outline-on-heading-p 'invisible-ok)
  13724. (save-excursion
  13725. (skip-chars-backward "^\n\r")
  13726. (and (looking-at outline-regexp)
  13727. (or invisible-ok
  13728. (bobp)
  13729. (equal (char-before) ?\n))))))
  13730. (defun org-on-target-p ()
  13731. (let ((pos (point)))
  13732. (save-excursion
  13733. (skip-chars-forward "<")
  13734. (and (re-search-backward "<<" nil t)
  13735. (or (looking-at org-radio-target-regexp)
  13736. (looking-at org-target-regexp))
  13737. (<= (match-beginning 0) pos)
  13738. (>= (1+ (match-end 0)) pos)))))
  13739. (defun org-up-heading-all (arg)
  13740. "Move to the heading line of which the present line is a subheading.
  13741. This function considers both visible and invisible heading lines.
  13742. With argument, move up ARG levels."
  13743. (if org-noutline-p
  13744. (if (fboundp 'outline-up-heading-all)
  13745. (outline-up-heading-all arg) ; emacs 21 version of outline.el
  13746. (outline-up-heading arg t)) ; emacs 22 version of outline.el
  13747. (org-back-to-heading t)
  13748. (looking-at outline-regexp)
  13749. (if (<= (- (match-end 0) (match-beginning 0)) arg)
  13750. (error "Cannot move up %d levels" arg)
  13751. (re-search-backward
  13752. (concat "[\n\r]" (regexp-quote
  13753. (make-string (- (match-end 0) (match-beginning 0) arg)
  13754. ?*))
  13755. "[^*]"))
  13756. (forward-char 1))))
  13757. (defun org-show-hidden-entry ()
  13758. "Show an entry where even the heading is hidden."
  13759. (save-excursion
  13760. (if (not org-noutline-p)
  13761. (progn
  13762. (org-back-to-heading t)
  13763. (org-flag-heading nil)))
  13764. (org-show-entry)))
  13765. (defun org-check-occur-regexp (regexp)
  13766. "If REGEXP starts with \"^\", modify it to check for \\r as well.
  13767. Of course, only for the old outline mode."
  13768. (if org-noutline-p
  13769. regexp
  13770. (if (string-match "^\\^" regexp)
  13771. (concat "[\n\r]" (substring regexp 1))
  13772. regexp)))
  13773. (defun org-flag-heading (flag &optional entry)
  13774. "Flag the current heading. FLAG non-nil means make invisible.
  13775. When ENTRY is non-nil, show the entire entry."
  13776. (save-excursion
  13777. (org-back-to-heading t)
  13778. (if (not org-noutline-p)
  13779. ;; Make the current headline visible
  13780. (outline-flag-region (max 1 (1- (point))) (point) (if flag ?\r ?\n)))
  13781. ;; Check if we should show the entire entry
  13782. (if entry
  13783. (progn
  13784. (org-show-entry)
  13785. (save-excursion
  13786. (and (outline-next-heading)
  13787. (org-flag-heading nil))))
  13788. (outline-flag-region (max 1 (1- (point)))
  13789. (save-excursion (outline-end-of-heading) (point))
  13790. (if org-noutline-p
  13791. flag
  13792. (if flag ?\r ?\n))))))
  13793. (defun org-end-of-subtree (&optional invisible-OK)
  13794. ;; This is an exact copy of the original function, but it uses
  13795. ;; `org-back-to-heading', to make it work also in invisible
  13796. ;; trees. And is uses an invisible-OK argument.
  13797. ;; Under Emacs this is not needed, but the old outline.el needs this fix.
  13798. (org-back-to-heading invisible-OK)
  13799. (let ((first t)
  13800. (level (funcall outline-level)))
  13801. (while (and (not (eobp))
  13802. (or first (> (funcall outline-level) level)))
  13803. (setq first nil)
  13804. (outline-next-heading))
  13805. (if (memq (preceding-char) '(?\n ?\^M))
  13806. (progn
  13807. ;; Go to end of line before heading
  13808. (forward-char -1)
  13809. (if (memq (preceding-char) '(?\n ?\^M))
  13810. ;; leave blank line before heading
  13811. (forward-char -1))))))
  13812. (defun org-show-subtree ()
  13813. "Show everything after this heading at deeper levels."
  13814. (outline-flag-region
  13815. (point)
  13816. (save-excursion
  13817. (outline-end-of-subtree) (outline-next-heading) (point))
  13818. (if org-noutline-p nil ?\n)))
  13819. (defun org-show-entry ()
  13820. "Show the body directly following this heading.
  13821. Show the heading too, if it is currently invisible."
  13822. (interactive)
  13823. (save-excursion
  13824. (org-back-to-heading t)
  13825. (outline-flag-region
  13826. (max 1 (1- (point)))
  13827. (save-excursion
  13828. (re-search-forward (concat "[\r\n]\\(" outline-regexp "\\)") nil 'move)
  13829. (or (match-beginning 1) (point-max)))
  13830. (if org-noutline-p nil ?\n))))
  13831. (defun org-make-options-regexp (kwds)
  13832. "Make a regular expression for keyword lines."
  13833. (concat
  13834. (if org-noutline-p "^" "[\n\r]")
  13835. "#?[ \t]*\\+\\("
  13836. (mapconcat 'regexp-quote kwds "\\|")
  13837. "\\):[ \t]*"
  13838. (if org-noutline-p "\\(.+\\)" "\\([^\n\r]+\\)")))
  13839. ;; Make `bookmark-jump' show the jump location if it was hidden.
  13840. (eval-after-load "bookmark"
  13841. '(if (boundp 'bookmark-after-jump-hook)
  13842. ;; We can use the hook
  13843. (add-hook 'bookmark-after-jump-hook 'org-bookmark-jump-unhide)
  13844. ;; Hook not available, use advice
  13845. (defadvice bookmark-jump (after org-make-visible activate)
  13846. "Make the position visible."
  13847. (org-bookmark-jump-unhide))))
  13848. (defun org-bookmark-jump-unhide ()
  13849. "Unhide the current position, to show the bookmark location."
  13850. (and (eq major-mode 'org-mode)
  13851. (or (org-invisible-p)
  13852. (save-excursion (goto-char (max (point-min) (1- (point))))
  13853. (org-invisible-p)))
  13854. (org-show-hierarchy-above)))
  13855. ;;; Finish up
  13856. (provide 'org)
  13857. (run-hooks 'org-load-hook)
  13858. ;; arch-tag: e77da1a7-acc7-4336-b19e-efa25af3f9fd
  13859. ;;; org.el ends here