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