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