ox.el 233 KB

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