1 ;;; ox.el --- Export Framework for Org Mode -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2018 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
25 ;; This library implements a generic export engine for Org, built on
26 ;; its syntactical parser: Org Elements.
28 ;; Besides that parser, the generic exporter is made of three distinct
31 ;; - The communication channel consists of a property list, which is
32 ;; created and updated during the process. Its use is to offer
33 ;; every piece of information, would it be about initial environment
34 ;; or contextual data, all in a single place.
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders. See "The Filter System"
45 ;; section for more information.
47 ;; The core functions is `org-export-as'. It returns the transcoded
48 ;; buffer as a string. Its derivatives are `org-export-to-buffer' and
49 ;; `org-export-to-file'.
51 ;; An export back-end is defined with `org-export-define-backend'.
52 ;; This function can also support specific buffer keywords, OPTION
53 ;; keyword's items and filters. Refer to function's documentation for
56 ;; If the new back-end shares most properties with another one,
57 ;; `org-export-define-derived-backend' can be used to simplify the
60 ;; Any back-end can define its own variables. Among them, those
61 ;; customizable should belong to the `org-export-BACKEND' group.
63 ;; Tools for common tasks across back-ends are implemented in the
64 ;; following part of the file.
66 ;; Eventually, a dispatcher (`org-export-dispatch') is provided in the
69 ;; See <https://orgmode.org/worg/dev/org-export-reference.html> for
76 (require 'org-element
)
78 (require 'tabulated-list
)
80 (declare-function org-src-coderef-format
"org-src" (&optional element
))
81 (declare-function org-src-coderef-regexp
"org-src" (fmt &optional label
))
82 (declare-function org-publish
"ox-publish" (project &optional force async
))
83 (declare-function org-publish-all
"ox-publish" (&optional force async
))
84 (declare-function org-publish-current-file
"ox-publish" (&optional force async
))
85 (declare-function org-publish-current-project
"ox-publish" (&optional force async
))
87 (defvar org-publish-project-alist
)
88 (defvar org-table-number-fraction
)
89 (defvar org-table-number-regexp
)
92 ;;; Internal Variables
94 ;; Among internal variables, the most important is
95 ;; `org-export-options-alist'. This variable define the global export
96 ;; options, shared between every exporter, and how they are acquired.
98 (defconst org-export-max-depth
19
99 "Maximum nesting depth for headlines, counting from 0.")
101 (defconst org-export-options-alist
102 '((:title
"TITLE" nil nil parse
)
103 (:date
"DATE" nil nil parse
)
104 (:author
"AUTHOR" nil user-full-name parse
)
105 (:email
"EMAIL" nil user-mail-address t
)
106 (:language
"LANGUAGE" nil org-export-default-language t
)
107 (:select-tags
"SELECT_TAGS" nil org-export-select-tags split
)
108 (:exclude-tags
"EXCLUDE_TAGS" nil org-export-exclude-tags split
)
109 (:creator
"CREATOR" nil org-export-creator-string
)
110 (:headline-levels nil
"H" org-export-headline-levels
)
111 (:preserve-breaks nil
"\\n" org-export-preserve-breaks
)
112 (:section-numbers nil
"num" org-export-with-section-numbers
)
113 (:time-stamp-file nil
"timestamp" org-export-time-stamp-file
)
114 (:with-archived-trees nil
"arch" org-export-with-archived-trees
)
115 (:with-author nil
"author" org-export-with-author
)
116 (:with-broken-links nil
"broken-links" org-export-with-broken-links
)
117 (:with-clocks nil
"c" org-export-with-clocks
)
118 (:with-creator nil
"creator" org-export-with-creator
)
119 (:with-date nil
"date" org-export-with-date
)
120 (:with-drawers nil
"d" org-export-with-drawers
)
121 (:with-email nil
"email" org-export-with-email
)
122 (:with-emphasize nil
"*" org-export-with-emphasize
)
123 (:with-entities nil
"e" org-export-with-entities
)
124 (:with-fixed-width nil
":" org-export-with-fixed-width
)
125 (:with-footnotes nil
"f" org-export-with-footnotes
)
126 (:with-inlinetasks nil
"inline" org-export-with-inlinetasks
)
127 (:with-latex nil
"tex" org-export-with-latex
)
128 (:with-planning nil
"p" org-export-with-planning
)
129 (:with-priority nil
"pri" org-export-with-priority
)
130 (:with-properties nil
"prop" org-export-with-properties
)
131 (:with-smart-quotes nil
"'" org-export-with-smart-quotes
)
132 (:with-special-strings nil
"-" org-export-with-special-strings
)
133 (:with-statistics-cookies nil
"stat" org-export-with-statistics-cookies
)
134 (:with-sub-superscript nil
"^" org-export-with-sub-superscripts
)
135 (:with-toc nil
"toc" org-export-with-toc
)
136 (:with-tables nil
"|" org-export-with-tables
)
137 (:with-tags nil
"tags" org-export-with-tags
)
138 (:with-tasks nil
"tasks" org-export-with-tasks
)
139 (:with-timestamps nil
"<" org-export-with-timestamps
)
140 (:with-title nil
"title" org-export-with-title
)
141 (:with-todo-keywords nil
"todo" org-export-with-todo-keywords
))
142 "Alist between export properties and ways to set them.
144 The key of the alist is the property name, and the value is a list
145 like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
147 KEYWORD is a string representing a buffer keyword, or nil. Each
148 property defined this way can also be set, during subtree
149 export, through a headline property named after the keyword
150 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
152 OPTION is a string that could be found in an #+OPTIONS: line.
153 DEFAULT is the default value for the property.
154 BEHAVIOR determines how Org should handle multiple keywords for
155 the same property. It is a symbol among:
156 nil Keep old value and discard the new one.
157 t Replace old value with the new one.
158 `space' Concatenate the values, separating them with a space.
159 `newline' Concatenate the values, separating them with
161 `split' Split values at white spaces, and cons them to the
163 `parse' Parse value as a list of strings and Org objects,
164 which can then be transcoded with, e.g.,
165 `org-export-data'. It implies `space' behavior.
167 Values set through KEYWORD and OPTION have precedence over
170 All these properties should be back-end agnostic. Back-end
171 specific properties are set through `org-export-define-backend'.
172 Properties redefined there have precedence over these.")
174 (defconst org-export-special-keywords
'("FILETAGS" "SETUPFILE" "OPTIONS")
175 "List of in-buffer keywords that require special treatment.
176 These keywords are not directly associated to a property. The
177 way they are handled must be hard-coded into
178 `org-export--get-inbuffer-options' function.")
180 (defconst org-export-filters-alist
181 '((:filter-body . org-export-filter-body-functions
)
182 (:filter-bold . org-export-filter-bold-functions
)
183 (:filter-babel-call . org-export-filter-babel-call-functions
)
184 (:filter-center-block . org-export-filter-center-block-functions
)
185 (:filter-clock . org-export-filter-clock-functions
)
186 (:filter-code . org-export-filter-code-functions
)
187 (:filter-diary-sexp . org-export-filter-diary-sexp-functions
)
188 (:filter-drawer . org-export-filter-drawer-functions
)
189 (:filter-dynamic-block . org-export-filter-dynamic-block-functions
)
190 (:filter-entity . org-export-filter-entity-functions
)
191 (:filter-example-block . org-export-filter-example-block-functions
)
192 (:filter-export-block . org-export-filter-export-block-functions
)
193 (:filter-export-snippet . org-export-filter-export-snippet-functions
)
194 (:filter-final-output . org-export-filter-final-output-functions
)
195 (:filter-fixed-width . org-export-filter-fixed-width-functions
)
196 (:filter-footnote-definition . org-export-filter-footnote-definition-functions
)
197 (:filter-footnote-reference . org-export-filter-footnote-reference-functions
)
198 (:filter-headline . org-export-filter-headline-functions
)
199 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions
)
200 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions
)
201 (:filter-inline-src-block . org-export-filter-inline-src-block-functions
)
202 (:filter-inlinetask . org-export-filter-inlinetask-functions
)
203 (:filter-italic . org-export-filter-italic-functions
)
204 (:filter-item . org-export-filter-item-functions
)
205 (:filter-keyword . org-export-filter-keyword-functions
)
206 (:filter-latex-environment . org-export-filter-latex-environment-functions
)
207 (:filter-latex-fragment . org-export-filter-latex-fragment-functions
)
208 (:filter-line-break . org-export-filter-line-break-functions
)
209 (:filter-link . org-export-filter-link-functions
)
210 (:filter-node-property . org-export-filter-node-property-functions
)
211 (:filter-options . org-export-filter-options-functions
)
212 (:filter-paragraph . org-export-filter-paragraph-functions
)
213 (:filter-parse-tree . org-export-filter-parse-tree-functions
)
214 (:filter-plain-list . org-export-filter-plain-list-functions
)
215 (:filter-plain-text . org-export-filter-plain-text-functions
)
216 (:filter-planning . org-export-filter-planning-functions
)
217 (:filter-property-drawer . org-export-filter-property-drawer-functions
)
218 (:filter-quote-block . org-export-filter-quote-block-functions
)
219 (:filter-radio-target . org-export-filter-radio-target-functions
)
220 (:filter-section . org-export-filter-section-functions
)
221 (:filter-special-block . org-export-filter-special-block-functions
)
222 (:filter-src-block . org-export-filter-src-block-functions
)
223 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions
)
224 (:filter-strike-through . org-export-filter-strike-through-functions
)
225 (:filter-subscript . org-export-filter-subscript-functions
)
226 (:filter-superscript . org-export-filter-superscript-functions
)
227 (:filter-table . org-export-filter-table-functions
)
228 (:filter-table-cell . org-export-filter-table-cell-functions
)
229 (:filter-table-row . org-export-filter-table-row-functions
)
230 (:filter-target . org-export-filter-target-functions
)
231 (:filter-timestamp . org-export-filter-timestamp-functions
)
232 (:filter-underline . org-export-filter-underline-functions
)
233 (:filter-verbatim . org-export-filter-verbatim-functions
)
234 (:filter-verse-block . org-export-filter-verse-block-functions
))
235 "Alist between filters properties and initial values.
237 The key of each association is a property name accessible through
238 the communication channel. Its value is a configurable global
239 variable defining initial filters.
241 This list is meant to install user specified filters. Back-end
242 developers may install their own filters using
243 `org-export-define-backend'. Filters defined there will always
244 be prepended to the current list, so they always get applied
247 (defconst org-export-default-inline-image-rule
251 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
252 "xpm" "pbm" "pgm" "ppm") t
))))
253 "Default rule for link matching an inline image.
254 This rule applies to links with no description. By default, it
255 will be considered as an inline image if it targets a local file
256 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
257 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
258 See `org-export-inline-image-p' for more information about
261 (defconst org-export-ignored-local-variables
262 '(org-font-lock-keywords
263 org-element--cache org-element--cache-objects org-element--cache-sync-keys
264 org-element--cache-sync-requests org-element--cache-sync-timer
)
265 "List of variables not copied through upon buffer duplication.
266 Export process takes place on a copy of the original buffer.
267 When this copy is created, all Org related local variables not in
268 this list are copied to the new buffer. Variables with an
269 unreadable value are also ignored.")
271 (defvar org-export-async-debug nil
272 "Non-nil means asynchronous export process should leave data behind.
274 This data is found in the appropriate \"*Org Export Process*\"
275 buffer, and in files prefixed with \"org-export-process\" and
276 located in `temporary-file-directory'.
278 When non-nil, it will also set `debug-on-error' to a non-nil
279 value in the external process.")
281 (defvar org-export-stack-contents nil
282 "Record asynchronously generated export results and processes.
283 This is an alist: its CAR is the source of the
284 result (destination file or buffer for a finished process,
285 original buffer for a running one) and its CDR is a list
286 containing the back-end used, as a symbol, and either a process
287 or the time at which it finished. It is used to build the menu
288 from `org-export-stack'.")
290 (defvar org-export-registered-backends nil
291 "List of backends currently available in the exporter.
292 This variable is set with `org-export-define-backend' and
293 `org-export-define-derived-backend' functions.")
295 (defvar org-export-dispatch-last-action nil
296 "Last command called from the dispatcher.
297 The value should be a list. Its CAR is the action, as a symbol,
298 and its CDR is a list of export options.")
300 (defvar org-export-dispatch-last-position
(make-marker)
301 "The position where the last export command was created using the dispatcher.
302 This marker will be used with `C-u C-c C-e' to make sure export repetition
303 uses the same subtree if the previous command was restricted to a subtree.")
305 ;; For compatibility with Org < 8
306 (defvar org-export-current-backend nil
307 "Name, if any, of the back-end used during an export process.
309 Its value is a symbol such as `html', `latex', `ascii', or nil if
310 the back-end is anonymous (see `org-export-create-backend') or if
311 there is no export process in progress.
313 It can be used to teach Babel blocks how to act differently
314 according to the back-end used.")
318 ;;; User-configurable Variables
320 ;; Configuration for the masses.
322 ;; They should never be accessed directly, as their value is to be
323 ;; stored in a property list (cf. `org-export-options-alist').
324 ;; Back-ends will read their value from there instead.
326 (defgroup org-export nil
327 "Options for exporting Org mode files."
331 (defgroup org-export-general nil
332 "General options for export engine."
333 :tag
"Org Export General"
336 (defcustom org-export-with-archived-trees
'headline
337 "Whether sub-trees with the ARCHIVE tag should be exported.
339 This can have three different values:
340 nil Do not export, pretend this tree is not present.
341 t Do export the entire tree.
342 `headline' Only export the headline, but skip the tree below it.
344 This option can also be set with the OPTIONS keyword,
346 :group
'org-export-general
348 (const :tag
"Not at all" nil
)
349 (const :tag
"Headline only" headline
)
350 (const :tag
"Entirely" t
))
351 :safe
(lambda (x) (memq x
'(t nil headline
))))
353 (defcustom org-export-with-author t
354 "Non-nil means insert author name into the exported file.
355 This option can also be set with the OPTIONS keyword,
356 e.g. \"author:nil\"."
357 :group
'org-export-general
361 (defcustom org-export-with-clocks nil
362 "Non-nil means export CLOCK keywords.
363 This option can also be set with the OPTIONS keyword,
365 :group
'org-export-general
369 (defcustom org-export-with-creator nil
370 "Non-nil means the postamble should contain a creator sentence.
372 The sentence can be set in `org-export-creator-string', which
375 This option can also be set with the OPTIONS keyword, e.g.,
377 :group
'org-export-general
379 :package-version
'(Org .
"8.3")
383 (defcustom org-export-with-date t
384 "Non-nil means insert date in the exported document.
385 This option can also be set with the OPTIONS keyword,
387 :group
'org-export-general
391 (defcustom org-export-date-timestamp-format nil
392 "Time-stamp format string to use for DATE keyword.
394 The format string, when specified, only applies if date consists
395 in a single time-stamp. Otherwise its value will be ignored.
397 See `format-time-string' for details on how to build this
399 :group
'org-export-general
401 (string :tag
"Time-stamp format string")
402 (const :tag
"No format string" nil
))
403 :safe
(lambda (x) (or (null x
) (stringp x
))))
405 (defcustom org-export-creator-string
406 (format "Emacs %s (Org mode %s)"
408 (if (fboundp 'org-version
) (org-version) "unknown version"))
409 "Information about the creator of the document.
410 This option can also be set on with the CREATOR keyword."
411 :group
'org-export-general
412 :type
'(string :tag
"Creator string")
415 (defcustom org-export-with-drawers
'(not "LOGBOOK")
416 "Non-nil means export contents of standard drawers.
418 When t, all drawers are exported. This may also be a list of
419 drawer names to export, as strings. If that list starts with
420 `not', only drawers with such names will be ignored.
422 This variable doesn't apply to properties drawers. See
423 `org-export-with-properties' instead.
425 This option can also be set with the OPTIONS keyword,
427 :group
'org-export-general
429 :package-version
'(Org .
"8.0")
431 (const :tag
"All drawers" t
)
432 (const :tag
"None" nil
)
433 (repeat :tag
"Selected drawers"
434 (string :tag
"Drawer name"))
435 (list :tag
"Ignored drawers"
436 (const :format
"" not
)
437 (repeat :tag
"Specify names of drawers to ignore during export"
439 (string :tag
"Drawer name"))))
440 :safe
(lambda (x) (or (booleanp x
) (consp x
))))
442 (defcustom org-export-with-email nil
443 "Non-nil means insert author email into the exported file.
444 This option can also be set with the OPTIONS keyword,
446 :group
'org-export-general
450 (defcustom org-export-with-emphasize t
451 "Non-nil means interpret *word*, /word/, _word_ and +word+.
453 If the export target supports emphasizing text, the word will be
454 typeset in bold, italic, with an underline or strike-through,
457 This option can also be set with the OPTIONS keyword,
459 :group
'org-export-general
463 (defcustom org-export-exclude-tags
'("noexport")
464 "Tags that exclude a tree from export.
466 All trees carrying any of these tags will be excluded from
467 export. This is without condition, so even subtrees inside that
468 carry one of the `org-export-select-tags' will be removed.
470 This option can also be set with the EXCLUDE_TAGS keyword."
471 :group
'org-export-general
472 :type
'(repeat (string :tag
"Tag"))
473 :safe
(lambda (x) (and (listp x
) (cl-every #'stringp x
))))
475 (defcustom org-export-with-fixed-width t
476 "Non-nil means export lines starting with \":\".
477 This option can also be set with the OPTIONS keyword,
479 :group
'org-export-general
481 :package-version
'(Org .
"8.0")
485 (defcustom org-export-with-footnotes t
486 "Non-nil means Org footnotes should be exported.
487 This option can also be set with the OPTIONS keyword,
489 :group
'org-export-general
493 (defcustom org-export-with-latex t
494 "Non-nil means process LaTeX environments and fragments.
496 This option can also be set with the OPTIONS line,
497 e.g. \"tex:verbatim\". Allowed values are:
499 nil Ignore math snippets.
500 `verbatim' Keep everything in verbatim.
501 t Allow export of math snippets."
502 :group
'org-export-general
504 :package-version
'(Org .
"8.0")
506 (const :tag
"Do not process math in any way" nil
)
507 (const :tag
"Interpret math snippets" t
)
508 (const :tag
"Leave math verbatim" verbatim
))
509 :safe
(lambda (x) (memq x
'(t nil verbatim
))))
511 (defcustom org-export-headline-levels
3
512 "The last level which is still exported as a headline.
514 Inferior levels will usually produce itemize or enumerate lists
515 when exported, but back-end behavior may differ.
517 This option can also be set with the OPTIONS keyword,
519 :group
'org-export-general
523 (defcustom org-export-default-language
"en"
524 "The default language for export and clocktable translations, as a string.
525 This may have an association in
526 `org-clock-clocktable-language-setup',
527 `org-export-smart-quotes-alist' and `org-export-dictionary'.
528 This option can also be set with the LANGUAGE keyword."
529 :group
'org-export-general
530 :type
'(string :tag
"Language")
533 (defcustom org-export-preserve-breaks nil
534 "Non-nil means preserve all line breaks when exporting.
535 This option can also be set with the OPTIONS keyword,
537 :group
'org-export-general
541 (defcustom org-export-with-entities t
542 "Non-nil means interpret entities when exporting.
544 For example, HTML export converts \\alpha to α and \\AA to
547 For a list of supported names, see the constant `org-entities'
548 and the user option `org-entities-user'.
550 This option can also be set with the OPTIONS keyword,
552 :group
'org-export-general
556 (defcustom org-export-with-inlinetasks t
557 "Non-nil means inlinetasks should be exported.
558 This option can also be set with the OPTIONS keyword,
559 e.g. \"inline:nil\"."
560 :group
'org-export-general
562 :package-version
'(Org .
"8.0")
566 (defcustom org-export-with-planning nil
567 "Non-nil means include planning info in export.
569 Planning info is the line containing either SCHEDULED:,
570 DEADLINE:, CLOSED: time-stamps, or a combination of them.
572 This option can also be set with the OPTIONS keyword,
574 :group
'org-export-general
576 :package-version
'(Org .
"8.0")
580 (defcustom org-export-with-priority nil
581 "Non-nil means include priority cookies in export.
582 This option can also be set with the OPTIONS keyword,
584 :group
'org-export-general
588 (defcustom org-export-with-properties nil
589 "Non-nil means export contents of properties drawers.
591 When t, all properties are exported. This may also be a list of
592 properties to export, as strings.
594 This option can also be set with the OPTIONS keyword,
596 :group
'org-export-general
598 :package-version
'(Org .
"8.3")
600 (const :tag
"All properties" t
)
601 (const :tag
"None" nil
)
602 (repeat :tag
"Selected properties"
603 (string :tag
"Property name")))
604 :safe
(lambda (x) (or (booleanp x
)
605 (and (listp x
) (cl-every #'stringp x
)))))
607 (defcustom org-export-with-section-numbers t
608 "Non-nil means add section numbers to headlines when exporting.
610 When set to an integer n, numbering will only happen for
611 headlines whose relative level is higher or equal to n.
613 This option can also be set with the OPTIONS keyword,
615 :group
'org-export-general
619 (defcustom org-export-select-tags
'("export")
620 "Tags that select a tree for export.
622 If any such tag is found in a buffer, all trees that do not carry
623 one of these tags will be ignored during export. Inside trees
624 that are selected like this, you can still deselect a subtree by
625 tagging it with one of the `org-export-exclude-tags'.
627 This option can also be set with the SELECT_TAGS keyword."
628 :group
'org-export-general
629 :type
'(repeat (string :tag
"Tag"))
630 :safe
(lambda (x) (and (listp x
) (cl-every #'stringp x
))))
632 (defcustom org-export-with-smart-quotes nil
633 "Non-nil means activate smart quotes during export.
634 This option can also be set with the OPTIONS keyword,
637 When setting this to non-nil, you need to take care of
638 using the correct Babel package when exporting to LaTeX.
639 E.g., you can load Babel for french like this:
641 #+LATEX_HEADER: \\usepackage[french]{babel}"
642 :group
'org-export-general
644 :package-version
'(Org .
"8.0")
648 (defcustom org-export-with-special-strings t
649 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
651 When this option is turned on, these strings will be exported as:
654 -----+----------+--------+-------
658 ... … \\ldots …
660 This option can also be set with the OPTIONS keyword,
662 :group
'org-export-general
666 (defcustom org-export-with-statistics-cookies t
667 "Non-nil means include statistics cookies in export.
668 This option can also be set with the OPTIONS keyword,
670 :group
'org-export-general
672 :package-version
'(Org .
"8.0")
676 (defcustom org-export-with-sub-superscripts t
677 "Non-nil means interpret \"_\" and \"^\" for export.
679 If you want to control how Org displays those characters, see
680 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
681 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
684 When this option is turned on, you can use TeX-like syntax for
685 sub- and superscripts and see them exported correctly.
687 You can also set the option with #+OPTIONS: ^:t
689 Several characters after \"_\" or \"^\" will be considered as a
690 single item - so grouping with {} is normally not needed. For
691 example, the following things will be parsed as single sub- or
694 10^24 or 10^tau several digits will be considered 1 item.
695 10^-12 or 10^-tau a leading sign with digits or a word
696 x^2-y^3 will be read as x^2 - y^3, because items are
697 terminated by almost any nonword/nondigit char.
698 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
700 Still, ambiguity is possible. So when in doubt, use {} to enclose
701 the sub/superscript. If you set this variable to the symbol `{}',
702 the braces are *required* in order to trigger interpretations as
703 sub/superscript. This can be helpful in documents that need \"_\"
704 frequently in plain text."
705 :group
'org-export-general
707 :package-version
'(Org .
"8.0")
709 (const :tag
"Interpret them" t
)
710 (const :tag
"Curly brackets only" {})
711 (const :tag
"Do not interpret them" nil
))
712 :safe
(lambda (x) (memq x
'(t nil
{}))))
714 (defcustom org-export-with-toc t
715 "Non-nil means create a table of contents in exported files.
717 The table of contents contains headlines with levels up to
718 `org-export-headline-levels'.
720 When this variable is set to an integer N, include levels up to
721 N in the table of contents. Although it may then be different
722 from `org-export-headline-levels', it is cannot be larger than
723 the number of headline levels.
725 When nil, no table of contents is created.
727 This option can also be set with the OPTIONS keyword,
728 e.g. \"toc:nil\" or \"toc:3\"."
729 :group
'org-export-general
731 (const :tag
"No Table of Contents" nil
)
732 (const :tag
"Full Table of Contents" t
)
733 (integer :tag
"TOC to level"))
738 (defcustom org-export-with-tables t
739 "Non-nil means export tables.
740 This option can also be set with the OPTIONS keyword,
742 :group
'org-export-general
744 :package-version
'(Org .
"8.0")
748 (defcustom org-export-with-tags t
749 "If nil, do not export tags, just remove them from headlines.
751 If this is the symbol `not-in-toc', tags will be removed from
752 table of contents entries, but still be shown in the headlines of
755 This option can also be set with the OPTIONS keyword,
757 :group
'org-export-general
759 (const :tag
"Off" nil
)
760 (const :tag
"Not in TOC" not-in-toc
)
762 :safe
(lambda (x) (memq x
'(t nil not-in-toc
))))
764 (defcustom org-export-with-tasks t
765 "Non-nil means include TODO items for export.
767 This may have the following values:
768 t include tasks independent of state.
769 `todo' include only tasks that are not yet done.
770 `done' include only tasks that are already done.
771 nil ignore all tasks.
772 list of keywords include tasks with these keywords.
774 This option can also be set with the OPTIONS keyword,
776 :group
'org-export-general
778 (const :tag
"All tasks" t
)
779 (const :tag
"No tasks" nil
)
780 (const :tag
"Not-done tasks" todo
)
781 (const :tag
"Only done tasks" done
)
782 (repeat :tag
"Specific TODO keywords"
783 (string :tag
"Keyword")))
784 :safe
(lambda (x) (or (memq x
'(nil t todo done
))
786 (cl-every #'stringp x
)))))
788 (defcustom org-export-with-title t
789 "Non-nil means print title into the exported file.
790 This option can also be set with the OPTIONS keyword,
792 :group
'org-export-general
794 :package-version
'(Org .
"8.3")
798 (defcustom org-export-time-stamp-file t
799 "Non-nil means insert a time stamp into the exported file.
800 The time stamp shows when the file was created. This option can
801 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
802 :group
'org-export-general
806 (defcustom org-export-with-timestamps t
807 "Non nil means allow timestamps in export.
809 It can be set to any of the following values:
810 t export all timestamps.
811 `active' export active timestamps only.
812 `inactive' export inactive timestamps only.
813 nil do not export timestamps
815 This only applies to timestamps isolated in a paragraph
816 containing only timestamps. Other timestamps are always
819 This option can also be set with the OPTIONS keyword, e.g.
821 :group
'org-export-general
823 (const :tag
"All timestamps" t
)
824 (const :tag
"Only active timestamps" active
)
825 (const :tag
"Only inactive timestamps" inactive
)
826 (const :tag
"No timestamp" nil
))
827 :safe
(lambda (x) (memq x
'(t nil active inactive
))))
829 (defcustom org-export-with-todo-keywords t
830 "Non-nil means include TODO keywords in export.
831 When nil, remove all these keywords from the export. This option
832 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
833 :group
'org-export-general
836 (defcustom org-export-allow-bind-keywords nil
837 "Non-nil means BIND keywords can define local variable values.
838 This is a potential security risk, which is why the default value
839 is nil. You can also allow them through local buffer variables."
840 :group
'org-export-general
842 :package-version
'(Org .
"8.0")
845 (defcustom org-export-with-broken-links nil
846 "Non-nil means do not raise an error on broken links.
848 When this variable is non-nil, broken links are ignored, without
849 stopping the export process. If it is set to `mark', broken
850 links are marked as such in the output, with a string like
854 where PATH is the un-resolvable reference.
856 This option can also be set with the OPTIONS keyword, e.g.,
857 \"broken-links:mark\"."
858 :group
'org-export-general
860 :package-version
'(Org .
"9.0")
862 (const :tag
"Ignore broken links" t
)
863 (const :tag
"Mark broken links in output" mark
)
864 (const :tag
"Raise an error" nil
)))
866 (defcustom org-export-snippet-translation-alist nil
867 "Alist between export snippets back-ends and exporter back-ends.
869 This variable allows providing shortcuts for export snippets.
871 For example, with a value of \\='((\"h\" . \"html\")), the
872 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
873 HTML code while every other back-end will ignore it."
874 :group
'org-export-general
876 :package-version
'(Org .
"8.0")
878 (cons (string :tag
"Shortcut")
879 (string :tag
"Back-end")))
883 (cl-every #'stringp
(mapcar #'car x
))
884 (cl-every #'stringp
(mapcar #'cdr x
)))))
886 (defcustom org-export-global-macros nil
887 "Alist between macro names and expansion templates.
889 This variable defines macro expansion templates available
890 globally. Associations follow the pattern
894 where NAME is a string beginning with a letter and consisting of
895 alphanumeric characters only.
897 TEMPLATE is the string to which the macro is going to be
898 expanded. Inside, \"$1\", \"$2\"... are place-holders for
899 macro's arguments. Moreover, if the template starts with
900 \"(eval\", it will be parsed as an Elisp expression and evaluated
902 :group
'org-export-general
904 :package-version
'(Org .
"9.1")
906 (cons (string :tag
"Name")
907 (string :tag
"Template"))))
909 (defcustom org-export-coding-system nil
910 "Coding system for the exported file."
911 :group
'org-export-general
913 :package-version
'(Org .
"8.0")
914 :type
'coding-system
)
916 (defcustom org-export-copy-to-kill-ring nil
917 "Non-nil means pushing export output to the kill ring.
918 This variable is ignored during asynchronous export."
919 :group
'org-export-general
921 :package-version
'(Org .
"8.3")
923 (const :tag
"Always" t
)
924 (const :tag
"When export is done interactively" if-interactive
)
925 (const :tag
"Never" nil
)))
927 (defcustom org-export-initial-scope
'buffer
928 "The initial scope when exporting with `org-export-dispatch'.
929 This variable can be either set to `buffer' or `subtree'."
930 :group
'org-export-general
932 (const :tag
"Export current buffer" buffer
)
933 (const :tag
"Export current subtree" subtree
)))
935 (defcustom org-export-show-temporary-export-buffer t
936 "Non-nil means show buffer after exporting to temp buffer.
937 When Org exports to a file, the buffer visiting that file is never
938 shown, but remains buried. However, when exporting to
939 a temporary buffer, that buffer is popped up in a second window.
940 When this variable is nil, the buffer remains buried also in
942 :group
'org-export-general
945 (defcustom org-export-in-background nil
946 "Non-nil means export and publishing commands will run in background.
947 Results from an asynchronous export are never displayed
948 automatically. But you can retrieve them with `\\[org-export-stack]'."
949 :group
'org-export-general
951 :package-version
'(Org .
"8.0")
954 (defcustom org-export-async-init-file nil
955 "File used to initialize external export process.
957 Value must be either nil or an absolute file name. When nil, the
958 external process is launched like a regular Emacs session,
959 loading user's initialization file and any site specific
960 configuration. If a file is provided, it, and only it, is loaded
963 Therefore, using a specific configuration makes the process to
964 load faster and the export more portable."
965 :group
'org-export-general
967 :package-version
'(Org .
"8.0")
969 (const :tag
"Regular startup" nil
)
970 (file :tag
"Specific start-up file" :must-match t
)))
972 (defcustom org-export-dispatch-use-expert-ui nil
973 "Non-nil means using a non-intrusive `org-export-dispatch'.
974 In that case, no help buffer is displayed. Though, an indicator
975 for current export scope is added to the prompt (\"b\" when
976 output is restricted to body only, \"s\" when it is restricted to
977 the current subtree, \"v\" when only visible elements are
978 considered for export, \"f\" when publishing functions should be
979 passed the FORCE argument and \"a\" when the export should be
980 asynchronous). Also, [?] allows switching back to standard
982 :group
'org-export-general
984 :package-version
'(Org .
"8.0")
989 ;;; Defining Back-ends
991 ;; An export back-end is a structure with `org-export-backend' type
992 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
995 ;; At the lowest level, a back-end is created with
996 ;; `org-export-create-backend' function.
998 ;; A named back-end can be registered with
999 ;; `org-export-register-backend' function. A registered back-end can
1000 ;; later be referred to by its name, with `org-export-get-backend'
1001 ;; function. Also, such a back-end can become the parent of a derived
1002 ;; back-end from which slot values will be inherited by default.
1003 ;; `org-export-derived-backend-p' can check if a given back-end is
1004 ;; derived from a list of back-end names.
1006 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
1007 ;; `org-export-get-all-filters' return the full alist of transcoders,
1008 ;; options and filters, including those inherited from ancestors.
1010 ;; At a higher level, `org-export-define-backend' is the standard way
1011 ;; to define an export back-end. If the new back-end is similar to
1012 ;; a registered back-end, `org-export-define-derived-backend' may be
1015 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
1016 ;; when a given back-end hasn't been registered yet.
1018 (cl-defstruct (org-export-backend (:constructor org-export-create-backend
)
1020 name parent transcoders options filters blocks menu
)
1023 (defun org-export-get-backend (name)
1024 "Return export back-end named after NAME.
1025 NAME is a symbol. Return nil if no such back-end is found."
1026 (cl-find-if (lambda (b) (and (eq name
(org-export-backend-name b
))))
1027 org-export-registered-backends
))
1029 (defun org-export-register-backend (backend)
1030 "Register BACKEND as a known export back-end.
1031 BACKEND is a structure with `org-export-backend' type."
1032 ;; Refuse to register an unnamed back-end.
1033 (unless (org-export-backend-name backend
)
1034 (error "Cannot register a unnamed export back-end"))
1035 ;; Refuse to register a back-end with an unknown parent.
1036 (let ((parent (org-export-backend-parent backend
)))
1037 (when (and parent
(not (org-export-get-backend parent
)))
1038 (error "Cannot use unknown \"%s\" back-end as a parent" parent
)))
1039 ;; If a back-end with the same name as BACKEND is already
1040 ;; registered, replace it with BACKEND. Otherwise, simply add
1041 ;; BACKEND to the list of registered back-ends.
1042 (let ((old (org-export-get-backend (org-export-backend-name backend
))))
1043 (if old
(setcar (memq old org-export-registered-backends
) backend
)
1044 (push backend org-export-registered-backends
))))
1046 (defun org-export-barf-if-invalid-backend (backend)
1047 "Signal an error if BACKEND isn't defined."
1048 (unless (org-export-backend-p backend
)
1049 (error "Unknown \"%s\" back-end: Aborting export" backend
)))
1051 (defun org-export-derived-backend-p (backend &rest backends
)
1052 "Non-nil if BACKEND is derived from one of BACKENDS.
1053 BACKEND is an export back-end, as returned by, e.g.,
1054 `org-export-create-backend', or a symbol referring to
1055 a registered back-end. BACKENDS is constituted of symbols."
1056 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1059 (while (org-export-backend-parent backend
)
1060 (when (memq (org-export-backend-name backend
) backends
)
1063 (org-export-get-backend (org-export-backend-parent backend
))))
1064 (memq (org-export-backend-name backend
) backends
))))
1066 (defun org-export-get-all-transcoders (backend)
1067 "Return full translation table for BACKEND.
1069 BACKEND is an export back-end, as return by, e.g,,
1070 `org-export-create-backend'. Return value is an alist where
1071 keys are element or object types, as symbols, and values are
1074 Unlike to `org-export-backend-transcoders', this function
1075 also returns transcoders inherited from parent back-ends,
1077 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1079 (let ((transcoders (org-export-backend-transcoders backend
))
1081 (while (setq parent
(org-export-backend-parent backend
))
1082 (setq backend
(org-export-get-backend parent
))
1084 (append transcoders
(org-export-backend-transcoders backend
))))
1087 (defun org-export-get-all-options (backend)
1088 "Return export options for BACKEND.
1090 BACKEND is an export back-end, as return by, e.g,,
1091 `org-export-create-backend'. See `org-export-options-alist'
1092 for the shape of the return value.
1094 Unlike to `org-export-backend-options', this function also
1095 returns options inherited from parent back-ends, if any.
1097 Return nil if BACKEND is unknown."
1098 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1100 (let ((options (org-export-backend-options backend
))
1102 (while (setq parent
(org-export-backend-parent backend
))
1103 (setq backend
(org-export-get-backend parent
))
1104 (setq options
(append options
(org-export-backend-options backend
))))
1107 (defun org-export-get-all-filters (backend)
1108 "Return complete list of filters for BACKEND.
1110 BACKEND is an export back-end, as return by, e.g,,
1111 `org-export-create-backend'. Return value is an alist where
1112 keys are symbols and values lists of functions.
1114 Unlike to `org-export-backend-filters', this function also
1115 returns filters inherited from parent back-ends, if any."
1116 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
1118 (let ((filters (org-export-backend-filters backend
))
1120 (while (setq parent
(org-export-backend-parent backend
))
1121 (setq backend
(org-export-get-backend parent
))
1122 (setq filters
(append filters
(org-export-backend-filters backend
))))
1125 (defun org-export-define-backend (backend transcoders
&rest body
)
1126 "Define a new back-end BACKEND.
1128 TRANSCODERS is an alist between object or element types and
1129 functions handling them.
1131 These functions should return a string without any trailing
1132 space, or nil. They must accept three arguments: the object or
1133 element itself, its contents or nil when it isn't recursive and
1134 the property list used as a communication channel.
1136 Contents, when not nil, are stripped from any global indentation
1137 \(although the relative one is preserved). They also always end
1138 with a single newline character.
1140 If, for a given type, no function is found, that element or
1141 object type will simply be ignored, along with any blank line or
1142 white space at its end. The same will happen if the function
1143 returns the nil value. If that function returns the empty
1144 string, the type will be ignored, but the blank lines or white
1145 spaces will be kept.
1147 In addition to element and object types, one function can be
1148 associated to the `template' (or `inner-template') symbol and
1149 another one to the `plain-text' symbol.
1151 The former returns the final transcoded string, and can be used
1152 to add a preamble and a postamble to document's body. It must
1153 accept two arguments: the transcoded string and the property list
1154 containing export options. A function associated to `template'
1155 will not be applied if export has option \"body-only\".
1156 A function associated to `inner-template' is always applied.
1158 The latter, when defined, is to be called on every text not
1159 recognized as an element or an object. It must accept two
1160 arguments: the text string and the information channel. It is an
1161 appropriate place to protect special chars relative to the
1164 BODY can start with pre-defined keyword arguments. The following
1165 keywords are understood:
1169 Alist between filters and function, or list of functions,
1170 specific to the back-end. See `org-export-filters-alist' for
1171 a list of all allowed filters. Filters defined here
1172 shouldn't make a back-end test, as it may prevent back-ends
1173 derived from this one to behave properly.
1177 Menu entry for the export dispatcher. It should be a list
1180 \\='(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1184 KEY is a free character selecting the back-end.
1186 DESCRIPTION-OR-ORDINAL is either a string or a number.
1188 If it is a string, is will be used to name the back-end in
1189 its menu entry. If it is a number, the following menu will
1190 be displayed as a sub-menu of the back-end with the same
1191 KEY. Also, the number will be used to determine in which
1192 order such sub-menus will appear (lowest first).
1194 ACTION-OR-MENU is either a function or an alist.
1196 If it is an action, it will be called with four
1197 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1198 BODY-ONLY. See `org-export-as' for further explanations on
1201 If it is an alist, associations should follow the
1204 \\='(KEY DESCRIPTION ACTION)
1206 where KEY, DESCRIPTION and ACTION are described above.
1208 Valid values include:
1210 \\='(?m \"My Special Back-end\" my-special-export-function)
1214 \\='(?l \"Export to LaTeX\"
1215 (?p \"As PDF file\" org-latex-export-to-pdf)
1216 (?o \"As PDF file and open\"
1218 (if a (org-latex-export-to-pdf t s v b)
1220 (org-latex-export-to-pdf nil s v b)))))))
1222 or the following, which will be added to the previous
1226 ((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1227 (?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1231 Alist between back-end specific properties introduced in
1232 communication channel and how their value are acquired. See
1233 `org-export-options-alist' for more information about
1234 structure of the values."
1235 (declare (indent 1))
1236 (let (filters menu-entry options
)
1237 (while (keywordp (car body
))
1238 (let ((keyword (pop body
)))
1240 (:filters-alist
(setq filters
(pop body
)))
1241 (:menu-entry
(setq menu-entry
(pop body
)))
1242 (:options-alist
(setq options
(pop body
)))
1243 (_ (error "Unknown keyword: %s" keyword
)))))
1244 (org-export-register-backend
1245 (org-export-create-backend :name backend
1246 :transcoders transcoders
1249 :menu menu-entry
))))
1251 (defun org-export-define-derived-backend (child parent
&rest body
)
1252 "Create a new back-end as a variant of an existing one.
1254 CHILD is the name of the derived back-end. PARENT is the name of
1255 the parent back-end.
1257 BODY can start with pre-defined keyword arguments. The following
1258 keywords are understood:
1262 Alist of filters that will overwrite or complete filters
1263 defined in PARENT back-end. See `org-export-filters-alist'
1264 for a list of allowed filters.
1268 Menu entry for the export dispatcher. See
1269 `org-export-define-backend' for more information about the
1274 Alist of back-end specific properties that will overwrite or
1275 complete those defined in PARENT back-end. Refer to
1276 `org-export-options-alist' for more information about
1277 structure of the values.
1281 Alist of element and object types and transcoders that will
1282 overwrite or complete transcode table from PARENT back-end.
1283 Refer to `org-export-define-backend' for detailed information
1286 As an example, here is how one could define \"my-latex\" back-end
1287 as a variant of `latex' back-end with a custom template function:
1289 (org-export-define-derived-backend \\='my-latex \\='latex
1290 :translate-alist \\='((template . my-latex-template-fun)))
1292 The back-end could then be called with, for example:
1294 (org-export-to-buffer \\='my-latex \"*Test my-latex*\")"
1295 (declare (indent 2))
1296 (let (filters menu-entry options transcoders
)
1297 (while (keywordp (car body
))
1298 (let ((keyword (pop body
)))
1300 (:filters-alist
(setq filters
(pop body
)))
1301 (:menu-entry
(setq menu-entry
(pop body
)))
1302 (:options-alist
(setq options
(pop body
)))
1303 (:translate-alist
(setq transcoders
(pop body
)))
1304 (_ (error "Unknown keyword: %s" keyword
)))))
1305 (org-export-register-backend
1306 (org-export-create-backend :name child
1308 :transcoders transcoders
1311 :menu menu-entry
))))
1315 ;;; The Communication Channel
1317 ;; During export process, every function has access to a number of
1318 ;; properties. They are of two types:
1320 ;; 1. Environment options are collected once at the very beginning of
1321 ;; the process, out of the original buffer and configuration.
1322 ;; Collecting them is handled by `org-export-get-environment'
1325 ;; Most environment options are defined through the
1326 ;; `org-export-options-alist' variable.
1328 ;; 2. Tree properties are extracted directly from the parsed tree,
1329 ;; just before export, by `org-export--collect-tree-properties'.
1331 ;;;; Environment Options
1333 ;; Environment options encompass all parameters defined outside the
1334 ;; scope of the parsed data. They come from five sources, in
1335 ;; increasing precedence order:
1337 ;; - Global variables,
1338 ;; - Buffer's attributes,
1339 ;; - Options keyword symbols,
1340 ;; - Buffer keywords,
1341 ;; - Subtree properties.
1343 ;; The central internal function with regards to environment options
1344 ;; is `org-export-get-environment'. It updates global variables with
1345 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1346 ;; the different sources.
1348 ;; The internal functions doing the retrieval are:
1349 ;; `org-export--get-global-options',
1350 ;; `org-export--get-buffer-attributes',
1351 ;; `org-export--parse-option-keyword',
1352 ;; `org-export--get-subtree-options' and
1353 ;; `org-export--get-inbuffer-options'
1355 ;; Also, `org-export--list-bound-variables' collects bound variables
1356 ;; along with their value in order to set them as buffer local
1357 ;; variables later in the process.
1360 (defun org-export-get-environment (&optional backend subtreep ext-plist
)
1361 "Collect export options from the current buffer.
1363 Optional argument BACKEND is an export back-end, as returned by
1364 `org-export-create-backend'.
1366 When optional argument SUBTREEP is non-nil, assume the export is
1367 done against the current sub-tree.
1369 Third optional argument EXT-PLIST is a property list with
1370 external parameters overriding Org default settings, but still
1371 inferior to file-local settings."
1372 ;; First install #+BIND variables since these must be set before
1373 ;; global options are read.
1374 (dolist (pair (org-export--list-bound-variables))
1375 (set (make-local-variable (car pair
)) (nth 1 pair
)))
1376 ;; Get and prioritize export options...
1378 ;; ... from global variables...
1379 (org-export--get-global-options backend
)
1380 ;; ... from an external property list...
1382 ;; ... from in-buffer settings...
1383 (org-export--get-inbuffer-options backend
)
1384 ;; ... and from subtree, when appropriate.
1385 (and subtreep
(org-export--get-subtree-options backend
))))
1387 (defun org-export--parse-option-keyword (options &optional backend
)
1388 "Parse an OPTIONS line and return values as a plist.
1389 Optional argument BACKEND is an export back-end, as returned by,
1390 e.g., `org-export-create-backend'. It specifies which back-end
1391 specific items to read, if any."
1394 (while (string-match "\\(.+?\\):\\((.*?)\\|\\S-*\\)[ \t]*" options s
)
1395 (setq s
(match-end 0))
1396 (push (cons (match-string 1 options
)
1397 (read (match-string 2 options
)))
1400 ;; Priority is given to back-end specific options.
1401 (all (append (org-export-get-all-options backend
)
1402 org-export-options-alist
))
1405 (dolist (entry all plist
)
1406 (let ((item (nth 2 entry
)))
1408 (let ((v (assoc-string item line t
)))
1409 (when v
(setq plist
(plist-put plist
(car entry
) (cdr v
)))))))))))
1411 (defun org-export--get-subtree-options (&optional backend
)
1412 "Get export options in subtree at point.
1413 Optional argument BACKEND is an export back-end, as returned by,
1414 e.g., `org-export-create-backend'. It specifies back-end used
1415 for export. Return options as a plist."
1416 ;; For each buffer keyword, create a headline property setting the
1417 ;; same property in communication channel. The name for the
1418 ;; property is the keyword with "EXPORT_" appended to it.
1419 (org-with-wide-buffer
1420 ;; Make sure point is at a heading.
1421 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t
))
1423 ;; EXPORT_OPTIONS are parsed in a non-standard way. Take
1424 ;; care of them right from the start.
1425 (let ((o (org-entry-get (point) "EXPORT_OPTIONS" 'selective
)))
1426 (and o
(org-export--parse-option-keyword o backend
))))
1427 ;; Take care of EXPORT_TITLE. If it isn't defined, use
1428 ;; headline's title (with no todo keyword, priority cookie or
1429 ;; tag) as its fallback value.
1432 (or (org-entry-get (point) "EXPORT_TITLE" 'selective
)
1433 (let ((case-fold-search nil
))
1434 (looking-at org-complex-heading-regexp
)
1435 (match-string-no-properties 4))))))
1436 ;; Look for both general keywords and back-end specific
1437 ;; options, with priority given to the latter.
1438 (options (append (org-export-get-all-options backend
)
1439 org-export-options-alist
)))
1440 ;; Handle other keywords. Then return PLIST.
1441 (dolist (option options plist
)
1442 (let ((property (car option
))
1443 (keyword (nth 1 option
)))
1446 (or (cdr (assoc keyword cache
))
1447 (let ((v (org-entry-get (point)
1448 (concat "EXPORT_" keyword
)
1450 (push (cons keyword v
) cache
) v
))))
1455 (cl-case (nth 4 option
)
1457 (org-element-parse-secondary-string
1458 value
(org-element-restriction 'keyword
)))
1459 (split (split-string value
))
1460 (t value
))))))))))))
1462 (defun org-export--get-inbuffer-options (&optional backend
)
1463 "Return current buffer export options, as a plist.
1465 Optional argument BACKEND, when non-nil, is an export back-end,
1466 as returned by, e.g., `org-export-create-backend'. It specifies
1467 which back-end specific options should also be read in the
1470 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1471 (let* ((case-fold-search t
)
1473 ;; Priority is given to back-end specific options.
1474 (org-export-get-all-options backend
)
1475 org-export-options-alist
))
1476 (regexp (format "^[ \t]*#\\+%s:"
1477 (regexp-opt (nconc (delq nil
(mapcar #'cadr options
))
1478 org-export-special-keywords
))))
1480 (letrec ((find-properties
1482 ;; Return all properties associated to KEYWORD.
1484 (dolist (option options properties
)
1485 (when (equal (nth 1 option
) keyword
)
1486 (cl-pushnew (car option
) properties
))))))
1488 (lambda (&optional files
)
1489 ;; Recursively read keywords in buffer. FILES is
1490 ;; a list of files read so far. PLIST is the current
1491 ;; property list obtained.
1492 (org-with-wide-buffer
1493 (goto-char (point-min))
1494 (while (re-search-forward regexp nil t
)
1495 (let ((element (org-element-at-point)))
1496 (when (eq (org-element-type element
) 'keyword
)
1497 (let ((key (org-element-property :key element
))
1498 (val (org-element-property :value element
)))
1500 ;; Options in `org-export-special-keywords'.
1501 ((equal key
"SETUPFILE")
1502 (let* ((uri (org-unbracket-string "\"" "\"" (org-trim val
)))
1503 (uri-is-url (org-file-url-p uri
))
1506 (expand-file-name uri
))))
1507 ;; Avoid circular dependencies.
1508 (unless (member uri files
)
1511 (setq default-directory
1512 (file-name-directory uri
)))
1513 (insert (org-file-contents uri
'noerror
))
1514 (let ((org-inhibit-startup t
)) (org-mode))
1515 (funcall get-options
(cons uri files
))))))
1516 ((equal key
"OPTIONS")
1520 (org-export--parse-option-keyword
1522 ((equal key
"FILETAGS")
1529 (org-split-string val
":")
1530 (plist-get plist
:filetags
)))))))
1532 ;; Options in `org-export-options-alist'.
1533 (dolist (property (funcall find-properties key
))
1538 ;; Handle value depending on specified
1540 (cl-case (nth 4 (assq property options
))
1542 (unless (memq property to-parse
)
1543 (push property to-parse
))
1544 ;; Even if `parse' implies `space'
1545 ;; behavior, we separate line with
1546 ;; "\n" so as to preserve
1547 ;; line-breaks. However, empty
1548 ;; lines are forbidden since `parse'
1549 ;; doesn't allow more than one
1551 (let ((old (plist-get plist property
)))
1552 (cond ((not (org-string-nw-p val
)) old
)
1553 (old (concat old
"\n" val
))
1556 (if (not (plist-get plist property
))
1558 (concat (plist-get plist property
)
1563 (concat (plist-get plist property
)
1566 (split `(,@(plist-get plist property
)
1567 ,@(split-string val
)))
1570 (if (not (plist-member plist property
)) val
1571 (plist-get plist property
)))))))))))))))))
1572 ;; Read options in the current buffer and return value.
1573 (funcall get-options
(and buffer-file-name
(list buffer-file-name
)))
1574 ;; Parse properties in TO-PARSE. Remove newline characters not
1575 ;; involved in line breaks to simulate `space' behavior.
1576 ;; Finally return options.
1577 (dolist (p to-parse plist
)
1578 (let ((value (org-element-parse-secondary-string
1580 (org-element-restriction 'keyword
))))
1581 (org-element-map value
'plain-text
1583 (org-element-set-element
1584 s
(replace-regexp-in-string "\n" " " s
))))
1585 (setq plist
(plist-put plist p value
)))))))
1587 (defun org-export--get-export-attributes
1588 (&optional backend subtreep visible-only body-only
)
1589 "Return properties related to export process, as a plist.
1590 Optional arguments BACKEND, SUBTREEP, VISIBLE-ONLY and BODY-ONLY
1591 are like the arguments with the same names of function
1593 (list :export-options
(delq nil
1594 (list (and subtreep
'subtree
)
1595 (and visible-only
'visible-only
)
1596 (and body-only
'body-only
)))
1598 :translate-alist
(org-export-get-all-transcoders backend
)
1599 :exported-data
(make-hash-table :test
#'eq
:size
4001)))
1601 (defun org-export--get-buffer-attributes ()
1602 "Return properties related to buffer attributes, as a plist."
1603 (list :input-buffer
(buffer-name (buffer-base-buffer))
1604 :input-file
(buffer-file-name (buffer-base-buffer))))
1606 (defun org-export--get-global-options (&optional backend
)
1607 "Return global export options as a plist.
1608 Optional argument BACKEND, if non-nil, is an export back-end, as
1609 returned by, e.g., `org-export-create-backend'. It specifies
1610 which back-end specific export options should also be read in the
1613 ;; Priority is given to back-end specific options.
1614 (all (append (org-export-get-all-options backend
)
1615 org-export-options-alist
)))
1616 (dolist (cell all plist
)
1617 (let ((prop (car cell
)))
1618 (unless (plist-member plist prop
)
1623 ;; Evaluate default value provided.
1624 (let ((value (eval (nth 3 cell
))))
1625 (if (eq (nth 4 cell
) 'parse
)
1626 (org-element-parse-secondary-string
1627 value
(org-element-restriction 'keyword
))
1630 (defun org-export--list-bound-variables ()
1631 "Return variables bound from BIND keywords in current buffer.
1632 Also look for BIND keywords in setup files. The return value is
1633 an alist where associations are (VARIABLE-NAME VALUE)."
1634 (when org-export-allow-bind-keywords
1635 (letrec ((collect-bind
1636 (lambda (files alist
)
1637 ;; Return an alist between variable names and their
1638 ;; value. FILES is a list of setup files names read
1639 ;; so far, used to avoid circular dependencies. ALIST
1640 ;; is the alist collected so far.
1641 (let ((case-fold-search t
))
1642 (org-with-wide-buffer
1643 (goto-char (point-min))
1644 (while (re-search-forward
1645 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t
)
1646 (let ((element (org-element-at-point)))
1647 (when (eq (org-element-type element
) 'keyword
)
1648 (let ((val (org-element-property :value element
)))
1649 (if (equal (org-element-property :key element
)
1651 (push (read (format "(%s)" val
)) alist
)
1652 ;; Enter setup file.
1653 (let* ((uri (org-unbracket-string "\"" "\"" val
))
1654 (uri-is-url (org-file-url-p uri
))
1657 (expand-file-name uri
))))
1658 ;; Avoid circular dependencies.
1659 (unless (member uri files
)
1662 (setq default-directory
1663 (file-name-directory uri
)))
1664 (let ((org-inhibit-startup t
)) (org-mode))
1665 (insert (org-file-contents uri
'noerror
))
1667 (funcall collect-bind
1671 ;; Return value in appropriate order of appearance.
1672 (nreverse (funcall collect-bind nil nil
)))))
1674 ;; defsubst org-export-get-parent must be defined before first use,
1675 ;; was originally defined in the topology section
1677 (defsubst org-export-get-parent
(blob)
1678 "Return BLOB parent or nil.
1679 BLOB is the element or object considered."
1680 (org-element-property :parent blob
))
1682 ;;;; Tree Properties
1684 ;; Tree properties are information extracted from parse tree. They
1685 ;; are initialized at the beginning of the transcoding process by
1686 ;; `org-export--collect-tree-properties'.
1688 ;; Dedicated functions focus on computing the value of specific tree
1689 ;; properties during initialization. Thus,
1690 ;; `org-export--populate-ignore-list' lists elements and objects that
1691 ;; should be skipped during export, `org-export--get-min-level' gets
1692 ;; the minimal exportable level, used as a basis to compute relative
1693 ;; level for headlines. Eventually
1694 ;; `org-export--collect-headline-numbering' builds an alist between
1695 ;; headlines and their numbering.
1697 (defun org-export--collect-tree-properties (data info
)
1698 "Extract tree properties from parse tree.
1700 DATA is the parse tree from which information is retrieved. INFO
1701 is a list holding export options.
1703 Following tree properties are set or updated:
1705 `:headline-offset' Offset between true level of headlines and
1706 local level. An offset of -1 means a headline
1707 of level 2 should be considered as a level
1708 1 headline in the context.
1710 `:headline-numbering' Alist of all headlines as key and the
1711 associated numbering as value.
1713 `:id-alist' Alist of all ID references as key and associated file
1716 Return updated plist."
1717 ;; Install the parse tree in the communication channel.
1718 (setq info
(plist-put info
:parse-tree data
))
1719 ;; Compute `:headline-offset' in order to be able to use
1720 ;; `org-export-get-relative-level'.
1724 (- 1 (org-export--get-min-level data info
))))
1725 ;; From now on, properties order doesn't matter: get the rest of the
1729 (list :headline-numbering
(org-export--collect-headline-numbering data info
)
1731 (org-element-map data
'link
1733 (and (string= (org-element-property :type l
) "id")
1734 (let* ((id (org-element-property :path l
))
1735 (file (car (org-id-find id
))))
1736 (and file
(cons id
(file-relative-name file
))))))))))
1738 (defun org-export--get-min-level (data options
)
1739 "Return minimum exportable headline's level in DATA.
1740 DATA is parsed tree as returned by `org-element-parse-buffer'.
1741 OPTIONS is a plist holding export options."
1743 (let ((min-level 10000))
1744 (dolist (datum (org-element-contents data
))
1745 (when (and (eq (org-element-type datum
) 'headline
)
1746 (not (org-element-property :footnote-section-p datum
))
1747 (not (memq datum
(plist-get options
:ignore-list
))))
1748 (setq min-level
(min (org-element-property :level datum
) min-level
))
1749 (when (= min-level
1) (throw 'exit
1))))
1750 ;; If no headline was found, for the sake of consistency, set
1751 ;; minimum level to 1 nonetheless.
1752 (if (= min-level
10000) 1 min-level
))))
1754 (defun org-export--collect-headline-numbering (data options
)
1755 "Return numbering of all exportable, numbered headlines in a parse tree.
1757 DATA is the parse tree. OPTIONS is the plist holding export
1760 Return an alist whose key is a headline and value is its
1761 associated numbering \(in the shape of a list of numbers) or nil
1762 for a footnotes section."
1763 (let ((numbering (make-vector org-export-max-depth
0)))
1764 (org-element-map data
'headline
1766 (when (and (org-export-numbered-headline-p headline options
)
1767 (not (org-element-property :footnote-section-p headline
)))
1768 (let ((relative-level
1769 (1- (org-export-get-relative-level headline options
))))
1773 for n across numbering
1774 for idx from
0 to org-export-max-depth
1775 when
(< idx relative-level
) collect n
1776 when
(= idx relative-level
) collect
(aset numbering idx
(1+ n
))
1777 when
(> idx relative-level
) do
(aset numbering idx
0))))))
1780 (defun org-export--selected-trees (data info
)
1781 "List headlines and inlinetasks with a select tag in their tree.
1782 DATA is parsed data as returned by `org-element-parse-buffer'.
1783 INFO is a plist holding export options."
1784 (let ((select (cl-mapcan (lambda (tag) (org-tags-expand tag t
))
1785 (plist-get info
:select-tags
))))
1786 (if (cl-some (lambda (tag) (member tag select
)) (plist-get info
:filetags
))
1787 ;; If FILETAGS contains a select tag, every headline or
1788 ;; inlinetask is returned.
1789 (org-element-map data
'(headline inlinetask
) #'identity
)
1790 (letrec ((selected-trees nil
)
1792 (lambda (data genealogy
)
1793 (let ((type (org-element-type data
)))
1795 ((memq type
'(headline inlinetask
))
1796 (let ((tags (org-element-property :tags data
)))
1797 (if (cl-some (lambda (tag) (member tag select
)) tags
)
1798 ;; When a select tag is found, mark full
1799 ;; genealogy and every headline within the
1800 ;; tree as acceptable.
1801 (setq selected-trees
1804 (org-element-map data
'(headline inlinetask
)
1807 ;; If at a headline, continue searching in
1808 ;; tree, recursively.
1809 (when (eq type
'headline
)
1810 (dolist (el (org-element-contents data
))
1811 (funcall walk-data el
(cons data genealogy
)))))))
1812 ((or (eq type
'org-data
)
1813 (memq type org-element-greater-elements
))
1814 (dolist (el (org-element-contents data
))
1815 (funcall walk-data el genealogy
))))))))
1816 (funcall walk-data data nil
)
1819 (defun org-export--skip-p (datum options selected excluded
)
1820 "Non-nil when element or object DATUM should be skipped during export.
1821 OPTIONS is the plist holding export options. SELECTED, when
1822 non-nil, is a list of headlines or inlinetasks belonging to
1823 a tree with a select tag. EXCLUDED is a list of tags, as
1824 strings. Any headline or inlinetask marked with one of those is
1826 (cl-case (org-element-type datum
)
1827 ((comment comment-block
)
1828 ;; Skip all comments and comment blocks. Make to keep maximum
1829 ;; number of blank lines around the comment so as to preserve
1830 ;; local structure of the document upon interpreting it back into
1832 (let* ((previous (org-export-get-previous-element datum options
))
1833 (before (or (org-element-property :post-blank previous
) 0))
1834 (after (or (org-element-property :post-blank datum
) 0)))
1836 (org-element-put-property previous
:post-blank
(max before after
1))))
1838 (clock (not (plist-get options
:with-clocks
)))
1840 (let ((with-drawers-p (plist-get options
:with-drawers
)))
1841 (or (not with-drawers-p
)
1842 (and (consp with-drawers-p
)
1843 ;; If `:with-drawers' value starts with `not', ignore
1844 ;; every drawer whose name belong to that list.
1845 ;; Otherwise, ignore drawers whose name isn't in that
1847 (let ((name (org-element-property :drawer-name datum
)))
1848 (if (eq (car with-drawers-p
) 'not
)
1849 (member-ignore-case name
(cdr with-drawers-p
))
1850 (not (member-ignore-case name with-drawers-p
))))))))
1851 (fixed-width (not (plist-get options
:with-fixed-width
)))
1852 ((footnote-definition footnote-reference
)
1853 (not (plist-get options
:with-footnotes
)))
1854 ((headline inlinetask
)
1855 (let ((with-tasks (plist-get options
:with-tasks
))
1856 (todo (org-element-property :todo-keyword datum
))
1857 (todo-type (org-element-property :todo-type datum
))
1858 (archived (plist-get options
:with-archived-trees
))
1859 (tags (org-export-get-tags datum options nil t
)))
1861 (and (eq (org-element-type datum
) 'inlinetask
)
1862 (not (plist-get options
:with-inlinetasks
)))
1863 ;; Ignore subtrees with an exclude tag.
1864 (cl-some (lambda (tag) (member tag excluded
)) tags
)
1865 ;; When a select tag is present in the buffer, ignore any tree
1867 (and selected
(not (memq datum selected
)))
1868 ;; Ignore commented sub-trees.
1869 (org-element-property :commentedp datum
)
1870 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1871 (and (not archived
) (org-element-property :archivedp datum
))
1872 ;; Ignore tasks, if specified by `:with-tasks' property.
1874 (or (not with-tasks
)
1875 (and (memq with-tasks
'(todo done
))
1876 (not (eq todo-type with-tasks
)))
1877 (and (consp with-tasks
) (not (member todo with-tasks
))))))))
1878 ((latex-environment latex-fragment
) (not (plist-get options
:with-latex
)))
1880 (let ((properties-set (plist-get options
:with-properties
)))
1881 (cond ((null properties-set
) t
)
1882 ((consp properties-set
)
1883 (not (member-ignore-case (org-element-property :key datum
)
1884 properties-set
))))))
1885 (planning (not (plist-get options
:with-planning
)))
1886 (property-drawer (not (plist-get options
:with-properties
)))
1887 (statistics-cookie (not (plist-get options
:with-statistics-cookies
)))
1888 (table (not (plist-get options
:with-tables
)))
1890 (and (org-export-table-has-special-column-p
1891 (org-export-get-parent-table datum
))
1892 (org-export-first-sibling-p datum options
)))
1893 (table-row (org-export-table-row-is-special-p datum options
))
1895 ;; `:with-timestamps' only applies to isolated timestamps
1896 ;; objects, i.e. timestamp objects in a paragraph containing only
1897 ;; timestamps and whitespaces.
1898 (when (let ((parent (org-export-get-parent-element datum
)))
1899 (and (memq (org-element-type parent
) '(paragraph verse-block
))
1900 (not (org-element-map parent
1902 (remq 'timestamp org-element-all-objects
))
1904 (or (not (stringp obj
)) (org-string-nw-p obj
)))
1906 (cl-case (plist-get options
:with-timestamps
)
1909 (not (memq (org-element-property :type datum
) '(active active-range
))))
1911 (not (memq (org-element-property :type datum
)
1912 '(inactive inactive-range
)))))))))
1917 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1918 ;; `org-element-parse-buffer') and transcodes it into a specified
1919 ;; back-end output. It takes care of filtering out elements or
1920 ;; objects according to export options and organizing the output blank
1921 ;; lines and white space are preserved. The function memoizes its
1922 ;; results, so it is cheap to call it within transcoders.
1924 ;; It is possible to modify locally the back-end used by
1925 ;; `org-export-data' or even use a temporary back-end by using
1926 ;; `org-export-data-with-backend'.
1928 ;; `org-export-transcoder' is an accessor returning appropriate
1929 ;; translator function for a given element or object.
1931 (defun org-export-transcoder (blob info
)
1932 "Return appropriate transcoder for BLOB.
1933 INFO is a plist containing export directives."
1934 (let ((type (org-element-type blob
)))
1935 ;; Return contents only for complete parse trees.
1936 (if (eq type
'org-data
) (lambda (_datum contents _info
) contents
)
1937 (let ((transcoder (cdr (assq type
(plist-get info
:translate-alist
)))))
1938 (and (functionp transcoder
) transcoder
)))))
1940 (defun org-export-data (data info
)
1941 "Convert DATA into current back-end format.
1943 DATA is a parse tree, an element or an object or a secondary
1944 string. INFO is a plist holding export options.
1947 (or (gethash data
(plist-get info
:exported-data
))
1948 ;; Handle broken links according to
1949 ;; `org-export-with-broken-links'.
1951 ((broken-link-handler
1953 `(condition-case err
1956 (pcase (plist-get info
:with-broken-links
)
1957 (`nil
(user-error "Unable to resolve link: %S" (nth 1 err
)))
1958 (`mark
(org-export-data
1959 (format "[BROKEN LINK: %s]" (nth 1 err
)) info
))
1961 (let* ((type (org-element-type data
))
1962 (parent (org-export-get-parent data
))
1965 ;; Ignored element/object.
1966 ((memq data
(plist-get info
:ignore-list
)) nil
)
1968 ((eq type
'plain-text
)
1969 (org-export-filter-apply-functions
1970 (plist-get info
:filter-plain-text
)
1971 (let ((transcoder (org-export-transcoder data info
)))
1972 (if transcoder
(funcall transcoder data info
) data
))
1974 ;; Secondary string.
1976 (mapconcat (lambda (obj) (org-export-data obj info
)) data
""))
1977 ;; Element/Object without contents or, as a special
1978 ;; case, headline with archive tag and archived trees
1979 ;; restricted to title only.
1980 ((or (not (org-element-contents data
))
1981 (and (eq type
'headline
)
1982 (eq (plist-get info
:with-archived-trees
) 'headline
)
1983 (org-element-property :archivedp data
)))
1984 (let ((transcoder (org-export-transcoder data info
)))
1985 (or (and (functionp transcoder
)
1986 (broken-link-handler
1987 (funcall transcoder data nil info
)))
1988 ;; Export snippets never return a nil value so
1989 ;; that white spaces following them are never
1991 (and (eq type
'export-snippet
) ""))))
1992 ;; Element/Object with contents.
1994 (let ((transcoder (org-export-transcoder data info
)))
1996 (let* ((greaterp (memq type org-element-greater-elements
))
1999 (memq type org-element-recursive-objects
)))
2002 (lambda (element) (org-export-data element info
))
2003 (org-element-contents
2004 (if (or greaterp objectp
) data
2005 ;; Elements directly containing
2006 ;; objects must have their indentation
2007 ;; normalized first.
2008 (org-element-normalize-contents
2010 ;; When normalizing first paragraph
2012 ;; a footnote-definition, ignore
2013 ;; first line's indentation.
2015 (eq type
'paragraph
)
2016 (memq (org-element-type parent
)
2017 '(footnote-definition item
))
2018 (eq (car (org-element-contents parent
))
2020 (eq (org-element-property :pre-blank parent
)
2023 (broken-link-handler
2024 (funcall transcoder data
2025 (if (not greaterp
) contents
2026 (org-element-normalize-string contents
))
2028 ;; Final result will be memoized before being returned.
2033 ((memq type
'(org-data plain-text nil
)) results
)
2034 ;; Append the same white space between elements or objects
2035 ;; as in the original buffer, and call appropriate filters.
2037 (org-export-filter-apply-functions
2038 (plist-get info
(intern (format ":filter-%s" type
)))
2039 (let ((blank (or (org-element-property :post-blank data
) 0)))
2040 (if (eq (org-element-class data parent
) 'object
)
2041 (concat results
(make-string blank ?\s
))
2042 (concat (org-element-normalize-string results
)
2043 (make-string blank ?
\n))))
2045 (plist-get info
:exported-data
))))))
2047 (defun org-export-data-with-backend (data backend info
)
2048 "Convert DATA into BACKEND format.
2050 DATA is an element, an object, a secondary string or a string.
2051 BACKEND is a symbol. INFO is a plist used as a communication
2054 Unlike to `org-export-with-backend', this function will
2055 recursively convert DATA using BACKEND translation table."
2056 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
2057 ;; Set-up a new communication channel with translations defined in
2058 ;; BACKEND as the translate table and a new hash table for
2063 (list :back-end backend
2064 :translate-alist
(org-export-get-all-transcoders backend
)
2065 ;; Size of the hash table is reduced since this
2066 ;; function will probably be used on small trees.
2067 :exported-data
(make-hash-table :test
'eq
:size
401)))))
2068 (prog1 (org-export-data data new-info
)
2069 ;; Preserve `:internal-references', as those do not depend on
2070 ;; the back-end used; we need to make sure that any new
2071 ;; reference when the temporary back-end was active gets through
2073 (plist-put info
:internal-references
2074 (plist-get new-info
:internal-references
)))))
2076 (defun org-export-expand (blob contents
&optional with-affiliated
)
2077 "Expand a parsed element or object to its original state.
2079 BLOB is either an element or an object. CONTENTS is its
2080 contents, as a string or nil.
2082 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2083 keywords before output."
2084 (let ((type (org-element-type blob
)))
2085 (concat (and with-affiliated
2086 (eq (org-element-class blob
) 'element
)
2087 (org-element--interpret-affiliated-keywords blob
))
2088 (funcall (intern (format "org-element-%s-interpreter" type
))
2093 ;;; The Filter System
2095 ;; Filters allow end-users to tweak easily the transcoded output.
2096 ;; They are the functional counterpart of hooks, as every filter in
2097 ;; a set is applied to the return value of the previous one.
2099 ;; Every set is back-end agnostic. Although, a filter is always
2100 ;; called, in addition to the string it applies to, with the back-end
2101 ;; used as argument, so it's easy for the end-user to add back-end
2102 ;; specific filters in the set. The communication channel, as
2103 ;; a plist, is required as the third argument.
2105 ;; From the developer side, filters sets can be installed in the
2106 ;; process with the help of `org-export-define-backend', which
2107 ;; internally stores filters as an alist. Each association has a key
2108 ;; among the following symbols and a function or a list of functions
2111 ;; - `:filter-options' applies to the property list containing export
2112 ;; options. Unlike to other filters, functions in this list accept
2113 ;; two arguments instead of three: the property list containing
2114 ;; export options and the back-end. Users can set its value through
2115 ;; `org-export-filter-options-functions' variable.
2117 ;; - `:filter-parse-tree' applies directly to the complete parsed
2118 ;; tree. Users can set it through
2119 ;; `org-export-filter-parse-tree-functions' variable.
2121 ;; - `:filter-body' applies to the body of the output, before template
2122 ;; translator chimes in. Users can set it through
2123 ;; `org-export-filter-body-functions' variable.
2125 ;; - `:filter-final-output' applies to the final transcoded string.
2126 ;; Users can set it with `org-export-filter-final-output-functions'
2129 ;; - `:filter-plain-text' applies to any string not recognized as Org
2130 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2133 ;; - `:filter-TYPE' applies on the string returned after an element or
2134 ;; object of type TYPE has been transcoded. A user can modify
2135 ;; `org-export-filter-TYPE-functions' to install these filters.
2137 ;; All filters sets are applied with
2138 ;; `org-export-filter-apply-functions' function. Filters in a set are
2139 ;; applied in a LIFO fashion. It allows developers to be sure that
2140 ;; their filters will be applied first.
2142 ;; Filters properties are installed in communication channel with
2143 ;; `org-export-install-filters' function.
2145 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2146 ;; `org-export-before-parsing-hook') are run at the beginning of the
2147 ;; export process and just before parsing to allow for heavy structure
2153 (defvar org-export-before-processing-hook nil
2154 "Hook run at the beginning of the export process.
2156 This is run before include keywords and macros are expanded and
2157 Babel code blocks executed, on a copy of the original buffer
2158 being exported. Visibility and narrowing are preserved. Point
2159 is at the beginning of the buffer.
2161 Every function in this hook will be called with one argument: the
2162 back-end currently used, as a symbol.")
2164 (defvar org-export-before-parsing-hook nil
2165 "Hook run before parsing an export buffer.
2167 This is run after include keywords and macros have been expanded
2168 and Babel code blocks executed, on a copy of the original buffer
2169 being exported. Visibility and narrowing are preserved. Point
2170 is at the beginning of the buffer.
2172 Every function in this hook will be called with one argument: the
2173 back-end currently used, as a symbol.")
2176 ;;;; Special Filters
2178 (defvar org-export-filter-options-functions nil
2179 "List of functions applied to the export options.
2180 Each filter is called with two arguments: the export options, as
2181 a plist, and the back-end, as a symbol. It must return
2182 a property list containing export options.")
2184 (defvar org-export-filter-parse-tree-functions nil
2185 "List of functions applied to the parsed tree.
2186 Each filter is called with three arguments: the parse tree, as
2187 returned by `org-element-parse-buffer', the back-end, as
2188 a symbol, and the communication channel, as a plist. It must
2189 return the modified parse tree to transcode.")
2191 (defvar org-export-filter-plain-text-functions nil
2192 "List of functions applied to plain text.
2193 Each filter is called with three arguments: a string which
2194 contains no Org syntax, the back-end, as a symbol, and the
2195 communication channel, as a plist. It must return a string or
2198 (defvar org-export-filter-body-functions nil
2199 "List of functions applied to transcoded body.
2200 Each filter is called with three arguments: a string which
2201 contains no Org syntax, the back-end, as a symbol, and the
2202 communication channel, as a plist. It must return a string or
2205 (defvar org-export-filter-final-output-functions nil
2206 "List of functions applied to the transcoded string.
2207 Each filter is called with three arguments: the full transcoded
2208 string, the back-end, as a symbol, and the communication channel,
2209 as a plist. It must return a string that will be used as the
2210 final export output.")
2213 ;;;; Elements Filters
2215 (defvar org-export-filter-babel-call-functions nil
2216 "List of functions applied to a transcoded babel-call.
2217 Each filter is called with three arguments: the transcoded data,
2218 as a string, the back-end, as a symbol, and the communication
2219 channel, as a plist. It must return a string or nil.")
2221 (defvar org-export-filter-center-block-functions nil
2222 "List of functions applied to a transcoded center block.
2223 Each filter is called with three arguments: the transcoded data,
2224 as a string, the back-end, as a symbol, and the communication
2225 channel, as a plist. It must return a string or nil.")
2227 (defvar org-export-filter-clock-functions nil
2228 "List of functions applied to a transcoded clock.
2229 Each filter is called with three arguments: the transcoded data,
2230 as a string, the back-end, as a symbol, and the communication
2231 channel, as a plist. It must return a string or nil.")
2233 (defvar org-export-filter-diary-sexp-functions nil
2234 "List of functions applied to a transcoded diary-sexp.
2235 Each filter is called with three arguments: the transcoded data,
2236 as a string, the back-end, as a symbol, and the communication
2237 channel, as a plist. It must return a string or nil.")
2239 (defvar org-export-filter-drawer-functions nil
2240 "List of functions applied to a transcoded drawer.
2241 Each filter is called with three arguments: the transcoded data,
2242 as a string, the back-end, as a symbol, and the communication
2243 channel, as a plist. It must return a string or nil.")
2245 (defvar org-export-filter-dynamic-block-functions nil
2246 "List of functions applied to a transcoded dynamic-block.
2247 Each filter is called with three arguments: the transcoded data,
2248 as a string, the back-end, as a symbol, and the communication
2249 channel, as a plist. It must return a string or nil.")
2251 (defvar org-export-filter-example-block-functions nil
2252 "List of functions applied to a transcoded example-block.
2253 Each filter is called with three arguments: the transcoded data,
2254 as a string, the back-end, as a symbol, and the communication
2255 channel, as a plist. It must return a string or nil.")
2257 (defvar org-export-filter-export-block-functions nil
2258 "List of functions applied to a transcoded export-block.
2259 Each filter is called with three arguments: the transcoded data,
2260 as a string, the back-end, as a symbol, and the communication
2261 channel, as a plist. It must return a string or nil.")
2263 (defvar org-export-filter-fixed-width-functions nil
2264 "List of functions applied to a transcoded fixed-width.
2265 Each filter is called with three arguments: the transcoded data,
2266 as a string, the back-end, as a symbol, and the communication
2267 channel, as a plist. It must return a string or nil.")
2269 (defvar org-export-filter-footnote-definition-functions nil
2270 "List of functions applied to a transcoded footnote-definition.
2271 Each filter is called with three arguments: the transcoded data,
2272 as a string, the back-end, as a symbol, and the communication
2273 channel, as a plist. It must return a string or nil.")
2275 (defvar org-export-filter-headline-functions nil
2276 "List of functions applied to a transcoded headline.
2277 Each filter is called with three arguments: the transcoded data,
2278 as a string, the back-end, as a symbol, and the communication
2279 channel, as a plist. It must return a string or nil.")
2281 (defvar org-export-filter-horizontal-rule-functions nil
2282 "List of functions applied to a transcoded horizontal-rule.
2283 Each filter is called with three arguments: the transcoded data,
2284 as a string, the back-end, as a symbol, and the communication
2285 channel, as a plist. It must return a string or nil.")
2287 (defvar org-export-filter-inlinetask-functions nil
2288 "List of functions applied to a transcoded inlinetask.
2289 Each filter is called with three arguments: the transcoded data,
2290 as a string, the back-end, as a symbol, and the communication
2291 channel, as a plist. It must return a string or nil.")
2293 (defvar org-export-filter-item-functions nil
2294 "List of functions applied to a transcoded item.
2295 Each filter is called with three arguments: the transcoded data,
2296 as a string, the back-end, as a symbol, and the communication
2297 channel, as a plist. It must return a string or nil.")
2299 (defvar org-export-filter-keyword-functions nil
2300 "List of functions applied to a transcoded keyword.
2301 Each filter is called with three arguments: the transcoded data,
2302 as a string, the back-end, as a symbol, and the communication
2303 channel, as a plist. It must return a string or nil.")
2305 (defvar org-export-filter-latex-environment-functions nil
2306 "List of functions applied to a transcoded latex-environment.
2307 Each filter is called with three arguments: the transcoded data,
2308 as a string, the back-end, as a symbol, and the communication
2309 channel, as a plist. It must return a string or nil.")
2311 (defvar org-export-filter-node-property-functions nil
2312 "List of functions applied to a transcoded node-property.
2313 Each filter is called with three arguments: the transcoded data,
2314 as a string, the back-end, as a symbol, and the communication
2315 channel, as a plist. It must return a string or nil.")
2317 (defvar org-export-filter-paragraph-functions nil
2318 "List of functions applied to a transcoded paragraph.
2319 Each filter is called with three arguments: the transcoded data,
2320 as a string, the back-end, as a symbol, and the communication
2321 channel, as a plist. It must return a string or nil.")
2323 (defvar org-export-filter-plain-list-functions nil
2324 "List of functions applied to a transcoded plain-list.
2325 Each filter is called with three arguments: the transcoded data,
2326 as a string, the back-end, as a symbol, and the communication
2327 channel, as a plist. It must return a string or nil.")
2329 (defvar org-export-filter-planning-functions nil
2330 "List of functions applied to a transcoded planning.
2331 Each filter is called with three arguments: the transcoded data,
2332 as a string, the back-end, as a symbol, and the communication
2333 channel, as a plist. It must return a string or nil.")
2335 (defvar org-export-filter-property-drawer-functions nil
2336 "List of functions applied to a transcoded property-drawer.
2337 Each filter is called with three arguments: the transcoded data,
2338 as a string, the back-end, as a symbol, and the communication
2339 channel, as a plist. It must return a string or nil.")
2341 (defvar org-export-filter-quote-block-functions nil
2342 "List of functions applied to a transcoded quote block.
2343 Each filter is called with three arguments: the transcoded quote
2344 data, as a string, the back-end, as a symbol, and the
2345 communication channel, as a plist. It must return a string or
2348 (defvar org-export-filter-section-functions nil
2349 "List of functions applied to a transcoded section.
2350 Each filter is called with three arguments: the transcoded data,
2351 as a string, the back-end, as a symbol, and the communication
2352 channel, as a plist. It must return a string or nil.")
2354 (defvar org-export-filter-special-block-functions nil
2355 "List of functions applied to a transcoded special block.
2356 Each filter is called with three arguments: the transcoded data,
2357 as a string, the back-end, as a symbol, and the communication
2358 channel, as a plist. It must return a string or nil.")
2360 (defvar org-export-filter-src-block-functions nil
2361 "List of functions applied to a transcoded src-block.
2362 Each filter is called with three arguments: the transcoded data,
2363 as a string, the back-end, as a symbol, and the communication
2364 channel, as a plist. It must return a string or nil.")
2366 (defvar org-export-filter-table-functions nil
2367 "List of functions applied to a transcoded table.
2368 Each filter is called with three arguments: the transcoded data,
2369 as a string, the back-end, as a symbol, and the communication
2370 channel, as a plist. It must return a string or nil.")
2372 (defvar org-export-filter-table-cell-functions nil
2373 "List of functions applied to a transcoded table-cell.
2374 Each filter is called with three arguments: the transcoded data,
2375 as a string, the back-end, as a symbol, and the communication
2376 channel, as a plist. It must return a string or nil.")
2378 (defvar org-export-filter-table-row-functions nil
2379 "List of functions applied to a transcoded table-row.
2380 Each filter is called with three arguments: the transcoded data,
2381 as a string, the back-end, as a symbol, and the communication
2382 channel, as a plist. It must return a string or nil.")
2384 (defvar org-export-filter-verse-block-functions nil
2385 "List of functions applied to a transcoded verse block.
2386 Each filter is called with three arguments: the transcoded data,
2387 as a string, the back-end, as a symbol, and the communication
2388 channel, as a plist. It must return a string or nil.")
2391 ;;;; Objects Filters
2393 (defvar org-export-filter-bold-functions nil
2394 "List of functions applied to transcoded bold text.
2395 Each filter is called with three arguments: the transcoded data,
2396 as a string, the back-end, as a symbol, and the communication
2397 channel, as a plist. It must return a string or nil.")
2399 (defvar org-export-filter-code-functions nil
2400 "List of functions applied to transcoded code text.
2401 Each filter is called with three arguments: the transcoded data,
2402 as a string, the back-end, as a symbol, and the communication
2403 channel, as a plist. It must return a string or nil.")
2405 (defvar org-export-filter-entity-functions nil
2406 "List of functions applied to a transcoded entity.
2407 Each filter is called with three arguments: the transcoded data,
2408 as a string, the back-end, as a symbol, and the communication
2409 channel, as a plist. It must return a string or nil.")
2411 (defvar org-export-filter-export-snippet-functions nil
2412 "List of functions applied to a transcoded export-snippet.
2413 Each filter is called with three arguments: the transcoded data,
2414 as a string, the back-end, as a symbol, and the communication
2415 channel, as a plist. It must return a string or nil.")
2417 (defvar org-export-filter-footnote-reference-functions nil
2418 "List of functions applied to a transcoded footnote-reference.
2419 Each filter is called with three arguments: the transcoded data,
2420 as a string, the back-end, as a symbol, and the communication
2421 channel, as a plist. It must return a string or nil.")
2423 (defvar org-export-filter-inline-babel-call-functions nil
2424 "List of functions applied to a transcoded inline-babel-call.
2425 Each filter is called with three arguments: the transcoded data,
2426 as a string, the back-end, as a symbol, and the communication
2427 channel, as a plist. It must return a string or nil.")
2429 (defvar org-export-filter-inline-src-block-functions nil
2430 "List of functions applied to a transcoded inline-src-block.
2431 Each filter is called with three arguments: the transcoded data,
2432 as a string, the back-end, as a symbol, and the communication
2433 channel, as a plist. It must return a string or nil.")
2435 (defvar org-export-filter-italic-functions nil
2436 "List of functions applied to transcoded italic text.
2437 Each filter is called with three arguments: the transcoded data,
2438 as a string, the back-end, as a symbol, and the communication
2439 channel, as a plist. It must return a string or nil.")
2441 (defvar org-export-filter-latex-fragment-functions nil
2442 "List of functions applied to a transcoded latex-fragment.
2443 Each filter is called with three arguments: the transcoded data,
2444 as a string, the back-end, as a symbol, and the communication
2445 channel, as a plist. It must return a string or nil.")
2447 (defvar org-export-filter-line-break-functions nil
2448 "List of functions applied to a transcoded line-break.
2449 Each filter is called with three arguments: the transcoded data,
2450 as a string, the back-end, as a symbol, and the communication
2451 channel, as a plist. It must return a string or nil.")
2453 (defvar org-export-filter-link-functions nil
2454 "List of functions applied to a transcoded link.
2455 Each filter is called with three arguments: the transcoded data,
2456 as a string, the back-end, as a symbol, and the communication
2457 channel, as a plist. It must return a string or nil.")
2459 (defvar org-export-filter-radio-target-functions nil
2460 "List of functions applied to a transcoded radio-target.
2461 Each filter is called with three arguments: the transcoded data,
2462 as a string, the back-end, as a symbol, and the communication
2463 channel, as a plist. It must return a string or nil.")
2465 (defvar org-export-filter-statistics-cookie-functions nil
2466 "List of functions applied to a transcoded statistics-cookie.
2467 Each filter is called with three arguments: the transcoded data,
2468 as a string, the back-end, as a symbol, and the communication
2469 channel, as a plist. It must return a string or nil.")
2471 (defvar org-export-filter-strike-through-functions nil
2472 "List of functions applied to transcoded strike-through text.
2473 Each filter is called with three arguments: the transcoded data,
2474 as a string, the back-end, as a symbol, and the communication
2475 channel, as a plist. It must return a string or nil.")
2477 (defvar org-export-filter-subscript-functions nil
2478 "List of functions applied to a transcoded subscript.
2479 Each filter is called with three arguments: the transcoded data,
2480 as a string, the back-end, as a symbol, and the communication
2481 channel, as a plist. It must return a string or nil.")
2483 (defvar org-export-filter-superscript-functions nil
2484 "List of functions applied to a transcoded superscript.
2485 Each filter is called with three arguments: the transcoded data,
2486 as a string, the back-end, as a symbol, and the communication
2487 channel, as a plist. It must return a string or nil.")
2489 (defvar org-export-filter-target-functions nil
2490 "List of functions applied to a transcoded target.
2491 Each filter is called with three arguments: the transcoded data,
2492 as a string, the back-end, as a symbol, and the communication
2493 channel, as a plist. It must return a string or nil.")
2495 (defvar org-export-filter-timestamp-functions nil
2496 "List of functions applied to a transcoded timestamp.
2497 Each filter is called with three arguments: the transcoded data,
2498 as a string, the back-end, as a symbol, and the communication
2499 channel, as a plist. It must return a string or nil.")
2501 (defvar org-export-filter-underline-functions nil
2502 "List of functions applied to transcoded underline text.
2503 Each filter is called with three arguments: the transcoded data,
2504 as a string, the back-end, as a symbol, and the communication
2505 channel, as a plist. It must return a string or nil.")
2507 (defvar org-export-filter-verbatim-functions nil
2508 "List of functions applied to transcoded verbatim text.
2509 Each filter is called with three arguments: the transcoded data,
2510 as a string, the back-end, as a symbol, and the communication
2511 channel, as a plist. It must return a string or nil.")
2516 ;; Internal function `org-export-install-filters' installs filters
2517 ;; hard-coded in back-ends (developer filters) and filters from global
2518 ;; variables (user filters) in the communication channel.
2520 ;; Internal function `org-export-filter-apply-functions' takes care
2521 ;; about applying each filter in order to a given data. It ignores
2522 ;; filters returning a nil value but stops whenever a filter returns
2525 (defun org-export-filter-apply-functions (filters value info
)
2526 "Call every function in FILTERS.
2528 Functions are called with three arguments: a value, the export
2529 back-end name and the communication channel. First function in
2530 FILTERS is called with VALUE as its first argument. Second
2531 function in FILTERS is called with the previous result as its
2534 Functions returning nil are skipped. Any function returning the
2535 empty string ends the process, which returns the empty string.
2537 Call is done in a LIFO fashion, to be sure that developer
2538 specified filters, if any, are called first."
2540 (let* ((backend (plist-get info
:back-end
))
2541 (backend-name (and backend
(org-export-backend-name backend
))))
2542 (dolist (filter filters value
)
2543 (let ((result (funcall filter value backend-name info
)))
2544 (cond ((not result
))
2545 ((equal result
"") (throw :exit
""))
2546 (t (setq value result
))))))))
2548 (defun org-export-install-filters (info)
2549 "Install filters properties in communication channel.
2550 INFO is a plist containing the current communication channel.
2551 Return the updated communication channel."
2553 ;; Install user-defined filters with `org-export-filters-alist'
2554 ;; and filters already in INFO (through ext-plist mechanism).
2555 (dolist (p org-export-filters-alist
)
2556 (let* ((prop (car p
))
2557 (info-value (plist-get info prop
))
2558 (default-value (symbol-value (cdr p
))))
2560 (plist-put plist prop
2561 ;; Filters in INFO will be called
2562 ;; before those user provided.
2563 (append (if (listp info-value
) info-value
2566 ;; Prepend back-end specific filters to that list.
2567 (dolist (p (org-export-get-all-filters (plist-get info
:back-end
)))
2568 ;; Single values get consed, lists are appended.
2569 (let ((key (car p
)) (value (cdr p
)))
2574 (if (atom value
) (cons value
(plist-get plist key
))
2575 (append value
(plist-get plist key
))))))))
2576 ;; Return new communication channel.
2577 (org-combine-plists info plist
)))
2583 ;; This is the room for the main function, `org-export-as', along with
2584 ;; its derivative, `org-export-string-as'.
2585 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2586 ;; function should be added to kill ring.
2588 ;; Note that `org-export-as' doesn't really parse the current buffer,
2589 ;; but a copy of it (with the same buffer-local variables and
2590 ;; visibility), where macros and include keywords are expanded and
2591 ;; Babel blocks are executed, if appropriate.
2592 ;; `org-export-with-buffer-copy' macro prepares that copy.
2594 ;; File inclusion is taken care of by
2595 ;; `org-export-expand-include-keyword' and
2596 ;; `org-export--prepare-file-contents'. Structure wise, including
2597 ;; a whole Org file in a buffer often makes little sense. For
2598 ;; example, if the file contains a headline and the include keyword
2599 ;; was within an item, the item should contain the headline. That's
2600 ;; why file inclusion should be done before any structure can be
2601 ;; associated to the file, that is before parsing.
2603 ;; `org-export-insert-default-template' is a command to insert
2604 ;; a default template (or a back-end specific template) at point or in
2607 (defun org-export-copy-buffer ()
2608 "Return a copy of the current buffer.
2609 The copy preserves Org buffer-local variables, visibility and
2611 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2612 (new-buf (generate-new-buffer (buffer-name))))
2613 (with-current-buffer new-buf
2614 (funcall copy-buffer-fun
)
2615 (set-buffer-modified-p nil
))
2618 (defmacro org-export-with-buffer-copy
(&rest body
)
2619 "Apply BODY in a copy of the current buffer.
2620 The copy preserves local variables, visibility and contents of
2621 the original buffer. Point is at the beginning of the buffer
2622 when BODY is applied."
2624 (org-with-gensyms (buf-copy)
2625 `(let ((,buf-copy
(org-export-copy-buffer)))
2627 (with-current-buffer ,buf-copy
2628 (goto-char (point-min))
2630 (and (buffer-live-p ,buf-copy
)
2631 ;; Kill copy without confirmation.
2632 (progn (with-current-buffer ,buf-copy
2633 (restore-buffer-modified-p nil
))
2634 (kill-buffer ,buf-copy
)))))))
2636 (defun org-export--generate-copy-script (buffer)
2637 "Generate a function duplicating BUFFER.
2639 The copy will preserve local variables, visibility, contents and
2640 narrowing of the original buffer. If a region was active in
2641 BUFFER, contents will be narrowed to that region instead.
2643 The resulting function can be evaluated at a later time, from
2644 another buffer, effectively cloning the original buffer there.
2646 The function assumes BUFFER's major mode is `org-mode'."
2647 (with-current-buffer buffer
2649 (let ((inhibit-modification-hooks t
))
2650 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2651 ;; already in BUFFER.
2652 (let ((org-mode-hook nil
) (org-inhibit-startup t
)) (org-mode))
2653 ;; Copy specific buffer local variables and variables set
2654 ;; through BIND keywords.
2655 ,@(let ((bound-variables (org-export--list-bound-variables))
2657 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars
)
2659 (let ((var (car entry
))
2661 (and (not (memq var org-export-ignored-local-variables
))
2665 buffer-file-coding-system
))
2666 (assq var bound-variables
)
2667 (string-match "^\\(org-\\|orgtbl-\\)"
2669 ;; Skip unreadable values, as they cannot be
2670 ;; sent to external process.
2671 (or (not val
) (ignore-errors (read (format "%S" val
))))
2672 (push `(set (make-local-variable (quote ,var
))
2675 ;; Whole buffer contents.
2677 ,(org-with-wide-buffer
2678 (buffer-substring-no-properties
2679 (point-min) (point-max))))
2681 ,(if (org-region-active-p)
2682 `(narrow-to-region ,(region-beginning) ,(region-end))
2683 `(narrow-to-region ,(point-min) ,(point-max)))
2684 ;; Current position of point.
2685 (goto-char ,(point))
2686 ;; Overlays with invisible property.
2688 (dolist (ov (overlays-in (point-min) (point-max)) ov-set
)
2689 (let ((invis-prop (overlay-get ov
'invisible
)))
2692 (make-overlay ,(overlay-start ov
)
2694 'invisible
(quote ,invis-prop
))
2697 (defun org-export--delete-comment-trees ()
2698 "Delete commented trees and commented inlinetasks in the buffer.
2699 Narrowing, if any, is ignored."
2700 (org-with-wide-buffer
2701 (goto-char (point-min))
2702 (let* ((case-fold-search t
)
2703 (regexp (concat org-outline-regexp-bol
".*" org-comment-string
)))
2704 (while (re-search-forward regexp nil t
)
2705 (let ((element (org-element-at-point)))
2706 (when (org-element-property :commentedp element
)
2707 (delete-region (org-element-property :begin element
)
2708 (org-element-property :end element
))))))))
2710 (defun org-export--prune-tree (data info
)
2711 "Prune non exportable elements from DATA.
2712 DATA is the parse tree to traverse. INFO is the plist holding
2713 export info. Also set `:ignore-list' in INFO to a list of
2714 objects which should be ignored during export, but not removed
2716 (letrec ((ignore nil
)
2717 ;; First find trees containing a select tag, if any.
2718 (selected (org-export--selected-trees data info
))
2719 ;; List tags that prevent export of headlines.
2720 (excluded (cl-mapcan (lambda (tag) (org-tags-expand tag t
))
2721 (plist-get info
:exclude-tags
)))
2724 ;; Prune non-exportable elements and objects from tree.
2725 ;; As a special case, special rows and cells from tables
2726 ;; are stored in IGNORE, as they still need to be
2727 ;; accessed during export.
2729 (let ((type (org-element-type data
)))
2730 (if (org-export--skip-p data info selected excluded
)
2731 (if (memq type
'(table-cell table-row
)) (push data ignore
)
2732 (org-element-extract-element data
))
2733 (if (and (eq type
'headline
)
2734 (eq (plist-get info
:with-archived-trees
)
2736 (org-element-property :archivedp data
))
2737 ;; If headline is archived but tree below has
2738 ;; to be skipped, remove contents.
2739 (org-element-set-contents data
)
2740 ;; Move into recursive objects/elements.
2741 (mapc walk-data
(org-element-contents data
)))
2742 ;; Move into secondary string, if any.
2743 (dolist (p (cdr (assq type
2744 org-element-secondary-value-alist
)))
2745 (mapc walk-data
(org-element-property p data
))))))))
2747 ;; Collect definitions before possibly pruning them so as
2748 ;; to avoid parsing them again if they are required.
2749 (org-element-map data
'(footnote-definition footnote-reference
)
2752 ((eq 'footnote-definition
(org-element-type f
)) f
)
2753 ((and (eq 'inline
(org-element-property :type f
))
2754 (org-element-property :label f
))
2757 ;; If a select tag is active, also ignore the section before the
2758 ;; first headline, if any.
2760 (let ((first-element (car (org-element-contents data
))))
2761 (when (eq (org-element-type first-element
) 'section
)
2762 (org-element-extract-element first-element
))))
2763 ;; Prune tree and communication channel.
2764 (funcall walk-data data
)
2765 (dolist (entry (append
2766 ;; Priority is given to back-end specific options.
2767 (org-export-get-all-options (plist-get info
:back-end
))
2768 org-export-options-alist
))
2769 (when (eq (nth 4 entry
) 'parse
)
2770 (funcall walk-data
(plist-get info
(car entry
)))))
2771 (let ((missing (org-export--missing-definitions data definitions
)))
2772 (funcall walk-data missing
)
2773 (org-export--install-footnote-definitions missing data
))
2774 ;; Eventually set `:ignore-list'.
2775 (plist-put info
:ignore-list ignore
)))
2777 (defun org-export--missing-definitions (tree definitions
)
2778 "List footnote definitions missing from TREE.
2779 Missing definitions are searched within DEFINITIONS, which is
2780 a list of footnote definitions or in the widened buffer."
2783 ;; List all footnote labels encountered in DATA. Inline
2784 ;; footnote references are ignored.
2785 (org-element-map data
'footnote-reference
2787 (and (eq (org-element-property :type reference
) 'standard
)
2788 (org-element-property :label reference
))))))
2789 defined undefined missing-definitions
)
2790 ;; Partition DIRECT-REFERENCES between DEFINED and UNDEFINED
2792 (let ((known-definitions
2793 (org-element-map tree
'(footnote-reference footnote-definition
)
2795 (and (or (eq (org-element-type f
) 'footnote-definition
)
2796 (eq (org-element-property :type f
) 'inline
))
2797 (org-element-property :label f
)))))
2799 (dolist (l (funcall list-labels tree
))
2800 (cond ((member l seen
))
2801 ((member l known-definitions
) (push l defined
))
2802 (t (push l undefined
)))))
2803 ;; Complete MISSING-DEFINITIONS by finding the definition of every
2804 ;; undefined label, first by looking into DEFINITIONS, then by
2805 ;; searching the widened buffer. This is a recursive process
2806 ;; since definitions found can themselves contain an undefined
2809 (let* ((label (pop undefined
))
2813 (lambda (d) (and (equal (org-element-property :label d
) label
)
2816 ((pcase (org-footnote-get-definition label
)
2818 (org-with-wide-buffer
2820 (let ((datum (org-element-context)))
2821 (if (eq (org-element-type datum
) 'footnote-reference
)
2823 ;; Parse definition with contents.
2826 (org-element-property :begin datum
)
2827 (org-element-property :end datum
))
2828 (org-element-map (org-element-parse-buffer)
2829 'footnote-definition
#'identity nil t
))))))
2831 (t (user-error "Definition not found for footnote %s" label
)))))
2832 (push label defined
)
2833 (push definition missing-definitions
)
2834 ;; Look for footnote references within DEFINITION, since
2835 ;; we may need to also find their definition.
2836 (dolist (l (funcall list-labels definition
))
2837 (unless (or (member l defined
) ;Known label
2838 (member l undefined
)) ;Processed later
2839 (push l undefined
)))))
2840 ;; MISSING-DEFINITIONS may contain footnote references with inline
2841 ;; definitions. Make sure those are changed into real footnote
2844 (if (eq (org-element-type d
) 'footnote-definition
) d
2845 (let ((label (org-element-property :label d
)))
2846 (apply #'org-element-create
2847 'footnote-definition
`(:label
,label
:post-blank
1)
2848 (org-element-contents d
)))))
2849 missing-definitions
)))
2851 (defun org-export--install-footnote-definitions (definitions tree
)
2852 "Install footnote definitions in tree.
2854 DEFINITIONS is the list of footnote definitions to install. TREE
2857 If there is a footnote section in TREE, definitions found are
2858 appended to it. If `org-footnote-section' is non-nil, a new
2859 footnote section containing all definitions is inserted in TREE.
2860 Otherwise, definitions are appended at the end of the section
2861 containing their first reference."
2863 ((null definitions
))
2864 ;; If there is a footnote section, insert definitions there.
2865 ((let ((footnote-section
2866 (org-element-map tree
'headline
2867 (lambda (h) (and (org-element-property :footnote-section-p h
) h
))
2869 (and footnote-section
2870 (apply #'org-element-adopt-elements
2872 (nreverse definitions
)))))
2873 ;; If there should be a footnote section, create one containing all
2874 ;; the definitions at the end of the tree.
2875 (org-footnote-section
2876 (org-element-adopt-elements
2878 (org-element-create 'headline
2879 (list :footnote-section-p t
2881 :title org-footnote-section
2882 :raw-value org-footnote-section
)
2883 (apply #'org-element-create
2886 (nreverse definitions
)))))
2887 ;; Otherwise add each definition at the end of the section where it
2888 ;; is first referenced.
2893 ;; Insert footnote definitions in the same section as
2894 ;; their first reference in DATA.
2895 (org-element-map data
'footnote-reference
2897 (when (eq (org-element-property :type reference
) 'standard
)
2898 (let ((label (org-element-property :label reference
)))
2899 (unless (member label seen
)
2904 (and (equal (org-element-property :label d
)
2908 (org-element-adopt-elements
2909 (org-element-lineage reference
'(section))
2911 ;; Also insert definitions for nested
2912 ;; references, if any.
2913 (funcall insert-definitions definition
))))))))))
2914 (funcall insert-definitions tree
)))))
2916 (defun org-export--remove-uninterpreted-data (data info
)
2917 "Change uninterpreted elements back into Org syntax.
2918 DATA is a parse tree or a secondary string. INFO is a plist
2919 containing export options. It is modified by side effect and
2920 returned by the function."
2921 (org-element-map data
2922 '(entity bold italic latex-environment latex-fragment strike-through
2923 subscript superscript underline
)
2926 (cl-case (org-element-type datum
)
2929 (and (not (plist-get info
:with-entities
))
2931 (org-export-expand datum nil
)
2933 (or (org-element-property :post-blank datum
) 0)
2936 ((bold italic strike-through underline
)
2937 (and (not (plist-get info
:with-emphasize
))
2938 (let ((marker (cl-case (org-element-type datum
)
2941 (strike-through "+")
2945 (org-element-contents datum
)
2949 (or (org-element-property :post-blank datum
)
2952 ;; ... LaTeX environments and fragments...
2953 ((latex-environment latex-fragment
)
2954 (and (eq (plist-get info
:with-latex
) 'verbatim
)
2955 (list (org-export-expand datum nil
))))
2956 ;; ... sub/superscripts...
2957 ((subscript superscript
)
2958 (let ((sub/super-p
(plist-get info
:with-sub-superscript
))
2959 (bracketp (org-element-property :use-brackets-p datum
)))
2960 (and (or (not sub
/super-p
)
2961 (and (eq sub
/super-p
'{}) (not bracketp
)))
2964 (if (eq (org-element-type datum
) 'subscript
)
2967 (and bracketp
"{")))
2968 (org-element-contents datum
)
2971 (and (org-element-property :post-blank datum
)
2973 (org-element-property :post-blank datum
)
2976 ;; Splice NEW at DATUM location in parse tree.
2977 (dolist (e new
(org-element-extract-element datum
))
2978 (unless (equal e
"") (org-element-insert-before e datum
))))))
2980 ;; Return modified parse tree.
2984 (defun org-export-as
2985 (backend &optional subtreep visible-only body-only ext-plist
)
2986 "Transcode current Org buffer into BACKEND code.
2988 BACKEND is either an export back-end, as returned by, e.g.,
2989 `org-export-create-backend', or a symbol referring to
2990 a registered back-end.
2992 If narrowing is active in the current buffer, only transcode its
2995 If a region is active, transcode that region.
2997 When optional argument SUBTREEP is non-nil, transcode the
2998 sub-tree at point, extracting information from the headline
3001 When optional argument VISIBLE-ONLY is non-nil, don't export
3002 contents of hidden elements.
3004 When optional argument BODY-ONLY is non-nil, only return body
3005 code, without surrounding template.
3007 Optional argument EXT-PLIST, when provided, is a property list
3008 with external parameters overriding Org default settings, but
3009 still inferior to file-local settings.
3011 Return code as a string."
3012 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3013 (org-export-barf-if-invalid-backend backend
)
3016 ;; Narrow buffer to an appropriate region or subtree for
3017 ;; parsing. If parsing subtree, be sure to remove main
3018 ;; headline, planning data and property drawer.
3019 (cond ((org-region-active-p)
3020 (narrow-to-region (region-beginning) (region-end)))
3022 (org-narrow-to-subtree)
3023 (goto-char (point-min))
3024 (org-end-of-meta-data)
3025 (narrow-to-region (point) (point-max))))
3026 ;; Initialize communication channel with original buffer
3027 ;; attributes, unavailable in its copy.
3028 (let* ((org-export-current-backend (org-export-backend-name backend
))
3029 (info (org-combine-plists
3030 (org-export--get-export-attributes
3031 backend subtreep visible-only body-only
)
3032 (org-export--get-buffer-attributes)))
3035 (mapcar (lambda (o) (and (eq (nth 4 o
) 'parse
) (nth 1 o
)))
3036 (append (org-export-get-all-options backend
)
3037 org-export-options-alist
))))
3039 ;; Update communication channel and get parse tree. Buffer
3040 ;; isn't parsed directly. Instead, all buffer modifications
3041 ;; and consequent parsing are undertaken in a temporary copy.
3042 (org-export-with-buffer-copy
3043 ;; Run first hook with current back-end's name as argument.
3044 (run-hook-with-args 'org-export-before-processing-hook
3045 (org-export-backend-name backend
))
3046 ;; Include files, delete comments and expand macros. Refresh
3047 ;; buffer properties and radio targets after these
3048 ;; potentially invasive changes.
3049 (org-export-expand-include-keyword)
3050 (org-export--delete-comment-trees)
3051 (org-macro-initialize-templates)
3052 (org-macro-replace-all (append org-macro-templates
3053 org-export-global-macros
)
3055 (org-set-regexps-and-options)
3056 (org-update-radio-target-regexp)
3057 ;; Possibly execute Babel code. Re-run a macro expansion
3058 ;; specifically for {{{results}}} since inline source blocks
3059 ;; may have generated some more. Refresh buffer properties
3060 ;; and radio targets another time.
3061 (when org-export-use-babel
3062 (org-babel-exp-process-buffer)
3063 (org-macro-replace-all '(("results" .
"$1")) parsed-keywords
)
3064 (org-set-regexps-and-options)
3065 (org-update-radio-target-regexp))
3066 ;; Run last hook with current back-end's name as argument.
3067 ;; Update buffer properties and radio targets one last time
3069 (goto-char (point-min))
3071 (run-hook-with-args 'org-export-before-parsing-hook
3072 (org-export-backend-name backend
)))
3073 (org-set-regexps-and-options)
3074 (org-update-radio-target-regexp)
3075 ;; Update communication channel with environment.
3078 info
(org-export-get-environment backend subtreep ext-plist
)))
3079 ;; De-activate uninterpreted data from parsed keywords.
3080 (dolist (entry (append (org-export-get-all-options backend
)
3081 org-export-options-alist
))
3083 (`(,p
,_
,_
,_ parse
)
3084 (let ((value (plist-get info p
)))
3087 (org-export--remove-uninterpreted-data value info
))))
3089 ;; Install user's and developer's filters.
3090 (setq info
(org-export-install-filters info
))
3091 ;; Call options filters and update export options. We do not
3092 ;; use `org-export-filter-apply-functions' here since the
3093 ;; arity of such filters is different.
3094 (let ((backend-name (org-export-backend-name backend
)))
3095 (dolist (filter (plist-get info
:filter-options
))
3096 (let ((result (funcall filter info backend-name
)))
3097 (when result
(setq info result
)))))
3099 (setq tree
(org-element-parse-buffer nil visible-only
))
3100 ;; Prune tree from non-exported elements and transform
3101 ;; uninterpreted elements or objects in both parse tree and
3102 ;; communication channel.
3103 (org-export--prune-tree tree info
)
3104 (org-export--remove-uninterpreted-data tree info
)
3105 ;; Call parse tree filters.
3107 (org-export-filter-apply-functions
3108 (plist-get info
:filter-parse-tree
) tree info
))
3109 ;; Now tree is complete, compute its properties and add them
3110 ;; to communication channel.
3111 (setq info
(org-export--collect-tree-properties tree info
))
3112 ;; Eventually transcode TREE. Wrap the resulting string into
3114 (let* ((body (org-element-normalize-string
3115 (or (org-export-data tree info
) "")))
3116 (inner-template (cdr (assq 'inner-template
3117 (plist-get info
:translate-alist
))))
3118 (full-body (org-export-filter-apply-functions
3119 (plist-get info
:filter-body
)
3120 (if (not (functionp inner-template
)) body
3121 (funcall inner-template body info
))
3123 (template (cdr (assq 'template
3124 (plist-get info
:translate-alist
)))))
3125 ;; Remove all text properties since they cannot be
3126 ;; retrieved from an external process. Finally call
3127 ;; final-output filter and return result.
3129 (org-export-filter-apply-functions
3130 (plist-get info
:filter-final-output
)
3131 (if (or (not (functionp template
)) body-only
) full-body
3132 (funcall template full-body info
))
3136 (defun org-export-string-as (string backend
&optional body-only ext-plist
)
3137 "Transcode STRING into BACKEND code.
3139 BACKEND is either an export back-end, as returned by, e.g.,
3140 `org-export-create-backend', or a symbol referring to
3141 a registered back-end.
3143 When optional argument BODY-ONLY is non-nil, only return body
3144 code, without preamble nor postamble.
3146 Optional argument EXT-PLIST, when provided, is a property list
3147 with external parameters overriding Org default settings, but
3148 still inferior to file-local settings.
3150 Return code as a string."
3153 (let ((org-inhibit-startup t
)) (org-mode))
3154 (org-export-as backend nil nil body-only ext-plist
)))
3157 (defun org-export-replace-region-by (backend)
3158 "Replace the active region by its export to BACKEND.
3159 BACKEND is either an export back-end, as returned by, e.g.,
3160 `org-export-create-backend', or a symbol referring to
3161 a registered back-end."
3162 (unless (org-region-active-p) (user-error "No active region to replace"))
3164 (org-export-string-as
3165 (delete-and-extract-region (region-beginning) (region-end)) backend t
)))
3168 (defun org-export-insert-default-template (&optional backend subtreep
)
3169 "Insert all export keywords with default values at beginning of line.
3171 BACKEND is a symbol referring to the name of a registered export
3172 back-end, for which specific export options should be added to
3173 the template, or `default' for default template. When it is nil,
3174 the user will be prompted for a category.
3176 If SUBTREEP is non-nil, export configuration will be set up
3177 locally for the subtree through node properties."
3179 (unless (derived-mode-p 'org-mode
) (user-error "Not in an Org mode buffer"))
3180 (when (and subtreep
(org-before-first-heading-p))
3181 (user-error "No subtree to set export options for"))
3182 (let ((node (and subtreep
(save-excursion (org-back-to-heading t
) (point))))
3186 (org-completing-read
3187 "Options category: "
3190 (symbol-name (org-export-backend-name b
)))
3191 org-export-registered-backends
))
3194 ;; Populate OPTIONS and KEYWORDS.
3195 (dolist (entry (cond ((eq backend
'default
) org-export-options-alist
)
3196 ((org-export-backend-p backend
)
3197 (org-export-backend-options backend
))
3198 (t (org-export-backend-options
3199 (org-export-get-backend backend
)))))
3200 (let ((keyword (nth 1 entry
))
3201 (option (nth 2 entry
)))
3203 (keyword (unless (assoc keyword keywords
)
3205 (if (eq (nth 4 entry
) 'split
)
3206 (mapconcat #'identity
(eval (nth 3 entry
)) " ")
3207 (eval (nth 3 entry
)))))
3208 (push (cons keyword value
) keywords
))))
3209 (option (unless (assoc option options
)
3210 (push (cons option
(eval (nth 3 entry
))) options
))))))
3211 ;; Move to an appropriate location in order to insert options.
3212 (unless subtreep
(beginning-of-line))
3213 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3217 #'(lambda (opt) (format "%s:%S" (car opt
) (cdr opt
)))
3218 (sort options
(lambda (k1 k2
) (string< (car k1
) (car k2
)))))))
3221 node
"EXPORT_OPTIONS" (mapconcat 'identity items
" "))
3223 (insert "#+OPTIONS:")
3226 (< (+ width
(length (car items
)) 1) fill-column
))
3227 (let ((item (pop items
)))
3229 (cl-incf width
(1+ (length item
))))))
3231 ;; Then the rest of keywords, in the order specified in either
3232 ;; `org-export-options-alist' or respective export back-ends.
3233 (dolist (key (nreverse keywords
))
3234 (let ((val (cond ((equal (car key
) "DATE")
3237 (org-insert-time-stamp (current-time)))))
3238 ((equal (car key
) "TITLE")
3239 (or (let ((visited-file
3240 (buffer-file-name (buffer-base-buffer))))
3242 (file-name-sans-extension
3243 (file-name-nondirectory visited-file
))))
3244 (buffer-name (buffer-base-buffer))))
3246 (if subtreep
(org-entry-put node
(concat "EXPORT_" (car key
)) val
)
3250 (if (org-string-nw-p val
) (format " %s" val
) ""))))))))
3252 (defun org-export-expand-include-keyword (&optional included dir footnotes
)
3253 "Expand every include keyword in buffer.
3254 Optional argument INCLUDED is a list of included file names along
3255 with their line restriction, when appropriate. It is used to
3256 avoid infinite recursion. Optional argument DIR is the current
3257 working directory. It is used to properly resolve relative
3258 paths. Optional argument FOOTNOTES is a hash-table used for
3259 storing and resolving footnotes. It is created automatically."
3260 (let ((includer-file (buffer-file-name (buffer-base-buffer)))
3261 (case-fold-search t
)
3262 (file-prefix (make-hash-table :test
#'equal
))
3264 (footnotes (or footnotes
(make-hash-table :test
#'equal
)))
3265 (include-re "^[ \t]*#\\+INCLUDE:"))
3266 ;; If :minlevel is not set the text-property
3267 ;; `:org-include-induced-level' will be used to determine the
3268 ;; relative level when expanding INCLUDE.
3269 ;; Only affects included Org documents.
3270 (goto-char (point-min))
3271 (while (re-search-forward include-re nil t
)
3272 (put-text-property (line-beginning-position) (line-end-position)
3273 :org-include-induced-level
3274 (1+ (org-reduced-level (or (org-current-level) 0)))))
3275 ;; Expand INCLUDE keywords.
3276 (goto-char (point-min))
3277 (while (re-search-forward include-re nil t
)
3278 (unless (org-in-commented-heading-p)
3279 (let ((element (save-match-data (org-element-at-point))))
3280 (when (eq (org-element-type element
) 'keyword
)
3282 ;; Extract arguments from keyword's value.
3283 (let* ((value (org-element-property :value element
))
3284 (ind (org-get-indentation))
3288 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value
)
3291 (let ((matched (match-string 1 value
)))
3292 (when (string-match "\\(::\\(.*?\\)\\)\"?\\'"
3294 (setq location
(match-string 2 matched
))
3296 (replace-match "" nil nil matched
1)))
3298 (org-unbracket-string "\"" "\"" matched
)
3300 (setq value
(replace-match "" nil nil value
)))))
3302 (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?"
3304 (prog1 (org-not-nil (match-string 1 value
))
3305 (setq value
(replace-match "" nil nil value
)))))
3308 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3310 (prog1 (match-string 1 value
)
3311 (setq value
(replace-match "" nil nil value
)))))
3313 ((string-match "\\<example\\>" value
) 'literal
)
3314 ((string-match "\\<export\\(?: +\\(.*\\)\\)?" value
)
3316 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value
)
3318 ;; Minimal level of included file defaults to the
3319 ;; child level of the current headline, if any, or
3320 ;; one. It only applies is the file is meant to be
3321 ;; included as an Org one.
3324 (if (string-match ":minlevel +\\([0-9]+\\)" value
)
3325 (prog1 (string-to-number (match-string 1 value
))
3326 (setq value
(replace-match "" nil nil value
)))
3327 (get-text-property (point)
3328 :org-include-induced-level
))))
3329 (args (and (eq env
'literal
) (match-string 1 value
)))
3330 (block (and (string-match "\\<\\(\\S-+\\)\\>" value
)
3331 (match-string 1 value
))))
3333 (delete-region (point) (line-beginning-position 2))
3336 ((not (file-readable-p file
))
3337 (error "Cannot include file %s" file
))
3338 ;; Check if files has already been parsed. Look after
3339 ;; inclusion lines too, as different parts of the same
3340 ;; file can be included too.
3341 ((member (list file lines
) included
)
3342 (error "Recursive file inclusion: %s" file
))
3347 (let ((ind-str (make-string ind ?\s
))
3348 (arg-str (if (stringp args
) (format " %s" args
) ""))
3350 (org-escape-code-in-string
3351 (org-export--prepare-file-contents file lines
))))
3352 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3353 ind-str block arg-str contents ind-str block
))))
3356 (let ((ind-str (make-string ind ?\s
))
3358 (org-export--prepare-file-contents file lines
)))
3359 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3360 ind-str block contents ind-str block
))))
3364 (let ((org-inhibit-startup t
)
3367 (org-export--inclusion-absolute-lines
3368 file location only-contents lines
)
3372 (org-export--prepare-file-contents
3373 file lines ind minlevel
3374 (or (gethash file file-prefix
)
3376 (cl-incf current-prefix
)
3380 (org-export-expand-include-keyword
3381 (cons (list file lines
) included
)
3382 (file-name-directory file
)
3385 ;; Expand footnotes after all files have been
3386 ;; included. Footnotes are stored at end of buffer.
3388 (org-with-wide-buffer
3389 (goto-char (point-max))
3390 (maphash (lambda (k v
)
3391 (insert (format "\n[fn:%s] %s\n" k v
)))
3392 footnotes
))))))))))))
3394 (defun org-export--inclusion-absolute-lines (file location only-contents lines
)
3395 "Resolve absolute lines for an included file with file-link.
3397 FILE is string file-name of the file to include. LOCATION is a
3398 string name within FILE to be included (located via
3399 `org-link-search'). If ONLY-CONTENTS is non-nil only the
3400 contents of the named element will be included, as determined
3401 Org-Element. If LINES is non-nil only those lines are included.
3403 Return a string of lines to be included in the format expected by
3404 `org-export--prepare-file-contents'."
3406 (insert-file-contents file
)
3407 (unless (eq major-mode
'org-mode
)
3408 (let ((org-inhibit-startup t
)) (org-mode)))
3410 ;; Enforce consistent search.
3411 (let ((org-link-search-must-match-exact-headline nil
))
3412 (org-link-search location
))
3414 (error "%s for %s::%s" (error-message-string err
) file location
)))
3415 (let* ((element (org-element-at-point))
3417 (and only-contents
(org-element-property :contents-begin element
))))
3419 (or contents-begin
(org-element-property :begin element
))
3420 (org-element-property (if contents-begin
:contents-end
:end
) element
))
3421 (when (and only-contents
3422 (memq (org-element-type element
) '(headline inlinetask
)))
3423 ;; Skip planning line and property-drawer.
3424 (goto-char (point-min))
3425 (when (looking-at-p org-planning-line-re
) (forward-line))
3426 (when (looking-at org-property-drawer-re
) (goto-char (match-end 0)))
3427 (unless (bolp) (forward-line))
3428 (narrow-to-region (point) (point-max))))
3430 (org-skip-whitespace)
3432 (let* ((lines (split-string lines
"-"))
3433 (lbeg (string-to-number (car lines
)))
3434 (lend (string-to-number (cadr lines
)))
3435 (beg (if (zerop lbeg
) (point-min)
3436 (goto-char (point-min))
3437 (forward-line (1- lbeg
))
3439 (end (if (zerop lend
) (point-max)
3441 (forward-line (1- lend
))
3443 (narrow-to-region beg end
)))
3444 (let ((end (point-max)))
3445 (goto-char (point-min))
3447 (let ((start-line (line-number-at-pos)))
3453 (while (< (point) end
) (cl-incf counter
) (forward-line))
3456 (defun org-export--prepare-file-contents
3457 (file &optional lines ind minlevel id footnotes includer
)
3458 "Prepare contents of FILE for inclusion and return it as a string.
3460 When optional argument LINES is a string specifying a range of
3461 lines, include only those lines.
3463 Optional argument IND, when non-nil, is an integer specifying the
3464 global indentation of returned contents. Since its purpose is to
3465 allow an included file to stay in the same environment it was
3466 created (e.g., a list item), it doesn't apply past the first
3467 headline encountered.
3469 Optional argument MINLEVEL, when non-nil, is an integer
3470 specifying the level that any top-level headline in the included
3473 Optional argument ID is an integer that will be inserted before
3474 each footnote definition and reference if FILE is an Org file.
3475 This is useful to avoid conflicts when more than one Org file
3476 with footnotes is included in a document.
3478 Optional argument FOOTNOTES is a hash-table to store footnotes in
3479 the included document.
3481 Optional argument INCLUDER is the file name where the inclusion
3484 (insert-file-contents file
)
3486 (let* ((lines (split-string lines
"-"))
3487 (lbeg (string-to-number (car lines
)))
3488 (lend (string-to-number (cadr lines
)))
3489 (beg (if (zerop lbeg
) (point-min)
3490 (goto-char (point-min))
3491 (forward-line (1- lbeg
))
3493 (end (if (zerop lend
) (point-max)
3494 (goto-char (point-min))
3495 (forward-line (1- lend
))
3497 (narrow-to-region beg end
)))
3498 ;; Adapt all file links within the included document that contain
3499 ;; relative paths in order to make these paths relative to the
3500 ;; base document, or absolute.
3502 (let ((file-dir (file-name-directory file
))
3503 (includer-dir (file-name-directory includer
)))
3504 (unless (file-equal-p file-dir includer-dir
)
3505 (goto-char (point-min))
3506 (unless (eq major-mode
'org-mode
)
3507 (let ((org-inhibit-startup t
)) (org-mode))) ;set regexps
3508 (while (re-search-forward org-any-link-re nil t
)
3509 (let ((link (save-excursion (backward-char) (org-element-context))))
3510 (when (and (eq 'link
(org-element-type link
))
3511 (string= "file" (org-element-property :type link
)))
3512 (let ((old-path (org-element-property :path link
)))
3513 (unless (or (org-file-remote-p old-path
)
3514 (file-name-absolute-p old-path
))
3515 (let ((new-path (file-relative-name
3516 (expand-file-name old-path file-dir
)
3519 (let ((new (org-element-copy link
)))
3520 (org-element-put-property new
:path new-path
)
3521 (when (org-element-property :contents-begin link
)
3522 (org-element-adopt-elements new
3524 (org-element-property :contents-begin link
)
3525 (org-element-property :contents-end link
))))
3526 (delete-region (org-element-property :begin link
)
3527 (org-element-property :end link
))
3528 (org-element-interpret-data new
))))))))))))
3529 ;; Remove blank lines at beginning and end of contents. The logic
3530 ;; behind that removal is that blank lines around include keyword
3531 ;; override blank lines in included file.
3532 (goto-char (point-min))
3533 (org-skip-whitespace)
3535 (delete-region (point-min) (point))
3536 (goto-char (point-max))
3537 (skip-chars-backward " \r\t\n")
3539 (delete-region (point) (point-max))
3540 ;; If IND is set, preserve indentation of include keyword until
3541 ;; the first headline encountered.
3542 (when (and ind
(> ind
0))
3543 (unless (eq major-mode
'org-mode
)
3544 (let ((org-inhibit-startup t
)) (org-mode)))
3545 (goto-char (point-min))
3546 (let ((ind-str (make-string ind ?\s
)))
3547 (while (not (or (eobp) (looking-at org-outline-regexp-bol
)))
3548 ;; Do not move footnote definitions out of column 0.
3549 (unless (and (looking-at org-footnote-definition-re
)
3550 (eq (org-element-type (org-element-at-point))
3551 'footnote-definition
))
3554 ;; When MINLEVEL is specified, compute minimal level for headlines
3555 ;; in the file (CUR-MIN), and remove stars to each headline so
3556 ;; that headlines with minimal level have a level of MINLEVEL.
3558 (unless (eq major-mode
'org-mode
)
3559 (let ((org-inhibit-startup t
)) (org-mode)))
3560 (org-with-limited-levels
3561 (let ((levels (org-map-entries
3562 (lambda () (org-reduced-level (org-current-level))))))
3564 (let ((offset (- minlevel
(apply #'min levels
))))
3565 (unless (zerop offset
)
3566 (when org-odd-levels-only
(setq offset
(* offset
2)))
3567 ;; Only change stars, don't bother moving whole
3571 (if (< offset
0) (delete-char (abs offset
))
3572 (insert (make-string offset ?
*)))))))))))
3573 ;; Append ID to all footnote references and definitions, so they
3574 ;; become file specific and cannot collide with footnotes in other
3575 ;; included files. Further, collect relevant footnote definitions
3576 ;; outside of LINES, in order to reintroduce them later.
3578 (let ((marker-min (point-min-marker))
3579 (marker-max (point-max-marker))
3582 ;; Generate new label from LABEL by prefixing it with
3584 (format "-%d-%s" id label
)))
3587 ;; Replace OLD label with NEW in footnote F.
3589 (goto-char (+ (org-element-property :begin f
) 4))
3590 (looking-at (regexp-quote old
))
3591 (replace-match new
))))
3593 (goto-char (point-min))
3594 (while (re-search-forward org-footnote-re nil t
)
3595 (let ((footnote (save-excursion
3597 (org-element-context))))
3598 (when (memq (org-element-type footnote
)
3599 '(footnote-definition footnote-reference
))
3600 (let* ((label (org-element-property :label footnote
)))
3601 ;; Update the footnote-reference at point and collect
3602 ;; the new label, which is only used for footnotes
3605 (let ((seen (cdr (assoc label seen-alist
))))
3606 (if seen
(funcall set-new-label footnote label seen
)
3607 (let ((new (funcall get-new-label label
)))
3608 (push (cons label new
) seen-alist
)
3609 (org-with-wide-buffer
3610 (let* ((def (org-footnote-get-definition label
))
3613 (or (< beg marker-min
)
3614 (>= beg marker-max
)))
3615 ;; Store since footnote-definition is
3616 ;; outside of LINES.
3618 (org-element-normalize-string (nth 3 def
))
3620 (funcall set-new-label footnote label new
)))))))))
3621 (set-marker marker-min nil
)
3622 (set-marker marker-max nil
)))
3623 (org-element-normalize-string (buffer-string))))
3625 (defun org-export--copy-to-kill-ring-p ()
3626 "Return a non-nil value when output should be added to the kill ring.
3627 See also `org-export-copy-to-kill-ring'."
3628 (if (eq org-export-copy-to-kill-ring
'if-interactive
)
3629 (not (or executing-kbd-macro noninteractive
))
3630 (eq org-export-copy-to-kill-ring t
)))
3634 ;;; Tools For Back-Ends
3636 ;; A whole set of tools is available to help build new exporters. Any
3637 ;; function general enough to have its use across many back-ends
3638 ;; should be added here.
3640 ;;;; For Affiliated Keywords
3642 ;; `org-export-read-attribute' reads a property from a given element
3643 ;; as a plist. It can be used to normalize affiliated keywords'
3646 ;; Since captions can span over multiple lines and accept dual values,
3647 ;; their internal representation is a bit tricky. Therefore,
3648 ;; `org-export-get-caption' transparently returns a given element's
3649 ;; caption as a secondary string.
3651 (defun org-export-read-attribute (attribute element
&optional property
)
3652 "Turn ATTRIBUTE property from ELEMENT into a plist.
3654 When optional argument PROPERTY is non-nil, return the value of
3655 that property within attributes.
3657 This function assumes attributes are defined as \":keyword
3658 value\" pairs. It is appropriate for `:attr_html' like
3661 All values will become strings except the empty string and
3662 \"nil\", which will become nil. Also, values containing only
3663 double quotes will be read as-is, which means that \"\" value
3664 will become the empty string."
3665 (let* ((prepare-value
3668 (cond ((member str
'(nil "" "nil")) nil
)
3669 ((string-match "^\"\\(\"+\\)?\"$" str
)
3670 (or (match-string 1 str
) ""))
3673 (let ((value (org-element-property attribute element
)))
3675 (let ((s (mapconcat 'identity value
" ")) result
)
3676 (while (string-match
3677 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3679 (let ((value (substring s
0 (match-beginning 0))))
3680 (push (funcall prepare-value value
) result
))
3681 (push (intern (match-string 1 s
)) result
)
3682 (setq s
(substring s
(match-end 0))))
3683 ;; Ignore any string before first property with `cdr'.
3684 (cdr (nreverse (cons (funcall prepare-value s
) result
))))))))
3685 (if property
(plist-get attributes property
) attributes
)))
3687 (defun org-export-get-caption (element &optional shortp
)
3688 "Return caption from ELEMENT as a secondary string.
3690 When optional argument SHORTP is non-nil, return short caption,
3691 as a secondary string, instead.
3693 Caption lines are separated by a white space."
3694 (let ((full-caption (org-element-property :caption element
)) caption
)
3695 (dolist (line full-caption
(cdr caption
))
3696 (let ((cap (funcall (if shortp
'cdr
'car
) line
)))
3698 (setq caption
(nconc (list " ") (copy-sequence cap
) caption
)))))))
3701 ;;;; For Derived Back-ends
3703 ;; `org-export-with-backend' is a function allowing to locally use
3704 ;; another back-end to transcode some object or element. In a derived
3705 ;; back-end, it may be used as a fall-back function once all specific
3706 ;; cases have been treated.
3708 (defun org-export-with-backend (backend data
&optional contents info
)
3709 "Call a transcoder from BACKEND on DATA.
3710 BACKEND is an export back-end, as returned by, e.g.,
3711 `org-export-create-backend', or a symbol referring to
3712 a registered back-end. DATA is an Org element, object, secondary
3713 string or string. CONTENTS, when non-nil, is the transcoded
3714 contents of DATA element, as a string. INFO, when non-nil, is
3715 the communication channel used for export, as a plist."
3716 (when (symbolp backend
) (setq backend
(org-export-get-backend backend
)))
3717 (org-export-barf-if-invalid-backend backend
)
3718 (let ((type (org-element-type data
)))
3719 (when (memq type
'(nil org-data
)) (error "No foreign transcoder available"))
3720 (let* ((all-transcoders (org-export-get-all-transcoders backend
))
3721 (transcoder (cdr (assq type all-transcoders
))))
3722 (unless (functionp transcoder
) (error "No foreign transcoder available"))
3727 :translate-alist all-transcoders
3728 :exported-data
(make-hash-table :test
#'eq
:size
401)))))
3729 ;; `:internal-references' are shared across back-ends.
3730 (prog1 (if (eq type
'plain-text
)
3731 (funcall transcoder data new-info
)
3732 (funcall transcoder data contents new-info
))
3733 (plist-put info
:internal-references
3734 (plist-get new-info
:internal-references
)))))))
3737 ;;;; For Export Snippets
3739 ;; Every export snippet is transmitted to the back-end. Though, the
3740 ;; latter will only retain one type of export-snippet, ignoring
3741 ;; others, based on the former's target back-end. The function
3742 ;; `org-export-snippet-backend' returns that back-end for a given
3745 (defun org-export-snippet-backend (export-snippet)
3746 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3747 Translation, with `org-export-snippet-translation-alist', is
3749 (let ((back-end (org-element-property :back-end export-snippet
)))
3751 (or (cdr (assoc back-end org-export-snippet-translation-alist
))
3757 ;; `org-export-collect-footnote-definitions' is a tool to list
3758 ;; actually used footnotes definitions in the whole parse tree, or in
3759 ;; a headline, in order to add footnote listings throughout the
3762 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3763 ;; back-ends, when they need to attach the footnote definition only to
3764 ;; the first occurrence of the corresponding label.
3766 ;; `org-export-get-footnote-definition' and
3767 ;; `org-export-get-footnote-number' provide easier access to
3768 ;; additional information relative to a footnote reference.
3770 (defun org-export-get-footnote-definition (footnote-reference info
)
3771 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3772 INFO is the plist used as a communication channel. If no such
3773 definition can be found, raise an error."
3774 (let ((label (org-element-property :label footnote-reference
)))
3775 (if (not label
) (org-element-contents footnote-reference
)
3776 (let ((cache (or (plist-get info
:footnote-definition-cache
)
3777 (let ((hash (make-hash-table :test
#'equal
)))
3778 (plist-put info
:footnote-definition-cache hash
)
3781 (gethash label cache
)
3783 (org-element-map (plist-get info
:parse-tree
)
3784 '(footnote-definition footnote-reference
)
3787 ;; Skip any footnote with a different label.
3788 ;; Also skip any standard footnote reference
3789 ;; with the same label since those cannot
3790 ;; contain a definition.
3791 ((not (equal (org-element-property :label f
) label
)) nil
)
3792 ((eq (org-element-property :type f
) 'standard
) nil
)
3793 ((org-element-contents f
))
3794 ;; Even if the contents are empty, we can not
3795 ;; return nil since that would eventually raise
3796 ;; the error. Instead, return the equivalent
3801 (error "Definition not found for footnote %s" label
))))))
3803 (defun org-export--footnote-reference-map
3804 (function data info
&optional body-first
)
3805 "Apply FUNCTION on every footnote reference in DATA.
3806 INFO is a plist containing export state. By default, as soon as
3807 a new footnote reference is encountered, FUNCTION is called onto
3808 its definition. However, if BODY-FIRST is non-nil, this step is
3809 delayed until the end of the process."
3810 (letrec ((definitions nil
)
3813 (lambda (data delayp
)
3814 ;; Search footnote references through DATA, filling
3815 ;; SEEN-REFS along the way. When DELAYP is non-nil,
3816 ;; store footnote definitions so they can be entered
3818 (org-element-map data
'footnote-reference
3820 (funcall function f
)
3821 (let ((--label (org-element-property :label f
)))
3822 (unless (and --label
(member --label seen-refs
))
3823 (when --label
(push --label seen-refs
))
3824 ;; Search for subsequent references in footnote
3825 ;; definition so numbering follows reading
3826 ;; logic, unless DELAYP in non-nil.
3829 (push (org-export-get-footnote-definition f info
)
3831 ;; Do not force entering inline definitions,
3832 ;; since `org-element-map' already traverses
3833 ;; them at the right time.
3834 ((eq (org-element-property :type f
) 'inline
))
3835 (t (funcall search-ref
3836 (org-export-get-footnote-definition f info
)
3839 ;; Don't enter footnote definitions since it will
3840 ;; happen when their first reference is found.
3841 ;; Moreover, if DELAYP is non-nil, make sure we
3842 ;; postpone entering definitions of inline references.
3843 (if delayp
'(footnote-definition footnote-reference
)
3844 'footnote-definition
)))))
3845 (funcall search-ref data body-first
)
3846 (funcall search-ref
(nreverse definitions
) nil
)))
3848 (defun org-export-collect-footnote-definitions (info &optional data body-first
)
3849 "Return an alist between footnote numbers, labels and definitions.
3851 INFO is the current export state, as a plist.
3853 Definitions are collected throughout the whole parse tree, or
3856 Sorting is done by order of references. As soon as a new
3857 reference is encountered, other references are searched within
3858 its definition. However, if BODY-FIRST is non-nil, this step is
3859 delayed after the whole tree is checked. This alters results
3860 when references are found in footnote definitions.
3862 Definitions either appear as Org data or as a secondary string
3863 for inlined footnotes. Unreferenced definitions are ignored."
3864 (let ((n 0) labels alist
)
3865 (org-export--footnote-reference-map
3867 ;; Collect footnote number, label and definition.
3868 (let ((l (org-element-property :label f
)))
3869 (unless (and l
(member l labels
))
3871 (push (list n l
(org-export-get-footnote-definition f info
)) alist
))
3872 (when l
(push l labels
))))
3873 (or data
(plist-get info
:parse-tree
)) info body-first
)
3876 (defun org-export-footnote-first-reference-p
3877 (footnote-reference info
&optional data body-first
)
3878 "Non-nil when a footnote reference is the first one for its label.
3880 FOOTNOTE-REFERENCE is the footnote reference being considered.
3881 INFO is a plist containing current export state.
3883 Search is done throughout the whole parse tree, or DATA when
3886 By default, as soon as a new footnote reference is encountered,
3887 other references are searched within its definition. However, if
3888 BODY-FIRST is non-nil, this step is delayed after the whole tree
3889 is checked. This alters results when references are found in
3890 footnote definitions."
3891 (let ((label (org-element-property :label footnote-reference
)))
3892 ;; Anonymous footnotes are always a first reference.
3895 (org-export--footnote-reference-map
3897 (let ((l (org-element-property :label f
)))
3898 (when (and l label
(string= label l
))
3899 (throw 'exit
(eq footnote-reference f
)))))
3900 (or data
(plist-get info
:parse-tree
)) info body-first
)))))
3902 (defun org-export-get-footnote-number (footnote info
&optional data body-first
)
3903 "Return number associated to a footnote.
3905 FOOTNOTE is either a footnote reference or a footnote definition.
3906 INFO is the plist containing export state.
3908 Number is unique throughout the whole parse tree, or DATA, when
3911 By default, as soon as a new footnote reference is encountered,
3912 counting process moves into its definition. However, if
3913 BODY-FIRST is non-nil, this step is delayed until the end of the
3914 process, leading to a different order when footnotes are nested."
3917 (label (org-element-property :label footnote
)))
3919 (org-export--footnote-reference-map
3921 (let ((l (org-element-property :label f
)))
3923 ;; Anonymous footnote match: return number.
3924 ((and (not l
) (not label
) (eq footnote f
)) (throw 'exit
(1+ count
)))
3925 ;; Labels match: return number.
3926 ((and label l
(string= label l
)) (throw 'exit
(1+ count
)))
3927 ;; Otherwise store label and increase counter if label
3928 ;; wasn't encountered yet.
3929 ((not l
) (cl-incf count
))
3930 ((not (member l seen
)) (push l seen
) (cl-incf count
)))))
3931 (or data
(plist-get info
:parse-tree
)) info body-first
))))
3936 ;; `org-export-get-relative-level' is a shortcut to get headline
3937 ;; level, relatively to the lower headline level in the parsed tree.
3939 ;; `org-export-get-headline-number' returns the section number of an
3940 ;; headline, while `org-export-number-to-roman' allows it to be
3941 ;; converted to roman numbers. With an optional argument,
3942 ;; `org-export-get-headline-number' returns a number to unnumbered
3943 ;; headlines (used for internal id).
3945 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3946 ;; `org-export-last-sibling-p' are three useful predicates when it
3947 ;; comes to fulfill the `:headline-levels' property.
3949 ;; `org-export-get-tags', `org-export-get-category' and
3950 ;; `org-export-get-node-property' extract useful information from an
3951 ;; headline or a parent headline. They all handle inheritance.
3953 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3954 ;; as a secondary string, suitable for table of contents. It falls
3955 ;; back onto default title.
3957 (defun org-export-get-relative-level (headline info
)
3958 "Return HEADLINE relative level within current parsed tree.
3959 INFO is a plist holding contextual information."
3960 (+ (org-element-property :level headline
)
3961 (or (plist-get info
:headline-offset
) 0)))
3963 (defun org-export-low-level-p (headline info
)
3964 "Non-nil when HEADLINE is considered as low level.
3966 INFO is a plist used as a communication channel.
3968 A low level headlines has a relative level greater than
3969 `:headline-levels' property value.
3971 Return value is the difference between HEADLINE relative level
3972 and the last level being considered as high enough, or nil."
3973 (let ((limit (plist-get info
:headline-levels
)))
3974 (when (wholenump limit
)
3975 (let ((level (org-export-get-relative-level headline info
)))
3976 (and (> level limit
) (- level limit
))))))
3978 (defun org-export-get-headline-number (headline info
)
3979 "Return numbered HEADLINE numbering as a list of numbers.
3980 INFO is a plist holding contextual information."
3981 (and (org-export-numbered-headline-p headline info
)
3982 (cdr (assq headline
(plist-get info
:headline-numbering
)))))
3984 (defun org-export-numbered-headline-p (headline info
)
3985 "Return a non-nil value if HEADLINE element should be numbered.
3986 INFO is a plist used as a communication channel."
3987 (unless (org-not-nil (org-export-get-node-property :UNNUMBERED headline t
))
3988 (let ((sec-num (plist-get info
:section-numbers
))
3989 (level (org-export-get-relative-level headline info
)))
3990 (if (wholenump sec-num
) (<= level sec-num
) sec-num
))))
3992 (defun org-export-number-to-roman (n)
3993 "Convert integer N into a roman numeral."
3994 (let ((roman '((1000 .
"M") (900 .
"CM") (500 .
"D") (400 .
"CD")
3995 ( 100 .
"C") ( 90 .
"XC") ( 50 .
"L") ( 40 .
"XL")
3996 ( 10 .
"X") ( 9 .
"IX") ( 5 .
"V") ( 4 .
"IV")
4000 (number-to-string n
)
4002 (if (>= n
(caar roman
))
4003 (setq n
(- n
(caar roman
))
4004 res
(concat res
(cdar roman
)))
4008 (defun org-export-get-tags (element info
&optional tags inherited
)
4009 "Return list of tags associated to ELEMENT.
4011 ELEMENT has either an `headline' or an `inlinetask' type. INFO
4012 is a plist used as a communication channel.
4014 When non-nil, optional argument TAGS should be a list of strings.
4015 Any tag belonging to this list will also be removed.
4017 When optional argument INHERITED is non-nil, tags can also be
4018 inherited from parent headlines and FILETAGS keywords."
4020 (lambda (tag) (member tag tags
))
4021 (if (not inherited
) (org-element-property :tags element
)
4022 ;; Build complete list of inherited tags.
4023 (let ((current-tag-list (org-element-property :tags element
)))
4024 (dolist (parent (org-element-lineage element
))
4025 (dolist (tag (org-element-property :tags parent
))
4026 (when (and (memq (org-element-type parent
) '(headline inlinetask
))
4027 (not (member tag current-tag-list
)))
4028 (push tag current-tag-list
))))
4029 ;; Add FILETAGS keywords and return results.
4030 (org-uniquify (append (plist-get info
:filetags
) current-tag-list
))))))
4032 (defun org-export-get-node-property (property blob
&optional inherited
)
4033 "Return node PROPERTY value for BLOB.
4035 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
4038 If optional argument INHERITED is non-nil, the value can be
4039 inherited from a parent headline.
4041 Return value is a string or nil."
4042 (let ((headline (if (eq (org-element-type blob
) 'headline
) blob
4043 (org-export-get-parent-headline blob
))))
4044 (if (not inherited
) (org-element-property property blob
)
4045 (let ((parent headline
))
4048 (when (plist-member (nth 1 parent
) property
)
4049 (throw 'found
(org-element-property property parent
)))
4050 (setq parent
(org-element-property :parent parent
))))))))
4052 (defun org-export-get-category (blob info
)
4053 "Return category for element or object BLOB.
4055 INFO is a plist used as a communication channel.
4057 CATEGORY is automatically inherited from a parent headline, from
4058 #+CATEGORY: keyword or created out of original file name. If all
4059 fail, the fall-back value is \"???\"."
4060 (or (org-export-get-node-property :CATEGORY blob t
)
4061 (org-element-map (plist-get info
:parse-tree
) 'keyword
4063 (when (equal (org-element-property :key kwd
) "CATEGORY")
4064 (org-element-property :value kwd
)))
4066 (let ((file (plist-get info
:input-file
)))
4067 (and file
(file-name-sans-extension (file-name-nondirectory file
))))
4070 (defun org-export-get-alt-title (headline _
)
4071 "Return alternative title for HEADLINE, as a secondary string.
4072 If no optional title is defined, fall-back to the regular title."
4073 (let ((alt (org-element-property :ALT_TITLE headline
)))
4074 (if alt
(org-element-parse-secondary-string
4075 alt
(org-element-restriction 'headline
) headline
)
4076 (org-element-property :title headline
))))
4078 (defun org-export-first-sibling-p (blob info
)
4079 "Non-nil when BLOB is the first sibling in its parent.
4080 BLOB is an element or an object. If BLOB is a headline, non-nil
4081 means it is the first sibling in the sub-tree. INFO is a plist
4082 used as a communication channel."
4083 (memq (org-element-type (org-export-get-previous-element blob info
))
4086 (defun org-export-last-sibling-p (datum info
)
4087 "Non-nil when DATUM is the last sibling in its parent.
4088 DATUM is an element or an object. INFO is a plist used as
4089 a communication channel."
4090 (let ((next (org-export-get-next-element datum info
)))
4092 (and (eq 'headline
(org-element-type datum
))
4093 (> (org-element-property :level datum
)
4094 (org-element-property :level next
))))))
4099 ;; `org-export-get-date' returns a date appropriate for the document
4100 ;; to about to be exported. In particular, it takes care of
4101 ;; `org-export-date-timestamp-format'.
4103 (defun org-export-get-date (info &optional fmt
)
4104 "Return date value for the current document.
4106 INFO is a plist used as a communication channel. FMT, when
4107 non-nil, is a time format string that will be applied on the date
4108 if it consists in a single timestamp object. It defaults to
4109 `org-export-date-timestamp-format' when nil.
4111 A proper date can be a secondary string, a string or nil. It is
4112 meant to be translated with `org-export-data' or alike."
4113 (let ((date (plist-get info
:date
))
4114 (fmt (or fmt org-export-date-timestamp-format
)))
4115 (cond ((not date
) nil
)
4118 (eq (org-element-type (car date
)) 'timestamp
))
4119 (org-timestamp-format (car date
) fmt
))
4125 ;; `org-export-custom-protocol-maybe' handles custom protocol defined
4126 ;; in `org-link-parameters'.
4128 ;; `org-export-get-coderef-format' returns an appropriate format
4129 ;; string for coderefs.
4131 ;; `org-export-inline-image-p' returns a non-nil value when the link
4132 ;; provided should be considered as an inline image.
4134 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
4135 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
4136 ;; returns an appropriate unique identifier.
4138 ;; `org-export-resolve-id-link' returns the first headline with
4139 ;; specified id or custom-id in parse tree, the path to the external
4140 ;; file with the id.
4142 ;; `org-export-resolve-coderef' associates a reference to a line
4143 ;; number in the element it belongs, or returns the reference itself
4144 ;; when the element isn't numbered.
4146 ;; `org-export-file-uri' expands a filename as stored in :path value
4147 ;; of a "file" link into a file URI.
4149 ;; Broken links raise a `org-link-broken' error, which is caught by
4150 ;; `org-export-data' for further processing, depending on
4151 ;; `org-export-with-broken-links' value.
4153 (org-define-error 'org-link-broken
"Unable to resolve link; aborting")
4155 (defun org-export-custom-protocol-maybe (link desc backend
)
4156 "Try exporting LINK with a dedicated function.
4158 DESC is its description, as a string, or nil. BACKEND is the
4159 back-end used for export, as a symbol.
4161 Return output as a string, or nil if no protocol handles LINK.
4163 A custom protocol has precedence over regular back-end export.
4164 The function ignores links with an implicit type (e.g.,
4166 (let ((type (org-element-property :type link
)))
4167 (unless (or (member type
'("coderef" "custom-id" "fuzzy" "radio"))
4169 (let ((protocol (org-link-get-parameter type
:export
)))
4170 (and (functionp protocol
)
4172 (org-link-unescape (org-element-property :path link
))
4176 (defun org-export-get-coderef-format (path desc
)
4177 "Return format string for code reference link.
4178 PATH is the link path. DESC is its description."
4180 (cond ((not desc
) "%s")
4181 ((string-match (regexp-quote (concat "(" path
")")) desc
)
4182 (replace-match "%s" t t desc
))
4185 (defun org-export-inline-image-p (link &optional rules
)
4186 "Non-nil if LINK object points to an inline image.
4188 Optional argument is a set of RULES defining inline images. It
4189 is an alist where associations have the following shape:
4193 Applying a rule means apply REGEXP against LINK's path when its
4194 type is TYPE. The function will return a non-nil value if any of
4195 the provided rules is non-nil. The default rule is
4196 `org-export-default-inline-image-rule'.
4198 This only applies to links without a description."
4199 (and (not (org-element-contents link
))
4200 (let ((case-fold-search t
))
4201 (cl-some (lambda (rule)
4202 (and (string= (org-element-property :type link
) (car rule
))
4203 (string-match-p (cdr rule
)
4204 (org-element-property :path link
))))
4205 (or rules org-export-default-inline-image-rule
)))))
4207 (defun org-export-insert-image-links (data info
&optional rules
)
4208 "Insert image links in DATA.
4210 Org syntax does not support nested links. Nevertheless, some
4211 export back-ends support images as descriptions of links. Since
4212 images are really links to image files, we need to make an
4213 exception about links nesting.
4215 This function recognizes links whose contents are really images
4216 and turn them into proper nested links. It is meant to be used
4217 as a parse tree filter in back-ends supporting such constructs.
4219 DATA is a parse tree. INFO is the current state of the export
4220 process, as a plist.
4222 A description is a valid images if it matches any rule in RULES,
4223 if non-nil, or `org-export-default-inline-image-rule' otherwise.
4224 See `org-export-inline-image-p' for more information about the
4227 Return modified DATA."
4228 (let ((link-re (format "\\`\\(?:%s\\|%s\\)\\'"
4231 (case-fold-search t
))
4232 (org-element-map data
'link
4234 (let ((contents (org-element-interpret-data (org-element-contents l
))))
4235 (when (and (org-string-nw-p contents
)
4236 (string-match link-re contents
))
4237 (let ((type (match-string 1 contents
))
4238 (path (match-string 2 contents
)))
4239 (when (cl-some (lambda (rule)
4240 (and (string= type
(car rule
))
4241 (string-match-p (cdr rule
) path
)))
4242 (or rules org-export-default-inline-image-rule
))
4243 ;; Replace contents with image link.
4244 (org-element-adopt-elements
4245 (org-element-set-contents l nil
)
4247 (save-excursion (insert contents
))
4248 (org-element-link-parser))))))))
4252 (defun org-export-resolve-coderef (ref info
)
4253 "Resolve a code reference REF.
4255 INFO is a plist used as a communication channel.
4257 Return associated line number in source code, or REF itself,
4258 depending on src-block or example element's switches. Throw an
4259 error if no block contains REF."
4260 (or (org-element-map (plist-get info
:parse-tree
) '(example-block src-block
)
4263 (insert (org-trim (org-element-property :value el
)))
4264 (let* ((label-fmt (or (org-element-property :label-fmt el
)
4265 org-coderef-label-format
))
4266 (ref-re (org-src-coderef-regexp label-fmt ref
)))
4267 ;; Element containing REF is found. Resolve it to
4268 ;; either a label or a line number, as needed.
4269 (when (re-search-backward ref-re nil t
)
4270 (if (org-element-property :use-labels el
) ref
4271 (+ (or (org-export-get-loc el info
) 0)
4272 (line-number-at-pos)))))))
4274 (signal 'org-link-broken
(list ref
))))
4276 (defun org-export-search-cells (datum)
4277 "List search cells for element or object DATUM.
4279 A search cell follows the pattern (TYPE . SEARCH) where
4281 TYPE is a symbol among `headline', `custom-id', `target' and
4284 SEARCH is the string a link is expected to match. More
4287 - headline's title, as a list of strings, if TYPE is
4290 - CUSTOM_ID value, as a string, if TYPE is `custom-id'.
4292 - target's or radio-target's name as a list of strings if
4295 - NAME affiliated keyword if TYPE is `other'.
4297 A search cell is the internal representation of a fuzzy link. It
4298 ignores white spaces and statistics cookies, if applicable."
4299 (pcase (org-element-type datum
)
4301 (let ((title (split-string
4302 (replace-regexp-in-string
4303 "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]" ""
4304 (org-element-property :raw-value datum
)))))
4307 (cons 'headline title
)
4309 (let ((custom-id (org-element-property :custom-id datum
)))
4310 (and custom-id
(cons 'custom-id custom-id
)))))))
4312 (list (cons 'target
(split-string (org-element-property :value datum
)))))
4313 ((and (let name
(org-element-property :name datum
))
4315 (list (cons 'other
(split-string name
))))
4318 (defun org-export-string-to-search-cell (s)
4319 "Return search cells associated to string S.
4320 S is either the path of a fuzzy link or a search option, i.e., it
4321 tries to match either a headline (through custom ID or title),
4322 a target or a named element."
4323 (pcase (string-to-char s
)
4324 (?
* (list (cons 'headline
(split-string (substring s
1)))))
4325 (?
# (list (cons 'custom-id
(substring s
1))))
4326 ((let search
(split-string s
))
4327 (list (cons 'target search
) (cons 'other search
)))))
4329 (defun org-export-match-search-cell-p (datum cells
)
4330 "Non-nil when DATUM matches search cells CELLS.
4331 DATUM is an element or object. CELLS is a list of search cells,
4332 as returned by `org-export-search-cells'."
4333 (let ((targets (org-export-search-cells datum
)))
4334 (and targets
(cl-some (lambda (cell) (member cell targets
)) cells
))))
4336 (defun org-export-resolve-fuzzy-link (link info
)
4337 "Return LINK destination.
4339 INFO is a plist holding contextual information.
4341 Return value can be an object or an element:
4343 - If LINK path matches a target object (i.e. <<path>>) return it.
4345 - If LINK path exactly matches the name affiliated keyword
4346 (i.e. #+NAME: path) of an element, return that element.
4348 - If LINK path exactly matches any headline name, return that
4351 - Otherwise, throw an error.
4353 Assume LINK type is \"fuzzy\". White spaces are not
4355 (let* ((search-cells (org-export-string-to-search-cell
4356 (org-link-unescape (org-element-property :path link
))))
4357 (link-cache (or (plist-get info
:resolve-fuzzy-link-cache
)
4358 (let ((table (make-hash-table :test
#'eq
)))
4359 (plist-put info
:resolve-fuzzy-link-cache table
)
4361 (cached (gethash search-cells link-cache
'not-found
)))
4362 (if (not (eq cached
'not-found
)) cached
4364 (org-element-map (plist-get info
:parse-tree
)
4365 (cons 'target org-element-all-elements
)
4367 (and (org-export-match-search-cell-p datum search-cells
)
4370 (signal 'org-link-broken
(list (org-element-property :path link
))))
4373 ;; There can be multiple matches for un-typed searches, i.e.,
4374 ;; for searches not starting with # or *. In this case,
4375 ;; prioritize targets and names over headline titles.
4376 ;; Matching both a name and a target is not valid, and
4377 ;; therefore undefined.
4378 (or (cl-some (lambda (datum)
4379 (and (not (eq (org-element-type datum
) 'headline
))
4385 (defun org-export-resolve-id-link (link info
)
4386 "Return headline referenced as LINK destination.
4388 INFO is a plist used as a communication channel.
4390 Return value can be the headline element matched in current parse
4391 tree or a file name. Assume LINK type is either \"id\" or
4392 \"custom-id\". Throw an error if no match is found."
4393 (let ((id (org-element-property :path link
)))
4394 ;; First check if id is within the current parse tree.
4395 (or (org-element-map (plist-get info
:parse-tree
) 'headline
4397 (when (or (equal (org-element-property :ID headline
) id
)
4398 (equal (org-element-property :CUSTOM_ID headline
) id
))
4401 ;; Otherwise, look for external files.
4402 (cdr (assoc id
(plist-get info
:id-alist
)))
4403 (signal 'org-link-broken
(list id
)))))
4405 (defun org-export-resolve-radio-link (link info
)
4406 "Return radio-target object referenced as LINK destination.
4408 INFO is a plist used as a communication channel.
4410 Return value can be a radio-target object or nil. Assume LINK
4411 has type \"radio\"."
4412 (let ((path (replace-regexp-in-string
4413 "[ \r\t\n]+" " " (org-element-property :path link
))))
4414 (org-element-map (plist-get info
:parse-tree
) 'radio-target
4416 (and (eq (compare-strings
4417 (replace-regexp-in-string
4418 "[ \r\t\n]+" " " (org-element-property :value radio
))
4419 nil nil path nil nil t
)
4422 info
'first-match
)))
4424 (defun org-export-file-uri (filename)
4425 "Return file URI associated to FILENAME."
4426 (cond ((string-prefix-p "//" filename
) (concat "file:" filename
))
4427 ((not (file-name-absolute-p filename
)) filename
)
4428 ((org-file-remote-p filename
) (concat "file:/" filename
))
4430 (let ((fullname (expand-file-name filename
)))
4431 (concat (if (string-prefix-p "/" fullname
) "file://" "file:///")
4436 ;; `org-export-get-reference' associate a unique reference for any
4437 ;; object or element. It uses `org-export-new-reference' and
4438 ;; `org-export-format-reference' to, respectively, generate new
4439 ;; internal references and turn them into a string suitable for
4442 ;; `org-export-get-ordinal' associates a sequence number to any object
4445 (defun org-export-new-reference (references)
4446 "Return a unique reference, among REFERENCES.
4447 REFERENCES is an alist whose values are in-use references, as
4448 numbers. Returns a number, which is the internal representation
4449 of a reference. See also `org-export-format-reference'."
4450 ;; Generate random 7 digits hexadecimal numbers. Collisions
4451 ;; increase exponentially with the numbers of references. However,
4452 ;; the odds for encountering at least one collision with 1000 active
4453 ;; references in the same document are roughly 0.2%, so this
4454 ;; shouldn't be the bottleneck.
4455 (let ((new (random #x10000000
)))
4456 (while (rassq new references
) (setq new
(random #x10000000
)))
4459 (defun org-export-format-reference (reference)
4460 "Format REFERENCE into a string.
4461 REFERENCE is a number representing a reference, as returned by
4462 `org-export-new-reference', which see."
4463 (format "org%07x" reference
))
4465 (defun org-export-get-reference (datum info
)
4466 "Return a unique reference for DATUM, as a string.
4468 DATUM is either an element or an object. INFO is the current
4469 export state, as a plist.
4471 References for the current document are stored in
4472 `:internal-references' property. Its value is an alist with
4473 associations of the following types:
4475 (REFERENCE . DATUM) and (SEARCH-CELL . ID)
4477 REFERENCE is the reference string to be used for object or
4478 element DATUM. SEARCH-CELL is a search cell, as returned by
4479 `org-export-search-cells'. ID is a number or a string uniquely
4480 identifying DATUM within the document.
4482 This function also checks `:crossrefs' property for search cells
4483 matching DATUM before creating a new reference."
4484 (let ((cache (plist-get info
:internal-references
)))
4485 (or (car (rassq datum cache
))
4486 (let* ((crossrefs (plist-get info
:crossrefs
))
4487 (cells (org-export-search-cells datum
))
4488 ;; Preserve any pre-existing association between
4489 ;; a search cell and a reference, i.e., when some
4490 ;; previously published document referenced a location
4491 ;; within current file (see
4492 ;; `org-publish-resolve-external-link').
4494 ;; However, there is no guarantee that search cells are
4495 ;; unique, e.g., there might be duplicate custom ID or
4496 ;; two headings with the same title in the file.
4498 ;; As a consequence, before re-using any reference to
4499 ;; an element or object, we check that it doesn't refer
4500 ;; to a previous element or object.
4503 (let ((stored (cdr (assoc cell crossrefs
))))
4505 (let ((old (org-export-format-reference stored
)))
4506 (and (not (assoc old cache
)) stored
)))))
4508 (org-export-new-reference cache
)))
4509 (reference-string (org-export-format-reference new
)))
4510 ;; Cache contains both data already associated to
4511 ;; a reference and in-use internal references, so as to make
4512 ;; unique references.
4513 (dolist (cell cells
) (push (cons cell new
) cache
))
4514 ;; Retain a direct association between reference string and
4515 ;; DATUM since (1) not every object or element can be given
4516 ;; a search cell (2) it permits quick lookup.
4517 (push (cons reference-string datum
) cache
)
4518 (plist-put info
:internal-references cache
)
4519 reference-string
))))
4521 (defun org-export-get-ordinal (element info
&optional types predicate
)
4522 "Return ordinal number of an element or object.
4524 ELEMENT is the element or object considered. INFO is the plist
4525 used as a communication channel.
4527 Optional argument TYPES, when non-nil, is a list of element or
4528 object types, as symbols, that should also be counted in.
4529 Otherwise, only provided element's type is considered.
4531 Optional argument PREDICATE is a function returning a non-nil
4532 value if the current element or object should be counted in. It
4533 accepts two arguments: the element or object being considered and
4534 the plist used as a communication channel. This allows counting
4535 only a certain type of object (i.e. inline images).
4537 Return value is a list of numbers if ELEMENT is a headline or an
4538 item. It is nil for keywords. It represents the footnote number
4539 for footnote definitions and footnote references. If ELEMENT is
4540 a target, return the same value as if ELEMENT was the closest
4541 table, item or headline containing the target. In any other
4542 case, return the sequence number of ELEMENT among elements or
4543 objects of the same type."
4544 ;; Ordinal of a target object refer to the ordinal of the closest
4545 ;; table, item, or headline containing the object.
4546 (when (eq (org-element-type element
) 'target
)
4548 (org-element-lineage
4550 '(footnote-definition footnote-reference headline item table
))))
4551 (cl-case (org-element-type element
)
4552 ;; Special case 1: A headline returns its number as a list.
4553 (headline (org-export-get-headline-number element info
))
4554 ;; Special case 2: An item returns its number as a list.
4555 (item (let ((struct (org-element-property :structure element
)))
4556 (org-list-get-item-number
4557 (org-element-property :begin element
)
4559 (org-list-prevs-alist struct
)
4560 (org-list-parents-alist struct
))))
4561 ((footnote-definition footnote-reference
)
4562 (org-export-get-footnote-number element info
))
4565 ;; Increment counter until ELEMENT is found again.
4566 (org-element-map (plist-get info
:parse-tree
)
4567 (or types
(org-element-type element
))
4570 ((eq element el
) (1+ counter
))
4571 ((not predicate
) (cl-incf counter
) nil
)
4572 ((funcall predicate el info
) (cl-incf counter
) nil
)))
4573 info
'first-match
)))))
4578 ;; `org-export-get-loc' counts number of code lines accumulated in
4579 ;; src-block or example-block elements with a "+n" switch until
4580 ;; a given element, excluded. Note: "-n" switches reset that count.
4582 ;; `org-export-unravel-code' extracts source code (along with a code
4583 ;; references alist) from an `element-block' or `src-block' type
4586 ;; `org-export-format-code' applies a formatting function to each line
4587 ;; of code, providing relative line number and code reference when
4588 ;; appropriate. Since it doesn't access the original element from
4589 ;; which the source code is coming, it expects from the code calling
4590 ;; it to know if lines should be numbered and if code references
4593 ;; Eventually, `org-export-format-code-default' is a higher-level
4594 ;; function (it makes use of the two previous functions) which handles
4595 ;; line numbering and code references inclusion, and returns source
4596 ;; code in a format suitable for plain text or verbatim output.
4598 (defun org-export-get-loc (element info
)
4599 "Return count of lines of code before ELEMENT.
4601 ELEMENT is an example-block or src-block element. INFO is the
4602 plist used as a communication channel.
4604 Count includes every line of code in example-block or src-block
4605 with a \"+n\" or \"-n\" switch before block. Return nil if
4606 ELEMENT doesn't allow line numbering."
4607 (pcase (org-element-property :number-lines element
)
4611 (org-element-map (plist-get info
:parse-tree
) '(src-block example-block
)
4613 ;; ELEMENT is reached: Quit loop and return locs.
4614 (if (eq el element
) (+ loc n
)
4615 ;; Only count lines from src-block and example-block
4616 ;; elements with a "+n" or "-n" switch.
4617 (let ((linum (org-element-property :number-lines el
)))
4619 (let ((lines (org-count-lines
4620 (org-element-property :value el
))))
4621 ;; Accumulate locs or reset them.
4623 (`(new .
,n
) (setq loc
(+ n lines
)))
4624 (`(continued .
,n
) (cl-incf loc
(+ n lines
)))))))
4625 nil
)) ;Return nil to stay in the loop.
4626 info
'first-match
)))))
4628 (defun org-export-unravel-code (element)
4629 "Clean source code and extract references out of it.
4631 ELEMENT has either a `src-block' an `example-block' type.
4633 Return a cons cell whose CAR is the source code, cleaned from any
4634 reference, protective commas and spurious indentation, and CDR is
4635 an alist between relative line number (integer) and name of code
4636 reference on that line (string)."
4637 (let* ((line 0) refs
4638 (value (org-element-property :value element
))
4639 ;; Remove global indentation from code, if necessary. Also
4640 ;; remove final newline character, since it doesn't belongs
4641 ;; to the code proper.
4642 (code (replace-regexp-in-string
4644 (if (or org-src-preserve-indentation
4645 (org-element-property :preserve-indent element
))
4647 (org-remove-indentation value
))))
4648 ;; Build a regexp matching a loc with a reference.
4649 (ref-re (org-src-coderef-regexp (org-src-coderef-format element
))))
4652 ;; Code with references removed.
4656 (if (not (string-match ref-re loc
)) loc
4657 ;; Ref line: remove ref, and add its position in REFS.
4658 (push (cons line
(match-string 3 loc
)) refs
)
4659 (replace-match "" nil nil loc
1)))
4660 (split-string code
"\n") "\n")
4664 (defun org-export-format-code (code fun
&optional num-lines ref-alist
)
4665 "Format CODE by applying FUN line-wise and return it.
4667 CODE is a string representing the code to format. FUN is
4668 a function. It must accept three arguments: a line of
4669 code (string), the current line number (integer) or nil and the
4670 reference associated to the current line (string) or nil.
4672 Optional argument NUM-LINES can be an integer representing the
4673 number of code lines accumulated until the current code. Line
4674 numbers passed to FUN will take it into account. If it is nil,
4675 FUN's second argument will always be nil. This number can be
4676 obtained with `org-export-get-loc' function.
4678 Optional argument REF-ALIST can be an alist between relative line
4679 number (i.e. ignoring NUM-LINES) and the name of the code
4680 reference on it. If it is nil, FUN's third argument will always
4681 be nil. It can be obtained through the use of
4682 `org-export-unravel-code' function."
4683 (let ((--locs (split-string code
"\n"))
4689 (let ((--ref (cdr (assq --line ref-alist
))))
4690 (funcall fun --loc
(and num-lines
(+ num-lines --line
)) --ref
)))
4694 (defun org-export-format-code-default (element info
)
4695 "Return source code from ELEMENT, formatted in a standard way.
4697 ELEMENT is either a `src-block' or `example-block' element. INFO
4698 is a plist used as a communication channel.
4700 This function takes care of line numbering and code references
4701 inclusion. Line numbers, when applicable, appear at the
4702 beginning of the line, separated from the code by two white
4703 spaces. Code references, on the other hand, appear flushed to
4704 the right, separated by six white spaces from the widest line of
4706 ;; Extract code and references.
4707 (let* ((code-info (org-export-unravel-code element
))
4708 (code (car code-info
))
4709 (code-lines (split-string code
"\n")))
4710 (if (null code-lines
) ""
4711 (let* ((refs (and (org-element-property :retain-labels element
)
4713 ;; Handle line numbering.
4714 (num-start (org-export-get-loc element info
))
4718 (length (number-to-string
4719 (+ (length code-lines
) num-start
))))))
4720 ;; Prepare references display, if required. Any reference
4721 ;; should start six columns after the widest line of code,
4722 ;; wrapped with parenthesis.
4724 (+ (apply 'max
(mapcar 'length code-lines
))
4725 (if (not num-start
) 0 (length (format num-fmt num-start
))))))
4726 (org-export-format-code
4728 (lambda (loc line-num ref
)
4729 (let ((number-str (and num-fmt
(format num-fmt line-num
))))
4734 (concat (make-string (- (+ 6 max-width
)
4735 (+ (length loc
) (length number-str
)))
4737 (format "(%s)" ref
))))))
4743 ;; `org-export-table-has-special-column-p' and and
4744 ;; `org-export-table-row-is-special-p' are predicates used to look for
4745 ;; meta-information about the table structure.
4747 ;; `org-table-has-header-p' tells when the rows before the first rule
4748 ;; should be considered as table's header.
4750 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4751 ;; and `org-export-table-cell-borders' extract information from
4752 ;; a table-cell element.
4754 ;; `org-export-table-dimensions' gives the number on rows and columns
4755 ;; in the table, ignoring horizontal rules and special columns.
4756 ;; `org-export-table-cell-address', given a table-cell object, returns
4757 ;; the absolute address of a cell. On the other hand,
4758 ;; `org-export-get-table-cell-at' does the contrary.
4760 ;; `org-export-table-cell-starts-colgroup-p',
4761 ;; `org-export-table-cell-ends-colgroup-p',
4762 ;; `org-export-table-row-starts-rowgroup-p',
4763 ;; `org-export-table-row-ends-rowgroup-p',
4764 ;; `org-export-table-row-starts-header-p',
4765 ;; `org-export-table-row-ends-header-p' and
4766 ;; `org-export-table-row-in-header-p' indicate position of current row
4767 ;; or cell within the table.
4769 (defun org-export-table-has-special-column-p (table)
4770 "Non-nil when TABLE has a special column.
4771 All special columns will be ignored during export."
4772 ;; The table has a special column when every first cell of every row
4773 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4774 ;; "*" "_" and "^". Though, do not consider a first column
4775 ;; containing only empty cells as special.
4776 (let ((special-column?
'empty
))
4778 (dolist (row (org-element-contents table
))
4779 (when (eq (org-element-property :type row
) 'standard
)
4780 (let ((value (org-element-contents
4781 (car (org-element-contents row
)))))
4782 (cond ((member value
4783 '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4784 (setq special-column?
'special
))
4786 (t (throw 'exit nil
))))))
4787 (eq special-column?
'special
))))
4789 (defun org-export-table-has-header-p (table info
)
4790 "Non-nil when TABLE has a header.
4792 INFO is a plist used as a communication channel.
4794 A table has a header when it contains at least two row groups."
4795 (let* ((cache (or (plist-get info
:table-header-cache
)
4796 (let ((table (make-hash-table :test
#'eq
)))
4797 (plist-put info
:table-header-cache table
)
4799 (cached (gethash table cache
'no-cache
)))
4800 (if (not (eq cached
'no-cache
)) cached
4801 (let ((rowgroup 1) row-flag
)
4803 (org-element-map table
'table-row
4808 (eq (org-element-property :type row
) 'rule
))
4810 (setq row-flag nil
))
4811 ((and (not row-flag
)
4812 (eq (org-element-property :type row
) 'standard
))
4818 (defun org-export-table-row-is-special-p (table-row _
)
4819 "Non-nil if TABLE-ROW is considered special.
4820 All special rows will be ignored during export."
4821 (when (eq (org-element-property :type table-row
) 'standard
)
4822 (let ((first-cell (org-element-contents
4823 (car (org-element-contents table-row
)))))
4824 ;; A row is special either when...
4826 ;; ... it starts with a field only containing "/",
4827 (equal first-cell
'("/"))
4828 ;; ... the table contains a special column and the row start
4829 ;; with a marking character among, "^", "_", "$" or "!",
4830 (and (org-export-table-has-special-column-p
4831 (org-export-get-parent table-row
))
4832 (member first-cell
'(("^") ("_") ("$") ("!"))))
4833 ;; ... it contains only alignment cookies and empty cells.
4834 (let ((special-row-p 'empty
))
4836 (dolist (cell (org-element-contents table-row
))
4837 (let ((value (org-element-contents cell
)))
4838 ;; Since VALUE is a secondary string, the following
4839 ;; checks avoid expanding it with `org-export-data'.
4841 ((and (not (cdr value
))
4842 (stringp (car value
))
4843 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4845 (setq special-row-p
'cookie
))
4846 (t (throw 'exit nil
)))))
4847 (eq special-row-p
'cookie
)))))))
4849 (defun org-export-table-row-group (table-row info
)
4850 "Return TABLE-ROW's group number, as an integer.
4852 INFO is a plist used as the communication channel.
4854 Return value is the group number, as an integer, or nil for
4855 special rows and rows separators. First group is also table's
4857 (when (eq (org-element-property :type table-row
) 'standard
)
4858 (let* ((cache (or (plist-get info
:table-row-group-cache
)
4859 (let ((table (make-hash-table :test
#'eq
)))
4860 (plist-put info
:table-row-group-cache table
)
4862 (cached (gethash table-row cache
'no-cache
)))
4863 (if (not (eq cached
'no-cache
)) cached
4864 ;; First time a row is queried, populate cache with all the
4865 ;; rows from the table.
4866 (let ((group 0) row-flag
)
4867 (org-element-map (org-export-get-parent table-row
) 'table-row
4869 (if (eq (org-element-property :type row
) 'rule
)
4871 (unless row-flag
(cl-incf group
) (setq row-flag t
))
4872 (puthash row group cache
)))
4874 (gethash table-row cache
)))))
4876 (defun org-export-table-cell-width (table-cell info
)
4877 "Return TABLE-CELL contents width.
4879 INFO is a plist used as the communication channel.
4881 Return value is the width given by the last width cookie in the
4882 same column as TABLE-CELL, or nil."
4883 (let* ((row (org-export-get-parent table-cell
))
4884 (table (org-export-get-parent row
))
4885 (cells (org-element-contents row
))
4886 (columns (length cells
))
4887 (column (- columns
(length (memq table-cell cells
))))
4888 (cache (or (plist-get info
:table-cell-width-cache
)
4889 (let ((table (make-hash-table :test
#'eq
)))
4890 (plist-put info
:table-cell-width-cache table
)
4892 (width-vector (or (gethash table cache
)
4893 (puthash table
(make-vector columns
'empty
) cache
)))
4894 (value (aref width-vector column
)))
4895 (if (not (eq value
'empty
)) value
4897 (dolist (row (org-element-contents table
)
4898 (aset width-vector column cookie-width
))
4899 (when (org-export-table-row-is-special-p row info
)
4900 ;; In a special row, try to find a width cookie at COLUMN.
4901 (let* ((value (org-element-contents
4902 (elt (org-element-contents row
) column
)))
4903 (cookie (car value
)))
4904 ;; The following checks avoid expanding unnecessarily
4905 ;; the cell with `org-export-data'.
4909 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie
)
4910 (match-string 1 cookie
))
4912 (string-to-number (match-string 1 cookie
)))))))))))
4914 (defun org-export-table-cell-alignment (table-cell info
)
4915 "Return TABLE-CELL contents alignment.
4917 INFO is a plist used as the communication channel.
4919 Return alignment as specified by the last alignment cookie in the
4920 same column as TABLE-CELL. If no such cookie is found, a default
4921 alignment value will be deduced from fraction of numbers in the
4922 column (see `org-table-number-fraction' for more information).
4923 Possible values are `left', `right' and `center'."
4924 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4925 (require 'org-table
)
4926 (let* ((row (org-export-get-parent table-cell
))
4927 (table (org-export-get-parent row
))
4928 (cells (org-element-contents row
))
4929 (columns (length cells
))
4930 (column (- columns
(length (memq table-cell cells
))))
4931 (cache (or (plist-get info
:table-cell-alignment-cache
)
4932 (let ((table (make-hash-table :test
#'eq
)))
4933 (plist-put info
:table-cell-alignment-cache table
)
4935 (align-vector (or (gethash table cache
)
4936 (puthash table
(make-vector columns nil
) cache
))))
4937 (or (aref align-vector column
)
4938 (let ((number-cells 0)
4941 previous-cell-number-p
)
4942 (dolist (row (org-element-contents (org-export-get-parent row
)))
4944 ;; In a special row, try to find an alignment cookie at
4946 ((org-export-table-row-is-special-p row info
)
4947 (let ((value (org-element-contents
4948 (elt (org-element-contents row
) column
))))
4949 ;; Since VALUE is a secondary string, the following
4950 ;; checks avoid useless expansion through
4951 ;; `org-export-data'.
4954 (stringp (car value
))
4955 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4957 (match-string 1 (car value
)))
4958 (setq cookie-align
(match-string 1 (car value
))))))
4959 ;; Ignore table rules.
4960 ((eq (org-element-property :type row
) 'rule
))
4961 ;; In a standard row, check if cell's contents are
4962 ;; expressing some kind of number. Increase NUMBER-CELLS
4963 ;; accordingly. Though, don't bother if an alignment
4964 ;; cookie has already defined cell's alignment.
4966 (let ((value (org-export-data
4967 (org-element-contents
4968 (elt (org-element-contents row
) column
))
4970 (cl-incf total-cells
)
4971 ;; Treat an empty cell as a number if it follows
4973 (if (not (or (string-match org-table-number-regexp value
)
4974 (and (string= value
"") previous-cell-number-p
)))
4975 (setq previous-cell-number-p nil
)
4976 (setq previous-cell-number-p t
)
4977 (cl-incf number-cells
))))))
4978 ;; Return value. Alignment specified by cookies has
4979 ;; precedence over alignment deduced from cell's contents.
4982 (cond ((equal cookie-align
"l") 'left
)
4983 ((equal cookie-align
"r") 'right
)
4984 ((equal cookie-align
"c") 'center
)
4985 ((>= (/ (float number-cells
) total-cells
)
4986 org-table-number-fraction
)
4990 (defun org-export-table-cell-borders (table-cell info
)
4991 "Return TABLE-CELL borders.
4993 INFO is a plist used as a communication channel.
4995 Return value is a list of symbols, or nil. Possible values are:
4996 `top', `bottom', `above', `below', `left' and `right'. Note:
4997 `top' (resp. `bottom') only happen for a cell in the first
4998 row (resp. last row) of the table, ignoring table rules, if any.
5000 Returned borders ignore special rows."
5001 (let* ((row (org-export-get-parent table-cell
))
5002 (table (org-export-get-parent-table table-cell
))
5004 ;; Top/above border? TABLE-CELL has a border above when a rule
5005 ;; used to demarcate row groups can be found above. Hence,
5006 ;; finding a rule isn't sufficient to push `above' in BORDERS:
5007 ;; another regular row has to be found above that rule.
5010 ;; Look at every row before the current one.
5011 (dolist (row (cdr (memq row
(reverse (org-element-contents table
)))))
5012 (cond ((eq (org-element-property :type row
) 'rule
)
5014 ((not (org-export-table-row-is-special-p row info
))
5015 (if rule-flag
(throw 'exit
(push 'above borders
))
5016 (throw 'exit nil
)))))
5017 ;; No rule above, or rule found starts the table (ignoring any
5018 ;; special row): TABLE-CELL is at the top of the table.
5019 (when rule-flag
(push 'above borders
))
5020 (push 'top borders
)))
5021 ;; Bottom/below border? TABLE-CELL has a border below when next
5022 ;; non-regular row below is a rule.
5025 ;; Look at every row after the current one.
5026 (dolist (row (cdr (memq row
(org-element-contents table
))))
5027 (cond ((eq (org-element-property :type row
) 'rule
)
5029 ((not (org-export-table-row-is-special-p row info
))
5030 (if rule-flag
(throw 'exit
(push 'below borders
))
5031 (throw 'exit nil
)))))
5032 ;; No rule below, or rule found ends the table (modulo some
5033 ;; special row): TABLE-CELL is at the bottom of the table.
5034 (when rule-flag
(push 'below borders
))
5035 (push 'bottom borders
)))
5036 ;; Right/left borders? They can only be specified by column
5037 ;; groups. Column groups are defined in a row starting with "/".
5038 ;; Also a column groups row only contains "<", "<>", ">" or blank
5041 (let ((column (let ((cells (org-element-contents row
)))
5042 (- (length cells
) (length (memq table-cell cells
))))))
5043 ;; Table rows are read in reverse order so last column groups
5044 ;; row has precedence over any previous one.
5045 (dolist (row (reverse (org-element-contents table
)))
5046 (unless (eq (org-element-property :type row
) 'rule
)
5047 (when (equal (org-element-contents
5048 (car (org-element-contents row
)))
5050 (let ((column-groups
5053 (let ((value (org-element-contents cell
)))
5054 (when (member value
'(("<") ("<>") (">") nil
))
5056 (org-element-contents row
))))
5057 ;; There's a left border when previous cell, if
5058 ;; any, ends a group, or current one starts one.
5059 (when (or (and (not (zerop column
))
5060 (member (elt column-groups
(1- column
))
5062 (member (elt column-groups column
) '("<" "<>")))
5063 (push 'left borders
))
5064 ;; There's a right border when next cell, if any,
5065 ;; starts a group, or current one ends one.
5066 (when (or (and (/= (1+ column
) (length column-groups
))
5067 (member (elt column-groups
(1+ column
))
5069 (member (elt column-groups column
) '(">" "<>")))
5070 (push 'right borders
))
5071 (throw 'exit nil
)))))))
5075 (defun org-export-table-cell-starts-colgroup-p (table-cell info
)
5076 "Non-nil when TABLE-CELL is at the beginning of a column group.
5077 INFO is a plist used as a communication channel."
5078 ;; A cell starts a column group either when it is at the beginning
5079 ;; of a row (or after the special column, if any) or when it has
5081 (or (eq (org-element-map (org-export-get-parent table-cell
) 'table-cell
5082 'identity info
'first-match
)
5084 (memq 'left
(org-export-table-cell-borders table-cell info
))))
5086 (defun org-export-table-cell-ends-colgroup-p (table-cell info
)
5087 "Non-nil when TABLE-CELL is at the end of a column group.
5088 INFO is a plist used as a communication channel."
5089 ;; A cell ends a column group either when it is at the end of a row
5090 ;; or when it has a right border.
5091 (or (eq (car (last (org-element-contents
5092 (org-export-get-parent table-cell
))))
5094 (memq 'right
(org-export-table-cell-borders table-cell info
))))
5096 (defun org-export-table-row-starts-rowgroup-p (table-row info
)
5097 "Non-nil when TABLE-ROW is at the beginning of a row group.
5098 INFO is a plist used as a communication channel."
5099 (unless (or (eq (org-element-property :type table-row
) 'rule
)
5100 (org-export-table-row-is-special-p table-row info
))
5101 (let ((borders (org-export-table-cell-borders
5102 (car (org-element-contents table-row
)) info
)))
5103 (or (memq 'top borders
) (memq 'above borders
)))))
5105 (defun org-export-table-row-ends-rowgroup-p (table-row info
)
5106 "Non-nil when TABLE-ROW is at the end of a row group.
5107 INFO is a plist used as a communication channel."
5108 (unless (or (eq (org-element-property :type table-row
) 'rule
)
5109 (org-export-table-row-is-special-p table-row info
))
5110 (let ((borders (org-export-table-cell-borders
5111 (car (org-element-contents table-row
)) info
)))
5112 (or (memq 'bottom borders
) (memq 'below borders
)))))
5114 (defun org-export-table-row-in-header-p (table-row info
)
5115 "Non-nil when TABLE-ROW is located within table's header.
5116 INFO is a plist used as a communication channel. Always return
5117 nil for special rows and rows separators."
5118 (and (org-export-table-has-header-p
5119 (org-export-get-parent-table table-row
) info
)
5120 (eql (org-export-table-row-group table-row info
) 1)))
5122 (defun org-export-table-row-starts-header-p (table-row info
)
5123 "Non-nil when TABLE-ROW is the first table header's row.
5124 INFO is a plist used as a communication channel."
5125 (and (org-export-table-row-in-header-p table-row info
)
5126 (org-export-table-row-starts-rowgroup-p table-row info
)))
5128 (defun org-export-table-row-ends-header-p (table-row info
)
5129 "Non-nil when TABLE-ROW is the last table header's row.
5130 INFO is a plist used as a communication channel."
5131 (and (org-export-table-row-in-header-p table-row info
)
5132 (org-export-table-row-ends-rowgroup-p table-row info
)))
5134 (defun org-export-table-row-number (table-row info
)
5135 "Return TABLE-ROW number.
5136 INFO is a plist used as a communication channel. Return value is
5137 zero-indexed and ignores separators. The function returns nil
5138 for special rows and separators."
5139 (when (eq (org-element-property :type table-row
) 'standard
)
5140 (let* ((cache (or (plist-get info
:table-row-number-cache
)
5141 (let ((table (make-hash-table :test
#'eq
)))
5142 (plist-put info
:table-row-number-cache table
)
5144 (cached (gethash table-row cache
'no-cache
)))
5145 (if (not (eq cached
'no-cache
)) cached
5146 ;; First time a row is queried, populate cache with all the
5147 ;; rows from the table.
5149 (org-element-map (org-export-get-parent-table table-row
) 'table-row
5151 (when (eq (org-element-property :type row
) 'standard
)
5152 (puthash row
(cl-incf number
) cache
)))
5154 (gethash table-row cache
)))))
5156 (defun org-export-table-dimensions (table info
)
5157 "Return TABLE dimensions.
5159 INFO is a plist used as a communication channel.
5161 Return value is a CONS like (ROWS . COLUMNS) where
5162 ROWS (resp. COLUMNS) is the number of exportable
5163 rows (resp. columns)."
5164 (let (first-row (columns 0) (rows 0))
5165 ;; Set number of rows, and extract first one.
5166 (org-element-map table
'table-row
5168 (when (eq (org-element-property :type row
) 'standard
)
5170 (unless first-row
(setq first-row row
)))) info
)
5171 ;; Set number of columns.
5172 (org-element-map first-row
'table-cell
(lambda (_) (cl-incf columns
)) info
)
5174 (cons rows columns
)))
5176 (defun org-export-table-cell-address (table-cell info
)
5177 "Return address of a regular TABLE-CELL object.
5179 TABLE-CELL is the cell considered. INFO is a plist used as
5180 a communication channel.
5182 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
5183 zero-based index. Only exportable cells are considered. The
5184 function returns nil for other cells."
5185 (let* ((table-row (org-export-get-parent table-cell
))
5186 (row-number (org-export-table-row-number table-row info
)))
5189 (let ((col-count 0))
5190 (org-element-map table-row
'table-cell
5192 (if (eq cell table-cell
) col-count
(cl-incf col-count
) nil
))
5193 info
'first-match
))))))
5195 (defun org-export-get-table-cell-at (address table info
)
5196 "Return regular table-cell object at ADDRESS in TABLE.
5198 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
5199 zero-based index. TABLE is a table type element. INFO is
5200 a plist used as a communication channel.
5202 If no table-cell, among exportable cells, is found at ADDRESS,
5204 (let ((column-pos (cdr address
)) (column-count 0))
5206 ;; Row at (car address) or nil.
5207 (let ((row-pos (car address
)) (row-count 0))
5208 (org-element-map table
'table-row
5210 (cond ((eq (org-element-property :type row
) 'rule
) nil
)
5211 ((= row-count row-pos
) row
)
5212 (t (cl-incf row-count
) nil
)))
5216 (if (= column-count column-pos
) cell
5217 (cl-incf column-count
) nil
))
5218 info
'first-match
)))
5221 ;;;; For Tables of Contents
5223 ;; `org-export-collect-headlines' builds a list of all exportable
5224 ;; headline elements, maybe limited to a certain depth. One can then
5225 ;; easily parse it and transcode it.
5227 ;; Building lists of tables, figures or listings is quite similar.
5228 ;; Once the generic function `org-export-collect-elements' is defined,
5229 ;; `org-export-collect-tables', `org-export-collect-figures' and
5230 ;; `org-export-collect-listings' can be derived from it.
5232 ;; `org-export-toc-entry-backend' builds a special anonymous back-end
5233 ;; useful to export table of contents' entries.
5235 (defun org-export-collect-headlines (info &optional n scope
)
5236 "Collect headlines in order to build a table of contents.
5238 INFO is a plist used as a communication channel.
5240 When optional argument N is an integer, it specifies the depth of
5241 the table of contents. Otherwise, it is set to the value of the
5242 last headline level. See `org-export-headline-levels' for more
5245 Optional argument SCOPE, when non-nil, is an element. If it is
5246 a headline, only children of SCOPE are collected. Otherwise,
5247 collect children of the headline containing provided element. If
5248 there is no such headline, collect all headlines. In any case,
5249 argument N becomes relative to the level of that headline.
5251 Return a list of all exportable headlines as parsed elements.
5252 Footnote sections are ignored."
5253 (let* ((scope (cond ((not scope
) (plist-get info
:parse-tree
))
5254 ((eq (org-element-type scope
) 'headline
) scope
)
5255 ((org-export-get-parent-headline scope
))
5256 (t (plist-get info
:parse-tree
))))
5257 (limit (plist-get info
:headline-levels
))
5258 (n (if (not (wholenump n
)) limit
5259 (min (if (eq (org-element-type scope
) 'org-data
) n
5260 (+ (org-export-get-relative-level scope info
) n
))
5262 (org-element-map (org-element-contents scope
) 'headline
5264 (and (not (org-element-property :footnote-section-p h
))
5266 (org-export-get-node-property :UNNUMBERED h t
)))
5267 (>= n
(org-export-get-relative-level h info
))
5271 (defun org-export-collect-elements (type info
&optional predicate
)
5272 "Collect referenceable elements of a determined type.
5274 TYPE can be a symbol or a list of symbols specifying element
5275 types to search. Only elements with a caption are collected.
5277 INFO is a plist used as a communication channel.
5279 When non-nil, optional argument PREDICATE is a function accepting
5280 one argument, an element of type TYPE. It returns a non-nil
5281 value when that element should be collected.
5283 Return a list of all elements found, in order of appearance."
5284 (org-element-map (plist-get info
:parse-tree
) type
5286 (and (org-element-property :caption element
)
5287 (or (not predicate
) (funcall predicate element
))
5291 (defun org-export-collect-tables (info)
5292 "Build a list of tables.
5293 INFO is a plist used as a communication channel.
5295 Return a list of table elements with a caption."
5296 (org-export-collect-elements 'table info
))
5298 (defun org-export-collect-figures (info predicate
)
5299 "Build a list of figures.
5301 INFO is a plist used as a communication channel. PREDICATE is
5302 a function which accepts one argument: a paragraph element and
5303 whose return value is non-nil when that element should be
5306 A figure is a paragraph type element, with a caption, verifying
5307 PREDICATE. The latter has to be provided since a \"figure\" is
5308 a vague concept that may depend on back-end.
5310 Return a list of elements recognized as figures."
5311 (org-export-collect-elements 'paragraph info predicate
))
5313 (defun org-export-collect-listings (info)
5314 "Build a list of src blocks.
5316 INFO is a plist used as a communication channel.
5318 Return a list of src-block elements with a caption."
5319 (org-export-collect-elements 'src-block info
))
5321 (defun org-export-excluded-from-toc-p (headline info
)
5322 "Non-nil if HEADLINE should be excluded from tables of contents.
5324 INFO is a plist used as a communication channel.
5326 Note that such headlines are already excluded from
5327 `org-export-collect-headlines'. Therefore, this function is not
5328 necessary if you only need to list headlines in the table of
5329 contents. However, it is useful if some additional processing is
5330 required on headlines excluded from table of contents."
5331 (or (org-element-property :footnote-section-p headline
)
5332 (org-export-low-level-p headline info
)
5333 (equal "notoc" (org-export-get-node-property :UNNUMBERED headline t
))))
5335 (defun org-export-toc-entry-backend (parent &rest transcoders
)
5336 "Return an export back-end appropriate for table of contents entries.
5338 PARENT is an export back-end the returned back-end should inherit
5341 By default, the back-end removes footnote references and targets.
5342 It also changes links and radio targets into regular text.
5343 TRANSCODERS optional argument, when non-nil, specifies additional
5344 transcoders. A transcoder follows the pattern (TYPE . FUNCTION)
5345 where type is an element or object type and FUNCTION the function
5347 (declare (indent 1))
5348 (org-export-create-backend
5352 `((footnote-reference .
,#'ignore
)
5353 (link .
,(lambda (l c i
)
5356 (org-element-property :raw-link l
)
5358 (radio-target .
,(lambda (_r c _
) c
))
5359 (target .
,#'ignore
)))))
5364 ;; The main function for the smart quotes sub-system is
5365 ;; `org-export-activate-smart-quotes', which replaces every quote in
5366 ;; a given string from the parse tree with its "smart" counterpart.
5368 ;; Dictionary for smart quotes is stored in
5369 ;; `org-export-smart-quotes-alist'.
5371 (defconst org-export-smart-quotes-alist
5374 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5375 :texinfo
"@guillemetleft{}")
5377 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5378 :texinfo
"@guillemetright{}")
5379 (secondary-opening :utf-8
"‹" :html
"‹" :latex
"\\guilsinglleft{}"
5380 :texinfo
"@guilsinglleft{}")
5381 (secondary-closing :utf-8
"›" :html
"›" :latex
"\\guilsinglright{}"
5382 :texinfo
"@guilsinglright{}")
5383 (apostrophe :utf-8
"’" :html
"’"))
5385 ;; one may use: »...«, "...", ›...‹, or '...'.
5386 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
5387 ;; LaTeX quotes require Babel!
5389 :utf-8
"»" :html
"»" :latex
">>" :texinfo
"@guillemetright{}")
5391 :utf-8
"«" :html
"«" :latex
"<<" :texinfo
"@guillemetleft{}")
5393 :utf-8
"›" :html
"›" :latex
"\\frq{}" :texinfo
"@guilsinglright{}")
5395 :utf-8
"‹" :html
"‹" :latex
"\\flq{}" :texinfo
"@guilsingleft{}")
5396 (apostrophe :utf-8
"’" :html
"’"))
5399 :utf-8
"„" :html
"„" :latex
"\"`" :texinfo
"@quotedblbase{}")
5401 :utf-8
"“" :html
"“" :latex
"\"'" :texinfo
"@quotedblleft{}")
5403 :utf-8
"‚" :html
"‚" :latex
"\\glq{}" :texinfo
"@quotesinglbase{}")
5405 :utf-8
"‘" :html
"‘" :latex
"\\grq{}" :texinfo
"@quoteleft{}")
5406 (apostrophe :utf-8
"’" :html
"’"))
5408 (primary-opening :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
5409 (primary-closing :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
5410 (secondary-opening :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5411 (secondary-closing :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5412 (apostrophe :utf-8
"’" :html
"’"))
5415 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5416 :texinfo
"@guillemetleft{}")
5418 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5419 :texinfo
"@guillemetright{}")
5420 (secondary-opening :utf-8
"“" :html
"“" :latex
"``" :texinfo
"``")
5421 (secondary-closing :utf-8
"”" :html
"”" :latex
"''" :texinfo
"''")
5422 (apostrophe :utf-8
"’" :html
"’"))
5425 :utf-8
"« " :html
"« " :latex
"\\og "
5426 :texinfo
"@guillemetleft{}@tie{}")
5428 :utf-8
" »" :html
" »" :latex
"\\fg{}"
5429 :texinfo
"@tie{}@guillemetright{}")
5431 :utf-8
"« " :html
"« " :latex
"\\og "
5432 :texinfo
"@guillemetleft{}@tie{}")
5433 (secondary-closing :utf-8
" »" :html
" »" :latex
"\\fg{}"
5434 :texinfo
"@tie{}@guillemetright{}")
5435 (apostrophe :utf-8
"’" :html
"’"))
5438 :utf-8
"„" :html
"„" :latex
"\"`" :texinfo
"@quotedblbase{}")
5440 :utf-8
"“" :html
"“" :latex
"\"'" :texinfo
"@quotedblleft{}")
5442 :utf-8
"‚" :html
"‚" :latex
"\\glq{}" :texinfo
"@quotesinglbase{}")
5444 :utf-8
"‘" :html
"‘" :latex
"\\grq{}" :texinfo
"@quoteleft{}")
5445 (apostrophe :utf-8
"’" :html
"’"))
5447 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5449 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5450 :texinfo
"@guillemetleft{}")
5452 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5453 :texinfo
"@guillemetright{}")
5454 (secondary-opening :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5455 (secondary-closing :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5456 (apostrophe :utf-8
"’" :html
"’"))
5458 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5460 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5461 :texinfo
"@guillemetleft{}")
5463 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5464 :texinfo
"@guillemetright{}")
5465 (secondary-opening :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5466 (secondary-closing :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5467 (apostrophe :utf-8
"’" :html
"’"))
5469 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5471 :utf-8
"«" :html
"«" :latex
"\\guillemotleft{}"
5472 :texinfo
"@guillemetleft{}")
5474 :utf-8
"»" :html
"»" :latex
"\\guillemotright{}"
5475 :texinfo
"@guillemetright{}")
5476 (secondary-opening :utf-8
"‘" :html
"‘" :latex
"`" :texinfo
"`")
5477 (secondary-closing :utf-8
"’" :html
"’" :latex
"'" :texinfo
"'")
5478 (apostrophe :utf-8
"’" :html
"’"))
5480 ;; http://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B2%D1%8B%D1%87%D0%BA%D0%B8#.D0.9A.D0.B0.D0.B2.D1.8B.D1.87.D0.BA.D0.B8.2C_.D0.B8.D1.81.D0.BF.D0.BE.D0.BB.D1.8C.D0.B7.D1.83.D0.B5.D0.BC.D1.8B.D0.B5_.D0.B2_.D1.80.D1.83.D1.81.D1.81.D0.BA.D0.BE.D0.BC_.D1.8F.D0.B7.D1.8B.D0.BA.D0.B5
5481 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5482 (primary-opening :utf-8
"«" :html
"«" :latex
"{}<<"
5483 :texinfo
"@guillemetleft{}")
5484 (primary-closing :utf-8
"»" :html
"»" :latex
">>{}"
5485 :texinfo
"@guillemetright{}")
5487 :utf-8
"„" :html
"„" :latex
"\\glqq{}" :texinfo
"@quotedblbase{}")
5489 :utf-8
"“" :html
"“" :latex
"\\grqq{}" :texinfo
"@quotedblleft{}")
5490 (apostrophe :utf-8
"’" :html
: "'"))
5492 ;; Based on https://sl.wikipedia.org/wiki/Narekovaj
5493 (primary-opening :utf-8
"«" :html
"«" :latex
"{}<<"
5494 :texinfo
"@guillemetleft{}")
5495 (primary-closing :utf-8
"»" :html
"»" :latex
">>{}"
5496 :texinfo
"@guillemetright{}")
5498 :utf-8
"„" :html
"„" :latex
"\\glqq{}" :texinfo
"@quotedblbase{}")
5500 :utf-8
"“" :html
"“" :latex
"\\grqq{}" :texinfo
"@quotedblleft{}")
5501 (apostrophe :utf-8
"’" :html
"’"))
5503 ;; Based on https://sv.wikipedia.org/wiki/Citattecken
5504 (primary-opening :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5505 (primary-closing :utf-8
"”" :html
"”" :latex
"’’" :texinfo
"’’")
5506 (secondary-opening :utf-8
"’" :html
"’" :latex
"’" :texinfo
"`")
5507 (secondary-closing :utf-8
"’" :html
"’" :latex
"’" :texinfo
"'")
5508 (apostrophe :utf-8
"’" :html
"’")))
5509 "Smart quotes translations.
5511 Alist whose CAR is a language string and CDR is an alist with
5512 quote type as key and a plist associating various encodings to
5513 their translation as value.
5515 A quote type can be any symbol among `primary-opening',
5516 `primary-closing', `secondary-opening', `secondary-closing' and
5519 Valid encodings include `:utf-8', `:html', `:latex' and
5522 If no translation is found, the quote character is left as-is.")
5524 (defun org-export--smart-quote-status (s info
)
5525 "Return smart quote status at the beginning of string S.
5526 INFO is the current export state, as a plist."
5527 (let* ((parent (org-element-property :parent s
))
5528 (cache (or (plist-get info
:smart-quote-cache
)
5529 (let ((table (make-hash-table :test
#'eq
)))
5530 (plist-put info
:smart-quote-cache table
)
5532 (value (gethash parent cache
'missing-data
)))
5533 (if (not (eq value
'missing-data
)) (cdr (assq s value
))
5534 (let (level1-open full-status
)
5536 (let ((secondary (org-element-secondary-p s
)))
5537 (if secondary
(org-element-property secondary parent
)
5538 (org-element-contents parent
)))
5541 (let ((start 0) current-status
)
5542 (while (setq start
(string-match "['\"]" text start
))
5545 ((equal (match-string 0 text
) "\"")
5546 (setf level1-open
(not level1-open
))
5547 (if level1-open
'primary-opening
'primary-closing
))
5548 ;; Not already in a level 1 quote: this is an
5550 ((not level1-open
) 'apostrophe
)
5551 ;; Extract previous char and next char. As
5552 ;; a special case, they can also be set to `blank',
5553 ;; `no-blank' or nil. Then determine if current
5554 ;; match is allowed as an opening quote or a closing
5558 (if (> start
0) (substring text
(1- start
) start
)
5559 (let ((p (org-export-get-previous-element
5562 ((stringp p
) (substring p -
1))
5563 ((memq (org-element-property :post-blank p
)
5568 (if (< (1+ start
) (length text
))
5569 (substring text
(1+ start
) (+ start
2))
5570 (let ((n (org-export-get-next-element text info
)))
5572 ((stringp n
) (substring n
0 1))
5575 (and (if (stringp previous
)
5576 (string-match "\\s\"\\|\\s-\\|\\s("
5578 (memq previous
'(blank nil
)))
5580 (string-match "\\w\\|\\s.\\|\\s_" next
)
5581 (eq next
'no-blank
))))
5583 (and (if (stringp previous
)
5584 (string-match "\\w\\|\\s.\\|\\s_" previous
)
5585 (eq previous
'no-blank
))
5587 (string-match "\\s-\\|\\s)\\|\\s.\\|\\s\""
5589 (memq next
'(blank nil
))))))
5591 ((and allow-open allow-close
) (error "Should not happen"))
5592 (allow-open 'secondary-opening
)
5593 (allow-close 'secondary-closing
)
5597 (when current-status
5598 (push (cons text
(nreverse current-status
)) full-status
))))
5599 info nil org-element-recursive-objects
)
5600 (puthash parent full-status cache
)
5601 (cdr (assq s full-status
))))))
5603 (defun org-export-activate-smart-quotes (s encoding info
&optional original
)
5604 "Replace regular quotes with \"smart\" quotes in string S.
5606 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5607 `:utf-8'. INFO is a plist used as a communication channel.
5609 The function has to retrieve information about string
5610 surroundings in parse tree. It can only happen with an
5611 unmodified string. Thus, if S has already been through another
5612 process, a non-nil ORIGINAL optional argument will provide that
5615 Return the new string."
5617 (copy-sequence (org-export--smart-quote-status (or original s
) info
))))
5618 (replace-regexp-in-string
5622 (cdr (assq (pop quote-status
)
5623 (cdr (assoc (plist-get info
:language
)
5624 org-export-smart-quotes-alist
))))
5631 ;; Here are various functions to retrieve information about the
5632 ;; neighborhood of a given element or object. Neighbors of interest
5633 ;; are direct parent (`org-export-get-parent'), parent headline
5634 ;; (`org-export-get-parent-headline'), first element containing an
5635 ;; object, (`org-export-get-parent-element'), parent table
5636 ;; (`org-export-get-parent-table'), previous element or object
5637 ;; (`org-export-get-previous-element') and next element or object
5638 ;; (`org-export-get-next-element').
5640 ;; defsubst org-export-get-parent must be defined before first use
5642 (defun org-export-get-parent-headline (blob)
5643 "Return BLOB parent headline or nil.
5644 BLOB is the element or object being considered."
5645 (org-element-lineage blob
'(headline)))
5647 (defun org-export-get-parent-element (object)
5648 "Return first element containing OBJECT or nil.
5649 OBJECT is the object to consider."
5650 (org-element-lineage object org-element-all-elements
))
5652 (defun org-export-get-parent-table (object)
5653 "Return OBJECT parent table or nil.
5654 OBJECT is either a `table-cell' or `table-element' type object."
5655 (org-element-lineage object
'(table)))
5657 (defun org-export-get-previous-element (blob info
&optional n
)
5658 "Return previous element or object.
5660 BLOB is an element or object. INFO is a plist used as
5661 a communication channel. Return previous exportable element or
5662 object, a string, or nil.
5664 When optional argument N is a positive integer, return a list
5665 containing up to N siblings before BLOB, from farthest to
5666 closest. With any other non-nil value, return a list containing
5668 (let* ((secondary (org-element-secondary-p blob
))
5669 (parent (org-export-get-parent blob
))
5671 (if secondary
(org-element-property secondary parent
)
5672 (org-element-contents parent
)))
5675 (dolist (obj (cdr (memq blob
(reverse siblings
))) prev
)
5676 (cond ((memq obj
(plist-get info
:ignore-list
)))
5677 ((null n
) (throw 'exit obj
))
5678 ((not (wholenump n
)) (push obj prev
))
5679 ((zerop n
) (throw 'exit prev
))
5680 (t (cl-decf n
) (push obj prev
)))))))
5682 (defun org-export-get-next-element (blob info
&optional n
)
5683 "Return next element or object.
5685 BLOB is an element or object. INFO is a plist used as
5686 a communication channel. Return next exportable element or
5687 object, a string, or nil.
5689 When optional argument N is a positive integer, return a list
5690 containing up to N siblings after BLOB, from closest to farthest.
5691 With any other non-nil value, return a list containing all of
5693 (let* ((secondary (org-element-secondary-p blob
))
5694 (parent (org-export-get-parent blob
))
5697 (if secondary
(org-element-property secondary parent
)
5698 (org-element-contents parent
)))))
5701 (dolist (obj siblings
(nreverse next
))
5702 (cond ((memq obj
(plist-get info
:ignore-list
)))
5703 ((null n
) (throw 'exit obj
))
5704 ((not (wholenump n
)) (push obj next
))
5705 ((zerop n
) (throw 'exit
(nreverse next
)))
5706 (t (cl-decf n
) (push obj next
)))))))
5711 ;; `org-export-translate' translates a string according to the language
5712 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5713 ;; the dictionary used for the translation.
5715 (defconst org-export-dictionary
5717 ("fr" :default
"%e %n : %c" :html
"%e %n : %c"))
5719 ("ar" :default
"تأليف")
5720 ("ca" :default
"Autor")
5721 ("cs" :default
"Autor")
5722 ("da" :default
"Forfatter")
5723 ("de" :default
"Autor")
5724 ("eo" :html
"Aŭtoro")
5725 ("es" :default
"Autor")
5726 ("et" :default
"Autor")
5727 ("fi" :html
"Tekijä")
5728 ("fr" :default
"Auteur")
5729 ("hu" :default
"Szerzõ")
5730 ("is" :html
"Höfundur")
5731 ("it" :default
"Autore")
5732 ("ja" :default
"著者" :html
"著者")
5733 ("nl" :default
"Auteur")
5734 ("no" :default
"Forfatter")
5735 ("nb" :default
"Forfatter")
5736 ("nn" :default
"Forfattar")
5737 ("pl" :default
"Autor")
5738 ("pt_BR" :default
"Autor")
5739 ("ru" :html
"Автор" :utf-8
"Автор")
5740 ("sl" :default
"Avtor")
5741 ("sv" :html
"Författare")
5742 ("uk" :html
"Автор" :utf-8
"Автор")
5743 ("zh-CN" :html
"作者" :utf-8
"作者")
5744 ("zh-TW" :html
"作者" :utf-8
"作者"))
5745 ("Continued from previous page"
5746 ("ar" :default
"تتمة الصفحة السابقة")
5747 ("cs" :default
"Pokračování z předchozí strany")
5748 ("de" :default
"Fortsetzung von vorheriger Seite")
5749 ("es" :html
"Continúa de la página anterior" :ascii
"Continua de la pagina anterior" :default
"Continúa de la página anterior")
5750 ("fr" :default
"Suite de la page précédente")
5751 ("it" :default
"Continua da pagina precedente")
5752 ("ja" :default
"前ページからの続き")
5753 ("nl" :default
"Vervolg van vorige pagina")
5754 ("pt" :default
"Continuação da página anterior")
5755 ("ru" :html
"(Продолжение)"
5756 :utf-8
"(Продолжение)")
5757 ("sl" :default
"Nadaljevanje s prejšnje strani"))
5758 ("Continued on next page"
5759 ("ar" :default
"التتمة في الصفحة التالية")
5760 ("cs" :default
"Pokračuje na další stránce")
5761 ("de" :default
"Fortsetzung nächste Seite")
5762 ("es" :html
"Continúa en la siguiente página" :ascii
"Continua en la siguiente pagina" :default
"Continúa en la siguiente página")
5763 ("fr" :default
"Suite page suivante")
5764 ("it" :default
"Continua alla pagina successiva")
5765 ("ja" :default
"次ページに続く")
5766 ("nl" :default
"Vervolg op volgende pagina")
5767 ("pt" :default
"Continua na página seguinte")
5768 ("ru" :html
"(Продолжение следует)"
5769 :utf-8
"(Продолжение следует)")
5770 ("sl" :default
"Nadaljevanje na naslednji strani"))
5772 ("cs" :default
"Vytvořeno")
5773 ("sl" :default
"Ustvarjeno"))
5775 ("ar" :default
"بتاريخ")
5776 ("ca" :default
"Data")
5777 ("cs" :default
"Datum")
5778 ("da" :default
"Dato")
5779 ("de" :default
"Datum")
5780 ("eo" :default
"Dato")
5781 ("es" :default
"Fecha")
5782 ("et" :html
"Kuupäev" :utf-8
"Kuupäev")
5783 ("fi" :html
"Päivämäärä")
5784 ("hu" :html
"Dátum")
5785 ("is" :default
"Dagsetning")
5786 ("it" :default
"Data")
5787 ("ja" :default
"日付" :html
"日付")
5788 ("nl" :default
"Datum")
5789 ("no" :default
"Dato")
5790 ("nb" :default
"Dato")
5791 ("nn" :default
"Dato")
5792 ("pl" :default
"Data")
5793 ("pt_BR" :default
"Data")
5794 ("ru" :html
"Дата" :utf-8
"Дата")
5795 ("sl" :default
"Datum")
5796 ("sv" :default
"Datum")
5797 ("uk" :html
"Дата" :utf-8
"Дата")
5798 ("zh-CN" :html
"日期" :utf-8
"日期")
5799 ("zh-TW" :html
"日期" :utf-8
"日期"))
5801 ("ar" :default
"معادلة")
5802 ("cs" :default
"Rovnice")
5803 ("da" :default
"Ligning")
5804 ("de" :default
"Gleichung")
5805 ("es" :ascii
"Ecuacion" :html
"Ecuación" :default
"Ecuación")
5806 ("et" :html
"Võrrand" :utf-8
"Võrrand")
5807 ("fr" :ascii
"Equation" :default
"Équation")
5808 ("is" :default
"Jafna")
5809 ("ja" :default
"方程式")
5810 ("no" :default
"Ligning")
5811 ("nb" :default
"Ligning")
5812 ("nn" :default
"Likning")
5813 ("pt_BR" :html
"Equação" :default
"Equação" :ascii
"Equacao")
5814 ("ru" :html
"Уравнение"
5816 ("sl" :default
"Enačba")
5817 ("sv" :default
"Ekvation")
5818 ("zh-CN" :html
"方程" :utf-8
"方程"))
5820 ("ar" :default
"شكل")
5821 ("cs" :default
"Obrázek")
5822 ("da" :default
"Figur")
5823 ("de" :default
"Abbildung")
5824 ("es" :default
"Figura")
5825 ("et" :default
"Joonis")
5826 ("is" :default
"Mynd")
5827 ("ja" :default
"図" :html
"図")
5828 ("no" :default
"Illustrasjon")
5829 ("nb" :default
"Illustrasjon")
5830 ("nn" :default
"Illustrasjon")
5831 ("pt_BR" :default
"Figura")
5832 ("ru" :html
"Рисунок" :utf-8
"Рисунок")
5833 ("sv" :default
"Illustration")
5834 ("zh-CN" :html
"图" :utf-8
"图"))
5836 ("ar" :default
"شكل %d:")
5837 ("cs" :default
"Obrázek %d:")
5838 ("da" :default
"Figur %d")
5839 ("de" :default
"Abbildung %d:")
5840 ("es" :default
"Figura %d:")
5841 ("et" :default
"Joonis %d:")
5842 ("fr" :default
"Figure %d :" :html
"Figure %d :")
5843 ("is" :default
"Mynd %d")
5844 ("ja" :default
"図%d: " :html
"図%d: ")
5845 ("no" :default
"Illustrasjon %d")
5846 ("nb" :default
"Illustrasjon %d")
5847 ("nn" :default
"Illustrasjon %d")
5848 ("pt_BR" :default
"Figura %d:")
5849 ("ru" :html
"Рис. %d.:" :utf-8
"Рис. %d.:")
5850 ("sl" :default
"Slika %d")
5851 ("sv" :default
"Illustration %d")
5852 ("zh-CN" :html
"图%d " :utf-8
"图%d "))
5854 ("ar" :default
"الهوامش")
5855 ("ca" :html
"Peus de pàgina")
5856 ("cs" :default
"Poznámky pod čarou")
5857 ("da" :default
"Fodnoter")
5858 ("de" :html
"Fußnoten" :default
"Fußnoten")
5859 ("eo" :default
"Piednotoj")
5860 ("es" :ascii
"Nota al pie de pagina" :html
"Nota al pie de página" :default
"Nota al pie de página")
5861 ("et" :html
"Allmärkused" :utf-8
"Allmärkused")
5862 ("fi" :default
"Alaviitteet")
5863 ("fr" :default
"Notes de bas de page")
5864 ("hu" :html
"Lábjegyzet")
5865 ("is" :html
"Aftanmálsgreinar")
5866 ("it" :html
"Note a piè di pagina")
5867 ("ja" :default
"脚注" :html
"脚注")
5868 ("nl" :default
"Voetnoten")
5869 ("no" :default
"Fotnoter")
5870 ("nb" :default
"Fotnoter")
5871 ("nn" :default
"Fotnotar")
5872 ("pl" :default
"Przypis")
5873 ("pt_BR" :html
"Notas de Rodapé" :default
"Notas de Rodapé" :ascii
"Notas de Rodape")
5874 ("ru" :html
"Сноски" :utf-8
"Сноски")
5875 ("sl" :default
"Opombe")
5876 ("sv" :default
"Fotnoter")
5877 ("uk" :html
"Примітки"
5879 ("zh-CN" :html
"脚注" :utf-8
"脚注")
5880 ("zh-TW" :html
"腳註" :utf-8
"腳註"))
5882 ("ar" :default
"قائمة بالبرامج")
5883 ("cs" :default
"Seznam programů")
5884 ("da" :default
"Programmer")
5885 ("de" :default
"Programmauflistungsverzeichnis")
5886 ("es" :ascii
"Indice de Listados de programas" :html
"Índice de Listados de programas" :default
"Índice de Listados de programas")
5887 ("et" :default
"Loendite nimekiri")
5888 ("fr" :default
"Liste des programmes")
5889 ("ja" :default
"ソースコード目次")
5890 ("no" :default
"Dataprogrammer")
5891 ("nb" :default
"Dataprogrammer")
5892 ("ru" :html
"Список распечаток"
5893 :utf-8
"Список распечаток")
5894 ("sl" :default
"Seznam programskih izpisov")
5895 ("zh-CN" :html
"代码目录" :utf-8
"代码目录"))
5897 ("ar" :default
"قائمة بالجداول")
5898 ("cs" :default
"Seznam tabulek")
5899 ("da" :default
"Tabeller")
5900 ("de" :default
"Tabellenverzeichnis")
5901 ("es" :ascii
"Indice de tablas" :html
"Índice de tablas" :default
"Índice de tablas")
5902 ("et" :default
"Tabelite nimekiri")
5903 ("fr" :default
"Liste des tableaux")
5904 ("is" :default
"Töfluskrá" :html
"Töfluskrá")
5905 ("ja" :default
"表目次")
5906 ("no" :default
"Tabeller")
5907 ("nb" :default
"Tabeller")
5908 ("nn" :default
"Tabeller")
5909 ("pt_BR" :default
"Índice de Tabelas" :ascii
"Indice de Tabelas")
5910 ("ru" :html
"Список таблиц"
5911 :utf-8
"Список таблиц")
5912 ("sl" :default
"Seznam tabel")
5913 ("sv" :default
"Tabeller")
5914 ("zh-CN" :html
"表格目录" :utf-8
"表格目录"))
5916 ("ar" :default
"برنامج")
5917 ("cs" :default
"Program")
5918 ("da" :default
"Program")
5919 ("de" :default
"Programmlisting")
5920 ("es" :default
"Listado de programa")
5921 ("et" :default
"Loend")
5922 ("fr" :default
"Programme" :html
"Programme")
5923 ("ja" :default
"ソースコード")
5924 ("no" :default
"Dataprogram")
5925 ("nb" :default
"Dataprogram")
5926 ("pt_BR" :default
"Listagem")
5927 ("ru" :html
"Распечатка"
5928 :utf-8
"Распечатка")
5929 ("sl" :default
"Izpis programa")
5930 ("zh-CN" :html
"代码" :utf-8
"代码"))
5932 ("ar" :default
"برنامج %d:")
5933 ("cs" :default
"Program %d:")
5934 ("da" :default
"Program %d")
5935 ("de" :default
"Programmlisting %d")
5936 ("es" :default
"Listado de programa %d")
5937 ("et" :default
"Loend %d")
5938 ("fr" :default
"Programme %d :" :html
"Programme %d :")
5939 ("ja" :default
"ソースコード%d:")
5940 ("no" :default
"Dataprogram %d")
5941 ("nb" :default
"Dataprogram %d")
5942 ("pt_BR" :default
"Listagem %d")
5943 ("ru" :html
"Распечатка %d.:"
5944 :utf-8
"Распечатка %d.:")
5945 ("sl" :default
"Izpis programa %d")
5946 ("zh-CN" :html
"代码%d " :utf-8
"代码%d "))
5948 ("ar" :default
"المراجع")
5949 ("cs" :default
"Reference")
5950 ("fr" :ascii
"References" :default
"Références")
5951 ("de" :default
"Quellen")
5952 ("es" :default
"Referencias")
5953 ("sl" :default
"Reference"))
5955 ("cs" :default
"Viz obrázek %s")
5956 ("fr" :default
"cf. figure %s"
5957 :html
"cf. figure %s" :latex
"cf.~figure~%s")
5958 ("sl" :default
"Glej sliko %s"))
5960 ("cs" :default
"Viz program %s")
5961 ("fr" :default
"cf. programme %s"
5962 :html
"cf. programme %s" :latex
"cf.~programme~%s")
5963 ("sl" :default
"Glej izpis programa %s"))
5965 ("ar" :default
"انظر قسم %s")
5966 ("cs" :default
"Viz sekce %s")
5967 ("da" :default
"jævnfør afsnit %s")
5968 ("de" :default
"siehe Abschnitt %s")
5969 ("es" :ascii
"Vea seccion %s" :html
"Vea sección %s" :default
"Vea sección %s")
5970 ("et" :html
"Vaata peatükki %s" :utf-8
"Vaata peatükki %s")
5971 ("fr" :default
"cf. section %s")
5972 ("ja" :default
"セクション %s を参照")
5973 ("pt_BR" :html
"Veja a seção %s" :default
"Veja a seção %s"
5974 :ascii
"Veja a secao %s")
5975 ("ru" :html
"См. раздел %s"
5976 :utf-8
"См. раздел %s")
5977 ("sl" :default
"Glej poglavje %d")
5978 ("zh-CN" :html
"参见第%s节" :utf-8
"参见第%s节"))
5980 ("cs" :default
"Viz tabulka %s")
5981 ("fr" :default
"cf. tableau %s"
5982 :html
"cf. tableau %s" :latex
"cf.~tableau~%s")
5983 ("sl" :default
"Glej tabelo %s"))
5985 ("ar" :default
"جدول")
5986 ("cs" :default
"Tabulka")
5987 ("de" :default
"Tabelle")
5988 ("es" :default
"Tabla")
5989 ("et" :default
"Tabel")
5990 ("fr" :default
"Tableau")
5991 ("is" :default
"Tafla")
5992 ("ja" :default
"表" :html
"表")
5993 ("pt_BR" :default
"Tabela")
5994 ("ru" :html
"Таблица"
5996 ("zh-CN" :html
"表" :utf-8
"表"))
5998 ("ar" :default
"جدول %d:")
5999 ("cs" :default
"Tabulka %d:")
6000 ("da" :default
"Tabel %d")
6001 ("de" :default
"Tabelle %d")
6002 ("es" :default
"Tabla %d")
6003 ("et" :default
"Tabel %d")
6004 ("fr" :default
"Tableau %d :")
6005 ("is" :default
"Tafla %d")
6006 ("ja" :default
"表%d:" :html
"表%d:")
6007 ("no" :default
"Tabell %d")
6008 ("nb" :default
"Tabell %d")
6009 ("nn" :default
"Tabell %d")
6010 ("pt_BR" :default
"Tabela %d")
6011 ("ru" :html
"Таблица %d.:"
6012 :utf-8
"Таблица %d.:")
6013 ("sl" :default
"Tabela %d")
6014 ("sv" :default
"Tabell %d")
6015 ("zh-CN" :html
"表%d " :utf-8
"表%d "))
6016 ("Table of Contents"
6017 ("ar" :default
"قائمة المحتويات")
6018 ("ca" :html
"Índex")
6019 ("cs" :default
"Obsah")
6020 ("da" :default
"Indhold")
6021 ("de" :default
"Inhaltsverzeichnis")
6022 ("eo" :default
"Enhavo")
6023 ("es" :ascii
"Indice" :html
"Índice" :default
"Índice")
6024 ("et" :default
"Sisukord")
6025 ("fi" :html
"Sisällysluettelo")
6026 ("fr" :ascii
"Sommaire" :default
"Table des matières")
6027 ("hu" :html
"Tartalomjegyzék")
6028 ("is" :default
"Efnisyfirlit")
6029 ("it" :default
"Indice")
6030 ("ja" :default
"目次" :html
"目次")
6031 ("nl" :default
"Inhoudsopgave")
6032 ("no" :default
"Innhold")
6033 ("nb" :default
"Innhold")
6034 ("nn" :default
"Innhald")
6035 ("pl" :html
"Spis treści")
6036 ("pt_BR" :html
"Índice" :utf8
"Índice" :ascii
"Indice")
6037 ("ru" :html
"Содержание"
6038 :utf-8
"Содержание")
6039 ("sl" :default
"Kazalo")
6040 ("sv" :html
"Innehåll")
6041 ("uk" :html
"Зміст" :utf-8
"Зміст")
6042 ("zh-CN" :html
"目录" :utf-8
"目录")
6043 ("zh-TW" :html
"目錄" :utf-8
"目錄"))
6044 ("Unknown reference"
6045 ("ar" :default
"مرجع غير معرّف")
6046 ("da" :default
"ukendt reference")
6047 ("de" :default
"Unbekannter Verweis")
6048 ("es" :default
"Referencia desconocida")
6049 ("et" :default
"Tundmatu viide")
6050 ("fr" :ascii
"Destination inconnue" :default
"Référence inconnue")
6051 ("ja" :default
"不明な参照先")
6052 ("pt_BR" :default
"Referência desconhecida"
6053 :ascii
"Referencia desconhecida")
6054 ("ru" :html
"Неизвестная ссылка"
6055 :utf-8
"Неизвестная ссылка")
6056 ("sl" :default
"Neznana referenca")
6057 ("zh-CN" :html
"未知引用" :utf-8
"未知引用")))
6058 "Dictionary for export engine.
6060 Alist whose car is the string to translate and cdr is an alist
6061 whose car is the language string and cdr is a plist whose
6062 properties are possible charsets and values translated terms.
6064 It is used as a database for `org-export-translate'. Since this
6065 function returns the string as-is if no translation was found,
6066 the variable only needs to record values different from the
6069 (defun org-export-translate (s encoding info
)
6070 "Translate string S according to language specification.
6072 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
6073 and `:utf-8'. INFO is a plist used as a communication channel.
6075 Translation depends on `:language' property. Return the
6076 translated string. If no translation is found, try to fall back
6077 to `:default' encoding. If it fails, return S."
6078 (let* ((lang (plist-get info
:language
))
6079 (translations (cdr (assoc lang
6080 (cdr (assoc s org-export-dictionary
))))))
6081 (or (plist-get translations encoding
)
6082 (plist-get translations
:default
)
6087 ;;; Asynchronous Export
6089 ;; `org-export-async-start' is the entry point for asynchronous
6090 ;; export. It recreates current buffer (including visibility,
6091 ;; narrowing and visited file) in an external Emacs process, and
6092 ;; evaluates a command there. It then applies a function on the
6093 ;; returned results in the current process.
6095 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
6096 ;; allow exporting to a buffer or a file, asynchronously or not.
6098 ;; `org-export-output-file-name' is an auxiliary function meant to be
6099 ;; used with `org-export-to-file'. With a given extension, it tries
6100 ;; to provide a canonical file name to write export output to.
6102 ;; Asynchronously generated results are never displayed directly.
6103 ;; Instead, they are stored in `org-export-stack-contents'. They can
6104 ;; then be retrieved by calling `org-export-stack'.
6106 ;; Export Stack is viewed through a dedicated major mode
6107 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
6108 ;;`org-export-stack-delete', `org-export-stack-view' and
6109 ;;`org-export-stack-clear'.
6111 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
6112 ;; It should be used whenever `org-export-async-start' is called.
6114 (defmacro org-export-async-start
(fun &rest body
)
6115 "Call function FUN on the results returned by BODY evaluation.
6117 FUN is an anonymous function of one argument. BODY evaluation
6118 happens in an asynchronous process, from a buffer which is an
6119 exact copy of the current one.
6121 Use `org-export-add-to-stack' in FUN in order to register results
6124 This is a low level function. See also `org-export-to-buffer'
6125 and `org-export-to-file' for more specialized functions."
6126 (declare (indent 1) (debug t
))
6127 (org-with-gensyms (process temp-file copy-fun proc-buffer coding
)
6128 ;; Write the full sexp evaluating BODY in a copy of the current
6129 ;; buffer to a temporary file, as it may be too long for program
6130 ;; args in `start-process'.
6131 `(with-temp-message "Initializing asynchronous export process"
6132 (let ((,copy-fun
(org-export--generate-copy-script (current-buffer)))
6133 (,temp-file
(make-temp-file "org-export-process"))
6134 (,coding buffer-file-coding-system
))
6135 (with-temp-file ,temp-file
6137 ;; Null characters (from variable values) are inserted
6138 ;; within the file. As a consequence, coding system for
6139 ;; buffer contents will not be recognized properly. So,
6140 ;; we make sure it is the same as the one used to display
6141 ;; the original buffer.
6142 (format ";; -*- coding: %s; -*-\n%S"
6145 (when org-export-async-debug
'(setq debug-on-error t
))
6146 ;; Ignore `kill-emacs-hook' and code evaluation
6147 ;; queries from Babel as we need a truly
6148 ;; non-interactive process.
6149 (setq kill-emacs-hook nil
6150 org-babel-confirm-evaluate-answer-no t
)
6151 ;; Initialize export framework.
6153 ;; Re-create current buffer there.
6154 (funcall ,,copy-fun
)
6155 (restore-buffer-modified-p nil
)
6156 ;; Sexp to evaluate in the buffer.
6157 (print (progn ,,@body
))))))
6158 ;; Start external process.
6159 (let* ((process-connection-type nil
)
6160 (,proc-buffer
(generate-new-buffer-name "*Org Export Process*"))
6165 (list "org-export-process"
6167 (expand-file-name invocation-name invocation-directory
)
6169 (if org-export-async-init-file
6170 (list "-Q" "-l" org-export-async-init-file
)
6171 (list "-l" user-init-file
))
6172 (list "-l" ,temp-file
)))))
6173 ;; Register running process in stack.
6174 (org-export-add-to-stack (get-buffer ,proc-buffer
) nil
,process
)
6175 ;; Set-up sentinel in order to catch results.
6176 (let ((handler ,fun
))
6177 (set-process-sentinel
6180 (let ((proc-buffer (process-buffer p
)))
6181 (when (eq (process-status p
) 'exit
)
6183 (if (zerop (process-exit-status p
))
6186 (with-current-buffer proc-buffer
6187 (goto-char (point-max))
6189 (read (current-buffer)))))
6190 (funcall ,handler results
))
6191 (unless org-export-async-debug
6192 (and (get-buffer proc-buffer
)
6193 (kill-buffer proc-buffer
))))
6194 (org-export-add-to-stack proc-buffer nil p
)
6196 (message "Process `%s' exited abnormally" p
))
6197 (unless org-export-async-debug
6198 (delete-file ,,temp-file
)))))))))))))
6201 (defun org-export-to-buffer
6203 &optional async subtreep visible-only body-only ext-plist
6205 "Call `org-export-as' with output to a specified buffer.
6207 BACKEND is either an export back-end, as returned by, e.g.,
6208 `org-export-create-backend', or a symbol referring to
6209 a registered back-end.
6211 BUFFER is the name of the output buffer. If it already exists,
6212 it will be erased first, otherwise, it will be created.
6214 A non-nil optional argument ASYNC means the process should happen
6215 asynchronously. The resulting buffer should then be accessible
6216 through the `org-export-stack' interface. When ASYNC is nil, the
6217 buffer is displayed if `org-export-show-temporary-export-buffer'
6220 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
6221 EXT-PLIST are similar to those used in `org-export-as', which
6224 Optional argument POST-PROCESS is a function which should accept
6225 no argument. It is always called within the current process,
6226 from BUFFER, with point at its beginning. Export back-ends can
6227 use it to set a major mode there, e.g,
6229 (defun org-latex-export-as-latex
6230 (&optional async subtreep visible-only body-only ext-plist)
6232 (org-export-to-buffer \\='latex \"*Org LATEX Export*\"
6233 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
6235 This function returns BUFFER."
6236 (declare (indent 2))
6238 (org-export-async-start
6240 (with-current-buffer (get-buffer-create ,buffer
)
6242 (setq buffer-file-coding-system
',buffer-file-coding-system
)
6244 (goto-char (point-min))
6245 (org-export-add-to-stack (current-buffer) ',backend
)
6246 (ignore-errors (funcall ,post-process
))))
6248 ',backend
,subtreep
,visible-only
,body-only
',ext-plist
))
6250 (org-export-as backend subtreep visible-only body-only ext-plist
))
6251 (buffer (get-buffer-create buffer
))
6252 (encoding buffer-file-coding-system
))
6253 (when (and (org-string-nw-p output
) (org-export--copy-to-kill-ring-p))
6254 (org-kill-new output
))
6255 (with-current-buffer buffer
6257 (setq buffer-file-coding-system encoding
)
6259 (goto-char (point-min))
6260 (and (functionp post-process
) (funcall post-process
)))
6261 (when org-export-show-temporary-export-buffer
6262 (switch-to-buffer-other-window buffer
))
6266 (defun org-export-to-file
6267 (backend file
&optional async subtreep visible-only body-only ext-plist
6269 "Call `org-export-as' with output to a specified file.
6271 BACKEND is either an export back-end, as returned by, e.g.,
6272 `org-export-create-backend', or a symbol referring to
6273 a registered back-end. FILE is the name of the output file, as
6276 A non-nil optional argument ASYNC means the process should happen
6277 asynchronously. The resulting buffer will then be accessible
6278 through the `org-export-stack' interface.
6280 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
6281 EXT-PLIST are similar to those used in `org-export-as', which
6284 Optional argument POST-PROCESS is called with FILE as its
6285 argument and happens asynchronously when ASYNC is non-nil. It
6286 has to return a file name, or nil. Export back-ends can use this
6287 to send the output file through additional processing, e.g,
6289 (defun org-latex-export-to-latex
6290 (&optional async subtreep visible-only body-only ext-plist)
6292 (let ((outfile (org-export-output-file-name \".tex\" subtreep)))
6293 (org-export-to-file \\='latex outfile
6294 async subtreep visible-only body-only ext-plist
6295 (lambda (file) (org-latex-compile file)))
6297 The function returns either a file name returned by POST-PROCESS,
6299 (declare (indent 2))
6300 (if (not (file-writable-p file
)) (error "Output file not writable")
6301 (let ((ext-plist (org-combine-plists `(:output-file
,file
) ext-plist
))
6302 (encoding (or org-export-coding-system buffer-file-coding-system
)))
6304 (org-export-async-start
6306 (org-export-add-to-stack (expand-file-name file
) ',backend
))
6309 ',backend
,subtreep
,visible-only
,body-only
6313 (let ((coding-system-for-write ',encoding
))
6314 (write-file ,file
)))
6315 (or (ignore-errors (funcall ',post-process
,file
)) ,file
)))
6316 (let ((output (org-export-as
6317 backend subtreep visible-only body-only ext-plist
)))
6320 (let ((coding-system-for-write encoding
))
6322 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output
))
6323 (org-kill-new output
))
6324 ;; Get proper return value.
6325 (or (and (functionp post-process
) (funcall post-process file
))
6328 (defun org-export-output-file-name (extension &optional subtreep pub-dir
)
6329 "Return output file's name according to buffer specifications.
6331 EXTENSION is a string representing the output file extension,
6332 with the leading dot.
6334 With a non-nil optional argument SUBTREEP, try to determine
6335 output file's name by looking for \"EXPORT_FILE_NAME\" property
6336 of subtree at point.
6338 When optional argument PUB-DIR is set, use it as the publishing
6341 Return file name as a string."
6342 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
6345 (file-name-sans-extension
6347 ;; Check EXPORT_FILE_NAME subtree property.
6348 (and subtreep
(org-entry-get nil
"EXPORT_FILE_NAME" 'selective
))
6349 ;; Check #+EXPORT_FILE_NAME keyword.
6350 (org-with-point-at (point-min)
6352 (let ((case-fold-search t
))
6353 (while (re-search-forward
6354 "^[ \t]*#\\+EXPORT_FILE_NAME:[ \t]+\\S-" nil t
)
6355 (let ((element (org-element-at-point)))
6356 (when (eq 'keyword
(org-element-type element
))
6358 (org-element-property :value element
))))))))
6359 ;; Extract from buffer's associated file, if any.
6360 (and visited-file
(file-name-nondirectory visited-file
))
6361 ;; Can't determine file name on our own: ask user.
6363 "Output file: " pub-dir nil nil nil
6364 (lambda (n) (string= extension
(file-name-extension n t
))))))
6367 ;; Build file name. Enforce EXTENSION over whatever user
6368 ;; may have come up with. PUB-DIR, if defined, always has
6369 ;; precedence over any provided path.
6371 (pub-dir (concat (file-name-as-directory pub-dir
)
6372 (file-name-nondirectory base-name
)))
6373 ((file-name-absolute-p base-name
) base-name
)
6375 ;; If writing to OUTPUT-FILE would overwrite original file, append
6376 ;; EXTENSION another time to final name.
6377 (if (and visited-file
(file-equal-p visited-file output-file
))
6378 (concat output-file extension
)
6381 (defun org-export-add-to-stack (source backend
&optional process
)
6382 "Add a new result to export stack if not present already.
6384 SOURCE is a buffer or a file name containing export results.
6385 BACKEND is a symbol representing export back-end used to generate
6388 Entries already pointing to SOURCE and unavailable entries are
6389 removed beforehand. Return the new stack."
6390 (setq org-export-stack-contents
6391 (cons (list source backend
(or process
(current-time)))
6392 (org-export-stack-remove source
))))
6394 (defun org-export-stack ()
6395 "Menu for asynchronous export results and running processes."
6397 (let ((buffer (get-buffer-create "*Org Export Stack*")))
6398 (with-current-buffer buffer
6399 (org-export-stack-mode)
6400 (tabulated-list-print t
))
6401 (pop-to-buffer buffer
))
6402 (message "Type \"q\" to quit, \"?\" for help"))
6404 (defun org-export-stack-clear ()
6405 "Remove all entries from export stack."
6407 (setq org-export-stack-contents nil
))
6409 (defun org-export-stack-refresh ()
6410 "Refresh the export stack."
6412 (tabulated-list-print t
))
6414 (defun org-export-stack-remove (&optional source
)
6415 "Remove export results at point from stack.
6416 If optional argument SOURCE is non-nil, remove it instead."
6418 (let ((source (or source
(org-export--stack-source-at-point))))
6419 (setq org-export-stack-contents
6420 (cl-remove-if (lambda (el) (equal (car el
) source
))
6421 org-export-stack-contents
))))
6423 (defun org-export-stack-view (&optional in-emacs
)
6424 "View export results at point in stack.
6425 With an optional prefix argument IN-EMACS, force viewing files
6428 (let ((source (org-export--stack-source-at-point)))
6429 (cond ((processp source
)
6430 (org-switch-to-buffer-other-window (process-buffer source
)))
6431 ((bufferp source
) (org-switch-to-buffer-other-window source
))
6432 (t (org-open-file source in-emacs
)))))
6434 (defvar org-export-stack-mode-map
6435 (let ((km (make-sparse-keymap)))
6436 (set-keymap-parent km tabulated-list-mode-map
)
6437 (define-key km
" " 'next-line
)
6438 (define-key km
"\C-n" 'next-line
)
6439 (define-key km
[down] 'next-line)
6440 (define-key km "\C-p" 'previous-line)
6441 (define-key km "\C-?" 'previous-line)
6442 (define-key km [up] 'previous-line)
6443 (define-key km "C" 'org-export-stack-clear)
6444 (define-key km "v" 'org-export-stack-view)
6445 (define-key km (kbd "RET") 'org-export-stack-view)
6446 (define-key km "d" 'org-export-stack-remove)
6448 "Keymap for Org Export Stack.")
6450 (define-derived-mode org-export-stack-mode tabulated-list-mode "Org-Stack"
6451 "Mode for displaying asynchronous export stack.
6453 Type `\\[org-export-stack]' to visualize the asynchronous export
6456 In an Org Export Stack buffer, use \
6457 \\<org-export-stack-mode-map>`\\[org-export-stack-view]' to view export output
6458 on current line, `\\[org-export-stack-remove]' to remove it from the stack and \
6459 `\\[org-export-stack-clear]' to clear
6462 Removing entries in a stack buffer does not affect files
6463 or buffers, only display.
6465 \\{org-export-stack-mode-map}"
6466 (setq tabulated-list-format
6467 (vector (list "#" 4 #'org-export--stack-num-predicate)
6468 (list "Back-End" 12 t)
6470 (list "Source" 0 nil)))
6471 (setq tabulated-list-sort-key (cons "#" nil))
6472 (setq tabulated-list-entries #'org-export--stack-generate)
6473 (add-hook 'tabulated-list-revert-hook #'org-export--stack-generate nil t)
6474 (add-hook 'post-command-hook #'org-export-stack-refresh nil t)
6475 (tabulated-list-init-header))
6477 (defun org-export--stack-generate ()
6478 "Generate the asynchronous export stack for display.
6479 Unavailable sources are removed from the list. Return a list
6480 appropriate for `tabulated-list-print'."
6481 ;; Clear stack from exited processes, dead buffers or non-existent
6483 (setq org-export-stack-contents
6486 (if (processp (nth 2 el))
6487 (buffer-live-p (process-buffer (nth 2 el)))
6488 (let ((source (car el)))
6489 (if (bufferp source) (buffer-live-p source)
6490 (file-exists-p source)))))
6491 org-export-stack-contents))
6492 ;; Update `tabulated-list-entries'.
6496 (let ((source (car entry)))
6500 (number-to-string (cl-incf counter))
6502 (if (nth 1 entry) (symbol-name (nth 1 entry)) "")
6504 (let ((info (nth 2 entry)))
6505 (if (processp info) (symbol-name (process-status info))
6506 (format-seconds "%h:%.2m" (float-time (time-since info)))))
6508 (if (stringp source) source (buffer-name source))))))
6509 org-export-stack-contents)))
6511 (defun org-export--stack-num-predicate (a b)
6512 (< (string-to-number (aref (nth 1 a) 0))
6513 (string-to-number (aref (nth 1 b) 0))))
6515 (defun org-export--stack-source-at-point ()
6516 "Return source from export results at point in stack."
6517 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
6518 (if (not source) (error "Source unavailable, please refresh buffer")
6519 (let ((source-name (if (stringp source) source (buffer-name source))))
6522 (looking-at-p (concat ".* +" (regexp-quote source-name) "$")))
6524 ;; SOURCE is not consistent with current line. The stack
6525 ;; view is outdated.
6526 (error (substitute-command-keys
6527 "Source unavailable; type `\\[org-export-stack-refresh]' \
6528 to refresh buffer")))))))
6534 ;; `org-export-dispatch' is the standard interactive way to start an
6535 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6536 ;; for its interface, which, in turn, delegates response to key
6537 ;; pressed to `org-export--dispatch-action'.
6540 (defun org-export-dispatch (&optional arg)
6541 "Export dispatcher for Org mode.
6543 It provides an access to common export related tasks in a buffer.
6544 Its interface comes in two flavors: standard and expert.
6546 While both share the same set of bindings, only the former
6547 displays the valid keys associations in a dedicated buffer.
6548 Scrolling (resp. line-wise motion) in this buffer is done with
6549 SPC and DEL (resp. C-n and C-p) keys.
6551 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6552 flavor or the other.
6554 When ARG is `\\[universal-argument]', repeat the last export action, with the\
6556 set of options used back then, on the current buffer.
6558 When ARG is `\\[universal-argument] \\[universal-argument]', display the \
6559 asynchronous export stack."
6562 (cond ((equal arg '(16)) '(stack))
6563 ((and arg org-export-dispatch-last-action))
6564 (t (save-window-excursion
6567 ;; Remember where we are
6568 (move-marker org-export-dispatch-last-position
6570 (org-base-buffer (current-buffer)))
6571 ;; Get and store an export command
6572 (setq org-export-dispatch-last-action
6573 (org-export--dispatch-ui
6574 (list org-export-initial-scope
6575 (and org-export-in-background 'async))
6577 org-export-dispatch-use-expert-ui)))
6578 (and (get-buffer "*Org Export Dispatcher*")
6579 (kill-buffer "*Org Export Dispatcher*")))))))
6580 (action (car input))
6581 (optns (cdr input)))
6582 (unless (memq 'subtree optns)
6583 (move-marker org-export-dispatch-last-position nil))
6585 ;; First handle special hard-coded actions.
6586 (template (org-export-insert-default-template nil optns))
6587 (stack (org-export-stack))
6588 (publish-current-file
6589 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6590 (publish-current-project
6591 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6592 (publish-choose-project
6593 (org-publish (assoc (completing-read
6595 org-publish-project-alist nil t)
6596 org-publish-project-alist)
6598 (memq 'async optns)))
6599 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6603 ;; Repeating command, maybe move cursor to restore subtree
6605 (if (eq (marker-buffer org-export-dispatch-last-position)
6606 (org-base-buffer (current-buffer)))
6607 (goto-char org-export-dispatch-last-position)
6608 ;; We are in a different buffer, forget position.
6609 (move-marker org-export-dispatch-last-position nil)))
6611 ;; Return a symbol instead of a list to ease
6612 ;; asynchronous export macro use.
6613 (and (memq 'async optns) t)
6614 (and (memq 'subtree optns) t)
6615 (and (memq 'visible optns) t)
6616 (and (memq 'body optns) t)))))))
6618 (defun org-export--dispatch-ui (options first-key expertp)
6619 "Handle interface for `org-export-dispatch'.
6621 OPTIONS is a list containing current interactive options set for
6622 export. It can contain any of the following symbols:
6623 `body' toggles a body-only export
6624 `subtree' restricts export to current subtree
6625 `visible' restricts export to visible part of buffer.
6626 `force' force publishing files.
6627 `async' use asynchronous export process
6629 FIRST-KEY is the key pressed to select the first level menu. It
6630 is nil when this menu hasn't been selected yet.
6632 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6633 buffer is provided, but indications about currently active
6634 options are given in the prompt. Moreover, [?] allows switching
6635 back to standard interface."
6637 (lambda (key &optional access-key)
6638 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6639 ;; non-nil is the required first-level key to activate
6640 ;; KEY. When its value is t, activate KEY independently
6641 ;; on the first key, if any. A nil value means KEY will
6642 ;; only be activated at first level.
6643 (if (or (eq access-key t) (eq access-key first-key))
6644 (propertize key 'face 'org-warning)
6648 ;; Fontify VALUE string.
6649 (propertize value 'face 'font-lock-variable-name-face)))
6650 ;; Prepare menu entries by extracting them from registered
6651 ;; back-ends and sorting them by access key and by ordinal,
6654 (sort (sort (delq nil
6655 (mapcar #'org-export-backend-menu
6656 org-export-registered-backends))
6658 (let ((key-a (nth 1 a))
6660 (cond ((and (numberp key-a) (numberp key-b))
6662 ((numberp key-b) t)))))
6663 'car-less-than-car))
6664 ;; Compute a list of allowed keys based on the first key
6665 ;; pressed, if any. Some keys
6666 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6669 (nconc (list 2 22 19 6 1)
6670 (if (not first-key) (org-uniquify (mapcar 'car entries))
6672 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6673 (when (eq (car entry) first-key)
6674 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6675 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6676 ((not first-key) (list ?P)))
6678 (when expertp (list ??))
6680 ;; Build the help menu for standard UI.
6684 ;; Options are hard-coded.
6685 (format "[%s] Body only: %s [%s] Visible only: %s
6686 \[%s] Export scope: %s [%s] Force publishing: %s
6687 \[%s] Async export: %s\n\n"
6688 (funcall fontify-key "C-b" t)
6689 (funcall fontify-value
6690 (if (memq 'body options) "On " "Off"))
6691 (funcall fontify-key "C-v" t)
6692 (funcall fontify-value
6693 (if (memq 'visible options) "On " "Off"))
6694 (funcall fontify-key "C-s" t)
6695 (funcall fontify-value
6696 (if (memq 'subtree options) "Subtree" "Buffer "))
6697 (funcall fontify-key "C-f" t)
6698 (funcall fontify-value
6699 (if (memq 'force options) "On " "Off"))
6700 (funcall fontify-key "C-a" t)
6701 (funcall fontify-value
6702 (if (memq 'async options) "On " "Off")))
6703 ;; Display registered back-end entries. When a key
6704 ;; appears for the second time, do not create another
6705 ;; entry, but append its sub-menu to existing menu.
6709 (let ((top-key (car entry)))
6711 (unless (eq top-key last-key)
6712 (setq last-key top-key)
6713 (format "\n[%s] %s\n"
6714 (funcall fontify-key (char-to-string top-key))
6716 (let ((sub-menu (nth 2 entry)))
6717 (unless (functionp sub-menu)
6718 ;; Split sub-menu into two columns.
6725 (if (zerop (mod index 2)) " [%s] %-26s"
6727 (funcall fontify-key
6728 (char-to-string (car sub-entry))
6732 (when (zerop (mod index 2)) "\n"))))))))
6734 ;; Publishing menu is hard-coded.
6735 (format "\n[%s] Publish
6736 [%s] Current file [%s] Current project
6737 [%s] Choose project [%s] All projects\n\n\n"
6738 (funcall fontify-key "P")
6739 (funcall fontify-key "f" ?P)
6740 (funcall fontify-key "p" ?P)
6741 (funcall fontify-key "x" ?P)
6742 (funcall fontify-key "a" ?P))
6743 (format "[%s] Export stack [%s] Insert template\n"
6744 (funcall fontify-key "&" t)
6745 (funcall fontify-key "#" t))
6747 (funcall fontify-key "q" t)
6748 (if first-key "Main menu" "Exit")))))
6749 ;; Build prompts for both standard and expert UI.
6750 (standard-prompt (unless expertp "Export command: "))
6754 "Export command (C-%s%s%s%s%s) [%s]: "
6755 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6756 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6757 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6758 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6759 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6760 (mapconcat (lambda (k)
6761 ;; Strip control characters.
6762 (unless (< k 27) (char-to-string k)))
6763 allowed-keys "")))))
6764 ;; With expert UI, just read key with a fancy prompt. In standard
6765 ;; UI, display an intrusive help buffer.
6767 (org-export--dispatch-action
6768 expert-prompt allowed-keys entries options first-key expertp)
6769 ;; At first call, create frame layout in order to display menu.
6770 (unless (get-buffer "*Org Export Dispatcher*")
6771 (delete-other-windows)
6772 (org-switch-to-buffer-other-window
6773 (get-buffer-create "*Org Export Dispatcher*"))
6774 (setq cursor-type nil
6775 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6776 ;; Make sure that invisible cursor will not highlight square
6778 (set-syntax-table (copy-syntax-table))
6779 (modify-syntax-entry ?\[ "w"))
6780 ;; At this point, the buffer containing the menu exists and is
6781 ;; visible in the current window. So, refresh it.
6782 (with-current-buffer "*Org Export Dispatcher*"
6783 ;; Refresh help. Maintain display continuity by re-visiting
6784 ;; previous window position.
6785 (let ((pos (window-start)))
6788 (set-window-start nil pos)))
6789 (org-fit-window-to-buffer)
6790 (org-export--dispatch-action
6791 standard-prompt allowed-keys entries options first-key expertp))))
6793 (defun org-export--dispatch-action
6794 (prompt allowed-keys entries options first-key expertp)
6795 "Read a character from command input and act accordingly.
6797 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6798 a list of characters available at a given step in the process.
6799 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6800 EXPERTP are the same as defined in `org-export--dispatch-ui',
6803 Toggle export options when required. Otherwise, return value is
6804 a list with action as CAR and a list of interactive export
6807 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6809 (while (and (setq key (read-char-exclusive prompt))
6811 (memq key '(14 16 ?\s ?\d)))
6813 (14 (if (not (pos-visible-in-window-p (point-max)))
6814 (ignore-errors (scroll-up 1))
6815 (message "End of buffer")
6817 (16 (if (not (pos-visible-in-window-p (point-min)))
6818 (ignore-errors (scroll-down 1))
6819 (message "Beginning of buffer")
6821 (?\s (if (not (pos-visible-in-window-p (point-max)))
6823 (message "End of buffer")
6825 (?\d (if (not (pos-visible-in-window-p (point-min)))
6827 (message "Beginning of buffer")
6830 ;; Ignore undefined associations.
6831 ((not (memq key allowed-keys))
6833 (unless expertp (message "Invalid key") (sit-for 1))
6834 (org-export--dispatch-ui options first-key expertp))
6835 ;; q key at first level aborts export. At second level, cancel
6836 ;; first key instead.
6837 ((eq key ?q) (if (not first-key) (error "Export aborted")
6838 (org-export--dispatch-ui options nil expertp)))
6839 ;; Help key: Switch back to standard interface if expert UI was
6841 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6842 ;; Send request for template insertion along with export scope.
6843 ((eq key ?#) (cons 'template (memq 'subtree options)))
6844 ;; Switch to asynchronous export stack.
6845 ((eq key ?&) '(stack))
6846 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6847 ((memq key '(2 22 19 6 1))
6848 (org-export--dispatch-ui
6849 (let ((option (cl-case key (2 'body) (22 'visible) (19 'subtree)
6850 (6 'force) (1 'async))))
6851 (if (memq option options) (remq option options)
6852 (cons option options)))
6854 ;; Action selected: Send key and options back to
6855 ;; `org-export-dispatch'.
6856 ((or first-key (functionp (nth 2 (assq key entries))))
6858 ((not first-key) (nth 2 (assq key entries)))
6859 ;; Publishing actions are hard-coded. Send a special
6860 ;; signal to `org-export-dispatch'.
6863 (?f 'publish-current-file)
6864 (?p 'publish-current-project)
6865 (?x 'publish-choose-project)
6867 ;; Return first action associated to FIRST-KEY + KEY
6868 ;; path. Indeed, derived backends can share the same
6871 (dolist (entry (member (assq first-key entries) entries))
6872 (let ((match (assq key (nth 2 entry))))
6873 (when match (throw 'found (nth 2 match))))))))
6875 ;; Otherwise, enter sub-menu.
6876 (t (org-export--dispatch-ui options key expertp)))))
6883 ;; generated-autoload-file: "org-loaddefs.el"