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