ox.el 225 KB

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