org-export: Add a filter for export options
[org-mode.git] / contrib / lisp / org-export.el
blob4f93fcdb6baaf6dc2941de8799e27469e4c29530
1 ;;; org-export.el --- Generic Export Engine For Org
3 ;; Copyright (C) 2012, 2013 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 determines 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 Values set through KEYWORD and OPTION have precedence over
167 DEFAULT.
169 All these properties should be back-end agnostic. Back-end
170 specific properties are set through `org-export-define-backend'.
171 Properties redefined there have precedence over these.")
173 (defconst org-export-special-keywords '("FILETAGS" "SETUP_FILE" "OPTIONS")
174 "List of in-buffer keywords that require special treatment.
175 These keywords are not directly associated to a property. The
176 way they are handled must be hard-coded into
177 `org-export--get-inbuffer-options' function.")
179 (defconst org-export-filters-alist
180 '((:filter-bold . org-export-filter-bold-functions)
181 (:filter-babel-call . org-export-filter-babel-call-functions)
182 (:filter-center-block . org-export-filter-center-block-functions)
183 (:filter-clock . org-export-filter-clock-functions)
184 (:filter-code . org-export-filter-code-functions)
185 (:filter-comment . org-export-filter-comment-functions)
186 (:filter-comment-block . org-export-filter-comment-block-functions)
187 (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
188 (:filter-drawer . org-export-filter-drawer-functions)
189 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
190 (:filter-entity . org-export-filter-entity-functions)
191 (:filter-example-block . org-export-filter-example-block-functions)
192 (:filter-export-block . org-export-filter-export-block-functions)
193 (:filter-export-snippet . org-export-filter-export-snippet-functions)
194 (:filter-final-output . org-export-filter-final-output-functions)
195 (:filter-fixed-width . org-export-filter-fixed-width-functions)
196 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
197 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
198 (:filter-headline . org-export-filter-headline-functions)
199 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
200 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
201 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
202 (:filter-inlinetask . org-export-filter-inlinetask-functions)
203 (:filter-italic . org-export-filter-italic-functions)
204 (:filter-item . org-export-filter-item-functions)
205 (:filter-keyword . org-export-filter-keyword-functions)
206 (:filter-latex-environment . org-export-filter-latex-environment-functions)
207 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
208 (:filter-line-break . org-export-filter-line-break-functions)
209 (:filter-link . org-export-filter-link-functions)
210 (:filter-macro . org-export-filter-macro-functions)
211 (:filter-node-property . org-export-filter-node-property-functions)
212 (:filter-options . org-export-filter-options-functions)
213 (:filter-paragraph . org-export-filter-paragraph-functions)
214 (:filter-parse-tree . org-export-filter-parse-tree-functions)
215 (:filter-plain-list . org-export-filter-plain-list-functions)
216 (:filter-plain-text . org-export-filter-plain-text-functions)
217 (:filter-planning . org-export-filter-planning-functions)
218 (:filter-property-drawer . org-export-filter-property-drawer-functions)
219 (:filter-quote-block . org-export-filter-quote-block-functions)
220 (:filter-quote-section . org-export-filter-quote-section-functions)
221 (:filter-radio-target . org-export-filter-radio-target-functions)
222 (:filter-section . org-export-filter-section-functions)
223 (:filter-special-block . org-export-filter-special-block-functions)
224 (:filter-src-block . org-export-filter-src-block-functions)
225 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
226 (:filter-strike-through . org-export-filter-strike-through-functions)
227 (:filter-subscript . org-export-filter-subscript-functions)
228 (:filter-superscript . org-export-filter-superscript-functions)
229 (:filter-table . org-export-filter-table-functions)
230 (:filter-table-cell . org-export-filter-table-cell-functions)
231 (:filter-table-row . org-export-filter-table-row-functions)
232 (:filter-target . org-export-filter-target-functions)
233 (:filter-timestamp . org-export-filter-timestamp-functions)
234 (:filter-underline . org-export-filter-underline-functions)
235 (:filter-verbatim . org-export-filter-verbatim-functions)
236 (:filter-verse-block . org-export-filter-verse-block-functions))
237 "Alist between filters properties and initial values.
239 The key of each association is a property name accessible through
240 the communication channel. Its value is a configurable global
241 variable defining initial filters.
243 This list is meant to install user specified filters. Back-end
244 developers may install their own filters using
245 `org-export-define-backend'. Filters defined there will always
246 be prepended to the current list, so they always get applied
247 first.")
249 (defconst org-export-default-inline-image-rule
250 `(("file" .
251 ,(format "\\.%s\\'"
252 (regexp-opt
253 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
254 "xpm" "pbm" "pgm" "ppm") t))))
255 "Default rule for link matching an inline image.
256 This rule applies to links with no description. By default, it
257 will be considered as an inline image if it targets a local file
258 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
259 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
260 See `org-export-inline-image-p' for more information about
261 rules.")
263 (defvar org-export-async-debug nil
264 "Non-nil means asynchronous export process should leave data behind.
266 This data is found in the appropriate \"*Org Export Process*\"
267 buffer, and in files prefixed with \"org-export-process\" and
268 located in `temporary-file-directory'.
270 When non-nil, it will also set `debug-on-error' to a non-nil
271 value in the external process.")
273 (defvar org-export-stack-contents nil
274 "Record asynchronously generated export results and processes.
275 This is an alist: its CAR is the source of the
276 result (destination file or buffer for a finished process,
277 original buffer for a running one) and its CDR is a list
278 containing the back-end used, as a symbol, and either a process
279 or the time at which it finished. It is used to build the menu
280 from `org-export-stack'.")
282 (defvar org-export-registered-backends nil
283 "List of backends currently available in the exporter.
285 A backend is stored as a list where CAR is its name, as a symbol,
286 and CDR is a plist with the following properties:
287 `:filters-alist', `:menu-entry', `:options-alist' and
288 `:translate-alist'.
290 This variable is set with `org-export-define-backend' and
291 `org-export-define-derived-backend' functions.")
293 (defvar org-export-dispatch-last-action nil
294 "Last command called from the dispatcher.
295 The value should be a list. Its CAR is the action, as a symbol,
296 and its CDR is a list of export options.")
300 ;;; User-configurable Variables
302 ;; Configuration for the masses.
304 ;; They should never be accessed directly, as their value is to be
305 ;; stored in a property list (cf. `org-export-options-alist').
306 ;; Back-ends will read their value from there instead.
308 (defgroup org-export nil
309 "Options for exporting Org mode files."
310 :tag "Org Export"
311 :group 'org)
313 (defgroup org-export-general nil
314 "General options for export engine."
315 :tag "Org Export General"
316 :group 'org-export)
318 (defcustom org-export-with-archived-trees 'headline
319 "Whether sub-trees with the ARCHIVE tag should be exported.
321 This can have three different values:
322 nil Do not export, pretend this tree is not present.
323 t Do export the entire tree.
324 `headline' Only export the headline, but skip the tree below it.
326 This option can also be set with the #+OPTIONS line,
327 e.g. \"arch:nil\"."
328 :group 'org-export-general
329 :type '(choice
330 (const :tag "Not at all" nil)
331 (const :tag "Headline only" 'headline)
332 (const :tag "Entirely" t)))
334 (defcustom org-export-with-author t
335 "Non-nil means insert author name into the exported file.
336 This option can also be set with the #+OPTIONS line,
337 e.g. \"author:nil\"."
338 :group 'org-export-general
339 :type 'boolean)
341 (defcustom org-export-with-clocks nil
342 "Non-nil means export CLOCK keywords.
343 This option can also be set with the #+OPTIONS line,
344 e.g. \"c:t\"."
345 :group 'org-export-general
346 :type 'boolean)
348 (defcustom org-export-with-creator 'comment
349 "Non-nil means the postamble should contain a creator sentence.
351 The sentence can be set in `org-export-creator-string' and
352 defaults to \"Generated by Org mode XX in Emacs XXX.\".
354 If the value is `comment' insert it as a comment."
355 :group 'org-export-general
356 :type '(choice
357 (const :tag "No creator sentence" nil)
358 (const :tag "Sentence as a comment" 'comment)
359 (const :tag "Insert the sentence" t)))
361 (defcustom org-export-with-date t
362 "Non-nil means insert date in the exported document.
363 This options can also be set with the OPTIONS keyword,
364 e.g. \"date:nil\".")
366 (defcustom org-export-creator-string
367 (format "Generated by Org mode %s in Emacs %s."
368 (if (fboundp 'org-version) (org-version) "(Unknown)")
369 emacs-version)
370 "String to insert at the end of the generated document."
371 :group 'org-export-general
372 :type '(string :tag "Creator string"))
374 (defcustom org-export-with-drawers t
375 "Non-nil means export contents of standard drawers.
377 When t, all drawers are exported. This may also be a list of
378 drawer names to export. This variable doesn't apply to
379 properties drawers.
381 This option can also be set with the #+OPTIONS line,
382 e.g. \"d:nil\"."
383 :group 'org-export-general
384 :type '(choice
385 (const :tag "All drawers" t)
386 (const :tag "None" nil)
387 (repeat :tag "Selected drawers"
388 (string :tag "Drawer name"))))
390 (defcustom org-export-with-email nil
391 "Non-nil means insert author email into the exported file.
392 This option can also be set with the #+OPTIONS line,
393 e.g. \"email:t\"."
394 :group 'org-export-general
395 :type 'boolean)
397 (defcustom org-export-with-emphasize t
398 "Non-nil means interpret *word*, /word/, _word_ and +word+.
400 If the export target supports emphasizing text, the word will be
401 typeset in bold, italic, with an underline or strike-through,
402 respectively.
404 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
405 :group 'org-export-general
406 :type 'boolean)
408 (defcustom org-export-exclude-tags '("noexport")
409 "Tags that exclude a tree from export.
411 All trees carrying any of these tags will be excluded from
412 export. This is without condition, so even subtrees inside that
413 carry one of the `org-export-select-tags' will be removed.
415 This option can also be set with the #+EXCLUDE_TAGS: keyword."
416 :group 'org-export-general
417 :type '(repeat (string :tag "Tag")))
419 (defcustom org-export-with-fixed-width t
420 "Non-nil means lines starting with \":\" will be in fixed width font.
422 This can be used to have pre-formatted text, fragments of code
423 etc. For example:
424 : ;; Some Lisp examples
425 : (while (defc cnt)
426 : (ding))
427 will be looking just like this in also HTML. See also the QUOTE
428 keyword. Not all export backends support this.
430 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
431 :group 'org-export-translation
432 :type 'boolean)
434 (defcustom org-export-with-footnotes t
435 "Non-nil means Org footnotes should be exported.
436 This option can also be set with the #+OPTIONS line,
437 e.g. \"f:nil\"."
438 :group 'org-export-general
439 :type 'boolean)
441 (defcustom org-export-headline-levels 3
442 "The last level which is still exported as a headline.
444 Inferior levels will produce itemize lists when exported.
446 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
447 :group 'org-export-general
448 :type 'integer)
450 (defcustom org-export-default-language "en"
451 "The default language for export and clocktable translations, as a string.
452 This may have an association in
453 `org-clock-clocktable-language-setup'."
454 :group 'org-export-general
455 :type '(string :tag "Language"))
457 (defcustom org-export-preserve-breaks nil
458 "Non-nil means preserve all line breaks when exporting.
460 Normally, in HTML output paragraphs will be reformatted.
462 This option can also be set with the #+OPTIONS line,
463 e.g. \"\\n:t\"."
464 :group 'org-export-general
465 :type 'boolean)
467 (defcustom org-export-with-entities t
468 "Non-nil means interpret entities when exporting.
470 For example, HTML export converts \\alpha to &alpha; and \\AA to
471 &Aring;.
473 For a list of supported names, see the constant `org-entities'
474 and the user option `org-entities-user'.
476 This option can also be set with the #+OPTIONS line,
477 e.g. \"e:nil\"."
478 :group 'org-export-general
479 :type 'boolean)
481 (defcustom org-export-with-inlinetasks t
482 "Non-nil means inlinetasks should be exported.
483 This option can also be set with the #+OPTIONS line,
484 e.g. \"inline:nil\"."
485 :group 'org-export-general
486 :type 'boolean)
488 (defcustom org-export-with-planning nil
489 "Non-nil means include planning info in export.
490 This option can also be set with the #+OPTIONS: line,
491 e.g. \"p:t\"."
492 :group 'org-export-general
493 :type 'boolean)
495 (defcustom org-export-with-priority nil
496 "Non-nil means include priority cookies in export.
497 This option can also be set with the #+OPTIONS line,
498 e.g. \"pri:t\"."
499 :group 'org-export-general
500 :type 'boolean)
502 (defcustom org-export-with-section-numbers t
503 "Non-nil means add section numbers to headlines when exporting.
505 When set to an integer n, numbering will only happen for
506 headlines whose relative level is higher or equal to n.
508 This option can also be set with the #+OPTIONS line,
509 e.g. \"num:t\"."
510 :group 'org-export-general
511 :type 'boolean)
513 (defcustom org-export-select-tags '("export")
514 "Tags that select a tree for export.
516 If any such tag is found in a buffer, all trees that do not carry
517 one of these tags will be ignored during export. Inside trees
518 that are selected like this, you can still deselect a subtree by
519 tagging it with one of the `org-export-exclude-tags'.
521 This option can also be set with the #+SELECT_TAGS: keyword."
522 :group 'org-export-general
523 :type '(repeat (string :tag "Tag")))
525 (defcustom org-export-with-smart-quotes nil
526 "Non-nil means activate smart quotes during export.
527 This option can also be set with the #+OPTIONS: line,
528 e.g. \"':t\"."
529 :group 'org-export-general
530 :type 'boolean)
532 (defcustom org-export-with-special-strings t
533 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
535 When this option is turned on, these strings will be exported as:
537 Org HTML LaTeX UTF-8
538 -----+----------+--------+-------
539 \\- &shy; \\-
540 -- &ndash; -- –
541 --- &mdash; --- —
542 ... &hellip; \\ldots …
544 This option can also be set with the #+OPTIONS line,
545 e.g. \"-:nil\"."
546 :group 'org-export-general
547 :type 'boolean)
549 (defcustom org-export-with-statistics-cookies t
550 "Non-nil means include statistics cookies in export.
551 This option can also be set with the #+OPTIONS: line,
552 e.g. \"stat:nil\""
553 :group 'org-export-general
554 :type 'boolean)
556 (defcustom org-export-with-sub-superscripts t
557 "Non-nil means interpret \"_\" and \"^\" for export.
559 When this option is turned on, you can use TeX-like syntax for
560 sub- and superscripts. Several characters after \"_\" or \"^\"
561 will be considered as a single item - so grouping with {} is
562 normally not needed. For example, the following things will be
563 parsed as single sub- or superscripts.
565 10^24 or 10^tau several digits will be considered 1 item.
566 10^-12 or 10^-tau a leading sign with digits or a word
567 x^2-y^3 will be read as x^2 - y^3, because items are
568 terminated by almost any nonword/nondigit char.
569 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
571 Still, ambiguity is possible - so when in doubt use {} to enclose
572 the sub/superscript. If you set this variable to the symbol
573 `{}', the braces are *required* in order to trigger
574 interpretations as sub/superscript. This can be helpful in
575 documents that need \"_\" frequently in plain text.
577 This option can also be set with the #+OPTIONS line,
578 e.g. \"^:nil\"."
579 :group 'org-export-general
580 :type '(choice
581 (const :tag "Interpret them" t)
582 (const :tag "Curly brackets only" {})
583 (const :tag "Do not interpret them" nil)))
585 (defcustom org-export-with-toc t
586 "Non-nil means create a table of contents in exported files.
588 The TOC contains headlines with levels up
589 to`org-export-headline-levels'. When an integer, include levels
590 up to N in the toc, this may then be different from
591 `org-export-headline-levels', but it will not be allowed to be
592 larger than the number of headline levels. When nil, no table of
593 contents is made.
595 This option can also be set with the #+OPTIONS line,
596 e.g. \"toc:nil\" or \"toc:3\"."
597 :group 'org-export-general
598 :type '(choice
599 (const :tag "No Table of Contents" nil)
600 (const :tag "Full Table of Contents" t)
601 (integer :tag "TOC to level")))
603 (defcustom org-export-with-tables t
604 "If non-nil, lines starting with \"|\" define a table.
605 For example:
607 | Name | Address | Birthday |
608 |-------------+----------+-----------|
609 | Arthur Dent | England | 29.2.2100 |
611 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
612 :group 'org-export-general
613 :type 'boolean)
615 (defcustom org-export-with-tags t
616 "If nil, do not export tags, just remove them from headlines.
618 If this is the symbol `not-in-toc', tags will be removed from
619 table of contents entries, but still be shown in the headlines of
620 the document.
622 This option can also be set with the #+OPTIONS line,
623 e.g. \"tags:nil\"."
624 :group 'org-export-general
625 :type '(choice
626 (const :tag "Off" nil)
627 (const :tag "Not in TOC" not-in-toc)
628 (const :tag "On" t)))
630 (defcustom org-export-with-tasks t
631 "Non-nil means include TODO items for export.
633 This may have the following values:
634 t include tasks independent of state.
635 `todo' include only tasks that are not yet done.
636 `done' include only tasks that are already done.
637 nil ignore all tasks.
638 list of keywords include tasks with these keywords.
640 This option can also be set with the #+OPTIONS line,
641 e.g. \"tasks:nil\"."
642 :group 'org-export-general
643 :type '(choice
644 (const :tag "All tasks" t)
645 (const :tag "No tasks" nil)
646 (const :tag "Not-done tasks" todo)
647 (const :tag "Only done tasks" done)
648 (repeat :tag "Specific TODO keywords"
649 (string :tag "Keyword"))))
651 (defcustom org-export-time-stamp-file t
652 "Non-nil means insert a time stamp into the exported file.
653 The time stamp shows when the file was created.
655 This option can also be set with the #+OPTIONS line,
656 e.g. \"timestamp:nil\"."
657 :group 'org-export-general
658 :type 'boolean)
660 (defcustom org-export-with-timestamps t
661 "Non nil means allow timestamps in export.
663 It can be set to `active', `inactive', t or nil, in order to
664 export, respectively, only active timestamps, only inactive ones,
665 all of them or none.
667 This option can also be set with the #+OPTIONS line, e.g.
668 \"<:nil\"."
669 :group 'org-export-general
670 :type '(choice
671 (const :tag "All timestamps" t)
672 (const :tag "Only active timestamps" active)
673 (const :tag "Only inactive timestamps" inactive)
674 (const :tag "No timestamp" nil)))
676 (defcustom org-export-with-todo-keywords t
677 "Non-nil means include TODO keywords in export.
678 When nil, remove all these keywords from the export."
679 :group 'org-export-general
680 :type 'boolean)
682 (defcustom org-export-allow-BIND 'confirm
683 "Non-nil means allow #+BIND to define local variable values for export.
684 This is a potential security risk, which is why the user must
685 confirm the use of these lines."
686 :group 'org-export-general
687 :type '(choice
688 (const :tag "Never" nil)
689 (const :tag "Always" t)
690 (const :tag "Ask a confirmation for each file" confirm)))
692 (defcustom org-export-snippet-translation-alist nil
693 "Alist between export snippets back-ends and exporter back-ends.
695 This variable allows to provide shortcuts for export snippets.
697 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
698 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
699 HTML code while every other back-end will ignore it."
700 :group 'org-export-general
701 :type '(repeat
702 (cons
703 (string :tag "Shortcut")
704 (string :tag "Back-end"))))
706 (defcustom org-export-coding-system nil
707 "Coding system for the exported file."
708 :group 'org-export-general
709 :type 'coding-system)
711 (defcustom org-export-copy-to-kill-ring t
712 "Non-nil means exported stuff will also be pushed onto the kill ring."
713 :group 'org-export-general
714 :type 'boolean)
716 (defcustom org-export-initial-scope 'buffer
717 "The initial scope when exporting with `org-export-dispatch'.
718 This variable can be either set to `buffer' or `subtree'."
719 :group 'org-export-general
720 :type '(choice
721 (const :tag "Export current buffer" 'buffer)
722 (const :tag "Export current subtree" 'subtree)))
724 (defcustom org-export-show-temporary-export-buffer t
725 "Non-nil means show buffer after exporting to temp buffer.
726 When Org exports to a file, the buffer visiting that file is ever
727 shown, but remains buried. However, when exporting to
728 a temporary buffer, that buffer is popped up in a second window.
729 When this variable is nil, the buffer remains buried also in
730 these cases."
731 :group 'org-export-general
732 :type 'boolean)
734 (defcustom org-export-in-background nil
735 "Non-nil means export and publishing commands will run in background.
736 Results from an asynchronous export are never displayed. You can
737 retrieve them with \\[org-export-stack]."
738 :group 'org-export-general
739 :type 'boolean)
741 (defcustom org-export-async-init-file user-init-file
742 "File used to initialize external export process.
743 Value must be an absolute file name. It defaults to user's
744 initialization file. Though, a specific configuration makes the
745 process faster and the export more portable."
746 :group 'org-export-general
747 :type '(file :must-match t))
749 (defcustom org-export-dispatch-use-expert-ui nil
750 "Non-nil means using a non-intrusive `org-export-dispatch'.
751 In that case, no help buffer is displayed. Though, an indicator
752 for current export scope is added to the prompt (\"b\" when
753 output is restricted to body only, \"s\" when it is restricted to
754 the current subtree, \"v\" when only visible elements are
755 considered for export and \"f\" when publishing functions should
756 be passed the FORCE argument). Also, \[?] allows to switch back
757 to standard mode."
758 :group 'org-export-general
759 :type 'boolean)
763 ;;; Defining Back-ends
765 ;; `org-export-define-backend' is the standard way to define an export
766 ;; back-end. It allows to specify translators, filters, buffer
767 ;; options and a menu entry. If the new back-end shares translators
768 ;; with another back-end, `org-export-define-derived-backend' may be
769 ;; used instead.
771 ;; Internally, a back-end is stored as a list, of which CAR is the
772 ;; name of the back-end, as a symbol, and CDR a plist. Accessors to
773 ;; properties of a given back-end are: `org-export-backend-filters',
774 ;; `org-export-backend-menu', `org-export-backend-options' and
775 ;; `org-export-backend-translate-table'.
777 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
778 ;; when a given back-end hasn't been registered yet.
780 (defmacro org-export-define-backend (backend translators &rest body)
781 "Define a new back-end BACKEND.
783 TRANSLATORS is an alist between object or element types and
784 functions handling them.
786 These functions should return a string without any trailing
787 space, or nil. They must accept three arguments: the object or
788 element itself, its contents or nil when it isn't recursive and
789 the property list used as a communication channel.
791 Contents, when not nil, are stripped from any global indentation
792 \(although the relative one is preserved). They also always end
793 with a single newline character.
795 If, for a given type, no function is found, that element or
796 object type will simply be ignored, along with any blank line or
797 white space at its end. The same will happen if the function
798 returns the nil value. If that function returns the empty
799 string, the type will be ignored, but the blank lines or white
800 spaces will be kept.
802 In addition to element and object types, one function can be
803 associated to the `template' symbol and another one to the
804 `plain-text' symbol.
806 The former returns the final transcoded string, and can be used
807 to add a preamble and a postamble to document's body. It must
808 accept two arguments: the transcoded string and the property list
809 containing export options.
811 The latter, when defined, is to be called on every text not
812 recognized as an element or an object. It must accept two
813 arguments: the text string and the information channel. It is an
814 appropriate place to protect special chars relative to the
815 back-end.
817 BODY can start with pre-defined keyword arguments. The following
818 keywords are understood:
820 :export-block
822 String, or list of strings, representing block names that
823 will not be parsed. This is used to specify blocks that will
824 contain raw code specific to the back-end. These blocks
825 still have to be handled by the relative `export-block' type
826 translator.
828 :filters-alist
830 Alist between filters and function, or list of functions,
831 specific to the back-end. See `org-export-filters-alist' for
832 a list of all allowed filters. Filters defined here
833 shouldn't make a back-end test, as it may prevent back-ends
834 derived from this one to behave properly.
836 :menu-entry
838 Menu entry for the export dispatcher. It should be a list
839 like:
841 \(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
843 where :
845 KEY is a free character selecting the back-end.
847 DESCRIPTION-OR-ORDINAL is either a string or a number.
849 If it is a string, is will be used to name the back-end in
850 its menu entry. If it is a number, the following menu will
851 be displayed as a sub-menu of the back-end with the same
852 KEY. Also, the number will be used to determine in which
853 order such sub-menus will appear (lowest first).
855 ACTION-OR-MENU is either a function or an alist.
857 If it is an action, it will be called with four
858 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
859 BODY-ONLY. See `org-export-as' for further explanations on
860 some of them.
862 If it is an alist, associations should follow the
863 pattern:
865 \(KEY DESCRIPTION ACTION)
867 where KEY, DESCRIPTION and ACTION are described above.
869 Valid values include:
871 \(?m \"My Special Back-end\" my-special-export-function)
875 \(?l \"Export to LaTeX\"
876 \(?p \"As PDF file\" org-e-latex-export-to-pdf)
877 \(?o \"As PDF file and open\"
878 \(lambda (a s v b)
879 \(if a (org-e-latex-export-to-pdf t s v b)
880 \(org-open-file
881 \(org-e-latex-export-to-pdf nil s v b)))))))
883 or the following, which will be added to the previous
884 sub-menu,
886 \(?l 1
887 \((?B \"As TEX buffer (Beamer)\" org-e-beamer-export-as-latex)
888 \(?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)))
890 :options-alist
892 Alist between back-end specific properties introduced in
893 communication channel and how their value are acquired. See
894 `org-export-options-alist' for more information about
895 structure of the values."
896 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
897 (indent 1))
898 (let (export-block filters menu-entry options contents)
899 (while (keywordp (car body))
900 (case (pop body)
901 (:export-block (let ((names (pop body)))
902 (setq export-block
903 (if (consp names) (mapcar 'upcase names)
904 (list (upcase names))))))
905 (:filters-alist (setq filters (pop body)))
906 (:menu-entry (setq menu-entry (pop body)))
907 (:options-alist (setq options (pop body)))
908 (t (pop body))))
909 (setq contents (append (list :translate-alist translators)
910 (and filters (list :filters-alist filters))
911 (and options (list :options-alist options))
912 (and menu-entry (list :menu-entry menu-entry))))
913 `(progn
914 ;; Register back-end.
915 (let ((registeredp (assq ',backend org-export-registered-backends)))
916 (if registeredp (setcdr registeredp ',contents)
917 (push (cons ',backend ',contents) org-export-registered-backends)))
918 ;; Tell parser to not parse EXPORT-BLOCK blocks.
919 ,(when export-block
920 `(mapc
921 (lambda (name)
922 (add-to-list 'org-element-block-name-alist
923 `(,name . org-element-export-block-parser)))
924 ',export-block))
925 ;; Splice in the body, if any.
926 ,@body)))
928 (defmacro org-export-define-derived-backend (child parent &rest body)
929 "Create a new back-end as a variant of an existing one.
931 CHILD is the name of the derived back-end. PARENT is the name of
932 the parent back-end.
934 BODY can start with pre-defined keyword arguments. The following
935 keywords are understood:
937 :export-block
939 String, or list of strings, representing block names that
940 will not be parsed. This is used to specify blocks that will
941 contain raw code specific to the back-end. These blocks
942 still have to be handled by the relative `export-block' type
943 translator.
945 :filters-alist
947 Alist of filters that will overwrite or complete filters
948 defined in PARENT back-end. See `org-export-filters-alist'
949 for a list of allowed filters.
951 :menu-entry
953 Menu entry for the export dispatcher. See
954 `org-export-define-backend' for more information about the
955 expected value.
957 :options-alist
959 Alist of back-end specific properties that will overwrite or
960 complete those defined in PARENT back-end. Refer to
961 `org-export-options-alist' for more information about
962 structure of the values.
964 :translate-alist
966 Alist of element and object types and transcoders that will
967 overwrite or complete transcode table from PARENT back-end.
968 Refer to `org-export-define-backend' for detailed information
969 about transcoders.
971 As an example, here is how one could define \"my-latex\" back-end
972 as a variant of `e-latex' back-end with a custom template
973 function:
975 \(org-export-define-derived-backend my-latex e-latex
976 :translate-alist ((template . my-latex-template-fun)))
978 The back-end could then be called with, for example:
980 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
981 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
982 (indent 2))
983 (org-export-barf-if-invalid-backend parent)
984 (let (export-block filters menu-entry options translators contents)
985 (while (keywordp (car body))
986 (case (pop body)
987 (:export-block (let ((names (pop body)))
988 (setq export-block
989 (if (consp names) (mapcar 'upcase names)
990 (list (upcase names))))))
991 (:filters-alist (setq filters (pop body)))
992 (:menu-entry (setq menu-entry (pop body)))
993 (:options-alist (setq options (pop body)))
994 (:translate-alist (setq translators (pop body)))
995 (t (pop body))))
996 (setq contents (append
997 (list :parent parent)
998 (let ((p-table (org-export-backend-translate-table parent)))
999 (list :translate-alist (append translators p-table)))
1000 (let ((p-filters (org-export-backend-filters parent)))
1001 (list :filters-alist (append filters p-filters)))
1002 (let ((p-options (org-export-backend-options parent)))
1003 (list :options-alist (append options p-options)))
1004 (and menu-entry (list :menu-entry menu-entry))))
1005 `(progn
1006 ;; Register back-end.
1007 (let ((registeredp (assq ',child org-export-registered-backends)))
1008 (if registeredp (setcdr registeredp ',contents)
1009 (push (cons ',child ',contents) org-export-registered-backends)))
1010 ;; Tell parser to not parse EXPORT-BLOCK blocks.
1011 ,(when export-block
1012 `(mapc
1013 (lambda (name)
1014 (add-to-list 'org-element-block-name-alist
1015 `(,name . org-element-export-block-parser)))
1016 ',export-block))
1017 ;; Splice in the body, if any.
1018 ,@body)))
1020 (defun org-export-backend-filters (backend)
1021 "Return filters for BACKEND."
1022 (plist-get (cdr (assq backend org-export-registered-backends))
1023 :filters-alist))
1025 (defun org-export-backend-menu (backend)
1026 "Return menu entry for BACKEND."
1027 (plist-get (cdr (assq backend org-export-registered-backends))
1028 :menu-entry))
1030 (defun org-export-backend-options (backend)
1031 "Return export options for BACKEND."
1032 (plist-get (cdr (assq backend org-export-registered-backends))
1033 :options-alist))
1035 (defun org-export-backend-translate-table (backend)
1036 "Return translate table for BACKEND."
1037 (plist-get (cdr (assq backend org-export-registered-backends))
1038 :translate-alist))
1040 (defun org-export-barf-if-invalid-backend (backend)
1041 "Signal an error if BACKEND isn't defined."
1042 (unless (org-export-backend-translate-table backend)
1043 (error "Unknown \"%s\" back-end: Aborting export" backend)))
1045 (defun org-export-derived-backend-p (backend &rest backends)
1046 "Non-nil if BACKEND is derived from one of BACKENDS."
1047 (let ((parent backend))
1048 (while (and (not (memq parent backends))
1049 (setq parent
1050 (plist-get (cdr (assq parent
1051 org-export-registered-backends))
1052 :parent))))
1053 parent))
1057 ;;; The Communication Channel
1059 ;; During export process, every function has access to a number of
1060 ;; properties. They are of two types:
1062 ;; 1. Environment options are collected once at the very beginning of
1063 ;; the process, out of the original buffer and configuration.
1064 ;; Collecting them is handled by `org-export-get-environment'
1065 ;; function.
1067 ;; Most environment options are defined through the
1068 ;; `org-export-options-alist' variable.
1070 ;; 2. Tree properties are extracted directly from the parsed tree,
1071 ;; just before export, by `org-export-collect-tree-properties'.
1073 ;; Here is the full list of properties available during transcode
1074 ;; process, with their category and their value type.
1076 ;; + `:author' :: Author's name.
1077 ;; - category :: option
1078 ;; - type :: string
1080 ;; + `:back-end' :: Current back-end used for transcoding.
1081 ;; - category :: tree
1082 ;; - type :: symbol
1084 ;; + `:creator' :: String to write as creation information.
1085 ;; - category :: option
1086 ;; - type :: string
1088 ;; + `:date' :: String to use as date.
1089 ;; - category :: option
1090 ;; - type :: string
1092 ;; + `:description' :: Description text for the current data.
1093 ;; - category :: option
1094 ;; - type :: string
1096 ;; + `:email' :: Author's email.
1097 ;; - category :: option
1098 ;; - type :: string
1100 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1101 ;; process.
1102 ;; - category :: option
1103 ;; - type :: list of strings
1105 ;; + `:exported-data' :: Hash table used for memoizing
1106 ;; `org-export-data'.
1107 ;; - category :: tree
1108 ;; - type :: hash table
1110 ;; + `:filetags' :: List of global tags for buffer. Used by
1111 ;; `org-export-get-tags' to get tags with inheritance.
1112 ;; - category :: option
1113 ;; - type :: list of strings
1115 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1116 ;; their definition, as parsed data. Only non-inlined footnotes
1117 ;; are represented in this alist. Also, every definition isn't
1118 ;; guaranteed to be referenced in the parse tree. The purpose of
1119 ;; this property is to preserve definitions from oblivion
1120 ;; (i.e. when the parse tree comes from a part of the original
1121 ;; buffer), it isn't meant for direct use in a back-end. To
1122 ;; retrieve a definition relative to a reference, use
1123 ;; `org-export-get-footnote-definition' instead.
1124 ;; - category :: option
1125 ;; - type :: alist (STRING . LIST)
1127 ;; + `:headline-levels' :: Maximum level being exported as an
1128 ;; headline. Comparison is done with the relative level of
1129 ;; headlines in the parse tree, not necessarily with their
1130 ;; actual level.
1131 ;; - category :: option
1132 ;; - type :: integer
1134 ;; + `:headline-offset' :: Difference between relative and real level
1135 ;; of headlines in the parse tree. For example, a value of -1
1136 ;; means a level 2 headline should be considered as level
1137 ;; 1 (cf. `org-export-get-relative-level').
1138 ;; - category :: tree
1139 ;; - type :: integer
1141 ;; + `:headline-numbering' :: Alist between headlines and their
1142 ;; numbering, as a list of numbers
1143 ;; (cf. `org-export-get-headline-number').
1144 ;; - category :: tree
1145 ;; - type :: alist (INTEGER . LIST)
1147 ;; + `:id-alist' :: Alist between ID strings and destination file's
1148 ;; path, relative to current directory. It is used by
1149 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1150 ;; external file.
1151 ;; - category :: option
1152 ;; - type :: alist (STRING . STRING)
1154 ;; + `:ignore-list' :: List of elements and objects that should be
1155 ;; ignored during export.
1156 ;; - category :: tree
1157 ;; - type :: list of elements and objects
1159 ;; + `:input-file' :: Full path to input file, if any.
1160 ;; - category :: option
1161 ;; - type :: string or nil
1163 ;; + `:keywords' :: List of keywords attached to data.
1164 ;; - category :: option
1165 ;; - type :: string
1167 ;; + `:language' :: Default language used for translations.
1168 ;; - category :: option
1169 ;; - type :: string
1171 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1172 ;; transcoding.
1173 ;; - category :: option
1174 ;; - type :: list (as returned by `org-element-parse-buffer')
1176 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1177 ;; all line breaks.
1178 ;; - category :: option
1179 ;; - type :: symbol (nil, t)
1181 ;; + `:section-numbers' :: Non-nil means transcoding should add
1182 ;; section numbers to headlines.
1183 ;; - category :: option
1184 ;; - type :: symbol (nil, t)
1186 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1187 ;; in transcoding. When such a tag is present, subtrees without
1188 ;; it are de facto excluded from the process. See
1189 ;; `use-select-tags'.
1190 ;; - category :: option
1191 ;; - type :: list of strings
1193 ;; + `:target-list' :: List of targets encountered in the parse tree.
1194 ;; This is used to partly resolve "fuzzy" links
1195 ;; (cf. `org-export-resolve-fuzzy-link').
1196 ;; - category :: tree
1197 ;; - type :: list of strings
1199 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1200 ;; a time stamp in the output.
1201 ;; - category :: option
1202 ;; - type :: symbol (nil, t)
1204 ;; + `:translate-alist' :: Alist between element and object types and
1205 ;; transcoding functions relative to the current back-end.
1206 ;; Special keys `template' and `plain-text' are also possible.
1207 ;; - category :: option
1208 ;; - type :: alist (SYMBOL . FUNCTION)
1210 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1211 ;; also be transcoded. If it is set to the `headline' symbol,
1212 ;; only the archived headline's name is retained.
1213 ;; - category :: option
1214 ;; - type :: symbol (nil, t, `headline')
1216 ;; + `:with-author' :: Non-nil means author's name should be included
1217 ;; in the output.
1218 ;; - category :: option
1219 ;; - type :: symbol (nil, t)
1221 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1222 ;; - category :: option
1223 ;; - type :: symbol (nil, t)
1225 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1226 ;; inserted at the end of the transcoded string. If the value
1227 ;; is `comment', it should be commented.
1228 ;; - category :: option
1229 ;; - type :: symbol (`comment', nil, t)
1231 ;; + `:with-date' :: Non-nil means output should contain a date.
1232 ;; - category :: option
1233 ;; - type :. symbol (nil, t)
1235 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1236 ;; its value is a list of names, only drawers with such names
1237 ;; will be transcoded.
1238 ;; - category :: option
1239 ;; - type :: symbol (nil, t) or list of strings
1241 ;; + `:with-email' :: Non-nil means output should contain author's
1242 ;; email.
1243 ;; - category :: option
1244 ;; - type :: symbol (nil, t)
1246 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1247 ;; interpreted.
1248 ;; - category :: option
1249 ;; - type :: symbol (nil, t)
1251 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1252 ;; strings starting with a colon as a fixed-with (verbatim) area.
1253 ;; - category :: option
1254 ;; - type :: symbol (nil, t)
1256 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1257 ;; footnotes.
1258 ;; - category :: option
1259 ;; - type :: symbol (nil, t)
1261 ;; + `:with-plannings' :: Non-nil means transcoding should include
1262 ;; planning info.
1263 ;; - category :: option
1264 ;; - type :: symbol (nil, t)
1266 ;; + `:with-priority' :: Non-nil means transcoding should include
1267 ;; priority cookies.
1268 ;; - category :: option
1269 ;; - type :: symbol (nil, t)
1271 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1272 ;; plain text.
1273 ;; - category :: option
1274 ;; - type :: symbol (nil, t)
1276 ;; + `:with-special-strings' :: Non-nil means transcoding should
1277 ;; interpret special strings in plain text.
1278 ;; - category :: option
1279 ;; - type :: symbol (nil, t)
1281 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1282 ;; interpret subscript and superscript. With a value of "{}",
1283 ;; only interpret those using curly brackets.
1284 ;; - category :: option
1285 ;; - type :: symbol (nil, {}, t)
1287 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1288 ;; tables.
1289 ;; - category :: option
1290 ;; - type :: symbol (nil, t)
1292 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1293 ;; headlines. A `not-in-toc' value will remove them from the
1294 ;; table of contents, if any, nonetheless.
1295 ;; - category :: option
1296 ;; - type :: symbol (nil, t, `not-in-toc')
1298 ;; + `:with-tasks' :: Non-nil means transcoding should include
1299 ;; headlines with a TODO keyword. A `todo' value will only
1300 ;; include headlines with a todo type keyword while a `done'
1301 ;; value will do the contrary. If a list of strings is provided,
1302 ;; only tasks with keywords belonging to that list will be kept.
1303 ;; - category :: option
1304 ;; - type :: symbol (t, todo, done, nil) or list of strings
1306 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1307 ;; time stamps. Special value `active' (resp. `inactive') ask to
1308 ;; export only active (resp. inactive) timestamps. Otherwise,
1309 ;; completely remove them.
1310 ;; - category :: option
1311 ;; - type :: symbol: (`active', `inactive', t, nil)
1313 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1314 ;; added to the output. An integer value limits its depth.
1315 ;; - category :: option
1316 ;; - type :: symbol (nil, t or integer)
1318 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1319 ;; include TODO keywords.
1320 ;; - category :: option
1321 ;; - type :: symbol (nil, t)
1324 ;;;; Environment Options
1326 ;; Environment options encompass all parameters defined outside the
1327 ;; scope of the parsed data. They come from five sources, in
1328 ;; increasing precedence order:
1330 ;; - Global variables,
1331 ;; - Buffer's attributes,
1332 ;; - Options keyword symbols,
1333 ;; - Buffer keywords,
1334 ;; - Subtree properties.
1336 ;; The central internal function with regards to environment options
1337 ;; is `org-export-get-environment'. It updates global variables with
1338 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1339 ;; the different sources.
1341 ;; The internal functions doing the retrieval are:
1342 ;; `org-export--get-global-options',
1343 ;; `org-export--get-buffer-attributes',
1344 ;; `org-export--parse-option-keyword',
1345 ;; `org-export--get-subtree-options' and
1346 ;; `org-export--get-inbuffer-options'
1348 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1349 ;; take care of the part relative to "#+BIND:" keywords.
1351 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1352 "Collect export options from the current buffer.
1354 Optional argument BACKEND is a symbol specifying which back-end
1355 specific options to read, if any.
1357 When optional argument SUBTREEP is non-nil, assume the export is
1358 done against the current sub-tree.
1360 Third optional argument EXT-PLIST is a property list with
1361 external parameters overriding Org default settings, but still
1362 inferior to file-local settings."
1363 ;; First install #+BIND variables.
1364 (org-export--install-letbind-maybe)
1365 ;; Get and prioritize export options...
1366 (org-combine-plists
1367 ;; ... from global variables...
1368 (org-export--get-global-options backend)
1369 ;; ... from an external property list...
1370 ext-plist
1371 ;; ... from in-buffer settings...
1372 (org-export--get-inbuffer-options
1373 backend
1374 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1375 ;; ... and from subtree, when appropriate.
1376 (and subtreep (org-export--get-subtree-options backend))
1377 ;; Eventually add misc. properties.
1378 (list
1379 :back-end
1380 backend
1381 :translate-alist
1382 (org-export-backend-translate-table backend)
1383 :footnote-definition-alist
1384 ;; Footnotes definitions must be collected in the original
1385 ;; buffer, as there's no insurance that they will still be in
1386 ;; the parse tree, due to possible narrowing.
1387 (let (alist)
1388 (org-with-wide-buffer
1389 (goto-char (point-min))
1390 (while (re-search-forward org-footnote-definition-re nil t)
1391 (let ((def (save-match-data (org-element-at-point))))
1392 (when (eq (org-element-type def) 'footnote-definition)
1393 (push
1394 (cons (org-element-property :label def)
1395 (let ((cbeg (org-element-property :contents-begin def)))
1396 (when cbeg
1397 (org-element--parse-elements
1398 cbeg (org-element-property :contents-end def)
1399 nil nil nil nil (list 'org-data nil)))))
1400 alist))))
1401 alist))
1402 :id-alist
1403 ;; Collect id references.
1404 (let (alist)
1405 (org-with-wide-buffer
1406 (goto-char (point-min))
1407 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1408 (let ((link (org-element-context)))
1409 (when (eq (org-element-type link) 'link)
1410 (let* ((id (org-element-property :path link))
1411 (file (org-id-find-id-file id)))
1412 (when file
1413 (push (cons id (file-relative-name file)) alist)))))))
1414 alist))))
1416 (defun org-export--parse-option-keyword (options &optional backend)
1417 "Parse an OPTIONS line and return values as a plist.
1418 Optional argument BACKEND is a symbol specifying which back-end
1419 specific items to read, if any."
1420 (let* ((all (append org-export-options-alist
1421 (and backend (org-export-backend-options backend))))
1422 ;; Build an alist between #+OPTION: item and property-name.
1423 (alist (delq nil
1424 (mapcar (lambda (e)
1425 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1426 (car e))))
1427 all)))
1428 plist)
1429 (mapc (lambda (e)
1430 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1431 (car e)
1432 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1433 options)
1434 (setq plist (plist-put plist
1435 (cdr e)
1436 (car (read-from-string
1437 (match-string 2 options)))))))
1438 alist)
1439 plist))
1441 (defun org-export--get-subtree-options (&optional backend)
1442 "Get export options in subtree at point.
1443 Optional argument BACKEND is a symbol specifying back-end used
1444 for export. Return options as a plist."
1445 ;; For each buffer keyword, create an headline property setting the
1446 ;; same property in communication channel. The name for the property
1447 ;; is the keyword with "EXPORT_" appended to it.
1448 (org-with-wide-buffer
1449 (let (prop plist)
1450 ;; Make sure point is at an heading.
1451 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t))
1452 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1453 ;; title as its fallback value.
1454 (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
1455 (progn (looking-at org-todo-line-regexp)
1456 (org-match-string-no-properties 3))))
1457 (setq plist
1458 (plist-put
1459 plist :title
1460 (org-element-parse-secondary-string
1461 prop (org-element-restriction 'keyword)))))
1462 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1463 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1464 (setq plist
1465 (nconc plist (org-export--parse-option-keyword prop backend))))
1466 ;; Handle other keywords. TITLE keyword is excluded as it has
1467 ;; been handled already.
1468 (let ((seen '("TITLE")))
1469 (mapc
1470 (lambda (option)
1471 (let ((property (nth 1 option)))
1472 (when (and property (not (member property seen)))
1473 (let* ((subtree-prop (concat "EXPORT_" property))
1474 ;; Export properties are not case-sensitive.
1475 (value (let ((case-fold-search t))
1476 (org-entry-get (point) subtree-prop))))
1477 (push property seen)
1478 (when value
1479 (setq plist
1480 (plist-put
1481 plist
1482 (car option)
1483 (cond
1484 ;; Parse VALUE if required.
1485 ((member property org-element-document-properties)
1486 (org-element-parse-secondary-string
1487 value (org-element-restriction 'keyword)))
1488 ;; If BEHAVIOUR is `split' expected value is
1489 ;; a list of strings, not a string.
1490 ((eq (nth 4 option) 'split) (org-split-string value))
1491 (t value)))))))))
1492 ;; Also look for both general keywords and back-end specific
1493 ;; options if BACKEND is provided.
1494 (append (and backend (org-export-backend-options backend))
1495 org-export-options-alist)))
1496 ;; Return value.
1497 plist)))
1499 (defun org-export--get-inbuffer-options (&optional backend files)
1500 "Return current buffer export options, as a plist.
1502 Optional argument BACKEND, when non-nil, is a symbol specifying
1503 which back-end specific options should also be read in the
1504 process.
1506 Optional argument FILES is a list of setup files names read so
1507 far, used to avoid circular dependencies.
1509 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1510 (org-with-wide-buffer
1511 (goto-char (point-min))
1512 (let ((case-fold-search t) plist)
1513 ;; 1. Special keywords, as in `org-export-special-keywords'.
1514 (let ((special-re
1515 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1516 (while (re-search-forward special-re nil t)
1517 (let ((element (org-element-at-point)))
1518 (when (eq (org-element-type element) 'keyword)
1519 (let* ((key (org-element-property :key element))
1520 (val (org-element-property :value element))
1521 (prop
1522 (cond
1523 ((equal key "SETUP_FILE")
1524 (let ((file
1525 (expand-file-name
1526 (org-remove-double-quotes (org-trim val)))))
1527 ;; Avoid circular dependencies.
1528 (unless (member file files)
1529 (with-temp-buffer
1530 (insert (org-file-contents file 'noerror))
1531 (org-mode)
1532 (org-export--get-inbuffer-options
1533 backend (cons file files))))))
1534 ((equal key "OPTIONS")
1535 (org-export--parse-option-keyword val backend))
1536 ((equal key "FILETAGS")
1537 (list :filetags
1538 (org-uniquify
1539 (append (org-split-string val ":")
1540 (plist-get plist :filetags))))))))
1541 (setq plist (org-combine-plists plist prop)))))))
1542 ;; 2. Standard options, as in `org-export-options-alist'.
1543 (let* ((all (append org-export-options-alist
1544 ;; Also look for back-end specific options if
1545 ;; BACKEND is defined.
1546 (and backend (org-export-backend-options backend))))
1547 ;; Build ALIST between keyword name and property name.
1548 (alist
1549 (delq nil (mapcar
1550 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1551 all)))
1552 ;; Build regexp matching all keywords associated to export
1553 ;; options. Note: the search is case insensitive.
1554 (opt-re (format "^[ \t]*#\\+%s:"
1555 (regexp-opt
1556 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1557 (goto-char (point-min))
1558 (while (re-search-forward opt-re nil t)
1559 (let ((element (org-element-at-point)))
1560 (when (eq (org-element-type element) 'keyword)
1561 (let* ((key (org-element-property :key element))
1562 (val (org-element-property :value element))
1563 (prop (cdr (assoc key alist)))
1564 (behaviour (nth 4 (assq prop all))))
1565 (setq plist
1566 (plist-put
1567 plist prop
1568 ;; Handle value depending on specified BEHAVIOUR.
1569 (case behaviour
1570 (space
1571 (if (not (plist-get plist prop)) (org-trim val)
1572 (concat (plist-get plist prop) " " (org-trim val))))
1573 (newline
1574 (org-trim
1575 (concat (plist-get plist prop) "\n" (org-trim val))))
1576 (split
1577 `(,@(plist-get plist prop) ,@(org-split-string val)))
1578 ('t val)
1579 (otherwise (if (not (plist-member plist prop)) val
1580 (plist-get plist prop))))))))))
1581 ;; Parse keywords specified in
1582 ;; `org-element-document-properties'.
1583 (mapc
1584 (lambda (key)
1585 (let* ((prop (cdr (assoc key alist)))
1586 (value (and prop (plist-get plist prop))))
1587 (when (stringp value)
1588 (setq plist
1589 (plist-put
1590 plist prop
1591 (org-element-parse-secondary-string
1592 value (org-element-restriction 'keyword)))))))
1593 org-element-document-properties))
1594 ;; 3. Return final value.
1595 plist)))
1597 (defun org-export--get-buffer-attributes ()
1598 "Return properties related to buffer attributes, as a plist."
1599 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1600 (list
1601 ;; Store full path of input file name, or nil. For internal use.
1602 :input-file visited-file
1603 :title (or (and visited-file
1604 (file-name-sans-extension
1605 (file-name-nondirectory visited-file)))
1606 (buffer-name (buffer-base-buffer))))))
1608 (defun org-export--get-global-options (&optional backend)
1609 "Return global export options as a plist.
1611 Optional argument BACKEND, if non-nil, is a symbol specifying
1612 which back-end specific export options should also be read in the
1613 process."
1614 (let ((all (append org-export-options-alist
1615 (and backend (org-export-backend-options backend))))
1616 ;; Output value.
1617 plist)
1618 (mapc
1619 (lambda (cell)
1620 (setq plist
1621 (plist-put
1622 plist
1623 (car cell)
1624 ;; Eval default value provided. If keyword is a member
1625 ;; of `org-element-document-properties', parse it as
1626 ;; a secondary string before storing it.
1627 (let ((value (eval (nth 3 cell))))
1628 (if (not (stringp value)) value
1629 (let ((keyword (nth 1 cell)))
1630 (if (not (member keyword org-element-document-properties))
1631 value
1632 (org-element-parse-secondary-string
1633 value (org-element-restriction 'keyword)))))))))
1634 all)
1635 ;; Return value.
1636 plist))
1638 (defvar org-export--allow-BIND-local nil)
1639 (defun org-export--confirm-letbind ()
1640 "Can we use #+BIND values during export?
1641 By default this will ask for confirmation by the user, to divert
1642 possible security risks."
1643 (cond
1644 ((not org-export-allow-BIND) nil)
1645 ((eq org-export-allow-BIND t) t)
1646 ((local-variable-p 'org-export--allow-BIND-local)
1647 org-export--allow-BIND-local)
1648 (t (org-set-local 'org-export--allow-BIND-local
1649 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1651 (defun org-export--install-letbind-maybe ()
1652 "Install the values from #+BIND lines as local variables.
1653 Variables must be installed before in-buffer options are
1654 retrieved."
1655 (let ((case-fold-search t) letbind pair)
1656 (org-with-wide-buffer
1657 (goto-char (point-min))
1658 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1659 (let* ((element (org-element-at-point))
1660 (value (org-element-property :value element)))
1661 (when (and (eq (org-element-type element) 'keyword)
1662 (not (equal value ""))
1663 (org-export--confirm-letbind))
1664 (push (read (format "(%s)" value)) letbind)))))
1665 (dolist (pair (nreverse letbind))
1666 (org-set-local (car pair) (nth 1 pair)))))
1669 ;;;; Tree Properties
1671 ;; Tree properties are information extracted from parse tree. They
1672 ;; are initialized at the beginning of the transcoding process by
1673 ;; `org-export-collect-tree-properties'.
1675 ;; Dedicated functions focus on computing the value of specific tree
1676 ;; properties during initialization. Thus,
1677 ;; `org-export--populate-ignore-list' lists elements and objects that
1678 ;; should be skipped during export, `org-export--get-min-level' gets
1679 ;; the minimal exportable level, used as a basis to compute relative
1680 ;; level for headlines. Eventually
1681 ;; `org-export--collect-headline-numbering' builds an alist between
1682 ;; headlines and their numbering.
1684 (defun org-export-collect-tree-properties (data info)
1685 "Extract tree properties from parse tree.
1687 DATA is the parse tree from which information is retrieved. INFO
1688 is a list holding export options.
1690 Following tree properties are set or updated:
1692 `:exported-data' Hash table used to memoize results from
1693 `org-export-data'.
1695 `:footnote-definition-alist' List of footnotes definitions in
1696 original buffer and current parse tree.
1698 `:headline-offset' Offset between true level of headlines and
1699 local level. An offset of -1 means an headline
1700 of level 2 should be considered as a level
1701 1 headline in the context.
1703 `:headline-numbering' Alist of all headlines as key an the
1704 associated numbering as value.
1706 `:ignore-list' List of elements that should be ignored during
1707 export.
1709 `:target-list' List of all targets in the parse tree.
1711 Return updated plist."
1712 ;; Install the parse tree in the communication channel, in order to
1713 ;; use `org-export-get-genealogy' and al.
1714 (setq info (plist-put info :parse-tree data))
1715 ;; Get the list of elements and objects to ignore, and put it into
1716 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1717 ;; been done during parse tree filtering.
1718 (setq info
1719 (plist-put info
1720 :ignore-list
1721 (append (org-export--populate-ignore-list data info)
1722 (plist-get info :ignore-list))))
1723 ;; Compute `:headline-offset' in order to be able to use
1724 ;; `org-export-get-relative-level'.
1725 (setq info
1726 (plist-put info
1727 :headline-offset
1728 (- 1 (org-export--get-min-level data info))))
1729 ;; Update footnotes definitions list with definitions in parse tree.
1730 ;; This is required since buffer expansion might have modified
1731 ;; boundaries of footnote definitions contained in the parse tree.
1732 ;; This way, definitions in `footnote-definition-alist' are bound to
1733 ;; match those in the parse tree.
1734 (let ((defs (plist-get info :footnote-definition-alist)))
1735 (org-element-map
1736 data 'footnote-definition
1737 (lambda (fn)
1738 (push (cons (org-element-property :label fn)
1739 `(org-data nil ,@(org-element-contents fn)))
1740 defs)))
1741 (setq info (plist-put info :footnote-definition-alist defs)))
1742 ;; Properties order doesn't matter: get the rest of the tree
1743 ;; properties.
1744 (nconc
1745 `(:target-list
1746 ,(org-element-map
1747 data '(keyword target)
1748 (lambda (blob)
1749 (when (or (eq (org-element-type blob) 'target)
1750 (string= (org-element-property :key blob) "TARGET"))
1751 blob)) info)
1752 :headline-numbering ,(org-export--collect-headline-numbering data info)
1753 :exported-data ,(make-hash-table :test 'eq :size 4001))
1754 info))
1756 (defun org-export--get-min-level (data options)
1757 "Return minimum exportable headline's level in DATA.
1758 DATA is parsed tree as returned by `org-element-parse-buffer'.
1759 OPTIONS is a plist holding export options."
1760 (catch 'exit
1761 (let ((min-level 10000))
1762 (mapc
1763 (lambda (blob)
1764 (when (and (eq (org-element-type blob) 'headline)
1765 (not (memq blob (plist-get options :ignore-list))))
1766 (setq min-level
1767 (min (org-element-property :level blob) min-level)))
1768 (when (= min-level 1) (throw 'exit 1)))
1769 (org-element-contents data))
1770 ;; If no headline was found, for the sake of consistency, set
1771 ;; minimum level to 1 nonetheless.
1772 (if (= min-level 10000) 1 min-level))))
1774 (defun org-export--collect-headline-numbering (data options)
1775 "Return numbering of all exportable headlines in a parse tree.
1777 DATA is the parse tree. OPTIONS is the plist holding export
1778 options.
1780 Return an alist whose key is an headline and value is its
1781 associated numbering \(in the shape of a list of numbers\)."
1782 (let ((numbering (make-vector org-export-max-depth 0)))
1783 (org-element-map
1784 data
1785 'headline
1786 (lambda (headline)
1787 (let ((relative-level
1788 (1- (org-export-get-relative-level headline options))))
1789 (cons
1790 headline
1791 (loop for n across numbering
1792 for idx from 0 to org-export-max-depth
1793 when (< idx relative-level) collect n
1794 when (= idx relative-level) collect (aset numbering idx (1+ n))
1795 when (> idx relative-level) do (aset numbering idx 0)))))
1796 options)))
1798 (defun org-export--populate-ignore-list (data options)
1799 "Return list of elements and objects to ignore during export.
1800 DATA is the parse tree to traverse. OPTIONS is the plist holding
1801 export options."
1802 (let* (ignore
1803 walk-data
1804 ;; First find trees containing a select tag, if any.
1805 (selected (org-export--selected-trees data options))
1806 (walk-data
1807 (lambda (data)
1808 ;; Collect ignored elements or objects into IGNORE-LIST.
1809 (let ((type (org-element-type data)))
1810 (if (org-export--skip-p data options selected) (push data ignore)
1811 (if (and (eq type 'headline)
1812 (eq (plist-get options :with-archived-trees) 'headline)
1813 (org-element-property :archivedp data))
1814 ;; If headline is archived but tree below has
1815 ;; to be skipped, add it to ignore list.
1816 (mapc (lambda (e) (push e ignore))
1817 (org-element-contents data))
1818 ;; Move into secondary string, if any.
1819 (let ((sec-prop
1820 (cdr (assq type org-element-secondary-value-alist))))
1821 (when sec-prop
1822 (mapc walk-data (org-element-property sec-prop data))))
1823 ;; Move into recursive objects/elements.
1824 (mapc walk-data (org-element-contents data))))))))
1825 ;; Main call.
1826 (funcall walk-data data)
1827 ;; Return value.
1828 ignore))
1830 (defun org-export--selected-trees (data info)
1831 "Return list of headlines containing a select tag in their tree.
1832 DATA is parsed data as returned by `org-element-parse-buffer'.
1833 INFO is a plist holding export options."
1834 (let* (selected-trees
1835 walk-data ; for byte-compiler.
1836 (walk-data
1837 (function
1838 (lambda (data genealogy)
1839 (case (org-element-type data)
1840 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1841 (org-element-contents data)))
1842 (headline
1843 (let ((tags (org-element-property :tags data)))
1844 (if (loop for tag in (plist-get info :select-tags)
1845 thereis (member tag tags))
1846 ;; When a select tag is found, mark full
1847 ;; genealogy and every headline within the tree
1848 ;; as acceptable.
1849 (setq selected-trees
1850 (append
1851 genealogy
1852 (org-element-map data 'headline 'identity)
1853 selected-trees))
1854 ;; Else, continue searching in tree, recursively.
1855 (mapc
1856 (lambda (el) (funcall walk-data el (cons data genealogy)))
1857 (org-element-contents data))))))))))
1858 (funcall walk-data data nil) selected-trees))
1860 (defun org-export--skip-p (blob options selected)
1861 "Non-nil when element or object BLOB should be skipped during export.
1862 OPTIONS is the plist holding export options. SELECTED, when
1863 non-nil, is a list of headlines belonging to a tree with a select
1864 tag."
1865 (case (org-element-type blob)
1866 (clock (not (plist-get options :with-clocks)))
1867 (drawer
1868 (or (not (plist-get options :with-drawers))
1869 (and (consp (plist-get options :with-drawers))
1870 (not (member (org-element-property :drawer-name blob)
1871 (plist-get options :with-drawers))))))
1872 (headline
1873 (let ((with-tasks (plist-get options :with-tasks))
1874 (todo (org-element-property :todo-keyword blob))
1875 (todo-type (org-element-property :todo-type blob))
1876 (archived (plist-get options :with-archived-trees))
1877 (tags (org-element-property :tags blob)))
1879 ;; Ignore subtrees with an exclude tag.
1880 (loop for k in (plist-get options :exclude-tags)
1881 thereis (member k tags))
1882 ;; When a select tag is present in the buffer, ignore any tree
1883 ;; without it.
1884 (and selected (not (memq blob selected)))
1885 ;; Ignore commented sub-trees.
1886 (org-element-property :commentedp blob)
1887 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1888 (and (not archived) (org-element-property :archivedp blob))
1889 ;; Ignore tasks, if specified by `:with-tasks' property.
1890 (and todo
1891 (or (not with-tasks)
1892 (and (memq with-tasks '(todo done))
1893 (not (eq todo-type with-tasks)))
1894 (and (consp with-tasks) (not (member todo with-tasks))))))))
1895 (inlinetask (not (plist-get options :with-inlinetasks)))
1896 (planning (not (plist-get options :with-plannings)))
1897 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1898 (table-cell
1899 (and (org-export-table-has-special-column-p
1900 (org-export-get-parent-table blob))
1901 (not (org-export-get-previous-element blob options))))
1902 (table-row (org-export-table-row-is-special-p blob options))
1903 (timestamp
1904 (case (plist-get options :with-timestamps)
1905 ;; No timestamp allowed.
1906 ('nil t)
1907 ;; Only active timestamps allowed and the current one isn't
1908 ;; active.
1909 (active
1910 (not (memq (org-element-property :type blob)
1911 '(active active-range))))
1912 ;; Only inactive timestamps allowed and the current one isn't
1913 ;; inactive.
1914 (inactive
1915 (not (memq (org-element-property :type blob)
1916 '(inactive inactive-range))))))))
1920 ;;; The Transcoder
1922 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1923 ;; `org-element-parse-buffer') and transcodes it into a specified
1924 ;; back-end output. It takes care of filtering out elements or
1925 ;; objects according to export options and organizing the output blank
1926 ;; lines and white space are preserved. The function memoizes its
1927 ;; results, so it is cheap to call it within translators.
1929 ;; It is possible to modify locally the back-end used by
1930 ;; `org-export-data' or even use a temporary back-end by using
1931 ;; `org-export-data-with-translations' and
1932 ;; `org-export-data-with-backend'.
1934 ;; Internally, three functions handle the filtering of objects and
1935 ;; elements during the export. In particular,
1936 ;; `org-export-ignore-element' marks an element or object so future
1937 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1938 ;; elements or objects should be seen as real Org syntax and
1939 ;; `org-export-expand' transforms the others back into their original
1940 ;; shape
1942 ;; `org-export-transcoder' is an accessor returning appropriate
1943 ;; translator function for a given element or object.
1945 (defun org-export-transcoder (blob info)
1946 "Return appropriate transcoder for BLOB.
1947 INFO is a plist containing export directives."
1948 (let ((type (org-element-type blob)))
1949 ;; Return contents only for complete parse trees.
1950 (if (eq type 'org-data) (lambda (blob contents info) contents)
1951 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1952 (and (functionp transcoder) transcoder)))))
1954 (defun org-export-data (data info)
1955 "Convert DATA into current back-end format.
1957 DATA is a parse tree, an element or an object or a secondary
1958 string. INFO is a plist holding export options.
1960 Return transcoded string."
1961 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1962 (if (not (eq memo 'no-memo)) memo
1963 (let* ((type (org-element-type data))
1964 (results
1965 (cond
1966 ;; Ignored element/object.
1967 ((memq data (plist-get info :ignore-list)) nil)
1968 ;; Plain text.
1969 ((eq type 'plain-text)
1970 (org-export-filter-apply-functions
1971 (plist-get info :filter-plain-text)
1972 (let ((transcoder (org-export-transcoder data info)))
1973 (if transcoder (funcall transcoder data info) data))
1974 info))
1975 ;; Uninterpreted element/object: change it back to Org
1976 ;; syntax and export again resulting raw string.
1977 ((not (org-export--interpret-p data info))
1978 (org-export-data
1979 (org-export-expand
1980 data
1981 (mapconcat (lambda (blob) (org-export-data blob info))
1982 (org-element-contents data)
1983 ""))
1984 info))
1985 ;; Secondary string.
1986 ((not type)
1987 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1988 ;; Element/Object without contents or, as a special case,
1989 ;; headline with archive tag and archived trees restricted
1990 ;; to title only.
1991 ((or (not (org-element-contents data))
1992 (and (eq type 'headline)
1993 (eq (plist-get info :with-archived-trees) 'headline)
1994 (org-element-property :archivedp data)))
1995 (let ((transcoder (org-export-transcoder data info)))
1996 (and (functionp transcoder)
1997 (funcall transcoder data nil info))))
1998 ;; Element/Object with contents.
2000 (let ((transcoder (org-export-transcoder data info)))
2001 (when transcoder
2002 (let* ((greaterp (memq type org-element-greater-elements))
2003 (objectp
2004 (and (not greaterp)
2005 (memq type org-element-recursive-objects)))
2006 (contents
2007 (mapconcat
2008 (lambda (element) (org-export-data element info))
2009 (org-element-contents
2010 (if (or greaterp objectp) data
2011 ;; Elements directly containing objects
2012 ;; must have their indentation normalized
2013 ;; first.
2014 (org-element-normalize-contents
2015 data
2016 ;; When normalizing contents of the first
2017 ;; paragraph in an item or a footnote
2018 ;; definition, ignore first line's
2019 ;; indentation: there is none and it
2020 ;; might be misleading.
2021 (when (eq type 'paragraph)
2022 (let ((parent (org-export-get-parent data)))
2023 (and
2024 (eq (car (org-element-contents parent))
2025 data)
2026 (memq (org-element-type parent)
2027 '(footnote-definition item))))))))
2028 "")))
2029 (funcall transcoder data
2030 (if (not greaterp) contents
2031 (org-element-normalize-string contents))
2032 info))))))))
2033 ;; Final result will be memoized before being returned.
2034 (puthash
2035 data
2036 (cond
2037 ((not results) nil)
2038 ((memq type '(org-data plain-text nil)) results)
2039 ;; Append the same white space between elements or objects as in
2040 ;; the original buffer, and call appropriate filters.
2042 (let ((results
2043 (org-export-filter-apply-functions
2044 (plist-get info (intern (format ":filter-%s" type)))
2045 (let ((post-blank (or (org-element-property :post-blank data)
2046 0)))
2047 (if (memq type org-element-all-elements)
2048 (concat (org-element-normalize-string results)
2049 (make-string post-blank ?\n))
2050 (concat results (make-string post-blank ? ))))
2051 info)))
2052 results)))
2053 (plist-get info :exported-data))))))
2055 (defun org-export-data-with-translations (data translations info)
2056 "Convert DATA into another format using a given translation table.
2057 DATA is an element, an object, a secondary string or a string.
2058 TRANSLATIONS is an alist between element or object types and
2059 a functions handling them. See `org-export-define-backend' for
2060 more information. INFO is a plist used as a communication
2061 channel."
2062 (org-export-data
2063 data
2064 ;; Set-up a new communication channel with TRANSLATIONS as the
2065 ;; translate table and a new hash table for memoization.
2066 (org-combine-plists
2067 info
2068 (list :translate-alist translations
2069 ;; Size of the hash table is reduced since this function
2070 ;; will probably be used on short trees.
2071 :exported-data (make-hash-table :test 'eq :size 401)))))
2073 (defun org-export-data-with-backend (data backend info)
2074 "Convert DATA into BACKEND format.
2076 DATA is an element, an object, a secondary string or a string.
2077 BACKEND is a symbol. INFO is a plist used as a communication
2078 channel.
2080 Unlike to `org-export-with-backend', this function will
2081 recursively convert DATA using BACKEND translation table."
2082 (org-export-barf-if-invalid-backend backend)
2083 (org-export-data-with-translations
2084 data (org-export-backend-translate-table backend) info))
2086 (defun org-export--interpret-p (blob info)
2087 "Non-nil if element or object BLOB should be interpreted as Org syntax.
2088 Check is done according to export options INFO, stored as
2089 a plist."
2090 (case (org-element-type blob)
2091 ;; ... entities...
2092 (entity (plist-get info :with-entities))
2093 ;; ... emphasis...
2094 ((bold italic strike-through underline)
2095 (plist-get info :with-emphasize))
2096 ;; ... fixed-width areas.
2097 (fixed-width (plist-get info :with-fixed-width))
2098 ;; ... footnotes...
2099 ((footnote-definition footnote-reference)
2100 (plist-get info :with-footnotes))
2101 ;; ... sub/superscripts...
2102 ((subscript superscript)
2103 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2104 (if (eq sub/super-p '{})
2105 (org-element-property :use-brackets-p blob)
2106 sub/super-p)))
2107 ;; ... tables...
2108 (table (plist-get info :with-tables))
2109 (otherwise t)))
2111 (defun org-export-expand (blob contents)
2112 "Expand a parsed element or object to its original state.
2113 BLOB is either an element or an object. CONTENTS is its
2114 contents, as a string or nil."
2115 (funcall
2116 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2117 blob contents))
2119 (defun org-export-ignore-element (element info)
2120 "Add ELEMENT to `:ignore-list' in INFO.
2122 Any element in `:ignore-list' will be skipped when using
2123 `org-element-map'. INFO is modified by side effects."
2124 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2128 ;;; The Filter System
2130 ;; Filters allow end-users to tweak easily the transcoded output.
2131 ;; They are the functional counterpart of hooks, as every filter in
2132 ;; a set is applied to the return value of the previous one.
2134 ;; Every set is back-end agnostic. Although, a filter is always
2135 ;; called, in addition to the string it applies to, with the back-end
2136 ;; used as argument, so it's easy for the end-user to add back-end
2137 ;; specific filters in the set. The communication channel, as
2138 ;; a plist, is required as the third argument.
2140 ;; From the developer side, filters sets can be installed in the
2141 ;; process with the help of `org-export-define-backend', which
2142 ;; internally stores filters as an alist. Each association has a key
2143 ;; among the following symbols and a function or a list of functions
2144 ;; as value.
2146 ;; - `:filter-options' applies to the property list containing export
2147 ;; options. Unlike to other filters, functions in this list accept
2148 ;; two arguments instead of three: the property list containing
2149 ;; export options and the back-end. Users can set its value through
2150 ;; `org-export-filter-options-functions' variable.
2152 ;; - `:filter-parse-tree' applies directly to the complete parsed
2153 ;; tree. Users can set it through
2154 ;; `org-export-filter-parse-tree-functions' variable.
2156 ;; - `:filter-final-output' applies to the final transcoded string.
2157 ;; Users can set it with `org-export-filter-final-output-functions'
2158 ;; variable
2160 ;; - `:filter-plain-text' applies to any string not recognized as Org
2161 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2162 ;; configure it.
2164 ;; - `:filter-TYPE' applies on the string returned after an element or
2165 ;; object of type TYPE has been transcoded. An user can modify
2166 ;; `org-export-filter-TYPE-functions'
2168 ;; All filters sets are applied with
2169 ;; `org-export-filter-apply-functions' function. Filters in a set are
2170 ;; applied in a LIFO fashion. It allows developers to be sure that
2171 ;; their filters will be applied first.
2173 ;; Filters properties are installed in communication channel with
2174 ;; `org-export-install-filters' function.
2176 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2177 ;; `org-export-before-parsing-hook') are run at the beginning of the
2178 ;; export process and just before parsing to allow for heavy structure
2179 ;; modifications.
2182 ;;;; Hooks
2184 (defvar org-export-before-processing-hook nil
2185 "Hook run at the beginning of the export process.
2187 This is run before include keywords and macros are expanded and
2188 Babel code blocks executed, on a copy of the original buffer
2189 being exported. Visibility and narrowing are preserved. Point
2190 is at the beginning of the buffer.
2192 Every function in this hook will be called with one argument: the
2193 back-end currently used, as a symbol.")
2195 (defvar org-export-before-parsing-hook nil
2196 "Hook run before parsing an export buffer.
2198 This is run after include keywords and macros have been expanded
2199 and Babel code blocks executed, on a copy of the original buffer
2200 being exported. Visibility and narrowing are preserved. Point
2201 is at the beginning of the buffer.
2203 Every function in this hook will be called with one argument: the
2204 back-end currently used, as a symbol.")
2207 ;;;; Special Filters
2209 (defvar org-export-filter-options-functions nil
2210 "List of functions applied to the export options.
2211 Each filter is called with two arguments: the export options, as
2212 a plist, and the back-end, as a symbol. It must return
2213 a property list containing export options.")
2215 (defvar org-export-filter-parse-tree-functions nil
2216 "List of functions applied to the parsed tree.
2217 Each filter is called with three arguments: the parse tree, as
2218 returned by `org-element-parse-buffer', the back-end, as
2219 a symbol, and the communication channel, as a plist. It must
2220 return the modified parse tree to transcode.")
2222 (defvar org-export-filter-plain-text-functions nil
2223 "List of functions applied to plain text.
2224 Each filter is called with three arguments: a string which
2225 contains no Org syntax, the back-end, as a symbol, and the
2226 communication channel, as a plist. It must return a string or
2227 nil.")
2229 (defvar org-export-filter-final-output-functions nil
2230 "List of functions applied to the transcoded string.
2231 Each filter is called with three arguments: the full transcoded
2232 string, the back-end, as a symbol, and the communication channel,
2233 as a plist. It must return a string that will be used as the
2234 final export output.")
2237 ;;;; Elements Filters
2239 (defvar org-export-filter-babel-call-functions nil
2240 "List of functions applied to a transcoded babel-call.
2241 Each filter is called with three arguments: the transcoded data,
2242 as a string, the back-end, as a symbol, and the communication
2243 channel, as a plist. It must return a string or nil.")
2245 (defvar org-export-filter-center-block-functions nil
2246 "List of functions applied to a transcoded center block.
2247 Each filter is called with three arguments: the transcoded data,
2248 as a string, the back-end, as a symbol, and the communication
2249 channel, as a plist. It must return a string or nil.")
2251 (defvar org-export-filter-clock-functions nil
2252 "List of functions applied to a transcoded clock.
2253 Each filter is called with three arguments: the transcoded data,
2254 as a string, the back-end, as a symbol, and the communication
2255 channel, as a plist. It must return a string or nil.")
2257 (defvar org-export-filter-comment-functions nil
2258 "List of functions applied to a transcoded comment.
2259 Each filter is called with three arguments: the transcoded data,
2260 as a string, the back-end, as a symbol, and the communication
2261 channel, as a plist. It must return a string or nil.")
2263 (defvar org-export-filter-comment-block-functions nil
2264 "List of functions applied to a transcoded comment-block.
2265 Each filter is called with three arguments: the transcoded data,
2266 as a string, the back-end, as a symbol, and the communication
2267 channel, as a plist. It must return a string or nil.")
2269 (defvar org-export-filter-diary-sexp-functions nil
2270 "List of functions applied to a transcoded diary-sexp.
2271 Each filter is called with three arguments: the transcoded data,
2272 as a string, the back-end, as a symbol, and the communication
2273 channel, as a plist. It must return a string or nil.")
2275 (defvar org-export-filter-drawer-functions nil
2276 "List of functions applied to a transcoded drawer.
2277 Each filter is called with three arguments: the transcoded data,
2278 as a string, the back-end, as a symbol, and the communication
2279 channel, as a plist. It must return a string or nil.")
2281 (defvar org-export-filter-dynamic-block-functions nil
2282 "List of functions applied to a transcoded dynamic-block.
2283 Each filter is called with three arguments: the transcoded data,
2284 as a string, the back-end, as a symbol, and the communication
2285 channel, as a plist. It must return a string or nil.")
2287 (defvar org-export-filter-example-block-functions nil
2288 "List of functions applied to a transcoded example-block.
2289 Each filter is called with three arguments: the transcoded data,
2290 as a string, the back-end, as a symbol, and the communication
2291 channel, as a plist. It must return a string or nil.")
2293 (defvar org-export-filter-export-block-functions nil
2294 "List of functions applied to a transcoded export-block.
2295 Each filter is called with three arguments: the transcoded data,
2296 as a string, the back-end, as a symbol, and the communication
2297 channel, as a plist. It must return a string or nil.")
2299 (defvar org-export-filter-fixed-width-functions nil
2300 "List of functions applied to a transcoded fixed-width.
2301 Each filter is called with three arguments: the transcoded data,
2302 as a string, the back-end, as a symbol, and the communication
2303 channel, as a plist. It must return a string or nil.")
2305 (defvar org-export-filter-footnote-definition-functions nil
2306 "List of functions applied to a transcoded footnote-definition.
2307 Each filter is called with three arguments: the transcoded data,
2308 as a string, the back-end, as a symbol, and the communication
2309 channel, as a plist. It must return a string or nil.")
2311 (defvar org-export-filter-headline-functions nil
2312 "List of functions applied to a transcoded headline.
2313 Each filter is called with three arguments: the transcoded data,
2314 as a string, the back-end, as a symbol, and the communication
2315 channel, as a plist. It must return a string or nil.")
2317 (defvar org-export-filter-horizontal-rule-functions nil
2318 "List of functions applied to a transcoded horizontal-rule.
2319 Each filter is called with three arguments: the transcoded data,
2320 as a string, the back-end, as a symbol, and the communication
2321 channel, as a plist. It must return a string or nil.")
2323 (defvar org-export-filter-inlinetask-functions nil
2324 "List of functions applied to a transcoded inlinetask.
2325 Each filter is called with three arguments: the transcoded data,
2326 as a string, the back-end, as a symbol, and the communication
2327 channel, as a plist. It must return a string or nil.")
2329 (defvar org-export-filter-item-functions nil
2330 "List of functions applied to a transcoded item.
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-keyword-functions nil
2336 "List of functions applied to a transcoded keyword.
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-latex-environment-functions nil
2342 "List of functions applied to a transcoded latex-environment.
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-node-property-functions nil
2348 "List of functions applied to a transcoded node-property.
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-paragraph-functions nil
2354 "List of functions applied to a transcoded paragraph.
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-plain-list-functions nil
2360 "List of functions applied to a transcoded plain-list.
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-planning-functions nil
2366 "List of functions applied to a transcoded planning.
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-property-drawer-functions nil
2372 "List of functions applied to a transcoded property-drawer.
2373 Each filter is called with three arguments: the transcoded data,
2374 as a string, the back-end, as a symbol, and the communication
2375 channel, as a plist. It must return a string or nil.")
2377 (defvar org-export-filter-quote-block-functions nil
2378 "List of functions applied to a transcoded quote block.
2379 Each filter is called with three arguments: the transcoded quote
2380 data, as a string, the back-end, as a symbol, and the
2381 communication channel, as a plist. It must return a string or
2382 nil.")
2384 (defvar org-export-filter-quote-section-functions nil
2385 "List of functions applied to a transcoded quote-section.
2386 Each filter is called with three arguments: the transcoded data,
2387 as a string, the back-end, as a symbol, and the communication
2388 channel, as a plist. It must return a string or nil.")
2390 (defvar org-export-filter-section-functions nil
2391 "List of functions applied to a transcoded section.
2392 Each filter is called with three arguments: the transcoded data,
2393 as a string, the back-end, as a symbol, and the communication
2394 channel, as a plist. It must return a string or nil.")
2396 (defvar org-export-filter-special-block-functions nil
2397 "List of functions applied to a transcoded special block.
2398 Each filter is called with three arguments: the transcoded data,
2399 as a string, the back-end, as a symbol, and the communication
2400 channel, as a plist. It must return a string or nil.")
2402 (defvar org-export-filter-src-block-functions nil
2403 "List of functions applied to a transcoded src-block.
2404 Each filter is called with three arguments: the transcoded data,
2405 as a string, the back-end, as a symbol, and the communication
2406 channel, as a plist. It must return a string or nil.")
2408 (defvar org-export-filter-table-functions nil
2409 "List of functions applied to a transcoded table.
2410 Each filter is called with three arguments: the transcoded data,
2411 as a string, the back-end, as a symbol, and the communication
2412 channel, as a plist. It must return a string or nil.")
2414 (defvar org-export-filter-table-cell-functions nil
2415 "List of functions applied to a transcoded table-cell.
2416 Each filter is called with three arguments: the transcoded data,
2417 as a string, the back-end, as a symbol, and the communication
2418 channel, as a plist. It must return a string or nil.")
2420 (defvar org-export-filter-table-row-functions nil
2421 "List of functions applied to a transcoded table-row.
2422 Each filter is called with three arguments: the transcoded data,
2423 as a string, the back-end, as a symbol, and the communication
2424 channel, as a plist. It must return a string or nil.")
2426 (defvar org-export-filter-verse-block-functions nil
2427 "List of functions applied to a transcoded verse block.
2428 Each filter is called with three arguments: the transcoded data,
2429 as a string, the back-end, as a symbol, and the communication
2430 channel, as a plist. It must return a string or nil.")
2433 ;;;; Objects Filters
2435 (defvar org-export-filter-bold-functions nil
2436 "List of functions applied to transcoded bold text.
2437 Each filter is called with three arguments: the transcoded data,
2438 as a string, the back-end, as a symbol, and the communication
2439 channel, as a plist. It must return a string or nil.")
2441 (defvar org-export-filter-code-functions nil
2442 "List of functions applied to transcoded code text.
2443 Each filter is called with three arguments: the transcoded data,
2444 as a string, the back-end, as a symbol, and the communication
2445 channel, as a plist. It must return a string or nil.")
2447 (defvar org-export-filter-entity-functions nil
2448 "List of functions applied to a transcoded entity.
2449 Each filter is called with three arguments: the transcoded data,
2450 as a string, the back-end, as a symbol, and the communication
2451 channel, as a plist. It must return a string or nil.")
2453 (defvar org-export-filter-export-snippet-functions nil
2454 "List of functions applied to a transcoded export-snippet.
2455 Each filter is called with three arguments: the transcoded data,
2456 as a string, the back-end, as a symbol, and the communication
2457 channel, as a plist. It must return a string or nil.")
2459 (defvar org-export-filter-footnote-reference-functions nil
2460 "List of functions applied to a transcoded footnote-reference.
2461 Each filter is called with three arguments: the transcoded data,
2462 as a string, the back-end, as a symbol, and the communication
2463 channel, as a plist. It must return a string or nil.")
2465 (defvar org-export-filter-inline-babel-call-functions nil
2466 "List of functions applied to a transcoded inline-babel-call.
2467 Each filter is called with three arguments: the transcoded data,
2468 as a string, the back-end, as a symbol, and the communication
2469 channel, as a plist. It must return a string or nil.")
2471 (defvar org-export-filter-inline-src-block-functions nil
2472 "List of functions applied to a transcoded inline-src-block.
2473 Each filter is called with three arguments: the transcoded data,
2474 as a string, the back-end, as a symbol, and the communication
2475 channel, as a plist. It must return a string or nil.")
2477 (defvar org-export-filter-italic-functions nil
2478 "List of functions applied to transcoded italic text.
2479 Each filter is called with three arguments: the transcoded data,
2480 as a string, the back-end, as a symbol, and the communication
2481 channel, as a plist. It must return a string or nil.")
2483 (defvar org-export-filter-latex-fragment-functions nil
2484 "List of functions applied to a transcoded latex-fragment.
2485 Each filter is called with three arguments: the transcoded data,
2486 as a string, the back-end, as a symbol, and the communication
2487 channel, as a plist. It must return a string or nil.")
2489 (defvar org-export-filter-line-break-functions nil
2490 "List of functions applied to a transcoded line-break.
2491 Each filter is called with three arguments: the transcoded data,
2492 as a string, the back-end, as a symbol, and the communication
2493 channel, as a plist. It must return a string or nil.")
2495 (defvar org-export-filter-link-functions nil
2496 "List of functions applied to a transcoded link.
2497 Each filter is called with three arguments: the transcoded data,
2498 as a string, the back-end, as a symbol, and the communication
2499 channel, as a plist. It must return a string or nil.")
2501 (defvar org-export-filter-macro-functions nil
2502 "List of functions applied to a transcoded macro.
2503 Each filter is called with three arguments: the transcoded data,
2504 as a string, the back-end, as a symbol, and the communication
2505 channel, as a plist. It must return a string or nil.")
2507 (defvar org-export-filter-radio-target-functions nil
2508 "List of functions applied to a transcoded radio-target.
2509 Each filter is called with three arguments: the transcoded data,
2510 as a string, the back-end, as a symbol, and the communication
2511 channel, as a plist. It must return a string or nil.")
2513 (defvar org-export-filter-statistics-cookie-functions nil
2514 "List of functions applied to a transcoded statistics-cookie.
2515 Each filter is called with three arguments: the transcoded data,
2516 as a string, the back-end, as a symbol, and the communication
2517 channel, as a plist. It must return a string or nil.")
2519 (defvar org-export-filter-strike-through-functions nil
2520 "List of functions applied to transcoded strike-through text.
2521 Each filter is called with three arguments: the transcoded data,
2522 as a string, the back-end, as a symbol, and the communication
2523 channel, as a plist. It must return a string or nil.")
2525 (defvar org-export-filter-subscript-functions nil
2526 "List of functions applied to a transcoded subscript.
2527 Each filter is called with three arguments: the transcoded data,
2528 as a string, the back-end, as a symbol, and the communication
2529 channel, as a plist. It must return a string or nil.")
2531 (defvar org-export-filter-superscript-functions nil
2532 "List of functions applied to a transcoded superscript.
2533 Each filter is called with three arguments: the transcoded data,
2534 as a string, the back-end, as a symbol, and the communication
2535 channel, as a plist. It must return a string or nil.")
2537 (defvar org-export-filter-target-functions nil
2538 "List of functions applied to a transcoded target.
2539 Each filter is called with three arguments: the transcoded data,
2540 as a string, the back-end, as a symbol, and the communication
2541 channel, as a plist. It must return a string or nil.")
2543 (defvar org-export-filter-timestamp-functions nil
2544 "List of functions applied to a transcoded timestamp.
2545 Each filter is called with three arguments: the transcoded data,
2546 as a string, the back-end, as a symbol, and the communication
2547 channel, as a plist. It must return a string or nil.")
2549 (defvar org-export-filter-underline-functions nil
2550 "List of functions applied to transcoded underline text.
2551 Each filter is called with three arguments: the transcoded data,
2552 as a string, the back-end, as a symbol, and the communication
2553 channel, as a plist. It must return a string or nil.")
2555 (defvar org-export-filter-verbatim-functions nil
2556 "List of functions applied to transcoded verbatim text.
2557 Each filter is called with three arguments: the transcoded data,
2558 as a string, the back-end, as a symbol, and the communication
2559 channel, as a plist. It must return a string or nil.")
2562 ;;;; Filters Tools
2564 ;; Internal function `org-export-install-filters' installs filters
2565 ;; hard-coded in back-ends (developer filters) and filters from global
2566 ;; variables (user filters) in the communication channel.
2568 ;; Internal function `org-export-filter-apply-functions' takes care
2569 ;; about applying each filter in order to a given data. It ignores
2570 ;; filters returning a nil value but stops whenever a filter returns
2571 ;; an empty string.
2573 (defun org-export-filter-apply-functions (filters value info)
2574 "Call every function in FILTERS.
2576 Functions are called with arguments VALUE, current export
2577 back-end and INFO. A function returning a nil value will be
2578 skipped. If it returns the empty string, the process ends and
2579 VALUE is ignored.
2581 Call is done in a LIFO fashion, to be sure that developer
2582 specified filters, if any, are called first."
2583 (catch 'exit
2584 (dolist (filter filters value)
2585 (let ((result (funcall filter value (plist-get info :back-end) info)))
2586 (cond ((not result) value)
2587 ((equal value "") (throw 'exit nil))
2588 (t (setq value result)))))))
2590 (defun org-export-install-filters (info)
2591 "Install filters properties in communication channel.
2592 INFO is a plist containing the current communication channel.
2593 Return the updated communication channel."
2594 (let (plist)
2595 ;; Install user defined filters with `org-export-filters-alist'.
2596 (mapc (lambda (p)
2597 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2598 org-export-filters-alist)
2599 ;; Prepend back-end specific filters to that list.
2600 (mapc (lambda (p)
2601 ;; Single values get consed, lists are prepended.
2602 (let ((key (car p)) (value (cdr p)))
2603 (when value
2604 (setq plist
2605 (plist-put
2606 plist key
2607 (if (atom value) (cons value (plist-get plist key))
2608 (append value (plist-get plist key))))))))
2609 (org-export-backend-filters (plist-get info :back-end)))
2610 ;; Return new communication channel.
2611 (org-combine-plists info plist)))
2615 ;;; Core functions
2617 ;; This is the room for the main function, `org-export-as', along with
2618 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2619 ;; They differ only by the way they output the resulting code.
2621 ;; `org-export-output-file-name' is an auxiliary function meant to be
2622 ;; used with `org-export-to-file'. With a given extension, it tries
2623 ;; to provide a canonical file name to write export output to.
2625 ;; Note that `org-export-as' doesn't really parse the current buffer,
2626 ;; but a copy of it (with the same buffer-local variables and
2627 ;; visibility), where macros and include keywords are expanded and
2628 ;; Babel blocks are executed, if appropriate.
2629 ;; `org-export-with-buffer-copy' macro prepares that copy.
2631 ;; File inclusion is taken care of by
2632 ;; `org-export-expand-include-keyword' and
2633 ;; `org-export--prepare-file-contents'. Structure wise, including
2634 ;; a whole Org file in a buffer often makes little sense. For
2635 ;; example, if the file contains an headline and the include keyword
2636 ;; was within an item, the item should contain the headline. That's
2637 ;; why file inclusion should be done before any structure can be
2638 ;; associated to the file, that is before parsing.
2640 (defun org-export-as
2641 (backend &optional subtreep visible-only body-only ext-plist)
2642 "Transcode current Org buffer into BACKEND code.
2644 If narrowing is active in the current buffer, only transcode its
2645 narrowed part.
2647 If a region is active, transcode that region.
2649 When optional argument SUBTREEP is non-nil, transcode the
2650 sub-tree at point, extracting information from the headline
2651 properties first.
2653 When optional argument VISIBLE-ONLY is non-nil, don't export
2654 contents of hidden elements.
2656 When optional argument BODY-ONLY is non-nil, only return body
2657 code, without preamble nor postamble.
2659 Optional argument EXT-PLIST, when provided, is a property list
2660 with external parameters overriding Org default settings, but
2661 still inferior to file-local settings.
2663 Return code as a string."
2664 ;; Barf if BACKEND isn't registered.
2665 (org-export-barf-if-invalid-backend backend)
2666 (save-excursion
2667 (save-restriction
2668 ;; Narrow buffer to an appropriate region or subtree for
2669 ;; parsing. If parsing subtree, be sure to remove main headline
2670 ;; too.
2671 (cond ((org-region-active-p)
2672 (narrow-to-region (region-beginning) (region-end)))
2673 (subtreep
2674 (org-narrow-to-subtree)
2675 (goto-char (point-min))
2676 (forward-line)
2677 (narrow-to-region (point) (point-max))))
2678 ;; Initialize communication channel with original buffer
2679 ;; attributes, unavailable in its copy.
2680 (let ((info (org-export--get-buffer-attributes)) tree)
2681 ;; Update communication channel and get parse tree. Buffer
2682 ;; isn't parsed directly. Instead, a temporary copy is
2683 ;; created, where include keywords, macros are expanded and
2684 ;; code blocks are evaluated.
2685 (org-export-with-buffer-copy
2686 ;; Run first hook with current back-end as argument.
2687 (run-hook-with-args 'org-export-before-processing-hook backend)
2688 (org-export-expand-include-keyword)
2689 ;; Update macro templates since #+INCLUDE keywords might have
2690 ;; added some new ones.
2691 (org-macro-initialize-templates)
2692 (org-macro-replace-all org-macro-templates)
2693 (org-export-execute-babel-code)
2694 ;; Update radio targets since keyword inclusion might have
2695 ;; added some more.
2696 (org-update-radio-target-regexp)
2697 ;; Run last hook with current back-end as argument.
2698 (goto-char (point-min))
2699 (run-hook-with-args 'org-export-before-parsing-hook backend)
2700 ;; Update communication channel with environment. Also
2701 ;; install user's and developer's filters.
2702 (setq info
2703 (org-export-install-filters
2704 (org-combine-plists
2705 info (org-export-get-environment backend subtreep ext-plist))))
2706 ;; Expand export-specific set of macros: {{{author}}},
2707 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
2708 ;; once regular macros have been expanded, since document
2709 ;; keywords may contain one of them.
2710 (org-macro-replace-all
2711 (list (cons "author"
2712 (org-element-interpret-data (plist-get info :author)))
2713 (cons "date"
2714 (org-element-interpret-data (plist-get info :date)))
2715 ;; EMAIL is not a parsed keyword: store it as-is.
2716 (cons "email" (or (plist-get info :email) ""))
2717 (cons "title"
2718 (org-element-interpret-data (plist-get info :title)))))
2719 ;; Call options filters and update export options. We do not
2720 ;; use `org-export-filter-apply-functions' here since the
2721 ;; arity of such filters is different.
2722 (dolist (filter (plist-get info :filter-options))
2723 (let ((result (funcall filter info backend)))
2724 (when result (setq info result))))
2725 ;; Parse buffer and call parse-tree filter on it.
2726 (setq tree
2727 (org-export-filter-apply-functions
2728 (plist-get info :filter-parse-tree)
2729 (org-element-parse-buffer nil visible-only) info)))
2730 ;; Now tree is complete, compute its properties and add them
2731 ;; to communication channel.
2732 (setq info
2733 (org-combine-plists
2734 info (org-export-collect-tree-properties tree info)))
2735 ;; Eventually transcode TREE. Wrap the resulting string into
2736 ;; a template, if required. Finally call final-output filter.
2737 (let* ((body (org-element-normalize-string
2738 (or (org-export-data tree info) "")))
2739 (template (cdr (assq 'template
2740 (plist-get info :translate-alist))))
2741 ;; Remove all text properties since they cannot be
2742 ;; retrieved from an external process.
2743 (output (org-no-properties
2744 (org-export-filter-apply-functions
2745 (plist-get info :filter-final-output)
2746 (if (or (not (functionp template)) body-only) body
2747 (funcall template body info))
2748 info))))
2749 ;; Maybe add final OUTPUT to kill ring, then return it.
2750 (when (and org-export-copy-to-kill-ring (org-string-nw-p output))
2751 (org-kill-new output))
2752 output)))))
2754 (defun org-export-to-buffer
2755 (backend buffer &optional subtreep visible-only body-only ext-plist)
2756 "Call `org-export-as' with output to a specified buffer.
2758 BACKEND is the back-end used for transcoding, as a symbol.
2760 BUFFER is the output buffer. If it already exists, it will be
2761 erased first, otherwise, it will be created.
2763 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
2764 EXT-PLIST are similar to those used in `org-export-as', which
2765 see.
2767 Return buffer."
2768 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist))
2769 (buffer (get-buffer-create buffer)))
2770 (with-current-buffer buffer
2771 (erase-buffer)
2772 (insert out)
2773 (goto-char (point-min)))
2774 buffer))
2776 (defun org-export-to-file
2777 (backend file &optional subtreep visible-only body-only ext-plist)
2778 "Call `org-export-as' with output to a specified file.
2780 BACKEND is the back-end used for transcoding, as a symbol. FILE
2781 is the name of the output file, as a string.
2783 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
2784 EXT-PLIST are similar to those used in `org-export-as', which
2785 see.
2787 Return output file's name."
2788 ;; Checks for FILE permissions. `write-file' would do the same, but
2789 ;; we'd rather avoid needless transcoding of parse tree.
2790 (unless (file-writable-p file) (error "Output file not writable"))
2791 ;; Insert contents to a temporary buffer and write it to FILE.
2792 (let ((out (org-export-as backend subtreep visible-only body-only ext-plist)))
2793 (with-temp-buffer
2794 (insert out)
2795 (let ((coding-system-for-write org-export-coding-system))
2796 (write-file file))))
2797 ;; Return full path.
2798 file)
2800 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2801 "Return output file's name according to buffer specifications.
2803 EXTENSION is a string representing the output file extension,
2804 with the leading dot.
2806 With a non-nil optional argument SUBTREEP, try to determine
2807 output file's name by looking for \"EXPORT_FILE_NAME\" property
2808 of subtree at point.
2810 When optional argument PUB-DIR is set, use it as the publishing
2811 directory.
2813 When optional argument VISIBLE-ONLY is non-nil, don't export
2814 contents of hidden elements.
2816 Return file name as a string, or nil if it couldn't be
2817 determined."
2818 (let ((base-name
2819 ;; File name may come from EXPORT_FILE_NAME subtree property,
2820 ;; assuming point is at beginning of said sub-tree.
2821 (file-name-sans-extension
2822 (or (and subtreep
2823 (org-entry-get
2824 (save-excursion
2825 (ignore-errors (org-back-to-heading) (point)))
2826 "EXPORT_FILE_NAME" t))
2827 ;; File name may be extracted from buffer's associated
2828 ;; file, if any.
2829 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
2830 (and visited-file (file-name-nondirectory visited-file)))
2831 ;; Can't determine file name on our own: Ask user.
2832 (let ((read-file-name-function
2833 (and org-completion-use-ido 'ido-read-file-name)))
2834 (read-file-name
2835 "Output file: " pub-dir nil nil nil
2836 (lambda (name)
2837 (string= (file-name-extension name t) extension))))))))
2838 ;; Build file name. Enforce EXTENSION over whatever user may have
2839 ;; come up with. PUB-DIR, if defined, always has precedence over
2840 ;; any provided path.
2841 (cond
2842 (pub-dir
2843 (concat (file-name-as-directory pub-dir)
2844 (file-name-nondirectory base-name)
2845 extension))
2846 ((file-name-absolute-p base-name) (concat base-name extension))
2847 (t (concat (file-name-as-directory ".") base-name extension)))))
2849 (defun org-export-copy-buffer ()
2850 "Return a copy of the current buffer.
2851 The copy preserves Org buffer-local variables, visibility and
2852 narrowing."
2853 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2854 (new-buf (generate-new-buffer (buffer-name))))
2855 (with-current-buffer new-buf
2856 (funcall copy-buffer-fun)
2857 (set-buffer-modified-p nil))
2858 new-buf))
2860 (defmacro org-export-with-buffer-copy (&rest body)
2861 "Apply BODY in a copy of the current buffer.
2862 The copy preserves local variables, visibility and contents of
2863 the original buffer. Point is at the beginning of the buffer
2864 when BODY is applied."
2865 (declare (debug t))
2866 (org-with-gensyms (buf-copy)
2867 `(let ((,buf-copy (org-export-copy-buffer)))
2868 (unwind-protect
2869 (with-current-buffer ,buf-copy
2870 (goto-char (point-min))
2871 (progn ,@body))
2872 (and (buffer-live-p ,buf-copy)
2873 ;; Kill copy without confirmation.
2874 (progn (with-current-buffer ,buf-copy
2875 (restore-buffer-modified-p nil))
2876 (kill-buffer ,buf-copy)))))))
2878 (defun org-export--generate-copy-script (buffer)
2879 "Generate a function duplicating BUFFER.
2881 The copy will preserve local variables, visibility, contents and
2882 narrowing of the original buffer. If a region was active in
2883 BUFFER, contents will be narrowed to that region instead.
2885 The resulting function can be eval'ed at a later time, from
2886 another buffer, effectively cloning the original buffer there."
2887 (with-current-buffer buffer
2888 `(lambda ()
2889 (let ((inhibit-modification-hooks t))
2890 ;; Buffer local variables.
2891 ,@(let (local-vars)
2892 (mapc
2893 (lambda (entry)
2894 (when (consp entry)
2895 (let ((var (car entry))
2896 (val (cdr entry)))
2897 (and (not (eq var 'org-font-lock-keywords))
2898 (or (memq var
2899 '(major-mode default-directory
2900 buffer-file-name outline-level
2901 outline-regexp
2902 buffer-invisibility-spec))
2903 (string-match "^\\(org-\\|orgtbl-\\)"
2904 (symbol-name var)))
2905 ;; Skip unreadable values, as they cannot be
2906 ;; sent to external process.
2907 (or (not val) (ignore-errors (read (format "%S" val))))
2908 (push `(set (make-local-variable (quote ,var))
2909 (quote ,val))
2910 local-vars)))))
2911 (buffer-local-variables (buffer-base-buffer)))
2912 local-vars)
2913 ;; Whole buffer contents.
2914 (insert
2915 ,(org-with-wide-buffer
2916 (buffer-substring-no-properties
2917 (point-min) (point-max))))
2918 ;; Narrowing.
2919 ,(if (org-region-active-p)
2920 `(narrow-to-region ,(region-beginning) ,(region-end))
2921 `(narrow-to-region ,(point-min) ,(point-max)))
2922 ;; Current position of point.
2923 (goto-char ,(point))
2924 ;; Overlays with invisible property.
2925 ,@(let (ov-set)
2926 (mapc
2927 (lambda (ov)
2928 (let ((invis-prop (overlay-get ov 'invisible)))
2929 (when invis-prop
2930 (push `(overlay-put
2931 (make-overlay ,(overlay-start ov)
2932 ,(overlay-end ov))
2933 'invisible (quote ,invis-prop))
2934 ov-set))))
2935 (overlays-in (point-min) (point-max)))
2936 ov-set)))))
2938 (defun org-export-expand-include-keyword (&optional included dir)
2939 "Expand every include keyword in buffer.
2940 Optional argument INCLUDED is a list of included file names along
2941 with their line restriction, when appropriate. It is used to
2942 avoid infinite recursion. Optional argument DIR is the current
2943 working directory. It is used to properly resolve relative
2944 paths."
2945 (let ((case-fold-search t))
2946 (goto-char (point-min))
2947 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t)
2948 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2949 'keyword)
2950 (beginning-of-line)
2951 ;; Extract arguments from keyword's value.
2952 (let* ((value (match-string 1))
2953 (ind (org-get-indentation))
2954 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2955 (prog1 (expand-file-name (match-string 1 value) dir)
2956 (setq value (replace-match "" nil nil value)))))
2957 (lines
2958 (and (string-match
2959 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2960 (prog1 (match-string 1 value)
2961 (setq value (replace-match "" nil nil value)))))
2962 (env (cond ((string-match "\\<example\\>" value) 'example)
2963 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2964 (match-string 1 value))))
2965 ;; Minimal level of included file defaults to the child
2966 ;; level of the current headline, if any, or one. It
2967 ;; only applies is the file is meant to be included as
2968 ;; an Org one.
2969 (minlevel
2970 (and (not env)
2971 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2972 (prog1 (string-to-number (match-string 1 value))
2973 (setq value (replace-match "" nil nil value)))
2974 (let ((cur (org-current-level)))
2975 (if cur (1+ (org-reduced-level cur)) 1))))))
2976 ;; Remove keyword.
2977 (delete-region (point) (progn (forward-line) (point)))
2978 (cond
2979 ((not file) (error "Invalid syntax in INCLUDE keyword"))
2980 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2981 ;; Check if files has already been parsed. Look after
2982 ;; inclusion lines too, as different parts of the same file
2983 ;; can be included too.
2984 ((member (list file lines) included)
2985 (error "Recursive file inclusion: %s" file))
2987 (cond
2988 ((eq env 'example)
2989 (insert
2990 (let ((ind-str (make-string ind ? ))
2991 (contents
2992 (org-escape-code-in-string
2993 (org-export--prepare-file-contents file lines))))
2994 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2995 ind-str contents ind-str))))
2996 ((stringp env)
2997 (insert
2998 (let ((ind-str (make-string ind ? ))
2999 (contents
3000 (org-escape-code-in-string
3001 (org-export--prepare-file-contents file lines))))
3002 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
3003 ind-str env contents ind-str))))
3005 (insert
3006 (with-temp-buffer
3007 (org-mode)
3008 (insert
3009 (org-export--prepare-file-contents file lines ind minlevel))
3010 (org-export-expand-include-keyword
3011 (cons (list file lines) included)
3012 (file-name-directory file))
3013 (buffer-string))))))))))))
3015 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
3016 "Prepare the contents of FILE for inclusion and return them as a string.
3018 When optional argument LINES is a string specifying a range of
3019 lines, include only those lines.
3021 Optional argument IND, when non-nil, is an integer specifying the
3022 global indentation of returned contents. Since its purpose is to
3023 allow an included file to stay in the same environment it was
3024 created \(i.e. a list item), it doesn't apply past the first
3025 headline encountered.
3027 Optional argument MINLEVEL, when non-nil, is an integer
3028 specifying the level that any top-level headline in the included
3029 file should have."
3030 (with-temp-buffer
3031 (insert-file-contents file)
3032 (when lines
3033 (let* ((lines (split-string lines "-"))
3034 (lbeg (string-to-number (car lines)))
3035 (lend (string-to-number (cadr lines)))
3036 (beg (if (zerop lbeg) (point-min)
3037 (goto-char (point-min))
3038 (forward-line (1- lbeg))
3039 (point)))
3040 (end (if (zerop lend) (point-max)
3041 (goto-char (point-min))
3042 (forward-line (1- lend))
3043 (point))))
3044 (narrow-to-region beg end)))
3045 ;; Remove blank lines at beginning and end of contents. The logic
3046 ;; behind that removal is that blank lines around include keyword
3047 ;; override blank lines in included file.
3048 (goto-char (point-min))
3049 (org-skip-whitespace)
3050 (beginning-of-line)
3051 (delete-region (point-min) (point))
3052 (goto-char (point-max))
3053 (skip-chars-backward " \r\t\n")
3054 (forward-line)
3055 (delete-region (point) (point-max))
3056 ;; If IND is set, preserve indentation of include keyword until
3057 ;; the first headline encountered.
3058 (when ind
3059 (unless (eq major-mode 'org-mode) (org-mode))
3060 (goto-char (point-min))
3061 (let ((ind-str (make-string ind ? )))
3062 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
3063 ;; Do not move footnote definitions out of column 0.
3064 (unless (and (looking-at org-footnote-definition-re)
3065 (eq (org-element-type (org-element-at-point))
3066 'footnote-definition))
3067 (insert ind-str))
3068 (forward-line))))
3069 ;; When MINLEVEL is specified, compute minimal level for headlines
3070 ;; in the file (CUR-MIN), and remove stars to each headline so
3071 ;; that headlines with minimal level have a level of MINLEVEL.
3072 (when minlevel
3073 (unless (eq major-mode 'org-mode) (org-mode))
3074 (org-with-limited-levels
3075 (let ((levels (org-map-entries
3076 (lambda () (org-reduced-level (org-current-level))))))
3077 (when levels
3078 (let ((offset (- minlevel (apply 'min levels))))
3079 (unless (zerop offset)
3080 (when org-odd-levels-only (setq offset (* offset 2)))
3081 ;; Only change stars, don't bother moving whole
3082 ;; sections.
3083 (org-map-entries
3084 (lambda () (if (< offset 0) (delete-char (abs offset))
3085 (insert (make-string offset ?*)))))))))))
3086 (org-element-normalize-string (buffer-string))))
3088 (defun org-export-execute-babel-code ()
3089 "Execute every Babel code in the visible part of current buffer.
3090 This function will return an error if the current buffer is
3091 visiting a file."
3092 ;; Get a pristine copy of current buffer so Babel references can be
3093 ;; properly resolved.
3094 (let ((reference (org-export-copy-buffer)))
3095 (unwind-protect (let ((org-current-export-file reference))
3096 (org-export-blocks-preprocess))
3097 (kill-buffer reference))))
3101 ;;; Tools For Back-Ends
3103 ;; A whole set of tools is available to help build new exporters. Any
3104 ;; function general enough to have its use across many back-ends
3105 ;; should be added here.
3107 ;;;; For Affiliated Keywords
3109 ;; `org-export-read-attribute' reads a property from a given element
3110 ;; as a plist. It can be used to normalize affiliated keywords'
3111 ;; syntax.
3113 ;; Since captions can span over multiple lines and accept dual values,
3114 ;; their internal representation is a bit tricky. Therefore,
3115 ;; `org-export-get-caption' transparently returns a given element's
3116 ;; caption as a secondary string.
3118 (defun org-export-read-attribute (attribute element &optional property)
3119 "Turn ATTRIBUTE property from ELEMENT into a plist.
3121 When optional argument PROPERTY is non-nil, return the value of
3122 that property within attributes.
3124 This function assumes attributes are defined as \":keyword
3125 value\" pairs. It is appropriate for `:attr_html' like
3126 properties."
3127 (let ((attributes
3128 (let ((value (org-element-property attribute element)))
3129 (and value
3130 (read (format "(%s)" (mapconcat 'identity value " ")))))))
3131 (if property (plist-get attributes property) attributes)))
3133 (defun org-export-get-caption (element &optional shortp)
3134 "Return caption from ELEMENT as a secondary string.
3136 When optional argument SHORTP is non-nil, return short caption,
3137 as a secondary string, instead.
3139 Caption lines are separated by a white space."
3140 (let ((full-caption (org-element-property :caption element)) caption)
3141 (dolist (line full-caption (cdr caption))
3142 (let ((cap (funcall (if shortp 'cdr 'car) line)))
3143 (when cap
3144 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
3147 ;;;; For Derived Back-ends
3149 ;; `org-export-with-backend' is a function allowing to locally use
3150 ;; another back-end to transcode some object or element. In a derived
3151 ;; back-end, it may be used as a fall-back function once all specific
3152 ;; cases have been treated.
3154 (defun org-export-with-backend (back-end data &optional contents info)
3155 "Call a transcoder from BACK-END on DATA.
3156 CONTENTS, when non-nil, is the transcoded contents of DATA
3157 element, as a string. INFO, when non-nil, is the communication
3158 channel used for export, as a plist.."
3159 (org-export-barf-if-invalid-backend back-end)
3160 (let ((type (org-element-type data)))
3161 (if (memq type '(nil org-data)) (error "No foreign transcoder available")
3162 (let ((transcoder
3163 (cdr (assq type (org-export-backend-translate-table back-end)))))
3164 (if (functionp transcoder) (funcall transcoder data contents info)
3165 (error "No foreign transcoder available"))))))
3168 ;;;; For Export Snippets
3170 ;; Every export snippet is transmitted to the back-end. Though, the
3171 ;; latter will only retain one type of export-snippet, ignoring
3172 ;; others, based on the former's target back-end. The function
3173 ;; `org-export-snippet-backend' returns that back-end for a given
3174 ;; export-snippet.
3176 (defun org-export-snippet-backend (export-snippet)
3177 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3178 Translation, with `org-export-snippet-translation-alist', is
3179 applied."
3180 (let ((back-end (org-element-property :back-end export-snippet)))
3181 (intern
3182 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3183 back-end))))
3186 ;;;; For Footnotes
3188 ;; `org-export-collect-footnote-definitions' is a tool to list
3189 ;; actually used footnotes definitions in the whole parse tree, or in
3190 ;; an headline, in order to add footnote listings throughout the
3191 ;; transcoded data.
3193 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3194 ;; back-ends, when they need to attach the footnote definition only to
3195 ;; the first occurrence of the corresponding label.
3197 ;; `org-export-get-footnote-definition' and
3198 ;; `org-export-get-footnote-number' provide easier access to
3199 ;; additional information relative to a footnote reference.
3201 (defun org-export-collect-footnote-definitions (data info)
3202 "Return an alist between footnote numbers, labels and definitions.
3204 DATA is the parse tree from which definitions are collected.
3205 INFO is the plist used as a communication channel.
3207 Definitions are sorted by order of references. They either
3208 appear as Org data or as a secondary string for inlined
3209 footnotes. Unreferenced definitions are ignored."
3210 (let* (num-alist
3211 collect-fn ; for byte-compiler.
3212 (collect-fn
3213 (function
3214 (lambda (data)
3215 ;; Collect footnote number, label and definition in DATA.
3216 (org-element-map
3217 data 'footnote-reference
3218 (lambda (fn)
3219 (when (org-export-footnote-first-reference-p fn info)
3220 (let ((def (org-export-get-footnote-definition fn info)))
3221 (push
3222 (list (org-export-get-footnote-number fn info)
3223 (org-element-property :label fn)
3224 def)
3225 num-alist)
3226 ;; Also search in definition for nested footnotes.
3227 (when (eq (org-element-property :type fn) 'standard)
3228 (funcall collect-fn def)))))
3229 ;; Don't enter footnote definitions since it will happen
3230 ;; when their first reference is found.
3231 info nil 'footnote-definition)))))
3232 (funcall collect-fn (plist-get info :parse-tree))
3233 (reverse num-alist)))
3235 (defun org-export-footnote-first-reference-p (footnote-reference info)
3236 "Non-nil when a footnote reference is the first one for its label.
3238 FOOTNOTE-REFERENCE is the footnote reference being considered.
3239 INFO is the plist used as a communication channel."
3240 (let ((label (org-element-property :label footnote-reference)))
3241 ;; Anonymous footnotes are always a first reference.
3242 (if (not label) t
3243 ;; Otherwise, return the first footnote with the same LABEL and
3244 ;; test if it is equal to FOOTNOTE-REFERENCE.
3245 (let* (search-refs ; for byte-compiler.
3246 (search-refs
3247 (function
3248 (lambda (data)
3249 (org-element-map
3250 data 'footnote-reference
3251 (lambda (fn)
3252 (cond
3253 ((string= (org-element-property :label fn) label)
3254 (throw 'exit fn))
3255 ;; If FN isn't inlined, be sure to traverse its
3256 ;; definition before resuming search. See
3257 ;; comments in `org-export-get-footnote-number'
3258 ;; for more information.
3259 ((eq (org-element-property :type fn) 'standard)
3260 (funcall search-refs
3261 (org-export-get-footnote-definition fn info)))))
3262 ;; Don't enter footnote definitions since it will
3263 ;; happen when their first reference is found.
3264 info 'first-match 'footnote-definition)))))
3265 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3266 footnote-reference)))))
3268 (defun org-export-get-footnote-definition (footnote-reference info)
3269 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3270 INFO is the plist used as a communication channel."
3271 (let ((label (org-element-property :label footnote-reference)))
3272 (or (org-element-property :inline-definition footnote-reference)
3273 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3275 (defun org-export-get-footnote-number (footnote info)
3276 "Return number associated to a footnote.
3278 FOOTNOTE is either a footnote reference or a footnote definition.
3279 INFO is the plist used as a communication channel."
3280 (let* ((label (org-element-property :label footnote))
3281 seen-refs
3282 search-ref ; For byte-compiler.
3283 (search-ref
3284 (function
3285 (lambda (data)
3286 ;; Search footnote references through DATA, filling
3287 ;; SEEN-REFS along the way.
3288 (org-element-map
3289 data 'footnote-reference
3290 (lambda (fn)
3291 (let ((fn-lbl (org-element-property :label fn)))
3292 (cond
3293 ;; Anonymous footnote match: return number.
3294 ((and (not fn-lbl) (eq fn footnote))
3295 (throw 'exit (1+ (length seen-refs))))
3296 ;; Labels match: return number.
3297 ((and label (string= label fn-lbl))
3298 (throw 'exit (1+ (length seen-refs))))
3299 ;; Anonymous footnote: it's always a new one. Also,
3300 ;; be sure to return nil from the `cond' so
3301 ;; `first-match' doesn't get us out of the loop.
3302 ((not fn-lbl) (push 'inline seen-refs) nil)
3303 ;; Label not seen so far: add it so SEEN-REFS.
3305 ;; Also search for subsequent references in
3306 ;; footnote definition so numbering follows reading
3307 ;; logic. Note that we don't have to care about
3308 ;; inline definitions, since `org-element-map'
3309 ;; already traverses them at the right time.
3311 ;; Once again, return nil to stay in the loop.
3312 ((not (member fn-lbl seen-refs))
3313 (push fn-lbl seen-refs)
3314 (funcall search-ref
3315 (org-export-get-footnote-definition fn info))
3316 nil))))
3317 ;; Don't enter footnote definitions since it will happen
3318 ;; when their first reference is found.
3319 info 'first-match 'footnote-definition)))))
3320 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3323 ;;;; For Headlines
3325 ;; `org-export-get-relative-level' is a shortcut to get headline
3326 ;; level, relatively to the lower headline level in the parsed tree.
3328 ;; `org-export-get-headline-number' returns the section number of an
3329 ;; headline, while `org-export-number-to-roman' allows to convert it
3330 ;; to roman numbers.
3332 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3333 ;; `org-export-last-sibling-p' are three useful predicates when it
3334 ;; comes to fulfill the `:headline-levels' property.
3336 ;; `org-export-get-tags', `org-export-get-category' and
3337 ;; `org-export-get-node-property' extract useful information from an
3338 ;; headline or a parent headline. They all handle inheritance.
3340 (defun org-export-get-relative-level (headline info)
3341 "Return HEADLINE relative level within current parsed tree.
3342 INFO is a plist holding contextual information."
3343 (+ (org-element-property :level headline)
3344 (or (plist-get info :headline-offset) 0)))
3346 (defun org-export-low-level-p (headline info)
3347 "Non-nil when HEADLINE is considered as low level.
3349 INFO is a plist used as a communication channel.
3351 A low level headlines has a relative level greater than
3352 `:headline-levels' property value.
3354 Return value is the difference between HEADLINE relative level
3355 and the last level being considered as high enough, or nil."
3356 (let ((limit (plist-get info :headline-levels)))
3357 (when (wholenump limit)
3358 (let ((level (org-export-get-relative-level headline info)))
3359 (and (> level limit) (- level limit))))))
3361 (defun org-export-get-headline-number (headline info)
3362 "Return HEADLINE numbering as a list of numbers.
3363 INFO is a plist holding contextual information."
3364 (cdr (assoc headline (plist-get info :headline-numbering))))
3366 (defun org-export-numbered-headline-p (headline info)
3367 "Return a non-nil value if HEADLINE element should be numbered.
3368 INFO is a plist used as a communication channel."
3369 (let ((sec-num (plist-get info :section-numbers))
3370 (level (org-export-get-relative-level headline info)))
3371 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3373 (defun org-export-number-to-roman (n)
3374 "Convert integer N into a roman numeral."
3375 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3376 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3377 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3378 ( 1 . "I")))
3379 (res ""))
3380 (if (<= n 0)
3381 (number-to-string n)
3382 (while roman
3383 (if (>= n (caar roman))
3384 (setq n (- n (caar roman))
3385 res (concat res (cdar roman)))
3386 (pop roman)))
3387 res)))
3389 (defun org-export-get-tags (element info &optional tags inherited)
3390 "Return list of tags associated to ELEMENT.
3392 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3393 is a plist used as a communication channel.
3395 Select tags (see `org-export-select-tags') and exclude tags (see
3396 `org-export-exclude-tags') are removed from the list.
3398 When non-nil, optional argument TAGS should be a list of strings.
3399 Any tag belonging to this list will also be removed.
3401 When optional argument INHERITED is non-nil, tags can also be
3402 inherited from parent headlines and FILETAGS keywords."
3403 (org-remove-if
3404 (lambda (tag) (or (member tag (plist-get info :select-tags))
3405 (member tag (plist-get info :exclude-tags))
3406 (member tag tags)))
3407 (if (not inherited) (org-element-property :tags element)
3408 ;; Build complete list of inherited tags.
3409 (let ((current-tag-list (org-element-property :tags element)))
3410 (mapc
3411 (lambda (parent)
3412 (mapc
3413 (lambda (tag)
3414 (when (and (memq (org-element-type parent) '(headline inlinetask))
3415 (not (member tag current-tag-list)))
3416 (push tag current-tag-list)))
3417 (org-element-property :tags parent)))
3418 (org-export-get-genealogy element))
3419 ;; Add FILETAGS keywords and return results.
3420 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
3422 (defun org-export-get-node-property (property blob &optional inherited)
3423 "Return node PROPERTY value for BLOB.
3425 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3426 element or object.
3428 If optional argument INHERITED is non-nil, the value can be
3429 inherited from a parent headline.
3431 Return value is a string or nil."
3432 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3433 (org-export-get-parent-headline blob))))
3434 (if (not inherited) (org-element-property property blob)
3435 (let ((parent headline) value)
3436 (catch 'found
3437 (while parent
3438 (when (plist-member (nth 1 parent) property)
3439 (throw 'found (org-element-property property parent)))
3440 (setq parent (org-element-property :parent parent))))))))
3442 (defun org-export-get-category (blob info)
3443 "Return category for element or object BLOB.
3445 INFO is a plist used as a communication channel.
3447 CATEGORY is automatically inherited from a parent headline, from
3448 #+CATEGORY: keyword or created out of original file name. If all
3449 fail, the fall-back value is \"???\"."
3450 (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
3451 (org-export-get-parent-headline blob))))
3452 ;; Almost like `org-export-node-property', but we cannot trust
3453 ;; `plist-member' as every headline has a `:category'
3454 ;; property, would it be nil or equal to "???" (which has the
3455 ;; same meaning).
3456 (let ((parent headline) value)
3457 (catch 'found
3458 (while parent
3459 (let ((category (org-element-property :category parent)))
3460 (and category (not (equal "???" category))
3461 (throw 'found category)))
3462 (setq parent (org-element-property :parent parent))))))
3463 (org-element-map
3464 (plist-get info :parse-tree) 'keyword
3465 (lambda (kwd)
3466 (when (equal (org-element-property :key kwd) "CATEGORY")
3467 (org-element-property :value kwd)))
3468 info 'first-match)
3469 (let ((file (plist-get info :input-file)))
3470 (and file (file-name-sans-extension (file-name-nondirectory file))))
3471 "???"))
3473 (defun org-export-first-sibling-p (headline info)
3474 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3475 INFO is a plist used as a communication channel."
3476 (not (eq (org-element-type (org-export-get-previous-element headline info))
3477 'headline)))
3479 (defun org-export-last-sibling-p (headline info)
3480 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3481 INFO is a plist used as a communication channel."
3482 (not (org-export-get-next-element headline info)))
3485 ;;;; For Links
3487 ;; `org-export-solidify-link-text' turns a string into a safer version
3488 ;; for links, replacing most non-standard characters with hyphens.
3490 ;; `org-export-get-coderef-format' returns an appropriate format
3491 ;; string for coderefs.
3493 ;; `org-export-inline-image-p' returns a non-nil value when the link
3494 ;; provided should be considered as an inline image.
3496 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3497 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3498 ;; returns an appropriate unique identifier when found, or nil.
3500 ;; `org-export-resolve-id-link' returns the first headline with
3501 ;; specified id or custom-id in parse tree, the path to the external
3502 ;; file with the id or nil when neither was found.
3504 ;; `org-export-resolve-coderef' associates a reference to a line
3505 ;; number in the element it belongs, or returns the reference itself
3506 ;; when the element isn't numbered.
3508 (defun org-export-solidify-link-text (s)
3509 "Take link text S and make a safe target out of it."
3510 (save-match-data
3511 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
3513 (defun org-export-get-coderef-format (path desc)
3514 "Return format string for code reference link.
3515 PATH is the link path. DESC is its description."
3516 (save-match-data
3517 (cond ((not desc) "%s")
3518 ((string-match (regexp-quote (concat "(" path ")")) desc)
3519 (replace-match "%s" t t desc))
3520 (t desc))))
3522 (defun org-export-inline-image-p (link &optional rules)
3523 "Non-nil if LINK object points to an inline image.
3525 Optional argument is a set of RULES defining inline images. It
3526 is an alist where associations have the following shape:
3528 \(TYPE . REGEXP)
3530 Applying a rule means apply REGEXP against LINK's path when its
3531 type is TYPE. The function will return a non-nil value if any of
3532 the provided rules is non-nil. The default rule is
3533 `org-export-default-inline-image-rule'.
3535 This only applies to links without a description."
3536 (and (not (org-element-contents link))
3537 (let ((case-fold-search t)
3538 (rules (or rules org-export-default-inline-image-rule)))
3539 (catch 'exit
3540 (mapc
3541 (lambda (rule)
3542 (and (string= (org-element-property :type link) (car rule))
3543 (string-match (cdr rule)
3544 (org-element-property :path link))
3545 (throw 'exit t)))
3546 rules)
3547 ;; Return nil if no rule matched.
3548 nil))))
3550 (defun org-export-resolve-coderef (ref info)
3551 "Resolve a code reference REF.
3553 INFO is a plist used as a communication channel.
3555 Return associated line number in source code, or REF itself,
3556 depending on src-block or example element's switches."
3557 (org-element-map
3558 (plist-get info :parse-tree) '(example-block src-block)
3559 (lambda (el)
3560 (with-temp-buffer
3561 (insert (org-trim (org-element-property :value el)))
3562 (let* ((label-fmt (regexp-quote
3563 (or (org-element-property :label-fmt el)
3564 org-coderef-label-format)))
3565 (ref-re
3566 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3567 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3568 ;; Element containing REF is found. Resolve it to either
3569 ;; a label or a line number, as needed.
3570 (when (re-search-backward ref-re nil t)
3571 (cond
3572 ((org-element-property :use-labels el) ref)
3573 ((eq (org-element-property :number-lines el) 'continued)
3574 (+ (org-export-get-loc el info) (line-number-at-pos)))
3575 (t (line-number-at-pos)))))))
3576 info 'first-match))
3578 (defun org-export-resolve-fuzzy-link (link info)
3579 "Return LINK destination.
3581 INFO is a plist holding contextual information.
3583 Return value can be an object, an element, or nil:
3585 - If LINK path matches a target object (i.e. <<path>>) or
3586 element (i.e. \"#+TARGET: path\"), return it.
3588 - If LINK path exactly matches the name affiliated keyword
3589 \(i.e. #+NAME: path) of an element, return that element.
3591 - If LINK path exactly matches any headline name, return that
3592 element. If more than one headline share that name, priority
3593 will be given to the one with the closest common ancestor, if
3594 any, or the first one in the parse tree otherwise.
3596 - Otherwise, return nil.
3598 Assume LINK type is \"fuzzy\"."
3599 (let* ((path (org-element-property :path link))
3600 (match-title-p (eq (aref path 0) ?*)))
3601 (cond
3602 ;; First try to find a matching "<<path>>" unless user specified
3603 ;; he was looking for an headline (path starts with a *
3604 ;; character).
3605 ((and (not match-title-p)
3606 (loop for target in (plist-get info :target-list)
3607 when (string= (org-element-property :value target) path)
3608 return target)))
3609 ;; Then try to find an element with a matching "#+NAME: path"
3610 ;; affiliated keyword.
3611 ((and (not match-title-p)
3612 (org-element-map
3613 (plist-get info :parse-tree) org-element-all-elements
3614 (lambda (el)
3615 (when (string= (org-element-property :name el) path) el))
3616 info 'first-match)))
3617 ;; Last case: link either points to an headline or to
3618 ;; nothingness. Try to find the source, with priority given to
3619 ;; headlines with the closest common ancestor. If such candidate
3620 ;; is found, return it, otherwise return nil.
3622 (let ((find-headline
3623 (function
3624 ;; Return first headline whose `:raw-value' property
3625 ;; is NAME in parse tree DATA, or nil.
3626 (lambda (name data)
3627 (org-element-map
3628 data 'headline
3629 (lambda (headline)
3630 (when (string=
3631 (org-element-property :raw-value headline)
3632 name)
3633 headline))
3634 info 'first-match)))))
3635 ;; Search among headlines sharing an ancestor with link,
3636 ;; from closest to farthest.
3637 (or (catch 'exit
3638 (mapc
3639 (lambda (parent)
3640 (when (eq (org-element-type parent) 'headline)
3641 (let ((foundp (funcall find-headline path parent)))
3642 (when foundp (throw 'exit foundp)))))
3643 (org-export-get-genealogy link)) nil)
3644 ;; No match with a common ancestor: try the full parse-tree.
3645 (funcall find-headline
3646 (if match-title-p (substring path 1) path)
3647 (plist-get info :parse-tree))))))))
3649 (defun org-export-resolve-id-link (link info)
3650 "Return headline referenced as LINK destination.
3652 INFO is a plist used as a communication channel.
3654 Return value can be the headline element matched in current parse
3655 tree, a file name or nil. Assume LINK type is either \"id\" or
3656 \"custom-id\"."
3657 (let ((id (org-element-property :path link)))
3658 ;; First check if id is within the current parse tree.
3659 (or (org-element-map
3660 (plist-get info :parse-tree) 'headline
3661 (lambda (headline)
3662 (when (or (string= (org-element-property :id headline) id)
3663 (string= (org-element-property :custom-id headline) id))
3664 headline))
3665 info 'first-match)
3666 ;; Otherwise, look for external files.
3667 (cdr (assoc id (plist-get info :id-alist))))))
3669 (defun org-export-resolve-radio-link (link info)
3670 "Return radio-target object referenced as LINK destination.
3672 INFO is a plist used as a communication channel.
3674 Return value can be a radio-target object or nil. Assume LINK
3675 has type \"radio\"."
3676 (let ((path (org-element-property :path link)))
3677 (org-element-map
3678 (plist-get info :parse-tree) 'radio-target
3679 (lambda (radio)
3680 (when (equal (org-element-property :value radio) path) radio))
3681 info 'first-match)))
3684 ;;;; For References
3686 ;; `org-export-get-ordinal' associates a sequence number to any object
3687 ;; or element.
3689 (defun org-export-get-ordinal (element info &optional types predicate)
3690 "Return ordinal number of an element or object.
3692 ELEMENT is the element or object considered. INFO is the plist
3693 used as a communication channel.
3695 Optional argument TYPES, when non-nil, is a list of element or
3696 object types, as symbols, that should also be counted in.
3697 Otherwise, only provided element's type is considered.
3699 Optional argument PREDICATE is a function returning a non-nil
3700 value if the current element or object should be counted in. It
3701 accepts two arguments: the element or object being considered and
3702 the plist used as a communication channel. This allows to count
3703 only a certain type of objects (i.e. inline images).
3705 Return value is a list of numbers if ELEMENT is an headline or an
3706 item. It is nil for keywords. It represents the footnote number
3707 for footnote definitions and footnote references. If ELEMENT is
3708 a target, return the same value as if ELEMENT was the closest
3709 table, item or headline containing the target. In any other
3710 case, return the sequence number of ELEMENT among elements or
3711 objects of the same type."
3712 ;; A target keyword, representing an invisible target, never has
3713 ;; a sequence number.
3714 (unless (eq (org-element-type element) 'keyword)
3715 ;; Ordinal of a target object refer to the ordinal of the closest
3716 ;; table, item, or headline containing the object.
3717 (when (eq (org-element-type element) 'target)
3718 (setq element
3719 (loop for parent in (org-export-get-genealogy element)
3720 when
3721 (memq
3722 (org-element-type parent)
3723 '(footnote-definition footnote-reference headline item
3724 table))
3725 return parent)))
3726 (case (org-element-type element)
3727 ;; Special case 1: An headline returns its number as a list.
3728 (headline (org-export-get-headline-number element info))
3729 ;; Special case 2: An item returns its number as a list.
3730 (item (let ((struct (org-element-property :structure element)))
3731 (org-list-get-item-number
3732 (org-element-property :begin element)
3733 struct
3734 (org-list-prevs-alist struct)
3735 (org-list-parents-alist struct))))
3736 ((footnote-definition footnote-reference)
3737 (org-export-get-footnote-number element info))
3738 (otherwise
3739 (let ((counter 0))
3740 ;; Increment counter until ELEMENT is found again.
3741 (org-element-map
3742 (plist-get info :parse-tree) (or types (org-element-type element))
3743 (lambda (el)
3744 (cond
3745 ((eq element el) (1+ counter))
3746 ((not predicate) (incf counter) nil)
3747 ((funcall predicate el info) (incf counter) nil)))
3748 info 'first-match))))))
3751 ;;;; For Src-Blocks
3753 ;; `org-export-get-loc' counts number of code lines accumulated in
3754 ;; src-block or example-block elements with a "+n" switch until
3755 ;; a given element, excluded. Note: "-n" switches reset that count.
3757 ;; `org-export-unravel-code' extracts source code (along with a code
3758 ;; references alist) from an `element-block' or `src-block' type
3759 ;; element.
3761 ;; `org-export-format-code' applies a formatting function to each line
3762 ;; of code, providing relative line number and code reference when
3763 ;; appropriate. Since it doesn't access the original element from
3764 ;; which the source code is coming, it expects from the code calling
3765 ;; it to know if lines should be numbered and if code references
3766 ;; should appear.
3768 ;; Eventually, `org-export-format-code-default' is a higher-level
3769 ;; function (it makes use of the two previous functions) which handles
3770 ;; line numbering and code references inclusion, and returns source
3771 ;; code in a format suitable for plain text or verbatim output.
3773 (defun org-export-get-loc (element info)
3774 "Return accumulated lines of code up to ELEMENT.
3776 INFO is the plist used as a communication channel.
3778 ELEMENT is excluded from count."
3779 (let ((loc 0))
3780 (org-element-map
3781 (plist-get info :parse-tree)
3782 `(src-block example-block ,(org-element-type element))
3783 (lambda (el)
3784 (cond
3785 ;; ELEMENT is reached: Quit the loop.
3786 ((eq el element))
3787 ;; Only count lines from src-block and example-block elements
3788 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3789 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3790 ((let ((linums (org-element-property :number-lines el)))
3791 (when linums
3792 ;; Accumulate locs or reset them.
3793 (let ((lines (org-count-lines
3794 (org-trim (org-element-property :value el)))))
3795 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3796 ;; Return nil to stay in the loop.
3797 nil)))
3798 info 'first-match)
3799 ;; Return value.
3800 loc))
3802 (defun org-export-unravel-code (element)
3803 "Clean source code and extract references out of it.
3805 ELEMENT has either a `src-block' an `example-block' type.
3807 Return a cons cell whose CAR is the source code, cleaned from any
3808 reference and protective comma and CDR is an alist between
3809 relative line number (integer) and name of code reference on that
3810 line (string)."
3811 (let* ((line 0) refs
3812 ;; Get code and clean it. Remove blank lines at its
3813 ;; beginning and end.
3814 (code (let ((c (replace-regexp-in-string
3815 "\\`\\([ \t]*\n\\)+" ""
3816 (replace-regexp-in-string
3817 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3818 (org-element-property :value element)))))
3819 ;; If appropriate, remove global indentation.
3820 (if (or org-src-preserve-indentation
3821 (org-element-property :preserve-indent element))
3823 (org-remove-indentation c))))
3824 ;; Get format used for references.
3825 (label-fmt (regexp-quote
3826 (or (org-element-property :label-fmt element)
3827 org-coderef-label-format)))
3828 ;; Build a regexp matching a loc with a reference.
3829 (with-ref-re
3830 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3831 (replace-regexp-in-string
3832 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3833 ;; Return value.
3834 (cons
3835 ;; Code with references removed.
3836 (org-element-normalize-string
3837 (mapconcat
3838 (lambda (loc)
3839 (incf line)
3840 (if (not (string-match with-ref-re loc)) loc
3841 ;; Ref line: remove ref, and signal its position in REFS.
3842 (push (cons line (match-string 3 loc)) refs)
3843 (replace-match "" nil nil loc 1)))
3844 (org-split-string code "\n") "\n"))
3845 ;; Reference alist.
3846 refs)))
3848 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3849 "Format CODE by applying FUN line-wise and return it.
3851 CODE is a string representing the code to format. FUN is
3852 a function. It must accept three arguments: a line of
3853 code (string), the current line number (integer) or nil and the
3854 reference associated to the current line (string) or nil.
3856 Optional argument NUM-LINES can be an integer representing the
3857 number of code lines accumulated until the current code. Line
3858 numbers passed to FUN will take it into account. If it is nil,
3859 FUN's second argument will always be nil. This number can be
3860 obtained with `org-export-get-loc' function.
3862 Optional argument REF-ALIST can be an alist between relative line
3863 number (i.e. ignoring NUM-LINES) and the name of the code
3864 reference on it. If it is nil, FUN's third argument will always
3865 be nil. It can be obtained through the use of
3866 `org-export-unravel-code' function."
3867 (let ((--locs (org-split-string code "\n"))
3868 (--line 0))
3869 (org-element-normalize-string
3870 (mapconcat
3871 (lambda (--loc)
3872 (incf --line)
3873 (let ((--ref (cdr (assq --line ref-alist))))
3874 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3875 --locs "\n"))))
3877 (defun org-export-format-code-default (element info)
3878 "Return source code from ELEMENT, formatted in a standard way.
3880 ELEMENT is either a `src-block' or `example-block' element. INFO
3881 is a plist used as a communication channel.
3883 This function takes care of line numbering and code references
3884 inclusion. Line numbers, when applicable, appear at the
3885 beginning of the line, separated from the code by two white
3886 spaces. Code references, on the other hand, appear flushed to
3887 the right, separated by six white spaces from the widest line of
3888 code."
3889 ;; Extract code and references.
3890 (let* ((code-info (org-export-unravel-code element))
3891 (code (car code-info))
3892 (code-lines (org-split-string code "\n"))
3893 (refs (and (org-element-property :retain-labels element)
3894 (cdr code-info)))
3895 ;; Handle line numbering.
3896 (num-start (case (org-element-property :number-lines element)
3897 (continued (org-export-get-loc element info))
3898 (new 0)))
3899 (num-fmt
3900 (and num-start
3901 (format "%%%ds "
3902 (length (number-to-string
3903 (+ (length code-lines) num-start))))))
3904 ;; Prepare references display, if required. Any reference
3905 ;; should start six columns after the widest line of code,
3906 ;; wrapped with parenthesis.
3907 (max-width
3908 (+ (apply 'max (mapcar 'length code-lines))
3909 (if (not num-start) 0 (length (format num-fmt num-start))))))
3910 (org-export-format-code
3911 code
3912 (lambda (loc line-num ref)
3913 (let ((number-str (and num-fmt (format num-fmt line-num))))
3914 (concat
3915 number-str
3917 (and ref
3918 (concat (make-string
3919 (- (+ 6 max-width)
3920 (+ (length loc) (length number-str))) ? )
3921 (format "(%s)" ref))))))
3922 num-start refs)))
3925 ;;;; For Tables
3927 ;; `org-export-table-has-special-column-p' and and
3928 ;; `org-export-table-row-is-special-p' are predicates used to look for
3929 ;; meta-information about the table structure.
3931 ;; `org-table-has-header-p' tells when the rows before the first rule
3932 ;; should be considered as table's header.
3934 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3935 ;; and `org-export-table-cell-borders' extract information from
3936 ;; a table-cell element.
3938 ;; `org-export-table-dimensions' gives the number on rows and columns
3939 ;; in the table, ignoring horizontal rules and special columns.
3940 ;; `org-export-table-cell-address', given a table-cell object, returns
3941 ;; the absolute address of a cell. On the other hand,
3942 ;; `org-export-get-table-cell-at' does the contrary.
3944 ;; `org-export-table-cell-starts-colgroup-p',
3945 ;; `org-export-table-cell-ends-colgroup-p',
3946 ;; `org-export-table-row-starts-rowgroup-p',
3947 ;; `org-export-table-row-ends-rowgroup-p',
3948 ;; `org-export-table-row-starts-header-p' and
3949 ;; `org-export-table-row-ends-header-p' indicate position of current
3950 ;; row or cell within the table.
3952 (defun org-export-table-has-special-column-p (table)
3953 "Non-nil when TABLE has a special column.
3954 All special columns will be ignored during export."
3955 ;; The table has a special column when every first cell of every row
3956 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3957 ;; "*" "_" and "^". Though, do not consider a first row containing
3958 ;; only empty cells as special.
3959 (let ((special-column-p 'empty))
3960 (catch 'exit
3961 (mapc
3962 (lambda (row)
3963 (when (eq (org-element-property :type row) 'standard)
3964 (let ((value (org-element-contents
3965 (car (org-element-contents row)))))
3966 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3967 (setq special-column-p 'special))
3968 ((not value))
3969 (t (throw 'exit nil))))))
3970 (org-element-contents table))
3971 (eq special-column-p 'special))))
3973 (defun org-export-table-has-header-p (table info)
3974 "Non-nil when TABLE has an header.
3976 INFO is a plist used as a communication channel.
3978 A table has an header when it contains at least two row groups."
3979 (let ((rowgroup 1) row-flag)
3980 (org-element-map
3981 table 'table-row
3982 (lambda (row)
3983 (cond
3984 ((> rowgroup 1) t)
3985 ((and row-flag (eq (org-element-property :type row) 'rule))
3986 (incf rowgroup) (setq row-flag nil))
3987 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3988 (setq row-flag t) nil)))
3989 info)))
3991 (defun org-export-table-row-is-special-p (table-row info)
3992 "Non-nil if TABLE-ROW is considered special.
3994 INFO is a plist used as the communication channel.
3996 All special rows will be ignored during export."
3997 (when (eq (org-element-property :type table-row) 'standard)
3998 (let ((first-cell (org-element-contents
3999 (car (org-element-contents table-row)))))
4000 ;; A row is special either when...
4002 ;; ... it starts with a field only containing "/",
4003 (equal first-cell '("/"))
4004 ;; ... the table contains a special column and the row start
4005 ;; with a marking character among, "^", "_", "$" or "!",
4006 (and (org-export-table-has-special-column-p
4007 (org-export-get-parent table-row))
4008 (member first-cell '(("^") ("_") ("$") ("!"))))
4009 ;; ... it contains only alignment cookies and empty cells.
4010 (let ((special-row-p 'empty))
4011 (catch 'exit
4012 (mapc
4013 (lambda (cell)
4014 (let ((value (org-element-contents cell)))
4015 ;; Since VALUE is a secondary string, the following
4016 ;; checks avoid expanding it with `org-export-data'.
4017 (cond ((not value))
4018 ((and (not (cdr value))
4019 (stringp (car value))
4020 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4021 (car value)))
4022 (setq special-row-p 'cookie))
4023 (t (throw 'exit nil)))))
4024 (org-element-contents table-row))
4025 (eq special-row-p 'cookie)))))))
4027 (defun org-export-table-row-group (table-row info)
4028 "Return TABLE-ROW's group.
4030 INFO is a plist used as the communication channel.
4032 Return value is the group number, as an integer, or nil special
4033 rows and table rules. Group 1 is also table's header."
4034 (unless (or (eq (org-element-property :type table-row) 'rule)
4035 (org-export-table-row-is-special-p table-row info))
4036 (let ((group 0) row-flag)
4037 (catch 'found
4038 (mapc
4039 (lambda (row)
4040 (cond
4041 ((and (eq (org-element-property :type row) 'standard)
4042 (not (org-export-table-row-is-special-p row info)))
4043 (unless row-flag (incf group) (setq row-flag t)))
4044 ((eq (org-element-property :type row) 'rule)
4045 (setq row-flag nil)))
4046 (when (eq table-row row) (throw 'found group)))
4047 (org-element-contents (org-export-get-parent table-row)))))))
4049 (defun org-export-table-cell-width (table-cell info)
4050 "Return TABLE-CELL contents width.
4052 INFO is a plist used as the communication channel.
4054 Return value is the width given by the last width cookie in the
4055 same column as TABLE-CELL, or nil."
4056 (let* ((row (org-export-get-parent table-cell))
4057 (column (let ((cells (org-element-contents row)))
4058 (- (length cells) (length (memq table-cell cells)))))
4059 (table (org-export-get-parent-table table-cell))
4060 cookie-width)
4061 (mapc
4062 (lambda (row)
4063 (cond
4064 ;; In a special row, try to find a width cookie at COLUMN.
4065 ((org-export-table-row-is-special-p row info)
4066 (let ((value (org-element-contents
4067 (elt (org-element-contents row) column))))
4068 ;; The following checks avoid expanding unnecessarily the
4069 ;; cell with `org-export-data'
4070 (when (and value
4071 (not (cdr value))
4072 (stringp (car value))
4073 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
4074 (match-string 1 (car value)))
4075 (setq cookie-width
4076 (string-to-number (match-string 1 (car value)))))))
4077 ;; Ignore table rules.
4078 ((eq (org-element-property :type row) 'rule))))
4079 (org-element-contents table))
4080 ;; Return value.
4081 cookie-width))
4083 (defun org-export-table-cell-alignment (table-cell info)
4084 "Return TABLE-CELL contents alignment.
4086 INFO is a plist used as the communication channel.
4088 Return alignment as specified by the last alignment cookie in the
4089 same column as TABLE-CELL. If no such cookie is found, a default
4090 alignment value will be deduced from fraction of numbers in the
4091 column (see `org-table-number-fraction' for more information).
4092 Possible values are `left', `right' and `center'."
4093 (let* ((row (org-export-get-parent table-cell))
4094 (column (let ((cells (org-element-contents row)))
4095 (- (length cells) (length (memq table-cell cells)))))
4096 (table (org-export-get-parent-table table-cell))
4097 (number-cells 0)
4098 (total-cells 0)
4099 cookie-align)
4100 (mapc
4101 (lambda (row)
4102 (cond
4103 ;; In a special row, try to find an alignment cookie at
4104 ;; COLUMN.
4105 ((org-export-table-row-is-special-p row info)
4106 (let ((value (org-element-contents
4107 (elt (org-element-contents row) column))))
4108 ;; Since VALUE is a secondary string, the following checks
4109 ;; avoid useless expansion through `org-export-data'.
4110 (when (and value
4111 (not (cdr value))
4112 (stringp (car value))
4113 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4114 (car value))
4115 (match-string 1 (car value)))
4116 (setq cookie-align (match-string 1 (car value))))))
4117 ;; Ignore table rules.
4118 ((eq (org-element-property :type row) 'rule))
4119 ;; In a standard row, check if cell's contents are expressing
4120 ;; some kind of number. Increase NUMBER-CELLS accordingly.
4121 ;; Though, don't bother if an alignment cookie has already
4122 ;; defined cell's alignment.
4123 ((not cookie-align)
4124 (let ((value (org-export-data
4125 (org-element-contents
4126 (elt (org-element-contents row) column))
4127 info)))
4128 (incf total-cells)
4129 (when (string-match org-table-number-regexp value)
4130 (incf number-cells))))))
4131 (org-element-contents table))
4132 ;; Return value. Alignment specified by cookies has precedence
4133 ;; over alignment deduced from cells contents.
4134 (cond ((equal cookie-align "l") 'left)
4135 ((equal cookie-align "r") 'right)
4136 ((equal cookie-align "c") 'center)
4137 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
4138 'right)
4139 (t 'left))))
4141 (defun org-export-table-cell-borders (table-cell info)
4142 "Return TABLE-CELL borders.
4144 INFO is a plist used as a communication channel.
4146 Return value is a list of symbols, or nil. Possible values are:
4147 `top', `bottom', `above', `below', `left' and `right'. Note:
4148 `top' (resp. `bottom') only happen for a cell in the first
4149 row (resp. last row) of the table, ignoring table rules, if any.
4151 Returned borders ignore special rows."
4152 (let* ((row (org-export-get-parent table-cell))
4153 (table (org-export-get-parent-table table-cell))
4154 borders)
4155 ;; Top/above border? TABLE-CELL has a border above when a rule
4156 ;; used to demarcate row groups can be found above. Hence,
4157 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4158 ;; another regular row has to be found above that rule.
4159 (let (rule-flag)
4160 (catch 'exit
4161 (mapc (lambda (row)
4162 (cond ((eq (org-element-property :type row) 'rule)
4163 (setq rule-flag t))
4164 ((not (org-export-table-row-is-special-p row info))
4165 (if rule-flag (throw 'exit (push 'above borders))
4166 (throw 'exit nil)))))
4167 ;; Look at every row before the current one.
4168 (cdr (memq row (reverse (org-element-contents table)))))
4169 ;; No rule above, or rule found starts the table (ignoring any
4170 ;; special row): TABLE-CELL is at the top of the table.
4171 (when rule-flag (push 'above borders))
4172 (push 'top borders)))
4173 ;; Bottom/below border? TABLE-CELL has a border below when next
4174 ;; non-regular row below is a rule.
4175 (let (rule-flag)
4176 (catch 'exit
4177 (mapc (lambda (row)
4178 (cond ((eq (org-element-property :type row) 'rule)
4179 (setq rule-flag t))
4180 ((not (org-export-table-row-is-special-p row info))
4181 (if rule-flag (throw 'exit (push 'below borders))
4182 (throw 'exit nil)))))
4183 ;; Look at every row after the current one.
4184 (cdr (memq row (org-element-contents table))))
4185 ;; No rule below, or rule found ends the table (modulo some
4186 ;; special row): TABLE-CELL is at the bottom of the table.
4187 (when rule-flag (push 'below borders))
4188 (push 'bottom borders)))
4189 ;; Right/left borders? They can only be specified by column
4190 ;; groups. Column groups are defined in a row starting with "/".
4191 ;; Also a column groups row only contains "<", "<>", ">" or blank
4192 ;; cells.
4193 (catch 'exit
4194 (let ((column (let ((cells (org-element-contents row)))
4195 (- (length cells) (length (memq table-cell cells))))))
4196 (mapc
4197 (lambda (row)
4198 (unless (eq (org-element-property :type row) 'rule)
4199 (when (equal (org-element-contents
4200 (car (org-element-contents row)))
4201 '("/"))
4202 (let ((column-groups
4203 (mapcar
4204 (lambda (cell)
4205 (let ((value (org-element-contents cell)))
4206 (when (member value '(("<") ("<>") (">") nil))
4207 (car value))))
4208 (org-element-contents row))))
4209 ;; There's a left border when previous cell, if
4210 ;; any, ends a group, or current one starts one.
4211 (when (or (and (not (zerop column))
4212 (member (elt column-groups (1- column))
4213 '(">" "<>")))
4214 (member (elt column-groups column) '("<" "<>")))
4215 (push 'left borders))
4216 ;; There's a right border when next cell, if any,
4217 ;; starts a group, or current one ends one.
4218 (when (or (and (/= (1+ column) (length column-groups))
4219 (member (elt column-groups (1+ column))
4220 '("<" "<>")))
4221 (member (elt column-groups column) '(">" "<>")))
4222 (push 'right borders))
4223 (throw 'exit nil)))))
4224 ;; Table rows are read in reverse order so last column groups
4225 ;; row has precedence over any previous one.
4226 (reverse (org-element-contents table)))))
4227 ;; Return value.
4228 borders))
4230 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4231 "Non-nil when TABLE-CELL is at the beginning of a row group.
4232 INFO is a plist used as a communication channel."
4233 ;; A cell starts a column group either when it is at the beginning
4234 ;; of a row (or after the special column, if any) or when it has
4235 ;; a left border.
4236 (or (eq (org-element-map
4237 (org-export-get-parent table-cell)
4238 'table-cell 'identity info 'first-match)
4239 table-cell)
4240 (memq 'left (org-export-table-cell-borders table-cell info))))
4242 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4243 "Non-nil when TABLE-CELL is at the end of a row group.
4244 INFO is a plist used as a communication channel."
4245 ;; A cell ends a column group either when it is at the end of a row
4246 ;; or when it has a right border.
4247 (or (eq (car (last (org-element-contents
4248 (org-export-get-parent table-cell))))
4249 table-cell)
4250 (memq 'right (org-export-table-cell-borders table-cell info))))
4252 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4253 "Non-nil when TABLE-ROW is at the beginning of a column group.
4254 INFO is a plist used as a communication channel."
4255 (unless (or (eq (org-element-property :type table-row) 'rule)
4256 (org-export-table-row-is-special-p table-row info))
4257 (let ((borders (org-export-table-cell-borders
4258 (car (org-element-contents table-row)) info)))
4259 (or (memq 'top borders) (memq 'above borders)))))
4261 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4262 "Non-nil when TABLE-ROW is at the end of a column group.
4263 INFO is a plist used as a communication channel."
4264 (unless (or (eq (org-element-property :type table-row) 'rule)
4265 (org-export-table-row-is-special-p table-row info))
4266 (let ((borders (org-export-table-cell-borders
4267 (car (org-element-contents table-row)) info)))
4268 (or (memq 'bottom borders) (memq 'below borders)))))
4270 (defun org-export-table-row-starts-header-p (table-row info)
4271 "Non-nil when TABLE-ROW is the first table header's row.
4272 INFO is a plist used as a communication channel."
4273 (and (org-export-table-has-header-p
4274 (org-export-get-parent-table table-row) info)
4275 (org-export-table-row-starts-rowgroup-p table-row info)
4276 (= (org-export-table-row-group table-row info) 1)))
4278 (defun org-export-table-row-ends-header-p (table-row info)
4279 "Non-nil when TABLE-ROW is the last table header's row.
4280 INFO is a plist used as a communication channel."
4281 (and (org-export-table-has-header-p
4282 (org-export-get-parent-table table-row) info)
4283 (org-export-table-row-ends-rowgroup-p table-row info)
4284 (= (org-export-table-row-group table-row info) 1)))
4286 (defun org-export-table-dimensions (table info)
4287 "Return TABLE dimensions.
4289 INFO is a plist used as a communication channel.
4291 Return value is a CONS like (ROWS . COLUMNS) where
4292 ROWS (resp. COLUMNS) is the number of exportable
4293 rows (resp. columns)."
4294 (let (first-row (columns 0) (rows 0))
4295 ;; Set number of rows, and extract first one.
4296 (org-element-map
4297 table 'table-row
4298 (lambda (row)
4299 (when (eq (org-element-property :type row) 'standard)
4300 (incf rows)
4301 (unless first-row (setq first-row row)))) info)
4302 ;; Set number of columns.
4303 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4304 ;; Return value.
4305 (cons rows columns)))
4307 (defun org-export-table-cell-address (table-cell info)
4308 "Return address of a regular TABLE-CELL object.
4310 TABLE-CELL is the cell considered. INFO is a plist used as
4311 a communication channel.
4313 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4314 zero-based index. Only exportable cells are considered. The
4315 function returns nil for other cells."
4316 (let* ((table-row (org-export-get-parent table-cell))
4317 (table (org-export-get-parent-table table-cell)))
4318 ;; Ignore cells in special rows or in special column.
4319 (unless (or (org-export-table-row-is-special-p table-row info)
4320 (and (org-export-table-has-special-column-p table)
4321 (eq (car (org-element-contents table-row)) table-cell)))
4322 (cons
4323 ;; Row number.
4324 (let ((row-count 0))
4325 (org-element-map
4326 table 'table-row
4327 (lambda (row)
4328 (cond ((eq (org-element-property :type row) 'rule) nil)
4329 ((eq row table-row) row-count)
4330 (t (incf row-count) nil)))
4331 info 'first-match))
4332 ;; Column number.
4333 (let ((col-count 0))
4334 (org-element-map
4335 table-row 'table-cell
4336 (lambda (cell)
4337 (if (eq cell table-cell) col-count (incf col-count) nil))
4338 info 'first-match))))))
4340 (defun org-export-get-table-cell-at (address table info)
4341 "Return regular table-cell object at ADDRESS in TABLE.
4343 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4344 zero-based index. TABLE is a table type element. INFO is
4345 a plist used as a communication channel.
4347 If no table-cell, among exportable cells, is found at ADDRESS,
4348 return nil."
4349 (let ((column-pos (cdr address)) (column-count 0))
4350 (org-element-map
4351 ;; Row at (car address) or nil.
4352 (let ((row-pos (car address)) (row-count 0))
4353 (org-element-map
4354 table 'table-row
4355 (lambda (row)
4356 (cond ((eq (org-element-property :type row) 'rule) nil)
4357 ((= row-count row-pos) row)
4358 (t (incf row-count) nil)))
4359 info 'first-match))
4360 'table-cell
4361 (lambda (cell)
4362 (if (= column-count column-pos) cell
4363 (incf column-count) nil))
4364 info 'first-match)))
4367 ;;;; For Tables Of Contents
4369 ;; `org-export-collect-headlines' builds a list of all exportable
4370 ;; headline elements, maybe limited to a certain depth. One can then
4371 ;; easily parse it and transcode it.
4373 ;; Building lists of tables, figures or listings is quite similar.
4374 ;; Once the generic function `org-export-collect-elements' is defined,
4375 ;; `org-export-collect-tables', `org-export-collect-figures' and
4376 ;; `org-export-collect-listings' can be derived from it.
4378 (defun org-export-collect-headlines (info &optional n)
4379 "Collect headlines in order to build a table of contents.
4381 INFO is a plist used as a communication channel.
4383 When optional argument N is an integer, it specifies the depth of
4384 the table of contents. Otherwise, it is set to the value of the
4385 last headline level. See `org-export-headline-levels' for more
4386 information.
4388 Return a list of all exportable headlines as parsed elements."
4389 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4390 (org-element-map
4391 (plist-get info :parse-tree)
4392 'headline
4393 (lambda (headline)
4394 ;; Strip contents from HEADLINE.
4395 (let ((relative-level (org-export-get-relative-level headline info)))
4396 (unless (> relative-level n) headline)))
4397 info))
4399 (defun org-export-collect-elements (type info &optional predicate)
4400 "Collect referenceable elements of a determined type.
4402 TYPE can be a symbol or a list of symbols specifying element
4403 types to search. Only elements with a caption are collected.
4405 INFO is a plist used as a communication channel.
4407 When non-nil, optional argument PREDICATE is a function accepting
4408 one argument, an element of type TYPE. It returns a non-nil
4409 value when that element should be collected.
4411 Return a list of all elements found, in order of appearance."
4412 (org-element-map
4413 (plist-get info :parse-tree) type
4414 (lambda (element)
4415 (and (org-element-property :caption element)
4416 (or (not predicate) (funcall predicate element))
4417 element))
4418 info))
4420 (defun org-export-collect-tables (info)
4421 "Build a list of tables.
4422 INFO is a plist used as a communication channel.
4424 Return a list of table elements with a caption."
4425 (org-export-collect-elements 'table info))
4427 (defun org-export-collect-figures (info predicate)
4428 "Build a list of figures.
4430 INFO is a plist used as a communication channel. PREDICATE is
4431 a function which accepts one argument: a paragraph element and
4432 whose return value is non-nil when that element should be
4433 collected.
4435 A figure is a paragraph type element, with a caption, verifying
4436 PREDICATE. The latter has to be provided since a \"figure\" is
4437 a vague concept that may depend on back-end.
4439 Return a list of elements recognized as figures."
4440 (org-export-collect-elements 'paragraph info predicate))
4442 (defun org-export-collect-listings (info)
4443 "Build a list of src blocks.
4445 INFO is a plist used as a communication channel.
4447 Return a list of src-block elements with a caption."
4448 (org-export-collect-elements 'src-block info))
4451 ;;;; Smart Quotes
4453 ;; The main function for the smart quotes sub-system is
4454 ;; `org-export-activate-smart-quotes', which replaces every quote in
4455 ;; a given string from the parse tree with its "smart" counterpart.
4457 ;; Dictionary for smart quotes is stored in
4458 ;; `org-export-smart-quotes-alist'.
4460 ;; Internally, regexps matching potential smart quotes (checks at
4461 ;; string boundaries are also necessary) are defined in
4462 ;; `org-export-smart-quotes-regexps'.
4464 (defconst org-export-smart-quotes-alist
4465 '(("de"
4466 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
4467 :texinfo "@quotedblbase{}")
4468 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
4469 :texinfo "@quotedblleft{}")
4470 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
4471 :texinfo "@quotesinglbase{}")
4472 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
4473 :texinfo "@quoteleft{}")
4474 (apostrophe :utf-8 "’" :html "&rsquo;"))
4475 ("en"
4476 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4477 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4478 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4479 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4480 (apostrophe :utf-8 "’" :html "&rsquo;"))
4481 ("es"
4482 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4483 :texinfo "@guillemetleft{}")
4484 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4485 :texinfo "@guillemetright{}")
4486 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4487 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4488 (apostrophe :utf-8 "’" :html "&rsquo;"))
4489 ("fr"
4490 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4491 :texinfo "@guillemetleft{}@tie{}")
4492 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4493 :texinfo "@tie{}@guillemetright{}")
4494 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4495 :texinfo "@guillemetleft{}@tie{}")
4496 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4497 :texinfo "@tie{}@guillemetright{}")
4498 (apostrophe :utf-8 "’" :html "&rsquo;")))
4499 "Smart quotes translations.
4501 Alist whose CAR is a language string and CDR is an alist with
4502 quote type as key and a plist associating various encodings to
4503 their translation as value.
4505 A quote type can be any symbol among `opening-double-quote',
4506 `closing-double-quote', `opening-single-quote',
4507 `closing-single-quote' and `apostrophe'.
4509 Valid encodings include `:utf-8', `:html', `:latex' and
4510 `:texinfo'.
4512 If no translation is found, the quote character is left as-is.")
4514 (defconst org-export-smart-quotes-regexps
4515 (list
4516 ;; Possible opening quote at beginning of string.
4517 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4518 ;; Possible closing quote at beginning of string.
4519 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4520 ;; Possible apostrophe at beginning of string.
4521 "\\`\\('\\)\\S-"
4522 ;; Opening single and double quotes.
4523 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4524 ;; Closing single and double quotes.
4525 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4526 ;; Apostrophe.
4527 "\\S-\\('\\)\\S-"
4528 ;; Possible opening quote at end of string.
4529 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4530 ;; Possible closing quote at end of string.
4531 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4532 ;; Possible apostrophe at end of string.
4533 "\\S-\\('\\)\\'")
4534 "List of regexps matching a quote or an apostrophe.
4535 In every regexp, quote or apostrophe matched is put in group 1.")
4537 (defun org-export-activate-smart-quotes (s encoding info &optional original)
4538 "Replace regular quotes with \"smart\" quotes in string S.
4540 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
4541 `:utf-8'. INFO is a plist used as a communication channel.
4543 The function has to retrieve information about string
4544 surroundings in parse tree. It can only happen with an
4545 unmodified string. Thus, if S has already been through another
4546 process, a non-nil ORIGINAL optional argument will provide that
4547 original string.
4549 Return the new string."
4550 (if (equal s "") ""
4551 (let* ((prev (org-export-get-previous-element (or original s) info))
4552 (pre-blank (and prev (org-element-property :post-blank prev)))
4553 (next (org-export-get-next-element (or original s) info))
4554 (get-smart-quote
4555 (lambda (q type)
4556 ;; Return smart quote associated to a give quote Q, as
4557 ;; a string. TYPE is a symbol among `open', `close' and
4558 ;; `apostrophe'.
4559 (let ((key (case type
4560 (apostrophe 'apostrophe)
4561 (open (if (equal "'" q) 'opening-single-quote
4562 'opening-double-quote))
4563 (otherwise (if (equal "'" q) 'closing-single-quote
4564 'closing-double-quote)))))
4565 (or (plist-get
4566 (cdr (assq key
4567 (cdr (assoc (plist-get info :language)
4568 org-export-smart-quotes-alist))))
4569 encoding)
4570 q)))))
4571 (if (or (equal "\"" s) (equal "'" s))
4572 ;; Only a quote: no regexp can match. We have to check both
4573 ;; sides and decide what to do.
4574 (cond ((and (not prev) (not next)) s)
4575 ((not prev) (funcall get-smart-quote s 'open))
4576 ((and (not next) (zerop pre-blank))
4577 (funcall get-smart-quote s 'close))
4578 ((not next) s)
4579 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
4580 (t (funcall get-smart-quote 'open)))
4581 ;; 1. Replace quote character at the beginning of S.
4582 (cond
4583 ;; Apostrophe?
4584 ((and prev (zerop pre-blank)
4585 (string-match (nth 2 org-export-smart-quotes-regexps) s))
4586 (setq s (replace-match
4587 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4588 nil t s 1)))
4589 ;; Closing quote?
4590 ((and prev (zerop pre-blank)
4591 (string-match (nth 1 org-export-smart-quotes-regexps) s))
4592 (setq s (replace-match
4593 (funcall get-smart-quote (match-string 1 s) 'close)
4594 nil t s 1)))
4595 ;; Opening quote?
4596 ((and (or (not prev) (> pre-blank 0))
4597 (string-match (nth 0 org-export-smart-quotes-regexps) s))
4598 (setq s (replace-match
4599 (funcall get-smart-quote (match-string 1 s) 'open)
4600 nil t s 1))))
4601 ;; 2. Replace quotes in the middle of the string.
4602 (setq s (replace-regexp-in-string
4603 ;; Opening quotes.
4604 (nth 3 org-export-smart-quotes-regexps)
4605 (lambda (text)
4606 (funcall get-smart-quote (match-string 1 text) 'open))
4607 s nil t 1))
4608 (setq s (replace-regexp-in-string
4609 ;; Closing quotes.
4610 (nth 4 org-export-smart-quotes-regexps)
4611 (lambda (text)
4612 (funcall get-smart-quote (match-string 1 text) 'close))
4613 s nil t 1))
4614 (setq s (replace-regexp-in-string
4615 ;; Apostrophes.
4616 (nth 5 org-export-smart-quotes-regexps)
4617 (lambda (text)
4618 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
4619 s nil t 1))
4620 ;; 3. Replace quote character at the end of S.
4621 (cond
4622 ;; Apostrophe?
4623 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
4624 (setq s (replace-match
4625 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
4626 nil t s 1)))
4627 ;; Closing quote?
4628 ((and (not next)
4629 (string-match (nth 7 org-export-smart-quotes-regexps) s))
4630 (setq s (replace-match
4631 (funcall get-smart-quote (match-string 1 s) 'close)
4632 nil t s 1)))
4633 ;; Opening quote?
4634 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
4635 (setq s (replace-match
4636 (funcall get-smart-quote (match-string 1 s) 'open)
4637 nil t s 1))))
4638 ;; Return string with smart quotes.
4639 s))))
4641 ;;;; Topology
4643 ;; Here are various functions to retrieve information about the
4644 ;; neighbourhood of a given element or object. Neighbours of interest
4645 ;; are direct parent (`org-export-get-parent'), parent headline
4646 ;; (`org-export-get-parent-headline'), first element containing an
4647 ;; object, (`org-export-get-parent-element'), parent table
4648 ;; (`org-export-get-parent-table'), previous element or object
4649 ;; (`org-export-get-previous-element') and next element or object
4650 ;; (`org-export-get-next-element').
4652 ;; `org-export-get-genealogy' returns the full genealogy of a given
4653 ;; element or object, from closest parent to full parse tree.
4655 (defun org-export-get-parent (blob)
4656 "Return BLOB parent or nil.
4657 BLOB is the element or object considered."
4658 (org-element-property :parent blob))
4660 (defun org-export-get-genealogy (blob)
4661 "Return full genealogy relative to a given element or object.
4663 BLOB is the element or object being considered.
4665 Ancestors are returned from closest to farthest, the last one
4666 being the full parse tree."
4667 (let (genealogy (parent blob))
4668 (while (setq parent (org-element-property :parent parent))
4669 (push parent genealogy))
4670 (nreverse genealogy)))
4672 (defun org-export-get-parent-headline (blob)
4673 "Return BLOB parent headline or nil.
4674 BLOB is the element or object being considered."
4675 (let ((parent blob))
4676 (while (and (setq parent (org-element-property :parent parent))
4677 (not (eq (org-element-type parent) 'headline))))
4678 parent))
4680 (defun org-export-get-parent-element (object)
4681 "Return first element containing OBJECT or nil.
4682 OBJECT is the object to consider."
4683 (let ((parent object))
4684 (while (and (setq parent (org-element-property :parent parent))
4685 (memq (org-element-type parent) org-element-all-objects)))
4686 parent))
4688 (defun org-export-get-parent-table (object)
4689 "Return OBJECT parent table or nil.
4690 OBJECT is either a `table-cell' or `table-element' type object."
4691 (let ((parent object))
4692 (while (and (setq parent (org-element-property :parent parent))
4693 (not (eq (org-element-type parent) 'table))))
4694 parent))
4696 (defun org-export-get-previous-element (blob info &optional n)
4697 "Return previous element or object.
4699 BLOB is an element or object. INFO is a plist used as
4700 a communication channel. Return previous exportable element or
4701 object, a string, or nil.
4703 When optional argument N is a positive integer, return a list
4704 containing up to N siblings before BLOB, from closest to
4705 farthest. With any other non-nil value, return a list containing
4706 all of them."
4707 (let ((siblings
4708 ;; An object can belong to the contents of its parent or
4709 ;; to a secondary string. We check the latter option
4710 ;; first.
4711 (let ((parent (org-export-get-parent blob)))
4712 (or (and (not (memq (org-element-type blob)
4713 org-element-all-elements))
4714 (let ((sec-value
4715 (org-element-property
4716 (cdr (assq (org-element-type parent)
4717 org-element-secondary-value-alist))
4718 parent)))
4719 (and (memq blob sec-value) sec-value)))
4720 (org-element-contents parent))))
4721 prev)
4722 (catch 'exit
4723 (mapc (lambda (obj)
4724 (cond ((memq obj (plist-get info :ignore-list)))
4725 ((null n) (throw 'exit obj))
4726 ((not (wholenump n)) (push obj prev))
4727 ((zerop n) (throw 'exit (nreverse prev)))
4728 (t (decf n) (push obj prev))))
4729 (cdr (memq blob (reverse siblings))))
4730 (nreverse prev))))
4732 (defun org-export-get-next-element (blob info &optional n)
4733 "Return next element or object.
4735 BLOB is an element or object. INFO is a plist used as
4736 a communication channel. Return next exportable element or
4737 object, a string, or nil.
4739 When optional argument N is a positive integer, return a list
4740 containing up to N siblings after BLOB, from closest to farthest.
4741 With any other non-nil value, return a list containing all of
4742 them."
4743 (let ((siblings
4744 ;; An object can belong to the contents of its parent or to
4745 ;; a secondary string. We check the latter option first.
4746 (let ((parent (org-export-get-parent blob)))
4747 (or (and (not (memq (org-element-type blob)
4748 org-element-all-objects))
4749 (let ((sec-value
4750 (org-element-property
4751 (cdr (assq (org-element-type parent)
4752 org-element-secondary-value-alist))
4753 parent)))
4754 (cdr (memq blob sec-value))))
4755 (cdr (memq blob (org-element-contents parent))))))
4756 next)
4757 (catch 'exit
4758 (mapc (lambda (obj)
4759 (cond ((memq obj (plist-get info :ignore-list)))
4760 ((null n) (throw 'exit obj))
4761 ((not (wholenump n)) (push obj next))
4762 ((zerop n) (throw 'exit (nreverse next)))
4763 (t (decf n) (push obj next))))
4764 siblings)
4765 (nreverse next))))
4768 ;;;; Translation
4770 ;; `org-export-translate' translates a string according to language
4771 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4772 ;; variable and a specified charset. `org-export-dictionary' contains
4773 ;; the dictionary used for the translation.
4775 (defconst org-export-dictionary
4776 '(("Author"
4777 ("ca" :default "Autor")
4778 ("cs" :default "Autor")
4779 ("da" :default "Ophavsmand")
4780 ("de" :default "Autor")
4781 ("eo" :html "A&#365;toro")
4782 ("es" :default "Autor")
4783 ("fi" :html "Tekij&auml;")
4784 ("fr" :default "Auteur")
4785 ("hu" :default "Szerz&otilde;")
4786 ("is" :html "H&ouml;fundur")
4787 ("it" :default "Autore")
4788 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4789 ("nl" :default "Auteur")
4790 ("no" :default "Forfatter")
4791 ("nb" :default "Forfatter")
4792 ("nn" :default "Forfattar")
4793 ("pl" :default "Autor")
4794 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4795 ("sv" :html "F&ouml;rfattare")
4796 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4797 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4798 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4799 ("Date"
4800 ("ca" :default "Data")
4801 ("cs" :default "Datum")
4802 ("da" :default "Dato")
4803 ("de" :default "Datum")
4804 ("eo" :default "Dato")
4805 ("es" :default "Fecha")
4806 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4807 ("hu" :html "D&aacute;tum")
4808 ("is" :default "Dagsetning")
4809 ("it" :default "Data")
4810 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4811 ("nl" :default "Datum")
4812 ("no" :default "Dato")
4813 ("nb" :default "Dato")
4814 ("nn" :default "Dato")
4815 ("pl" :default "Data")
4816 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4817 ("sv" :default "Datum")
4818 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4819 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4820 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4821 ("Equation"
4822 ("fr" :ascii "Equation" :default "Équation"))
4823 ("Figure")
4824 ("Footnotes"
4825 ("ca" :html "Peus de p&agrave;gina")
4826 ("cs" :default "Pozn\xe1mky pod carou")
4827 ("da" :default "Fodnoter")
4828 ("de" :html "Fu&szlig;noten")
4829 ("eo" :default "Piednotoj")
4830 ("es" :html "Pies de p&aacute;gina")
4831 ("fi" :default "Alaviitteet")
4832 ("fr" :default "Notes de bas de page")
4833 ("hu" :html "L&aacute;bjegyzet")
4834 ("is" :html "Aftanm&aacute;lsgreinar")
4835 ("it" :html "Note a pi&egrave; di pagina")
4836 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4837 ("nl" :default "Voetnoten")
4838 ("no" :default "Fotnoter")
4839 ("nb" :default "Fotnoter")
4840 ("nn" :default "Fotnotar")
4841 ("pl" :default "Przypis")
4842 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4843 ("sv" :default "Fotnoter")
4844 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4845 :utf-8 "Примітки")
4846 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4847 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4848 ("List of Listings"
4849 ("fr" :default "Liste des programmes"))
4850 ("List of Tables"
4851 ("fr" :default "Liste des tableaux"))
4852 ("Listing %d:"
4853 ("fr"
4854 :ascii "Programme %d :" :default "Programme nº %d :"
4855 :latin1 "Programme %d :"))
4856 ("Listing %d: %s"
4857 ("fr"
4858 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4859 :latin1 "Programme %d : %s"))
4860 ("See section %s"
4861 ("fr" :default "cf. section %s"))
4862 ("Table %d:"
4863 ("fr"
4864 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4865 ("Table %d: %s"
4866 ("fr"
4867 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4868 :latin1 "Tableau %d : %s"))
4869 ("Table of Contents"
4870 ("ca" :html "&Iacute;ndex")
4871 ("cs" :default "Obsah")
4872 ("da" :default "Indhold")
4873 ("de" :default "Inhaltsverzeichnis")
4874 ("eo" :default "Enhavo")
4875 ("es" :html "&Iacute;ndice")
4876 ("fi" :html "Sis&auml;llysluettelo")
4877 ("fr" :ascii "Sommaire" :default "Table des matières")
4878 ("hu" :html "Tartalomjegyz&eacute;k")
4879 ("is" :default "Efnisyfirlit")
4880 ("it" :default "Indice")
4881 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4882 ("nl" :default "Inhoudsopgave")
4883 ("no" :default "Innhold")
4884 ("nb" :default "Innhold")
4885 ("nn" :default "Innhald")
4886 ("pl" :html "Spis tre&#x015b;ci")
4887 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4888 :utf-8 "Содержание")
4889 ("sv" :html "Inneh&aring;ll")
4890 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4891 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4892 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4893 ("Unknown reference"
4894 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4895 "Dictionary for export engine.
4897 Alist whose CAR is the string to translate and CDR is an alist
4898 whose CAR is the language string and CDR is a plist whose
4899 properties are possible charsets and values translated terms.
4901 It is used as a database for `org-export-translate'. Since this
4902 function returns the string as-is if no translation was found,
4903 the variable only needs to record values different from the
4904 entry.")
4906 (defun org-export-translate (s encoding info)
4907 "Translate string S according to language specification.
4909 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4910 and `:utf-8'. INFO is a plist used as a communication channel.
4912 Translation depends on `:language' property. Return the
4913 translated string. If no translation is found, try to fall back
4914 to `:default' encoding. If it fails, return S."
4915 (let* ((lang (plist-get info :language))
4916 (translations (cdr (assoc lang
4917 (cdr (assoc s org-export-dictionary))))))
4918 (or (plist-get translations encoding)
4919 (plist-get translations :default)
4920 s)))
4924 ;;; Asynchronous Export
4926 ;; `org-export-async-start' is the entry point for asynchronous
4927 ;; export. It recreates current buffer (including visibility,
4928 ;; narrowing and visited file) in an external Emacs process, and
4929 ;; evaluates a command there. It then applies a function on the
4930 ;; returned results in the current process.
4932 ;; Asynchronously generated results are never displayed directly.
4933 ;; Instead, they are stored in `org-export-stack-contents'. They can
4934 ;; then be retrieved by calling `org-export-stack'.
4936 ;; Export Stack is viewed through a dedicated major mode
4937 ;;`org-export-stack-mode' and tools: `org-export--stack-refresh',
4938 ;;`org-export--stack-delete', `org-export--stack-view' and
4939 ;;`org-export--stack-clear'.
4941 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
4942 ;; It should used whenever `org-export-async-start' is called.
4944 (defmacro org-export-async-start (fun &rest body)
4945 "Call function FUN on the results returned by BODY evaluation.
4947 BODY evaluation happens in an asynchronous process, from a buffer
4948 which is an exact copy of the current one.
4950 Use `org-export-add-to-stack' in FUN in order to register results
4951 in the stack. Examples for, respectively a temporary buffer and
4952 a file are:
4954 \(org-export-async-start
4955 \(lambda (output)
4956 \(with-current-buffer (get-buffer-create \"*Org BACKEND Export*\")
4957 \(erase-buffer)
4958 \(insert output)
4959 \(goto-char (point-min))
4960 \(org-export-add-to-stack (current-buffer) 'backend)))
4961 `(org-export-as 'backend ,subtreep ,visible-only ,body-only ',ext-plist))
4965 \(org-export-async-start
4966 \(lambda (f) (org-export-add-to-stack f 'backend))
4967 `(expand-file-name
4968 \(org-export-to-file
4969 'backend ,outfile ,subtreep ,visible-only ,body-only ',ext-plist)))"
4970 (declare (indent 1) (debug t))
4971 (org-with-gensyms (process temp-file copy-fun proc-buffer handler)
4972 ;; Write the full sexp evaluating BODY in a copy of the current
4973 ;; buffer to a temporary file, as it may be too long for program
4974 ;; args in `start-process'.
4975 `(with-temp-message "Initializing asynchronous export process"
4976 (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
4977 (,temp-file (make-temp-file "org-export-process")))
4978 (with-temp-file ,temp-file
4979 (insert
4980 (format
4981 "%S"
4982 `(with-temp-buffer
4983 ,(when org-export-async-debug '(setq debug-on-error t))
4984 ;; Initialize `org-mode' in the external process.
4985 (org-mode)
4986 ;; Re-create current buffer there.
4987 (funcall ,,copy-fun)
4988 (restore-buffer-modified-p nil)
4989 ;; Sexp to evaluate in the buffer.
4990 (print (progn ,,@body))))))
4991 ;; Start external process.
4992 (let* ((process-connection-type nil)
4993 (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
4994 (,process
4995 (start-process
4996 "org-export-process" ,proc-buffer
4997 (expand-file-name invocation-name invocation-directory)
4998 "-Q" "--batch"
4999 "-l" org-export-async-init-file
5000 "-l" ,temp-file)))
5001 ;; Register running process in stack.
5002 (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
5003 ;; Set-up sentinel in order to catch results.
5004 (set-process-sentinel
5005 ,process
5006 (let ((handler ',fun))
5007 `(lambda (p status)
5008 (let ((proc-buffer (process-buffer p)))
5009 (when (eq (process-status p) 'exit)
5010 (unwind-protect
5011 (if (zerop (process-exit-status p))
5012 (unwind-protect
5013 (let ((results
5014 (with-current-buffer proc-buffer
5015 (goto-char (point-max))
5016 (backward-sexp)
5017 (read (current-buffer)))))
5018 (funcall ,handler results))
5019 (unless org-export-async-debug
5020 (and (get-buffer proc-buffer)
5021 (kill-buffer proc-buffer))))
5022 (org-export-add-to-stack proc-buffer nil p)
5023 (ding)
5024 (message "Process '%s' exited abnormally" p))
5025 (unless org-export-async-debug
5026 (delete-file ,,temp-file)))))))))))))
5028 (defun org-export-add-to-stack (source backend &optional process)
5029 "Add a new result to export stack if not present already.
5031 SOURCE is a buffer or a file name containing export results.
5032 BACKEND is a symbol representing export back-end used to generate
5035 Entries already pointing to SOURCE and unavailable entries are
5036 removed beforehand. Return the new stack."
5037 (setq org-export-stack-contents
5038 (cons (list source backend (or process (current-time)))
5039 (org-export--stack-remove source))))
5041 (defun org-export-stack ()
5042 "Menu for asynchronous export results and running processes."
5043 (interactive)
5044 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5045 (set-buffer buffer)
5046 (when (zerop (buffer-size)) (org-export-stack-mode))
5047 (org-export--stack-refresh)
5048 (pop-to-buffer buffer))
5049 (message "Type \"q\" to quit, \"?\" for help"))
5051 (defun org-export--stack-source-at-point ()
5052 "Return source from export results at point in stack."
5053 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
5054 (if (not source) (error "Source unavailable, please refresh buffer")
5055 (let ((source-name (if (stringp source) source (buffer-name source))))
5056 (if (save-excursion
5057 (beginning-of-line)
5058 (looking-at (concat ".* +" (regexp-quote source-name) "$")))
5059 source
5060 ;; SOURCE is not consistent with current line. The stack
5061 ;; view is outdated.
5062 (error "Source unavailable; type `g' to update buffer"))))))
5064 (defun org-export--stack-clear ()
5065 "Remove all entries from export stack."
5066 (interactive)
5067 (setq org-export-stack-contents nil))
5069 (defun org-export--stack-refresh (&rest dummy)
5070 "Refresh the asynchronous export stack.
5071 DUMMY is ignored. Unavailable sources are removed from the list.
5072 Return the new stack."
5073 (let ((inhibit-read-only t))
5074 (org-preserve-lc
5075 (erase-buffer)
5076 (insert (concat
5077 (let ((counter 0))
5078 (mapconcat
5079 (lambda (entry)
5080 (let ((proc-p (processp (nth 2 entry))))
5081 (concat
5082 ;; Back-end.
5083 (format " %-12s " (or (nth 1 entry) ""))
5084 ;; Age.
5085 (let ((data (nth 2 entry)))
5086 (if proc-p (format " %6s " (process-status data))
5087 ;; Compute age of the results.
5088 (org-format-seconds
5089 "%4h:%.2m "
5090 (float-time (time-since data)))))
5091 ;; Source.
5092 (format " %s"
5093 (let ((source (car entry)))
5094 (if (stringp source) source
5095 (buffer-name source)))))))
5096 ;; Clear stack from exited processes, dead buffers or
5097 ;; non-existent files.
5098 (setq org-export-stack-contents
5099 (org-remove-if-not
5100 (lambda (el)
5101 (if (processp (nth 2 el))
5102 (buffer-live-p (process-buffer (nth 2 el)))
5103 (let ((source (car el)))
5104 (if (bufferp source) (buffer-live-p source)
5105 (file-exists-p source)))))
5106 org-export-stack-contents)) "\n")))))))
5108 (defun org-export--stack-remove (&optional source)
5109 "Remove export results at point from stack.
5110 If optional argument SOURCE is non-nil, remove it instead."
5111 (interactive)
5112 (let ((source (or source (org-export--stack-source-at-point))))
5113 (setq org-export-stack-contents
5114 (org-remove-if (lambda (el) (equal (car el) source))
5115 org-export-stack-contents))))
5117 (defun org-export--stack-view (&optional in-emacs)
5118 "View export results at point in stack.
5119 With an optional prefix argument IN-EMACS, force viewing files
5120 within Emacs."
5121 (interactive "P")
5122 (let ((source (org-export--stack-source-at-point)))
5123 (cond ((processp source)
5124 (org-switch-to-buffer-other-window (process-buffer source)))
5125 ((bufferp source) (org-switch-to-buffer-other-window source))
5126 (t (org-open-file source in-emacs)))))
5128 (defconst org-export-stack-mode-map
5129 (let ((km (make-sparse-keymap)))
5130 (define-key km " " 'next-line)
5131 (define-key km "n" 'next-line)
5132 (define-key km "\C-n" 'next-line)
5133 (define-key km [down] 'next-line)
5134 (define-key km "p" 'previous-line)
5135 (define-key km "\C-p" 'previous-line)
5136 (define-key km "\C-?" 'previous-line)
5137 (define-key km [up] 'previous-line)
5138 (define-key km "C" 'org-export--stack-clear)
5139 (define-key km "v" 'org-export--stack-view)
5140 (define-key km (kbd "RET") 'org-export--stack-view)
5141 (define-key km "d" 'org-export--stack-remove)
5143 "Keymap for Org Export Stack.")
5145 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5146 "Mode for displaying asynchronous export stack.
5148 Type \\[org-export-stack] to visualize the asynchronous export
5149 stack.
5151 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export--stack-view] to view export output
5152 on current line, \\[org-export--stack-remove] to remove it from the stack and \\[org-export--stack-clear] to clear
5153 stack completely.
5155 Removal entries in an Org Export Stack buffer doesn't affect
5156 files or buffers, only view in the stack.
5158 \\{org-export-stack-mode-map}"
5159 (abbrev-mode 0)
5160 (auto-fill-mode 0)
5161 (setq buffer-read-only t
5162 buffer-undo-list t
5163 truncate-lines t
5164 header-line-format
5165 '(:eval
5166 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
5167 (add-hook 'post-command-hook 'org-export--stack-refresh nil t)
5168 (set (make-local-variable 'revert-buffer-function)
5169 'org-export--stack-refresh))
5173 ;;; The Dispatcher
5175 ;; `org-export-dispatch' is the standard interactive way to start an
5176 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
5177 ;; for its interface, which, in turn, delegates response to key
5178 ;; pressed to `org-export-dispatch-action'.
5180 ;;;###autoload
5181 (defun org-export-dispatch (&optional arg)
5182 "Export dispatcher for Org mode.
5184 It provides an access to common export related tasks in a buffer.
5185 Its interface comes in two flavours: standard and expert. While
5186 both share the same set of bindings, only the former displays the
5187 valid keys associations. Set `org-export-dispatch-use-expert-ui'
5188 to switch to one or the other.
5190 When called with C-u prefix ARG, repeat the last export action,
5191 with the same set of options used back then, on the current
5192 buffer.
5194 When called with a double universal argument, display the
5195 asynchronous export stack directly."
5196 (interactive "P")
5197 (let* ((input
5198 (cond ((equal arg '(16)) '(stack))
5199 ((and arg org-export-dispatch-last-action))
5200 (t (save-window-excursion
5201 (unwind-protect
5202 ;; Store this export command.
5203 (setq org-export-dispatch-last-action
5204 (org-export-dispatch-ui
5205 (list org-export-initial-scope
5206 (and org-export-in-background 'async))
5208 org-export-dispatch-use-expert-ui))
5209 (and (get-buffer "*Org Export Dispatcher*")
5210 (kill-buffer "*Org Export Dispatcher*")))))))
5211 (action (car input))
5212 (optns (cdr input)))
5213 (case action
5214 ;; First handle special hard-coded actions.
5215 (stack (org-export-stack))
5216 (publish-current-file
5217 (org-e-publish-current-file (memq 'force optns) (memq 'async optns)))
5218 (publish-current-project
5219 (org-e-publish-current-project (memq 'force optns) (memq 'async optns)))
5220 (publish-choose-project
5221 (org-e-publish (assoc (org-icompleting-read
5222 "Publish project: "
5223 org-e-publish-project-alist nil t)
5224 org-e-publish-project-alist)
5225 (memq 'force optns)
5226 (memq 'async optns)))
5227 (publish-all (org-e-publish-all (memq 'force optns) (memq 'async optns)))
5228 (otherwise (funcall action
5229 ;; Return a symbol instead of a list to ease
5230 ;; asynchronous export macro use.
5231 (and (memq 'async optns) t)
5232 (and (memq 'subtree optns) t)
5233 (and (memq 'visible optns) t)
5234 (and (memq 'body optns) t))))))
5236 (defun org-export-dispatch-ui (options first-key expertp)
5237 "Handle interface for `org-export-dispatch'.
5239 OPTIONS is a list containing current interactive options set for
5240 export. It can contain any of the following symbols:
5241 `body' toggles a body-only export
5242 `subtree' restricts export to current subtree
5243 `visible' restricts export to visible part of buffer.
5244 `force' force publishing files.
5245 `async' use asynchronous export process
5247 FIRST-KEY is the key pressed to select the first level menu. It
5248 is nil when this menu hasn't been selected yet.
5250 EXPERTP, when non-nil, triggers expert UI. In that case, no help
5251 buffer is provided, but indications about currently active
5252 options are given in the prompt. Moreover, \[?] allows to switch
5253 back to standard interface."
5254 (let* ((fontify-key
5255 (lambda (key &optional access-key)
5256 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
5257 ;; non-nil is the required first-level key to activate
5258 ;; KEY. When its value is t, activate KEY independently
5259 ;; on the first key, if any. A nil value means KEY will
5260 ;; only be activated at first level.
5261 (if (or (eq access-key t) (eq access-key first-key))
5262 (org-propertize key 'face 'org-warning)
5263 key)))
5264 (fontify-value
5265 (lambda (value)
5266 ;; Fontify VALUE string.
5267 (org-propertize value 'face 'font-lock-variable-name-face)))
5268 ;; Prepare menu entries by extracting them from
5269 ;; `org-export-registered-backends', and sorting them by
5270 ;; access key and by ordinal, if any.
5271 (backends (sort
5272 (sort
5273 (delq nil
5274 (mapcar (lambda (b)
5275 (org-export-backend-menu (car b)))
5276 org-export-registered-backends))
5277 (lambda (a b)
5278 (let ((key-a (nth 1 a))
5279 (key-b (nth 1 b)))
5280 (cond ((and (numberp key-a) (numberp key-b))
5281 (< key-a key-b))
5282 ((numberp key-b) t)))))
5283 (lambda (a b) (< (car a) (car b)))))
5284 ;; Compute a list of allowed keys based on the first key
5285 ;; pressed, if any. Some keys (?^B, ?^V, ?^S, ?^F, ?^A
5286 ;; and ?q) are always available.
5287 (allowed-keys
5288 (nconc (list ?\x02 ?\x16 ?\x13 ?\x06 ?\x01)
5289 (if (not first-key) (org-uniquify (mapcar 'car backends))
5290 (let (sub-menu)
5291 (dolist (backend backends (sort (mapcar 'car sub-menu) '<))
5292 (when (eq (car backend) first-key)
5293 (setq sub-menu (append (nth 2 backend) sub-menu))))))
5294 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
5295 ((not first-key) (list ?P)))
5296 (list ?&)
5297 (when expertp (list ??))
5298 (list ?q)))
5299 ;; Build the help menu for standard UI.
5300 (help
5301 (unless expertp
5302 (concat
5303 ;; Options are hard-coded.
5304 (format "Options
5305 [%s] Body only: %s [%s] Visible only: %s
5306 [%s] Export scope: %s [%s] Force publishing: %s
5307 [%s] Async export: %s\n"
5308 (funcall fontify-key "C-b" t)
5309 (funcall fontify-value
5310 (if (memq 'body options) "On " "Off"))
5311 (funcall fontify-key "C-v" t)
5312 (funcall fontify-value
5313 (if (memq 'visible options) "On " "Off"))
5314 (funcall fontify-key "C-s" t)
5315 (funcall fontify-value
5316 (if (memq 'subtree options) "Subtree" "Buffer "))
5317 (funcall fontify-key "C-f" t)
5318 (funcall fontify-value
5319 (if (memq 'force options) "On " "Off"))
5320 (funcall fontify-key "C-a" t)
5321 (funcall fontify-value
5322 (if (memq 'async options) "On " "Off")))
5323 ;; Display registered back-end entries. When a key
5324 ;; appears for the second time, do not create another
5325 ;; entry, but append its sub-menu to existing menu.
5326 (let (last-key)
5327 (mapconcat
5328 (lambda (entry)
5329 (let ((top-key (car entry)))
5330 (concat
5331 (unless (eq top-key last-key)
5332 (setq last-key top-key)
5333 (format "\n[%s] %s\n"
5334 (funcall fontify-key (char-to-string top-key))
5335 (nth 1 entry)))
5336 (let ((sub-menu (nth 2 entry)))
5337 (unless (functionp sub-menu)
5338 ;; Split sub-menu into two columns.
5339 (let ((index -1))
5340 (concat
5341 (mapconcat
5342 (lambda (sub-entry)
5343 (incf index)
5344 (format
5345 (if (zerop (mod index 2)) " [%s] %-24s"
5346 "[%s] %s\n")
5347 (funcall fontify-key
5348 (char-to-string (car sub-entry))
5349 top-key)
5350 (nth 1 sub-entry)))
5351 sub-menu "")
5352 (when (zerop (mod index 2)) "\n"))))))))
5353 backends ""))
5354 ;; Publishing menu is hard-coded.
5355 (format "\n[%s] Publish
5356 [%s] Current file [%s] Current project
5357 [%s] Choose project [%s] All projects\n\n"
5358 (funcall fontify-key "P")
5359 (funcall fontify-key "f" ?P)
5360 (funcall fontify-key "p" ?P)
5361 (funcall fontify-key "x" ?P)
5362 (funcall fontify-key "a" ?P))
5363 (format "\[%s] Export stack\n" (funcall fontify-key "&" t))
5364 (format "\[%s] %s"
5365 (funcall fontify-key "q" t)
5366 (if first-key "Main menu" "Exit")))))
5367 ;; Build prompts for both standard and expert UI.
5368 (standard-prompt (unless expertp "Export command: "))
5369 (expert-prompt
5370 (when expertp
5371 (format
5372 "Export command (Options: %s%s%s%s%s) [%s]: "
5373 (if (memq 'body options) (funcall fontify-key "b" t) "b")
5374 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
5375 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
5376 (if (memq 'force options) (funcall fontify-key "f" t) "f")
5377 (if (memq 'async options) (funcall fontify-key "a" t) "a")
5378 (mapconcat (lambda (k)
5379 ;; Strip control characters.
5380 (unless (< k 27) (char-to-string k)))
5381 allowed-keys "")))))
5382 ;; With expert UI, just read key with a fancy prompt. In standard
5383 ;; UI, display an intrusive help buffer.
5384 (if expertp
5385 (org-export-dispatch-action
5386 expert-prompt allowed-keys backends options first-key expertp)
5387 ;; At first call, create frame layout in order to display menu.
5388 (unless (get-buffer "*Org Export Dispatcher*")
5389 (delete-other-windows)
5390 (org-switch-to-buffer-other-window
5391 (get-buffer-create "*Org Export Dispatcher*"))
5392 (setq cursor-type nil))
5393 ;; At this point, the buffer containing the menu exists and is
5394 ;; visible in the current window. So, refresh it.
5395 (with-current-buffer "*Org Export Dispatcher*"
5396 (erase-buffer)
5397 (insert help))
5398 (org-fit-window-to-buffer)
5399 (org-export-dispatch-action
5400 standard-prompt allowed-keys backends options first-key expertp))))
5402 (defun org-export-dispatch-action
5403 (prompt allowed-keys backends options first-key expertp)
5404 "Read a character from command input and act accordingly.
5406 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
5407 a list of characters available at a given step in the process.
5408 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
5409 EXPERTP are the same as defined in `org-export-dispatch-ui',
5410 which see.
5412 Toggle export options when required. Otherwise, return value is
5413 a list with action as CAR and a list of interactive export
5414 options as CDR."
5415 (let ((key (read-char-exclusive prompt)))
5416 (cond
5417 ;; Ignore undefined associations.
5418 ((not (memq key allowed-keys))
5419 (ding)
5420 (unless expertp (message "Invalid key") (sit-for 1))
5421 (org-export-dispatch-ui options first-key expertp))
5422 ;; q key at first level aborts export. At second
5423 ;; level, cancel first key instead.
5424 ((eq key ?q) (if (not first-key) (error "Export aborted")
5425 (org-export-dispatch-ui options nil expertp)))
5426 ;; Help key: Switch back to standard interface if
5427 ;; expert UI was active.
5428 ((eq key ??) (org-export-dispatch-ui options first-key nil))
5429 ;; Switch to asynchronous export stack.
5430 ((eq key ?&) '(stack))
5431 ;; Toggle export options.
5432 ((memq key '(?\x02 ?\x16 ?\x13 ?\x06 ?\x01))
5433 (org-export-dispatch-ui
5434 (let ((option (case key (?\x02 'body) (?\x16 'visible) (?\x13 'subtree)
5435 (?\x06 'force) (?\x01 'async))))
5436 (if (memq option options) (remq option options)
5437 (cons option options)))
5438 first-key expertp))
5439 ;; Action selected: Send key and options back to
5440 ;; `org-export-dispatch'.
5441 ((or first-key (functionp (nth 2 (assq key backends))))
5442 (cons (cond
5443 ((not first-key) (nth 2 (assq key backends)))
5444 ;; Publishing actions are hard-coded. Send a special
5445 ;; signal to `org-export-dispatch'.
5446 ((eq first-key ?P)
5447 (case key
5448 (?f 'publish-current-file)
5449 (?p 'publish-current-project)
5450 (?x 'publish-choose-project)
5451 (?a 'publish-all)))
5452 ;; Return first action associated to FIRST-KEY + KEY
5453 ;; path. Indeed, derived backends can share the same
5454 ;; FIRST-KEY.
5455 (t (catch 'found
5456 (mapc (lambda (backend)
5457 (let ((match (assq key (nth 2 backend))))
5458 (when match (throw 'found (nth 2 match)))))
5459 (member (assq first-key backends) backends)))))
5460 options))
5461 ;; Otherwise, enter sub-menu.
5462 (t (org-export-dispatch-ui options key expertp)))))
5465 (provide 'org-export)
5466 ;;; org-export.el ends here