org-export: Fix docstring
[org-mode.git] / contrib / lisp / org-export.el
blob09e0c82995c4f8f7089022867bb4638bf6999297
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 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
875 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
876 \(?p \"PDF file\" org-e-latex-export-to-pdf)
877 \(?o \"PDF file and open\"
878 \(lambda (subtree visible body-only)
879 \(org-open-file
880 \(org-e-latex-export-to-pdf subtree visible body-only))))))
882 :options-alist
884 Alist between back-end specific properties introduced in
885 communication channel and how their value are acquired. See
886 `org-export-options-alist' for more information about
887 structure of the values."
888 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
889 (indent 1))
890 (let (export-block filters menu-entry options contents)
891 (while (keywordp (car body))
892 (case (pop body)
893 (:export-block (let ((names (pop body)))
894 (setq export-block
895 (if (consp names) (mapcar 'upcase names)
896 (list (upcase names))))))
897 (:filters-alist (setq filters (pop body)))
898 (:menu-entry (setq menu-entry (pop body)))
899 (:options-alist (setq options (pop body)))
900 (t (pop body))))
901 (setq contents (append (list :translate-alist translators)
902 (and filters (list :filters-alist filters))
903 (and options (list :options-alist options))
904 (and menu-entry (list :menu-entry menu-entry))))
905 `(progn
906 ;; Register back-end.
907 (let ((registeredp (assq ',backend org-export-registered-backends)))
908 (if registeredp (setcdr registeredp ',contents)
909 (push (cons ',backend ',contents) org-export-registered-backends)))
910 ;; Tell parser to not parse EXPORT-BLOCK blocks.
911 ,(when export-block
912 `(mapc
913 (lambda (name)
914 (add-to-list 'org-element-block-name-alist
915 `(,name . org-element-export-block-parser)))
916 ',export-block))
917 ;; Splice in the body, if any.
918 ,@body)))
920 (defmacro org-export-define-derived-backend (child parent &rest body)
921 "Create a new back-end as a variant of an existing one.
923 CHILD is the name of the derived back-end. PARENT is the name of
924 the parent back-end.
926 BODY can start with pre-defined keyword arguments. The following
927 keywords are understood:
929 :export-block
931 String, or list of strings, representing block names that
932 will not be parsed. This is used to specify blocks that will
933 contain raw code specific to the back-end. These blocks
934 still have to be handled by the relative `export-block' type
935 translator.
937 :filters-alist
939 Alist of filters that will overwrite or complete filters
940 defined in PARENT back-end. See `org-export-filters-alist'
941 for a list of allowed filters.
943 :menu-entry
945 Menu entry for the export dispatcher. See
946 `org-export-define-backend' for more information about the
947 expected value.
949 :options-alist
951 Alist of back-end specific properties that will overwrite or
952 complete those defined in PARENT back-end. Refer to
953 `org-export-options-alist' for more information about
954 structure of the values.
956 :translate-alist
958 Alist of element and object types and transcoders that will
959 overwrite or complete transcode table from PARENT back-end.
960 Refer to `org-export-define-backend' for detailed information
961 about transcoders.
963 As an example, here is how one could define \"my-latex\" back-end
964 as a variant of `e-latex' back-end with a custom template
965 function:
967 \(org-export-define-derived-backend my-latex e-latex
968 :translate-alist ((template . my-latex-template-fun)))
970 The back-end could then be called with, for example:
972 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
973 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
974 (indent 2))
975 (org-export-barf-if-invalid-backend parent)
976 (let (export-block filters menu-entry options translators contents)
977 (while (keywordp (car body))
978 (case (pop body)
979 (:export-block (let ((names (pop body)))
980 (setq export-block
981 (if (consp names) (mapcar 'upcase names)
982 (list (upcase names))))))
983 (:filters-alist (setq filters (pop body)))
984 (:menu-entry (setq menu-entry (pop body)))
985 (:options-alist (setq options (pop body)))
986 (:translate-alist (setq translators (pop body)))
987 (t (pop body))))
988 (setq contents (append
989 (list :parent parent)
990 (let ((p-table (org-export-backend-translate-table parent)))
991 (list :translate-alist (append translators p-table)))
992 (let ((p-filters (org-export-backend-filters parent)))
993 (list :filters-alist (append filters p-filters)))
994 (let ((p-options (org-export-backend-options parent)))
995 (list :options-alist (append options p-options)))
996 (and menu-entry (list :menu-entry menu-entry))))
997 `(progn
998 ;; Register back-end.
999 (let ((registeredp (assq ',child org-export-registered-backends)))
1000 (if registeredp (setcdr registeredp ',contents)
1001 (push (cons ',child ',contents) org-export-registered-backends)))
1002 ;; Tell parser to not parse EXPORT-BLOCK blocks.
1003 ,(when export-block
1004 `(mapc
1005 (lambda (name)
1006 (add-to-list 'org-element-block-name-alist
1007 `(,name . org-element-export-block-parser)))
1008 ',export-block))
1009 ;; Splice in the body, if any.
1010 ,@body)))
1012 (defun org-export-backend-filters (backend)
1013 "Return filters for BACKEND."
1014 (plist-get (cdr (assq backend org-export-registered-backends))
1015 :filters-alist))
1017 (defun org-export-backend-menu (backend)
1018 "Return menu entry for BACKEND."
1019 (plist-get (cdr (assq backend org-export-registered-backends))
1020 :menu-entry))
1022 (defun org-export-backend-options (backend)
1023 "Return export options for BACKEND."
1024 (plist-get (cdr (assq backend org-export-registered-backends))
1025 :options-alist))
1027 (defun org-export-backend-translate-table (backend)
1028 "Return translate table for BACKEND."
1029 (plist-get (cdr (assq backend org-export-registered-backends))
1030 :translate-alist))
1032 (defun org-export-barf-if-invalid-backend (backend)
1033 "Signal an error if BACKEND isn't defined."
1034 (unless (org-export-backend-translate-table backend)
1035 (error "Unknown \"%s\" back-end: Aborting export" backend)))
1037 (defun org-export-derived-backend-p (backend &rest backends)
1038 "Non-nil if BACKEND is derived from one of BACKENDS."
1039 (let ((parent backend))
1040 (while (and (not (memq parent backends))
1041 (setq parent
1042 (plist-get (cdr (assq parent
1043 org-export-registered-backends))
1044 :parent))))
1045 parent))
1049 ;;; The Communication Channel
1051 ;; During export process, every function has access to a number of
1052 ;; properties. They are of two types:
1054 ;; 1. Environment options are collected once at the very beginning of
1055 ;; the process, out of the original buffer and configuration.
1056 ;; Collecting them is handled by `org-export-get-environment'
1057 ;; function.
1059 ;; Most environment options are defined through the
1060 ;; `org-export-options-alist' variable.
1062 ;; 2. Tree properties are extracted directly from the parsed tree,
1063 ;; just before export, by `org-export-collect-tree-properties'.
1065 ;; Here is the full list of properties available during transcode
1066 ;; process, with their category and their value type.
1068 ;; + `:author' :: Author's name.
1069 ;; - category :: option
1070 ;; - type :: string
1072 ;; + `:back-end' :: Current back-end used for transcoding.
1073 ;; - category :: tree
1074 ;; - type :: symbol
1076 ;; + `:creator' :: String to write as creation information.
1077 ;; - category :: option
1078 ;; - type :: string
1080 ;; + `:date' :: String to use as date.
1081 ;; - category :: option
1082 ;; - type :: string
1084 ;; + `:description' :: Description text for the current data.
1085 ;; - category :: option
1086 ;; - type :: string
1088 ;; + `:email' :: Author's email.
1089 ;; - category :: option
1090 ;; - type :: string
1092 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1093 ;; process.
1094 ;; - category :: option
1095 ;; - type :: list of strings
1097 ;; + `:exported-data' :: Hash table used for memoizing
1098 ;; `org-export-data'.
1099 ;; - category :: tree
1100 ;; - type :: hash table
1102 ;; + `:filetags' :: List of global tags for buffer. Used by
1103 ;; `org-export-get-tags' to get tags with inheritance.
1104 ;; - category :: option
1105 ;; - type :: list of strings
1107 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1108 ;; their definition, as parsed data. Only non-inlined footnotes
1109 ;; are represented in this alist. Also, every definition isn't
1110 ;; guaranteed to be referenced in the parse tree. The purpose of
1111 ;; this property is to preserve definitions from oblivion
1112 ;; (i.e. when the parse tree comes from a part of the original
1113 ;; buffer), it isn't meant for direct use in a back-end. To
1114 ;; retrieve a definition relative to a reference, use
1115 ;; `org-export-get-footnote-definition' instead.
1116 ;; - category :: option
1117 ;; - type :: alist (STRING . LIST)
1119 ;; + `:headline-levels' :: Maximum level being exported as an
1120 ;; headline. Comparison is done with the relative level of
1121 ;; headlines in the parse tree, not necessarily with their
1122 ;; actual level.
1123 ;; - category :: option
1124 ;; - type :: integer
1126 ;; + `:headline-offset' :: Difference between relative and real level
1127 ;; of headlines in the parse tree. For example, a value of -1
1128 ;; means a level 2 headline should be considered as level
1129 ;; 1 (cf. `org-export-get-relative-level').
1130 ;; - category :: tree
1131 ;; - type :: integer
1133 ;; + `:headline-numbering' :: Alist between headlines and their
1134 ;; numbering, as a list of numbers
1135 ;; (cf. `org-export-get-headline-number').
1136 ;; - category :: tree
1137 ;; - type :: alist (INTEGER . LIST)
1139 ;; + `:id-alist' :: Alist between ID strings and destination file's
1140 ;; path, relative to current directory. It is used by
1141 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1142 ;; external file.
1143 ;; - category :: option
1144 ;; - type :: alist (STRING . STRING)
1146 ;; + `:ignore-list' :: List of elements and objects that should be
1147 ;; ignored during export.
1148 ;; - category :: tree
1149 ;; - type :: list of elements and objects
1151 ;; + `:input-file' :: Full path to input file, if any.
1152 ;; - category :: option
1153 ;; - type :: string or nil
1155 ;; + `:keywords' :: List of keywords attached to data.
1156 ;; - category :: option
1157 ;; - type :: string
1159 ;; + `:language' :: Default language used for translations.
1160 ;; - category :: option
1161 ;; - type :: string
1163 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1164 ;; transcoding.
1165 ;; - category :: option
1166 ;; - type :: list (as returned by `org-element-parse-buffer')
1168 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1169 ;; all line breaks.
1170 ;; - category :: option
1171 ;; - type :: symbol (nil, t)
1173 ;; + `:section-numbers' :: Non-nil means transcoding should add
1174 ;; section numbers to headlines.
1175 ;; - category :: option
1176 ;; - type :: symbol (nil, t)
1178 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1179 ;; in transcoding. When such a tag is present, subtrees without
1180 ;; it are de facto excluded from the process. See
1181 ;; `use-select-tags'.
1182 ;; - category :: option
1183 ;; - type :: list of strings
1185 ;; + `:target-list' :: List of targets encountered in the parse tree.
1186 ;; This is used to partly resolve "fuzzy" links
1187 ;; (cf. `org-export-resolve-fuzzy-link').
1188 ;; - category :: tree
1189 ;; - type :: list of strings
1191 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1192 ;; a time stamp in the output.
1193 ;; - category :: option
1194 ;; - type :: symbol (nil, t)
1196 ;; + `:translate-alist' :: Alist between element and object types and
1197 ;; transcoding functions relative to the current back-end.
1198 ;; Special keys `template' and `plain-text' are also possible.
1199 ;; - category :: option
1200 ;; - type :: alist (SYMBOL . FUNCTION)
1202 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1203 ;; also be transcoded. If it is set to the `headline' symbol,
1204 ;; only the archived headline's name is retained.
1205 ;; - category :: option
1206 ;; - type :: symbol (nil, t, `headline')
1208 ;; + `:with-author' :: Non-nil means author's name should be included
1209 ;; in the output.
1210 ;; - category :: option
1211 ;; - type :: symbol (nil, t)
1213 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1214 ;; - category :: option
1215 ;; - type :: symbol (nil, t)
1217 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1218 ;; inserted at the end of the transcoded string. If the value
1219 ;; is `comment', it should be commented.
1220 ;; - category :: option
1221 ;; - type :: symbol (`comment', nil, t)
1223 ;; + `:with-date' :: Non-nil means output should contain a date.
1224 ;; - category :: option
1225 ;; - type :. symbol (nil, t)
1227 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1228 ;; its value is a list of names, only drawers with such names
1229 ;; will be transcoded.
1230 ;; - category :: option
1231 ;; - type :: symbol (nil, t) or list of strings
1233 ;; + `:with-email' :: Non-nil means output should contain author's
1234 ;; email.
1235 ;; - category :: option
1236 ;; - type :: symbol (nil, t)
1238 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1239 ;; interpreted.
1240 ;; - category :: option
1241 ;; - type :: symbol (nil, t)
1243 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1244 ;; strings starting with a colon as a fixed-with (verbatim) area.
1245 ;; - category :: option
1246 ;; - type :: symbol (nil, t)
1248 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1249 ;; footnotes.
1250 ;; - category :: option
1251 ;; - type :: symbol (nil, t)
1253 ;; + `:with-plannings' :: Non-nil means transcoding should include
1254 ;; planning info.
1255 ;; - category :: option
1256 ;; - type :: symbol (nil, t)
1258 ;; + `:with-priority' :: Non-nil means transcoding should include
1259 ;; priority cookies.
1260 ;; - category :: option
1261 ;; - type :: symbol (nil, t)
1263 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1264 ;; plain text.
1265 ;; - category :: option
1266 ;; - type :: symbol (nil, t)
1268 ;; + `:with-special-strings' :: Non-nil means transcoding should
1269 ;; interpret special strings in plain text.
1270 ;; - category :: option
1271 ;; - type :: symbol (nil, t)
1273 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1274 ;; interpret subscript and superscript. With a value of "{}",
1275 ;; only interpret those using curly brackets.
1276 ;; - category :: option
1277 ;; - type :: symbol (nil, {}, t)
1279 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1280 ;; tables.
1281 ;; - category :: option
1282 ;; - type :: symbol (nil, t)
1284 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1285 ;; headlines. A `not-in-toc' value will remove them from the
1286 ;; table of contents, if any, nonetheless.
1287 ;; - category :: option
1288 ;; - type :: symbol (nil, t, `not-in-toc')
1290 ;; + `:with-tasks' :: Non-nil means transcoding should include
1291 ;; headlines with a TODO keyword. A `todo' value will only
1292 ;; include headlines with a todo type keyword while a `done'
1293 ;; value will do the contrary. If a list of strings is provided,
1294 ;; only tasks with keywords belonging to that list will be kept.
1295 ;; - category :: option
1296 ;; - type :: symbol (t, todo, done, nil) or list of strings
1298 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1299 ;; time stamps. Special value `active' (resp. `inactive') ask to
1300 ;; export only active (resp. inactive) timestamps. Otherwise,
1301 ;; completely remove them.
1302 ;; - category :: option
1303 ;; - type :: symbol: (`active', `inactive', t, nil)
1305 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1306 ;; added to the output. An integer value limits its depth.
1307 ;; - category :: option
1308 ;; - type :: symbol (nil, t or integer)
1310 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1311 ;; include TODO keywords.
1312 ;; - category :: option
1313 ;; - type :: symbol (nil, t)
1316 ;;;; Environment Options
1318 ;; Environment options encompass all parameters defined outside the
1319 ;; scope of the parsed data. They come from five sources, in
1320 ;; increasing precedence order:
1322 ;; - Global variables,
1323 ;; - Buffer's attributes,
1324 ;; - Options keyword symbols,
1325 ;; - Buffer keywords,
1326 ;; - Subtree properties.
1328 ;; The central internal function with regards to environment options
1329 ;; is `org-export-get-environment'. It updates global variables with
1330 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1331 ;; the different sources.
1333 ;; The internal functions doing the retrieval are:
1334 ;; `org-export--get-global-options',
1335 ;; `org-export--get-buffer-attributes',
1336 ;; `org-export--parse-option-keyword',
1337 ;; `org-export--get-subtree-options' and
1338 ;; `org-export--get-inbuffer-options'
1340 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1341 ;; take care of the part relative to "#+BIND:" keywords.
1343 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1344 "Collect export options from the current buffer.
1346 Optional argument BACKEND is a symbol specifying which back-end
1347 specific options to read, if any.
1349 When optional argument SUBTREEP is non-nil, assume the export is
1350 done against the current sub-tree.
1352 Third optional argument EXT-PLIST is a property list with
1353 external parameters overriding Org default settings, but still
1354 inferior to file-local settings."
1355 ;; First install #+BIND variables.
1356 (org-export--install-letbind-maybe)
1357 ;; Get and prioritize export options...
1358 (org-combine-plists
1359 ;; ... from global variables...
1360 (org-export--get-global-options backend)
1361 ;; ... from an external property list...
1362 ext-plist
1363 ;; ... from in-buffer settings...
1364 (org-export--get-inbuffer-options
1365 backend
1366 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1367 ;; ... and from subtree, when appropriate.
1368 (and subtreep (org-export--get-subtree-options backend))
1369 ;; Eventually add misc. properties.
1370 (list
1371 :back-end
1372 backend
1373 :translate-alist
1374 (org-export-backend-translate-table backend)
1375 :footnote-definition-alist
1376 ;; Footnotes definitions must be collected in the original
1377 ;; buffer, as there's no insurance that they will still be in
1378 ;; the parse tree, due to possible narrowing.
1379 (let (alist)
1380 (org-with-wide-buffer
1381 (goto-char (point-min))
1382 (while (re-search-forward org-footnote-definition-re nil t)
1383 (let ((def (save-match-data (org-element-at-point))))
1384 (when (eq (org-element-type def) 'footnote-definition)
1385 (push
1386 (cons (org-element-property :label def)
1387 (let ((cbeg (org-element-property :contents-begin def)))
1388 (when cbeg
1389 (org-element--parse-elements
1390 cbeg (org-element-property :contents-end def)
1391 nil nil nil nil (list 'org-data nil)))))
1392 alist))))
1393 alist))
1394 :id-alist
1395 ;; Collect id references.
1396 (let (alist)
1397 (org-with-wide-buffer
1398 (goto-char (point-min))
1399 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1400 (let ((link (org-element-context)))
1401 (when (eq (org-element-type link) 'link)
1402 (let* ((id (org-element-property :path link))
1403 (file (org-id-find-id-file id)))
1404 (when file
1405 (push (cons id (file-relative-name file)) alist)))))))
1406 alist))))
1408 (defun org-export--parse-option-keyword (options &optional backend)
1409 "Parse an OPTIONS line and return values as a plist.
1410 Optional argument BACKEND is a symbol specifying which back-end
1411 specific items to read, if any."
1412 (let* ((all (append org-export-options-alist
1413 (and backend (org-export-backend-options backend))))
1414 ;; Build an alist between #+OPTION: item and property-name.
1415 (alist (delq nil
1416 (mapcar (lambda (e)
1417 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1418 (car e))))
1419 all)))
1420 plist)
1421 (mapc (lambda (e)
1422 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1423 (car e)
1424 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1425 options)
1426 (setq plist (plist-put plist
1427 (cdr e)
1428 (car (read-from-string
1429 (match-string 2 options)))))))
1430 alist)
1431 plist))
1433 (defun org-export--get-subtree-options (&optional backend)
1434 "Get export options in subtree at point.
1435 Optional argument BACKEND is a symbol specifying back-end used
1436 for export. Return options as a plist."
1437 ;; For each buffer keyword, create an headline property setting the
1438 ;; same property in communication channel. The name for the property
1439 ;; is the keyword with "EXPORT_" appended to it.
1440 (org-with-wide-buffer
1441 (let (prop plist)
1442 ;; Make sure point is at an heading.
1443 (unless (org-at-heading-p) (org-back-to-heading t))
1444 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1445 ;; title as its fallback value.
1446 (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
1447 (progn (looking-at org-todo-line-regexp)
1448 (org-match-string-no-properties 3))))
1449 (setq plist
1450 (plist-put
1451 plist :title
1452 (org-element-parse-secondary-string
1453 prop (org-element-restriction 'keyword)))))
1454 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1455 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1456 (setq plist
1457 (nconc plist (org-export--parse-option-keyword prop backend))))
1458 ;; Handle other keywords. TITLE keyword is excluded as it has
1459 ;; been handled already.
1460 (let ((seen '("TITLE")))
1461 (mapc
1462 (lambda (option)
1463 (let ((property (nth 1 option)))
1464 (when (and property (not (member property seen)))
1465 (let* ((subtree-prop (concat "EXPORT_" property))
1466 ;; Export properties are not case-sensitive.
1467 (value (let ((case-fold-search t))
1468 (org-entry-get (point) subtree-prop))))
1469 (push property seen)
1470 (when value
1471 (setq plist
1472 (plist-put
1473 plist
1474 (car option)
1475 (cond
1476 ;; Parse VALUE if required.
1477 ((member property org-element-document-properties)
1478 (org-element-parse-secondary-string
1479 value (org-element-restriction 'keyword)))
1480 ;; If BEHAVIOUR is `split' expected value is
1481 ;; a list of strings, not a string.
1482 ((eq (nth 4 option) 'split) (org-split-string value))
1483 (t value)))))))))
1484 ;; Also look for both general keywords and back-end specific
1485 ;; options if BACKEND is provided.
1486 (append (and backend (org-export-backend-options backend))
1487 org-export-options-alist)))
1488 ;; Return value.
1489 plist)))
1491 (defun org-export--get-inbuffer-options (&optional backend files)
1492 "Return current buffer export options, as a plist.
1494 Optional argument BACKEND, when non-nil, is a symbol specifying
1495 which back-end specific options should also be read in the
1496 process.
1498 Optional argument FILES is a list of setup files names read so
1499 far, used to avoid circular dependencies.
1501 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1502 (org-with-wide-buffer
1503 (goto-char (point-min))
1504 (let ((case-fold-search t) plist)
1505 ;; 1. Special keywords, as in `org-export-special-keywords'.
1506 (let ((special-re
1507 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1508 (while (re-search-forward special-re nil t)
1509 (let ((element (org-element-at-point)))
1510 (when (eq (org-element-type element) 'keyword)
1511 (let* ((key (org-element-property :key element))
1512 (val (org-element-property :value element))
1513 (prop
1514 (cond
1515 ((equal key "SETUP_FILE")
1516 (let ((file
1517 (expand-file-name
1518 (org-remove-double-quotes (org-trim val)))))
1519 ;; Avoid circular dependencies.
1520 (unless (member file files)
1521 (with-temp-buffer
1522 (insert (org-file-contents file 'noerror))
1523 (org-mode)
1524 (org-export--get-inbuffer-options
1525 backend (cons file files))))))
1526 ((equal key "OPTIONS")
1527 (org-export--parse-option-keyword val backend))
1528 ((equal key "FILETAGS")
1529 (list :filetags
1530 (org-uniquify
1531 (append (org-split-string val ":")
1532 (plist-get plist :filetags))))))))
1533 (setq plist (org-combine-plists plist prop)))))))
1534 ;; 2. Standard options, as in `org-export-options-alist'.
1535 (let* ((all (append org-export-options-alist
1536 ;; Also look for back-end specific options if
1537 ;; BACKEND is defined.
1538 (and backend (org-export-backend-options backend))))
1539 ;; Build ALIST between keyword name and property name.
1540 (alist
1541 (delq nil (mapcar
1542 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1543 all)))
1544 ;; Build regexp matching all keywords associated to export
1545 ;; options. Note: the search is case insensitive.
1546 (opt-re (format "^[ \t]*#\\+%s:"
1547 (regexp-opt
1548 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1549 (goto-char (point-min))
1550 (while (re-search-forward opt-re nil t)
1551 (let ((element (org-element-at-point)))
1552 (when (eq (org-element-type element) 'keyword)
1553 (let* ((key (org-element-property :key element))
1554 (val (org-element-property :value element))
1555 (prop (cdr (assoc key alist)))
1556 (behaviour (nth 4 (assq prop all))))
1557 (setq plist
1558 (plist-put
1559 plist prop
1560 ;; Handle value depending on specified BEHAVIOUR.
1561 (case behaviour
1562 (space
1563 (if (not (plist-get plist prop)) (org-trim val)
1564 (concat (plist-get plist prop) " " (org-trim val))))
1565 (newline
1566 (org-trim
1567 (concat (plist-get plist prop) "\n" (org-trim val))))
1568 (split
1569 `(,@(plist-get plist prop) ,@(org-split-string val)))
1570 ('t val)
1571 (otherwise (if (not (plist-member plist prop)) val
1572 (plist-get plist prop))))))))))
1573 ;; Parse keywords specified in
1574 ;; `org-element-document-properties'.
1575 (mapc
1576 (lambda (key)
1577 (let* ((prop (cdr (assoc key alist)))
1578 (value (and prop (plist-get plist prop))))
1579 (when (stringp value)
1580 (setq plist
1581 (plist-put
1582 plist prop
1583 (org-element-parse-secondary-string
1584 value (org-element-restriction 'keyword)))))))
1585 org-element-document-properties))
1586 ;; 3. Return final value.
1587 plist)))
1589 (defun org-export--get-buffer-attributes ()
1590 "Return properties related to buffer attributes, as a plist."
1591 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1592 (list
1593 ;; Store full path of input file name, or nil. For internal use.
1594 :input-file visited-file
1595 :title (or (and visited-file
1596 (file-name-sans-extension
1597 (file-name-nondirectory visited-file)))
1598 (buffer-name (buffer-base-buffer))))))
1600 (defun org-export--get-global-options (&optional backend)
1601 "Return global export options as a plist.
1603 Optional argument BACKEND, if non-nil, is a symbol specifying
1604 which back-end specific export options should also be read in the
1605 process."
1606 (let ((all (append org-export-options-alist
1607 (and backend (org-export-backend-options backend))))
1608 ;; Output value.
1609 plist)
1610 (mapc
1611 (lambda (cell)
1612 (setq plist
1613 (plist-put
1614 plist
1615 (car cell)
1616 ;; Eval default value provided. If keyword is a member
1617 ;; of `org-element-document-properties', parse it as
1618 ;; a secondary string before storing it.
1619 (let ((value (eval (nth 3 cell))))
1620 (if (not (stringp value)) value
1621 (let ((keyword (nth 1 cell)))
1622 (if (not (member keyword org-element-document-properties))
1623 value
1624 (org-element-parse-secondary-string
1625 value (org-element-restriction 'keyword)))))))))
1626 all)
1627 ;; Return value.
1628 plist))
1630 (defvar org-export--allow-BIND-local nil)
1631 (defun org-export--confirm-letbind ()
1632 "Can we use #+BIND values during export?
1633 By default this will ask for confirmation by the user, to divert
1634 possible security risks."
1635 (cond
1636 ((not org-export-allow-BIND) nil)
1637 ((eq org-export-allow-BIND t) t)
1638 ((local-variable-p 'org-export--allow-BIND-local)
1639 org-export--allow-BIND-local)
1640 (t (org-set-local 'org-export--allow-BIND-local
1641 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1643 (defun org-export--install-letbind-maybe ()
1644 "Install the values from #+BIND lines as local variables.
1645 Variables must be installed before in-buffer options are
1646 retrieved."
1647 (let ((case-fold-search t) letbind pair)
1648 (org-with-wide-buffer
1649 (goto-char (point-min))
1650 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1651 (let* ((element (org-element-at-point))
1652 (value (org-element-property :value element)))
1653 (when (and (eq (org-element-type element) 'keyword)
1654 (not (equal value ""))
1655 (org-export--confirm-letbind))
1656 (push (read (format "(%s)" value)) letbind)))))
1657 (dolist (pair (nreverse letbind))
1658 (org-set-local (car pair) (nth 1 pair)))))
1661 ;;;; Tree Properties
1663 ;; Tree properties are information extracted from parse tree. They
1664 ;; are initialized at the beginning of the transcoding process by
1665 ;; `org-export-collect-tree-properties'.
1667 ;; Dedicated functions focus on computing the value of specific tree
1668 ;; properties during initialization. Thus,
1669 ;; `org-export--populate-ignore-list' lists elements and objects that
1670 ;; should be skipped during export, `org-export--get-min-level' gets
1671 ;; the minimal exportable level, used as a basis to compute relative
1672 ;; level for headlines. Eventually
1673 ;; `org-export--collect-headline-numbering' builds an alist between
1674 ;; headlines and their numbering.
1676 (defun org-export-collect-tree-properties (data info)
1677 "Extract tree properties from parse tree.
1679 DATA is the parse tree from which information is retrieved. INFO
1680 is a list holding export options.
1682 Following tree properties are set or updated:
1684 `:exported-data' Hash table used to memoize results from
1685 `org-export-data'.
1687 `:footnote-definition-alist' List of footnotes definitions in
1688 original buffer and current parse tree.
1690 `:headline-offset' Offset between true level of headlines and
1691 local level. An offset of -1 means an headline
1692 of level 2 should be considered as a level
1693 1 headline in the context.
1695 `:headline-numbering' Alist of all headlines as key an the
1696 associated numbering as value.
1698 `:ignore-list' List of elements that should be ignored during
1699 export.
1701 `:target-list' List of all targets in the parse tree.
1703 Return updated plist."
1704 ;; Install the parse tree in the communication channel, in order to
1705 ;; use `org-export-get-genealogy' and al.
1706 (setq info (plist-put info :parse-tree data))
1707 ;; Get the list of elements and objects to ignore, and put it into
1708 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1709 ;; been done during parse tree filtering.
1710 (setq info
1711 (plist-put info
1712 :ignore-list
1713 (append (org-export--populate-ignore-list data info)
1714 (plist-get info :ignore-list))))
1715 ;; Compute `:headline-offset' in order to be able to use
1716 ;; `org-export-get-relative-level'.
1717 (setq info
1718 (plist-put info
1719 :headline-offset
1720 (- 1 (org-export--get-min-level data info))))
1721 ;; Update footnotes definitions list with definitions in parse tree.
1722 ;; This is required since buffer expansion might have modified
1723 ;; boundaries of footnote definitions contained in the parse tree.
1724 ;; This way, definitions in `footnote-definition-alist' are bound to
1725 ;; match those in the parse tree.
1726 (let ((defs (plist-get info :footnote-definition-alist)))
1727 (org-element-map
1728 data 'footnote-definition
1729 (lambda (fn)
1730 (push (cons (org-element-property :label fn)
1731 `(org-data nil ,@(org-element-contents fn)))
1732 defs)))
1733 (setq info (plist-put info :footnote-definition-alist defs)))
1734 ;; Properties order doesn't matter: get the rest of the tree
1735 ;; properties.
1736 (nconc
1737 `(:target-list
1738 ,(org-element-map
1739 data '(keyword target)
1740 (lambda (blob)
1741 (when (or (eq (org-element-type blob) 'target)
1742 (string= (org-element-property :key blob) "TARGET"))
1743 blob)) info)
1744 :headline-numbering ,(org-export--collect-headline-numbering data info)
1745 :exported-data ,(make-hash-table :test 'eq :size 4001))
1746 info))
1748 (defun org-export--get-min-level (data options)
1749 "Return minimum exportable headline's level in DATA.
1750 DATA is parsed tree as returned by `org-element-parse-buffer'.
1751 OPTIONS is a plist holding export options."
1752 (catch 'exit
1753 (let ((min-level 10000))
1754 (mapc
1755 (lambda (blob)
1756 (when (and (eq (org-element-type blob) 'headline)
1757 (not (memq blob (plist-get options :ignore-list))))
1758 (setq min-level
1759 (min (org-element-property :level blob) min-level)))
1760 (when (= min-level 1) (throw 'exit 1)))
1761 (org-element-contents data))
1762 ;; If no headline was found, for the sake of consistency, set
1763 ;; minimum level to 1 nonetheless.
1764 (if (= min-level 10000) 1 min-level))))
1766 (defun org-export--collect-headline-numbering (data options)
1767 "Return numbering of all exportable headlines in a parse tree.
1769 DATA is the parse tree. OPTIONS is the plist holding export
1770 options.
1772 Return an alist whose key is an headline and value is its
1773 associated numbering \(in the shape of a list of numbers\)."
1774 (let ((numbering (make-vector org-export-max-depth 0)))
1775 (org-element-map
1776 data
1777 'headline
1778 (lambda (headline)
1779 (let ((relative-level
1780 (1- (org-export-get-relative-level headline options))))
1781 (cons
1782 headline
1783 (loop for n across numbering
1784 for idx from 0 to org-export-max-depth
1785 when (< idx relative-level) collect n
1786 when (= idx relative-level) collect (aset numbering idx (1+ n))
1787 when (> idx relative-level) do (aset numbering idx 0)))))
1788 options)))
1790 (defun org-export--populate-ignore-list (data options)
1791 "Return list of elements and objects to ignore during export.
1792 DATA is the parse tree to traverse. OPTIONS is the plist holding
1793 export options."
1794 (let* (ignore
1795 walk-data
1796 ;; First find trees containing a select tag, if any.
1797 (selected (org-export--selected-trees data options))
1798 (walk-data
1799 (lambda (data)
1800 ;; Collect ignored elements or objects into IGNORE-LIST.
1801 (let ((type (org-element-type data)))
1802 (if (org-export--skip-p data options selected) (push data ignore)
1803 (if (and (eq type 'headline)
1804 (eq (plist-get options :with-archived-trees) 'headline)
1805 (org-element-property :archivedp data))
1806 ;; If headline is archived but tree below has
1807 ;; to be skipped, add it to ignore list.
1808 (mapc (lambda (e) (push e ignore))
1809 (org-element-contents data))
1810 ;; Move into secondary string, if any.
1811 (let ((sec-prop
1812 (cdr (assq type org-element-secondary-value-alist))))
1813 (when sec-prop
1814 (mapc walk-data (org-element-property sec-prop data))))
1815 ;; Move into recursive objects/elements.
1816 (mapc walk-data (org-element-contents data))))))))
1817 ;; Main call.
1818 (funcall walk-data data)
1819 ;; Return value.
1820 ignore))
1822 (defun org-export--selected-trees (data info)
1823 "Return list of headlines containing a select tag in their tree.
1824 DATA is parsed data as returned by `org-element-parse-buffer'.
1825 INFO is a plist holding export options."
1826 (let* (selected-trees
1827 walk-data ; for byte-compiler.
1828 (walk-data
1829 (function
1830 (lambda (data genealogy)
1831 (case (org-element-type data)
1832 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1833 (org-element-contents data)))
1834 (headline
1835 (let ((tags (org-element-property :tags data)))
1836 (if (loop for tag in (plist-get info :select-tags)
1837 thereis (member tag tags))
1838 ;; When a select tag is found, mark full
1839 ;; genealogy and every headline within the tree
1840 ;; as acceptable.
1841 (setq selected-trees
1842 (append
1843 genealogy
1844 (org-element-map data 'headline 'identity)
1845 selected-trees))
1846 ;; Else, continue searching in tree, recursively.
1847 (mapc
1848 (lambda (el) (funcall walk-data el (cons data genealogy)))
1849 (org-element-contents data))))))))))
1850 (funcall walk-data data nil) selected-trees))
1852 (defun org-export--skip-p (blob options selected)
1853 "Non-nil when element or object BLOB should be skipped during export.
1854 OPTIONS is the plist holding export options. SELECTED, when
1855 non-nil, is a list of headlines belonging to a tree with a select
1856 tag."
1857 (case (org-element-type blob)
1858 (clock (not (plist-get options :with-clocks)))
1859 (drawer
1860 (or (not (plist-get options :with-drawers))
1861 (and (consp (plist-get options :with-drawers))
1862 (not (member (org-element-property :drawer-name blob)
1863 (plist-get options :with-drawers))))))
1864 (headline
1865 (let ((with-tasks (plist-get options :with-tasks))
1866 (todo (org-element-property :todo-keyword blob))
1867 (todo-type (org-element-property :todo-type blob))
1868 (archived (plist-get options :with-archived-trees))
1869 (tags (org-element-property :tags blob)))
1871 ;; Ignore subtrees with an exclude tag.
1872 (loop for k in (plist-get options :exclude-tags)
1873 thereis (member k tags))
1874 ;; When a select tag is present in the buffer, ignore any tree
1875 ;; without it.
1876 (and selected (not (memq blob selected)))
1877 ;; Ignore commented sub-trees.
1878 (org-element-property :commentedp blob)
1879 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1880 (and (not archived) (org-element-property :archivedp blob))
1881 ;; Ignore tasks, if specified by `:with-tasks' property.
1882 (and todo
1883 (or (not with-tasks)
1884 (and (memq with-tasks '(todo done))
1885 (not (eq todo-type with-tasks)))
1886 (and (consp with-tasks) (not (member todo with-tasks))))))))
1887 (inlinetask (not (plist-get options :with-inlinetasks)))
1888 (planning (not (plist-get options :with-plannings)))
1889 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1890 (table-cell
1891 (and (org-export-table-has-special-column-p
1892 (org-export-get-parent-table blob))
1893 (not (org-export-get-previous-element blob options))))
1894 (table-row (org-export-table-row-is-special-p blob options))
1895 (timestamp
1896 (case (plist-get options :with-timestamps)
1897 ;; No timestamp allowed.
1898 ('nil t)
1899 ;; Only active timestamps allowed and the current one isn't
1900 ;; active.
1901 (active
1902 (not (memq (org-element-property :type blob)
1903 '(active active-range))))
1904 ;; Only inactive timestamps allowed and the current one isn't
1905 ;; inactive.
1906 (inactive
1907 (not (memq (org-element-property :type blob)
1908 '(inactive inactive-range))))))))
1912 ;;; The Transcoder
1914 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1915 ;; `org-element-parse-buffer') and transcodes it into a specified
1916 ;; back-end output. It takes care of filtering out elements or
1917 ;; objects according to export options and organizing the output blank
1918 ;; lines and white space are preserved. The function memoizes its
1919 ;; results, so it is cheap to call it within translators.
1921 ;; Internally, three functions handle the filtering of objects and
1922 ;; elements during the export. In particular,
1923 ;; `org-export-ignore-element' marks an element or object so future
1924 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1925 ;; elements or objects should be seen as real Org syntax and
1926 ;; `org-export-expand' transforms the others back into their original
1927 ;; shape
1929 ;; `org-export-transcoder' is an accessor returning appropriate
1930 ;; translator function for a given element or object.
1932 (defun org-export-transcoder (blob info)
1933 "Return appropriate transcoder for BLOB.
1934 INFO is a plist containing export directives."
1935 (let ((type (org-element-type blob)))
1936 ;; Return contents only for complete parse trees.
1937 (if (eq type 'org-data) (lambda (blob contents info) contents)
1938 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1939 (and (functionp transcoder) transcoder)))))
1941 (defun org-export-data (data info)
1942 "Convert DATA into current back-end format.
1944 DATA is a parse tree, an element or an object or a secondary
1945 string. INFO is a plist holding export options.
1947 Return transcoded string."
1948 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1949 (if (not (eq memo 'no-memo)) memo
1950 (let* ((type (org-element-type data))
1951 (results
1952 (cond
1953 ;; Ignored element/object.
1954 ((memq data (plist-get info :ignore-list)) nil)
1955 ;; Plain text.
1956 ((eq type 'plain-text)
1957 (org-export-filter-apply-functions
1958 (plist-get info :filter-plain-text)
1959 (let ((transcoder (org-export-transcoder data info)))
1960 (if transcoder (funcall transcoder data info) data))
1961 info))
1962 ;; Uninterpreted element/object: change it back to Org
1963 ;; syntax and export again resulting raw string.
1964 ((not (org-export--interpret-p data info))
1965 (org-export-data
1966 (org-export-expand
1967 data
1968 (mapconcat (lambda (blob) (org-export-data blob info))
1969 (org-element-contents data)
1970 ""))
1971 info))
1972 ;; Secondary string.
1973 ((not type)
1974 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1975 ;; Element/Object without contents or, as a special case,
1976 ;; headline with archive tag and archived trees restricted
1977 ;; to title only.
1978 ((or (not (org-element-contents data))
1979 (and (eq type 'headline)
1980 (eq (plist-get info :with-archived-trees) 'headline)
1981 (org-element-property :archivedp data)))
1982 (let ((transcoder (org-export-transcoder data info)))
1983 (and (functionp transcoder)
1984 (funcall transcoder data nil info))))
1985 ;; Element/Object with contents.
1987 (let ((transcoder (org-export-transcoder data info)))
1988 (when transcoder
1989 (let* ((greaterp (memq type org-element-greater-elements))
1990 (objectp
1991 (and (not greaterp)
1992 (memq type org-element-recursive-objects)))
1993 (contents
1994 (mapconcat
1995 (lambda (element) (org-export-data element info))
1996 (org-element-contents
1997 (if (or greaterp objectp) data
1998 ;; Elements directly containing objects
1999 ;; must have their indentation normalized
2000 ;; first.
2001 (org-element-normalize-contents
2002 data
2003 ;; When normalizing contents of the first
2004 ;; paragraph in an item or a footnote
2005 ;; definition, ignore first line's
2006 ;; indentation: there is none and it
2007 ;; might be misleading.
2008 (when (eq type 'paragraph)
2009 (let ((parent (org-export-get-parent data)))
2010 (and
2011 (eq (car (org-element-contents parent))
2012 data)
2013 (memq (org-element-type parent)
2014 '(footnote-definition item))))))))
2015 "")))
2016 (funcall transcoder data
2017 (if (not greaterp) contents
2018 (org-element-normalize-string contents))
2019 info))))))))
2020 ;; Final result will be memoized before being returned.
2021 (puthash
2022 data
2023 (cond
2024 ((not results) nil)
2025 ((memq type '(org-data plain-text nil)) results)
2026 ;; Append the same white space between elements or objects as in
2027 ;; the original buffer, and call appropriate filters.
2029 (let ((results
2030 (org-export-filter-apply-functions
2031 (plist-get info (intern (format ":filter-%s" type)))
2032 (let ((post-blank (or (org-element-property :post-blank data)
2033 0)))
2034 (if (memq type org-element-all-elements)
2035 (concat (org-element-normalize-string results)
2036 (make-string post-blank ?\n))
2037 (concat results (make-string post-blank ? ))))
2038 info)))
2039 results)))
2040 (plist-get info :exported-data))))))
2042 (defun org-export--interpret-p (blob info)
2043 "Non-nil if element or object BLOB should be interpreted as Org syntax.
2044 Check is done according to export options INFO, stored as
2045 a plist."
2046 (case (org-element-type blob)
2047 ;; ... entities...
2048 (entity (plist-get info :with-entities))
2049 ;; ... emphasis...
2050 ((bold italic strike-through underline)
2051 (plist-get info :with-emphasize))
2052 ;; ... fixed-width areas.
2053 (fixed-width (plist-get info :with-fixed-width))
2054 ;; ... footnotes...
2055 ((footnote-definition footnote-reference)
2056 (plist-get info :with-footnotes))
2057 ;; ... sub/superscripts...
2058 ((subscript superscript)
2059 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2060 (if (eq sub/super-p '{})
2061 (org-element-property :use-brackets-p blob)
2062 sub/super-p)))
2063 ;; ... tables...
2064 (table (plist-get info :with-tables))
2065 (otherwise t)))
2067 (defun org-export-expand (blob contents)
2068 "Expand a parsed element or object to its original state.
2069 BLOB is either an element or an object. CONTENTS is its
2070 contents, as a string or nil."
2071 (funcall
2072 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2073 blob contents))
2075 (defun org-export-ignore-element (element info)
2076 "Add ELEMENT to `:ignore-list' in INFO.
2078 Any element in `:ignore-list' will be skipped when using
2079 `org-element-map'. INFO is modified by side effects."
2080 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2084 ;;; The Filter System
2086 ;; Filters allow end-users to tweak easily the transcoded output.
2087 ;; They are the functional counterpart of hooks, as every filter in
2088 ;; a set is applied to the return value of the previous one.
2090 ;; Every set is back-end agnostic. Although, a filter is always
2091 ;; called, in addition to the string it applies to, with the back-end
2092 ;; used as argument, so it's easy for the end-user to add back-end
2093 ;; specific filters in the set. The communication channel, as
2094 ;; a plist, is required as the third argument.
2096 ;; From the developer side, filters sets can be installed in the
2097 ;; process with the help of `org-export-define-backend', which
2098 ;; internally stores filters as an alist. Each association has a key
2099 ;; among the following symbols and a function or a list of functions
2100 ;; as value.
2102 ;; - `:filter-parse-tree' applies directly on the complete parsed
2103 ;; tree. It's the only filters set that doesn't apply to a string.
2104 ;; Users can set it through `org-export-filter-parse-tree-functions'
2105 ;; variable.
2107 ;; - `:filter-final-output' applies to the final transcoded string.
2108 ;; Users can set it with `org-export-filter-final-output-functions'
2109 ;; variable
2111 ;; - `:filter-plain-text' applies to any string not recognized as Org
2112 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2113 ;; configure it.
2115 ;; - `:filter-TYPE' applies on the string returned after an element or
2116 ;; object of type TYPE has been transcoded. An user can modify
2117 ;; `org-export-filter-TYPE-functions'
2119 ;; All filters sets are applied with
2120 ;; `org-export-filter-apply-functions' function. Filters in a set are
2121 ;; applied in a LIFO fashion. It allows developers to be sure that
2122 ;; their filters will be applied first.
2124 ;; Filters properties are installed in communication channel with
2125 ;; `org-export-install-filters' function.
2127 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2128 ;; `org-export-before-parsing-hook') are run at the beginning of the
2129 ;; export process and just before parsing to allow for heavy structure
2130 ;; modifications.
2133 ;;;; Hooks
2135 (defvar org-export-before-processing-hook nil
2136 "Hook run at the beginning of the export process.
2138 This is run before include keywords and macros are expanded and
2139 Babel code blocks executed, on a copy of the original buffer
2140 being exported. Visibility and narrowing are preserved. Point
2141 is at the beginning of the buffer.
2143 Every function in this hook will be called with one argument: the
2144 back-end currently used, as a symbol.")
2146 (defvar org-export-before-parsing-hook nil
2147 "Hook run before parsing an export buffer.
2149 This is run after include keywords and macros have been expanded
2150 and Babel code blocks executed, on a copy of the original buffer
2151 being exported. Visibility and narrowing are preserved. Point
2152 is at the beginning of the buffer.
2154 Every function in this hook will be called with one argument: the
2155 back-end currently used, as a symbol.")
2158 ;;;; Special Filters
2160 (defvar org-export-filter-parse-tree-functions nil
2161 "List of functions applied to the parsed tree.
2162 Each filter is called with three arguments: the parse tree, as
2163 returned by `org-element-parse-buffer', the back-end, as
2164 a symbol, and the communication channel, as a plist. It must
2165 return the modified parse tree to transcode.")
2167 (defvar org-export-filter-final-output-functions nil
2168 "List of functions applied to the transcoded string.
2169 Each filter is called with three arguments: the full transcoded
2170 string, the back-end, as a symbol, and the communication channel,
2171 as a plist. It must return a string that will be used as the
2172 final export output.")
2174 (defvar org-export-filter-plain-text-functions nil
2175 "List of functions applied to plain text.
2176 Each filter is called with three arguments: a string which
2177 contains no Org syntax, the back-end, as a symbol, and the
2178 communication channel, as a plist. It must return a string or
2179 nil.")
2182 ;;;; Elements Filters
2184 (defvar org-export-filter-babel-call-functions nil
2185 "List of functions applied to a transcoded babel-call.
2186 Each filter is called with three arguments: the transcoded data,
2187 as a string, the back-end, as a symbol, and the communication
2188 channel, as a plist. It must return a string or nil.")
2190 (defvar org-export-filter-center-block-functions nil
2191 "List of functions applied to a transcoded center block.
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-clock-functions nil
2197 "List of functions applied to a transcoded clock.
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-comment-functions nil
2203 "List of functions applied to a transcoded comment.
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-block-functions nil
2209 "List of functions applied to a transcoded comment-block.
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-diary-sexp-functions nil
2215 "List of functions applied to a transcoded diary-sexp.
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-drawer-functions nil
2221 "List of functions applied to a transcoded drawer.
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-dynamic-block-functions nil
2227 "List of functions applied to a transcoded dynamic-block.
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-example-block-functions nil
2233 "List of functions applied to a transcoded example-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-export-block-functions nil
2239 "List of functions applied to a transcoded export-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-fixed-width-functions nil
2245 "List of functions applied to a transcoded fixed-width.
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-footnote-definition-functions nil
2251 "List of functions applied to a transcoded footnote-definition.
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-headline-functions nil
2257 "List of functions applied to a transcoded headline.
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-horizontal-rule-functions nil
2263 "List of functions applied to a transcoded horizontal-rule.
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-inlinetask-functions nil
2269 "List of functions applied to a transcoded inlinetask.
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-item-functions nil
2275 "List of functions applied to a transcoded item.
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-keyword-functions nil
2281 "List of functions applied to a transcoded keyword.
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-latex-environment-functions nil
2287 "List of functions applied to a transcoded latex-environment.
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-node-property-functions nil
2293 "List of functions applied to a transcoded node-property.
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-paragraph-functions nil
2299 "List of functions applied to a transcoded paragraph.
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-plain-list-functions nil
2305 "List of functions applied to a transcoded plain-list.
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-planning-functions nil
2311 "List of functions applied to a transcoded planning.
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-property-drawer-functions nil
2317 "List of functions applied to a transcoded property-drawer.
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-quote-block-functions nil
2323 "List of functions applied to a transcoded quote block.
2324 Each filter is called with three arguments: the transcoded quote
2325 data, as a string, the back-end, as a symbol, and the
2326 communication channel, as a plist. It must return a string or
2327 nil.")
2329 (defvar org-export-filter-quote-section-functions nil
2330 "List of functions applied to a transcoded quote-section.
2331 Each filter is called with three arguments: the transcoded data,
2332 as a string, the back-end, as a symbol, and the communication
2333 channel, as a plist. It must return a string or nil.")
2335 (defvar org-export-filter-section-functions nil
2336 "List of functions applied to a transcoded 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-special-block-functions nil
2342 "List of functions applied to a transcoded special block.
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-src-block-functions nil
2348 "List of functions applied to a transcoded src-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-table-functions nil
2354 "List of functions applied to a transcoded table.
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-cell-functions nil
2360 "List of functions applied to a transcoded table-cell.
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-row-functions nil
2366 "List of functions applied to a transcoded table-row.
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-verse-block-functions nil
2372 "List of functions applied to a transcoded verse block.
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.")
2378 ;;;; Objects Filters
2380 (defvar org-export-filter-bold-functions nil
2381 "List of functions applied to transcoded bold text.
2382 Each filter is called with three arguments: the transcoded data,
2383 as a string, the back-end, as a symbol, and the communication
2384 channel, as a plist. It must return a string or nil.")
2386 (defvar org-export-filter-code-functions nil
2387 "List of functions applied to transcoded code 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-entity-functions nil
2393 "List of functions applied to a transcoded entity.
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-export-snippet-functions nil
2399 "List of functions applied to a transcoded export-snippet.
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-footnote-reference-functions nil
2405 "List of functions applied to a transcoded footnote-reference.
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-inline-babel-call-functions nil
2411 "List of functions applied to a transcoded inline-babel-call.
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-src-block-functions nil
2417 "List of functions applied to a transcoded inline-src-block.
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-italic-functions nil
2423 "List of functions applied to transcoded italic text.
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-latex-fragment-functions nil
2429 "List of functions applied to a transcoded latex-fragment.
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-line-break-functions nil
2435 "List of functions applied to a transcoded line-break.
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-link-functions nil
2441 "List of functions applied to a transcoded link.
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-macro-functions nil
2447 "List of functions applied to a transcoded macro.
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-radio-target-functions nil
2453 "List of functions applied to a transcoded radio-target.
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-statistics-cookie-functions nil
2459 "List of functions applied to a transcoded statistics-cookie.
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-strike-through-functions nil
2465 "List of functions applied to transcoded strike-through text.
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-subscript-functions nil
2471 "List of functions applied to a transcoded subscript.
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-superscript-functions nil
2477 "List of functions applied to a transcoded superscript.
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-target-functions nil
2483 "List of functions applied to a transcoded target.
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-timestamp-functions nil
2489 "List of functions applied to a transcoded timestamp.
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-underline-functions nil
2495 "List of functions applied to transcoded underline text.
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-verbatim-functions nil
2501 "List of functions applied to transcoded verbatim 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.")
2507 ;;;; Filters Tools
2509 ;; Internal function `org-export-install-filters' installs filters
2510 ;; hard-coded in back-ends (developer filters) and filters from global
2511 ;; variables (user filters) in the communication channel.
2513 ;; Internal function `org-export-filter-apply-functions' takes care
2514 ;; about applying each filter in order to a given data. It ignores
2515 ;; filters returning a nil value but stops whenever a filter returns
2516 ;; an empty string.
2518 (defun org-export-filter-apply-functions (filters value info)
2519 "Call every function in FILTERS.
2521 Functions are called with arguments VALUE, current export
2522 back-end and INFO. A function returning a nil value will be
2523 skipped. If it returns the empty string, the process ends and
2524 VALUE is ignored.
2526 Call is done in a LIFO fashion, to be sure that developer
2527 specified filters, if any, are called first."
2528 (catch 'exit
2529 (dolist (filter filters value)
2530 (let ((result (funcall filter value (plist-get info :back-end) info)))
2531 (cond ((not result) value)
2532 ((equal value "") (throw 'exit nil))
2533 (t (setq value result)))))))
2535 (defun org-export-install-filters (info)
2536 "Install filters properties in communication channel.
2538 INFO is a plist containing the current communication channel.
2540 Return the updated communication channel."
2541 (let (plist)
2542 ;; Install user defined filters with `org-export-filters-alist'.
2543 (mapc (lambda (p)
2544 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2545 org-export-filters-alist)
2546 ;; Prepend back-end specific filters to that list.
2547 (mapc (lambda (p)
2548 ;; Single values get consed, lists are prepended.
2549 (let ((key (car p)) (value (cdr p)))
2550 (when value
2551 (setq plist
2552 (plist-put
2553 plist key
2554 (if (atom value) (cons value (plist-get plist key))
2555 (append value (plist-get plist key))))))))
2556 (org-export-backend-filters (plist-get info :back-end)))
2557 ;; Return new communication channel.
2558 (org-combine-plists info plist)))
2562 ;;; Core functions
2564 ;; This is the room for the main function, `org-export-as', along with
2565 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2566 ;; They differ only by the way they output the resulting code.
2568 ;; `org-export-output-file-name' is an auxiliary function meant to be
2569 ;; used with `org-export-to-file'. With a given extension, it tries
2570 ;; to provide a canonical file name to write export output to.
2572 ;; Note that `org-export-as' doesn't really parse the current buffer,
2573 ;; but a copy of it (with the same buffer-local variables and
2574 ;; visibility), where macros and include keywords are expanded and
2575 ;; Babel blocks are executed, if appropriate.
2576 ;; `org-export-with-buffer-copy' macro prepares that copy.
2578 ;; File inclusion is taken care of by
2579 ;; `org-export-expand-include-keyword' and
2580 ;; `org-export--prepare-file-contents'. Structure wise, including
2581 ;; a whole Org file in a buffer often makes little sense. For
2582 ;; example, if the file contains an headline and the include keyword
2583 ;; was within an item, the item should contain the headline. That's
2584 ;; why file inclusion should be done before any structure can be
2585 ;; associated to the file, that is before parsing.
2587 (defun org-export-as
2588 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2589 "Transcode current Org buffer into BACKEND code.
2591 If narrowing is active in the current buffer, only transcode its
2592 narrowed part.
2594 If a region is active, transcode that region.
2596 When optional argument SUBTREEP is non-nil, transcode the
2597 sub-tree at point, extracting information from the headline
2598 properties first.
2600 When optional argument VISIBLE-ONLY is non-nil, don't export
2601 contents of hidden elements.
2603 When optional argument BODY-ONLY is non-nil, only return body
2604 code, without preamble nor postamble.
2606 Optional argument EXT-PLIST, when provided, is a property list
2607 with external parameters overriding Org default settings, but
2608 still inferior to file-local settings.
2610 Optional argument NOEXPAND, when non-nil, prevents included files
2611 to be expanded and Babel code to be executed.
2613 Return code as a string."
2614 ;; Barf if BACKEND isn't registered.
2615 (org-export-barf-if-invalid-backend backend)
2616 (save-excursion
2617 (save-restriction
2618 ;; Narrow buffer to an appropriate region or subtree for
2619 ;; parsing. If parsing subtree, be sure to remove main headline
2620 ;; too.
2621 (cond ((org-region-active-p)
2622 (narrow-to-region (region-beginning) (region-end)))
2623 (subtreep
2624 (org-narrow-to-subtree)
2625 (goto-char (point-min))
2626 (forward-line)
2627 (narrow-to-region (point) (point-max))))
2628 ;; Initialize communication channel with original buffer
2629 ;; attributes, unavailable in its copy.
2630 (let ((info (org-export--get-buffer-attributes)) tree)
2631 (org-export-with-buffer-copy
2632 ;; Run first hook with current back-end as argument.
2633 (run-hook-with-args 'org-export-before-processing-hook backend)
2634 ;; Update communication channel and get parse tree. Buffer
2635 ;; isn't parsed directly. Instead, a temporary copy is
2636 ;; created, where include keywords, macros are expanded and
2637 ;; code blocks are evaluated.
2638 (unless noexpand
2639 (org-export-expand-include-keyword)
2640 ;; Update macro templates since #+INCLUDE keywords might
2641 ;; have added some new ones.
2642 (org-macro-initialize-templates)
2643 (org-macro-replace-all org-macro-templates)
2644 (org-export-execute-babel-code))
2645 ;; Update radio targets since keyword inclusion might have
2646 ;; added some more.
2647 (org-update-radio-target-regexp)
2648 ;; Run last hook with current back-end as argument.
2649 (goto-char (point-min))
2650 (run-hook-with-args 'org-export-before-parsing-hook backend)
2651 ;; Update communication channel with environment. Also
2652 ;; install user's and developer's filters.
2653 (setq info
2654 (org-export-install-filters
2655 (org-combine-plists
2656 info (org-export-get-environment backend subtreep ext-plist))))
2657 ;; Expand export-specific set of macros: {{{author}}},
2658 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2659 ;; once regular macros have been expanded, since document
2660 ;; keywords may contain one of them.
2661 (unless noexpand
2662 (org-macro-replace-all
2663 (list (cons "author"
2664 (org-element-interpret-data (plist-get info :author)))
2665 (cons "date"
2666 (org-element-interpret-data (plist-get info :date)))
2667 ;; EMAIL is not a parsed keyword: store it as-is.
2668 (cons "email" (or (plist-get info :email) ""))
2669 (cons "title"
2670 (org-element-interpret-data (plist-get info :title))))))
2671 ;; Eventually parse buffer. Call parse-tree filters to get
2672 ;; the final tree.
2673 (setq tree
2674 (org-export-filter-apply-functions
2675 (plist-get info :filter-parse-tree)
2676 (org-element-parse-buffer nil visible-only) info)))
2677 ;; Now tree is complete, compute its properties and add them
2678 ;; to communication channel.
2679 (setq info
2680 (org-combine-plists
2681 info (org-export-collect-tree-properties tree info)))
2682 ;; Eventually transcode TREE. Wrap the resulting string into
2683 ;; a template, if required. Finally call final-output filter.
2684 (let* ((body (org-element-normalize-string
2685 (or (org-export-data tree info) "")))
2686 (template (cdr (assq 'template
2687 (plist-get info :translate-alist))))
2688 ;; Remove all text properties since they cannot be
2689 ;; retrieved from an external process.
2690 (output (org-no-properties
2691 (org-export-filter-apply-functions
2692 (plist-get info :filter-final-output)
2693 (if (or (not (functionp template)) body-only) body
2694 (funcall template body info))
2695 info))))
2696 ;; Maybe add final OUTPUT to kill ring, then return it.
2697 (when (and org-export-copy-to-kill-ring (org-string-nw-p output))
2698 (org-kill-new output))
2699 output)))))
2701 (defun org-export-to-buffer
2702 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2703 "Call `org-export-as' with output to a specified buffer.
2705 BACKEND is the back-end used for transcoding, as a symbol.
2707 BUFFER is the output buffer. If it already exists, it will be
2708 erased first, otherwise, it will be created.
2710 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2711 and NOEXPAND are similar to those used in `org-export-as', which
2712 see.
2714 Return buffer."
2715 (let ((out (org-export-as
2716 backend subtreep visible-only body-only ext-plist noexpand))
2717 (buffer (get-buffer-create buffer)))
2718 (with-current-buffer buffer
2719 (erase-buffer)
2720 (insert out)
2721 (goto-char (point-min)))
2722 buffer))
2724 (defun org-export-to-file
2725 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2726 "Call `org-export-as' with output to a specified file.
2728 BACKEND is the back-end used for transcoding, as a symbol. FILE
2729 is the name of the output file, as a string.
2731 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2732 and NOEXPAND are similar to those used in `org-export-as', which
2733 see.
2735 Return output file's name."
2736 ;; Checks for FILE permissions. `write-file' would do the same, but
2737 ;; we'd rather avoid needless transcoding of parse tree.
2738 (unless (file-writable-p file) (error "Output file not writable"))
2739 ;; Insert contents to a temporary buffer and write it to FILE.
2740 (let ((out (org-export-as
2741 backend subtreep visible-only body-only ext-plist noexpand)))
2742 (with-temp-buffer
2743 (insert out)
2744 (let ((coding-system-for-write org-export-coding-system))
2745 (write-file file))))
2746 ;; Return full path.
2747 file)
2749 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2750 "Return output file's name according to buffer specifications.
2752 EXTENSION is a string representing the output file extension,
2753 with the leading dot.
2755 With a non-nil optional argument SUBTREEP, try to determine
2756 output file's name by looking for \"EXPORT_FILE_NAME\" property
2757 of subtree at point.
2759 When optional argument PUB-DIR is set, use it as the publishing
2760 directory.
2762 When optional argument VISIBLE-ONLY is non-nil, don't export
2763 contents of hidden elements.
2765 Return file name as a string, or nil if it couldn't be
2766 determined."
2767 (let ((base-name
2768 ;; File name may come from EXPORT_FILE_NAME subtree property,
2769 ;; assuming point is at beginning of said sub-tree.
2770 (file-name-sans-extension
2771 (or (and subtreep
2772 (org-entry-get
2773 (save-excursion
2774 (ignore-errors (org-back-to-heading) (point)))
2775 "EXPORT_FILE_NAME" t))
2776 ;; File name may be extracted from buffer's associated
2777 ;; file, if any.
2778 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
2779 (and visited-file (file-name-nondirectory visited-file)))
2780 ;; Can't determine file name on our own: Ask user.
2781 (let ((read-file-name-function
2782 (and org-completion-use-ido 'ido-read-file-name)))
2783 (read-file-name
2784 "Output file: " pub-dir nil nil nil
2785 (lambda (name)
2786 (string= (file-name-extension name t) extension))))))))
2787 ;; Build file name. Enforce EXTENSION over whatever user may have
2788 ;; come up with. PUB-DIR, if defined, always has precedence over
2789 ;; any provided path.
2790 (cond
2791 (pub-dir
2792 (concat (file-name-as-directory pub-dir)
2793 (file-name-nondirectory base-name)
2794 extension))
2795 ((file-name-absolute-p base-name) (concat base-name extension))
2796 (t (concat (file-name-as-directory ".") base-name extension)))))
2798 (defun org-export-copy-buffer ()
2799 "Return a copy of the current buffer.
2800 The copy preserves Org buffer-local variables, visibility and
2801 narrowing."
2802 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2803 (new-buf (generate-new-buffer (buffer-name))))
2804 (with-current-buffer new-buf
2805 (funcall copy-buffer-fun)
2806 (set-buffer-modified-p nil))
2807 new-buf))
2809 (defmacro org-export-with-buffer-copy (&rest body)
2810 "Apply BODY in a copy of the current buffer.
2811 The copy preserves local variables, visibility and contents of
2812 the original buffer. Point is at the beginning of the buffer
2813 when BODY is applied."
2814 (declare (debug t))
2815 (org-with-gensyms (buf-copy)
2816 `(let ((,buf-copy (org-export-copy-buffer)))
2817 (unwind-protect
2818 (with-current-buffer ,buf-copy
2819 (goto-char (point-min))
2820 (progn ,@body))
2821 (and (buffer-live-p ,buf-copy)
2822 ;; Kill copy without confirmation.
2823 (progn (with-current-buffer ,buf-copy
2824 (restore-buffer-modified-p nil))
2825 (kill-buffer ,buf-copy)))))))
2827 (defun org-export--generate-copy-script (buffer)
2828 "Generate a function duplicating BUFFER.
2830 The copy will preserve local variables, visibility, contents and
2831 narrowing of the original buffer. If a region was active in
2832 BUFFER, contents will be narrowed to that region instead.
2834 The resulting function can be eval'ed at a later time, from
2835 another buffer, effectively cloning the original buffer there."
2836 (with-current-buffer buffer
2837 `(lambda ()
2838 (let ((inhibit-modification-hooks t))
2839 ;; Buffer local variables.
2840 ,@(let (local-vars)
2841 (mapc
2842 (lambda (entry)
2843 (when (consp entry)
2844 (let ((var (car entry))
2845 (val (cdr entry)))
2846 (and (not (eq var 'org-font-lock-keywords))
2847 (or (memq var
2848 '(major-mode default-directory
2849 buffer-file-name outline-level
2850 outline-regexp
2851 buffer-invisibility-spec))
2852 (string-match "^\\(org-\\|orgtbl-\\)"
2853 (symbol-name var)))
2854 ;; Skip unreadable values, as they cannot be
2855 ;; sent to external process.
2856 (or (not val) (ignore-errors (read (format "%S" val))))
2857 (push `(set (make-local-variable (quote ,var))
2858 (quote ,val))
2859 local-vars)))))
2860 (buffer-local-variables (buffer-base-buffer)))
2861 local-vars)
2862 ;; Whole buffer contents.
2863 (insert
2864 ,(org-with-wide-buffer
2865 (buffer-substring-no-properties
2866 (point-min) (point-max))))
2867 ;; Narrowing.
2868 ,(if (org-region-active-p)
2869 `(narrow-to-region ,(region-beginning) ,(region-end))
2870 `(narrow-to-region ,(point-min) ,(point-max)))
2871 ;; Current position of point.
2872 (goto-char ,(point))
2873 ;; Overlays with invisible property.
2874 ,@(let (ov-set)
2875 (mapc
2876 (lambda (ov)
2877 (let ((invis-prop (overlay-get ov 'invisible)))
2878 (when invis-prop
2879 (push `(overlay-put
2880 (make-overlay ,(overlay-start ov)
2881 ,(overlay-end ov))
2882 'invisible (quote ,invis-prop))
2883 ov-set))))
2884 (overlays-in (point-min) (point-max)))
2885 ov-set)))))
2887 (defun org-export-expand-include-keyword (&optional included dir)
2888 "Expand every include keyword in buffer.
2889 Optional argument INCLUDED is a list of included file names along
2890 with their line restriction, when appropriate. It is used to
2891 avoid infinite recursion. Optional argument DIR is the current
2892 working directory. It is used to properly resolve relative
2893 paths."
2894 (let ((case-fold-search t))
2895 (goto-char (point-min))
2896 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t)
2897 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2898 'keyword)
2899 (beginning-of-line)
2900 ;; Extract arguments from keyword's value.
2901 (let* ((value (match-string 1))
2902 (ind (org-get-indentation))
2903 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2904 (prog1 (expand-file-name (match-string 1 value) dir)
2905 (setq value (replace-match "" nil nil value)))))
2906 (lines
2907 (and (string-match
2908 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2909 (prog1 (match-string 1 value)
2910 (setq value (replace-match "" nil nil value)))))
2911 (env (cond ((string-match "\\<example\\>" value) 'example)
2912 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2913 (match-string 1 value))))
2914 ;; Minimal level of included file defaults to the child
2915 ;; level of the current headline, if any, or one. It
2916 ;; only applies is the file is meant to be included as
2917 ;; an Org one.
2918 (minlevel
2919 (and (not env)
2920 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2921 (prog1 (string-to-number (match-string 1 value))
2922 (setq value (replace-match "" nil nil value)))
2923 (let ((cur (org-current-level)))
2924 (if cur (1+ (org-reduced-level cur)) 1))))))
2925 ;; Remove keyword.
2926 (delete-region (point) (progn (forward-line) (point)))
2927 (cond
2928 ((not file) (error "Invalid syntax in INCLUDE keyword"))
2929 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2930 ;; Check if files has already been parsed. Look after
2931 ;; inclusion lines too, as different parts of the same file
2932 ;; can be included too.
2933 ((member (list file lines) included)
2934 (error "Recursive file inclusion: %s" file))
2936 (cond
2937 ((eq env 'example)
2938 (insert
2939 (let ((ind-str (make-string ind ? ))
2940 (contents
2941 (org-escape-code-in-string
2942 (org-export--prepare-file-contents file lines))))
2943 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2944 ind-str contents ind-str))))
2945 ((stringp env)
2946 (insert
2947 (let ((ind-str (make-string ind ? ))
2948 (contents
2949 (org-escape-code-in-string
2950 (org-export--prepare-file-contents file lines))))
2951 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2952 ind-str env contents ind-str))))
2954 (insert
2955 (with-temp-buffer
2956 (org-mode)
2957 (insert
2958 (org-export--prepare-file-contents file lines ind minlevel))
2959 (org-export-expand-include-keyword
2960 (cons (list file lines) included)
2961 (file-name-directory file))
2962 (buffer-string))))))))))))
2964 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2965 "Prepare the contents of FILE for inclusion and return them as a string.
2967 When optional argument LINES is a string specifying a range of
2968 lines, include only those lines.
2970 Optional argument IND, when non-nil, is an integer specifying the
2971 global indentation of returned contents. Since its purpose is to
2972 allow an included file to stay in the same environment it was
2973 created \(i.e. a list item), it doesn't apply past the first
2974 headline encountered.
2976 Optional argument MINLEVEL, when non-nil, is an integer
2977 specifying the level that any top-level headline in the included
2978 file should have."
2979 (with-temp-buffer
2980 (insert-file-contents file)
2981 (when lines
2982 (let* ((lines (split-string lines "-"))
2983 (lbeg (string-to-number (car lines)))
2984 (lend (string-to-number (cadr lines)))
2985 (beg (if (zerop lbeg) (point-min)
2986 (goto-char (point-min))
2987 (forward-line (1- lbeg))
2988 (point)))
2989 (end (if (zerop lend) (point-max)
2990 (goto-char (point-min))
2991 (forward-line (1- lend))
2992 (point))))
2993 (narrow-to-region beg end)))
2994 ;; Remove blank lines at beginning and end of contents. The logic
2995 ;; behind that removal is that blank lines around include keyword
2996 ;; override blank lines in included file.
2997 (goto-char (point-min))
2998 (org-skip-whitespace)
2999 (beginning-of-line)
3000 (delete-region (point-min) (point))
3001 (goto-char (point-max))
3002 (skip-chars-backward " \r\t\n")
3003 (forward-line)
3004 (delete-region (point) (point-max))
3005 ;; If IND is set, preserve indentation of include keyword until
3006 ;; the first headline encountered.
3007 (when ind
3008 (unless (eq major-mode 'org-mode) (org-mode))
3009 (goto-char (point-min))
3010 (let ((ind-str (make-string ind ? )))
3011 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
3012 ;; Do not move footnote definitions out of column 0.
3013 (unless (and (looking-at org-footnote-definition-re)
3014 (eq (org-element-type (org-element-at-point))
3015 'footnote-definition))
3016 (insert ind-str))
3017 (forward-line))))
3018 ;; When MINLEVEL is specified, compute minimal level for headlines
3019 ;; in the file (CUR-MIN), and remove stars to each headline so
3020 ;; that headlines with minimal level have a level of MINLEVEL.
3021 (when minlevel
3022 (unless (eq major-mode 'org-mode) (org-mode))
3023 (org-with-limited-levels
3024 (let ((levels (org-map-entries
3025 (lambda () (org-reduced-level (org-current-level))))))
3026 (when levels
3027 (let ((offset (- minlevel (apply 'min levels))))
3028 (unless (zerop offset)
3029 (when org-odd-levels-only (setq offset (* offset 2)))
3030 ;; Only change stars, don't bother moving whole
3031 ;; sections.
3032 (org-map-entries
3033 (lambda () (if (< offset 0) (delete-char (abs offset))
3034 (insert (make-string offset ?*)))))))))))
3035 (org-element-normalize-string (buffer-string))))
3037 (defun org-export-execute-babel-code ()
3038 "Execute every Babel code in the visible part of current buffer.
3039 This function will return an error if the current buffer is
3040 visiting a file."
3041 ;; Get a pristine copy of current buffer so Babel references can be
3042 ;; properly resolved.
3043 (let ((reference (org-export-copy-buffer)))
3044 (unwind-protect (let ((org-current-export-file reference))
3045 (org-export-blocks-preprocess))
3046 (kill-buffer reference))))
3050 ;;; Tools For Back-Ends
3052 ;; A whole set of tools is available to help build new exporters. Any
3053 ;; function general enough to have its use across many back-ends
3054 ;; should be added here.
3056 ;;;; For Affiliated Keywords
3058 ;; `org-export-read-attribute' reads a property from a given element
3059 ;; as a plist. It can be used to normalize affiliated keywords'
3060 ;; syntax.
3062 ;; Since captions can span over multiple lines and accept dual values,
3063 ;; their internal representation is a bit tricky. Therefore,
3064 ;; `org-export-get-caption' transparently returns a given element's
3065 ;; caption as a secondary string.
3067 (defun org-export-read-attribute (attribute element &optional property)
3068 "Turn ATTRIBUTE property from ELEMENT into a plist.
3070 When optional argument PROPERTY is non-nil, return the value of
3071 that property within attributes.
3073 This function assumes attributes are defined as \":keyword
3074 value\" pairs. It is appropriate for `:attr_html' like
3075 properties."
3076 (let ((attributes
3077 (let ((value (org-element-property attribute element)))
3078 (and value
3079 (read (format "(%s)" (mapconcat 'identity value " ")))))))
3080 (if property (plist-get attributes property) attributes)))
3082 (defun org-export-get-caption (element &optional shortp)
3083 "Return caption from ELEMENT as a secondary string.
3085 When optional argument SHORTP is non-nil, return short caption,
3086 as a secondary string, instead.
3088 Caption lines are separated by a white space."
3089 (let ((full-caption (org-element-property :caption element)) caption)
3090 (dolist (line full-caption (cdr caption))
3091 (let ((cap (funcall (if shortp 'cdr 'car) line)))
3092 (when cap
3093 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
3096 ;;;; For Derived Back-ends
3098 ;; `org-export-with-backend' is a function allowing to locally use
3099 ;; another back-end to transcode some object or element. In a derived
3100 ;; back-end, it may be used as a fall-back function once all specific
3101 ;; cases have been treated.
3103 (defun org-export-with-backend (back-end data &optional contents info)
3104 "Call a transcoder from BACK-END on DATA.
3105 CONTENTS, when non-nil, is the transcoded contents of DATA
3106 element, as a string. INFO, when non-nil, is the communication
3107 channel used for export, as a plist.."
3108 (org-export-barf-if-invalid-backend back-end)
3109 (let ((type (org-element-type data)))
3110 (if (memq type '(nil org-data)) (error "No foreign transcoder available")
3111 (let ((transcoder
3112 (cdr (assq type (org-export-backend-translate-table back-end)))))
3113 (if (functionp transcoder) (funcall transcoder data contents info)
3114 (error "No foreign transcoder available"))))))
3117 ;;;; For Export Snippets
3119 ;; Every export snippet is transmitted to the back-end. Though, the
3120 ;; latter will only retain one type of export-snippet, ignoring
3121 ;; others, based on the former's target back-end. The function
3122 ;; `org-export-snippet-backend' returns that back-end for a given
3123 ;; export-snippet.
3125 (defun org-export-snippet-backend (export-snippet)
3126 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3127 Translation, with `org-export-snippet-translation-alist', is
3128 applied."
3129 (let ((back-end (org-element-property :back-end export-snippet)))
3130 (intern
3131 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3132 back-end))))
3135 ;;;; For Footnotes
3137 ;; `org-export-collect-footnote-definitions' is a tool to list
3138 ;; actually used footnotes definitions in the whole parse tree, or in
3139 ;; an headline, in order to add footnote listings throughout the
3140 ;; transcoded data.
3142 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3143 ;; back-ends, when they need to attach the footnote definition only to
3144 ;; the first occurrence of the corresponding label.
3146 ;; `org-export-get-footnote-definition' and
3147 ;; `org-export-get-footnote-number' provide easier access to
3148 ;; additional information relative to a footnote reference.
3150 (defun org-export-collect-footnote-definitions (data info)
3151 "Return an alist between footnote numbers, labels and definitions.
3153 DATA is the parse tree from which definitions are collected.
3154 INFO is the plist used as a communication channel.
3156 Definitions are sorted by order of references. They either
3157 appear as Org data or as a secondary string for inlined
3158 footnotes. Unreferenced definitions are ignored."
3159 (let* (num-alist
3160 collect-fn ; for byte-compiler.
3161 (collect-fn
3162 (function
3163 (lambda (data)
3164 ;; Collect footnote number, label and definition in DATA.
3165 (org-element-map
3166 data 'footnote-reference
3167 (lambda (fn)
3168 (when (org-export-footnote-first-reference-p fn info)
3169 (let ((def (org-export-get-footnote-definition fn info)))
3170 (push
3171 (list (org-export-get-footnote-number fn info)
3172 (org-element-property :label fn)
3173 def)
3174 num-alist)
3175 ;; Also search in definition for nested footnotes.
3176 (when (eq (org-element-property :type fn) 'standard)
3177 (funcall collect-fn def)))))
3178 ;; Don't enter footnote definitions since it will happen
3179 ;; when their first reference is found.
3180 info nil 'footnote-definition)))))
3181 (funcall collect-fn (plist-get info :parse-tree))
3182 (reverse num-alist)))
3184 (defun org-export-footnote-first-reference-p (footnote-reference info)
3185 "Non-nil when a footnote reference is the first one for its label.
3187 FOOTNOTE-REFERENCE is the footnote reference being considered.
3188 INFO is the plist used as a communication channel."
3189 (let ((label (org-element-property :label footnote-reference)))
3190 ;; Anonymous footnotes are always a first reference.
3191 (if (not label) t
3192 ;; Otherwise, return the first footnote with the same LABEL and
3193 ;; test if it is equal to FOOTNOTE-REFERENCE.
3194 (let* (search-refs ; for byte-compiler.
3195 (search-refs
3196 (function
3197 (lambda (data)
3198 (org-element-map
3199 data 'footnote-reference
3200 (lambda (fn)
3201 (cond
3202 ((string= (org-element-property :label fn) label)
3203 (throw 'exit fn))
3204 ;; If FN isn't inlined, be sure to traverse its
3205 ;; definition before resuming search. See
3206 ;; comments in `org-export-get-footnote-number'
3207 ;; for more information.
3208 ((eq (org-element-property :type fn) 'standard)
3209 (funcall search-refs
3210 (org-export-get-footnote-definition fn info)))))
3211 ;; Don't enter footnote definitions since it will
3212 ;; happen when their first reference is found.
3213 info 'first-match 'footnote-definition)))))
3214 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3215 footnote-reference)))))
3217 (defun org-export-get-footnote-definition (footnote-reference info)
3218 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3219 INFO is the plist used as a communication channel."
3220 (let ((label (org-element-property :label footnote-reference)))
3221 (or (org-element-property :inline-definition footnote-reference)
3222 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3224 (defun org-export-get-footnote-number (footnote info)
3225 "Return number associated to a footnote.
3227 FOOTNOTE is either a footnote reference or a footnote definition.
3228 INFO is the plist used as a communication channel."
3229 (let* ((label (org-element-property :label footnote))
3230 seen-refs
3231 search-ref ; For byte-compiler.
3232 (search-ref
3233 (function
3234 (lambda (data)
3235 ;; Search footnote references through DATA, filling
3236 ;; SEEN-REFS along the way.
3237 (org-element-map
3238 data 'footnote-reference
3239 (lambda (fn)
3240 (let ((fn-lbl (org-element-property :label fn)))
3241 (cond
3242 ;; Anonymous footnote match: return number.
3243 ((and (not fn-lbl) (eq fn footnote))
3244 (throw 'exit (1+ (length seen-refs))))
3245 ;; Labels match: return number.
3246 ((and label (string= label fn-lbl))
3247 (throw 'exit (1+ (length seen-refs))))
3248 ;; Anonymous footnote: it's always a new one. Also,
3249 ;; be sure to return nil from the `cond' so
3250 ;; `first-match' doesn't get us out of the loop.
3251 ((not fn-lbl) (push 'inline seen-refs) nil)
3252 ;; Label not seen so far: add it so SEEN-REFS.
3254 ;; Also search for subsequent references in
3255 ;; footnote definition so numbering follows reading
3256 ;; logic. Note that we don't have to care about
3257 ;; inline definitions, since `org-element-map'
3258 ;; already traverses them at the right time.
3260 ;; Once again, return nil to stay in the loop.
3261 ((not (member fn-lbl seen-refs))
3262 (push fn-lbl seen-refs)
3263 (funcall search-ref
3264 (org-export-get-footnote-definition fn info))
3265 nil))))
3266 ;; Don't enter footnote definitions since it will happen
3267 ;; when their first reference is found.
3268 info 'first-match 'footnote-definition)))))
3269 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3272 ;;;; For Headlines
3274 ;; `org-export-get-relative-level' is a shortcut to get headline
3275 ;; level, relatively to the lower headline level in the parsed tree.
3277 ;; `org-export-get-headline-number' returns the section number of an
3278 ;; headline, while `org-export-number-to-roman' allows to convert it
3279 ;; to roman numbers.
3281 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3282 ;; `org-export-last-sibling-p' are three useful predicates when it
3283 ;; comes to fulfill the `:headline-levels' property.
3285 ;; `org-export-get-tags', `org-export-get-category' and
3286 ;; `org-export-get-node-property' extract useful information from an
3287 ;; headline or a parent headline. They all handle inheritance.
3289 (defun org-export-get-relative-level (headline info)
3290 "Return HEADLINE relative level within current parsed tree.
3291 INFO is a plist holding contextual information."
3292 (+ (org-element-property :level headline)
3293 (or (plist-get info :headline-offset) 0)))
3295 (defun org-export-low-level-p (headline info)
3296 "Non-nil when HEADLINE is considered as low level.
3298 INFO is a plist used as a communication channel.
3300 A low level headlines has a relative level greater than
3301 `:headline-levels' property value.
3303 Return value is the difference between HEADLINE relative level
3304 and the last level being considered as high enough, or nil."
3305 (let ((limit (plist-get info :headline-levels)))
3306 (when (wholenump limit)
3307 (let ((level (org-export-get-relative-level headline info)))
3308 (and (> level limit) (- level limit))))))
3310 (defun org-export-get-headline-number (headline info)
3311 "Return HEADLINE numbering as a list of numbers.
3312 INFO is a plist holding contextual information."
3313 (cdr (assoc headline (plist-get info :headline-numbering))))
3315 (defun org-export-numbered-headline-p (headline info)
3316 "Return a non-nil value if HEADLINE element should be numbered.
3317 INFO is a plist used as a communication channel."
3318 (let ((sec-num (plist-get info :section-numbers))
3319 (level (org-export-get-relative-level headline info)))
3320 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3322 (defun org-export-number-to-roman (n)
3323 "Convert integer N into a roman numeral."
3324 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3325 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3326 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3327 ( 1 . "I")))
3328 (res ""))
3329 (if (<= n 0)
3330 (number-to-string n)
3331 (while roman
3332 (if (>= n (caar roman))
3333 (setq n (- n (caar roman))
3334 res (concat res (cdar roman)))
3335 (pop roman)))
3336 res)))
3338 (defun org-export-get-tags (element info &optional tags inherited)
3339 "Return list of tags associated to ELEMENT.
3341 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3342 is a plist used as a communication channel.
3344 Select tags (see `org-export-select-tags') and exclude tags (see
3345 `org-export-exclude-tags') are removed from the list.
3347 When non-nil, optional argument TAGS should be a list of strings.
3348 Any tag belonging to this list will also be removed.
3350 When optional argument INHERITED is non-nil, tags can also be
3351 inherited from parent headlines and FILETAGS keywords."
3352 (org-remove-if
3353 (lambda (tag) (or (member tag (plist-get info :select-tags))
3354 (member tag (plist-get info :exclude-tags))
3355 (member tag tags)))
3356 (if (not inherited) (org-element-property :tags element)
3357 ;; Build complete list of inherited tags.
3358 (let ((current-tag-list (org-element-property :tags element)))
3359 (mapc
3360 (lambda (parent)
3361 (mapc
3362 (lambda (tag)
3363 (when (and (memq (org-element-type parent) '(headline inlinetask))
3364 (not (member tag current-tag-list)))
3365 (push tag current-tag-list)))
3366 (org-element-property :tags parent)))
3367 (org-export-get-genealogy element))
3368 ;; Add FILETAGS keywords and return results.
3369 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
3371 (defun org-export-get-node-property (property blob &optional inherited)
3372 "Return node PROPERTY value for BLOB.
3374 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3375 element or object.
3377 If optional argument INHERITED is non-nil, the value can be
3378 inherited from a parent headline.
3380 Return value is a string or nil."
3381 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3382 (org-export-get-parent-headline blob))))
3383 (if (not inherited) (org-element-property property blob)
3384 (let ((parent headline) value)
3385 (catch 'found
3386 (while parent
3387 (when (plist-member (nth 1 parent) property)
3388 (throw 'found (org-element-property property parent)))
3389 (setq parent (org-element-property :parent parent))))))))
3391 (defun org-export-get-category (blob info)
3392 "Return category for element or object BLOB.
3394 INFO is a plist used as a communication channel.
3396 CATEGORY is automatically inherited from a parent headline, from
3397 #+CATEGORY: keyword or created out of original file name. If all
3398 fail, the fall-back value is \"???\"."
3399 (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
3400 (org-export-get-parent-headline blob))))
3401 ;; Almost like `org-export-node-property', but we cannot trust
3402 ;; `plist-member' as every headline has a `:category'
3403 ;; property, would it be nil or equal to "???" (which has the
3404 ;; same meaning).
3405 (let ((parent headline) value)
3406 (catch 'found
3407 (while parent
3408 (let ((category (org-element-property :category parent)))
3409 (and category (not (equal "???" category))
3410 (throw 'found category)))
3411 (setq parent (org-element-property :parent parent))))))
3412 (org-element-map
3413 (plist-get info :parse-tree) 'keyword
3414 (lambda (kwd)
3415 (when (equal (org-element-property :key kwd) "CATEGORY")
3416 (org-element-property :value kwd)))
3417 info 'first-match)
3418 (let ((file (plist-get info :input-file)))
3419 (and file (file-name-sans-extension (file-name-nondirectory file))))
3420 "???"))
3422 (defun org-export-first-sibling-p (headline info)
3423 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3424 INFO is a plist used as a communication channel."
3425 (not (eq (org-element-type (org-export-get-previous-element headline info))
3426 'headline)))
3428 (defun org-export-last-sibling-p (headline info)
3429 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3430 INFO is a plist used as a communication channel."
3431 (not (org-export-get-next-element headline info)))
3434 ;;;; For Links
3436 ;; `org-export-solidify-link-text' turns a string into a safer version
3437 ;; for links, replacing most non-standard characters with hyphens.
3439 ;; `org-export-get-coderef-format' returns an appropriate format
3440 ;; string for coderefs.
3442 ;; `org-export-inline-image-p' returns a non-nil value when the link
3443 ;; provided should be considered as an inline image.
3445 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3446 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3447 ;; returns an appropriate unique identifier when found, or nil.
3449 ;; `org-export-resolve-id-link' returns the first headline with
3450 ;; specified id or custom-id in parse tree, the path to the external
3451 ;; file with the id or nil when neither was found.
3453 ;; `org-export-resolve-coderef' associates a reference to a line
3454 ;; number in the element it belongs, or returns the reference itself
3455 ;; when the element isn't numbered.
3457 (defun org-export-solidify-link-text (s)
3458 "Take link text S and make a safe target out of it."
3459 (save-match-data
3460 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
3462 (defun org-export-get-coderef-format (path desc)
3463 "Return format string for code reference link.
3464 PATH is the link path. DESC is its description."
3465 (save-match-data
3466 (cond ((not desc) "%s")
3467 ((string-match (regexp-quote (concat "(" path ")")) desc)
3468 (replace-match "%s" t t desc))
3469 (t desc))))
3471 (defun org-export-inline-image-p (link &optional rules)
3472 "Non-nil if LINK object points to an inline image.
3474 Optional argument is a set of RULES defining inline images. It
3475 is an alist where associations have the following shape:
3477 \(TYPE . REGEXP)
3479 Applying a rule means apply REGEXP against LINK's path when its
3480 type is TYPE. The function will return a non-nil value if any of
3481 the provided rules is non-nil. The default rule is
3482 `org-export-default-inline-image-rule'.
3484 This only applies to links without a description."
3485 (and (not (org-element-contents link))
3486 (let ((case-fold-search t)
3487 (rules (or rules org-export-default-inline-image-rule)))
3488 (catch 'exit
3489 (mapc
3490 (lambda (rule)
3491 (and (string= (org-element-property :type link) (car rule))
3492 (string-match (cdr rule)
3493 (org-element-property :path link))
3494 (throw 'exit t)))
3495 rules)
3496 ;; Return nil if no rule matched.
3497 nil))))
3499 (defun org-export-resolve-coderef (ref info)
3500 "Resolve a code reference REF.
3502 INFO is a plist used as a communication channel.
3504 Return associated line number in source code, or REF itself,
3505 depending on src-block or example element's switches."
3506 (org-element-map
3507 (plist-get info :parse-tree) '(example-block src-block)
3508 (lambda (el)
3509 (with-temp-buffer
3510 (insert (org-trim (org-element-property :value el)))
3511 (let* ((label-fmt (regexp-quote
3512 (or (org-element-property :label-fmt el)
3513 org-coderef-label-format)))
3514 (ref-re
3515 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3516 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3517 ;; Element containing REF is found. Resolve it to either
3518 ;; a label or a line number, as needed.
3519 (when (re-search-backward ref-re nil t)
3520 (cond
3521 ((org-element-property :use-labels el) ref)
3522 ((eq (org-element-property :number-lines el) 'continued)
3523 (+ (org-export-get-loc el info) (line-number-at-pos)))
3524 (t (line-number-at-pos)))))))
3525 info 'first-match))
3527 (defun org-export-resolve-fuzzy-link (link info)
3528 "Return LINK destination.
3530 INFO is a plist holding contextual information.
3532 Return value can be an object, an element, or nil:
3534 - If LINK path matches a target object (i.e. <<path>>) or
3535 element (i.e. \"#+TARGET: path\"), return it.
3537 - If LINK path exactly matches the name affiliated keyword
3538 \(i.e. #+NAME: path) of an element, return that element.
3540 - If LINK path exactly matches any headline name, return that
3541 element. If more than one headline share that name, priority
3542 will be given to the one with the closest common ancestor, if
3543 any, or the first one in the parse tree otherwise.
3545 - Otherwise, return nil.
3547 Assume LINK type is \"fuzzy\"."
3548 (let* ((path (org-element-property :path link))
3549 (match-title-p (eq (aref path 0) ?*)))
3550 (cond
3551 ;; First try to find a matching "<<path>>" unless user specified
3552 ;; he was looking for an headline (path starts with a *
3553 ;; character).
3554 ((and (not match-title-p)
3555 (loop for target in (plist-get info :target-list)
3556 when (string= (org-element-property :value target) path)
3557 return target)))
3558 ;; Then try to find an element with a matching "#+NAME: path"
3559 ;; affiliated keyword.
3560 ((and (not match-title-p)
3561 (org-element-map
3562 (plist-get info :parse-tree) org-element-all-elements
3563 (lambda (el)
3564 (when (string= (org-element-property :name el) path) el))
3565 info 'first-match)))
3566 ;; Last case: link either points to an headline or to
3567 ;; nothingness. Try to find the source, with priority given to
3568 ;; headlines with the closest common ancestor. If such candidate
3569 ;; is found, return it, otherwise return nil.
3571 (let ((find-headline
3572 (function
3573 ;; Return first headline whose `:raw-value' property
3574 ;; is NAME in parse tree DATA, or nil.
3575 (lambda (name data)
3576 (org-element-map
3577 data 'headline
3578 (lambda (headline)
3579 (when (string=
3580 (org-element-property :raw-value headline)
3581 name)
3582 headline))
3583 info 'first-match)))))
3584 ;; Search among headlines sharing an ancestor with link,
3585 ;; from closest to farthest.
3586 (or (catch 'exit
3587 (mapc
3588 (lambda (parent)
3589 (when (eq (org-element-type parent) 'headline)
3590 (let ((foundp (funcall find-headline path parent)))
3591 (when foundp (throw 'exit foundp)))))
3592 (org-export-get-genealogy link)) nil)
3593 ;; No match with a common ancestor: try the full parse-tree.
3594 (funcall find-headline
3595 (if match-title-p (substring path 1) path)
3596 (plist-get info :parse-tree))))))))
3598 (defun org-export-resolve-id-link (link info)
3599 "Return headline referenced as LINK destination.
3601 INFO is a plist used as a communication channel.
3603 Return value can be the headline element matched in current parse
3604 tree, a file name or nil. Assume LINK type is either \"id\" or
3605 \"custom-id\"."
3606 (let ((id (org-element-property :path link)))
3607 ;; First check if id is within the current parse tree.
3608 (or (org-element-map
3609 (plist-get info :parse-tree) 'headline
3610 (lambda (headline)
3611 (when (or (string= (org-element-property :id headline) id)
3612 (string= (org-element-property :custom-id headline) id))
3613 headline))
3614 info 'first-match)
3615 ;; Otherwise, look for external files.
3616 (cdr (assoc id (plist-get info :id-alist))))))
3618 (defun org-export-resolve-radio-link (link info)
3619 "Return radio-target object referenced as LINK destination.
3621 INFO is a plist used as a communication channel.
3623 Return value can be a radio-target object or nil. Assume LINK
3624 has type \"radio\"."
3625 (let ((path (org-element-property :path link)))
3626 (org-element-map
3627 (plist-get info :parse-tree) 'radio-target
3628 (lambda (radio)
3629 (when (equal (org-element-property :value radio) path) radio))
3630 info 'first-match)))
3633 ;;;; For References
3635 ;; `org-export-get-ordinal' associates a sequence number to any object
3636 ;; or element.
3638 (defun org-export-get-ordinal (element info &optional types predicate)
3639 "Return ordinal number of an element or object.
3641 ELEMENT is the element or object considered. INFO is the plist
3642 used as a communication channel.
3644 Optional argument TYPES, when non-nil, is a list of element or
3645 object types, as symbols, that should also be counted in.
3646 Otherwise, only provided element's type is considered.
3648 Optional argument PREDICATE is a function returning a non-nil
3649 value if the current element or object should be counted in. It
3650 accepts two arguments: the element or object being considered and
3651 the plist used as a communication channel. This allows to count
3652 only a certain type of objects (i.e. inline images).
3654 Return value is a list of numbers if ELEMENT is an headline or an
3655 item. It is nil for keywords. It represents the footnote number
3656 for footnote definitions and footnote references. If ELEMENT is
3657 a target, return the same value as if ELEMENT was the closest
3658 table, item or headline containing the target. In any other
3659 case, return the sequence number of ELEMENT among elements or
3660 objects of the same type."
3661 ;; A target keyword, representing an invisible target, never has
3662 ;; a sequence number.
3663 (unless (eq (org-element-type element) 'keyword)
3664 ;; Ordinal of a target object refer to the ordinal of the closest
3665 ;; table, item, or headline containing the object.
3666 (when (eq (org-element-type element) 'target)
3667 (setq element
3668 (loop for parent in (org-export-get-genealogy element)
3669 when
3670 (memq
3671 (org-element-type parent)
3672 '(footnote-definition footnote-reference headline item
3673 table))
3674 return parent)))
3675 (case (org-element-type element)
3676 ;; Special case 1: An headline returns its number as a list.
3677 (headline (org-export-get-headline-number element info))
3678 ;; Special case 2: An item returns its number as a list.
3679 (item (let ((struct (org-element-property :structure element)))
3680 (org-list-get-item-number
3681 (org-element-property :begin element)
3682 struct
3683 (org-list-prevs-alist struct)
3684 (org-list-parents-alist struct))))
3685 ((footnote-definition footnote-reference)
3686 (org-export-get-footnote-number element info))
3687 (otherwise
3688 (let ((counter 0))
3689 ;; Increment counter until ELEMENT is found again.
3690 (org-element-map
3691 (plist-get info :parse-tree) (or types (org-element-type element))
3692 (lambda (el)
3693 (cond
3694 ((eq element el) (1+ counter))
3695 ((not predicate) (incf counter) nil)
3696 ((funcall predicate el info) (incf counter) nil)))
3697 info 'first-match))))))
3700 ;;;; For Src-Blocks
3702 ;; `org-export-get-loc' counts number of code lines accumulated in
3703 ;; src-block or example-block elements with a "+n" switch until
3704 ;; a given element, excluded. Note: "-n" switches reset that count.
3706 ;; `org-export-unravel-code' extracts source code (along with a code
3707 ;; references alist) from an `element-block' or `src-block' type
3708 ;; element.
3710 ;; `org-export-format-code' applies a formatting function to each line
3711 ;; of code, providing relative line number and code reference when
3712 ;; appropriate. Since it doesn't access the original element from
3713 ;; which the source code is coming, it expects from the code calling
3714 ;; it to know if lines should be numbered and if code references
3715 ;; should appear.
3717 ;; Eventually, `org-export-format-code-default' is a higher-level
3718 ;; function (it makes use of the two previous functions) which handles
3719 ;; line numbering and code references inclusion, and returns source
3720 ;; code in a format suitable for plain text or verbatim output.
3722 (defun org-export-get-loc (element info)
3723 "Return accumulated lines of code up to ELEMENT.
3725 INFO is the plist used as a communication channel.
3727 ELEMENT is excluded from count."
3728 (let ((loc 0))
3729 (org-element-map
3730 (plist-get info :parse-tree)
3731 `(src-block example-block ,(org-element-type element))
3732 (lambda (el)
3733 (cond
3734 ;; ELEMENT is reached: Quit the loop.
3735 ((eq el element))
3736 ;; Only count lines from src-block and example-block elements
3737 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3738 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3739 ((let ((linums (org-element-property :number-lines el)))
3740 (when linums
3741 ;; Accumulate locs or reset them.
3742 (let ((lines (org-count-lines
3743 (org-trim (org-element-property :value el)))))
3744 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3745 ;; Return nil to stay in the loop.
3746 nil)))
3747 info 'first-match)
3748 ;; Return value.
3749 loc))
3751 (defun org-export-unravel-code (element)
3752 "Clean source code and extract references out of it.
3754 ELEMENT has either a `src-block' an `example-block' type.
3756 Return a cons cell whose CAR is the source code, cleaned from any
3757 reference and protective comma and CDR is an alist between
3758 relative line number (integer) and name of code reference on that
3759 line (string)."
3760 (let* ((line 0) refs
3761 ;; Get code and clean it. Remove blank lines at its
3762 ;; beginning and end.
3763 (code (let ((c (replace-regexp-in-string
3764 "\\`\\([ \t]*\n\\)+" ""
3765 (replace-regexp-in-string
3766 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3767 (org-element-property :value element)))))
3768 ;; If appropriate, remove global indentation.
3769 (if (or org-src-preserve-indentation
3770 (org-element-property :preserve-indent element))
3772 (org-remove-indentation c))))
3773 ;; Get format used for references.
3774 (label-fmt (regexp-quote
3775 (or (org-element-property :label-fmt element)
3776 org-coderef-label-format)))
3777 ;; Build a regexp matching a loc with a reference.
3778 (with-ref-re
3779 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3780 (replace-regexp-in-string
3781 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3782 ;; Return value.
3783 (cons
3784 ;; Code with references removed.
3785 (org-element-normalize-string
3786 (mapconcat
3787 (lambda (loc)
3788 (incf line)
3789 (if (not (string-match with-ref-re loc)) loc
3790 ;; Ref line: remove ref, and signal its position in REFS.
3791 (push (cons line (match-string 3 loc)) refs)
3792 (replace-match "" nil nil loc 1)))
3793 (org-split-string code "\n") "\n"))
3794 ;; Reference alist.
3795 refs)))
3797 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3798 "Format CODE by applying FUN line-wise and return it.
3800 CODE is a string representing the code to format. FUN is
3801 a function. It must accept three arguments: a line of
3802 code (string), the current line number (integer) or nil and the
3803 reference associated to the current line (string) or nil.
3805 Optional argument NUM-LINES can be an integer representing the
3806 number of code lines accumulated until the current code. Line
3807 numbers passed to FUN will take it into account. If it is nil,
3808 FUN's second argument will always be nil. This number can be
3809 obtained with `org-export-get-loc' function.
3811 Optional argument REF-ALIST can be an alist between relative line
3812 number (i.e. ignoring NUM-LINES) and the name of the code
3813 reference on it. If it is nil, FUN's third argument will always
3814 be nil. It can be obtained through the use of
3815 `org-export-unravel-code' function."
3816 (let ((--locs (org-split-string code "\n"))
3817 (--line 0))
3818 (org-element-normalize-string
3819 (mapconcat
3820 (lambda (--loc)
3821 (incf --line)
3822 (let ((--ref (cdr (assq --line ref-alist))))
3823 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3824 --locs "\n"))))
3826 (defun org-export-format-code-default (element info)
3827 "Return source code from ELEMENT, formatted in a standard way.
3829 ELEMENT is either a `src-block' or `example-block' element. INFO
3830 is a plist used as a communication channel.
3832 This function takes care of line numbering and code references
3833 inclusion. Line numbers, when applicable, appear at the
3834 beginning of the line, separated from the code by two white
3835 spaces. Code references, on the other hand, appear flushed to
3836 the right, separated by six white spaces from the widest line of
3837 code."
3838 ;; Extract code and references.
3839 (let* ((code-info (org-export-unravel-code element))
3840 (code (car code-info))
3841 (code-lines (org-split-string code "\n"))
3842 (refs (and (org-element-property :retain-labels element)
3843 (cdr code-info)))
3844 ;; Handle line numbering.
3845 (num-start (case (org-element-property :number-lines element)
3846 (continued (org-export-get-loc element info))
3847 (new 0)))
3848 (num-fmt
3849 (and num-start
3850 (format "%%%ds "
3851 (length (number-to-string
3852 (+ (length code-lines) num-start))))))
3853 ;; Prepare references display, if required. Any reference
3854 ;; should start six columns after the widest line of code,
3855 ;; wrapped with parenthesis.
3856 (max-width
3857 (+ (apply 'max (mapcar 'length code-lines))
3858 (if (not num-start) 0 (length (format num-fmt num-start))))))
3859 (org-export-format-code
3860 code
3861 (lambda (loc line-num ref)
3862 (let ((number-str (and num-fmt (format num-fmt line-num))))
3863 (concat
3864 number-str
3866 (and ref
3867 (concat (make-string
3868 (- (+ 6 max-width)
3869 (+ (length loc) (length number-str))) ? )
3870 (format "(%s)" ref))))))
3871 num-start refs)))
3874 ;;;; For Tables
3876 ;; `org-export-table-has-special-column-p' and and
3877 ;; `org-export-table-row-is-special-p' are predicates used to look for
3878 ;; meta-information about the table structure.
3880 ;; `org-table-has-header-p' tells when the rows before the first rule
3881 ;; should be considered as table's header.
3883 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3884 ;; and `org-export-table-cell-borders' extract information from
3885 ;; a table-cell element.
3887 ;; `org-export-table-dimensions' gives the number on rows and columns
3888 ;; in the table, ignoring horizontal rules and special columns.
3889 ;; `org-export-table-cell-address', given a table-cell object, returns
3890 ;; the absolute address of a cell. On the other hand,
3891 ;; `org-export-get-table-cell-at' does the contrary.
3893 ;; `org-export-table-cell-starts-colgroup-p',
3894 ;; `org-export-table-cell-ends-colgroup-p',
3895 ;; `org-export-table-row-starts-rowgroup-p',
3896 ;; `org-export-table-row-ends-rowgroup-p',
3897 ;; `org-export-table-row-starts-header-p' and
3898 ;; `org-export-table-row-ends-header-p' indicate position of current
3899 ;; row or cell within the table.
3901 (defun org-export-table-has-special-column-p (table)
3902 "Non-nil when TABLE has a special column.
3903 All special columns will be ignored during export."
3904 ;; The table has a special column when every first cell of every row
3905 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3906 ;; "*" "_" and "^". Though, do not consider a first row containing
3907 ;; only empty cells as special.
3908 (let ((special-column-p 'empty))
3909 (catch 'exit
3910 (mapc
3911 (lambda (row)
3912 (when (eq (org-element-property :type row) 'standard)
3913 (let ((value (org-element-contents
3914 (car (org-element-contents row)))))
3915 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3916 (setq special-column-p 'special))
3917 ((not value))
3918 (t (throw 'exit nil))))))
3919 (org-element-contents table))
3920 (eq special-column-p 'special))))
3922 (defun org-export-table-has-header-p (table info)
3923 "Non-nil when TABLE has an header.
3925 INFO is a plist used as a communication channel.
3927 A table has an header when it contains at least two row groups."
3928 (let ((rowgroup 1) row-flag)
3929 (org-element-map
3930 table 'table-row
3931 (lambda (row)
3932 (cond
3933 ((> rowgroup 1) t)
3934 ((and row-flag (eq (org-element-property :type row) 'rule))
3935 (incf rowgroup) (setq row-flag nil))
3936 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3937 (setq row-flag t) nil)))
3938 info)))
3940 (defun org-export-table-row-is-special-p (table-row info)
3941 "Non-nil if TABLE-ROW is considered special.
3943 INFO is a plist used as the communication channel.
3945 All special rows will be ignored during export."
3946 (when (eq (org-element-property :type table-row) 'standard)
3947 (let ((first-cell (org-element-contents
3948 (car (org-element-contents table-row)))))
3949 ;; A row is special either when...
3951 ;; ... it starts with a field only containing "/",
3952 (equal first-cell '("/"))
3953 ;; ... the table contains a special column and the row start
3954 ;; with a marking character among, "^", "_", "$" or "!",
3955 (and (org-export-table-has-special-column-p
3956 (org-export-get-parent table-row))
3957 (member first-cell '(("^") ("_") ("$") ("!"))))
3958 ;; ... it contains only alignment cookies and empty cells.
3959 (let ((special-row-p 'empty))
3960 (catch 'exit
3961 (mapc
3962 (lambda (cell)
3963 (let ((value (org-element-contents cell)))
3964 ;; Since VALUE is a secondary string, the following
3965 ;; checks avoid expanding it with `org-export-data'.
3966 (cond ((not value))
3967 ((and (not (cdr value))
3968 (stringp (car value))
3969 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3970 (car value)))
3971 (setq special-row-p 'cookie))
3972 (t (throw 'exit nil)))))
3973 (org-element-contents table-row))
3974 (eq special-row-p 'cookie)))))))
3976 (defun org-export-table-row-group (table-row info)
3977 "Return TABLE-ROW's group.
3979 INFO is a plist used as the communication channel.
3981 Return value is the group number, as an integer, or nil special
3982 rows and table rules. Group 1 is also table's header."
3983 (unless (or (eq (org-element-property :type table-row) 'rule)
3984 (org-export-table-row-is-special-p table-row info))
3985 (let ((group 0) row-flag)
3986 (catch 'found
3987 (mapc
3988 (lambda (row)
3989 (cond
3990 ((and (eq (org-element-property :type row) 'standard)
3991 (not (org-export-table-row-is-special-p row info)))
3992 (unless row-flag (incf group) (setq row-flag t)))
3993 ((eq (org-element-property :type row) 'rule)
3994 (setq row-flag nil)))
3995 (when (eq table-row row) (throw 'found group)))
3996 (org-element-contents (org-export-get-parent table-row)))))))
3998 (defun org-export-table-cell-width (table-cell info)
3999 "Return TABLE-CELL contents width.
4001 INFO is a plist used as the communication channel.
4003 Return value is the width given by the last width cookie in the
4004 same column as TABLE-CELL, or nil."
4005 (let* ((row (org-export-get-parent table-cell))
4006 (column (let ((cells (org-element-contents row)))
4007 (- (length cells) (length (memq table-cell cells)))))
4008 (table (org-export-get-parent-table table-cell))
4009 cookie-width)
4010 (mapc
4011 (lambda (row)
4012 (cond
4013 ;; In a special row, try to find a width cookie at COLUMN.
4014 ((org-export-table-row-is-special-p row info)
4015 (let ((value (org-element-contents
4016 (elt (org-element-contents row) column))))
4017 ;; The following checks avoid expanding unnecessarily the
4018 ;; cell with `org-export-data'
4019 (when (and value
4020 (not (cdr value))
4021 (stringp (car value))
4022 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
4023 (match-string 1 (car value)))
4024 (setq cookie-width
4025 (string-to-number (match-string 1 (car value)))))))
4026 ;; Ignore table rules.
4027 ((eq (org-element-property :type row) 'rule))))
4028 (org-element-contents table))
4029 ;; Return value.
4030 cookie-width))
4032 (defun org-export-table-cell-alignment (table-cell info)
4033 "Return TABLE-CELL contents alignment.
4035 INFO is a plist used as the communication channel.
4037 Return alignment as specified by the last alignment cookie in the
4038 same column as TABLE-CELL. If no such cookie is found, a default
4039 alignment value will be deduced from fraction of numbers in the
4040 column (see `org-table-number-fraction' for more information).
4041 Possible values are `left', `right' and `center'."
4042 (let* ((row (org-export-get-parent table-cell))
4043 (column (let ((cells (org-element-contents row)))
4044 (- (length cells) (length (memq table-cell cells)))))
4045 (table (org-export-get-parent-table table-cell))
4046 (number-cells 0)
4047 (total-cells 0)
4048 cookie-align)
4049 (mapc
4050 (lambda (row)
4051 (cond
4052 ;; In a special row, try to find an alignment cookie at
4053 ;; COLUMN.
4054 ((org-export-table-row-is-special-p row info)
4055 (let ((value (org-element-contents
4056 (elt (org-element-contents row) column))))
4057 ;; Since VALUE is a secondary string, the following checks
4058 ;; avoid useless expansion through `org-export-data'.
4059 (when (and value
4060 (not (cdr value))
4061 (stringp (car value))
4062 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4063 (car value))
4064 (match-string 1 (car value)))
4065 (setq cookie-align (match-string 1 (car value))))))
4066 ;; Ignore table rules.
4067 ((eq (org-element-property :type row) 'rule))
4068 ;; In a standard row, check if cell's contents are expressing
4069 ;; some kind of number. Increase NUMBER-CELLS accordingly.
4070 ;; Though, don't bother if an alignment cookie has already
4071 ;; defined cell's alignment.
4072 ((not cookie-align)
4073 (let ((value (org-export-data
4074 (org-element-contents
4075 (elt (org-element-contents row) column))
4076 info)))
4077 (incf total-cells)
4078 (when (string-match org-table-number-regexp value)
4079 (incf number-cells))))))
4080 (org-element-contents table))
4081 ;; Return value. Alignment specified by cookies has precedence
4082 ;; over alignment deduced from cells contents.
4083 (cond ((equal cookie-align "l") 'left)
4084 ((equal cookie-align "r") 'right)
4085 ((equal cookie-align "c") 'center)
4086 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
4087 'right)
4088 (t 'left))))
4090 (defun org-export-table-cell-borders (table-cell info)
4091 "Return TABLE-CELL borders.
4093 INFO is a plist used as a communication channel.
4095 Return value is a list of symbols, or nil. Possible values are:
4096 `top', `bottom', `above', `below', `left' and `right'. Note:
4097 `top' (resp. `bottom') only happen for a cell in the first
4098 row (resp. last row) of the table, ignoring table rules, if any.
4100 Returned borders ignore special rows."
4101 (let* ((row (org-export-get-parent table-cell))
4102 (table (org-export-get-parent-table table-cell))
4103 borders)
4104 ;; Top/above border? TABLE-CELL has a border above when a rule
4105 ;; used to demarcate row groups can be found above. Hence,
4106 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4107 ;; another regular row has to be found above that rule.
4108 (let (rule-flag)
4109 (catch 'exit
4110 (mapc (lambda (row)
4111 (cond ((eq (org-element-property :type row) 'rule)
4112 (setq rule-flag t))
4113 ((not (org-export-table-row-is-special-p row info))
4114 (if rule-flag (throw 'exit (push 'above borders))
4115 (throw 'exit nil)))))
4116 ;; Look at every row before the current one.
4117 (cdr (memq row (reverse (org-element-contents table)))))
4118 ;; No rule above, or rule found starts the table (ignoring any
4119 ;; special row): TABLE-CELL is at the top of the table.
4120 (when rule-flag (push 'above borders))
4121 (push 'top borders)))
4122 ;; Bottom/below border? TABLE-CELL has a border below when next
4123 ;; non-regular row below is a rule.
4124 (let (rule-flag)
4125 (catch 'exit
4126 (mapc (lambda (row)
4127 (cond ((eq (org-element-property :type row) 'rule)
4128 (setq rule-flag t))
4129 ((not (org-export-table-row-is-special-p row info))
4130 (if rule-flag (throw 'exit (push 'below borders))
4131 (throw 'exit nil)))))
4132 ;; Look at every row after the current one.
4133 (cdr (memq row (org-element-contents table))))
4134 ;; No rule below, or rule found ends the table (modulo some
4135 ;; special row): TABLE-CELL is at the bottom of the table.
4136 (when rule-flag (push 'below borders))
4137 (push 'bottom borders)))
4138 ;; Right/left borders? They can only be specified by column
4139 ;; groups. Column groups are defined in a row starting with "/".
4140 ;; Also a column groups row only contains "<", "<>", ">" or blank
4141 ;; cells.
4142 (catch 'exit
4143 (let ((column (let ((cells (org-element-contents row)))
4144 (- (length cells) (length (memq table-cell cells))))))
4145 (mapc
4146 (lambda (row)
4147 (unless (eq (org-element-property :type row) 'rule)
4148 (when (equal (org-element-contents
4149 (car (org-element-contents row)))
4150 '("/"))
4151 (let ((column-groups
4152 (mapcar
4153 (lambda (cell)
4154 (let ((value (org-element-contents cell)))
4155 (when (member value '(("<") ("<>") (">") nil))
4156 (car value))))
4157 (org-element-contents row))))
4158 ;; There's a left border when previous cell, if
4159 ;; any, ends a group, or current one starts one.
4160 (when (or (and (not (zerop column))
4161 (member (elt column-groups (1- column))
4162 '(">" "<>")))
4163 (member (elt column-groups column) '("<" "<>")))
4164 (push 'left borders))
4165 ;; There's a right border when next cell, if any,
4166 ;; starts a group, or current one ends one.
4167 (when (or (and (/= (1+ column) (length column-groups))
4168 (member (elt column-groups (1+ column))
4169 '("<" "<>")))
4170 (member (elt column-groups column) '(">" "<>")))
4171 (push 'right borders))
4172 (throw 'exit nil)))))
4173 ;; Table rows are read in reverse order so last column groups
4174 ;; row has precedence over any previous one.
4175 (reverse (org-element-contents table)))))
4176 ;; Return value.
4177 borders))
4179 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4180 "Non-nil when TABLE-CELL is at the beginning of a row group.
4181 INFO is a plist used as a communication channel."
4182 ;; A cell starts a column group either when it is at the beginning
4183 ;; of a row (or after the special column, if any) or when it has
4184 ;; a left border.
4185 (or (eq (org-element-map
4186 (org-export-get-parent table-cell)
4187 'table-cell 'identity info 'first-match)
4188 table-cell)
4189 (memq 'left (org-export-table-cell-borders table-cell info))))
4191 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4192 "Non-nil when TABLE-CELL is at the end of a row group.
4193 INFO is a plist used as a communication channel."
4194 ;; A cell ends a column group either when it is at the end of a row
4195 ;; or when it has a right border.
4196 (or (eq (car (last (org-element-contents
4197 (org-export-get-parent table-cell))))
4198 table-cell)
4199 (memq 'right (org-export-table-cell-borders table-cell info))))
4201 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4202 "Non-nil when TABLE-ROW is at the beginning of a column group.
4203 INFO is a plist used as a communication channel."
4204 (unless (or (eq (org-element-property :type table-row) 'rule)
4205 (org-export-table-row-is-special-p table-row info))
4206 (let ((borders (org-export-table-cell-borders
4207 (car (org-element-contents table-row)) info)))
4208 (or (memq 'top borders) (memq 'above borders)))))
4210 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4211 "Non-nil when TABLE-ROW is at the end of a column group.
4212 INFO is a plist used as a communication channel."
4213 (unless (or (eq (org-element-property :type table-row) 'rule)
4214 (org-export-table-row-is-special-p table-row info))
4215 (let ((borders (org-export-table-cell-borders
4216 (car (org-element-contents table-row)) info)))
4217 (or (memq 'bottom borders) (memq 'below borders)))))
4219 (defun org-export-table-row-starts-header-p (table-row info)
4220 "Non-nil when TABLE-ROW is the first table header's row.
4221 INFO is a plist used as a communication channel."
4222 (and (org-export-table-has-header-p
4223 (org-export-get-parent-table table-row) info)
4224 (org-export-table-row-starts-rowgroup-p table-row info)
4225 (= (org-export-table-row-group table-row info) 1)))
4227 (defun org-export-table-row-ends-header-p (table-row info)
4228 "Non-nil when TABLE-ROW is the last table header's row.
4229 INFO is a plist used as a communication channel."
4230 (and (org-export-table-has-header-p
4231 (org-export-get-parent-table table-row) info)
4232 (org-export-table-row-ends-rowgroup-p table-row info)
4233 (= (org-export-table-row-group table-row info) 1)))
4235 (defun org-export-table-dimensions (table info)
4236 "Return TABLE dimensions.
4238 INFO is a plist used as a communication channel.
4240 Return value is a CONS like (ROWS . COLUMNS) where
4241 ROWS (resp. COLUMNS) is the number of exportable
4242 rows (resp. columns)."
4243 (let (first-row (columns 0) (rows 0))
4244 ;; Set number of rows, and extract first one.
4245 (org-element-map
4246 table 'table-row
4247 (lambda (row)
4248 (when (eq (org-element-property :type row) 'standard)
4249 (incf rows)
4250 (unless first-row (setq first-row row)))) info)
4251 ;; Set number of columns.
4252 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4253 ;; Return value.
4254 (cons rows columns)))
4256 (defun org-export-table-cell-address (table-cell info)
4257 "Return address of a regular TABLE-CELL object.
4259 TABLE-CELL is the cell considered. INFO is a plist used as
4260 a communication channel.
4262 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4263 zero-based index. Only exportable cells are considered. The
4264 function returns nil for other cells."
4265 (let* ((table-row (org-export-get-parent table-cell))
4266 (table (org-export-get-parent-table table-cell)))
4267 ;; Ignore cells in special rows or in special column.
4268 (unless (or (org-export-table-row-is-special-p table-row info)
4269 (and (org-export-table-has-special-column-p table)
4270 (eq (car (org-element-contents table-row)) table-cell)))
4271 (cons
4272 ;; Row number.
4273 (let ((row-count 0))
4274 (org-element-map
4275 table 'table-row
4276 (lambda (row)
4277 (cond ((eq (org-element-property :type row) 'rule) nil)
4278 ((eq row table-row) row-count)
4279 (t (incf row-count) nil)))
4280 info 'first-match))
4281 ;; Column number.
4282 (let ((col-count 0))
4283 (org-element-map
4284 table-row 'table-cell
4285 (lambda (cell)
4286 (if (eq cell table-cell) col-count (incf col-count) nil))
4287 info 'first-match))))))
4289 (defun org-export-get-table-cell-at (address table info)
4290 "Return regular table-cell object at ADDRESS in TABLE.
4292 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4293 zero-based index. TABLE is a table type element. INFO is
4294 a plist used as a communication channel.
4296 If no table-cell, among exportable cells, is found at ADDRESS,
4297 return nil."
4298 (let ((column-pos (cdr address)) (column-count 0))
4299 (org-element-map
4300 ;; Row at (car address) or nil.
4301 (let ((row-pos (car address)) (row-count 0))
4302 (org-element-map
4303 table 'table-row
4304 (lambda (row)
4305 (cond ((eq (org-element-property :type row) 'rule) nil)
4306 ((= row-count row-pos) row)
4307 (t (incf row-count) nil)))
4308 info 'first-match))
4309 'table-cell
4310 (lambda (cell)
4311 (if (= column-count column-pos) cell
4312 (incf column-count) nil))
4313 info 'first-match)))
4316 ;;;; For Tables Of Contents
4318 ;; `org-export-collect-headlines' builds a list of all exportable
4319 ;; headline elements, maybe limited to a certain depth. One can then
4320 ;; easily parse it and transcode it.
4322 ;; Building lists of tables, figures or listings is quite similar.
4323 ;; Once the generic function `org-export-collect-elements' is defined,
4324 ;; `org-export-collect-tables', `org-export-collect-figures' and
4325 ;; `org-export-collect-listings' can be derived from it.
4327 (defun org-export-collect-headlines (info &optional n)
4328 "Collect headlines in order to build a table of contents.
4330 INFO is a plist used as a communication channel.
4332 When optional argument N is an integer, it specifies the depth of
4333 the table of contents. Otherwise, it is set to the value of the
4334 last headline level. See `org-export-headline-levels' for more
4335 information.
4337 Return a list of all exportable headlines as parsed elements."
4338 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4339 (org-element-map
4340 (plist-get info :parse-tree)
4341 'headline
4342 (lambda (headline)
4343 ;; Strip contents from HEADLINE.
4344 (let ((relative-level (org-export-get-relative-level headline info)))
4345 (unless (> relative-level n) headline)))
4346 info))
4348 (defun org-export-collect-elements (type info &optional predicate)
4349 "Collect referenceable elements of a determined type.
4351 TYPE can be a symbol or a list of symbols specifying element
4352 types to search. Only elements with a caption are collected.
4354 INFO is a plist used as a communication channel.
4356 When non-nil, optional argument PREDICATE is a function accepting
4357 one argument, an element of type TYPE. It returns a non-nil
4358 value when that element should be collected.
4360 Return a list of all elements found, in order of appearance."
4361 (org-element-map
4362 (plist-get info :parse-tree) type
4363 (lambda (element)
4364 (and (org-element-property :caption element)
4365 (or (not predicate) (funcall predicate element))
4366 element))
4367 info))
4369 (defun org-export-collect-tables (info)
4370 "Build a list of tables.
4371 INFO is a plist used as a communication channel.
4373 Return a list of table elements with a caption."
4374 (org-export-collect-elements 'table info))
4376 (defun org-export-collect-figures (info predicate)
4377 "Build a list of figures.
4379 INFO is a plist used as a communication channel. PREDICATE is
4380 a function which accepts one argument: a paragraph element and
4381 whose return value is non-nil when that element should be
4382 collected.
4384 A figure is a paragraph type element, with a caption, verifying
4385 PREDICATE. The latter has to be provided since a \"figure\" is
4386 a vague concept that may depend on back-end.
4388 Return a list of elements recognized as figures."
4389 (org-export-collect-elements 'paragraph info predicate))
4391 (defun org-export-collect-listings (info)
4392 "Build a list of src blocks.
4394 INFO is a plist used as a communication channel.
4396 Return a list of src-block elements with a caption."
4397 (org-export-collect-elements 'src-block info))
4400 ;;;; For Timestamps
4402 ;; `org-export-timestamp-has-time-p' is a predicate to know if hours
4403 ;; and minutes are defined in a given timestamp.
4405 ;; `org-export-format-timestamp' allows to format a timestamp object
4406 ;; with an arbitrary format string.
4408 (defun org-export-timestamp-has-time-p (timestamp)
4409 "Non-nil when TIMESTAMP has a time specified."
4410 (org-element-property :hour-start timestamp))
4412 (defun org-export-format-timestamp (timestamp format &optional end utc)
4413 "Format a TIMESTAMP element into a string.
4415 FORMAT is a format specifier to be passed to
4416 `format-time-string'.
4418 When optional argument END is non-nil, use end of date-range or
4419 time-range, if possible.
4421 When optional argument UTC is non-nil, time will be expressed as
4422 Universal Time."
4423 (format-time-string
4424 format
4425 (apply 'encode-time
4426 (cons 0
4427 (mapcar
4428 (lambda (prop) (or (org-element-property prop timestamp) 0))
4429 (if end '(:minute-end :hour-end :day-end :month-end :year-end)
4430 '(:minute-start :hour-start :day-start :month-start
4431 :year-start)))))
4432 utc))
4434 (defun org-export-split-timestamp-range (timestamp &optional end)
4435 "Extract a timestamp object from a date or time range.
4437 TIMESTAMP is a timestamp object. END, when non-nil, means extract
4438 the end of the range. Otherwise, extract its start.
4440 Return a new timestamp object sharing the same parent as
4441 TIMESTAMP."
4442 (let ((type (org-element-property :type timestamp)))
4443 (if (memq type '(active inactive diary)) timestamp
4444 (let ((split-ts (list 'timestamp (copy-sequence (nth 1 timestamp)))))
4445 ;; Set new type.
4446 (org-element-put-property
4447 split-ts :type (if (eq type 'active-range) 'active 'inactive))
4448 ;; Copy start properties over end properties if END is
4449 ;; non-nil. Otherwise, copy end properties over `start' ones.
4450 (let ((p-alist '((:minute-start . :minute-end)
4451 (:hour-start . :hour-end)
4452 (:day-start . :day-end)
4453 (:month-start . :month-end)
4454 (:year-start . :year-end))))
4455 (dolist (p-cell p-alist)
4456 (org-element-put-property
4457 split-ts
4458 (funcall (if end 'car 'cdr) p-cell)
4459 (org-element-property
4460 (funcall (if end 'cdr 'car) p-cell) split-ts)))
4461 ;; Eventually refresh `:raw-value'.
4462 (org-element-put-property split-ts :raw-value nil)
4463 (org-element-put-property
4464 split-ts :raw-value (org-element-interpret-data split-ts)))))))
4466 (defun org-export-translate-timestamp (timestamp &optional boundary)
4467 "Apply `org-translate-time' on a TIMESTAMP object.
4468 When optional argument BOUNDARY is non-nil, it is either the
4469 symbol `start' or `end'. In this case, only translate the
4470 starting or ending part of TIMESTAMP if it is a date or time
4471 range. Otherwise, translate both parts."
4472 (if (and (not boundary)
4473 (memq (org-element-property :type timestamp)
4474 '(active-range inactive-range)))
4475 (concat
4476 (org-translate-time
4477 (org-element-property :raw-value
4478 (org-export-split-timestamp-range timestamp)))
4479 "--"
4480 (org-translate-time
4481 (org-element-property :raw-value
4482 (org-export-split-timestamp-range timestamp t))))
4483 (org-translate-time
4484 (org-element-property
4485 :raw-value
4486 (if (not boundary) timestamp
4487 (org-export-split-timestamp-range timestamp (eq boundary 'end)))))))
4490 ;;;; Smart Quotes
4492 ;; The main function for the smart quotes sub-system is
4493 ;; `org-export-activate-smart-quotes', which replaces every quote in
4494 ;; a given string from the parse tree with its "smart" counterpart.
4496 ;; Dictionary for smart quotes is stored in
4497 ;; `org-export-smart-quotes-alist'.
4499 ;; Internally, regexps matching potential smart quotes (checks at
4500 ;; string boundaries are also necessary) are defined in
4501 ;; `org-export-smart-quotes-regexps'.
4503 (defconst org-export-smart-quotes-alist
4504 '(("de"
4505 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
4506 :texinfo "@quotedblbase{}")
4507 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
4508 :texinfo "@quotedblleft{}")
4509 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
4510 :texinfo "@quotesinglbase{}")
4511 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
4512 :texinfo "@quoteleft{}")
4513 (apostrophe :utf-8 "’" :html "&rsquo;"))
4514 ("en"
4515 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4516 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4517 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4518 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4519 (apostrophe :utf-8 "’" :html "&rsquo;"))
4520 ("es"
4521 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4522 :texinfo "@guillemetleft{}")
4523 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4524 :texinfo "@guillemetright{}")
4525 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4526 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4527 (apostrophe :utf-8 "’" :html "&rsquo;"))
4528 ("fr"
4529 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4530 :texinfo "@guillemetleft{}@tie{}")
4531 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4532 :texinfo "@tie{}@guillemetright{}")
4533 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4534 :texinfo "@guillemetleft{}@tie{}")
4535 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4536 :texinfo "@tie{}@guillemetright{}")
4537 (apostrophe :utf-8 "’" :html "&rsquo;")))
4538 "Smart quotes translations.
4540 Alist whose CAR is a language string and CDR is an alist with
4541 quote type as key and a plist associating various encodings to
4542 their translation as value.
4544 A quote type can be any symbol among `opening-double-quote',
4545 `closing-double-quote', `opening-single-quote',
4546 `closing-single-quote' and `apostrophe'.
4548 Valid encodings include `:utf-8', `:html', `:latex' and
4549 `:texinfo'.
4551 If no translation is found, the quote character is left as-is.")
4553 (defconst org-export-smart-quotes-regexps
4554 (list
4555 ;; Possible opening quote at beginning of string.
4556 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4557 ;; Possible closing quote at beginning of string.
4558 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4559 ;; Possible apostrophe at beginning of string.
4560 "\\`\\('\\)\\S-"
4561 ;; Opening single and double quotes.
4562 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4563 ;; Closing single and double quotes.
4564 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4565 ;; Apostrophe.
4566 "\\S-\\('\\)\\S-"
4567 ;; Possible opening quote at end of string.
4568 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4569 ;; Possible closing quote at end of string.
4570 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4571 ;; Possible apostrophe at end of string.
4572 "\\S-\\('\\)\\'")
4573 "List of regexps matching a quote or an apostrophe.
4574 In every regexp, quote or apostrophe matched is put in group 1.")
4576 (defun org-export-activate-smart-quotes (s encoding info &optional original)
4577 "Replace regular quotes with \"smart\" quotes in string S.
4579 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
4580 `:utf-8'. INFO is a plist used as a communication channel.
4582 The function has to retrieve information about string
4583 surroundings in parse tree. It can only happen with an
4584 unmodified string. Thus, if S has already been through another
4585 process, a non-nil ORIGINAL optional argument will provide that
4586 original string.
4588 Return the new string."
4589 (if (equal s "") ""
4590 (let* ((prev (org-export-get-previous-element (or original s) info))
4591 (pre-blank (and prev (org-element-property :post-blank prev)))
4592 (next (org-export-get-next-element (or original s) info))
4593 (get-smart-quote
4594 (lambda (q type)
4595 ;; Return smart quote associated to a give quote Q, as
4596 ;; a string. TYPE is a symbol among `open', `close' and
4597 ;; `apostrophe'.
4598 (let ((key (case type
4599 (apostrophe 'apostrophe)
4600 (open (if (equal "'" q) 'opening-single-quote
4601 'opening-double-quote))
4602 (otherwise (if (equal "'" q) 'closing-single-quote
4603 'closing-double-quote)))))
4604 (or (plist-get
4605 (cdr (assq key
4606 (cdr (assoc (plist-get info :language)
4607 org-export-smart-quotes-alist))))
4608 encoding)
4609 q)))))
4610 (if (or (equal "\"" s) (equal "'" s))
4611 ;; Only a quote: no regexp can match. We have to check both
4612 ;; sides and decide what to do.
4613 (cond ((and (not prev) (not next)) s)
4614 ((not prev) (funcall get-smart-quote s 'open))
4615 ((and (not next) (zerop pre-blank))
4616 (funcall get-smart-quote s 'close))
4617 ((not next) s)
4618 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
4619 (t (funcall get-smart-quote 'open)))
4620 ;; 1. Replace quote character at the beginning of S.
4621 (cond
4622 ;; Apostrophe?
4623 ((and prev (zerop pre-blank)
4624 (string-match (nth 2 org-export-smart-quotes-regexps) s))
4625 (setq s (replace-match
4626 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4627 nil t s 1)))
4628 ;; Closing quote?
4629 ((and prev (zerop pre-blank)
4630 (string-match (nth 1 org-export-smart-quotes-regexps) s))
4631 (setq s (replace-match
4632 (funcall get-smart-quote (match-string 1 s) 'close)
4633 nil t s 1)))
4634 ;; Opening quote?
4635 ((and (or (not prev) (> pre-blank 0))
4636 (string-match (nth 0 org-export-smart-quotes-regexps) s))
4637 (setq s (replace-match
4638 (funcall get-smart-quote (match-string 1 s) 'open)
4639 nil t s 1))))
4640 ;; 2. Replace quotes in the middle of the string.
4641 (setq s (replace-regexp-in-string
4642 ;; Opening quotes.
4643 (nth 3 org-export-smart-quotes-regexps)
4644 (lambda (text)
4645 (funcall get-smart-quote (match-string 1 text) 'open))
4646 s nil t 1))
4647 (setq s (replace-regexp-in-string
4648 ;; Closing quotes.
4649 (nth 4 org-export-smart-quotes-regexps)
4650 (lambda (text)
4651 (funcall get-smart-quote (match-string 1 text) 'close))
4652 s nil t 1))
4653 (setq s (replace-regexp-in-string
4654 ;; Apostrophes.
4655 (nth 5 org-export-smart-quotes-regexps)
4656 (lambda (text)
4657 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
4658 s nil t 1))
4659 ;; 3. Replace quote character at the end of S.
4660 (cond
4661 ;; Apostrophe?
4662 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
4663 (setq s (replace-match
4664 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4665 nil t s 1)))
4666 ;; Closing quote?
4667 ((and (not next)
4668 (string-match (nth 7 org-export-smart-quotes-regexps) s))
4669 (setq s (replace-match
4670 (funcall get-smart-quote (match-string 1 s) 'close)
4671 nil t s 1)))
4672 ;; Opening quote?
4673 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
4674 (setq s (replace-match
4675 (funcall get-smart-quote (match-string 1 s) 'open)
4676 nil t s 1))))
4677 ;; Return string with smart quotes.
4678 s))))
4680 ;;;; Topology
4682 ;; Here are various functions to retrieve information about the
4683 ;; neighbourhood of a given element or object. Neighbours of interest
4684 ;; are direct parent (`org-export-get-parent'), parent headline
4685 ;; (`org-export-get-parent-headline'), first element containing an
4686 ;; object, (`org-export-get-parent-element'), parent table
4687 ;; (`org-export-get-parent-table'), previous element or object
4688 ;; (`org-export-get-previous-element') and next element or object
4689 ;; (`org-export-get-next-element').
4691 ;; `org-export-get-genealogy' returns the full genealogy of a given
4692 ;; element or object, from closest parent to full parse tree.
4694 (defun org-export-get-parent (blob)
4695 "Return BLOB parent or nil.
4696 BLOB is the element or object considered."
4697 (org-element-property :parent blob))
4699 (defun org-export-get-genealogy (blob)
4700 "Return full genealogy relative to a given element or object.
4702 BLOB is the element or object being considered.
4704 Ancestors are returned from closest to farthest, the last one
4705 being the full parse tree."
4706 (let (genealogy (parent blob))
4707 (while (setq parent (org-element-property :parent parent))
4708 (push parent genealogy))
4709 (nreverse genealogy)))
4711 (defun org-export-get-parent-headline (blob)
4712 "Return BLOB parent headline or nil.
4713 BLOB is the element or object being considered."
4714 (let ((parent blob))
4715 (while (and (setq parent (org-element-property :parent parent))
4716 (not (eq (org-element-type parent) 'headline))))
4717 parent))
4719 (defun org-export-get-parent-element (object)
4720 "Return first element containing OBJECT or nil.
4721 OBJECT is the object to consider."
4722 (let ((parent object))
4723 (while (and (setq parent (org-element-property :parent parent))
4724 (memq (org-element-type parent) org-element-all-objects)))
4725 parent))
4727 (defun org-export-get-parent-table (object)
4728 "Return OBJECT parent table or nil.
4729 OBJECT is either a `table-cell' or `table-element' type object."
4730 (let ((parent object))
4731 (while (and (setq parent (org-element-property :parent parent))
4732 (not (eq (org-element-type parent) 'table))))
4733 parent))
4735 (defun org-export-get-previous-element (blob info &optional n)
4736 "Return previous element or object.
4738 BLOB is an element or object. INFO is a plist used as
4739 a communication channel. Return previous exportable element or
4740 object, a string, or nil.
4742 When optional argument N is a positive integer, return a list
4743 containing up to N siblings before BLOB, from closest to
4744 farthest. With any other non-nil value, return a list containing
4745 all of them."
4746 (let ((siblings
4747 ;; An object can belong to the contents of its parent or
4748 ;; to a secondary string. We check the latter option
4749 ;; first.
4750 (let ((parent (org-export-get-parent blob)))
4751 (or (and (not (memq (org-element-type blob)
4752 org-element-all-elements))
4753 (let ((sec-value
4754 (org-element-property
4755 (cdr (assq (org-element-type parent)
4756 org-element-secondary-value-alist))
4757 parent)))
4758 (and (memq blob sec-value) sec-value)))
4759 (org-element-contents parent))))
4760 prev)
4761 (catch 'exit
4762 (mapc (lambda (obj)
4763 (cond ((memq obj (plist-get info :ignore-list)))
4764 ((null n) (throw 'exit obj))
4765 ((not (wholenump n)) (push obj prev))
4766 ((zerop n) (throw 'exit (nreverse prev)))
4767 (t (decf n) (push obj prev))))
4768 (cdr (memq blob (reverse siblings))))
4769 (nreverse prev))))
4771 (defun org-export-get-next-element (blob info &optional n)
4772 "Return next element or object.
4774 BLOB is an element or object. INFO is a plist used as
4775 a communication channel. Return next exportable element or
4776 object, a string, or nil.
4778 When optional argument N is a positive integer, return a list
4779 containing up to N siblings after BLOB, from closest to farthest.
4780 With any other non-nil value, return a list containing all of
4781 them."
4782 (let ((siblings
4783 ;; An object can belong to the contents of its parent or to
4784 ;; a secondary string. We check the latter option first.
4785 (let ((parent (org-export-get-parent blob)))
4786 (or (and (not (memq (org-element-type blob)
4787 org-element-all-objects))
4788 (let ((sec-value
4789 (org-element-property
4790 (cdr (assq (org-element-type parent)
4791 org-element-secondary-value-alist))
4792 parent)))
4793 (cdr (memq blob sec-value))))
4794 (cdr (memq blob (org-element-contents parent))))))
4795 next)
4796 (catch 'exit
4797 (mapc (lambda (obj)
4798 (cond ((memq obj (plist-get info :ignore-list)))
4799 ((null n) (throw 'exit obj))
4800 ((not (wholenump n)) (push obj next))
4801 ((zerop n) (throw 'exit (nreverse next)))
4802 (t (decf n) (push obj next))))
4803 siblings)
4804 (nreverse next))))
4807 ;;;; Translation
4809 ;; `org-export-translate' translates a string according to language
4810 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4811 ;; variable and a specified charset. `org-export-dictionary' contains
4812 ;; the dictionary used for the translation.
4814 (defconst org-export-dictionary
4815 '(("Author"
4816 ("ca" :default "Autor")
4817 ("cs" :default "Autor")
4818 ("da" :default "Ophavsmand")
4819 ("de" :default "Autor")
4820 ("eo" :html "A&#365;toro")
4821 ("es" :default "Autor")
4822 ("fi" :html "Tekij&auml;")
4823 ("fr" :default "Auteur")
4824 ("hu" :default "Szerz&otilde;")
4825 ("is" :html "H&ouml;fundur")
4826 ("it" :default "Autore")
4827 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4828 ("nl" :default "Auteur")
4829 ("no" :default "Forfatter")
4830 ("nb" :default "Forfatter")
4831 ("nn" :default "Forfattar")
4832 ("pl" :default "Autor")
4833 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4834 ("sv" :html "F&ouml;rfattare")
4835 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4836 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4837 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4838 ("Date"
4839 ("ca" :default "Data")
4840 ("cs" :default "Datum")
4841 ("da" :default "Dato")
4842 ("de" :default "Datum")
4843 ("eo" :default "Dato")
4844 ("es" :default "Fecha")
4845 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4846 ("hu" :html "D&aacute;tum")
4847 ("is" :default "Dagsetning")
4848 ("it" :default "Data")
4849 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4850 ("nl" :default "Datum")
4851 ("no" :default "Dato")
4852 ("nb" :default "Dato")
4853 ("nn" :default "Dato")
4854 ("pl" :default "Data")
4855 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4856 ("sv" :default "Datum")
4857 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4858 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4859 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4860 ("Equation"
4861 ("fr" :ascii "Equation" :default "Équation"))
4862 ("Figure")
4863 ("Footnotes"
4864 ("ca" :html "Peus de p&agrave;gina")
4865 ("cs" :default "Pozn\xe1mky pod carou")
4866 ("da" :default "Fodnoter")
4867 ("de" :html "Fu&szlig;noten")
4868 ("eo" :default "Piednotoj")
4869 ("es" :html "Pies de p&aacute;gina")
4870 ("fi" :default "Alaviitteet")
4871 ("fr" :default "Notes de bas de page")
4872 ("hu" :html "L&aacute;bjegyzet")
4873 ("is" :html "Aftanm&aacute;lsgreinar")
4874 ("it" :html "Note a pi&egrave; di pagina")
4875 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4876 ("nl" :default "Voetnoten")
4877 ("no" :default "Fotnoter")
4878 ("nb" :default "Fotnoter")
4879 ("nn" :default "Fotnotar")
4880 ("pl" :default "Przypis")
4881 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4882 ("sv" :default "Fotnoter")
4883 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4884 :utf-8 "Примітки")
4885 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4886 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4887 ("List of Listings"
4888 ("fr" :default "Liste des programmes"))
4889 ("List of Tables"
4890 ("fr" :default "Liste des tableaux"))
4891 ("Listing %d:"
4892 ("fr"
4893 :ascii "Programme %d :" :default "Programme nº %d :"
4894 :latin1 "Programme %d :"))
4895 ("Listing %d: %s"
4896 ("fr"
4897 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4898 :latin1 "Programme %d : %s"))
4899 ("See section %s"
4900 ("fr" :default "cf. section %s"))
4901 ("Table %d:"
4902 ("fr"
4903 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4904 ("Table %d: %s"
4905 ("fr"
4906 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4907 :latin1 "Tableau %d : %s"))
4908 ("Table of Contents"
4909 ("ca" :html "&Iacute;ndex")
4910 ("cs" :default "Obsah")
4911 ("da" :default "Indhold")
4912 ("de" :default "Inhaltsverzeichnis")
4913 ("eo" :default "Enhavo")
4914 ("es" :html "&Iacute;ndice")
4915 ("fi" :html "Sis&auml;llysluettelo")
4916 ("fr" :ascii "Sommaire" :default "Table des matières")
4917 ("hu" :html "Tartalomjegyz&eacute;k")
4918 ("is" :default "Efnisyfirlit")
4919 ("it" :default "Indice")
4920 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4921 ("nl" :default "Inhoudsopgave")
4922 ("no" :default "Innhold")
4923 ("nb" :default "Innhold")
4924 ("nn" :default "Innhald")
4925 ("pl" :html "Spis tre&#x015b;ci")
4926 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4927 :utf-8 "Содержание")
4928 ("sv" :html "Inneh&aring;ll")
4929 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4930 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4931 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4932 ("Unknown reference"
4933 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4934 "Dictionary for export engine.
4936 Alist whose CAR is the string to translate and CDR is an alist
4937 whose CAR is the language string and CDR is a plist whose
4938 properties are possible charsets and values translated terms.
4940 It is used as a database for `org-export-translate'. Since this
4941 function returns the string as-is if no translation was found,
4942 the variable only needs to record values different from the
4943 entry.")
4945 (defun org-export-translate (s encoding info)
4946 "Translate string S according to language specification.
4948 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4949 and `:utf-8'. INFO is a plist used as a communication channel.
4951 Translation depends on `:language' property. Return the
4952 translated string. If no translation is found, try to fall back
4953 to `:default' encoding. If it fails, return S."
4954 (let* ((lang (plist-get info :language))
4955 (translations (cdr (assoc lang
4956 (cdr (assoc s org-export-dictionary))))))
4957 (or (plist-get translations encoding)
4958 (plist-get translations :default)
4959 s)))
4963 ;;; Asynchronous Export
4965 ;; `org-export-async-start' is the entry point for asynchronous
4966 ;; export. It recreates current buffer (including visibility,
4967 ;; narrowing and visited file) in an external Emacs process, and
4968 ;; evaluates a command there. It then applies a function on the
4969 ;; returned results in the current process.
4971 ;; Asynchronously generated results are never displayed directly.
4972 ;; Instead, they are stored in `org-export-stack-contents'. They can
4973 ;; then be retrieved by calling `org-export-stack'.
4975 ;; Export Stack is viewed through a dedicated major mode
4976 ;;`org-export-stack-mode' and tools: `org-export--stack-refresh',
4977 ;;`org-export--stack-delete', `org-export--stack-view' and
4978 ;;`org-export--stack-clear'.
4980 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
4981 ;; It should used whenever `org-export-async-start' is called.
4983 (defmacro org-export-async-start (fun &rest body)
4984 "Call function FUN on the results returned by BODY evaluation.
4986 BODY evaluation happens in an asynchronous process, from a buffer
4987 which is an exact copy of the current one.
4989 Use `org-export-add-to-stack' in FUN in order to register results
4990 in the stack. Examples for, respectively a temporary buffer and
4991 a file are:
4993 \(org-export-async-start
4994 \(lambda (output)
4995 \(with-current-buffer (get-buffer-create \"*Org BACKEND Export*\")
4996 \(erase-buffer)
4997 \(insert output)
4998 \(goto-char (point-min))
4999 \(org-export-add-to-stack (current-buffer) 'backend)))
5000 `(org-export-as 'backend ,subtreep ,visible-only ,body-only ',ext-plist))
5004 \(org-export-async-start
5005 \(lambda (f) (org-export-add-to-stack f 'backend))
5006 `(expand-file-name
5007 \(org-export-to-file
5008 'backend ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))"
5009 (declare (indent 1) (debug t))
5010 (org-with-gensyms (process temp-file copy-fun proc-buffer handler)
5011 ;; Write the full sexp evaluating BODY in a copy of the current
5012 ;; buffer to a temporary file, as it may be too long for program
5013 ;; args in `start-process'.
5014 `(with-temp-message "Initializing asynchronous export process"
5015 (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
5016 (,temp-file (make-temp-file "org-export-process")))
5017 (with-temp-file ,temp-file
5018 (insert
5019 (format
5020 "%S"
5021 `(with-temp-buffer
5022 ,(when org-export-async-debug '(setq debug-on-error t))
5023 ;; Initialize `org-mode' in the external process.
5024 (org-mode)
5025 ;; Re-create current buffer there.
5026 (funcall ,,copy-fun)
5027 (restore-buffer-modified-p nil)
5028 ;; Sexp to evaluate in the buffer.
5029 (print (progn ,,@body))))))
5030 ;; Start external process.
5031 (let* ((process-connection-type nil)
5032 (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
5033 (,process
5034 (start-process
5035 "org-export-process" ,proc-buffer
5036 (expand-file-name invocation-name invocation-directory)
5037 "-Q" "--batch"
5038 "-l" org-export-async-init-file
5039 "-l" ,temp-file)))
5040 ;; Register running process in stack.
5041 (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
5042 ;; Set-up sentinel in order to catch results.
5043 (set-process-sentinel
5044 ,process
5045 (let ((handler #',fun))
5046 `(lambda (p status)
5047 (let ((proc-buffer (process-buffer p)))
5048 (when (eq (process-status p) 'exit)
5049 (unwind-protect
5050 (if (zerop (process-exit-status p))
5051 (unwind-protect
5052 (let ((results
5053 (with-current-buffer proc-buffer
5054 (goto-char (point-max))
5055 (backward-sexp)
5056 (read (current-buffer)))))
5057 (funcall ,handler results))
5058 (unless org-export-async-debug
5059 (and (get-buffer proc-buffer)
5060 (kill-buffer proc-buffer))))
5061 (org-export-add-to-stack proc-buffer nil p)
5062 (ding)
5063 (message "Process '%s' exited abnormally" p))
5064 (unless org-export-async-debug
5065 (delete-file ,,temp-file)))))))))))))
5067 (defun org-export-add-to-stack (source backend &optional process)
5068 "Add a new result to export stack if not present already.
5070 SOURCE is a buffer or a file name containing export results.
5071 BACKEND is a symbol representing export back-end used to generate
5074 Entries already pointing to SOURCE and unavailable entries are
5075 removed beforehand. Return the new stack."
5076 (setq org-export-stack-contents
5077 (cons (list source backend (or process (current-time)))
5078 (org-export--stack-remove source))))
5080 (defun org-export-stack ()
5081 "Menu for asynchronous export results and running processes."
5082 (interactive)
5083 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5084 (set-buffer buffer)
5085 (when (zerop (buffer-size)) (org-export-stack-mode))
5086 (org-export--stack-refresh)
5087 (pop-to-buffer buffer))
5088 (message "Type \"q\" to quit, \"?\" for help"))
5090 (defun org-export--stack-source-at-point ()
5091 "Return source from export results at point in stack."
5092 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
5093 (if (not source) (error "Source unavailable, please refresh buffer")
5094 (let ((source-name (if (stringp source) source (buffer-name source))))
5095 (if (save-excursion
5096 (beginning-of-line)
5097 (looking-at (concat ".* +" (regexp-quote source-name) "$")))
5098 source
5099 ;; SOURCE is not consistent with current line. The stack
5100 ;; view is outdated.
5101 (error "Source unavailable; type `g' to update buffer"))))))
5103 (defun org-export--stack-clear ()
5104 "Remove all entries from export stack."
5105 (interactive)
5106 (setq org-export-stack-contents nil))
5108 (defun org-export--stack-refresh (&rest dummy)
5109 "Refresh the asynchronous export stack.
5110 DUMMY is ignored. Unavailable sources are removed from the list.
5111 Return the new stack."
5112 (let ((inhibit-read-only t))
5113 (org-preserve-lc
5114 (erase-buffer)
5115 (insert (concat
5116 (let ((counter 0))
5117 (mapconcat
5118 (lambda (entry)
5119 (let ((proc-p (processp (nth 2 entry))))
5120 (concat
5121 ;; Back-end.
5122 (format " %-12s " (or (nth 1 entry) ""))
5123 ;; Age.
5124 (let ((data (nth 2 entry)))
5125 (if proc-p (format " %6s " (process-status data))
5126 ;; Compute age of the results.
5127 (org-format-seconds
5128 "%4h:%.2m "
5129 (float-time (time-since data)))))
5130 ;; Source.
5131 (format " %s"
5132 (let ((source (car entry)))
5133 (if (stringp source) source
5134 (buffer-name source)))))))
5135 ;; Clear stack from exited processes, dead buffers or
5136 ;; non-existent files.
5137 (setq org-export-stack-contents
5138 (org-remove-if-not
5139 (lambda (el)
5140 (if (processp (nth 2 el))
5141 (buffer-live-p (process-buffer (nth 2 el)))
5142 (let ((source (car el)))
5143 (if (bufferp source) (buffer-live-p source)
5144 (file-exists-p source)))))
5145 org-export-stack-contents)) "\n")))))))
5147 (defun org-export--stack-remove (&optional source)
5148 "Remove export results at point from stack.
5149 If optional argument SOURCE is non-nil, remove it instead."
5150 (interactive)
5151 (let ((source (or source (org-export--stack-source-at-point))))
5152 (setq org-export-stack-contents
5153 (org-remove-if (lambda (el) (equal (car el) source))
5154 org-export-stack-contents))))
5156 (defun org-export--stack-view ()
5157 "View export results at point in stack."
5158 (interactive)
5159 (let ((source (org-export--stack-source-at-point)))
5160 (cond ((processp source)
5161 (org-switch-to-buffer-other-window (process-buffer source)))
5162 ((bufferp source) (org-switch-to-buffer-other-window source))
5163 (t (org-open-file source)))))
5165 (defconst org-export-stack-mode-map
5166 (let ((km (make-sparse-keymap)))
5167 (define-key km " " 'next-line)
5168 (define-key km "n" 'next-line)
5169 (define-key km "\C-n" 'next-line)
5170 (define-key km [down] 'next-line)
5171 (define-key km "p" 'previous-line)
5172 (define-key km "\C-p" 'previous-line)
5173 (define-key km "\C-?" 'previous-line)
5174 (define-key km [up] 'previous-line)
5175 (define-key km "C" 'org-export--stack-clear)
5176 (define-key km "v" 'org-export--stack-view)
5177 (define-key km (kbd "RET") 'org-export--stack-view)
5178 (define-key km "d" 'org-export--stack-remove)
5180 "Keymap for Org Export Stack.")
5182 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5183 "Mode for displaying asynchronous export stack.
5185 Type \\[org-export-stack] to visualize the asynchronous export
5186 stack.
5188 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export--stack-view] to view export output
5189 on current line, \\[org-export--stack-remove] to remove it from the stack and \\[org-export--stack-clear] to clear
5190 stack completely.
5192 Removal entries in an Org Export Stack buffer doesn't affect
5193 files or buffers, only view in the stack.
5195 \\{org-export-stack-mode-map}"
5196 (abbrev-mode 0)
5197 (auto-fill-mode 0)
5198 (setq buffer-read-only t
5199 buffer-undo-list t
5200 truncate-lines t
5201 header-line-format
5202 '(:eval
5203 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
5204 (add-hook 'post-command-hook 'org-export--stack-refresh nil t)
5205 (set (make-local-variable 'revert-buffer-function)
5206 'org-export--stack-refresh))
5210 ;;; The Dispatcher
5212 ;; `org-export-dispatch' is the standard interactive way to start an
5213 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
5214 ;; for its interface, which, in turn, delegates response to key
5215 ;; pressed to `org-export-dispatch-action'.
5217 ;;;###autoload
5218 (defun org-export-dispatch (&optional arg)
5219 "Export dispatcher for Org mode.
5221 It provides an access to common export related tasks in a buffer.
5222 Its interface comes in two flavours: standard and expert. While
5223 both share the same set of bindings, only the former displays the
5224 valid keys associations. Set `org-export-dispatch-use-expert-ui'
5225 to switch to one or the other.
5227 When called with C-u prefix ARG, repeat the last export action,
5228 with the same set of options used back then, on the current
5229 buffer.
5231 When called with a double universal argument, display the
5232 asynchronous export stack directly."
5233 (interactive "P")
5234 (let* ((input
5235 (cond ((equal arg '(16)) '(stack))
5236 ((and arg org-export-dispatch-last-action))
5237 (t (save-window-excursion
5238 (unwind-protect
5239 ;; Store this export command.
5240 (setq org-export-dispatch-last-action
5241 (org-export-dispatch-ui
5242 (list org-export-initial-scope
5243 (and org-export-in-background 'async))
5245 org-export-dispatch-use-expert-ui))
5246 (and (get-buffer "*Org Export Dispatcher*")
5247 (kill-buffer "*Org Export Dispatcher*")))))))
5248 (action (car input))
5249 (optns (cdr input)))
5250 (case action
5251 ;; First handle special hard-coded actions.
5252 (stack (org-export-stack))
5253 (publish-current-file
5254 (org-e-publish-current-file (memq 'force optns) (memq 'async optns)))
5255 (publish-current-project
5256 (org-e-publish-current-project (memq 'force optns) (memq 'async optns)))
5257 (publish-choose-project
5258 (org-e-publish (assoc (org-icompleting-read
5259 "Publish project: "
5260 org-e-publish-project-alist nil t)
5261 org-e-publish-project-alist)
5262 (memq 'force optns)
5263 (memq 'async optns)))
5264 (publish-all (org-e-publish-all (memq 'force optns) (memq 'async optns)))
5265 (otherwise (funcall action
5266 ;; Return a symbol instead of a list to ease
5267 ;; asynchronous export macro use.
5268 (and (memq 'async optns) t)
5269 (and (memq 'subtree optns) t)
5270 (and (memq 'visible optns) t)
5271 (and (memq 'body optns) t))))))
5273 (defun org-export-dispatch-ui (options first-key expertp)
5274 "Handle interface for `org-export-dispatch'.
5276 OPTIONS is a list containing current interactive options set for
5277 export. It can contain any of the following symbols:
5278 `body' toggles a body-only export
5279 `subtree' restricts export to current subtree
5280 `visible' restricts export to visible part of buffer.
5281 `force' force publishing files.
5282 `async' use asynchronous export process
5284 FIRST-KEY is the key pressed to select the first level menu. It
5285 is nil when this menu hasn't been selected yet.
5287 EXPERTP, when non-nil, triggers expert UI. In that case, no help
5288 buffer is provided, but indications about currently active
5289 options are given in the prompt. Moreover, \[?] allows to switch
5290 back to standard interface."
5291 (let* ((fontify-key
5292 (lambda (key &optional access-key)
5293 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
5294 ;; non-nil is the required first-level key to activate
5295 ;; KEY. When its value is t, activate KEY independently
5296 ;; on the first key, if any. A nil value means KEY will
5297 ;; only be activated at first level.
5298 (if (or (eq access-key t) (eq access-key first-key))
5299 (org-add-props key nil 'face 'org-warning)
5300 (org-no-properties key))))
5301 ;; Prepare menu entries by extracting them from
5302 ;; `org-export-registered-backends', and sorting them by
5303 ;; access key and by ordinal, if any.
5304 (backends (sort
5305 (sort
5306 (delq nil
5307 (mapcar (lambda (b)
5308 (org-export-backend-menu (car b)))
5309 org-export-registered-backends))
5310 (lambda (a b)
5311 (let ((key-a (nth 1 a))
5312 (key-b (nth 1 b)))
5313 (cond ((and (numberp key-a) (numberp key-b))
5314 (< key-a key-b))
5315 ((numberp key-b) t)))))
5316 (lambda (a b) (< (car a) (car b)))))
5317 ;; Compute a list of allowed keys based on the first key
5318 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4, ?5 and ?q)
5319 ;; are always available.
5320 (allowed-keys
5321 (nconc (list ?1 ?2 ?3 ?4 ?5)
5322 (if (not first-key) (org-uniquify (mapcar 'car backends))
5323 (let (sub-menu)
5324 (dolist (backend backends (sort (mapcar 'car sub-menu) '<))
5325 (when (eq (car backend) first-key)
5326 (setq sub-menu (append (nth 2 backend) sub-menu))))))
5327 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
5328 ((not first-key) (list ?P)))
5329 (list ?&)
5330 (when expertp (list ??))
5331 (list ?q)))
5332 ;; Build the help menu for standard UI.
5333 (help
5334 (unless expertp
5335 (concat
5336 ;; Options are hard-coded.
5337 (format "Options
5338 [%s] Body only: %s [%s] Visible only: %s
5339 [%s] Export scope: %s [%s] Force publishing: %s
5340 [%s] Asynchronous export: %s\n"
5341 (funcall fontify-key "1" t)
5342 (if (memq 'body options) "On " "Off")
5343 (funcall fontify-key "2" t)
5344 (if (memq 'visible options) "On " "Off")
5345 (funcall fontify-key "3" t)
5346 (if (memq 'subtree options) "Subtree" "Buffer ")
5347 (funcall fontify-key "4" t)
5348 (if (memq 'force options) "On " "Off")
5349 (funcall fontify-key "5" t)
5350 (if (memq 'async options) "On " "Off"))
5351 ;; Display registered back-end entries. When a key
5352 ;; appears for the second time, do not create another
5353 ;; entry, but append its sub-menu to existing menu.
5354 (let (last-key)
5355 (mapconcat
5356 (lambda (entry)
5357 (let ((top-key (car entry)))
5358 (concat
5359 (unless (eq top-key last-key)
5360 (setq last-key top-key)
5361 (format "\n[%s] %s\n"
5362 (funcall fontify-key (char-to-string top-key))
5363 (nth 1 entry)))
5364 (let ((sub-menu (nth 2 entry)))
5365 (unless (functionp sub-menu)
5366 ;; Split sub-menu into two columns.
5367 (let ((index -1))
5368 (concat
5369 (mapconcat
5370 (lambda (sub-entry)
5371 (incf index)
5372 (format
5373 (if (zerop (mod index 2)) " [%s] %-24s"
5374 "[%s] %s\n")
5375 (funcall fontify-key
5376 (char-to-string (car sub-entry))
5377 top-key)
5378 (nth 1 sub-entry)))
5379 sub-menu "")
5380 (when (zerop (mod index 2)) "\n"))))))))
5381 backends ""))
5382 ;; Publishing menu is hard-coded.
5383 (format "\n[%s] Publish
5384 [%s] Current file [%s] Current project
5385 [%s] Choose project [%s] All projects\n\n"
5386 (funcall fontify-key "P")
5387 (funcall fontify-key "f" ?P)
5388 (funcall fontify-key "p" ?P)
5389 (funcall fontify-key "x" ?P)
5390 (funcall fontify-key "a" ?P))
5391 (format "\[%s] Export stack\n" (funcall fontify-key "&" t))
5392 (format "\[%s] %s"
5393 (funcall fontify-key "q" t)
5394 (if first-key "Main menu" "Exit")))))
5395 ;; Build prompts for both standard and expert UI.
5396 (standard-prompt (unless expertp "Export command: "))
5397 (expert-prompt
5398 (when expertp
5399 (format
5400 "Export command (Options: %s%s%s%s%s) [%s]: "
5401 (if (memq 'body options) (funcall fontify-key "b" t) "-")
5402 (if (memq 'visible options) (funcall fontify-key "v" t) "-")
5403 (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
5404 (if (memq 'force options) (funcall fontify-key "f" t) "-")
5405 (if (memq 'async options) (funcall fontify-key "a" t) "-")
5406 (concat allowed-keys)))))
5407 ;; With expert UI, just read key with a fancy prompt. In standard
5408 ;; UI, display an intrusive help buffer.
5409 (if expertp
5410 (org-export-dispatch-action
5411 expert-prompt allowed-keys backends options first-key expertp)
5412 ;; At first call, create frame layout in order to display menu.
5413 (unless (get-buffer "*Org Export Dispatcher*")
5414 (delete-other-windows)
5415 (org-switch-to-buffer-other-window
5416 (get-buffer-create "*Org Export Dispatcher*"))
5417 (setq cursor-type nil))
5418 ;; At this point, the buffer containing the menu exists and is
5419 ;; visible in the current window. So, refresh it.
5420 (with-current-buffer "*Org Export Dispatcher*"
5421 (erase-buffer)
5422 (insert help))
5423 (org-fit-window-to-buffer)
5424 (org-export-dispatch-action
5425 standard-prompt allowed-keys backends options first-key expertp))))
5427 (defun org-export-dispatch-action
5428 (prompt allowed-keys backends options first-key expertp)
5429 "Read a character from command input and act accordingly.
5431 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
5432 a list of characters available at a given step in the process.
5433 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
5434 EXPERTP are the same as defined in `org-export-dispatch-ui',
5435 which see.
5437 Toggle export options when required. Otherwise, return value is
5438 a list with action as CAR and a list of interactive export
5439 options as CDR."
5440 (let ((key (let ((k (read-char-exclusive prompt)))
5441 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
5442 ;; depending on user's key pressed.
5443 (if (< k 27) (+ k 96) k))))
5444 (cond
5445 ;; Ignore non-standard characters (i.e. "M-a") and
5446 ;; undefined associations.
5447 ((not (memq key allowed-keys))
5448 (ding)
5449 (unless expertp (message "Invalid key") (sit-for 1))
5450 (org-export-dispatch-ui options first-key expertp))
5451 ;; q key at first level aborts export. At second
5452 ;; level, cancel first key instead.
5453 ((eq key ?q) (if (not first-key) (error "Export aborted")
5454 (org-export-dispatch-ui options nil expertp)))
5455 ;; Help key: Switch back to standard interface if
5456 ;; expert UI was active.
5457 ((eq key ??) (org-export-dispatch-ui options first-key nil))
5458 ;; Switch to asynchronous export stack.
5459 ((eq key ?&) '(stack))
5460 ;; Toggle export options.
5461 ((memq key '(?1 ?2 ?3 ?4 ?5))
5462 (org-export-dispatch-ui
5463 (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
5464 (?4 'force) (?5 'async))))
5465 (if (memq option options) (remq option options)
5466 (cons option options)))
5467 first-key expertp))
5468 ;; Action selected: Send key and options back to
5469 ;; `org-export-dispatch'.
5470 ((or first-key (functionp (nth 2 (assq key backends))))
5471 (cons (cond
5472 ((not first-key) (nth 2 (assq key backends)))
5473 ;; Publishing actions are hard-coded. Send a special
5474 ;; signal to `org-export-dispatch'.
5475 ((eq first-key ?P)
5476 (case key
5477 (?f 'publish-current-file)
5478 (?p 'publish-current-project)
5479 (?x 'publish-choose-project)
5480 (?a 'publish-all)))
5481 ;; Return first action associated to FIRST-KEY + KEY
5482 ;; path. Indeed, derived backends can share the same
5483 ;; FIRST-KEY.
5484 (t (catch 'found
5485 (mapc (lambda (backend)
5486 (let ((match (assq key (nth 2 backend))))
5487 (when match (throw 'found (nth 2 match)))))
5488 (member (assq first-key backends) backends)))))
5489 options))
5490 ;; Otherwise, enter sub-menu.
5491 (t (org-export-dispatch-ui options key expertp)))))
5494 (provide 'org-export)
5495 ;;; org-export.el ends here