org-export: Explicit error when using invalid syntax for INCLUDE keywords
[org-mode.git] / contrib / lisp / org-export.el
blob647518b2743e00ca4cdca23d5a8a034b7f433d24
1 ;;; org-export.el --- Generic Export Engine For Org
3 ;; Copyright (C) 2012 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This program is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; This program is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a generic export engine for Org, built on
24 ;; its syntactical parser: Org Elements.
26 ;; Besides that parser, the generic exporter is made of three distinct
27 ;; parts:
29 ;; - The communication channel consists in a property list, which is
30 ;; created and updated during the process. Its use is to offer
31 ;; every piece of information, would it be about initial environment
32 ;; or contextual data, all in a single place. The exhaustive list
33 ;; of properties is given in "The Communication Channel" section of
34 ;; this file.
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders. See "The Filter System"
45 ;; section for more information.
47 ;; The core function is `org-export-as'. It returns the transcoded
48 ;; buffer as a string.
50 ;; An export back-end is defined with `org-export-define-backend',
51 ;; which sets one mandatory variable: his translation table. Its name
52 ;; is always `org-BACKEND-translate-alist' where BACKEND stands for
53 ;; the name chosen for the back-end. Its value is an alist whose keys
54 ;; are elements and objects types and values translator functions.
55 ;; See function's docstring for more information about translators.
57 ;; Optionally, `org-export-define-backend' can also support specific
58 ;; buffer keywords, OPTION keyword's items and filters. Also refer to
59 ;; function documentation for more information.
61 ;; If the new back-end shares most properties with another one,
62 ;; `org-export-define-derived-backend' can be used to simplify the
63 ;; process.
65 ;; Any back-end can define its own variables. Among them, those
66 ;; customizable should belong to the `org-export-BACKEND' group.
68 ;; Tools for common tasks across back-ends are implemented in the
69 ;; penultimate part of this file. A dispatcher for standard back-ends
70 ;; is provided in the last one.
72 ;;; Code:
74 (eval-when-compile (require 'cl))
75 (require 'org-element)
77 (declare-function org-e-publish "org-e-publish" (project &optional force))
78 (declare-function org-e-publish-all "org-e-publish" (&optional force))
79 (declare-function org-e-publish-current-file "org-e-publish" (&optional force))
80 (declare-function org-e-publish-current-project "org-e-publish"
81 (&optional force))
82 (declare-function org-export-blocks-preprocess "ob-exp")
84 (defvar org-e-publish-project-alist)
85 (defvar org-table-number-fraction)
86 (defvar org-table-number-regexp)
90 ;;; Internal Variables
92 ;; Among internal variables, the most important is
93 ;; `org-export-options-alist'. This variable define the global export
94 ;; options, shared between every exporter, and how they are acquired.
96 (defconst org-export-max-depth 19
97 "Maximum nesting depth for headlines, counting from 0.")
99 (defconst org-export-options-alist
100 '((:author "AUTHOR" nil user-full-name t)
101 (:creator "CREATOR" nil org-export-creator-string)
102 (:date "DATE" nil nil t)
103 (:description "DESCRIPTION" nil nil newline)
104 (:email "EMAIL" nil user-mail-address t)
105 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
106 (:headline-levels nil "H" org-export-headline-levels)
107 (:keywords "KEYWORDS" nil nil space)
108 (:language "LANGUAGE" nil org-export-default-language t)
109 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
110 (:section-numbers nil "num" org-export-with-section-numbers)
111 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
112 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
113 (:title "TITLE" nil nil space)
114 (:with-archived-trees nil "arch" org-export-with-archived-trees)
115 (:with-author nil "author" org-export-with-author)
116 (:with-clocks nil "c" org-export-with-clocks)
117 (:with-creator nil "creator" org-export-with-creator)
118 (:with-drawers nil "d" org-export-with-drawers)
119 (:with-email nil "email" org-export-with-email)
120 (:with-emphasize nil "*" org-export-with-emphasize)
121 (:with-entities nil "e" org-export-with-entities)
122 (:with-fixed-width nil ":" org-export-with-fixed-width)
123 (:with-footnotes nil "f" org-export-with-footnotes)
124 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
125 (:with-plannings nil "p" org-export-with-planning)
126 (:with-priority nil "pri" org-export-with-priority)
127 (:with-smart-quotes nil "'" org-export-with-smart-quotes)
128 (:with-special-strings nil "-" org-export-with-special-strings)
129 (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
130 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
131 (:with-toc nil "toc" org-export-with-toc)
132 (:with-tables nil "|" org-export-with-tables)
133 (:with-tags nil "tags" org-export-with-tags)
134 (:with-tasks nil "tasks" org-export-with-tasks)
135 (:with-timestamps nil "<" org-export-with-timestamps)
136 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
137 "Alist between export properties and ways to set them.
139 The CAR of the alist is the property name, and the CDR is a list
140 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
142 KEYWORD is a string representing a buffer keyword, or nil. Each
143 property defined this way can also be set, during subtree
144 export, through an headline property named after the keyword
145 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
146 property).
147 OPTION is a string that could be found in an #+OPTIONS: line.
148 DEFAULT is the default value for the property.
149 BEHAVIOUR determine how Org should handle multiple keywords for
150 the same property. It is a symbol among:
151 nil Keep old value and discard the new one.
152 t Replace old value with the new one.
153 `space' Concatenate the values, separating them with a space.
154 `newline' Concatenate the values, separating them with
155 a newline.
156 `split' Split values at white spaces, and cons them to the
157 previous list.
159 KEYWORD and OPTION have precedence over DEFAULT.
161 All these properties should be back-end agnostic. Back-end
162 specific properties are set through `org-export-define-backend'.
163 Properties redefined there have precedence over these.")
165 (defconst org-export-special-keywords '("SETUP_FILE" "OPTIONS")
166 "List of in-buffer keywords that require special treatment.
167 These keywords are not directly associated to a property. The
168 way they are handled must be hard-coded into
169 `org-export--get-inbuffer-options' function.")
171 (defconst org-export-filters-alist
172 '((:filter-bold . org-export-filter-bold-functions)
173 (:filter-babel-call . org-export-filter-babel-call-functions)
174 (:filter-center-block . org-export-filter-center-block-functions)
175 (:filter-clock . org-export-filter-clock-functions)
176 (:filter-code . org-export-filter-code-functions)
177 (:filter-comment . org-export-filter-comment-functions)
178 (:filter-comment-block . org-export-filter-comment-block-functions)
179 (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
180 (:filter-drawer . org-export-filter-drawer-functions)
181 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
182 (:filter-entity . org-export-filter-entity-functions)
183 (:filter-example-block . org-export-filter-example-block-functions)
184 (:filter-export-block . org-export-filter-export-block-functions)
185 (:filter-export-snippet . org-export-filter-export-snippet-functions)
186 (:filter-final-output . org-export-filter-final-output-functions)
187 (:filter-fixed-width . org-export-filter-fixed-width-functions)
188 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
189 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
190 (:filter-headline . org-export-filter-headline-functions)
191 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
192 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
193 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
194 (:filter-inlinetask . org-export-filter-inlinetask-functions)
195 (:filter-italic . org-export-filter-italic-functions)
196 (:filter-item . org-export-filter-item-functions)
197 (:filter-keyword . org-export-filter-keyword-functions)
198 (:filter-latex-environment . org-export-filter-latex-environment-functions)
199 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
200 (:filter-line-break . org-export-filter-line-break-functions)
201 (:filter-link . org-export-filter-link-functions)
202 (:filter-macro . org-export-filter-macro-functions)
203 (:filter-node-property . org-export-filter-node-property-functions)
204 (:filter-paragraph . org-export-filter-paragraph-functions)
205 (:filter-parse-tree . org-export-filter-parse-tree-functions)
206 (:filter-plain-list . org-export-filter-plain-list-functions)
207 (:filter-plain-text . org-export-filter-plain-text-functions)
208 (:filter-planning . org-export-filter-planning-functions)
209 (:filter-property-drawer . org-export-filter-property-drawer-functions)
210 (:filter-quote-block . org-export-filter-quote-block-functions)
211 (:filter-quote-section . org-export-filter-quote-section-functions)
212 (:filter-radio-target . org-export-filter-radio-target-functions)
213 (:filter-section . org-export-filter-section-functions)
214 (:filter-special-block . org-export-filter-special-block-functions)
215 (:filter-src-block . org-export-filter-src-block-functions)
216 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
217 (:filter-strike-through . org-export-filter-strike-through-functions)
218 (:filter-subscript . org-export-filter-subscript-functions)
219 (:filter-superscript . org-export-filter-superscript-functions)
220 (:filter-table . org-export-filter-table-functions)
221 (:filter-table-cell . org-export-filter-table-cell-functions)
222 (:filter-table-row . org-export-filter-table-row-functions)
223 (:filter-target . org-export-filter-target-functions)
224 (:filter-timestamp . org-export-filter-timestamp-functions)
225 (:filter-underline . org-export-filter-underline-functions)
226 (:filter-verbatim . org-export-filter-verbatim-functions)
227 (:filter-verse-block . org-export-filter-verse-block-functions))
228 "Alist between filters properties and initial values.
230 The key of each association is a property name accessible through
231 the communication channel. Its value is a configurable global
232 variable defining initial filters.
234 This list is meant to install user specified filters. Back-end
235 developers may install their own filters using
236 `org-export-define-backend'. Filters defined there will always
237 be prepended to the current list, so they always get applied
238 first.")
240 (defconst org-export-default-inline-image-rule
241 `(("file" .
242 ,(format "\\.%s\\'"
243 (regexp-opt
244 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
245 "xpm" "pbm" "pgm" "ppm") t))))
246 "Default rule for link matching an inline image.
247 This rule applies to links with no description. By default, it
248 will be considered as an inline image if it targets a local file
249 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
250 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
251 See `org-export-inline-image-p' for more information about
252 rules.")
256 ;;; User-configurable Variables
258 ;; Configuration for the masses.
260 ;; They should never be accessed directly, as their value is to be
261 ;; stored in a property list (cf. `org-export-options-alist').
262 ;; Back-ends will read their value from there instead.
264 (defgroup org-export nil
265 "Options for exporting Org mode files."
266 :tag "Org Export"
267 :group 'org)
269 (defgroup org-export-general nil
270 "General options for export engine."
271 :tag "Org Export General"
272 :group 'org-export)
274 (defcustom org-export-with-archived-trees 'headline
275 "Whether sub-trees with the ARCHIVE tag should be exported.
277 This can have three different values:
278 nil Do not export, pretend this tree is not present.
279 t Do export the entire tree.
280 `headline' Only export the headline, but skip the tree below it.
282 This option can also be set with the #+OPTIONS line,
283 e.g. \"arch:nil\"."
284 :group 'org-export-general
285 :type '(choice
286 (const :tag "Not at all" nil)
287 (const :tag "Headline only" 'headline)
288 (const :tag "Entirely" t)))
290 (defcustom org-export-with-author t
291 "Non-nil means insert author name into the exported file.
292 This option can also be set with the #+OPTIONS line,
293 e.g. \"author:nil\"."
294 :group 'org-export-general
295 :type 'boolean)
297 (defcustom org-export-with-clocks nil
298 "Non-nil means export CLOCK keywords.
299 This option can also be set with the #+OPTIONS line,
300 e.g. \"c:t\"."
301 :group 'org-export-general
302 :type 'boolean)
304 (defcustom org-export-with-creator 'comment
305 "Non-nil means the postamble should contain a creator sentence.
307 The sentence can be set in `org-export-creator-string' and
308 defaults to \"Generated by Org mode XX in Emacs XXX.\".
310 If the value is `comment' insert it as a comment."
311 :group 'org-export-general
312 :type '(choice
313 (const :tag "No creator sentence" nil)
314 (const :tag "Sentence as a comment" 'comment)
315 (const :tag "Insert the sentence" t)))
317 (defcustom org-export-creator-string
318 (format "Generated by Org mode %s in Emacs %s."
319 (if (fboundp 'org-version) (org-version) "(Unknown)")
320 emacs-version)
321 "String to insert at the end of the generated document."
322 :group 'org-export-general
323 :type '(string :tag "Creator string"))
325 (defcustom org-export-with-drawers t
326 "Non-nil means export contents of standard drawers.
328 When t, all drawers are exported. This may also be a list of
329 drawer names to export. This variable doesn't apply to
330 properties drawers.
332 This option can also be set with the #+OPTIONS line,
333 e.g. \"d:nil\"."
334 :group 'org-export-general
335 :type '(choice
336 (const :tag "All drawers" t)
337 (const :tag "None" nil)
338 (repeat :tag "Selected drawers"
339 (string :tag "Drawer name"))))
341 (defcustom org-export-with-email nil
342 "Non-nil means insert author email into the exported file.
343 This option can also be set with the #+OPTIONS line,
344 e.g. \"email:t\"."
345 :group 'org-export-general
346 :type 'boolean)
348 (defcustom org-export-with-emphasize t
349 "Non-nil means interpret *word*, /word/, and _word_ as emphasized text.
351 If the export target supports emphasizing text, the word will be
352 typeset in bold, italic, or underlined, respectively. Not all
353 export backends support this.
355 This option can also be set with the #+OPTIONS line, e.g. \"*:nil\"."
356 :group 'org-export-general
357 :type 'boolean)
359 (defcustom org-export-exclude-tags '("noexport")
360 "Tags that exclude a tree from export.
362 All trees carrying any of these tags will be excluded from
363 export. This is without condition, so even subtrees inside that
364 carry one of the `org-export-select-tags' will be removed.
366 This option can also be set with the #+EXCLUDE_TAGS: keyword."
367 :group 'org-export-general
368 :type '(repeat (string :tag "Tag")))
370 (defcustom org-export-with-fixed-width t
371 "Non-nil means lines starting with \":\" will be in fixed width font.
373 This can be used to have pre-formatted text, fragments of code
374 etc. For example:
375 : ;; Some Lisp examples
376 : (while (defc cnt)
377 : (ding))
378 will be looking just like this in also HTML. See also the QUOTE
379 keyword. Not all export backends support this.
381 This option can also be set with the #+OPTIONS line, e.g. \"::nil\"."
382 :group 'org-export-translation
383 :type 'boolean)
385 (defcustom org-export-with-footnotes t
386 "Non-nil means Org footnotes should be exported.
387 This option can also be set with the #+OPTIONS line,
388 e.g. \"f:nil\"."
389 :group 'org-export-general
390 :type 'boolean)
392 (defcustom org-export-headline-levels 3
393 "The last level which is still exported as a headline.
395 Inferior levels will produce itemize lists when exported.
397 This option can also be set with the #+OPTIONS line, e.g. \"H:2\"."
398 :group 'org-export-general
399 :type 'integer)
401 (defcustom org-export-default-language "en"
402 "The default language for export and clocktable translations, as a string.
403 This may have an association in
404 `org-clock-clocktable-language-setup'."
405 :group 'org-export-general
406 :type '(string :tag "Language"))
408 (defcustom org-export-preserve-breaks nil
409 "Non-nil means preserve all line breaks when exporting.
411 Normally, in HTML output paragraphs will be reformatted.
413 This option can also be set with the #+OPTIONS line,
414 e.g. \"\\n:t\"."
415 :group 'org-export-general
416 :type 'boolean)
418 (defcustom org-export-with-entities t
419 "Non-nil means interpret entities when exporting.
421 For example, HTML export converts \\alpha to &alpha; and \\AA to
422 &Aring;.
424 For a list of supported names, see the constant `org-entities'
425 and the user option `org-entities-user'.
427 This option can also be set with the #+OPTIONS line,
428 e.g. \"e:nil\"."
429 :group 'org-export-general
430 :type 'boolean)
432 (defcustom org-export-with-inlinetasks t
433 "Non-nil means inlinetasks should be exported.
434 This option can also be set with the #+OPTIONS line,
435 e.g. \"inline:nil\"."
436 :group 'org-export-general
437 :type 'boolean)
439 (defcustom org-export-with-planning nil
440 "Non-nil means include planning info in export.
441 This option can also be set with the #+OPTIONS: line,
442 e.g. \"p:t\"."
443 :group 'org-export-general
444 :type 'boolean)
446 (defcustom org-export-with-priority nil
447 "Non-nil means include priority cookies in export.
448 This option can also be set with the #+OPTIONS line,
449 e.g. \"pri:t\"."
450 :group 'org-export-general
451 :type 'boolean)
453 (defcustom org-export-with-section-numbers t
454 "Non-nil means add section numbers to headlines when exporting.
456 When set to an integer n, numbering will only happen for
457 headlines whose relative level is higher or equal to n.
459 This option can also be set with the #+OPTIONS line,
460 e.g. \"num:t\"."
461 :group 'org-export-general
462 :type 'boolean)
464 (defcustom org-export-select-tags '("export")
465 "Tags that select a tree for export.
467 If any such tag is found in a buffer, all trees that do not carry
468 one of these tags will be ignored during export. Inside trees
469 that are selected like this, you can still deselect a subtree by
470 tagging it with one of the `org-export-exclude-tags'.
472 This option can also be set with the #+SELECT_TAGS: keyword."
473 :group 'org-export-general
474 :type '(repeat (string :tag "Tag")))
476 (defcustom org-export-with-smart-quotes nil
477 "Non-nil means activate smart quotes during export.
478 This option can also be set with the #+OPTIONS: line,
479 e.g. \"':t\"."
480 :group 'org-export-general
481 :type 'boolean)
483 (defcustom org-export-with-special-strings t
484 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
486 When this option is turned on, these strings will be exported as:
488 Org HTML LaTeX UTF-8
489 -----+----------+--------+-------
490 \\- &shy; \\-
491 -- &ndash; -- –
492 --- &mdash; --- —
493 ... &hellip; \\ldots …
495 This option can also be set with the #+OPTIONS line,
496 e.g. \"-:nil\"."
497 :group 'org-export-general
498 :type 'boolean)
500 (defcustom org-export-with-statistics-cookies t
501 "Non-nil means include statistics cookies in export.
502 This option can also be set with the #+OPTIONS: line,
503 e.g. \"stat:nil\""
504 :group 'org-export-general
505 :type 'boolean)
507 (defcustom org-export-with-sub-superscripts t
508 "Non-nil means interpret \"_\" and \"^\" for export.
510 When this option is turned on, you can use TeX-like syntax for
511 sub- and superscripts. Several characters after \"_\" or \"^\"
512 will be considered as a single item - so grouping with {} is
513 normally not needed. For example, the following things will be
514 parsed as single sub- or superscripts.
516 10^24 or 10^tau several digits will be considered 1 item.
517 10^-12 or 10^-tau a leading sign with digits or a word
518 x^2-y^3 will be read as x^2 - y^3, because items are
519 terminated by almost any nonword/nondigit char.
520 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
522 Still, ambiguity is possible - so when in doubt use {} to enclose
523 the sub/superscript. If you set this variable to the symbol
524 `{}', the braces are *required* in order to trigger
525 interpretations as sub/superscript. This can be helpful in
526 documents that need \"_\" frequently in plain text.
528 This option can also be set with the #+OPTIONS line,
529 e.g. \"^:nil\"."
530 :group 'org-export-general
531 :type '(choice
532 (const :tag "Interpret them" t)
533 (const :tag "Curly brackets only" {})
534 (const :tag "Do not interpret them" nil)))
536 (defcustom org-export-with-toc t
537 "Non-nil means create a table of contents in exported files.
539 The TOC contains headlines with levels up
540 to`org-export-headline-levels'. When an integer, include levels
541 up to N in the toc, this may then be different from
542 `org-export-headline-levels', but it will not be allowed to be
543 larger than the number of headline levels. When nil, no table of
544 contents is made.
546 This option can also be set with the #+OPTIONS line,
547 e.g. \"toc:nil\" or \"toc:3\"."
548 :group 'org-export-general
549 :type '(choice
550 (const :tag "No Table of Contents" nil)
551 (const :tag "Full Table of Contents" t)
552 (integer :tag "TOC to level")))
554 (defcustom org-export-with-tables t
555 "If non-nil, lines starting with \"|\" define a table.
556 For example:
558 | Name | Address | Birthday |
559 |-------------+----------+-----------|
560 | Arthur Dent | England | 29.2.2100 |
562 This option can also be set with the #+OPTIONS line, e.g. \"|:nil\"."
563 :group 'org-export-general
564 :type 'boolean)
566 (defcustom org-export-with-tags t
567 "If nil, do not export tags, just remove them from headlines.
569 If this is the symbol `not-in-toc', tags will be removed from
570 table of contents entries, but still be shown in the headlines of
571 the document.
573 This option can also be set with the #+OPTIONS line,
574 e.g. \"tags:nil\"."
575 :group 'org-export-general
576 :type '(choice
577 (const :tag "Off" nil)
578 (const :tag "Not in TOC" not-in-toc)
579 (const :tag "On" t)))
581 (defcustom org-export-with-tasks t
582 "Non-nil means include TODO items for export.
583 This may have the following values:
584 t include tasks independent of state.
585 todo include only tasks that are not yet done.
586 done include only tasks that are already done.
587 nil remove all tasks before export
588 list of keywords keep only tasks with these keywords"
589 :group 'org-export-general
590 :type '(choice
591 (const :tag "All tasks" t)
592 (const :tag "No tasks" nil)
593 (const :tag "Not-done tasks" todo)
594 (const :tag "Only done tasks" done)
595 (repeat :tag "Specific TODO keywords"
596 (string :tag "Keyword"))))
598 (defcustom org-export-time-stamp-file t
599 "Non-nil means insert a time stamp into the exported file.
600 The time stamp shows when the file was created.
602 This option can also be set with the #+OPTIONS line,
603 e.g. \"timestamp:nil\"."
604 :group 'org-export-general
605 :type 'boolean)
607 (defcustom org-export-with-timestamps t
608 "Non nil means allow timestamps in export.
610 It can be set to `active', `inactive', t or nil, in order to
611 export, respectively, only active timestamps, only inactive ones,
612 all of them or none.
614 This option can also be set with the #+OPTIONS line, e.g.
615 \"<:nil\"."
616 :group 'org-export-general
617 :type '(choice
618 (const :tag "All timestamps" t)
619 (const :tag "Only active timestamps" active)
620 (const :tag "Only inactive timestamps" inactive)
621 (const :tag "No timestamp" nil)))
623 (defcustom org-export-with-todo-keywords t
624 "Non-nil means include TODO keywords in export.
625 When nil, remove all these keywords from the export."
626 :group 'org-export-general
627 :type 'boolean)
629 (defcustom org-export-allow-BIND 'confirm
630 "Non-nil means allow #+BIND to define local variable values for export.
631 This is a potential security risk, which is why the user must
632 confirm the use of these lines."
633 :group 'org-export-general
634 :type '(choice
635 (const :tag "Never" nil)
636 (const :tag "Always" t)
637 (const :tag "Ask a confirmation for each file" confirm)))
639 (defcustom org-export-snippet-translation-alist nil
640 "Alist between export snippets back-ends and exporter back-ends.
642 This variable allows to provide shortcuts for export snippets.
644 For example, with a value of '\(\(\"h\" . \"e-html\"\)\), the
645 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
646 HTML code while every other back-end will ignore it."
647 :group 'org-export-general
648 :type '(repeat
649 (cons
650 (string :tag "Shortcut")
651 (string :tag "Back-end"))))
653 (defcustom org-export-coding-system nil
654 "Coding system for the exported file."
655 :group 'org-export-general
656 :type 'coding-system)
658 (defcustom org-export-copy-to-kill-ring t
659 "Non-nil means exported stuff will also be pushed onto the kill ring."
660 :group 'org-export-general
661 :type 'boolean)
663 (defcustom org-export-initial-scope 'buffer
664 "The initial scope when exporting with `org-export-dispatch'.
665 This variable can be either set to `buffer' or `subtree'."
666 :group 'org-export-general
667 :type '(choice
668 (const :tag "Export current buffer" 'buffer)
669 (const :tag "Export current subtree" 'subtree)))
671 (defcustom org-export-show-temporary-export-buffer t
672 "Non-nil means show buffer after exporting to temp buffer.
673 When Org exports to a file, the buffer visiting that file is ever
674 shown, but remains buried. However, when exporting to
675 a temporary buffer, that buffer is popped up in a second window.
676 When this variable is nil, the buffer remains buried also in
677 these cases."
678 :group 'org-export-general
679 :type 'boolean)
681 (defcustom org-export-dispatch-use-expert-ui nil
682 "Non-nil means using a non-intrusive `org-export-dispatch'.
683 In that case, no help buffer is displayed. Though, an indicator
684 for current export scope is added to the prompt (\"b\" when
685 output is restricted to body only, \"s\" when it is restricted to
686 the current subtree, \"v\" when only visible elements are
687 considered for export and \"f\" when publishing functions should
688 be passed the FORCE argument). Also, \[?] allows to switch back
689 to standard mode."
690 :group 'org-export-general
691 :type 'boolean)
695 ;;; Defining New Back-ends
697 ;; `org-export-define-backend' is the standard way to define an export
698 ;; back-end. It allows to specify translators, filters, buffer
699 ;; options and a menu entry. If the new back-end shares translators
700 ;; with another back-end, `org-export-define-derived-backend' may be
701 ;; used instead.
703 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
704 ;; when a given back-end hasn't been registered yet.
706 (defmacro org-export-define-backend (backend translators &rest body)
707 "Define a new back-end BACKEND.
709 TRANSLATORS is an alist between object or element types and
710 functions handling them.
712 These functions should return a string without any trailing
713 space, or nil. They must accept three arguments: the object or
714 element itself, its contents or nil when it isn't recursive and
715 the property list used as a communication channel.
717 Contents, when not nil, are stripped from any global indentation
718 \(although the relative one is preserved). They also always end
719 with a single newline character.
721 If, for a given type, no function is found, that element or
722 object type will simply be ignored, along with any blank line or
723 white space at its end. The same will happen if the function
724 returns the nil value. If that function returns the empty
725 string, the type will be ignored, but the blank lines or white
726 spaces will be kept.
728 In addition to element and object types, one function can be
729 associated to the `template' symbol and another one to the
730 `plain-text' symbol.
732 The former returns the final transcoded string, and can be used
733 to add a preamble and a postamble to document's body. It must
734 accept two arguments: the transcoded string and the property list
735 containing export options.
737 The latter, when defined, is to be called on every text not
738 recognized as an element or an object. It must accept two
739 arguments: the text string and the information channel. It is an
740 appropriate place to protect special chars relative to the
741 back-end.
743 BODY can start with pre-defined keyword arguments. The following
744 keywords are understood:
746 :export-block
748 String, or list of strings, representing block names that
749 will not be parsed. This is used to specify blocks that will
750 contain raw code specific to the back-end. These blocks
751 still have to be handled by the relative `export-block' type
752 translator.
754 :filters-alist
756 Alist between filters and function, or list of functions,
757 specific to the back-end. See `org-export-filters-alist' for
758 a list of all allowed filters. Filters defined here
759 shouldn't make a back-end test, as it may prevent back-ends
760 derived from this one to behave properly.
762 :menu-entry
764 Menu entry for the export dispatcher. It should be a list
765 like:
767 \(KEY DESCRIPTION ACTION-OR-MENU)
769 where :
771 KEY is a free character selecting the back-end.
772 DESCRIPTION is a string naming the back-end.
773 ACTION-OR-MENU is either a function or an alist.
775 If it is an action, it will be called with three arguments:
776 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
777 for further explanations.
779 If it is an alist, associations should follow the
780 pattern:
782 \(KEY DESCRIPTION ACTION)
784 where KEY, DESCRIPTION and ACTION are described above.
786 Valid values include:
788 \(?m \"My Special Back-end\" my-special-export-function)
792 \(?l \"Export to LaTeX\"
793 \((?b \"TEX (buffer)\" org-e-latex-export-as-latex)
794 \(?l \"TEX (file)\" org-e-latex-export-to-latex)
795 \(?p \"PDF file\" org-e-latex-export-to-pdf)
796 \(?o \"PDF file and open\"
797 \(lambda (subtree visible body-only)
798 \(org-open-file
799 \(org-e-latex-export-to-pdf subtree visible body-only))))))
801 :options-alist
803 Alist between back-end specific properties introduced in
804 communication channel and how their value are acquired. See
805 `org-export-options-alist' for more information about
806 structure of the values."
807 (declare (debug (&define name sexp [&rest [keywordp sexp]] defbody))
808 (indent 1))
809 (let (export-block filters menu-entry options)
810 (while (keywordp (car body))
811 (case (pop body)
812 (:export-block (let ((names (pop body)))
813 (setq export-block
814 (if (consp names) (mapcar 'upcase names)
815 (list (upcase names))))))
816 (:filters-alist (setq filters (pop body)))
817 (:menu-entry (setq menu-entry (pop body)))
818 (:options-alist (setq options (pop body)))
819 (t (pop body))))
820 `(progn
821 ;; Define translators.
822 (defvar ,(intern (format "org-%s-translate-alist" backend)) ',translators
823 "Alist between element or object types and translators.")
824 ;; Define options.
825 ,(when options
826 `(defconst ,(intern (format "org-%s-options-alist" backend)) ',options
827 ,(format "Alist between %s export properties and ways to set them.
828 See `org-export-options-alist' for more information on the
829 structure of the values."
830 backend)))
831 ;; Define filters.
832 ,(when filters
833 `(defconst ,(intern (format "org-%s-filters-alist" backend)) ',filters
834 "Alist between filters keywords and back-end specific filters.
835 See `org-export-filters-alist' for more information."))
836 ;; Tell parser to not parse EXPORT-BLOCK blocks.
837 ,(when export-block
838 `(mapc
839 (lambda (name)
840 (add-to-list 'org-element-block-name-alist
841 `(,name . org-element-export-block-parser)))
842 ',export-block))
843 ;; Add an entry for back-end in `org-export-dispatch'.
844 ,(when menu-entry
845 `(unless (assq (car ',menu-entry) org-export-dispatch-menu-entries)
846 (add-to-list 'org-export-dispatch-menu-entries ',menu-entry)))
847 ;; Splice in the body, if any.
848 ,@body)))
850 (defmacro org-export-define-derived-backend (child parent &rest body)
851 "Create a new back-end as a variant of an existing one.
853 CHILD is the name of the derived back-end. PARENT is the name of
854 the parent back-end.
856 BODY can start with pre-defined keyword arguments. The following
857 keywords are understood:
859 :export-block
861 String, or list of strings, representing block names that
862 will not be parsed. This is used to specify blocks that will
863 contain raw code specific to the back-end. These blocks
864 still have to be handled by the relative `export-block' type
865 translator.
867 :filters-alist
869 Alist of filters that will overwrite or complete filters
870 defined in PARENT back-end. See `org-export-filters-alist'
871 for a list of allowed filters.
873 :menu-entry
875 Menu entry for the export dispatcher. See
876 `org-export-define-backend' for more information about the
877 expected value.
879 :options-alist
881 Alist of back-end specific properties that will overwrite or
882 complete those defined in PARENT back-end. Refer to
883 `org-export-options-alist' for more information about
884 structure of the values.
886 :sub-menu-entry
888 Append entries to an existing menu in the export dispatcher.
889 The associated value should be a list whose CAR is the
890 character selecting the menu to expand and CDR a list of
891 entries following the pattern:
893 \(KEY DESCRIPTION ACTION)
895 where KEY is a free character triggering the action,
896 DESCRIPTION is a string defining the action, and ACTION is
897 a function that will be called with three arguments:
898 SUBTREEP, VISIBLE-ONLY and BODY-ONLY. See `org-export-as'
899 for further explanations.
901 Valid values include:
903 \(?l (?P \"As PDF file (Beamer)\" org-e-beamer-export-to-pdf)
904 \(?O \"As PDF file and open (Beamer)\"
905 \(lambda (s v b)
906 \(org-open-file (org-e-beamer-export-to-pdf s v b)))))
908 :translate-alist
910 Alist of element and object types and transcoders that will
911 overwrite or complete transcode table from PARENT back-end.
912 Refer to `org-export-define-backend' for detailed information
913 about transcoders.
915 As an example, here is how one could define \"my-latex\" back-end
916 as a variant of `e-latex' back-end with a custom template
917 function:
919 \(org-export-define-derived-backend my-latex e-latex
920 :translate-alist ((template . my-latex-template-fun)))
922 The back-end could then be called with, for example:
924 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
925 (declare (debug (&define name sexp [&rest [keywordp sexp]] def-body))
926 (indent 2))
927 (let (export-block filters menu-entry options sub-menu-entry translate)
928 (while (keywordp (car body))
929 (case (pop body)
930 (:export-block (let ((names (pop body)))
931 (setq export-block
932 (if (consp names) (mapcar 'upcase names)
933 (list (upcase names))))))
934 (:filters-alist (setq filters (pop body)))
935 (:menu-entry (setq menu-entry (pop body)))
936 (:options-alist (setq options (pop body)))
937 (:sub-menu-entry (setq sub-menu-entry (pop body)))
938 (:translate-alist (setq translate (pop body)))
939 (t (pop body))))
940 `(progn
941 ;; Tell parser to not parse EXPORT-BLOCK blocks.
942 ,(when export-block
943 `(mapc
944 (lambda (name)
945 (add-to-list 'org-element-block-name-alist
946 `(,name . org-element-export-block-parser)))
947 ',export-block))
948 ;; Define filters.
949 ,(let ((parent-filters (intern (format "org-%s-filters-alist" parent))))
950 (when (or (boundp parent-filters) filters)
951 `(defconst ,(intern (format "org-%s-filters-alist" child))
952 ',(append filters
953 (and (boundp parent-filters)
954 (copy-sequence (symbol-value parent-filters))))
955 "Alist between filters keywords and back-end specific filters.
956 See `org-export-filters-alist' for more information.")))
957 ;; Define options.
958 ,(let ((parent-options (intern (format "org-%s-options-alist" parent))))
959 (when (or (boundp parent-options) options)
960 `(defconst ,(intern (format "org-%s-options-alist" child))
961 ',(append options
962 (and (boundp parent-options)
963 (copy-sequence (symbol-value parent-options))))
964 ,(format "Alist between %s export properties and ways to set them.
965 See `org-export-options-alist' for more information on the
966 structure of the values."
967 child))))
968 ;; Define translators.
969 (defvar ,(intern (format "org-%s-translate-alist" child))
970 ',(append translate
971 (copy-sequence
972 (symbol-value
973 (intern (format "org-%s-translate-alist" parent)))))
974 "Alist between element or object types and translators.")
975 ;; Add an entry for back-end in `org-export-dispatch'.
976 ,(when menu-entry
977 `(unless (assq (car ',menu-entry) org-export-dispatch-menu-entries)
978 (add-to-list 'org-export-dispatch-menu-entries ',menu-entry)))
979 ,(when sub-menu-entry
980 (let ((menu (nth 2 (assq (car sub-menu-entry)
981 org-export-dispatch-menu-entries))))
982 (when menu `(nconc ',menu
983 ',(org-remove-if (lambda (e) (member e menu))
984 (cdr sub-menu-entry))))))
985 ;; Splice in the body, if any.
986 ,@body)))
988 (defun org-export-barf-if-invalid-backend (backend)
989 "Signal an error if BACKEND isn't defined."
990 (unless (boundp (intern (format "org-%s-translate-alist" backend)))
991 (error "Unknown \"%s\" back-end: Aborting export" backend)))
995 ;;; The Communication Channel
997 ;; During export process, every function has access to a number of
998 ;; properties. They are of two types:
1000 ;; 1. Environment options are collected once at the very beginning of
1001 ;; the process, out of the original buffer and configuration.
1002 ;; Collecting them is handled by `org-export-get-environment'
1003 ;; function.
1005 ;; Most environment options are defined through the
1006 ;; `org-export-options-alist' variable.
1008 ;; 2. Tree properties are extracted directly from the parsed tree,
1009 ;; just before export, by `org-export-collect-tree-properties'.
1011 ;; Here is the full list of properties available during transcode
1012 ;; process, with their category and their value type.
1014 ;; + `:author' :: Author's name.
1015 ;; - category :: option
1016 ;; - type :: string
1018 ;; + `:back-end' :: Current back-end used for transcoding.
1019 ;; - category :: tree
1020 ;; - type :: symbol
1022 ;; + `:creator' :: String to write as creation information.
1023 ;; - category :: option
1024 ;; - type :: string
1026 ;; + `:date' :: String to use as date.
1027 ;; - category :: option
1028 ;; - type :: string
1030 ;; + `:description' :: Description text for the current data.
1031 ;; - category :: option
1032 ;; - type :: string
1034 ;; + `:email' :: Author's email.
1035 ;; - category :: option
1036 ;; - type :: string
1038 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1039 ;; process.
1040 ;; - category :: option
1041 ;; - type :: list of strings
1043 ;; + `:exported-data' :: Hash table used for memoizing
1044 ;; `org-export-data'.
1045 ;; - category :: tree
1046 ;; - type :: hash table
1048 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1049 ;; their definition, as parsed data. Only non-inlined footnotes
1050 ;; are represented in this alist. Also, every definition isn't
1051 ;; guaranteed to be referenced in the parse tree. The purpose of
1052 ;; this property is to preserve definitions from oblivion
1053 ;; (i.e. when the parse tree comes from a part of the original
1054 ;; buffer), it isn't meant for direct use in a back-end. To
1055 ;; retrieve a definition relative to a reference, use
1056 ;; `org-export-get-footnote-definition' instead.
1057 ;; - category :: option
1058 ;; - type :: alist (STRING . LIST)
1060 ;; + `:headline-levels' :: Maximum level being exported as an
1061 ;; headline. Comparison is done with the relative level of
1062 ;; headlines in the parse tree, not necessarily with their
1063 ;; actual level.
1064 ;; - category :: option
1065 ;; - type :: integer
1067 ;; + `:headline-offset' :: Difference between relative and real level
1068 ;; of headlines in the parse tree. For example, a value of -1
1069 ;; means a level 2 headline should be considered as level
1070 ;; 1 (cf. `org-export-get-relative-level').
1071 ;; - category :: tree
1072 ;; - type :: integer
1074 ;; + `:headline-numbering' :: Alist between headlines and their
1075 ;; numbering, as a list of numbers
1076 ;; (cf. `org-export-get-headline-number').
1077 ;; - category :: tree
1078 ;; - type :: alist (INTEGER . LIST)
1080 ;; + `:id-alist' :: Alist between ID strings and destination file's
1081 ;; path, relative to current directory. It is used by
1082 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1083 ;; external file.
1084 ;; - category :: option
1085 ;; - type :: alist (STRING . STRING)
1087 ;; + `:ignore-list' :: List of elements and objects that should be
1088 ;; ignored during export.
1089 ;; - category :: tree
1090 ;; - type :: list of elements and objects
1092 ;; + `:input-file' :: Full path to input file, if any.
1093 ;; - category :: option
1094 ;; - type :: string or nil
1096 ;; + `:keywords' :: List of keywords attached to data.
1097 ;; - category :: option
1098 ;; - type :: string
1100 ;; + `:language' :: Default language used for translations.
1101 ;; - category :: option
1102 ;; - type :: string
1104 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1105 ;; transcoding.
1106 ;; - category :: option
1107 ;; - type :: list (as returned by `org-element-parse-buffer')
1109 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1110 ;; all line breaks.
1111 ;; - category :: option
1112 ;; - type :: symbol (nil, t)
1114 ;; + `:section-numbers' :: Non-nil means transcoding should add
1115 ;; section numbers to headlines.
1116 ;; - category :: option
1117 ;; - type :: symbol (nil, t)
1119 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1120 ;; in transcoding. When such a tag is present, subtrees without
1121 ;; it are de facto excluded from the process. See
1122 ;; `use-select-tags'.
1123 ;; - category :: option
1124 ;; - type :: list of strings
1126 ;; + `:target-list' :: List of targets encountered in the parse tree.
1127 ;; This is used to partly resolve "fuzzy" links
1128 ;; (cf. `org-export-resolve-fuzzy-link').
1129 ;; - category :: tree
1130 ;; - type :: list of strings
1132 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1133 ;; a time stamp in the output.
1134 ;; - category :: option
1135 ;; - type :: symbol (nil, t)
1137 ;; + `:translate-alist' :: Alist between element and object types and
1138 ;; transcoding functions relative to the current back-end.
1139 ;; Special keys `template' and `plain-text' are also possible.
1140 ;; - category :: option
1141 ;; - type :: alist (SYMBOL . FUNCTION)
1143 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1144 ;; also be transcoded. If it is set to the `headline' symbol,
1145 ;; only the archived headline's name is retained.
1146 ;; - category :: option
1147 ;; - type :: symbol (nil, t, `headline')
1149 ;; + `:with-author' :: Non-nil means author's name should be included
1150 ;; in the output.
1151 ;; - category :: option
1152 ;; - type :: symbol (nil, t)
1154 ;; + `:with-clocks' :: Non-nild means clock keywords should be exported.
1155 ;; - category :: option
1156 ;; - type :: symbol (nil, t)
1158 ;; + `:with-creator' :: Non-nild means a creation sentence should be
1159 ;; inserted at the end of the transcoded string. If the value
1160 ;; is `comment', it should be commented.
1161 ;; - category :: option
1162 ;; - type :: symbol (`comment', nil, t)
1164 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1165 ;; its value is a list of names, only drawers with such names
1166 ;; will be transcoded.
1167 ;; - category :: option
1168 ;; - type :: symbol (nil, t) or list of strings
1170 ;; + `:with-email' :: Non-nil means output should contain author's
1171 ;; email.
1172 ;; - category :: option
1173 ;; - type :: symbol (nil, t)
1175 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1176 ;; interpreted.
1177 ;; - category :: option
1178 ;; - type :: symbol (nil, t)
1180 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1181 ;; strings starting with a colon as a fixed-with (verbatim) area.
1182 ;; - category :: option
1183 ;; - type :: symbol (nil, t)
1185 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1186 ;; footnotes.
1187 ;; - category :: option
1188 ;; - type :: symbol (nil, t)
1190 ;; + `:with-plannings' :: Non-nil means transcoding should include
1191 ;; planning info.
1192 ;; - category :: option
1193 ;; - type :: symbol (nil, t)
1195 ;; + `:with-priority' :: Non-nil means transcoding should include
1196 ;; priority cookies.
1197 ;; - category :: option
1198 ;; - type :: symbol (nil, t)
1200 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1201 ;; plain text.
1202 ;; - category :: option
1203 ;; - type :: symbol (nil, t)
1205 ;; + `:with-special-strings' :: Non-nil means transcoding should
1206 ;; interpret special strings in plain text.
1207 ;; - category :: option
1208 ;; - type :: symbol (nil, t)
1210 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1211 ;; interpret subscript and superscript. With a value of "{}",
1212 ;; only interpret those using curly brackets.
1213 ;; - category :: option
1214 ;; - type :: symbol (nil, {}, t)
1216 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1217 ;; tables.
1218 ;; - category :: option
1219 ;; - type :: symbol (nil, t)
1221 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1222 ;; headlines. A `not-in-toc' value will remove them from the
1223 ;; table of contents, if any, nonetheless.
1224 ;; - category :: option
1225 ;; - type :: symbol (nil, t, `not-in-toc')
1227 ;; + `:with-tasks' :: Non-nil means transcoding should include
1228 ;; headlines with a TODO keyword. A `todo' value will only
1229 ;; include headlines with a todo type keyword while a `done'
1230 ;; value will do the contrary. If a list of strings is provided,
1231 ;; only tasks with keywords belonging to that list will be kept.
1232 ;; - category :: option
1233 ;; - type :: symbol (t, todo, done, nil) or list of strings
1235 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1236 ;; time stamps. Special value `active' (resp. `inactive') ask to
1237 ;; export only active (resp. inactive) timestamps. Otherwise,
1238 ;; completely remove them.
1239 ;; - category :: option
1240 ;; - type :: symbol: (`active', `inactive', t, nil)
1242 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1243 ;; added to the output. An integer value limits its depth.
1244 ;; - category :: option
1245 ;; - type :: symbol (nil, t or integer)
1247 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1248 ;; include TODO keywords.
1249 ;; - category :: option
1250 ;; - type :: symbol (nil, t)
1253 ;;;; Environment Options
1255 ;; Environment options encompass all parameters defined outside the
1256 ;; scope of the parsed data. They come from five sources, in
1257 ;; increasing precedence order:
1259 ;; - Global variables,
1260 ;; - Buffer's attributes,
1261 ;; - Options keyword symbols,
1262 ;; - Buffer keywords,
1263 ;; - Subtree properties.
1265 ;; The central internal function with regards to environment options
1266 ;; is `org-export-get-environment'. It updates global variables with
1267 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1268 ;; the different sources.
1270 ;; The internal functions doing the retrieval are:
1271 ;; `org-export--get-global-options',
1272 ;; `org-export--get-buffer-attributes',
1273 ;; `org-export--parse-option-keyword',
1274 ;; `org-export--get-subtree-options' and
1275 ;; `org-export--get-inbuffer-options'
1277 ;; Also, `org-export--confirm-letbind' and `org-export--install-letbind'
1278 ;; take care of the part relative to "#+BIND:" keywords.
1280 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1281 "Collect export options from the current buffer.
1283 Optional argument BACKEND is a symbol specifying which back-end
1284 specific options to read, if any.
1286 When optional argument SUBTREEP is non-nil, assume the export is
1287 done against the current sub-tree.
1289 Third optional argument EXT-PLIST is a property list with
1290 external parameters overriding Org default settings, but still
1291 inferior to file-local settings."
1292 ;; First install #+BIND variables.
1293 (org-export--install-letbind-maybe)
1294 ;; Get and prioritize export options...
1295 (org-combine-plists
1296 ;; ... from global variables...
1297 (org-export--get-global-options backend)
1298 ;; ... from buffer's attributes...
1299 (org-export--get-buffer-attributes)
1300 ;; ... from an external property list...
1301 ext-plist
1302 ;; ... from in-buffer settings...
1303 (org-export--get-inbuffer-options
1304 backend
1305 (and buffer-file-name (org-remove-double-quotes buffer-file-name)))
1306 ;; ... and from subtree, when appropriate.
1307 (and subtreep (org-export--get-subtree-options backend))
1308 ;; Eventually install back-end symbol and its translation table.
1309 `(:back-end
1310 ,backend
1311 :translate-alist
1312 ,(let ((trans-alist (intern (format "org-%s-translate-alist" backend))))
1313 (when (boundp trans-alist) (symbol-value trans-alist))))))
1315 (defun org-export--parse-option-keyword (options &optional backend)
1316 "Parse an OPTIONS line and return values as a plist.
1317 Optional argument BACKEND is a symbol specifying which back-end
1318 specific items to read, if any."
1319 (let* ((all
1320 (append org-export-options-alist
1321 (and backend
1322 (let ((var (intern
1323 (format "org-%s-options-alist" backend))))
1324 (and (boundp var) (eval var))))))
1325 ;; Build an alist between #+OPTION: item and property-name.
1326 (alist (delq nil
1327 (mapcar (lambda (e)
1328 (when (nth 2 e) (cons (regexp-quote (nth 2 e))
1329 (car e))))
1330 all)))
1331 plist)
1332 (mapc (lambda (e)
1333 (when (string-match (concat "\\(\\`\\|[ \t]\\)"
1334 (car e)
1335 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1336 options)
1337 (setq plist (plist-put plist
1338 (cdr e)
1339 (car (read-from-string
1340 (match-string 2 options)))))))
1341 alist)
1342 plist))
1344 (defun org-export--get-subtree-options (&optional backend)
1345 "Get export options in subtree at point.
1346 Optional argument BACKEND is a symbol specifying back-end used
1347 for export. Return options as a plist."
1348 ;; For each buffer keyword, create an headline property setting the
1349 ;; same property in communication channel. The name for the property
1350 ;; is the keyword with "EXPORT_" appended to it.
1351 (org-with-wide-buffer
1352 (let (prop plist)
1353 ;; Make sure point is at an heading.
1354 (unless (org-at-heading-p) (org-back-to-heading t))
1355 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1356 ;; title as its fallback value.
1357 (when (setq prop (progn (looking-at org-todo-line-regexp)
1358 (or (save-match-data
1359 (org-entry-get (point) "EXPORT_TITLE"))
1360 (org-match-string-no-properties 3))))
1361 (setq plist
1362 (plist-put
1363 plist :title
1364 (org-element-parse-secondary-string
1365 prop (org-element-restriction 'keyword)))))
1366 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1367 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1368 (setq plist
1369 (nconc plist (org-export--parse-option-keyword prop backend))))
1370 ;; Handle other keywords. TITLE keyword is excluded as it has
1371 ;; already been handled already.
1372 (let ((seen '("TITLE")))
1373 (mapc
1374 (lambda (option)
1375 (let ((property (nth 1 option)))
1376 (when (and property (not (member property seen)))
1377 (let* ((subtree-prop (concat "EXPORT_" property))
1378 ;; Export properties are not case-sensitive.
1379 (value (let ((case-fold-search t))
1380 (org-entry-get (point) subtree-prop))))
1381 (push property seen)
1382 (when value
1383 (setq plist
1384 (plist-put
1385 plist
1386 (car option)
1387 ;; Parse VALUE if required.
1388 (if (member property org-element-document-properties)
1389 (org-element-parse-secondary-string
1390 value (org-element-restriction 'keyword))
1391 value))))))))
1392 ;; Also look for both general keywords and back-end specific
1393 ;; options if BACKEND is provided.
1394 (append (and backend
1395 (let ((var (intern
1396 (format "org-%s-options-alist" backend))))
1397 (and (boundp var) (symbol-value var))))
1398 org-export-options-alist)))
1399 ;; Return value.
1400 plist)))
1402 (defun org-export--get-inbuffer-options (&optional backend files)
1403 "Return current buffer export options, as a plist.
1405 Optional argument BACKEND, when non-nil, is a symbol specifying
1406 which back-end specific options should also be read in the
1407 process.
1409 Optional argument FILES is a list of setup files names read so
1410 far, used to avoid circular dependencies.
1412 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1413 (org-with-wide-buffer
1414 (goto-char (point-min))
1415 (let ((case-fold-search t) plist)
1416 ;; 1. Special keywords, as in `org-export-special-keywords'.
1417 (let ((special-re
1418 (format "^[ \t]*#\\+%s:" (regexp-opt org-export-special-keywords))))
1419 (while (re-search-forward special-re nil t)
1420 (let ((element (org-element-at-point)))
1421 (when (eq (org-element-type element) 'keyword)
1422 (let* ((key (org-element-property :key element))
1423 (val (org-element-property :value element))
1424 (prop
1425 (cond
1426 ((string= key "SETUP_FILE")
1427 (let ((file
1428 (expand-file-name
1429 (org-remove-double-quotes (org-trim val)))))
1430 ;; Avoid circular dependencies.
1431 (unless (member file files)
1432 (with-temp-buffer
1433 (insert (org-file-contents file 'noerror))
1434 (org-mode)
1435 (org-export--get-inbuffer-options
1436 backend (cons file files))))))
1437 ((string= key "OPTIONS")
1438 (org-export--parse-option-keyword val backend)))))
1439 (setq plist (org-combine-plists plist prop)))))))
1440 ;; 2. Standard options, as in `org-export-options-alist'.
1441 (let* ((all (append org-export-options-alist
1442 ;; Also look for back-end specific options
1443 ;; if BACKEND is defined.
1444 (and backend
1445 (let ((var
1446 (intern
1447 (format "org-%s-options-alist" backend))))
1448 (and (boundp var) (eval var))))))
1449 ;; Build ALIST between keyword name and property name.
1450 (alist
1451 (delq nil (mapcar
1452 (lambda (e) (when (nth 1 e) (cons (nth 1 e) (car e))))
1453 all)))
1454 ;; Build regexp matching all keywords associated to export
1455 ;; options. Note: the search is case insensitive.
1456 (opt-re (format "^[ \t]*#\\+%s:"
1457 (regexp-opt
1458 (delq nil (mapcar (lambda (e) (nth 1 e)) all))))))
1459 (goto-char (point-min))
1460 (while (re-search-forward opt-re nil t)
1461 (let ((element (org-element-at-point)))
1462 (when (eq (org-element-type element) 'keyword)
1463 (let* ((key (org-element-property :key element))
1464 (val (org-element-property :value element))
1465 (prop (cdr (assoc key alist)))
1466 (behaviour (nth 4 (assq prop all))))
1467 (setq plist
1468 (plist-put
1469 plist prop
1470 ;; Handle value depending on specified BEHAVIOUR.
1471 (case behaviour
1472 (space
1473 (if (not (plist-get plist prop)) (org-trim val)
1474 (concat (plist-get plist prop) " " (org-trim val))))
1475 (newline
1476 (org-trim
1477 (concat (plist-get plist prop) "\n" (org-trim val))))
1478 (split
1479 `(,@(plist-get plist prop) ,@(org-split-string val)))
1480 ('t val)
1481 (otherwise (if (not (plist-member plist prop)) val
1482 (plist-get plist prop))))))))))
1483 ;; Parse keywords specified in
1484 ;; `org-element-document-properties'.
1485 (mapc
1486 (lambda (key)
1487 (let* ((prop (cdr (assoc key alist)))
1488 (value (and prop (plist-get plist prop))))
1489 (when (stringp value)
1490 (setq plist
1491 (plist-put
1492 plist prop
1493 (org-element-parse-secondary-string
1494 value (org-element-restriction 'keyword)))))))
1495 org-element-document-properties))
1496 ;; 3. Return final value.
1497 plist)))
1499 (defun org-export--get-buffer-attributes ()
1500 "Return properties related to buffer attributes, as a plist."
1501 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1502 (list
1503 ;; Store full path of input file name, or nil. For internal use.
1504 :input-file visited-file
1505 :title (or (and visited-file
1506 (file-name-sans-extension
1507 (file-name-nondirectory visited-file)))
1508 (buffer-name (buffer-base-buffer)))
1509 :footnote-definition-alist
1510 ;; Footnotes definitions must be collected in the original
1511 ;; buffer, as there's no insurance that they will still be in the
1512 ;; parse tree, due to possible narrowing.
1513 (let (alist)
1514 (org-with-wide-buffer
1515 (goto-char (point-min))
1516 (while (re-search-forward org-footnote-definition-re nil t)
1517 (let ((def (save-match-data (org-element-at-point))))
1518 (when (eq (org-element-type def) 'footnote-definition)
1519 (push
1520 (cons (org-element-property :label def)
1521 (let ((cbeg (org-element-property :contents-begin def)))
1522 (when cbeg
1523 (org-element--parse-elements
1524 cbeg (org-element-property :contents-end def)
1525 nil nil nil nil (list 'org-data nil)))))
1526 alist))))
1527 alist))
1528 :id-alist
1529 ;; Collect id references.
1530 (let (alist)
1531 (org-with-wide-buffer
1532 (goto-char (point-min))
1533 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1534 (let ((link (org-element-context)))
1535 (when (eq (org-element-type link) 'link)
1536 (let* ((id (org-element-property :path link))
1537 (file (org-id-find-id-file id)))
1538 (when file
1539 (push (cons id (file-relative-name file)) alist)))))))
1540 alist))))
1542 (defun org-export--get-global-options (&optional backend)
1543 "Return global export options as a plist.
1545 Optional argument BACKEND, if non-nil, is a symbol specifying
1546 which back-end specific export options should also be read in the
1547 process."
1548 (let ((all (append org-export-options-alist
1549 (and backend
1550 (let ((var (intern
1551 (format "org-%s-options-alist" backend))))
1552 (and (boundp var) (symbol-value var))))))
1553 ;; Output value.
1554 plist)
1555 (mapc
1556 (lambda (cell)
1557 (setq plist
1558 (plist-put
1559 plist
1560 (car cell)
1561 ;; Eval default value provided. If keyword is a member
1562 ;; of `org-element-document-properties', parse it as
1563 ;; a secondary string before storing it.
1564 (let ((value (eval (nth 3 cell))))
1565 (if (not (stringp value)) value
1566 (let ((keyword (nth 1 cell)))
1567 (if (not (member keyword org-element-document-properties))
1568 value
1569 (org-element-parse-secondary-string
1570 value (org-element-restriction 'keyword)))))))))
1571 all)
1572 ;; Return value.
1573 plist))
1575 (defvar org-export--allow-BIND-local nil)
1576 (defun org-export--confirm-letbind ()
1577 "Can we use #+BIND values during export?
1578 By default this will ask for confirmation by the user, to divert
1579 possible security risks."
1580 (cond
1581 ((not org-export-allow-BIND) nil)
1582 ((eq org-export-allow-BIND t) t)
1583 ((local-variable-p 'org-export--allow-BIND-local)
1584 org-export--allow-BIND-local)
1585 (t (org-set-local 'org-export--allow-BIND-local
1586 (yes-or-no-p "Allow BIND values in this buffer? ")))))
1588 (defun org-export--install-letbind-maybe ()
1589 "Install the values from #+BIND lines as local variables.
1590 Variables must be installed before in-buffer options are
1591 retrieved."
1592 (let ((case-fold-search t) letbind pair)
1593 (org-with-wide-buffer
1594 (goto-char (point-min))
1595 (while (re-search-forward "^[ \t]*#\\+BIND:" nil t)
1596 (let* ((element (org-element-at-point))
1597 (value (org-element-property :value element)))
1598 (when (and (eq (org-element-type element) 'keyword)
1599 (not (equal value ""))
1600 (org-export--confirm-letbind))
1601 (push (read (format "(%s)" value)) letbind)))))
1602 (dolist (pair (nreverse letbind))
1603 (org-set-local (car pair) (nth 1 pair)))))
1606 ;;;; Tree Properties
1608 ;; Tree properties are information extracted from parse tree. They
1609 ;; are initialized at the beginning of the transcoding process by
1610 ;; `org-export-collect-tree-properties'.
1612 ;; Dedicated functions focus on computing the value of specific tree
1613 ;; properties during initialization. Thus,
1614 ;; `org-export--populate-ignore-list' lists elements and objects that
1615 ;; should be skipped during export, `org-export--get-min-level' gets
1616 ;; the minimal exportable level, used as a basis to compute relative
1617 ;; level for headlines. Eventually
1618 ;; `org-export--collect-headline-numbering' builds an alist between
1619 ;; headlines and their numbering.
1621 (defun org-export-collect-tree-properties (data info)
1622 "Extract tree properties from parse tree.
1624 DATA is the parse tree from which information is retrieved. INFO
1625 is a list holding export options.
1627 Following tree properties are set or updated:
1629 `:exported-data' Hash table used to memoize results from
1630 `org-export-data'.
1632 `:footnote-definition-alist' List of footnotes definitions in
1633 original buffer and current parse tree.
1635 `:headline-offset' Offset between true level of headlines and
1636 local level. An offset of -1 means an headline
1637 of level 2 should be considered as a level
1638 1 headline in the context.
1640 `:headline-numbering' Alist of all headlines as key an the
1641 associated numbering as value.
1643 `:ignore-list' List of elements that should be ignored during
1644 export.
1646 `:target-list' List of all targets in the parse tree.
1648 Return updated plist."
1649 ;; Install the parse tree in the communication channel, in order to
1650 ;; use `org-export-get-genealogy' and al.
1651 (setq info (plist-put info :parse-tree data))
1652 ;; Get the list of elements and objects to ignore, and put it into
1653 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1654 ;; been done during parse tree filtering.
1655 (setq info
1656 (plist-put info
1657 :ignore-list
1658 (append (org-export--populate-ignore-list data info)
1659 (plist-get info :ignore-list))))
1660 ;; Compute `:headline-offset' in order to be able to use
1661 ;; `org-export-get-relative-level'.
1662 (setq info
1663 (plist-put info
1664 :headline-offset
1665 (- 1 (org-export--get-min-level data info))))
1666 ;; Update footnotes definitions list with definitions in parse tree.
1667 ;; This is required since buffer expansion might have modified
1668 ;; boundaries of footnote definitions contained in the parse tree.
1669 ;; This way, definitions in `footnote-definition-alist' are bound to
1670 ;; match those in the parse tree.
1671 (let ((defs (plist-get info :footnote-definition-alist)))
1672 (org-element-map
1673 data 'footnote-definition
1674 (lambda (fn)
1675 (push (cons (org-element-property :label fn)
1676 `(org-data nil ,@(org-element-contents fn)))
1677 defs)))
1678 (setq info (plist-put info :footnote-definition-alist defs)))
1679 ;; Properties order doesn't matter: get the rest of the tree
1680 ;; properties.
1681 (nconc
1682 `(:target-list
1683 ,(org-element-map
1684 data '(keyword target)
1685 (lambda (blob)
1686 (when (or (eq (org-element-type blob) 'target)
1687 (string= (org-element-property :key blob) "TARGET"))
1688 blob)) info)
1689 :headline-numbering ,(org-export--collect-headline-numbering data info)
1690 :exported-data ,(make-hash-table :test 'eq :size 4001))
1691 info))
1693 (defun org-export--get-min-level (data options)
1694 "Return minimum exportable headline's level in DATA.
1695 DATA is parsed tree as returned by `org-element-parse-buffer'.
1696 OPTIONS is a plist holding export options."
1697 (catch 'exit
1698 (let ((min-level 10000))
1699 (mapc
1700 (lambda (blob)
1701 (when (and (eq (org-element-type blob) 'headline)
1702 (not (memq blob (plist-get options :ignore-list))))
1703 (setq min-level
1704 (min (org-element-property :level blob) min-level)))
1705 (when (= min-level 1) (throw 'exit 1)))
1706 (org-element-contents data))
1707 ;; If no headline was found, for the sake of consistency, set
1708 ;; minimum level to 1 nonetheless.
1709 (if (= min-level 10000) 1 min-level))))
1711 (defun org-export--collect-headline-numbering (data options)
1712 "Return numbering of all exportable headlines in a parse tree.
1714 DATA is the parse tree. OPTIONS is the plist holding export
1715 options.
1717 Return an alist whose key is an headline and value is its
1718 associated numbering \(in the shape of a list of numbers\)."
1719 (let ((numbering (make-vector org-export-max-depth 0)))
1720 (org-element-map
1721 data
1722 'headline
1723 (lambda (headline)
1724 (let ((relative-level
1725 (1- (org-export-get-relative-level headline options))))
1726 (cons
1727 headline
1728 (loop for n across numbering
1729 for idx from 0 to org-export-max-depth
1730 when (< idx relative-level) collect n
1731 when (= idx relative-level) collect (aset numbering idx (1+ n))
1732 when (> idx relative-level) do (aset numbering idx 0)))))
1733 options)))
1735 (defun org-export--populate-ignore-list (data options)
1736 "Return list of elements and objects to ignore during export.
1737 DATA is the parse tree to traverse. OPTIONS is the plist holding
1738 export options."
1739 (let* (ignore
1740 walk-data
1741 ;; First find trees containing a select tag, if any.
1742 (selected (org-export--selected-trees data options))
1743 (walk-data
1744 (lambda (data)
1745 ;; Collect ignored elements or objects into IGNORE-LIST.
1746 (let ((type (org-element-type data)))
1747 (if (org-export--skip-p data options selected) (push data ignore)
1748 (if (and (eq type 'headline)
1749 (eq (plist-get options :with-archived-trees) 'headline)
1750 (org-element-property :archivedp data))
1751 ;; If headline is archived but tree below has
1752 ;; to be skipped, add it to ignore list.
1753 (mapc (lambda (e) (push e ignore))
1754 (org-element-contents data))
1755 ;; Move into secondary string, if any.
1756 (let ((sec-prop
1757 (cdr (assq type org-element-secondary-value-alist))))
1758 (when sec-prop
1759 (mapc walk-data (org-element-property sec-prop data))))
1760 ;; Move into recursive objects/elements.
1761 (mapc walk-data (org-element-contents data))))))))
1762 ;; Main call.
1763 (funcall walk-data data)
1764 ;; Return value.
1765 ignore))
1767 (defun org-export--selected-trees (data info)
1768 "Return list of headlines containing a select tag in their tree.
1769 DATA is parsed data as returned by `org-element-parse-buffer'.
1770 INFO is a plist holding export options."
1771 (let* (selected-trees
1772 walk-data ; for byte-compiler.
1773 (walk-data
1774 (function
1775 (lambda (data genealogy)
1776 (case (org-element-type data)
1777 (org-data (mapc (lambda (el) (funcall walk-data el genealogy))
1778 (org-element-contents data)))
1779 (headline
1780 (let ((tags (org-element-property :tags data)))
1781 (if (loop for tag in (plist-get info :select-tags)
1782 thereis (member tag tags))
1783 ;; When a select tag is found, mark full
1784 ;; genealogy and every headline within the tree
1785 ;; as acceptable.
1786 (setq selected-trees
1787 (append
1788 genealogy
1789 (org-element-map data 'headline 'identity)
1790 selected-trees))
1791 ;; Else, continue searching in tree, recursively.
1792 (mapc
1793 (lambda (el) (funcall walk-data el (cons data genealogy)))
1794 (org-element-contents data))))))))))
1795 (funcall walk-data data nil) selected-trees))
1797 (defun org-export--skip-p (blob options selected)
1798 "Non-nil when element or object BLOB should be skipped during export.
1799 OPTIONS is the plist holding export options. SELECTED, when
1800 non-nil, is a list of headlines belonging to a tree with a select
1801 tag."
1802 (case (org-element-type blob)
1803 (clock (not (plist-get options :with-clocks)))
1804 (drawer
1805 (or (not (plist-get options :with-drawers))
1806 (and (consp (plist-get options :with-drawers))
1807 (not (member (org-element-property :drawer-name blob)
1808 (plist-get options :with-drawers))))))
1809 (headline
1810 (let ((with-tasks (plist-get options :with-tasks))
1811 (todo (org-element-property :todo-keyword blob))
1812 (todo-type (org-element-property :todo-type blob))
1813 (archived (plist-get options :with-archived-trees))
1814 (tags (org-element-property :tags blob)))
1816 ;; Ignore subtrees with an exclude tag.
1817 (loop for k in (plist-get options :exclude-tags)
1818 thereis (member k tags))
1819 ;; When a select tag is present in the buffer, ignore any tree
1820 ;; without it.
1821 (and selected (not (memq blob selected)))
1822 ;; Ignore commented sub-trees.
1823 (org-element-property :commentedp blob)
1824 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
1825 (and (not archived) (org-element-property :archivedp blob))
1826 ;; Ignore tasks, if specified by `:with-tasks' property.
1827 (and todo
1828 (or (not with-tasks)
1829 (and (memq with-tasks '(todo done))
1830 (not (eq todo-type with-tasks)))
1831 (and (consp with-tasks) (not (member todo with-tasks))))))))
1832 (inlinetask (not (plist-get options :with-inlinetasks)))
1833 (planning (not (plist-get options :with-plannings)))
1834 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
1835 (table-cell
1836 (and (org-export-table-has-special-column-p
1837 (org-export-get-parent-table blob))
1838 (not (org-export-get-previous-element blob options))))
1839 (table-row (org-export-table-row-is-special-p blob options))
1840 (timestamp
1841 (case (plist-get options :with-timestamps)
1842 ;; No timestamp allowed.
1843 ('nil t)
1844 ;; Only active timestamps allowed and the current one isn't
1845 ;; active.
1846 (active
1847 (not (memq (org-element-property :type blob)
1848 '(active active-range))))
1849 ;; Only inactive timestamps allowed and the current one isn't
1850 ;; inactive.
1851 (inactive
1852 (not (memq (org-element-property :type blob)
1853 '(inactive inactive-range))))))))
1857 ;;; The Transcoder
1859 ;; `org-export-data' reads a parse tree (obtained with, i.e.
1860 ;; `org-element-parse-buffer') and transcodes it into a specified
1861 ;; back-end output. It takes care of filtering out elements or
1862 ;; objects according to export options and organizing the output blank
1863 ;; lines and white space are preserved. The function memoizes its
1864 ;; results, so it is cheap to call it within translators.
1866 ;; Internally, three functions handle the filtering of objects and
1867 ;; elements during the export. In particular,
1868 ;; `org-export-ignore-element' marks an element or object so future
1869 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
1870 ;; elements or objects should be seen as real Org syntax and
1871 ;; `org-export-expand' transforms the others back into their original
1872 ;; shape
1874 ;; `org-export-transcoder' is an accessor returning appropriate
1875 ;; translator function for a given element or object.
1877 (defun org-export-transcoder (blob info)
1878 "Return appropriate transcoder for BLOB.
1879 INFO is a plist containing export directives."
1880 (let ((type (org-element-type blob)))
1881 ;; Return contents only for complete parse trees.
1882 (if (eq type 'org-data) (lambda (blob contents info) contents)
1883 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
1884 (and (functionp transcoder) transcoder)))))
1886 (defun org-export-data (data info)
1887 "Convert DATA into current back-end format.
1889 DATA is a parse tree, an element or an object or a secondary
1890 string. INFO is a plist holding export options.
1892 Return transcoded string."
1893 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
1894 (if (not (eq memo 'no-memo)) memo
1895 (let* ((type (org-element-type data))
1896 (results
1897 (cond
1898 ;; Ignored element/object.
1899 ((memq data (plist-get info :ignore-list)) nil)
1900 ;; Plain text. All residual text properties from parse
1901 ;; tree (i.e. `:parent' property) are removed.
1902 ((eq type 'plain-text)
1903 (org-no-properties
1904 (org-export-filter-apply-functions
1905 (plist-get info :filter-plain-text)
1906 (let ((transcoder (org-export-transcoder data info)))
1907 (if transcoder (funcall transcoder data info) data))
1908 info)))
1909 ;; Uninterpreted element/object: change it back to Org
1910 ;; syntax and export again resulting raw string.
1911 ((not (org-export--interpret-p data info))
1912 (org-export-data
1913 (org-export-expand
1914 data
1915 (mapconcat (lambda (blob) (org-export-data blob info))
1916 (org-element-contents data)
1917 ""))
1918 info))
1919 ;; Secondary string.
1920 ((not type)
1921 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
1922 ;; Element/Object without contents or, as a special case,
1923 ;; headline with archive tag and archived trees restricted
1924 ;; to title only.
1925 ((or (not (org-element-contents data))
1926 (and (eq type 'headline)
1927 (eq (plist-get info :with-archived-trees) 'headline)
1928 (org-element-property :archivedp data)))
1929 (let ((transcoder (org-export-transcoder data info)))
1930 (and (functionp transcoder)
1931 (funcall transcoder data nil info))))
1932 ;; Element/Object with contents.
1934 (let ((transcoder (org-export-transcoder data info)))
1935 (when transcoder
1936 (let* ((greaterp (memq type org-element-greater-elements))
1937 (objectp
1938 (and (not greaterp)
1939 (memq type org-element-recursive-objects)))
1940 (contents
1941 (mapconcat
1942 (lambda (element) (org-export-data element info))
1943 (org-element-contents
1944 (if (or greaterp objectp) data
1945 ;; Elements directly containing objects
1946 ;; must have their indentation normalized
1947 ;; first.
1948 (org-element-normalize-contents
1949 data
1950 ;; When normalizing contents of the first
1951 ;; paragraph in an item or a footnote
1952 ;; definition, ignore first line's
1953 ;; indentation: there is none and it
1954 ;; might be misleading.
1955 (when (eq type 'paragraph)
1956 (let ((parent (org-export-get-parent data)))
1957 (and
1958 (eq (car (org-element-contents parent))
1959 data)
1960 (memq (org-element-type parent)
1961 '(footnote-definition item))))))))
1962 "")))
1963 (funcall transcoder data
1964 (if (not greaterp) contents
1965 (org-element-normalize-string contents))
1966 info))))))))
1967 ;; Final result will be memoized before being returned.
1968 (puthash
1969 data
1970 (cond
1971 ((not results) nil)
1972 ((memq type '(org-data plain-text nil)) results)
1973 ;; Append the same white space between elements or objects as in
1974 ;; the original buffer, and call appropriate filters.
1976 (let ((results
1977 (org-export-filter-apply-functions
1978 (plist-get info (intern (format ":filter-%s" type)))
1979 (let ((post-blank (or (org-element-property :post-blank data)
1980 0)))
1981 (if (memq type org-element-all-elements)
1982 (concat (org-element-normalize-string results)
1983 (make-string post-blank ?\n))
1984 (concat results (make-string post-blank ? ))))
1985 info)))
1986 results)))
1987 (plist-get info :exported-data))))))
1989 (defun org-export--interpret-p (blob info)
1990 "Non-nil if element or object BLOB should be interpreted as Org syntax.
1991 Check is done according to export options INFO, stored as
1992 a plist."
1993 (case (org-element-type blob)
1994 ;; ... entities...
1995 (entity (plist-get info :with-entities))
1996 ;; ... emphasis...
1997 (emphasis (plist-get info :with-emphasize))
1998 ;; ... fixed-width areas.
1999 (fixed-width (plist-get info :with-fixed-width))
2000 ;; ... footnotes...
2001 ((footnote-definition footnote-reference)
2002 (plist-get info :with-footnotes))
2003 ;; ... sub/superscripts...
2004 ((subscript superscript)
2005 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2006 (if (eq sub/super-p '{})
2007 (org-element-property :use-brackets-p blob)
2008 sub/super-p)))
2009 ;; ... tables...
2010 (table (plist-get info :with-tables))
2011 (otherwise t)))
2013 (defun org-export-expand (blob contents)
2014 "Expand a parsed element or object to its original state.
2015 BLOB is either an element or an object. CONTENTS is its
2016 contents, as a string or nil."
2017 (funcall
2018 (intern (format "org-element-%s-interpreter" (org-element-type blob)))
2019 blob contents))
2021 (defun org-export-ignore-element (element info)
2022 "Add ELEMENT to `:ignore-list' in INFO.
2024 Any element in `:ignore-list' will be skipped when using
2025 `org-element-map'. INFO is modified by side effects."
2026 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2030 ;;; The Filter System
2032 ;; Filters allow end-users to tweak easily the transcoded output.
2033 ;; They are the functional counterpart of hooks, as every filter in
2034 ;; a set is applied to the return value of the previous one.
2036 ;; Every set is back-end agnostic. Although, a filter is always
2037 ;; called, in addition to the string it applies to, with the back-end
2038 ;; used as argument, so it's easy for the end-user to add back-end
2039 ;; specific filters in the set. The communication channel, as
2040 ;; a plist, is required as the third argument.
2042 ;; From the developer side, filters sets can be installed in the
2043 ;; process with the help of `org-export-define-backend', which
2044 ;; internally sets `org-BACKEND-filters-alist' variable. Each
2045 ;; association has a key among the following symbols and a function or
2046 ;; a list of functions as value.
2048 ;; - `:filter-parse-tree' applies directly on the complete parsed
2049 ;; tree. It's the only filters set that doesn't apply to a string.
2050 ;; Users can set it through `org-export-filter-parse-tree-functions'
2051 ;; variable.
2053 ;; - `:filter-final-output' applies to the final transcoded string.
2054 ;; Users can set it with `org-export-filter-final-output-functions'
2055 ;; variable
2057 ;; - `:filter-plain-text' applies to any string not recognized as Org
2058 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2059 ;; configure it.
2061 ;; - `:filter-TYPE' applies on the string returned after an element or
2062 ;; object of type TYPE has been transcoded. An user can modify
2063 ;; `org-export-filter-TYPE-functions'
2065 ;; All filters sets are applied with
2066 ;; `org-export-filter-apply-functions' function. Filters in a set are
2067 ;; applied in a LIFO fashion. It allows developers to be sure that
2068 ;; their filters will be applied first.
2070 ;; Filters properties are installed in communication channel with
2071 ;; `org-export-install-filters' function.
2073 ;; Eventually, a hook (`org-export-before-parsing-hook') is run just
2074 ;; before parsing to allow for heavy structure modifications.
2077 ;;;; Before Parsing Hook
2079 (defvar org-export-before-parsing-hook nil
2080 "Hook run before parsing an export buffer.
2082 This is run after include keywords have been expanded and Babel
2083 code executed, on a copy of original buffer's area being
2084 exported. Visibility is the same as in the original one. Point
2085 is left at the beginning of the new one.
2087 Every function in this hook will be called with one argument: the
2088 back-end currently used, as a symbol.")
2091 ;;;; Special Filters
2093 (defvar org-export-filter-parse-tree-functions nil
2094 "List of functions applied to the parsed tree.
2095 Each filter is called with three arguments: the parse tree, as
2096 returned by `org-element-parse-buffer', the back-end, as
2097 a symbol, and the communication channel, as a plist. It must
2098 return the modified parse tree to transcode.")
2100 (defvar org-export-filter-final-output-functions nil
2101 "List of functions applied to the transcoded string.
2102 Each filter is called with three arguments: the full transcoded
2103 string, the back-end, as a symbol, and the communication channel,
2104 as a plist. It must return a string that will be used as the
2105 final export output.")
2107 (defvar org-export-filter-plain-text-functions nil
2108 "List of functions applied to plain text.
2109 Each filter is called with three arguments: a string which
2110 contains no Org syntax, the back-end, as a symbol, and the
2111 communication channel, as a plist. It must return a string or
2112 nil.")
2115 ;;;; Elements Filters
2117 (defvar org-export-filter-babel-call-functions nil
2118 "List of functions applied to a transcoded babel-call.
2119 Each filter is called with three arguments: the transcoded data,
2120 as a string, the back-end, as a symbol, and the communication
2121 channel, as a plist. It must return a string or nil.")
2123 (defvar org-export-filter-center-block-functions nil
2124 "List of functions applied to a transcoded center block.
2125 Each filter is called with three arguments: the transcoded data,
2126 as a string, the back-end, as a symbol, and the communication
2127 channel, as a plist. It must return a string or nil.")
2129 (defvar org-export-filter-clock-functions nil
2130 "List of functions applied to a transcoded clock.
2131 Each filter is called with three arguments: the transcoded data,
2132 as a string, the back-end, as a symbol, and the communication
2133 channel, as a plist. It must return a string or nil.")
2135 (defvar org-export-filter-comment-functions nil
2136 "List of functions applied to a transcoded comment.
2137 Each filter is called with three arguments: the transcoded data,
2138 as a string, the back-end, as a symbol, and the communication
2139 channel, as a plist. It must return a string or nil.")
2141 (defvar org-export-filter-comment-block-functions nil
2142 "List of functions applied to a transcoded comment-block.
2143 Each filter is called with three arguments: the transcoded data,
2144 as a string, the back-end, as a symbol, and the communication
2145 channel, as a plist. It must return a string or nil.")
2147 (defvar org-export-filter-diary-sexp-functions nil
2148 "List of functions applied to a transcoded diary-sexp.
2149 Each filter is called with three arguments: the transcoded data,
2150 as a string, the back-end, as a symbol, and the communication
2151 channel, as a plist. It must return a string or nil.")
2153 (defvar org-export-filter-drawer-functions nil
2154 "List of functions applied to a transcoded drawer.
2155 Each filter is called with three arguments: the transcoded data,
2156 as a string, the back-end, as a symbol, and the communication
2157 channel, as a plist. It must return a string or nil.")
2159 (defvar org-export-filter-dynamic-block-functions nil
2160 "List of functions applied to a transcoded dynamic-block.
2161 Each filter is called with three arguments: the transcoded data,
2162 as a string, the back-end, as a symbol, and the communication
2163 channel, as a plist. It must return a string or nil.")
2165 (defvar org-export-filter-example-block-functions nil
2166 "List of functions applied to a transcoded example-block.
2167 Each filter is called with three arguments: the transcoded data,
2168 as a string, the back-end, as a symbol, and the communication
2169 channel, as a plist. It must return a string or nil.")
2171 (defvar org-export-filter-export-block-functions nil
2172 "List of functions applied to a transcoded export-block.
2173 Each filter is called with three arguments: the transcoded data,
2174 as a string, the back-end, as a symbol, and the communication
2175 channel, as a plist. It must return a string or nil.")
2177 (defvar org-export-filter-fixed-width-functions nil
2178 "List of functions applied to a transcoded fixed-width.
2179 Each filter is called with three arguments: the transcoded data,
2180 as a string, the back-end, as a symbol, and the communication
2181 channel, as a plist. It must return a string or nil.")
2183 (defvar org-export-filter-footnote-definition-functions nil
2184 "List of functions applied to a transcoded footnote-definition.
2185 Each filter is called with three arguments: the transcoded data,
2186 as a string, the back-end, as a symbol, and the communication
2187 channel, as a plist. It must return a string or nil.")
2189 (defvar org-export-filter-headline-functions nil
2190 "List of functions applied to a transcoded headline.
2191 Each filter is called with three arguments: the transcoded data,
2192 as a string, the back-end, as a symbol, and the communication
2193 channel, as a plist. It must return a string or nil.")
2195 (defvar org-export-filter-horizontal-rule-functions nil
2196 "List of functions applied to a transcoded horizontal-rule.
2197 Each filter is called with three arguments: the transcoded data,
2198 as a string, the back-end, as a symbol, and the communication
2199 channel, as a plist. It must return a string or nil.")
2201 (defvar org-export-filter-inlinetask-functions nil
2202 "List of functions applied to a transcoded inlinetask.
2203 Each filter is called with three arguments: the transcoded data,
2204 as a string, the back-end, as a symbol, and the communication
2205 channel, as a plist. It must return a string or nil.")
2207 (defvar org-export-filter-item-functions nil
2208 "List of functions applied to a transcoded item.
2209 Each filter is called with three arguments: the transcoded data,
2210 as a string, the back-end, as a symbol, and the communication
2211 channel, as a plist. It must return a string or nil.")
2213 (defvar org-export-filter-keyword-functions nil
2214 "List of functions applied to a transcoded keyword.
2215 Each filter is called with three arguments: the transcoded data,
2216 as a string, the back-end, as a symbol, and the communication
2217 channel, as a plist. It must return a string or nil.")
2219 (defvar org-export-filter-latex-environment-functions nil
2220 "List of functions applied to a transcoded latex-environment.
2221 Each filter is called with three arguments: the transcoded data,
2222 as a string, the back-end, as a symbol, and the communication
2223 channel, as a plist. It must return a string or nil.")
2225 (defvar org-export-filter-node-property-functions nil
2226 "List of functions applied to a transcoded node-property.
2227 Each filter is called with three arguments: the transcoded data,
2228 as a string, the back-end, as a symbol, and the communication
2229 channel, as a plist. It must return a string or nil.")
2231 (defvar org-export-filter-paragraph-functions nil
2232 "List of functions applied to a transcoded paragraph.
2233 Each filter is called with three arguments: the transcoded data,
2234 as a string, the back-end, as a symbol, and the communication
2235 channel, as a plist. It must return a string or nil.")
2237 (defvar org-export-filter-plain-list-functions nil
2238 "List of functions applied to a transcoded plain-list.
2239 Each filter is called with three arguments: the transcoded data,
2240 as a string, the back-end, as a symbol, and the communication
2241 channel, as a plist. It must return a string or nil.")
2243 (defvar org-export-filter-planning-functions nil
2244 "List of functions applied to a transcoded planning.
2245 Each filter is called with three arguments: the transcoded data,
2246 as a string, the back-end, as a symbol, and the communication
2247 channel, as a plist. It must return a string or nil.")
2249 (defvar org-export-filter-property-drawer-functions nil
2250 "List of functions applied to a transcoded property-drawer.
2251 Each filter is called with three arguments: the transcoded data,
2252 as a string, the back-end, as a symbol, and the communication
2253 channel, as a plist. It must return a string or nil.")
2255 (defvar org-export-filter-quote-block-functions nil
2256 "List of functions applied to a transcoded quote block.
2257 Each filter is called with three arguments: the transcoded quote
2258 data, as a string, the back-end, as a symbol, and the
2259 communication channel, as a plist. It must return a string or
2260 nil.")
2262 (defvar org-export-filter-quote-section-functions nil
2263 "List of functions applied to a transcoded quote-section.
2264 Each filter is called with three arguments: the transcoded data,
2265 as a string, the back-end, as a symbol, and the communication
2266 channel, as a plist. It must return a string or nil.")
2268 (defvar org-export-filter-section-functions nil
2269 "List of functions applied to a transcoded section.
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-special-block-functions nil
2275 "List of functions applied to a transcoded special 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-src-block-functions nil
2281 "List of functions applied to a transcoded src-block.
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-table-functions nil
2287 "List of functions applied to a transcoded table.
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-table-cell-functions nil
2293 "List of functions applied to a transcoded table-cell.
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-table-row-functions nil
2299 "List of functions applied to a transcoded table-row.
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-verse-block-functions nil
2305 "List of functions applied to a transcoded verse block.
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.")
2311 ;;;; Objects Filters
2313 (defvar org-export-filter-bold-functions nil
2314 "List of functions applied to transcoded bold text.
2315 Each filter is called with three arguments: the transcoded data,
2316 as a string, the back-end, as a symbol, and the communication
2317 channel, as a plist. It must return a string or nil.")
2319 (defvar org-export-filter-code-functions nil
2320 "List of functions applied to transcoded code text.
2321 Each filter is called with three arguments: the transcoded data,
2322 as a string, the back-end, as a symbol, and the communication
2323 channel, as a plist. It must return a string or nil.")
2325 (defvar org-export-filter-entity-functions nil
2326 "List of functions applied to a transcoded entity.
2327 Each filter is called with three arguments: the transcoded data,
2328 as a string, the back-end, as a symbol, and the communication
2329 channel, as a plist. It must return a string or nil.")
2331 (defvar org-export-filter-export-snippet-functions nil
2332 "List of functions applied to a transcoded export-snippet.
2333 Each filter is called with three arguments: the transcoded data,
2334 as a string, the back-end, as a symbol, and the communication
2335 channel, as a plist. It must return a string or nil.")
2337 (defvar org-export-filter-footnote-reference-functions nil
2338 "List of functions applied to a transcoded footnote-reference.
2339 Each filter is called with three arguments: the transcoded data,
2340 as a string, the back-end, as a symbol, and the communication
2341 channel, as a plist. It must return a string or nil.")
2343 (defvar org-export-filter-inline-babel-call-functions nil
2344 "List of functions applied to a transcoded inline-babel-call.
2345 Each filter is called with three arguments: the transcoded data,
2346 as a string, the back-end, as a symbol, and the communication
2347 channel, as a plist. It must return a string or nil.")
2349 (defvar org-export-filter-inline-src-block-functions nil
2350 "List of functions applied to a transcoded inline-src-block.
2351 Each filter is called with three arguments: the transcoded data,
2352 as a string, the back-end, as a symbol, and the communication
2353 channel, as a plist. It must return a string or nil.")
2355 (defvar org-export-filter-italic-functions nil
2356 "List of functions applied to transcoded italic text.
2357 Each filter is called with three arguments: the transcoded data,
2358 as a string, the back-end, as a symbol, and the communication
2359 channel, as a plist. It must return a string or nil.")
2361 (defvar org-export-filter-latex-fragment-functions nil
2362 "List of functions applied to a transcoded latex-fragment.
2363 Each filter is called with three arguments: the transcoded data,
2364 as a string, the back-end, as a symbol, and the communication
2365 channel, as a plist. It must return a string or nil.")
2367 (defvar org-export-filter-line-break-functions nil
2368 "List of functions applied to a transcoded line-break.
2369 Each filter is called with three arguments: the transcoded data,
2370 as a string, the back-end, as a symbol, and the communication
2371 channel, as a plist. It must return a string or nil.")
2373 (defvar org-export-filter-link-functions nil
2374 "List of functions applied to a transcoded link.
2375 Each filter is called with three arguments: the transcoded data,
2376 as a string, the back-end, as a symbol, and the communication
2377 channel, as a plist. It must return a string or nil.")
2379 (defvar org-export-filter-macro-functions nil
2380 "List of functions applied to a transcoded macro.
2381 Each filter is called with three arguments: the transcoded data,
2382 as a string, the back-end, as a symbol, and the communication
2383 channel, as a plist. It must return a string or nil.")
2385 (defvar org-export-filter-radio-target-functions nil
2386 "List of functions applied to a transcoded radio-target.
2387 Each filter is called with three arguments: the transcoded data,
2388 as a string, the back-end, as a symbol, and the communication
2389 channel, as a plist. It must return a string or nil.")
2391 (defvar org-export-filter-statistics-cookie-functions nil
2392 "List of functions applied to a transcoded statistics-cookie.
2393 Each filter is called with three arguments: the transcoded data,
2394 as a string, the back-end, as a symbol, and the communication
2395 channel, as a plist. It must return a string or nil.")
2397 (defvar org-export-filter-strike-through-functions nil
2398 "List of functions applied to transcoded strike-through text.
2399 Each filter is called with three arguments: the transcoded data,
2400 as a string, the back-end, as a symbol, and the communication
2401 channel, as a plist. It must return a string or nil.")
2403 (defvar org-export-filter-subscript-functions nil
2404 "List of functions applied to a transcoded subscript.
2405 Each filter is called with three arguments: the transcoded data,
2406 as a string, the back-end, as a symbol, and the communication
2407 channel, as a plist. It must return a string or nil.")
2409 (defvar org-export-filter-superscript-functions nil
2410 "List of functions applied to a transcoded superscript.
2411 Each filter is called with three arguments: the transcoded data,
2412 as a string, the back-end, as a symbol, and the communication
2413 channel, as a plist. It must return a string or nil.")
2415 (defvar org-export-filter-target-functions nil
2416 "List of functions applied to a transcoded target.
2417 Each filter is called with three arguments: the transcoded data,
2418 as a string, the back-end, as a symbol, and the communication
2419 channel, as a plist. It must return a string or nil.")
2421 (defvar org-export-filter-timestamp-functions nil
2422 "List of functions applied to a transcoded timestamp.
2423 Each filter is called with three arguments: the transcoded data,
2424 as a string, the back-end, as a symbol, and the communication
2425 channel, as a plist. It must return a string or nil.")
2427 (defvar org-export-filter-underline-functions nil
2428 "List of functions applied to transcoded underline text.
2429 Each filter is called with three arguments: the transcoded data,
2430 as a string, the back-end, as a symbol, and the communication
2431 channel, as a plist. It must return a string or nil.")
2433 (defvar org-export-filter-verbatim-functions nil
2434 "List of functions applied to transcoded verbatim text.
2435 Each filter is called with three arguments: the transcoded data,
2436 as a string, the back-end, as a symbol, and the communication
2437 channel, as a plist. It must return a string or nil.")
2440 ;;;; Filters Tools
2442 ;; Internal function `org-export-install-filters' installs filters
2443 ;; hard-coded in back-ends (developer filters) and filters from global
2444 ;; variables (user filters) in the communication channel.
2446 ;; Internal function `org-export-filter-apply-functions' takes care
2447 ;; about applying each filter in order to a given data. It ignores
2448 ;; filters returning a nil value but stops whenever a filter returns
2449 ;; an empty string.
2451 (defun org-export-filter-apply-functions (filters value info)
2452 "Call every function in FILTERS.
2454 Functions are called with arguments VALUE, current export
2455 back-end and INFO. A function returning a nil value will be
2456 skipped. If it returns the empty string, the process ends and
2457 VALUE is ignored.
2459 Call is done in a LIFO fashion, to be sure that developer
2460 specified filters, if any, are called first."
2461 (catch 'exit
2462 (dolist (filter filters value)
2463 (let ((result (funcall filter value (plist-get info :back-end) info)))
2464 (cond ((not result) value)
2465 ((equal value "") (throw 'exit nil))
2466 (t (setq value result)))))))
2468 (defun org-export-install-filters (info)
2469 "Install filters properties in communication channel.
2471 INFO is a plist containing the current communication channel.
2473 Return the updated communication channel."
2474 (let (plist)
2475 ;; Install user defined filters with `org-export-filters-alist'.
2476 (mapc (lambda (p)
2477 (setq plist (plist-put plist (car p) (eval (cdr p)))))
2478 org-export-filters-alist)
2479 ;; Prepend back-end specific filters to that list.
2480 (let ((back-end-filters (intern (format "org-%s-filters-alist"
2481 (plist-get info :back-end)))))
2482 (when (boundp back-end-filters)
2483 (mapc (lambda (p)
2484 ;; Single values get consed, lists are prepended.
2485 (let ((key (car p)) (value (cdr p)))
2486 (when value
2487 (setq plist
2488 (plist-put
2489 plist key
2490 (if (atom value) (cons value (plist-get plist key))
2491 (append value (plist-get plist key))))))))
2492 (eval back-end-filters))))
2493 ;; Return new communication channel.
2494 (org-combine-plists info plist)))
2498 ;;; Core functions
2500 ;; This is the room for the main function, `org-export-as', along with
2501 ;; its derivatives, `org-export-to-buffer' and `org-export-to-file'.
2502 ;; They differ only by the way they output the resulting code.
2504 ;; `org-export-output-file-name' is an auxiliary function meant to be
2505 ;; used with `org-export-to-file'. With a given extension, it tries
2506 ;; to provide a canonical file name to write export output to.
2508 ;; Note that `org-export-as' doesn't really parse the current buffer,
2509 ;; but a copy of it (with the same buffer-local variables and
2510 ;; visibility), where macros and include keywords are expanded and
2511 ;; Babel blocks are executed, if appropriate.
2512 ;; `org-export-with-current-buffer-copy' macro prepares that copy.
2514 ;; File inclusion is taken care of by
2515 ;; `org-export-expand-include-keyword' and
2516 ;; `org-export--prepare-file-contents'. Structure wise, including
2517 ;; a whole Org file in a buffer often makes little sense. For
2518 ;; example, if the file contains an headline and the include keyword
2519 ;; was within an item, the item should contain the headline. That's
2520 ;; why file inclusion should be done before any structure can be
2521 ;; associated to the file, that is before parsing.
2523 ;; Macro are expanded with `org-export-expand-macro'.
2525 (defun org-export-as
2526 (backend &optional subtreep visible-only body-only ext-plist noexpand)
2527 "Transcode current Org buffer into BACKEND code.
2529 If narrowing is active in the current buffer, only transcode its
2530 narrowed part.
2532 If a region is active, transcode that region.
2534 When optional argument SUBTREEP is non-nil, transcode the
2535 sub-tree at point, extracting information from the headline
2536 properties first.
2538 When optional argument VISIBLE-ONLY is non-nil, don't export
2539 contents of hidden elements.
2541 When optional argument BODY-ONLY is non-nil, only return body
2542 code, without preamble nor postamble.
2544 Optional argument EXT-PLIST, when provided, is a property list
2545 with external parameters overriding Org default settings, but
2546 still inferior to file-local settings.
2548 Optional argument NOEXPAND, when non-nil, prevents included files
2549 to be expanded and Babel code to be executed.
2551 Return code as a string."
2552 ;; Barf if BACKEND isn't registered.
2553 (org-export-barf-if-invalid-backend backend)
2554 (save-excursion
2555 (save-restriction
2556 ;; Narrow buffer to an appropriate region or subtree for
2557 ;; parsing. If parsing subtree, be sure to remove main headline
2558 ;; too.
2559 (cond ((org-region-active-p)
2560 (narrow-to-region (region-beginning) (region-end)))
2561 (subtreep
2562 (org-narrow-to-subtree)
2563 (goto-char (point-min))
2564 (forward-line)
2565 (narrow-to-region (point) (point-max))))
2566 ;; 1. Get export environment from original buffer. Also install
2567 ;; user's and developer's filters.
2568 (let* ((info (org-export-install-filters
2569 (org-export-get-environment backend subtreep ext-plist)))
2570 ;; 2. Get parse tree. Buffer isn't parsed directly.
2571 ;; Instead, a temporary copy is created, where include
2572 ;; keywords and macros are expanded and code blocks
2573 ;; are evaluated.
2574 (tree (org-export-with-current-buffer-copy
2575 (unless noexpand
2576 (org-export-expand-include-keyword)
2577 ;; Update radio targets since keyword
2578 ;; inclusion might have added some more.
2579 (org-update-radio-target-regexp)
2580 (org-export-expand-macro info)
2581 ;; TODO: Setting `org-current-export-file' is
2582 ;; required by Org Babel to properly resolve
2583 ;; noweb references. Once "org-exp.el" is
2584 ;; removed, modify
2585 ;; `org-export-blocks-preprocess' so it
2586 ;; accepts the value as an argument instead.
2587 (let ((org-current-export-file (current-buffer)))
2588 (org-export-blocks-preprocess)))
2589 (goto-char (point-min))
2590 ;; Run hook
2591 ;; `org-export-before-parsing-hook'. with current
2592 ;; back-end as argument.
2593 (run-hook-with-args
2594 'org-export-before-parsing-hook backend)
2595 ;; Eventually parse buffer.
2596 (org-element-parse-buffer nil visible-only))))
2597 ;; 3. Call parse-tree filters to get the final tree.
2598 (setq tree
2599 (org-export-filter-apply-functions
2600 (plist-get info :filter-parse-tree) tree info))
2601 ;; 4. Now tree is complete, compute its properties and add
2602 ;; them to communication channel.
2603 (setq info
2604 (org-combine-plists
2605 info (org-export-collect-tree-properties tree info)))
2606 ;; 5. Eventually transcode TREE. Wrap the resulting string
2607 ;; into a template, if required. Eventually call
2608 ;; final-output filter.
2609 (let* ((body (org-element-normalize-string (org-export-data tree info)))
2610 (template (cdr (assq 'template
2611 (plist-get info :translate-alist))))
2612 (output (org-export-filter-apply-functions
2613 (plist-get info :filter-final-output)
2614 (if (or (not (functionp template)) body-only) body
2615 (funcall template body info))
2616 info)))
2617 ;; Maybe add final OUTPUT to kill ring, then return it.
2618 (when org-export-copy-to-kill-ring (org-kill-new output))
2619 output)))))
2621 (defun org-export-to-buffer
2622 (backend buffer &optional subtreep visible-only body-only ext-plist noexpand)
2623 "Call `org-export-as' with output to a specified buffer.
2625 BACKEND is the back-end used for transcoding, as a symbol.
2627 BUFFER is the output buffer. If it already exists, it will be
2628 erased first, otherwise, it will be created.
2630 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2631 and NOEXPAND are similar to those used in `org-export-as', which
2632 see.
2634 Return buffer."
2635 (let ((out (org-export-as
2636 backend subtreep visible-only body-only ext-plist noexpand))
2637 (buffer (get-buffer-create buffer)))
2638 (with-current-buffer buffer
2639 (erase-buffer)
2640 (insert out)
2641 (goto-char (point-min)))
2642 buffer))
2644 (defun org-export-to-file
2645 (backend file &optional subtreep visible-only body-only ext-plist noexpand)
2646 "Call `org-export-as' with output to a specified file.
2648 BACKEND is the back-end used for transcoding, as a symbol. FILE
2649 is the name of the output file, as a string.
2651 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY, EXT-PLIST
2652 and NOEXPAND are similar to those used in `org-export-as', which
2653 see.
2655 Return output file's name."
2656 ;; Checks for FILE permissions. `write-file' would do the same, but
2657 ;; we'd rather avoid needless transcoding of parse tree.
2658 (unless (file-writable-p file) (error "Output file not writable"))
2659 ;; Insert contents to a temporary buffer and write it to FILE.
2660 (let ((out (org-export-as
2661 backend subtreep visible-only body-only ext-plist noexpand)))
2662 (with-temp-buffer
2663 (insert out)
2664 (let ((coding-system-for-write org-export-coding-system))
2665 (write-file file))))
2666 ;; Return full path.
2667 file)
2669 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
2670 "Return output file's name according to buffer specifications.
2672 EXTENSION is a string representing the output file extension,
2673 with the leading dot.
2675 With a non-nil optional argument SUBTREEP, try to determine
2676 output file's name by looking for \"EXPORT_FILE_NAME\" property
2677 of subtree at point.
2679 When optional argument PUB-DIR is set, use it as the publishing
2680 directory.
2682 When optional argument VISIBLE-ONLY is non-nil, don't export
2683 contents of hidden elements.
2685 Return file name as a string, or nil if it couldn't be
2686 determined."
2687 (let ((base-name
2688 ;; File name may come from EXPORT_FILE_NAME subtree property,
2689 ;; assuming point is at beginning of said sub-tree.
2690 (file-name-sans-extension
2691 (or (and subtreep
2692 (org-entry-get
2693 (save-excursion
2694 (ignore-errors (org-back-to-heading) (point)))
2695 "EXPORT_FILE_NAME" t))
2696 ;; File name may be extracted from buffer's associated
2697 ;; file, if any.
2698 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
2699 (and visited-file (file-name-nondirectory visited-file)))
2700 ;; Can't determine file name on our own: Ask user.
2701 (let ((read-file-name-function
2702 (and org-completion-use-ido 'ido-read-file-name)))
2703 (read-file-name
2704 "Output file: " pub-dir nil nil nil
2705 (lambda (name)
2706 (string= (file-name-extension name t) extension))))))))
2707 ;; Build file name. Enforce EXTENSION over whatever user may have
2708 ;; come up with. PUB-DIR, if defined, always has precedence over
2709 ;; any provided path.
2710 (cond
2711 (pub-dir
2712 (concat (file-name-as-directory pub-dir)
2713 (file-name-nondirectory base-name)
2714 extension))
2715 ((file-name-absolute-p base-name) (concat base-name extension))
2716 (t (concat (file-name-as-directory ".") base-name extension)))))
2718 (defmacro org-export-with-current-buffer-copy (&rest body)
2719 "Apply BODY in a copy of the current buffer.
2721 The copy preserves local variables and visibility of the original
2722 buffer.
2724 Point is at buffer's beginning when BODY is applied."
2725 (declare (debug (body)))
2726 (org-with-gensyms (original-buffer offset buffer-string overlays region)
2727 `(let* ((,original-buffer (current-buffer))
2728 (,region (list (point-min) (point-max)))
2729 (,buffer-string (org-with-wide-buffer (buffer-string)))
2730 (,overlays (mapcar 'copy-overlay (apply 'overlays-in ,region))))
2731 (with-temp-buffer
2732 (let ((buffer-invisibility-spec nil))
2733 (org-clone-local-variables
2734 ,original-buffer
2735 "^\\(org-\\|orgtbl-\\|major-mode$\\|outline-\\(regexp\\|level\\)$\\)")
2736 (insert ,buffer-string)
2737 (apply 'narrow-to-region ,region)
2738 (mapc (lambda (ov)
2739 (move-overlay
2740 ov (overlay-start ov) (overlay-end ov) (current-buffer)))
2741 ,overlays)
2742 (goto-char (point-min))
2743 (progn ,@body))))))
2745 (defun org-export-expand-macro (info)
2746 "Expand every macro in buffer.
2747 INFO is a plist containing export options and buffer properties."
2748 ;; First update macro templates since #+INCLUDE keywords might have
2749 ;; added some new ones.
2750 (org-macro-initialize-templates)
2751 (org-macro-replace-all
2752 ;; Before expanding macros, install {{{author}}}, {{{date}}},
2753 ;; {{{email}}} and {{{title}}} templates.
2754 (nconc
2755 (list (cons "author"
2756 (org-element-interpret-data (plist-get info :author)))
2757 (cons "date"
2758 (org-element-interpret-data (plist-get info :date)))
2759 ;; EMAIL is not a parsed keyword: store it as-is.
2760 (cons "email" (or (plist-get info :email) ""))
2761 (cons "title"
2762 (org-element-interpret-data (plist-get info :title))))
2763 org-macro-templates)))
2765 (defun org-export-expand-include-keyword (&optional included dir)
2766 "Expand every include keyword in buffer.
2767 Optional argument INCLUDED is a list of included file names along
2768 with their line restriction, when appropriate. It is used to
2769 avoid infinite recursion. Optional argument DIR is the current
2770 working directory. It is used to properly resolve relative
2771 paths."
2772 (let ((case-fold-search t))
2773 (goto-char (point-min))
2774 (while (re-search-forward "^[ \t]*#\\+INCLUDE: +\\(.*\\)[ \t]*$" nil t)
2775 (when (eq (org-element-type (save-match-data (org-element-at-point)))
2776 'keyword)
2777 (beginning-of-line)
2778 ;; Extract arguments from keyword's value.
2779 (let* ((value (match-string 1))
2780 (ind (org-get-indentation))
2781 (file (and (string-match "^\"\\(\\S-+\\)\"" value)
2782 (prog1 (expand-file-name (match-string 1 value) dir)
2783 (setq value (replace-match "" nil nil value)))))
2784 (lines
2785 (and (string-match
2786 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\"" value)
2787 (prog1 (match-string 1 value)
2788 (setq value (replace-match "" nil nil value)))))
2789 (env (cond ((string-match "\\<example\\>" value) 'example)
2790 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
2791 (match-string 1 value))))
2792 ;; Minimal level of included file defaults to the child
2793 ;; level of the current headline, if any, or one. It
2794 ;; only applies is the file is meant to be included as
2795 ;; an Org one.
2796 (minlevel
2797 (and (not env)
2798 (if (string-match ":minlevel +\\([0-9]+\\)" value)
2799 (prog1 (string-to-number (match-string 1 value))
2800 (setq value (replace-match "" nil nil value)))
2801 (let ((cur (org-current-level)))
2802 (if cur (1+ (org-reduced-level cur)) 1))))))
2803 ;; Remove keyword.
2804 (delete-region (point) (progn (forward-line) (point)))
2805 (cond
2806 ((not file) (error "Invalid syntax in INCLUDE keyword"))
2807 ((not (file-readable-p file)) (error "Cannot include file %s" file))
2808 ;; Check if files has already been parsed. Look after
2809 ;; inclusion lines too, as different parts of the same file
2810 ;; can be included too.
2811 ((member (list file lines) included)
2812 (error "Recursive file inclusion: %s" file))
2814 (cond
2815 ((eq env 'example)
2816 (insert
2817 (let ((ind-str (make-string ind ? ))
2818 (contents
2819 (org-escape-code-in-string
2820 (org-export--prepare-file-contents file lines))))
2821 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
2822 ind-str contents ind-str))))
2823 ((stringp env)
2824 (insert
2825 (let ((ind-str (make-string ind ? ))
2826 (contents
2827 (org-escape-code-in-string
2828 (org-export--prepare-file-contents file lines))))
2829 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
2830 ind-str env contents ind-str))))
2832 (insert
2833 (with-temp-buffer
2834 (org-mode)
2835 (insert
2836 (org-export--prepare-file-contents file lines ind minlevel))
2837 (org-export-expand-include-keyword
2838 (cons (list file lines) included)
2839 (file-name-directory file))
2840 (buffer-string))))))))))))
2842 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
2843 "Prepare the contents of FILE for inclusion and return them as a string.
2845 When optional argument LINES is a string specifying a range of
2846 lines, include only those lines.
2848 Optional argument IND, when non-nil, is an integer specifying the
2849 global indentation of returned contents. Since its purpose is to
2850 allow an included file to stay in the same environment it was
2851 created \(i.e. a list item), it doesn't apply past the first
2852 headline encountered.
2854 Optional argument MINLEVEL, when non-nil, is an integer
2855 specifying the level that any top-level headline in the included
2856 file should have."
2857 (with-temp-buffer
2858 (insert-file-contents file)
2859 (when lines
2860 (let* ((lines (split-string lines "-"))
2861 (lbeg (string-to-number (car lines)))
2862 (lend (string-to-number (cadr lines)))
2863 (beg (if (zerop lbeg) (point-min)
2864 (goto-char (point-min))
2865 (forward-line (1- lbeg))
2866 (point)))
2867 (end (if (zerop lend) (point-max)
2868 (goto-char (point-min))
2869 (forward-line (1- lend))
2870 (point))))
2871 (narrow-to-region beg end)))
2872 ;; Remove blank lines at beginning and end of contents. The logic
2873 ;; behind that removal is that blank lines around include keyword
2874 ;; override blank lines in included file.
2875 (goto-char (point-min))
2876 (org-skip-whitespace)
2877 (beginning-of-line)
2878 (delete-region (point-min) (point))
2879 (goto-char (point-max))
2880 (skip-chars-backward " \r\t\n")
2881 (forward-line)
2882 (delete-region (point) (point-max))
2883 ;; If IND is set, preserve indentation of include keyword until
2884 ;; the first headline encountered.
2885 (when ind
2886 (unless (eq major-mode 'org-mode) (org-mode))
2887 (goto-char (point-min))
2888 (let ((ind-str (make-string ind ? )))
2889 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
2890 ;; Do not move footnote definitions out of column 0.
2891 (unless (and (looking-at org-footnote-definition-re)
2892 (eq (org-element-type (org-element-at-point))
2893 'footnote-definition))
2894 (insert ind-str))
2895 (forward-line))))
2896 ;; When MINLEVEL is specified, compute minimal level for headlines
2897 ;; in the file (CUR-MIN), and remove stars to each headline so
2898 ;; that headlines with minimal level have a level of MINLEVEL.
2899 (when minlevel
2900 (unless (eq major-mode 'org-mode) (org-mode))
2901 (org-with-limited-levels
2902 (let ((levels (org-map-entries
2903 (lambda () (org-reduced-level (org-current-level))))))
2904 (when levels
2905 (let ((offset (- minlevel (apply 'min levels))))
2906 (unless (zerop offset)
2907 (when org-odd-levels-only (setq offset (* offset 2)))
2908 ;; Only change stars, don't bother moving whole
2909 ;; sections.
2910 (org-map-entries
2911 (lambda () (if (< offset 0) (delete-char (abs offset))
2912 (insert (make-string offset ?*)))))))))))
2913 (buffer-string)))
2916 ;;; Tools For Back-Ends
2918 ;; A whole set of tools is available to help build new exporters. Any
2919 ;; function general enough to have its use across many back-ends
2920 ;; should be added here.
2922 ;;;; For Affiliated Keywords
2924 ;; `org-export-read-attribute' reads a property from a given element
2925 ;; as a plist. It can be used to normalize affiliated keywords'
2926 ;; syntax.
2928 ;; Since captions can span over multiple lines and accept dual values,
2929 ;; their internal representation is a bit tricky. Therefore,
2930 ;; `org-export-get-caption' transparently returns a given element's
2931 ;; caption as a secondary string.
2933 (defun org-export-read-attribute (attribute element &optional property)
2934 "Turn ATTRIBUTE property from ELEMENT into a plist.
2936 When optional argument PROPERTY is non-nil, return the value of
2937 that property within attributes.
2939 This function assumes attributes are defined as \":keyword
2940 value\" pairs. It is appropriate for `:attr_html' like
2941 properties."
2942 (let ((attributes
2943 (let ((value (org-element-property attribute element)))
2944 (and value
2945 (read (format "(%s)" (mapconcat 'identity value " ")))))))
2946 (if property (plist-get attributes property) attributes)))
2948 (defun org-export-get-caption (element &optional shortp)
2949 "Return caption from ELEMENT as a secondary string.
2951 When optional argument SHORTP is non-nil, return short caption,
2952 as a secondary string, instead.
2954 Caption lines are separated by a white space."
2955 (let ((full-caption (org-element-property :caption element)) caption)
2956 (dolist (line full-caption (cdr caption))
2957 (let ((cap (funcall (if shortp 'cdr 'car) line)))
2958 (when cap
2959 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
2962 ;;;; For Export Snippets
2964 ;; Every export snippet is transmitted to the back-end. Though, the
2965 ;; latter will only retain one type of export-snippet, ignoring
2966 ;; others, based on the former's target back-end. The function
2967 ;; `org-export-snippet-backend' returns that back-end for a given
2968 ;; export-snippet.
2970 (defun org-export-snippet-backend (export-snippet)
2971 "Return EXPORT-SNIPPET targeted back-end as a symbol.
2972 Translation, with `org-export-snippet-translation-alist', is
2973 applied."
2974 (let ((back-end (org-element-property :back-end export-snippet)))
2975 (intern
2976 (or (cdr (assoc back-end org-export-snippet-translation-alist))
2977 back-end))))
2980 ;;;; For Footnotes
2982 ;; `org-export-collect-footnote-definitions' is a tool to list
2983 ;; actually used footnotes definitions in the whole parse tree, or in
2984 ;; an headline, in order to add footnote listings throughout the
2985 ;; transcoded data.
2987 ;; `org-export-footnote-first-reference-p' is a predicate used by some
2988 ;; back-ends, when they need to attach the footnote definition only to
2989 ;; the first occurrence of the corresponding label.
2991 ;; `org-export-get-footnote-definition' and
2992 ;; `org-export-get-footnote-number' provide easier access to
2993 ;; additional information relative to a footnote reference.
2995 (defun org-export-collect-footnote-definitions (data info)
2996 "Return an alist between footnote numbers, labels and definitions.
2998 DATA is the parse tree from which definitions are collected.
2999 INFO is the plist used as a communication channel.
3001 Definitions are sorted by order of references. They either
3002 appear as Org data or as a secondary string for inlined
3003 footnotes. Unreferenced definitions are ignored."
3004 (let* (num-alist
3005 collect-fn ; for byte-compiler.
3006 (collect-fn
3007 (function
3008 (lambda (data)
3009 ;; Collect footnote number, label and definition in DATA.
3010 (org-element-map
3011 data 'footnote-reference
3012 (lambda (fn)
3013 (when (org-export-footnote-first-reference-p fn info)
3014 (let ((def (org-export-get-footnote-definition fn info)))
3015 (push
3016 (list (org-export-get-footnote-number fn info)
3017 (org-element-property :label fn)
3018 def)
3019 num-alist)
3020 ;; Also search in definition for nested footnotes.
3021 (when (eq (org-element-property :type fn) 'standard)
3022 (funcall collect-fn def)))))
3023 ;; Don't enter footnote definitions since it will happen
3024 ;; when their first reference is found.
3025 info nil 'footnote-definition)))))
3026 (funcall collect-fn (plist-get info :parse-tree))
3027 (reverse num-alist)))
3029 (defun org-export-footnote-first-reference-p (footnote-reference info)
3030 "Non-nil when a footnote reference is the first one for its label.
3032 FOOTNOTE-REFERENCE is the footnote reference being considered.
3033 INFO is the plist used as a communication channel."
3034 (let ((label (org-element-property :label footnote-reference)))
3035 ;; Anonymous footnotes are always a first reference.
3036 (if (not label) t
3037 ;; Otherwise, return the first footnote with the same LABEL and
3038 ;; test if it is equal to FOOTNOTE-REFERENCE.
3039 (let* (search-refs ; for byte-compiler.
3040 (search-refs
3041 (function
3042 (lambda (data)
3043 (org-element-map
3044 data 'footnote-reference
3045 (lambda (fn)
3046 (cond
3047 ((string= (org-element-property :label fn) label)
3048 (throw 'exit fn))
3049 ;; If FN isn't inlined, be sure to traverse its
3050 ;; definition before resuming search. See
3051 ;; comments in `org-export-get-footnote-number'
3052 ;; for more information.
3053 ((eq (org-element-property :type fn) 'standard)
3054 (funcall search-refs
3055 (org-export-get-footnote-definition fn info)))))
3056 ;; Don't enter footnote definitions since it will
3057 ;; happen when their first reference is found.
3058 info 'first-match 'footnote-definition)))))
3059 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3060 footnote-reference)))))
3062 (defun org-export-get-footnote-definition (footnote-reference info)
3063 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3064 INFO is the plist used as a communication channel."
3065 (let ((label (org-element-property :label footnote-reference)))
3066 (or (org-element-property :inline-definition footnote-reference)
3067 (cdr (assoc label (plist-get info :footnote-definition-alist))))))
3069 (defun org-export-get-footnote-number (footnote info)
3070 "Return number associated to a footnote.
3072 FOOTNOTE is either a footnote reference or a footnote definition.
3073 INFO is the plist used as a communication channel."
3074 (let* ((label (org-element-property :label footnote))
3075 seen-refs
3076 search-ref ; For byte-compiler.
3077 (search-ref
3078 (function
3079 (lambda (data)
3080 ;; Search footnote references through DATA, filling
3081 ;; SEEN-REFS along the way.
3082 (org-element-map
3083 data 'footnote-reference
3084 (lambda (fn)
3085 (let ((fn-lbl (org-element-property :label fn)))
3086 (cond
3087 ;; Anonymous footnote match: return number.
3088 ((and (not fn-lbl) (eq fn footnote))
3089 (throw 'exit (1+ (length seen-refs))))
3090 ;; Labels match: return number.
3091 ((and label (string= label fn-lbl))
3092 (throw 'exit (1+ (length seen-refs))))
3093 ;; Anonymous footnote: it's always a new one. Also,
3094 ;; be sure to return nil from the `cond' so
3095 ;; `first-match' doesn't get us out of the loop.
3096 ((not fn-lbl) (push 'inline seen-refs) nil)
3097 ;; Label not seen so far: add it so SEEN-REFS.
3099 ;; Also search for subsequent references in
3100 ;; footnote definition so numbering follows reading
3101 ;; logic. Note that we don't have to care about
3102 ;; inline definitions, since `org-element-map'
3103 ;; already traverses them at the right time.
3105 ;; Once again, return nil to stay in the loop.
3106 ((not (member fn-lbl seen-refs))
3107 (push fn-lbl seen-refs)
3108 (funcall search-ref
3109 (org-export-get-footnote-definition fn info))
3110 nil))))
3111 ;; Don't enter footnote definitions since it will happen
3112 ;; when their first reference is found.
3113 info 'first-match 'footnote-definition)))))
3114 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3117 ;;;; For Headlines
3119 ;; `org-export-get-relative-level' is a shortcut to get headline
3120 ;; level, relatively to the lower headline level in the parsed tree.
3122 ;; `org-export-get-headline-number' returns the section number of an
3123 ;; headline, while `org-export-number-to-roman' allows to convert it
3124 ;; to roman numbers.
3126 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3127 ;; `org-export-last-sibling-p' are three useful predicates when it
3128 ;; comes to fulfill the `:headline-levels' property.
3130 (defun org-export-get-relative-level (headline info)
3131 "Return HEADLINE relative level within current parsed tree.
3132 INFO is a plist holding contextual information."
3133 (+ (org-element-property :level headline)
3134 (or (plist-get info :headline-offset) 0)))
3136 (defun org-export-low-level-p (headline info)
3137 "Non-nil when HEADLINE is considered as low level.
3139 INFO is a plist used as a communication channel.
3141 A low level headlines has a relative level greater than
3142 `:headline-levels' property value.
3144 Return value is the difference between HEADLINE relative level
3145 and the last level being considered as high enough, or nil."
3146 (let ((limit (plist-get info :headline-levels)))
3147 (when (wholenump limit)
3148 (let ((level (org-export-get-relative-level headline info)))
3149 (and (> level limit) (- level limit))))))
3151 (defun org-export-get-headline-number (headline info)
3152 "Return HEADLINE numbering as a list of numbers.
3153 INFO is a plist holding contextual information."
3154 (cdr (assoc headline (plist-get info :headline-numbering))))
3156 (defun org-export-numbered-headline-p (headline info)
3157 "Return a non-nil value if HEADLINE element should be numbered.
3158 INFO is a plist used as a communication channel."
3159 (let ((sec-num (plist-get info :section-numbers))
3160 (level (org-export-get-relative-level headline info)))
3161 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3163 (defun org-export-number-to-roman (n)
3164 "Convert integer N into a roman numeral."
3165 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3166 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3167 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3168 ( 1 . "I")))
3169 (res ""))
3170 (if (<= n 0)
3171 (number-to-string n)
3172 (while roman
3173 (if (>= n (caar roman))
3174 (setq n (- n (caar roman))
3175 res (concat res (cdar roman)))
3176 (pop roman)))
3177 res)))
3179 (defun org-export-get-tags (element info &optional tags inherited)
3180 "Return list of tags associated to ELEMENT.
3182 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3183 is a plist used as a communication channel.
3185 Select tags (see `org-export-select-tags') and exclude tags (see
3186 `org-export-exclude-tags') are removed from the list.
3188 When non-nil, optional argument TAGS should be a list of strings.
3189 Any tag belonging to this list will also be removed.
3191 When optional argument INHERITED is non-nil, tags can also be
3192 inherited from parent headlines.."
3193 (org-remove-if
3194 (lambda (tag) (or (member tag (plist-get info :select-tags))
3195 (member tag (plist-get info :exclude-tags))
3196 (member tag tags)))
3197 (if (not inherited) (org-element-property :tags element)
3198 ;; Build complete list of inherited tags.
3199 (let ((current-tag-list (org-element-property :tags element)))
3200 (mapc
3201 (lambda (parent)
3202 (mapc
3203 (lambda (tag)
3204 (when (and (memq (org-element-type parent) '(headline inlinetask))
3205 (not (member tag current-tag-list)))
3206 (push tag current-tag-list)))
3207 (org-element-property :tags parent)))
3208 (org-export-get-genealogy element))
3209 current-tag-list))))
3211 (defun org-export-get-node-property (property blob &optional inherited)
3212 "Return node PROPERTY value for BLOB.
3214 PROPERTY is normalized symbol (i.e. `:cookie-data'). BLOB is an
3215 element or object.
3217 If optional argument INHERITED is non-nil, the value can be
3218 inherited from a parent headline.
3220 Return value is a string or nil."
3221 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3222 (org-export-get-parent-headline blob))))
3223 (if (not inherited) (org-element-property property blob)
3224 (let ((parent headline) value)
3225 (catch 'found
3226 (while parent
3227 (when (plist-member (nth 1 parent) property)
3228 (throw 'found (org-element-property property parent)))
3229 (setq parent (org-element-property :parent parent))))))))
3231 (defun org-export-first-sibling-p (headline info)
3232 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3233 INFO is a plist used as a communication channel."
3234 (not (eq (org-element-type (org-export-get-previous-element headline info))
3235 'headline)))
3237 (defun org-export-last-sibling-p (headline info)
3238 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3239 INFO is a plist used as a communication channel."
3240 (not (org-export-get-next-element headline info)))
3243 ;;;; For Links
3245 ;; `org-export-solidify-link-text' turns a string into a safer version
3246 ;; for links, replacing most non-standard characters with hyphens.
3248 ;; `org-export-get-coderef-format' returns an appropriate format
3249 ;; string for coderefs.
3251 ;; `org-export-inline-image-p' returns a non-nil value when the link
3252 ;; provided should be considered as an inline image.
3254 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3255 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3256 ;; returns an appropriate unique identifier when found, or nil.
3258 ;; `org-export-resolve-id-link' returns the first headline with
3259 ;; specified id or custom-id in parse tree, the path to the external
3260 ;; file with the id or nil when neither was found.
3262 ;; `org-export-resolve-coderef' associates a reference to a line
3263 ;; number in the element it belongs, or returns the reference itself
3264 ;; when the element isn't numbered.
3266 (defun org-export-solidify-link-text (s)
3267 "Take link text S and make a safe target out of it."
3268 (save-match-data
3269 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-]+") "-")))
3271 (defun org-export-get-coderef-format (path desc)
3272 "Return format string for code reference link.
3273 PATH is the link path. DESC is its description."
3274 (save-match-data
3275 (cond ((not desc) "%s")
3276 ((string-match (regexp-quote (concat "(" path ")")) desc)
3277 (replace-match "%s" t t desc))
3278 (t desc))))
3280 (defun org-export-inline-image-p (link &optional rules)
3281 "Non-nil if LINK object points to an inline image.
3283 Optional argument is a set of RULES defining inline images. It
3284 is an alist where associations have the following shape:
3286 \(TYPE . REGEXP)
3288 Applying a rule means apply REGEXP against LINK's path when its
3289 type is TYPE. The function will return a non-nil value if any of
3290 the provided rules is non-nil. The default rule is
3291 `org-export-default-inline-image-rule'.
3293 This only applies to links without a description."
3294 (and (not (org-element-contents link))
3295 (let ((case-fold-search t)
3296 (rules (or rules org-export-default-inline-image-rule)))
3297 (catch 'exit
3298 (mapc
3299 (lambda (rule)
3300 (and (string= (org-element-property :type link) (car rule))
3301 (string-match (cdr rule)
3302 (org-element-property :path link))
3303 (throw 'exit t)))
3304 rules)
3305 ;; Return nil if no rule matched.
3306 nil))))
3308 (defun org-export-resolve-coderef (ref info)
3309 "Resolve a code reference REF.
3311 INFO is a plist used as a communication channel.
3313 Return associated line number in source code, or REF itself,
3314 depending on src-block or example element's switches."
3315 (org-element-map
3316 (plist-get info :parse-tree) '(example-block src-block)
3317 (lambda (el)
3318 (with-temp-buffer
3319 (insert (org-trim (org-element-property :value el)))
3320 (let* ((label-fmt (regexp-quote
3321 (or (org-element-property :label-fmt el)
3322 org-coderef-label-format)))
3323 (ref-re
3324 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3325 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3326 ;; Element containing REF is found. Resolve it to either
3327 ;; a label or a line number, as needed.
3328 (when (re-search-backward ref-re nil t)
3329 (cond
3330 ((org-element-property :use-labels el) ref)
3331 ((eq (org-element-property :number-lines el) 'continued)
3332 (+ (org-export-get-loc el info) (line-number-at-pos)))
3333 (t (line-number-at-pos)))))))
3334 info 'first-match))
3336 (defun org-export-resolve-fuzzy-link (link info)
3337 "Return LINK destination.
3339 INFO is a plist holding contextual information.
3341 Return value can be an object, an element, or nil:
3343 - If LINK path matches a target object (i.e. <<path>>) or
3344 element (i.e. \"#+TARGET: path\"), return it.
3346 - If LINK path exactly matches the name affiliated keyword
3347 \(i.e. #+NAME: path) of an element, return that element.
3349 - If LINK path exactly matches any headline name, return that
3350 element. If more than one headline share that name, priority
3351 will be given to the one with the closest common ancestor, if
3352 any, or the first one in the parse tree otherwise.
3354 - Otherwise, return nil.
3356 Assume LINK type is \"fuzzy\"."
3357 (let* ((path (org-element-property :path link))
3358 (match-title-p (eq (aref path 0) ?*)))
3359 (cond
3360 ;; First try to find a matching "<<path>>" unless user specified
3361 ;; he was looking for an headline (path starts with a *
3362 ;; character).
3363 ((and (not match-title-p)
3364 (loop for target in (plist-get info :target-list)
3365 when (string= (org-element-property :value target) path)
3366 return target)))
3367 ;; Then try to find an element with a matching "#+NAME: path"
3368 ;; affiliated keyword.
3369 ((and (not match-title-p)
3370 (org-element-map
3371 (plist-get info :parse-tree) org-element-all-elements
3372 (lambda (el)
3373 (when (string= (org-element-property :name el) path) el))
3374 info 'first-match)))
3375 ;; Last case: link either points to an headline or to
3376 ;; nothingness. Try to find the source, with priority given to
3377 ;; headlines with the closest common ancestor. If such candidate
3378 ;; is found, return it, otherwise return nil.
3380 (let ((find-headline
3381 (function
3382 ;; Return first headline whose `:raw-value' property
3383 ;; is NAME in parse tree DATA, or nil.
3384 (lambda (name data)
3385 (org-element-map
3386 data 'headline
3387 (lambda (headline)
3388 (when (string=
3389 (org-element-property :raw-value headline)
3390 name)
3391 headline))
3392 info 'first-match)))))
3393 ;; Search among headlines sharing an ancestor with link,
3394 ;; from closest to farthest.
3395 (or (catch 'exit
3396 (mapc
3397 (lambda (parent)
3398 (when (eq (org-element-type parent) 'headline)
3399 (let ((foundp (funcall find-headline path parent)))
3400 (when foundp (throw 'exit foundp)))))
3401 (org-export-get-genealogy link)) nil)
3402 ;; No match with a common ancestor: try the full parse-tree.
3403 (funcall find-headline
3404 (if match-title-p (substring path 1) path)
3405 (plist-get info :parse-tree))))))))
3407 (defun org-export-resolve-id-link (link info)
3408 "Return headline referenced as LINK destination.
3410 INFO is a plist used as a communication channel.
3412 Return value can be the headline element matched in current parse
3413 tree, a file name or nil. Assume LINK type is either \"id\" or
3414 \"custom-id\"."
3415 (let ((id (org-element-property :path link)))
3416 ;; First check if id is within the current parse tree.
3417 (or (org-element-map
3418 (plist-get info :parse-tree) 'headline
3419 (lambda (headline)
3420 (when (or (string= (org-element-property :id headline) id)
3421 (string= (org-element-property :custom-id headline) id))
3422 headline))
3423 info 'first-match)
3424 ;; Otherwise, look for external files.
3425 (cdr (assoc id (plist-get info :id-alist))))))
3427 (defun org-export-resolve-radio-link (link info)
3428 "Return radio-target object referenced as LINK destination.
3430 INFO is a plist used as a communication channel.
3432 Return value can be a radio-target object or nil. Assume LINK
3433 has type \"radio\"."
3434 (let ((path (org-element-property :path link)))
3435 (org-element-map
3436 (plist-get info :parse-tree) 'radio-target
3437 (lambda (radio)
3438 (when (equal (org-element-property :value radio) path) radio))
3439 info 'first-match)))
3442 ;;;; For References
3444 ;; `org-export-get-ordinal' associates a sequence number to any object
3445 ;; or element.
3447 (defun org-export-get-ordinal (element info &optional types predicate)
3448 "Return ordinal number of an element or object.
3450 ELEMENT is the element or object considered. INFO is the plist
3451 used as a communication channel.
3453 Optional argument TYPES, when non-nil, is a list of element or
3454 object types, as symbols, that should also be counted in.
3455 Otherwise, only provided element's type is considered.
3457 Optional argument PREDICATE is a function returning a non-nil
3458 value if the current element or object should be counted in. It
3459 accepts two arguments: the element or object being considered and
3460 the plist used as a communication channel. This allows to count
3461 only a certain type of objects (i.e. inline images).
3463 Return value is a list of numbers if ELEMENT is an headline or an
3464 item. It is nil for keywords. It represents the footnote number
3465 for footnote definitions and footnote references. If ELEMENT is
3466 a target, return the same value as if ELEMENT was the closest
3467 table, item or headline containing the target. In any other
3468 case, return the sequence number of ELEMENT among elements or
3469 objects of the same type."
3470 ;; A target keyword, representing an invisible target, never has
3471 ;; a sequence number.
3472 (unless (eq (org-element-type element) 'keyword)
3473 ;; Ordinal of a target object refer to the ordinal of the closest
3474 ;; table, item, or headline containing the object.
3475 (when (eq (org-element-type element) 'target)
3476 (setq element
3477 (loop for parent in (org-export-get-genealogy element)
3478 when
3479 (memq
3480 (org-element-type parent)
3481 '(footnote-definition footnote-reference headline item
3482 table))
3483 return parent)))
3484 (case (org-element-type element)
3485 ;; Special case 1: An headline returns its number as a list.
3486 (headline (org-export-get-headline-number element info))
3487 ;; Special case 2: An item returns its number as a list.
3488 (item (let ((struct (org-element-property :structure element)))
3489 (org-list-get-item-number
3490 (org-element-property :begin element)
3491 struct
3492 (org-list-prevs-alist struct)
3493 (org-list-parents-alist struct))))
3494 ((footnote-definition footnote-reference)
3495 (org-export-get-footnote-number element info))
3496 (otherwise
3497 (let ((counter 0))
3498 ;; Increment counter until ELEMENT is found again.
3499 (org-element-map
3500 (plist-get info :parse-tree) (or types (org-element-type element))
3501 (lambda (el)
3502 (cond
3503 ((eq element el) (1+ counter))
3504 ((not predicate) (incf counter) nil)
3505 ((funcall predicate el info) (incf counter) nil)))
3506 info 'first-match))))))
3509 ;;;; For Src-Blocks
3511 ;; `org-export-get-loc' counts number of code lines accumulated in
3512 ;; src-block or example-block elements with a "+n" switch until
3513 ;; a given element, excluded. Note: "-n" switches reset that count.
3515 ;; `org-export-unravel-code' extracts source code (along with a code
3516 ;; references alist) from an `element-block' or `src-block' type
3517 ;; element.
3519 ;; `org-export-format-code' applies a formatting function to each line
3520 ;; of code, providing relative line number and code reference when
3521 ;; appropriate. Since it doesn't access the original element from
3522 ;; which the source code is coming, it expects from the code calling
3523 ;; it to know if lines should be numbered and if code references
3524 ;; should appear.
3526 ;; Eventually, `org-export-format-code-default' is a higher-level
3527 ;; function (it makes use of the two previous functions) which handles
3528 ;; line numbering and code references inclusion, and returns source
3529 ;; code in a format suitable for plain text or verbatim output.
3531 (defun org-export-get-loc (element info)
3532 "Return accumulated lines of code up to ELEMENT.
3534 INFO is the plist used as a communication channel.
3536 ELEMENT is excluded from count."
3537 (let ((loc 0))
3538 (org-element-map
3539 (plist-get info :parse-tree)
3540 `(src-block example-block ,(org-element-type element))
3541 (lambda (el)
3542 (cond
3543 ;; ELEMENT is reached: Quit the loop.
3544 ((eq el element))
3545 ;; Only count lines from src-block and example-block elements
3546 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
3547 ((not (memq (org-element-type el) '(src-block example-block))) nil)
3548 ((let ((linums (org-element-property :number-lines el)))
3549 (when linums
3550 ;; Accumulate locs or reset them.
3551 (let ((lines (org-count-lines
3552 (org-trim (org-element-property :value el)))))
3553 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
3554 ;; Return nil to stay in the loop.
3555 nil)))
3556 info 'first-match)
3557 ;; Return value.
3558 loc))
3560 (defun org-export-unravel-code (element)
3561 "Clean source code and extract references out of it.
3563 ELEMENT has either a `src-block' an `example-block' type.
3565 Return a cons cell whose CAR is the source code, cleaned from any
3566 reference and protective comma and CDR is an alist between
3567 relative line number (integer) and name of code reference on that
3568 line (string)."
3569 (let* ((line 0) refs
3570 ;; Get code and clean it. Remove blank lines at its
3571 ;; beginning and end.
3572 (code (let ((c (replace-regexp-in-string
3573 "\\`\\([ \t]*\n\\)+" ""
3574 (replace-regexp-in-string
3575 "\\(:?[ \t]*\n\\)*[ \t]*\\'" "\n"
3576 (org-element-property :value element)))))
3577 ;; If appropriate, remove global indentation.
3578 (if (or org-src-preserve-indentation
3579 (org-element-property :preserve-indent element))
3581 (org-remove-indentation c))))
3582 ;; Get format used for references.
3583 (label-fmt (regexp-quote
3584 (or (org-element-property :label-fmt element)
3585 org-coderef-label-format)))
3586 ;; Build a regexp matching a loc with a reference.
3587 (with-ref-re
3588 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
3589 (replace-regexp-in-string
3590 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
3591 ;; Return value.
3592 (cons
3593 ;; Code with references removed.
3594 (org-element-normalize-string
3595 (mapconcat
3596 (lambda (loc)
3597 (incf line)
3598 (if (not (string-match with-ref-re loc)) loc
3599 ;; Ref line: remove ref, and signal its position in REFS.
3600 (push (cons line (match-string 3 loc)) refs)
3601 (replace-match "" nil nil loc 1)))
3602 (org-split-string code "\n") "\n"))
3603 ;; Reference alist.
3604 refs)))
3606 (defun org-export-format-code (code fun &optional num-lines ref-alist)
3607 "Format CODE by applying FUN line-wise and return it.
3609 CODE is a string representing the code to format. FUN is
3610 a function. It must accept three arguments: a line of
3611 code (string), the current line number (integer) or nil and the
3612 reference associated to the current line (string) or nil.
3614 Optional argument NUM-LINES can be an integer representing the
3615 number of code lines accumulated until the current code. Line
3616 numbers passed to FUN will take it into account. If it is nil,
3617 FUN's second argument will always be nil. This number can be
3618 obtained with `org-export-get-loc' function.
3620 Optional argument REF-ALIST can be an alist between relative line
3621 number (i.e. ignoring NUM-LINES) and the name of the code
3622 reference on it. If it is nil, FUN's third argument will always
3623 be nil. It can be obtained through the use of
3624 `org-export-unravel-code' function."
3625 (let ((--locs (org-split-string code "\n"))
3626 (--line 0))
3627 (org-element-normalize-string
3628 (mapconcat
3629 (lambda (--loc)
3630 (incf --line)
3631 (let ((--ref (cdr (assq --line ref-alist))))
3632 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
3633 --locs "\n"))))
3635 (defun org-export-format-code-default (element info)
3636 "Return source code from ELEMENT, formatted in a standard way.
3638 ELEMENT is either a `src-block' or `example-block' element. INFO
3639 is a plist used as a communication channel.
3641 This function takes care of line numbering and code references
3642 inclusion. Line numbers, when applicable, appear at the
3643 beginning of the line, separated from the code by two white
3644 spaces. Code references, on the other hand, appear flushed to
3645 the right, separated by six white spaces from the widest line of
3646 code."
3647 ;; Extract code and references.
3648 (let* ((code-info (org-export-unravel-code element))
3649 (code (car code-info))
3650 (code-lines (org-split-string code "\n"))
3651 (refs (and (org-element-property :retain-labels element)
3652 (cdr code-info)))
3653 ;; Handle line numbering.
3654 (num-start (case (org-element-property :number-lines element)
3655 (continued (org-export-get-loc element info))
3656 (new 0)))
3657 (num-fmt
3658 (and num-start
3659 (format "%%%ds "
3660 (length (number-to-string
3661 (+ (length code-lines) num-start))))))
3662 ;; Prepare references display, if required. Any reference
3663 ;; should start six columns after the widest line of code,
3664 ;; wrapped with parenthesis.
3665 (max-width
3666 (+ (apply 'max (mapcar 'length code-lines))
3667 (if (not num-start) 0 (length (format num-fmt num-start))))))
3668 (org-export-format-code
3669 code
3670 (lambda (loc line-num ref)
3671 (let ((number-str (and num-fmt (format num-fmt line-num))))
3672 (concat
3673 number-str
3675 (and ref
3676 (concat (make-string
3677 (- (+ 6 max-width)
3678 (+ (length loc) (length number-str))) ? )
3679 (format "(%s)" ref))))))
3680 num-start refs)))
3683 ;;;; For Tables
3685 ;; `org-export-table-has-special-column-p' and and
3686 ;; `org-export-table-row-is-special-p' are predicates used to look for
3687 ;; meta-information about the table structure.
3689 ;; `org-table-has-header-p' tells when the rows before the first rule
3690 ;; should be considered as table's header.
3692 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
3693 ;; and `org-export-table-cell-borders' extract information from
3694 ;; a table-cell element.
3696 ;; `org-export-table-dimensions' gives the number on rows and columns
3697 ;; in the table, ignoring horizontal rules and special columns.
3698 ;; `org-export-table-cell-address', given a table-cell object, returns
3699 ;; the absolute address of a cell. On the other hand,
3700 ;; `org-export-get-table-cell-at' does the contrary.
3702 ;; `org-export-table-cell-starts-colgroup-p',
3703 ;; `org-export-table-cell-ends-colgroup-p',
3704 ;; `org-export-table-row-starts-rowgroup-p',
3705 ;; `org-export-table-row-ends-rowgroup-p',
3706 ;; `org-export-table-row-starts-header-p' and
3707 ;; `org-export-table-row-ends-header-p' indicate position of current
3708 ;; row or cell within the table.
3710 (defun org-export-table-has-special-column-p (table)
3711 "Non-nil when TABLE has a special column.
3712 All special columns will be ignored during export."
3713 ;; The table has a special column when every first cell of every row
3714 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
3715 ;; "*" "_" and "^". Though, do not consider a first row containing
3716 ;; only empty cells as special.
3717 (let ((special-column-p 'empty))
3718 (catch 'exit
3719 (mapc
3720 (lambda (row)
3721 (when (eq (org-element-property :type row) 'standard)
3722 (let ((value (org-element-contents
3723 (car (org-element-contents row)))))
3724 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
3725 (setq special-column-p 'special))
3726 ((not value))
3727 (t (throw 'exit nil))))))
3728 (org-element-contents table))
3729 (eq special-column-p 'special))))
3731 (defun org-export-table-has-header-p (table info)
3732 "Non-nil when TABLE has an header.
3734 INFO is a plist used as a communication channel.
3736 A table has an header when it contains at least two row groups."
3737 (let ((rowgroup 1) row-flag)
3738 (org-element-map
3739 table 'table-row
3740 (lambda (row)
3741 (cond
3742 ((> rowgroup 1) t)
3743 ((and row-flag (eq (org-element-property :type row) 'rule))
3744 (incf rowgroup) (setq row-flag nil))
3745 ((and (not row-flag) (eq (org-element-property :type row) 'standard))
3746 (setq row-flag t) nil)))
3747 info)))
3749 (defun org-export-table-row-is-special-p (table-row info)
3750 "Non-nil if TABLE-ROW is considered special.
3752 INFO is a plist used as the communication channel.
3754 All special rows will be ignored during export."
3755 (when (eq (org-element-property :type table-row) 'standard)
3756 (let ((first-cell (org-element-contents
3757 (car (org-element-contents table-row)))))
3758 ;; A row is special either when...
3760 ;; ... it starts with a field only containing "/",
3761 (equal first-cell '("/"))
3762 ;; ... the table contains a special column and the row start
3763 ;; with a marking character among, "^", "_", "$" or "!",
3764 (and (org-export-table-has-special-column-p
3765 (org-export-get-parent table-row))
3766 (member first-cell '(("^") ("_") ("$") ("!"))))
3767 ;; ... it contains only alignment cookies and empty cells.
3768 (let ((special-row-p 'empty))
3769 (catch 'exit
3770 (mapc
3771 (lambda (cell)
3772 (let ((value (org-element-contents cell)))
3773 ;; Since VALUE is a secondary string, the following
3774 ;; checks avoid expanding it with `org-export-data'.
3775 (cond ((not value))
3776 ((and (not (cdr value))
3777 (stringp (car value))
3778 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
3779 (car value)))
3780 (setq special-row-p 'cookie))
3781 (t (throw 'exit nil)))))
3782 (org-element-contents table-row))
3783 (eq special-row-p 'cookie)))))))
3785 (defun org-export-table-row-group (table-row info)
3786 "Return TABLE-ROW's group.
3788 INFO is a plist used as the communication channel.
3790 Return value is the group number, as an integer, or nil special
3791 rows and table rules. Group 1 is also table's header."
3792 (unless (or (eq (org-element-property :type table-row) 'rule)
3793 (org-export-table-row-is-special-p table-row info))
3794 (let ((group 0) row-flag)
3795 (catch 'found
3796 (mapc
3797 (lambda (row)
3798 (cond
3799 ((and (eq (org-element-property :type row) 'standard)
3800 (not (org-export-table-row-is-special-p row info)))
3801 (unless row-flag (incf group) (setq row-flag t)))
3802 ((eq (org-element-property :type row) 'rule)
3803 (setq row-flag nil)))
3804 (when (eq table-row row) (throw 'found group)))
3805 (org-element-contents (org-export-get-parent table-row)))))))
3807 (defun org-export-table-cell-width (table-cell info)
3808 "Return TABLE-CELL contents width.
3810 INFO is a plist used as the communication channel.
3812 Return value is the width given by the last width cookie in the
3813 same column as TABLE-CELL, or nil."
3814 (let* ((row (org-export-get-parent table-cell))
3815 (column (let ((cells (org-element-contents row)))
3816 (- (length cells) (length (memq table-cell cells)))))
3817 (table (org-export-get-parent-table table-cell))
3818 cookie-width)
3819 (mapc
3820 (lambda (row)
3821 (cond
3822 ;; In a special row, try to find a width cookie at COLUMN.
3823 ((org-export-table-row-is-special-p row info)
3824 (let ((value (org-element-contents
3825 (elt (org-element-contents row) column))))
3826 ;; The following checks avoid expanding unnecessarily the
3827 ;; cell with `org-export-data'
3828 (when (and value
3829 (not (cdr value))
3830 (stringp (car value))
3831 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" (car value))
3832 (match-string 1 (car value)))
3833 (setq cookie-width
3834 (string-to-number (match-string 1 (car value)))))))
3835 ;; Ignore table rules.
3836 ((eq (org-element-property :type row) 'rule))))
3837 (org-element-contents table))
3838 ;; Return value.
3839 cookie-width))
3841 (defun org-export-table-cell-alignment (table-cell info)
3842 "Return TABLE-CELL contents alignment.
3844 INFO is a plist used as the communication channel.
3846 Return alignment as specified by the last alignment cookie in the
3847 same column as TABLE-CELL. If no such cookie is found, a default
3848 alignment value will be deduced from fraction of numbers in the
3849 column (see `org-table-number-fraction' for more information).
3850 Possible values are `left', `right' and `center'."
3851 (let* ((row (org-export-get-parent table-cell))
3852 (column (let ((cells (org-element-contents row)))
3853 (- (length cells) (length (memq table-cell cells)))))
3854 (table (org-export-get-parent-table table-cell))
3855 (number-cells 0)
3856 (total-cells 0)
3857 cookie-align)
3858 (mapc
3859 (lambda (row)
3860 (cond
3861 ;; In a special row, try to find an alignment cookie at
3862 ;; COLUMN.
3863 ((org-export-table-row-is-special-p row info)
3864 (let ((value (org-element-contents
3865 (elt (org-element-contents row) column))))
3866 ;; Since VALUE is a secondary string, the following checks
3867 ;; avoid useless expansion through `org-export-data'.
3868 (when (and value
3869 (not (cdr value))
3870 (stringp (car value))
3871 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
3872 (car value))
3873 (match-string 1 (car value)))
3874 (setq cookie-align (match-string 1 (car value))))))
3875 ;; Ignore table rules.
3876 ((eq (org-element-property :type row) 'rule))
3877 ;; In a standard row, check if cell's contents are expressing
3878 ;; some kind of number. Increase NUMBER-CELLS accordingly.
3879 ;; Though, don't bother if an alignment cookie has already
3880 ;; defined cell's alignment.
3881 ((not cookie-align)
3882 (let ((value (org-export-data
3883 (org-element-contents
3884 (elt (org-element-contents row) column))
3885 info)))
3886 (incf total-cells)
3887 (when (string-match org-table-number-regexp value)
3888 (incf number-cells))))))
3889 (org-element-contents table))
3890 ;; Return value. Alignment specified by cookies has precedence
3891 ;; over alignment deduced from cells contents.
3892 (cond ((equal cookie-align "l") 'left)
3893 ((equal cookie-align "r") 'right)
3894 ((equal cookie-align "c") 'center)
3895 ((>= (/ (float number-cells) total-cells) org-table-number-fraction)
3896 'right)
3897 (t 'left))))
3899 (defun org-export-table-cell-borders (table-cell info)
3900 "Return TABLE-CELL borders.
3902 INFO is a plist used as a communication channel.
3904 Return value is a list of symbols, or nil. Possible values are:
3905 `top', `bottom', `above', `below', `left' and `right'. Note:
3906 `top' (resp. `bottom') only happen for a cell in the first
3907 row (resp. last row) of the table, ignoring table rules, if any.
3909 Returned borders ignore special rows."
3910 (let* ((row (org-export-get-parent table-cell))
3911 (table (org-export-get-parent-table table-cell))
3912 borders)
3913 ;; Top/above border? TABLE-CELL has a border above when a rule
3914 ;; used to demarcate row groups can be found above. Hence,
3915 ;; finding a rule isn't sufficient to push `above' in BORDERS:
3916 ;; another regular row has to be found above that rule.
3917 (let (rule-flag)
3918 (catch 'exit
3919 (mapc (lambda (row)
3920 (cond ((eq (org-element-property :type row) 'rule)
3921 (setq rule-flag t))
3922 ((not (org-export-table-row-is-special-p row info))
3923 (if rule-flag (throw 'exit (push 'above borders))
3924 (throw 'exit nil)))))
3925 ;; Look at every row before the current one.
3926 (cdr (memq row (reverse (org-element-contents table)))))
3927 ;; No rule above, or rule found starts the table (ignoring any
3928 ;; special row): TABLE-CELL is at the top of the table.
3929 (when rule-flag (push 'above borders))
3930 (push 'top borders)))
3931 ;; Bottom/below border? TABLE-CELL has a border below when next
3932 ;; non-regular row below is a rule.
3933 (let (rule-flag)
3934 (catch 'exit
3935 (mapc (lambda (row)
3936 (cond ((eq (org-element-property :type row) 'rule)
3937 (setq rule-flag t))
3938 ((not (org-export-table-row-is-special-p row info))
3939 (if rule-flag (throw 'exit (push 'below borders))
3940 (throw 'exit nil)))))
3941 ;; Look at every row after the current one.
3942 (cdr (memq row (org-element-contents table))))
3943 ;; No rule below, or rule found ends the table (modulo some
3944 ;; special row): TABLE-CELL is at the bottom of the table.
3945 (when rule-flag (push 'below borders))
3946 (push 'bottom borders)))
3947 ;; Right/left borders? They can only be specified by column
3948 ;; groups. Column groups are defined in a row starting with "/".
3949 ;; Also a column groups row only contains "<", "<>", ">" or blank
3950 ;; cells.
3951 (catch 'exit
3952 (let ((column (let ((cells (org-element-contents row)))
3953 (- (length cells) (length (memq table-cell cells))))))
3954 (mapc
3955 (lambda (row)
3956 (unless (eq (org-element-property :type row) 'rule)
3957 (when (equal (org-element-contents
3958 (car (org-element-contents row)))
3959 '("/"))
3960 (let ((column-groups
3961 (mapcar
3962 (lambda (cell)
3963 (let ((value (org-element-contents cell)))
3964 (when (member value '(("<") ("<>") (">") nil))
3965 (car value))))
3966 (org-element-contents row))))
3967 ;; There's a left border when previous cell, if
3968 ;; any, ends a group, or current one starts one.
3969 (when (or (and (not (zerop column))
3970 (member (elt column-groups (1- column))
3971 '(">" "<>")))
3972 (member (elt column-groups column) '("<" "<>")))
3973 (push 'left borders))
3974 ;; There's a right border when next cell, if any,
3975 ;; starts a group, or current one ends one.
3976 (when (or (and (/= (1+ column) (length column-groups))
3977 (member (elt column-groups (1+ column))
3978 '("<" "<>")))
3979 (member (elt column-groups column) '(">" "<>")))
3980 (push 'right borders))
3981 (throw 'exit nil)))))
3982 ;; Table rows are read in reverse order so last column groups
3983 ;; row has precedence over any previous one.
3984 (reverse (org-element-contents table)))))
3985 ;; Return value.
3986 borders))
3988 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
3989 "Non-nil when TABLE-CELL is at the beginning of a row group.
3990 INFO is a plist used as a communication channel."
3991 ;; A cell starts a column group either when it is at the beginning
3992 ;; of a row (or after the special column, if any) or when it has
3993 ;; a left border.
3994 (or (eq (org-element-map
3995 (org-export-get-parent table-cell)
3996 'table-cell 'identity info 'first-match)
3997 table-cell)
3998 (memq 'left (org-export-table-cell-borders table-cell info))))
4000 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4001 "Non-nil when TABLE-CELL is at the end of a row group.
4002 INFO is a plist used as a communication channel."
4003 ;; A cell ends a column group either when it is at the end of a row
4004 ;; or when it has a right border.
4005 (or (eq (car (last (org-element-contents
4006 (org-export-get-parent table-cell))))
4007 table-cell)
4008 (memq 'right (org-export-table-cell-borders table-cell info))))
4010 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4011 "Non-nil when TABLE-ROW is at the beginning of a column group.
4012 INFO is a plist used as a communication channel."
4013 (unless (or (eq (org-element-property :type table-row) 'rule)
4014 (org-export-table-row-is-special-p table-row info))
4015 (let ((borders (org-export-table-cell-borders
4016 (car (org-element-contents table-row)) info)))
4017 (or (memq 'top borders) (memq 'above borders)))))
4019 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4020 "Non-nil when TABLE-ROW is at the end of a column group.
4021 INFO is a plist used as a communication channel."
4022 (unless (or (eq (org-element-property :type table-row) 'rule)
4023 (org-export-table-row-is-special-p table-row info))
4024 (let ((borders (org-export-table-cell-borders
4025 (car (org-element-contents table-row)) info)))
4026 (or (memq 'bottom borders) (memq 'below borders)))))
4028 (defun org-export-table-row-starts-header-p (table-row info)
4029 "Non-nil when TABLE-ROW is the first table header's row.
4030 INFO is a plist used as a communication channel."
4031 (and (org-export-table-has-header-p
4032 (org-export-get-parent-table table-row) info)
4033 (org-export-table-row-starts-rowgroup-p table-row info)
4034 (= (org-export-table-row-group table-row info) 1)))
4036 (defun org-export-table-row-ends-header-p (table-row info)
4037 "Non-nil when TABLE-ROW is the last table header's row.
4038 INFO is a plist used as a communication channel."
4039 (and (org-export-table-has-header-p
4040 (org-export-get-parent-table table-row) info)
4041 (org-export-table-row-ends-rowgroup-p table-row info)
4042 (= (org-export-table-row-group table-row info) 1)))
4044 (defun org-export-table-dimensions (table info)
4045 "Return TABLE dimensions.
4047 INFO is a plist used as a communication channel.
4049 Return value is a CONS like (ROWS . COLUMNS) where
4050 ROWS (resp. COLUMNS) is the number of exportable
4051 rows (resp. columns)."
4052 (let (first-row (columns 0) (rows 0))
4053 ;; Set number of rows, and extract first one.
4054 (org-element-map
4055 table 'table-row
4056 (lambda (row)
4057 (when (eq (org-element-property :type row) 'standard)
4058 (incf rows)
4059 (unless first-row (setq first-row row)))) info)
4060 ;; Set number of columns.
4061 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4062 ;; Return value.
4063 (cons rows columns)))
4065 (defun org-export-table-cell-address (table-cell info)
4066 "Return address of a regular TABLE-CELL object.
4068 TABLE-CELL is the cell considered. INFO is a plist used as
4069 a communication channel.
4071 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4072 zero-based index. Only exportable cells are considered. The
4073 function returns nil for other cells."
4074 (let* ((table-row (org-export-get-parent table-cell))
4075 (table (org-export-get-parent-table table-cell)))
4076 ;; Ignore cells in special rows or in special column.
4077 (unless (or (org-export-table-row-is-special-p table-row info)
4078 (and (org-export-table-has-special-column-p table)
4079 (eq (car (org-element-contents table-row)) table-cell)))
4080 (cons
4081 ;; Row number.
4082 (let ((row-count 0))
4083 (org-element-map
4084 table 'table-row
4085 (lambda (row)
4086 (cond ((eq (org-element-property :type row) 'rule) nil)
4087 ((eq row table-row) row-count)
4088 (t (incf row-count) nil)))
4089 info 'first-match))
4090 ;; Column number.
4091 (let ((col-count 0))
4092 (org-element-map
4093 table-row 'table-cell
4094 (lambda (cell)
4095 (if (eq cell table-cell) col-count (incf col-count) nil))
4096 info 'first-match))))))
4098 (defun org-export-get-table-cell-at (address table info)
4099 "Return regular table-cell object at ADDRESS in TABLE.
4101 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4102 zero-based index. TABLE is a table type element. INFO is
4103 a plist used as a communication channel.
4105 If no table-cell, among exportable cells, is found at ADDRESS,
4106 return nil."
4107 (let ((column-pos (cdr address)) (column-count 0))
4108 (org-element-map
4109 ;; Row at (car address) or nil.
4110 (let ((row-pos (car address)) (row-count 0))
4111 (org-element-map
4112 table 'table-row
4113 (lambda (row)
4114 (cond ((eq (org-element-property :type row) 'rule) nil)
4115 ((= row-count row-pos) row)
4116 (t (incf row-count) nil)))
4117 info 'first-match))
4118 'table-cell
4119 (lambda (cell)
4120 (if (= column-count column-pos) cell
4121 (incf column-count) nil))
4122 info 'first-match)))
4125 ;;;; For Tables Of Contents
4127 ;; `org-export-collect-headlines' builds a list of all exportable
4128 ;; headline elements, maybe limited to a certain depth. One can then
4129 ;; easily parse it and transcode it.
4131 ;; Building lists of tables, figures or listings is quite similar.
4132 ;; Once the generic function `org-export-collect-elements' is defined,
4133 ;; `org-export-collect-tables', `org-export-collect-figures' and
4134 ;; `org-export-collect-listings' can be derived from it.
4136 (defun org-export-collect-headlines (info &optional n)
4137 "Collect headlines in order to build a table of contents.
4139 INFO is a plist used as a communication channel.
4141 When optional argument N is an integer, it specifies the depth of
4142 the table of contents. Otherwise, it is set to the value of the
4143 last headline level. See `org-export-headline-levels' for more
4144 information.
4146 Return a list of all exportable headlines as parsed elements."
4147 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4148 (org-element-map
4149 (plist-get info :parse-tree)
4150 'headline
4151 (lambda (headline)
4152 ;; Strip contents from HEADLINE.
4153 (let ((relative-level (org-export-get-relative-level headline info)))
4154 (unless (> relative-level n) headline)))
4155 info))
4157 (defun org-export-collect-elements (type info &optional predicate)
4158 "Collect referenceable elements of a determined type.
4160 TYPE can be a symbol or a list of symbols specifying element
4161 types to search. Only elements with a caption are collected.
4163 INFO is a plist used as a communication channel.
4165 When non-nil, optional argument PREDICATE is a function accepting
4166 one argument, an element of type TYPE. It returns a non-nil
4167 value when that element should be collected.
4169 Return a list of all elements found, in order of appearance."
4170 (org-element-map
4171 (plist-get info :parse-tree) type
4172 (lambda (element)
4173 (and (org-element-property :caption element)
4174 (or (not predicate) (funcall predicate element))
4175 element))
4176 info))
4178 (defun org-export-collect-tables (info)
4179 "Build a list of tables.
4180 INFO is a plist used as a communication channel.
4182 Return a list of table elements with a caption."
4183 (org-export-collect-elements 'table info))
4185 (defun org-export-collect-figures (info predicate)
4186 "Build a list of figures.
4188 INFO is a plist used as a communication channel. PREDICATE is
4189 a function which accepts one argument: a paragraph element and
4190 whose return value is non-nil when that element should be
4191 collected.
4193 A figure is a paragraph type element, with a caption, verifying
4194 PREDICATE. The latter has to be provided since a \"figure\" is
4195 a vague concept that may depend on back-end.
4197 Return a list of elements recognized as figures."
4198 (org-export-collect-elements 'paragraph info predicate))
4200 (defun org-export-collect-listings (info)
4201 "Build a list of src blocks.
4203 INFO is a plist used as a communication channel.
4205 Return a list of src-block elements with a caption."
4206 (org-export-collect-elements 'src-block info))
4209 ;;;; Smart Quotes
4211 (defconst org-export-smart-quotes-alist
4212 '(("de"
4213 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
4214 :texinfo "@quotedblbase{}")
4215 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
4216 :texinfo "@quotedblleft{}")
4217 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
4218 :texinfo "@quotesinglbase{}")
4219 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
4220 :texinfo "@quoteleft{}")
4221 (apostrophe :utf-8 "’" :html "&rsquo;"))
4222 ("en"
4223 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4224 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4225 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4226 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4227 (apostrophe :utf-8 "’" :html "&rsquo;"))
4228 ("es"
4229 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4230 :texinfo "@guillemetleft{}")
4231 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4232 :texinfo "@guillemetright{}")
4233 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4234 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4235 (apostrophe :utf-8 "’" :html "&rsquo;"))
4236 ("fr"
4237 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4238 :texinfo "@guillemetleft{}@tie{}")
4239 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4240 :texinfo "@tie{}@guillemetright{}")
4241 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4242 :texinfo "@guillemetleft{}@tie{}")
4243 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4244 :texinfo "@tie{}@guillemetright{}")
4245 (apostrophe :utf-8 "’" :html "&rsquo;")))
4246 "Smart quotes translations.
4248 Alist whose CAR is a language string and CDR is an alist with
4249 quote type as key and a plist associating various encodings to
4250 their translation as value.
4252 A quote type can be any symbol among `opening-double-quote',
4253 `closing-double-quote', `opening-single-quote',
4254 `closing-single-quote' and `apostrophe'.
4256 Valid encodings include `:utf-8', `:html', `:latex' and
4257 `:texinfo'.
4259 If no translation is found, the quote character is left as-is.")
4261 (defconst org-export-smart-quotes-regexps
4262 (list
4263 ;; Possible opening quote at beginning of string.
4264 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4265 ;; Possible closing quote at beginning of string.
4266 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4267 ;; Possible apostrophe at beginning of string.
4268 "\\`\\('\\)\\S-"
4269 ;; Opening single and double quotes.
4270 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4271 ;; Closing single and double quotes.
4272 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4273 ;; Apostrophe.
4274 "\\S-\\('\\)\\S-"
4275 ;; Possible opening quote at end of string.
4276 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4277 ;; Possible closing quote at end of string.
4278 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4279 ;; Possible apostrophe at end of string.
4280 "\\S-\\('\\)\\'")
4281 "List of regexps matching a quote or an apostrophe.
4282 In every regexp, quote or apostrophe matched is put in group 1.")
4284 (defun org-export-activate-smart-quotes (s encoding info &optional original)
4285 "Replace regular quotes with \"smart\" quotes in string S.
4287 ENCODING is a symbol among `:html', `:latex' and `:utf-8'. INFO
4288 is a plist used as a communication channel.
4290 The function has to retrieve information about string
4291 surroundings in parse tree. It can only happen with an
4292 unmodified string. Thus, if S has already been through another
4293 process, a non-nil ORIGINAL optional argument will provide that
4294 original string.
4296 Return the new string."
4297 (if (equal s "") ""
4298 (let ((quotes-alist (cdr (assoc (plist-get info :language)
4299 org-export-smart-quotes-alist))))
4300 ;; 1. Replace quote character at the beginning of S.
4301 (let* ((prev (org-export-get-previous-element (or original s) info))
4302 (pre-blank (and prev (org-element-property :post-blank prev))))
4303 (cond
4304 ;; Apostrophe?
4305 ((and prev (zerop pre-blank)
4306 (string-match (nth 2 org-export-smart-quotes-regexps) s))
4307 (let ((smart-quote
4308 (plist-get (cdr (assq 'apostrophe quotes-alist)) encoding)))
4309 (when smart-quote
4310 (setq s (replace-match smart-quote nil t s 1)))))
4311 ;; Closing quote?
4312 ((and prev (zerop pre-blank)
4313 (string-match (nth 1 org-export-smart-quotes-regexps) s))
4314 (let ((smart-quote
4315 (plist-get (cdr (assq (if (equal (match-string 1 s) "'")
4316 'closing-single-quote
4317 'closing-double-quote)
4318 quotes-alist))
4319 encoding)))
4320 (when smart-quote
4321 (setq s (replace-match smart-quote nil t s 1)))))
4322 ;; Opening quote?
4323 ((and (or (not prev) (> pre-blank 0))
4324 (string-match (nth 0 org-export-smart-quotes-regexps) s))
4325 (let ((smart-quote
4326 (plist-get (cdr (assq (if (equal (match-string 1 s) "'")
4327 'opening-single-quote
4328 'opening-double-quote)
4329 quotes-alist))
4330 encoding)))
4331 (when smart-quote
4332 (setq s (replace-match smart-quote nil t s 1)))))))
4333 ;; 2. Replace quotes in the middle of the string.
4334 (setq s
4335 ;; Opening quotes.
4336 (replace-regexp-in-string
4337 (nth 3 org-export-smart-quotes-regexps)
4338 (lambda (text)
4339 (or (plist-get
4340 (cdr (assq (if (equal (match-string 1 text) "'")
4341 'opening-single-quote
4342 'opening-double-quote)
4343 quotes-alist))
4344 encoding)
4345 (match-string 1 text)))
4346 s nil t 1))
4347 (setq s
4348 (replace-regexp-in-string
4349 ;; Closing quotes.
4350 (nth 4 org-export-smart-quotes-regexps)
4351 (lambda (text)
4352 (or (plist-get
4353 (cdr (assq (if (equal (match-string 1 text) "'")
4354 'closing-single-quote
4355 'closing-double-quote)
4356 quotes-alist))
4357 encoding)
4358 (match-string 1 text)))
4359 s nil t 1))
4360 (setq s
4361 (replace-regexp-in-string
4362 ;; Apostrophes.
4363 (nth 5 org-export-smart-quotes-regexps)
4364 (lambda (text)
4365 (or (plist-get (cdr (assq 'apostrophe quotes-alist)) encoding)
4366 (match-string 1 text)))
4367 s nil t 1))
4368 ;; 3. Replace quote character at the end of S.
4369 (let ((next (org-export-get-next-element (or original s) info)))
4370 (cond
4371 ;; Apostrophe?
4372 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
4373 (let ((smart-quote
4374 (plist-get (cdr (assq 'apostrophe quotes-alist)) encoding)))
4375 (when smart-quote (setq s (replace-match smart-quote nil t s 1)))))
4376 ;; Closing quote?
4377 ((and (not next)
4378 (string-match (nth 7 org-export-smart-quotes-regexps) s))
4379 (let ((smart-quote
4380 (plist-get (cdr (assq (if (equal (match-string 1 s) "'")
4381 'closing-single-quote
4382 'closing-double-quote)
4383 quotes-alist))
4384 encoding)))
4385 (when smart-quote (setq s (replace-match smart-quote nil t s 1)))))
4386 ;; Opening quote?
4387 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
4388 (let ((smart-quote
4389 (plist-get (cdr (assq (if (equal (match-string 1 s) "'")
4390 'opening-single-quote
4391 'opening-double-quote)
4392 quotes-alist))
4393 encoding)))
4394 (when smart-quote
4395 (setq s (replace-match smart-quote nil t s 1)))))))
4396 ;; Return string with smart quotes.
4397 s)))
4400 ;;;; Topology
4402 ;; Here are various functions to retrieve information about the
4403 ;; neighbourhood of a given element or object. Neighbours of interest
4404 ;; are direct parent (`org-export-get-parent'), parent headline
4405 ;; (`org-export-get-parent-headline'), first element containing an
4406 ;; object, (`org-export-get-parent-element'), parent table
4407 ;; (`org-export-get-parent-table'), previous element or object
4408 ;; (`org-export-get-previous-element') and next element or object
4409 ;; (`org-export-get-next-element').
4411 ;; `org-export-get-genealogy' returns the full genealogy of a given
4412 ;; element or object, from closest parent to full parse tree.
4414 (defun org-export-get-parent (blob)
4415 "Return BLOB parent or nil.
4416 BLOB is the element or object considered."
4417 (org-element-property :parent blob))
4419 (defun org-export-get-genealogy (blob)
4420 "Return full genealogy relative to a given element or object.
4422 BLOB is the element or object being considered.
4424 Ancestors are returned from closest to farthest, the last one
4425 being the full parse tree."
4426 (let (genealogy (parent blob))
4427 (while (setq parent (org-element-property :parent parent))
4428 (push parent genealogy))
4429 (nreverse genealogy)))
4431 (defun org-export-get-parent-headline (blob)
4432 "Return BLOB parent headline or nil.
4433 BLOB is the element or object being considered."
4434 (let ((parent blob))
4435 (while (and (setq parent (org-element-property :parent parent))
4436 (not (eq (org-element-type parent) 'headline))))
4437 parent))
4439 (defun org-export-get-parent-element (object)
4440 "Return first element containing OBJECT or nil.
4441 OBJECT is the object to consider."
4442 (let ((parent object))
4443 (while (and (setq parent (org-element-property :parent parent))
4444 (memq (org-element-type parent) org-element-all-objects)))
4445 parent))
4447 (defun org-export-get-parent-table (object)
4448 "Return OBJECT parent table or nil.
4449 OBJECT is either a `table-cell' or `table-element' type object."
4450 (let ((parent object))
4451 (while (and (setq parent (org-element-property :parent parent))
4452 (not (eq (org-element-type parent) 'table))))
4453 parent))
4455 (defun org-export-get-previous-element (blob info)
4456 "Return previous element or object.
4457 BLOB is an element or object. INFO is a plist used as
4458 a communication channel. Return previous exportable element or
4459 object, a string, or nil."
4460 (let (prev)
4461 (catch 'exit
4462 (mapc (lambda (obj)
4463 (cond ((eq obj blob) (throw 'exit prev))
4464 ((memq obj (plist-get info :ignore-list)))
4465 (t (setq prev obj))))
4466 (org-element-contents (org-export-get-parent blob))))))
4468 (defun org-export-get-next-element (blob info)
4469 "Return next element or object.
4470 BLOB is an element or object. INFO is a plist used as
4471 a communication channel. Return next exportable element or
4472 object, a string, or nil."
4473 (catch 'found
4474 (mapc (lambda (obj)
4475 (unless (memq obj (plist-get info :ignore-list))
4476 (throw 'found obj)))
4477 (cdr (memq blob (org-element-contents (org-export-get-parent blob)))))
4478 nil))
4481 ;;;; Translation
4483 ;; `org-export-translate' translates a string according to language
4484 ;; specified by LANGUAGE keyword or `org-export-language-setup'
4485 ;; variable and a specified charset. `org-export-dictionary' contains
4486 ;; the dictionary used for the translation.
4488 (defconst org-export-dictionary
4489 '(("Author"
4490 ("ca" :default "Autor")
4491 ("cs" :default "Autor")
4492 ("da" :default "Ophavsmand")
4493 ("de" :default "Autor")
4494 ("eo" :html "A&#365;toro")
4495 ("es" :default "Autor")
4496 ("fi" :html "Tekij&auml;")
4497 ("fr" :default "Auteur")
4498 ("hu" :default "Szerz&otilde;")
4499 ("is" :html "H&ouml;fundur")
4500 ("it" :default "Autore")
4501 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
4502 ("nl" :default "Auteur")
4503 ("no" :default "Forfatter")
4504 ("nb" :default "Forfatter")
4505 ("nn" :default "Forfattar")
4506 ("pl" :default "Autor")
4507 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4508 ("sv" :html "F&ouml;rfattare")
4509 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
4510 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
4511 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
4512 ("Date"
4513 ("ca" :default "Data")
4514 ("cs" :default "Datum")
4515 ("da" :default "Dato")
4516 ("de" :default "Datum")
4517 ("eo" :default "Dato")
4518 ("es" :default "Fecha")
4519 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
4520 ("hu" :html "D&aacute;tum")
4521 ("is" :default "Dagsetning")
4522 ("it" :default "Data")
4523 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
4524 ("nl" :default "Datum")
4525 ("no" :default "Dato")
4526 ("nb" :default "Dato")
4527 ("nn" :default "Dato")
4528 ("pl" :default "Data")
4529 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4530 ("sv" :default "Datum")
4531 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
4532 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
4533 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
4534 ("Equation"
4535 ("fr" :ascii "Equation" :default "Équation"))
4536 ("Figure")
4537 ("Footnotes"
4538 ("ca" :html "Peus de p&agrave;gina")
4539 ("cs" :default "Pozn\xe1mky pod carou")
4540 ("da" :default "Fodnoter")
4541 ("de" :html "Fu&szlig;noten")
4542 ("eo" :default "Piednotoj")
4543 ("es" :html "Pies de p&aacute;gina")
4544 ("fi" :default "Alaviitteet")
4545 ("fr" :default "Notes de bas de page")
4546 ("hu" :html "L&aacute;bjegyzet")
4547 ("is" :html "Aftanm&aacute;lsgreinar")
4548 ("it" :html "Note a pi&egrave; di pagina")
4549 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
4550 ("nl" :default "Voetnoten")
4551 ("no" :default "Fotnoter")
4552 ("nb" :default "Fotnoter")
4553 ("nn" :default "Fotnotar")
4554 ("pl" :default "Przypis")
4555 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
4556 ("sv" :default "Fotnoter")
4557 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
4558 :utf-8 "Примітки")
4559 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
4560 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
4561 ("List of Listings"
4562 ("fr" :default "Liste des programmes"))
4563 ("List of Tables"
4564 ("fr" :default "Liste des tableaux"))
4565 ("Listing %d:"
4566 ("fr"
4567 :ascii "Programme %d :" :default "Programme nº %d :"
4568 :latin1 "Programme %d :"))
4569 ("Listing %d: %s"
4570 ("fr"
4571 :ascii "Programme %d : %s" :default "Programme nº %d : %s"
4572 :latin1 "Programme %d : %s"))
4573 ("See section %s"
4574 ("fr" :default "cf. section %s"))
4575 ("Table %d:"
4576 ("fr"
4577 :ascii "Tableau %d :" :default "Tableau nº %d :" :latin1 "Tableau %d :"))
4578 ("Table %d: %s"
4579 ("fr"
4580 :ascii "Tableau %d : %s" :default "Tableau nº %d : %s"
4581 :latin1 "Tableau %d : %s"))
4582 ("Table of Contents"
4583 ("ca" :html "&Iacute;ndex")
4584 ("cs" :default "Obsah")
4585 ("da" :default "Indhold")
4586 ("de" :default "Inhaltsverzeichnis")
4587 ("eo" :default "Enhavo")
4588 ("es" :html "&Iacute;ndice")
4589 ("fi" :html "Sis&auml;llysluettelo")
4590 ("fr" :ascii "Sommaire" :default "Table des matières")
4591 ("hu" :html "Tartalomjegyz&eacute;k")
4592 ("is" :default "Efnisyfirlit")
4593 ("it" :default "Indice")
4594 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
4595 ("nl" :default "Inhoudsopgave")
4596 ("no" :default "Innhold")
4597 ("nb" :default "Innhold")
4598 ("nn" :default "Innhald")
4599 ("pl" :html "Spis tre&#x015b;ci")
4600 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
4601 :utf-8 "Содержание")
4602 ("sv" :html "Inneh&aring;ll")
4603 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
4604 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
4605 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
4606 ("Unknown reference"
4607 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")))
4608 "Dictionary for export engine.
4610 Alist whose CAR is the string to translate and CDR is an alist
4611 whose CAR is the language string and CDR is a plist whose
4612 properties are possible charsets and values translated terms.
4614 It is used as a database for `org-export-translate'. Since this
4615 function returns the string as-is if no translation was found,
4616 the variable only needs to record values different from the
4617 entry.")
4619 (defun org-export-translate (s encoding info)
4620 "Translate string S according to language specification.
4622 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
4623 and `:utf-8'. INFO is a plist used as a communication channel.
4625 Translation depends on `:language' property. Return the
4626 translated string. If no translation is found, try to fall back
4627 to `:default' encoding. If it fails, return S."
4628 (let* ((lang (plist-get info :language))
4629 (translations (cdr (assoc lang
4630 (cdr (assoc s org-export-dictionary))))))
4631 (or (plist-get translations encoding)
4632 (plist-get translations :default)
4633 s)))
4637 ;;; The Dispatcher
4639 ;; `org-export-dispatch' is the standard interactive way to start an
4640 ;; export process. It uses `org-export-dispatch-ui' as a subroutine
4641 ;; for its interface, which, in turn, delegates response to key
4642 ;; pressed to `org-export-dispatch-action'.
4644 (defvar org-export-dispatch-menu-entries nil
4645 "List of menu entries available for `org-export-dispatch'.
4646 This variable shouldn't be set directly. Set-up :menu-entry
4647 keyword in either `org-export-define-backend' or
4648 `org-export-define-derived-backend' instead.")
4650 ;;;###autoload
4651 (defun org-export-dispatch ()
4652 "Export dispatcher for Org mode.
4654 It provides an access to common export related tasks in a buffer.
4655 Its interface comes in two flavours: standard and expert. While
4656 both share the same set of bindings, only the former displays the
4657 valid keys associations. Set `org-export-dispatch-use-expert-ui'
4658 to switch to one or the other."
4659 (interactive)
4660 (let* ((input (save-window-excursion
4661 (unwind-protect
4662 (org-export-dispatch-ui (list org-export-initial-scope)
4664 org-export-dispatch-use-expert-ui)
4665 (and (get-buffer "*Org Export Dispatcher*")
4666 (kill-buffer "*Org Export Dispatcher*")))))
4667 (action (car input))
4668 (optns (cdr input)))
4669 (case action
4670 ;; First handle special hard-coded actions.
4671 (publish-current-file (org-e-publish-current-file (memq 'force optns)))
4672 (publish-current-project
4673 (org-e-publish-current-project (memq 'force optns)))
4674 (publish-choose-project
4675 (org-e-publish (assoc (org-icompleting-read
4676 "Publish project: "
4677 org-e-publish-project-alist nil t)
4678 org-e-publish-project-alist)
4679 (memq 'force optns)))
4680 (publish-all (org-e-publish-all (memq 'force optns)))
4681 (otherwise
4682 (funcall action
4683 (memq 'subtree optns)
4684 (memq 'visible optns)
4685 (memq 'body optns))))))
4687 (defun org-export-dispatch-ui (options first-key expertp)
4688 "Handle interface for `org-export-dispatch'.
4690 OPTIONS is a list containing current interactive options set for
4691 export. It can contain any of the following symbols:
4692 `body' toggles a body-only export
4693 `subtree' restricts export to current subtree
4694 `visible' restricts export to visible part of buffer.
4695 `force' force publishing files.
4697 FIRST-KEY is the key pressed to select the first level menu. It
4698 is nil when this menu hasn't been selected yet.
4700 EXPERTP, when non-nil, triggers expert UI. In that case, no help
4701 buffer is provided, but indications about currently active
4702 options are given in the prompt. Moreover, \[?] allows to switch
4703 back to standard interface."
4704 (let* ((fontify-key
4705 (lambda (key &optional access-key)
4706 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
4707 ;; non-nil is the required first-level key to activate
4708 ;; KEY. When its value is t, activate KEY independently
4709 ;; on the first key, if any. A nil value means KEY will
4710 ;; only be activated at first level.
4711 (if (or (eq access-key t) (eq access-key first-key))
4712 (org-add-props key nil 'face 'org-warning)
4713 (org-no-properties key))))
4714 ;; Make sure order of menu doesn't depend on the order in
4715 ;; which back-ends are loaded.
4716 (backends (sort (copy-sequence org-export-dispatch-menu-entries)
4717 (lambda (a b) (< (car a) (car b)))))
4718 ;; Compute a list of allowed keys based on the first key
4719 ;; pressed, if any. Some keys (?1, ?2, ?3, ?4 and ?q) are
4720 ;; always available.
4721 (allowed-keys
4722 (nconc (list ?1 ?2 ?3 ?4)
4723 (mapcar 'car
4724 (if (not first-key) backends
4725 (nth 2 (assq first-key backends))))
4726 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
4727 ((not first-key) (list ?P)))
4728 (when expertp (list ??))
4729 (list ?q)))
4730 ;; Build the help menu for standard UI.
4731 (help
4732 (unless expertp
4733 (concat
4734 ;; Options are hard-coded.
4735 (format "Options
4736 [%s] Body only: %s [%s] Visible only: %s
4737 [%s] Export scope: %s [%s] Force publishing: %s\n\n"
4738 (funcall fontify-key "1" t)
4739 (if (memq 'body options) "On " "Off")
4740 (funcall fontify-key "2" t)
4741 (if (memq 'visible options) "On " "Off")
4742 (funcall fontify-key "3" t)
4743 (if (memq 'subtree options) "Subtree" "Buffer ")
4744 (funcall fontify-key "4" t)
4745 (if (memq 'force options) "On " "Off"))
4746 ;; Display registered back-end entries.
4747 (mapconcat
4748 (lambda (entry)
4749 (let ((top-key (car entry)))
4750 (concat
4751 (format "[%s] %s\n"
4752 (funcall fontify-key (char-to-string top-key))
4753 (nth 1 entry))
4754 (let ((sub-menu (nth 2 entry)))
4755 (unless (functionp sub-menu)
4756 ;; Split sub-menu into two columns.
4757 (let ((index -1))
4758 (concat
4759 (mapconcat
4760 (lambda (sub-entry)
4761 (incf index)
4762 (format (if (zerop (mod index 2)) " [%s] %-24s"
4763 "[%s] %s\n")
4764 (funcall fontify-key
4765 (char-to-string (car sub-entry))
4766 top-key)
4767 (nth 1 sub-entry)))
4768 sub-menu "")
4769 (when (zerop (mod index 2)) "\n"))))))))
4770 backends "\n")
4771 ;; Publishing menu is hard-coded.
4772 (format "\n[%s] Publish
4773 [%s] Current file [%s] Current project
4774 [%s] Choose project [%s] All projects\n\n"
4775 (funcall fontify-key "P")
4776 (funcall fontify-key "f" ?P)
4777 (funcall fontify-key "p" ?P)
4778 (funcall fontify-key "x" ?P)
4779 (funcall fontify-key "a" ?P))
4780 (format "\[%s] %s"
4781 (funcall fontify-key "q" t)
4782 (if first-key "Main menu" "Exit")))))
4783 ;; Build prompts for both standard and expert UI.
4784 (standard-prompt (unless expertp "Export command: "))
4785 (expert-prompt
4786 (when expertp
4787 (format
4788 "Export command (Options: %s%s%s%s) [%s]: "
4789 (if (memq 'body options) (funcall fontify-key "b" t) "-")
4790 (if (memq 'visible options) (funcall fontify-key "v" t) "-")
4791 (if (memq 'subtree options) (funcall fontify-key "s" t) "-")
4792 (if (memq 'force options) (funcall fontify-key "f" t) "-")
4793 (concat allowed-keys)))))
4794 ;; With expert UI, just read key with a fancy prompt. In standard
4795 ;; UI, display an intrusive help buffer.
4796 (if expertp
4797 (org-export-dispatch-action
4798 expert-prompt allowed-keys backends options first-key expertp)
4799 ;; At first call, create frame layout in order to display menu.
4800 (unless (get-buffer "*Org Export Dispatcher*")
4801 (delete-other-windows)
4802 (org-switch-to-buffer-other-window
4803 (get-buffer-create "*Org Export Dispatcher*"))
4804 (setq cursor-type nil))
4805 ;; At this point, the buffer containing the menu exists and is
4806 ;; visible in the current window. So, refresh it.
4807 (with-current-buffer "*Org Export Dispatcher*"
4808 (erase-buffer)
4809 (insert help))
4810 (org-fit-window-to-buffer)
4811 (org-export-dispatch-action
4812 standard-prompt allowed-keys backends options first-key expertp))))
4814 (defun org-export-dispatch-action
4815 (prompt allowed-keys backends options first-key expertp)
4816 "Read a character from command input and act accordingly.
4818 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
4819 a list of characters available at a given step in the process.
4820 BACKENDS is a list of menu entries. OPTIONS, FIRST-KEY and
4821 EXPERTP are the same as defined in `org-export-dispatch-ui',
4822 which see.
4824 Toggle export options when required. Otherwise, return value is
4825 a list with action as CAR and a list of interactive export
4826 options as CDR."
4827 (let ((key (let ((k (read-char-exclusive prompt)))
4828 ;; Translate "C-a", "C-b"... into "a", "b"... Then take action
4829 ;; depending on user's key pressed.
4830 (if (< k 27) (+ k 96) k))))
4831 (cond
4832 ;; Ignore non-standard characters (i.e. "M-a") and
4833 ;; undefined associations.
4834 ((not (memq key allowed-keys))
4835 (ding)
4836 (unless expertp (message "Invalid key") (sit-for 1))
4837 (org-export-dispatch-ui options first-key expertp))
4838 ;; q key at first level aborts export. At second
4839 ;; level, cancel first key instead.
4840 ((eq key ?q) (if (not first-key) (error "Export aborted")
4841 (org-export-dispatch-ui options nil expertp)))
4842 ;; Help key: Switch back to standard interface if
4843 ;; expert UI was active.
4844 ((eq key ??) (org-export-dispatch-ui options first-key nil))
4845 ;; Toggle export options.
4846 ((memq key '(?1 ?2 ?3 ?4))
4847 (org-export-dispatch-ui
4848 (let ((option (case key (?1 'body) (?2 'visible) (?3 'subtree)
4849 (?4 'force))))
4850 (if (memq option options) (remq option options)
4851 (cons option options)))
4852 first-key expertp))
4853 ;; Action selected: Send key and options back to
4854 ;; `org-export-dispatch'.
4855 ((or first-key
4856 (and (eq first-key ?P) (memq key '(?f ?p ?x ?a)))
4857 (functionp (nth 2 (assq key backends))))
4858 (cons (cond
4859 ((not first-key) (nth 2 (assq key backends)))
4860 ;; Publishing actions are hard-coded. Send a special
4861 ;; signal to `org-export-dispatch'.
4862 ((eq first-key ?P)
4863 (case key
4864 (?f 'publish-current-file)
4865 (?p 'publish-current-project)
4866 (?x 'publish-choose-project)
4867 (?a 'publish-all)))
4868 (t (nth 2 (assq key (nth 2 (assq first-key backends))))))
4869 options))
4870 ;; Otherwise, enter sub-menu.
4871 (t (org-export-dispatch-ui options key expertp)))))
4874 (provide 'org-export)
4875 ;;; org-export.el ends here