ox.el 242 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305
  1. ;;; ox.el --- Generic Export Engine for Org Mode
  2. ;; Copyright (C) 2012-2013 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;
  18. ;; This library implements a generic export engine for Org, built on
  19. ;; its syntactical parser: Org Elements.
  20. ;;
  21. ;; Besides that parser, the generic exporter is made of three distinct
  22. ;; parts:
  23. ;;
  24. ;; - The communication channel consists in a property list, which is
  25. ;; created and updated during the process. Its use is to offer
  26. ;; every piece of information, would it be about initial environment
  27. ;; or contextual data, all in a single place. The exhaustive list
  28. ;; of properties is given in "The Communication Channel" section of
  29. ;; this file.
  30. ;;
  31. ;; - The transcoder walks the parse tree, ignores or treat as plain
  32. ;; text elements and objects according to export options, and
  33. ;; eventually calls back-end specific functions to do the real
  34. ;; transcoding, concatenating their return value along the way.
  35. ;;
  36. ;; - The filter system is activated at the very beginning and the very
  37. ;; end of the export process, and each time an element or an object
  38. ;; has been converted. It is the entry point to fine-tune standard
  39. ;; output from back-end transcoders. See "The Filter System"
  40. ;; section for more information.
  41. ;;
  42. ;; The core function is `org-export-as'. It returns the transcoded
  43. ;; buffer as a string.
  44. ;;
  45. ;; An export back-end is defined with `org-export-define-backend'.
  46. ;; This function can also support specific buffer keywords, OPTION
  47. ;; keyword's items and filters. Refer to function's documentation for
  48. ;; more information.
  49. ;;
  50. ;; If the new back-end shares most properties with another one,
  51. ;; `org-export-define-derived-backend' can be used to simplify the
  52. ;; process.
  53. ;;
  54. ;; Any back-end can define its own variables. Among them, those
  55. ;; customizable should belong to the `org-export-BACKEND' group.
  56. ;;
  57. ;; Tools for common tasks across back-ends are implemented in the
  58. ;; following part of the file.
  59. ;;
  60. ;; Then, a wrapper macro for asynchronous export,
  61. ;; `org-export-async-start', along with tools to display results. are
  62. ;; given in the penultimate part.
  63. ;;
  64. ;; Eventually, a dispatcher (`org-export-dispatch') for standard
  65. ;; back-ends is provided in the last one.
  66. ;;; Code:
  67. (eval-when-compile (require 'cl))
  68. (require 'org-element)
  69. (require 'org-macro)
  70. (require 'ob-exp)
  71. (declare-function org-publish "ox-publish" (project &optional force async))
  72. (declare-function org-publish-all "ox-publish" (&optional force async))
  73. (declare-function
  74. org-publish-current-file "ox-publish" (&optional force async))
  75. (declare-function org-publish-current-project "ox-publish"
  76. (&optional force async))
  77. (defvar org-publish-project-alist)
  78. (defvar org-table-number-fraction)
  79. (defvar org-table-number-regexp)
  80. ;;; Internal Variables
  81. ;;
  82. ;; Among internal variables, the most important is
  83. ;; `org-export-options-alist'. This variable define the global export
  84. ;; options, shared between every exporter, and how they are acquired.
  85. (defconst org-export-max-depth 19
  86. "Maximum nesting depth for headlines, counting from 0.")
  87. (defconst org-export-options-alist
  88. '((:author "AUTHOR" nil user-full-name t)
  89. (:creator "CREATOR" nil org-export-creator-string)
  90. (:date "DATE" nil nil t)
  91. (:description "DESCRIPTION" nil nil newline)
  92. (:email "EMAIL" nil user-mail-address t)
  93. (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
  94. (:headline-levels nil "H" org-export-headline-levels)
  95. (:keywords "KEYWORDS" nil nil space)
  96. (:language "LANGUAGE" nil org-export-default-language t)
  97. (:preserve-breaks nil "\\n" org-export-preserve-breaks)
  98. (:section-numbers nil "num" org-export-with-section-numbers)
  99. (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
  100. (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
  101. (:title "TITLE" nil nil space)
  102. (:with-archived-trees nil "arch" org-export-with-archived-trees)
  103. (:with-author nil "author" org-export-with-author)
  104. (:with-clocks nil "c" org-export-with-clocks)
  105. (:with-creator nil "creator" org-export-with-creator)
  106. (:with-date nil "date" org-export-with-date)
  107. (:with-drawers nil "d" org-export-with-drawers)
  108. (:with-email nil "email" org-export-with-email)
  109. (:with-emphasize nil "*" org-export-with-emphasize)
  110. (:with-entities nil "e" org-export-with-entities)
  111. (:with-fixed-width nil ":" org-export-with-fixed-width)
  112. (:with-footnotes nil "f" org-export-with-footnotes)
  113. (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
  114. (:with-latex nil "tex" org-export-with-latex)
  115. (:with-planning nil "p" org-export-with-planning)
  116. (:with-priority nil "pri" org-export-with-priority)
  117. (:with-properties nil "prop" org-export-with-properties)
  118. (:with-smart-quotes nil "'" org-export-with-smart-quotes)
  119. (:with-special-strings nil "-" org-export-with-special-strings)
  120. (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
  121. (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
  122. (:with-toc nil "toc" org-export-with-toc)
  123. (:with-tables nil "|" org-export-with-tables)
  124. (:with-tags nil "tags" org-export-with-tags)
  125. (:with-tasks nil "tasks" org-export-with-tasks)
  126. (:with-timestamps nil "<" org-export-with-timestamps)
  127. (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
  128. "Alist between export properties and ways to set them.
  129. The CAR of the alist is the property name, and the CDR is a list
  130. like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
  131. KEYWORD is a string representing a buffer keyword, or nil. Each
  132. property defined this way can also be set, during subtree
  133. export, through a headline property named after the keyword
  134. with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
  135. property).
  136. OPTION is a string that could be found in an #+OPTIONS: line.
  137. DEFAULT is the default value for the property.
  138. BEHAVIOUR determines how Org should handle multiple keywords for
  139. the same property. It is a symbol among:
  140. nil Keep old value and discard the new one.
  141. t Replace old value with the new one.
  142. `space' Concatenate the values, separating them with a space.
  143. `newline' Concatenate the values, separating them with
  144. a newline.
  145. `split' Split values at white spaces, and cons them to the
  146. previous list.
  147. Values set through KEYWORD and OPTION have precedence over
  148. DEFAULT.
  149. All these properties should be back-end agnostic. Back-end
  150. specific properties are set through `org-export-define-backend'.
  151. Properties redefined there have precedence over these.")
  152. (defconst org-export-special-keywords '("FILETAGS" "SETUPFILE" "OPTIONS")
  153. "List of in-buffer keywords that require special treatment.
  154. These keywords are not directly associated to a property. The
  155. way they are handled must be hard-coded into
  156. `org-export--get-inbuffer-options' function.")
  157. (defconst org-export-filters-alist
  158. '((:filter-bold . org-export-filter-bold-functions)
  159. (:filter-babel-call . org-export-filter-babel-call-functions)
  160. (:filter-center-block . org-export-filter-center-block-functions)
  161. (:filter-clock . org-export-filter-clock-functions)
  162. (:filter-code . org-export-filter-code-functions)
  163. (:filter-comment . org-export-filter-comment-functions)
  164. (:filter-comment-block . org-export-filter-comment-block-functions)
  165. (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
  166. (:filter-drawer . org-export-filter-drawer-functions)
  167. (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
  168. (:filter-entity . org-export-filter-entity-functions)
  169. (:filter-example-block . org-export-filter-example-block-functions)
  170. (:filter-export-block . org-export-filter-export-block-functions)
  171. (:filter-export-snippet . org-export-filter-export-snippet-functions)
  172. (:filter-final-output . org-export-filter-final-output-functions)
  173. (:filter-fixed-width . org-export-filter-fixed-width-functions)
  174. (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
  175. (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
  176. (:filter-headline . org-export-filter-headline-functions)
  177. (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
  178. (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
  179. (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
  180. (:filter-inlinetask . org-export-filter-inlinetask-functions)
  181. (:filter-italic . org-export-filter-italic-functions)
  182. (:filter-item . org-export-filter-item-functions)
  183. (:filter-keyword . org-export-filter-keyword-functions)
  184. (:filter-latex-environment . org-export-filter-latex-environment-functions)
  185. (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
  186. (:filter-line-break . org-export-filter-line-break-functions)
  187. (:filter-link . org-export-filter-link-functions)
  188. (:filter-node-property . org-export-filter-node-property-functions)
  189. (:filter-options . org-export-filter-options-functions)
  190. (:filter-paragraph . org-export-filter-paragraph-functions)
  191. (:filter-parse-tree . org-export-filter-parse-tree-functions)
  192. (:filter-plain-list . org-export-filter-plain-list-functions)
  193. (:filter-plain-text . org-export-filter-plain-text-functions)
  194. (:filter-planning . org-export-filter-planning-functions)
  195. (:filter-property-drawer . org-export-filter-property-drawer-functions)
  196. (:filter-quote-block . org-export-filter-quote-block-functions)
  197. (:filter-quote-section . org-export-filter-quote-section-functions)
  198. (:filter-radio-target . org-export-filter-radio-target-functions)
  199. (:filter-section . org-export-filter-section-functions)
  200. (:filter-special-block . org-export-filter-special-block-functions)
  201. (:filter-src-block . org-export-filter-src-block-functions)
  202. (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
  203. (:filter-strike-through . org-export-filter-strike-through-functions)
  204. (:filter-subscript . org-export-filter-subscript-functions)
  205. (:filter-superscript . org-export-filter-superscript-functions)
  206. (:filter-table . org-export-filter-table-functions)
  207. (:filter-table-cell . org-export-filter-table-cell-functions)
  208. (:filter-table-row . org-export-filter-table-row-functions)
  209. (:filter-target . org-export-filter-target-functions)
  210. (:filter-timestamp . org-export-filter-timestamp-functions)
  211. (:filter-underline . org-export-filter-underline-functions)
  212. (:filter-verbatim . org-export-filter-verbatim-functions)
  213. (:filter-verse-block . org-export-filter-verse-block-functions))
  214. "Alist between filters properties and initial values.
  215. The key of each association is a property name accessible through
  216. the communication channel. Its value is a configurable global
  217. variable defining initial filters.
  218. This list is meant to install user specified filters. Back-end
  219. developers may install their own filters using
  220. `org-export-define-backend'. Filters defined there will always
  221. be prepended to the current list, so they always get applied
  222. first.")
  223. (defconst org-export-default-inline-image-rule
  224. `(("file" .
  225. ,(format "\\.%s\\'"
  226. (regexp-opt
  227. '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
  228. "xpm" "pbm" "pgm" "ppm") t))))
  229. "Default rule for link matching an inline image.
  230. This rule applies to links with no description. By default, it
  231. will be considered as an inline image if it targets a local file
  232. whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
  233. \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
  234. See `org-export-inline-image-p' for more information about
  235. rules.")
  236. (defvar org-export-async-debug nil
  237. "Non-nil means asynchronous export process should leave data behind.
  238. This data is found in the appropriate \"*Org Export Process*\"
  239. buffer, and in files prefixed with \"org-export-process\" and
  240. located in `temporary-file-directory'.
  241. When non-nil, it will also set `debug-on-error' to a non-nil
  242. value in the external process.")
  243. (defvar org-export-stack-contents nil
  244. "Record asynchronously generated export results and processes.
  245. This is an alist: its CAR is the source of the
  246. result (destination file or buffer for a finished process,
  247. original buffer for a running one) and its CDR is a list
  248. containing the back-end used, as a symbol, and either a process
  249. or the time at which it finished. It is used to build the menu
  250. from `org-export-stack'.")
  251. (defvar org-export--registered-backends nil
  252. "List of backends currently available in the exporter.
  253. This variable is set with `org-export-define-backend' and
  254. `org-export-define-derived-backend' functions.")
  255. (defvar org-export-dispatch-last-action nil
  256. "Last command called from the dispatcher.
  257. The value should be a list. Its CAR is the action, as a symbol,
  258. and its CDR is a list of export options.")
  259. (defvar org-export-dispatch-last-position (make-marker)
  260. "The position where the last export command was created using the dispatcher.
  261. This marker will be used with `C-u C-c C-e' to make sure export repetition
  262. uses the same subtree if the previous command was restricted to a subtree.")
  263. ;; For compatibility with Org < 8
  264. (defvar org-export-current-backend nil
  265. "Name, if any, of the back-end used during an export process.
  266. Its value is a symbol such as `html', `latex', `ascii', or nil if
  267. the back-end is anonymous (see `org-export-create-backend') or if
  268. there is no export process in progress.
  269. It can be used to teach Babel blocks how to act differently
  270. according to the back-end used.")
  271. ;;; User-configurable Variables
  272. ;;
  273. ;; Configuration for the masses.
  274. ;;
  275. ;; They should never be accessed directly, as their value is to be
  276. ;; stored in a property list (cf. `org-export-options-alist').
  277. ;; Back-ends will read their value from there instead.
  278. (defgroup org-export nil
  279. "Options for exporting Org mode files."
  280. :tag "Org Export"
  281. :group 'org)
  282. (defgroup org-export-general nil
  283. "General options for export engine."
  284. :tag "Org Export General"
  285. :group 'org-export)
  286. (defcustom org-export-with-archived-trees 'headline
  287. "Whether sub-trees with the ARCHIVE tag should be exported.
  288. This can have three different values:
  289. nil Do not export, pretend this tree is not present.
  290. t Do export the entire tree.
  291. `headline' Only export the headline, but skip the tree below it.
  292. This option can also be set with the OPTIONS keyword,
  293. e.g. \"arch:nil\"."
  294. :group 'org-export-general
  295. :type '(choice
  296. (const :tag "Not at all" nil)
  297. (const :tag "Headline only" headline)
  298. (const :tag "Entirely" t)))
  299. (defcustom org-export-with-author t
  300. "Non-nil means insert author name into the exported file.
  301. This option can also be set with the OPTIONS keyword,
  302. e.g. \"author:nil\"."
  303. :group 'org-export-general
  304. :type 'boolean)
  305. (defcustom org-export-with-clocks nil
  306. "Non-nil means export CLOCK keywords.
  307. This option can also be set with the OPTIONS keyword,
  308. e.g. \"c:t\"."
  309. :group 'org-export-general
  310. :type 'boolean)
  311. (defcustom org-export-with-creator 'comment
  312. "Non-nil means the postamble should contain a creator sentence.
  313. The sentence can be set in `org-export-creator-string' and
  314. defaults to \"Generated by Org mode XX in Emacs XXX.\".
  315. If the value is `comment' insert it as a comment."
  316. :group 'org-export-general
  317. :type '(choice
  318. (const :tag "No creator sentence" nil)
  319. (const :tag "Sentence as a comment" 'comment)
  320. (const :tag "Insert the sentence" t)))
  321. (defcustom org-export-with-date t
  322. "Non-nil means insert date in the exported document.
  323. This option can also be set with the OPTIONS keyword,
  324. e.g. \"date:nil\"."
  325. :group 'org-export-general
  326. :type 'boolean)
  327. (defcustom org-export-date-timestamp-format nil
  328. "Time-stamp format string to use for DATE keyword.
  329. The format string, when specified, only applies if date consists
  330. in a single time-stamp. Otherwise its value will be ignored.
  331. See `format-time-string' for details on how to build this
  332. string."
  333. :group 'org-export-general
  334. :type '(choice
  335. (string :tag "Time-stamp format string")
  336. (const :tag "No format string" nil)))
  337. (defcustom org-export-creator-string
  338. (format "Emacs %s (Org mode %s)"
  339. emacs-version
  340. (if (fboundp 'org-version) (org-version) "unknown version"))
  341. "Information about the creator of the document.
  342. This option can also be set on with the CREATOR keyword."
  343. :group 'org-export-general
  344. :type '(string :tag "Creator string"))
  345. (defcustom org-export-with-drawers '(not "LOGBOOK")
  346. "Non-nil means export contents of standard drawers.
  347. When t, all drawers are exported. This may also be a list of
  348. drawer names to export, as strings. If that list starts with
  349. `not', only drawers with such names will be ignored.
  350. This variable doesn't apply to properties drawers. See
  351. `org-export-with-properties' instead.
  352. This option can also be set with the OPTIONS keyword,
  353. e.g. \"d:nil\"."
  354. :group 'org-export-general
  355. :version "24.4"
  356. :package-version '(Org . "8.0")
  357. :type '(choice
  358. (const :tag "All drawers" t)
  359. (const :tag "None" nil)
  360. (repeat :tag "Selected drawers"
  361. (string :tag "Drawer name"))
  362. (list :tag "Ignored drawers"
  363. (const :format "" not)
  364. (repeat :tag "Specify names of drawers to ignore during export"
  365. :inline t
  366. (string :tag "Drawer name")))))
  367. (defcustom org-export-with-email nil
  368. "Non-nil means insert author email into the exported file.
  369. This option can also be set with the OPTIONS keyword,
  370. e.g. \"email:t\"."
  371. :group 'org-export-general
  372. :type 'boolean)
  373. (defcustom org-export-with-emphasize t
  374. "Non-nil means interpret *word*, /word/, _word_ and +word+.
  375. If the export target supports emphasizing text, the word will be
  376. typeset in bold, italic, with an underline or strike-through,
  377. respectively.
  378. This option can also be set with the OPTIONS keyword,
  379. e.g. \"*:nil\"."
  380. :group 'org-export-general
  381. :type 'boolean)
  382. (defcustom org-export-exclude-tags '("noexport")
  383. "Tags that exclude a tree from export.
  384. All trees carrying any of these tags will be excluded from
  385. export. This is without condition, so even subtrees inside that
  386. carry one of the `org-export-select-tags' will be removed.
  387. This option can also be set with the EXCLUDE_TAGS keyword."
  388. :group 'org-export-general
  389. :type '(repeat (string :tag "Tag")))
  390. (defcustom org-export-with-fixed-width t
  391. "Non-nil means lines starting with \":\" will be in fixed width font.
  392. This can be used to have pre-formatted text, fragments of code
  393. etc. For example:
  394. : ;; Some Lisp examples
  395. : (while (defc cnt)
  396. : (ding))
  397. will be looking just like this in also HTML. See also the QUOTE
  398. keyword. Not all export backends support this.
  399. This option can also be set with the OPTIONS keyword,
  400. e.g. \"::nil\"."
  401. :group 'org-export-general
  402. :type 'boolean)
  403. (defcustom org-export-with-footnotes t
  404. "Non-nil means Org footnotes should be exported.
  405. This option can also be set with the OPTIONS keyword,
  406. e.g. \"f:nil\"."
  407. :group 'org-export-general
  408. :type 'boolean)
  409. (defcustom org-export-with-latex t
  410. "Non-nil means process LaTeX environments and fragments.
  411. This option can also be set with the OPTIONS line,
  412. e.g. \"tex:verbatim\". Allowed values are:
  413. nil Ignore math snippets.
  414. `verbatim' Keep everything in verbatim.
  415. t Allow export of math snippets."
  416. :group 'org-export-general
  417. :version "24.4"
  418. :package-version '(Org . "8.0")
  419. :type '(choice
  420. (const :tag "Do not process math in any way" nil)
  421. (const :tag "Interpret math snippets" t)
  422. (const :tag "Leave math verbatim" verbatim)))
  423. (defcustom org-export-headline-levels 3
  424. "The last level which is still exported as a headline.
  425. Inferior levels will usually produce itemize or enumerate lists
  426. when exported, but back-end behaviour may differ.
  427. This option can also be set with the OPTIONS keyword,
  428. e.g. \"H:2\"."
  429. :group 'org-export-general
  430. :type 'integer)
  431. (defcustom org-export-default-language "en"
  432. "The default language for export and clocktable translations, as a string.
  433. This may have an association in
  434. `org-clock-clocktable-language-setup',
  435. `org-export-smart-quotes-alist' and `org-export-dictionary'.
  436. This option can also be set with the LANGUAGE keyword."
  437. :group 'org-export-general
  438. :type '(string :tag "Language"))
  439. (defcustom org-export-preserve-breaks nil
  440. "Non-nil means preserve all line breaks when exporting.
  441. This option can also be set with the OPTIONS keyword,
  442. e.g. \"\\n:t\"."
  443. :group 'org-export-general
  444. :type 'boolean)
  445. (defcustom org-export-with-entities t
  446. "Non-nil means interpret entities when exporting.
  447. For example, HTML export converts \\alpha to &alpha; and \\AA to
  448. &Aring;.
  449. For a list of supported names, see the constant `org-entities'
  450. and the user option `org-entities-user'.
  451. This option can also be set with the OPTIONS keyword,
  452. e.g. \"e:nil\"."
  453. :group 'org-export-general
  454. :type 'boolean)
  455. (defcustom org-export-with-inlinetasks t
  456. "Non-nil means inlinetasks should be exported.
  457. This option can also be set with the OPTIONS keyword,
  458. e.g. \"inline:nil\"."
  459. :group 'org-export-general
  460. :version "24.4"
  461. :package-version '(Org . "8.0")
  462. :type 'boolean)
  463. (defcustom org-export-with-planning nil
  464. "Non-nil means include planning info in export.
  465. Planning info is the line containing either SCHEDULED:,
  466. DEADLINE:, CLOSED: time-stamps, or a combination of them.
  467. This option can also be set with the OPTIONS keyword,
  468. e.g. \"p:t\"."
  469. :group 'org-export-general
  470. :version "24.4"
  471. :package-version '(Org . "8.0")
  472. :type 'boolean)
  473. (defcustom org-export-with-priority nil
  474. "Non-nil means include priority cookies in export.
  475. This option can also be set with the OPTIONS keyword,
  476. e.g. \"pri:t\"."
  477. :group 'org-export-general
  478. :type 'boolean)
  479. (defcustom org-export-with-properties nil
  480. "Non-nil means export contents of properties drawers.
  481. When t, all properties are exported. This may also be a list of
  482. properties to export, as strings.
  483. This option can also be set with the OPTIONS keyword,
  484. e.g. \"prop:t\"."
  485. :group 'org-export-general
  486. :version "24.4"
  487. :package-version '(Org . "8.3")
  488. :type '(choice
  489. (const :tag "All properties" t)
  490. (const :tag "None" nil)
  491. (repeat :tag "Selected properties"
  492. (string :tag "Property name"))))
  493. (defcustom org-export-with-section-numbers t
  494. "Non-nil means add section numbers to headlines when exporting.
  495. When set to an integer n, numbering will only happen for
  496. headlines whose relative level is higher or equal to n.
  497. This option can also be set with the OPTIONS keyword,
  498. e.g. \"num:t\"."
  499. :group 'org-export-general
  500. :type 'boolean)
  501. (defcustom org-export-select-tags '("export")
  502. "Tags that select a tree for export.
  503. If any such tag is found in a buffer, all trees that do not carry
  504. one of these tags will be ignored during export. Inside trees
  505. that are selected like this, you can still deselect a subtree by
  506. tagging it with one of the `org-export-exclude-tags'.
  507. This option can also be set with the SELECT_TAGS keyword."
  508. :group 'org-export-general
  509. :type '(repeat (string :tag "Tag")))
  510. (defcustom org-export-with-smart-quotes nil
  511. "Non-nil means activate smart quotes during export.
  512. This option can also be set with the OPTIONS keyword,
  513. e.g., \"':t\".
  514. When setting this to non-nil, you need to take care of
  515. using the correct Babel package when exporting to LaTeX.
  516. E.g., you can load Babel for french like this:
  517. #+LATEX_HEADER: \\usepackage[french]{babel}"
  518. :group 'org-export-general
  519. :version "24.4"
  520. :package-version '(Org . "8.0")
  521. :type 'boolean)
  522. (defcustom org-export-with-special-strings t
  523. "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
  524. When this option is turned on, these strings will be exported as:
  525. Org HTML LaTeX UTF-8
  526. -----+----------+--------+-------
  527. \\- &shy; \\-
  528. -- &ndash; -- –
  529. --- &mdash; --- —
  530. ... &hellip; \\ldots …
  531. This option can also be set with the OPTIONS keyword,
  532. e.g. \"-:nil\"."
  533. :group 'org-export-general
  534. :type 'boolean)
  535. (defcustom org-export-with-statistics-cookies t
  536. "Non-nil means include statistics cookies in export.
  537. This option can also be set with the OPTIONS keyword,
  538. e.g. \"stat:nil\""
  539. :group 'org-export-general
  540. :version "24.4"
  541. :package-version '(Org . "8.0")
  542. :type 'boolean)
  543. (defcustom org-export-with-sub-superscripts t
  544. "Non-nil means interpret \"_\" and \"^\" for export.
  545. When this option is turned on, you can use TeX-like syntax for
  546. sub- and superscripts. Several characters after \"_\" or \"^\"
  547. will be considered as a single item - so grouping with {} is
  548. normally not needed. For example, the following things will be
  549. parsed as single sub- or superscripts.
  550. 10^24 or 10^tau several digits will be considered 1 item.
  551. 10^-12 or 10^-tau a leading sign with digits or a word
  552. x^2-y^3 will be read as x^2 - y^3, because items are
  553. terminated by almost any nonword/nondigit char.
  554. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  555. Still, ambiguity is possible - so when in doubt use {} to enclose
  556. the sub/superscript. If you set this variable to the symbol
  557. `{}', the braces are *required* in order to trigger
  558. interpretations as sub/superscript. This can be helpful in
  559. documents that need \"_\" frequently in plain text.
  560. This option can also be set with the OPTIONS keyword,
  561. e.g. \"^:nil\"."
  562. :group 'org-export-general
  563. :type '(choice
  564. (const :tag "Interpret them" t)
  565. (const :tag "Curly brackets only" {})
  566. (const :tag "Do not interpret them" nil)))
  567. (defcustom org-export-with-toc t
  568. "Non-nil means create a table of contents in exported files.
  569. The TOC contains headlines with levels up
  570. to`org-export-headline-levels'. When an integer, include levels
  571. up to N in the toc, this may then be different from
  572. `org-export-headline-levels', but it will not be allowed to be
  573. larger than the number of headline levels. When nil, no table of
  574. contents is made.
  575. This option can also be set with the OPTIONS keyword,
  576. e.g. \"toc:nil\" or \"toc:3\"."
  577. :group 'org-export-general
  578. :type '(choice
  579. (const :tag "No Table of Contents" nil)
  580. (const :tag "Full Table of Contents" t)
  581. (integer :tag "TOC to level")))
  582. (defcustom org-export-with-tables t
  583. "If non-nil, lines starting with \"|\" define a table.
  584. For example:
  585. | Name | Address | Birthday |
  586. |-------------+----------+-----------|
  587. | Arthur Dent | England | 29.2.2100 |
  588. This option can also be set with the OPTIONS keyword,
  589. e.g. \"|:nil\"."
  590. :group 'org-export-general
  591. :type 'boolean)
  592. (defcustom org-export-with-tags t
  593. "If nil, do not export tags, just remove them from headlines.
  594. If this is the symbol `not-in-toc', tags will be removed from
  595. table of contents entries, but still be shown in the headlines of
  596. the document.
  597. This option can also be set with the OPTIONS keyword,
  598. e.g. \"tags:nil\"."
  599. :group 'org-export-general
  600. :type '(choice
  601. (const :tag "Off" nil)
  602. (const :tag "Not in TOC" not-in-toc)
  603. (const :tag "On" t)))
  604. (defcustom org-export-with-tasks t
  605. "Non-nil means include TODO items for export.
  606. This may have the following values:
  607. t include tasks independent of state.
  608. `todo' include only tasks that are not yet done.
  609. `done' include only tasks that are already done.
  610. nil ignore all tasks.
  611. list of keywords include tasks with these keywords.
  612. This option can also be set with the OPTIONS keyword,
  613. e.g. \"tasks:nil\"."
  614. :group 'org-export-general
  615. :type '(choice
  616. (const :tag "All tasks" t)
  617. (const :tag "No tasks" nil)
  618. (const :tag "Not-done tasks" todo)
  619. (const :tag "Only done tasks" done)
  620. (repeat :tag "Specific TODO keywords"
  621. (string :tag "Keyword"))))
  622. (defcustom org-export-time-stamp-file t
  623. "Non-nil means insert a time stamp into the exported file.
  624. The time stamp shows when the file was created. This option can
  625. also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
  626. :group 'org-export-general
  627. :type 'boolean)
  628. (defcustom org-export-with-timestamps t
  629. "Non nil means allow timestamps in export.
  630. It can be set to any of the following values:
  631. t export all timestamps.
  632. `active' export active timestamps only.
  633. `inactive' export inactive timestamps only.
  634. nil do not export timestamps
  635. This only applies to timestamps isolated in a paragraph
  636. containing only timestamps. Other timestamps are always
  637. exported.
  638. This option can also be set with the OPTIONS keyword, e.g.
  639. \"<:nil\"."
  640. :group 'org-export-general
  641. :type '(choice
  642. (const :tag "All timestamps" t)
  643. (const :tag "Only active timestamps" active)
  644. (const :tag "Only inactive timestamps" inactive)
  645. (const :tag "No timestamp" nil)))
  646. (defcustom org-export-with-todo-keywords t
  647. "Non-nil means include TODO keywords in export.
  648. When nil, remove all these keywords from the export. This option
  649. can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
  650. :group 'org-export-general
  651. :type 'boolean)
  652. (defcustom org-export-allow-bind-keywords nil
  653. "Non-nil means BIND keywords can define local variable values.
  654. This is a potential security risk, which is why the default value
  655. is nil. You can also allow them through local buffer variables."
  656. :group 'org-export-general
  657. :version "24.4"
  658. :package-version '(Org . "8.0")
  659. :type 'boolean)
  660. (defcustom org-export-snippet-translation-alist nil
  661. "Alist between export snippets back-ends and exporter back-ends.
  662. This variable allows to provide shortcuts for export snippets.
  663. For example, with a value of '\(\(\"h\" . \"html\"\)\), the
  664. HTML back-end will recognize the contents of \"@@h:<b>@@\" as
  665. HTML code while every other back-end will ignore it."
  666. :group 'org-export-general
  667. :version "24.4"
  668. :package-version '(Org . "8.0")
  669. :type '(repeat
  670. (cons (string :tag "Shortcut")
  671. (string :tag "Back-end"))))
  672. (defcustom org-export-coding-system nil
  673. "Coding system for the exported file."
  674. :group 'org-export-general
  675. :version "24.4"
  676. :package-version '(Org . "8.0")
  677. :type 'coding-system)
  678. (defcustom org-export-copy-to-kill-ring 'if-interactive
  679. "Should we push exported content to the kill ring?"
  680. :group 'org-export-general
  681. :version "24.3"
  682. :type '(choice
  683. (const :tag "Always" t)
  684. (const :tag "When export is done interactively" if-interactive)
  685. (const :tag "Never" nil)))
  686. (defcustom org-export-initial-scope 'buffer
  687. "The initial scope when exporting with `org-export-dispatch'.
  688. This variable can be either set to `buffer' or `subtree'."
  689. :group 'org-export-general
  690. :type '(choice
  691. (const :tag "Export current buffer" buffer)
  692. (const :tag "Export current subtree" subtree)))
  693. (defcustom org-export-show-temporary-export-buffer t
  694. "Non-nil means show buffer after exporting to temp buffer.
  695. When Org exports to a file, the buffer visiting that file is ever
  696. shown, but remains buried. However, when exporting to
  697. a temporary buffer, that buffer is popped up in a second window.
  698. When this variable is nil, the buffer remains buried also in
  699. these cases."
  700. :group 'org-export-general
  701. :type 'boolean)
  702. (defcustom org-export-in-background nil
  703. "Non-nil means export and publishing commands will run in background.
  704. Results from an asynchronous export are never displayed
  705. automatically. But you can retrieve them with \\[org-export-stack]."
  706. :group 'org-export-general
  707. :version "24.4"
  708. :package-version '(Org . "8.0")
  709. :type 'boolean)
  710. (defcustom org-export-async-init-file nil
  711. "File used to initialize external export process.
  712. Value must be either nil or an absolute file name. When nil, the
  713. external process is launched like a regular Emacs session,
  714. loading user's initialization file and any site specific
  715. configuration. If a file is provided, it, and only it, is loaded
  716. at start-up.
  717. Therefore, using a specific configuration makes the process to
  718. load faster and the export more portable."
  719. :group 'org-export-general
  720. :version "24.4"
  721. :package-version '(Org . "8.0")
  722. :type '(choice
  723. (const :tag "Regular startup" nil)
  724. (file :tag "Specific start-up file" :must-match t)))
  725. (defcustom org-export-dispatch-use-expert-ui nil
  726. "Non-nil means using a non-intrusive `org-export-dispatch'.
  727. In that case, no help buffer is displayed. Though, an indicator
  728. for current export scope is added to the prompt (\"b\" when
  729. output is restricted to body only, \"s\" when it is restricted to
  730. the current subtree, \"v\" when only visible elements are
  731. considered for export, \"f\" when publishing functions should be
  732. passed the FORCE argument and \"a\" when the export should be
  733. asynchronous). Also, \[?] allows to switch back to standard
  734. mode."
  735. :group 'org-export-general
  736. :version "24.4"
  737. :package-version '(Org . "8.0")
  738. :type 'boolean)
  739. ;;; Defining Back-ends
  740. ;;
  741. ;; An export back-end is a structure with `org-export-backend' type
  742. ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
  743. ;; and `menu' slots.
  744. ;;
  745. ;; At the lowest level, a back-end is created with
  746. ;; `org-export-create-backend' function.
  747. ;;
  748. ;; A named back-end can be registered with
  749. ;; `org-export-register-backend' function. A registered back-end can
  750. ;; later be referred to by its name, with `org-export-get-backend'
  751. ;; function. Also, such a back-end can become the parent of a derived
  752. ;; back-end from which slot values will be inherited by default.
  753. ;; `org-export-derived-backend-p' can check if a given back-end is
  754. ;; derived from a list of back-end names.
  755. ;;
  756. ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
  757. ;; `org-export-get-all-filters' return the full alist of transcoders,
  758. ;; options and filters, including those inherited from ancestors.
  759. ;;
  760. ;; At a higher level, `org-export-define-backend' is the standard way
  761. ;; to define an export back-end. If the new back-end is similar to
  762. ;; a registered back-end, `org-export-define-derived-backend' may be
  763. ;; used instead.
  764. ;;
  765. ;; Eventually `org-export-barf-if-invalid-backend' returns an error
  766. ;; when a given back-end hasn't been registered yet.
  767. (defstruct (org-export-backend (:constructor org-export-create-backend)
  768. (:copier nil))
  769. name parent transcoders options filters blocks menu)
  770. (defun org-export-get-backend (name)
  771. "Return export back-end named after NAME.
  772. NAME is a symbol. Return nil if no such back-end is found."
  773. (catch 'found
  774. (dolist (b org-export--registered-backends)
  775. (when (eq (org-export-backend-name b) name)
  776. (throw 'found b)))))
  777. (defun org-export-register-backend (backend)
  778. "Register BACKEND as a known export back-end.
  779. BACKEND is a structure with `org-export-backend' type."
  780. ;; Refuse to register an unnamed back-end.
  781. (unless (org-export-backend-name backend)
  782. (error "Cannot register a unnamed export back-end"))
  783. ;; Refuse to register a back-end with an unknown parent.
  784. (let ((parent (org-export-backend-parent backend)))
  785. (when (and parent (not (org-export-get-backend parent)))
  786. (error "Cannot use unknown \"%s\" back-end as a parent" parent)))
  787. ;; Register dedicated export blocks in the parser.
  788. (dolist (name (org-export-backend-blocks backend))
  789. (add-to-list 'org-element-block-name-alist
  790. (cons name 'org-element-export-block-parser)))
  791. ;; If a back-end with the same name as BACKEND is already
  792. ;; registered, replace it with BACKEND. Otherwise, simply add
  793. ;; BACKEND to the list of registered back-ends.
  794. (let ((old (org-export-get-backend (org-export-backend-name backend))))
  795. (if old (setcar (memq old org-export--registered-backends) backend)
  796. (push backend org-export--registered-backends))))
  797. (defun org-export-barf-if-invalid-backend (backend)
  798. "Signal an error if BACKEND isn't defined."
  799. (unless (org-export-backend-p backend)
  800. (error "Unknown \"%s\" back-end: Aborting export" backend)))
  801. (defun org-export-derived-backend-p (backend &rest backends)
  802. "Non-nil if BACKEND is derived from one of BACKENDS.
  803. BACKEND is an export back-end, as returned by, e.g.,
  804. `org-export-create-backend', or a symbol referring to
  805. a registered back-end. BACKENDS is constituted of symbols."
  806. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  807. (when backend
  808. (catch 'exit
  809. (while (org-export-backend-parent backend)
  810. (when (memq (org-export-backend-name backend) backends)
  811. (throw 'exit t))
  812. (setq backend
  813. (org-export-get-backend (org-export-backend-parent backend))))
  814. (memq (org-export-backend-name backend) backends))))
  815. (defun org-export-get-all-transcoders (backend)
  816. "Return full translation table for BACKEND.
  817. BACKEND is an export back-end, as return by, e.g,,
  818. `org-export-create-backend'. Return value is an alist where
  819. keys are element or object types, as symbols, and values are
  820. transcoders.
  821. Unlike to `org-export-backend-transcoders', this function
  822. also returns transcoders inherited from parent back-ends,
  823. if any."
  824. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  825. (when backend
  826. (let ((transcoders (org-export-backend-transcoders backend))
  827. parent)
  828. (while (setq parent (org-export-backend-parent backend))
  829. (setq backend (org-export-get-backend parent))
  830. (setq transcoders
  831. (append transcoders (org-export-backend-transcoders backend))))
  832. transcoders)))
  833. (defun org-export-get-all-options (backend)
  834. "Return export options for BACKEND.
  835. BACKEND is an export back-end, as return by, e.g,,
  836. `org-export-create-backend'. See `org-export-options-alist'
  837. for the shape of the return value.
  838. Unlike to `org-export-backend-options', this function also
  839. returns options inherited from parent back-ends, if any."
  840. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  841. (when backend
  842. (let ((options (org-export-backend-options backend))
  843. parent)
  844. (while (setq parent (org-export-backend-parent backend))
  845. (setq backend (org-export-get-backend parent))
  846. (setq options (append options (org-export-backend-options backend))))
  847. options)))
  848. (defun org-export-get-all-filters (backend)
  849. "Return complete list of filters for BACKEND.
  850. BACKEND is an export back-end, as return by, e.g,,
  851. `org-export-create-backend'. Return value is an alist where
  852. keys are symbols and values lists of functions.
  853. Unlike to `org-export-backend-filters', this function also
  854. returns filters inherited from parent back-ends, if any."
  855. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  856. (when backend
  857. (let ((filters (org-export-backend-filters backend))
  858. parent)
  859. (while (setq parent (org-export-backend-parent backend))
  860. (setq backend (org-export-get-backend parent))
  861. (setq filters (append filters (org-export-backend-filters backend))))
  862. filters)))
  863. (defun org-export-define-backend (backend transcoders &rest body)
  864. "Define a new back-end BACKEND.
  865. TRANSCODERS is an alist between object or element types and
  866. functions handling them.
  867. These functions should return a string without any trailing
  868. space, or nil. They must accept three arguments: the object or
  869. element itself, its contents or nil when it isn't recursive and
  870. the property list used as a communication channel.
  871. Contents, when not nil, are stripped from any global indentation
  872. \(although the relative one is preserved). They also always end
  873. with a single newline character.
  874. If, for a given type, no function is found, that element or
  875. object type will simply be ignored, along with any blank line or
  876. white space at its end. The same will happen if the function
  877. returns the nil value. If that function returns the empty
  878. string, the type will be ignored, but the blank lines or white
  879. spaces will be kept.
  880. In addition to element and object types, one function can be
  881. associated to the `template' (or `inner-template') symbol and
  882. another one to the `plain-text' symbol.
  883. The former returns the final transcoded string, and can be used
  884. to add a preamble and a postamble to document's body. It must
  885. accept two arguments: the transcoded string and the property list
  886. containing export options. A function associated to `template'
  887. will not be applied if export has option \"body-only\".
  888. A function associated to `inner-template' is always applied.
  889. The latter, when defined, is to be called on every text not
  890. recognized as an element or an object. It must accept two
  891. arguments: the text string and the information channel. It is an
  892. appropriate place to protect special chars relative to the
  893. back-end.
  894. BODY can start with pre-defined keyword arguments. The following
  895. keywords are understood:
  896. :export-block
  897. String, or list of strings, representing block names that
  898. will not be parsed. This is used to specify blocks that will
  899. contain raw code specific to the back-end. These blocks
  900. still have to be handled by the relative `export-block' type
  901. translator.
  902. :filters-alist
  903. Alist between filters and function, or list of functions,
  904. specific to the back-end. See `org-export-filters-alist' for
  905. a list of all allowed filters. Filters defined here
  906. shouldn't make a back-end test, as it may prevent back-ends
  907. derived from this one to behave properly.
  908. :menu-entry
  909. Menu entry for the export dispatcher. It should be a list
  910. like:
  911. '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
  912. where :
  913. KEY is a free character selecting the back-end.
  914. DESCRIPTION-OR-ORDINAL is either a string or a number.
  915. If it is a string, is will be used to name the back-end in
  916. its menu entry. If it is a number, the following menu will
  917. be displayed as a sub-menu of the back-end with the same
  918. KEY. Also, the number will be used to determine in which
  919. order such sub-menus will appear (lowest first).
  920. ACTION-OR-MENU is either a function or an alist.
  921. If it is an action, it will be called with four
  922. arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
  923. BODY-ONLY. See `org-export-as' for further explanations on
  924. some of them.
  925. If it is an alist, associations should follow the
  926. pattern:
  927. '(KEY DESCRIPTION ACTION)
  928. where KEY, DESCRIPTION and ACTION are described above.
  929. Valid values include:
  930. '(?m \"My Special Back-end\" my-special-export-function)
  931. or
  932. '(?l \"Export to LaTeX\"
  933. \(?p \"As PDF file\" org-latex-export-to-pdf)
  934. \(?o \"As PDF file and open\"
  935. \(lambda (a s v b)
  936. \(if a (org-latex-export-to-pdf t s v b)
  937. \(org-open-file
  938. \(org-latex-export-to-pdf nil s v b)))))))
  939. or the following, which will be added to the previous
  940. sub-menu,
  941. '(?l 1
  942. \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
  943. \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
  944. :options-alist
  945. Alist between back-end specific properties introduced in
  946. communication channel and how their value are acquired. See
  947. `org-export-options-alist' for more information about
  948. structure of the values."
  949. (declare (indent 1))
  950. (let (blocks filters menu-entry options contents)
  951. (while (keywordp (car body))
  952. (case (pop body)
  953. (:export-block (let ((names (pop body)))
  954. (setq blocks (if (consp names) (mapcar 'upcase names)
  955. (list (upcase names))))))
  956. (:filters-alist (setq filters (pop body)))
  957. (:menu-entry (setq menu-entry (pop body)))
  958. (:options-alist (setq options (pop body)))
  959. (t (pop body))))
  960. (org-export-register-backend
  961. (org-export-create-backend :name backend
  962. :transcoders transcoders
  963. :options options
  964. :filters filters
  965. :blocks blocks
  966. :menu menu-entry))))
  967. (defun org-export-define-derived-backend (child parent &rest body)
  968. "Create a new back-end as a variant of an existing one.
  969. CHILD is the name of the derived back-end. PARENT is the name of
  970. the parent back-end.
  971. BODY can start with pre-defined keyword arguments. The following
  972. keywords are understood:
  973. :export-block
  974. String, or list of strings, representing block names that
  975. will not be parsed. This is used to specify blocks that will
  976. contain raw code specific to the back-end. These blocks
  977. still have to be handled by the relative `export-block' type
  978. translator.
  979. :filters-alist
  980. Alist of filters that will overwrite or complete filters
  981. defined in PARENT back-end. See `org-export-filters-alist'
  982. for a list of allowed filters.
  983. :menu-entry
  984. Menu entry for the export dispatcher. See
  985. `org-export-define-backend' for more information about the
  986. expected value.
  987. :options-alist
  988. Alist of back-end specific properties that will overwrite or
  989. complete those defined in PARENT back-end. Refer to
  990. `org-export-options-alist' for more information about
  991. structure of the values.
  992. :translate-alist
  993. Alist of element and object types and transcoders that will
  994. overwrite or complete transcode table from PARENT back-end.
  995. Refer to `org-export-define-backend' for detailed information
  996. about transcoders.
  997. As an example, here is how one could define \"my-latex\" back-end
  998. as a variant of `latex' back-end with a custom template function:
  999. \(org-export-define-derived-backend 'my-latex 'latex
  1000. :translate-alist '((template . my-latex-template-fun)))
  1001. The back-end could then be called with, for example:
  1002. \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
  1003. (declare (indent 2))
  1004. (let (blocks filters menu-entry options transcoders contents)
  1005. (while (keywordp (car body))
  1006. (case (pop body)
  1007. (:export-block (let ((names (pop body)))
  1008. (setq blocks (if (consp names) (mapcar 'upcase names)
  1009. (list (upcase names))))))
  1010. (:filters-alist (setq filters (pop body)))
  1011. (:menu-entry (setq menu-entry (pop body)))
  1012. (:options-alist (setq options (pop body)))
  1013. (:translate-alist (setq transcoders (pop body)))
  1014. (t (pop body))))
  1015. (org-export-register-backend
  1016. (org-export-create-backend :name child
  1017. :parent parent
  1018. :transcoders transcoders
  1019. :options options
  1020. :filters filters
  1021. :blocks blocks
  1022. :menu menu-entry))))
  1023. ;;; The Communication Channel
  1024. ;;
  1025. ;; During export process, every function has access to a number of
  1026. ;; properties. They are of two types:
  1027. ;;
  1028. ;; 1. Environment options are collected once at the very beginning of
  1029. ;; the process, out of the original buffer and configuration.
  1030. ;; Collecting them is handled by `org-export-get-environment'
  1031. ;; function.
  1032. ;;
  1033. ;; Most environment options are defined through the
  1034. ;; `org-export-options-alist' variable.
  1035. ;;
  1036. ;; 2. Tree properties are extracted directly from the parsed tree,
  1037. ;; just before export, by `org-export-collect-tree-properties'.
  1038. ;;
  1039. ;; Here is the full list of properties available during transcode
  1040. ;; process, with their category and their value type.
  1041. ;;
  1042. ;; + `:author' :: Author's name.
  1043. ;; - category :: option
  1044. ;; - type :: string
  1045. ;;
  1046. ;; + `:back-end' :: Current back-end used for transcoding.
  1047. ;; - category :: tree
  1048. ;; - type :: structure
  1049. ;;
  1050. ;; + `:creator' :: String to write as creation information.
  1051. ;; - category :: option
  1052. ;; - type :: string
  1053. ;;
  1054. ;; + `:date' :: String to use as date.
  1055. ;; - category :: option
  1056. ;; - type :: string
  1057. ;;
  1058. ;; + `:description' :: Description text for the current data.
  1059. ;; - category :: option
  1060. ;; - type :: string
  1061. ;;
  1062. ;; + `:email' :: Author's email.
  1063. ;; - category :: option
  1064. ;; - type :: string
  1065. ;;
  1066. ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
  1067. ;; process.
  1068. ;; - category :: option
  1069. ;; - type :: list of strings
  1070. ;;
  1071. ;; + `:export-options' :: List of export options available for current
  1072. ;; process.
  1073. ;; - category :: none
  1074. ;; - type :: list of symbols, among `subtree', `body-only' and
  1075. ;; `visible-only'.
  1076. ;;
  1077. ;; + `:exported-data' :: Hash table used for memoizing
  1078. ;; `org-export-data'.
  1079. ;; - category :: tree
  1080. ;; - type :: hash table
  1081. ;;
  1082. ;; + `:filetags' :: List of global tags for buffer. Used by
  1083. ;; `org-export-get-tags' to get tags with inheritance.
  1084. ;; - category :: option
  1085. ;; - type :: list of strings
  1086. ;;
  1087. ;; + `:footnote-definition-alist' :: Alist between footnote labels and
  1088. ;; their definition, as parsed data. Only non-inlined footnotes
  1089. ;; are represented in this alist. Also, every definition isn't
  1090. ;; guaranteed to be referenced in the parse tree. The purpose of
  1091. ;; this property is to preserve definitions from oblivion
  1092. ;; (i.e. when the parse tree comes from a part of the original
  1093. ;; buffer), it isn't meant for direct use in a back-end. To
  1094. ;; retrieve a definition relative to a reference, use
  1095. ;; `org-export-get-footnote-definition' instead.
  1096. ;; - category :: option
  1097. ;; - type :: alist (STRING . LIST)
  1098. ;;
  1099. ;; + `:headline-levels' :: Maximum level being exported as an
  1100. ;; headline. Comparison is done with the relative level of
  1101. ;; headlines in the parse tree, not necessarily with their
  1102. ;; actual level.
  1103. ;; - category :: option
  1104. ;; - type :: integer
  1105. ;;
  1106. ;; + `:headline-offset' :: Difference between relative and real level
  1107. ;; of headlines in the parse tree. For example, a value of -1
  1108. ;; means a level 2 headline should be considered as level
  1109. ;; 1 (cf. `org-export-get-relative-level').
  1110. ;; - category :: tree
  1111. ;; - type :: integer
  1112. ;;
  1113. ;; + `:headline-numbering' :: Alist between headlines and their
  1114. ;; numbering, as a list of numbers
  1115. ;; (cf. `org-export-get-headline-number').
  1116. ;; - category :: tree
  1117. ;; - type :: alist (INTEGER . LIST)
  1118. ;;
  1119. ;; + `:id-alist' :: Alist between ID strings and destination file's
  1120. ;; path, relative to current directory. It is used by
  1121. ;; `org-export-resolve-id-link' to resolve ID links targeting an
  1122. ;; external file.
  1123. ;; - category :: option
  1124. ;; - type :: alist (STRING . STRING)
  1125. ;;
  1126. ;; + `:ignore-list' :: List of elements and objects that should be
  1127. ;; ignored during export.
  1128. ;; - category :: tree
  1129. ;; - type :: list of elements and objects
  1130. ;;
  1131. ;; + `:input-buffer' :: Original buffer name.
  1132. ;; - category :: option
  1133. ;; - type :: string
  1134. ;;
  1135. ;; + `:input-file' :: Full path to input file, if any.
  1136. ;; - category :: option
  1137. ;; - type :: string or nil
  1138. ;;
  1139. ;; + `:keywords' :: List of keywords attached to data.
  1140. ;; - category :: option
  1141. ;; - type :: string
  1142. ;;
  1143. ;; + `:language' :: Default language used for translations.
  1144. ;; - category :: option
  1145. ;; - type :: string
  1146. ;;
  1147. ;; + `:parse-tree' :: Whole parse tree, available at any time during
  1148. ;; transcoding.
  1149. ;; - category :: option
  1150. ;; - type :: list (as returned by `org-element-parse-buffer')
  1151. ;;
  1152. ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
  1153. ;; all line breaks.
  1154. ;; - category :: option
  1155. ;; - type :: symbol (nil, t)
  1156. ;;
  1157. ;; + `:section-numbers' :: Non-nil means transcoding should add
  1158. ;; section numbers to headlines.
  1159. ;; - category :: option
  1160. ;; - type :: symbol (nil, t)
  1161. ;;
  1162. ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
  1163. ;; in transcoding. When such a tag is present, subtrees without
  1164. ;; it are de facto excluded from the process. See
  1165. ;; `use-select-tags'.
  1166. ;; - category :: option
  1167. ;; - type :: list of strings
  1168. ;;
  1169. ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
  1170. ;; a time stamp in the output.
  1171. ;; - category :: option
  1172. ;; - type :: symbol (nil, t)
  1173. ;;
  1174. ;; + `:translate-alist' :: Alist between element and object types and
  1175. ;; transcoding functions relative to the current back-end.
  1176. ;; Special keys `inner-template', `template' and `plain-text' are
  1177. ;; also possible.
  1178. ;; - category :: option
  1179. ;; - type :: alist (SYMBOL . FUNCTION)
  1180. ;;
  1181. ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
  1182. ;; also be transcoded. If it is set to the `headline' symbol,
  1183. ;; only the archived headline's name is retained.
  1184. ;; - category :: option
  1185. ;; - type :: symbol (nil, t, `headline')
  1186. ;;
  1187. ;; + `:with-author' :: Non-nil means author's name should be included
  1188. ;; in the output.
  1189. ;; - category :: option
  1190. ;; - type :: symbol (nil, t)
  1191. ;;
  1192. ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
  1193. ;; - category :: option
  1194. ;; - type :: symbol (nil, t)
  1195. ;;
  1196. ;; + `:with-creator' :: Non-nil means a creation sentence should be
  1197. ;; inserted at the end of the transcoded string. If the value
  1198. ;; is `comment', it should be commented.
  1199. ;; - category :: option
  1200. ;; - type :: symbol (`comment', nil, t)
  1201. ;;
  1202. ;; + `:with-date' :: Non-nil means output should contain a date.
  1203. ;; - category :: option
  1204. ;; - type :. symbol (nil, t)
  1205. ;;
  1206. ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
  1207. ;; its value is a list of names, only drawers with such names
  1208. ;; will be transcoded. If that list starts with `not', drawer
  1209. ;; with these names will be skipped.
  1210. ;; - category :: option
  1211. ;; - type :: symbol (nil, t) or list of strings
  1212. ;;
  1213. ;; + `:with-email' :: Non-nil means output should contain author's
  1214. ;; email.
  1215. ;; - category :: option
  1216. ;; - type :: symbol (nil, t)
  1217. ;;
  1218. ;; + `:with-emphasize' :: Non-nil means emphasized text should be
  1219. ;; interpreted.
  1220. ;; - category :: option
  1221. ;; - type :: symbol (nil, t)
  1222. ;;
  1223. ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
  1224. ;; strings starting with a colon as a fixed-with (verbatim) area.
  1225. ;; - category :: option
  1226. ;; - type :: symbol (nil, t)
  1227. ;;
  1228. ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
  1229. ;; footnotes.
  1230. ;; - category :: option
  1231. ;; - type :: symbol (nil, t)
  1232. ;;
  1233. ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
  1234. ;; `latex-fragment' objects should appear in export output. When
  1235. ;; this property is set to `verbatim', they will be left as-is.
  1236. ;; - category :: option
  1237. ;; - type :: symbol (`verbatim', nil, t)
  1238. ;;
  1239. ;; + `:with-planning' :: Non-nil means transcoding should include
  1240. ;; planning info.
  1241. ;; - category :: option
  1242. ;; - type :: symbol (nil, t)
  1243. ;;
  1244. ;; + `:with-priority' :: Non-nil means transcoding should include
  1245. ;; priority cookies.
  1246. ;; - category :: option
  1247. ;; - type :: symbol (nil, t)
  1248. ;;
  1249. ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
  1250. ;; plain text.
  1251. ;; - category :: option
  1252. ;; - type :: symbol (nil, t)
  1253. ;;
  1254. ;; + `:with-special-strings' :: Non-nil means transcoding should
  1255. ;; interpret special strings in plain text.
  1256. ;; - category :: option
  1257. ;; - type :: symbol (nil, t)
  1258. ;;
  1259. ;; + `:with-sub-superscript' :: Non-nil means transcoding should
  1260. ;; interpret subscript and superscript. With a value of "{}",
  1261. ;; only interpret those using curly brackets.
  1262. ;; - category :: option
  1263. ;; - type :: symbol (nil, {}, t)
  1264. ;;
  1265. ;; + `:with-tables' :: Non-nil means transcoding should interpret
  1266. ;; tables.
  1267. ;; - category :: option
  1268. ;; - type :: symbol (nil, t)
  1269. ;;
  1270. ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
  1271. ;; headlines. A `not-in-toc' value will remove them from the
  1272. ;; table of contents, if any, nonetheless.
  1273. ;; - category :: option
  1274. ;; - type :: symbol (nil, t, `not-in-toc')
  1275. ;;
  1276. ;; + `:with-tasks' :: Non-nil means transcoding should include
  1277. ;; headlines with a TODO keyword. A `todo' value will only
  1278. ;; include headlines with a todo type keyword while a `done'
  1279. ;; value will do the contrary. If a list of strings is provided,
  1280. ;; only tasks with keywords belonging to that list will be kept.
  1281. ;; - category :: option
  1282. ;; - type :: symbol (t, todo, done, nil) or list of strings
  1283. ;;
  1284. ;; + `:with-timestamps' :: Non-nil means transcoding should include
  1285. ;; time stamps. Special value `active' (resp. `inactive') ask to
  1286. ;; export only active (resp. inactive) timestamps. Otherwise,
  1287. ;; completely remove them.
  1288. ;; - category :: option
  1289. ;; - type :: symbol: (`active', `inactive', t, nil)
  1290. ;;
  1291. ;; + `:with-toc' :: Non-nil means that a table of contents has to be
  1292. ;; added to the output. An integer value limits its depth.
  1293. ;; - category :: option
  1294. ;; - type :: symbol (nil, t or integer)
  1295. ;;
  1296. ;; + `:with-todo-keywords' :: Non-nil means transcoding should
  1297. ;; include TODO keywords.
  1298. ;; - category :: option
  1299. ;; - type :: symbol (nil, t)
  1300. ;;;; Environment Options
  1301. ;;
  1302. ;; Environment options encompass all parameters defined outside the
  1303. ;; scope of the parsed data. They come from five sources, in
  1304. ;; increasing precedence order:
  1305. ;;
  1306. ;; - Global variables,
  1307. ;; - Buffer's attributes,
  1308. ;; - Options keyword symbols,
  1309. ;; - Buffer keywords,
  1310. ;; - Subtree properties.
  1311. ;;
  1312. ;; The central internal function with regards to environment options
  1313. ;; is `org-export-get-environment'. It updates global variables with
  1314. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  1315. ;; the different sources.
  1316. ;;
  1317. ;; The internal functions doing the retrieval are:
  1318. ;; `org-export--get-global-options',
  1319. ;; `org-export--get-buffer-attributes',
  1320. ;; `org-export--parse-option-keyword',
  1321. ;; `org-export--get-subtree-options' and
  1322. ;; `org-export--get-inbuffer-options'
  1323. ;;
  1324. ;; Also, `org-export--list-bound-variables' collects bound variables
  1325. ;; along with their value in order to set them as buffer local
  1326. ;; variables later in the process.
  1327. (defun org-export-get-environment (&optional backend subtreep ext-plist)
  1328. "Collect export options from the current buffer.
  1329. Optional argument BACKEND is an export back-end, as returned by
  1330. `org-export-create-backend'.
  1331. When optional argument SUBTREEP is non-nil, assume the export is
  1332. done against the current sub-tree.
  1333. Third optional argument EXT-PLIST is a property list with
  1334. external parameters overriding Org default settings, but still
  1335. inferior to file-local settings."
  1336. ;; First install #+BIND variables since these must be set before
  1337. ;; global options are read.
  1338. (dolist (pair (org-export--list-bound-variables))
  1339. (org-set-local (car pair) (nth 1 pair)))
  1340. ;; Get and prioritize export options...
  1341. (org-combine-plists
  1342. ;; ... from global variables...
  1343. (org-export--get-global-options backend)
  1344. ;; ... from an external property list...
  1345. ext-plist
  1346. ;; ... from in-buffer settings...
  1347. (org-export--get-inbuffer-options backend)
  1348. ;; ... and from subtree, when appropriate.
  1349. (and subtreep (org-export--get-subtree-options backend))
  1350. ;; Eventually add misc. properties.
  1351. (list
  1352. :back-end
  1353. backend
  1354. :translate-alist (org-export-get-all-transcoders backend)
  1355. :footnote-definition-alist
  1356. ;; Footnotes definitions must be collected in the original
  1357. ;; buffer, as there's no insurance that they will still be in
  1358. ;; the parse tree, due to possible narrowing.
  1359. (let (alist)
  1360. (org-with-wide-buffer
  1361. (goto-char (point-min))
  1362. (while (re-search-forward org-footnote-definition-re nil t)
  1363. (let ((def (save-match-data (org-element-at-point))))
  1364. (when (eq (org-element-type def) 'footnote-definition)
  1365. (push
  1366. (cons (org-element-property :label def)
  1367. (let ((cbeg (org-element-property :contents-begin def)))
  1368. (when cbeg
  1369. (org-element--parse-elements
  1370. cbeg (org-element-property :contents-end def)
  1371. nil nil nil nil (list 'org-data nil)))))
  1372. alist))))
  1373. alist))
  1374. :id-alist
  1375. ;; Collect id references.
  1376. (let (alist)
  1377. (org-with-wide-buffer
  1378. (goto-char (point-min))
  1379. (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
  1380. (let ((link (org-element-context)))
  1381. (when (eq (org-element-type link) 'link)
  1382. (let* ((id (org-element-property :path link))
  1383. (file (org-id-find-id-file id)))
  1384. (when file
  1385. (push (cons id (file-relative-name file)) alist)))))))
  1386. alist))))
  1387. (defun org-export--parse-option-keyword (options &optional backend)
  1388. "Parse an OPTIONS line and return values as a plist.
  1389. Optional argument BACKEND is an export back-end, as returned by,
  1390. e.g., `org-export-create-backend'. It specifies which back-end
  1391. specific items to read, if any."
  1392. (let* ((all
  1393. ;; Priority is given to back-end specific options.
  1394. (append (and backend (org-export-get-all-options backend))
  1395. org-export-options-alist))
  1396. plist)
  1397. (dolist (option all)
  1398. (let ((property (car option))
  1399. (item (nth 2 option)))
  1400. (when (and item
  1401. (not (plist-member plist property))
  1402. (string-match (concat "\\(\\`\\|[ \t]\\)"
  1403. (regexp-quote item)
  1404. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  1405. options))
  1406. (setq plist (plist-put plist
  1407. property
  1408. (car (read-from-string
  1409. (match-string 2 options))))))))
  1410. plist))
  1411. (defun org-export--get-subtree-options (&optional backend)
  1412. "Get export options in subtree at point.
  1413. Optional argument BACKEND is an export back-end, as returned by,
  1414. e.g., `org-export-create-backend'. It specifies back-end used
  1415. for export. Return options as a plist."
  1416. ;; For each buffer keyword, create a headline property setting the
  1417. ;; same property in communication channel. The name for the property
  1418. ;; is the keyword with "EXPORT_" appended to it.
  1419. (org-with-wide-buffer
  1420. (let (prop plist)
  1421. ;; Make sure point is at a heading.
  1422. (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t))
  1423. ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
  1424. ;; title as its fallback value.
  1425. (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
  1426. (progn (looking-at org-todo-line-regexp)
  1427. (org-match-string-no-properties 3))))
  1428. (setq plist
  1429. (plist-put
  1430. plist :title
  1431. (org-element-parse-secondary-string
  1432. prop (org-element-restriction 'keyword)))))
  1433. ;; EXPORT_OPTIONS are parsed in a non-standard way.
  1434. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  1435. (setq plist
  1436. (nconc plist (org-export--parse-option-keyword prop backend))))
  1437. ;; Handle other keywords. TITLE keyword is excluded as it has
  1438. ;; been handled already.
  1439. (let ((seen '("TITLE")))
  1440. (mapc
  1441. (lambda (option)
  1442. (let ((property (car option))
  1443. (keyword (nth 1 option)))
  1444. (when (and keyword (not (member keyword seen)))
  1445. (let* ((subtree-prop (concat "EXPORT_" keyword))
  1446. ;; Export properties are not case-sensitive.
  1447. (value (let ((case-fold-search t))
  1448. (org-entry-get (point) subtree-prop))))
  1449. (push keyword seen)
  1450. (when (and value (not (plist-member plist property)))
  1451. (setq plist
  1452. (plist-put
  1453. plist
  1454. property
  1455. (cond
  1456. ;; Parse VALUE if required.
  1457. ((member keyword org-element-document-properties)
  1458. (org-element-parse-secondary-string
  1459. value (org-element-restriction 'keyword)))
  1460. ;; If BEHAVIOUR is `split' expected value is
  1461. ;; a list of strings, not a string.
  1462. ((eq (nth 4 option) 'split) (org-split-string value))
  1463. (t value)))))))))
  1464. ;; Look for both general keywords and back-end specific
  1465. ;; options, with priority given to the latter.
  1466. (append (and backend (org-export-get-all-options backend))
  1467. org-export-options-alist)))
  1468. ;; Return value.
  1469. plist)))
  1470. (defun org-export--get-inbuffer-options (&optional backend)
  1471. "Return current buffer export options, as a plist.
  1472. Optional argument BACKEND, when non-nil, is an export back-end,
  1473. as returned by, e.g., `org-export-create-backend'. It specifies
  1474. which back-end specific options should also be read in the
  1475. process.
  1476. Assume buffer is in Org mode. Narrowing, if any, is ignored."
  1477. (let* (plist
  1478. get-options ; For byte-compiler.
  1479. (case-fold-search t)
  1480. (options (append
  1481. ;; Priority is given to back-end specific options.
  1482. (and backend (org-export-get-all-options backend))
  1483. org-export-options-alist))
  1484. (regexp (format "^[ \t]*#\\+%s:"
  1485. (regexp-opt (nconc (delq nil (mapcar 'cadr options))
  1486. org-export-special-keywords))))
  1487. (find-properties
  1488. (lambda (keyword)
  1489. ;; Return all properties associated to KEYWORD.
  1490. (let (properties)
  1491. (dolist (option options properties)
  1492. (when (equal (nth 1 option) keyword)
  1493. (pushnew (car option) properties))))))
  1494. (get-options
  1495. (lambda (&optional files plist)
  1496. ;; Recursively read keywords in buffer. FILES is a list
  1497. ;; of files read so far. PLIST is the current property
  1498. ;; list obtained.
  1499. (org-with-wide-buffer
  1500. (goto-char (point-min))
  1501. (while (re-search-forward regexp nil t)
  1502. (let ((element (org-element-at-point)))
  1503. (when (eq (org-element-type element) 'keyword)
  1504. (let ((key (org-element-property :key element))
  1505. (val (org-element-property :value element)))
  1506. (cond
  1507. ;; Options in `org-export-special-keywords'.
  1508. ((equal key "SETUPFILE")
  1509. (let ((file (expand-file-name
  1510. (org-remove-double-quotes (org-trim val)))))
  1511. ;; Avoid circular dependencies.
  1512. (unless (member file files)
  1513. (with-temp-buffer
  1514. (insert (org-file-contents file 'noerror))
  1515. (let ((org-inhibit-startup t)) (org-mode))
  1516. (setq plist (funcall get-options
  1517. (cons file files) plist))))))
  1518. ((equal key "OPTIONS")
  1519. (setq plist
  1520. (org-combine-plists
  1521. plist
  1522. (org-export--parse-option-keyword val backend))))
  1523. ((equal key "FILETAGS")
  1524. (setq plist
  1525. (org-combine-plists
  1526. plist
  1527. (list :filetags
  1528. (org-uniquify
  1529. (append (org-split-string val ":")
  1530. (plist-get plist :filetags)))))))
  1531. (t
  1532. ;; Options in `org-export-options-alist'.
  1533. (dolist (property (funcall find-properties key))
  1534. (let ((behaviour (nth 4 (assq property options))))
  1535. (setq plist
  1536. (plist-put
  1537. plist property
  1538. ;; Handle value depending on specified
  1539. ;; BEHAVIOUR.
  1540. (case behaviour
  1541. (space
  1542. (if (not (plist-get plist property))
  1543. (org-trim val)
  1544. (concat (plist-get plist property)
  1545. " "
  1546. (org-trim val))))
  1547. (newline
  1548. (org-trim
  1549. (concat (plist-get plist property)
  1550. "\n"
  1551. (org-trim val))))
  1552. (split `(,@(plist-get plist property)
  1553. ,@(org-split-string val)))
  1554. ('t val)
  1555. (otherwise
  1556. (if (not (plist-member plist property)) val
  1557. (plist-get plist property))))))))))))))
  1558. ;; Return final value.
  1559. plist))))
  1560. ;; Read options in the current buffer.
  1561. (setq plist (funcall get-options
  1562. (and buffer-file-name (list buffer-file-name)) nil))
  1563. ;; Parse keywords specified in `org-element-document-properties'
  1564. ;; and return PLIST.
  1565. (dolist (keyword org-element-document-properties plist)
  1566. (dolist (property (funcall find-properties keyword))
  1567. (let ((value (plist-get plist property)))
  1568. (when (stringp value)
  1569. (setq plist
  1570. (plist-put plist property
  1571. (org-element-parse-secondary-string
  1572. value (org-element-restriction 'keyword))))))))))
  1573. (defun org-export--get-buffer-attributes ()
  1574. "Return properties related to buffer attributes, as a plist."
  1575. ;; Store full path of input file name, or nil. For internal use.
  1576. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  1577. (list :input-buffer (buffer-name (buffer-base-buffer))
  1578. :input-file visited-file
  1579. :title (if (not visited-file) (buffer-name (buffer-base-buffer))
  1580. (file-name-sans-extension
  1581. (file-name-nondirectory visited-file))))))
  1582. (defun org-export--get-global-options (&optional backend)
  1583. "Return global export options as a plist.
  1584. Optional argument BACKEND, if non-nil, is an export back-end, as
  1585. returned by, e.g., `org-export-create-backend'. It specifies
  1586. which back-end specific export options should also be read in the
  1587. process."
  1588. (let (plist
  1589. ;; Priority is given to back-end specific options.
  1590. (all (append (and backend (org-export-get-all-options backend))
  1591. org-export-options-alist)))
  1592. (dolist (cell all plist)
  1593. (let ((prop (car cell))
  1594. (default-value (nth 3 cell)))
  1595. (unless (or (not default-value) (plist-member plist prop))
  1596. (setq plist
  1597. (plist-put
  1598. plist
  1599. prop
  1600. ;; Eval default value provided. If keyword is
  1601. ;; a member of `org-element-document-properties',
  1602. ;; parse it as a secondary string before storing it.
  1603. (let ((value (eval (nth 3 cell))))
  1604. (if (not (stringp value)) value
  1605. (let ((keyword (nth 1 cell)))
  1606. (if (member keyword org-element-document-properties)
  1607. (org-element-parse-secondary-string
  1608. value (org-element-restriction 'keyword))
  1609. value)))))))))))
  1610. (defun org-export--list-bound-variables ()
  1611. "Return variables bound from BIND keywords in current buffer.
  1612. Also look for BIND keywords in setup files. The return value is
  1613. an alist where associations are (VARIABLE-NAME VALUE)."
  1614. (when org-export-allow-bind-keywords
  1615. (let* (collect-bind ; For byte-compiler.
  1616. (collect-bind
  1617. (lambda (files alist)
  1618. ;; Return an alist between variable names and their
  1619. ;; value. FILES is a list of setup files names read so
  1620. ;; far, used to avoid circular dependencies. ALIST is
  1621. ;; the alist collected so far.
  1622. (let ((case-fold-search t))
  1623. (org-with-wide-buffer
  1624. (goto-char (point-min))
  1625. (while (re-search-forward
  1626. "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t)
  1627. (let ((element (org-element-at-point)))
  1628. (when (eq (org-element-type element) 'keyword)
  1629. (let ((val (org-element-property :value element)))
  1630. (if (equal (org-element-property :key element) "BIND")
  1631. (push (read (format "(%s)" val)) alist)
  1632. ;; Enter setup file.
  1633. (let ((file (expand-file-name
  1634. (org-remove-double-quotes val))))
  1635. (unless (member file files)
  1636. (with-temp-buffer
  1637. (let ((org-inhibit-startup t)) (org-mode))
  1638. (insert (org-file-contents file 'noerror))
  1639. (setq alist
  1640. (funcall collect-bind
  1641. (cons file files)
  1642. alist))))))))))
  1643. alist)))))
  1644. ;; Return value in appropriate order of appearance.
  1645. (nreverse (funcall collect-bind nil nil)))))
  1646. ;;;; Tree Properties
  1647. ;;
  1648. ;; Tree properties are information extracted from parse tree. They
  1649. ;; are initialized at the beginning of the transcoding process by
  1650. ;; `org-export-collect-tree-properties'.
  1651. ;;
  1652. ;; Dedicated functions focus on computing the value of specific tree
  1653. ;; properties during initialization. Thus,
  1654. ;; `org-export--populate-ignore-list' lists elements and objects that
  1655. ;; should be skipped during export, `org-export--get-min-level' gets
  1656. ;; the minimal exportable level, used as a basis to compute relative
  1657. ;; level for headlines. Eventually
  1658. ;; `org-export--collect-headline-numbering' builds an alist between
  1659. ;; headlines and their numbering.
  1660. (defun org-export-collect-tree-properties (data info)
  1661. "Extract tree properties from parse tree.
  1662. DATA is the parse tree from which information is retrieved. INFO
  1663. is a list holding export options.
  1664. Following tree properties are set or updated:
  1665. `:exported-data' Hash table used to memoize results from
  1666. `org-export-data'.
  1667. `:footnote-definition-alist' List of footnotes definitions in
  1668. original buffer and current parse tree.
  1669. `:headline-offset' Offset between true level of headlines and
  1670. local level. An offset of -1 means a headline
  1671. of level 2 should be considered as a level
  1672. 1 headline in the context.
  1673. `:headline-numbering' Alist of all headlines as key an the
  1674. associated numbering as value.
  1675. `:ignore-list' List of elements that should be ignored during
  1676. export.
  1677. Return updated plist."
  1678. ;; Install the parse tree in the communication channel, in order to
  1679. ;; use `org-export-get-genealogy' and al.
  1680. (setq info (plist-put info :parse-tree data))
  1681. ;; Get the list of elements and objects to ignore, and put it into
  1682. ;; `:ignore-list'. Do not overwrite any user ignore that might have
  1683. ;; been done during parse tree filtering.
  1684. (setq info
  1685. (plist-put info
  1686. :ignore-list
  1687. (append (org-export--populate-ignore-list data info)
  1688. (plist-get info :ignore-list))))
  1689. ;; Compute `:headline-offset' in order to be able to use
  1690. ;; `org-export-get-relative-level'.
  1691. (setq info
  1692. (plist-put info
  1693. :headline-offset
  1694. (- 1 (org-export--get-min-level data info))))
  1695. ;; Update footnotes definitions list with definitions in parse tree.
  1696. ;; This is required since buffer expansion might have modified
  1697. ;; boundaries of footnote definitions contained in the parse tree.
  1698. ;; This way, definitions in `footnote-definition-alist' are bound to
  1699. ;; match those in the parse tree.
  1700. (let ((defs (plist-get info :footnote-definition-alist)))
  1701. (org-element-map data 'footnote-definition
  1702. (lambda (fn)
  1703. (push (cons (org-element-property :label fn)
  1704. `(org-data nil ,@(org-element-contents fn)))
  1705. defs)))
  1706. (setq info (plist-put info :footnote-definition-alist defs)))
  1707. ;; Properties order doesn't matter: get the rest of the tree
  1708. ;; properties.
  1709. (nconc
  1710. `(:headline-numbering ,(org-export--collect-headline-numbering data info)
  1711. :exported-data ,(make-hash-table :test 'eq :size 4001))
  1712. info))
  1713. (defun org-export--get-min-level (data options)
  1714. "Return minimum exportable headline's level in DATA.
  1715. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1716. OPTIONS is a plist holding export options."
  1717. (catch 'exit
  1718. (let ((min-level 10000))
  1719. (mapc
  1720. (lambda (blob)
  1721. (when (and (eq (org-element-type blob) 'headline)
  1722. (not (org-element-property :footnote-section-p blob))
  1723. (not (memq blob (plist-get options :ignore-list))))
  1724. (setq min-level (min (org-element-property :level blob) min-level)))
  1725. (when (= min-level 1) (throw 'exit 1)))
  1726. (org-element-contents data))
  1727. ;; If no headline was found, for the sake of consistency, set
  1728. ;; minimum level to 1 nonetheless.
  1729. (if (= min-level 10000) 1 min-level))))
  1730. (defun org-export--collect-headline-numbering (data options)
  1731. "Return numbering of all exportable headlines in a parse tree.
  1732. DATA is the parse tree. OPTIONS is the plist holding export
  1733. options.
  1734. Return an alist whose key is a headline and value is its
  1735. associated numbering \(in the shape of a list of numbers\) or nil
  1736. for a footnotes section."
  1737. (let ((numbering (make-vector org-export-max-depth 0)))
  1738. (org-element-map data 'headline
  1739. (lambda (headline)
  1740. (unless (org-element-property :footnote-section-p headline)
  1741. (let ((relative-level
  1742. (1- (org-export-get-relative-level headline options))))
  1743. (cons
  1744. headline
  1745. (loop for n across numbering
  1746. for idx from 0 to org-export-max-depth
  1747. when (< idx relative-level) collect n
  1748. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1749. when (> idx relative-level) do (aset numbering idx 0))))))
  1750. options)))
  1751. (defun org-export--populate-ignore-list (data options)
  1752. "Return list of elements and objects to ignore during export.
  1753. DATA is the parse tree to traverse. OPTIONS is the plist holding
  1754. export options."
  1755. (let* (walk-data
  1756. ;; First find trees containing a select tag, if any.
  1757. (selected (org-export--selected-trees data options))
  1758. ;; If a select tag is active, also ignore the section before
  1759. ;; the first headline, if any.
  1760. (ignore (and selected
  1761. (let ((first-element (car (org-element-contents data))))
  1762. (and (eq (org-element-type first-element) 'section)
  1763. first-element))))
  1764. (walk-data
  1765. (lambda (data)
  1766. ;; Collect ignored elements or objects into IGNORE-LIST.
  1767. (let ((type (org-element-type data)))
  1768. (if (org-export--skip-p data options selected) (push data ignore)
  1769. (if (and (eq type 'headline)
  1770. (eq (plist-get options :with-archived-trees) 'headline)
  1771. (org-element-property :archivedp data))
  1772. ;; If headline is archived but tree below has
  1773. ;; to be skipped, add it to ignore list.
  1774. (dolist (element (org-element-contents data))
  1775. (push element ignore))
  1776. ;; Move into secondary string, if any.
  1777. (let ((sec-prop
  1778. (cdr (assq type org-element-secondary-value-alist))))
  1779. (when sec-prop
  1780. (mapc walk-data (org-element-property sec-prop data))))
  1781. ;; Move into recursive objects/elements.
  1782. (mapc walk-data (org-element-contents data))))))))
  1783. ;; Main call.
  1784. (funcall walk-data data)
  1785. ;; Return value.
  1786. ignore))
  1787. (defun org-export--selected-trees (data info)
  1788. "Return list of headlines and inlinetasks with a select tag in their tree.
  1789. DATA is parsed data as returned by `org-element-parse-buffer'.
  1790. INFO is a plist holding export options."
  1791. (let* (selected-trees
  1792. walk-data ; For byte-compiler.
  1793. (walk-data
  1794. (function
  1795. (lambda (data genealogy)
  1796. (let ((type (org-element-type data)))
  1797. (cond
  1798. ((memq type '(headline inlinetask))
  1799. (let ((tags (org-element-property :tags data)))
  1800. (if (loop for tag in (plist-get info :select-tags)
  1801. thereis (member tag tags))
  1802. ;; When a select tag is found, mark full
  1803. ;; genealogy and every headline within the tree
  1804. ;; as acceptable.
  1805. (setq selected-trees
  1806. (append
  1807. genealogy
  1808. (org-element-map data '(headline inlinetask)
  1809. 'identity)
  1810. selected-trees))
  1811. ;; If at a headline, continue searching in tree,
  1812. ;; recursively.
  1813. (when (eq type 'headline)
  1814. (mapc (lambda (el)
  1815. (funcall walk-data el (cons data genealogy)))
  1816. (org-element-contents data))))))
  1817. ((or (eq type 'org-data)
  1818. (memq type org-element-greater-elements))
  1819. (mapc (lambda (el) (funcall walk-data el genealogy))
  1820. (org-element-contents data)))))))))
  1821. (funcall walk-data data nil)
  1822. selected-trees))
  1823. (defun org-export--skip-p (blob options selected)
  1824. "Non-nil when element or object BLOB should be skipped during export.
  1825. OPTIONS is the plist holding export options. SELECTED, when
  1826. non-nil, is a list of headlines or inlinetasks belonging to
  1827. a tree with a select tag."
  1828. (case (org-element-type blob)
  1829. (clock (not (plist-get options :with-clocks)))
  1830. (drawer
  1831. (let ((with-drawers-p (plist-get options :with-drawers)))
  1832. (or (not with-drawers-p)
  1833. (and (consp with-drawers-p)
  1834. ;; If `:with-drawers' value starts with `not', ignore
  1835. ;; every drawer whose name belong to that list.
  1836. ;; Otherwise, ignore drawers whose name isn't in that
  1837. ;; list.
  1838. (let ((name (org-element-property :drawer-name blob)))
  1839. (if (eq (car with-drawers-p) 'not)
  1840. (member-ignore-case name (cdr with-drawers-p))
  1841. (not (member-ignore-case name with-drawers-p))))))))
  1842. ((footnote-definition footnote-reference)
  1843. (not (plist-get options :with-footnotes)))
  1844. ((headline inlinetask)
  1845. (let ((with-tasks (plist-get options :with-tasks))
  1846. (todo (org-element-property :todo-keyword blob))
  1847. (todo-type (org-element-property :todo-type blob))
  1848. (archived (plist-get options :with-archived-trees))
  1849. (tags (org-element-property :tags blob)))
  1850. (or
  1851. (and (eq (org-element-type blob) 'inlinetask)
  1852. (not (plist-get options :with-inlinetasks)))
  1853. ;; Ignore subtrees with an exclude tag.
  1854. (loop for k in (plist-get options :exclude-tags)
  1855. thereis (member k tags))
  1856. ;; When a select tag is present in the buffer, ignore any tree
  1857. ;; without it.
  1858. (and selected (not (memq blob selected)))
  1859. ;; Ignore commented sub-trees.
  1860. (org-element-property :commentedp blob)
  1861. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1862. (and (not archived) (org-element-property :archivedp blob))
  1863. ;; Ignore tasks, if specified by `:with-tasks' property.
  1864. (and todo
  1865. (or (not with-tasks)
  1866. (and (memq with-tasks '(todo done))
  1867. (not (eq todo-type with-tasks)))
  1868. (and (consp with-tasks) (not (member todo with-tasks))))))))
  1869. ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
  1870. (node-property
  1871. (let ((properties-set (plist-get options :with-properties)))
  1872. (cond ((null properties-set) t)
  1873. ((consp properties-set)
  1874. (not (member-ignore-case (org-element-property :key blob)
  1875. properties-set))))))
  1876. (planning (not (plist-get options :with-planning)))
  1877. (property-drawer (not (plist-get options :with-properties)))
  1878. (statistics-cookie (not (plist-get options :with-statistics-cookies)))
  1879. (table-cell
  1880. (and (org-export-table-has-special-column-p
  1881. (org-export-get-parent-table blob))
  1882. (not (org-export-get-previous-element blob options))))
  1883. (table-row (org-export-table-row-is-special-p blob options))
  1884. (timestamp
  1885. ;; `:with-timestamps' only applies to isolated timestamps
  1886. ;; objects, i.e. timestamp objects in a paragraph containing only
  1887. ;; timestamps and whitespaces.
  1888. (when (let ((parent (org-export-get-parent-element blob)))
  1889. (and (memq (org-element-type parent) '(paragraph verse-block))
  1890. (not (org-element-map parent
  1891. (cons 'plain-text
  1892. (remq 'timestamp org-element-all-objects))
  1893. (lambda (obj)
  1894. (or (not (stringp obj)) (org-string-nw-p obj)))
  1895. options t))))
  1896. (case (plist-get options :with-timestamps)
  1897. ('nil t)
  1898. (active
  1899. (not (memq (org-element-property :type blob) '(active active-range))))
  1900. (inactive
  1901. (not (memq (org-element-property :type blob)
  1902. '(inactive inactive-range)))))))))
  1903. ;;; The Transcoder
  1904. ;;
  1905. ;; `org-export-data' reads a parse tree (obtained with, i.e.
  1906. ;; `org-element-parse-buffer') and transcodes it into a specified
  1907. ;; back-end output. It takes care of filtering out elements or
  1908. ;; objects according to export options and organizing the output blank
  1909. ;; lines and white space are preserved. The function memoizes its
  1910. ;; results, so it is cheap to call it within transcoders.
  1911. ;;
  1912. ;; It is possible to modify locally the back-end used by
  1913. ;; `org-export-data' or even use a temporary back-end by using
  1914. ;; `org-export-data-with-backend'.
  1915. ;;
  1916. ;; Internally, three functions handle the filtering of objects and
  1917. ;; elements during the export. In particular,
  1918. ;; `org-export-ignore-element' marks an element or object so future
  1919. ;; parse tree traversals skip it, `org-export--interpret-p' tells which
  1920. ;; elements or objects should be seen as real Org syntax and
  1921. ;; `org-export-expand' transforms the others back into their original
  1922. ;; shape
  1923. ;;
  1924. ;; `org-export-transcoder' is an accessor returning appropriate
  1925. ;; translator function for a given element or object.
  1926. (defun org-export-transcoder (blob info)
  1927. "Return appropriate transcoder for BLOB.
  1928. INFO is a plist containing export directives."
  1929. (let ((type (org-element-type blob)))
  1930. ;; Return contents only for complete parse trees.
  1931. (if (eq type 'org-data) (lambda (blob contents info) contents)
  1932. (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
  1933. (and (functionp transcoder) transcoder)))))
  1934. (defun org-export-data (data info)
  1935. "Convert DATA into current back-end format.
  1936. DATA is a parse tree, an element or an object or a secondary
  1937. string. INFO is a plist holding export options.
  1938. Return transcoded string."
  1939. (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
  1940. (if (not (eq memo 'no-memo)) memo
  1941. (let* ((type (org-element-type data))
  1942. (results
  1943. (cond
  1944. ;; Ignored element/object.
  1945. ((memq data (plist-get info :ignore-list)) nil)
  1946. ;; Plain text.
  1947. ((eq type 'plain-text)
  1948. (org-export-filter-apply-functions
  1949. (plist-get info :filter-plain-text)
  1950. (let ((transcoder (org-export-transcoder data info)))
  1951. (if transcoder (funcall transcoder data info) data))
  1952. info))
  1953. ;; Uninterpreted element/object: change it back to Org
  1954. ;; syntax and export again resulting raw string.
  1955. ((not (org-export--interpret-p data info))
  1956. (org-export-data
  1957. (org-export-expand
  1958. data
  1959. (mapconcat (lambda (blob) (org-export-data blob info))
  1960. (org-element-contents data)
  1961. ""))
  1962. info))
  1963. ;; Secondary string.
  1964. ((not type)
  1965. (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
  1966. ;; Element/Object without contents or, as a special case,
  1967. ;; headline with archive tag and archived trees restricted
  1968. ;; to title only.
  1969. ((or (not (org-element-contents data))
  1970. (and (eq type 'headline)
  1971. (eq (plist-get info :with-archived-trees) 'headline)
  1972. (org-element-property :archivedp data)))
  1973. (let ((transcoder (org-export-transcoder data info)))
  1974. (or (and (functionp transcoder)
  1975. (funcall transcoder data nil info))
  1976. ;; Export snippets never return a nil value so
  1977. ;; that white spaces following them are never
  1978. ;; ignored.
  1979. (and (eq type 'export-snippet) ""))))
  1980. ;; Element/Object with contents.
  1981. (t
  1982. (let ((transcoder (org-export-transcoder data info)))
  1983. (when transcoder
  1984. (let* ((greaterp (memq type org-element-greater-elements))
  1985. (objectp
  1986. (and (not greaterp)
  1987. (memq type org-element-recursive-objects)))
  1988. (contents
  1989. (mapconcat
  1990. (lambda (element) (org-export-data element info))
  1991. (org-element-contents
  1992. (if (or greaterp objectp) data
  1993. ;; Elements directly containing objects
  1994. ;; must have their indentation normalized
  1995. ;; first.
  1996. (org-element-normalize-contents
  1997. data
  1998. ;; When normalizing contents of the first
  1999. ;; paragraph in an item or a footnote
  2000. ;; definition, ignore first line's
  2001. ;; indentation: there is none and it
  2002. ;; might be misleading.
  2003. (when (eq type 'paragraph)
  2004. (let ((parent (org-export-get-parent data)))
  2005. (and
  2006. (eq (car (org-element-contents parent))
  2007. data)
  2008. (memq (org-element-type parent)
  2009. '(footnote-definition item))))))))
  2010. "")))
  2011. (funcall transcoder data
  2012. (if (not greaterp) contents
  2013. (org-element-normalize-string contents))
  2014. info))))))))
  2015. ;; Final result will be memoized before being returned.
  2016. (puthash
  2017. data
  2018. (cond
  2019. ((not results) nil)
  2020. ((memq type '(org-data plain-text nil)) results)
  2021. ;; Append the same white space between elements or objects as in
  2022. ;; the original buffer, and call appropriate filters.
  2023. (t
  2024. (let ((results
  2025. (org-export-filter-apply-functions
  2026. (plist-get info (intern (format ":filter-%s" type)))
  2027. (let ((post-blank (or (org-element-property :post-blank data)
  2028. 0)))
  2029. (if (memq type org-element-all-elements)
  2030. (concat (org-element-normalize-string results)
  2031. (make-string post-blank ?\n))
  2032. (concat results (make-string post-blank ? ))))
  2033. info)))
  2034. results)))
  2035. (plist-get info :exported-data))))))
  2036. (defun org-export-data-with-backend (data backend info)
  2037. "Convert DATA into BACKEND format.
  2038. DATA is an element, an object, a secondary string or a string.
  2039. BACKEND is a symbol. INFO is a plist used as a communication
  2040. channel.
  2041. Unlike to `org-export-with-backend', this function will
  2042. recursively convert DATA using BACKEND translation table."
  2043. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  2044. (org-export-data
  2045. data
  2046. ;; Set-up a new communication channel with translations defined in
  2047. ;; BACKEND as the translate table and a new hash table for
  2048. ;; memoization.
  2049. (org-combine-plists
  2050. info
  2051. (list :back-end backend
  2052. :translate-alist (org-export-get-all-transcoders backend)
  2053. ;; Size of the hash table is reduced since this function
  2054. ;; will probably be used on small trees.
  2055. :exported-data (make-hash-table :test 'eq :size 401)))))
  2056. (defun org-export--interpret-p (blob info)
  2057. "Non-nil if element or object BLOB should be interpreted during export.
  2058. If nil, BLOB will appear as raw Org syntax. Check is done
  2059. according to export options INFO, stored as a plist."
  2060. (case (org-element-type blob)
  2061. ;; ... entities...
  2062. (entity (plist-get info :with-entities))
  2063. ;; ... emphasis...
  2064. ((bold italic strike-through underline)
  2065. (plist-get info :with-emphasize))
  2066. ;; ... fixed-width areas.
  2067. (fixed-width (plist-get info :with-fixed-width))
  2068. ;; ... LaTeX environments and fragments...
  2069. ((latex-environment latex-fragment)
  2070. (let ((with-latex-p (plist-get info :with-latex)))
  2071. (and with-latex-p (not (eq with-latex-p 'verbatim)))))
  2072. ;; ... sub/superscripts...
  2073. ((subscript superscript)
  2074. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  2075. (if (eq sub/super-p '{})
  2076. (org-element-property :use-brackets-p blob)
  2077. sub/super-p)))
  2078. ;; ... tables...
  2079. (table (plist-get info :with-tables))
  2080. (otherwise t)))
  2081. (defun org-export-expand (blob contents &optional with-affiliated)
  2082. "Expand a parsed element or object to its original state.
  2083. BLOB is either an element or an object. CONTENTS is its
  2084. contents, as a string or nil.
  2085. When optional argument WITH-AFFILIATED is non-nil, add affiliated
  2086. keywords before output."
  2087. (let ((type (org-element-type blob)))
  2088. (concat (and with-affiliated (memq type org-element-all-elements)
  2089. (org-element--interpret-affiliated-keywords blob))
  2090. (funcall (intern (format "org-element-%s-interpreter" type))
  2091. blob contents))))
  2092. (defun org-export-ignore-element (element info)
  2093. "Add ELEMENT to `:ignore-list' in INFO.
  2094. Any element in `:ignore-list' will be skipped when using
  2095. `org-element-map'. INFO is modified by side effects."
  2096. (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
  2097. ;;; The Filter System
  2098. ;;
  2099. ;; Filters allow end-users to tweak easily the transcoded output.
  2100. ;; They are the functional counterpart of hooks, as every filter in
  2101. ;; a set is applied to the return value of the previous one.
  2102. ;;
  2103. ;; Every set is back-end agnostic. Although, a filter is always
  2104. ;; called, in addition to the string it applies to, with the back-end
  2105. ;; used as argument, so it's easy for the end-user to add back-end
  2106. ;; specific filters in the set. The communication channel, as
  2107. ;; a plist, is required as the third argument.
  2108. ;;
  2109. ;; From the developer side, filters sets can be installed in the
  2110. ;; process with the help of `org-export-define-backend', which
  2111. ;; internally stores filters as an alist. Each association has a key
  2112. ;; among the following symbols and a function or a list of functions
  2113. ;; as value.
  2114. ;;
  2115. ;; - `:filter-options' applies to the property list containing export
  2116. ;; options. Unlike to other filters, functions in this list accept
  2117. ;; two arguments instead of three: the property list containing
  2118. ;; export options and the back-end. Users can set its value through
  2119. ;; `org-export-filter-options-functions' variable.
  2120. ;;
  2121. ;; - `:filter-parse-tree' applies directly to the complete parsed
  2122. ;; tree. Users can set it through
  2123. ;; `org-export-filter-parse-tree-functions' variable.
  2124. ;;
  2125. ;; - `:filter-final-output' applies to the final transcoded string.
  2126. ;; Users can set it with `org-export-filter-final-output-functions'
  2127. ;; variable
  2128. ;;
  2129. ;; - `:filter-plain-text' applies to any string not recognized as Org
  2130. ;; syntax. `org-export-filter-plain-text-functions' allows users to
  2131. ;; configure it.
  2132. ;;
  2133. ;; - `:filter-TYPE' applies on the string returned after an element or
  2134. ;; object of type TYPE has been transcoded. A user can modify
  2135. ;; `org-export-filter-TYPE-functions'
  2136. ;;
  2137. ;; All filters sets are applied with
  2138. ;; `org-export-filter-apply-functions' function. Filters in a set are
  2139. ;; applied in a LIFO fashion. It allows developers to be sure that
  2140. ;; their filters will be applied first.
  2141. ;;
  2142. ;; Filters properties are installed in communication channel with
  2143. ;; `org-export-install-filters' function.
  2144. ;;
  2145. ;; Eventually, two hooks (`org-export-before-processing-hook' and
  2146. ;; `org-export-before-parsing-hook') are run at the beginning of the
  2147. ;; export process and just before parsing to allow for heavy structure
  2148. ;; modifications.
  2149. ;;;; Hooks
  2150. (defvar org-export-before-processing-hook nil
  2151. "Hook run at the beginning of the export process.
  2152. This is run before include keywords and macros are expanded and
  2153. Babel code blocks executed, on a copy of the original buffer
  2154. being exported. Visibility and narrowing are preserved. Point
  2155. is at the beginning of the buffer.
  2156. Every function in this hook will be called with one argument: the
  2157. back-end currently used, as a symbol.")
  2158. (defvar org-export-before-parsing-hook nil
  2159. "Hook run before parsing an export buffer.
  2160. This is run after include keywords and macros have been expanded
  2161. and Babel code blocks executed, on a copy of the original buffer
  2162. being exported. Visibility and narrowing are preserved. Point
  2163. is at the beginning of the buffer.
  2164. Every function in this hook will be called with one argument: the
  2165. back-end currently used, as a symbol.")
  2166. ;;;; Special Filters
  2167. (defvar org-export-filter-options-functions nil
  2168. "List of functions applied to the export options.
  2169. Each filter is called with two arguments: the export options, as
  2170. a plist, and the back-end, as a symbol. It must return
  2171. a property list containing export options.")
  2172. (defvar org-export-filter-parse-tree-functions nil
  2173. "List of functions applied to the parsed tree.
  2174. Each filter is called with three arguments: the parse tree, as
  2175. returned by `org-element-parse-buffer', the back-end, as
  2176. a symbol, and the communication channel, as a plist. It must
  2177. return the modified parse tree to transcode.")
  2178. (defvar org-export-filter-plain-text-functions nil
  2179. "List of functions applied to plain text.
  2180. Each filter is called with three arguments: a string which
  2181. contains no Org syntax, the back-end, as a symbol, and the
  2182. communication channel, as a plist. It must return a string or
  2183. nil.")
  2184. (defvar org-export-filter-final-output-functions nil
  2185. "List of functions applied to the transcoded string.
  2186. Each filter is called with three arguments: the full transcoded
  2187. string, the back-end, as a symbol, and the communication channel,
  2188. as a plist. It must return a string that will be used as the
  2189. final export output.")
  2190. ;;;; Elements Filters
  2191. (defvar org-export-filter-babel-call-functions nil
  2192. "List of functions applied to a transcoded babel-call.
  2193. Each filter is called with three arguments: the transcoded data,
  2194. as a string, the back-end, as a symbol, and the communication
  2195. channel, as a plist. It must return a string or nil.")
  2196. (defvar org-export-filter-center-block-functions nil
  2197. "List of functions applied to a transcoded center block.
  2198. Each filter is called with three arguments: the transcoded data,
  2199. as a string, the back-end, as a symbol, and the communication
  2200. channel, as a plist. It must return a string or nil.")
  2201. (defvar org-export-filter-clock-functions nil
  2202. "List of functions applied to a transcoded clock.
  2203. Each filter is called with three arguments: the transcoded data,
  2204. as a string, the back-end, as a symbol, and the communication
  2205. channel, as a plist. It must return a string or nil.")
  2206. (defvar org-export-filter-comment-functions nil
  2207. "List of functions applied to a transcoded comment.
  2208. Each filter is called with three arguments: the transcoded data,
  2209. as a string, the back-end, as a symbol, and the communication
  2210. channel, as a plist. It must return a string or nil.")
  2211. (defvar org-export-filter-comment-block-functions nil
  2212. "List of functions applied to a transcoded comment-block.
  2213. Each filter is called with three arguments: the transcoded data,
  2214. as a string, the back-end, as a symbol, and the communication
  2215. channel, as a plist. It must return a string or nil.")
  2216. (defvar org-export-filter-diary-sexp-functions nil
  2217. "List of functions applied to a transcoded diary-sexp.
  2218. Each filter is called with three arguments: the transcoded data,
  2219. as a string, the back-end, as a symbol, and the communication
  2220. channel, as a plist. It must return a string or nil.")
  2221. (defvar org-export-filter-drawer-functions nil
  2222. "List of functions applied to a transcoded drawer.
  2223. Each filter is called with three arguments: the transcoded data,
  2224. as a string, the back-end, as a symbol, and the communication
  2225. channel, as a plist. It must return a string or nil.")
  2226. (defvar org-export-filter-dynamic-block-functions nil
  2227. "List of functions applied to a transcoded dynamic-block.
  2228. Each filter is called with three arguments: the transcoded data,
  2229. as a string, the back-end, as a symbol, and the communication
  2230. channel, as a plist. It must return a string or nil.")
  2231. (defvar org-export-filter-example-block-functions nil
  2232. "List of functions applied to a transcoded example-block.
  2233. Each filter is called with three arguments: the transcoded data,
  2234. as a string, the back-end, as a symbol, and the communication
  2235. channel, as a plist. It must return a string or nil.")
  2236. (defvar org-export-filter-export-block-functions nil
  2237. "List of functions applied to a transcoded export-block.
  2238. Each filter is called with three arguments: the transcoded data,
  2239. as a string, the back-end, as a symbol, and the communication
  2240. channel, as a plist. It must return a string or nil.")
  2241. (defvar org-export-filter-fixed-width-functions nil
  2242. "List of functions applied to a transcoded fixed-width.
  2243. Each filter is called with three arguments: the transcoded data,
  2244. as a string, the back-end, as a symbol, and the communication
  2245. channel, as a plist. It must return a string or nil.")
  2246. (defvar org-export-filter-footnote-definition-functions nil
  2247. "List of functions applied to a transcoded footnote-definition.
  2248. Each filter is called with three arguments: the transcoded data,
  2249. as a string, the back-end, as a symbol, and the communication
  2250. channel, as a plist. It must return a string or nil.")
  2251. (defvar org-export-filter-headline-functions nil
  2252. "List of functions applied to a transcoded headline.
  2253. Each filter is called with three arguments: the transcoded data,
  2254. as a string, the back-end, as a symbol, and the communication
  2255. channel, as a plist. It must return a string or nil.")
  2256. (defvar org-export-filter-horizontal-rule-functions nil
  2257. "List of functions applied to a transcoded horizontal-rule.
  2258. Each filter is called with three arguments: the transcoded data,
  2259. as a string, the back-end, as a symbol, and the communication
  2260. channel, as a plist. It must return a string or nil.")
  2261. (defvar org-export-filter-inlinetask-functions nil
  2262. "List of functions applied to a transcoded inlinetask.
  2263. Each filter is called with three arguments: the transcoded data,
  2264. as a string, the back-end, as a symbol, and the communication
  2265. channel, as a plist. It must return a string or nil.")
  2266. (defvar org-export-filter-item-functions nil
  2267. "List of functions applied to a transcoded item.
  2268. Each filter is called with three arguments: the transcoded data,
  2269. as a string, the back-end, as a symbol, and the communication
  2270. channel, as a plist. It must return a string or nil.")
  2271. (defvar org-export-filter-keyword-functions nil
  2272. "List of functions applied to a transcoded keyword.
  2273. Each filter is called with three arguments: the transcoded data,
  2274. as a string, the back-end, as a symbol, and the communication
  2275. channel, as a plist. It must return a string or nil.")
  2276. (defvar org-export-filter-latex-environment-functions nil
  2277. "List of functions applied to a transcoded latex-environment.
  2278. Each filter is called with three arguments: the transcoded data,
  2279. as a string, the back-end, as a symbol, and the communication
  2280. channel, as a plist. It must return a string or nil.")
  2281. (defvar org-export-filter-node-property-functions nil
  2282. "List of functions applied to a transcoded node-property.
  2283. Each filter is called with three arguments: the transcoded data,
  2284. as a string, the back-end, as a symbol, and the communication
  2285. channel, as a plist. It must return a string or nil.")
  2286. (defvar org-export-filter-paragraph-functions nil
  2287. "List of functions applied to a transcoded paragraph.
  2288. Each filter is called with three arguments: the transcoded data,
  2289. as a string, the back-end, as a symbol, and the communication
  2290. channel, as a plist. It must return a string or nil.")
  2291. (defvar org-export-filter-plain-list-functions nil
  2292. "List of functions applied to a transcoded plain-list.
  2293. Each filter is called with three arguments: the transcoded data,
  2294. as a string, the back-end, as a symbol, and the communication
  2295. channel, as a plist. It must return a string or nil.")
  2296. (defvar org-export-filter-planning-functions nil
  2297. "List of functions applied to a transcoded planning.
  2298. Each filter is called with three arguments: the transcoded data,
  2299. as a string, the back-end, as a symbol, and the communication
  2300. channel, as a plist. It must return a string or nil.")
  2301. (defvar org-export-filter-property-drawer-functions nil
  2302. "List of functions applied to a transcoded property-drawer.
  2303. Each filter is called with three arguments: the transcoded data,
  2304. as a string, the back-end, as a symbol, and the communication
  2305. channel, as a plist. It must return a string or nil.")
  2306. (defvar org-export-filter-quote-block-functions nil
  2307. "List of functions applied to a transcoded quote block.
  2308. Each filter is called with three arguments: the transcoded quote
  2309. data, as a string, the back-end, as a symbol, and the
  2310. communication channel, as a plist. It must return a string or
  2311. nil.")
  2312. (defvar org-export-filter-quote-section-functions nil
  2313. "List of functions applied to a transcoded quote-section.
  2314. Each filter is called with three arguments: the transcoded data,
  2315. as a string, the back-end, as a symbol, and the communication
  2316. channel, as a plist. It must return a string or nil.")
  2317. (defvar org-export-filter-section-functions nil
  2318. "List of functions applied to a transcoded section.
  2319. Each filter is called with three arguments: the transcoded data,
  2320. as a string, the back-end, as a symbol, and the communication
  2321. channel, as a plist. It must return a string or nil.")
  2322. (defvar org-export-filter-special-block-functions nil
  2323. "List of functions applied to a transcoded special block.
  2324. Each filter is called with three arguments: the transcoded data,
  2325. as a string, the back-end, as a symbol, and the communication
  2326. channel, as a plist. It must return a string or nil.")
  2327. (defvar org-export-filter-src-block-functions nil
  2328. "List of functions applied to a transcoded src-block.
  2329. Each filter is called with three arguments: the transcoded data,
  2330. as a string, the back-end, as a symbol, and the communication
  2331. channel, as a plist. It must return a string or nil.")
  2332. (defvar org-export-filter-table-functions nil
  2333. "List of functions applied to a transcoded table.
  2334. Each filter is called with three arguments: the transcoded data,
  2335. as a string, the back-end, as a symbol, and the communication
  2336. channel, as a plist. It must return a string or nil.")
  2337. (defvar org-export-filter-table-cell-functions nil
  2338. "List of functions applied to a transcoded table-cell.
  2339. Each filter is called with three arguments: the transcoded data,
  2340. as a string, the back-end, as a symbol, and the communication
  2341. channel, as a plist. It must return a string or nil.")
  2342. (defvar org-export-filter-table-row-functions nil
  2343. "List of functions applied to a transcoded table-row.
  2344. Each filter is called with three arguments: the transcoded data,
  2345. as a string, the back-end, as a symbol, and the communication
  2346. channel, as a plist. It must return a string or nil.")
  2347. (defvar org-export-filter-verse-block-functions nil
  2348. "List of functions applied to a transcoded verse block.
  2349. Each filter is called with three arguments: the transcoded data,
  2350. as a string, the back-end, as a symbol, and the communication
  2351. channel, as a plist. It must return a string or nil.")
  2352. ;;;; Objects Filters
  2353. (defvar org-export-filter-bold-functions nil
  2354. "List of functions applied to transcoded bold text.
  2355. Each filter is called with three arguments: the transcoded data,
  2356. as a string, the back-end, as a symbol, and the communication
  2357. channel, as a plist. It must return a string or nil.")
  2358. (defvar org-export-filter-code-functions nil
  2359. "List of functions applied to transcoded code text.
  2360. Each filter is called with three arguments: the transcoded data,
  2361. as a string, the back-end, as a symbol, and the communication
  2362. channel, as a plist. It must return a string or nil.")
  2363. (defvar org-export-filter-entity-functions nil
  2364. "List of functions applied to a transcoded entity.
  2365. Each filter is called with three arguments: the transcoded data,
  2366. as a string, the back-end, as a symbol, and the communication
  2367. channel, as a plist. It must return a string or nil.")
  2368. (defvar org-export-filter-export-snippet-functions nil
  2369. "List of functions applied to a transcoded export-snippet.
  2370. Each filter is called with three arguments: the transcoded data,
  2371. as a string, the back-end, as a symbol, and the communication
  2372. channel, as a plist. It must return a string or nil.")
  2373. (defvar org-export-filter-footnote-reference-functions nil
  2374. "List of functions applied to a transcoded footnote-reference.
  2375. Each filter is called with three arguments: the transcoded data,
  2376. as a string, the back-end, as a symbol, and the communication
  2377. channel, as a plist. It must return a string or nil.")
  2378. (defvar org-export-filter-inline-babel-call-functions nil
  2379. "List of functions applied to a transcoded inline-babel-call.
  2380. Each filter is called with three arguments: the transcoded data,
  2381. as a string, the back-end, as a symbol, and the communication
  2382. channel, as a plist. It must return a string or nil.")
  2383. (defvar org-export-filter-inline-src-block-functions nil
  2384. "List of functions applied to a transcoded inline-src-block.
  2385. Each filter is called with three arguments: the transcoded data,
  2386. as a string, the back-end, as a symbol, and the communication
  2387. channel, as a plist. It must return a string or nil.")
  2388. (defvar org-export-filter-italic-functions nil
  2389. "List of functions applied to transcoded italic text.
  2390. Each filter is called with three arguments: the transcoded data,
  2391. as a string, the back-end, as a symbol, and the communication
  2392. channel, as a plist. It must return a string or nil.")
  2393. (defvar org-export-filter-latex-fragment-functions nil
  2394. "List of functions applied to a transcoded latex-fragment.
  2395. Each filter is called with three arguments: the transcoded data,
  2396. as a string, the back-end, as a symbol, and the communication
  2397. channel, as a plist. It must return a string or nil.")
  2398. (defvar org-export-filter-line-break-functions nil
  2399. "List of functions applied to a transcoded line-break.
  2400. Each filter is called with three arguments: the transcoded data,
  2401. as a string, the back-end, as a symbol, and the communication
  2402. channel, as a plist. It must return a string or nil.")
  2403. (defvar org-export-filter-link-functions nil
  2404. "List of functions applied to a transcoded link.
  2405. Each filter is called with three arguments: the transcoded data,
  2406. as a string, the back-end, as a symbol, and the communication
  2407. channel, as a plist. It must return a string or nil.")
  2408. (defvar org-export-filter-radio-target-functions nil
  2409. "List of functions applied to a transcoded radio-target.
  2410. Each filter is called with three arguments: the transcoded data,
  2411. as a string, the back-end, as a symbol, and the communication
  2412. channel, as a plist. It must return a string or nil.")
  2413. (defvar org-export-filter-statistics-cookie-functions nil
  2414. "List of functions applied to a transcoded statistics-cookie.
  2415. Each filter is called with three arguments: the transcoded data,
  2416. as a string, the back-end, as a symbol, and the communication
  2417. channel, as a plist. It must return a string or nil.")
  2418. (defvar org-export-filter-strike-through-functions nil
  2419. "List of functions applied to transcoded strike-through text.
  2420. Each filter is called with three arguments: the transcoded data,
  2421. as a string, the back-end, as a symbol, and the communication
  2422. channel, as a plist. It must return a string or nil.")
  2423. (defvar org-export-filter-subscript-functions nil
  2424. "List of functions applied to a transcoded subscript.
  2425. Each filter is called with three arguments: the transcoded data,
  2426. as a string, the back-end, as a symbol, and the communication
  2427. channel, as a plist. It must return a string or nil.")
  2428. (defvar org-export-filter-superscript-functions nil
  2429. "List of functions applied to a transcoded superscript.
  2430. Each filter is called with three arguments: the transcoded data,
  2431. as a string, the back-end, as a symbol, and the communication
  2432. channel, as a plist. It must return a string or nil.")
  2433. (defvar org-export-filter-target-functions nil
  2434. "List of functions applied to a transcoded target.
  2435. Each filter is called with three arguments: the transcoded data,
  2436. as a string, the back-end, as a symbol, and the communication
  2437. channel, as a plist. It must return a string or nil.")
  2438. (defvar org-export-filter-timestamp-functions nil
  2439. "List of functions applied to a transcoded timestamp.
  2440. Each filter is called with three arguments: the transcoded data,
  2441. as a string, the back-end, as a symbol, and the communication
  2442. channel, as a plist. It must return a string or nil.")
  2443. (defvar org-export-filter-underline-functions nil
  2444. "List of functions applied to transcoded underline text.
  2445. Each filter is called with three arguments: the transcoded data,
  2446. as a string, the back-end, as a symbol, and the communication
  2447. channel, as a plist. It must return a string or nil.")
  2448. (defvar org-export-filter-verbatim-functions nil
  2449. "List of functions applied to transcoded verbatim text.
  2450. Each filter is called with three arguments: the transcoded data,
  2451. as a string, the back-end, as a symbol, and the communication
  2452. channel, as a plist. It must return a string or nil.")
  2453. ;;;; Filters Tools
  2454. ;;
  2455. ;; Internal function `org-export-install-filters' installs filters
  2456. ;; hard-coded in back-ends (developer filters) and filters from global
  2457. ;; variables (user filters) in the communication channel.
  2458. ;;
  2459. ;; Internal function `org-export-filter-apply-functions' takes care
  2460. ;; about applying each filter in order to a given data. It ignores
  2461. ;; filters returning a nil value but stops whenever a filter returns
  2462. ;; an empty string.
  2463. (defun org-export-filter-apply-functions (filters value info)
  2464. "Call every function in FILTERS.
  2465. Functions are called with arguments VALUE, current export
  2466. back-end's name and INFO. A function returning a nil value will
  2467. be skipped. If it returns the empty string, the process ends and
  2468. VALUE is ignored.
  2469. Call is done in a LIFO fashion, to be sure that developer
  2470. specified filters, if any, are called first."
  2471. (catch 'exit
  2472. (let* ((backend (plist-get info :back-end))
  2473. (backend-name (and backend (org-export-backend-name backend))))
  2474. (dolist (filter filters value)
  2475. (let ((result (funcall filter value backend-name info)))
  2476. (cond ((not result) value)
  2477. ((equal value "") (throw 'exit nil))
  2478. (t (setq value result))))))))
  2479. (defun org-export-install-filters (info)
  2480. "Install filters properties in communication channel.
  2481. INFO is a plist containing the current communication channel.
  2482. Return the updated communication channel."
  2483. (let (plist)
  2484. ;; Install user-defined filters with `org-export-filters-alist'
  2485. ;; and filters already in INFO (through ext-plist mechanism).
  2486. (mapc (lambda (p)
  2487. (let* ((prop (car p))
  2488. (info-value (plist-get info prop))
  2489. (default-value (symbol-value (cdr p))))
  2490. (setq plist
  2491. (plist-put plist prop
  2492. ;; Filters in INFO will be called
  2493. ;; before those user provided.
  2494. (append (if (listp info-value) info-value
  2495. (list info-value))
  2496. default-value)))))
  2497. org-export-filters-alist)
  2498. ;; Prepend back-end specific filters to that list.
  2499. (mapc (lambda (p)
  2500. ;; Single values get consed, lists are appended.
  2501. (let ((key (car p)) (value (cdr p)))
  2502. (when value
  2503. (setq plist
  2504. (plist-put
  2505. plist key
  2506. (if (atom value) (cons value (plist-get plist key))
  2507. (append value (plist-get plist key))))))))
  2508. (org-export-get-all-filters (plist-get info :back-end)))
  2509. ;; Return new communication channel.
  2510. (org-combine-plists info plist)))
  2511. ;;; Core functions
  2512. ;;
  2513. ;; This is the room for the main function, `org-export-as', along with
  2514. ;; its derivative, `org-export-string-as'.
  2515. ;; `org-export--copy-to-kill-ring-p' determines if output of these
  2516. ;; function should be added to kill ring.
  2517. ;;
  2518. ;; Note that `org-export-as' doesn't really parse the current buffer,
  2519. ;; but a copy of it (with the same buffer-local variables and
  2520. ;; visibility), where macros and include keywords are expanded and
  2521. ;; Babel blocks are executed, if appropriate.
  2522. ;; `org-export-with-buffer-copy' macro prepares that copy.
  2523. ;;
  2524. ;; File inclusion is taken care of by
  2525. ;; `org-export-expand-include-keyword' and
  2526. ;; `org-export--prepare-file-contents'. Structure wise, including
  2527. ;; a whole Org file in a buffer often makes little sense. For
  2528. ;; example, if the file contains a headline and the include keyword
  2529. ;; was within an item, the item should contain the headline. That's
  2530. ;; why file inclusion should be done before any structure can be
  2531. ;; associated to the file, that is before parsing.
  2532. ;;
  2533. ;; `org-export-insert-default-template' is a command to insert
  2534. ;; a default template (or a back-end specific template) at point or in
  2535. ;; current subtree.
  2536. (defun org-export-copy-buffer ()
  2537. "Return a copy of the current buffer.
  2538. The copy preserves Org buffer-local variables, visibility and
  2539. narrowing."
  2540. (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
  2541. (new-buf (generate-new-buffer (buffer-name))))
  2542. (with-current-buffer new-buf
  2543. (funcall copy-buffer-fun)
  2544. (set-buffer-modified-p nil))
  2545. new-buf))
  2546. (defmacro org-export-with-buffer-copy (&rest body)
  2547. "Apply BODY in a copy of the current buffer.
  2548. The copy preserves local variables, visibility and contents of
  2549. the original buffer. Point is at the beginning of the buffer
  2550. when BODY is applied."
  2551. (declare (debug t))
  2552. (org-with-gensyms (buf-copy)
  2553. `(let ((,buf-copy (org-export-copy-buffer)))
  2554. (unwind-protect
  2555. (with-current-buffer ,buf-copy
  2556. (goto-char (point-min))
  2557. (progn ,@body))
  2558. (and (buffer-live-p ,buf-copy)
  2559. ;; Kill copy without confirmation.
  2560. (progn (with-current-buffer ,buf-copy
  2561. (restore-buffer-modified-p nil))
  2562. (kill-buffer ,buf-copy)))))))
  2563. (defun org-export--generate-copy-script (buffer)
  2564. "Generate a function duplicating BUFFER.
  2565. The copy will preserve local variables, visibility, contents and
  2566. narrowing of the original buffer. If a region was active in
  2567. BUFFER, contents will be narrowed to that region instead.
  2568. The resulting function can be evaled at a later time, from
  2569. another buffer, effectively cloning the original buffer there.
  2570. The function assumes BUFFER's major mode is `org-mode'."
  2571. (with-current-buffer buffer
  2572. `(lambda ()
  2573. (let ((inhibit-modification-hooks t))
  2574. ;; Set major mode. Ignore `org-mode-hook' as it has been run
  2575. ;; already in BUFFER.
  2576. (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
  2577. ;; Copy specific buffer local variables and variables set
  2578. ;; through BIND keywords.
  2579. ,@(let ((bound-variables (org-export--list-bound-variables))
  2580. vars)
  2581. (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
  2582. (when (consp entry)
  2583. (let ((var (car entry))
  2584. (val (cdr entry)))
  2585. (and (not (eq var 'org-font-lock-keywords))
  2586. (or (memq var
  2587. '(default-directory
  2588. buffer-file-name
  2589. buffer-file-coding-system))
  2590. (assq var bound-variables)
  2591. (string-match "^\\(org-\\|orgtbl-\\)"
  2592. (symbol-name var)))
  2593. ;; Skip unreadable values, as they cannot be
  2594. ;; sent to external process.
  2595. (or (not val) (ignore-errors (read (format "%S" val))))
  2596. (push `(set (make-local-variable (quote ,var))
  2597. (quote ,val))
  2598. vars))))))
  2599. ;; Whole buffer contents.
  2600. (insert
  2601. ,(org-with-wide-buffer
  2602. (buffer-substring-no-properties
  2603. (point-min) (point-max))))
  2604. ;; Narrowing.
  2605. ,(if (org-region-active-p)
  2606. `(narrow-to-region ,(region-beginning) ,(region-end))
  2607. `(narrow-to-region ,(point-min) ,(point-max)))
  2608. ;; Current position of point.
  2609. (goto-char ,(point))
  2610. ;; Overlays with invisible property.
  2611. ,@(let (ov-set)
  2612. (mapc
  2613. (lambda (ov)
  2614. (let ((invis-prop (overlay-get ov 'invisible)))
  2615. (when invis-prop
  2616. (push `(overlay-put
  2617. (make-overlay ,(overlay-start ov)
  2618. ,(overlay-end ov))
  2619. 'invisible (quote ,invis-prop))
  2620. ov-set))))
  2621. (overlays-in (point-min) (point-max)))
  2622. ov-set)))))
  2623. ;;;###autoload
  2624. (defun org-export-as
  2625. (backend &optional subtreep visible-only body-only ext-plist)
  2626. "Transcode current Org buffer into BACKEND code.
  2627. BACKEND is either an export back-end, as returned by, e.g.,
  2628. `org-export-create-backend', or a symbol referring to
  2629. a registered back-end.
  2630. If narrowing is active in the current buffer, only transcode its
  2631. narrowed part.
  2632. If a region is active, transcode that region.
  2633. When optional argument SUBTREEP is non-nil, transcode the
  2634. sub-tree at point, extracting information from the headline
  2635. properties first.
  2636. When optional argument VISIBLE-ONLY is non-nil, don't export
  2637. contents of hidden elements.
  2638. When optional argument BODY-ONLY is non-nil, only return body
  2639. code, without surrounding template.
  2640. Optional argument EXT-PLIST, when provided, is a property list
  2641. with external parameters overriding Org default settings, but
  2642. still inferior to file-local settings.
  2643. Return code as a string."
  2644. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  2645. (org-export-barf-if-invalid-backend backend)
  2646. (save-excursion
  2647. (save-restriction
  2648. ;; Narrow buffer to an appropriate region or subtree for
  2649. ;; parsing. If parsing subtree, be sure to remove main headline
  2650. ;; too.
  2651. (cond ((org-region-active-p)
  2652. (narrow-to-region (region-beginning) (region-end)))
  2653. (subtreep
  2654. (org-narrow-to-subtree)
  2655. (goto-char (point-min))
  2656. (forward-line)
  2657. (narrow-to-region (point) (point-max))))
  2658. ;; Initialize communication channel with original buffer
  2659. ;; attributes, unavailable in its copy.
  2660. (let* ((org-export-current-backend (org-export-backend-name backend))
  2661. (info (org-combine-plists
  2662. (list :export-options
  2663. (delq nil
  2664. (list (and subtreep 'subtree)
  2665. (and visible-only 'visible-only)
  2666. (and body-only 'body-only))))
  2667. (org-export--get-buffer-attributes)))
  2668. tree)
  2669. ;; Update communication channel and get parse tree. Buffer
  2670. ;; isn't parsed directly. Instead, a temporary copy is
  2671. ;; created, where include keywords, macros are expanded and
  2672. ;; code blocks are evaluated.
  2673. (org-export-with-buffer-copy
  2674. ;; Run first hook with current back-end's name as argument.
  2675. (run-hook-with-args 'org-export-before-processing-hook
  2676. (org-export-backend-name backend))
  2677. (org-export-expand-include-keyword)
  2678. ;; Update macro templates since #+INCLUDE keywords might have
  2679. ;; added some new ones.
  2680. (org-macro-initialize-templates)
  2681. (org-macro-replace-all org-macro-templates)
  2682. (org-export-execute-babel-code)
  2683. ;; Update radio targets since keyword inclusion might have
  2684. ;; added some more.
  2685. (org-update-radio-target-regexp)
  2686. ;; Run last hook with current back-end's name as argument.
  2687. (goto-char (point-min))
  2688. (save-excursion
  2689. (run-hook-with-args 'org-export-before-parsing-hook
  2690. (org-export-backend-name backend)))
  2691. ;; Update communication channel with environment. Also
  2692. ;; install user's and developer's filters.
  2693. (setq info
  2694. (org-export-install-filters
  2695. (org-combine-plists
  2696. info (org-export-get-environment backend subtreep ext-plist))))
  2697. ;; Expand export-specific set of macros: {{{author}}},
  2698. ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
  2699. ;; once regular macros have been expanded, since document
  2700. ;; keywords may contain one of them.
  2701. (org-macro-replace-all
  2702. (list (cons "author"
  2703. (org-element-interpret-data (plist-get info :author)))
  2704. (cons "date"
  2705. (org-element-interpret-data (plist-get info :date)))
  2706. ;; EMAIL is not a parsed keyword: store it as-is.
  2707. (cons "email" (or (plist-get info :email) ""))
  2708. (cons "title"
  2709. (org-element-interpret-data (plist-get info :title)))))
  2710. ;; Call options filters and update export options. We do not
  2711. ;; use `org-export-filter-apply-functions' here since the
  2712. ;; arity of such filters is different.
  2713. (let ((backend-name (org-export-backend-name backend)))
  2714. (dolist (filter (plist-get info :filter-options))
  2715. (let ((result (funcall filter info backend-name)))
  2716. (when result (setq info result)))))
  2717. ;; Parse buffer and call parse-tree filter on it.
  2718. (setq tree
  2719. (org-export-filter-apply-functions
  2720. (plist-get info :filter-parse-tree)
  2721. (org-element-parse-buffer nil visible-only) info))
  2722. ;; Now tree is complete, compute its properties and add them
  2723. ;; to communication channel.
  2724. (setq info
  2725. (org-combine-plists
  2726. info (org-export-collect-tree-properties tree info)))
  2727. ;; Eventually transcode TREE. Wrap the resulting string into
  2728. ;; a template.
  2729. (let* ((body (org-element-normalize-string
  2730. (or (org-export-data tree info) "")))
  2731. (inner-template (cdr (assq 'inner-template
  2732. (plist-get info :translate-alist))))
  2733. (full-body (if (not (functionp inner-template)) body
  2734. (funcall inner-template body info)))
  2735. (template (cdr (assq 'template
  2736. (plist-get info :translate-alist)))))
  2737. ;; Remove all text properties since they cannot be
  2738. ;; retrieved from an external process. Finally call
  2739. ;; final-output filter and return result.
  2740. (org-no-properties
  2741. (org-export-filter-apply-functions
  2742. (plist-get info :filter-final-output)
  2743. (if (or (not (functionp template)) body-only) full-body
  2744. (funcall template full-body info))
  2745. info))))))))
  2746. ;;;###autoload
  2747. (defun org-export-string-as (string backend &optional body-only ext-plist)
  2748. "Transcode STRING into BACKEND code.
  2749. BACKEND is either an export back-end, as returned by, e.g.,
  2750. `org-export-create-backend', or a symbol referring to
  2751. a registered back-end.
  2752. When optional argument BODY-ONLY is non-nil, only return body
  2753. code, without preamble nor postamble.
  2754. Optional argument EXT-PLIST, when provided, is a property list
  2755. with external parameters overriding Org default settings, but
  2756. still inferior to file-local settings.
  2757. Return code as a string."
  2758. (with-temp-buffer
  2759. (insert string)
  2760. (let ((org-inhibit-startup t)) (org-mode))
  2761. (org-export-as backend nil nil body-only ext-plist)))
  2762. ;;;###autoload
  2763. (defun org-export-replace-region-by (backend)
  2764. "Replace the active region by its export to BACKEND.
  2765. BACKEND is either an export back-end, as returned by, e.g.,
  2766. `org-export-create-backend', or a symbol referring to
  2767. a registered back-end."
  2768. (if (not (org-region-active-p))
  2769. (user-error "No active region to replace")
  2770. (let* ((beg (region-beginning))
  2771. (end (region-end))
  2772. (str (buffer-substring beg end)) rpl)
  2773. (setq rpl (org-export-string-as str backend t))
  2774. (delete-region beg end)
  2775. (insert rpl))))
  2776. ;;;###autoload
  2777. (defun org-export-insert-default-template (&optional backend subtreep)
  2778. "Insert all export keywords with default values at beginning of line.
  2779. BACKEND is a symbol referring to the name of a registered export
  2780. back-end, for which specific export options should be added to
  2781. the template, or `default' for default template. When it is nil,
  2782. the user will be prompted for a category.
  2783. If SUBTREEP is non-nil, export configuration will be set up
  2784. locally for the subtree through node properties."
  2785. (interactive)
  2786. (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
  2787. (when (and subtreep (org-before-first-heading-p))
  2788. (user-error "No subtree to set export options for"))
  2789. (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
  2790. (backend
  2791. (or backend
  2792. (intern
  2793. (org-completing-read
  2794. "Options category: "
  2795. (cons "default"
  2796. (mapcar (lambda (b)
  2797. (symbol-name (org-export-backend-name b)))
  2798. org-export--registered-backends))))))
  2799. options keywords)
  2800. ;; Populate OPTIONS and KEYWORDS.
  2801. (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
  2802. ((org-export-backend-p backend)
  2803. (org-export-get-all-options backend))
  2804. (t (org-export-get-all-options
  2805. (org-export-get-backend backend)))))
  2806. (let ((keyword (nth 1 entry))
  2807. (option (nth 2 entry)))
  2808. (cond
  2809. (keyword (unless (assoc keyword keywords)
  2810. (let ((value
  2811. (if (eq (nth 4 entry) 'split)
  2812. (mapconcat 'identity (eval (nth 3 entry)) " ")
  2813. (eval (nth 3 entry)))))
  2814. (push (cons keyword value) keywords))))
  2815. (option (unless (assoc option options)
  2816. (push (cons option (eval (nth 3 entry))) options))))))
  2817. ;; Move to an appropriate location in order to insert options.
  2818. (unless subtreep (beginning-of-line))
  2819. ;; First get TITLE, DATE, AUTHOR and EMAIL if they belong to the
  2820. ;; list of available keywords.
  2821. (when (assoc "TITLE" keywords)
  2822. (let ((title
  2823. (or (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  2824. (and visited-file
  2825. (file-name-sans-extension
  2826. (file-name-nondirectory visited-file))))
  2827. (buffer-name (buffer-base-buffer)))))
  2828. (if (not subtreep) (insert (format "#+TITLE: %s\n" title))
  2829. (org-entry-put node "EXPORT_TITLE" title))))
  2830. (when (assoc "DATE" keywords)
  2831. (let ((date (with-temp-buffer (org-insert-time-stamp (current-time)))))
  2832. (if (not subtreep) (insert "#+DATE: " date "\n")
  2833. (org-entry-put node "EXPORT_DATE" date))))
  2834. (when (assoc "AUTHOR" keywords)
  2835. (let ((author (cdr (assoc "AUTHOR" keywords))))
  2836. (if subtreep (org-entry-put node "EXPORT_AUTHOR" author)
  2837. (insert
  2838. (format "#+AUTHOR:%s\n"
  2839. (if (not (org-string-nw-p author)) ""
  2840. (concat " " author)))))))
  2841. (when (assoc "EMAIL" keywords)
  2842. (let ((email (cdr (assoc "EMAIL" keywords))))
  2843. (if subtreep (org-entry-put node "EXPORT_EMAIL" email)
  2844. (insert
  2845. (format "#+EMAIL:%s\n"
  2846. (if (not (org-string-nw-p email)) ""
  2847. (concat " " email)))))))
  2848. ;; Then (multiple) OPTIONS lines. Never go past fill-column.
  2849. (when options
  2850. (let ((items
  2851. (mapcar
  2852. #'(lambda (opt) (format "%s:%S" (car opt) (cdr opt)))
  2853. (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
  2854. (if subtreep
  2855. (org-entry-put
  2856. node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
  2857. (while items
  2858. (insert "#+OPTIONS:")
  2859. (let ((width 10))
  2860. (while (and items
  2861. (< (+ width (length (car items)) 1) fill-column))
  2862. (let ((item (pop items)))
  2863. (insert " " item)
  2864. (incf width (1+ (length item))))))
  2865. (insert "\n")))))
  2866. ;; And the rest of keywords.
  2867. (dolist (key (sort keywords (lambda (k1 k2) (string< (car k1) (car k2)))))
  2868. (unless (member (car key) '("TITLE" "DATE" "AUTHOR" "EMAIL"))
  2869. (let ((val (cdr key)))
  2870. (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
  2871. (insert
  2872. (format "#+%s:%s\n"
  2873. (car key)
  2874. (if (org-string-nw-p val) (format " %s" val) "")))))))))
  2875. (defun org-export-expand-include-keyword (&optional included dir)
  2876. "Expand every include keyword in buffer.
  2877. Optional argument INCLUDED is a list of included file names along
  2878. with their line restriction, when appropriate. It is used to
  2879. avoid infinite recursion. Optional argument DIR is the current
  2880. working directory. It is used to properly resolve relative
  2881. paths."
  2882. (let ((case-fold-search t))
  2883. (goto-char (point-min))
  2884. (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
  2885. (let ((element (save-match-data (org-element-at-point))))
  2886. (when (eq (org-element-type element) 'keyword)
  2887. (beginning-of-line)
  2888. ;; Extract arguments from keyword's value.
  2889. (let* ((value (org-element-property :value element))
  2890. (ind (org-get-indentation))
  2891. (file (and (string-match
  2892. "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
  2893. (prog1 (expand-file-name
  2894. (org-remove-double-quotes
  2895. (match-string 1 value))
  2896. dir)
  2897. (setq value (replace-match "" nil nil value)))))
  2898. (lines
  2899. (and (string-match
  2900. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
  2901. value)
  2902. (prog1 (match-string 1 value)
  2903. (setq value (replace-match "" nil nil value)))))
  2904. (env (cond ((string-match "\\<example\\>" value) 'example)
  2905. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  2906. (match-string 1 value))))
  2907. ;; Minimal level of included file defaults to the child
  2908. ;; level of the current headline, if any, or one. It
  2909. ;; only applies is the file is meant to be included as
  2910. ;; an Org one.
  2911. (minlevel
  2912. (and (not env)
  2913. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  2914. (prog1 (string-to-number (match-string 1 value))
  2915. (setq value (replace-match "" nil nil value)))
  2916. (let ((cur (org-current-level)))
  2917. (if cur (1+ (org-reduced-level cur)) 1))))))
  2918. ;; Remove keyword.
  2919. (delete-region (point) (progn (forward-line) (point)))
  2920. (cond
  2921. ((not file) nil)
  2922. ((not (file-readable-p file))
  2923. (error "Cannot include file %s" file))
  2924. ;; Check if files has already been parsed. Look after
  2925. ;; inclusion lines too, as different parts of the same file
  2926. ;; can be included too.
  2927. ((member (list file lines) included)
  2928. (error "Recursive file inclusion: %s" file))
  2929. (t
  2930. (cond
  2931. ((eq env 'example)
  2932. (insert
  2933. (let ((ind-str (make-string ind ? ))
  2934. (contents
  2935. (org-escape-code-in-string
  2936. (org-export--prepare-file-contents file lines))))
  2937. (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
  2938. ind-str contents ind-str))))
  2939. ((stringp env)
  2940. (insert
  2941. (let ((ind-str (make-string ind ? ))
  2942. (contents
  2943. (org-escape-code-in-string
  2944. (org-export--prepare-file-contents file lines))))
  2945. (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
  2946. ind-str env contents ind-str))))
  2947. (t
  2948. (insert
  2949. (with-temp-buffer
  2950. (let ((org-inhibit-startup t)) (org-mode))
  2951. (insert
  2952. (org-export--prepare-file-contents file lines ind minlevel))
  2953. (org-export-expand-include-keyword
  2954. (cons (list file lines) included)
  2955. (file-name-directory file))
  2956. (buffer-string)))))))))))))
  2957. (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
  2958. "Prepare the contents of FILE for inclusion and return them as a string.
  2959. When optional argument LINES is a string specifying a range of
  2960. lines, include only those lines.
  2961. Optional argument IND, when non-nil, is an integer specifying the
  2962. global indentation of returned contents. Since its purpose is to
  2963. allow an included file to stay in the same environment it was
  2964. created \(i.e. a list item), it doesn't apply past the first
  2965. headline encountered.
  2966. Optional argument MINLEVEL, when non-nil, is an integer
  2967. specifying the level that any top-level headline in the included
  2968. file should have."
  2969. (with-temp-buffer
  2970. (insert-file-contents file)
  2971. (when lines
  2972. (let* ((lines (split-string lines "-"))
  2973. (lbeg (string-to-number (car lines)))
  2974. (lend (string-to-number (cadr lines)))
  2975. (beg (if (zerop lbeg) (point-min)
  2976. (goto-char (point-min))
  2977. (forward-line (1- lbeg))
  2978. (point)))
  2979. (end (if (zerop lend) (point-max)
  2980. (goto-char (point-min))
  2981. (forward-line (1- lend))
  2982. (point))))
  2983. (narrow-to-region beg end)))
  2984. ;; Remove blank lines at beginning and end of contents. The logic
  2985. ;; behind that removal is that blank lines around include keyword
  2986. ;; override blank lines in included file.
  2987. (goto-char (point-min))
  2988. (org-skip-whitespace)
  2989. (beginning-of-line)
  2990. (delete-region (point-min) (point))
  2991. (goto-char (point-max))
  2992. (skip-chars-backward " \r\t\n")
  2993. (forward-line)
  2994. (delete-region (point) (point-max))
  2995. ;; If IND is set, preserve indentation of include keyword until
  2996. ;; the first headline encountered.
  2997. (when ind
  2998. (unless (eq major-mode 'org-mode)
  2999. (let ((org-inhibit-startup t)) (org-mode)))
  3000. (goto-char (point-min))
  3001. (let ((ind-str (make-string ind ? )))
  3002. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  3003. ;; Do not move footnote definitions out of column 0.
  3004. (unless (and (looking-at org-footnote-definition-re)
  3005. (eq (org-element-type (org-element-at-point))
  3006. 'footnote-definition))
  3007. (insert ind-str))
  3008. (forward-line))))
  3009. ;; When MINLEVEL is specified, compute minimal level for headlines
  3010. ;; in the file (CUR-MIN), and remove stars to each headline so
  3011. ;; that headlines with minimal level have a level of MINLEVEL.
  3012. (when minlevel
  3013. (unless (eq major-mode 'org-mode)
  3014. (let ((org-inhibit-startup t)) (org-mode)))
  3015. (org-with-limited-levels
  3016. (let ((levels (org-map-entries
  3017. (lambda () (org-reduced-level (org-current-level))))))
  3018. (when levels
  3019. (let ((offset (- minlevel (apply 'min levels))))
  3020. (unless (zerop offset)
  3021. (when org-odd-levels-only (setq offset (* offset 2)))
  3022. ;; Only change stars, don't bother moving whole
  3023. ;; sections.
  3024. (org-map-entries
  3025. (lambda () (if (< offset 0) (delete-char (abs offset))
  3026. (insert (make-string offset ?*)))))))))))
  3027. (org-element-normalize-string (buffer-string))))
  3028. (defun org-export-execute-babel-code ()
  3029. "Execute every Babel code in the visible part of current buffer."
  3030. ;; Get a pristine copy of current buffer so Babel references can be
  3031. ;; properly resolved.
  3032. (let ((reference (org-export-copy-buffer)))
  3033. (unwind-protect (let ((org-current-export-file reference))
  3034. (org-babel-exp-process-buffer))
  3035. (kill-buffer reference))))
  3036. (defun org-export--copy-to-kill-ring-p ()
  3037. "Return a non-nil value when output should be added to the kill ring.
  3038. See also `org-export-copy-to-kill-ring'."
  3039. (if (eq org-export-copy-to-kill-ring 'if-interactive)
  3040. (not (or executing-kbd-macro noninteractive))
  3041. (eq org-export-copy-to-kill-ring t)))
  3042. ;;; Tools For Back-Ends
  3043. ;;
  3044. ;; A whole set of tools is available to help build new exporters. Any
  3045. ;; function general enough to have its use across many back-ends
  3046. ;; should be added here.
  3047. ;;;; For Affiliated Keywords
  3048. ;;
  3049. ;; `org-export-read-attribute' reads a property from a given element
  3050. ;; as a plist. It can be used to normalize affiliated keywords'
  3051. ;; syntax.
  3052. ;;
  3053. ;; Since captions can span over multiple lines and accept dual values,
  3054. ;; their internal representation is a bit tricky. Therefore,
  3055. ;; `org-export-get-caption' transparently returns a given element's
  3056. ;; caption as a secondary string.
  3057. (defun org-export-read-attribute (attribute element &optional property)
  3058. "Turn ATTRIBUTE property from ELEMENT into a plist.
  3059. When optional argument PROPERTY is non-nil, return the value of
  3060. that property within attributes.
  3061. This function assumes attributes are defined as \":keyword
  3062. value\" pairs. It is appropriate for `:attr_html' like
  3063. properties.
  3064. All values will become strings except the empty string and
  3065. \"nil\", which will become nil. Also, values containing only
  3066. double quotes will be read as-is, which means that \"\" value
  3067. will become the empty string."
  3068. (let* ((prepare-value
  3069. (lambda (str)
  3070. (save-match-data
  3071. (cond ((member str '(nil "" "nil")) nil)
  3072. ((string-match "^\"\\(\"+\\)?\"$" str)
  3073. (or (match-string 1 str) ""))
  3074. (t str)))))
  3075. (attributes
  3076. (let ((value (org-element-property attribute element)))
  3077. (when value
  3078. (let ((s (mapconcat 'identity value " ")) result)
  3079. (while (string-match
  3080. "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
  3081. s)
  3082. (let ((value (substring s 0 (match-beginning 0))))
  3083. (push (funcall prepare-value value) result))
  3084. (push (intern (match-string 1 s)) result)
  3085. (setq s (substring s (match-end 0))))
  3086. ;; Ignore any string before first property with `cdr'.
  3087. (cdr (nreverse (cons (funcall prepare-value s) result))))))))
  3088. (if property (plist-get attributes property) attributes)))
  3089. (defun org-export-get-caption (element &optional shortp)
  3090. "Return caption from ELEMENT as a secondary string.
  3091. When optional argument SHORTP is non-nil, return short caption,
  3092. as a secondary string, instead.
  3093. Caption lines are separated by a white space."
  3094. (let ((full-caption (org-element-property :caption element)) caption)
  3095. (dolist (line full-caption (cdr caption))
  3096. (let ((cap (funcall (if shortp 'cdr 'car) line)))
  3097. (when cap
  3098. (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
  3099. ;;;; For Derived Back-ends
  3100. ;;
  3101. ;; `org-export-with-backend' is a function allowing to locally use
  3102. ;; another back-end to transcode some object or element. In a derived
  3103. ;; back-end, it may be used as a fall-back function once all specific
  3104. ;; cases have been treated.
  3105. (defun org-export-with-backend (backend data &optional contents info)
  3106. "Call a transcoder from BACKEND on DATA.
  3107. BACKEND is an export back-end, as returned by, e.g.,
  3108. `org-export-create-backend', or a symbol referring to
  3109. a registered back-end. DATA is an Org element, object, secondary
  3110. string or string. CONTENTS, when non-nil, is the transcoded
  3111. contents of DATA element, as a string. INFO, when non-nil, is
  3112. the communication channel used for export, as a plist."
  3113. (when (symbolp backend) (setq backend (org-export-get-backend backend)))
  3114. (org-export-barf-if-invalid-backend backend)
  3115. (let ((type (org-element-type data)))
  3116. (if (memq type '(nil org-data)) (error "No foreign transcoder available")
  3117. (let* ((all-transcoders (org-export-get-all-transcoders backend))
  3118. (transcoder (cdr (assq type all-transcoders))))
  3119. (if (not (functionp transcoder))
  3120. (error "No foreign transcoder available")
  3121. (funcall
  3122. transcoder data contents
  3123. (org-combine-plists
  3124. info (list :back-end backend
  3125. :translate-alist all-transcoders
  3126. :exported-data (make-hash-table :test 'eq :size 401)))))))))
  3127. ;;;; For Export Snippets
  3128. ;;
  3129. ;; Every export snippet is transmitted to the back-end. Though, the
  3130. ;; latter will only retain one type of export-snippet, ignoring
  3131. ;; others, based on the former's target back-end. The function
  3132. ;; `org-export-snippet-backend' returns that back-end for a given
  3133. ;; export-snippet.
  3134. (defun org-export-snippet-backend (export-snippet)
  3135. "Return EXPORT-SNIPPET targeted back-end as a symbol.
  3136. Translation, with `org-export-snippet-translation-alist', is
  3137. applied."
  3138. (let ((back-end (org-element-property :back-end export-snippet)))
  3139. (intern
  3140. (or (cdr (assoc back-end org-export-snippet-translation-alist))
  3141. back-end))))
  3142. ;;;; For Footnotes
  3143. ;;
  3144. ;; `org-export-collect-footnote-definitions' is a tool to list
  3145. ;; actually used footnotes definitions in the whole parse tree, or in
  3146. ;; a headline, in order to add footnote listings throughout the
  3147. ;; transcoded data.
  3148. ;;
  3149. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  3150. ;; back-ends, when they need to attach the footnote definition only to
  3151. ;; the first occurrence of the corresponding label.
  3152. ;;
  3153. ;; `org-export-get-footnote-definition' and
  3154. ;; `org-export-get-footnote-number' provide easier access to
  3155. ;; additional information relative to a footnote reference.
  3156. (defun org-export-collect-footnote-definitions (data info)
  3157. "Return an alist between footnote numbers, labels and definitions.
  3158. DATA is the parse tree from which definitions are collected.
  3159. INFO is the plist used as a communication channel.
  3160. Definitions are sorted by order of references. They either
  3161. appear as Org data or as a secondary string for inlined
  3162. footnotes. Unreferenced definitions are ignored."
  3163. (let* (num-alist
  3164. collect-fn ; for byte-compiler.
  3165. (collect-fn
  3166. (function
  3167. (lambda (data)
  3168. ;; Collect footnote number, label and definition in DATA.
  3169. (org-element-map data 'footnote-reference
  3170. (lambda (fn)
  3171. (when (org-export-footnote-first-reference-p fn info)
  3172. (let ((def (org-export-get-footnote-definition fn info)))
  3173. (push
  3174. (list (org-export-get-footnote-number fn info)
  3175. (org-element-property :label fn)
  3176. def)
  3177. num-alist)
  3178. ;; Also search in definition for nested footnotes.
  3179. (when (eq (org-element-property :type fn) 'standard)
  3180. (funcall collect-fn def)))))
  3181. ;; Don't enter footnote definitions since it will happen
  3182. ;; when their first reference is found.
  3183. info nil 'footnote-definition)))))
  3184. (funcall collect-fn (plist-get info :parse-tree))
  3185. (reverse num-alist)))
  3186. (defun org-export-footnote-first-reference-p (footnote-reference info)
  3187. "Non-nil when a footnote reference is the first one for its label.
  3188. FOOTNOTE-REFERENCE is the footnote reference being considered.
  3189. INFO is the plist used as a communication channel."
  3190. (let ((label (org-element-property :label footnote-reference)))
  3191. ;; Anonymous footnotes are always a first reference.
  3192. (if (not label) t
  3193. ;; Otherwise, return the first footnote with the same LABEL and
  3194. ;; test if it is equal to FOOTNOTE-REFERENCE.
  3195. (let* (search-refs ; for byte-compiler.
  3196. (search-refs
  3197. (function
  3198. (lambda (data)
  3199. (org-element-map data 'footnote-reference
  3200. (lambda (fn)
  3201. (cond
  3202. ((string= (org-element-property :label fn) label)
  3203. (throw 'exit fn))
  3204. ;; If FN isn't inlined, be sure to traverse its
  3205. ;; definition before resuming search. See
  3206. ;; comments in `org-export-get-footnote-number'
  3207. ;; for more information.
  3208. ((eq (org-element-property :type fn) 'standard)
  3209. (funcall search-refs
  3210. (org-export-get-footnote-definition fn info)))))
  3211. ;; Don't enter footnote definitions since it will
  3212. ;; happen when their first reference is found.
  3213. info 'first-match 'footnote-definition)))))
  3214. (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
  3215. footnote-reference)))))
  3216. (defun org-export-get-footnote-definition (footnote-reference info)
  3217. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  3218. INFO is the plist used as a communication channel. If no such
  3219. definition can be found, return the \"DEFINITION NOT FOUND\"
  3220. string."
  3221. (let ((label (org-element-property :label footnote-reference)))
  3222. (or (org-element-property :inline-definition footnote-reference)
  3223. (cdr (assoc label (plist-get info :footnote-definition-alist)))
  3224. "DEFINITION NOT FOUND.")))
  3225. (defun org-export-get-footnote-number (footnote info)
  3226. "Return number associated to a footnote.
  3227. FOOTNOTE is either a footnote reference or a footnote definition.
  3228. INFO is the plist used as a communication channel."
  3229. (let* ((label (org-element-property :label footnote))
  3230. seen-refs
  3231. search-ref ; For byte-compiler.
  3232. (search-ref
  3233. (function
  3234. (lambda (data)
  3235. ;; Search footnote references through DATA, filling
  3236. ;; SEEN-REFS along the way.
  3237. (org-element-map data 'footnote-reference
  3238. (lambda (fn)
  3239. (let ((fn-lbl (org-element-property :label fn)))
  3240. (cond
  3241. ;; Anonymous footnote match: return number.
  3242. ((and (not fn-lbl) (eq fn footnote))
  3243. (throw 'exit (1+ (length seen-refs))))
  3244. ;; Labels match: return number.
  3245. ((and label (string= label fn-lbl))
  3246. (throw 'exit (1+ (length seen-refs))))
  3247. ;; Anonymous footnote: it's always a new one.
  3248. ;; Also, be sure to return nil from the `cond' so
  3249. ;; `first-match' doesn't get us out of the loop.
  3250. ((not fn-lbl) (push 'inline seen-refs) nil)
  3251. ;; Label not seen so far: add it so SEEN-REFS.
  3252. ;;
  3253. ;; Also search for subsequent references in
  3254. ;; footnote definition so numbering follows
  3255. ;; reading logic. Note that we don't have to care
  3256. ;; about inline definitions, since
  3257. ;; `org-element-map' already traverses them at the
  3258. ;; right time.
  3259. ;;
  3260. ;; Once again, return nil to stay in the loop.
  3261. ((not (member fn-lbl seen-refs))
  3262. (push fn-lbl seen-refs)
  3263. (funcall search-ref
  3264. (org-export-get-footnote-definition fn info))
  3265. nil))))
  3266. ;; Don't enter footnote definitions since it will
  3267. ;; happen when their first reference is found.
  3268. info 'first-match 'footnote-definition)))))
  3269. (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
  3270. ;;;; For Headlines
  3271. ;;
  3272. ;; `org-export-get-relative-level' is a shortcut to get headline
  3273. ;; level, relatively to the lower headline level in the parsed tree.
  3274. ;;
  3275. ;; `org-export-get-headline-number' returns the section number of an
  3276. ;; headline, while `org-export-number-to-roman' allows to convert it
  3277. ;; to roman numbers.
  3278. ;;
  3279. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  3280. ;; `org-export-last-sibling-p' are three useful predicates when it
  3281. ;; comes to fulfill the `:headline-levels' property.
  3282. ;;
  3283. ;; `org-export-get-tags', `org-export-get-category' and
  3284. ;; `org-export-get-node-property' extract useful information from an
  3285. ;; headline or a parent headline. They all handle inheritance.
  3286. ;;
  3287. ;; `org-export-get-alt-title' tries to retrieve an alternative title,
  3288. ;; as a secondary string, suitable for table of contents. It falls
  3289. ;; back onto default title.
  3290. (defun org-export-get-relative-level (headline info)
  3291. "Return HEADLINE relative level within current parsed tree.
  3292. INFO is a plist holding contextual information."
  3293. (+ (org-element-property :level headline)
  3294. (or (plist-get info :headline-offset) 0)))
  3295. (defun org-export-low-level-p (headline info)
  3296. "Non-nil when HEADLINE is considered as low level.
  3297. INFO is a plist used as a communication channel.
  3298. A low level headlines has a relative level greater than
  3299. `:headline-levels' property value.
  3300. Return value is the difference between HEADLINE relative level
  3301. and the last level being considered as high enough, or nil."
  3302. (let ((limit (plist-get info :headline-levels)))
  3303. (when (wholenump limit)
  3304. (let ((level (org-export-get-relative-level headline info)))
  3305. (and (> level limit) (- level limit))))))
  3306. (defun org-export-get-headline-number (headline info)
  3307. "Return HEADLINE numbering as a list of numbers.
  3308. INFO is a plist holding contextual information."
  3309. (cdr (assoc headline (plist-get info :headline-numbering))))
  3310. (defun org-export-numbered-headline-p (headline info)
  3311. "Return a non-nil value if HEADLINE element should be numbered.
  3312. INFO is a plist used as a communication channel."
  3313. (let ((sec-num (plist-get info :section-numbers))
  3314. (level (org-export-get-relative-level headline info)))
  3315. (if (wholenump sec-num) (<= level sec-num) sec-num)))
  3316. (defun org-export-number-to-roman (n)
  3317. "Convert integer N into a roman numeral."
  3318. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  3319. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  3320. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  3321. ( 1 . "I")))
  3322. (res ""))
  3323. (if (<= n 0)
  3324. (number-to-string n)
  3325. (while roman
  3326. (if (>= n (caar roman))
  3327. (setq n (- n (caar roman))
  3328. res (concat res (cdar roman)))
  3329. (pop roman)))
  3330. res)))
  3331. (defun org-export-get-tags (element info &optional tags inherited)
  3332. "Return list of tags associated to ELEMENT.
  3333. ELEMENT has either an `headline' or an `inlinetask' type. INFO
  3334. is a plist used as a communication channel.
  3335. Select tags (see `org-export-select-tags') and exclude tags (see
  3336. `org-export-exclude-tags') are removed from the list.
  3337. When non-nil, optional argument TAGS should be a list of strings.
  3338. Any tag belonging to this list will also be removed.
  3339. When optional argument INHERITED is non-nil, tags can also be
  3340. inherited from parent headlines and FILETAGS keywords."
  3341. (org-remove-if
  3342. (lambda (tag) (or (member tag (plist-get info :select-tags))
  3343. (member tag (plist-get info :exclude-tags))
  3344. (member tag tags)))
  3345. (if (not inherited) (org-element-property :tags element)
  3346. ;; Build complete list of inherited tags.
  3347. (let ((current-tag-list (org-element-property :tags element)))
  3348. (mapc
  3349. (lambda (parent)
  3350. (mapc
  3351. (lambda (tag)
  3352. (when (and (memq (org-element-type parent) '(headline inlinetask))
  3353. (not (member tag current-tag-list)))
  3354. (push tag current-tag-list)))
  3355. (org-element-property :tags parent)))
  3356. (org-export-get-genealogy element))
  3357. ;; Add FILETAGS keywords and return results.
  3358. (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
  3359. (defun org-export-get-node-property (property blob &optional inherited)
  3360. "Return node PROPERTY value for BLOB.
  3361. PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
  3362. element or object.
  3363. If optional argument INHERITED is non-nil, the value can be
  3364. inherited from a parent headline.
  3365. Return value is a string or nil."
  3366. (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3367. (org-export-get-parent-headline blob))))
  3368. (if (not inherited) (org-element-property property blob)
  3369. (let ((parent headline) value)
  3370. (catch 'found
  3371. (while parent
  3372. (when (plist-member (nth 1 parent) property)
  3373. (throw 'found (org-element-property property parent)))
  3374. (setq parent (org-element-property :parent parent))))))))
  3375. (defun org-export-get-category (blob info)
  3376. "Return category for element or object BLOB.
  3377. INFO is a plist used as a communication channel.
  3378. CATEGORY is automatically inherited from a parent headline, from
  3379. #+CATEGORY: keyword or created out of original file name. If all
  3380. fail, the fall-back value is \"???\"."
  3381. (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
  3382. (org-export-get-parent-headline blob))))
  3383. ;; Almost like `org-export-node-property', but we cannot trust
  3384. ;; `plist-member' as every headline has a `:CATEGORY'
  3385. ;; property, would it be nil or equal to "???" (which has the
  3386. ;; same meaning).
  3387. (let ((parent headline) value)
  3388. (catch 'found
  3389. (while parent
  3390. (let ((category (org-element-property :CATEGORY parent)))
  3391. (and category (not (equal "???" category))
  3392. (throw 'found category)))
  3393. (setq parent (org-element-property :parent parent))))))
  3394. (org-element-map (plist-get info :parse-tree) 'keyword
  3395. (lambda (kwd)
  3396. (when (equal (org-element-property :key kwd) "CATEGORY")
  3397. (org-element-property :value kwd)))
  3398. info 'first-match)
  3399. (let ((file (plist-get info :input-file)))
  3400. (and file (file-name-sans-extension (file-name-nondirectory file))))
  3401. "???"))
  3402. (defun org-export-get-alt-title (headline info)
  3403. "Return alternative title for HEADLINE, as a secondary string.
  3404. INFO is a plist used as a communication channel. If no optional
  3405. title is defined, fall-back to the regular title."
  3406. (or (org-element-property :alt-title headline)
  3407. (org-element-property :title headline)))
  3408. (defun org-export-first-sibling-p (headline info)
  3409. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  3410. INFO is a plist used as a communication channel."
  3411. (not (eq (org-element-type (org-export-get-previous-element headline info))
  3412. 'headline)))
  3413. (defun org-export-last-sibling-p (headline info)
  3414. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  3415. INFO is a plist used as a communication channel."
  3416. (not (org-export-get-next-element headline info)))
  3417. ;;;; For Keywords
  3418. ;;
  3419. ;; `org-export-get-date' returns a date appropriate for the document
  3420. ;; to about to be exported. In particular, it takes care of
  3421. ;; `org-export-date-timestamp-format'.
  3422. (defun org-export-get-date (info &optional fmt)
  3423. "Return date value for the current document.
  3424. INFO is a plist used as a communication channel. FMT, when
  3425. non-nil, is a time format string that will be applied on the date
  3426. if it consists in a single timestamp object. It defaults to
  3427. `org-export-date-timestamp-format' when nil.
  3428. A proper date can be a secondary string, a string or nil. It is
  3429. meant to be translated with `org-export-data' or alike."
  3430. (let ((date (plist-get info :date))
  3431. (fmt (or fmt org-export-date-timestamp-format)))
  3432. (cond ((not date) nil)
  3433. ((and fmt
  3434. (not (cdr date))
  3435. (eq (org-element-type (car date)) 'timestamp))
  3436. (org-timestamp-format (car date) fmt))
  3437. (t date))))
  3438. ;;;; For Links
  3439. ;;
  3440. ;; `org-export-solidify-link-text' turns a string into a safer version
  3441. ;; for links, replacing most non-standard characters with hyphens.
  3442. ;;
  3443. ;; `org-export-get-coderef-format' returns an appropriate format
  3444. ;; string for coderefs.
  3445. ;;
  3446. ;; `org-export-inline-image-p' returns a non-nil value when the link
  3447. ;; provided should be considered as an inline image.
  3448. ;;
  3449. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  3450. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  3451. ;; returns an appropriate unique identifier when found, or nil.
  3452. ;;
  3453. ;; `org-export-resolve-id-link' returns the first headline with
  3454. ;; specified id or custom-id in parse tree, the path to the external
  3455. ;; file with the id or nil when neither was found.
  3456. ;;
  3457. ;; `org-export-resolve-coderef' associates a reference to a line
  3458. ;; number in the element it belongs, or returns the reference itself
  3459. ;; when the element isn't numbered.
  3460. (defun org-export-solidify-link-text (s)
  3461. "Take link text S and make a safe target out of it."
  3462. (save-match-data
  3463. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
  3464. (defun org-export-get-coderef-format (path desc)
  3465. "Return format string for code reference link.
  3466. PATH is the link path. DESC is its description."
  3467. (save-match-data
  3468. (cond ((not desc) "%s")
  3469. ((string-match (regexp-quote (concat "(" path ")")) desc)
  3470. (replace-match "%s" t t desc))
  3471. (t desc))))
  3472. (defun org-export-inline-image-p (link &optional rules)
  3473. "Non-nil if LINK object points to an inline image.
  3474. Optional argument is a set of RULES defining inline images. It
  3475. is an alist where associations have the following shape:
  3476. \(TYPE . REGEXP)
  3477. Applying a rule means apply REGEXP against LINK's path when its
  3478. type is TYPE. The function will return a non-nil value if any of
  3479. the provided rules is non-nil. The default rule is
  3480. `org-export-default-inline-image-rule'.
  3481. This only applies to links without a description."
  3482. (and (not (org-element-contents link))
  3483. (let ((case-fold-search t)
  3484. (rules (or rules org-export-default-inline-image-rule)))
  3485. (catch 'exit
  3486. (mapc
  3487. (lambda (rule)
  3488. (and (string= (org-element-property :type link) (car rule))
  3489. (string-match (cdr rule)
  3490. (org-element-property :path link))
  3491. (throw 'exit t)))
  3492. rules)
  3493. ;; Return nil if no rule matched.
  3494. nil))))
  3495. (defun org-export-resolve-coderef (ref info)
  3496. "Resolve a code reference REF.
  3497. INFO is a plist used as a communication channel.
  3498. Return associated line number in source code, or REF itself,
  3499. depending on src-block or example element's switches."
  3500. (org-element-map (plist-get info :parse-tree) '(example-block src-block)
  3501. (lambda (el)
  3502. (with-temp-buffer
  3503. (insert (org-trim (org-element-property :value el)))
  3504. (let* ((label-fmt (regexp-quote
  3505. (or (org-element-property :label-fmt el)
  3506. org-coderef-label-format)))
  3507. (ref-re
  3508. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  3509. (replace-regexp-in-string "%s" ref label-fmt nil t))))
  3510. ;; Element containing REF is found. Resolve it to either
  3511. ;; a label or a line number, as needed.
  3512. (when (re-search-backward ref-re nil t)
  3513. (cond
  3514. ((org-element-property :use-labels el) ref)
  3515. ((eq (org-element-property :number-lines el) 'continued)
  3516. (+ (org-export-get-loc el info) (line-number-at-pos)))
  3517. (t (line-number-at-pos)))))))
  3518. info 'first-match))
  3519. (defun org-export-resolve-fuzzy-link (link info)
  3520. "Return LINK destination.
  3521. INFO is a plist holding contextual information.
  3522. Return value can be an object, an element, or nil:
  3523. - If LINK path matches a target object (i.e. <<path>>) return it.
  3524. - If LINK path exactly matches the name affiliated keyword
  3525. \(i.e. #+NAME: path) of an element, return that element.
  3526. - If LINK path exactly matches any headline name, return that
  3527. element. If more than one headline share that name, priority
  3528. will be given to the one with the closest common ancestor, if
  3529. any, or the first one in the parse tree otherwise.
  3530. - Otherwise, return nil.
  3531. Assume LINK type is \"fuzzy\". White spaces are not
  3532. significant."
  3533. (let* ((raw-path (org-element-property :path link))
  3534. (match-title-p (eq (aref raw-path 0) ?*))
  3535. ;; Split PATH at white spaces so matches are space
  3536. ;; insensitive.
  3537. (path (org-split-string
  3538. (if match-title-p (substring raw-path 1) raw-path)))
  3539. ;; Cache for destinations that are not position dependent.
  3540. (link-cache
  3541. (or (plist-get info :resolve-fuzzy-link-cache)
  3542. (plist-get (setq info (plist-put info :resolve-fuzzy-link-cache
  3543. (make-hash-table :test 'equal)))
  3544. :resolve-fuzzy-link-cache)))
  3545. (cached (gethash path link-cache 'not-found)))
  3546. (cond
  3547. ;; Destination is not position dependent: use cached value.
  3548. ((and (not match-title-p) (not (eq cached 'not-found))) cached)
  3549. ;; First try to find a matching "<<path>>" unless user specified
  3550. ;; he was looking for a headline (path starts with a "*"
  3551. ;; character).
  3552. ((and (not match-title-p)
  3553. (let ((match (org-element-map (plist-get info :parse-tree) 'target
  3554. (lambda (blob)
  3555. (and (equal (org-split-string
  3556. (org-element-property :value blob))
  3557. path)
  3558. blob))
  3559. info 'first-match)))
  3560. (and match (puthash path match link-cache)))))
  3561. ;; Then try to find an element with a matching "#+NAME: path"
  3562. ;; affiliated keyword.
  3563. ((and (not match-title-p)
  3564. (let ((match (org-element-map (plist-get info :parse-tree)
  3565. org-element-all-elements
  3566. (lambda (el)
  3567. (let ((name (org-element-property :name el)))
  3568. (when (and name
  3569. (equal (org-split-string name) path))
  3570. el)))
  3571. info 'first-match)))
  3572. (and match (puthash path match link-cache)))))
  3573. ;; Last case: link either points to a headline or to nothingness.
  3574. ;; Try to find the source, with priority given to headlines with
  3575. ;; the closest common ancestor. If such candidate is found,
  3576. ;; return it, otherwise return nil.
  3577. (t
  3578. (let ((find-headline
  3579. (function
  3580. ;; Return first headline whose `:raw-value' property is
  3581. ;; NAME in parse tree DATA, or nil. Statistics cookies
  3582. ;; are ignored.
  3583. (lambda (name data)
  3584. (org-element-map data 'headline
  3585. (lambda (headline)
  3586. (when (equal (org-split-string
  3587. (replace-regexp-in-string
  3588. "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
  3589. (org-element-property :raw-value headline)))
  3590. name)
  3591. headline))
  3592. info 'first-match)))))
  3593. ;; Search among headlines sharing an ancestor with link, from
  3594. ;; closest to farthest.
  3595. (catch 'exit
  3596. (mapc
  3597. (lambda (parent)
  3598. (let ((foundp (funcall find-headline path parent)))
  3599. (when foundp (throw 'exit foundp))))
  3600. (let ((parent-hl (org-export-get-parent-headline link)))
  3601. (if (not parent-hl) (list (plist-get info :parse-tree))
  3602. (cons parent-hl (org-export-get-genealogy parent-hl)))))
  3603. ;; No destination found: return nil.
  3604. (and (not match-title-p) (puthash path nil link-cache))))))))
  3605. (defun org-export-resolve-id-link (link info)
  3606. "Return headline referenced as LINK destination.
  3607. INFO is a plist used as a communication channel.
  3608. Return value can be the headline element matched in current parse
  3609. tree, a file name or nil. Assume LINK type is either \"id\" or
  3610. \"custom-id\"."
  3611. (let ((id (org-element-property :path link)))
  3612. ;; First check if id is within the current parse tree.
  3613. (or (org-element-map (plist-get info :parse-tree) 'headline
  3614. (lambda (headline)
  3615. (when (or (string= (org-element-property :ID headline) id)
  3616. (string= (org-element-property :CUSTOM_ID headline) id))
  3617. headline))
  3618. info 'first-match)
  3619. ;; Otherwise, look for external files.
  3620. (cdr (assoc id (plist-get info :id-alist))))))
  3621. (defun org-export-resolve-radio-link (link info)
  3622. "Return radio-target object referenced as LINK destination.
  3623. INFO is a plist used as a communication channel.
  3624. Return value can be a radio-target object or nil. Assume LINK
  3625. has type \"radio\"."
  3626. (let ((path (replace-regexp-in-string
  3627. "[ \r\t\n]+" " " (org-element-property :path link))))
  3628. (org-element-map (plist-get info :parse-tree) 'radio-target
  3629. (lambda (radio)
  3630. (and (eq (compare-strings
  3631. (replace-regexp-in-string
  3632. "[ \r\t\n]+" " " (org-element-property :value radio))
  3633. nil nil path nil nil t)
  3634. t)
  3635. radio))
  3636. info 'first-match)))
  3637. ;;;; For References
  3638. ;;
  3639. ;; `org-export-get-ordinal' associates a sequence number to any object
  3640. ;; or element.
  3641. (defun org-export-get-ordinal (element info &optional types predicate)
  3642. "Return ordinal number of an element or object.
  3643. ELEMENT is the element or object considered. INFO is the plist
  3644. used as a communication channel.
  3645. Optional argument TYPES, when non-nil, is a list of element or
  3646. object types, as symbols, that should also be counted in.
  3647. Otherwise, only provided element's type is considered.
  3648. Optional argument PREDICATE is a function returning a non-nil
  3649. value if the current element or object should be counted in. It
  3650. accepts two arguments: the element or object being considered and
  3651. the plist used as a communication channel. This allows to count
  3652. only a certain type of objects (i.e. inline images).
  3653. Return value is a list of numbers if ELEMENT is a headline or an
  3654. item. It is nil for keywords. It represents the footnote number
  3655. for footnote definitions and footnote references. If ELEMENT is
  3656. a target, return the same value as if ELEMENT was the closest
  3657. table, item or headline containing the target. In any other
  3658. case, return the sequence number of ELEMENT among elements or
  3659. objects of the same type."
  3660. ;; Ordinal of a target object refer to the ordinal of the closest
  3661. ;; table, item, or headline containing the object.
  3662. (when (eq (org-element-type element) 'target)
  3663. (setq element
  3664. (loop for parent in (org-export-get-genealogy element)
  3665. when
  3666. (memq
  3667. (org-element-type parent)
  3668. '(footnote-definition footnote-reference headline item
  3669. table))
  3670. return parent)))
  3671. (case (org-element-type element)
  3672. ;; Special case 1: A headline returns its number as a list.
  3673. (headline (org-export-get-headline-number element info))
  3674. ;; Special case 2: An item returns its number as a list.
  3675. (item (let ((struct (org-element-property :structure element)))
  3676. (org-list-get-item-number
  3677. (org-element-property :begin element)
  3678. struct
  3679. (org-list-prevs-alist struct)
  3680. (org-list-parents-alist struct))))
  3681. ((footnote-definition footnote-reference)
  3682. (org-export-get-footnote-number element info))
  3683. (otherwise
  3684. (let ((counter 0))
  3685. ;; Increment counter until ELEMENT is found again.
  3686. (org-element-map (plist-get info :parse-tree)
  3687. (or types (org-element-type element))
  3688. (lambda (el)
  3689. (cond
  3690. ((eq element el) (1+ counter))
  3691. ((not predicate) (incf counter) nil)
  3692. ((funcall predicate el info) (incf counter) nil)))
  3693. info 'first-match)))))
  3694. ;;;; For Src-Blocks
  3695. ;;
  3696. ;; `org-export-get-loc' counts number of code lines accumulated in
  3697. ;; src-block or example-block elements with a "+n" switch until
  3698. ;; a given element, excluded. Note: "-n" switches reset that count.
  3699. ;;
  3700. ;; `org-export-unravel-code' extracts source code (along with a code
  3701. ;; references alist) from an `element-block' or `src-block' type
  3702. ;; element.
  3703. ;;
  3704. ;; `org-export-format-code' applies a formatting function to each line
  3705. ;; of code, providing relative line number and code reference when
  3706. ;; appropriate. Since it doesn't access the original element from
  3707. ;; which the source code is coming, it expects from the code calling
  3708. ;; it to know if lines should be numbered and if code references
  3709. ;; should appear.
  3710. ;;
  3711. ;; Eventually, `org-export-format-code-default' is a higher-level
  3712. ;; function (it makes use of the two previous functions) which handles
  3713. ;; line numbering and code references inclusion, and returns source
  3714. ;; code in a format suitable for plain text or verbatim output.
  3715. (defun org-export-get-loc (element info)
  3716. "Return accumulated lines of code up to ELEMENT.
  3717. INFO is the plist used as a communication channel.
  3718. ELEMENT is excluded from count."
  3719. (let ((loc 0))
  3720. (org-element-map (plist-get info :parse-tree)
  3721. `(src-block example-block ,(org-element-type element))
  3722. (lambda (el)
  3723. (cond
  3724. ;; ELEMENT is reached: Quit the loop.
  3725. ((eq el element))
  3726. ;; Only count lines from src-block and example-block elements
  3727. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  3728. ((not (memq (org-element-type el) '(src-block example-block))) nil)
  3729. ((let ((linums (org-element-property :number-lines el)))
  3730. (when linums
  3731. ;; Accumulate locs or reset them.
  3732. (let ((lines (org-count-lines
  3733. (org-trim (org-element-property :value el)))))
  3734. (setq loc (if (eq linums 'new) lines (+ loc lines))))))
  3735. ;; Return nil to stay in the loop.
  3736. nil)))
  3737. info 'first-match)
  3738. ;; Return value.
  3739. loc))
  3740. (defun org-export-unravel-code (element)
  3741. "Clean source code and extract references out of it.
  3742. ELEMENT has either a `src-block' an `example-block' type.
  3743. Return a cons cell whose CAR is the source code, cleaned from any
  3744. reference, protective commas and spurious indentation, and CDR is
  3745. an alist between relative line number (integer) and name of code
  3746. reference on that line (string)."
  3747. (let* ((line 0) refs
  3748. (value (org-element-property :value element))
  3749. ;; Get code and clean it. Remove blank lines at its
  3750. ;; beginning and end.
  3751. (code (replace-regexp-in-string
  3752. "\\`\\([ \t]*\n\\)+" ""
  3753. (replace-regexp-in-string
  3754. "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
  3755. (if (or org-src-preserve-indentation
  3756. (org-element-property :preserve-indent element))
  3757. value
  3758. (org-element-remove-indentation value)))))
  3759. ;; Get format used for references.
  3760. (label-fmt (regexp-quote
  3761. (or (org-element-property :label-fmt element)
  3762. org-coderef-label-format)))
  3763. ;; Build a regexp matching a loc with a reference.
  3764. (with-ref-re
  3765. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
  3766. (replace-regexp-in-string
  3767. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  3768. ;; Return value.
  3769. (cons
  3770. ;; Code with references removed.
  3771. (org-element-normalize-string
  3772. (mapconcat
  3773. (lambda (loc)
  3774. (incf line)
  3775. (if (not (string-match with-ref-re loc)) loc
  3776. ;; Ref line: remove ref, and signal its position in REFS.
  3777. (push (cons line (match-string 3 loc)) refs)
  3778. (replace-match "" nil nil loc 1)))
  3779. (org-split-string code "\n") "\n"))
  3780. ;; Reference alist.
  3781. refs)))
  3782. (defun org-export-format-code (code fun &optional num-lines ref-alist)
  3783. "Format CODE by applying FUN line-wise and return it.
  3784. CODE is a string representing the code to format. FUN is
  3785. a function. It must accept three arguments: a line of
  3786. code (string), the current line number (integer) or nil and the
  3787. reference associated to the current line (string) or nil.
  3788. Optional argument NUM-LINES can be an integer representing the
  3789. number of code lines accumulated until the current code. Line
  3790. numbers passed to FUN will take it into account. If it is nil,
  3791. FUN's second argument will always be nil. This number can be
  3792. obtained with `org-export-get-loc' function.
  3793. Optional argument REF-ALIST can be an alist between relative line
  3794. number (i.e. ignoring NUM-LINES) and the name of the code
  3795. reference on it. If it is nil, FUN's third argument will always
  3796. be nil. It can be obtained through the use of
  3797. `org-export-unravel-code' function."
  3798. (let ((--locs (org-split-string code "\n"))
  3799. (--line 0))
  3800. (org-element-normalize-string
  3801. (mapconcat
  3802. (lambda (--loc)
  3803. (incf --line)
  3804. (let ((--ref (cdr (assq --line ref-alist))))
  3805. (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
  3806. --locs "\n"))))
  3807. (defun org-export-format-code-default (element info)
  3808. "Return source code from ELEMENT, formatted in a standard way.
  3809. ELEMENT is either a `src-block' or `example-block' element. INFO
  3810. is a plist used as a communication channel.
  3811. This function takes care of line numbering and code references
  3812. inclusion. Line numbers, when applicable, appear at the
  3813. beginning of the line, separated from the code by two white
  3814. spaces. Code references, on the other hand, appear flushed to
  3815. the right, separated by six white spaces from the widest line of
  3816. code."
  3817. ;; Extract code and references.
  3818. (let* ((code-info (org-export-unravel-code element))
  3819. (code (car code-info))
  3820. (code-lines (org-split-string code "\n")))
  3821. (if (null code-lines) ""
  3822. (let* ((refs (and (org-element-property :retain-labels element)
  3823. (cdr code-info)))
  3824. ;; Handle line numbering.
  3825. (num-start (case (org-element-property :number-lines element)
  3826. (continued (org-export-get-loc element info))
  3827. (new 0)))
  3828. (num-fmt
  3829. (and num-start
  3830. (format "%%%ds "
  3831. (length (number-to-string
  3832. (+ (length code-lines) num-start))))))
  3833. ;; Prepare references display, if required. Any reference
  3834. ;; should start six columns after the widest line of code,
  3835. ;; wrapped with parenthesis.
  3836. (max-width
  3837. (+ (apply 'max (mapcar 'length code-lines))
  3838. (if (not num-start) 0 (length (format num-fmt num-start))))))
  3839. (org-export-format-code
  3840. code
  3841. (lambda (loc line-num ref)
  3842. (let ((number-str (and num-fmt (format num-fmt line-num))))
  3843. (concat
  3844. number-str
  3845. loc
  3846. (and ref
  3847. (concat (make-string
  3848. (- (+ 6 max-width)
  3849. (+ (length loc) (length number-str))) ? )
  3850. (format "(%s)" ref))))))
  3851. num-start refs)))))
  3852. ;;;; For Tables
  3853. ;;
  3854. ;; `org-export-table-has-special-column-p' and and
  3855. ;; `org-export-table-row-is-special-p' are predicates used to look for
  3856. ;; meta-information about the table structure.
  3857. ;;
  3858. ;; `org-table-has-header-p' tells when the rows before the first rule
  3859. ;; should be considered as table's header.
  3860. ;;
  3861. ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
  3862. ;; and `org-export-table-cell-borders' extract information from
  3863. ;; a table-cell element.
  3864. ;;
  3865. ;; `org-export-table-dimensions' gives the number on rows and columns
  3866. ;; in the table, ignoring horizontal rules and special columns.
  3867. ;; `org-export-table-cell-address', given a table-cell object, returns
  3868. ;; the absolute address of a cell. On the other hand,
  3869. ;; `org-export-get-table-cell-at' does the contrary.
  3870. ;;
  3871. ;; `org-export-table-cell-starts-colgroup-p',
  3872. ;; `org-export-table-cell-ends-colgroup-p',
  3873. ;; `org-export-table-row-starts-rowgroup-p',
  3874. ;; `org-export-table-row-ends-rowgroup-p',
  3875. ;; `org-export-table-row-starts-header-p' and
  3876. ;; `org-export-table-row-ends-header-p' indicate position of current
  3877. ;; row or cell within the table.
  3878. (defun org-export-table-has-special-column-p (table)
  3879. "Non-nil when TABLE has a special column.
  3880. All special columns will be ignored during export."
  3881. ;; The table has a special column when every first cell of every row
  3882. ;; has an empty value or contains a symbol among "/", "#", "!", "$",
  3883. ;; "*" "_" and "^". Though, do not consider a first row containing
  3884. ;; only empty cells as special.
  3885. (let ((special-column-p 'empty))
  3886. (catch 'exit
  3887. (mapc
  3888. (lambda (row)
  3889. (when (eq (org-element-property :type row) 'standard)
  3890. (let ((value (org-element-contents
  3891. (car (org-element-contents row)))))
  3892. (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
  3893. (setq special-column-p 'special))
  3894. ((not value))
  3895. (t (throw 'exit nil))))))
  3896. (org-element-contents table))
  3897. (eq special-column-p 'special))))
  3898. (defun org-export-table-has-header-p (table info)
  3899. "Non-nil when TABLE has a header.
  3900. INFO is a plist used as a communication channel.
  3901. A table has a header when it contains at least two row groups."
  3902. (let ((cache (or (plist-get info :table-header-cache)
  3903. (plist-get (setq info
  3904. (plist-put info :table-header-cache
  3905. (make-hash-table :test 'eq)))
  3906. :table-header-cache))))
  3907. (or (gethash table cache)
  3908. (let ((rowgroup 1) row-flag)
  3909. (puthash
  3910. table
  3911. (org-element-map table 'table-row
  3912. (lambda (row)
  3913. (cond
  3914. ((> rowgroup 1) t)
  3915. ((and row-flag (eq (org-element-property :type row) 'rule))
  3916. (incf rowgroup) (setq row-flag nil))
  3917. ((and (not row-flag) (eq (org-element-property :type row)
  3918. 'standard))
  3919. (setq row-flag t) nil)))
  3920. info 'first-match)
  3921. cache)))))
  3922. (defun org-export-table-row-is-special-p (table-row info)
  3923. "Non-nil if TABLE-ROW is considered special.
  3924. INFO is a plist used as the communication channel.
  3925. All special rows will be ignored during export."
  3926. (when (eq (org-element-property :type table-row) 'standard)
  3927. (let ((first-cell (org-element-contents
  3928. (car (org-element-contents table-row)))))
  3929. ;; A row is special either when...
  3930. (or
  3931. ;; ... it starts with a field only containing "/",
  3932. (equal first-cell '("/"))
  3933. ;; ... the table contains a special column and the row start
  3934. ;; with a marking character among, "^", "_", "$" or "!",
  3935. (and (org-export-table-has-special-column-p
  3936. (org-export-get-parent table-row))
  3937. (member first-cell '(("^") ("_") ("$") ("!"))))
  3938. ;; ... it contains only alignment cookies and empty cells.
  3939. (let ((special-row-p 'empty))
  3940. (catch 'exit
  3941. (mapc
  3942. (lambda (cell)
  3943. (let ((value (org-element-contents cell)))
  3944. ;; Since VALUE is a secondary string, the following
  3945. ;; checks avoid expanding it with `org-export-data'.
  3946. (cond ((not value))
  3947. ((and (not (cdr value))
  3948. (stringp (car value))
  3949. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
  3950. (car value)))
  3951. (setq special-row-p 'cookie))
  3952. (t (throw 'exit nil)))))
  3953. (org-element-contents table-row))
  3954. (eq special-row-p 'cookie)))))))
  3955. (defun org-export-table-row-group (table-row info)
  3956. "Return TABLE-ROW's group number, as an integer.
  3957. INFO is a plist used as the communication channel.
  3958. Return value is the group number, as an integer, or nil for
  3959. special rows and rows separators. First group is also table's
  3960. header."
  3961. (let ((cache (or (plist-get info :table-row-group-cache)
  3962. (plist-get (setq info
  3963. (plist-put info :table-row-group-cache
  3964. (make-hash-table :test 'eq)))
  3965. :table-row-group-cache))))
  3966. (cond ((gethash table-row cache))
  3967. ((eq (org-element-property :type table-row) 'rule) nil)
  3968. (t (let ((group 0) row-flag)
  3969. (org-element-map (org-export-get-parent table-row) 'table-row
  3970. (lambda (row)
  3971. (if (eq (org-element-property :type row) 'rule)
  3972. (setq row-flag nil)
  3973. (unless row-flag (incf group) (setq row-flag t)))
  3974. (when (eq table-row row) (puthash table-row group cache)))
  3975. info 'first-match))))))
  3976. (defun org-export-table-cell-width (table-cell info)
  3977. "Return TABLE-CELL contents width.
  3978. INFO is a plist used as the communication channel.
  3979. Return value is the width given by the last width cookie in the
  3980. same column as TABLE-CELL, or nil."
  3981. (let* ((row (org-export-get-parent table-cell))
  3982. (table (org-export-get-parent row))
  3983. (cells (org-element-contents row))
  3984. (columns (length cells))
  3985. (column (- columns (length (memq table-cell cells))))
  3986. (cache (or (plist-get info :table-cell-width-cache)
  3987. (plist-get (setq info
  3988. (plist-put info :table-cell-width-cache
  3989. (make-hash-table :test 'eq)))
  3990. :table-cell-width-cache)))
  3991. (width-vector (or (gethash table cache)
  3992. (puthash table (make-vector columns 'empty) cache)))
  3993. (value (aref width-vector column)))
  3994. (if (not (eq value 'empty)) value
  3995. (let (cookie-width)
  3996. (dolist (row (org-element-contents table)
  3997. (aset width-vector column cookie-width))
  3998. (when (org-export-table-row-is-special-p row info)
  3999. ;; In a special row, try to find a width cookie at COLUMN.
  4000. (let* ((value (org-element-contents
  4001. (elt (org-element-contents row) column)))
  4002. (cookie (car value)))
  4003. ;; The following checks avoid expanding unnecessarily
  4004. ;; the cell with `org-export-data'.
  4005. (when (and value
  4006. (not (cdr value))
  4007. (stringp cookie)
  4008. (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
  4009. (match-string 1 cookie))
  4010. (setq cookie-width
  4011. (string-to-number (match-string 1 cookie)))))))))))
  4012. (defun org-export-table-cell-alignment (table-cell info)
  4013. "Return TABLE-CELL contents alignment.
  4014. INFO is a plist used as the communication channel.
  4015. Return alignment as specified by the last alignment cookie in the
  4016. same column as TABLE-CELL. If no such cookie is found, a default
  4017. alignment value will be deduced from fraction of numbers in the
  4018. column (see `org-table-number-fraction' for more information).
  4019. Possible values are `left', `right' and `center'."
  4020. ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
  4021. (require 'org-table)
  4022. (let* ((row (org-export-get-parent table-cell))
  4023. (table (org-export-get-parent row))
  4024. (cells (org-element-contents row))
  4025. (columns (length cells))
  4026. (column (- columns (length (memq table-cell cells))))
  4027. (cache (or (plist-get info :table-cell-alignment-cache)
  4028. (plist-get (setq info
  4029. (plist-put info :table-cell-alignment-cache
  4030. (make-hash-table :test 'eq)))
  4031. :table-cell-alignment-cache)))
  4032. (align-vector (or (gethash table cache)
  4033. (puthash table (make-vector columns nil) cache))))
  4034. (or (aref align-vector column)
  4035. (let ((number-cells 0)
  4036. (total-cells 0)
  4037. cookie-align
  4038. previous-cell-number-p)
  4039. (dolist (row (org-element-contents (org-export-get-parent row)))
  4040. (cond
  4041. ;; In a special row, try to find an alignment cookie at
  4042. ;; COLUMN.
  4043. ((org-export-table-row-is-special-p row info)
  4044. (let ((value (org-element-contents
  4045. (elt (org-element-contents row) column))))
  4046. ;; Since VALUE is a secondary string, the following
  4047. ;; checks avoid useless expansion through
  4048. ;; `org-export-data'.
  4049. (when (and value
  4050. (not (cdr value))
  4051. (stringp (car value))
  4052. (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
  4053. (car value))
  4054. (match-string 1 (car value)))
  4055. (setq cookie-align (match-string 1 (car value))))))
  4056. ;; Ignore table rules.
  4057. ((eq (org-element-property :type row) 'rule))
  4058. ;; In a standard row, check if cell's contents are
  4059. ;; expressing some kind of number. Increase NUMBER-CELLS
  4060. ;; accordingly. Though, don't bother if an alignment
  4061. ;; cookie has already defined cell's alignment.
  4062. ((not cookie-align)
  4063. (let ((value (org-export-data
  4064. (org-element-contents
  4065. (elt (org-element-contents row) column))
  4066. info)))
  4067. (incf total-cells)
  4068. ;; Treat an empty cell as a number if it follows
  4069. ;; a number.
  4070. (if (not (or (string-match org-table-number-regexp value)
  4071. (and (string= value "") previous-cell-number-p)))
  4072. (setq previous-cell-number-p nil)
  4073. (setq previous-cell-number-p t)
  4074. (incf number-cells))))))
  4075. ;; Return value. Alignment specified by cookies has
  4076. ;; precedence over alignment deduced from cell's contents.
  4077. (aset align-vector
  4078. column
  4079. (cond ((equal cookie-align "l") 'left)
  4080. ((equal cookie-align "r") 'right)
  4081. ((equal cookie-align "c") 'center)
  4082. ((>= (/ (float number-cells) total-cells)
  4083. org-table-number-fraction)
  4084. 'right)
  4085. (t 'left)))))))
  4086. (defun org-export-table-cell-borders (table-cell info)
  4087. "Return TABLE-CELL borders.
  4088. INFO is a plist used as a communication channel.
  4089. Return value is a list of symbols, or nil. Possible values are:
  4090. `top', `bottom', `above', `below', `left' and `right'. Note:
  4091. `top' (resp. `bottom') only happen for a cell in the first
  4092. row (resp. last row) of the table, ignoring table rules, if any.
  4093. Returned borders ignore special rows."
  4094. (let* ((row (org-export-get-parent table-cell))
  4095. (table (org-export-get-parent-table table-cell))
  4096. borders)
  4097. ;; Top/above border? TABLE-CELL has a border above when a rule
  4098. ;; used to demarcate row groups can be found above. Hence,
  4099. ;; finding a rule isn't sufficient to push `above' in BORDERS:
  4100. ;; another regular row has to be found above that rule.
  4101. (let (rule-flag)
  4102. (catch 'exit
  4103. (mapc (lambda (row)
  4104. (cond ((eq (org-element-property :type row) 'rule)
  4105. (setq rule-flag t))
  4106. ((not (org-export-table-row-is-special-p row info))
  4107. (if rule-flag (throw 'exit (push 'above borders))
  4108. (throw 'exit nil)))))
  4109. ;; Look at every row before the current one.
  4110. (cdr (memq row (reverse (org-element-contents table)))))
  4111. ;; No rule above, or rule found starts the table (ignoring any
  4112. ;; special row): TABLE-CELL is at the top of the table.
  4113. (when rule-flag (push 'above borders))
  4114. (push 'top borders)))
  4115. ;; Bottom/below border? TABLE-CELL has a border below when next
  4116. ;; non-regular row below is a rule.
  4117. (let (rule-flag)
  4118. (catch 'exit
  4119. (mapc (lambda (row)
  4120. (cond ((eq (org-element-property :type row) 'rule)
  4121. (setq rule-flag t))
  4122. ((not (org-export-table-row-is-special-p row info))
  4123. (if rule-flag (throw 'exit (push 'below borders))
  4124. (throw 'exit nil)))))
  4125. ;; Look at every row after the current one.
  4126. (cdr (memq row (org-element-contents table))))
  4127. ;; No rule below, or rule found ends the table (modulo some
  4128. ;; special row): TABLE-CELL is at the bottom of the table.
  4129. (when rule-flag (push 'below borders))
  4130. (push 'bottom borders)))
  4131. ;; Right/left borders? They can only be specified by column
  4132. ;; groups. Column groups are defined in a row starting with "/".
  4133. ;; Also a column groups row only contains "<", "<>", ">" or blank
  4134. ;; cells.
  4135. (catch 'exit
  4136. (let ((column (let ((cells (org-element-contents row)))
  4137. (- (length cells) (length (memq table-cell cells))))))
  4138. (mapc
  4139. (lambda (row)
  4140. (unless (eq (org-element-property :type row) 'rule)
  4141. (when (equal (org-element-contents
  4142. (car (org-element-contents row)))
  4143. '("/"))
  4144. (let ((column-groups
  4145. (mapcar
  4146. (lambda (cell)
  4147. (let ((value (org-element-contents cell)))
  4148. (when (member value '(("<") ("<>") (">") nil))
  4149. (car value))))
  4150. (org-element-contents row))))
  4151. ;; There's a left border when previous cell, if
  4152. ;; any, ends a group, or current one starts one.
  4153. (when (or (and (not (zerop column))
  4154. (member (elt column-groups (1- column))
  4155. '(">" "<>")))
  4156. (member (elt column-groups column) '("<" "<>")))
  4157. (push 'left borders))
  4158. ;; There's a right border when next cell, if any,
  4159. ;; starts a group, or current one ends one.
  4160. (when (or (and (/= (1+ column) (length column-groups))
  4161. (member (elt column-groups (1+ column))
  4162. '("<" "<>")))
  4163. (member (elt column-groups column) '(">" "<>")))
  4164. (push 'right borders))
  4165. (throw 'exit nil)))))
  4166. ;; Table rows are read in reverse order so last column groups
  4167. ;; row has precedence over any previous one.
  4168. (reverse (org-element-contents table)))))
  4169. ;; Return value.
  4170. borders))
  4171. (defun org-export-table-cell-starts-colgroup-p (table-cell info)
  4172. "Non-nil when TABLE-CELL is at the beginning of a column group.
  4173. INFO is a plist used as a communication channel."
  4174. ;; A cell starts a column group either when it is at the beginning
  4175. ;; of a row (or after the special column, if any) or when it has
  4176. ;; a left border.
  4177. (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
  4178. 'identity info 'first-match)
  4179. table-cell)
  4180. (memq 'left (org-export-table-cell-borders table-cell info))))
  4181. (defun org-export-table-cell-ends-colgroup-p (table-cell info)
  4182. "Non-nil when TABLE-CELL is at the end of a column group.
  4183. INFO is a plist used as a communication channel."
  4184. ;; A cell ends a column group either when it is at the end of a row
  4185. ;; or when it has a right border.
  4186. (or (eq (car (last (org-element-contents
  4187. (org-export-get-parent table-cell))))
  4188. table-cell)
  4189. (memq 'right (org-export-table-cell-borders table-cell info))))
  4190. (defun org-export-table-row-starts-rowgroup-p (table-row info)
  4191. "Non-nil when TABLE-ROW is at the beginning of a row group.
  4192. INFO is a plist used as a communication channel."
  4193. (unless (or (eq (org-element-property :type table-row) 'rule)
  4194. (org-export-table-row-is-special-p table-row info))
  4195. (let ((borders (org-export-table-cell-borders
  4196. (car (org-element-contents table-row)) info)))
  4197. (or (memq 'top borders) (memq 'above borders)))))
  4198. (defun org-export-table-row-ends-rowgroup-p (table-row info)
  4199. "Non-nil when TABLE-ROW is at the end of a row group.
  4200. INFO is a plist used as a communication channel."
  4201. (unless (or (eq (org-element-property :type table-row) 'rule)
  4202. (org-export-table-row-is-special-p table-row info))
  4203. (let ((borders (org-export-table-cell-borders
  4204. (car (org-element-contents table-row)) info)))
  4205. (or (memq 'bottom borders) (memq 'below borders)))))
  4206. (defun org-export-table-row-starts-header-p (table-row info)
  4207. "Non-nil when TABLE-ROW is the first table header's row.
  4208. INFO is a plist used as a communication channel."
  4209. (and (org-export-table-has-header-p
  4210. (org-export-get-parent-table table-row) info)
  4211. (org-export-table-row-starts-rowgroup-p table-row info)
  4212. (= (org-export-table-row-group table-row info) 1)))
  4213. (defun org-export-table-row-ends-header-p (table-row info)
  4214. "Non-nil when TABLE-ROW is the last table header's row.
  4215. INFO is a plist used as a communication channel."
  4216. (and (org-export-table-has-header-p
  4217. (org-export-get-parent-table table-row) info)
  4218. (org-export-table-row-ends-rowgroup-p table-row info)
  4219. (= (org-export-table-row-group table-row info) 1)))
  4220. (defun org-export-table-row-number (table-row info)
  4221. "Return TABLE-ROW number.
  4222. INFO is a plist used as a communication channel. Return value is
  4223. zero-based and ignores separators. The function returns nil for
  4224. special colums and separators."
  4225. (when (and (eq (org-element-property :type table-row) 'standard)
  4226. (not (org-export-table-row-is-special-p table-row info)))
  4227. (let ((number 0))
  4228. (org-element-map (org-export-get-parent-table table-row) 'table-row
  4229. (lambda (row)
  4230. (cond ((eq row table-row) number)
  4231. ((eq (org-element-property :type row) 'standard)
  4232. (incf number) nil)))
  4233. info 'first-match))))
  4234. (defun org-export-table-dimensions (table info)
  4235. "Return TABLE dimensions.
  4236. INFO is a plist used as a communication channel.
  4237. Return value is a CONS like (ROWS . COLUMNS) where
  4238. ROWS (resp. COLUMNS) is the number of exportable
  4239. rows (resp. columns)."
  4240. (let (first-row (columns 0) (rows 0))
  4241. ;; Set number of rows, and extract first one.
  4242. (org-element-map table 'table-row
  4243. (lambda (row)
  4244. (when (eq (org-element-property :type row) 'standard)
  4245. (incf rows)
  4246. (unless first-row (setq first-row row)))) info)
  4247. ;; Set number of columns.
  4248. (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
  4249. ;; Return value.
  4250. (cons rows columns)))
  4251. (defun org-export-table-cell-address (table-cell info)
  4252. "Return address of a regular TABLE-CELL object.
  4253. TABLE-CELL is the cell considered. INFO is a plist used as
  4254. a communication channel.
  4255. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4256. zero-based index. Only exportable cells are considered. The
  4257. function returns nil for other cells."
  4258. (let* ((table-row (org-export-get-parent table-cell))
  4259. (row-number (org-export-table-row-number table-row info)))
  4260. (when row-number
  4261. (cons row-number
  4262. (let ((col-count 0))
  4263. (org-element-map table-row 'table-cell
  4264. (lambda (cell)
  4265. (if (eq cell table-cell) col-count (incf col-count) nil))
  4266. info 'first-match))))))
  4267. (defun org-export-get-table-cell-at (address table info)
  4268. "Return regular table-cell object at ADDRESS in TABLE.
  4269. Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
  4270. zero-based index. TABLE is a table type element. INFO is
  4271. a plist used as a communication channel.
  4272. If no table-cell, among exportable cells, is found at ADDRESS,
  4273. return nil."
  4274. (let ((column-pos (cdr address)) (column-count 0))
  4275. (org-element-map
  4276. ;; Row at (car address) or nil.
  4277. (let ((row-pos (car address)) (row-count 0))
  4278. (org-element-map table 'table-row
  4279. (lambda (row)
  4280. (cond ((eq (org-element-property :type row) 'rule) nil)
  4281. ((= row-count row-pos) row)
  4282. (t (incf row-count) nil)))
  4283. info 'first-match))
  4284. 'table-cell
  4285. (lambda (cell)
  4286. (if (= column-count column-pos) cell
  4287. (incf column-count) nil))
  4288. info 'first-match)))
  4289. ;;;; For Tables Of Contents
  4290. ;;
  4291. ;; `org-export-collect-headlines' builds a list of all exportable
  4292. ;; headline elements, maybe limited to a certain depth. One can then
  4293. ;; easily parse it and transcode it.
  4294. ;;
  4295. ;; Building lists of tables, figures or listings is quite similar.
  4296. ;; Once the generic function `org-export-collect-elements' is defined,
  4297. ;; `org-export-collect-tables', `org-export-collect-figures' and
  4298. ;; `org-export-collect-listings' can be derived from it.
  4299. (defun org-export-collect-headlines (info &optional n)
  4300. "Collect headlines in order to build a table of contents.
  4301. INFO is a plist used as a communication channel.
  4302. When optional argument N is an integer, it specifies the depth of
  4303. the table of contents. Otherwise, it is set to the value of the
  4304. last headline level. See `org-export-headline-levels' for more
  4305. information.
  4306. Return a list of all exportable headlines as parsed elements.
  4307. Footnote sections, if any, will be ignored."
  4308. (let ((limit (plist-get info :headline-levels)))
  4309. (setq n (if (wholenump n) (min n limit) limit))
  4310. (org-element-map (plist-get info :parse-tree) 'headline
  4311. #'(lambda (headline)
  4312. (unless (org-element-property :footnote-section-p headline)
  4313. (let ((level (org-export-get-relative-level headline info)))
  4314. (and (<= level n) headline))))
  4315. info)))
  4316. (defun org-export-collect-elements (type info &optional predicate)
  4317. "Collect referenceable elements of a determined type.
  4318. TYPE can be a symbol or a list of symbols specifying element
  4319. types to search. Only elements with a caption are collected.
  4320. INFO is a plist used as a communication channel.
  4321. When non-nil, optional argument PREDICATE is a function accepting
  4322. one argument, an element of type TYPE. It returns a non-nil
  4323. value when that element should be collected.
  4324. Return a list of all elements found, in order of appearance."
  4325. (org-element-map (plist-get info :parse-tree) type
  4326. (lambda (element)
  4327. (and (org-element-property :caption element)
  4328. (or (not predicate) (funcall predicate element))
  4329. element))
  4330. info))
  4331. (defun org-export-collect-tables (info)
  4332. "Build a list of tables.
  4333. INFO is a plist used as a communication channel.
  4334. Return a list of table elements with a caption."
  4335. (org-export-collect-elements 'table info))
  4336. (defun org-export-collect-figures (info predicate)
  4337. "Build a list of figures.
  4338. INFO is a plist used as a communication channel. PREDICATE is
  4339. a function which accepts one argument: a paragraph element and
  4340. whose return value is non-nil when that element should be
  4341. collected.
  4342. A figure is a paragraph type element, with a caption, verifying
  4343. PREDICATE. The latter has to be provided since a \"figure\" is
  4344. a vague concept that may depend on back-end.
  4345. Return a list of elements recognized as figures."
  4346. (org-export-collect-elements 'paragraph info predicate))
  4347. (defun org-export-collect-listings (info)
  4348. "Build a list of src blocks.
  4349. INFO is a plist used as a communication channel.
  4350. Return a list of src-block elements with a caption."
  4351. (org-export-collect-elements 'src-block info))
  4352. ;;;; Smart Quotes
  4353. ;;
  4354. ;; The main function for the smart quotes sub-system is
  4355. ;; `org-export-activate-smart-quotes', which replaces every quote in
  4356. ;; a given string from the parse tree with its "smart" counterpart.
  4357. ;;
  4358. ;; Dictionary for smart quotes is stored in
  4359. ;; `org-export-smart-quotes-alist'.
  4360. ;;
  4361. ;; Internally, regexps matching potential smart quotes (checks at
  4362. ;; string boundaries are also necessary) are defined in
  4363. ;; `org-export-smart-quotes-regexps'.
  4364. (defconst org-export-smart-quotes-alist
  4365. '(("da"
  4366. ;; one may use: »...«, "...", ›...‹, or '...'.
  4367. ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
  4368. ;; LaTeX quotes require Babel!
  4369. (opening-double-quote :utf-8 "»" :html "&raquo;" :latex ">>"
  4370. :texinfo "@guillemetright{}")
  4371. (closing-double-quote :utf-8 "«" :html "&laquo;" :latex "<<"
  4372. :texinfo "@guillemetleft{}")
  4373. (opening-single-quote :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}"
  4374. :texinfo "@guilsinglright{}")
  4375. (closing-single-quote :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}"
  4376. :texinfo "@guilsingleft{}")
  4377. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4378. ("de"
  4379. (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
  4380. :texinfo "@quotedblbase{}")
  4381. (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
  4382. :texinfo "@quotedblleft{}")
  4383. (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
  4384. :texinfo "@quotesinglbase{}")
  4385. (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
  4386. :texinfo "@quoteleft{}")
  4387. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4388. ("en"
  4389. (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4390. (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4391. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4392. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4393. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4394. ("es"
  4395. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4396. :texinfo "@guillemetleft{}")
  4397. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4398. :texinfo "@guillemetright{}")
  4399. (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
  4400. (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
  4401. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4402. ("fr"
  4403. (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4404. :texinfo "@guillemetleft{}@tie{}")
  4405. (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4406. :texinfo "@tie{}@guillemetright{}")
  4407. (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
  4408. :texinfo "@guillemetleft{}@tie{}")
  4409. (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
  4410. :texinfo "@tie{}@guillemetright{}")
  4411. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4412. ("no"
  4413. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4414. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4415. :texinfo "@guillemetleft{}")
  4416. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4417. :texinfo "@guillemetright{}")
  4418. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4419. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4420. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4421. ("nb"
  4422. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4423. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4424. :texinfo "@guillemetleft{}")
  4425. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4426. :texinfo "@guillemetright{}")
  4427. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4428. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4429. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4430. ("nn"
  4431. ;; https://nn.wikipedia.org/wiki/Sitatteikn
  4432. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
  4433. :texinfo "@guillemetleft{}")
  4434. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
  4435. :texinfo "@guillemetright{}")
  4436. (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
  4437. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
  4438. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4439. ("ru"
  4440. ;; http://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B2%D1%8B%D1%87%D0%BA%D0%B8#.D0.9A.D0.B0.D0.B2.D1.8B.D1.87.D0.BA.D0.B8.2C_.D0.B8.D1.81.D0.BF.D0.BE.D0.BB.D1.8C.D0.B7.D1.83.D0.B5.D0.BC.D1.8B.D0.B5_.D0.B2_.D1.80.D1.83.D1.81.D1.81.D0.BA.D0.BE.D0.BC_.D1.8F.D0.B7.D1.8B.D0.BA.D0.B5
  4441. ;; http://www.artlebedev.ru/kovodstvo/sections/104/
  4442. (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "{}<<"
  4443. :texinfo "@guillemetleft{}")
  4444. (closing-double-quote :utf-8 "»" :html "&raquo;" :latex ">>{}"
  4445. :texinfo "@guillemetright{}")
  4446. (opening-single-quote :utf-8 "„" :html "&bdquo;" :latex "\\glqq{}"
  4447. :texinfo "@quotedblbase{}")
  4448. (closing-single-quote :utf-8 "“" :html "&ldquo;" :latex "\\grqq{}"
  4449. :texinfo "@quotedblleft{}")
  4450. (apostrophe :utf-8 "’" :html: "&#39;"))
  4451. ("sv"
  4452. ;; based on https://sv.wikipedia.org/wiki/Citattecken
  4453. (opening-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4454. (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
  4455. (opening-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
  4456. (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
  4457. (apostrophe :utf-8 "’" :html "&rsquo;"))
  4458. )
  4459. "Smart quotes translations.
  4460. Alist whose CAR is a language string and CDR is an alist with
  4461. quote type as key and a plist associating various encodings to
  4462. their translation as value.
  4463. A quote type can be any symbol among `opening-double-quote',
  4464. `closing-double-quote', `opening-single-quote',
  4465. `closing-single-quote' and `apostrophe'.
  4466. Valid encodings include `:utf-8', `:html', `:latex' and
  4467. `:texinfo'.
  4468. If no translation is found, the quote character is left as-is.")
  4469. (defconst org-export-smart-quotes-regexps
  4470. (list
  4471. ;; Possible opening quote at beginning of string.
  4472. "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
  4473. ;; Possible closing quote at beginning of string.
  4474. "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
  4475. ;; Possible apostrophe at beginning of string.
  4476. "\\`\\('\\)\\S-"
  4477. ;; Opening single and double quotes.
  4478. "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
  4479. ;; Closing single and double quotes.
  4480. "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
  4481. ;; Apostrophe.
  4482. "\\S-\\('\\)\\S-"
  4483. ;; Possible opening quote at end of string.
  4484. "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
  4485. ;; Possible closing quote at end of string.
  4486. "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
  4487. ;; Possible apostrophe at end of string.
  4488. "\\S-\\('\\)\\'")
  4489. "List of regexps matching a quote or an apostrophe.
  4490. In every regexp, quote or apostrophe matched is put in group 1.")
  4491. (defun org-export-activate-smart-quotes (s encoding info &optional original)
  4492. "Replace regular quotes with \"smart\" quotes in string S.
  4493. ENCODING is a symbol among `:html', `:latex', `:texinfo' and
  4494. `:utf-8'. INFO is a plist used as a communication channel.
  4495. The function has to retrieve information about string
  4496. surroundings in parse tree. It can only happen with an
  4497. unmodified string. Thus, if S has already been through another
  4498. process, a non-nil ORIGINAL optional argument will provide that
  4499. original string.
  4500. Return the new string."
  4501. (if (equal s "") ""
  4502. (let* ((prev (org-export-get-previous-element (or original s) info))
  4503. ;; Try to be flexible when computing number of blanks
  4504. ;; before object. The previous object may be a string
  4505. ;; introduced by the back-end and not completely parsed.
  4506. (pre-blank (and prev
  4507. (or (org-element-property :post-blank prev)
  4508. ;; A string with missing `:post-blank'
  4509. ;; property.
  4510. (and (stringp prev)
  4511. (string-match " *\\'" prev)
  4512. (length (match-string 0 prev)))
  4513. ;; Fallback value.
  4514. 0)))
  4515. (next (org-export-get-next-element (or original s) info))
  4516. (get-smart-quote
  4517. (lambda (q type)
  4518. ;; Return smart quote associated to a give quote Q, as
  4519. ;; a string. TYPE is a symbol among `open', `close' and
  4520. ;; `apostrophe'.
  4521. (let ((key (case type
  4522. (apostrophe 'apostrophe)
  4523. (open (if (equal "'" q) 'opening-single-quote
  4524. 'opening-double-quote))
  4525. (otherwise (if (equal "'" q) 'closing-single-quote
  4526. 'closing-double-quote)))))
  4527. (or (plist-get
  4528. (cdr (assq key
  4529. (cdr (assoc (plist-get info :language)
  4530. org-export-smart-quotes-alist))))
  4531. encoding)
  4532. q)))))
  4533. (if (or (equal "\"" s) (equal "'" s))
  4534. ;; Only a quote: no regexp can match. We have to check both
  4535. ;; sides and decide what to do.
  4536. (cond ((and (not prev) (not next)) s)
  4537. ((not prev) (funcall get-smart-quote s 'open))
  4538. ((and (not next) (zerop pre-blank))
  4539. (funcall get-smart-quote s 'close))
  4540. ((not next) s)
  4541. ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
  4542. (t (funcall get-smart-quote 'open)))
  4543. ;; 1. Replace quote character at the beginning of S.
  4544. (cond
  4545. ;; Apostrophe?
  4546. ((and prev (zerop pre-blank)
  4547. (string-match (nth 2 org-export-smart-quotes-regexps) s))
  4548. (setq s (replace-match
  4549. (funcall get-smart-quote (match-string 1 s) 'apostrophe)
  4550. nil t s 1)))
  4551. ;; Closing quote?
  4552. ((and prev (zerop pre-blank)
  4553. (string-match (nth 1 org-export-smart-quotes-regexps) s))
  4554. (setq s (replace-match
  4555. (funcall get-smart-quote (match-string 1 s) 'close)
  4556. nil t s 1)))
  4557. ;; Opening quote?
  4558. ((and (or (not prev) (> pre-blank 0))
  4559. (string-match (nth 0 org-export-smart-quotes-regexps) s))
  4560. (setq s (replace-match
  4561. (funcall get-smart-quote (match-string 1 s) 'open)
  4562. nil t s 1))))
  4563. ;; 2. Replace quotes in the middle of the string.
  4564. (setq s (replace-regexp-in-string
  4565. ;; Opening quotes.
  4566. (nth 3 org-export-smart-quotes-regexps)
  4567. (lambda (text)
  4568. (funcall get-smart-quote (match-string 1 text) 'open))
  4569. s nil t 1))
  4570. (setq s (replace-regexp-in-string
  4571. ;; Closing quotes.
  4572. (nth 4 org-export-smart-quotes-regexps)
  4573. (lambda (text)
  4574. (funcall get-smart-quote (match-string 1 text) 'close))
  4575. s nil t 1))
  4576. (setq s (replace-regexp-in-string
  4577. ;; Apostrophes.
  4578. (nth 5 org-export-smart-quotes-regexps)
  4579. (lambda (text)
  4580. (funcall get-smart-quote (match-string 1 text) 'apostrophe))
  4581. s nil t 1))
  4582. ;; 3. Replace quote character at the end of S.
  4583. (cond
  4584. ;; Apostrophe?
  4585. ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
  4586. (setq s (replace-match
  4587. (funcall get-smart-quote (match-string 1 s) 'apostrophe)
  4588. nil t s 1)))
  4589. ;; Closing quote?
  4590. ((and (not next)
  4591. (string-match (nth 7 org-export-smart-quotes-regexps) s))
  4592. (setq s (replace-match
  4593. (funcall get-smart-quote (match-string 1 s) 'close)
  4594. nil t s 1)))
  4595. ;; Opening quote?
  4596. ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
  4597. (setq s (replace-match
  4598. (funcall get-smart-quote (match-string 1 s) 'open)
  4599. nil t s 1))))
  4600. ;; Return string with smart quotes.
  4601. s))))
  4602. ;;;; Topology
  4603. ;;
  4604. ;; Here are various functions to retrieve information about the
  4605. ;; neighbourhood of a given element or object. Neighbours of interest
  4606. ;; are direct parent (`org-export-get-parent'), parent headline
  4607. ;; (`org-export-get-parent-headline'), first element containing an
  4608. ;; object, (`org-export-get-parent-element'), parent table
  4609. ;; (`org-export-get-parent-table'), previous element or object
  4610. ;; (`org-export-get-previous-element') and next element or object
  4611. ;; (`org-export-get-next-element').
  4612. ;;
  4613. ;; `org-export-get-genealogy' returns the full genealogy of a given
  4614. ;; element or object, from closest parent to full parse tree.
  4615. (defsubst org-export-get-parent (blob)
  4616. "Return BLOB parent or nil.
  4617. BLOB is the element or object considered."
  4618. (org-element-property :parent blob))
  4619. (defun org-export-get-genealogy (blob)
  4620. "Return full genealogy relative to a given element or object.
  4621. BLOB is the element or object being considered.
  4622. Ancestors are returned from closest to farthest, the last one
  4623. being the full parse tree."
  4624. (let (genealogy (parent blob))
  4625. (while (setq parent (org-element-property :parent parent))
  4626. (push parent genealogy))
  4627. (nreverse genealogy)))
  4628. (defun org-export-get-parent-headline (blob)
  4629. "Return BLOB parent headline or nil.
  4630. BLOB is the element or object being considered."
  4631. (let ((parent blob))
  4632. (while (and (setq parent (org-element-property :parent parent))
  4633. (not (eq (org-element-type parent) 'headline))))
  4634. parent))
  4635. (defun org-export-get-parent-element (object)
  4636. "Return first element containing OBJECT or nil.
  4637. OBJECT is the object to consider."
  4638. (let ((parent object))
  4639. (while (and (setq parent (org-element-property :parent parent))
  4640. (memq (org-element-type parent) org-element-all-objects)))
  4641. parent))
  4642. (defun org-export-get-parent-table (object)
  4643. "Return OBJECT parent table or nil.
  4644. OBJECT is either a `table-cell' or `table-element' type object."
  4645. (let ((parent object))
  4646. (while (and (setq parent (org-element-property :parent parent))
  4647. (not (eq (org-element-type parent) 'table))))
  4648. parent))
  4649. (defun org-export-get-previous-element (blob info &optional n)
  4650. "Return previous element or object.
  4651. BLOB is an element or object. INFO is a plist used as
  4652. a communication channel. Return previous exportable element or
  4653. object, a string, or nil.
  4654. When optional argument N is a positive integer, return a list
  4655. containing up to N siblings before BLOB, from farthest to
  4656. closest. With any other non-nil value, return a list containing
  4657. all of them."
  4658. (let ((siblings
  4659. ;; An object can belong to the contents of its parent or
  4660. ;; to a secondary string. We check the latter option
  4661. ;; first.
  4662. (let ((parent (org-export-get-parent blob)))
  4663. (or (let ((sec-value (org-element-property
  4664. (cdr (assq (org-element-type parent)
  4665. org-element-secondary-value-alist))
  4666. parent)))
  4667. (and (memq blob sec-value) sec-value))
  4668. (org-element-contents parent))))
  4669. prev)
  4670. (catch 'exit
  4671. (mapc (lambda (obj)
  4672. (cond ((memq obj (plist-get info :ignore-list)))
  4673. ((null n) (throw 'exit obj))
  4674. ((not (wholenump n)) (push obj prev))
  4675. ((zerop n) (throw 'exit prev))
  4676. (t (decf n) (push obj prev))))
  4677. (cdr (memq blob (reverse siblings))))
  4678. prev)))
  4679. (defun org-export-get-next-element (blob info &optional n)
  4680. "Return next element or object.
  4681. BLOB is an element or object. INFO is a plist used as
  4682. a communication channel. Return next exportable element or
  4683. object, a string, or nil.
  4684. When optional argument N is a positive integer, return a list
  4685. containing up to N siblings after BLOB, from closest to farthest.
  4686. With any other non-nil value, return a list containing all of
  4687. them."
  4688. (let ((siblings
  4689. ;; An object can belong to the contents of its parent or to
  4690. ;; a secondary string. We check the latter option first.
  4691. (let ((parent (org-export-get-parent blob)))
  4692. (or (let ((sec-value (org-element-property
  4693. (cdr (assq (org-element-type parent)
  4694. org-element-secondary-value-alist))
  4695. parent)))
  4696. (cdr (memq blob sec-value)))
  4697. (cdr (memq blob (org-element-contents parent))))))
  4698. next)
  4699. (catch 'exit
  4700. (mapc (lambda (obj)
  4701. (cond ((memq obj (plist-get info :ignore-list)))
  4702. ((null n) (throw 'exit obj))
  4703. ((not (wholenump n)) (push obj next))
  4704. ((zerop n) (throw 'exit (nreverse next)))
  4705. (t (decf n) (push obj next))))
  4706. siblings)
  4707. (nreverse next))))
  4708. ;;;; Translation
  4709. ;;
  4710. ;; `org-export-translate' translates a string according to the language
  4711. ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
  4712. ;; the dictionary used for the translation.
  4713. (defconst org-export-dictionary
  4714. '(("%e %n: %c"
  4715. ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
  4716. ("Author"
  4717. ("ca" :default "Autor")
  4718. ("cs" :default "Autor")
  4719. ("da" :default "Forfatter")
  4720. ("de" :default "Autor")
  4721. ("eo" :html "A&#365;toro")
  4722. ("es" :default "Autor")
  4723. ("et" :default "Autor")
  4724. ("fi" :html "Tekij&auml;")
  4725. ("fr" :default "Auteur")
  4726. ("hu" :default "Szerz&otilde;")
  4727. ("is" :html "H&ouml;fundur")
  4728. ("it" :default "Autore")
  4729. ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
  4730. ("nl" :default "Auteur")
  4731. ("no" :default "Forfatter")
  4732. ("nb" :default "Forfatter")
  4733. ("nn" :default "Forfattar")
  4734. ("pl" :default "Autor")
  4735. ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4736. ("sv" :html "F&ouml;rfattare")
  4737. ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
  4738. ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
  4739. ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
  4740. ("Continued from previous page"
  4741. ("de" :default "Fortsetzung von vorheriger Seite")
  4742. ("es" :default "Continúa de la página anterior")
  4743. ("fr" :default "Suite de la page précédente")
  4744. ("it" :default "Continua da pagina precedente")
  4745. ("ja" :utf-8 "前ページから続く")
  4746. ("nl" :default "Vervolg van vorige pagina")
  4747. ("pt" :default "Continuação da página anterior"))
  4748. ("Continued on next page"
  4749. ("de" :default "Fortsetzung nächste Seite")
  4750. ("es" :default "Continúa en la siguiente página")
  4751. ("fr" :default "Suite page suivante")
  4752. ("it" :default "Continua alla pagina successiva")
  4753. ("ja" :utf-8 "次ページに続く")
  4754. ("nl" :default "Vervolg op volgende pagina")
  4755. ("pt" :default "Continua na página seguinte"))
  4756. ("Date"
  4757. ("ca" :default "Data")
  4758. ("cs" :default "Datum")
  4759. ("da" :default "Dato")
  4760. ("de" :default "Datum")
  4761. ("eo" :default "Dato")
  4762. ("es" :default "Fecha")
  4763. ("et" :html "Kuup&#228;ev" :utf-8 "Kuupäev")
  4764. ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
  4765. ("hu" :html "D&aacute;tum")
  4766. ("is" :default "Dagsetning")
  4767. ("it" :default "Data")
  4768. ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
  4769. ("nl" :default "Datum")
  4770. ("no" :default "Dato")
  4771. ("nb" :default "Dato")
  4772. ("nn" :default "Dato")
  4773. ("pl" :default "Data")
  4774. ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4775. ("sv" :default "Datum")
  4776. ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
  4777. ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
  4778. ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
  4779. ("Equation"
  4780. ("da" :default "Ligning")
  4781. ("de" :default "Gleichung")
  4782. ("es" :html "Ecuaci&oacute;n" :default "Ecuación")
  4783. ("et" :html "V&#245;rrand" :utf-8 "Võrrand")
  4784. ("fr" :ascii "Equation" :default "Équation")
  4785. ("no" :default "Ligning")
  4786. ("nb" :default "Ligning")
  4787. ("nn" :default "Likning")
  4788. ("sv" :default "Ekvation")
  4789. ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
  4790. ("Figure"
  4791. ("da" :default "Figur")
  4792. ("de" :default "Abbildung")
  4793. ("es" :default "Figura")
  4794. ("et" :default "Joonis")
  4795. ("ja" :html "&#22259;" :utf-8 "図")
  4796. ("no" :default "Illustrasjon")
  4797. ("nb" :default "Illustrasjon")
  4798. ("nn" :default "Illustrasjon")
  4799. ("sv" :default "Illustration")
  4800. ("zh-CN" :html "&#22270;" :utf-8 "图"))
  4801. ("Figure %d:"
  4802. ("da" :default "Figur %d")
  4803. ("de" :default "Abbildung %d:")
  4804. ("es" :default "Figura %d:")
  4805. ("et" :default "Joonis %d:")
  4806. ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
  4807. ("ja" :html "&#22259;%d: " :utf-8 "図%d: ")
  4808. ("no" :default "Illustrasjon %d")
  4809. ("nb" :default "Illustrasjon %d")
  4810. ("nn" :default "Illustrasjon %d")
  4811. ("sv" :default "Illustration %d")
  4812. ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
  4813. ("Footnotes"
  4814. ("ca" :html "Peus de p&agrave;gina")
  4815. ("cs" :default "Pozn\xe1mky pod carou")
  4816. ("da" :default "Fodnoter")
  4817. ("de" :html "Fu&szlig;noten" :default "Fußnoten")
  4818. ("eo" :default "Piednotoj")
  4819. ("es" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
  4820. ("et" :html "Allm&#228;rkused" :utf-8 "Allmärkused")
  4821. ("fi" :default "Alaviitteet")
  4822. ("fr" :default "Notes de bas de page")
  4823. ("hu" :html "L&aacute;bjegyzet")
  4824. ("is" :html "Aftanm&aacute;lsgreinar")
  4825. ("it" :html "Note a pi&egrave; di pagina")
  4826. ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
  4827. ("nl" :default "Voetnoten")
  4828. ("no" :default "Fotnoter")
  4829. ("nb" :default "Fotnoter")
  4830. ("nn" :default "Fotnotar")
  4831. ("pl" :default "Przypis")
  4832. ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
  4833. ("sv" :default "Fotnoter")
  4834. ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
  4835. :utf-8 "Примітки")
  4836. ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
  4837. ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
  4838. ("List of Listings"
  4839. ("da" :default "Programmer")
  4840. ("de" :default "Programmauflistungsverzeichnis")
  4841. ("es" :default "Indice de Listados de programas")
  4842. ("et" :default "Loendite nimekiri")
  4843. ("fr" :default "Liste des programmes")
  4844. ("no" :default "Dataprogrammer")
  4845. ("nb" :default "Dataprogrammer")
  4846. ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
  4847. ("List of Tables"
  4848. ("da" :default "Tabeller")
  4849. ("de" :default "Tabellenverzeichnis")
  4850. ("es" :default "Indice de tablas")
  4851. ("et" :default "Tabelite nimekiri")
  4852. ("fr" :default "Liste des tableaux")
  4853. ("no" :default "Tabeller")
  4854. ("nb" :default "Tabeller")
  4855. ("nn" :default "Tabeller")
  4856. ("sv" :default "Tabeller")
  4857. ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
  4858. ("Listing %d:"
  4859. ("da" :default "Program %d")
  4860. ("de" :default "Programmlisting %d")
  4861. ("es" :default "Listado de programa %d")
  4862. ("et" :default "Loend %d")
  4863. ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
  4864. ("no" :default "Dataprogram %d")
  4865. ("nb" :default "Dataprogram %d")
  4866. ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
  4867. ("See section %s"
  4868. ("da" :default "jævnfør afsnit %s")
  4869. ("de" :default "siehe Abschnitt %s")
  4870. ("es" :default "vea seccion %s")
  4871. ("et" :html "Vaata peat&#252;kki %s" :utf-8 "Vaata peatükki %s")
  4872. ("fr" :default "cf. section %s")
  4873. ("zh-CN" :html "&#21442;&#35265;&#31532;%s&#33410;" :utf-8 "参见第%s节"))
  4874. ("Table"
  4875. ("de" :default "Tabelle")
  4876. ("es" :default "Tabla")
  4877. ("et" :default "Tabel")
  4878. ("fr" :default "Tableau")
  4879. ("ja" :html "&#34920;" :utf-8 "表")
  4880. ("zh-CN" :html "&#34920;" :utf-8 "表"))
  4881. ("Table %d:"
  4882. ("da" :default "Tabel %d")
  4883. ("de" :default "Tabelle %d")
  4884. ("es" :default "Tabla %d")
  4885. ("et" :default "Tabel %d")
  4886. ("fr" :default "Tableau %d :")
  4887. ("ja" :html "&#34920;%d:" :utf-8 "表%d:")
  4888. ("no" :default "Tabell %d")
  4889. ("nb" :default "Tabell %d")
  4890. ("nn" :default "Tabell %d")
  4891. ("sv" :default "Tabell %d")
  4892. ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
  4893. ("Table of Contents"
  4894. ("ca" :html "&Iacute;ndex")
  4895. ("cs" :default "Obsah")
  4896. ("da" :default "Indhold")
  4897. ("de" :default "Inhaltsverzeichnis")
  4898. ("eo" :default "Enhavo")
  4899. ("es" :html "&Iacute;ndice")
  4900. ("et" :default "Sisukord")
  4901. ("fi" :html "Sis&auml;llysluettelo")
  4902. ("fr" :ascii "Sommaire" :default "Table des matières")
  4903. ("hu" :html "Tartalomjegyz&eacute;k")
  4904. ("is" :default "Efnisyfirlit")
  4905. ("it" :default "Indice")
  4906. ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
  4907. ("nl" :default "Inhoudsopgave")
  4908. ("no" :default "Innhold")
  4909. ("nb" :default "Innhold")
  4910. ("nn" :default "Innhald")
  4911. ("pl" :html "Spis tre&#x015b;ci")
  4912. ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
  4913. :utf-8 "Содержание")
  4914. ("sv" :html "Inneh&aring;ll")
  4915. ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
  4916. ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
  4917. ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
  4918. ("Unknown reference"
  4919. ("da" :default "ukendt reference")
  4920. ("de" :default "Unbekannter Verweis")
  4921. ("es" :default "referencia desconocida")
  4922. ("et" :default "Tundmatu viide")
  4923. ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
  4924. ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
  4925. "Dictionary for export engine.
  4926. Alist whose CAR is the string to translate and CDR is an alist
  4927. whose CAR is the language string and CDR is a plist whose
  4928. properties are possible charsets and values translated terms.
  4929. It is used as a database for `org-export-translate'. Since this
  4930. function returns the string as-is if no translation was found,
  4931. the variable only needs to record values different from the
  4932. entry.")
  4933. (defun org-export-translate (s encoding info)
  4934. "Translate string S according to language specification.
  4935. ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
  4936. and `:utf-8'. INFO is a plist used as a communication channel.
  4937. Translation depends on `:language' property. Return the
  4938. translated string. If no translation is found, try to fall back
  4939. to `:default' encoding. If it fails, return S."
  4940. (let* ((lang (plist-get info :language))
  4941. (translations (cdr (assoc lang
  4942. (cdr (assoc s org-export-dictionary))))))
  4943. (or (plist-get translations encoding)
  4944. (plist-get translations :default)
  4945. s)))
  4946. ;;; Asynchronous Export
  4947. ;;
  4948. ;; `org-export-async-start' is the entry point for asynchronous
  4949. ;; export. It recreates current buffer (including visibility,
  4950. ;; narrowing and visited file) in an external Emacs process, and
  4951. ;; evaluates a command there. It then applies a function on the
  4952. ;; returned results in the current process.
  4953. ;;
  4954. ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
  4955. ;; allow to export to a buffer or a file, asynchronously or not.
  4956. ;;
  4957. ;; `org-export-output-file-name' is an auxiliary function meant to be
  4958. ;; used with `org-export-to-file'. With a given extension, it tries
  4959. ;; to provide a canonical file name to write export output to.
  4960. ;;
  4961. ;; Asynchronously generated results are never displayed directly.
  4962. ;; Instead, they are stored in `org-export-stack-contents'. They can
  4963. ;; then be retrieved by calling `org-export-stack'.
  4964. ;;
  4965. ;; Export Stack is viewed through a dedicated major mode
  4966. ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
  4967. ;;`org-export-stack-delete', `org-export-stack-view' and
  4968. ;;`org-export-stack-clear'.
  4969. ;;
  4970. ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
  4971. ;; It should be used whenever `org-export-async-start' is called.
  4972. (defmacro org-export-async-start (fun &rest body)
  4973. "Call function FUN on the results returned by BODY evaluation.
  4974. BODY evaluation happens in an asynchronous process, from a buffer
  4975. which is an exact copy of the current one.
  4976. Use `org-export-add-to-stack' in FUN in order to register results
  4977. in the stack.
  4978. This is a low level function. See also `org-export-to-buffer'
  4979. and `org-export-to-file' for more specialized functions."
  4980. (declare (indent 1) (debug t))
  4981. (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
  4982. ;; Write the full sexp evaluating BODY in a copy of the current
  4983. ;; buffer to a temporary file, as it may be too long for program
  4984. ;; args in `start-process'.
  4985. `(with-temp-message "Initializing asynchronous export process"
  4986. (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
  4987. (,temp-file (make-temp-file "org-export-process"))
  4988. (,coding buffer-file-coding-system))
  4989. (with-temp-file ,temp-file
  4990. (insert
  4991. ;; Null characters (from variable values) are inserted
  4992. ;; within the file. As a consequence, coding system for
  4993. ;; buffer contents will not be recognized properly. So,
  4994. ;; we make sure it is the same as the one used to display
  4995. ;; the original buffer.
  4996. (format ";; -*- coding: %s; -*-\n%S"
  4997. ,coding
  4998. `(with-temp-buffer
  4999. (when org-export-async-debug '(setq debug-on-error t))
  5000. ;; Ignore `kill-emacs-hook' and code evaluation
  5001. ;; queries from Babel as we need a truly
  5002. ;; non-interactive process.
  5003. (setq kill-emacs-hook nil
  5004. org-babel-confirm-evaluate-answer-no t)
  5005. ;; Initialize export framework.
  5006. (require 'ox)
  5007. ;; Re-create current buffer there.
  5008. (funcall ,,copy-fun)
  5009. (restore-buffer-modified-p nil)
  5010. ;; Sexp to evaluate in the buffer.
  5011. (print (progn ,,@body))))))
  5012. ;; Start external process.
  5013. (let* ((process-connection-type nil)
  5014. (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
  5015. (,process
  5016. (apply
  5017. #'start-process
  5018. (append
  5019. (list "org-export-process"
  5020. ,proc-buffer
  5021. (expand-file-name invocation-name invocation-directory)
  5022. "--batch")
  5023. (if org-export-async-init-file
  5024. (list "-Q" "-l" org-export-async-init-file)
  5025. (list "-l" user-init-file))
  5026. (list "-l" ,temp-file)))))
  5027. ;; Register running process in stack.
  5028. (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
  5029. ;; Set-up sentinel in order to catch results.
  5030. (let ((handler ,fun))
  5031. (set-process-sentinel
  5032. ,process
  5033. `(lambda (p status)
  5034. (let ((proc-buffer (process-buffer p)))
  5035. (when (eq (process-status p) 'exit)
  5036. (unwind-protect
  5037. (if (zerop (process-exit-status p))
  5038. (unwind-protect
  5039. (let ((results
  5040. (with-current-buffer proc-buffer
  5041. (goto-char (point-max))
  5042. (backward-sexp)
  5043. (read (current-buffer)))))
  5044. (funcall ,handler results))
  5045. (unless org-export-async-debug
  5046. (and (get-buffer proc-buffer)
  5047. (kill-buffer proc-buffer))))
  5048. (org-export-add-to-stack proc-buffer nil p)
  5049. (ding)
  5050. (message "Process '%s' exited abnormally" p))
  5051. (unless org-export-async-debug
  5052. (delete-file ,,temp-file)))))))))))))
  5053. ;;;###autoload
  5054. (defun org-export-to-buffer
  5055. (backend buffer
  5056. &optional async subtreep visible-only body-only ext-plist
  5057. post-process)
  5058. "Call `org-export-as' with output to a specified buffer.
  5059. BACKEND is either an export back-end, as returned by, e.g.,
  5060. `org-export-create-backend', or a symbol referring to
  5061. a registered back-end.
  5062. BUFFER is the name of the output buffer. If it already exists,
  5063. it will be erased first, otherwise, it will be created.
  5064. A non-nil optional argument ASYNC means the process should happen
  5065. asynchronously. The resulting buffer should then be accessible
  5066. through the `org-export-stack' interface. When ASYNC is nil, the
  5067. buffer is displayed if `org-export-show-temporary-export-buffer'
  5068. is non-nil.
  5069. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  5070. EXT-PLIST are similar to those used in `org-export-as', which
  5071. see.
  5072. Optional argument POST-PROCESS is a function which should accept
  5073. no argument. It is always called within the current process,
  5074. from BUFFER, with point at its beginning. Export back-ends can
  5075. use it to set a major mode there, e.g,
  5076. \(defun org-latex-export-as-latex
  5077. \(&optional async subtreep visible-only body-only ext-plist)
  5078. \(interactive)
  5079. \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
  5080. async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
  5081. This function returns BUFFER."
  5082. (declare (indent 2))
  5083. (if async
  5084. (org-export-async-start
  5085. `(lambda (output)
  5086. (with-current-buffer (get-buffer-create ,buffer)
  5087. (erase-buffer)
  5088. (setq buffer-file-coding-system ',buffer-file-coding-system)
  5089. (insert output)
  5090. (goto-char (point-min))
  5091. (org-export-add-to-stack (current-buffer) ',backend)
  5092. (ignore-errors (funcall ,post-process))))
  5093. `(org-export-as
  5094. ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
  5095. (let ((output
  5096. (org-export-as backend subtreep visible-only body-only ext-plist))
  5097. (buffer (get-buffer-create buffer))
  5098. (encoding buffer-file-coding-system))
  5099. (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
  5100. (org-kill-new output))
  5101. (with-current-buffer buffer
  5102. (erase-buffer)
  5103. (setq buffer-file-coding-system encoding)
  5104. (insert output)
  5105. (goto-char (point-min))
  5106. (and (functionp post-process) (funcall post-process)))
  5107. (when org-export-show-temporary-export-buffer
  5108. (switch-to-buffer-other-window buffer))
  5109. buffer)))
  5110. ;;;###autoload
  5111. (defun org-export-to-file
  5112. (backend file &optional async subtreep visible-only body-only ext-plist
  5113. post-process)
  5114. "Call `org-export-as' with output to a specified file.
  5115. BACKEND is either an export back-end, as returned by, e.g.,
  5116. `org-export-create-backend', or a symbol referring to
  5117. a registered back-end. FILE is the name of the output file, as
  5118. a string.
  5119. A non-nil optional argument ASYNC means the process should happen
  5120. asynchronously. The resulting buffer file then be accessible
  5121. through the `org-export-stack' interface.
  5122. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
  5123. EXT-PLIST are similar to those used in `org-export-as', which
  5124. see.
  5125. Optional argument POST-PROCESS is called with FILE as its
  5126. argument and happens asynchronously when ASYNC is non-nil. It
  5127. has to return a file name, or nil. Export back-ends can use this
  5128. to send the output file through additional processing, e.g,
  5129. \(defun org-latex-export-to-latex
  5130. \(&optional async subtreep visible-only body-only ext-plist)
  5131. \(interactive)
  5132. \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
  5133. \(org-export-to-file 'latex outfile
  5134. async subtreep visible-only body-only ext-plist
  5135. \(lambda (file) (org-latex-compile file)))
  5136. The function returns either a file name returned by POST-PROCESS,
  5137. or FILE."
  5138. (declare (indent 2))
  5139. (if (not (file-writable-p file)) (error "Output file not writable")
  5140. (let ((encoding (or org-export-coding-system buffer-file-coding-system)))
  5141. (if async
  5142. (org-export-async-start
  5143. `(lambda (file)
  5144. (org-export-add-to-stack (expand-file-name file) ',backend))
  5145. `(let ((output
  5146. (org-export-as
  5147. ',backend ,subtreep ,visible-only ,body-only
  5148. ',ext-plist)))
  5149. (with-temp-buffer
  5150. (insert output)
  5151. (let ((coding-system-for-write ',encoding))
  5152. (write-file ,file)))
  5153. (or (ignore-errors (funcall ',post-process ,file)) ,file)))
  5154. (let ((output (org-export-as
  5155. backend subtreep visible-only body-only ext-plist)))
  5156. (with-temp-buffer
  5157. (insert output)
  5158. (let ((coding-system-for-write encoding))
  5159. (write-file file)))
  5160. (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
  5161. (org-kill-new output))
  5162. ;; Get proper return value.
  5163. (or (and (functionp post-process) (funcall post-process file))
  5164. file))))))
  5165. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  5166. "Return output file's name according to buffer specifications.
  5167. EXTENSION is a string representing the output file extension,
  5168. with the leading dot.
  5169. With a non-nil optional argument SUBTREEP, try to determine
  5170. output file's name by looking for \"EXPORT_FILE_NAME\" property
  5171. of subtree at point.
  5172. When optional argument PUB-DIR is set, use it as the publishing
  5173. directory.
  5174. When optional argument VISIBLE-ONLY is non-nil, don't export
  5175. contents of hidden elements.
  5176. Return file name as a string."
  5177. (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
  5178. (base-name
  5179. ;; File name may come from EXPORT_FILE_NAME subtree
  5180. ;; property, assuming point is at beginning of said
  5181. ;; sub-tree.
  5182. (file-name-sans-extension
  5183. (or (and subtreep
  5184. (org-entry-get
  5185. (save-excursion
  5186. (ignore-errors (org-back-to-heading) (point)))
  5187. "EXPORT_FILE_NAME" t))
  5188. ;; File name may be extracted from buffer's associated
  5189. ;; file, if any.
  5190. (and visited-file (file-name-nondirectory visited-file))
  5191. ;; Can't determine file name on our own: Ask user.
  5192. (let ((read-file-name-function
  5193. (and org-completion-use-ido 'ido-read-file-name)))
  5194. (read-file-name
  5195. "Output file: " pub-dir nil nil nil
  5196. (lambda (name)
  5197. (string= (file-name-extension name t) extension)))))))
  5198. (output-file
  5199. ;; Build file name. Enforce EXTENSION over whatever user
  5200. ;; may have come up with. PUB-DIR, if defined, always has
  5201. ;; precedence over any provided path.
  5202. (cond
  5203. (pub-dir
  5204. (concat (file-name-as-directory pub-dir)
  5205. (file-name-nondirectory base-name)
  5206. extension))
  5207. ((file-name-absolute-p base-name) (concat base-name extension))
  5208. (t (concat (file-name-as-directory ".") base-name extension)))))
  5209. ;; If writing to OUTPUT-FILE would overwrite original file, append
  5210. ;; EXTENSION another time to final name.
  5211. (if (and visited-file (org-file-equal-p visited-file output-file))
  5212. (concat output-file extension)
  5213. output-file)))
  5214. (defun org-export-add-to-stack (source backend &optional process)
  5215. "Add a new result to export stack if not present already.
  5216. SOURCE is a buffer or a file name containing export results.
  5217. BACKEND is a symbol representing export back-end used to generate
  5218. it.
  5219. Entries already pointing to SOURCE and unavailable entries are
  5220. removed beforehand. Return the new stack."
  5221. (setq org-export-stack-contents
  5222. (cons (list source backend (or process (current-time)))
  5223. (org-export-stack-remove source))))
  5224. (defun org-export-stack ()
  5225. "Menu for asynchronous export results and running processes."
  5226. (interactive)
  5227. (let ((buffer (get-buffer-create "*Org Export Stack*")))
  5228. (set-buffer buffer)
  5229. (when (zerop (buffer-size)) (org-export-stack-mode))
  5230. (org-export-stack-refresh)
  5231. (pop-to-buffer buffer))
  5232. (message "Type \"q\" to quit, \"?\" for help"))
  5233. (defun org-export--stack-source-at-point ()
  5234. "Return source from export results at point in stack."
  5235. (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
  5236. (if (not source) (error "Source unavailable, please refresh buffer")
  5237. (let ((source-name (if (stringp source) source (buffer-name source))))
  5238. (if (save-excursion
  5239. (beginning-of-line)
  5240. (looking-at (concat ".* +" (regexp-quote source-name) "$")))
  5241. source
  5242. ;; SOURCE is not consistent with current line. The stack
  5243. ;; view is outdated.
  5244. (error "Source unavailable; type `g' to update buffer"))))))
  5245. (defun org-export-stack-clear ()
  5246. "Remove all entries from export stack."
  5247. (interactive)
  5248. (setq org-export-stack-contents nil))
  5249. (defun org-export-stack-refresh (&rest dummy)
  5250. "Refresh the asynchronous export stack.
  5251. DUMMY is ignored. Unavailable sources are removed from the list.
  5252. Return the new stack."
  5253. (let ((inhibit-read-only t))
  5254. (org-preserve-lc
  5255. (erase-buffer)
  5256. (insert (concat
  5257. (let ((counter 0))
  5258. (mapconcat
  5259. (lambda (entry)
  5260. (let ((proc-p (processp (nth 2 entry))))
  5261. (concat
  5262. ;; Back-end.
  5263. (format " %-12s " (or (nth 1 entry) ""))
  5264. ;; Age.
  5265. (let ((data (nth 2 entry)))
  5266. (if proc-p (format " %6s " (process-status data))
  5267. ;; Compute age of the results.
  5268. (org-format-seconds
  5269. "%4h:%.2m "
  5270. (float-time (time-since data)))))
  5271. ;; Source.
  5272. (format " %s"
  5273. (let ((source (car entry)))
  5274. (if (stringp source) source
  5275. (buffer-name source)))))))
  5276. ;; Clear stack from exited processes, dead buffers or
  5277. ;; non-existent files.
  5278. (setq org-export-stack-contents
  5279. (org-remove-if-not
  5280. (lambda (el)
  5281. (if (processp (nth 2 el))
  5282. (buffer-live-p (process-buffer (nth 2 el)))
  5283. (let ((source (car el)))
  5284. (if (bufferp source) (buffer-live-p source)
  5285. (file-exists-p source)))))
  5286. org-export-stack-contents)) "\n")))))))
  5287. (defun org-export-stack-remove (&optional source)
  5288. "Remove export results at point from stack.
  5289. If optional argument SOURCE is non-nil, remove it instead."
  5290. (interactive)
  5291. (let ((source (or source (org-export--stack-source-at-point))))
  5292. (setq org-export-stack-contents
  5293. (org-remove-if (lambda (el) (equal (car el) source))
  5294. org-export-stack-contents))))
  5295. (defun org-export-stack-view (&optional in-emacs)
  5296. "View export results at point in stack.
  5297. With an optional prefix argument IN-EMACS, force viewing files
  5298. within Emacs."
  5299. (interactive "P")
  5300. (let ((source (org-export--stack-source-at-point)))
  5301. (cond ((processp source)
  5302. (org-switch-to-buffer-other-window (process-buffer source)))
  5303. ((bufferp source) (org-switch-to-buffer-other-window source))
  5304. (t (org-open-file source in-emacs)))))
  5305. (defvar org-export-stack-mode-map
  5306. (let ((km (make-sparse-keymap)))
  5307. (define-key km " " 'next-line)
  5308. (define-key km "n" 'next-line)
  5309. (define-key km "\C-n" 'next-line)
  5310. (define-key km [down] 'next-line)
  5311. (define-key km "p" 'previous-line)
  5312. (define-key km "\C-p" 'previous-line)
  5313. (define-key km "\C-?" 'previous-line)
  5314. (define-key km [up] 'previous-line)
  5315. (define-key km "C" 'org-export-stack-clear)
  5316. (define-key km "v" 'org-export-stack-view)
  5317. (define-key km (kbd "RET") 'org-export-stack-view)
  5318. (define-key km "d" 'org-export-stack-remove)
  5319. km)
  5320. "Keymap for Org Export Stack.")
  5321. (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
  5322. "Mode for displaying asynchronous export stack.
  5323. Type \\[org-export-stack] to visualize the asynchronous export
  5324. stack.
  5325. In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
  5326. on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
  5327. stack completely.
  5328. Removing entries in an Org Export Stack buffer doesn't affect
  5329. files or buffers, only the display.
  5330. \\{org-export-stack-mode-map}"
  5331. (abbrev-mode 0)
  5332. (auto-fill-mode 0)
  5333. (setq buffer-read-only t
  5334. buffer-undo-list t
  5335. truncate-lines t
  5336. header-line-format
  5337. '(:eval
  5338. (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
  5339. (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
  5340. (set (make-local-variable 'revert-buffer-function)
  5341. 'org-export-stack-refresh))
  5342. ;;; The Dispatcher
  5343. ;;
  5344. ;; `org-export-dispatch' is the standard interactive way to start an
  5345. ;; export process. It uses `org-export--dispatch-ui' as a subroutine
  5346. ;; for its interface, which, in turn, delegates response to key
  5347. ;; pressed to `org-export--dispatch-action'.
  5348. ;;;###autoload
  5349. (defun org-export-dispatch (&optional arg)
  5350. "Export dispatcher for Org mode.
  5351. It provides an access to common export related tasks in a buffer.
  5352. Its interface comes in two flavours: standard and expert.
  5353. While both share the same set of bindings, only the former
  5354. displays the valid keys associations in a dedicated buffer.
  5355. Scrolling (resp. line-wise motion) in this buffer is done with
  5356. SPC and DEL (resp. C-n and C-p) keys.
  5357. Set variable `org-export-dispatch-use-expert-ui' to switch to one
  5358. flavour or the other.
  5359. When ARG is \\[universal-argument], repeat the last export action, with the same set
  5360. of options used back then, on the current buffer.
  5361. When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
  5362. (interactive "P")
  5363. (let* ((input
  5364. (cond ((equal arg '(16)) '(stack))
  5365. ((and arg org-export-dispatch-last-action))
  5366. (t (save-window-excursion
  5367. (unwind-protect
  5368. (progn
  5369. ;; Remember where we are
  5370. (move-marker org-export-dispatch-last-position
  5371. (point)
  5372. (org-base-buffer (current-buffer)))
  5373. ;; Get and store an export command
  5374. (setq org-export-dispatch-last-action
  5375. (org-export--dispatch-ui
  5376. (list org-export-initial-scope
  5377. (and org-export-in-background 'async))
  5378. nil
  5379. org-export-dispatch-use-expert-ui)))
  5380. (and (get-buffer "*Org Export Dispatcher*")
  5381. (kill-buffer "*Org Export Dispatcher*")))))))
  5382. (action (car input))
  5383. (optns (cdr input)))
  5384. (unless (memq 'subtree optns)
  5385. (move-marker org-export-dispatch-last-position nil))
  5386. (case action
  5387. ;; First handle special hard-coded actions.
  5388. (template (org-export-insert-default-template nil optns))
  5389. (stack (org-export-stack))
  5390. (publish-current-file
  5391. (org-publish-current-file (memq 'force optns) (memq 'async optns)))
  5392. (publish-current-project
  5393. (org-publish-current-project (memq 'force optns) (memq 'async optns)))
  5394. (publish-choose-project
  5395. (org-publish (assoc (org-icompleting-read
  5396. "Publish project: "
  5397. org-publish-project-alist nil t)
  5398. org-publish-project-alist)
  5399. (memq 'force optns)
  5400. (memq 'async optns)))
  5401. (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
  5402. (otherwise
  5403. (save-excursion
  5404. (when arg
  5405. ;; Repeating command, maybe move cursor to restore subtree
  5406. ;; context.
  5407. (if (eq (marker-buffer org-export-dispatch-last-position)
  5408. (org-base-buffer (current-buffer)))
  5409. (goto-char org-export-dispatch-last-position)
  5410. ;; We are in a different buffer, forget position.
  5411. (move-marker org-export-dispatch-last-position nil)))
  5412. (funcall action
  5413. ;; Return a symbol instead of a list to ease
  5414. ;; asynchronous export macro use.
  5415. (and (memq 'async optns) t)
  5416. (and (memq 'subtree optns) t)
  5417. (and (memq 'visible optns) t)
  5418. (and (memq 'body optns) t)))))))
  5419. (defun org-export--dispatch-ui (options first-key expertp)
  5420. "Handle interface for `org-export-dispatch'.
  5421. OPTIONS is a list containing current interactive options set for
  5422. export. It can contain any of the following symbols:
  5423. `body' toggles a body-only export
  5424. `subtree' restricts export to current subtree
  5425. `visible' restricts export to visible part of buffer.
  5426. `force' force publishing files.
  5427. `async' use asynchronous export process
  5428. FIRST-KEY is the key pressed to select the first level menu. It
  5429. is nil when this menu hasn't been selected yet.
  5430. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  5431. buffer is provided, but indications about currently active
  5432. options are given in the prompt. Moreover, \[?] allows to switch
  5433. back to standard interface."
  5434. (let* ((fontify-key
  5435. (lambda (key &optional access-key)
  5436. ;; Fontify KEY string. Optional argument ACCESS-KEY, when
  5437. ;; non-nil is the required first-level key to activate
  5438. ;; KEY. When its value is t, activate KEY independently
  5439. ;; on the first key, if any. A nil value means KEY will
  5440. ;; only be activated at first level.
  5441. (if (or (eq access-key t) (eq access-key first-key))
  5442. (org-propertize key 'face 'org-warning)
  5443. key)))
  5444. (fontify-value
  5445. (lambda (value)
  5446. ;; Fontify VALUE string.
  5447. (org-propertize value 'face 'font-lock-variable-name-face)))
  5448. ;; Prepare menu entries by extracting them from registered
  5449. ;; back-ends and sorting them by access key and by ordinal,
  5450. ;; if any.
  5451. (entries
  5452. (sort (sort (delq nil
  5453. (mapcar 'org-export-backend-menu
  5454. org-export--registered-backends))
  5455. (lambda (a b)
  5456. (let ((key-a (nth 1 a))
  5457. (key-b (nth 1 b)))
  5458. (cond ((and (numberp key-a) (numberp key-b))
  5459. (< key-a key-b))
  5460. ((numberp key-b) t)))))
  5461. 'car-less-than-car))
  5462. ;; Compute a list of allowed keys based on the first key
  5463. ;; pressed, if any. Some keys
  5464. ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
  5465. ;; available.
  5466. (allowed-keys
  5467. (nconc (list 2 22 19 6 1)
  5468. (if (not first-key) (org-uniquify (mapcar 'car entries))
  5469. (let (sub-menu)
  5470. (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
  5471. (when (eq (car entry) first-key)
  5472. (setq sub-menu (append (nth 2 entry) sub-menu))))))
  5473. (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
  5474. ((not first-key) (list ?P)))
  5475. (list ?& ?#)
  5476. (when expertp (list ??))
  5477. (list ?q)))
  5478. ;; Build the help menu for standard UI.
  5479. (help
  5480. (unless expertp
  5481. (concat
  5482. ;; Options are hard-coded.
  5483. (format "[%s] Body only: %s [%s] Visible only: %s
  5484. \[%s] Export scope: %s [%s] Force publishing: %s
  5485. \[%s] Async export: %s\n\n"
  5486. (funcall fontify-key "C-b" t)
  5487. (funcall fontify-value
  5488. (if (memq 'body options) "On " "Off"))
  5489. (funcall fontify-key "C-v" t)
  5490. (funcall fontify-value
  5491. (if (memq 'visible options) "On " "Off"))
  5492. (funcall fontify-key "C-s" t)
  5493. (funcall fontify-value
  5494. (if (memq 'subtree options) "Subtree" "Buffer "))
  5495. (funcall fontify-key "C-f" t)
  5496. (funcall fontify-value
  5497. (if (memq 'force options) "On " "Off"))
  5498. (funcall fontify-key "C-a" t)
  5499. (funcall fontify-value
  5500. (if (memq 'async options) "On " "Off")))
  5501. ;; Display registered back-end entries. When a key
  5502. ;; appears for the second time, do not create another
  5503. ;; entry, but append its sub-menu to existing menu.
  5504. (let (last-key)
  5505. (mapconcat
  5506. (lambda (entry)
  5507. (let ((top-key (car entry)))
  5508. (concat
  5509. (unless (eq top-key last-key)
  5510. (setq last-key top-key)
  5511. (format "\n[%s] %s\n"
  5512. (funcall fontify-key (char-to-string top-key))
  5513. (nth 1 entry)))
  5514. (let ((sub-menu (nth 2 entry)))
  5515. (unless (functionp sub-menu)
  5516. ;; Split sub-menu into two columns.
  5517. (let ((index -1))
  5518. (concat
  5519. (mapconcat
  5520. (lambda (sub-entry)
  5521. (incf index)
  5522. (format
  5523. (if (zerop (mod index 2)) " [%s] %-26s"
  5524. "[%s] %s\n")
  5525. (funcall fontify-key
  5526. (char-to-string (car sub-entry))
  5527. top-key)
  5528. (nth 1 sub-entry)))
  5529. sub-menu "")
  5530. (when (zerop (mod index 2)) "\n"))))))))
  5531. entries ""))
  5532. ;; Publishing menu is hard-coded.
  5533. (format "\n[%s] Publish
  5534. [%s] Current file [%s] Current project
  5535. [%s] Choose project [%s] All projects\n\n\n"
  5536. (funcall fontify-key "P")
  5537. (funcall fontify-key "f" ?P)
  5538. (funcall fontify-key "p" ?P)
  5539. (funcall fontify-key "x" ?P)
  5540. (funcall fontify-key "a" ?P))
  5541. (format "[%s] Export stack [%s] Insert template\n"
  5542. (funcall fontify-key "&" t)
  5543. (funcall fontify-key "#" t))
  5544. (format "[%s] %s"
  5545. (funcall fontify-key "q" t)
  5546. (if first-key "Main menu" "Exit")))))
  5547. ;; Build prompts for both standard and expert UI.
  5548. (standard-prompt (unless expertp "Export command: "))
  5549. (expert-prompt
  5550. (when expertp
  5551. (format
  5552. "Export command (C-%s%s%s%s%s) [%s]: "
  5553. (if (memq 'body options) (funcall fontify-key "b" t) "b")
  5554. (if (memq 'visible options) (funcall fontify-key "v" t) "v")
  5555. (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
  5556. (if (memq 'force options) (funcall fontify-key "f" t) "f")
  5557. (if (memq 'async options) (funcall fontify-key "a" t) "a")
  5558. (mapconcat (lambda (k)
  5559. ;; Strip control characters.
  5560. (unless (< k 27) (char-to-string k)))
  5561. allowed-keys "")))))
  5562. ;; With expert UI, just read key with a fancy prompt. In standard
  5563. ;; UI, display an intrusive help buffer.
  5564. (if expertp
  5565. (org-export--dispatch-action
  5566. expert-prompt allowed-keys entries options first-key expertp)
  5567. ;; At first call, create frame layout in order to display menu.
  5568. (unless (get-buffer "*Org Export Dispatcher*")
  5569. (delete-other-windows)
  5570. (org-switch-to-buffer-other-window
  5571. (get-buffer-create "*Org Export Dispatcher*"))
  5572. (setq cursor-type nil
  5573. header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
  5574. ;; Make sure that invisible cursor will not highlight square
  5575. ;; brackets.
  5576. (set-syntax-table (copy-syntax-table))
  5577. (modify-syntax-entry ?\[ "w"))
  5578. ;; At this point, the buffer containing the menu exists and is
  5579. ;; visible in the current window. So, refresh it.
  5580. (with-current-buffer "*Org Export Dispatcher*"
  5581. ;; Refresh help. Maintain display continuity by re-visiting
  5582. ;; previous window position.
  5583. (let ((pos (window-start)))
  5584. (erase-buffer)
  5585. (insert help)
  5586. (set-window-start nil pos)))
  5587. (org-fit-window-to-buffer)
  5588. (org-export--dispatch-action
  5589. standard-prompt allowed-keys entries options first-key expertp))))
  5590. (defun org-export--dispatch-action
  5591. (prompt allowed-keys entries options first-key expertp)
  5592. "Read a character from command input and act accordingly.
  5593. PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
  5594. a list of characters available at a given step in the process.
  5595. ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
  5596. EXPERTP are the same as defined in `org-export--dispatch-ui',
  5597. which see.
  5598. Toggle export options when required. Otherwise, return value is
  5599. a list with action as CAR and a list of interactive export
  5600. options as CDR."
  5601. (let (key)
  5602. ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
  5603. ;; C-p, SPC, DEL).
  5604. (while (and (setq key (read-char-exclusive prompt))
  5605. (not expertp)
  5606. (memq key '(14 16 ?\s ?\d)))
  5607. (case key
  5608. (14 (if (not (pos-visible-in-window-p (point-max)))
  5609. (ignore-errors (scroll-up 1))
  5610. (message "End of buffer")
  5611. (sit-for 1)))
  5612. (16 (if (not (pos-visible-in-window-p (point-min)))
  5613. (ignore-errors (scroll-down 1))
  5614. (message "Beginning of buffer")
  5615. (sit-for 1)))
  5616. (?\s (if (not (pos-visible-in-window-p (point-max)))
  5617. (scroll-up nil)
  5618. (message "End of buffer")
  5619. (sit-for 1)))
  5620. (?\d (if (not (pos-visible-in-window-p (point-min)))
  5621. (scroll-down nil)
  5622. (message "Beginning of buffer")
  5623. (sit-for 1)))))
  5624. (cond
  5625. ;; Ignore undefined associations.
  5626. ((not (memq key allowed-keys))
  5627. (ding)
  5628. (unless expertp (message "Invalid key") (sit-for 1))
  5629. (org-export--dispatch-ui options first-key expertp))
  5630. ;; q key at first level aborts export. At second level, cancel
  5631. ;; first key instead.
  5632. ((eq key ?q) (if (not first-key) (error "Export aborted")
  5633. (org-export--dispatch-ui options nil expertp)))
  5634. ;; Help key: Switch back to standard interface if expert UI was
  5635. ;; active.
  5636. ((eq key ??) (org-export--dispatch-ui options first-key nil))
  5637. ;; Send request for template insertion along with export scope.
  5638. ((eq key ?#) (cons 'template (memq 'subtree options)))
  5639. ;; Switch to asynchronous export stack.
  5640. ((eq key ?&) '(stack))
  5641. ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
  5642. ((memq key '(2 22 19 6 1))
  5643. (org-export--dispatch-ui
  5644. (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
  5645. (6 'force) (1 'async))))
  5646. (if (memq option options) (remq option options)
  5647. (cons option options)))
  5648. first-key expertp))
  5649. ;; Action selected: Send key and options back to
  5650. ;; `org-export-dispatch'.
  5651. ((or first-key (functionp (nth 2 (assq key entries))))
  5652. (cons (cond
  5653. ((not first-key) (nth 2 (assq key entries)))
  5654. ;; Publishing actions are hard-coded. Send a special
  5655. ;; signal to `org-export-dispatch'.
  5656. ((eq first-key ?P)
  5657. (case key
  5658. (?f 'publish-current-file)
  5659. (?p 'publish-current-project)
  5660. (?x 'publish-choose-project)
  5661. (?a 'publish-all)))
  5662. ;; Return first action associated to FIRST-KEY + KEY
  5663. ;; path. Indeed, derived backends can share the same
  5664. ;; FIRST-KEY.
  5665. (t (catch 'found
  5666. (mapc (lambda (entry)
  5667. (let ((match (assq key (nth 2 entry))))
  5668. (when match (throw 'found (nth 2 match)))))
  5669. (member (assq first-key entries) entries)))))
  5670. options))
  5671. ;; Otherwise, enter sub-menu.
  5672. (t (org-export--dispatch-ui options key expertp)))))
  5673. (provide 'ox)
  5674. ;; Local variables:
  5675. ;; generated-autoload-file: "org-loaddefs.el"
  5676. ;; End:
  5677. ;;; ox.el ends here