ox.el 217 KB

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