org-export.el 125 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  1. ;;; org-export.el --- Generic Export Engine For Org
  2. ;; Copyright (C) 2012 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; This program 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. ;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library implements a generic export engine for Org, built on
  17. ;; its syntactical parser: Org Elements.
  18. ;; Besides that parser, the generic exporter is made of three distinct
  19. ;; parts:
  20. ;; - The communication channel consists in a property list, which is
  21. ;; created and updated during the process. Its use is to offer
  22. ;; every piece of information, would it be about initial environment
  23. ;; or contextual data, all in a single place. The exhaustive list
  24. ;; of properties is given in "The Communication Channel" section of
  25. ;; this file.
  26. ;; - The transcoder walks the parse tree, ignores or treat as plain
  27. ;; text elements and objects according to export options, and
  28. ;; eventually calls back-end specific functions to do the real
  29. ;; transcoding, concatenating their return value along the way.
  30. ;; - The filter system is activated at the very beginning and the very
  31. ;; end of the export process, and each time an element or an object
  32. ;; has been converted. It is the entry point to fine-tune standard
  33. ;; output from back-end transcoders.
  34. ;; The core function is `org-export-as'. It returns the transcoded
  35. ;; buffer as a string.
  36. ;; In order to derive an exporter out of this generic implementation,
  37. ;; one can define a transcode function for each element or object.
  38. ;; Such function should return a string for the corresponding element,
  39. ;; without any trailing space, or nil. It must accept three
  40. ;; arguments:
  41. ;; 1. the element or object itself,
  42. ;; 2. its contents, or nil when it isn't recursive,
  43. ;; 3. the property list used as a communication channel.
  44. ;; If no such function is found, that element or object type will
  45. ;; simply be ignored, along with any separating blank line. The same
  46. ;; will happen if the function returns the nil value. If that
  47. ;; function returns the empty string, the type will be ignored, but
  48. ;; the blank lines will be kept.
  49. ;; Contents, when not nil, are stripped from any global indentation
  50. ;; (although the relative one is preserved). They also always end
  51. ;; with a single newline character.
  52. ;; These functions must follow a strict naming convention:
  53. ;; `org-BACKEND-TYPE' where, obviously, BACKEND is the name of the
  54. ;; export back-end and TYPE the type of the element or object handled.
  55. ;; Moreover, two additional functions can be defined. On the one
  56. ;; hand, `org-BACKEND-template' returns the final transcoded string,
  57. ;; and can be used to add a preamble and a postamble to document's
  58. ;; body. It must accept two arguments: the transcoded string and the
  59. ;; property list containing export options. On the other hand,
  60. ;; `org-BACKEND-plain-text', when defined, is to be called on every
  61. ;; text not recognized as an element or an object. It must accept two
  62. ;; arguments: the text string and the information channel.
  63. ;; Any back-end can define its own variables. Among them, those
  64. ;; customizables should belong to the `org-export-BACKEND' group.
  65. ;; Also, a special variable, `org-BACKEND-option-alist', allows to
  66. ;; define buffer keywords and "#+options:" items specific to that
  67. ;; back-end. See `org-export-option-alist' for supported defaults and
  68. ;; syntax.
  69. ;; Tools for common tasks across back-ends are implemented in the
  70. ;; penultimate part of this file. A dispatcher for standard back-ends
  71. ;; is provided in the last one.
  72. ;;; Code:
  73. (eval-when-compile (require 'cl))
  74. (require 'org-element)
  75. ;; Require major back-ends and publishing tools
  76. (require 'org-e-ascii "../../EXPERIMENTAL/org-e-ascii.el")
  77. (require 'org-e-latex "../../EXPERIMENTAL/org-e-latex.el")
  78. (require 'org-e-publish "../../EXPERIMENTAL/org-e-publish.el")
  79. ;;; Internal Variables
  80. ;; Among internal variables, the most important is
  81. ;; `org-export-option-alist'. This variable define the global export
  82. ;; options, shared between every exporter, and how they are acquired.
  83. (defconst org-export-max-depth 19
  84. "Maximum nesting depth for headlines, counting from 0.")
  85. (defconst org-export-option-alist
  86. '((:author "AUTHOR" nil user-full-name t)
  87. (:creator "CREATOR" nil org-export-creator-string)
  88. (:date "DATE" nil nil t)
  89. (:description "DESCRIPTION" nil nil newline)
  90. (:email "EMAIL" nil user-mail-address t)
  91. (:exclude-tags "EXPORT_EXCLUDE_TAGS" nil org-export-exclude-tags split)
  92. (:headline-levels nil "H" org-export-headline-levels)
  93. (:keywords "KEYWORDS" nil nil space)
  94. (:language "LANGUAGE" nil org-export-default-language t)
  95. (:preserve-breaks nil "\\n" org-export-preserve-breaks)
  96. (:section-numbers nil "num" org-export-with-section-numbers)
  97. (:select-tags "EXPORT_SELECT_TAGS" nil org-export-select-tags split)
  98. (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
  99. (:title "TITLE" nil nil space)
  100. (:with-archived-trees nil "arch" org-export-with-archived-trees)
  101. (:with-author nil "author" org-export-with-author)
  102. (:with-creator nil "creator" org-export-with-creator)
  103. (:with-drawers nil "d" org-export-with-drawers)
  104. (:with-email nil "email" org-export-with-email)
  105. (:with-emphasize nil "*" org-export-with-emphasize)
  106. (:with-entities nil "e" org-export-with-entities)
  107. (:with-fixed-width nil ":" org-export-with-fixed-width)
  108. (:with-footnotes nil "f" org-export-with-footnotes)
  109. (:with-priority nil "pri" org-export-with-priority)
  110. (:with-special-strings nil "-" org-export-with-special-strings)
  111. (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
  112. (:with-toc nil "toc" org-export-with-toc)
  113. (:with-tables nil "|" org-export-with-tables)
  114. (:with-tags nil "tags" org-export-with-tags)
  115. (:with-tasks nil "tasks" org-export-with-tasks)
  116. (:with-timestamps nil "<" org-export-with-timestamps)
  117. (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
  118. "Alist between export properties and ways to set them.
  119. The car of the alist is the property name, and the cdr is a list
  120. like \(KEYWORD OPTION DEFAULT BEHAVIOUR\) where:
  121. KEYWORD is a string representing a buffer keyword, or nil.
  122. OPTION is a string that could be found in an #+OPTIONS: line.
  123. DEFAULT is the default value for the property.
  124. BEHAVIOUR determine how Org should handle multiple keywords for
  125. the same property. It is a symbol among:
  126. nil Keep old value and discard the new one.
  127. t Replace old value with the new one.
  128. `space' Concatenate the values, separating them with a space.
  129. `newline' Concatenate the values, separating them with
  130. a newline.
  131. `split' Split values at white spaces, and cons them to the
  132. previous list.
  133. KEYWORD and OPTION have precedence over DEFAULT.
  134. All these properties should be back-end agnostic. For back-end
  135. specific properties, define a similar variable named
  136. `org-BACKEND-option-alist', replacing BACKEND with the name of
  137. the appropriate back-end. You can also redefine properties
  138. there, as they have precedence over these.")
  139. (defconst org-export-special-keywords
  140. '("SETUP_FILE" "OPTIONS" "MACRO")
  141. "List of in-buffer keywords that require special treatment.
  142. These keywords are not directly associated to a property. The
  143. way they are handled must be hard-coded into
  144. `org-export-get-inbuffer-options' function.")
  145. (defconst org-export-filters-alist
  146. '((:filter-babel-call . org-export-filter-babel-call-functions)
  147. (:filter-center-block . org-export-filter-center-block-functions)
  148. (:filter-comment . org-export-filter-comment-functions)
  149. (:filter-comment-block . org-export-filter-comment-block-functions)
  150. (:filter-drawer . org-export-filter-drawer-functions)
  151. (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
  152. (:filter-emphasis . org-export-filter-emphasis-functions)
  153. (:filter-entity . org-export-filter-entity-functions)
  154. (:filter-example-block . org-export-filter-example-block-functions)
  155. (:filter-export-block . org-export-filter-export-block-functions)
  156. (:filter-export-snippet . org-export-filter-export-snippet-functions)
  157. (:filter-final-output . org-export-filter-final-output-functions)
  158. (:filter-fixed-width . org-export-filter-fixed-width-functions)
  159. (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
  160. (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
  161. (:filter-headline . org-export-filter-headline-functions)
  162. (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
  163. (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
  164. (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
  165. (:filter-inlinetask . org-export-filter-inlinetask-functions)
  166. (:filter-item . org-export-filter-item-functions)
  167. (:filter-keyword . org-export-filter-keyword-functions)
  168. (:filter-latex-environment . org-export-filter-latex-environment-functions)
  169. (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
  170. (:filter-line-break . org-export-filter-line-break-functions)
  171. (:filter-link . org-export-filter-link-functions)
  172. (:filter-macro . org-export-filter-macro-functions)
  173. (:filter-paragraph . org-export-filter-paragraph-functions)
  174. (:filter-parse-tree . org-export-filter-parse-tree-functions)
  175. (:filter-plain-list . org-export-filter-plain-list-functions)
  176. (:filter-plain-text . org-export-filter-plain-text-functions)
  177. (:filter-property-drawer . org-export-filter-property-drawer-functions)
  178. (:filter-quote-block . org-export-filter-quote-block-functions)
  179. (:filter-quote-section . org-export-filter-quote-section-functions)
  180. (:filter-radio-target . org-export-filter-radio-target-functions)
  181. (:filter-section . org-export-filter-section-functions)
  182. (:filter-special-block . org-export-filter-special-block-functions)
  183. (:filter-src-block . org-export-filter-src-block-functions)
  184. (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
  185. (:filter-subscript . org-export-filter-subscript-functions)
  186. (:filter-superscript . org-export-filter-superscript-functions)
  187. (:filter-table . org-export-filter-table-functions)
  188. (:filter-target . org-export-filter-target-functions)
  189. (:filter-time-stamp . org-export-filter-time-stamp-functions)
  190. (:filter-verbatim . org-export-filter-verbatim-functions)
  191. (:filter-verse-block . org-export-filter-verse-block-functions))
  192. "Alist between filters properties and initial values.
  193. The key of each association is a property name accessible through
  194. the communication channel its value is a configurable global
  195. variable defining initial filters.
  196. This list is meant to install user specified filters. Back-end
  197. developers may install their own filters using
  198. `org-BACKEND-filters-alist', where BACKEND is the name of the
  199. considered back-end. Filters defined there will always be
  200. prepended to the current list, so they always get applied
  201. first.")
  202. (defconst org-export-default-inline-image-rule
  203. `(("file" .
  204. ,(format "\\.%s\\'"
  205. (regexp-opt
  206. '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
  207. "xpm" "pbm" "pgm" "ppm") t))))
  208. "Default rule for link matching an inline image.
  209. This rule applies to links with no description. By default, it
  210. will be considered as an inline image if it targets a local file
  211. whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
  212. \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
  213. See `org-export-inline-image-p' for more information about
  214. rules.")
  215. ;;; User-configurable Variables
  216. ;; Configuration for the masses.
  217. ;; They should never be evaled directly, as their value is to be
  218. ;; stored in a property list (cf. `org-export-option-alist').
  219. (defgroup org-export nil
  220. "Options for exporting Org mode files."
  221. :tag "Org Export"
  222. :group 'org)
  223. (defgroup org-export-general nil
  224. "General options for export engine."
  225. :tag "Org Export General"
  226. :group 'org-export)
  227. (defcustom org-export-with-archived-trees 'headline
  228. "Whether sub-trees with the ARCHIVE tag should be exported.
  229. This can have three different values:
  230. nil Do not export, pretend this tree is not present.
  231. t Do export the entire tree.
  232. `headline' Only export the headline, but skip the tree below it.
  233. This option can also be set with the #+OPTIONS line,
  234. e.g. \"arch:nil\"."
  235. :group 'org-export-general
  236. :type '(choice
  237. (const :tag "Not at all" nil)
  238. (const :tag "Headline only" 'headline)
  239. (const :tag "Entirely" t)))
  240. (defcustom org-export-with-author t
  241. "Non-nil means insert author name into the exported file.
  242. This option can also be set with the #+OPTIONS line,
  243. e.g. \"author:nil\"."
  244. :group 'org-export-general
  245. :type 'boolean)
  246. (defcustom org-export-with-creator 'comment
  247. "Non-nil means the postamble should contain a creator sentence.
  248. The sentence can be set in `org-export-creator-string' and
  249. defaults to \"Generated by Org mode XX in Emacs XXX.\".
  250. If the value is `comment' insert it as a comment."
  251. :group 'org-export-general
  252. :type '(choice
  253. (const :tag "No creator sentence" nil)
  254. (const :tag "Sentence as a comment" 'comment)
  255. (const :tag "Insert the sentence" t)))
  256. (defcustom org-export-creator-string
  257. (format "Generated by Org mode %s in Emacs %s." org-version emacs-version)
  258. "String to insert at the end of the generated document."
  259. :group 'org-export-general
  260. :type '(string :tag "Creator string"))
  261. (defcustom org-export-with-drawers t
  262. "Non-nil means export contents of standard drawers.
  263. When t, all drawers are exported. This may also be a list of
  264. drawer names to export. This variable doesn't apply to
  265. properties drawers.
  266. This option can also be set with the #+OPTIONS line,
  267. e.g. \"d:nil\"."
  268. :group 'org-export-general
  269. :type '(choice
  270. (const :tag "All drawers" t)
  271. (const :tag "None" nil)
  272. (repeat :tag "Selected drawers"
  273. (string :tag "Drawer name"))))
  274. (defcustom org-export-with-email nil
  275. "Non-nil means insert author email into the exported file.
  276. This option can also be set with the #+OPTIONS line,
  277. e.g. \"email:t\"."
  278. :group 'org-export-general
  279. :type 'boolean)
  280. (defcustom org-export-with-emphasize t
  281. "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
  282. If the export target supports emphasizing text, the word will be
  283. typeset in bold, italic, or underlined, respectively. Not all
  284. export backends support this.
  285. This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
  286. :group 'org-export-general
  287. :type 'boolean)
  288. (defcustom org-export-exclude-tags '("noexport")
  289. "Tags that exclude a tree from export.
  290. All trees carrying any of these tags will be excluded from
  291. export. This is without condition, so even subtrees inside that
  292. carry one of the `org-export-select-tags' will be removed."
  293. :group 'org-export-general
  294. :type '(repeat (string :tag "Tag")))
  295. (defcustom org-export-with-fixed-width t
  296. "Non-nil means lines starting with \":\" will be in fixed width font.
  297. This can be used to have pre-formatted text, fragments of code
  298. etc. For example:
  299. : ;; Some Lisp examples
  300. : (while (defc cnt)
  301. : (ding))
  302. will be looking just like this in also HTML. See also the QUOTE
  303. keyword. Not all export backends support this.
  304. This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
  305. :group 'org-export-translation
  306. :type 'boolean)
  307. (defcustom org-export-with-footnotes t
  308. "Non-nil means Org footnotes should be exported.
  309. This option can also be set with the #+OPTIONS line,
  310. e.g. \"f:nil\"."
  311. :group 'org-export-general
  312. :type 'boolean)
  313. (defcustom org-export-headline-levels 3
  314. "The last level which is still exported as a headline.
  315. Inferior levels will produce itemize lists when exported. Note
  316. that a numeric prefix argument to an exporter function overrides
  317. this setting.
  318. This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
  319. :group 'org-export-general
  320. :type 'integer)
  321. (defcustom org-export-default-language "en"
  322. "The default language for export and clocktable translations, as a string.
  323. This may have an association in
  324. `org-clock-clocktable-language-setup'."
  325. :group 'org-export-general
  326. :type '(string :tag "Language"))
  327. (defcustom org-export-preserve-breaks nil
  328. "Non-nil means preserve all line breaks when exporting.
  329. Normally, in HTML output paragraphs will be reformatted.
  330. This option can also be set with the #+OPTIONS line,
  331. e.g. \"\\n:t\"."
  332. :group 'org-export-general
  333. :type 'boolean)
  334. (defcustom org-export-with-entities t
  335. "Non-nil means interpret entities when exporting.
  336. For example, HTML export converts \\alpha to &alpha; and \\AA to
  337. &Aring;.
  338. For a list of supported names, see the constant `org-entities'
  339. and the user option `org-entities-user'.
  340. This option can also be set with the #+OPTIONS line,
  341. e.g. \"e:nil\"."
  342. :group 'org-export-general
  343. :type 'boolean)
  344. (defcustom org-export-with-priority nil
  345. "Non-nil means include priority cookies in export.
  346. When nil, remove priority cookies for export."
  347. :group 'org-export-general
  348. :type 'boolean)
  349. (defcustom org-export-with-section-numbers t
  350. "Non-nil means add section numbers to headlines when exporting.
  351. When set to an integer n, numbering will only happen for
  352. headlines whose relative level is higher or equal to n.
  353. This option can also be set with the #+OPTIONS line,
  354. e.g. \"num:t\"."
  355. :group 'org-export-general
  356. :type 'boolean)
  357. (defcustom org-export-select-tags '("export")
  358. "Tags that select a tree for export.
  359. If any such tag is found in a buffer, all trees that do not carry
  360. one of these tags will be deleted before export. Inside trees
  361. that are selected like this, you can still deselect a subtree by
  362. tagging it with one of the `org-export-exclude-tags'."
  363. :group 'org-export-general
  364. :type '(repeat (string :tag "Tag")))
  365. (defcustom org-export-with-special-strings t
  366. "Non-nil means interpret \"\-\", \"--\" and \"---\" for export.
  367. When this option is turned on, these strings will be exported as:
  368. Org HTML LaTeX
  369. -----+----------+--------
  370. \\- &shy; \\-
  371. -- &ndash; --
  372. --- &mdash; ---
  373. ... &hellip; \ldots
  374. This option can also be set with the #+OPTIONS line,
  375. e.g. \"-:nil\"."
  376. :group 'org-export-general
  377. :type 'boolean)
  378. (defcustom org-export-with-sub-superscripts t
  379. "Non-nil means interpret \"_\" and \"^\" for export.
  380. When this option is turned on, you can use TeX-like syntax for
  381. sub- and superscripts. Several characters after \"_\" or \"^\"
  382. will be considered as a single item - so grouping with {} is
  383. normally not needed. For example, the following things will be
  384. parsed as single sub- or superscripts.
  385. 10^24 or 10^tau several digits will be considered 1 item.
  386. 10^-12 or 10^-tau a leading sign with digits or a word
  387. x^2-y^3 will be read as x^2 - y^3, because items are
  388. terminated by almost any nonword/nondigit char.
  389. x_{i^2} or x^(2-i) braces or parenthesis do grouping.
  390. Still, ambiguity is possible - so when in doubt use {} to enclose
  391. the sub/superscript. If you set this variable to the symbol
  392. `{}', the braces are *required* in order to trigger
  393. interpretations as sub/superscript. This can be helpful in
  394. documents that need \"_\" frequently in plain text.
  395. This option can also be set with the #+OPTIONS line,
  396. e.g. \"^:nil\"."
  397. :group 'org-export-general
  398. :type '(choice
  399. (const :tag "Interpret them" t)
  400. (const :tag "Curly brackets only" {})
  401. (const :tag "Do not interpret them" nil)))
  402. (defcustom org-export-with-toc t
  403. "Non-nil means create a table of contents in exported files.
  404. The TOC contains headlines with levels up
  405. to`org-export-headline-levels'. When an integer, include levels
  406. up to N in the toc, this may then be different from
  407. `org-export-headline-levels', but it will not be allowed to be
  408. larger than the number of headline levels. When nil, no table of
  409. contents is made.
  410. This option can also be set with the #+OPTIONS line,
  411. e.g. \"toc:nil\" or \"toc:3\"."
  412. :group 'org-export-general
  413. :type '(choice
  414. (const :tag "No Table of Contents" nil)
  415. (const :tag "Full Table of Contents" t)
  416. (integer :tag "TOC to level")))
  417. (defcustom org-export-with-tables t
  418. "If non-nil, lines starting with \"|\" define a table.
  419. For example:
  420. | Name | Address | Birthday |
  421. |-------------+----------+-----------|
  422. | Arthur Dent | England | 29.2.2100 |
  423. This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
  424. :group 'org-export-general
  425. :type 'boolean)
  426. (defcustom org-export-with-tags t
  427. "If nil, do not export tags, just remove them from headlines.
  428. If this is the symbol `not-in-toc', tags will be removed from
  429. table of contents entries, but still be shown in the headlines of
  430. the document.
  431. This option can also be set with the #+OPTIONS line,
  432. e.g. \"tags:nil\"."
  433. :group 'org-export-general
  434. :type '(choice
  435. (const :tag "Off" nil)
  436. (const :tag "Not in TOC" not-in-toc)
  437. (const :tag "On" t)))
  438. (defcustom org-export-with-tasks t
  439. "Non-nil means include TODO items for export.
  440. This may have the following values:
  441. t include tasks independent of state.
  442. todo include only tasks that are not yet done.
  443. done include only tasks that are already done.
  444. nil remove all tasks before export
  445. list of keywords keep only tasks with these keywords"
  446. :group 'org-export-general
  447. :type '(choice
  448. (const :tag "All tasks" t)
  449. (const :tag "No tasks" nil)
  450. (const :tag "Not-done tasks" todo)
  451. (const :tag "Only done tasks" done)
  452. (repeat :tag "Specific TODO keywords"
  453. (string :tag "Keyword"))))
  454. (defcustom org-export-time-stamp-file t
  455. "Non-nil means insert a time stamp into the exported file.
  456. The time stamp shows when the file was created.
  457. This option can also be set with the #+OPTIONS line,
  458. e.g. \"timestamp:nil\"."
  459. :group 'org-export-general
  460. :type 'boolean)
  461. (defcustom org-export-with-timestamps t
  462. "If nil, do not export time stamps and associated keywords."
  463. :group 'org-export-general
  464. :type 'boolean)
  465. (defcustom org-export-with-todo-keywords t
  466. "Non-nil means include TODO keywords in export.
  467. When nil, remove all these keywords from the export.")
  468. (defcustom org-export-allow-BIND 'confirm
  469. "Non-nil means allow #+BIND to define local variable values for export.
  470. This is a potential security risk, which is why the user must
  471. confirm the use of these lines."
  472. :group 'org-export-general
  473. :type '(choice
  474. (const :tag "Never" nil)
  475. (const :tag "Always" t)
  476. (const :tag "Ask a confirmation for each file" confirm)))
  477. (defcustom org-export-snippet-translation-alist nil
  478. "Alist between export snippets back-ends and exporter back-ends.
  479. This variable allows to provide shortcuts for export snippets.
  480. For example, with a value of '\(\(\"h\" . \"html\"\)\), the HTML
  481. back-end will recognize the contents of \"@h{<b>}\" as HTML code
  482. while every other back-end will ignore it."
  483. :group 'org-export-general
  484. :type '(repeat
  485. (cons
  486. (string :tag "Shortcut")
  487. (string :tag "Back-end"))))
  488. (defcustom org-export-coding-system nil
  489. "Coding system for the exported file."
  490. :group 'org-export-general
  491. :type 'coding-system)
  492. (defcustom org-export-copy-to-kill-ring t
  493. "Non-nil means exported stuff will also be pushed onto the kill ring."
  494. :group 'org-export-general
  495. :type 'boolean)
  496. (defcustom org-export-initial-scope 'buffer
  497. "The initial scope when exporting with `org-export-dispatch'.
  498. This variable can be either set to `buffer' or `subtree'."
  499. :group 'org-export-general
  500. :type '(choice
  501. (const :tag "Export current buffer" 'buffer)
  502. (const :tag "Export current subtree" 'subtree)))
  503. (defcustom org-export-show-temporary-export-buffer t
  504. "Non-nil means show buffer after exporting to temp buffer.
  505. When Org exports to a file, the buffer visiting that file is ever
  506. shown, but remains buried. However, when exporting to a temporary
  507. buffer, that buffer is popped up in a second window. When this variable
  508. is nil, the buffer remains buried also in these cases."
  509. :group 'org-export-general
  510. :type 'boolean)
  511. (defcustom org-export-dispatch-use-expert-ui nil
  512. "Non-nil means using a non-intrusive `org-export-dispatch'.
  513. In that case, no help buffer is displayed. Though, an indicator
  514. for current export scope is added to the prompt \(i.e. \"b\" when
  515. output is restricted to body only, \"s\" when it is restricted to
  516. the current subtree and \"v\" when only visible elements are
  517. considered for export\). Also, \[?] allows to switch back to
  518. standard mode."
  519. :group 'org-export-general
  520. :type 'boolean)
  521. ;;; The Communication Channel
  522. ;; During export process, every function has access to a number of
  523. ;; properties. They are of three types:
  524. ;; 1. Environment options are collected once at the very beginning of
  525. ;; the process, out of the original buffer and configuration.
  526. ;; Associated to the parse tree, they make an Org closure.
  527. ;; Collecting them is handled by `org-export-get-environment'
  528. ;; function.
  529. ;;
  530. ;; Most environment options are defined through the
  531. ;; `org-export-option-alist' variable.
  532. ;;
  533. ;; 2. Tree properties are extracted directly from the parsed tree,
  534. ;; just before export, by `org-export-collect-tree-properties'.
  535. ;;
  536. ;; 3. Local options are updated during parsing, and their value
  537. ;; depends on the level of recursion. For now, only `:genealogy'
  538. ;; belongs to that category.
  539. ;; Here is the full list of properties available during transcode
  540. ;; process, with their category (option, tree or local) and their
  541. ;; value type.
  542. ;; + `:author' :: Author's name.
  543. ;; - category :: option
  544. ;; - type :: string
  545. ;; + `:back-end' :: Current back-end used for transcoding.
  546. ;; - category :: tree
  547. ;; - type :: symbol
  548. ;; + `:creator' :: String to write as creation information.
  549. ;; - category :: option
  550. ;; - type :: string
  551. ;; + `:date' :: String to use as date.
  552. ;; - category :: option
  553. ;; - type :: string
  554. ;; + `:description' :: Description text for the current data.
  555. ;; - category :: option
  556. ;; - type :: string
  557. ;; + `:email' :: Author's email.
  558. ;; - category :: option
  559. ;; - type :: string
  560. ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
  561. ;; process.
  562. ;; - category :: option
  563. ;; - type :: list of strings
  564. ;; + `:footnote-definition-alist' :: Alist between footnote labels and
  565. ;; their definition, as parsed data. Only non-inlined footnotes
  566. ;; are represented in this alist. Also, every definition isn't
  567. ;; guaranteed to be referenced in the parse tree. The purpose of
  568. ;; this property is to preserve definitions from oblivion
  569. ;; (i.e. when the parse tree comes from a part of the original
  570. ;; buffer), it isn't meant for direct use in a back-end. To
  571. ;; retrieve a definition relative to a reference, use
  572. ;; `org-export-get-footnote-definition' instead.
  573. ;; - category :: option
  574. ;; - type :: alist (STRING . LIST)
  575. ;; + `:genealogy' :: Flat list of current object or element's parents
  576. ;; from closest to farthest.
  577. ;; - category :: local
  578. ;; - type :: list of elements and objects
  579. ;; + `:headline-levels' :: Maximum level being exported as an
  580. ;; headline. Comparison is done with the relative level of
  581. ;; headlines in the parse tree, not necessarily with their
  582. ;; actual level.
  583. ;; - category :: option
  584. ;; - type :: integer
  585. ;; + `:headline-offset' :: Difference between relative and real level
  586. ;; of headlines in the parse tree. For example, a value of -1
  587. ;; means a level 2 headline should be considered as level
  588. ;; 1 (cf. `org-export-get-relative-level').
  589. ;; - category :: tree
  590. ;; - type :: integer
  591. ;; + `:headline-numbering' :: Alist between headlines' beginning
  592. ;; position and their numbering, as a list of numbers
  593. ;; (cf. `org-export-get-headline-number').
  594. ;; - category :: tree
  595. ;; - type :: alist (INTEGER . LIST)
  596. ;; + `:input-file' :: Full path to input file, if any.
  597. ;; - category :: option
  598. ;; - type :: string or nil
  599. ;; + `:keywords' :: List of keywords attached to data.
  600. ;; - category :: option
  601. ;; - type :: string
  602. ;; + `:language' :: Default language used for translations.
  603. ;; - category :: option
  604. ;; - type :: string
  605. ;; + `:macro-input-file' :: Macro returning file name of input file,
  606. ;; or nil.
  607. ;; - category :: option
  608. ;; - type :: string or nil
  609. ;; + `:parse-tree' :: Whole parse tree, available at any time during
  610. ;; transcoding.
  611. ;; - category :: global
  612. ;; - type :: list (as returned by `org-element-parse-buffer')
  613. ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
  614. ;; all line breaks.
  615. ;; - category :: option
  616. ;; - type :: symbol (nil, t)
  617. ;; + `:section-numbers' :: Non-nil means transcoding should add
  618. ;; section numbers to headlines.
  619. ;; - category :: option
  620. ;; - type :: symbol (nil, t)
  621. ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
  622. ;; in transcoding. When such a tag is present,
  623. ;; subtrees without it are de facto excluded from
  624. ;; the process. See `use-select-tags'.
  625. ;; - category :: option
  626. ;; - type :: list of strings
  627. ;; + `:target-list' :: List of targets encountered in the parse tree.
  628. ;; This is used to partly resolve "fuzzy" links
  629. ;; (cf. `org-export-resolve-fuzzy-link').
  630. ;; - category :: tree
  631. ;; - type :: list of strings
  632. ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
  633. ;; a time stamp in the output.
  634. ;; - category :: option
  635. ;; - type :: symbol (nil, t)
  636. ;; + `:use-select-tags' :: When non-nil, a select tags has been found
  637. ;; in the parse tree. Thus, any headline without one will be
  638. ;; filtered out. See `select-tags'.
  639. ;; - category :: tree
  640. ;; - type :: interger or nil
  641. ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
  642. ;; also be transcoded. If it is set to the `headline' symbol,
  643. ;; only the archived headline's name is retained.
  644. ;; - category :: option
  645. ;; - type :: symbol (nil, t, `headline')
  646. ;; + `:with-author' :: Non-nil means author's name should be included
  647. ;; in the output.
  648. ;; - category :: option
  649. ;; - type :: symbol (nil, t)
  650. ;; + `:with-creator' :: Non-nild means a creation sentence should be
  651. ;; inserted at the end of the transcoded string. If the value
  652. ;; is `comment', it should be commented.
  653. ;; - category :: option
  654. ;; - type :: symbol (`comment', nil, t)
  655. ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
  656. ;; its value is a list of names, only drawers with such names
  657. ;; will be transcoded.
  658. ;; - category :: option
  659. ;; - type :: symbol (nil, t) or list of strings
  660. ;; + `:with-email' :: Non-nil means output should contain author's
  661. ;; email.
  662. ;; - category :: option
  663. ;; - type :: symbol (nil, t)
  664. ;; + `:with-emphasize' :: Non-nil means emphasized text should be
  665. ;; interpreted.
  666. ;; - category :: option
  667. ;; - type :: symbol (nil, t)
  668. ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
  669. ;; strings starting with a colon as a fixed-with (verbatim) area.
  670. ;; - category :: option
  671. ;; - type :: symbol (nil, t)
  672. ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
  673. ;; footnotes.
  674. ;; - category :: option
  675. ;; - type :: symbol (nil, t)
  676. ;; + `:with-priority' :: Non-nil means transcoding should include
  677. ;; priority cookies.
  678. ;; - category :: option
  679. ;; - type :: symbol (nil, t)
  680. ;; + `:with-special-strings' :: Non-nil means transcoding should
  681. ;; interpret special strings in plain text.
  682. ;; - category :: option
  683. ;; - type :: symbol (nil, t)
  684. ;; + `:with-sub-superscript' :: Non-nil means transcoding should
  685. ;; interpret subscript and superscript. With a value of "{}",
  686. ;; only interpret those using curly brackets.
  687. ;; - category :: option
  688. ;; - type :: symbol (nil, {}, t)
  689. ;; + `:with-tables' :: Non-nil means transcoding should interpret
  690. ;; tables.
  691. ;; - category :: option
  692. ;; - type :: symbol (nil, t)
  693. ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
  694. ;; headlines. A `not-in-toc' value will remove them
  695. ;; from the table of contents, if any, nonetheless.
  696. ;; - category :: option
  697. ;; - type :: symbol (nil, t, `not-in-toc')
  698. ;; + `:with-tasks' :: Non-nil means transcoding should include
  699. ;; headlines with a TODO keyword. A `todo' value
  700. ;; will only include headlines with a todo type
  701. ;; keyword while a `done' value will do the
  702. ;; contrary. If a list of strings is provided, only
  703. ;; tasks with keywords belonging to that list will
  704. ;; be kept.
  705. ;; - category :: option
  706. ;; - type :: symbol (t, todo, done, nil) or list of strings
  707. ;; + `:with-timestamps' :: Non-nil means transcoding should include
  708. ;; time stamps and associated keywords. Otherwise, completely
  709. ;; remove them.
  710. ;; - category :: option
  711. ;; - type :: symbol: (t, nil)
  712. ;; + `:with-toc' :: Non-nil means that a table of contents has to be
  713. ;; added to the output. An integer value limits its
  714. ;; depth.
  715. ;; - category :: option
  716. ;; - type :: symbol (nil, t or integer)
  717. ;; + `:with-todo-keywords' :: Non-nil means transcoding should
  718. ;; include TODO keywords.
  719. ;; - category :: option
  720. ;; - type :: symbol (nil, t)
  721. ;;;; Environment Options
  722. ;; Environment options encompass all parameters defined outside the
  723. ;; scope of the parsed data. They come from five sources, in
  724. ;; increasing precedence order:
  725. ;;
  726. ;; - Global variables,
  727. ;; - Options keyword symbols,
  728. ;; - Buffer keywords,
  729. ;; - Subtree properties.
  730. ;; The central internal function with regards to environment options
  731. ;; is `org-export-get-environment'. It updates global variables with
  732. ;; "#+BIND:" keywords, then retrieve and prioritize properties from
  733. ;; the different sources.
  734. ;; The internal functions doing the retrieval are:
  735. ;; `org-export-parse-option-keyword' ,
  736. ;; `org-export-get-subtree-options' ,
  737. ;; `org-export-get-inbuffer-options' and
  738. ;; `org-export-get-global-options'.
  739. ;;
  740. ;; Some properties, which do not rely on the previous sources but
  741. ;; still depend on the original buffer, are taken care of with
  742. ;; `org-export-initial-options'.
  743. ;; Also, `org-export-confirm-letbind' and `org-export-install-letbind'
  744. ;; take care of the part relative to "#+BIND:" keywords.
  745. (defun org-export-get-environment (&optional backend subtreep ext-plist)
  746. "Collect export options from the current buffer.
  747. Optional argument BACKEND is a symbol specifying which back-end
  748. specific options to read, if any.
  749. When optional argument SUBTREEP is non-nil, assume the export is
  750. done against the current sub-tree.
  751. Third optional argument EXT-PLIST is a property list with
  752. external parameters overriding Org default settings, but still
  753. inferior to file-local settings."
  754. ;; First install #+BIND variables.
  755. (org-export-install-letbind-maybe)
  756. ;; Get and prioritize export options...
  757. (let ((options (org-combine-plists
  758. ;; ... from global variables...
  759. (org-export-get-global-options backend)
  760. ;; ... from buffer's name (default title)...
  761. `(:title
  762. ,(or (let ((file (buffer-file-name (buffer-base-buffer))))
  763. (and file
  764. (file-name-sans-extension
  765. (file-name-nondirectory file))))
  766. (buffer-name (buffer-base-buffer))))
  767. ;; ... from an external property list...
  768. ext-plist
  769. ;; ... from in-buffer settings...
  770. (org-export-get-inbuffer-options
  771. backend
  772. (and buffer-file-name
  773. (org-remove-double-quotes buffer-file-name)))
  774. ;; ... and from subtree, when appropriate.
  775. (and subtreep (org-export-get-subtree-options)))))
  776. ;; Add initial options.
  777. (setq options (append (org-export-initial-options) options))
  778. ;; Return plist.
  779. options))
  780. (defun org-export-parse-option-keyword (options &optional backend)
  781. "Parse an OPTIONS line and return values as a plist.
  782. Optional argument BACKEND is a symbol specifying which back-end
  783. specific items to read, if any."
  784. (let* ((all
  785. (append org-export-option-alist
  786. (and backend
  787. (let ((var (intern
  788. (format "org-%s-option-alist" backend))))
  789. (and (boundp var) (eval var))))))
  790. ;; Build an alist between #+OPTION: item and property-name.
  791. (alist (delq nil
  792. (mapcar (lambda (e)
  793. (when (nth 2 e) (cons (regexp-quote (nth 2 e))
  794. (car e))))
  795. all)))
  796. plist)
  797. (mapc (lambda (e)
  798. (when (string-match (concat "\\(\\`\\|[ \t]\\)"
  799. (car e)
  800. ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
  801. options)
  802. (setq plist (plist-put plist
  803. (cdr e)
  804. (car (read-from-string
  805. (match-string 2 options)))))))
  806. alist)
  807. plist))
  808. (defun org-export-get-subtree-options ()
  809. "Get export options in subtree at point.
  810. Assume point is at subtree's beginning.
  811. Return options as a plist."
  812. (let (prop plist)
  813. (when (setq prop (progn (looking-at org-todo-line-regexp)
  814. (or (save-match-data
  815. (org-entry-get (point) "EXPORT_TITLE"))
  816. (org-match-string-no-properties 3))))
  817. (setq plist
  818. (plist-put
  819. plist :title
  820. (org-element-parse-secondary-string
  821. prop
  822. (cdr (assq 'keyword org-element-string-restrictions))))))
  823. (when (setq prop (org-entry-get (point) "EXPORT_TEXT"))
  824. (setq plist (plist-put plist :text prop)))
  825. (when (setq prop (org-entry-get (point) "EXPORT_AUTHOR"))
  826. (setq plist (plist-put plist :author prop)))
  827. (when (setq prop (org-entry-get (point) "EXPORT_DATE"))
  828. (setq plist (plist-put plist :date prop)))
  829. (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
  830. (setq plist (org-export-add-options-to-plist plist prop)))
  831. plist))
  832. (defun org-export-get-inbuffer-options (&optional backend files)
  833. "Return current buffer export options, as a plist.
  834. Optional argument BACKEND, when non-nil, is a symbol specifying
  835. which back-end specific options should also be read in the
  836. process.
  837. Optional argument FILES is a list of setup files names read so
  838. far, used to avoid circular dependencies.
  839. Assume buffer is in Org mode. Narrowing, if any, is ignored."
  840. (org-with-wide-buffer
  841. (goto-char (point-min))
  842. (let ((case-fold-search t) plist)
  843. ;; 1. Special keywords, as in `org-export-special-keywords'.
  844. (let ((special-re (org-make-options-regexp org-export-special-keywords)))
  845. (while (re-search-forward special-re nil t)
  846. (let ((element (org-element-at-point)))
  847. (when (eq (car element) 'keyword)
  848. (let* ((key (upcase (org-element-get-property :key element)))
  849. (val (org-element-get-property :value element))
  850. (prop
  851. (cond
  852. ((string= key "SETUP_FILE")
  853. (let ((file
  854. (expand-file-name
  855. (org-remove-double-quotes (org-trim val)))))
  856. ;; Avoid circular dependencies.
  857. (unless (member file files)
  858. (with-temp-buffer
  859. (insert (org-file-contents file 'noerror))
  860. (org-mode)
  861. (org-export-get-inbuffer-options
  862. backend (cons file files))))))
  863. ((string= key "OPTIONS")
  864. (org-export-parse-option-keyword val backend))
  865. ((string= key "MACRO")
  866. (when (string-match
  867. "^\\([-a-zA-Z0-9_]+\\)\\(?:[ \t]+\\(.*?\\)[ \t]*$\\)?"
  868. val)
  869. (let ((key
  870. (intern
  871. (concat ":macro-"
  872. (downcase (match-string 1 val)))))
  873. (value (org-match-string-no-properties 2 val)))
  874. (cond
  875. ((not value) "")
  876. ;; Value will be evaled. Leave it as-is.
  877. ((string-match "\\`(eval\\>" value)
  878. (list key value))
  879. ;; Value has to be parsed for nested
  880. ;; macros.
  881. (t
  882. (list
  883. key
  884. (let ((restr
  885. (cdr
  886. (assq 'macro
  887. org-element-object-restrictions))))
  888. (org-element-parse-secondary-string
  889. ;; If user explicitly asks for
  890. ;; a newline, be sure to preserve it
  891. ;; from further filling with
  892. ;; `hard-newline'. Also replace
  893. ;; "\\n" with "\n", "\\\n" with "\\n"
  894. ;; and so on...
  895. (replace-regexp-in-string
  896. "\\(\\\\\\\\\\)n" "\\\\"
  897. (replace-regexp-in-string
  898. "\\(?:^\\|[^\\\\]\\)\\(\\\\n\\)"
  899. hard-newline value nil nil 1)
  900. nil nil 1)
  901. restr)))))))))))
  902. (setq plist (org-combine-plists plist prop)))))))
  903. ;; 2. Standard options, as in `org-export-option-alist'.
  904. (let* ((all (append org-export-option-alist
  905. ;; Also look for back-end specific options
  906. ;; if BACKEND is defined.
  907. (and backend
  908. (let ((var
  909. (intern
  910. (format "org-%s-option-alist" backend))))
  911. (and (boundp var) (eval var))))))
  912. ;; Build alist between keyword name and property name.
  913. (alist
  914. (delq nil (mapcar
  915. (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
  916. all)))
  917. ;; Build regexp matching all keywords associated to export
  918. ;; options. Note: the search is case insensitive.
  919. (opt-re (org-make-options-regexp
  920. (delq nil (mapcar (lambda (e) (nth 1 e)) all)))))
  921. (goto-char (point-min))
  922. (while (re-search-forward opt-re nil t)
  923. (let ((element (org-element-at-point)))
  924. (when (eq (car element) 'keyword)
  925. (let* ((key (upcase (org-element-get-property :key element)))
  926. (val (org-element-get-property :value element))
  927. (prop (cdr (assoc key alist)))
  928. (behaviour (nth 4 (assq prop all))))
  929. (setq plist
  930. (plist-put
  931. plist prop
  932. ;; Handle value depending on specified BEHAVIOUR.
  933. (case behaviour
  934. (space
  935. (if (not (plist-get plist prop)) (org-trim val)
  936. (concat (plist-get plist prop) " " (org-trim val))))
  937. (newline
  938. (org-trim
  939. (concat (plist-get plist prop) "\n" (org-trim val))))
  940. (split
  941. `(,@(plist-get plist prop) ,@(org-split-string val)))
  942. ('t val)
  943. (otherwise (plist-get plist prop)))))))))
  944. ;; Parse keywords specified in `org-element-parsed-keywords'.
  945. (mapc
  946. (lambda (key)
  947. (let* ((prop (cdr (assoc key alist)))
  948. (value (and prop (plist-get plist prop))))
  949. (when (stringp value)
  950. (setq plist
  951. (plist-put
  952. plist prop
  953. (org-element-parse-secondary-string
  954. value
  955. (cdr (assq 'keyword org-element-string-restrictions))))))))
  956. org-element-parsed-keywords))
  957. ;; 3. Return final value.
  958. plist)))
  959. (defun org-export-get-global-options (&optional backend)
  960. "Return global export options as a plist.
  961. Optional argument BACKEND, if non-nil, is a symbol specifying
  962. which back-end specific export options should also be read in the
  963. process."
  964. (let ((all (append org-export-option-alist
  965. (and backend
  966. (let ((var (intern
  967. (format "org-%s-option-alist" backend))))
  968. (and (boundp var) (eval var))))))
  969. ;; Output value.
  970. plist)
  971. (mapc (lambda (cell)
  972. (setq plist (plist-put plist (car cell) (eval (nth 3 cell)))))
  973. all)
  974. ;; Return value.
  975. plist))
  976. (defun org-export-initial-options ()
  977. "Return a plist with properties related to input buffer."
  978. (let ((visited-file (buffer-file-name (buffer-base-buffer))))
  979. (list
  980. ;; Store full path of input file name, or nil. For internal use.
  981. :input-file visited-file
  982. ;; `:macro-date', `:macro-time' and `:macro-property' could as well
  983. ;; be initialized as tree properties, since they don't depend on
  984. ;; initial environment. Though, it may be more logical to keep
  985. ;; them close to other ":macro-" properties.
  986. :macro-date "(eval (format-time-string \"$1\"))"
  987. :macro-time "(eval (format-time-string \"$1\"))"
  988. :macro-property "(eval (org-entry-get nil \"$1\" 'selective))"
  989. :macro-modification-time
  990. (and visited-file
  991. (file-exists-p visited-file)
  992. (concat "(eval (format-time-string \"$1\" '"
  993. (prin1-to-string (nth 5 (file-attributes visited-file)))
  994. "))"))
  995. ;; Store input file name as a macro.
  996. :macro-input-file (and visited-file (file-name-nondirectory visited-file))
  997. ;; Footnotes definitions must be collected in the original buffer,
  998. ;; as there's no insurance that they will still be in the parse
  999. ;; tree, due to some narrowing.
  1000. :footnote-definition-alist
  1001. (let (alist)
  1002. (org-with-wide-buffer
  1003. (goto-char (point-min))
  1004. (while (re-search-forward org-footnote-definition-re nil t)
  1005. (let ((def (org-footnote-at-definition-p)))
  1006. (when def
  1007. (org-skip-whitespace)
  1008. (push (cons (car def)
  1009. (save-restriction
  1010. (narrow-to-region (point) (nth 2 def))
  1011. ;; Like `org-element-parse-buffer', but
  1012. ;; makes sure the definition doesn't start
  1013. ;; with a section element.
  1014. (nconc
  1015. (list 'org-data nil)
  1016. (org-element-parse-elements
  1017. (point-min) (point-max) nil nil nil nil nil))))
  1018. alist))))
  1019. alist)))))
  1020. (defvar org-export-allow-BIND-local nil)
  1021. (defun org-export-confirm-letbind ()
  1022. "Can we use #+BIND values during export?
  1023. By default this will ask for confirmation by the user, to divert
  1024. possible security risks."
  1025. (cond
  1026. ((not org-export-allow-BIND) nil)
  1027. ((eq org-export-allow-BIND t) t)
  1028. ((local-variable-p 'org-export-allow-BIND-local) org-export-allow-BIND-local)
  1029. (t (org-set-local 'org-export-allow-BIND-local
  1030. (yes-or-no-p "Allow BIND values in this buffer? ")))))
  1031. (defun org-export-install-letbind-maybe ()
  1032. "Install the values from #+BIND lines as local variables.
  1033. Variables must be installed before in-buffer options are
  1034. retrieved."
  1035. (let (letbind pair)
  1036. (org-with-wide-buffer
  1037. (goto-char (point-min))
  1038. (while (re-search-forward (org-make-options-regexp '("BIND")) nil t)
  1039. (when (org-export-confirm-letbind)
  1040. (push (read (concat "(" (org-match-string-no-properties 2) ")"))
  1041. letbind))))
  1042. (while (setq pair (pop letbind))
  1043. (org-set-local (car pair) (nth 1 pair)))))
  1044. ;;;; Tree Properties
  1045. ;; Tree properties are infromation extracted from parse tree. They
  1046. ;; are initialized at the beginning of the transcoding process by
  1047. ;; `org-export-collect-tree-properties'.
  1048. ;; Dedicated functions focus on computing the value of specific tree
  1049. ;; properties during initialization. Thus,
  1050. ;; `org-export-use-select-tag-p' determines if an headline makes use
  1051. ;; of an export tag enforcing inclusion. `org-export-get-min-level'
  1052. ;; gets the minimal exportable level, used as a basis to compute
  1053. ;; relative level for headlines. `org-export-get-point-max' returns
  1054. ;; the maximum exportable ending position in the parse tree.
  1055. ;; Eventually `org-export-collect-headline-numbering' builds an alist
  1056. ;; between headlines' beginning position and their numbering.
  1057. (defun org-export-collect-tree-properties (data info backend)
  1058. "Extract tree properties from parse tree.
  1059. DATA is the parse tree from which information is retrieved. INFO
  1060. is a list holding export options. BACKEND is the back-end called
  1061. for transcoding, as a symbol.
  1062. Following tree properties are set:
  1063. `:back-end' Back-end used for transcoding.
  1064. `:headline-offset' Offset between true level of headlines and
  1065. local level. An offset of -1 means an headline
  1066. of level 2 should be considered as a level
  1067. 1 headline in the context.
  1068. `:headline-numbering' Alist of all headlines' beginning position
  1069. as key an the associated numbering as value.
  1070. `:parse-tree' Whole parse tree.
  1071. `:target-list' List of all targets in the parse tree.
  1072. `:use-select-tags' Non-nil when parsed tree use a special tag to
  1073. enforce transcoding of the headline."
  1074. ;; First, set `:use-select-tags' property, as it will be required
  1075. ;; for further computations.
  1076. (setq info
  1077. (org-combine-plists
  1078. info `(:use-select-tags ,(org-export-use-select-tags-p data info))))
  1079. ;; Then get `:headline-offset' in order to be able to use
  1080. ;; `org-export-get-relative-level'.
  1081. (setq info
  1082. (org-combine-plists
  1083. info `(:headline-offset ,(- 1 (org-export-get-min-level data info)))))
  1084. ;; Now, get the rest of the tree properties, now `:use-select-tags'
  1085. ;; is set...
  1086. (nconc
  1087. `(:parse-tree
  1088. ,data
  1089. :target-list
  1090. ,(org-element-map data 'target (lambda (target local) target) info)
  1091. :headline-numbering ,(org-export-collect-headline-numbering data info)
  1092. :back-end ,backend)
  1093. info))
  1094. (defun org-export-use-select-tags-p (data options)
  1095. "Non-nil when data use a tag enforcing transcoding.
  1096. DATA is parsed data as returned by `org-element-parse-buffer'.
  1097. OPTIONS is a plist holding export options."
  1098. (org-element-map
  1099. data
  1100. 'headline
  1101. (lambda (headline info)
  1102. (let ((tags (org-element-get-property :with-tags headline)))
  1103. (and tags (string-match
  1104. (format ":%s:" (plist-get info :select-tags)) tags))))
  1105. options
  1106. 'stop-at-first-match))
  1107. (defun org-export-get-min-level (data options)
  1108. "Return minimum exportable headline's level in DATA.
  1109. DATA is parsed tree as returned by `org-element-parse-buffer'.
  1110. OPTIONS is a plist holding export options."
  1111. (catch 'exit
  1112. (let ((min-level 10000))
  1113. (mapc (lambda (blob)
  1114. (when (and (eq (car blob) 'headline)
  1115. (not (org-export-skip-p blob options)))
  1116. (setq min-level
  1117. (min (org-element-get-property :level blob) min-level)))
  1118. (when (= min-level 1) (throw 'exit 1)))
  1119. (org-element-get-contents data))
  1120. ;; If no headline was found, for the sake of consistency, set
  1121. ;; minimum level to 1 nonetheless.
  1122. (if (= min-level 10000) 1 min-level))))
  1123. (defun org-export-collect-headline-numbering (data options)
  1124. "Return numbering of all exportable headlines in a parse tree.
  1125. DATA is the parse tree. OPTIONS is the plist holding export
  1126. options.
  1127. Return an alist whose key is an headline and value is its
  1128. associated numbering \(in the shape of a list of numbers\)."
  1129. (let ((numbering (make-vector org-export-max-depth 0)))
  1130. (org-element-map
  1131. data
  1132. 'headline
  1133. (lambda (headline info)
  1134. (let ((relative-level
  1135. (1- (org-export-get-relative-level headline info))))
  1136. (cons
  1137. headline
  1138. (loop for n across numbering
  1139. for idx from 0 to org-export-max-depth
  1140. when (< idx relative-level) collect n
  1141. when (= idx relative-level) collect (aset numbering idx (1+ n))
  1142. when (> idx relative-level) do (aset numbering idx 0)))))
  1143. options)))
  1144. ;;; The Transcoder
  1145. ;; This function reads Org data (obtained with, i.e.
  1146. ;; `org-element-parse-buffer') and transcodes it into a specified
  1147. ;; back-end output. It takes care of updating local properties,
  1148. ;; filtering out elements or objects according to export options and
  1149. ;; organizing the output blank lines and white space are preserved.
  1150. ;; Though, this function is inapropriate for secondary strings, which
  1151. ;; require a fresh copy of the plist passed as INFO argument. Thus,
  1152. ;; `org-export-secondary-string' is provided for that specific task.
  1153. ;; Internally, three functions handle the filtering of objects and
  1154. ;; elements during the export. More precisely, `org-export-skip-p'
  1155. ;; determines if the considered object or element should be ignored
  1156. ;; altogether, `org-export-interpret-p' tells which elements or
  1157. ;; objects should be seen as real Org syntax and `org-export-expand'
  1158. ;; transforms the others back into their original shape.
  1159. (defun org-export-data (data backend info)
  1160. "Convert DATA to a string into BACKEND format.
  1161. DATA is a nested list as returned by `org-element-parse-buffer'.
  1162. BACKEND is a symbol among supported exporters.
  1163. INFO is a plist holding export options and also used as
  1164. a communication channel between elements when walking the nested
  1165. list. See `org-export-update-info' function for more
  1166. details.
  1167. Return transcoded string."
  1168. (mapconcat
  1169. ;; BLOB can be an element, an object, a string, or nil.
  1170. (lambda (blob)
  1171. (cond
  1172. ((not blob) nil)
  1173. ;; BLOB is a string. Check if the optional transcoder for plain
  1174. ;; text exists, and call it in that case. Otherwise, simply
  1175. ;; return string. Also update INFO and call
  1176. ;; `org-export-filter-plain-text-functions'.
  1177. ((stringp blob)
  1178. (let ((transcoder (intern (format "org-%s-plain-text" backend))))
  1179. (org-export-filter-apply-functions
  1180. (plist-get info :filter-plain-text)
  1181. (if (fboundp transcoder) (funcall transcoder blob info) blob)
  1182. backend info)))
  1183. ;; BLOB is an element or an object.
  1184. (t
  1185. (let* ((type (if (stringp blob) 'plain-text (car blob)))
  1186. ;; 1. Determine the appropriate TRANSCODER.
  1187. (transcoder
  1188. (cond
  1189. ;; 1.0 A full Org document is inserted.
  1190. ((eq type 'org-data) 'identity)
  1191. ;; 1.1. BLOB should be ignored.
  1192. ((org-export-skip-p blob info) nil)
  1193. ;; 1.2. BLOB shouldn't be transcoded. Interpret it
  1194. ;; back into Org syntax.
  1195. ((not (org-export-interpret-p blob info))
  1196. 'org-export-expand)
  1197. ;; 1.3. Else apply naming convention.
  1198. (t (let ((trans (intern
  1199. (format "org-%s-%s" backend type))))
  1200. (and (fboundp trans) trans)))))
  1201. ;; 2. Compute CONTENTS of BLOB.
  1202. (contents
  1203. (cond
  1204. ;; Case 0. No transcoder defined: ignore BLOB.
  1205. ((not transcoder) nil)
  1206. ;; Case 1. Transparently export an Org document.
  1207. ((eq type 'org-data) (org-export-data blob backend info))
  1208. ;; Case 2. For a recursive object.
  1209. ((memq type org-element-recursive-objects)
  1210. (org-export-data
  1211. blob backend
  1212. (org-combine-plists
  1213. info
  1214. `(:genealogy ,(cons blob (plist-get info :genealogy))))))
  1215. ;; Case 3. For a recursive element.
  1216. ((memq type org-element-greater-elements)
  1217. ;; Ignore contents of an archived tree
  1218. ;; when `:with-archived-trees' is `headline'.
  1219. (unless (and
  1220. (eq type 'headline)
  1221. (eq (plist-get info :with-archived-trees) 'headline)
  1222. (org-element-get-property :archivedp blob))
  1223. (org-element-normalize-string
  1224. (org-export-data
  1225. blob backend
  1226. (org-combine-plists
  1227. info `(:genealogy
  1228. ,(cons blob (plist-get info :genealogy))))))))
  1229. ;; Case 4. For a paragraph.
  1230. ((eq type 'paragraph)
  1231. (let ((paragraph
  1232. (org-element-normalize-contents
  1233. blob
  1234. ;; When normalizing contents of an item or
  1235. ;; a footnote definition, ignore first line's
  1236. ;; indentation: there is none and it might be
  1237. ;; misleading.
  1238. (and (not (org-export-get-previous-element blob info))
  1239. (let ((parent (caar (plist-get info :genealogy))))
  1240. (memq parent '(footnote-definition item)))))))
  1241. (org-export-data
  1242. paragraph backend
  1243. (org-combine-plists
  1244. info `(:genealogy
  1245. ,(cons paragraph (plist-get info :genealogy)))))))))
  1246. ;; 3. Transcode BLOB into RESULTS string.
  1247. (results (cond
  1248. ((not transcoder) nil)
  1249. ((eq transcoder 'org-export-expand)
  1250. (org-export-data
  1251. `(org-data nil ,(funcall transcoder blob contents))
  1252. backend info))
  1253. (t (funcall transcoder blob contents info)))))
  1254. ;; 4. Discard nil results. Otherwise, update INFO, append
  1255. ;; the same white space between elements or objects as in
  1256. ;; the original buffer, and call appropriate filters.
  1257. (when results
  1258. ;; No filter for a full document.
  1259. (if (eq type 'org-data) results
  1260. (org-export-filter-apply-functions
  1261. (plist-get info (intern (format ":filter-%s" type)))
  1262. (let ((post-blank (org-element-get-property :post-blank blob)))
  1263. (if (memq type org-element-all-elements)
  1264. (concat (org-element-normalize-string results)
  1265. (make-string post-blank ?\n))
  1266. (concat results (make-string post-blank ? ))))
  1267. backend info)))))))
  1268. (org-element-get-contents data) ""))
  1269. (defun org-export-secondary-string (secondary backend info)
  1270. "Convert SECONDARY string into BACKEND format.
  1271. SECONDARY is a nested list as returned by
  1272. `org-element-parse-secondary-string'.
  1273. BACKEND is a symbol among supported exporters. INFO is a plist
  1274. used as a communication channel.
  1275. Return transcoded string."
  1276. ;; Make SECONDARY acceptable for `org-export-data'.
  1277. (let ((s (if (listp secondary) secondary (list secondary))))
  1278. (org-export-data `(org-data nil ,@s) backend (copy-sequence info))))
  1279. (defun org-export-skip-p (blob info)
  1280. "Non-nil when element or object BLOB should be skipped during export.
  1281. INFO is the plist holding export options."
  1282. ;; Check headline.
  1283. (unless (stringp blob)
  1284. (case (car blob)
  1285. ('headline
  1286. (let ((with-tasks (plist-get info :with-tasks))
  1287. (todo (org-element-get-property :todo-keyword blob))
  1288. (todo-type (org-element-get-property :todo-type blob))
  1289. (archived (plist-get info :with-archived-trees))
  1290. (tag-list (let ((tags (org-element-get-property :tags blob)))
  1291. (and tags (org-split-string tags ":")))))
  1292. (or
  1293. ;; Ignore subtrees with an exclude tag.
  1294. (loop for k in (plist-get info :exclude-tags)
  1295. thereis (member k tag-list))
  1296. ;; Ignore subtrees without a select tag, when such tag is found
  1297. ;; in the buffer.
  1298. (and (plist-get info :use-select-tags)
  1299. (loop for k in (plist-get info :select-tags)
  1300. never (member k tag-list)))
  1301. ;; Ignore commented sub-trees.
  1302. (org-element-get-property :commentedp blob)
  1303. ;; Ignore archived subtrees if `:with-archived-trees' is nil.
  1304. (and (not archived) (org-element-get-property :archivedp blob))
  1305. ;; Ignore tasks, if specified by `:with-tasks' property.
  1306. (and todo (not with-tasks))
  1307. (and todo
  1308. (memq with-tasks '(todo done))
  1309. (not (eq todo-type with-tasks)))
  1310. (and todo
  1311. (consp with-tasks)
  1312. (not (member todo with-tasks))))))
  1313. ;; Check time-stamp.
  1314. ('time-stamp (not (plist-get info :with-timestamps)))
  1315. ;; Check drawer.
  1316. ('drawer
  1317. (or (not (plist-get info :with-drawers))
  1318. (and (consp (plist-get info :with-drawers))
  1319. (not (member (org-element-get-property :drawer-name blob)
  1320. (plist-get info :with-drawers))))))
  1321. ;; Check export snippet.
  1322. ('export-snippet
  1323. (let* ((raw-back-end (org-element-get-property :back-end blob))
  1324. (true-back-end
  1325. (or (cdr (assoc raw-back-end org-export-snippet-translation-alist))
  1326. raw-back-end)))
  1327. (not (string= (symbol-name (plist-get info :back-end))
  1328. true-back-end)))))))
  1329. (defun org-export-interpret-p (blob info)
  1330. "Non-nil if element or object BLOB should be interpreted as Org syntax.
  1331. Check is done according to export options INFO, stored as
  1332. a plist."
  1333. (case (car blob)
  1334. ;; ... entities...
  1335. (entity (plist-get info :with-entities))
  1336. ;; ... emphasis...
  1337. (emphasis (plist-get info :with-emphasize))
  1338. ;; ... fixed-width areas.
  1339. (fixed-width (plist-get info :with-fixed-width))
  1340. ;; ... footnotes...
  1341. ((footnote-definition footnote-reference)
  1342. (plist-get info :with-footnotes))
  1343. ;; ... sub/superscripts...
  1344. ((subscript superscript)
  1345. (let ((sub/super-p (plist-get info :with-sub-superscript)))
  1346. (if (eq sub/super-p '{})
  1347. (org-element-get-property :use-brackets-p blob)
  1348. sub/super-p)))
  1349. ;; ... tables...
  1350. (table (plist-get info :with-tables))
  1351. (otherwise t)))
  1352. (defsubst org-export-expand (blob contents)
  1353. "Expand a parsed element or object to its original state.
  1354. BLOB is either an element or an object. CONTENTS is its
  1355. contents, as a string or nil."
  1356. (funcall
  1357. (intern (format "org-element-%s-interpreter" (car blob))) blob contents))
  1358. ;;; The Filter System
  1359. ;; Filters allow end-users to tweak easily the transcoded output.
  1360. ;; They are the functional counterpart of hooks, as every filter in
  1361. ;; a set is applied to the return value of the previous one.
  1362. ;; Every set is back-end agnostic. Although, a filter is always
  1363. ;; called, in addition to the string it applies to, with the back-end
  1364. ;; used as argument, so it's easy enough for the end-user to add
  1365. ;; back-end specific filters in the set. The communication channel,
  1366. ;; as a plist, is required as the third argument.
  1367. ;; Filters sets are defined below. There are of four types:
  1368. ;; - `org-export-filter-parse-tree-functions' applies directly on the
  1369. ;; complete parsed tree. It's the only filters set that doesn't
  1370. ;; apply to a string.
  1371. ;; - `org-export-filter-final-output-functions' applies to the final
  1372. ;; transcoded string.
  1373. ;; - `org-export-filter-plain-text-functions' applies to any string
  1374. ;; not recognized as Org syntax.
  1375. ;; - `org-export-filter-TYPE-functions' applies on the string returned
  1376. ;; after an element or object of type TYPE has been transcoded.
  1377. ;; All filters sets are applied through
  1378. ;; `org-export-filter-apply-functions' function. Filters in a set are
  1379. ;; applied in a LIFO fashion. It allows developers to be sure that
  1380. ;; their filters will be applied first.
  1381. ;; Filters properties are installed in communication channel just
  1382. ;; before parsing, with `org-export-install-filters' function.
  1383. ;;;; Special Filters
  1384. (defvar org-export-filter-parse-tree-functions nil
  1385. "Filter, or list of filters, applied to the parsed tree.
  1386. Each filter is called with three arguments: the parse tree, as
  1387. returned by `org-element-parse-buffer', the back-end, as
  1388. a symbol, and the communication channel, as a plist. It must
  1389. return the modified parse tree to transcode.")
  1390. (defvar org-export-filter-final-output-functions nil
  1391. "Filter, or list of filters, applied to the transcoded string.
  1392. Each filter is called with three arguments: the full transcoded
  1393. string, the back-end, as a symbol, and the communication channel,
  1394. as a plist. It must return a string that will be used as the
  1395. final export output.")
  1396. (defvar org-export-filter-plain-text-functions nil
  1397. "Filter, or list of filters, applied to plain text.
  1398. Each filter is called with three arguments: a string which
  1399. contains no Org syntax, the back-end, as a symbol, and the
  1400. communication channel, as a plist. It must return a string or
  1401. nil.")
  1402. ;;;; Elements Filters
  1403. (defvar org-export-filter-center-block-functions nil
  1404. "List of functions applied to a transcoded center block.
  1405. Each filter is called with three arguments: the transcoded center
  1406. block, as a string, the back-end, as a symbol, and the
  1407. communication channel, as a plist. It must return a string or
  1408. nil.")
  1409. (defvar org-export-filter-drawer-functions nil
  1410. "List of functions applied to a transcoded drawer.
  1411. Each filter is called with three arguments: the transcoded
  1412. drawer, as a string, the back-end, as a symbol, and the
  1413. communication channel, as a plist. It must return a string or
  1414. nil.")
  1415. (defvar org-export-filter-dynamic-block-functions nil
  1416. "List of functions applied to a transcoded dynamic-block.
  1417. Each filter is called with three arguments: the transcoded
  1418. dynamic-block, as a string, the back-end, as a symbol, and the
  1419. communication channel, as a plist. It must return a string or
  1420. nil.")
  1421. (defvar org-export-filter-headline-functions nil
  1422. "List of functions applied to a transcoded headline.
  1423. Each filter is called with three arguments: the transcoded
  1424. headline, as a string, the back-end, as a symbol, and the
  1425. communication channel, as a plist. It must return a string or
  1426. nil.")
  1427. (defvar org-export-filter-inlinetask-functions nil
  1428. "List of functions applied to a transcoded inlinetask.
  1429. Each filter is called with three arguments: the transcoded
  1430. inlinetask, as a string, the back-end, as a symbol, and the
  1431. communication channel, as a plist. It must return a string or
  1432. nil.")
  1433. (defvar org-export-filter-plain-list-functions nil
  1434. "List of functions applied to a transcoded plain-list.
  1435. Each filter is called with three arguments: the transcoded
  1436. plain-list, as a string, the back-end, as a symbol, and the
  1437. communication channel, as a plist. It must return a string or
  1438. nil.")
  1439. (defvar org-export-filter-item-functions nil
  1440. "List of functions applied to a transcoded item.
  1441. Each filter is called with three arguments: the transcoded item,
  1442. as a string, the back-end, as a symbol, and the communication
  1443. channel, as a plist. It must return a string or nil.")
  1444. (defvar org-export-filter-comment-functions nil
  1445. "List of functions applied to a transcoded comment.
  1446. Each filter is called with three arguments: the transcoded
  1447. comment, as a string, the back-end, as a symbol, and the
  1448. communication channel, as a plist. It must return a string or
  1449. nil.")
  1450. (defvar org-export-filter-comment-block-functions nil
  1451. "List of functions applied to a transcoded comment-comment.
  1452. Each filter is called with three arguments: the transcoded
  1453. comment-block, as a string, the back-end, as a symbol, and the
  1454. communication channel, as a plist. It must return a string or
  1455. nil.")
  1456. (defvar org-export-filter-example-block-functions nil
  1457. "List of functions applied to a transcoded example-block.
  1458. Each filter is called with three arguments: the transcoded
  1459. example-block, as a string, the back-end, as a symbol, and the
  1460. communication channel, as a plist. It must return a string or
  1461. nil.")
  1462. (defvar org-export-filter-export-block-functions nil
  1463. "List of functions applied to a transcoded export-block.
  1464. Each filter is called with three arguments: the transcoded
  1465. export-block, as a string, the back-end, as a symbol, and the
  1466. communication channel, as a plist. It must return a string or
  1467. nil.")
  1468. (defvar org-export-filter-fixed-width-functions nil
  1469. "List of functions applied to a transcoded fixed-width.
  1470. Each filter is called with three arguments: the transcoded
  1471. fixed-width, as a string, the back-end, as a symbol, and the
  1472. communication channel, as a plist. It must return a string or
  1473. nil.")
  1474. (defvar org-export-filter-footnote-definition-functions nil
  1475. "List of functions applied to a transcoded footnote-definition.
  1476. Each filter is called with three arguments: the transcoded
  1477. footnote-definition, as a string, the back-end, as a symbol, and
  1478. the communication channel, as a plist. It must return a string
  1479. or nil.")
  1480. (defvar org-export-filter-horizontal-rule-functions nil
  1481. "List of functions applied to a transcoded horizontal-rule.
  1482. Each filter is called with three arguments: the transcoded
  1483. horizontal-rule, as a string, the back-end, as a symbol, and the
  1484. communication channel, as a plist. It must return a string or
  1485. nil.")
  1486. (defvar org-export-filter-keyword-functions nil
  1487. "List of functions applied to a transcoded keyword.
  1488. Each filter is called with three arguments: the transcoded
  1489. keyword, as a string, the back-end, as a symbol, and the
  1490. communication channel, as a plist. It must return a string or
  1491. nil.")
  1492. (defvar org-export-filter-latex-environment-functions nil
  1493. "List of functions applied to a transcoded latex-environment.
  1494. Each filter is called with three arguments: the transcoded
  1495. latex-environment, as a string, the back-end, as a symbol, and
  1496. the communication channel, as a plist. It must return a string
  1497. or nil.")
  1498. (defvar org-export-filter-babel-call-functions nil
  1499. "List of functions applied to a transcoded babel-call.
  1500. Each filter is called with three arguments: the transcoded
  1501. babel-call, as a string, the back-end, as a symbol, and the
  1502. communication channel, as a plist. It must return a string or
  1503. nil.")
  1504. (defvar org-export-filter-paragraph-functions nil
  1505. "List of functions applied to a transcoded paragraph.
  1506. Each filter is called with three arguments: the transcoded
  1507. paragraph, as a string, the back-end, as a symbol, and the
  1508. communication channel, as a plist. It must return a string or
  1509. nil.")
  1510. (defvar org-export-filter-property-drawer-functions nil
  1511. "List of functions applied to a transcoded property-drawer.
  1512. Each filter is called with three arguments: the transcoded
  1513. property-drawer, as a string, the back-end, as a symbol, and the
  1514. communication channel, as a plist. It must return a string or
  1515. nil.")
  1516. (defvar org-export-filter-quote-block-functions nil
  1517. "List of functions applied to a transcoded quote block.
  1518. Each filter is called with three arguments: the transcoded quote
  1519. block, as a string, the back-end, as a symbol, and the
  1520. communication channel, as a plist. It must return a string or
  1521. nil.")
  1522. (defvar org-export-filter-quote-section-functions nil
  1523. "List of functions applied to a transcoded quote-section.
  1524. Each filter is called with three arguments: the transcoded
  1525. quote-section, as a string, the back-end, as a symbol, and the
  1526. communication channel, as a plist. It must return a string or
  1527. nil.")
  1528. (defvar org-export-filter-section-functions nil
  1529. "List of functions applied to a transcoded section.
  1530. Each filter is called with three arguments: the transcoded
  1531. section, as a string, the back-end, as a symbol, and the
  1532. communication channel, as a plist. It must return a string or
  1533. nil.")
  1534. (defvar org-export-filter-special-block-functions nil
  1535. "List of functions applied to a transcoded special block.
  1536. Each filter is called with three arguments: the transcoded
  1537. special block, as a string, the back-end, as a symbol, and the
  1538. communication channel, as a plist. It must return a string or
  1539. nil.")
  1540. (defvar org-export-filter-src-block-functions nil
  1541. "List of functions applied to a transcoded src-block.
  1542. Each filter is called with three arguments: the transcoded
  1543. src-block, as a string, the back-end, as a symbol, and the
  1544. communication channel, as a plist. It must return a string or
  1545. nil.")
  1546. (defvar org-export-filter-table-functions nil
  1547. "List of functions applied to a transcoded table.
  1548. Each filter is called with three arguments: the transcoded table,
  1549. as a string, the back-end, as a symbol, and the communication
  1550. channel, as a plist. It must return a string or nil.")
  1551. (defvar org-export-filter-verse-block-functions nil
  1552. "List of functions applied to a transcoded verse block.
  1553. Each filter is called with three arguments: the transcoded verse
  1554. block, as a string, the back-end, as a symbol, and the
  1555. communication channel, as a plist. It must return a string or
  1556. nil.")
  1557. ;;;; Objects Filters
  1558. (defvar org-export-filter-emphasis-functions nil
  1559. "List of functions applied to a transcoded emphasis.
  1560. Each filter is called with three arguments: the transcoded
  1561. emphasis, as a string, the back-end, as a symbol, and the
  1562. communication channel, as a plist. It must return a string or
  1563. nil.")
  1564. (defvar org-export-filter-entity-functions nil
  1565. "List of functions applied to a transcoded entity.
  1566. Each filter is called with three arguments: the transcoded
  1567. entity, as a string, the back-end, as a symbol, and the
  1568. communication channel, as a plist. It must return a string or
  1569. nil.")
  1570. (defvar org-export-filter-export-snippet-functions nil
  1571. "List of functions applied to a transcoded export-snippet.
  1572. Each filter is called with three arguments: the transcoded
  1573. export-snippet, as a string, the back-end, as a symbol, and the
  1574. communication channel, as a plist. It must return a string or
  1575. nil.")
  1576. (defvar org-export-filter-footnote-reference-functions nil
  1577. "List of functions applied to a transcoded footnote-reference.
  1578. Each filter is called with three arguments: the transcoded
  1579. footnote-reference, as a string, the back-end, as a symbol, and
  1580. the communication channel, as a plist. It must return a string
  1581. or nil.")
  1582. (defvar org-export-filter-inline-babel-call-functions nil
  1583. "List of functions applied to a transcoded inline-babel-call.
  1584. Each filter is called with three arguments: the transcoded
  1585. inline-babel-call, as a string, the back-end, as a symbol, and
  1586. the communication channel, as a plist. It must return a string
  1587. or nil.")
  1588. (defvar org-export-filter-inline-src-block-functions nil
  1589. "List of functions applied to a transcoded inline-src-block.
  1590. Each filter is called with three arguments: the transcoded
  1591. inline-src-block, as a string, the back-end, as a symbol, and the
  1592. communication channel, as a plist. It must return a string or
  1593. nil.")
  1594. (defvar org-export-filter-latex-fragment-functions nil
  1595. "List of functions applied to a transcoded latex-fragment.
  1596. Each filter is called with three arguments: the transcoded
  1597. latex-fragment, as a string, the back-end, as a symbol, and the
  1598. communication channel, as a plist. It must return a string or
  1599. nil.")
  1600. (defvar org-export-filter-line-break-functions nil
  1601. "List of functions applied to a transcoded line-break.
  1602. Each filter is called with three arguments: the transcoded
  1603. line-break, as a string, the back-end, as a symbol, and the
  1604. communication channel, as a plist. It must return a string or
  1605. nil.")
  1606. (defvar org-export-filter-link-functions nil
  1607. "List of functions applied to a transcoded link.
  1608. Each filter is called with three arguments: the transcoded link,
  1609. as a string, the back-end, as a symbol, and the communication
  1610. channel, as a plist. It must return a string or nil.")
  1611. (defvar org-export-filter-macro-functions nil
  1612. "List of functions applied to a transcoded macro.
  1613. Each filter is called with three arguments: the transcoded macro,
  1614. as a string, the back-end, as a symbol, and the communication
  1615. channel, as a plist. It must return a string or nil.")
  1616. (defvar org-export-filter-radio-target-functions nil
  1617. "List of functions applied to a transcoded radio-target.
  1618. Each filter is called with three arguments: the transcoded
  1619. radio-target, as a string, the back-end, as a symbol, and the
  1620. communication channel, as a plist. It must return a string or
  1621. nil.")
  1622. (defvar org-export-filter-statistics-cookie-functions nil
  1623. "List of functions applied to a transcoded statistics-cookie.
  1624. Each filter is called with three arguments: the transcoded
  1625. statistics-cookie, as a string, the back-end, as a symbol, and
  1626. the communication channel, as a plist. It must return a string
  1627. or nil.")
  1628. (defvar org-export-filter-subscript-functions nil
  1629. "List of functions applied to a transcoded subscript.
  1630. Each filter is called with three arguments: the transcoded
  1631. subscript, as a string, the back-end, as a symbol, and the
  1632. communication channel, as a plist. It must return a string or
  1633. nil.")
  1634. (defvar org-export-filter-superscript-functions nil
  1635. "List of functions applied to a transcoded superscript.
  1636. Each filter is called with three arguments: the transcoded
  1637. superscript, as a string, the back-end, as a symbol, and the
  1638. communication channel, as a plist. It must return a string or
  1639. nil.")
  1640. (defvar org-export-filter-target-functions nil
  1641. "List of functions applied to a transcoded target.
  1642. Each filter is called with three arguments: the transcoded
  1643. target, as a string, the back-end, as a symbol, and the
  1644. communication channel, as a plist. It must return a string or
  1645. nil.")
  1646. (defvar org-export-filter-time-stamp-functions nil
  1647. "List of functions applied to a transcoded time-stamp.
  1648. Each filter is called with three arguments: the transcoded
  1649. time-stamp, as a string, the back-end, as a symbol, and the
  1650. communication channel, as a plist. It must return a string or
  1651. nil.")
  1652. (defvar org-export-filter-verbatim-functions nil
  1653. "List of functions applied to a transcoded verbatim.
  1654. Each filter is called with three arguments: the transcoded
  1655. verbatim, as a string, the back-end, as a symbol, and the
  1656. communication channel, as a plist. It must return a string or
  1657. nil.")
  1658. (defun org-export-filter-apply-functions (filters value backend info)
  1659. "Call every function in FILTERS with arguments VALUE, BACKEND and INFO.
  1660. Functions are called in a LIFO fashion, to be sure that developer
  1661. specified filters, if any, are called first."
  1662. (loop for filter in filters
  1663. if (not value) return nil else
  1664. do (setq value (funcall filter value backend info)))
  1665. value)
  1666. (defun org-export-install-filters (backend info)
  1667. "Install filters properties in communication channel.
  1668. BACKEND is a symbol specifying which back-end specific filters to
  1669. install, if any. INFO is a plist containing the current
  1670. communication channel.
  1671. Return the updated communication channel."
  1672. (let (plist)
  1673. ;; Install user defined filters with `org-export-filters-alist'.
  1674. (mapc (lambda (p)
  1675. (setq plist (plist-put plist (car p) (eval (cdr p)))))
  1676. org-export-filters-alist)
  1677. ;; Prepend back-end specific filters to that list.
  1678. (let ((back-end-filters (intern (format "org-%s-filters-alist" backend))))
  1679. (when (boundp back-end-filters)
  1680. (mapc (lambda (p)
  1681. ;; Single values get consed, lists are prepended.
  1682. (let ((key (car p)) (value (cdr p)))
  1683. (when value
  1684. (setq plist
  1685. (plist-put
  1686. plist key
  1687. (if (atom value) (cons value (plist-get plist key))
  1688. (append value (plist-get plist key))))))))
  1689. (eval back-end-filters))))
  1690. ;; Return new communication channel.
  1691. (org-combine-plists info plist)))
  1692. ;;; Core functions
  1693. ;; This is the room for the main function, `org-export-as', along with
  1694. ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
  1695. ;; They differ only by the way they output the resulting code.
  1696. ;; `org-export-output-file-name' is an auxiliary function meant to be
  1697. ;; used with `org-export-to-file'. With a given extension, it tries
  1698. ;; to provide a canonical file name to write export output to.
  1699. ;; Note that `org-export-as' doesn't really parse the current buffer,
  1700. ;; but a copy of it (with the same buffer-local variables and
  1701. ;; visibility), where include keywords are expanded and Babel blocks
  1702. ;; are executed, if appropriate.
  1703. ;; `org-export-with-current-buffer-copy' macro prepares that copy.
  1704. ;; File inclusion is taken care of by
  1705. ;; `org-export-expand-include-keyword' and
  1706. ;; `org-export-prepare-file-contents'. Structure wise, including
  1707. ;; a whole Org file in a buffer often makes little sense. For
  1708. ;; example, if the file contains an headline and the include keyword
  1709. ;; was within an item, the item should contain the headline. That's
  1710. ;; why file inclusion should be done before any structure can be
  1711. ;; associated to the file, that is before parsing.
  1712. (defun org-export-as
  1713. (backend &optional subtreep visible-only body-only ext-plist noexpand)
  1714. "Transcode current Org buffer into BACKEND code.
  1715. If narrowing is active in the current buffer, only transcode its
  1716. narrowed part.
  1717. If a region is active, transcode that region.
  1718. When optional argument SUBTREEP is non-nil, transcode the
  1719. sub-tree at point, extracting information from the headline
  1720. properties first.
  1721. When optional argument VISIBLE-ONLY is non-nil, don't export
  1722. contents of hidden elements.
  1723. When optional argument BODY-ONLY is non-nil, only return body
  1724. code, without preamble nor postamble.
  1725. Optional argument EXT-PLIST, when provided, is a property list
  1726. with external parameters overriding Org default settings, but
  1727. still inferior to file-local settings.
  1728. Optional argument NOEXPAND, when non-nil, prevents included files
  1729. to be expanded and Babel code to be executed.
  1730. Return code as a string."
  1731. (save-excursion
  1732. (save-restriction
  1733. ;; Narrow buffer to an appropriate region for parsing.
  1734. (when (org-region-active-p)
  1735. (narrow-to-region (region-beginning) (region-end)))
  1736. (when (and subtreep (not (org-at-heading-p)))
  1737. ;; Ensure point is at sub-tree's beginning.
  1738. (org-narrow-to-subtree))
  1739. ;; Retrieve export options (INFO) and parsed tree (RAW-DATA),
  1740. ;; Then options can be completed with tree properties. Note:
  1741. ;; Buffer isn't parsed directly. Instead, a temporary copy is
  1742. ;; created, where include keywords are expanded and code blocks
  1743. ;; are evaluated. RAW-DATA is the parsed tree of the buffer
  1744. ;; resulting from that process. Eventually call
  1745. ;; `org-export-filter-parse-tree-functions'.
  1746. (goto-char (point-min))
  1747. (let ((info (org-export-get-environment backend subtreep ext-plist)))
  1748. ;; Remove subtree's headline from contents if subtree mode is
  1749. ;; activated.
  1750. (when subtreep (forward-line) (narrow-to-region (point) (point-max)))
  1751. ;; Install filters in communication channel.
  1752. (setq info (org-export-install-filters backend info))
  1753. (let ((raw-data
  1754. (org-export-filter-apply-functions
  1755. (plist-get info :filter-parse-tree)
  1756. ;; If NOEXPAND is non-nil, simply parse current
  1757. ;; visible part of buffer.
  1758. (if noexpand (org-element-parse-buffer nil visible-only)
  1759. (org-export-with-current-buffer-copy
  1760. (org-export-expand-include-keyword nil)
  1761. (let ((org-current-export-file (current-buffer)))
  1762. (org-export-blocks-preprocess))
  1763. (org-element-parse-buffer nil visible-only)))
  1764. backend info)))
  1765. ;; Complete communication channel with tree properties.
  1766. (setq info
  1767. (org-combine-plists
  1768. info
  1769. (org-export-collect-tree-properties raw-data info backend)))
  1770. ;; Transcode RAW-DATA. Also call
  1771. ;; `org-export-filter-final-output-functions'.
  1772. (let* ((body (org-element-normalize-string
  1773. (org-export-data raw-data backend info)))
  1774. (template (intern (format "org-%s-template" backend)))
  1775. (output (org-export-filter-apply-functions
  1776. (plist-get info :filter-final-output)
  1777. (if (or (not (fboundp template)) body-only) body
  1778. (funcall template body info))
  1779. backend info)))
  1780. ;; Maybe add final OUTPUT to kill ring before returning it.
  1781. (when org-export-copy-to-kill-ring (org-kill-new output))
  1782. output))))))
  1783. (defun org-export-to-buffer
  1784. (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
  1785. "Call `org-export-as' with output to a specified buffer.
  1786. BACKEND is the back-end used for transcoding, as a symbol.
  1787. BUFFER is the output buffer. If it already exists, it will be
  1788. erased first, otherwise, it will be created.
  1789. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  1790. and NOEXPAND are similar to those used in `org-export-as', which
  1791. see.
  1792. Return buffer."
  1793. (let ((out (org-export-as
  1794. backend subtreep visible-only body-only ext-plist noexpand))
  1795. (buffer (get-buffer-create buffer)))
  1796. (with-current-buffer buffer
  1797. (erase-buffer)
  1798. (insert out)
  1799. (goto-char (point-min)))
  1800. buffer))
  1801. (defun org-export-to-file
  1802. (backend file &optional subtreep visible-only body-only ext-plist noexpand)
  1803. "Call `org-export-as' with output to a specified file.
  1804. BACKEND is the back-end used for transcoding, as a symbol. FILE
  1805. is the name of the output file, as a string.
  1806. Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
  1807. and NOEXPAND are similar to those used in `org-export-as', which
  1808. see.
  1809. Return output file's name."
  1810. ;; Checks for FILE permissions. `write-file' would do the same, but
  1811. ;; we'd rather avoid needless transcoding of parse tree.
  1812. (unless (file-writable-p file) (error "Output file not writable"))
  1813. ;; Insert contents to a temporary buffer and write it to FILE.
  1814. (let ((out (org-export-as
  1815. backend subtreep visible-only body-only ext-plist noexpand)))
  1816. (with-temp-buffer
  1817. (insert out)
  1818. (let ((coding-system-for-write org-export-coding-system))
  1819. (write-file file))))
  1820. ;; Return full path.
  1821. file)
  1822. (defun org-export-output-file-name (extension &optional subtreep pub-dir)
  1823. "Return output file's name according to buffer specifications.
  1824. EXTENSION is a string representing the output file extension,
  1825. with the leading dot.
  1826. With a non-nil optional argument SUBTREEP, try to determine
  1827. output file's name by looking for \"EXPORT_FILE_NAME\" property
  1828. of subtree at point.
  1829. When optional argument PUB-DIR is set, use it as the publishing
  1830. directory.
  1831. Return file name as a string, or nil if it couldn't be
  1832. determined."
  1833. (let ((base-name
  1834. ;; File name may come from EXPORT_FILE_NAME subtree property,
  1835. ;; assuming point is at beginning of said sub-tree.
  1836. (file-name-sans-extension
  1837. (or (and subtreep
  1838. (org-entry-get
  1839. (save-excursion
  1840. (ignore-errors
  1841. (org-back-to-heading (not visible-only)) (point)))
  1842. "EXPORT_FILE_NAME" t))
  1843. ;; File name may be extracted from buffer's associated
  1844. ;; file, if any.
  1845. (buffer-file-name (buffer-base-buffer))
  1846. ;; Can't determine file name on our own: Ask user.
  1847. (let ((read-file-name-function
  1848. (and org-completion-use-ido 'ido-read-file-name)))
  1849. (read-file-name
  1850. "Output file: " pub-dir nil nil nil
  1851. (lambda (name)
  1852. (string= (file-name-extension name t) extension))))))))
  1853. ;; Build file name. Enforce EXTENSION over whatever user may have
  1854. ;; come up with. PUB-DIR, if defined, always has precedence over
  1855. ;; any provided path.
  1856. (cond
  1857. (pub-dir
  1858. (concat (file-name-as-directory pub-dir)
  1859. (file-name-nondirectory base-name)
  1860. extension))
  1861. ((string= (file-name-nondirectory base-name) base-name)
  1862. (concat (file-name-as-directory ".") base-name extension))
  1863. (t (concat base-name extension)))))
  1864. (defmacro org-export-with-current-buffer-copy (&rest body)
  1865. "Apply BODY in a copy of the current buffer.
  1866. The copy preserves local variables and visibility of the original
  1867. buffer.
  1868. Point is at buffer's beginning when BODY is applied."
  1869. (org-with-gensyms (original-buffer offset buffer-string overlays)
  1870. `(let ((,original-buffer ,(current-buffer))
  1871. (,offset ,(1- (point-min)))
  1872. (,buffer-string ,(buffer-string))
  1873. (,overlays (mapcar
  1874. 'copy-overlay (overlays-in (point-min) (point-max)))))
  1875. (with-temp-buffer
  1876. (let ((buffer-invisibility-spec nil))
  1877. (org-clone-local-variables
  1878. ,original-buffer
  1879. "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
  1880. (insert ,buffer-string)
  1881. (mapc (lambda (ov)
  1882. (move-overlay
  1883. ov
  1884. (- (overlay-start ov) ,offset)
  1885. (- (overlay-end ov) ,offset)
  1886. (current-buffer)))
  1887. ,overlays)
  1888. (goto-char (point-min))
  1889. (progn ,@body))))))
  1890. (def-edebug-spec org-export-with-current-buffer-copy (body))
  1891. (defun org-export-expand-include-keyword (included)
  1892. "Expand every include keyword in buffer.
  1893. INCLUDED is a list of included file names along with their line
  1894. restriction, when appropriate. It is used to avoid infinite
  1895. recursion."
  1896. (let ((case-fold-search nil))
  1897. (goto-char (point-min))
  1898. (while (re-search-forward "^[ \t]*#\\+include: \\(.*\\)" nil t)
  1899. (when (eq (car (save-match-data (org-element-at-point))) 'keyword)
  1900. (beginning-of-line)
  1901. ;; Extract arguments from keyword's value.
  1902. (let* ((value (match-string 1))
  1903. (ind (org-get-indentation))
  1904. (file (and (string-match "^\"\\(\\S-+\\)\"" value)
  1905. (prog1 (expand-file-name (match-string 1 value))
  1906. (setq value (replace-match "" nil nil value)))))
  1907. (lines
  1908. (and (string-match
  1909. ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
  1910. (prog1 (match-string 1 value)
  1911. (setq value (replace-match "" nil nil value)))))
  1912. (env (cond ((string-match "\\<example\\>" value) 'example)
  1913. ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
  1914. (match-string 1 value))))
  1915. ;; Minimal level of included file defaults to the child
  1916. ;; level of the current headline, if any, or one. It
  1917. ;; only applies is the file is meant to be included as
  1918. ;; an Org one.
  1919. (minlevel
  1920. (and (not env)
  1921. (if (string-match ":minlevel +\\([0-9]+\\)" value)
  1922. (prog1 (string-to-number (match-string 1 value))
  1923. (setq value (replace-match "" nil nil value)))
  1924. (let ((cur (org-current-level)))
  1925. (if cur (1+ (org-reduced-level cur)) 1))))))
  1926. ;; Remove keyword.
  1927. (delete-region (point) (progn (forward-line) (point)))
  1928. (cond
  1929. ((not (file-readable-p file)) (error "Cannot include file %s" file))
  1930. ;; Check if files has already been parsed. Look after
  1931. ;; inclusion lines too, as different parts of the same file
  1932. ;; can be included too.
  1933. ((member (list file lines) included)
  1934. (error "Recursive file inclusion: %s" file))
  1935. (t
  1936. (cond
  1937. ((eq env 'example)
  1938. (insert
  1939. (let ((ind-str (make-string ind ? ))
  1940. (contents
  1941. ;; Protect sensitive contents with commas.
  1942. (replace-regexp-in-string
  1943. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)" ","
  1944. (org-export-prepare-file-contents file lines)
  1945. nil nil 1)))
  1946. (format "%s#+begin_example\n%s%s#+end_example\n"
  1947. ind-str contents ind-str))))
  1948. ((stringp env)
  1949. (insert
  1950. (let ((ind-str (make-string ind ? ))
  1951. (contents
  1952. ;; Protect sensitive contents with commas.
  1953. (replace-regexp-in-string
  1954. (if (string= env "org") "\\(^\\)\\(.\\)"
  1955. "\\(^\\)\\([*]\\|[ \t]*#\\+\\)") ","
  1956. (org-export-prepare-file-contents file lines)
  1957. nil nil 1)))
  1958. (format "%s#+begin_src %s\n%s%s#+end_src\n"
  1959. ind-str env contents ind-str))))
  1960. (t
  1961. (insert
  1962. (with-temp-buffer
  1963. (org-mode)
  1964. (insert
  1965. (org-export-prepare-file-contents file lines ind minlevel))
  1966. (org-export-expand-include-keyword
  1967. (cons (list file lines) included))
  1968. (buffer-string))))))))))))
  1969. (defun org-export-prepare-file-contents (file &optional lines ind minlevel)
  1970. "Prepare the contents of FILE for inclusion and return them as a string.
  1971. When optional argument LINES is a string specifying a range of
  1972. lines, include only those lines.
  1973. Optional argument IND, when non-nil, is an integer specifying the
  1974. global indentation of returned contents. Since its purpose is to
  1975. allow an included file to stay in the same environment it was
  1976. created \(i.e. a list item), it doesn't apply past the first
  1977. headline encountered.
  1978. Optional argument MINLEVEL, when non-nil, is an integer
  1979. specifying the level that any top-level headline in the included
  1980. file should have."
  1981. (with-temp-buffer
  1982. (insert-file-contents file)
  1983. (when lines
  1984. (let* ((lines (split-string lines "-"))
  1985. (lbeg (string-to-number (car lines)))
  1986. (lend (string-to-number (cadr lines)))
  1987. (beg (if (zerop lbeg) (point-min)
  1988. (goto-char (point-min))
  1989. (forward-line (1- lbeg))
  1990. (point)))
  1991. (end (if (zerop lend) (point-max)
  1992. (goto-char (point-min))
  1993. (forward-line (1- lend))
  1994. (point))))
  1995. (narrow-to-region beg end)))
  1996. ;; Remove blank lines at beginning and end of contents. The logic
  1997. ;; behind that removal is that blank lines around include keyword
  1998. ;; override blank lines in included file.
  1999. (goto-char (point-min))
  2000. (org-skip-whitespace)
  2001. (beginning-of-line)
  2002. (delete-region (point-min) (point))
  2003. (goto-char (point-max))
  2004. (skip-chars-backward " \r\t\n")
  2005. (forward-line)
  2006. (delete-region (point) (point-max))
  2007. ;; If IND is set, preserve indentation of include keyword until
  2008. ;; the first headline encountered.
  2009. (when ind
  2010. (unless (eq major-mode 'org-mode) (org-mode))
  2011. (goto-char (point-min))
  2012. (let ((ind-str (make-string ind ? )))
  2013. (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
  2014. ;; Do not move footnote definitions out of column 0.
  2015. (unless (and (looking-at org-footnote-definition-re)
  2016. (eq (car (org-element-at-point)) 'footnote-definition))
  2017. (insert ind-str))
  2018. (forward-line))))
  2019. ;; When MINLEVEL is specified, compute minimal level for headlines
  2020. ;; in the file (CUR-MIN), and remove stars to each headline so
  2021. ;; that headlines with minimal level have a level of MINLEVEL.
  2022. (when minlevel
  2023. (unless (eq major-mode 'org-mode) (org-mode))
  2024. (let ((levels (org-map-entries
  2025. (lambda () (org-reduced-level (org-current-level))))))
  2026. (when levels
  2027. (let ((offset (- minlevel (apply 'min levels))))
  2028. (unless (zerop offset)
  2029. (when org-odd-levels-only (setq offset (* offset 2)))
  2030. ;; Only change stars, don't bother moving whole
  2031. ;; sections.
  2032. (org-map-entries
  2033. (lambda () (if (< offset 0) (delete-char (abs offset))
  2034. (insert (make-string offset ?*))))))))))
  2035. (buffer-string)))
  2036. ;;; Tools For Back-Ends
  2037. ;; A whole set of tools is available to help build new exporters. Any
  2038. ;; function general enough to have its use across many back-ends
  2039. ;; should be added here.
  2040. ;; As of now, functions operating on footnotes, headlines, links,
  2041. ;; macros, references, src-blocks, tables and tables of contents are
  2042. ;; implemented.
  2043. ;;;; For Footnotes
  2044. ;; `org-export-collect-footnote-definitions' is a tool to list
  2045. ;; actually used footnotes definitions in the whole parse tree, or in
  2046. ;; an headline, in order to add footnote listings throughout the
  2047. ;; transcoded data.
  2048. ;; `org-export-footnote-first-reference-p' is a predicate used by some
  2049. ;; back-ends, when they need to attach the footnote definition only to
  2050. ;; the first occurrence of the corresponding label.
  2051. ;; `org-export-get-footnote-definition' and
  2052. ;; `org-export-get-footnote-number' provide easier access to
  2053. ;; additional information relative to a footnote reference.
  2054. (defun org-export-collect-footnote-definitions (data info)
  2055. "Return an alist between footnote numbers, labels and definitions.
  2056. DATA is the parse tree from which definitions are collected.
  2057. INFO is the plist used as a communication channel.
  2058. Definitions are sorted by order of references. They either
  2059. appear as Org data \(transcoded with `org-export-data'\) or as
  2060. a secondary string for inlined footnotes \(transcoded with
  2061. `org-export-secondary-string'\). Unreferenced definitions are
  2062. ignored."
  2063. (let (refs)
  2064. ;; Collect seen references in REFS.
  2065. (org-element-map
  2066. data 'footnote-reference
  2067. (lambda (footnote local)
  2068. (when (org-export-footnote-first-reference-p footnote local)
  2069. (list (org-export-get-footnote-number footnote local)
  2070. (org-element-get-property :label footnote)
  2071. (org-export-get-footnote-definition footnote local))))
  2072. info)))
  2073. (defun org-export-footnote-first-reference-p (footnote-reference info)
  2074. "Non-nil when a footnote reference is the first one for its label.
  2075. FOOTNOTE-REFERENCE is the footnote reference being considered.
  2076. INFO is the plist used as a communication channel."
  2077. (let ((label (org-element-get-property :label footnote-reference)))
  2078. (or (not label)
  2079. (equal
  2080. footnote-reference
  2081. (org-element-map
  2082. (plist-get info :parse-tree) 'footnote-reference
  2083. (lambda (footnote local)
  2084. (when (string= (org-element-get-property :label footnote) label)
  2085. footnote))
  2086. info 'first-match)))))
  2087. (defun org-export-get-footnote-definition (footnote-reference info)
  2088. "Return definition of FOOTNOTE-REFERENCE as parsed data.
  2089. INFO is the plist used as a communication channel."
  2090. (let ((label (org-element-get-property :label footnote-reference)))
  2091. (or (org-element-get-property :inline-definition footnote-reference)
  2092. (cdr (assoc label (plist-get info :footnote-definition-alist))))))
  2093. (defun org-export-get-footnote-number (footnote info)
  2094. "Return number associated to a footnote.
  2095. FOOTNOTE is either a footnote reference or a footnote definition.
  2096. INFO is the plist used as a communication channel."
  2097. (let ((label (org-element-get-property :label footnote)) seen-refs)
  2098. (org-element-map
  2099. (plist-get info :parse-tree) 'footnote-reference
  2100. (lambda (fn local)
  2101. (let ((fn-lbl (org-element-get-property :label fn)))
  2102. (cond
  2103. ((and (not fn-lbl) (equal fn footnote)) (1+ (length seen-refs)))
  2104. ((and label (string= label fn-lbl)) (1+ (length seen-refs)))
  2105. ;; Anonymous footnote: it's always a new one. Also, be sure
  2106. ;; to return nil from the `cond' so `first-match' doesn't
  2107. ;; get us out of the loop.
  2108. ((not fn-lbl) (push 'inline seen-refs) nil)
  2109. ;; Label not seen so far: add it so SEEN-REFS. Again,
  2110. ;; return nil to stay in the loop.
  2111. ((not (member fn-lbl seen-refs)) (push fn-lbl seen-refs) nil))))
  2112. info 'first-match)))
  2113. ;;;; For Headlines
  2114. ;; `org-export-get-relative-level' is a shortcut to get headline
  2115. ;; level, relatively to the lower headline level in the parsed tree.
  2116. ;; `org-export-get-headline-number' returns the section number of an
  2117. ;; headline, while `org-export-number-to-roman' allows to convert it
  2118. ;; to roman numbers.
  2119. ;; `org-export-low-level-p', `org-export-first-sibling-p' and
  2120. ;; `org-export-last-sibling-p' are three useful predicates when it
  2121. ;; comes to fulfill the `:headline-levels' property.
  2122. (defun org-export-get-relative-level (headline info)
  2123. "Return HEADLINE relative level within current parsed tree.
  2124. INFO is a plist holding contextual information."
  2125. (+ (org-element-get-property :level headline)
  2126. (or (plist-get info :headline-offset) 0)))
  2127. (defun org-export-low-level-p (headline info)
  2128. "Non-nil when HEADLINE is considered as low level.
  2129. INFO is a plist used as a communication channel.
  2130. A low level headlines has a relative level greater than
  2131. `:headline-levels' property value.
  2132. Return value is the difference between HEADLINE relative level
  2133. and the last level being considered as high enough, or nil."
  2134. (let ((limit (plist-get info :headline-levels)))
  2135. (when (wholenump limit)
  2136. (let ((level (org-export-get-relative-level headline info)))
  2137. (and (> level limit) (- level limit))))))
  2138. (defun org-export-get-headline-number (headline info)
  2139. "Return HEADLINE numbering as a list of numbers.
  2140. INFO is a plist holding contextual information."
  2141. (cdr (assoc headline (plist-get info :headline-numbering))))
  2142. (defun org-export-number-to-roman (n)
  2143. "Convert integer N into a roman numeral."
  2144. (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
  2145. ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
  2146. ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
  2147. ( 1 . "I")))
  2148. (res ""))
  2149. (if (<= n 0)
  2150. (number-to-string n)
  2151. (while roman
  2152. (if (>= n (caar roman))
  2153. (setq n (- n (caar roman))
  2154. res (concat res (cdar roman)))
  2155. (pop roman)))
  2156. res)))
  2157. (defun org-export-first-sibling-p (headline info)
  2158. "Non-nil when HEADLINE is the first sibling in its sub-tree.
  2159. INFO is the plist used as a communication channel."
  2160. (not (eq (car (org-export-get-previous-element headline info)) 'headline)))
  2161. (defun org-export-last-sibling-p (headline info)
  2162. "Non-nil when HEADLINE is the last sibling in its sub-tree.
  2163. INFO is the plist used as a communication channel."
  2164. (equal
  2165. (car (last (org-element-get-contents (car (plist-get info :genealogy)))))
  2166. headline))
  2167. ;;;; For Links
  2168. ;; `org-export-solidify-link-text' turns a string into a safer version
  2169. ;; for links, replacing most non-standard characters with hyphens.
  2170. ;; `org-export-get-coderef-format' returns an appropriate format
  2171. ;; string for coderefs.
  2172. ;; `org-export-inline-image-p' returns a non-nil value when the link
  2173. ;; provided should be considered as an inline image.
  2174. ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
  2175. ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
  2176. ;; returns an appropriate unique identifier when found, or nil.
  2177. ;; `org-export-resolve-id-link' returns the first headline with
  2178. ;; specified id or custom-id in parse tree, or nil when none was
  2179. ;; found.
  2180. ;; `org-export-resolve-coderef' associates a reference to a line
  2181. ;; number in the element it belongs, or returns the reference itself
  2182. ;; when the element isn't numbered.
  2183. (defun org-export-solidify-link-text (s)
  2184. "Take link text S and make a safe target out of it."
  2185. (save-match-data
  2186. (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
  2187. (defun org-export-get-coderef-format (path desc)
  2188. "Return format string for code reference link.
  2189. PATH is the link path. DESC is its description."
  2190. (save-match-data
  2191. (cond ((string-match (regexp-quote (concat "(" path ")")) desc)
  2192. (replace-match "%s" t t desc))
  2193. ((string= desc "") "%s")
  2194. (t desc))))
  2195. (defun org-export-inline-image-p (link &optional rules)
  2196. "Non-nil if LINK object points to an inline image.
  2197. Optional argument is a set of RULES defining inline images. It
  2198. is an alist where associations have the following shape:
  2199. \(TYPE . REGEXP)
  2200. Applying a rule means apply REGEXP against LINK's path when its
  2201. type is TYPE. The function will return a non-nil value if any of
  2202. the provided rules is non-nil. The default rule is
  2203. `org-export-default-inline-image-rule'.
  2204. This only applies to links without a description."
  2205. (and (not (org-element-get-contents link))
  2206. (let ((case-fold-search t)
  2207. (rules (or rules org-export-default-inline-image-rule)))
  2208. (some
  2209. (lambda (rule)
  2210. (and (string= (org-element-get-property :type link) (car rule))
  2211. (string-match (cdr rule)
  2212. (org-element-get-property :path link))))
  2213. rules))))
  2214. (defun org-export-resolve-fuzzy-link (link info)
  2215. "Return LINK destination.
  2216. INFO is a plist holding contextual information.
  2217. Return value can be an object, an element, or nil:
  2218. - If LINK path exactly matches any target, return the target
  2219. object.
  2220. - If LINK path exactly matches any headline name, return that
  2221. element. If more than one headline share that name, priority
  2222. will be given to the one with the closest common ancestor, if
  2223. any, or the first one in the parse tree otherwise.
  2224. - Otherwise, return nil.
  2225. Assume LINK type is \"fuzzy\"."
  2226. (let ((path (org-element-get-property :path link)))
  2227. ;; Link points to a target: return it.
  2228. (or (loop for target in (plist-get info :target-list)
  2229. when (string= (org-element-get-property :raw-value target) path)
  2230. return target)
  2231. ;; Link either points to an headline or nothing. Try to find
  2232. ;; the source, with priority given to headlines with the closest
  2233. ;; common ancestor. If such candidate is found, return its
  2234. ;; beginning position as an unique identifier, otherwise return
  2235. ;; nil.
  2236. (let ((find-headline
  2237. (function
  2238. ;; Return first headline whose `:raw-value' property
  2239. ;; is NAME in parse tree DATA, or nil.
  2240. (lambda (name data)
  2241. (org-element-map
  2242. data 'headline
  2243. (lambda (headline local)
  2244. (when (string=
  2245. (org-element-get-property :raw-value headline)
  2246. name)
  2247. headline))
  2248. info 'first-match)))))
  2249. ;; Search among headlines sharing an ancestor with link,
  2250. ;; from closest to farthest.
  2251. (or (catch 'exit
  2252. (mapc
  2253. (lambda (parent)
  2254. (when (eq (car parent) 'headline)
  2255. (let ((foundp (funcall find-headline path parent)))
  2256. (when foundp (throw 'exit foundp)))))
  2257. (plist-get info :genealogy)) nil)
  2258. ;; No match with a common ancestor: try the full parse-tree.
  2259. (funcall find-headline path (plist-get info :parse-tree)))))))
  2260. (defun org-export-resolve-id-link (link info)
  2261. "Return headline referenced as LINK destination.
  2262. INFO is a plist used as a communication channel.
  2263. Return value can be an headline element or nil. Assume LINK type
  2264. is either \"id\" or \"custom-id\"."
  2265. (let ((id (org-element-get-property :path link)))
  2266. (org-element-map
  2267. (plist-get info :parse-tree) 'headline
  2268. (lambda (headline local)
  2269. (when (or (string= (org-element-get-property :id headline) id)
  2270. (string= (org-element-get-property :custom-id headline) id))
  2271. headline))
  2272. info 'first-match)))
  2273. (defun org-export-resolve-coderef (ref info)
  2274. "Resolve a code reference REF.
  2275. INFO is a plist used as a communication channel.
  2276. Return associated line number in source code, or REF itself,
  2277. depending on src-block or example element's switches."
  2278. (org-element-map
  2279. (plist-get info :parse-tree) '(src-block example)
  2280. (lambda (el local)
  2281. (let ((switches (or (org-element-get-property :switches el) "")))
  2282. (with-temp-buffer
  2283. (insert (org-trim (org-element-get-property :value el)))
  2284. ;; Build reference regexp.
  2285. (let* ((label
  2286. (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2287. (match-string 1 switches))
  2288. org-coderef-label-format))
  2289. (ref-re
  2290. (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2291. (replace-regexp-in-string "%s" ref label nil t))))
  2292. ;; Element containing REF is found. Only associate REF to
  2293. ;; a line number if element has "+n" or "-n" and "-k" or
  2294. ;; "-r" as switches. When it has "+n", count accumulated
  2295. ;; locs before, too.
  2296. (when (re-search-backward ref-re nil t)
  2297. (cond
  2298. ((not (string-match "-[kr]\\>" switches)) ref)
  2299. ((string-match "-n\\>" switches) (line-number-at-pos))
  2300. ((string-match "\\+n\\>" switches)
  2301. (+ (org-export-get-loc el local) (line-number-at-pos)))
  2302. (t ref)))))))
  2303. info 'first-match))
  2304. ;;;; For Macros
  2305. ;; `org-export-expand-macro' simply takes care of expanding macros.
  2306. (defun org-export-expand-macro (macro info)
  2307. "Expand MACRO and return it as a string.
  2308. INFO is a plist holding export options."
  2309. (let* ((key (org-element-get-property :key macro))
  2310. (args (org-element-get-property :args macro))
  2311. ;; User's macros are stored in the communication channel with
  2312. ;; a ":macro-" prefix. If it's a string leave it as-is.
  2313. ;; Otherwise, it's a secondary string that needs to be
  2314. ;; expanded recursively.
  2315. (value
  2316. (let ((val (plist-get info (intern (format ":macro-%s" key)))))
  2317. (if (stringp val) val
  2318. (org-export-secondary-string
  2319. val (plist-get info :back-end) info)))))
  2320. ;; Replace arguments in VALUE.
  2321. (let ((s 0) n)
  2322. (while (string-match "\\$\\([0-9]+\\)" value s)
  2323. (setq s (1+ (match-beginning 0))
  2324. n (string-to-number (match-string 1 value)))
  2325. (and (>= (length args) n)
  2326. (setq value (replace-match (nth (1- n) args) t t value)))))
  2327. ;; VALUE starts with "(eval": it is a s-exp, `eval' it.
  2328. (when (string-match "\\`(eval\\>" value)
  2329. (setq value (eval (read value))))
  2330. ;; Return string.
  2331. (format "%s" (or value ""))))
  2332. ;;;; For References
  2333. ;; `org-export-get-ordinal' associates a sequence number to any object
  2334. ;; or element.
  2335. (defun org-export-get-ordinal
  2336. (element info &optional types within-section predicate)
  2337. "Return ordinal number of an element or object.
  2338. ELEMENT is the element or object considered. INFO is the plist
  2339. used as a communication channel.
  2340. Optional argument TYPES, when non-nil, is a list of element or
  2341. object types, as symbols, that should also be counted in.
  2342. Otherwise, only provided element's type is considered.
  2343. When optional argument WITHIN-SECTION is non-nil, narrow counting
  2344. to the section containing ELEMENT.
  2345. Optional argument PREDICATE is a function returning a non-nil
  2346. value if the current element or object should be counted in. It
  2347. accepts one argument: the element or object being considered.
  2348. This argument allows to count only a certain type of objects,
  2349. like inline images, which are a subset of links \(in that case,
  2350. `org-export-inline-image-p' might be an useful predicate\)."
  2351. (let ((counter 0)
  2352. ;; Determine if search should apply to current section, in
  2353. ;; which case it should be retrieved first, or to full parse
  2354. ;; tree. As a special case, an element or object without
  2355. ;; a parent headline will also trigger a full search,
  2356. ;; notwithstanding WITHIN-SECTION value.
  2357. (data
  2358. (if (not within-section) (plist-get info :parse-tree)
  2359. (or (org-export-get-parent-headline element info)
  2360. (plist-get info :parse-tree)))))
  2361. ;; Increment counter until ELEMENT is found again.
  2362. (org-element-map
  2363. data (or types (car element))
  2364. (lambda (el local)
  2365. (cond
  2366. ((equal element el) (1+ counter))
  2367. ((not predicate) (incf counter) nil)
  2368. ((funcall predicate el) (incf counter) nil)))
  2369. info 'first-match)))
  2370. ;;;; For Src-Blocks
  2371. ;; `org-export-get-loc' counts number of code lines accumulated in
  2372. ;; src-block or example-block elements with a "+n" switch until
  2373. ;; a given element, excluded. Note: "-n" switches reset that count.
  2374. ;; `org-export-handle-code' takes care of line numbering and reference
  2375. ;; cleaning in source code, when appropriate.
  2376. (defun org-export-get-loc (element info)
  2377. "Return accumulated lines of code up to ELEMENT.
  2378. INFO is the plist used as a communication channel.
  2379. ELEMENT is excluded from count."
  2380. (let ((loc 0))
  2381. (org-element-map
  2382. (plist-get info :parse-tree) `(src-block example-block ,(car element))
  2383. (lambda (el local)
  2384. (cond
  2385. ;; ELEMENT is reached: Quit the loop.
  2386. ((equal el element) t)
  2387. ;; Only count lines from src-block and example-block elements
  2388. ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
  2389. ((not (memq (car el) '(src-block example-block))) nil)
  2390. ((let ((switches (org-element-get-property :switches el)))
  2391. (when (and switches (string-match "\\([-+]\\)n\\>" switches))
  2392. ;; Accumulate locs or reset them.
  2393. (let ((accumulatep (string= (match-string 1 switches) "-"))
  2394. (lines (org-count-lines
  2395. (org-trim (org-element-get-property :value el)))))
  2396. (setq loc (if accumulatep lines (+ loc lines))))))
  2397. ;; Return nil to stay in the loop.
  2398. nil)))
  2399. info 'first-match)
  2400. ;; Return value.
  2401. loc))
  2402. (defun org-export-handle-code (element info &optional num-fmt ref-fmt delayed)
  2403. "Handle line numbers and code references in ELEMENT.
  2404. ELEMENT has either a `src-block' an `example-block' type. INFO
  2405. is a plist used as a communication channel.
  2406. If optional argument NUM-FMT is a string, it will be used as
  2407. a format string for numbers at beginning of each line.
  2408. If optional argument REF-FMT is a string, it will be used as
  2409. a format string for each line of code containing a reference.
  2410. When optional argument DELAYED is non-nil, `org-loc' and
  2411. `org-coderef' properties, set to an adequate value, are applied
  2412. to, respectively, numbered lines and lines with a reference. No
  2413. line numbering is done and all references are stripped from the
  2414. resulting string. Both NUM-FMT and REF-FMT arguments are ignored
  2415. in that situation.
  2416. Return new code as a string."
  2417. (let* ((switches (or (org-element-get-property :switches element) ""))
  2418. (code (org-element-get-property :value element))
  2419. (numberp (string-match "[-+]n\\>" switches))
  2420. (accumulatep (string-match "\\+n\\>" switches))
  2421. ;; Initialize loc counter when any kind of numbering is
  2422. ;; active.
  2423. (total-LOC (cond
  2424. (accumulatep (org-export-get-loc element info))
  2425. (numberp 0)))
  2426. ;; Get code and clean it. Remove blank lines at its
  2427. ;; beginning and end. Also remove protective commas.
  2428. (preserve-indent-p (or org-src-preserve-indentation
  2429. (string-match "-i\\>" switches)))
  2430. (replace-labels (when (string-match "-r\\>" switches)
  2431. (if (string-match "-k\\>" switches) 'keep t)))
  2432. (code (let ((c (replace-regexp-in-string
  2433. "\\`\\([ \t]*\n\\)+" ""
  2434. (replace-regexp-in-string
  2435. "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n" code))))
  2436. ;; If appropriate, remove global indentation.
  2437. (unless preserve-indent-p (setq c (org-remove-indentation c)))
  2438. ;; Free up the protected lines. Note: Org blocks
  2439. ;; have commas at the beginning or every line.
  2440. (if (string=
  2441. (or (org-element-get-property :language element) "")
  2442. "org")
  2443. (replace-regexp-in-string "^," "" c)
  2444. (replace-regexp-in-string
  2445. "^\\(,\\)\\(:?\\*\\|[ \t]*#\\+\\)" "" c nil nil 1))))
  2446. ;; Split code to process it line by line.
  2447. (code-lines (org-split-string code "\n"))
  2448. ;; If numbering is active, ensure line numbers will be
  2449. ;; correctly padded before applying the format string.
  2450. (num-fmt
  2451. (when (and (not delayed) numberp)
  2452. (format (if (stringp num-fmt) num-fmt "%s: ")
  2453. (format "%%%ds"
  2454. (length (number-to-string
  2455. (+ (length code-lines) total-LOC)))))))
  2456. ;; Get format used for references.
  2457. (label-fmt (or (and (string-match "-l +\"\\([^\"\n]+\\)\"" switches)
  2458. (match-string 1 switches))
  2459. org-coderef-label-format))
  2460. ;; Build a regexp matching a loc with a reference.
  2461. (with-ref-re (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
  2462. (replace-regexp-in-string
  2463. "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
  2464. (org-element-normalize-string
  2465. (mapconcat
  2466. (lambda (loc)
  2467. ;; Maybe add line number to current line of code (LOC).
  2468. (when numberp
  2469. (incf total-LOC)
  2470. (setq loc (if delayed (org-add-props loc nil 'org-loc total-LOC)
  2471. (concat (format num-fmt total-LOC) loc))))
  2472. ;; Take action if at a ref line.
  2473. (when (string-match with-ref-re loc)
  2474. (let ((ref (match-string 3 loc)))
  2475. (setq loc
  2476. ;; Option "-r" without "-k" removes labels.
  2477. ;; A non-nil DELAYED removes labels unconditionally.
  2478. (if (or delayed
  2479. (and replace-labels (not (eq replace-labels 'keep))))
  2480. (replace-match "" nil nil loc 1)
  2481. (replace-match (format "(%s)" ref) nil nil loc 2)))
  2482. ;; Store REF in `org-coderef' property if DELAYED asks to.
  2483. (cond (delayed (setq loc (org-add-props loc nil 'org-coderef ref)))
  2484. ;; If REF-FMT is defined, apply it to current LOC.
  2485. ((stringp ref-fmt) (setq loc (format ref-fmt loc))))))
  2486. ;; Return updated LOC for concatenation.
  2487. loc)
  2488. code-lines "\n"))))
  2489. ;;;; For Tables
  2490. ;; `org-export-table-format-info' extracts formatting information
  2491. ;; (alignment, column groups and presence of a special column) from
  2492. ;; a raw table and returns it as a property list.
  2493. ;;
  2494. ;; `org-export-clean-table' cleans the raw table from any Org
  2495. ;; table-specific syntax.
  2496. (defun org-export-table-format-info (table)
  2497. "Extract info from TABLE.
  2498. Return a plist whose properties and values are:
  2499. `:alignment' vector of strings among \"r\", \"l\" and \"c\",
  2500. `:column-groups' vector of symbols among `start', `end', `start-end',
  2501. `:row-groups' list of integers representing row groups.
  2502. `:special-column-p' non-nil if table has a special column.
  2503. `:width' vector of integers representing desired width of
  2504. current column, or nil."
  2505. (with-temp-buffer
  2506. (insert table)
  2507. (goto-char 1)
  2508. (org-table-align)
  2509. (let ((align (vconcat (mapcar (lambda (c) (if c "r" "l"))
  2510. org-table-last-alignment)))
  2511. (width (make-vector (length org-table-last-alignment) nil))
  2512. (colgroups (make-vector (length org-table-last-alignment) nil))
  2513. (row-group 0)
  2514. (rowgroups)
  2515. (special-column-p 'empty))
  2516. (mapc (lambda (row)
  2517. (if (string-match "^[ \t]*|[-+]+|[ \t]*$" row)
  2518. (incf row-group)
  2519. ;; Determine if a special column is present by looking
  2520. ;; for special markers in the first column. More
  2521. ;; accurately, the first column is considered special
  2522. ;; if it only contains special markers and, maybe,
  2523. ;; empty cells.
  2524. (setq special-column-p
  2525. (cond
  2526. ((not special-column-p) nil)
  2527. ((string-match "^[ \t]*| *\\\\?\\([/#!$*_^]\\) *|" row)
  2528. 'special)
  2529. ((string-match "^[ \t]*| +|" row) special-column-p))))
  2530. (cond
  2531. ;; Read forced alignment and width information, if any,
  2532. ;; and determine final alignment for the table.
  2533. ((org-table-cookie-line-p row)
  2534. (let ((col 0))
  2535. (mapc (lambda (field)
  2536. (when (string-match
  2537. "<\\([lrc]\\)?\\([0-9]+\\)?>" field)
  2538. (let ((align-data (match-string 1 field)))
  2539. (when align-data (aset align col align-data)))
  2540. (let ((w-data (match-string 2 field)))
  2541. (when w-data
  2542. (aset width col (string-to-number w-data)))))
  2543. (incf col))
  2544. (org-split-string row "[ \t]*|[ \t]*"))))
  2545. ;; Read column groups information.
  2546. ((org-table-colgroup-line-p row)
  2547. (let ((col 0))
  2548. (mapc (lambda (field)
  2549. (aset colgroups col
  2550. (cond ((string= "<" field) 'start)
  2551. ((string= ">" field) 'end)
  2552. ((string= "<>" field) 'start-end)))
  2553. (incf col))
  2554. (org-split-string row "[ \t]*|[ \t]*"))))
  2555. ;; Contents line.
  2556. (t (push row-group rowgroups))))
  2557. (org-split-string table "\n"))
  2558. ;; Return plist.
  2559. (list :alignment align
  2560. :column-groups colgroups
  2561. :row-groups (reverse rowgroups)
  2562. :special-column-p (eq special-column-p 'special)
  2563. :width width))))
  2564. (defun org-export-clean-table (table specialp)
  2565. "Clean string TABLE from its formatting elements.
  2566. Remove any row containing column groups or formatting cookies and
  2567. rows starting with a special marker. If SPECIALP is non-nil,
  2568. assume the table contains a special formatting column and remove
  2569. it also."
  2570. (let ((rows (org-split-string table "\n")))
  2571. (mapconcat 'identity
  2572. (delq nil
  2573. (mapcar
  2574. (lambda (row)
  2575. (cond
  2576. ((org-table-colgroup-line-p row) nil)
  2577. ((org-table-cookie-line-p row) nil)
  2578. ;; Ignore rows starting with a special marker.
  2579. ((string-match "^[ \t]*| *[!_^/] *|" row) nil)
  2580. ;; Remove special column.
  2581. ((and specialp
  2582. (or (string-match "^\\([ \t]*\\)|-+\\+" row)
  2583. (string-match "^\\([ \t]*\\)|[^|]*|" row)))
  2584. (replace-match "\\1|" t nil row))
  2585. (t row)))
  2586. rows))
  2587. "\n")))
  2588. ;;;; For Tables Of Contents
  2589. ;; `org-export-collect-headlines' builds a list of all exportable
  2590. ;; headline elements, maybe limited to a certain depth. One can then
  2591. ;; easily parse it and transcode it.
  2592. ;; Building lists of tables, figures or listings is quite similar.
  2593. ;; Once the generic function `org-export-collect-elements' is defined,
  2594. ;; `org-export-collect-tables', `org-export-collect-figures' and
  2595. ;; `org-export-collect-listings' can be derived from it.
  2596. (defun org-export-collect-headlines (info &optional n)
  2597. "Collect headlines in order to build a table of contents.
  2598. INFO is a plist used as a communication channel.
  2599. When non-nil, optional argument N must be an integer. It
  2600. specifies the depth of the table of contents.
  2601. Return a list of all exportable headlines as parsed elements."
  2602. (org-element-map
  2603. (plist-get info :parse-tree)
  2604. 'headline
  2605. (lambda (headline local)
  2606. ;; Strip contents from HEADLINE.
  2607. (let ((relative-level (org-export-get-relative-level headline local)))
  2608. (unless (and n (> relative-level n)) headline)))
  2609. info))
  2610. (defun org-export-collect-elements (type info &optional predicate)
  2611. "Collect referenceable elements of a determined type.
  2612. TYPE can be a symbol or a list of symbols specifying element
  2613. types to search. Only elements with a caption or a name are
  2614. collected.
  2615. INFO is a plist used as a communication channel.
  2616. When non-nil, optional argument PREDICATE is a function accepting
  2617. one argument, an element of type TYPE. It returns a non-nil
  2618. value when that element should be collected.
  2619. Return a list of all elements found, in order of appearance."
  2620. (org-element-map
  2621. (plist-get info :parse-tree) type
  2622. (lambda (element local)
  2623. (and (or (org-element-get-property :caption element)
  2624. (org-element-get-property :name element))
  2625. (or (not predicate) (funcall predicate element))
  2626. element)) info))
  2627. (defun org-export-collect-tables (info)
  2628. "Build a list of tables.
  2629. INFO is a plist used as a communication channel.
  2630. Return a list of table elements with a caption or a name
  2631. affiliated keyword."
  2632. (org-export-collect-elements 'table info))
  2633. (defun org-export-collect-figures (info predicate)
  2634. "Build a list of figures.
  2635. INFO is a plist used as a communication channel. PREDICATE is
  2636. a function which accepts one argument: a paragraph element and
  2637. whose return value is non-nil when that element should be
  2638. collected.
  2639. A figure is a paragraph type element, with a caption or a name,
  2640. verifying PREDICATE. The latter has to be provided since
  2641. a \"figure\" is a vague concept that may depend on back-end.
  2642. Return a list of elements recognized as figures."
  2643. (org-export-collect-elements 'paragraph info predicate))
  2644. (defun org-export-collect-listings (info)
  2645. "Build a list of src blocks.
  2646. INFO is a plist used as a communication channel.
  2647. Return a list of src-block elements with a caption or a name
  2648. affiliated keyword."
  2649. (org-export-collect-elements 'src-block info))
  2650. ;;;; Topology
  2651. ;; Here are various functions to retrieve information about the
  2652. ;; neighbourhood of a given element or object. Neighbours of interest
  2653. ;; are parent headline (`org-export-get-parent-headline'), parent
  2654. ;; paragraph (`org-export-get-parent-paragraph'), previous element or
  2655. ;; object (`org-export-get-previous-element') and next element or
  2656. ;; object (`org-export-get-next-element').
  2657. ;; All of these functions are just a specific use of the more generic
  2658. ;; `org-export-get-genealogy', which returns the genealogy relative to
  2659. ;; the element or object.
  2660. (defun org-export-get-genealogy (blob info)
  2661. "Return genealogy relative to a given element or object.
  2662. BLOB is the element or object being considered. INFO is a plist
  2663. used as a communication channel."
  2664. ;; LOCALP tells if current `:genealogy' is sufficient to find parent
  2665. ;; headline, or if it should be computed.
  2666. (let ((localp (member blob (org-element-get-contents
  2667. (car (plist-get info :genealogy))))))
  2668. (if localp (plist-get info :genealogy)
  2669. (catch 'exit
  2670. (org-element-map
  2671. (plist-get info :parse-tree) (car blob)
  2672. (lambda (el local)
  2673. (when (equal el blob)
  2674. (throw 'exit (plist-get local :genealogy))))
  2675. info)))))
  2676. (defun org-export-get-parent-headline (blob info)
  2677. "Return closest parent headline or nil.
  2678. BLOB is the element or object being considered. INFO is a plist
  2679. used as a communication channel."
  2680. (catch 'exit
  2681. (mapc
  2682. (lambda (el) (when (eq (car el) 'headline) (throw 'exit el)))
  2683. (org-export-get-genealogy blob info))
  2684. nil))
  2685. (defun org-export-get-parent-paragraph (object info)
  2686. "Return parent paragraph or nil.
  2687. INFO is a plist used as a communication channel.
  2688. Optional argument OBJECT, when provided, is the object to consider.
  2689. Otherwise, return the paragraph containing current object.
  2690. This is useful for objects, which share attributes with the
  2691. paragraph containing them."
  2692. (catch 'exit
  2693. (mapc
  2694. (lambda (el) (when (eq (car el) 'paragraph) (throw 'exit el)))
  2695. (org-export-get-genealogy object info))
  2696. nil))
  2697. (defun org-export-get-previous-element (blob info)
  2698. "Return previous element or object.
  2699. BLOB is an element or object. INFO is a plist used as
  2700. a communication channel.
  2701. Return previous element or object, a string, or nil."
  2702. (let ((parent (car (org-export-get-genealogy blob info))))
  2703. (cadr (member blob (reverse (org-element-get-contents parent))))))
  2704. (defun org-export-get-next-element (blob info)
  2705. "Return next element or object.
  2706. BLOB is an element or object. INFO is a plist used as
  2707. a communication channel.
  2708. Return next element or object, a string, or nil."
  2709. (let ((parent (car (org-export-get-genealogy blob info))))
  2710. (cadr (member blob (org-element-get-contents parent)))))
  2711. ;;; The Dispatcher
  2712. ;; `org-export-dispatch' is the standard interactive way to start an
  2713. ;; export process. It uses `org-export-dispatch-ui' as a subroutine
  2714. ;; for its interface. Most commons back-ends should have an entry in
  2715. ;; it.
  2716. (defun org-export-dispatch ()
  2717. "Export dispatcher for Org mode.
  2718. It provides an access to common export related tasks in a buffer.
  2719. Its interface comes in two flavours: standard and expert. While
  2720. both share the same set of bindings, only the former displays the
  2721. valid keys associations. Set `org-export-dispatch-use-expert-ui'
  2722. to switch to one or the other.
  2723. Return an error if key pressed has no associated command."
  2724. (interactive)
  2725. (let* ((input (org-export-dispatch-ui
  2726. (if (listp org-export-initial-scope) org-export-initial-scope
  2727. (list org-export-initial-scope))
  2728. org-export-dispatch-use-expert-ui))
  2729. (raw-key (car input))
  2730. (optns (cdr input)))
  2731. ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
  2732. ;; depending on user's key pressed.
  2733. (case (if (< raw-key 27) (+ raw-key 96) raw-key)
  2734. ;; Export with `e-ascii' back-end.
  2735. ((?A ?N ?U)
  2736. (let ((outbuf
  2737. (org-export-to-buffer
  2738. 'e-ascii "*Org E-ASCII Export*"
  2739. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  2740. `(:ascii-charset
  2741. ,(case raw-key (?A 'ascii) (?N 'latin1) (t 'utf-8))))))
  2742. (with-current-buffer outbuf (text-mode))
  2743. (when org-export-show-temporary-export-buffer
  2744. (switch-to-buffer-other-window outbuf))))
  2745. ((?a ?n ?u)
  2746. (org-e-ascii-export-to-ascii
  2747. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)
  2748. `(:ascii-charset ,(case raw-key (?a 'ascii) (?n 'latin1) (t 'utf-8)))))
  2749. ;; Export with `e-latex' back-end.
  2750. (?L
  2751. (let ((outbuf
  2752. (org-export-to-buffer
  2753. 'e-latex "*Org E-LaTeX Export*"
  2754. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2755. (with-current-buffer outbuf (latex-mode))
  2756. (when org-export-show-temporary-export-buffer
  2757. (switch-to-buffer-other-window outbuf))))
  2758. (?l (org-e-latex-export-to-latex
  2759. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2760. (?p (org-e-latex-export-to-pdf
  2761. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns)))
  2762. (?d (org-open-file
  2763. (org-e-latex-export-to-pdf
  2764. (memq 'subtree optns) (memq 'visible optns) (memq 'body optns))))
  2765. ;; Publishing facilities
  2766. (?F (org-e-publish-current-file (memq 'force optns)))
  2767. (?P (org-e-publish-current-project (memq 'force optns)))
  2768. (?X (let ((project
  2769. (assoc (org-icompleting-read
  2770. "Publish project: " org-e-publish-project-alist nil t)
  2771. org-e-publish-project-alist)))
  2772. (org-e-publish project (memq 'force optns))))
  2773. (?E (org-e-publish-all (memq 'force optns)))
  2774. ;; Undefined command.
  2775. (t (error "No command associated with key %s"
  2776. (char-to-string raw-key))))))
  2777. (defun org-export-dispatch-ui (options expertp)
  2778. "Handle interface for `org-export-dispatch'.
  2779. OPTIONS is a list containing current interactive options set for
  2780. export. It can contain any of the following symbols:
  2781. `body' toggles a body-only export
  2782. `subtree' restricts export to current subtree
  2783. `visible' restricts export to visible part of buffer.
  2784. `force' force publishing files.
  2785. EXPERTP, when non-nil, triggers expert UI. In that case, no help
  2786. buffer is provided, but indications about currently active
  2787. options are given in the prompt. Moreover, \[?] allows to switch
  2788. back to standard interface.
  2789. Return value is a list with key pressed as CAR and a list of
  2790. final interactive export options as CDR."
  2791. (let ((help
  2792. (format "-------------------- General Options --------------------
  2793. \[1] Body only: %s [2] Export scope: %s
  2794. \[3] Visible only: %s [4] Force publishing: %s
  2795. -------------- ASCII/Latin-1/UTF-8 Export ---------------
  2796. \[a/n/u] to TXT file [A/N/U] to temporary buffer
  2797. --------------------- LaTeX Export ----------------------
  2798. \[l] to TEX file [L] to temporary buffer
  2799. \[p] to PDF file [d] ... and open it
  2800. ------------------------- Publish -------------------------
  2801. \[F] current file [P] current project
  2802. \[X] a project [E] every project"
  2803. (if (memq 'body options) "On" "Off")
  2804. (if (memq 'subtree options) "Subtree" "Buffer")
  2805. (if (memq 'visible options) "On" "Off")
  2806. (if (memq 'force options) "On" "Off")))
  2807. (standard-prompt "Export command: ")
  2808. (expert-prompt (format "Export command (%s%s%s%s): "
  2809. (if (memq 'body options) "b" "-")
  2810. (if (memq 'subtree options) "s" "-")
  2811. (if (memq 'visible options) "v" "-")
  2812. (if (memq 'force options) "f" "-")))
  2813. (handle-keypress
  2814. (function
  2815. ;; Read a character from command input, toggling interactive
  2816. ;; options when applicable. PROMPT is the displayed prompt,
  2817. ;; as a string.
  2818. (lambda (prompt)
  2819. (let ((key (read-char-exclusive prompt)))
  2820. (cond
  2821. ;; Ignore non-standard characters (i.e. "M-a").
  2822. ((not (characterp key)) (org-export-dispatch-ui options expertp))
  2823. ;; Help key: Switch back to standard interface if
  2824. ;; expert UI was active.
  2825. ((eq key ??) (org-export-dispatch-ui options nil))
  2826. ;; Toggle export options.
  2827. ((memq key '(?1 ?2 ?3 ?4))
  2828. (org-export-dispatch-ui
  2829. (let ((option (case key (?1 'body) (?2 'subtree) (?3 'visible)
  2830. (?4 'force))))
  2831. (if (memq option options) (remq option options)
  2832. (cons option options)))
  2833. expertp))
  2834. ;; Action selected: Send key and options back to
  2835. ;; `org-export-dispatch'.
  2836. (t (cons key options))))))))
  2837. ;; With expert UI, just read key with a fancy prompt. In standard
  2838. ;; UI, display an intrusive help buffer.
  2839. (if expertp (funcall handle-keypress expert-prompt)
  2840. (save-window-excursion
  2841. (delete-other-windows)
  2842. (with-output-to-temp-buffer "*Org Export/Publishing Help*" (princ help))
  2843. (org-fit-window-to-buffer
  2844. (get-buffer-window "*Org Export/Publishing Help*"))
  2845. (funcall handle-keypress standard-prompt)))))
  2846. (provide 'org-export)
  2847. ;;; org-export.el ends here