ox.el 215 KB

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