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