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