ox.el 226 KB

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