ox-html: Respect the "title" option
[org-mode.git] / lisp / ox.el
blob3b793a00f2d10476055c23bcafae3bfd5847a022
1 ;;; ox.el --- Export Framework for Org Mode -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2012-2017 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 <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
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
29 ;; parts:
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
54 ;; more information.
56 ;; If the new back-end shares most properties with another one,
57 ;; `org-export-define-derived-backend' can be used to simplify the
58 ;; process.
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
67 ;; last one.
69 ;; See <http://orgmode.org/worg/dev/org-export-reference.html> for
70 ;; more information.
72 ;;; Code:
74 (require 'cl-lib)
75 (require 'ob-exp)
76 (require 'org-element)
77 (require 'org-macro)
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
151 property).
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
160 a newline.
161 `split' Split values at white spaces, and cons them to the
162 previous list.
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
168 DEFAULT.
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
245 first.")
247 (defconst org-export-default-inline-image-rule
248 `(("file" .
249 ,(format "\\.%s\\'"
250 (regexp-opt
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
259 rules.")
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."
328 :tag "Org Export"
329 :group 'org)
331 (defgroup org-export-general nil
332 "General options for export engine."
333 :tag "Org Export General"
334 :group 'org-export)
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,
345 e.g. \"arch:nil\"."
346 :group 'org-export-general
347 :type '(choice
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
358 :type 'boolean
359 :safe #'booleanp)
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,
364 e.g. \"c:t\"."
365 :group 'org-export-general
366 :type 'boolean
367 :safe #'booleanp)
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
373 see.
375 This option can also be set with the OPTIONS keyword, e.g.,
376 \"creator:t\"."
377 :group 'org-export-general
378 :version "26.1"
379 :package-version '(Org . "8.3")
380 :type 'boolean
381 :safe #'booleanp)
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,
386 e.g. \"date:nil\"."
387 :group 'org-export-general
388 :type 'boolean
389 :safe #'booleanp)
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
398 string."
399 :group 'org-export-general
400 :type '(choice
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)"
407 emacs-version
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")
413 :safe #'stringp)
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,
426 e.g. \"d:nil\"."
427 :group 'org-export-general
428 :version "24.4"
429 :package-version '(Org . "8.0")
430 :type '(choice
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"
438 :inline t
439 (string :tag "Drawer name"))))
440 :safe (lambda (x) (or (booleanp x)
441 (and (listp x)
442 (or (cl-every #'stringp x)
443 (and (eq (nth 0 x) 'not)
444 (cl-every #'stringp (cdr x))))))))
446 (defcustom org-export-with-email nil
447 "Non-nil means insert author email into the exported file.
448 This option can also be set with the OPTIONS keyword,
449 e.g. \"email:t\"."
450 :group 'org-export-general
451 :type 'boolean
452 :safe #'booleanp)
454 (defcustom org-export-with-emphasize t
455 "Non-nil means interpret *word*, /word/, _word_ and +word+.
457 If the export target supports emphasizing text, the word will be
458 typeset in bold, italic, with an underline or strike-through,
459 respectively.
461 This option can also be set with the OPTIONS keyword,
462 e.g. \"*:nil\"."
463 :group 'org-export-general
464 :type 'boolean
465 :safe #'booleanp)
467 (defcustom org-export-exclude-tags '("noexport")
468 "Tags that exclude a tree from export.
470 All trees carrying any of these tags will be excluded from
471 export. This is without condition, so even subtrees inside that
472 carry one of the `org-export-select-tags' will be removed.
474 This option can also be set with the EXCLUDE_TAGS keyword."
475 :group 'org-export-general
476 :type '(repeat (string :tag "Tag"))
477 :safe (lambda (x) (and (listp x) (cl-every #'stringp x))))
479 (defcustom org-export-with-fixed-width t
480 "Non-nil means export lines starting with \":\".
481 This option can also be set with the OPTIONS keyword,
482 e.g. \"::nil\"."
483 :group 'org-export-general
484 :version "24.4"
485 :package-version '(Org . "8.0")
486 :type 'boolean
487 :safe #'booleanp)
489 (defcustom org-export-with-footnotes t
490 "Non-nil means Org footnotes should be exported.
491 This option can also be set with the OPTIONS keyword,
492 e.g. \"f:nil\"."
493 :group 'org-export-general
494 :type 'boolean
495 :safe #'booleanp)
497 (defcustom org-export-with-latex t
498 "Non-nil means process LaTeX environments and fragments.
500 This option can also be set with the OPTIONS line,
501 e.g. \"tex:verbatim\". Allowed values are:
503 nil Ignore math snippets.
504 `verbatim' Keep everything in verbatim.
505 t Allow export of math snippets."
506 :group 'org-export-general
507 :version "24.4"
508 :package-version '(Org . "8.0")
509 :type '(choice
510 (const :tag "Do not process math in any way" nil)
511 (const :tag "Interpret math snippets" t)
512 (const :tag "Leave math verbatim" verbatim))
513 :safe (lambda (x) (memq x '(t nil verbatim))))
515 (defcustom org-export-headline-levels 3
516 "The last level which is still exported as a headline.
518 Inferior levels will usually produce itemize or enumerate lists
519 when exported, but back-end behavior may differ.
521 This option can also be set with the OPTIONS keyword,
522 e.g. \"H:2\"."
523 :group 'org-export-general
524 :type 'integer
525 :safe #'integerp)
527 (defcustom org-export-default-language "en"
528 "The default language for export and clocktable translations, as a string.
529 This may have an association in
530 `org-clock-clocktable-language-setup',
531 `org-export-smart-quotes-alist' and `org-export-dictionary'.
532 This option can also be set with the LANGUAGE keyword."
533 :group 'org-export-general
534 :type '(string :tag "Language")
535 :safe #'stringp)
537 (defcustom org-export-preserve-breaks nil
538 "Non-nil means preserve all line breaks when exporting.
539 This option can also be set with the OPTIONS keyword,
540 e.g. \"\\n:t\"."
541 :group 'org-export-general
542 :type 'boolean
543 :safe #'booleanp)
545 (defcustom org-export-with-entities t
546 "Non-nil means interpret entities when exporting.
548 For example, HTML export converts \\alpha to &alpha; and \\AA to
549 &Aring;.
551 For a list of supported names, see the constant `org-entities'
552 and the user option `org-entities-user'.
554 This option can also be set with the OPTIONS keyword,
555 e.g. \"e:nil\"."
556 :group 'org-export-general
557 :type 'boolean
558 :safe #'booleanp)
560 (defcustom org-export-with-inlinetasks t
561 "Non-nil means inlinetasks should be exported.
562 This option can also be set with the OPTIONS keyword,
563 e.g. \"inline:nil\"."
564 :group 'org-export-general
565 :version "24.4"
566 :package-version '(Org . "8.0")
567 :type 'boolean
568 :safe #'booleanp)
570 (defcustom org-export-with-planning nil
571 "Non-nil means include planning info in export.
573 Planning info is the line containing either SCHEDULED:,
574 DEADLINE:, CLOSED: time-stamps, or a combination of them.
576 This option can also be set with the OPTIONS keyword,
577 e.g. \"p:t\"."
578 :group 'org-export-general
579 :version "24.4"
580 :package-version '(Org . "8.0")
581 :type 'boolean
582 :safe #'booleanp)
584 (defcustom org-export-with-priority nil
585 "Non-nil means include priority cookies in export.
586 This option can also be set with the OPTIONS keyword,
587 e.g. \"pri:t\"."
588 :group 'org-export-general
589 :type 'boolean
590 :safe #'booleanp)
592 (defcustom org-export-with-properties nil
593 "Non-nil means export contents of properties drawers.
595 When t, all properties are exported. This may also be a list of
596 properties to export, as strings.
598 This option can also be set with the OPTIONS keyword,
599 e.g. \"prop:t\"."
600 :group 'org-export-general
601 :version "24.4"
602 :package-version '(Org . "8.3")
603 :type '(choice
604 (const :tag "All properties" t)
605 (const :tag "None" nil)
606 (repeat :tag "Selected properties"
607 (string :tag "Property name")))
608 :safe (lambda (x) (or (booleanp x)
609 (and (listp x) (cl-every #'stringp x)))))
611 (defcustom org-export-with-section-numbers t
612 "Non-nil means add section numbers to headlines when exporting.
614 When set to an integer n, numbering will only happen for
615 headlines whose relative level is higher or equal to n.
617 This option can also be set with the OPTIONS keyword,
618 e.g. \"num:t\"."
619 :group 'org-export-general
620 :type 'boolean
621 :safe #'booleanp)
623 (defcustom org-export-select-tags '("export")
624 "Tags that select a tree for export.
626 If any such tag is found in a buffer, all trees that do not carry
627 one of these tags will be ignored during export. Inside trees
628 that are selected like this, you can still deselect a subtree by
629 tagging it with one of the `org-export-exclude-tags'.
631 This option can also be set with the SELECT_TAGS keyword."
632 :group 'org-export-general
633 :type '(repeat (string :tag "Tag"))
634 :safe (lambda (x) (and (listp x) (cl-every #'stringp x))))
636 (defcustom org-export-with-smart-quotes nil
637 "Non-nil means activate smart quotes during export.
638 This option can also be set with the OPTIONS keyword,
639 e.g., \"':t\".
641 When setting this to non-nil, you need to take care of
642 using the correct Babel package when exporting to LaTeX.
643 E.g., you can load Babel for french like this:
645 #+LATEX_HEADER: \\usepackage[french]{babel}"
646 :group 'org-export-general
647 :version "24.4"
648 :package-version '(Org . "8.0")
649 :type 'boolean
650 :safe #'booleanp)
652 (defcustom org-export-with-special-strings t
653 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
655 When this option is turned on, these strings will be exported as:
657 Org HTML LaTeX UTF-8
658 -----+----------+--------+-------
659 \\- &shy; \\-
660 -- &ndash; -- –
661 --- &mdash; --- —
662 ... &hellip; \\ldots …
664 This option can also be set with the OPTIONS keyword,
665 e.g. \"-:nil\"."
666 :group 'org-export-general
667 :type 'boolean
668 :safe #'booleanp)
670 (defcustom org-export-with-statistics-cookies t
671 "Non-nil means include statistics cookies in export.
672 This option can also be set with the OPTIONS keyword,
673 e.g. \"stat:nil\""
674 :group 'org-export-general
675 :version "24.4"
676 :package-version '(Org . "8.0")
677 :type 'boolean
678 :safe #'booleanp)
680 (defcustom org-export-with-sub-superscripts t
681 "Non-nil means interpret \"_\" and \"^\" for export.
683 If you want to control how Org displays those characters, see
684 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
685 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
686 it is not anymore.
688 When this option is turned on, you can use TeX-like syntax for
689 sub- and superscripts and see them exported correctly.
691 You can also set the option with #+OPTIONS: ^:t
693 Several characters after \"_\" or \"^\" will be considered as a
694 single item - so grouping with {} is normally not needed. For
695 example, the following things will be parsed as single sub- or
696 superscripts:
698 10^24 or 10^tau several digits will be considered 1 item.
699 10^-12 or 10^-tau a leading sign with digits or a word
700 x^2-y^3 will be read as x^2 - y^3, because items are
701 terminated by almost any nonword/nondigit char.
702 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
704 Still, ambiguity is possible. So when in doubt, use {} to enclose
705 the sub/superscript. If you set this variable to the symbol `{}',
706 the braces are *required* in order to trigger interpretations as
707 sub/superscript. This can be helpful in documents that need \"_\"
708 frequently in plain text."
709 :group 'org-export-general
710 :version "24.4"
711 :package-version '(Org . "8.0")
712 :type '(choice
713 (const :tag "Interpret them" t)
714 (const :tag "Curly brackets only" {})
715 (const :tag "Do not interpret them" nil))
716 :safe (lambda (x) (memq x '(t nil {}))))
718 (defcustom org-export-with-toc t
719 "Non-nil means create a table of contents in exported files.
721 The TOC contains headlines with levels up
722 to`org-export-headline-levels'. When an integer, include levels
723 up to N in the toc, this may then be different from
724 `org-export-headline-levels', but it will not be allowed to be
725 larger than the number of headline levels. When nil, no table of
726 contents is made.
728 This option can also be set with the OPTIONS keyword,
729 e.g. \"toc:nil\" or \"toc:3\"."
730 :group 'org-export-general
731 :type '(choice
732 (const :tag "No Table of Contents" nil)
733 (const :tag "Full Table of Contents" t)
734 (integer :tag "TOC to level"))
735 :safe (lambda (x) (or (booleanp x)
736 (integerp x))))
738 (defcustom org-export-with-tables t
739 "Non-nil means export tables.
740 This option can also be set with the OPTIONS keyword,
741 e.g. \"|:nil\"."
742 :group 'org-export-general
743 :version "24.4"
744 :package-version '(Org . "8.0")
745 :type 'boolean
746 :safe #'booleanp)
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
753 the document.
755 This option can also be set with the OPTIONS keyword,
756 e.g. \"tags:nil\"."
757 :group 'org-export-general
758 :type '(choice
759 (const :tag "Off" nil)
760 (const :tag "Not in TOC" not-in-toc)
761 (const :tag "On" t))
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,
775 e.g. \"tasks:nil\"."
776 :group 'org-export-general
777 :type '(choice
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))
785 (and (listp x)
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,
791 e.g. \"title:nil\"."
792 :group 'org-export-general
793 :version "26.1"
794 :package-version '(Org . "8.3")
795 :type 'boolean
796 :safe #'booleanp)
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
803 :type 'boolean
804 :safe #'booleanp)
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
817 exported.
819 This option can also be set with the OPTIONS keyword, e.g.
820 \"<:nil\"."
821 :group 'org-export-general
822 :type '(choice
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
834 :type 'boolean)
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
841 :version "24.4"
842 :package-version '(Org . "8.0")
843 :type 'boolean)
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
852 [BROKEN LINK: path]
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
859 :version "26.1"
860 :package-version '(Org . "9.0")
861 :type '(choice
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
875 :version "24.4"
876 :package-version '(Org . "8.0")
877 :type '(repeat
878 (cons (string :tag "Shortcut")
879 (string :tag "Back-end")))
880 :safe (lambda (x)
881 (and (listp x)
882 (cl-every #'consp x)
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
892 (NAME . TEMPLATE)
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
901 accordingly."
902 :group 'org-export-general
903 :version "26.1"
904 :package-version '(Org . "9.1")
905 :type '(repeat
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
912 :version "24.4"
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
920 :version "26.1"
921 :package-version '(Org . "8.3")
922 :type '(choice
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
931 :type '(choice
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
941 these cases."
942 :group 'org-export-general
943 :type 'boolean)
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
950 :version "24.4"
951 :package-version '(Org . "8.0")
952 :type 'boolean)
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
961 at start-up.
963 Therefore, using a specific configuration makes the process to
964 load faster and the export more portable."
965 :group 'org-export-general
966 :version "24.4"
967 :package-version '(Org . "8.0")
968 :type '(choice
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
981 mode."
982 :group 'org-export-general
983 :version "24.4"
984 :package-version '(Org . "8.0")
985 :type 'boolean)
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'
993 ;; and `menu' slots.
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
1013 ;; used instead.
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)
1019 (:copier nil))
1020 name parent transcoders options filters blocks menu)
1022 ;;;###autoload
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)))
1057 (when backend
1058 (catch 'exit
1059 (while (org-export-backend-parent backend)
1060 (when (memq (org-export-backend-name backend) backends)
1061 (throw 'exit t))
1062 (setq backend
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
1072 transcoders.
1074 Unlike to `org-export-backend-transcoders', this function
1075 also returns transcoders inherited from parent back-ends,
1076 if any."
1077 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
1078 (when backend
1079 (let ((transcoders (org-export-backend-transcoders backend))
1080 parent)
1081 (while (setq parent (org-export-backend-parent backend))
1082 (setq backend (org-export-get-backend parent))
1083 (setq transcoders
1084 (append transcoders (org-export-backend-transcoders backend))))
1085 transcoders)))
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)))
1099 (when backend
1100 (let ((options (org-export-backend-options backend))
1101 parent)
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))))
1105 options)))
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)))
1117 (when backend
1118 (let ((filters (org-export-backend-filters backend))
1119 parent)
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))))
1123 filters)))
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
1162 back-end.
1164 BODY can start with pre-defined keyword arguments. The following
1165 keywords are understood:
1167 :filters-alist
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.
1175 :menu-entry
1177 Menu entry for the export dispatcher. It should be a list
1178 like:
1180 \\='(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1182 where :
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
1199 some of them.
1201 If it is an alist, associations should follow the
1202 pattern:
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\"
1217 (lambda (a s v b)
1218 (if a (org-latex-export-to-pdf t s v b)
1219 (org-open-file
1220 (org-latex-export-to-pdf nil s v b)))))))
1222 or the following, which will be added to the previous
1223 sub-menu,
1225 \\='(?l 1
1226 ((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1227 (?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1229 :options-alist
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)))
1239 (pcase keyword
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
1247 :options options
1248 :filters filters
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:
1260 :filters-alist
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.
1266 :menu-entry
1268 Menu entry for the export dispatcher. See
1269 `org-export-define-backend' for more information about the
1270 expected value.
1272 :options-alist
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.
1279 :translate-alist
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
1284 about transcoders.
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)))
1299 (pcase keyword
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
1307 :parent parent
1308 :transcoders transcoders
1309 :options options
1310 :filters filters
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'
1323 ;; function.
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.
1359 ;;;###autoload
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...
1377 (org-combine-plists
1378 ;; ... from global variables...
1379 (org-export--get-global-options backend)
1380 ;; ... from an external property list...
1381 ext-plist
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."
1392 (let ((line
1393 (let ((s 0) alist)
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)))
1398 alist))
1399 alist))
1400 ;; Priority is given to back-end specific options.
1401 (all (append (org-export-get-all-options backend)
1402 org-export-options-alist))
1403 (plist))
1404 (when line
1405 (dolist (entry all plist)
1406 (let ((item (nth 2 entry)))
1407 (when item
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))
1422 (let ((plist
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.
1430 (cache (list
1431 (cons "TITLE"
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)))
1444 (when keyword
1445 (let ((value
1446 (or (cdr (assoc keyword cache))
1447 (let ((v (org-entry-get (point)
1448 (concat "EXPORT_" keyword)
1449 'selective)))
1450 (push (cons keyword v) cache) v))))
1451 (when value
1452 (setq plist
1453 (plist-put plist
1454 property
1455 (cl-case (nth 4 option)
1456 (parse
1457 (org-element-parse-secondary-string
1458 value (org-element-restriction 'keyword)))
1459 (split (org-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
1468 process.
1470 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1471 (let* ((case-fold-search t)
1472 (options (append
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))))
1479 plist to-parse)
1480 (letrec ((find-properties
1481 (lambda (keyword)
1482 ;; Return all properties associated to KEYWORD.
1483 (let (properties)
1484 (dolist (option options properties)
1485 (when (equal (nth 1 option) keyword)
1486 (cl-pushnew (car option) properties))))))
1487 (get-options
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)))
1499 (cond
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))
1504 (uri (if uri-is-url
1506 (expand-file-name uri))))
1507 ;; Avoid circular dependencies.
1508 (unless (member uri files)
1509 (with-temp-buffer
1510 (unless uri-is-url
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")
1517 (setq plist
1518 (org-combine-plists
1519 plist
1520 (org-export--parse-option-keyword
1521 val backend))))
1522 ((equal key "FILETAGS")
1523 (setq plist
1524 (org-combine-plists
1525 plist
1526 (list :filetags
1527 (org-uniquify
1528 (append
1529 (org-split-string val ":")
1530 (plist-get plist :filetags)))))))
1532 ;; Options in `org-export-options-alist'.
1533 (dolist (property (funcall find-properties key))
1534 (setq
1535 plist
1536 (plist-put
1537 plist property
1538 ;; Handle value depending on specified
1539 ;; BEHAVIOR.
1540 (cl-case (nth 4 (assq property options))
1541 (parse
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
1550 ;; paragraph.
1551 (let ((old (plist-get plist property)))
1552 (cond ((not (org-string-nw-p val)) old)
1553 (old (concat old "\n" val))
1554 (t val))))
1555 (space
1556 (if (not (plist-get plist property))
1557 (org-trim val)
1558 (concat (plist-get plist property)
1560 (org-trim val))))
1561 (newline
1562 (org-trim
1563 (concat (plist-get plist property)
1564 "\n"
1565 (org-trim val))))
1566 (split `(,@(plist-get plist property)
1567 ,@(org-split-string val)))
1568 ((t) val)
1569 (otherwise
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
1579 (plist-get plist p)
1580 (org-element-restriction 'keyword))))
1581 (org-element-map value 'plain-text
1582 (lambda (s)
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
1592 `org-export-as'."
1593 (list :export-options (delq nil
1594 (list (and subtreep 'subtree)
1595 (and visible-only 'visible-only)
1596 (and body-only 'body-only)))
1597 :back-end backend
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
1611 process."
1612 (let (plist
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)
1619 (setq plist
1620 (plist-put
1621 plist
1622 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))
1628 value)))))))))
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)
1650 "BIND")
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))
1655 (uri (if uri-is-url
1657 (expand-file-name uri))))
1658 ;; Avoid circular dependencies.
1659 (unless (member uri files)
1660 (with-temp-buffer
1661 (unless uri-is-url
1662 (setq default-directory
1663 (file-name-directory uri)))
1664 (let ((org-inhibit-startup t)) (org-mode))
1665 (insert (org-file-contents uri 'noerror))
1666 (setq alist
1667 (funcall collect-bind
1668 (cons uri files)
1669 alist))))))))))
1670 alist)))))
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
1714 as value.
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'.
1721 (setq info
1722 (plist-put info
1723 :headline-offset
1724 (- 1 (org-export--get-min-level data info))))
1725 ;; From now on, properties order doesn't matter: get the rest of the
1726 ;; tree properties.
1727 (org-combine-plists
1728 info
1729 (list :headline-numbering (org-export--collect-headline-numbering data info)
1730 :id-alist
1731 (org-element-map data 'link
1732 (lambda (l)
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."
1742 (catch 'exit
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
1758 options.
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
1765 (lambda (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))))
1770 (cons
1771 headline
1772 (cl-loop
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))))))
1778 options)))
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 (plist-get info :select-tags)))
1785 (if (cl-some (lambda (tag) (member tag select)) (plist-get info :filetags))
1786 ;; If FILETAGS contains a select tag, every headline or
1787 ;; inlinetask is returned.
1788 (org-element-map data '(headline inlinetask) #'identity)
1789 (letrec ((selected-trees nil)
1790 (walk-data
1791 (lambda (data genealogy)
1792 (let ((type (org-element-type data)))
1793 (cond
1794 ((memq type '(headline inlinetask))
1795 (let ((tags (org-element-property :tags data)))
1796 (if (cl-some (lambda (tag) (member tag select)) tags)
1797 ;; When a select tag is found, mark full
1798 ;; genealogy and every headline within the
1799 ;; tree as acceptable.
1800 (setq selected-trees
1801 (append
1802 genealogy
1803 (org-element-map data '(headline inlinetask)
1804 #'identity)
1805 selected-trees))
1806 ;; If at a headline, continue searching in
1807 ;; tree, recursively.
1808 (when (eq type 'headline)
1809 (dolist (el (org-element-contents data))
1810 (funcall walk-data el (cons data genealogy)))))))
1811 ((or (eq type 'org-data)
1812 (memq type org-element-greater-elements))
1813 (dolist (el (org-element-contents data))
1814 (funcall walk-data el genealogy))))))))
1815 (funcall walk-data data nil)
1816 selected-trees))))
1818 (defun org-export--skip-p (datum options selected)
1819 "Non-nil when element or object DATUM should be skipped during export.
1820 OPTIONS is the plist holding export options. SELECTED, when
1821 non-nil, is a list of headlines or inlinetasks belonging to
1822 a tree with a select tag."
1823 (cl-case (org-element-type datum)
1824 ((comment comment-block)
1825 ;; Skip all comments and comment blocks. Make to keep maximum
1826 ;; number of blank lines around the comment so as to preserve
1827 ;; local structure of the document upon interpreting it back into
1828 ;; Org syntax.
1829 (let* ((previous (org-export-get-previous-element datum options))
1830 (before (or (org-element-property :post-blank previous) 0))
1831 (after (or (org-element-property :post-blank datum) 0)))
1832 (when previous
1833 (org-element-put-property previous :post-blank (max before after 1))))
1835 (clock (not (plist-get options :with-clocks)))
1836 (drawer
1837 (let ((with-drawers-p (plist-get options :with-drawers)))
1838 (or (not with-drawers-p)
1839 (and (consp with-drawers-p)
1840 ;; If `:with-drawers' value starts with `not', ignore
1841 ;; every drawer whose name belong to that list.
1842 ;; Otherwise, ignore drawers whose name isn't in that
1843 ;; list.
1844 (let ((name (org-element-property :drawer-name datum)))
1845 (if (eq (car with-drawers-p) 'not)
1846 (member-ignore-case name (cdr with-drawers-p))
1847 (not (member-ignore-case name with-drawers-p))))))))
1848 (fixed-width (not (plist-get options :with-fixed-width)))
1849 ((footnote-definition footnote-reference)
1850 (not (plist-get options :with-footnotes)))
1851 ((headline inlinetask)
1852 (let ((with-tasks (plist-get options :with-tasks))
1853 (todo (org-element-property :todo-keyword datum))
1854 (todo-type (org-element-property :todo-type datum))
1855 (archived (plist-get options :with-archived-trees))
1856 (tags (org-export-get-tags datum options nil t)))
1858 (and (eq (org-element-type datum) 'inlinetask)
1859 (not (plist-get options :with-inlinetasks)))
1860 ;; Ignore subtrees with an exclude tag.
1861 (cl-loop for k in (plist-get options :exclude-tags)
1862 thereis (member k tags))
1863 ;; When a select tag is present in the buffer, ignore any tree
1864 ;; without it.
1865 (and selected (not (memq datum selected)))
1866 ;; Ignore commented sub-trees.
1867 (org-element-property :commentedp datum)
1868 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1869 (and (not archived) (org-element-property :archivedp datum))
1870 ;; Ignore tasks, if specified by `:with-tasks' property.
1871 (and todo
1872 (or (not with-tasks)
1873 (and (memq with-tasks '(todo done))
1874 (not (eq todo-type with-tasks)))
1875 (and (consp with-tasks) (not (member todo with-tasks))))))))
1876 ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
1877 (node-property
1878 (let ((properties-set (plist-get options :with-properties)))
1879 (cond ((null properties-set) t)
1880 ((consp properties-set)
1881 (not (member-ignore-case (org-element-property :key datum)
1882 properties-set))))))
1883 (planning (not (plist-get options :with-planning)))
1884 (property-drawer (not (plist-get options :with-properties)))
1885 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1886 (table (not (plist-get options :with-tables)))
1887 (table-cell
1888 (and (org-export-table-has-special-column-p
1889 (org-export-get-parent-table datum))
1890 (org-export-first-sibling-p datum options)))
1891 (table-row (org-export-table-row-is-special-p datum options))
1892 (timestamp
1893 ;; `:with-timestamps' only applies to isolated timestamps
1894 ;; objects, i.e. timestamp objects in a paragraph containing only
1895 ;; timestamps and whitespaces.
1896 (when (let ((parent (org-export-get-parent-element datum)))
1897 (and (memq (org-element-type parent) '(paragraph verse-block))
1898 (not (org-element-map parent
1899 (cons 'plain-text
1900 (remq 'timestamp org-element-all-objects))
1901 (lambda (obj)
1902 (or (not (stringp obj)) (org-string-nw-p obj)))
1903 options t))))
1904 (cl-case (plist-get options :with-timestamps)
1905 ((nil) t)
1906 (active
1907 (not (memq (org-element-property :type datum) '(active active-range))))
1908 (inactive
1909 (not (memq (org-element-property :type datum)
1910 '(inactive inactive-range)))))))))
1913 ;;; The Transcoder
1915 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1916 ;; `org-element-parse-buffer') and transcodes it into a specified
1917 ;; back-end output. It takes care of filtering out elements or
1918 ;; objects according to export options and organizing the output blank
1919 ;; lines and white space are preserved. The function memoizes its
1920 ;; results, so it is cheap to call it within transcoders.
1922 ;; It is possible to modify locally the back-end used by
1923 ;; `org-export-data' or even use a temporary back-end by using
1924 ;; `org-export-data-with-backend'.
1926 ;; `org-export-transcoder' is an accessor returning appropriate
1927 ;; translator function for a given element or object.
1929 (defun org-export-transcoder (blob info)
1930 "Return appropriate transcoder for BLOB.
1931 INFO is a plist containing export directives."
1932 (let ((type (org-element-type blob)))
1933 ;; Return contents only for complete parse trees.
1934 (if (eq type 'org-data) (lambda (_datum contents _info) contents)
1935 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1936 (and (functionp transcoder) transcoder)))))
1938 (defun org-export-data (data info)
1939 "Convert DATA into current back-end format.
1941 DATA is a parse tree, an element or an object or a secondary
1942 string. INFO is a plist holding export options.
1944 Return a string."
1945 (or (gethash data (plist-get info :exported-data))
1946 ;; Handle broken links according to
1947 ;; `org-export-with-broken-links'.
1948 (cl-macrolet
1949 ((broken-link-handler
1950 (&rest body)
1951 `(condition-case err
1952 (progn ,@body)
1953 (org-link-broken
1954 (pcase (plist-get info :with-broken-links)
1955 (`nil (user-error "Unable to resolve link: %S" (nth 1 err)))
1956 (`mark (org-export-data
1957 (format "[BROKEN LINK: %s]" (nth 1 err)) info))
1958 (_ nil))))))
1959 (let* ((type (org-element-type data))
1960 (parent (org-export-get-parent data))
1961 (results
1962 (cond
1963 ;; Ignored element/object.
1964 ((memq data (plist-get info :ignore-list)) nil)
1965 ;; Plain text.
1966 ((eq type 'plain-text)
1967 (org-export-filter-apply-functions
1968 (plist-get info :filter-plain-text)
1969 (let ((transcoder (org-export-transcoder data info)))
1970 (if transcoder (funcall transcoder data info) data))
1971 info))
1972 ;; Secondary string.
1973 ((not type)
1974 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1975 ;; Element/Object without contents or, as a special
1976 ;; case, headline with archive tag and archived trees
1977 ;; restricted to title only.
1978 ((or (not (org-element-contents data))
1979 (and (eq type 'headline)
1980 (eq (plist-get info :with-archived-trees) 'headline)
1981 (org-element-property :archivedp data)))
1982 (let ((transcoder (org-export-transcoder data info)))
1983 (or (and (functionp transcoder)
1984 (broken-link-handler
1985 (funcall transcoder data nil info)))
1986 ;; Export snippets never return a nil value so
1987 ;; that white spaces following them are never
1988 ;; ignored.
1989 (and (eq type 'export-snippet) ""))))
1990 ;; Element/Object with contents.
1992 (let ((transcoder (org-export-transcoder data info)))
1993 (when transcoder
1994 (let* ((greaterp (memq type org-element-greater-elements))
1995 (objectp
1996 (and (not greaterp)
1997 (memq type org-element-recursive-objects)))
1998 (contents
1999 (mapconcat
2000 (lambda (element) (org-export-data element info))
2001 (org-element-contents
2002 (if (or greaterp objectp) data
2003 ;; Elements directly containing
2004 ;; objects must have their indentation
2005 ;; normalized first.
2006 (org-element-normalize-contents
2007 data
2008 ;; When normalizing contents of the
2009 ;; first paragraph in an item or
2010 ;; a footnote definition, ignore
2011 ;; first line's indentation: there is
2012 ;; none and it might be misleading.
2013 (when (eq type 'paragraph)
2014 (and
2015 (eq (car (org-element-contents parent))
2016 data)
2017 (memq (org-element-type parent)
2018 '(footnote-definition item)))))))
2019 "")))
2020 (broken-link-handler
2021 (funcall transcoder data
2022 (if (not greaterp) contents
2023 (org-element-normalize-string contents))
2024 info)))))))))
2025 ;; Final result will be memoized before being returned.
2026 (puthash
2027 data
2028 (cond
2029 ((not results) "")
2030 ((memq type '(org-data plain-text nil)) results)
2031 ;; Append the same white space between elements or objects
2032 ;; as in the original buffer, and call appropriate filters.
2034 (org-export-filter-apply-functions
2035 (plist-get info (intern (format ":filter-%s" type)))
2036 (let ((blank (or (org-element-property :post-blank data) 0)))
2037 (if (eq (org-element-class data parent) 'object)
2038 (concat results (make-string blank ?\s))
2039 (concat (org-element-normalize-string results)
2040 (make-string blank ?\n))))
2041 info)))
2042 (plist-get info :exported-data))))))
2044 (defun org-export-data-with-backend (data backend info)
2045 "Convert DATA into BACKEND format.
2047 DATA is an element, an object, a secondary string or a string.
2048 BACKEND is a symbol. INFO is a plist used as a communication
2049 channel.
2051 Unlike to `org-export-with-backend', this function will
2052 recursively convert DATA using BACKEND translation table."
2053 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
2054 ;; Set-up a new communication channel with translations defined in
2055 ;; BACKEND as the translate table and a new hash table for
2056 ;; memoization.
2057 (let ((new-info
2058 (org-combine-plists
2059 info
2060 (list :back-end backend
2061 :translate-alist (org-export-get-all-transcoders backend)
2062 ;; Size of the hash table is reduced since this
2063 ;; function will probably be used on small trees.
2064 :exported-data (make-hash-table :test 'eq :size 401)))))
2065 (prog1 (org-export-data data new-info)
2066 ;; Preserve `:internal-references', as those do not depend on
2067 ;; the back-end used; we need to make sure that any new
2068 ;; reference when the temporary back-end was active gets through
2069 ;; the default one.
2070 (plist-put info :internal-references
2071 (plist-get new-info :internal-references)))))
2073 (defun org-export-expand (blob contents &optional with-affiliated)
2074 "Expand a parsed element or object to its original state.
2076 BLOB is either an element or an object. CONTENTS is its
2077 contents, as a string or nil.
2079 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2080 keywords before output."
2081 (let ((type (org-element-type blob)))
2082 (concat (and with-affiliated
2083 (eq (org-element-class blob) 'element)
2084 (org-element--interpret-affiliated-keywords blob))
2085 (funcall (intern (format "org-element-%s-interpreter" type))
2086 blob contents))))
2090 ;;; The Filter System
2092 ;; Filters allow end-users to tweak easily the transcoded output.
2093 ;; They are the functional counterpart of hooks, as every filter in
2094 ;; a set is applied to the return value of the previous one.
2096 ;; Every set is back-end agnostic. Although, a filter is always
2097 ;; called, in addition to the string it applies to, with the back-end
2098 ;; used as argument, so it's easy for the end-user to add back-end
2099 ;; specific filters in the set. The communication channel, as
2100 ;; a plist, is required as the third argument.
2102 ;; From the developer side, filters sets can be installed in the
2103 ;; process with the help of `org-export-define-backend', which
2104 ;; internally stores filters as an alist. Each association has a key
2105 ;; among the following symbols and a function or a list of functions
2106 ;; as value.
2108 ;; - `:filter-options' applies to the property list containing export
2109 ;; options. Unlike to other filters, functions in this list accept
2110 ;; two arguments instead of three: the property list containing
2111 ;; export options and the back-end. Users can set its value through
2112 ;; `org-export-filter-options-functions' variable.
2114 ;; - `:filter-parse-tree' applies directly to the complete parsed
2115 ;; tree. Users can set it through
2116 ;; `org-export-filter-parse-tree-functions' variable.
2118 ;; - `:filter-body' applies to the body of the output, before template
2119 ;; translator chimes in. Users can set it through
2120 ;; `org-export-filter-body-functions' variable.
2122 ;; - `:filter-final-output' applies to the final transcoded string.
2123 ;; Users can set it with `org-export-filter-final-output-functions'
2124 ;; variable.
2126 ;; - `:filter-plain-text' applies to any string not recognized as Org
2127 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2128 ;; configure it.
2130 ;; - `:filter-TYPE' applies on the string returned after an element or
2131 ;; object of type TYPE has been transcoded. A user can modify
2132 ;; `org-export-filter-TYPE-functions' to install these filters.
2134 ;; All filters sets are applied with
2135 ;; `org-export-filter-apply-functions' function. Filters in a set are
2136 ;; applied in a LIFO fashion. It allows developers to be sure that
2137 ;; their filters will be applied first.
2139 ;; Filters properties are installed in communication channel with
2140 ;; `org-export-install-filters' function.
2142 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2143 ;; `org-export-before-parsing-hook') are run at the beginning of the
2144 ;; export process and just before parsing to allow for heavy structure
2145 ;; modifications.
2148 ;;;; Hooks
2150 (defvar org-export-before-processing-hook nil
2151 "Hook run at the beginning of the export process.
2153 This is run before include keywords and macros are expanded and
2154 Babel code blocks executed, on a copy of the original buffer
2155 being exported. Visibility and narrowing are preserved. Point
2156 is at the beginning of the buffer.
2158 Every function in this hook will be called with one argument: the
2159 back-end currently used, as a symbol.")
2161 (defvar org-export-before-parsing-hook nil
2162 "Hook run before parsing an export buffer.
2164 This is run after include keywords and macros have been expanded
2165 and Babel code blocks executed, on a copy of the original buffer
2166 being exported. Visibility and narrowing are preserved. Point
2167 is at the beginning of the buffer.
2169 Every function in this hook will be called with one argument: the
2170 back-end currently used, as a symbol.")
2173 ;;;; Special Filters
2175 (defvar org-export-filter-options-functions nil
2176 "List of functions applied to the export options.
2177 Each filter is called with two arguments: the export options, as
2178 a plist, and the back-end, as a symbol. It must return
2179 a property list containing export options.")
2181 (defvar org-export-filter-parse-tree-functions nil
2182 "List of functions applied to the parsed tree.
2183 Each filter is called with three arguments: the parse tree, as
2184 returned by `org-element-parse-buffer', the back-end, as
2185 a symbol, and the communication channel, as a plist. It must
2186 return the modified parse tree to transcode.")
2188 (defvar org-export-filter-plain-text-functions nil
2189 "List of functions applied to plain text.
2190 Each filter is called with three arguments: a string which
2191 contains no Org syntax, the back-end, as a symbol, and the
2192 communication channel, as a plist. It must return a string or
2193 nil.")
2195 (defvar org-export-filter-body-functions nil
2196 "List of functions applied to transcoded body.
2197 Each filter is called with three arguments: a string which
2198 contains no Org syntax, the back-end, as a symbol, and the
2199 communication channel, as a plist. It must return a string or
2200 nil.")
2202 (defvar org-export-filter-final-output-functions nil
2203 "List of functions applied to the transcoded string.
2204 Each filter is called with three arguments: the full transcoded
2205 string, the back-end, as a symbol, and the communication channel,
2206 as a plist. It must return a string that will be used as the
2207 final export output.")
2210 ;;;; Elements Filters
2212 (defvar org-export-filter-babel-call-functions nil
2213 "List of functions applied to a transcoded babel-call.
2214 Each filter is called with three arguments: the transcoded data,
2215 as a string, the back-end, as a symbol, and the communication
2216 channel, as a plist. It must return a string or nil.")
2218 (defvar org-export-filter-center-block-functions nil
2219 "List of functions applied to a transcoded center block.
2220 Each filter is called with three arguments: the transcoded data,
2221 as a string, the back-end, as a symbol, and the communication
2222 channel, as a plist. It must return a string or nil.")
2224 (defvar org-export-filter-clock-functions nil
2225 "List of functions applied to a transcoded clock.
2226 Each filter is called with three arguments: the transcoded data,
2227 as a string, the back-end, as a symbol, and the communication
2228 channel, as a plist. It must return a string or nil.")
2230 (defvar org-export-filter-diary-sexp-functions nil
2231 "List of functions applied to a transcoded diary-sexp.
2232 Each filter is called with three arguments: the transcoded data,
2233 as a string, the back-end, as a symbol, and the communication
2234 channel, as a plist. It must return a string or nil.")
2236 (defvar org-export-filter-drawer-functions nil
2237 "List of functions applied to a transcoded drawer.
2238 Each filter is called with three arguments: the transcoded data,
2239 as a string, the back-end, as a symbol, and the communication
2240 channel, as a plist. It must return a string or nil.")
2242 (defvar org-export-filter-dynamic-block-functions nil
2243 "List of functions applied to a transcoded dynamic-block.
2244 Each filter is called with three arguments: the transcoded data,
2245 as a string, the back-end, as a symbol, and the communication
2246 channel, as a plist. It must return a string or nil.")
2248 (defvar org-export-filter-example-block-functions nil
2249 "List of functions applied to a transcoded example-block.
2250 Each filter is called with three arguments: the transcoded data,
2251 as a string, the back-end, as a symbol, and the communication
2252 channel, as a plist. It must return a string or nil.")
2254 (defvar org-export-filter-export-block-functions nil
2255 "List of functions applied to a transcoded export-block.
2256 Each filter is called with three arguments: the transcoded data,
2257 as a string, the back-end, as a symbol, and the communication
2258 channel, as a plist. It must return a string or nil.")
2260 (defvar org-export-filter-fixed-width-functions nil
2261 "List of functions applied to a transcoded fixed-width.
2262 Each filter is called with three arguments: the transcoded data,
2263 as a string, the back-end, as a symbol, and the communication
2264 channel, as a plist. It must return a string or nil.")
2266 (defvar org-export-filter-footnote-definition-functions nil
2267 "List of functions applied to a transcoded footnote-definition.
2268 Each filter is called with three arguments: the transcoded data,
2269 as a string, the back-end, as a symbol, and the communication
2270 channel, as a plist. It must return a string or nil.")
2272 (defvar org-export-filter-headline-functions nil
2273 "List of functions applied to a transcoded headline.
2274 Each filter is called with three arguments: the transcoded data,
2275 as a string, the back-end, as a symbol, and the communication
2276 channel, as a plist. It must return a string or nil.")
2278 (defvar org-export-filter-horizontal-rule-functions nil
2279 "List of functions applied to a transcoded horizontal-rule.
2280 Each filter is called with three arguments: the transcoded data,
2281 as a string, the back-end, as a symbol, and the communication
2282 channel, as a plist. It must return a string or nil.")
2284 (defvar org-export-filter-inlinetask-functions nil
2285 "List of functions applied to a transcoded inlinetask.
2286 Each filter is called with three arguments: the transcoded data,
2287 as a string, the back-end, as a symbol, and the communication
2288 channel, as a plist. It must return a string or nil.")
2290 (defvar org-export-filter-item-functions nil
2291 "List of functions applied to a transcoded item.
2292 Each filter is called with three arguments: the transcoded data,
2293 as a string, the back-end, as a symbol, and the communication
2294 channel, as a plist. It must return a string or nil.")
2296 (defvar org-export-filter-keyword-functions nil
2297 "List of functions applied to a transcoded keyword.
2298 Each filter is called with three arguments: the transcoded data,
2299 as a string, the back-end, as a symbol, and the communication
2300 channel, as a plist. It must return a string or nil.")
2302 (defvar org-export-filter-latex-environment-functions nil
2303 "List of functions applied to a transcoded latex-environment.
2304 Each filter is called with three arguments: the transcoded data,
2305 as a string, the back-end, as a symbol, and the communication
2306 channel, as a plist. It must return a string or nil.")
2308 (defvar org-export-filter-node-property-functions nil
2309 "List of functions applied to a transcoded node-property.
2310 Each filter is called with three arguments: the transcoded data,
2311 as a string, the back-end, as a symbol, and the communication
2312 channel, as a plist. It must return a string or nil.")
2314 (defvar org-export-filter-paragraph-functions nil
2315 "List of functions applied to a transcoded paragraph.
2316 Each filter is called with three arguments: the transcoded data,
2317 as a string, the back-end, as a symbol, and the communication
2318 channel, as a plist. It must return a string or nil.")
2320 (defvar org-export-filter-plain-list-functions nil
2321 "List of functions applied to a transcoded plain-list.
2322 Each filter is called with three arguments: the transcoded data,
2323 as a string, the back-end, as a symbol, and the communication
2324 channel, as a plist. It must return a string or nil.")
2326 (defvar org-export-filter-planning-functions nil
2327 "List of functions applied to a transcoded planning.
2328 Each filter is called with three arguments: the transcoded data,
2329 as a string, the back-end, as a symbol, and the communication
2330 channel, as a plist. It must return a string or nil.")
2332 (defvar org-export-filter-property-drawer-functions nil
2333 "List of functions applied to a transcoded property-drawer.
2334 Each filter is called with three arguments: the transcoded data,
2335 as a string, the back-end, as a symbol, and the communication
2336 channel, as a plist. It must return a string or nil.")
2338 (defvar org-export-filter-quote-block-functions nil
2339 "List of functions applied to a transcoded quote block.
2340 Each filter is called with three arguments: the transcoded quote
2341 data, as a string, the back-end, as a symbol, and the
2342 communication channel, as a plist. It must return a string or
2343 nil.")
2345 (defvar org-export-filter-section-functions nil
2346 "List of functions applied to a transcoded section.
2347 Each filter is called with three arguments: the transcoded data,
2348 as a string, the back-end, as a symbol, and the communication
2349 channel, as a plist. It must return a string or nil.")
2351 (defvar org-export-filter-special-block-functions nil
2352 "List of functions applied to a transcoded special block.
2353 Each filter is called with three arguments: the transcoded data,
2354 as a string, the back-end, as a symbol, and the communication
2355 channel, as a plist. It must return a string or nil.")
2357 (defvar org-export-filter-src-block-functions nil
2358 "List of functions applied to a transcoded src-block.
2359 Each filter is called with three arguments: the transcoded data,
2360 as a string, the back-end, as a symbol, and the communication
2361 channel, as a plist. It must return a string or nil.")
2363 (defvar org-export-filter-table-functions nil
2364 "List of functions applied to a transcoded table.
2365 Each filter is called with three arguments: the transcoded data,
2366 as a string, the back-end, as a symbol, and the communication
2367 channel, as a plist. It must return a string or nil.")
2369 (defvar org-export-filter-table-cell-functions nil
2370 "List of functions applied to a transcoded table-cell.
2371 Each filter is called with three arguments: the transcoded data,
2372 as a string, the back-end, as a symbol, and the communication
2373 channel, as a plist. It must return a string or nil.")
2375 (defvar org-export-filter-table-row-functions nil
2376 "List of functions applied to a transcoded table-row.
2377 Each filter is called with three arguments: the transcoded data,
2378 as a string, the back-end, as a symbol, and the communication
2379 channel, as a plist. It must return a string or nil.")
2381 (defvar org-export-filter-verse-block-functions nil
2382 "List of functions applied to a transcoded verse block.
2383 Each filter is called with three arguments: the transcoded data,
2384 as a string, the back-end, as a symbol, and the communication
2385 channel, as a plist. It must return a string or nil.")
2388 ;;;; Objects Filters
2390 (defvar org-export-filter-bold-functions nil
2391 "List of functions applied to transcoded bold text.
2392 Each filter is called with three arguments: the transcoded data,
2393 as a string, the back-end, as a symbol, and the communication
2394 channel, as a plist. It must return a string or nil.")
2396 (defvar org-export-filter-code-functions nil
2397 "List of functions applied to transcoded code text.
2398 Each filter is called with three arguments: the transcoded data,
2399 as a string, the back-end, as a symbol, and the communication
2400 channel, as a plist. It must return a string or nil.")
2402 (defvar org-export-filter-entity-functions nil
2403 "List of functions applied to a transcoded entity.
2404 Each filter is called with three arguments: the transcoded data,
2405 as a string, the back-end, as a symbol, and the communication
2406 channel, as a plist. It must return a string or nil.")
2408 (defvar org-export-filter-export-snippet-functions nil
2409 "List of functions applied to a transcoded export-snippet.
2410 Each filter is called with three arguments: the transcoded data,
2411 as a string, the back-end, as a symbol, and the communication
2412 channel, as a plist. It must return a string or nil.")
2414 (defvar org-export-filter-footnote-reference-functions nil
2415 "List of functions applied to a transcoded footnote-reference.
2416 Each filter is called with three arguments: the transcoded data,
2417 as a string, the back-end, as a symbol, and the communication
2418 channel, as a plist. It must return a string or nil.")
2420 (defvar org-export-filter-inline-babel-call-functions nil
2421 "List of functions applied to a transcoded inline-babel-call.
2422 Each filter is called with three arguments: the transcoded data,
2423 as a string, the back-end, as a symbol, and the communication
2424 channel, as a plist. It must return a string or nil.")
2426 (defvar org-export-filter-inline-src-block-functions nil
2427 "List of functions applied to a transcoded inline-src-block.
2428 Each filter is called with three arguments: the transcoded data,
2429 as a string, the back-end, as a symbol, and the communication
2430 channel, as a plist. It must return a string or nil.")
2432 (defvar org-export-filter-italic-functions nil
2433 "List of functions applied to transcoded italic text.
2434 Each filter is called with three arguments: the transcoded data,
2435 as a string, the back-end, as a symbol, and the communication
2436 channel, as a plist. It must return a string or nil.")
2438 (defvar org-export-filter-latex-fragment-functions nil
2439 "List of functions applied to a transcoded latex-fragment.
2440 Each filter is called with three arguments: the transcoded data,
2441 as a string, the back-end, as a symbol, and the communication
2442 channel, as a plist. It must return a string or nil.")
2444 (defvar org-export-filter-line-break-functions nil
2445 "List of functions applied to a transcoded line-break.
2446 Each filter is called with three arguments: the transcoded data,
2447 as a string, the back-end, as a symbol, and the communication
2448 channel, as a plist. It must return a string or nil.")
2450 (defvar org-export-filter-link-functions nil
2451 "List of functions applied to a transcoded link.
2452 Each filter is called with three arguments: the transcoded data,
2453 as a string, the back-end, as a symbol, and the communication
2454 channel, as a plist. It must return a string or nil.")
2456 (defvar org-export-filter-radio-target-functions nil
2457 "List of functions applied to a transcoded radio-target.
2458 Each filter is called with three arguments: the transcoded data,
2459 as a string, the back-end, as a symbol, and the communication
2460 channel, as a plist. It must return a string or nil.")
2462 (defvar org-export-filter-statistics-cookie-functions nil
2463 "List of functions applied to a transcoded statistics-cookie.
2464 Each filter is called with three arguments: the transcoded data,
2465 as a string, the back-end, as a symbol, and the communication
2466 channel, as a plist. It must return a string or nil.")
2468 (defvar org-export-filter-strike-through-functions nil
2469 "List of functions applied to transcoded strike-through text.
2470 Each filter is called with three arguments: the transcoded data,
2471 as a string, the back-end, as a symbol, and the communication
2472 channel, as a plist. It must return a string or nil.")
2474 (defvar org-export-filter-subscript-functions nil
2475 "List of functions applied to a transcoded subscript.
2476 Each filter is called with three arguments: the transcoded data,
2477 as a string, the back-end, as a symbol, and the communication
2478 channel, as a plist. It must return a string or nil.")
2480 (defvar org-export-filter-superscript-functions nil
2481 "List of functions applied to a transcoded superscript.
2482 Each filter is called with three arguments: the transcoded data,
2483 as a string, the back-end, as a symbol, and the communication
2484 channel, as a plist. It must return a string or nil.")
2486 (defvar org-export-filter-target-functions nil
2487 "List of functions applied to a transcoded target.
2488 Each filter is called with three arguments: the transcoded data,
2489 as a string, the back-end, as a symbol, and the communication
2490 channel, as a plist. It must return a string or nil.")
2492 (defvar org-export-filter-timestamp-functions nil
2493 "List of functions applied to a transcoded timestamp.
2494 Each filter is called with three arguments: the transcoded data,
2495 as a string, the back-end, as a symbol, and the communication
2496 channel, as a plist. It must return a string or nil.")
2498 (defvar org-export-filter-underline-functions nil
2499 "List of functions applied to transcoded underline text.
2500 Each filter is called with three arguments: the transcoded data,
2501 as a string, the back-end, as a symbol, and the communication
2502 channel, as a plist. It must return a string or nil.")
2504 (defvar org-export-filter-verbatim-functions nil
2505 "List of functions applied to transcoded verbatim text.
2506 Each filter is called with three arguments: the transcoded data,
2507 as a string, the back-end, as a symbol, and the communication
2508 channel, as a plist. It must return a string or nil.")
2511 ;;;; Filters Tools
2513 ;; Internal function `org-export-install-filters' installs filters
2514 ;; hard-coded in back-ends (developer filters) and filters from global
2515 ;; variables (user filters) in the communication channel.
2517 ;; Internal function `org-export-filter-apply-functions' takes care
2518 ;; about applying each filter in order to a given data. It ignores
2519 ;; filters returning a nil value but stops whenever a filter returns
2520 ;; an empty string.
2522 (defun org-export-filter-apply-functions (filters value info)
2523 "Call every function in FILTERS.
2525 Functions are called with three arguments: a value, the export
2526 back-end name and the communication channel. First function in
2527 FILTERS is called with VALUE as its first argument. Second
2528 function in FILTERS is called with the previous result as its
2529 value, etc.
2531 Functions returning nil are skipped. Any function returning the
2532 empty string ends the process, which returns the empty string.
2534 Call is done in a LIFO fashion, to be sure that developer
2535 specified filters, if any, are called first."
2536 (catch :exit
2537 (let* ((backend (plist-get info :back-end))
2538 (backend-name (and backend (org-export-backend-name backend))))
2539 (dolist (filter filters value)
2540 (let ((result (funcall filter value backend-name info)))
2541 (cond ((not result))
2542 ((equal result "") (throw :exit ""))
2543 (t (setq value result))))))))
2545 (defun org-export-install-filters (info)
2546 "Install filters properties in communication channel.
2547 INFO is a plist containing the current communication channel.
2548 Return the updated communication channel."
2549 (let (plist)
2550 ;; Install user-defined filters with `org-export-filters-alist'
2551 ;; and filters already in INFO (through ext-plist mechanism).
2552 (dolist (p org-export-filters-alist)
2553 (let* ((prop (car p))
2554 (info-value (plist-get info prop))
2555 (default-value (symbol-value (cdr p))))
2556 (setq plist
2557 (plist-put plist prop
2558 ;; Filters in INFO will be called
2559 ;; before those user provided.
2560 (append (if (listp info-value) info-value
2561 (list info-value))
2562 default-value)))))
2563 ;; Prepend back-end specific filters to that list.
2564 (dolist (p (org-export-get-all-filters (plist-get info :back-end)))
2565 ;; Single values get consed, lists are appended.
2566 (let ((key (car p)) (value (cdr p)))
2567 (when value
2568 (setq plist
2569 (plist-put
2570 plist key
2571 (if (atom value) (cons value (plist-get plist key))
2572 (append value (plist-get plist key))))))))
2573 ;; Return new communication channel.
2574 (org-combine-plists info plist)))
2578 ;;; Core functions
2580 ;; This is the room for the main function, `org-export-as', along with
2581 ;; its derivative, `org-export-string-as'.
2582 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2583 ;; function should be added to kill ring.
2585 ;; Note that `org-export-as' doesn't really parse the current buffer,
2586 ;; but a copy of it (with the same buffer-local variables and
2587 ;; visibility), where macros and include keywords are expanded and
2588 ;; Babel blocks are executed, if appropriate.
2589 ;; `org-export-with-buffer-copy' macro prepares that copy.
2591 ;; File inclusion is taken care of by
2592 ;; `org-export-expand-include-keyword' and
2593 ;; `org-export--prepare-file-contents'. Structure wise, including
2594 ;; a whole Org file in a buffer often makes little sense. For
2595 ;; example, if the file contains a headline and the include keyword
2596 ;; was within an item, the item should contain the headline. That's
2597 ;; why file inclusion should be done before any structure can be
2598 ;; associated to the file, that is before parsing.
2600 ;; `org-export-insert-default-template' is a command to insert
2601 ;; a default template (or a back-end specific template) at point or in
2602 ;; current subtree.
2604 (defun org-export-copy-buffer ()
2605 "Return a copy of the current buffer.
2606 The copy preserves Org buffer-local variables, visibility and
2607 narrowing."
2608 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2609 (new-buf (generate-new-buffer (buffer-name))))
2610 (with-current-buffer new-buf
2611 (funcall copy-buffer-fun)
2612 (set-buffer-modified-p nil))
2613 new-buf))
2615 (defmacro org-export-with-buffer-copy (&rest body)
2616 "Apply BODY in a copy of the current buffer.
2617 The copy preserves local variables, visibility and contents of
2618 the original buffer. Point is at the beginning of the buffer
2619 when BODY is applied."
2620 (declare (debug t))
2621 (org-with-gensyms (buf-copy)
2622 `(let ((,buf-copy (org-export-copy-buffer)))
2623 (unwind-protect
2624 (with-current-buffer ,buf-copy
2625 (goto-char (point-min))
2626 (progn ,@body))
2627 (and (buffer-live-p ,buf-copy)
2628 ;; Kill copy without confirmation.
2629 (progn (with-current-buffer ,buf-copy
2630 (restore-buffer-modified-p nil))
2631 (kill-buffer ,buf-copy)))))))
2633 (defun org-export--generate-copy-script (buffer)
2634 "Generate a function duplicating BUFFER.
2636 The copy will preserve local variables, visibility, contents and
2637 narrowing of the original buffer. If a region was active in
2638 BUFFER, contents will be narrowed to that region instead.
2640 The resulting function can be evaluated at a later time, from
2641 another buffer, effectively cloning the original buffer there.
2643 The function assumes BUFFER's major mode is `org-mode'."
2644 (with-current-buffer buffer
2645 `(lambda ()
2646 (let ((inhibit-modification-hooks t))
2647 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2648 ;; already in BUFFER.
2649 (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
2650 ;; Copy specific buffer local variables and variables set
2651 ;; through BIND keywords.
2652 ,@(let ((bound-variables (org-export--list-bound-variables))
2653 vars)
2654 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
2655 (when (consp entry)
2656 (let ((var (car entry))
2657 (val (cdr entry)))
2658 (and (not (memq var org-export-ignored-local-variables))
2659 (or (memq var
2660 '(default-directory
2661 buffer-file-name
2662 buffer-file-coding-system))
2663 (assq var bound-variables)
2664 (string-match "^\\(org-\\|orgtbl-\\)"
2665 (symbol-name var)))
2666 ;; Skip unreadable values, as they cannot be
2667 ;; sent to external process.
2668 (or (not val) (ignore-errors (read (format "%S" val))))
2669 (push `(set (make-local-variable (quote ,var))
2670 (quote ,val))
2671 vars))))))
2672 ;; Whole buffer contents.
2673 (insert
2674 ,(org-with-wide-buffer
2675 (buffer-substring-no-properties
2676 (point-min) (point-max))))
2677 ;; Narrowing.
2678 ,(if (org-region-active-p)
2679 `(narrow-to-region ,(region-beginning) ,(region-end))
2680 `(narrow-to-region ,(point-min) ,(point-max)))
2681 ;; Current position of point.
2682 (goto-char ,(point))
2683 ;; Overlays with invisible property.
2684 ,@(let (ov-set)
2685 (dolist (ov (overlays-in (point-min) (point-max)) ov-set)
2686 (let ((invis-prop (overlay-get ov 'invisible)))
2687 (when invis-prop
2688 (push `(overlay-put
2689 (make-overlay ,(overlay-start ov)
2690 ,(overlay-end ov))
2691 'invisible (quote ,invis-prop))
2692 ov-set)))))))))
2694 (defun org-export--delete-comment-trees ()
2695 "Delete commented trees and commented inlinetasks in the buffer.
2696 Narrowing, if any, is ignored."
2697 (org-with-wide-buffer
2698 (goto-char (point-min))
2699 (let* ((case-fold-search t)
2700 (regexp (concat org-outline-regexp-bol ".*" org-comment-string)))
2701 (while (re-search-forward regexp nil t)
2702 (let ((element (org-element-at-point)))
2703 (when (org-element-property :commentedp element)
2704 (delete-region (org-element-property :begin element)
2705 (org-element-property :end element))))))))
2707 (defun org-export--prune-tree (data info)
2708 "Prune non exportable elements from DATA.
2709 DATA is the parse tree to traverse. INFO is the plist holding
2710 export info. Also set `:ignore-list' in INFO to a list of
2711 objects which should be ignored during export, but not removed
2712 from tree."
2713 (letrec ((ignore nil)
2714 ;; First find trees containing a select tag, if any.
2715 (selected (org-export--selected-trees data info))
2716 (walk-data
2717 (lambda (data)
2718 ;; Prune non-exportable elements and objects from tree.
2719 ;; As a special case, special rows and cells from tables
2720 ;; are stored in IGNORE, as they still need to be
2721 ;; accessed during export.
2722 (when data
2723 (let ((type (org-element-type data)))
2724 (if (org-export--skip-p data info selected)
2725 (if (memq type '(table-cell table-row)) (push data ignore)
2726 (org-element-extract-element data))
2727 (if (and (eq type 'headline)
2728 (eq (plist-get info :with-archived-trees)
2729 'headline)
2730 (org-element-property :archivedp data))
2731 ;; If headline is archived but tree below has
2732 ;; to be skipped, remove contents.
2733 (org-element-set-contents data)
2734 ;; Move into recursive objects/elements.
2735 (mapc walk-data (org-element-contents data)))
2736 ;; Move into secondary string, if any.
2737 (dolist (p (cdr (assq type
2738 org-element-secondary-value-alist)))
2739 (mapc walk-data (org-element-property p data))))))))
2740 (definitions
2741 ;; Collect definitions before possibly pruning them so as
2742 ;; to avoid parsing them again if they are required.
2743 (org-element-map data '(footnote-definition footnote-reference)
2744 (lambda (f)
2745 (cond
2746 ((eq (org-element-type f) 'footnote-definition) f)
2747 ((eq (org-element-property :type f) 'standard) nil)
2748 (t (let ((label (org-element-property :label f)))
2749 (when label ;Skip anonymous references.
2750 (apply
2751 #'org-element-create
2752 'footnote-definition `(:label ,label :post-blank 1)
2753 (org-element-contents f))))))))))
2754 ;; If a select tag is active, also ignore the section before the
2755 ;; first headline, if any.
2756 (when selected
2757 (let ((first-element (car (org-element-contents data))))
2758 (when (eq (org-element-type first-element) 'section)
2759 (org-element-extract-element first-element))))
2760 ;; Prune tree and communication channel.
2761 (funcall walk-data data)
2762 (dolist (entry (append
2763 ;; Priority is given to back-end specific options.
2764 (org-export-get-all-options (plist-get info :back-end))
2765 org-export-options-alist))
2766 (when (eq (nth 4 entry) 'parse)
2767 (funcall walk-data (plist-get info (car entry)))))
2768 (let ((missing (org-export--missing-definitions data definitions)))
2769 (funcall walk-data missing)
2770 (org-export--install-footnote-definitions missing data))
2771 ;; Eventually set `:ignore-list'.
2772 (plist-put info :ignore-list ignore)))
2774 (defun org-export--missing-definitions (tree definitions)
2775 "List footnote definitions missing from TREE.
2776 Missing definitions are searched within DEFINITIONS, which is
2777 a list of footnote definitions or in the widened buffer."
2778 (let* ((list-labels
2779 (lambda (data)
2780 ;; List all footnote labels encountered in DATA. Inline
2781 ;; footnote references are ignored.
2782 (org-element-map data 'footnote-reference
2783 (lambda (reference)
2784 (and (eq (org-element-property :type reference) 'standard)
2785 (org-element-property :label reference))))))
2786 defined undefined missing-definitions)
2787 ;; Partition DIRECT-REFERENCES between DEFINED and UNDEFINED
2788 ;; references.
2789 (let ((known-definitions
2790 (org-element-map tree '(footnote-reference footnote-definition)
2791 (lambda (f)
2792 (and (or (eq (org-element-type f) 'footnote-definition)
2793 (eq (org-element-property :type f) 'inline))
2794 (org-element-property :label f)))))
2795 seen)
2796 (dolist (l (funcall list-labels tree))
2797 (cond ((member l seen))
2798 ((member l known-definitions) (push l defined))
2799 (t (push l undefined)))))
2800 ;; Complete MISSING-DEFINITIONS by finding the definition of every
2801 ;; undefined label, first by looking into DEFINITIONS, then by
2802 ;; searching the widened buffer. This is a recursive process
2803 ;; since definitions found can themselves contain an undefined
2804 ;; reference.
2805 (while undefined
2806 (let* ((label (pop undefined))
2807 (definition
2808 (cond
2809 ((cl-some
2810 (lambda (d) (and (equal (org-element-property :label d) label)
2812 definitions))
2813 ((pcase (org-footnote-get-definition label)
2814 (`(,_ ,beg . ,_)
2815 (org-with-wide-buffer
2816 (goto-char beg)
2817 (let ((datum (org-element-context)))
2818 (if (eq (org-element-type datum) 'footnote-reference)
2819 datum
2820 ;; Parse definition with contents.
2821 (save-restriction
2822 (narrow-to-region
2823 (org-element-property :begin datum)
2824 (org-element-property :end datum))
2825 (org-element-map (org-element-parse-buffer)
2826 'footnote-definition #'identity nil t))))))
2827 (_ nil)))
2828 (t (user-error "Definition not found for footnote %s" label)))))
2829 (push label defined)
2830 (push definition missing-definitions)
2831 ;; Look for footnote references within DEFINITION, since
2832 ;; we may need to also find their definition.
2833 (dolist (l (funcall list-labels definition))
2834 (unless (or (member l defined) ;Known label
2835 (member l undefined)) ;Processed later
2836 (push l undefined)))))
2837 ;; MISSING-DEFINITIONS may contain footnote references with inline
2838 ;; definitions. Make sure those are changed into real footnote
2839 ;; definitions.
2840 (mapcar (lambda (d)
2841 (if (eq (org-element-type d) 'footnote-definition) d
2842 (let ((label (org-element-property :label d)))
2843 (apply #'org-element-create
2844 'footnote-definition `(:label ,label :post-blank 1)
2845 (org-element-contents d)))))
2846 missing-definitions)))
2848 (defun org-export--install-footnote-definitions (definitions tree)
2849 "Install footnote definitions in tree.
2851 DEFINITIONS is the list of footnote definitions to install. TREE
2852 is the parse tree.
2854 If there is a footnote section in TREE, definitions found are
2855 appended to it. If `org-footnote-section' is non-nil, a new
2856 footnote section containing all definitions is inserted in TREE.
2857 Otherwise, definitions are appended at the end of the section
2858 containing their first reference."
2859 (cond
2860 ((null definitions))
2861 ;; If there is a footnote section, insert definitions there.
2862 ((let ((footnote-section
2863 (org-element-map tree 'headline
2864 (lambda (h) (and (org-element-property :footnote-section-p h) h))
2865 nil t)))
2866 (and footnote-section
2867 (apply #'org-element-adopt-elements
2868 footnote-section
2869 (nreverse definitions)))))
2870 ;; If there should be a footnote section, create one containing all
2871 ;; the definitions at the end of the tree.
2872 (org-footnote-section
2873 (org-element-adopt-elements
2874 tree
2875 (org-element-create 'headline
2876 (list :footnote-section-p t
2877 :level 1
2878 :title org-footnote-section
2879 :raw-value org-footnote-section)
2880 (apply #'org-element-create
2881 'section
2883 (nreverse definitions)))))
2884 ;; Otherwise add each definition at the end of the section where it
2885 ;; is first referenced.
2887 (letrec ((seen nil)
2888 (insert-definitions
2889 (lambda (data)
2890 ;; Insert footnote definitions in the same section as
2891 ;; their first reference in DATA.
2892 (org-element-map data 'footnote-reference
2893 (lambda (reference)
2894 (when (eq (org-element-property :type reference) 'standard)
2895 (let ((label (org-element-property :label reference)))
2896 (unless (member label seen)
2897 (push label seen)
2898 (let ((definition
2899 (cl-some
2900 (lambda (d)
2901 (and (equal (org-element-property :label d)
2902 label)
2904 definitions)))
2905 (org-element-adopt-elements
2906 (org-element-lineage reference '(section))
2907 definition)
2908 ;; Also insert definitions for nested
2909 ;; references, if any.
2910 (funcall insert-definitions definition))))))))))
2911 (funcall insert-definitions tree)))))
2913 (defun org-export--remove-uninterpreted-data (data info)
2914 "Change uninterpreted elements back into Org syntax.
2915 DATA is a parse tree or a secondary string. INFO is a plist
2916 containing export options. It is modified by side effect and
2917 returned by the function."
2918 (org-element-map data
2919 '(entity bold italic latex-environment latex-fragment strike-through
2920 subscript superscript underline)
2921 (lambda (datum)
2922 (let ((new
2923 (cl-case (org-element-type datum)
2924 ;; ... entities...
2925 (entity
2926 (and (not (plist-get info :with-entities))
2927 (list (concat
2928 (org-export-expand datum nil)
2929 (make-string
2930 (or (org-element-property :post-blank datum) 0)
2931 ?\s)))))
2932 ;; ... emphasis...
2933 ((bold italic strike-through underline)
2934 (and (not (plist-get info :with-emphasize))
2935 (let ((marker (cl-case (org-element-type datum)
2936 (bold "*")
2937 (italic "/")
2938 (strike-through "+")
2939 (underline "_"))))
2940 (append
2941 (list marker)
2942 (org-element-contents datum)
2943 (list (concat
2944 marker
2945 (make-string
2946 (or (org-element-property :post-blank datum)
2948 ?\s)))))))
2949 ;; ... LaTeX environments and fragments...
2950 ((latex-environment latex-fragment)
2951 (and (eq (plist-get info :with-latex) 'verbatim)
2952 (list (org-export-expand datum nil))))
2953 ;; ... sub/superscripts...
2954 ((subscript superscript)
2955 (let ((sub/super-p (plist-get info :with-sub-superscript))
2956 (bracketp (org-element-property :use-brackets-p datum)))
2957 (and (or (not sub/super-p)
2958 (and (eq sub/super-p '{}) (not bracketp)))
2959 (append
2960 (list (concat
2961 (if (eq (org-element-type datum) 'subscript)
2963 "^")
2964 (and bracketp "{")))
2965 (org-element-contents datum)
2966 (list (concat
2967 (and bracketp "}")
2968 (and (org-element-property :post-blank datum)
2969 (make-string
2970 (org-element-property :post-blank datum)
2971 ?\s)))))))))))
2972 (when new
2973 ;; Splice NEW at DATUM location in parse tree.
2974 (dolist (e new (org-element-extract-element datum))
2975 (unless (equal e "") (org-element-insert-before e datum))))))
2976 info nil nil t)
2977 ;; Return modified parse tree.
2978 data)
2980 ;;;###autoload
2981 (defun org-export-as
2982 (backend &optional subtreep visible-only body-only ext-plist)
2983 "Transcode current Org buffer into BACKEND code.
2985 BACKEND is either an export back-end, as returned by, e.g.,
2986 `org-export-create-backend', or a symbol referring to
2987 a registered back-end.
2989 If narrowing is active in the current buffer, only transcode its
2990 narrowed part.
2992 If a region is active, transcode that region.
2994 When optional argument SUBTREEP is non-nil, transcode the
2995 sub-tree at point, extracting information from the headline
2996 properties first.
2998 When optional argument VISIBLE-ONLY is non-nil, don't export
2999 contents of hidden elements.
3001 When optional argument BODY-ONLY is non-nil, only return body
3002 code, without surrounding template.
3004 Optional argument EXT-PLIST, when provided, is a property list
3005 with external parameters overriding Org default settings, but
3006 still inferior to file-local settings.
3008 Return code as a string."
3009 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
3010 (org-export-barf-if-invalid-backend backend)
3011 (save-excursion
3012 (save-restriction
3013 ;; Narrow buffer to an appropriate region or subtree for
3014 ;; parsing. If parsing subtree, be sure to remove main
3015 ;; headline, planning data and property drawer.
3016 (cond ((org-region-active-p)
3017 (narrow-to-region (region-beginning) (region-end)))
3018 (subtreep
3019 (org-narrow-to-subtree)
3020 (goto-char (point-min))
3021 (org-end-of-meta-data)
3022 (narrow-to-region (point) (point-max))))
3023 ;; Initialize communication channel with original buffer
3024 ;; attributes, unavailable in its copy.
3025 (let* ((org-export-current-backend (org-export-backend-name backend))
3026 (info (org-combine-plists
3027 (org-export--get-export-attributes
3028 backend subtreep visible-only body-only)
3029 (org-export--get-buffer-attributes)))
3030 (parsed-keywords
3031 (delq nil
3032 (mapcar (lambda (o) (and (eq (nth 4 o) 'parse) (nth 1 o)))
3033 (append (org-export-get-all-options backend)
3034 org-export-options-alist))))
3035 tree)
3036 ;; Update communication channel and get parse tree. Buffer
3037 ;; isn't parsed directly. Instead, all buffer modifications
3038 ;; and consequent parsing are undertaken in a temporary copy.
3039 (org-export-with-buffer-copy
3040 ;; Run first hook with current back-end's name as argument.
3041 (run-hook-with-args 'org-export-before-processing-hook
3042 (org-export-backend-name backend))
3043 ;; Include files, delete comments and expand macros.
3044 (org-export-expand-include-keyword)
3045 (org-export--delete-comment-trees)
3046 (org-macro-initialize-templates)
3047 (org-macro-replace-all
3048 (append org-macro-templates org-export-global-macros)
3049 nil parsed-keywords)
3050 ;; Refresh buffer properties and radio targets after
3051 ;; potentially invasive previous changes. Likewise, do it
3052 ;; again after executing Babel code.
3053 (org-set-regexps-and-options)
3054 (org-update-radio-target-regexp)
3055 (when org-export-use-babel
3056 (org-babel-exp-process-buffer)
3057 (org-set-regexps-and-options)
3058 (org-update-radio-target-regexp))
3059 ;; Run last hook with current back-end's name as argument.
3060 ;; Update buffer properties and radio targets one last time
3061 ;; before parsing.
3062 (goto-char (point-min))
3063 (save-excursion
3064 (run-hook-with-args 'org-export-before-parsing-hook
3065 (org-export-backend-name backend)))
3066 (org-set-regexps-and-options)
3067 (org-update-radio-target-regexp)
3068 ;; Update communication channel with environment.
3069 (setq info
3070 (org-combine-plists
3071 info (org-export-get-environment backend subtreep ext-plist)))
3072 ;; De-activate uninterpreted data from parsed keywords.
3073 (dolist (entry (append (org-export-get-all-options backend)
3074 org-export-options-alist))
3075 (pcase entry
3076 (`(,p ,_ ,_ ,_ parse)
3077 (let ((value (plist-get info p)))
3078 (plist-put info
3080 (org-export--remove-uninterpreted-data value info))))
3081 (_ nil)))
3082 ;; Install user's and developer's filters.
3083 (setq info (org-export-install-filters info))
3084 ;; Call options filters and update export options. We do not
3085 ;; use `org-export-filter-apply-functions' here since the
3086 ;; arity of such filters is different.
3087 (let ((backend-name (org-export-backend-name backend)))
3088 (dolist (filter (plist-get info :filter-options))
3089 (let ((result (funcall filter info backend-name)))
3090 (when result (setq info result)))))
3091 ;; Expand export-specific set of macros: {{{author}}},
3092 ;; {{{date(FORMAT)}}}, {{{email}}} and {{{title}}}. It must
3093 ;; be done once regular macros have been expanded, since
3094 ;; parsed keywords may contain one of them.
3095 (org-macro-replace-all
3096 (list
3097 (cons "author" (org-element-interpret-data (plist-get info :author)))
3098 (cons "date"
3099 (let* ((date (plist-get info :date))
3100 (value (or (org-element-interpret-data date) "")))
3101 (if (and (consp date)
3102 (not (cdr date))
3103 (eq (org-element-type (car date)) 'timestamp))
3104 (format "(eval (if (org-string-nw-p \"$1\") %s %S))"
3105 (format "(org-timestamp-format '%S \"$1\")"
3106 (org-element-copy (car date)))
3107 value)
3108 value)))
3109 (cons "email" (org-element-interpret-data (plist-get info :email)))
3110 (cons "title" (org-element-interpret-data (plist-get info :title)))
3111 (cons "results" "$1"))
3112 'finalize
3113 parsed-keywords)
3114 ;; Parse buffer.
3115 (setq tree (org-element-parse-buffer nil visible-only))
3116 ;; Prune tree from non-exported elements and transform
3117 ;; uninterpreted elements or objects in both parse tree and
3118 ;; communication channel.
3119 (org-export--prune-tree tree info)
3120 (org-export--remove-uninterpreted-data tree info)
3121 ;; Call parse tree filters.
3122 (setq tree
3123 (org-export-filter-apply-functions
3124 (plist-get info :filter-parse-tree) tree info))
3125 ;; Now tree is complete, compute its properties and add them
3126 ;; to communication channel.
3127 (setq info (org-export--collect-tree-properties tree info))
3128 ;; Eventually transcode TREE. Wrap the resulting string into
3129 ;; a template.
3130 (let* ((body (org-element-normalize-string
3131 (or (org-export-data tree info) "")))
3132 (inner-template (cdr (assq 'inner-template
3133 (plist-get info :translate-alist))))
3134 (full-body (org-export-filter-apply-functions
3135 (plist-get info :filter-body)
3136 (if (not (functionp inner-template)) body
3137 (funcall inner-template body info))
3138 info))
3139 (template (cdr (assq 'template
3140 (plist-get info :translate-alist)))))
3141 ;; Remove all text properties since they cannot be
3142 ;; retrieved from an external process. Finally call
3143 ;; final-output filter and return result.
3144 (org-no-properties
3145 (org-export-filter-apply-functions
3146 (plist-get info :filter-final-output)
3147 (if (or (not (functionp template)) body-only) full-body
3148 (funcall template full-body info))
3149 info))))))))
3151 ;;;###autoload
3152 (defun org-export-string-as (string backend &optional body-only ext-plist)
3153 "Transcode STRING into BACKEND code.
3155 BACKEND is either an export back-end, as returned by, e.g.,
3156 `org-export-create-backend', or a symbol referring to
3157 a registered back-end.
3159 When optional argument BODY-ONLY is non-nil, only return body
3160 code, without preamble nor postamble.
3162 Optional argument EXT-PLIST, when provided, is a property list
3163 with external parameters overriding Org default settings, but
3164 still inferior to file-local settings.
3166 Return code as a string."
3167 (with-temp-buffer
3168 (insert string)
3169 (let ((org-inhibit-startup t)) (org-mode))
3170 (org-export-as backend nil nil body-only ext-plist)))
3172 ;;;###autoload
3173 (defun org-export-replace-region-by (backend)
3174 "Replace the active region by its export to BACKEND.
3175 BACKEND is either an export back-end, as returned by, e.g.,
3176 `org-export-create-backend', or a symbol referring to
3177 a registered back-end."
3178 (unless (org-region-active-p) (user-error "No active region to replace"))
3179 (insert
3180 (org-export-string-as
3181 (delete-and-extract-region (region-beginning) (region-end)) backend t)))
3183 ;;;###autoload
3184 (defun org-export-insert-default-template (&optional backend subtreep)
3185 "Insert all export keywords with default values at beginning of line.
3187 BACKEND is a symbol referring to the name of a registered export
3188 back-end, for which specific export options should be added to
3189 the template, or `default' for default template. When it is nil,
3190 the user will be prompted for a category.
3192 If SUBTREEP is non-nil, export configuration will be set up
3193 locally for the subtree through node properties."
3194 (interactive)
3195 (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
3196 (when (and subtreep (org-before-first-heading-p))
3197 (user-error "No subtree to set export options for"))
3198 (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
3199 (backend
3200 (or backend
3201 (intern
3202 (org-completing-read
3203 "Options category: "
3204 (cons "default"
3205 (mapcar (lambda (b)
3206 (symbol-name (org-export-backend-name b)))
3207 org-export-registered-backends))
3208 nil t))))
3209 options keywords)
3210 ;; Populate OPTIONS and KEYWORDS.
3211 (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
3212 ((org-export-backend-p backend)
3213 (org-export-backend-options backend))
3214 (t (org-export-backend-options
3215 (org-export-get-backend backend)))))
3216 (let ((keyword (nth 1 entry))
3217 (option (nth 2 entry)))
3218 (cond
3219 (keyword (unless (assoc keyword keywords)
3220 (let ((value
3221 (if (eq (nth 4 entry) 'split)
3222 (mapconcat #'identity (eval (nth 3 entry)) " ")
3223 (eval (nth 3 entry)))))
3224 (push (cons keyword value) keywords))))
3225 (option (unless (assoc option options)
3226 (push (cons option (eval (nth 3 entry))) options))))))
3227 ;; Move to an appropriate location in order to insert options.
3228 (unless subtreep (beginning-of-line))
3229 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3230 (when options
3231 (let ((items
3232 (mapcar
3233 #'(lambda (opt) (format "%s:%S" (car opt) (cdr opt)))
3234 (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
3235 (if subtreep
3236 (org-entry-put
3237 node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
3238 (while items
3239 (insert "#+OPTIONS:")
3240 (let ((width 10))
3241 (while (and items
3242 (< (+ width (length (car items)) 1) fill-column))
3243 (let ((item (pop items)))
3244 (insert " " item)
3245 (cl-incf width (1+ (length item))))))
3246 (insert "\n")))))
3247 ;; Then the rest of keywords, in the order specified in either
3248 ;; `org-export-options-alist' or respective export back-ends.
3249 (dolist (key (nreverse keywords))
3250 (let ((val (cond ((equal (car key) "DATE")
3251 (or (cdr key)
3252 (with-temp-buffer
3253 (org-insert-time-stamp (current-time)))))
3254 ((equal (car key) "TITLE")
3255 (or (let ((visited-file
3256 (buffer-file-name (buffer-base-buffer))))
3257 (and visited-file
3258 (file-name-sans-extension
3259 (file-name-nondirectory visited-file))))
3260 (buffer-name (buffer-base-buffer))))
3261 (t (cdr key)))))
3262 (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
3263 (insert
3264 (format "#+%s:%s\n"
3265 (car key)
3266 (if (org-string-nw-p val) (format " %s" val) ""))))))))
3268 (defun org-export-expand-include-keyword (&optional included dir footnotes)
3269 "Expand every include keyword in buffer.
3270 Optional argument INCLUDED is a list of included file names along
3271 with their line restriction, when appropriate. It is used to
3272 avoid infinite recursion. Optional argument DIR is the current
3273 working directory. It is used to properly resolve relative
3274 paths. Optional argument FOOTNOTES is a hash-table used for
3275 storing and resolving footnotes. It is created automatically."
3276 (let ((case-fold-search t)
3277 (file-prefix (make-hash-table :test #'equal))
3278 (current-prefix 0)
3279 (footnotes (or footnotes (make-hash-table :test #'equal)))
3280 (include-re "^[ \t]*#\\+INCLUDE:"))
3281 ;; If :minlevel is not set the text-property
3282 ;; `:org-include-induced-level' will be used to determine the
3283 ;; relative level when expanding INCLUDE.
3284 ;; Only affects included Org documents.
3285 (goto-char (point-min))
3286 (while (re-search-forward include-re nil t)
3287 (put-text-property (line-beginning-position) (line-end-position)
3288 :org-include-induced-level
3289 (1+ (org-reduced-level (or (org-current-level) 0)))))
3290 ;; Expand INCLUDE keywords.
3291 (goto-char (point-min))
3292 (while (re-search-forward include-re nil t)
3293 (unless (org-in-commented-heading-p)
3294 (let ((element (save-match-data (org-element-at-point))))
3295 (when (eq (org-element-type element) 'keyword)
3296 (beginning-of-line)
3297 ;; Extract arguments from keyword's value.
3298 (let* ((value (org-element-property :value element))
3299 (ind (org-get-indentation))
3300 location
3301 (file
3302 (and (string-match
3303 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
3304 (prog1
3305 (save-match-data
3306 (let ((matched (match-string 1 value)))
3307 (when (string-match "\\(::\\(.*?\\)\\)\"?\\'"
3308 matched)
3309 (setq location (match-string 2 matched))
3310 (setq matched
3311 (replace-match "" nil nil matched 1)))
3312 (expand-file-name
3313 (org-unbracket-string "\"" "\"" matched)
3314 dir)))
3315 (setq value (replace-match "" nil nil value)))))
3316 (only-contents
3317 (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?"
3318 value)
3319 (prog1 (org-not-nil (match-string 1 value))
3320 (setq value (replace-match "" nil nil value)))))
3321 (lines
3322 (and (string-match
3323 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3324 value)
3325 (prog1 (match-string 1 value)
3326 (setq value (replace-match "" nil nil value)))))
3327 (env (cond
3328 ((string-match "\\<example\\>" value) 'literal)
3329 ((string-match "\\<export\\(?: +\\(.*\\)\\)?" value)
3330 'literal)
3331 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
3332 'literal)))
3333 ;; Minimal level of included file defaults to the
3334 ;; child level of the current headline, if any, or
3335 ;; one. It only applies is the file is meant to be
3336 ;; included as an Org one.
3337 (minlevel
3338 (and (not env)
3339 (if (string-match ":minlevel +\\([0-9]+\\)" value)
3340 (prog1 (string-to-number (match-string 1 value))
3341 (setq value (replace-match "" nil nil value)))
3342 (get-text-property (point)
3343 :org-include-induced-level))))
3344 (args (and (eq env 'literal) (match-string 1 value)))
3345 (block (and (string-match "\\<\\(\\S-+\\)\\>" value)
3346 (match-string 1 value))))
3347 ;; Remove keyword.
3348 (delete-region (point) (line-beginning-position 2))
3349 (cond
3350 ((not file) nil)
3351 ((not (file-readable-p file))
3352 (error "Cannot include file %s" file))
3353 ;; Check if files has already been parsed. Look after
3354 ;; inclusion lines too, as different parts of the same
3355 ;; file can be included too.
3356 ((member (list file lines) included)
3357 (error "Recursive file inclusion: %s" file))
3359 (cond
3360 ((eq env 'literal)
3361 (insert
3362 (let ((ind-str (make-string ind ?\s))
3363 (arg-str (if (stringp args) (format " %s" args) ""))
3364 (contents
3365 (org-escape-code-in-string
3366 (org-export--prepare-file-contents file lines))))
3367 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3368 ind-str block arg-str contents ind-str block))))
3369 ((stringp block)
3370 (insert
3371 (let ((ind-str (make-string ind ?\s))
3372 (contents
3373 (org-export--prepare-file-contents file lines)))
3374 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3375 ind-str block contents ind-str block))))
3377 (insert
3378 (with-temp-buffer
3379 (let ((org-inhibit-startup t)
3380 (lines
3381 (if location
3382 (org-export--inclusion-absolute-lines
3383 file location only-contents lines)
3384 lines)))
3385 (org-mode)
3386 (insert
3387 (org-export--prepare-file-contents
3388 file lines ind minlevel
3390 (gethash file file-prefix)
3391 (puthash file (cl-incf current-prefix) file-prefix))
3392 footnotes)))
3393 (org-export-expand-include-keyword
3394 (cons (list file lines) included)
3395 (file-name-directory file)
3396 footnotes)
3397 (buffer-string)))))
3398 ;; Expand footnotes after all files have been
3399 ;; included. Footnotes are stored at end of buffer.
3400 (unless included
3401 (org-with-wide-buffer
3402 (goto-char (point-max))
3403 (maphash (lambda (k v)
3404 (insert (format "\n[fn:%s] %s\n" k v)))
3405 footnotes))))))))))))
3407 (defun org-export--inclusion-absolute-lines (file location only-contents lines)
3408 "Resolve absolute lines for an included file with file-link.
3410 FILE is string file-name of the file to include. LOCATION is a
3411 string name within FILE to be included (located via
3412 `org-link-search'). If ONLY-CONTENTS is non-nil only the
3413 contents of the named element will be included, as determined
3414 Org-Element. If LINES is non-nil only those lines are included.
3416 Return a string of lines to be included in the format expected by
3417 `org-export--prepare-file-contents'."
3418 (with-temp-buffer
3419 (insert-file-contents file)
3420 (unless (eq major-mode 'org-mode)
3421 (let ((org-inhibit-startup t)) (org-mode)))
3422 (condition-case err
3423 ;; Enforce consistent search.
3424 (let ((org-link-search-must-match-exact-headline nil))
3425 (org-link-search location))
3426 (error
3427 (error "%s for %s::%s" (error-message-string err) file location)))
3428 (let* ((element (org-element-at-point))
3429 (contents-begin
3430 (and only-contents (org-element-property :contents-begin element))))
3431 (narrow-to-region
3432 (or contents-begin (org-element-property :begin element))
3433 (org-element-property (if contents-begin :contents-end :end) element))
3434 (when (and only-contents
3435 (memq (org-element-type element) '(headline inlinetask)))
3436 ;; Skip planning line and property-drawer.
3437 (goto-char (point-min))
3438 (when (looking-at-p org-planning-line-re) (forward-line))
3439 (when (looking-at org-property-drawer-re) (goto-char (match-end 0)))
3440 (unless (bolp) (forward-line))
3441 (narrow-to-region (point) (point-max))))
3442 (when lines
3443 (org-skip-whitespace)
3444 (beginning-of-line)
3445 (let* ((lines (split-string lines "-"))
3446 (lbeg (string-to-number (car lines)))
3447 (lend (string-to-number (cadr lines)))
3448 (beg (if (zerop lbeg) (point-min)
3449 (goto-char (point-min))
3450 (forward-line (1- lbeg))
3451 (point)))
3452 (end (if (zerop lend) (point-max)
3453 (goto-char beg)
3454 (forward-line (1- lend))
3455 (point))))
3456 (narrow-to-region beg end)))
3457 (let ((end (point-max)))
3458 (goto-char (point-min))
3459 (widen)
3460 (let ((start-line (line-number-at-pos)))
3461 (format "%d-%d"
3462 start-line
3463 (save-excursion
3464 (+ start-line
3465 (let ((counter 0))
3466 (while (< (point) end) (cl-incf counter) (forward-line))
3467 counter))))))))
3469 (defun org-export--prepare-file-contents
3470 (file &optional lines ind minlevel id footnotes)
3471 "Prepare contents of FILE for inclusion and return it as a string.
3473 When optional argument LINES is a string specifying a range of
3474 lines, include only those lines.
3476 Optional argument IND, when non-nil, is an integer specifying the
3477 global indentation of returned contents. Since its purpose is to
3478 allow an included file to stay in the same environment it was
3479 created (e.g., a list item), it doesn't apply past the first
3480 headline encountered.
3482 Optional argument MINLEVEL, when non-nil, is an integer
3483 specifying the level that any top-level headline in the included
3484 file should have.
3486 Optional argument ID is an integer that will be inserted before
3487 each footnote definition and reference if FILE is an Org file.
3488 This is useful to avoid conflicts when more than one Org file
3489 with footnotes is included in a document.
3491 Optional argument FOOTNOTES is a hash-table to store footnotes in
3492 the included document."
3493 (with-temp-buffer
3494 (insert-file-contents file)
3495 (when lines
3496 (let* ((lines (split-string lines "-"))
3497 (lbeg (string-to-number (car lines)))
3498 (lend (string-to-number (cadr lines)))
3499 (beg (if (zerop lbeg) (point-min)
3500 (goto-char (point-min))
3501 (forward-line (1- lbeg))
3502 (point)))
3503 (end (if (zerop lend) (point-max)
3504 (goto-char (point-min))
3505 (forward-line (1- lend))
3506 (point))))
3507 (narrow-to-region beg end)))
3508 ;; Remove blank lines at beginning and end of contents. The logic
3509 ;; behind that removal is that blank lines around include keyword
3510 ;; override blank lines in included file.
3511 (goto-char (point-min))
3512 (org-skip-whitespace)
3513 (beginning-of-line)
3514 (delete-region (point-min) (point))
3515 (goto-char (point-max))
3516 (skip-chars-backward " \r\t\n")
3517 (forward-line)
3518 (delete-region (point) (point-max))
3519 ;; If IND is set, preserve indentation of include keyword until
3520 ;; the first headline encountered.
3521 (when (and ind (> ind 0))
3522 (unless (eq major-mode 'org-mode)
3523 (let ((org-inhibit-startup t)) (org-mode)))
3524 (goto-char (point-min))
3525 (let ((ind-str (make-string ind ?\s)))
3526 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
3527 ;; Do not move footnote definitions out of column 0.
3528 (unless (and (looking-at org-footnote-definition-re)
3529 (eq (org-element-type (org-element-at-point))
3530 'footnote-definition))
3531 (insert ind-str))
3532 (forward-line))))
3533 ;; When MINLEVEL is specified, compute minimal level for headlines
3534 ;; in the file (CUR-MIN), and remove stars to each headline so
3535 ;; that headlines with minimal level have a level of MINLEVEL.
3536 (when minlevel
3537 (unless (eq major-mode 'org-mode)
3538 (let ((org-inhibit-startup t)) (org-mode)))
3539 (org-with-limited-levels
3540 (let ((levels (org-map-entries
3541 (lambda () (org-reduced-level (org-current-level))))))
3542 (when levels
3543 (let ((offset (- minlevel (apply #'min levels))))
3544 (unless (zerop offset)
3545 (when org-odd-levels-only (setq offset (* offset 2)))
3546 ;; Only change stars, don't bother moving whole
3547 ;; sections.
3548 (org-map-entries
3549 (lambda ()
3550 (if (< offset 0) (delete-char (abs offset))
3551 (insert (make-string offset ?*)))))))))))
3552 ;; Append ID to all footnote references and definitions, so they
3553 ;; become file specific and cannot collide with footnotes in other
3554 ;; included files. Further, collect relevant footnote definitions
3555 ;; outside of LINES, in order to reintroduce them later.
3556 (when id
3557 (let ((marker-min (point-min-marker))
3558 (marker-max (point-max-marker))
3559 (get-new-label
3560 (lambda (label)
3561 ;; Generate new label from LABEL by prefixing it with
3562 ;; "-ID-".
3563 (format "-%d-%s" id label)))
3564 (set-new-label
3565 (lambda (f old new)
3566 ;; Replace OLD label with NEW in footnote F.
3567 (save-excursion
3568 (goto-char (+ (org-element-property :begin f) 4))
3569 (looking-at (regexp-quote old))
3570 (replace-match new))))
3571 (seen-alist))
3572 (goto-char (point-min))
3573 (while (re-search-forward org-footnote-re nil t)
3574 (let ((footnote (save-excursion
3575 (backward-char)
3576 (org-element-context))))
3577 (when (memq (org-element-type footnote)
3578 '(footnote-definition footnote-reference))
3579 (let* ((label (org-element-property :label footnote)))
3580 ;; Update the footnote-reference at point and collect
3581 ;; the new label, which is only used for footnotes
3582 ;; outsides LINES.
3583 (when label
3584 (let ((seen (cdr (assoc label seen-alist))))
3585 (if seen (funcall set-new-label footnote label seen)
3586 (let ((new (funcall get-new-label label)))
3587 (push (cons label new) seen-alist)
3588 (org-with-wide-buffer
3589 (let* ((def (org-footnote-get-definition label))
3590 (beg (nth 1 def)))
3591 (when (and def
3592 (or (< beg marker-min)
3593 (>= beg marker-max)))
3594 ;; Store since footnote-definition is
3595 ;; outside of LINES.
3596 (puthash new
3597 (org-element-normalize-string (nth 3 def))
3598 footnotes))))
3599 (funcall set-new-label footnote label new)))))))))
3600 (set-marker marker-min nil)
3601 (set-marker marker-max nil)))
3602 (org-element-normalize-string (buffer-string))))
3604 (defun org-export--copy-to-kill-ring-p ()
3605 "Return a non-nil value when output should be added to the kill ring.
3606 See also `org-export-copy-to-kill-ring'."
3607 (if (eq org-export-copy-to-kill-ring 'if-interactive)
3608 (not (or executing-kbd-macro noninteractive))
3609 (eq org-export-copy-to-kill-ring t)))
3613 ;;; Tools For Back-Ends
3615 ;; A whole set of tools is available to help build new exporters. Any
3616 ;; function general enough to have its use across many back-ends
3617 ;; should be added here.
3619 ;;;; For Affiliated Keywords
3621 ;; `org-export-read-attribute' reads a property from a given element
3622 ;; as a plist. It can be used to normalize affiliated keywords'
3623 ;; syntax.
3625 ;; Since captions can span over multiple lines and accept dual values,
3626 ;; their internal representation is a bit tricky. Therefore,
3627 ;; `org-export-get-caption' transparently returns a given element's
3628 ;; caption as a secondary string.
3630 (defun org-export-read-attribute (attribute element &optional property)
3631 "Turn ATTRIBUTE property from ELEMENT into a plist.
3633 When optional argument PROPERTY is non-nil, return the value of
3634 that property within attributes.
3636 This function assumes attributes are defined as \":keyword
3637 value\" pairs. It is appropriate for `:attr_html' like
3638 properties.
3640 All values will become strings except the empty string and
3641 \"nil\", which will become nil. Also, values containing only
3642 double quotes will be read as-is, which means that \"\" value
3643 will become the empty string."
3644 (let* ((prepare-value
3645 (lambda (str)
3646 (save-match-data
3647 (cond ((member str '(nil "" "nil")) nil)
3648 ((string-match "^\"\\(\"+\\)?\"$" str)
3649 (or (match-string 1 str) ""))
3650 (t str)))))
3651 (attributes
3652 (let ((value (org-element-property attribute element)))
3653 (when value
3654 (let ((s (mapconcat 'identity value " ")) result)
3655 (while (string-match
3656 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3658 (let ((value (substring s 0 (match-beginning 0))))
3659 (push (funcall prepare-value value) result))
3660 (push (intern (match-string 1 s)) result)
3661 (setq s (substring s (match-end 0))))
3662 ;; Ignore any string before first property with `cdr'.
3663 (cdr (nreverse (cons (funcall prepare-value s) result))))))))
3664 (if property (plist-get attributes property) attributes)))
3666 (defun org-export-get-caption (element &optional shortp)
3667 "Return caption from ELEMENT as a secondary string.
3669 When optional argument SHORTP is non-nil, return short caption,
3670 as a secondary string, instead.
3672 Caption lines are separated by a white space."
3673 (let ((full-caption (org-element-property :caption element)) caption)
3674 (dolist (line full-caption (cdr caption))
3675 (let ((cap (funcall (if shortp 'cdr 'car) line)))
3676 (when cap
3677 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
3680 ;;;; For Derived Back-ends
3682 ;; `org-export-with-backend' is a function allowing to locally use
3683 ;; another back-end to transcode some object or element. In a derived
3684 ;; back-end, it may be used as a fall-back function once all specific
3685 ;; cases have been treated.
3687 (defun org-export-with-backend (backend data &optional contents info)
3688 "Call a transcoder from BACKEND on DATA.
3689 BACKEND is an export back-end, as returned by, e.g.,
3690 `org-export-create-backend', or a symbol referring to
3691 a registered back-end. DATA is an Org element, object, secondary
3692 string or string. CONTENTS, when non-nil, is the transcoded
3693 contents of DATA element, as a string. INFO, when non-nil, is
3694 the communication channel used for export, as a plist."
3695 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
3696 (org-export-barf-if-invalid-backend backend)
3697 (let ((type (org-element-type data)))
3698 (when (memq type '(nil org-data)) (error "No foreign transcoder available"))
3699 (let* ((all-transcoders (org-export-get-all-transcoders backend))
3700 (transcoder (cdr (assq type all-transcoders))))
3701 (unless (functionp transcoder) (error "No foreign transcoder available"))
3702 (let ((new-info
3703 (org-combine-plists
3704 info (list
3705 :back-end backend
3706 :translate-alist all-transcoders
3707 :exported-data (make-hash-table :test #'eq :size 401)))))
3708 ;; `:internal-references' are shared across back-ends.
3709 (prog1 (funcall transcoder data contents new-info)
3710 (plist-put info :internal-references
3711 (plist-get new-info :internal-references)))))))
3714 ;;;; For Export Snippets
3716 ;; Every export snippet is transmitted to the back-end. Though, the
3717 ;; latter will only retain one type of export-snippet, ignoring
3718 ;; others, based on the former's target back-end. The function
3719 ;; `org-export-snippet-backend' returns that back-end for a given
3720 ;; export-snippet.
3722 (defun org-export-snippet-backend (export-snippet)
3723 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3724 Translation, with `org-export-snippet-translation-alist', is
3725 applied."
3726 (let ((back-end (org-element-property :back-end export-snippet)))
3727 (intern
3728 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3729 back-end))))
3732 ;;;; For Footnotes
3734 ;; `org-export-collect-footnote-definitions' is a tool to list
3735 ;; actually used footnotes definitions in the whole parse tree, or in
3736 ;; a headline, in order to add footnote listings throughout the
3737 ;; transcoded data.
3739 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3740 ;; back-ends, when they need to attach the footnote definition only to
3741 ;; the first occurrence of the corresponding label.
3743 ;; `org-export-get-footnote-definition' and
3744 ;; `org-export-get-footnote-number' provide easier access to
3745 ;; additional information relative to a footnote reference.
3747 (defun org-export-get-footnote-definition (footnote-reference info)
3748 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3749 INFO is the plist used as a communication channel. If no such
3750 definition can be found, raise an error."
3751 (let ((label (org-element-property :label footnote-reference)))
3752 (if (not label) (org-element-contents footnote-reference)
3753 (let ((cache (or (plist-get info :footnote-definition-cache)
3754 (let ((hash (make-hash-table :test #'equal)))
3755 (plist-put info :footnote-definition-cache hash)
3756 hash))))
3758 (gethash label cache)
3759 (puthash label
3760 (org-element-map (plist-get info :parse-tree)
3761 '(footnote-definition footnote-reference)
3762 (lambda (f)
3763 (cond
3764 ;; Skip any footnote with a different label.
3765 ;; Also skip any standard footnote reference
3766 ;; with the same label since those cannot
3767 ;; contain a definition.
3768 ((not (equal (org-element-property :label f) label)) nil)
3769 ((eq (org-element-property :type f) 'standard) nil)
3770 ((org-element-contents f))
3771 ;; Even if the contents are empty, we can not
3772 ;; return nil since that would eventually raise
3773 ;; the error. Instead, return the equivalent
3774 ;; empty string.
3775 (t "")))
3776 info t)
3777 cache)
3778 (error "Definition not found for footnote %s" label))))))
3780 (defun org-export--footnote-reference-map
3781 (function data info &optional body-first)
3782 "Apply FUNCTION on every footnote reference in DATA.
3783 INFO is a plist containing export state. By default, as soon as
3784 a new footnote reference is encountered, FUNCTION is called onto
3785 its definition. However, if BODY-FIRST is non-nil, this step is
3786 delayed until the end of the process."
3787 (letrec ((definitions nil)
3788 (seen-refs nil)
3789 (search-ref
3790 (lambda (data delayp)
3791 ;; Search footnote references through DATA, filling
3792 ;; SEEN-REFS along the way. When DELAYP is non-nil,
3793 ;; store footnote definitions so they can be entered
3794 ;; later.
3795 (org-element-map data 'footnote-reference
3796 (lambda (f)
3797 (funcall function f)
3798 (let ((--label (org-element-property :label f)))
3799 (unless (and --label (member --label seen-refs))
3800 (when --label (push --label seen-refs))
3801 ;; Search for subsequent references in footnote
3802 ;; definition so numbering follows reading
3803 ;; logic, unless DELAYP in non-nil.
3804 (cond
3805 (delayp
3806 (push (org-export-get-footnote-definition f info)
3807 definitions))
3808 ;; Do not force entering inline definitions,
3809 ;; since `org-element-map' already traverses
3810 ;; them at the right time.
3811 ((eq (org-element-property :type f) 'inline))
3812 (t (funcall search-ref
3813 (org-export-get-footnote-definition f info)
3814 nil))))))
3815 info nil
3816 ;; Don't enter footnote definitions since it will
3817 ;; happen when their first reference is found.
3818 ;; Moreover, if DELAYP is non-nil, make sure we
3819 ;; postpone entering definitions of inline references.
3820 (if delayp '(footnote-definition footnote-reference)
3821 'footnote-definition)))))
3822 (funcall search-ref data body-first)
3823 (funcall search-ref (nreverse definitions) nil)))
3825 (defun org-export-collect-footnote-definitions (info &optional data body-first)
3826 "Return an alist between footnote numbers, labels and definitions.
3828 INFO is the current export state, as a plist.
3830 Definitions are collected throughout the whole parse tree, or
3831 DATA when non-nil.
3833 Sorting is done by order of references. As soon as a new
3834 reference is encountered, other references are searched within
3835 its definition. However, if BODY-FIRST is non-nil, this step is
3836 delayed after the whole tree is checked. This alters results
3837 when references are found in footnote definitions.
3839 Definitions either appear as Org data or as a secondary string
3840 for inlined footnotes. Unreferenced definitions are ignored."
3841 (let ((n 0) labels alist)
3842 (org-export--footnote-reference-map
3843 (lambda (f)
3844 ;; Collect footnote number, label and definition.
3845 (let ((l (org-element-property :label f)))
3846 (unless (and l (member l labels))
3847 (cl-incf n)
3848 (push (list n l (org-export-get-footnote-definition f info)) alist))
3849 (when l (push l labels))))
3850 (or data (plist-get info :parse-tree)) info body-first)
3851 (nreverse alist)))
3853 (defun org-export-footnote-first-reference-p
3854 (footnote-reference info &optional data body-first)
3855 "Non-nil when a footnote reference is the first one for its label.
3857 FOOTNOTE-REFERENCE is the footnote reference being considered.
3858 INFO is a plist containing current export state.
3860 Search is done throughout the whole parse tree, or DATA when
3861 non-nil.
3863 By default, as soon as a new footnote reference is encountered,
3864 other references are searched within its definition. However, if
3865 BODY-FIRST is non-nil, this step is delayed after the whole tree
3866 is checked. This alters results when references are found in
3867 footnote definitions."
3868 (let ((label (org-element-property :label footnote-reference)))
3869 ;; Anonymous footnotes are always a first reference.
3870 (or (not label)
3871 (catch 'exit
3872 (org-export--footnote-reference-map
3873 (lambda (f)
3874 (let ((l (org-element-property :label f)))
3875 (when (and l label (string= label l))
3876 (throw 'exit (eq footnote-reference f)))))
3877 (or data (plist-get info :parse-tree)) info body-first)))))
3879 (defun org-export-get-footnote-number (footnote info &optional data body-first)
3880 "Return number associated to a footnote.
3882 FOOTNOTE is either a footnote reference or a footnote definition.
3883 INFO is the plist containing export state.
3885 Number is unique throughout the whole parse tree, or DATA, when
3886 non-nil.
3888 By default, as soon as a new footnote reference is encountered,
3889 counting process moves into its definition. However, if
3890 BODY-FIRST is non-nil, this step is delayed until the end of the
3891 process, leading to a different order when footnotes are nested."
3892 (let ((count 0)
3893 (seen)
3894 (label (org-element-property :label footnote)))
3895 (catch 'exit
3896 (org-export--footnote-reference-map
3897 (lambda (f)
3898 (let ((l (org-element-property :label f)))
3899 (cond
3900 ;; Anonymous footnote match: return number.
3901 ((and (not l) (not label) (eq footnote f)) (throw 'exit (1+ count)))
3902 ;; Labels match: return number.
3903 ((and label l (string= label l)) (throw 'exit (1+ count)))
3904 ;; Otherwise store label and increase counter if label
3905 ;; wasn't encountered yet.
3906 ((not l) (cl-incf count))
3907 ((not (member l seen)) (push l seen) (cl-incf count)))))
3908 (or data (plist-get info :parse-tree)) info body-first))))
3911 ;;;; For Headlines
3913 ;; `org-export-get-relative-level' is a shortcut to get headline
3914 ;; level, relatively to the lower headline level in the parsed tree.
3916 ;; `org-export-get-headline-number' returns the section number of an
3917 ;; headline, while `org-export-number-to-roman' allows it to be
3918 ;; converted to roman numbers. With an optional argument,
3919 ;; `org-export-get-headline-number' returns a number to unnumbered
3920 ;; headlines (used for internal id).
3922 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3923 ;; `org-export-last-sibling-p' are three useful predicates when it
3924 ;; comes to fulfill the `:headline-levels' property.
3926 ;; `org-export-get-tags', `org-export-get-category' and
3927 ;; `org-export-get-node-property' extract useful information from an
3928 ;; headline or a parent headline. They all handle inheritance.
3930 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3931 ;; as a secondary string, suitable for table of contents. It falls
3932 ;; back onto default title.
3934 (defun org-export-get-relative-level (headline info)
3935 "Return HEADLINE relative level within current parsed tree.
3936 INFO is a plist holding contextual information."
3937 (+ (org-element-property :level headline)
3938 (or (plist-get info :headline-offset) 0)))
3940 (defun org-export-low-level-p (headline info)
3941 "Non-nil when HEADLINE is considered as low level.
3943 INFO is a plist used as a communication channel.
3945 A low level headlines has a relative level greater than
3946 `:headline-levels' property value.
3948 Return value is the difference between HEADLINE relative level
3949 and the last level being considered as high enough, or nil."
3950 (let ((limit (plist-get info :headline-levels)))
3951 (when (wholenump limit)
3952 (let ((level (org-export-get-relative-level headline info)))
3953 (and (> level limit) (- level limit))))))
3955 (defun org-export-get-headline-number (headline info)
3956 "Return numbered HEADLINE numbering as a list of numbers.
3957 INFO is a plist holding contextual information."
3958 (and (org-export-numbered-headline-p headline info)
3959 (cdr (assq headline (plist-get info :headline-numbering)))))
3961 (defun org-export-numbered-headline-p (headline info)
3962 "Return a non-nil value if HEADLINE element should be numbered.
3963 INFO is a plist used as a communication channel."
3964 (unless (cl-some
3965 (lambda (head) (org-not-nil (org-element-property :UNNUMBERED head)))
3966 (org-element-lineage headline nil t))
3967 (let ((sec-num (plist-get info :section-numbers))
3968 (level (org-export-get-relative-level headline info)))
3969 (if (wholenump sec-num) (<= level sec-num) sec-num))))
3971 (defun org-export-number-to-roman (n)
3972 "Convert integer N into a roman numeral."
3973 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3974 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3975 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3976 ( 1 . "I")))
3977 (res ""))
3978 (if (<= n 0)
3979 (number-to-string n)
3980 (while roman
3981 (if (>= n (caar roman))
3982 (setq n (- n (caar roman))
3983 res (concat res (cdar roman)))
3984 (pop roman)))
3985 res)))
3987 (defun org-export-get-tags (element info &optional tags inherited)
3988 "Return list of tags associated to ELEMENT.
3990 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3991 is a plist used as a communication channel.
3993 When non-nil, optional argument TAGS should be a list of strings.
3994 Any tag belonging to this list will also be removed.
3996 When optional argument INHERITED is non-nil, tags can also be
3997 inherited from parent headlines and FILETAGS keywords."
3998 (cl-remove-if
3999 (lambda (tag) (member tag tags))
4000 (if (not inherited) (org-element-property :tags element)
4001 ;; Build complete list of inherited tags.
4002 (let ((current-tag-list (org-element-property :tags element)))
4003 (dolist (parent (org-element-lineage element))
4004 (dolist (tag (org-element-property :tags parent))
4005 (when (and (memq (org-element-type parent) '(headline inlinetask))
4006 (not (member tag current-tag-list)))
4007 (push tag current-tag-list))))
4008 ;; Add FILETAGS keywords and return results.
4009 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
4011 (defun org-export-get-node-property (property blob &optional inherited)
4012 "Return node PROPERTY value for BLOB.
4014 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
4015 element or object.
4017 If optional argument INHERITED is non-nil, the value can be
4018 inherited from a parent headline.
4020 Return value is a string or nil."
4021 (let ((headline (if (eq (org-element-type blob) 'headline) blob
4022 (org-export-get-parent-headline blob))))
4023 (if (not inherited) (org-element-property property blob)
4024 (let ((parent headline))
4025 (catch 'found
4026 (while parent
4027 (when (plist-member (nth 1 parent) property)
4028 (throw 'found (org-element-property property parent)))
4029 (setq parent (org-element-property :parent parent))))))))
4031 (defun org-export-get-category (blob info)
4032 "Return category for element or object BLOB.
4034 INFO is a plist used as a communication channel.
4036 CATEGORY is automatically inherited from a parent headline, from
4037 #+CATEGORY: keyword or created out of original file name. If all
4038 fail, the fall-back value is \"???\"."
4039 (or (org-export-get-node-property :CATEGORY blob t)
4040 (org-element-map (plist-get info :parse-tree) 'keyword
4041 (lambda (kwd)
4042 (when (equal (org-element-property :key kwd) "CATEGORY")
4043 (org-element-property :value kwd)))
4044 info 'first-match)
4045 (let ((file (plist-get info :input-file)))
4046 (and file (file-name-sans-extension (file-name-nondirectory file))))
4047 "???"))
4049 (defun org-export-get-alt-title (headline _)
4050 "Return alternative title for HEADLINE, as a secondary string.
4051 If no optional title is defined, fall-back to the regular title."
4052 (let ((alt (org-element-property :ALT_TITLE headline)))
4053 (if alt (org-element-parse-secondary-string
4054 alt (org-element-restriction 'headline) headline)
4055 (org-element-property :title headline))))
4057 (defun org-export-first-sibling-p (blob info)
4058 "Non-nil when BLOB is the first sibling in its parent.
4059 BLOB is an element or an object. If BLOB is a headline, non-nil
4060 means it is the first sibling in the sub-tree. INFO is a plist
4061 used as a communication channel."
4062 (memq (org-element-type (org-export-get-previous-element blob info))
4063 '(nil section)))
4065 (defun org-export-last-sibling-p (blob info)
4066 "Non-nil when BLOB is the last sibling in its parent.
4067 BLOB is an element or an object. INFO is a plist used as
4068 a communication channel."
4069 (not (org-export-get-next-element blob info)))
4072 ;;;; For Keywords
4074 ;; `org-export-get-date' returns a date appropriate for the document
4075 ;; to about to be exported. In particular, it takes care of
4076 ;; `org-export-date-timestamp-format'.
4078 (defun org-export-get-date (info &optional fmt)
4079 "Return date value for the current document.
4081 INFO is a plist used as a communication channel. FMT, when
4082 non-nil, is a time format string that will be applied on the date
4083 if it consists in a single timestamp object. It defaults to
4084 `org-export-date-timestamp-format' when nil.
4086 A proper date can be a secondary string, a string or nil. It is
4087 meant to be translated with `org-export-data' or alike."
4088 (let ((date (plist-get info :date))
4089 (fmt (or fmt org-export-date-timestamp-format)))
4090 (cond ((not date) nil)
4091 ((and fmt
4092 (not (cdr date))
4093 (eq (org-element-type (car date)) 'timestamp))
4094 (org-timestamp-format (car date) fmt))
4095 (t date))))
4098 ;;;; For Links
4100 ;; `org-export-custom-protocol-maybe' handles custom protocol defined
4101 ;; in `org-link-parameters'.
4103 ;; `org-export-get-coderef-format' returns an appropriate format
4104 ;; string for coderefs.
4106 ;; `org-export-inline-image-p' returns a non-nil value when the link
4107 ;; provided should be considered as an inline image.
4109 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
4110 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
4111 ;; returns an appropriate unique identifier.
4113 ;; `org-export-resolve-id-link' returns the first headline with
4114 ;; specified id or custom-id in parse tree, the path to the external
4115 ;; file with the id.
4117 ;; `org-export-resolve-coderef' associates a reference to a line
4118 ;; number in the element it belongs, or returns the reference itself
4119 ;; when the element isn't numbered.
4121 ;; `org-export-file-uri' expands a filename as stored in :path value
4122 ;; of a "file" link into a file URI.
4124 ;; Broken links raise a `org-link-broken' error, which is caught by
4125 ;; `org-export-data' for further processing, depending on
4126 ;; `org-export-with-broken-links' value.
4128 (org-define-error 'org-link-broken "Unable to resolve link; aborting")
4130 (defun org-export-custom-protocol-maybe (link desc backend)
4131 "Try exporting LINK with a dedicated function.
4133 DESC is its description, as a string, or nil. BACKEND is the
4134 back-end used for export, as a symbol.
4136 Return output as a string, or nil if no protocol handles LINK.
4138 A custom protocol has precedence over regular back-end export.
4139 The function ignores links with an implicit type (e.g.,
4140 \"custom-id\")."
4141 (let ((type (org-element-property :type link)))
4142 (unless (or (member type '("coderef" "custom-id" "fuzzy" "radio"))
4143 (not backend))
4144 (let ((protocol (org-link-get-parameter type :export)))
4145 (and (functionp protocol)
4146 (funcall protocol
4147 (org-link-unescape (org-element-property :path link))
4148 desc
4149 backend))))))
4151 (defun org-export-get-coderef-format (path desc)
4152 "Return format string for code reference link.
4153 PATH is the link path. DESC is its description."
4154 (save-match-data
4155 (cond ((not desc) "%s")
4156 ((string-match (regexp-quote (concat "(" path ")")) desc)
4157 (replace-match "%s" t t desc))
4158 (t desc))))
4160 (defun org-export-inline-image-p (link &optional rules)
4161 "Non-nil if LINK object points to an inline image.
4163 Optional argument is a set of RULES defining inline images. It
4164 is an alist where associations have the following shape:
4166 (TYPE . REGEXP)
4168 Applying a rule means apply REGEXP against LINK's path when its
4169 type is TYPE. The function will return a non-nil value if any of
4170 the provided rules is non-nil. The default rule is
4171 `org-export-default-inline-image-rule'.
4173 This only applies to links without a description."
4174 (and (not (org-element-contents link))
4175 (let ((case-fold-search t))
4176 (cl-some (lambda (rule)
4177 (and (string= (org-element-property :type link) (car rule))
4178 (string-match-p (cdr rule)
4179 (org-element-property :path link))))
4180 (or rules org-export-default-inline-image-rule)))))
4182 (defun org-export-insert-image-links (data info &optional rules)
4183 "Insert image links in DATA.
4185 Org syntax does not support nested links. Nevertheless, some
4186 export back-ends support images as descriptions of links. Since
4187 images are really links to image files, we need to make an
4188 exception about links nesting.
4190 This function recognizes links whose contents are really images
4191 and turn them into proper nested links. It is meant to be used
4192 as a parse tree filter in back-ends supporting such constructs.
4194 DATA is a parse tree. INFO is the current state of the export
4195 process, as a plist.
4197 A description is a valid images if it matches any rule in RULES,
4198 if non-nil, or `org-export-default-inline-image-rule' otherwise.
4199 See `org-export-inline-image-p' for more information about the
4200 structure of RULES.
4202 Return modified DATA."
4203 (let ((link-re (format "\\`\\(?:%s\\|%s\\)\\'"
4204 org-plain-link-re
4205 org-angle-link-re))
4206 (case-fold-search t))
4207 (org-element-map data 'link
4208 (lambda (l)
4209 (let ((contents (org-element-interpret-data (org-element-contents l))))
4210 (when (and (org-string-nw-p contents)
4211 (string-match link-re contents))
4212 (let ((type (match-string 1 contents))
4213 (path (match-string 2 contents)))
4214 (when (cl-some (lambda (rule)
4215 (and (string= type (car rule))
4216 (string-match-p (cdr rule) path)))
4217 (or rules org-export-default-inline-image-rule))
4218 ;; Replace contents with image link.
4219 (org-element-adopt-elements
4220 (org-element-set-contents l nil)
4221 (with-temp-buffer
4222 (save-excursion (insert contents))
4223 (org-element-link-parser))))))))
4224 info nil nil t))
4225 data)
4227 (defun org-export-resolve-coderef (ref info)
4228 "Resolve a code reference REF.
4230 INFO is a plist used as a communication channel.
4232 Return associated line number in source code, or REF itself,
4233 depending on src-block or example element's switches. Throw an
4234 error if no block contains REF."
4235 (or (org-element-map (plist-get info :parse-tree) '(example-block src-block)
4236 (lambda (el)
4237 (with-temp-buffer
4238 (insert (org-trim (org-element-property :value el)))
4239 (let* ((label-fmt (or (org-element-property :label-fmt el)
4240 org-coderef-label-format))
4241 (ref-re (org-src-coderef-regexp label-fmt ref)))
4242 ;; Element containing REF is found. Resolve it to
4243 ;; either a label or a line number, as needed.
4244 (when (re-search-backward ref-re nil t)
4245 (if (org-element-property :use-labels el) ref
4246 (+ (or (org-export-get-loc el info) 0)
4247 (line-number-at-pos)))))))
4248 info 'first-match)
4249 (signal 'org-link-broken (list ref))))
4251 (defun org-export-search-cells (datum)
4252 "List search cells for element or object DATUM.
4254 A search cell follows the pattern (TYPE . SEARCH) where
4256 TYPE is a symbol among `headline', `custom-id', `target' and
4257 `other'.
4259 SEARCH is the string a link is expected to match. More
4260 accurately, it is
4262 - headline's title, as a list of strings, if TYPE is
4263 `headline'.
4265 - CUSTOM_ID value, as a string, if TYPE is `custom-id'.
4267 - target's or radio-target's name as a list of strings if
4268 TYPE is `target'.
4270 - NAME affiliated keyword is TYPE is `other'.
4272 A search cell is the internal representation of a fuzzy link. It
4273 ignores white spaces and statistics cookies, if applicable."
4274 (pcase (org-element-type datum)
4275 (`headline
4276 (let ((title (split-string
4277 (replace-regexp-in-string
4278 "\\[[0-9]*\\(?:%\\|/[0-9]*\\)\\]" ""
4279 (org-element-property :raw-value datum)))))
4280 (delq nil
4281 (list
4282 (cons 'headline title)
4283 (cons 'other title)
4284 (let ((custom-id (org-element-property :custom-id datum)))
4285 (and custom-id (cons 'custom-id custom-id)))))))
4286 (`target
4287 (list (cons 'target (split-string (org-element-property :value datum)))))
4288 ((and (let name (org-element-property :name datum))
4289 (guard name))
4290 (list (cons 'other (split-string name))))
4291 (_ nil)))
4293 (defun org-export-string-to-search-cell (s)
4294 "Return search cells associated to string S.
4295 S is either the path of a fuzzy link or a search option, i.e., it
4296 tries to match either a headline (through custom ID or title),
4297 a target or a named element."
4298 (pcase (string-to-char s)
4299 (?* (list (cons 'headline (split-string (substring s 1)))))
4300 (?# (list (cons 'custom-id (substring s 1))))
4301 ((let search (split-string s))
4302 (list (cons 'target search) (cons 'other search)))))
4304 (defun org-export-match-search-cell-p (datum cells)
4305 "Non-nil when DATUM matches search cells CELLS.
4306 DATUM is an element or object. CELLS is a list of search cells,
4307 as returned by `org-export-search-cells'."
4308 (let ((targets (org-export-search-cells datum)))
4309 (and targets (cl-some (lambda (cell) (member cell targets)) cells))))
4311 (defun org-export-resolve-fuzzy-link (link info)
4312 "Return LINK destination.
4314 INFO is a plist holding contextual information.
4316 Return value can be an object or an element:
4318 - If LINK path matches a target object (i.e. <<path>>) return it.
4320 - If LINK path exactly matches the name affiliated keyword
4321 (i.e. #+NAME: path) of an element, return that element.
4323 - If LINK path exactly matches any headline name, return that
4324 element.
4326 - Otherwise, throw an error.
4328 Assume LINK type is \"fuzzy\". White spaces are not
4329 significant."
4330 (let* ((search-cells (org-export-string-to-search-cell
4331 (org-link-unescape (org-element-property :path link))))
4332 (link-cache (or (plist-get info :resolve-fuzzy-link-cache)
4333 (let ((table (make-hash-table :test #'eq)))
4334 (plist-put info :resolve-fuzzy-link-cache table)
4335 table)))
4336 (cached (gethash search-cells link-cache 'not-found)))
4337 (if (not (eq cached 'not-found)) cached
4338 (let ((matches
4339 (org-element-map (plist-get info :parse-tree)
4340 (cons 'target org-element-all-elements)
4341 (lambda (datum)
4342 (and (org-export-match-search-cell-p datum search-cells)
4343 datum)))))
4344 (unless matches
4345 (signal 'org-link-broken (list (org-element-property :path link))))
4346 (puthash
4347 search-cells
4348 ;; There can be multiple matches for un-typed searches, i.e.,
4349 ;; for searches not starting with # or *. In this case,
4350 ;; prioritize targets and names over headline titles.
4351 ;; Matching both a name and a target is not valid, and
4352 ;; therefore undefined.
4353 (or (cl-some (lambda (datum)
4354 (and (not (eq (org-element-type datum) 'headline))
4355 datum))
4356 matches)
4357 (car matches))
4358 link-cache)))))
4360 (defun org-export-resolve-id-link (link info)
4361 "Return headline referenced as LINK destination.
4363 INFO is a plist used as a communication channel.
4365 Return value can be the headline element matched in current parse
4366 tree or a file name. Assume LINK type is either \"id\" or
4367 \"custom-id\". Throw an error if no match is found."
4368 (let ((id (org-element-property :path link)))
4369 ;; First check if id is within the current parse tree.
4370 (or (org-element-map (plist-get info :parse-tree) 'headline
4371 (lambda (headline)
4372 (when (or (equal (org-element-property :ID headline) id)
4373 (equal (org-element-property :CUSTOM_ID headline) id))
4374 headline))
4375 info 'first-match)
4376 ;; Otherwise, look for external files.
4377 (cdr (assoc id (plist-get info :id-alist)))
4378 (signal 'org-link-broken (list id)))))
4380 (defun org-export-resolve-radio-link (link info)
4381 "Return radio-target object referenced as LINK destination.
4383 INFO is a plist used as a communication channel.
4385 Return value can be a radio-target object or nil. Assume LINK
4386 has type \"radio\"."
4387 (let ((path (replace-regexp-in-string
4388 "[ \r\t\n]+" " " (org-element-property :path link))))
4389 (org-element-map (plist-get info :parse-tree) 'radio-target
4390 (lambda (radio)
4391 (and (eq (compare-strings
4392 (replace-regexp-in-string
4393 "[ \r\t\n]+" " " (org-element-property :value radio))
4394 nil nil path nil nil t)
4396 radio))
4397 info 'first-match)))
4399 (defun org-export-file-uri (filename)
4400 "Return file URI associated to FILENAME."
4401 (cond ((string-prefix-p "//" filename) (concat "file:" filename))
4402 ((not (file-name-absolute-p filename)) filename)
4403 ((org-file-remote-p filename) (concat "file:/" filename))
4405 (let ((fullname (expand-file-name filename)))
4406 (concat (if (string-prefix-p "/" fullname) "file://" "file:///")
4407 fullname)))))
4409 ;;;; For References
4411 ;; `org-export-get-reference' associate a unique reference for any
4412 ;; object or element. It uses `org-export-new-reference' and
4413 ;; `org-export-format-reference' to, respectively, generate new
4414 ;; internal references and turn them into a string suitable for
4415 ;; output.
4417 ;; `org-export-get-ordinal' associates a sequence number to any object
4418 ;; or element.
4420 (defun org-export-new-reference (references)
4421 "Return a unique reference, among REFERENCES.
4422 REFERENCES is an alist whose values are in-use references, as
4423 numbers. Returns a number, which is the internal representation
4424 of a reference. See also `org-export-format-reference'."
4425 ;; Generate random 7 digits hexadecimal numbers. Collisions
4426 ;; increase exponentially with the numbers of references. However,
4427 ;; the odds for encountering at least one collision with 1000 active
4428 ;; references in the same document are roughly 0.2%, so this
4429 ;; shouldn't be the bottleneck.
4430 (let ((new (random #x10000000)))
4431 (while (rassq new references) (setq new (random #x10000000)))
4432 new))
4434 (defun org-export-format-reference (reference)
4435 "Format REFERENCE into a string.
4436 REFERENCE is a number representing a reference, as returned by
4437 `org-export-new-reference', which see."
4438 (format "org%07x" reference))
4440 (defun org-export-get-reference (datum info)
4441 "Return a unique reference for DATUM, as a string.
4443 DATUM is either an element or an object. INFO is the current
4444 export state, as a plist.
4446 This function checks `:crossrefs' property in INFO for search
4447 cells matching DATUM before creating a new reference. Returned
4448 reference consists of alphanumeric characters only."
4449 (let ((cache (plist-get info :internal-references)))
4450 (or (car (rassq datum cache))
4451 (let* ((crossrefs (plist-get info :crossrefs))
4452 (cells (org-export-search-cells datum))
4453 ;; Preserve any pre-existing association between
4454 ;; a search cell and a reference, i.e., when some
4455 ;; previously published document referenced a location
4456 ;; within current file (see
4457 ;; `org-publish-resolve-external-link').
4459 ;; However, there is no guarantee that search cells are
4460 ;; unique, e.g., there might be duplicate custom ID or
4461 ;; two headings with the same title in the file.
4463 ;; As a consequence, before re-using any reference to
4464 ;; an element or object, we check that it doesn't refer
4465 ;; to a previous element or object.
4466 (new (or (cl-some
4467 (lambda (cell)
4468 (let ((stored (cdr (assoc cell crossrefs))))
4469 (when stored
4470 (let ((old (org-export-format-reference stored)))
4471 (and (not (assoc old cache)) stored)))))
4472 cells)
4473 (org-export-new-reference cache)))
4474 (reference-string (org-export-format-reference new)))
4475 ;; Cache contains both data already associated to
4476 ;; a reference and in-use internal references, so as to make
4477 ;; unique references.
4478 (dolist (cell cells) (push (cons cell new) cache))
4479 ;; Retain a direct association between reference string and
4480 ;; DATUM since (1) not every object or element can be given
4481 ;; a search cell (2) it permits quick lookup.
4482 (push (cons reference-string datum) cache)
4483 (plist-put info :internal-references cache)
4484 reference-string))))
4486 (defun org-export-get-ordinal (element info &optional types predicate)
4487 "Return ordinal number of an element or object.
4489 ELEMENT is the element or object considered. INFO is the plist
4490 used as a communication channel.
4492 Optional argument TYPES, when non-nil, is a list of element or
4493 object types, as symbols, that should also be counted in.
4494 Otherwise, only provided element's type is considered.
4496 Optional argument PREDICATE is a function returning a non-nil
4497 value if the current element or object should be counted in. It
4498 accepts two arguments: the element or object being considered and
4499 the plist used as a communication channel. This allows counting
4500 only a certain type of object (i.e. inline images).
4502 Return value is a list of numbers if ELEMENT is a headline or an
4503 item. It is nil for keywords. It represents the footnote number
4504 for footnote definitions and footnote references. If ELEMENT is
4505 a target, return the same value as if ELEMENT was the closest
4506 table, item or headline containing the target. In any other
4507 case, return the sequence number of ELEMENT among elements or
4508 objects of the same type."
4509 ;; Ordinal of a target object refer to the ordinal of the closest
4510 ;; table, item, or headline containing the object.
4511 (when (eq (org-element-type element) 'target)
4512 (setq element
4513 (org-element-lineage
4514 element
4515 '(footnote-definition footnote-reference headline item table))))
4516 (cl-case (org-element-type element)
4517 ;; Special case 1: A headline returns its number as a list.
4518 (headline (org-export-get-headline-number element info))
4519 ;; Special case 2: An item returns its number as a list.
4520 (item (let ((struct (org-element-property :structure element)))
4521 (org-list-get-item-number
4522 (org-element-property :begin element)
4523 struct
4524 (org-list-prevs-alist struct)
4525 (org-list-parents-alist struct))))
4526 ((footnote-definition footnote-reference)
4527 (org-export-get-footnote-number element info))
4528 (otherwise
4529 (let ((counter 0))
4530 ;; Increment counter until ELEMENT is found again.
4531 (org-element-map (plist-get info :parse-tree)
4532 (or types (org-element-type element))
4533 (lambda (el)
4534 (cond
4535 ((eq element el) (1+ counter))
4536 ((not predicate) (cl-incf counter) nil)
4537 ((funcall predicate el info) (cl-incf counter) nil)))
4538 info 'first-match)))))
4541 ;;;; For Src-Blocks
4543 ;; `org-export-get-loc' counts number of code lines accumulated in
4544 ;; src-block or example-block elements with a "+n" switch until
4545 ;; a given element, excluded. Note: "-n" switches reset that count.
4547 ;; `org-export-unravel-code' extracts source code (along with a code
4548 ;; references alist) from an `element-block' or `src-block' type
4549 ;; element.
4551 ;; `org-export-format-code' applies a formatting function to each line
4552 ;; of code, providing relative line number and code reference when
4553 ;; appropriate. Since it doesn't access the original element from
4554 ;; which the source code is coming, it expects from the code calling
4555 ;; it to know if lines should be numbered and if code references
4556 ;; should appear.
4558 ;; Eventually, `org-export-format-code-default' is a higher-level
4559 ;; function (it makes use of the two previous functions) which handles
4560 ;; line numbering and code references inclusion, and returns source
4561 ;; code in a format suitable for plain text or verbatim output.
4563 (defun org-export-get-loc (element info)
4564 "Return count of lines of code before ELEMENT.
4566 ELEMENT is an example-block or src-block element. INFO is the
4567 plist used as a communication channel.
4569 Count includes every line of code in example-block or src-block
4570 with a \"+n\" or \"-n\" switch before block. Return nil if
4571 ELEMENT doesn't allow line numbering."
4572 (pcase (org-element-property :number-lines element)
4573 (`(new . ,n) n)
4574 (`(continued . ,n)
4575 (let ((loc 0))
4576 (org-element-map (plist-get info :parse-tree) '(src-block example-block)
4577 (lambda (el)
4578 ;; ELEMENT is reached: Quit loop and return locs.
4579 (if (eq el element) (+ loc n)
4580 ;; Only count lines from src-block and example-block
4581 ;; elements with a "+n" or "-n" switch.
4582 (let ((linum (org-element-property :number-lines el)))
4583 (when linum
4584 (let ((lines (org-count-lines
4585 (org-element-property :value el))))
4586 ;; Accumulate locs or reset them.
4587 (pcase linum
4588 (`(new . ,n) (setq loc (+ n lines)))
4589 (`(continued . ,n) (cl-incf loc (+ n lines)))))))
4590 nil)) ;Return nil to stay in the loop.
4591 info 'first-match)))))
4593 (defun org-export-unravel-code (element)
4594 "Clean source code and extract references out of it.
4596 ELEMENT has either a `src-block' an `example-block' type.
4598 Return a cons cell whose CAR is the source code, cleaned from any
4599 reference, protective commas and spurious indentation, and CDR is
4600 an alist between relative line number (integer) and name of code
4601 reference on that line (string)."
4602 (let* ((line 0) refs
4603 (value (org-element-property :value element))
4604 ;; Remove global indentation from code, if necessary. Also
4605 ;; remove final newline character, since it doesn't belongs
4606 ;; to the code proper.
4607 (code (replace-regexp-in-string
4608 "\n\\'" ""
4609 (if (or org-src-preserve-indentation
4610 (org-element-property :preserve-indent element))
4611 value
4612 (org-remove-indentation value))))
4613 ;; Build a regexp matching a loc with a reference.
4614 (ref-re (org-src-coderef-regexp (org-src-coderef-format element))))
4615 ;; Return value.
4616 (cons
4617 ;; Code with references removed.
4618 (mapconcat
4619 (lambda (loc)
4620 (cl-incf line)
4621 (if (not (string-match ref-re loc)) loc
4622 ;; Ref line: remove ref, and add its position in REFS.
4623 (push (cons line (match-string 3 loc)) refs)
4624 (replace-match "" nil nil loc 1)))
4625 (split-string code "\n") "\n")
4626 ;; Reference alist.
4627 refs)))
4629 (defun org-export-format-code (code fun &optional num-lines ref-alist)
4630 "Format CODE by applying FUN line-wise and return it.
4632 CODE is a string representing the code to format. FUN is
4633 a function. It must accept three arguments: a line of
4634 code (string), the current line number (integer) or nil and the
4635 reference associated to the current line (string) or nil.
4637 Optional argument NUM-LINES can be an integer representing the
4638 number of code lines accumulated until the current code. Line
4639 numbers passed to FUN will take it into account. If it is nil,
4640 FUN's second argument will always be nil. This number can be
4641 obtained with `org-export-get-loc' function.
4643 Optional argument REF-ALIST can be an alist between relative line
4644 number (i.e. ignoring NUM-LINES) and the name of the code
4645 reference on it. If it is nil, FUN's third argument will always
4646 be nil. It can be obtained through the use of
4647 `org-export-unravel-code' function."
4648 (let ((--locs (split-string code "\n"))
4649 (--line 0))
4650 (concat
4651 (mapconcat
4652 (lambda (--loc)
4653 (cl-incf --line)
4654 (let ((--ref (cdr (assq --line ref-alist))))
4655 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
4656 --locs "\n")
4657 "\n")))
4659 (defun org-export-format-code-default (element info)
4660 "Return source code from ELEMENT, formatted in a standard way.
4662 ELEMENT is either a `src-block' or `example-block' element. INFO
4663 is a plist used as a communication channel.
4665 This function takes care of line numbering and code references
4666 inclusion. Line numbers, when applicable, appear at the
4667 beginning of the line, separated from the code by two white
4668 spaces. Code references, on the other hand, appear flushed to
4669 the right, separated by six white spaces from the widest line of
4670 code."
4671 ;; Extract code and references.
4672 (let* ((code-info (org-export-unravel-code element))
4673 (code (car code-info))
4674 (code-lines (split-string code "\n")))
4675 (if (null code-lines) ""
4676 (let* ((refs (and (org-element-property :retain-labels element)
4677 (cdr code-info)))
4678 ;; Handle line numbering.
4679 (num-start (org-export-get-loc element info))
4680 (num-fmt
4681 (and num-start
4682 (format "%%%ds "
4683 (length (number-to-string
4684 (+ (length code-lines) num-start))))))
4685 ;; Prepare references display, if required. Any reference
4686 ;; should start six columns after the widest line of code,
4687 ;; wrapped with parenthesis.
4688 (max-width
4689 (+ (apply 'max (mapcar 'length code-lines))
4690 (if (not num-start) 0 (length (format num-fmt num-start))))))
4691 (org-export-format-code
4692 code
4693 (lambda (loc line-num ref)
4694 (let ((number-str (and num-fmt (format num-fmt line-num))))
4695 (concat
4696 number-str
4698 (and ref
4699 (concat (make-string (- (+ 6 max-width)
4700 (+ (length loc) (length number-str)))
4701 ?\s)
4702 (format "(%s)" ref))))))
4703 num-start refs)))))
4706 ;;;; For Tables
4708 ;; `org-export-table-has-special-column-p' and and
4709 ;; `org-export-table-row-is-special-p' are predicates used to look for
4710 ;; meta-information about the table structure.
4712 ;; `org-table-has-header-p' tells when the rows before the first rule
4713 ;; should be considered as table's header.
4715 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4716 ;; and `org-export-table-cell-borders' extract information from
4717 ;; a table-cell element.
4719 ;; `org-export-table-dimensions' gives the number on rows and columns
4720 ;; in the table, ignoring horizontal rules and special columns.
4721 ;; `org-export-table-cell-address', given a table-cell object, returns
4722 ;; the absolute address of a cell. On the other hand,
4723 ;; `org-export-get-table-cell-at' does the contrary.
4725 ;; `org-export-table-cell-starts-colgroup-p',
4726 ;; `org-export-table-cell-ends-colgroup-p',
4727 ;; `org-export-table-row-starts-rowgroup-p',
4728 ;; `org-export-table-row-ends-rowgroup-p',
4729 ;; `org-export-table-row-starts-header-p',
4730 ;; `org-export-table-row-ends-header-p' and
4731 ;; `org-export-table-row-in-header-p' indicate position of current row
4732 ;; or cell within the table.
4734 (defun org-export-table-has-special-column-p (table)
4735 "Non-nil when TABLE has a special column.
4736 All special columns will be ignored during export."
4737 ;; The table has a special column when every first cell of every row
4738 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4739 ;; "*" "_" and "^". Though, do not consider a first column
4740 ;; containing only empty cells as special.
4741 (let ((special-column? 'empty))
4742 (catch 'exit
4743 (dolist (row (org-element-contents table))
4744 (when (eq (org-element-property :type row) 'standard)
4745 (let ((value (org-element-contents
4746 (car (org-element-contents row)))))
4747 (cond ((member value
4748 '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4749 (setq special-column? 'special))
4750 ((null value))
4751 (t (throw 'exit nil))))))
4752 (eq special-column? 'special))))
4754 (defun org-export-table-has-header-p (table info)
4755 "Non-nil when TABLE has a header.
4757 INFO is a plist used as a communication channel.
4759 A table has a header when it contains at least two row groups."
4760 (let* ((cache (or (plist-get info :table-header-cache)
4761 (let ((table (make-hash-table :test #'eq)))
4762 (plist-put info :table-header-cache table)
4763 table)))
4764 (cached (gethash table cache 'no-cache)))
4765 (if (not (eq cached 'no-cache)) cached
4766 (let ((rowgroup 1) row-flag)
4767 (puthash table
4768 (org-element-map table 'table-row
4769 (lambda (row)
4770 (cond
4771 ((> rowgroup 1) t)
4772 ((and row-flag
4773 (eq (org-element-property :type row) 'rule))
4774 (cl-incf rowgroup)
4775 (setq row-flag nil))
4776 ((and (not row-flag)
4777 (eq (org-element-property :type row) 'standard))
4778 (setq row-flag t)
4779 nil)))
4780 info 'first-match)
4781 cache)))))
4783 (defun org-export-table-row-is-special-p (table-row _)
4784 "Non-nil if TABLE-ROW is considered special.
4785 All special rows will be ignored during export."
4786 (when (eq (org-element-property :type table-row) 'standard)
4787 (let ((first-cell (org-element-contents
4788 (car (org-element-contents table-row)))))
4789 ;; A row is special either when...
4791 ;; ... it starts with a field only containing "/",
4792 (equal first-cell '("/"))
4793 ;; ... the table contains a special column and the row start
4794 ;; with a marking character among, "^", "_", "$" or "!",
4795 (and (org-export-table-has-special-column-p
4796 (org-export-get-parent table-row))
4797 (member first-cell '(("^") ("_") ("$") ("!"))))
4798 ;; ... it contains only alignment cookies and empty cells.
4799 (let ((special-row-p 'empty))
4800 (catch 'exit
4801 (dolist (cell (org-element-contents table-row))
4802 (let ((value (org-element-contents cell)))
4803 ;; Since VALUE is a secondary string, the following
4804 ;; checks avoid expanding it with `org-export-data'.
4805 (cond ((not value))
4806 ((and (not (cdr value))
4807 (stringp (car value))
4808 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4809 (car value)))
4810 (setq special-row-p 'cookie))
4811 (t (throw 'exit nil)))))
4812 (eq special-row-p 'cookie)))))))
4814 (defun org-export-table-row-group (table-row info)
4815 "Return TABLE-ROW's group number, as an integer.
4817 INFO is a plist used as the communication channel.
4819 Return value is the group number, as an integer, or nil for
4820 special rows and rows separators. First group is also table's
4821 header."
4822 (when (eq (org-element-property :type table-row) 'standard)
4823 (let* ((cache (or (plist-get info :table-row-group-cache)
4824 (let ((table (make-hash-table :test #'eq)))
4825 (plist-put info :table-row-group-cache table)
4826 table)))
4827 (cached (gethash table-row cache 'no-cache)))
4828 (if (not (eq cached 'no-cache)) cached
4829 ;; First time a row is queried, populate cache with all the
4830 ;; rows from the table.
4831 (let ((group 0) row-flag)
4832 (org-element-map (org-export-get-parent table-row) 'table-row
4833 (lambda (row)
4834 (if (eq (org-element-property :type row) 'rule)
4835 (setq row-flag nil)
4836 (unless row-flag (cl-incf group) (setq row-flag t))
4837 (puthash row group cache)))
4838 info))
4839 (gethash table-row cache)))))
4841 (defun org-export-table-cell-width (table-cell info)
4842 "Return TABLE-CELL contents width.
4844 INFO is a plist used as the communication channel.
4846 Return value is the width given by the last width cookie in the
4847 same column as TABLE-CELL, or nil."
4848 (let* ((row (org-export-get-parent table-cell))
4849 (table (org-export-get-parent row))
4850 (cells (org-element-contents row))
4851 (columns (length cells))
4852 (column (- columns (length (memq table-cell cells))))
4853 (cache (or (plist-get info :table-cell-width-cache)
4854 (let ((table (make-hash-table :test #'eq)))
4855 (plist-put info :table-cell-width-cache table)
4856 table)))
4857 (width-vector (or (gethash table cache)
4858 (puthash table (make-vector columns 'empty) cache)))
4859 (value (aref width-vector column)))
4860 (if (not (eq value 'empty)) value
4861 (let (cookie-width)
4862 (dolist (row (org-element-contents table)
4863 (aset width-vector column cookie-width))
4864 (when (org-export-table-row-is-special-p row info)
4865 ;; In a special row, try to find a width cookie at COLUMN.
4866 (let* ((value (org-element-contents
4867 (elt (org-element-contents row) column)))
4868 (cookie (car value)))
4869 ;; The following checks avoid expanding unnecessarily
4870 ;; the cell with `org-export-data'.
4871 (when (and value
4872 (not (cdr value))
4873 (stringp cookie)
4874 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
4875 (match-string 1 cookie))
4876 (setq cookie-width
4877 (string-to-number (match-string 1 cookie)))))))))))
4879 (defun org-export-table-cell-alignment (table-cell info)
4880 "Return TABLE-CELL contents alignment.
4882 INFO is a plist used as the communication channel.
4884 Return alignment as specified by the last alignment cookie in the
4885 same column as TABLE-CELL. If no such cookie is found, a default
4886 alignment value will be deduced from fraction of numbers in the
4887 column (see `org-table-number-fraction' for more information).
4888 Possible values are `left', `right' and `center'."
4889 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4890 (require 'org-table)
4891 (let* ((row (org-export-get-parent table-cell))
4892 (table (org-export-get-parent row))
4893 (cells (org-element-contents row))
4894 (columns (length cells))
4895 (column (- columns (length (memq table-cell cells))))
4896 (cache (or (plist-get info :table-cell-alignment-cache)
4897 (let ((table (make-hash-table :test #'eq)))
4898 (plist-put info :table-cell-alignment-cache table)
4899 table)))
4900 (align-vector (or (gethash table cache)
4901 (puthash table (make-vector columns nil) cache))))
4902 (or (aref align-vector column)
4903 (let ((number-cells 0)
4904 (total-cells 0)
4905 cookie-align
4906 previous-cell-number-p)
4907 (dolist (row (org-element-contents (org-export-get-parent row)))
4908 (cond
4909 ;; In a special row, try to find an alignment cookie at
4910 ;; COLUMN.
4911 ((org-export-table-row-is-special-p row info)
4912 (let ((value (org-element-contents
4913 (elt (org-element-contents row) column))))
4914 ;; Since VALUE is a secondary string, the following
4915 ;; checks avoid useless expansion through
4916 ;; `org-export-data'.
4917 (when (and value
4918 (not (cdr value))
4919 (stringp (car value))
4920 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4921 (car value))
4922 (match-string 1 (car value)))
4923 (setq cookie-align (match-string 1 (car value))))))
4924 ;; Ignore table rules.
4925 ((eq (org-element-property :type row) 'rule))
4926 ;; In a standard row, check if cell's contents are
4927 ;; expressing some kind of number. Increase NUMBER-CELLS
4928 ;; accordingly. Though, don't bother if an alignment
4929 ;; cookie has already defined cell's alignment.
4930 ((not cookie-align)
4931 (let ((value (org-export-data
4932 (org-element-contents
4933 (elt (org-element-contents row) column))
4934 info)))
4935 (cl-incf total-cells)
4936 ;; Treat an empty cell as a number if it follows
4937 ;; a number.
4938 (if (not (or (string-match org-table-number-regexp value)
4939 (and (string= value "") previous-cell-number-p)))
4940 (setq previous-cell-number-p nil)
4941 (setq previous-cell-number-p t)
4942 (cl-incf number-cells))))))
4943 ;; Return value. Alignment specified by cookies has
4944 ;; precedence over alignment deduced from cell's contents.
4945 (aset align-vector
4946 column
4947 (cond ((equal cookie-align "l") 'left)
4948 ((equal cookie-align "r") 'right)
4949 ((equal cookie-align "c") 'center)
4950 ((>= (/ (float number-cells) total-cells)
4951 org-table-number-fraction)
4952 'right)
4953 (t 'left)))))))
4955 (defun org-export-table-cell-borders (table-cell info)
4956 "Return TABLE-CELL borders.
4958 INFO is a plist used as a communication channel.
4960 Return value is a list of symbols, or nil. Possible values are:
4961 `top', `bottom', `above', `below', `left' and `right'. Note:
4962 `top' (resp. `bottom') only happen for a cell in the first
4963 row (resp. last row) of the table, ignoring table rules, if any.
4965 Returned borders ignore special rows."
4966 (let* ((row (org-export-get-parent table-cell))
4967 (table (org-export-get-parent-table table-cell))
4968 borders)
4969 ;; Top/above border? TABLE-CELL has a border above when a rule
4970 ;; used to demarcate row groups can be found above. Hence,
4971 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4972 ;; another regular row has to be found above that rule.
4973 (let (rule-flag)
4974 (catch 'exit
4975 ;; Look at every row before the current one.
4976 (dolist (row (cdr (memq row (reverse (org-element-contents table)))))
4977 (cond ((eq (org-element-property :type row) 'rule)
4978 (setq rule-flag t))
4979 ((not (org-export-table-row-is-special-p row info))
4980 (if rule-flag (throw 'exit (push 'above borders))
4981 (throw 'exit nil)))))
4982 ;; No rule above, or rule found starts the table (ignoring any
4983 ;; special row): TABLE-CELL is at the top of the table.
4984 (when rule-flag (push 'above borders))
4985 (push 'top borders)))
4986 ;; Bottom/below border? TABLE-CELL has a border below when next
4987 ;; non-regular row below is a rule.
4988 (let (rule-flag)
4989 (catch 'exit
4990 ;; Look at every row after the current one.
4991 (dolist (row (cdr (memq row (org-element-contents table))))
4992 (cond ((eq (org-element-property :type row) 'rule)
4993 (setq rule-flag t))
4994 ((not (org-export-table-row-is-special-p row info))
4995 (if rule-flag (throw 'exit (push 'below borders))
4996 (throw 'exit nil)))))
4997 ;; No rule below, or rule found ends the table (modulo some
4998 ;; special row): TABLE-CELL is at the bottom of the table.
4999 (when rule-flag (push 'below borders))
5000 (push 'bottom borders)))
5001 ;; Right/left borders? They can only be specified by column
5002 ;; groups. Column groups are defined in a row starting with "/".
5003 ;; Also a column groups row only contains "<", "<>", ">" or blank
5004 ;; cells.
5005 (catch 'exit
5006 (let ((column (let ((cells (org-element-contents row)))
5007 (- (length cells) (length (memq table-cell cells))))))
5008 ;; Table rows are read in reverse order so last column groups
5009 ;; row has precedence over any previous one.
5010 (dolist (row (reverse (org-element-contents table)))
5011 (unless (eq (org-element-property :type row) 'rule)
5012 (when (equal (org-element-contents
5013 (car (org-element-contents row)))
5014 '("/"))
5015 (let ((column-groups
5016 (mapcar
5017 (lambda (cell)
5018 (let ((value (org-element-contents cell)))
5019 (when (member value '(("<") ("<>") (">") nil))
5020 (car value))))
5021 (org-element-contents row))))
5022 ;; There's a left border when previous cell, if
5023 ;; any, ends a group, or current one starts one.
5024 (when (or (and (not (zerop column))
5025 (member (elt column-groups (1- column))
5026 '(">" "<>")))
5027 (member (elt column-groups column) '("<" "<>")))
5028 (push 'left borders))
5029 ;; There's a right border when next cell, if any,
5030 ;; starts a group, or current one ends one.
5031 (when (or (and (/= (1+ column) (length column-groups))
5032 (member (elt column-groups (1+ column))
5033 '("<" "<>")))
5034 (member (elt column-groups column) '(">" "<>")))
5035 (push 'right borders))
5036 (throw 'exit nil)))))))
5037 ;; Return value.
5038 borders))
5040 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
5041 "Non-nil when TABLE-CELL is at the beginning of a column group.
5042 INFO is a plist used as a communication channel."
5043 ;; A cell starts a column group either when it is at the beginning
5044 ;; of a row (or after the special column, if any) or when it has
5045 ;; a left border.
5046 (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
5047 'identity info 'first-match)
5048 table-cell)
5049 (memq 'left (org-export-table-cell-borders table-cell info))))
5051 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
5052 "Non-nil when TABLE-CELL is at the end of a column group.
5053 INFO is a plist used as a communication channel."
5054 ;; A cell ends a column group either when it is at the end of a row
5055 ;; or when it has a right border.
5056 (or (eq (car (last (org-element-contents
5057 (org-export-get-parent table-cell))))
5058 table-cell)
5059 (memq 'right (org-export-table-cell-borders table-cell info))))
5061 (defun org-export-table-row-starts-rowgroup-p (table-row info)
5062 "Non-nil when TABLE-ROW is at the beginning of a row group.
5063 INFO is a plist used as a communication channel."
5064 (unless (or (eq (org-element-property :type table-row) 'rule)
5065 (org-export-table-row-is-special-p table-row info))
5066 (let ((borders (org-export-table-cell-borders
5067 (car (org-element-contents table-row)) info)))
5068 (or (memq 'top borders) (memq 'above borders)))))
5070 (defun org-export-table-row-ends-rowgroup-p (table-row info)
5071 "Non-nil when TABLE-ROW is at the end of a row group.
5072 INFO is a plist used as a communication channel."
5073 (unless (or (eq (org-element-property :type table-row) 'rule)
5074 (org-export-table-row-is-special-p table-row info))
5075 (let ((borders (org-export-table-cell-borders
5076 (car (org-element-contents table-row)) info)))
5077 (or (memq 'bottom borders) (memq 'below borders)))))
5079 (defun org-export-table-row-in-header-p (table-row info)
5080 "Non-nil when TABLE-ROW is located within table's header.
5081 INFO is a plist used as a communication channel. Always return
5082 nil for special rows and rows separators."
5083 (and (org-export-table-has-header-p
5084 (org-export-get-parent-table table-row) info)
5085 (eql (org-export-table-row-group table-row info) 1)))
5087 (defun org-export-table-row-starts-header-p (table-row info)
5088 "Non-nil when TABLE-ROW is the first table header's row.
5089 INFO is a plist used as a communication channel."
5090 (and (org-export-table-row-in-header-p table-row info)
5091 (org-export-table-row-starts-rowgroup-p table-row info)))
5093 (defun org-export-table-row-ends-header-p (table-row info)
5094 "Non-nil when TABLE-ROW is the last table header's row.
5095 INFO is a plist used as a communication channel."
5096 (and (org-export-table-row-in-header-p table-row info)
5097 (org-export-table-row-ends-rowgroup-p table-row info)))
5099 (defun org-export-table-row-number (table-row info)
5100 "Return TABLE-ROW number.
5101 INFO is a plist used as a communication channel. Return value is
5102 zero-indexed and ignores separators. The function returns nil
5103 for special rows and separators."
5104 (when (eq (org-element-property :type table-row) 'standard)
5105 (let* ((cache (or (plist-get info :table-row-number-cache)
5106 (let ((table (make-hash-table :test #'eq)))
5107 (plist-put info :table-row-number-cache table)
5108 table)))
5109 (cached (gethash table-row cache 'no-cache)))
5110 (if (not (eq cached 'no-cache)) cached
5111 ;; First time a row is queried, populate cache with all the
5112 ;; rows from the table.
5113 (let ((number -1))
5114 (org-element-map (org-export-get-parent-table table-row) 'table-row
5115 (lambda (row)
5116 (when (eq (org-element-property :type row) 'standard)
5117 (puthash row (cl-incf number) cache)))
5118 info))
5119 (gethash table-row cache)))))
5121 (defun org-export-table-dimensions (table info)
5122 "Return TABLE dimensions.
5124 INFO is a plist used as a communication channel.
5126 Return value is a CONS like (ROWS . COLUMNS) where
5127 ROWS (resp. COLUMNS) is the number of exportable
5128 rows (resp. columns)."
5129 (let (first-row (columns 0) (rows 0))
5130 ;; Set number of rows, and extract first one.
5131 (org-element-map table 'table-row
5132 (lambda (row)
5133 (when (eq (org-element-property :type row) 'standard)
5134 (cl-incf rows)
5135 (unless first-row (setq first-row row)))) info)
5136 ;; Set number of columns.
5137 (org-element-map first-row 'table-cell (lambda (_) (cl-incf columns)) info)
5138 ;; Return value.
5139 (cons rows columns)))
5141 (defun org-export-table-cell-address (table-cell info)
5142 "Return address of a regular TABLE-CELL object.
5144 TABLE-CELL is the cell considered. INFO is a plist used as
5145 a communication channel.
5147 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
5148 zero-based index. Only exportable cells are considered. The
5149 function returns nil for other cells."
5150 (let* ((table-row (org-export-get-parent table-cell))
5151 (row-number (org-export-table-row-number table-row info)))
5152 (when row-number
5153 (cons row-number
5154 (let ((col-count 0))
5155 (org-element-map table-row 'table-cell
5156 (lambda (cell)
5157 (if (eq cell table-cell) col-count (cl-incf col-count) nil))
5158 info 'first-match))))))
5160 (defun org-export-get-table-cell-at (address table info)
5161 "Return regular table-cell object at ADDRESS in TABLE.
5163 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
5164 zero-based index. TABLE is a table type element. INFO is
5165 a plist used as a communication channel.
5167 If no table-cell, among exportable cells, is found at ADDRESS,
5168 return nil."
5169 (let ((column-pos (cdr address)) (column-count 0))
5170 (org-element-map
5171 ;; Row at (car address) or nil.
5172 (let ((row-pos (car address)) (row-count 0))
5173 (org-element-map table 'table-row
5174 (lambda (row)
5175 (cond ((eq (org-element-property :type row) 'rule) nil)
5176 ((= row-count row-pos) row)
5177 (t (cl-incf row-count) nil)))
5178 info 'first-match))
5179 'table-cell
5180 (lambda (cell)
5181 (if (= column-count column-pos) cell
5182 (cl-incf column-count) nil))
5183 info 'first-match)))
5186 ;;;; For Tables Of Contents
5188 ;; `org-export-collect-headlines' builds a list of all exportable
5189 ;; headline elements, maybe limited to a certain depth. One can then
5190 ;; easily parse it and transcode it.
5192 ;; Building lists of tables, figures or listings is quite similar.
5193 ;; Once the generic function `org-export-collect-elements' is defined,
5194 ;; `org-export-collect-tables', `org-export-collect-figures' and
5195 ;; `org-export-collect-listings' can be derived from it.
5197 (defun org-export-collect-headlines (info &optional n scope)
5198 "Collect headlines in order to build a table of contents.
5200 INFO is a plist used as a communication channel.
5202 When optional argument N is an integer, it specifies the depth of
5203 the table of contents. Otherwise, it is set to the value of the
5204 last headline level. See `org-export-headline-levels' for more
5205 information.
5207 Optional argument SCOPE, when non-nil, is an element. If it is
5208 a headline, only children of SCOPE are collected. Otherwise,
5209 collect children of the headline containing provided element. If
5210 there is no such headline, collect all headlines. In any case,
5211 argument N becomes relative to the level of that headline.
5213 Return a list of all exportable headlines as parsed elements.
5214 Footnote sections are ignored."
5215 (let* ((scope (cond ((not scope) (plist-get info :parse-tree))
5216 ((eq (org-element-type scope) 'headline) scope)
5217 ((org-export-get-parent-headline scope))
5218 (t (plist-get info :parse-tree))))
5219 (limit (plist-get info :headline-levels))
5220 (n (if (not (wholenump n)) limit
5221 (min (if (eq (org-element-type scope) 'org-data) n
5222 (+ (org-export-get-relative-level scope info) n))
5223 limit))))
5224 (org-element-map (org-element-contents scope) 'headline
5225 (lambda (headline)
5226 (unless (org-element-property :footnote-section-p headline)
5227 (let ((level (org-export-get-relative-level headline info)))
5228 (and (<= level n) headline))))
5229 info)))
5231 (defun org-export-collect-elements (type info &optional predicate)
5232 "Collect referenceable elements of a determined type.
5234 TYPE can be a symbol or a list of symbols specifying element
5235 types to search. Only elements with a caption are collected.
5237 INFO is a plist used as a communication channel.
5239 When non-nil, optional argument PREDICATE is a function accepting
5240 one argument, an element of type TYPE. It returns a non-nil
5241 value when that element should be collected.
5243 Return a list of all elements found, in order of appearance."
5244 (org-element-map (plist-get info :parse-tree) type
5245 (lambda (element)
5246 (and (org-element-property :caption element)
5247 (or (not predicate) (funcall predicate element))
5248 element))
5249 info))
5251 (defun org-export-collect-tables (info)
5252 "Build a list of tables.
5253 INFO is a plist used as a communication channel.
5255 Return a list of table elements with a caption."
5256 (org-export-collect-elements 'table info))
5258 (defun org-export-collect-figures (info predicate)
5259 "Build a list of figures.
5261 INFO is a plist used as a communication channel. PREDICATE is
5262 a function which accepts one argument: a paragraph element and
5263 whose return value is non-nil when that element should be
5264 collected.
5266 A figure is a paragraph type element, with a caption, verifying
5267 PREDICATE. The latter has to be provided since a \"figure\" is
5268 a vague concept that may depend on back-end.
5270 Return a list of elements recognized as figures."
5271 (org-export-collect-elements 'paragraph info predicate))
5273 (defun org-export-collect-listings (info)
5274 "Build a list of src blocks.
5276 INFO is a plist used as a communication channel.
5278 Return a list of src-block elements with a caption."
5279 (org-export-collect-elements 'src-block info))
5282 ;;;; Smart Quotes
5284 ;; The main function for the smart quotes sub-system is
5285 ;; `org-export-activate-smart-quotes', which replaces every quote in
5286 ;; a given string from the parse tree with its "smart" counterpart.
5288 ;; Dictionary for smart quotes is stored in
5289 ;; `org-export-smart-quotes-alist'.
5291 (defconst org-export-smart-quotes-alist
5292 '(("ar"
5293 (primary-opening
5294 :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5295 :texinfo "@guillemetleft{}")
5296 (primary-closing
5297 :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5298 :texinfo "@guillemetright{}")
5299 (secondary-opening :utf-8 "‹" :html "&lsaquo;" :latex "\\guilsinglleft{}"
5300 :texinfo "@guilsinglleft{}")
5301 (secondary-closing :utf-8 "›" :html "&rsaquo;" :latex "\\guilsinglright{}"
5302 :texinfo "@guilsinglright{}")
5303 (apostrophe :utf-8 "’" :html "&rsquo;"))
5304 ("da"
5305 ;; one may use: »...«, "...", ›...‹, or '...'.
5306 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
5307 ;; LaTeX quotes require Babel!
5308 (primary-opening
5309 :utf-8 "»" :html "&raquo;" :latex ">>" :texinfo "@guillemetright{}")
5310 (primary-closing
5311 :utf-8 "«" :html "&laquo;" :latex "<<" :texinfo "@guillemetleft{}")
5312 (secondary-opening
5313 :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}" :texinfo "@guilsinglright{}")
5314 (secondary-closing
5315 :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}" :texinfo "@guilsingleft{}")
5316 (apostrophe :utf-8 "’" :html "&rsquo;"))
5317 ("de"
5318 (primary-opening
5319 :utf-8 "„" :html "&bdquo;" :latex "\"`" :texinfo "@quotedblbase{}")
5320 (primary-closing
5321 :utf-8 "“" :html "&ldquo;" :latex "\"'" :texinfo "@quotedblleft{}")
5322 (secondary-opening
5323 :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}" :texinfo "@quotesinglbase{}")
5324 (secondary-closing
5325 :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}" :texinfo "@quoteleft{}")
5326 (apostrophe :utf-8 "’" :html "&rsquo;"))
5327 ("en"
5328 (primary-opening :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
5329 (primary-closing :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
5330 (secondary-opening :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5331 (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5332 (apostrophe :utf-8 "’" :html "&rsquo;"))
5333 ("es"
5334 (primary-opening
5335 :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5336 :texinfo "@guillemetleft{}")
5337 (primary-closing
5338 :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5339 :texinfo "@guillemetright{}")
5340 (secondary-opening :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
5341 (secondary-closing :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
5342 (apostrophe :utf-8 "’" :html "&rsquo;"))
5343 ("fr"
5344 (primary-opening
5345 :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
5346 :texinfo "@guillemetleft{}@tie{}")
5347 (primary-closing
5348 :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
5349 :texinfo "@tie{}@guillemetright{}")
5350 (secondary-opening
5351 :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
5352 :texinfo "@guillemetleft{}@tie{}")
5353 (secondary-closing :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
5354 :texinfo "@tie{}@guillemetright{}")
5355 (apostrophe :utf-8 "’" :html "&rsquo;"))
5356 ("is"
5357 (primary-opening
5358 :utf-8 "„" :html "&bdquo;" :latex "\"`" :texinfo "@quotedblbase{}")
5359 (primary-closing
5360 :utf-8 "“" :html "&ldquo;" :latex "\"'" :texinfo "@quotedblleft{}")
5361 (secondary-opening
5362 :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}" :texinfo "@quotesinglbase{}")
5363 (secondary-closing
5364 :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}" :texinfo "@quoteleft{}")
5365 (apostrophe :utf-8 "’" :html "&rsquo;"))
5366 ("no"
5367 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5368 (primary-opening
5369 :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5370 :texinfo "@guillemetleft{}")
5371 (primary-closing
5372 :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5373 :texinfo "@guillemetright{}")
5374 (secondary-opening :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5375 (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5376 (apostrophe :utf-8 "’" :html "&rsquo;"))
5377 ("nb"
5378 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5379 (primary-opening
5380 :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5381 :texinfo "@guillemetleft{}")
5382 (primary-closing
5383 :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5384 :texinfo "@guillemetright{}")
5385 (secondary-opening :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5386 (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5387 (apostrophe :utf-8 "’" :html "&rsquo;"))
5388 ("nn"
5389 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5390 (primary-opening
5391 :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5392 :texinfo "@guillemetleft{}")
5393 (primary-closing
5394 :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5395 :texinfo "@guillemetright{}")
5396 (secondary-opening :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5397 (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5398 (apostrophe :utf-8 "’" :html "&rsquo;"))
5399 ("ru"
5400 ;; 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
5401 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5402 (primary-opening :utf-8 "«" :html "&laquo;" :latex "{}<<"
5403 :texinfo "@guillemetleft{}")
5404 (primary-closing :utf-8 "»" :html "&raquo;" :latex ">>{}"
5405 :texinfo "@guillemetright{}")
5406 (secondary-opening
5407 :utf-8 "„" :html "&bdquo;" :latex "\\glqq{}" :texinfo "@quotedblbase{}")
5408 (secondary-closing
5409 :utf-8 "“" :html "&ldquo;" :latex "\\grqq{}" :texinfo "@quotedblleft{}")
5410 (apostrophe :utf-8 "’" :html: "&#39;"))
5411 ("sl"
5412 ;; Based on https://sl.wikipedia.org/wiki/Narekovaj
5413 (primary-opening :utf-8 "«" :html "&laquo;" :latex "{}<<"
5414 :texinfo "@guillemetleft{}")
5415 (primary-closing :utf-8 "»" :html "&raquo;" :latex ">>{}"
5416 :texinfo "@guillemetright{}")
5417 (secondary-opening
5418 :utf-8 "„" :html "&bdquo;" :latex "\\glqq{}" :texinfo "@quotedblbase{}")
5419 (secondary-closing
5420 :utf-8 "“" :html "&ldquo;" :latex "\\grqq{}" :texinfo "@quotedblleft{}")
5421 (apostrophe :utf-8 "’" :html "&rsquo;"))
5422 ("sv"
5423 ;; Based on https://sv.wikipedia.org/wiki/Citattecken
5424 (primary-opening :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
5425 (primary-closing :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
5426 (secondary-opening :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
5427 (secondary-closing :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
5428 (apostrophe :utf-8 "’" :html "&rsquo;")))
5429 "Smart quotes translations.
5431 Alist whose CAR is a language string and CDR is an alist with
5432 quote type as key and a plist associating various encodings to
5433 their translation as value.
5435 A quote type can be any symbol among `primary-opening',
5436 `primary-closing', `secondary-opening', `secondary-closing' and
5437 `apostrophe'.
5439 Valid encodings include `:utf-8', `:html', `:latex' and
5440 `:texinfo'.
5442 If no translation is found, the quote character is left as-is.")
5444 (defun org-export--smart-quote-status (s info)
5445 "Return smart quote status at the beginning of string S.
5446 INFO is the current export state, as a plist."
5447 (let* ((parent (org-element-property :parent s))
5448 (cache (or (plist-get info :smart-quote-cache)
5449 (let ((table (make-hash-table :test #'eq)))
5450 (plist-put info :smart-quote-cache table)
5451 table)))
5452 (value (gethash parent cache 'missing-data)))
5453 (if (not (eq value 'missing-data)) (cdr (assq s value))
5454 (let (level1-open full-status)
5455 (org-element-map
5456 (let ((secondary (org-element-secondary-p s)))
5457 (if secondary (org-element-property secondary parent)
5458 (org-element-contents parent)))
5459 'plain-text
5460 (lambda (text)
5461 (let ((start 0) current-status)
5462 (while (setq start (string-match "['\"]" text start))
5463 (push
5464 (cond
5465 ((equal (match-string 0 text) "\"")
5466 (setf level1-open (not level1-open))
5467 (if level1-open 'primary-opening 'primary-closing))
5468 ;; Not already in a level 1 quote: this is an
5469 ;; apostrophe.
5470 ((not level1-open) 'apostrophe)
5471 ;; Extract previous char and next char. As
5472 ;; a special case, they can also be set to `blank',
5473 ;; `no-blank' or nil. Then determine if current
5474 ;; match is allowed as an opening quote or a closing
5475 ;; quote.
5477 (let* ((previous
5478 (if (> start 0) (substring text (1- start) start)
5479 (let ((p (org-export-get-previous-element
5480 text info)))
5481 (cond ((not p) nil)
5482 ((stringp p) (substring p -1))
5483 ((memq (org-element-property :post-blank p)
5484 '(0 nil))
5485 'no-blank)
5486 (t 'blank)))))
5487 (next
5488 (if (< (1+ start) (length text))
5489 (substring text (1+ start) (+ start 2))
5490 (let ((n (org-export-get-next-element text info)))
5491 (cond ((not n) nil)
5492 ((stringp n) (substring n 0 1))
5493 (t 'no-blank)))))
5494 (allow-open
5495 (and (if (stringp previous)
5496 (string-match "\\s\"\\|\\s-\\|\\s("
5497 previous)
5498 (memq previous '(blank nil)))
5499 (if (stringp next)
5500 (string-match "\\w\\|\\s.\\|\\s_" next)
5501 (eq next 'no-blank))))
5502 (allow-close
5503 (and (if (stringp previous)
5504 (string-match "\\w\\|\\s.\\|\\s_" previous)
5505 (eq previous 'no-blank))
5506 (if (stringp next)
5507 (string-match "\\s-\\|\\s)\\|\\s.\\|\\s\""
5508 next)
5509 (memq next '(blank nil))))))
5510 (cond
5511 ((and allow-open allow-close) (error "Should not happen"))
5512 (allow-open 'secondary-opening)
5513 (allow-close 'secondary-closing)
5514 (t 'apostrophe)))))
5515 current-status)
5516 (cl-incf start))
5517 (when current-status
5518 (push (cons text (nreverse current-status)) full-status))))
5519 info nil org-element-recursive-objects)
5520 (puthash parent full-status cache)
5521 (cdr (assq s full-status))))))
5523 (defun org-export-activate-smart-quotes (s encoding info &optional original)
5524 "Replace regular quotes with \"smart\" quotes in string S.
5526 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5527 `:utf-8'. INFO is a plist used as a communication channel.
5529 The function has to retrieve information about string
5530 surroundings in parse tree. It can only happen with an
5531 unmodified string. Thus, if S has already been through another
5532 process, a non-nil ORIGINAL optional argument will provide that
5533 original string.
5535 Return the new string."
5536 (let ((quote-status
5537 (copy-sequence (org-export--smart-quote-status (or original s) info))))
5538 (replace-regexp-in-string
5539 "['\"]"
5540 (lambda (match)
5541 (or (plist-get
5542 (cdr (assq (pop quote-status)
5543 (cdr (assoc (plist-get info :language)
5544 org-export-smart-quotes-alist))))
5545 encoding)
5546 match))
5547 s nil t)))
5549 ;;;; Topology
5551 ;; Here are various functions to retrieve information about the
5552 ;; neighborhood of a given element or object. Neighbors of interest
5553 ;; are direct parent (`org-export-get-parent'), parent headline
5554 ;; (`org-export-get-parent-headline'), first element containing an
5555 ;; object, (`org-export-get-parent-element'), parent table
5556 ;; (`org-export-get-parent-table'), previous element or object
5557 ;; (`org-export-get-previous-element') and next element or object
5558 ;; (`org-export-get-next-element').
5560 ;; defsubst org-export-get-parent must be defined before first use
5562 (defun org-export-get-parent-headline (blob)
5563 "Return BLOB parent headline or nil.
5564 BLOB is the element or object being considered."
5565 (org-element-lineage blob '(headline)))
5567 (defun org-export-get-parent-element (object)
5568 "Return first element containing OBJECT or nil.
5569 OBJECT is the object to consider."
5570 (org-element-lineage object org-element-all-elements))
5572 (defun org-export-get-parent-table (object)
5573 "Return OBJECT parent table or nil.
5574 OBJECT is either a `table-cell' or `table-element' type object."
5575 (org-element-lineage object '(table)))
5577 (defun org-export-get-previous-element (blob info &optional n)
5578 "Return previous element or object.
5580 BLOB is an element or object. INFO is a plist used as
5581 a communication channel. Return previous exportable element or
5582 object, a string, or nil.
5584 When optional argument N is a positive integer, return a list
5585 containing up to N siblings before BLOB, from farthest to
5586 closest. With any other non-nil value, return a list containing
5587 all of them."
5588 (let* ((secondary (org-element-secondary-p blob))
5589 (parent (org-export-get-parent blob))
5590 (siblings
5591 (if secondary (org-element-property secondary parent)
5592 (org-element-contents parent)))
5593 prev)
5594 (catch 'exit
5595 (dolist (obj (cdr (memq blob (reverse siblings))) prev)
5596 (cond ((memq obj (plist-get info :ignore-list)))
5597 ((null n) (throw 'exit obj))
5598 ((not (wholenump n)) (push obj prev))
5599 ((zerop n) (throw 'exit prev))
5600 (t (cl-decf n) (push obj prev)))))))
5602 (defun org-export-get-next-element (blob info &optional n)
5603 "Return next element or object.
5605 BLOB is an element or object. INFO is a plist used as
5606 a communication channel. Return next exportable element or
5607 object, a string, or nil.
5609 When optional argument N is a positive integer, return a list
5610 containing up to N siblings after BLOB, from closest to farthest.
5611 With any other non-nil value, return a list containing all of
5612 them."
5613 (let* ((secondary (org-element-secondary-p blob))
5614 (parent (org-export-get-parent blob))
5615 (siblings
5616 (cdr (memq blob
5617 (if secondary (org-element-property secondary parent)
5618 (org-element-contents parent)))))
5619 next)
5620 (catch 'exit
5621 (dolist (obj siblings (nreverse next))
5622 (cond ((memq obj (plist-get info :ignore-list)))
5623 ((null n) (throw 'exit obj))
5624 ((not (wholenump n)) (push obj next))
5625 ((zerop n) (throw 'exit (nreverse next)))
5626 (t (cl-decf n) (push obj next)))))))
5629 ;;;; Translation
5631 ;; `org-export-translate' translates a string according to the language
5632 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5633 ;; the dictionary used for the translation.
5635 (defconst org-export-dictionary
5636 '(("%e %n: %c"
5637 ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
5638 ("Author"
5639 ("ar" :default "تأليف")
5640 ("ca" :default "Autor")
5641 ("cs" :default "Autor")
5642 ("da" :default "Forfatter")
5643 ("de" :default "Autor")
5644 ("eo" :html "A&#365;toro")
5645 ("es" :default "Autor")
5646 ("et" :default "Autor")
5647 ("fi" :html "Tekij&auml;")
5648 ("fr" :default "Auteur")
5649 ("hu" :default "Szerz&otilde;")
5650 ("is" :html "H&ouml;fundur")
5651 ("it" :default "Autore")
5652 ("ja" :default "著者" :html "&#33879;&#32773;")
5653 ("nl" :default "Auteur")
5654 ("no" :default "Forfatter")
5655 ("nb" :default "Forfatter")
5656 ("nn" :default "Forfattar")
5657 ("pl" :default "Autor")
5658 ("pt_BR" :default "Autor")
5659 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5660 ("sl" :default "Avtor")
5661 ("sv" :html "F&ouml;rfattare")
5662 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5663 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
5664 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
5665 ("Continued from previous page"
5666 ("ar" :default "تتمة الصفحة السابقة")
5667 ("de" :default "Fortsetzung von vorheriger Seite")
5668 ("es" :html "Contin&uacute;a de la p&aacute;gina anterior" :ascii "Continua de la pagina anterior" :default "Continúa de la página anterior")
5669 ("fr" :default "Suite de la page précédente")
5670 ("it" :default "Continua da pagina precedente")
5671 ("ja" :default "前ページからの続き")
5672 ("nl" :default "Vervolg van vorige pagina")
5673 ("pt" :default "Continuação da página anterior")
5674 ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077;)"
5675 :utf-8 "(Продолжение)")
5676 ("sl" :default "Nadaljevanje s prejšnje strani"))
5677 ("Continued on next page"
5678 ("ar" :default "التتمة في الصفحة التالية")
5679 ("de" :default "Fortsetzung nächste Seite")
5680 ("es" :html "Contin&uacute;a en la siguiente p&aacute;gina" :ascii "Continua en la siguiente pagina" :default "Continúa en la siguiente página")
5681 ("fr" :default "Suite page suivante")
5682 ("it" :default "Continua alla pagina successiva")
5683 ("ja" :default "次ページに続く")
5684 ("nl" :default "Vervolg op volgende pagina")
5685 ("pt" :default "Continua na página seguinte")
5686 ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077; &#1089;&#1083;&#1077;&#1076;&#1091;&#1077;&#1090;)"
5687 :utf-8 "(Продолжение следует)")
5688 ("sl" :default "Nadaljevanje na naslednji strani"))
5689 ("Created"
5690 ("sl" :default "Ustvarjeno"))
5691 ("Date"
5692 ("ar" :default "بتاريخ")
5693 ("ca" :default "Data")
5694 ("cs" :default "Datum")
5695 ("da" :default "Dato")
5696 ("de" :default "Datum")
5697 ("eo" :default "Dato")
5698 ("es" :default "Fecha")
5699 ("et" :html "Kuup&#228;ev" :utf-8 "Kuupäev")
5700 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
5701 ("hu" :html "D&aacute;tum")
5702 ("is" :default "Dagsetning")
5703 ("it" :default "Data")
5704 ("ja" :default "日付" :html "&#26085;&#20184;")
5705 ("nl" :default "Datum")
5706 ("no" :default "Dato")
5707 ("nb" :default "Dato")
5708 ("nn" :default "Dato")
5709 ("pl" :default "Data")
5710 ("pt_BR" :default "Data")
5711 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5712 ("sl" :default "Datum")
5713 ("sv" :default "Datum")
5714 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5715 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
5716 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
5717 ("Equation"
5718 ("ar" :default "معادلة")
5719 ("da" :default "Ligning")
5720 ("de" :default "Gleichung")
5721 ("es" :ascii "Ecuacion" :html "Ecuaci&oacute;n" :default "Ecuación")
5722 ("et" :html "V&#245;rrand" :utf-8 "Võrrand")
5723 ("fr" :ascii "Equation" :default "Équation")
5724 ("is" :default "Jafna")
5725 ("ja" :default "方程式")
5726 ("no" :default "Ligning")
5727 ("nb" :default "Ligning")
5728 ("nn" :default "Likning")
5729 ("pt_BR" :html "Equa&ccedil;&atilde;o" :default "Equação" :ascii "Equacao")
5730 ("ru" :html "&#1059;&#1088;&#1072;&#1074;&#1085;&#1077;&#1085;&#1080;&#1077;"
5731 :utf-8 "Уравнение")
5732 ("sl" :default "Enačba")
5733 ("sv" :default "Ekvation")
5734 ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
5735 ("Figure"
5736 ("ar" :default "شكل")
5737 ("da" :default "Figur")
5738 ("de" :default "Abbildung")
5739 ("es" :default "Figura")
5740 ("et" :default "Joonis")
5741 ("is" :default "Mynd")
5742 ("ja" :default "図" :html "&#22259;")
5743 ("no" :default "Illustrasjon")
5744 ("nb" :default "Illustrasjon")
5745 ("nn" :default "Illustrasjon")
5746 ("pt_BR" :default "Figura")
5747 ("ru" :html "&#1056;&#1080;&#1089;&#1091;&#1085;&#1086;&#1082;" :utf-8 "Рисунок")
5748 ("sv" :default "Illustration")
5749 ("zh-CN" :html "&#22270;" :utf-8 "图"))
5750 ("Figure %d:"
5751 ("ar" :default "شكل %d:")
5752 ("da" :default "Figur %d")
5753 ("de" :default "Abbildung %d:")
5754 ("es" :default "Figura %d:")
5755 ("et" :default "Joonis %d:")
5756 ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
5757 ("is" :default "Mynd %d")
5758 ("ja" :default "図%d: " :html "&#22259;%d: ")
5759 ("no" :default "Illustrasjon %d")
5760 ("nb" :default "Illustrasjon %d")
5761 ("nn" :default "Illustrasjon %d")
5762 ("pt_BR" :default "Figura %d:")
5763 ("ru" :html "&#1056;&#1080;&#1089;. %d.:" :utf-8 "Рис. %d.:")
5764 ("sl" :default "Slika %d")
5765 ("sv" :default "Illustration %d")
5766 ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
5767 ("Footnotes"
5768 ("ar" :default "الهوامش")
5769 ("ca" :html "Peus de p&agrave;gina")
5770 ("cs" :default "Pozn\xe1mky pod carou")
5771 ("da" :default "Fodnoter")
5772 ("de" :html "Fu&szlig;noten" :default "Fußnoten")
5773 ("eo" :default "Piednotoj")
5774 ("es" :ascii "Nota al pie de pagina" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
5775 ("et" :html "Allm&#228;rkused" :utf-8 "Allmärkused")
5776 ("fi" :default "Alaviitteet")
5777 ("fr" :default "Notes de bas de page")
5778 ("hu" :html "L&aacute;bjegyzet")
5779 ("is" :html "Aftanm&aacute;lsgreinar")
5780 ("it" :html "Note a pi&egrave; di pagina")
5781 ("ja" :default "脚注" :html "&#33050;&#27880;")
5782 ("nl" :default "Voetnoten")
5783 ("no" :default "Fotnoter")
5784 ("nb" :default "Fotnoter")
5785 ("nn" :default "Fotnotar")
5786 ("pl" :default "Przypis")
5787 ("pt_BR" :html "Notas de Rodap&eacute;" :default "Notas de Rodapé" :ascii "Notas de Rodape")
5788 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
5789 ("sl" :default "Opombe")
5790 ("sv" :default "Fotnoter")
5791 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
5792 :utf-8 "Примітки")
5793 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
5794 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
5795 ("List of Listings"
5796 ("ar" :default "قائمة بالبرامج")
5797 ("da" :default "Programmer")
5798 ("de" :default "Programmauflistungsverzeichnis")
5799 ("es" :ascii "Indice de Listados de programas" :html "&Iacute;ndice de Listados de programas" :default "Índice de Listados de programas")
5800 ("et" :default "Loendite nimekiri")
5801 ("fr" :default "Liste des programmes")
5802 ("ja" :default "ソースコード目次")
5803 ("no" :default "Dataprogrammer")
5804 ("nb" :default "Dataprogrammer")
5805 ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1088;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1086;&#1082;"
5806 :utf-8 "Список распечаток")
5807 ("sl" :default "Seznam programskih izpisov")
5808 ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
5809 ("List of Tables"
5810 ("ar" :default "قائمة بالجداول")
5811 ("da" :default "Tabeller")
5812 ("de" :default "Tabellenverzeichnis")
5813 ("es" :ascii "Indice de tablas" :html "&Iacute;ndice de tablas" :default "Índice de tablas")
5814 ("et" :default "Tabelite nimekiri")
5815 ("fr" :default "Liste des tableaux")
5816 ("is" :default "Töfluskrá" :html "T&ouml;fluskr&aacute;")
5817 ("ja" :default "表目次")
5818 ("no" :default "Tabeller")
5819 ("nb" :default "Tabeller")
5820 ("nn" :default "Tabeller")
5821 ("pt_BR" :default "Índice de Tabelas" :ascii "Indice de Tabelas")
5822 ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1090;&#1072;&#1073;&#1083;&#1080;&#1094;"
5823 :utf-8 "Список таблиц")
5824 ("sl" :default "Seznam tabel")
5825 ("sv" :default "Tabeller")
5826 ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
5827 ("Listing"
5828 ("ar" :default "برنامج")
5829 ("da" :default "Program")
5830 ("de" :default "Programmlisting")
5831 ("es" :default "Listado de programa")
5832 ("et" :default "Loend")
5833 ("fr" :default "Programme" :html "Programme")
5834 ("ja" :default "ソースコード")
5835 ("no" :default "Dataprogram")
5836 ("nb" :default "Dataprogram")
5837 ("pt_BR" :default "Listagem")
5838 ("ru" :html "&#1056;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1082;&#1072;"
5839 :utf-8 "Распечатка")
5840 ("sl" :default "Izpis programa")
5841 ("zh-CN" :html "&#20195;&#30721;" :utf-8 "代码"))
5842 ("Listing %d:"
5843 ("ar" :default "برنامج %d:")
5844 ("da" :default "Program %d")
5845 ("de" :default "Programmlisting %d")
5846 ("es" :default "Listado de programa %d")
5847 ("et" :default "Loend %d")
5848 ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
5849 ("ja" :default "ソースコード%d:")
5850 ("no" :default "Dataprogram %d")
5851 ("nb" :default "Dataprogram %d")
5852 ("pt_BR" :default "Listagem %d")
5853 ("ru" :html "&#1056;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1082;&#1072; %d.:"
5854 :utf-8 "Распечатка %d.:")
5855 ("sl" :default "Izpis programa %d")
5856 ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
5857 ("References"
5858 ("ar" :default "المراجع")
5859 ("fr" :ascii "References" :default "Références")
5860 ("de" :default "Quellen")
5861 ("es" :default "Referencias")
5862 ("sl" :default "Reference"))
5863 ("See figure %s"
5864 ("fr" :default "cf. figure %s"
5865 :html "cf.&nbsp;figure&nbsp;%s" :latex "cf.~figure~%s")
5866 ("sl" :default "Glej sliko %s"))
5867 ("See listing %s"
5868 ("fr" :default "cf. programme %s"
5869 :html "cf.&nbsp;programme&nbsp;%s" :latex "cf.~programme~%s")
5870 ("sl" :default "Glej izpis programa %s"))
5871 ("See section %s"
5872 ("ar" :default "انظر قسم %s")
5873 ("da" :default "jævnfør afsnit %s")
5874 ("de" :default "siehe Abschnitt %s")
5875 ("es" :ascii "Vea seccion %s" :html "Vea secci&oacute;n %s" :default "Vea sección %s")
5876 ("et" :html "Vaata peat&#252;kki %s" :utf-8 "Vaata peatükki %s")
5877 ("fr" :default "cf. section %s")
5878 ("ja" :default "セクション %s を参照")
5879 ("pt_BR" :html "Veja a se&ccedil;&atilde;o %s" :default "Veja a seção %s"
5880 :ascii "Veja a secao %s")
5881 ("ru" :html "&#1057;&#1084;. &#1088;&#1072;&#1079;&#1076;&#1077;&#1083; %s"
5882 :utf-8 "См. раздел %s")
5883 ("sl" :default "Glej poglavje %d")
5884 ("zh-CN" :html "&#21442;&#35265;&#31532;%s&#33410;" :utf-8 "参见第%s节"))
5885 ("See table %s"
5886 ("fr" :default "cf. tableau %s"
5887 :html "cf.&nbsp;tableau&nbsp;%s" :latex "cf.~tableau~%s")
5888 ("sl" :default "Glej tabelo %s"))
5889 ("Table"
5890 ("ar" :default "جدول")
5891 ("de" :default "Tabelle")
5892 ("es" :default "Tabla")
5893 ("et" :default "Tabel")
5894 ("fr" :default "Tableau")
5895 ("is" :default "Tafla")
5896 ("ja" :default "表" :html "&#34920;")
5897 ("pt_BR" :default "Tabela")
5898 ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072;"
5899 :utf-8 "Таблица")
5900 ("zh-CN" :html "&#34920;" :utf-8 "表"))
5901 ("Table %d:"
5902 ("ar" :default "جدول %d:")
5903 ("da" :default "Tabel %d")
5904 ("de" :default "Tabelle %d")
5905 ("es" :default "Tabla %d")
5906 ("et" :default "Tabel %d")
5907 ("fr" :default "Tableau %d :")
5908 ("is" :default "Tafla %d")
5909 ("ja" :default "表%d:" :html "&#34920;%d:")
5910 ("no" :default "Tabell %d")
5911 ("nb" :default "Tabell %d")
5912 ("nn" :default "Tabell %d")
5913 ("pt_BR" :default "Tabela %d")
5914 ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072; %d.:"
5915 :utf-8 "Таблица %d.:")
5916 ("sl" :default "Tabela %d")
5917 ("sv" :default "Tabell %d")
5918 ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
5919 ("Table of Contents"
5920 ("ar" :default "قائمة المحتويات")
5921 ("ca" :html "&Iacute;ndex")
5922 ("cs" :default "Obsah")
5923 ("da" :default "Indhold")
5924 ("de" :default "Inhaltsverzeichnis")
5925 ("eo" :default "Enhavo")
5926 ("es" :ascii "Indice" :html "&Iacute;ndice" :default "Índice")
5927 ("et" :default "Sisukord")
5928 ("fi" :html "Sis&auml;llysluettelo")
5929 ("fr" :ascii "Sommaire" :default "Table des matières")
5930 ("hu" :html "Tartalomjegyz&eacute;k")
5931 ("is" :default "Efnisyfirlit")
5932 ("it" :default "Indice")
5933 ("ja" :default "目次" :html "&#30446;&#27425;")
5934 ("nl" :default "Inhoudsopgave")
5935 ("no" :default "Innhold")
5936 ("nb" :default "Innhold")
5937 ("nn" :default "Innhald")
5938 ("pl" :html "Spis tre&#x015b;ci")
5939 ("pt_BR" :html "&Iacute;ndice" :utf8 "Índice" :ascii "Indice")
5940 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
5941 :utf-8 "Содержание")
5942 ("sl" :default "Kazalo")
5943 ("sv" :html "Inneh&aring;ll")
5944 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
5945 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
5946 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
5947 ("Unknown reference"
5948 ("ar" :default "مرجع غير معرّف")
5949 ("da" :default "ukendt reference")
5950 ("de" :default "Unbekannter Verweis")
5951 ("es" :default "Referencia desconocida")
5952 ("et" :default "Tundmatu viide")
5953 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
5954 ("ja" :default "不明な参照先")
5955 ("pt_BR" :default "Referência desconhecida"
5956 :ascii "Referencia desconhecida")
5957 ("ru" :html "&#1053;&#1077;&#1080;&#1079;&#1074;&#1077;&#1089;&#1090;&#1085;&#1072;&#1103; &#1089;&#1089;&#1099;&#1083;&#1082;&#1072;"
5958 :utf-8 "Неизвестная ссылка")
5959 ("sl" :default "Neznana referenca")
5960 ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
5961 "Dictionary for export engine.
5963 Alist whose car is the string to translate and cdr is an alist
5964 whose car is the language string and cdr is a plist whose
5965 properties are possible charsets and values translated terms.
5967 It is used as a database for `org-export-translate'. Since this
5968 function returns the string as-is if no translation was found,
5969 the variable only needs to record values different from the
5970 entry.")
5972 (defun org-export-translate (s encoding info)
5973 "Translate string S according to language specification.
5975 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5976 and `:utf-8'. INFO is a plist used as a communication channel.
5978 Translation depends on `:language' property. Return the
5979 translated string. If no translation is found, try to fall back
5980 to `:default' encoding. If it fails, return S."
5981 (let* ((lang (plist-get info :language))
5982 (translations (cdr (assoc lang
5983 (cdr (assoc s org-export-dictionary))))))
5984 (or (plist-get translations encoding)
5985 (plist-get translations :default)
5986 s)))
5990 ;;; Asynchronous Export
5992 ;; `org-export-async-start' is the entry point for asynchronous
5993 ;; export. It recreates current buffer (including visibility,
5994 ;; narrowing and visited file) in an external Emacs process, and
5995 ;; evaluates a command there. It then applies a function on the
5996 ;; returned results in the current process.
5998 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5999 ;; allow exporting to a buffer or a file, asynchronously or not.
6001 ;; `org-export-output-file-name' is an auxiliary function meant to be
6002 ;; used with `org-export-to-file'. With a given extension, it tries
6003 ;; to provide a canonical file name to write export output to.
6005 ;; Asynchronously generated results are never displayed directly.
6006 ;; Instead, they are stored in `org-export-stack-contents'. They can
6007 ;; then be retrieved by calling `org-export-stack'.
6009 ;; Export Stack is viewed through a dedicated major mode
6010 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
6011 ;;`org-export-stack-delete', `org-export-stack-view' and
6012 ;;`org-export-stack-clear'.
6014 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
6015 ;; It should be used whenever `org-export-async-start' is called.
6017 (defmacro org-export-async-start (fun &rest body)
6018 "Call function FUN on the results returned by BODY evaluation.
6020 FUN is an anonymous function of one argument. BODY evaluation
6021 happens in an asynchronous process, from a buffer which is an
6022 exact copy of the current one.
6024 Use `org-export-add-to-stack' in FUN in order to register results
6025 in the stack.
6027 This is a low level function. See also `org-export-to-buffer'
6028 and `org-export-to-file' for more specialized functions."
6029 (declare (indent 1) (debug t))
6030 (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
6031 ;; Write the full sexp evaluating BODY in a copy of the current
6032 ;; buffer to a temporary file, as it may be too long for program
6033 ;; args in `start-process'.
6034 `(with-temp-message "Initializing asynchronous export process"
6035 (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
6036 (,temp-file (make-temp-file "org-export-process"))
6037 (,coding buffer-file-coding-system))
6038 (with-temp-file ,temp-file
6039 (insert
6040 ;; Null characters (from variable values) are inserted
6041 ;; within the file. As a consequence, coding system for
6042 ;; buffer contents will not be recognized properly. So,
6043 ;; we make sure it is the same as the one used to display
6044 ;; the original buffer.
6045 (format ";; -*- coding: %s; -*-\n%S"
6046 ,coding
6047 `(with-temp-buffer
6048 (when org-export-async-debug '(setq debug-on-error t))
6049 ;; Ignore `kill-emacs-hook' and code evaluation
6050 ;; queries from Babel as we need a truly
6051 ;; non-interactive process.
6052 (setq kill-emacs-hook nil
6053 org-babel-confirm-evaluate-answer-no t)
6054 ;; Initialize export framework.
6055 (require 'ox)
6056 ;; Re-create current buffer there.
6057 (funcall ,,copy-fun)
6058 (restore-buffer-modified-p nil)
6059 ;; Sexp to evaluate in the buffer.
6060 (print (progn ,,@body))))))
6061 ;; Start external process.
6062 (let* ((process-connection-type nil)
6063 (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
6064 (,process
6065 (apply
6066 #'start-process
6067 (append
6068 (list "org-export-process"
6069 ,proc-buffer
6070 (expand-file-name invocation-name invocation-directory)
6071 "--batch")
6072 (if org-export-async-init-file
6073 (list "-Q" "-l" org-export-async-init-file)
6074 (list "-l" user-init-file))
6075 (list "-l" ,temp-file)))))
6076 ;; Register running process in stack.
6077 (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
6078 ;; Set-up sentinel in order to catch results.
6079 (let ((handler ,fun))
6080 (set-process-sentinel
6081 ,process
6082 `(lambda (p status)
6083 (let ((proc-buffer (process-buffer p)))
6084 (when (eq (process-status p) 'exit)
6085 (unwind-protect
6086 (if (zerop (process-exit-status p))
6087 (unwind-protect
6088 (let ((results
6089 (with-current-buffer proc-buffer
6090 (goto-char (point-max))
6091 (backward-sexp)
6092 (read (current-buffer)))))
6093 (funcall ,handler results))
6094 (unless org-export-async-debug
6095 (and (get-buffer proc-buffer)
6096 (kill-buffer proc-buffer))))
6097 (org-export-add-to-stack proc-buffer nil p)
6098 (ding)
6099 (message "Process `%s' exited abnormally" p))
6100 (unless org-export-async-debug
6101 (delete-file ,,temp-file)))))))))))))
6103 ;;;###autoload
6104 (defun org-export-to-buffer
6105 (backend buffer
6106 &optional async subtreep visible-only body-only ext-plist
6107 post-process)
6108 "Call `org-export-as' with output to a specified buffer.
6110 BACKEND is either an export back-end, as returned by, e.g.,
6111 `org-export-create-backend', or a symbol referring to
6112 a registered back-end.
6114 BUFFER is the name of the output buffer. If it already exists,
6115 it will be erased first, otherwise, it will be created.
6117 A non-nil optional argument ASYNC means the process should happen
6118 asynchronously. The resulting buffer should then be accessible
6119 through the `org-export-stack' interface. When ASYNC is nil, the
6120 buffer is displayed if `org-export-show-temporary-export-buffer'
6121 is non-nil.
6123 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
6124 EXT-PLIST are similar to those used in `org-export-as', which
6125 see.
6127 Optional argument POST-PROCESS is a function which should accept
6128 no argument. It is always called within the current process,
6129 from BUFFER, with point at its beginning. Export back-ends can
6130 use it to set a major mode there, e.g,
6132 (defun org-latex-export-as-latex
6133 (&optional async subtreep visible-only body-only ext-plist)
6134 (interactive)
6135 (org-export-to-buffer \\='latex \"*Org LATEX Export*\"
6136 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
6138 This function returns BUFFER."
6139 (declare (indent 2))
6140 (if async
6141 (org-export-async-start
6142 `(lambda (output)
6143 (with-current-buffer (get-buffer-create ,buffer)
6144 (erase-buffer)
6145 (setq buffer-file-coding-system ',buffer-file-coding-system)
6146 (insert output)
6147 (goto-char (point-min))
6148 (org-export-add-to-stack (current-buffer) ',backend)
6149 (ignore-errors (funcall ,post-process))))
6150 `(org-export-as
6151 ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
6152 (let ((output
6153 (org-export-as backend subtreep visible-only body-only ext-plist))
6154 (buffer (get-buffer-create buffer))
6155 (encoding buffer-file-coding-system))
6156 (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
6157 (org-kill-new output))
6158 (with-current-buffer buffer
6159 (erase-buffer)
6160 (setq buffer-file-coding-system encoding)
6161 (insert output)
6162 (goto-char (point-min))
6163 (and (functionp post-process) (funcall post-process)))
6164 (when org-export-show-temporary-export-buffer
6165 (switch-to-buffer-other-window buffer))
6166 buffer)))
6168 ;;;###autoload
6169 (defun org-export-to-file
6170 (backend file &optional async subtreep visible-only body-only ext-plist
6171 post-process)
6172 "Call `org-export-as' with output to a specified file.
6174 BACKEND is either an export back-end, as returned by, e.g.,
6175 `org-export-create-backend', or a symbol referring to
6176 a registered back-end. FILE is the name of the output file, as
6177 a string.
6179 A non-nil optional argument ASYNC means the process should happen
6180 asynchronously. The resulting buffer will then be accessible
6181 through the `org-export-stack' interface.
6183 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
6184 EXT-PLIST are similar to those used in `org-export-as', which
6185 see.
6187 Optional argument POST-PROCESS is called with FILE as its
6188 argument and happens asynchronously when ASYNC is non-nil. It
6189 has to return a file name, or nil. Export back-ends can use this
6190 to send the output file through additional processing, e.g,
6192 (defun org-latex-export-to-latex
6193 (&optional async subtreep visible-only body-only ext-plist)
6194 (interactive)
6195 (let ((outfile (org-export-output-file-name \".tex\" subtreep)))
6196 (org-export-to-file \\='latex outfile
6197 async subtreep visible-only body-only ext-plist
6198 (lambda (file) (org-latex-compile file)))
6200 The function returns either a file name returned by POST-PROCESS,
6201 or FILE."
6202 (declare (indent 2))
6203 (if (not (file-writable-p file)) (error "Output file not writable")
6204 (let ((ext-plist (org-combine-plists `(:output-file ,file) ext-plist))
6205 (encoding (or org-export-coding-system buffer-file-coding-system)))
6206 (if async
6207 (org-export-async-start
6208 `(lambda (file)
6209 (org-export-add-to-stack (expand-file-name file) ',backend))
6210 `(let ((output
6211 (org-export-as
6212 ',backend ,subtreep ,visible-only ,body-only
6213 ',ext-plist)))
6214 (with-temp-buffer
6215 (insert output)
6216 (let ((coding-system-for-write ',encoding))
6217 (write-file ,file)))
6218 (or (ignore-errors (funcall ',post-process ,file)) ,file)))
6219 (let ((output (org-export-as
6220 backend subtreep visible-only body-only ext-plist)))
6221 (with-temp-buffer
6222 (insert output)
6223 (let ((coding-system-for-write encoding))
6224 (write-file file)))
6225 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
6226 (org-kill-new output))
6227 ;; Get proper return value.
6228 (or (and (functionp post-process) (funcall post-process file))
6229 file))))))
6231 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
6232 "Return output file's name according to buffer specifications.
6234 EXTENSION is a string representing the output file extension,
6235 with the leading dot.
6237 With a non-nil optional argument SUBTREEP, try to determine
6238 output file's name by looking for \"EXPORT_FILE_NAME\" property
6239 of subtree at point.
6241 When optional argument PUB-DIR is set, use it as the publishing
6242 directory.
6244 Return file name as a string."
6245 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
6246 (base-name
6247 (concat
6248 (file-name-sans-extension
6250 ;; Check EXPORT_FILE_NAME subtree property.
6251 (and subtreep (org-entry-get nil "EXPORT_FILE_NAME" 'selective))
6252 ;; Check #+EXPORT_FILE_NAME keyword.
6253 (org-with-point-at (point-min)
6254 (catch :found
6255 (let ((case-fold-search t))
6256 (while (re-search-forward
6257 "^[ \t]*#\\+EXPORT_FILE_NAME:[ \t]+\\S-" nil t)
6258 (let ((element (org-element-at-point)))
6259 (when (eq 'keyword (org-element-type element))
6260 (throw :found
6261 (org-element-property :value element))))))))
6262 ;; Extract from buffer's associated file, if any.
6263 (and visited-file (file-name-nondirectory visited-file))
6264 ;; Can't determine file name on our own: ask user.
6265 (read-file-name
6266 "Output file: " pub-dir nil nil nil
6267 (lambda (n) (string= extension (file-name-extension n t))))))
6268 extension))
6269 (output-file
6270 ;; Build file name. Enforce EXTENSION over whatever user
6271 ;; may have come up with. PUB-DIR, if defined, always has
6272 ;; precedence over any provided path.
6273 (cond
6274 (pub-dir (concat (file-name-as-directory pub-dir)
6275 (file-name-nondirectory base-name)))
6276 ((file-name-absolute-p base-name) base-name)
6277 (t base-name))))
6278 ;; If writing to OUTPUT-FILE would overwrite original file, append
6279 ;; EXTENSION another time to final name.
6280 (if (and visited-file (file-equal-p visited-file output-file))
6281 (concat output-file extension)
6282 output-file)))
6284 (defun org-export-add-to-stack (source backend &optional process)
6285 "Add a new result to export stack if not present already.
6287 SOURCE is a buffer or a file name containing export results.
6288 BACKEND is a symbol representing export back-end used to generate
6291 Entries already pointing to SOURCE and unavailable entries are
6292 removed beforehand. Return the new stack."
6293 (setq org-export-stack-contents
6294 (cons (list source backend (or process (current-time)))
6295 (org-export-stack-remove source))))
6297 (defun org-export-stack ()
6298 "Menu for asynchronous export results and running processes."
6299 (interactive)
6300 (let ((buffer (get-buffer-create "*Org Export Stack*")))
6301 (with-current-buffer buffer
6302 (org-export-stack-mode)
6303 (tabulated-list-print t))
6304 (pop-to-buffer buffer))
6305 (message "Type \"q\" to quit, \"?\" for help"))
6307 (defun org-export-stack-clear ()
6308 "Remove all entries from export stack."
6309 (interactive)
6310 (setq org-export-stack-contents nil))
6312 (defun org-export-stack-refresh ()
6313 "Refresh the export stack."
6314 (interactive)
6315 (tabulated-list-print t))
6317 (defun org-export-stack-remove (&optional source)
6318 "Remove export results at point from stack.
6319 If optional argument SOURCE is non-nil, remove it instead."
6320 (interactive)
6321 (let ((source (or source (org-export--stack-source-at-point))))
6322 (setq org-export-stack-contents
6323 (cl-remove-if (lambda (el) (equal (car el) source))
6324 org-export-stack-contents))))
6326 (defun org-export-stack-view (&optional in-emacs)
6327 "View export results at point in stack.
6328 With an optional prefix argument IN-EMACS, force viewing files
6329 within Emacs."
6330 (interactive "P")
6331 (let ((source (org-export--stack-source-at-point)))
6332 (cond ((processp source)
6333 (org-switch-to-buffer-other-window (process-buffer source)))
6334 ((bufferp source) (org-switch-to-buffer-other-window source))
6335 (t (org-open-file source in-emacs)))))
6337 (defvar org-export-stack-mode-map
6338 (let ((km (make-sparse-keymap)))
6339 (set-keymap-parent km tabulated-list-mode-map)
6340 (define-key km " " 'next-line)
6341 (define-key km "\C-n" 'next-line)
6342 (define-key km [down] 'next-line)
6343 (define-key km "\C-p" 'previous-line)
6344 (define-key km "\C-?" 'previous-line)
6345 (define-key km [up] 'previous-line)
6346 (define-key km "C" 'org-export-stack-clear)
6347 (define-key km "v" 'org-export-stack-view)
6348 (define-key km (kbd "RET") 'org-export-stack-view)
6349 (define-key km "d" 'org-export-stack-remove)
6351 "Keymap for Org Export Stack.")
6353 (define-derived-mode org-export-stack-mode tabulated-list-mode "Org-Stack"
6354 "Mode for displaying asynchronous export stack.
6356 Type `\\[org-export-stack]' to visualize the asynchronous export
6357 stack.
6359 In an Org Export Stack buffer, use \
6360 \\<org-export-stack-mode-map>`\\[org-export-stack-view]' to view export output
6361 on current line, `\\[org-export-stack-remove]' to remove it from the stack and \
6362 `\\[org-export-stack-clear]' to clear
6363 stack completely.
6365 Removing entries in a stack buffer does not affect files
6366 or buffers, only display.
6368 \\{org-export-stack-mode-map}"
6369 (setq tabulated-list-format
6370 (vector (list "#" 4 #'org-export--stack-num-predicate)
6371 (list "Back-End" 12 t)
6372 (list "Age" 6 nil)
6373 (list "Source" 0 nil)))
6374 (setq tabulated-list-sort-key (cons "#" nil))
6375 (setq tabulated-list-entries #'org-export--stack-generate)
6376 (add-hook 'tabulated-list-revert-hook #'org-export--stack-generate nil t)
6377 (add-hook 'post-command-hook #'org-export-stack-refresh nil t)
6378 (tabulated-list-init-header))
6380 (defun org-export--stack-generate ()
6381 "Generate the asynchronous export stack for display.
6382 Unavailable sources are removed from the list. Return a list
6383 appropriate for `tabulated-list-print'."
6384 ;; Clear stack from exited processes, dead buffers or non-existent
6385 ;; files.
6386 (setq org-export-stack-contents
6387 (cl-remove-if-not
6388 (lambda (el)
6389 (if (processp (nth 2 el))
6390 (buffer-live-p (process-buffer (nth 2 el)))
6391 (let ((source (car el)))
6392 (if (bufferp source) (buffer-live-p source)
6393 (file-exists-p source)))))
6394 org-export-stack-contents))
6395 ;; Update `tabulated-list-entries'.
6396 (let ((counter 0))
6397 (mapcar
6398 (lambda (entry)
6399 (let ((source (car entry)))
6400 (list source
6401 (vector
6402 ;; Counter.
6403 (number-to-string (cl-incf counter))
6404 ;; Back-End.
6405 (if (nth 1 entry) (symbol-name (nth 1 entry)) "")
6406 ;; Age.
6407 (let ((info (nth 2 entry)))
6408 (if (processp info) (symbol-name (process-status info))
6409 (format-seconds "%h:%.2m" (float-time (time-since info)))))
6410 ;; Source.
6411 (if (stringp source) source (buffer-name source))))))
6412 org-export-stack-contents)))
6414 (defun org-export--stack-num-predicate (a b)
6415 (< (string-to-number (aref (nth 1 a) 0))
6416 (string-to-number (aref (nth 1 b) 0))))
6418 (defun org-export--stack-source-at-point ()
6419 "Return source from export results at point in stack."
6420 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
6421 (if (not source) (error "Source unavailable, please refresh buffer")
6422 (let ((source-name (if (stringp source) source (buffer-name source))))
6423 (if (save-excursion
6424 (beginning-of-line)
6425 (looking-at-p (concat ".* +" (regexp-quote source-name) "$")))
6426 source
6427 ;; SOURCE is not consistent with current line. The stack
6428 ;; view is outdated.
6429 (error (substitute-command-keys
6430 "Source unavailable; type `\\[org-export-stack-refresh]' \
6431 to refresh buffer")))))))
6435 ;;; The Dispatcher
6437 ;; `org-export-dispatch' is the standard interactive way to start an
6438 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6439 ;; for its interface, which, in turn, delegates response to key
6440 ;; pressed to `org-export--dispatch-action'.
6442 ;;;###autoload
6443 (defun org-export-dispatch (&optional arg)
6444 "Export dispatcher for Org mode.
6446 It provides an access to common export related tasks in a buffer.
6447 Its interface comes in two flavors: standard and expert.
6449 While both share the same set of bindings, only the former
6450 displays the valid keys associations in a dedicated buffer.
6451 Scrolling (resp. line-wise motion) in this buffer is done with
6452 SPC and DEL (resp. C-n and C-p) keys.
6454 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6455 flavor or the other.
6457 When ARG is `\\[universal-argument]', repeat the last export action, with the\
6458 same
6459 set of options used back then, on the current buffer.
6461 When ARG is `\\[universal-argument] \\[universal-argument]', display the \
6462 asynchronous export stack."
6463 (interactive "P")
6464 (let* ((input
6465 (cond ((equal arg '(16)) '(stack))
6466 ((and arg org-export-dispatch-last-action))
6467 (t (save-window-excursion
6468 (unwind-protect
6469 (progn
6470 ;; Remember where we are
6471 (move-marker org-export-dispatch-last-position
6472 (point)
6473 (org-base-buffer (current-buffer)))
6474 ;; Get and store an export command
6475 (setq org-export-dispatch-last-action
6476 (org-export--dispatch-ui
6477 (list org-export-initial-scope
6478 (and org-export-in-background 'async))
6480 org-export-dispatch-use-expert-ui)))
6481 (and (get-buffer "*Org Export Dispatcher*")
6482 (kill-buffer "*Org Export Dispatcher*")))))))
6483 (action (car input))
6484 (optns (cdr input)))
6485 (unless (memq 'subtree optns)
6486 (move-marker org-export-dispatch-last-position nil))
6487 (cl-case action
6488 ;; First handle special hard-coded actions.
6489 (template (org-export-insert-default-template nil optns))
6490 (stack (org-export-stack))
6491 (publish-current-file
6492 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6493 (publish-current-project
6494 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6495 (publish-choose-project
6496 (org-publish (assoc (completing-read
6497 "Publish project: "
6498 org-publish-project-alist nil t)
6499 org-publish-project-alist)
6500 (memq 'force optns)
6501 (memq 'async optns)))
6502 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6503 (otherwise
6504 (save-excursion
6505 (when arg
6506 ;; Repeating command, maybe move cursor to restore subtree
6507 ;; context.
6508 (if (eq (marker-buffer org-export-dispatch-last-position)
6509 (org-base-buffer (current-buffer)))
6510 (goto-char org-export-dispatch-last-position)
6511 ;; We are in a different buffer, forget position.
6512 (move-marker org-export-dispatch-last-position nil)))
6513 (funcall action
6514 ;; Return a symbol instead of a list to ease
6515 ;; asynchronous export macro use.
6516 (and (memq 'async optns) t)
6517 (and (memq 'subtree optns) t)
6518 (and (memq 'visible optns) t)
6519 (and (memq 'body optns) t)))))))
6521 (defun org-export--dispatch-ui (options first-key expertp)
6522 "Handle interface for `org-export-dispatch'.
6524 OPTIONS is a list containing current interactive options set for
6525 export. It can contain any of the following symbols:
6526 `body' toggles a body-only export
6527 `subtree' restricts export to current subtree
6528 `visible' restricts export to visible part of buffer.
6529 `force' force publishing files.
6530 `async' use asynchronous export process
6532 FIRST-KEY is the key pressed to select the first level menu. It
6533 is nil when this menu hasn't been selected yet.
6535 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6536 buffer is provided, but indications about currently active
6537 options are given in the prompt. Moreover, [?] allows switching
6538 back to standard interface."
6539 (let* ((fontify-key
6540 (lambda (key &optional access-key)
6541 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6542 ;; non-nil is the required first-level key to activate
6543 ;; KEY. When its value is t, activate KEY independently
6544 ;; on the first key, if any. A nil value means KEY will
6545 ;; only be activated at first level.
6546 (if (or (eq access-key t) (eq access-key first-key))
6547 (propertize key 'face 'org-warning)
6548 key)))
6549 (fontify-value
6550 (lambda (value)
6551 ;; Fontify VALUE string.
6552 (propertize value 'face 'font-lock-variable-name-face)))
6553 ;; Prepare menu entries by extracting them from registered
6554 ;; back-ends and sorting them by access key and by ordinal,
6555 ;; if any.
6556 (entries
6557 (sort (sort (delq nil
6558 (mapcar #'org-export-backend-menu
6559 org-export-registered-backends))
6560 (lambda (a b)
6561 (let ((key-a (nth 1 a))
6562 (key-b (nth 1 b)))
6563 (cond ((and (numberp key-a) (numberp key-b))
6564 (< key-a key-b))
6565 ((numberp key-b) t)))))
6566 'car-less-than-car))
6567 ;; Compute a list of allowed keys based on the first key
6568 ;; pressed, if any. Some keys
6569 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6570 ;; available.
6571 (allowed-keys
6572 (nconc (list 2 22 19 6 1)
6573 (if (not first-key) (org-uniquify (mapcar 'car entries))
6574 (let (sub-menu)
6575 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6576 (when (eq (car entry) first-key)
6577 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6578 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6579 ((not first-key) (list ?P)))
6580 (list ?& ?#)
6581 (when expertp (list ??))
6582 (list ?q)))
6583 ;; Build the help menu for standard UI.
6584 (help
6585 (unless expertp
6586 (concat
6587 ;; Options are hard-coded.
6588 (format "[%s] Body only: %s [%s] Visible only: %s
6589 \[%s] Export scope: %s [%s] Force publishing: %s
6590 \[%s] Async export: %s\n\n"
6591 (funcall fontify-key "C-b" t)
6592 (funcall fontify-value
6593 (if (memq 'body options) "On " "Off"))
6594 (funcall fontify-key "C-v" t)
6595 (funcall fontify-value
6596 (if (memq 'visible options) "On " "Off"))
6597 (funcall fontify-key "C-s" t)
6598 (funcall fontify-value
6599 (if (memq 'subtree options) "Subtree" "Buffer "))
6600 (funcall fontify-key "C-f" t)
6601 (funcall fontify-value
6602 (if (memq 'force options) "On " "Off"))
6603 (funcall fontify-key "C-a" t)
6604 (funcall fontify-value
6605 (if (memq 'async options) "On " "Off")))
6606 ;; Display registered back-end entries. When a key
6607 ;; appears for the second time, do not create another
6608 ;; entry, but append its sub-menu to existing menu.
6609 (let (last-key)
6610 (mapconcat
6611 (lambda (entry)
6612 (let ((top-key (car entry)))
6613 (concat
6614 (unless (eq top-key last-key)
6615 (setq last-key top-key)
6616 (format "\n[%s] %s\n"
6617 (funcall fontify-key (char-to-string top-key))
6618 (nth 1 entry)))
6619 (let ((sub-menu (nth 2 entry)))
6620 (unless (functionp sub-menu)
6621 ;; Split sub-menu into two columns.
6622 (let ((index -1))
6623 (concat
6624 (mapconcat
6625 (lambda (sub-entry)
6626 (cl-incf index)
6627 (format
6628 (if (zerop (mod index 2)) " [%s] %-26s"
6629 "[%s] %s\n")
6630 (funcall fontify-key
6631 (char-to-string (car sub-entry))
6632 top-key)
6633 (nth 1 sub-entry)))
6634 sub-menu "")
6635 (when (zerop (mod index 2)) "\n"))))))))
6636 entries ""))
6637 ;; Publishing menu is hard-coded.
6638 (format "\n[%s] Publish
6639 [%s] Current file [%s] Current project
6640 [%s] Choose project [%s] All projects\n\n\n"
6641 (funcall fontify-key "P")
6642 (funcall fontify-key "f" ?P)
6643 (funcall fontify-key "p" ?P)
6644 (funcall fontify-key "x" ?P)
6645 (funcall fontify-key "a" ?P))
6646 (format "[%s] Export stack [%s] Insert template\n"
6647 (funcall fontify-key "&" t)
6648 (funcall fontify-key "#" t))
6649 (format "[%s] %s"
6650 (funcall fontify-key "q" t)
6651 (if first-key "Main menu" "Exit")))))
6652 ;; Build prompts for both standard and expert UI.
6653 (standard-prompt (unless expertp "Export command: "))
6654 (expert-prompt
6655 (when expertp
6656 (format
6657 "Export command (C-%s%s%s%s%s) [%s]: "
6658 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6659 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6660 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6661 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6662 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6663 (mapconcat (lambda (k)
6664 ;; Strip control characters.
6665 (unless (< k 27) (char-to-string k)))
6666 allowed-keys "")))))
6667 ;; With expert UI, just read key with a fancy prompt. In standard
6668 ;; UI, display an intrusive help buffer.
6669 (if expertp
6670 (org-export--dispatch-action
6671 expert-prompt allowed-keys entries options first-key expertp)
6672 ;; At first call, create frame layout in order to display menu.
6673 (unless (get-buffer "*Org Export Dispatcher*")
6674 (delete-other-windows)
6675 (org-switch-to-buffer-other-window
6676 (get-buffer-create "*Org Export Dispatcher*"))
6677 (setq cursor-type nil
6678 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6679 ;; Make sure that invisible cursor will not highlight square
6680 ;; brackets.
6681 (set-syntax-table (copy-syntax-table))
6682 (modify-syntax-entry ?\[ "w"))
6683 ;; At this point, the buffer containing the menu exists and is
6684 ;; visible in the current window. So, refresh it.
6685 (with-current-buffer "*Org Export Dispatcher*"
6686 ;; Refresh help. Maintain display continuity by re-visiting
6687 ;; previous window position.
6688 (let ((pos (window-start)))
6689 (erase-buffer)
6690 (insert help)
6691 (set-window-start nil pos)))
6692 (org-fit-window-to-buffer)
6693 (org-export--dispatch-action
6694 standard-prompt allowed-keys entries options first-key expertp))))
6696 (defun org-export--dispatch-action
6697 (prompt allowed-keys entries options first-key expertp)
6698 "Read a character from command input and act accordingly.
6700 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6701 a list of characters available at a given step in the process.
6702 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6703 EXPERTP are the same as defined in `org-export--dispatch-ui',
6704 which see.
6706 Toggle export options when required. Otherwise, return value is
6707 a list with action as CAR and a list of interactive export
6708 options as CDR."
6709 (let (key)
6710 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6711 ;; C-p, SPC, DEL).
6712 (while (and (setq key (read-char-exclusive prompt))
6713 (not expertp)
6714 (memq key '(14 16 ?\s ?\d)))
6715 (cl-case key
6716 (14 (if (not (pos-visible-in-window-p (point-max)))
6717 (ignore-errors (scroll-up 1))
6718 (message "End of buffer")
6719 (sit-for 1)))
6720 (16 (if (not (pos-visible-in-window-p (point-min)))
6721 (ignore-errors (scroll-down 1))
6722 (message "Beginning of buffer")
6723 (sit-for 1)))
6724 (?\s (if (not (pos-visible-in-window-p (point-max)))
6725 (scroll-up nil)
6726 (message "End of buffer")
6727 (sit-for 1)))
6728 (?\d (if (not (pos-visible-in-window-p (point-min)))
6729 (scroll-down nil)
6730 (message "Beginning of buffer")
6731 (sit-for 1)))))
6732 (cond
6733 ;; Ignore undefined associations.
6734 ((not (memq key allowed-keys))
6735 (ding)
6736 (unless expertp (message "Invalid key") (sit-for 1))
6737 (org-export--dispatch-ui options first-key expertp))
6738 ;; q key at first level aborts export. At second level, cancel
6739 ;; first key instead.
6740 ((eq key ?q) (if (not first-key) (error "Export aborted")
6741 (org-export--dispatch-ui options nil expertp)))
6742 ;; Help key: Switch back to standard interface if expert UI was
6743 ;; active.
6744 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6745 ;; Send request for template insertion along with export scope.
6746 ((eq key ?#) (cons 'template (memq 'subtree options)))
6747 ;; Switch to asynchronous export stack.
6748 ((eq key ?&) '(stack))
6749 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6750 ((memq key '(2 22 19 6 1))
6751 (org-export--dispatch-ui
6752 (let ((option (cl-case key (2 'body) (22 'visible) (19 'subtree)
6753 (6 'force) (1 'async))))
6754 (if (memq option options) (remq option options)
6755 (cons option options)))
6756 first-key expertp))
6757 ;; Action selected: Send key and options back to
6758 ;; `org-export-dispatch'.
6759 ((or first-key (functionp (nth 2 (assq key entries))))
6760 (cons (cond
6761 ((not first-key) (nth 2 (assq key entries)))
6762 ;; Publishing actions are hard-coded. Send a special
6763 ;; signal to `org-export-dispatch'.
6764 ((eq first-key ?P)
6765 (cl-case key
6766 (?f 'publish-current-file)
6767 (?p 'publish-current-project)
6768 (?x 'publish-choose-project)
6769 (?a 'publish-all)))
6770 ;; Return first action associated to FIRST-KEY + KEY
6771 ;; path. Indeed, derived backends can share the same
6772 ;; FIRST-KEY.
6773 (t (catch 'found
6774 (dolist (entry (member (assq first-key entries) entries))
6775 (let ((match (assq key (nth 2 entry))))
6776 (when match (throw 'found (nth 2 match))))))))
6777 options))
6778 ;; Otherwise, enter sub-menu.
6779 (t (org-export--dispatch-ui options key expertp)))))
6783 (provide 'ox)
6785 ;; Local variables:
6786 ;; generated-autoload-file: "org-loaddefs.el"
6787 ;; End:
6789 ;;; ox.el ends here