ORG-NEWS: Document new options
[org-mode/org-kjn.git] / lisp / ox.el
blob72eb70b49f5fe416b8aeb40d894eeddd92c9fa90
1 ;;; ox.el --- Generic Export Engine for Org Mode
3 ;; Copyright (C) 2012-2014 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This library implements a generic export engine for Org, built on
26 ;; its syntactical parser: Org Elements.
28 ;; Besides that parser, the generic exporter is made of three distinct
29 ;; parts:
31 ;; - The communication channel consists in a property list, which is
32 ;; created and updated during the process. Its use is to offer
33 ;; every piece of information, would it be about initial environment
34 ;; or contextual data, all in a single place. The exhaustive list
35 ;; of properties is given in "The Communication Channel" section of
36 ;; this file.
38 ;; - The transcoder walks the parse tree, ignores or treat as plain
39 ;; text elements and objects according to export options, and
40 ;; eventually calls back-end specific functions to do the real
41 ;; transcoding, concatenating their return value along the way.
43 ;; - The filter system is activated at the very beginning and the very
44 ;; end of the export process, and each time an element or an object
45 ;; has been converted. It is the entry point to fine-tune standard
46 ;; output from back-end transcoders. See "The Filter System"
47 ;; section for more information.
49 ;; The core function is `org-export-as'. It returns the transcoded
50 ;; buffer as a string.
52 ;; An export back-end is defined with `org-export-define-backend'.
53 ;; This function can also support specific buffer keywords, OPTION
54 ;; keyword's items and filters. Refer to function's documentation for
55 ;; more information.
57 ;; If the new back-end shares most properties with another one,
58 ;; `org-export-define-derived-backend' can be used to simplify the
59 ;; process.
61 ;; Any back-end can define its own variables. Among them, those
62 ;; customizable should belong to the `org-export-BACKEND' group.
64 ;; Tools for common tasks across back-ends are implemented in the
65 ;; following part of the file.
67 ;; Then, a wrapper macro for asynchronous export,
68 ;; `org-export-async-start', along with tools to display results. are
69 ;; given in the penultimate part.
71 ;; Eventually, a dispatcher (`org-export-dispatch') for standard
72 ;; back-ends is provided in the last one.
74 ;;; Code:
76 (eval-when-compile (require 'cl))
77 (require 'org-element)
78 (require 'org-macro)
79 (require 'ob-exp)
81 (declare-function org-publish "ox-publish" (project &optional force async))
82 (declare-function org-publish-all "ox-publish" (&optional force async))
83 (declare-function
84 org-publish-current-file "ox-publish" (&optional force async))
85 (declare-function org-publish-current-project "ox-publish"
86 (&optional force async))
88 (defvar org-publish-project-alist)
89 (defvar org-table-number-fraction)
90 (defvar org-table-number-regexp)
93 ;;; Internal Variables
95 ;; Among internal variables, the most important is
96 ;; `org-export-options-alist'. This variable define the global export
97 ;; options, shared between every exporter, and how they are acquired.
99 (defconst org-export-max-depth 19
100 "Maximum nesting depth for headlines, counting from 0.")
102 (defconst org-export-options-alist
103 '((:title "TITLE" nil nil space)
104 (:date "DATE" nil nil t)
105 (:author "AUTHOR" nil user-full-name t)
106 (:email "EMAIL" nil user-mail-address t)
107 (:description "DESCRIPTION" nil nil newline)
108 (:keywords "KEYWORDS" nil nil space)
109 (:language "LANGUAGE" nil org-export-default-language t)
110 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
111 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
112 (:creator "CREATOR" nil org-export-creator-string)
113 (:headline-levels nil "H" org-export-headline-levels)
114 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
115 (:section-numbers nil "num" org-export-with-section-numbers)
116 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
117 (:with-archived-trees nil "arch" org-export-with-archived-trees)
118 (:with-author nil "author" org-export-with-author)
119 (:with-clocks nil "c" org-export-with-clocks)
120 (:with-creator nil "creator" org-export-with-creator)
121 (:with-date nil "date" org-export-with-date)
122 (:with-drawers nil "d" org-export-with-drawers)
123 (:with-email nil "email" org-export-with-email)
124 (:with-emphasize nil "*" org-export-with-emphasize)
125 (:with-entities nil "e" org-export-with-entities)
126 (:with-fixed-width nil ":" org-export-with-fixed-width)
127 (:with-footnotes nil "f" org-export-with-footnotes)
128 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
129 (:with-latex nil "tex" org-export-with-latex)
130 (:with-planning nil "p" org-export-with-planning)
131 (:with-priority nil "pri" org-export-with-priority)
132 (:with-properties nil "prop" org-export-with-properties)
133 (:with-smart-quotes nil "'" org-export-with-smart-quotes)
134 (:with-special-strings nil "-" org-export-with-special-strings)
135 (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
136 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
137 (:with-toc nil "toc" org-export-with-toc)
138 (:with-tables nil "|" org-export-with-tables)
139 (:with-tags nil "tags" org-export-with-tags)
140 (:with-tasks nil "tasks" org-export-with-tasks)
141 (:with-timestamps nil "<" org-export-with-timestamps)
142 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
143 "Alist between export properties and ways to set them.
145 The CAR of the alist is the property name, and the CDR is a list
146 like (KEYWORD OPTION DEFAULT BEHAVIOR) where:
148 KEYWORD is a string representing a buffer keyword, or nil. Each
149 property defined this way can also be set, during subtree
150 export, through a headline property named after the keyword
151 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
152 property).
153 OPTION is a string that could be found in an #+OPTIONS: line.
154 DEFAULT is the default value for the property.
155 BEHAVIOR determines how Org should handle multiple keywords for
156 the same property. It is a symbol among:
157 nil Keep old value and discard the new one.
158 t Replace old value with the new one.
159 `space' Concatenate the values, separating them with a space.
160 `newline' Concatenate the values, separating them with
161 a newline.
162 `split' Split values at white spaces, and cons them to the
163 previous list.
165 Values set through KEYWORD and OPTION have precedence over
166 DEFAULT.
168 All these properties should be back-end agnostic. Back-end
169 specific properties are set through `org-export-define-backend'.
170 Properties redefined there have precedence over these.")
172 (defconst org-export-special-keywords '("FILETAGS" "SETUPFILE" "OPTIONS")
173 "List of in-buffer keywords that require special treatment.
174 These keywords are not directly associated to a property. The
175 way they are handled must be hard-coded into
176 `org-export--get-inbuffer-options' function.")
178 (defconst org-export-filters-alist
179 '((:filter-body . org-export-filter-body-functions)
180 (:filter-bold . org-export-filter-bold-functions)
181 (:filter-babel-call . org-export-filter-babel-call-functions)
182 (:filter-center-block . org-export-filter-center-block-functions)
183 (:filter-clock . org-export-filter-clock-functions)
184 (:filter-code . org-export-filter-code-functions)
185 (:filter-comment . org-export-filter-comment-functions)
186 (:filter-comment-block . org-export-filter-comment-block-functions)
187 (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
188 (:filter-drawer . org-export-filter-drawer-functions)
189 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
190 (:filter-entity . org-export-filter-entity-functions)
191 (:filter-example-block . org-export-filter-example-block-functions)
192 (:filter-export-block . org-export-filter-export-block-functions)
193 (:filter-export-snippet . org-export-filter-export-snippet-functions)
194 (:filter-final-output . org-export-filter-final-output-functions)
195 (:filter-fixed-width . org-export-filter-fixed-width-functions)
196 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
197 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
198 (:filter-headline . org-export-filter-headline-functions)
199 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
200 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
201 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
202 (:filter-inlinetask . org-export-filter-inlinetask-functions)
203 (:filter-italic . org-export-filter-italic-functions)
204 (:filter-item . org-export-filter-item-functions)
205 (:filter-keyword . org-export-filter-keyword-functions)
206 (:filter-latex-environment . org-export-filter-latex-environment-functions)
207 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
208 (:filter-line-break . org-export-filter-line-break-functions)
209 (:filter-link . org-export-filter-link-functions)
210 (:filter-node-property . org-export-filter-node-property-functions)
211 (:filter-options . org-export-filter-options-functions)
212 (:filter-paragraph . org-export-filter-paragraph-functions)
213 (:filter-parse-tree . org-export-filter-parse-tree-functions)
214 (:filter-plain-list . org-export-filter-plain-list-functions)
215 (:filter-plain-text . org-export-filter-plain-text-functions)
216 (:filter-planning . org-export-filter-planning-functions)
217 (:filter-property-drawer . org-export-filter-property-drawer-functions)
218 (:filter-quote-block . org-export-filter-quote-block-functions)
219 (:filter-radio-target . org-export-filter-radio-target-functions)
220 (:filter-section . org-export-filter-section-functions)
221 (:filter-special-block . org-export-filter-special-block-functions)
222 (:filter-src-block . org-export-filter-src-block-functions)
223 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
224 (:filter-strike-through . org-export-filter-strike-through-functions)
225 (:filter-subscript . org-export-filter-subscript-functions)
226 (:filter-superscript . org-export-filter-superscript-functions)
227 (:filter-table . org-export-filter-table-functions)
228 (:filter-table-cell . org-export-filter-table-cell-functions)
229 (:filter-table-row . org-export-filter-table-row-functions)
230 (:filter-target . org-export-filter-target-functions)
231 (:filter-timestamp . org-export-filter-timestamp-functions)
232 (:filter-underline . org-export-filter-underline-functions)
233 (:filter-verbatim . org-export-filter-verbatim-functions)
234 (:filter-verse-block . org-export-filter-verse-block-functions))
235 "Alist between filters properties and initial values.
237 The key of each association is a property name accessible through
238 the communication channel. Its value is a configurable global
239 variable defining initial filters.
241 This list is meant to install user specified filters. Back-end
242 developers may install their own filters using
243 `org-export-define-backend'. Filters defined there will always
244 be prepended to the current list, so they always get applied
245 first.")
247 (defconst org-export-default-inline-image-rule
248 `(("file" .
249 ,(format "\\.%s\\'"
250 (regexp-opt
251 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
252 "xpm" "pbm" "pgm" "ppm") t))))
253 "Default rule for link matching an inline image.
254 This rule applies to links with no description. By default, it
255 will be considered as an inline image if it targets a local file
256 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
257 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
258 See `org-export-inline-image-p' for more information about
259 rules.")
261 (defconst org-export-ignored-local-variables
262 '(org-font-lock-keywords
263 org-element--cache org-element--cache-objects org-element--cache-sync-keys
264 org-element--cache-sync-requests org-element--cache-sync-timer)
265 "List of variables not copied through upon buffer duplication.
266 Export process takes place on a copy of the original buffer.
267 When this copy is created, all Org related local variables not in
268 this list are copied to the new buffer. Variables with an
269 unreadable value are also ignored.")
271 (defvar org-export-async-debug nil
272 "Non-nil means asynchronous export process should leave data behind.
274 This data is found in the appropriate \"*Org Export Process*\"
275 buffer, and in files prefixed with \"org-export-process\" and
276 located in `temporary-file-directory'.
278 When non-nil, it will also set `debug-on-error' to a non-nil
279 value in the external process.")
281 (defvar org-export-stack-contents nil
282 "Record asynchronously generated export results and processes.
283 This is an alist: its CAR is the source of the
284 result (destination file or buffer for a finished process,
285 original buffer for a running one) and its CDR is a list
286 containing the back-end used, as a symbol, and either a process
287 or the time at which it finished. It is used to build the menu
288 from `org-export-stack'.")
290 (defvar org-export--registered-backends nil
291 "List of backends currently available in the exporter.
292 This variable is set with `org-export-define-backend' and
293 `org-export-define-derived-backend' functions.")
295 (defvar org-export-dispatch-last-action nil
296 "Last command called from the dispatcher.
297 The value should be a list. Its CAR is the action, as a symbol,
298 and its CDR is a list of export options.")
300 (defvar org-export-dispatch-last-position (make-marker)
301 "The position where the last export command was created using the dispatcher.
302 This marker will be used with `C-u C-c C-e' to make sure export repetition
303 uses the same subtree if the previous command was restricted to a subtree.")
305 ;; For compatibility with Org < 8
306 (defvar org-export-current-backend nil
307 "Name, if any, of the back-end used during an export process.
309 Its value is a symbol such as `html', `latex', `ascii', or nil if
310 the back-end is anonymous (see `org-export-create-backend') or if
311 there is no export process in progress.
313 It can be used to teach Babel blocks how to act differently
314 according to the back-end used.")
318 ;;; User-configurable Variables
320 ;; Configuration for the masses.
322 ;; They should never be accessed directly, as their value is to be
323 ;; stored in a property list (cf. `org-export-options-alist').
324 ;; Back-ends will read their value from there instead.
326 (defgroup org-export nil
327 "Options for exporting Org mode files."
328 :tag "Org Export"
329 :group 'org)
331 (defgroup org-export-general nil
332 "General options for export engine."
333 :tag "Org Export General"
334 :group 'org-export)
336 (defcustom org-export-with-archived-trees 'headline
337 "Whether sub-trees with the ARCHIVE tag should be exported.
339 This can have three different values:
340 nil Do not export, pretend this tree is not present.
341 t Do export the entire tree.
342 `headline' Only export the headline, but skip the tree below it.
344 This option can also be set with the OPTIONS keyword,
345 e.g. \"arch:nil\"."
346 :group 'org-export-general
347 :type '(choice
348 (const :tag "Not at all" nil)
349 (const :tag "Headline only" headline)
350 (const :tag "Entirely" t)))
352 (defcustom org-export-with-author t
353 "Non-nil means insert author name into the exported file.
354 This option can also be set with the OPTIONS keyword,
355 e.g. \"author:nil\"."
356 :group 'org-export-general
357 :type 'boolean)
359 (defcustom org-export-with-clocks nil
360 "Non-nil means export CLOCK keywords.
361 This option can also be set with the OPTIONS keyword,
362 e.g. \"c:t\"."
363 :group 'org-export-general
364 :type 'boolean)
366 (defcustom org-export-with-creator 'comment
367 "Non-nil means the postamble should contain a creator sentence.
369 The sentence can be set in `org-export-creator-string' and
370 defaults to \"Generated by Org mode XX in Emacs XXX.\".
372 If the value is `comment' insert it as a comment."
373 :group 'org-export-general
374 :type '(choice
375 (const :tag "No creator sentence" nil)
376 (const :tag "Sentence as a comment" comment)
377 (const :tag "Insert the sentence" t)))
379 (defcustom org-export-with-date t
380 "Non-nil means insert date in the exported document.
381 This option can also be set with the OPTIONS keyword,
382 e.g. \"date:nil\"."
383 :group 'org-export-general
384 :type 'boolean)
386 (defcustom org-export-date-timestamp-format nil
387 "Time-stamp format string to use for DATE keyword.
389 The format string, when specified, only applies if date consists
390 in a single time-stamp. Otherwise its value will be ignored.
392 See `format-time-string' for details on how to build this
393 string."
394 :group 'org-export-general
395 :type '(choice
396 (string :tag "Time-stamp format string")
397 (const :tag "No format string" nil)))
399 (defcustom org-export-creator-string
400 (format "Emacs %s (Org mode %s)"
401 emacs-version
402 (if (fboundp 'org-version) (org-version) "unknown version"))
403 "Information about the creator of the document.
404 This option can also be set on with the CREATOR keyword."
405 :group 'org-export-general
406 :type '(string :tag "Creator string"))
408 (defcustom org-export-with-drawers '(not "LOGBOOK")
409 "Non-nil means export contents of standard drawers.
411 When t, all drawers are exported. This may also be a list of
412 drawer names to export, as strings. If that list starts with
413 `not', only drawers with such names will be ignored.
415 This variable doesn't apply to properties drawers. See
416 `org-export-with-properties' instead.
418 This option can also be set with the OPTIONS keyword,
419 e.g. \"d:nil\"."
420 :group 'org-export-general
421 :version "24.4"
422 :package-version '(Org . "8.0")
423 :type '(choice
424 (const :tag "All drawers" t)
425 (const :tag "None" nil)
426 (repeat :tag "Selected drawers"
427 (string :tag "Drawer name"))
428 (list :tag "Ignored drawers"
429 (const :format "" not)
430 (repeat :tag "Specify names of drawers to ignore during export"
431 :inline t
432 (string :tag "Drawer name")))))
434 (defcustom org-export-with-email nil
435 "Non-nil means insert author email into the exported file.
436 This option can also be set with the OPTIONS keyword,
437 e.g. \"email:t\"."
438 :group 'org-export-general
439 :type 'boolean)
441 (defcustom org-export-with-emphasize t
442 "Non-nil means interpret *word*, /word/, _word_ and +word+.
444 If the export target supports emphasizing text, the word will be
445 typeset in bold, italic, with an underline or strike-through,
446 respectively.
448 This option can also be set with the OPTIONS keyword,
449 e.g. \"*:nil\"."
450 :group 'org-export-general
451 :type 'boolean)
453 (defcustom org-export-exclude-tags '("noexport")
454 "Tags that exclude a tree from export.
456 All trees carrying any of these tags will be excluded from
457 export. This is without condition, so even subtrees inside that
458 carry one of the `org-export-select-tags' will be removed.
460 This option can also be set with the EXCLUDE_TAGS keyword."
461 :group 'org-export-general
462 :type '(repeat (string :tag "Tag")))
464 (defcustom org-export-with-fixed-width t
465 "Non-nil means export lines starting with \":\".
466 This option can also be set with the OPTIONS keyword,
467 e.g. \"::nil\"."
468 :group 'org-export-general
469 :version "24.4"
470 :package-version '(Org . "8.0")
471 :type 'boolean)
473 (defcustom org-export-with-footnotes t
474 "Non-nil means Org footnotes should be exported.
475 This option can also be set with the OPTIONS keyword,
476 e.g. \"f:nil\"."
477 :group 'org-export-general
478 :type 'boolean)
480 (defcustom org-export-with-latex t
481 "Non-nil means process LaTeX environments and fragments.
483 This option can also be set with the OPTIONS line,
484 e.g. \"tex:verbatim\". Allowed values are:
486 nil Ignore math snippets.
487 `verbatim' Keep everything in verbatim.
488 t Allow export of math snippets."
489 :group 'org-export-general
490 :version "24.4"
491 :package-version '(Org . "8.0")
492 :type '(choice
493 (const :tag "Do not process math in any way" nil)
494 (const :tag "Interpret math snippets" t)
495 (const :tag "Leave math verbatim" verbatim)))
497 (defcustom org-export-headline-levels 3
498 "The last level which is still exported as a headline.
500 Inferior levels will usually produce itemize or enumerate lists
501 when exported, but back-end behaviour may differ.
503 This option can also be set with the OPTIONS keyword,
504 e.g. \"H:2\"."
505 :group 'org-export-general
506 :type 'integer)
508 (defcustom org-export-default-language "en"
509 "The default language for export and clocktable translations, as a string.
510 This may have an association in
511 `org-clock-clocktable-language-setup',
512 `org-export-smart-quotes-alist' and `org-export-dictionary'.
513 This option can also be set with the LANGUAGE keyword."
514 :group 'org-export-general
515 :type '(string :tag "Language"))
517 (defcustom org-export-preserve-breaks nil
518 "Non-nil means preserve all line breaks when exporting.
519 This option can also be set with the OPTIONS keyword,
520 e.g. \"\\n:t\"."
521 :group 'org-export-general
522 :type 'boolean)
524 (defcustom org-export-with-entities t
525 "Non-nil means interpret entities when exporting.
527 For example, HTML export converts \\alpha to &alpha; and \\AA to
528 &Aring;.
530 For a list of supported names, see the constant `org-entities'
531 and the user option `org-entities-user'.
533 This option can also be set with the OPTIONS keyword,
534 e.g. \"e:nil\"."
535 :group 'org-export-general
536 :type 'boolean)
538 (defcustom org-export-with-inlinetasks t
539 "Non-nil means inlinetasks should be exported.
540 This option can also be set with the OPTIONS keyword,
541 e.g. \"inline:nil\"."
542 :group 'org-export-general
543 :version "24.4"
544 :package-version '(Org . "8.0")
545 :type 'boolean)
547 (defcustom org-export-with-planning nil
548 "Non-nil means include planning info in export.
550 Planning info is the line containing either SCHEDULED:,
551 DEADLINE:, CLOSED: time-stamps, or a combination of them.
553 This option can also be set with the OPTIONS keyword,
554 e.g. \"p:t\"."
555 :group 'org-export-general
556 :version "24.4"
557 :package-version '(Org . "8.0")
558 :type 'boolean)
560 (defcustom org-export-with-priority nil
561 "Non-nil means include priority cookies in export.
562 This option can also be set with the OPTIONS keyword,
563 e.g. \"pri:t\"."
564 :group 'org-export-general
565 :type 'boolean)
567 (defcustom org-export-with-properties nil
568 "Non-nil means export contents of properties drawers.
570 When t, all properties are exported. This may also be a list of
571 properties to export, as strings.
573 This option can also be set with the OPTIONS keyword,
574 e.g. \"prop:t\"."
575 :group 'org-export-general
576 :version "24.4"
577 :package-version '(Org . "8.3")
578 :type '(choice
579 (const :tag "All properties" t)
580 (const :tag "None" nil)
581 (repeat :tag "Selected properties"
582 (string :tag "Property name"))))
584 (defcustom org-export-with-section-numbers t
585 "Non-nil means add section numbers to headlines when exporting.
587 When set to an integer n, numbering will only happen for
588 headlines whose relative level is higher or equal to n.
590 This option can also be set with the OPTIONS keyword,
591 e.g. \"num:t\"."
592 :group 'org-export-general
593 :type 'boolean)
595 (defcustom org-export-select-tags '("export")
596 "Tags that select a tree for export.
598 If any such tag is found in a buffer, all trees that do not carry
599 one of these tags will be ignored during export. Inside trees
600 that are selected like this, you can still deselect a subtree by
601 tagging it with one of the `org-export-exclude-tags'.
603 This option can also be set with the SELECT_TAGS keyword."
604 :group 'org-export-general
605 :type '(repeat (string :tag "Tag")))
607 (defcustom org-export-with-smart-quotes nil
608 "Non-nil means activate smart quotes during export.
609 This option can also be set with the OPTIONS keyword,
610 e.g., \"':t\".
612 When setting this to non-nil, you need to take care of
613 using the correct Babel package when exporting to LaTeX.
614 E.g., you can load Babel for french like this:
616 #+LATEX_HEADER: \\usepackage[french]{babel}"
617 :group 'org-export-general
618 :version "24.4"
619 :package-version '(Org . "8.0")
620 :type 'boolean)
622 (defcustom org-export-with-special-strings t
623 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
625 When this option is turned on, these strings will be exported as:
627 Org HTML LaTeX UTF-8
628 -----+----------+--------+-------
629 \\- &shy; \\-
630 -- &ndash; -- –
631 --- &mdash; --- —
632 ... &hellip; \\ldots …
634 This option can also be set with the OPTIONS keyword,
635 e.g. \"-:nil\"."
636 :group 'org-export-general
637 :type 'boolean)
639 (defcustom org-export-with-statistics-cookies t
640 "Non-nil means include statistics cookies in export.
641 This option can also be set with the OPTIONS keyword,
642 e.g. \"stat:nil\""
643 :group 'org-export-general
644 :version "24.4"
645 :package-version '(Org . "8.0")
646 :type 'boolean)
648 (defcustom org-export-with-sub-superscripts t
649 "Non-nil means interpret \"_\" and \"^\" for export.
651 If you want to control how Org displays those characters, see
652 `org-use-sub-superscripts'. `org-export-with-sub-superscripts'
653 used to be an alias for `org-use-sub-superscripts' in Org <8.0,
654 it is not anymore.
656 When this option is turned on, you can use TeX-like syntax for
657 sub- and superscripts and see them exported correctly.
659 You can also set the option with #+OPTIONS: ^:t
661 Several characters after \"_\" or \"^\" will be considered as a
662 single item - so grouping with {} is normally not needed. For
663 example, the following things will be parsed as single sub- or
664 superscripts:
666 10^24 or 10^tau several digits will be considered 1 item.
667 10^-12 or 10^-tau a leading sign with digits or a word
668 x^2-y^3 will be read as x^2 - y^3, because items are
669 terminated by almost any nonword/nondigit char.
670 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
672 Still, ambiguity is possible. So when in doubt, use {} to enclose
673 the sub/superscript. If you set this variable to the symbol `{}',
674 the braces are *required* in order to trigger interpretations as
675 sub/superscript. This can be helpful in documents that need \"_\"
676 frequently in plain text."
677 :group 'org-export-general
678 :version "24.4"
679 :package-version '(Org . "8.0")
680 :type '(choice
681 (const :tag "Interpret them" t)
682 (const :tag "Curly brackets only" {})
683 (const :tag "Do not interpret them" nil)))
685 (defcustom org-export-with-toc t
686 "Non-nil means create a table of contents in exported files.
688 The TOC contains headlines with levels up
689 to`org-export-headline-levels'. When an integer, include levels
690 up to N in the toc, this may then be different from
691 `org-export-headline-levels', but it will not be allowed to be
692 larger than the number of headline levels. When nil, no table of
693 contents is made.
695 This option can also be set with the OPTIONS keyword,
696 e.g. \"toc:nil\" or \"toc:3\"."
697 :group 'org-export-general
698 :type '(choice
699 (const :tag "No Table of Contents" nil)
700 (const :tag "Full Table of Contents" t)
701 (integer :tag "TOC to level")))
703 (defcustom org-export-with-tables t
704 "Non-nil means export tables.
705 This option can also be set with the OPTIONS keyword,
706 e.g. \"|:nil\"."
707 :group 'org-export-general
708 :version "24.4"
709 :package-version '(Org . "8.0")
710 :type 'boolean)
712 (defcustom org-export-with-tags t
713 "If nil, do not export tags, just remove them from headlines.
715 If this is the symbol `not-in-toc', tags will be removed from
716 table of contents entries, but still be shown in the headlines of
717 the document.
719 This option can also be set with the OPTIONS keyword,
720 e.g. \"tags:nil\"."
721 :group 'org-export-general
722 :type '(choice
723 (const :tag "Off" nil)
724 (const :tag "Not in TOC" not-in-toc)
725 (const :tag "On" t)))
727 (defcustom org-export-with-tasks t
728 "Non-nil means include TODO items for export.
730 This may have the following values:
731 t include tasks independent of state.
732 `todo' include only tasks that are not yet done.
733 `done' include only tasks that are already done.
734 nil ignore all tasks.
735 list of keywords include tasks with these keywords.
737 This option can also be set with the OPTIONS keyword,
738 e.g. \"tasks:nil\"."
739 :group 'org-export-general
740 :type '(choice
741 (const :tag "All tasks" t)
742 (const :tag "No tasks" nil)
743 (const :tag "Not-done tasks" todo)
744 (const :tag "Only done tasks" done)
745 (repeat :tag "Specific TODO keywords"
746 (string :tag "Keyword"))))
748 (defcustom org-export-time-stamp-file t
749 "Non-nil means insert a time stamp into the exported file.
750 The time stamp shows when the file was created. This option can
751 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
752 :group 'org-export-general
753 :type 'boolean)
755 (defcustom org-export-with-timestamps t
756 "Non nil means allow timestamps in export.
758 It can be set to any of the following values:
759 t export all timestamps.
760 `active' export active timestamps only.
761 `inactive' export inactive timestamps only.
762 nil do not export timestamps
764 This only applies to timestamps isolated in a paragraph
765 containing only timestamps. Other timestamps are always
766 exported.
768 This option can also be set with the OPTIONS keyword, e.g.
769 \"<:nil\"."
770 :group 'org-export-general
771 :type '(choice
772 (const :tag "All timestamps" t)
773 (const :tag "Only active timestamps" active)
774 (const :tag "Only inactive timestamps" inactive)
775 (const :tag "No timestamp" nil)))
777 (defcustom org-export-with-todo-keywords t
778 "Non-nil means include TODO keywords in export.
779 When nil, remove all these keywords from the export. This option
780 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
781 :group 'org-export-general
782 :type 'boolean)
784 (defcustom org-export-allow-bind-keywords nil
785 "Non-nil means BIND keywords can define local variable values.
786 This is a potential security risk, which is why the default value
787 is nil. You can also allow them through local buffer variables."
788 :group 'org-export-general
789 :version "24.4"
790 :package-version '(Org . "8.0")
791 :type 'boolean)
793 (defcustom org-export-snippet-translation-alist nil
794 "Alist between export snippets back-ends and exporter back-ends.
796 This variable allows to provide shortcuts for export snippets.
798 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
799 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
800 HTML code while every other back-end will ignore it."
801 :group 'org-export-general
802 :version "24.4"
803 :package-version '(Org . "8.0")
804 :type '(repeat
805 (cons (string :tag "Shortcut")
806 (string :tag "Back-end"))))
808 (defcustom org-export-coding-system nil
809 "Coding system for the exported file."
810 :group 'org-export-general
811 :version "24.4"
812 :package-version '(Org . "8.0")
813 :type 'coding-system)
815 (defcustom org-export-copy-to-kill-ring nil
816 "Non-nil means pushing export output to the kill ring.
817 This variable is ignored during asynchronous export."
818 :group 'org-export-general
819 :version "25.1"
820 :package-version '(Org . "8.3")
821 :type '(choice
822 (const :tag "Always" t)
823 (const :tag "When export is done interactively" if-interactive)
824 (const :tag "Never" nil)))
826 (defcustom org-export-initial-scope 'buffer
827 "The initial scope when exporting with `org-export-dispatch'.
828 This variable can be either set to `buffer' or `subtree'."
829 :group 'org-export-general
830 :type '(choice
831 (const :tag "Export current buffer" buffer)
832 (const :tag "Export current subtree" subtree)))
834 (defcustom org-export-show-temporary-export-buffer t
835 "Non-nil means show buffer after exporting to temp buffer.
836 When Org exports to a file, the buffer visiting that file is never
837 shown, but remains buried. However, when exporting to
838 a temporary buffer, that buffer is popped up in a second window.
839 When this variable is nil, the buffer remains buried also in
840 these cases."
841 :group 'org-export-general
842 :type 'boolean)
844 (defcustom org-export-in-background nil
845 "Non-nil means export and publishing commands will run in background.
846 Results from an asynchronous export are never displayed
847 automatically. But you can retrieve them with \\[org-export-stack]."
848 :group 'org-export-general
849 :version "24.4"
850 :package-version '(Org . "8.0")
851 :type 'boolean)
853 (defcustom org-export-async-init-file nil
854 "File used to initialize external export process.
856 Value must be either nil or an absolute file name. When nil, the
857 external process is launched like a regular Emacs session,
858 loading user's initialization file and any site specific
859 configuration. If a file is provided, it, and only it, is loaded
860 at start-up.
862 Therefore, using a specific configuration makes the process to
863 load faster and the export more portable."
864 :group 'org-export-general
865 :version "24.4"
866 :package-version '(Org . "8.0")
867 :type '(choice
868 (const :tag "Regular startup" nil)
869 (file :tag "Specific start-up file" :must-match t)))
871 (defcustom org-export-dispatch-use-expert-ui nil
872 "Non-nil means using a non-intrusive `org-export-dispatch'.
873 In that case, no help buffer is displayed. Though, an indicator
874 for current export scope is added to the prompt (\"b\" when
875 output is restricted to body only, \"s\" when it is restricted to
876 the current subtree, \"v\" when only visible elements are
877 considered for export, \"f\" when publishing functions should be
878 passed the FORCE argument and \"a\" when the export should be
879 asynchronous). Also, \[?] allows to switch back to standard
880 mode."
881 :group 'org-export-general
882 :version "24.4"
883 :package-version '(Org . "8.0")
884 :type 'boolean)
888 ;;; Defining Back-ends
890 ;; An export back-end is a structure with `org-export-backend' type
891 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
892 ;; and `menu' slots.
894 ;; At the lowest level, a back-end is created with
895 ;; `org-export-create-backend' function.
897 ;; A named back-end can be registered with
898 ;; `org-export-register-backend' function. A registered back-end can
899 ;; later be referred to by its name, with `org-export-get-backend'
900 ;; function. Also, such a back-end can become the parent of a derived
901 ;; back-end from which slot values will be inherited by default.
902 ;; `org-export-derived-backend-p' can check if a given back-end is
903 ;; derived from a list of back-end names.
905 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
906 ;; `org-export-get-all-filters' return the full alist of transcoders,
907 ;; options and filters, including those inherited from ancestors.
909 ;; At a higher level, `org-export-define-backend' is the standard way
910 ;; to define an export back-end. If the new back-end is similar to
911 ;; a registered back-end, `org-export-define-derived-backend' may be
912 ;; used instead.
914 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
915 ;; when a given back-end hasn't been registered yet.
917 (defstruct (org-export-backend (:constructor org-export-create-backend)
918 (:copier nil))
919 name parent transcoders options filters blocks menu)
921 (defun org-export-get-backend (name)
922 "Return export back-end named after NAME.
923 NAME is a symbol. Return nil if no such back-end is found."
924 (catch 'found
925 (dolist (b org-export--registered-backends)
926 (when (eq (org-export-backend-name b) name)
927 (throw 'found b)))))
929 (defun org-export-register-backend (backend)
930 "Register BACKEND as a known export back-end.
931 BACKEND is a structure with `org-export-backend' type."
932 ;; Refuse to register an unnamed back-end.
933 (unless (org-export-backend-name backend)
934 (error "Cannot register a unnamed export back-end"))
935 ;; Refuse to register a back-end with an unknown parent.
936 (let ((parent (org-export-backend-parent backend)))
937 (when (and parent (not (org-export-get-backend parent)))
938 (error "Cannot use unknown \"%s\" back-end as a parent" parent)))
939 ;; Register dedicated export blocks in the parser.
940 (dolist (name (org-export-backend-blocks backend))
941 (add-to-list 'org-element-block-name-alist
942 (cons name 'org-element-export-block-parser)))
943 ;; If a back-end with the same name as BACKEND is already
944 ;; registered, replace it with BACKEND. Otherwise, simply add
945 ;; BACKEND to the list of registered back-ends.
946 (let ((old (org-export-get-backend (org-export-backend-name backend))))
947 (if old (setcar (memq old org-export--registered-backends) backend)
948 (push backend org-export--registered-backends))))
950 (defun org-export-barf-if-invalid-backend (backend)
951 "Signal an error if BACKEND isn't defined."
952 (unless (org-export-backend-p backend)
953 (error "Unknown \"%s\" back-end: Aborting export" backend)))
955 (defun org-export-derived-backend-p (backend &rest backends)
956 "Non-nil if BACKEND is derived from one of BACKENDS.
957 BACKEND is an export back-end, as returned by, e.g.,
958 `org-export-create-backend', or a symbol referring to
959 a registered back-end. BACKENDS is constituted of symbols."
960 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
961 (when backend
962 (catch 'exit
963 (while (org-export-backend-parent backend)
964 (when (memq (org-export-backend-name backend) backends)
965 (throw 'exit t))
966 (setq backend
967 (org-export-get-backend (org-export-backend-parent backend))))
968 (memq (org-export-backend-name backend) backends))))
970 (defun org-export-get-all-transcoders (backend)
971 "Return full translation table for BACKEND.
973 BACKEND is an export back-end, as return by, e.g,,
974 `org-export-create-backend'. Return value is an alist where
975 keys are element or object types, as symbols, and values are
976 transcoders.
978 Unlike to `org-export-backend-transcoders', this function
979 also returns transcoders inherited from parent back-ends,
980 if any."
981 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
982 (when backend
983 (let ((transcoders (org-export-backend-transcoders backend))
984 parent)
985 (while (setq parent (org-export-backend-parent backend))
986 (setq backend (org-export-get-backend parent))
987 (setq transcoders
988 (append transcoders (org-export-backend-transcoders backend))))
989 transcoders)))
991 (defun org-export-get-all-options (backend)
992 "Return export options for BACKEND.
994 BACKEND is an export back-end, as return by, e.g,,
995 `org-export-create-backend'. See `org-export-options-alist'
996 for the shape of the return value.
998 Unlike to `org-export-backend-options', this function also
999 returns options inherited from parent back-ends, if any."
1000 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
1001 (when backend
1002 (let ((options (org-export-backend-options backend))
1003 parent)
1004 (while (setq parent (org-export-backend-parent backend))
1005 (setq backend (org-export-get-backend parent))
1006 (setq options (append options (org-export-backend-options backend))))
1007 options)))
1009 (defun org-export-get-all-filters (backend)
1010 "Return complete list of filters for BACKEND.
1012 BACKEND is an export back-end, as return by, e.g,,
1013 `org-export-create-backend'. Return value is an alist where
1014 keys are symbols and values lists of functions.
1016 Unlike to `org-export-backend-filters', this function also
1017 returns filters inherited from parent back-ends, if any."
1018 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
1019 (when backend
1020 (let ((filters (org-export-backend-filters backend))
1021 parent)
1022 (while (setq parent (org-export-backend-parent backend))
1023 (setq backend (org-export-get-backend parent))
1024 (setq filters (append filters (org-export-backend-filters backend))))
1025 filters)))
1027 (defun org-export-define-backend (backend transcoders &rest body)
1028 "Define a new back-end BACKEND.
1030 TRANSCODERS is an alist between object or element types and
1031 functions handling them.
1033 These functions should return a string without any trailing
1034 space, or nil. They must accept three arguments: the object or
1035 element itself, its contents or nil when it isn't recursive and
1036 the property list used as a communication channel.
1038 Contents, when not nil, are stripped from any global indentation
1039 \(although the relative one is preserved). They also always end
1040 with a single newline character.
1042 If, for a given type, no function is found, that element or
1043 object type will simply be ignored, along with any blank line or
1044 white space at its end. The same will happen if the function
1045 returns the nil value. If that function returns the empty
1046 string, the type will be ignored, but the blank lines or white
1047 spaces will be kept.
1049 In addition to element and object types, one function can be
1050 associated to the `template' (or `inner-template') symbol and
1051 another one to the `plain-text' symbol.
1053 The former returns the final transcoded string, and can be used
1054 to add a preamble and a postamble to document's body. It must
1055 accept two arguments: the transcoded string and the property list
1056 containing export options. A function associated to `template'
1057 will not be applied if export has option \"body-only\".
1058 A function associated to `inner-template' is always applied.
1060 The latter, when defined, is to be called on every text not
1061 recognized as an element or an object. It must accept two
1062 arguments: the text string and the information channel. It is an
1063 appropriate place to protect special chars relative to the
1064 back-end.
1066 BODY can start with pre-defined keyword arguments. The following
1067 keywords are understood:
1069 :export-block
1071 String, or list of strings, representing block names that
1072 will not be parsed. This is used to specify blocks that will
1073 contain raw code specific to the back-end. These blocks
1074 still have to be handled by the relative `export-block' type
1075 translator.
1077 :filters-alist
1079 Alist between filters and function, or list of functions,
1080 specific to the back-end. See `org-export-filters-alist' for
1081 a list of all allowed filters. Filters defined here
1082 shouldn't make a back-end test, as it may prevent back-ends
1083 derived from this one to behave properly.
1085 :menu-entry
1087 Menu entry for the export dispatcher. It should be a list
1088 like:
1090 '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1092 where :
1094 KEY is a free character selecting the back-end.
1096 DESCRIPTION-OR-ORDINAL is either a string or a number.
1098 If it is a string, is will be used to name the back-end in
1099 its menu entry. If it is a number, the following menu will
1100 be displayed as a sub-menu of the back-end with the same
1101 KEY. Also, the number will be used to determine in which
1102 order such sub-menus will appear (lowest first).
1104 ACTION-OR-MENU is either a function or an alist.
1106 If it is an action, it will be called with four
1107 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1108 BODY-ONLY. See `org-export-as' for further explanations on
1109 some of them.
1111 If it is an alist, associations should follow the
1112 pattern:
1114 '(KEY DESCRIPTION ACTION)
1116 where KEY, DESCRIPTION and ACTION are described above.
1118 Valid values include:
1120 '(?m \"My Special Back-end\" my-special-export-function)
1124 '(?l \"Export to LaTeX\"
1125 \(?p \"As PDF file\" org-latex-export-to-pdf)
1126 \(?o \"As PDF file and open\"
1127 \(lambda (a s v b)
1128 \(if a (org-latex-export-to-pdf t s v b)
1129 \(org-open-file
1130 \(org-latex-export-to-pdf nil s v b)))))))
1132 or the following, which will be added to the previous
1133 sub-menu,
1135 '(?l 1
1136 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1137 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1139 :options-alist
1141 Alist between back-end specific properties introduced in
1142 communication channel and how their value are acquired. See
1143 `org-export-options-alist' for more information about
1144 structure of the values."
1145 (declare (indent 1))
1146 (let (blocks filters menu-entry options contents)
1147 (while (keywordp (car body))
1148 (let ((keyword (pop body)))
1149 (case keyword
1150 (:export-block (let ((names (pop body)))
1151 (setq blocks (if (consp names) (mapcar 'upcase names)
1152 (list (upcase names))))))
1153 (:filters-alist (setq filters (pop body)))
1154 (:menu-entry (setq menu-entry (pop body)))
1155 (:options-alist (setq options (pop body)))
1156 (t (error "Unknown keyword: %s" keyword)))))
1157 (org-export-register-backend
1158 (org-export-create-backend :name backend
1159 :transcoders transcoders
1160 :options options
1161 :filters filters
1162 :blocks blocks
1163 :menu menu-entry))))
1165 (defun org-export-define-derived-backend (child parent &rest body)
1166 "Create a new back-end as a variant of an existing one.
1168 CHILD is the name of the derived back-end. PARENT is the name of
1169 the parent back-end.
1171 BODY can start with pre-defined keyword arguments. The following
1172 keywords are understood:
1174 :export-block
1176 String, or list of strings, representing block names that
1177 will not be parsed. This is used to specify blocks that will
1178 contain raw code specific to the back-end. These blocks
1179 still have to be handled by the relative `export-block' type
1180 translator.
1182 :filters-alist
1184 Alist of filters that will overwrite or complete filters
1185 defined in PARENT back-end. See `org-export-filters-alist'
1186 for a list of allowed filters.
1188 :menu-entry
1190 Menu entry for the export dispatcher. See
1191 `org-export-define-backend' for more information about the
1192 expected value.
1194 :options-alist
1196 Alist of back-end specific properties that will overwrite or
1197 complete those defined in PARENT back-end. Refer to
1198 `org-export-options-alist' for more information about
1199 structure of the values.
1201 :translate-alist
1203 Alist of element and object types and transcoders that will
1204 overwrite or complete transcode table from PARENT back-end.
1205 Refer to `org-export-define-backend' for detailed information
1206 about transcoders.
1208 As an example, here is how one could define \"my-latex\" back-end
1209 as a variant of `latex' back-end with a custom template function:
1211 \(org-export-define-derived-backend 'my-latex 'latex
1212 :translate-alist '((template . my-latex-template-fun)))
1214 The back-end could then be called with, for example:
1216 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1217 (declare (indent 2))
1218 (let (blocks filters menu-entry options transcoders contents)
1219 (while (keywordp (car body))
1220 (let ((keyword (pop body)))
1221 (case keyword
1222 (:export-block (let ((names (pop body)))
1223 (setq blocks (if (consp names) (mapcar 'upcase names)
1224 (list (upcase names))))))
1225 (:filters-alist (setq filters (pop body)))
1226 (:menu-entry (setq menu-entry (pop body)))
1227 (:options-alist (setq options (pop body)))
1228 (:translate-alist (setq transcoders (pop body)))
1229 (t (error "Unknown keyword: %s" keyword)))))
1230 (org-export-register-backend
1231 (org-export-create-backend :name child
1232 :parent parent
1233 :transcoders transcoders
1234 :options options
1235 :filters filters
1236 :blocks blocks
1237 :menu menu-entry))))
1241 ;;; The Communication Channel
1243 ;; During export process, every function has access to a number of
1244 ;; properties. They are of two types:
1246 ;; 1. Environment options are collected once at the very beginning of
1247 ;; the process, out of the original buffer and configuration.
1248 ;; Collecting them is handled by `org-export-get-environment'
1249 ;; function.
1251 ;; Most environment options are defined through the
1252 ;; `org-export-options-alist' variable.
1254 ;; 2. Tree properties are extracted directly from the parsed tree,
1255 ;; just before export, by `org-export-collect-tree-properties'.
1257 ;; Here is the full list of properties available during transcode
1258 ;; process, with their category and their value type.
1260 ;; + `:author' :: Author's name.
1261 ;; - category :: option
1262 ;; - type :: string
1264 ;; + `:back-end' :: Current back-end used for transcoding.
1265 ;; - category :: tree
1266 ;; - type :: structure
1268 ;; + `:creator' :: String to write as creation information.
1269 ;; - category :: option
1270 ;; - type :: string
1272 ;; + `:date' :: String to use as date.
1273 ;; - category :: option
1274 ;; - type :: string
1276 ;; + `:description' :: Description text for the current data.
1277 ;; - category :: option
1278 ;; - type :: string
1280 ;; + `:email' :: Author's email.
1281 ;; - category :: option
1282 ;; - type :: string
1284 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1285 ;; process.
1286 ;; - category :: option
1287 ;; - type :: list of strings
1289 ;; + `:export-options' :: List of export options available for current
1290 ;; process.
1291 ;; - category :: none
1292 ;; - type :: list of symbols, among `subtree', `body-only' and
1293 ;; `visible-only'.
1295 ;; + `:exported-data' :: Hash table used for memoizing
1296 ;; `org-export-data'.
1297 ;; - category :: tree
1298 ;; - type :: hash table
1300 ;; + `:filetags' :: List of global tags for buffer. Used by
1301 ;; `org-export-get-tags' to get tags with inheritance.
1302 ;; - category :: option
1303 ;; - type :: list of strings
1305 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1306 ;; their definition, as parsed data. Only non-inlined footnotes
1307 ;; are represented in this alist. Also, every definition isn't
1308 ;; guaranteed to be referenced in the parse tree. The purpose of
1309 ;; this property is to preserve definitions from oblivion
1310 ;; (i.e. when the parse tree comes from a part of the original
1311 ;; buffer), it isn't meant for direct use in a back-end. To
1312 ;; retrieve a definition relative to a reference, use
1313 ;; `org-export-get-footnote-definition' instead.
1314 ;; - category :: option
1315 ;; - type :: alist (STRING . LIST)
1317 ;; + `:headline-levels' :: Maximum level being exported as an
1318 ;; headline. Comparison is done with the relative level of
1319 ;; headlines in the parse tree, not necessarily with their
1320 ;; actual level.
1321 ;; - category :: option
1322 ;; - type :: integer
1324 ;; + `:headline-offset' :: Difference between relative and real level
1325 ;; of headlines in the parse tree. For example, a value of -1
1326 ;; means a level 2 headline should be considered as level
1327 ;; 1 (cf. `org-export-get-relative-level').
1328 ;; - category :: tree
1329 ;; - type :: integer
1331 ;; + `:headline-numbering' :: Alist between headlines and their
1332 ;; numbering, as a list of numbers
1333 ;; (cf. `org-export-get-headline-number').
1334 ;; - category :: tree
1335 ;; - type :: alist (INTEGER . LIST)
1337 ;; + `:id-alist' :: Alist between ID strings and destination file's
1338 ;; path, relative to current directory. It is used by
1339 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1340 ;; external file.
1341 ;; - category :: option
1342 ;; - type :: alist (STRING . STRING)
1344 ;; + `:ignore-list' :: List of elements and objects that should be
1345 ;; ignored during export.
1346 ;; - category :: tree
1347 ;; - type :: list of elements and objects
1349 ;; + `:input-buffer' :: Original buffer name.
1350 ;; - category :: option
1351 ;; - type :: string
1353 ;; + `:input-file' :: Full path to input file, if any.
1354 ;; - category :: option
1355 ;; - type :: string or nil
1357 ;; + `:keywords' :: List of keywords attached to data.
1358 ;; - category :: option
1359 ;; - type :: string
1361 ;; + `:language' :: Default language used for translations.
1362 ;; - category :: option
1363 ;; - type :: string
1365 ;; + `:output-file' :: Full path to output file, if any.
1366 ;; - category :: option
1367 ;; - type :: string or nil
1369 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1370 ;; transcoding.
1371 ;; - category :: option
1372 ;; - type :: list (as returned by `org-element-parse-buffer')
1374 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1375 ;; all line breaks.
1376 ;; - category :: option
1377 ;; - type :: symbol (nil, t)
1379 ;; + `:section-numbers' :: Non-nil means transcoding should add
1380 ;; section numbers to headlines.
1381 ;; - category :: option
1382 ;; - type :: symbol (nil, t)
1384 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1385 ;; in transcoding. When such a tag is present, subtrees without
1386 ;; it are de facto excluded from the process. See
1387 ;; `use-select-tags'.
1388 ;; - category :: option
1389 ;; - type :: list of strings
1391 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1392 ;; a time stamp in the output.
1393 ;; - category :: option
1394 ;; - type :: symbol (nil, t)
1396 ;; + `:translate-alist' :: Alist between element and object types and
1397 ;; transcoding functions relative to the current back-end.
1398 ;; Special keys `inner-template', `template' and `plain-text' are
1399 ;; also possible.
1400 ;; - category :: option
1401 ;; - type :: alist (SYMBOL . FUNCTION)
1403 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1404 ;; also be transcoded. If it is set to the `headline' symbol,
1405 ;; only the archived headline's name is retained.
1406 ;; - category :: option
1407 ;; - type :: symbol (nil, t, `headline')
1409 ;; + `:with-author' :: Non-nil means author's name should be included
1410 ;; in the output.
1411 ;; - category :: option
1412 ;; - type :: symbol (nil, t)
1414 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1415 ;; - category :: option
1416 ;; - type :: symbol (nil, t)
1418 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1419 ;; inserted at the end of the transcoded string. If the value
1420 ;; is `comment', it should be commented.
1421 ;; - category :: option
1422 ;; - type :: symbol (`comment', nil, t)
1424 ;; + `:with-date' :: Non-nil means output should contain a date.
1425 ;; - category :: option
1426 ;; - type :. symbol (nil, t)
1428 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1429 ;; its value is a list of names, only drawers with such names
1430 ;; will be transcoded. If that list starts with `not', drawer
1431 ;; with these names will be skipped.
1432 ;; - category :: option
1433 ;; - type :: symbol (nil, t) or list of strings
1435 ;; + `:with-email' :: Non-nil means output should contain author's
1436 ;; email.
1437 ;; - category :: option
1438 ;; - type :: symbol (nil, t)
1440 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1441 ;; interpreted.
1442 ;; - category :: option
1443 ;; - type :: symbol (nil, t)
1445 ;; + `:with-fixed-width' :: Non-nil if transcoder should export
1446 ;; strings starting with a colon as a fixed-with (verbatim) area.
1447 ;; - category :: option
1448 ;; - type :: symbol (nil, t)
1450 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1451 ;; footnotes.
1452 ;; - category :: option
1453 ;; - type :: symbol (nil, t)
1455 ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
1456 ;; `latex-fragment' objects should appear in export output. When
1457 ;; this property is set to `verbatim', they will be left as-is.
1458 ;; - category :: option
1459 ;; - type :: symbol (`verbatim', nil, t)
1461 ;; + `:with-planning' :: Non-nil means transcoding should include
1462 ;; planning info.
1463 ;; - category :: option
1464 ;; - type :: symbol (nil, t)
1466 ;; + `:with-priority' :: Non-nil means transcoding should include
1467 ;; priority cookies.
1468 ;; - category :: option
1469 ;; - type :: symbol (nil, t)
1471 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1472 ;; plain text.
1473 ;; - category :: option
1474 ;; - type :: symbol (nil, t)
1476 ;; + `:with-special-strings' :: Non-nil means transcoding should
1477 ;; interpret special strings in plain text.
1478 ;; - category :: option
1479 ;; - type :: symbol (nil, t)
1481 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1482 ;; interpret subscript and superscript. With a value of "{}",
1483 ;; only interpret those using curly brackets.
1484 ;; - category :: option
1485 ;; - type :: symbol (nil, {}, t)
1487 ;; + `:with-tables' :: Non-nil means transcoding should export tables.
1488 ;; - category :: option
1489 ;; - type :: symbol (nil, t)
1491 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1492 ;; headlines. A `not-in-toc' value will remove them from the
1493 ;; table of contents, if any, nonetheless.
1494 ;; - category :: option
1495 ;; - type :: symbol (nil, t, `not-in-toc')
1497 ;; + `:with-tasks' :: Non-nil means transcoding should include
1498 ;; headlines with a TODO keyword. A `todo' value will only
1499 ;; include headlines with a todo type keyword while a `done'
1500 ;; value will do the contrary. If a list of strings is provided,
1501 ;; only tasks with keywords belonging to that list will be kept.
1502 ;; - category :: option
1503 ;; - type :: symbol (t, todo, done, nil) or list of strings
1505 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1506 ;; time stamps. Special value `active' (resp. `inactive') ask to
1507 ;; export only active (resp. inactive) timestamps. Otherwise,
1508 ;; completely remove them.
1509 ;; - category :: option
1510 ;; - type :: symbol: (`active', `inactive', t, nil)
1512 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1513 ;; added to the output. An integer value limits its depth.
1514 ;; - category :: option
1515 ;; - type :: symbol (nil, t or integer)
1517 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1518 ;; include TODO keywords.
1519 ;; - category :: option
1520 ;; - type :: symbol (nil, t)
1523 ;;;; Environment Options
1525 ;; Environment options encompass all parameters defined outside the
1526 ;; scope of the parsed data. They come from five sources, in
1527 ;; increasing precedence order:
1529 ;; - Global variables,
1530 ;; - Buffer's attributes,
1531 ;; - Options keyword symbols,
1532 ;; - Buffer keywords,
1533 ;; - Subtree properties.
1535 ;; The central internal function with regards to environment options
1536 ;; is `org-export-get-environment'. It updates global variables with
1537 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1538 ;; the different sources.
1540 ;; The internal functions doing the retrieval are:
1541 ;; `org-export--get-global-options',
1542 ;; `org-export--get-buffer-attributes',
1543 ;; `org-export--parse-option-keyword',
1544 ;; `org-export--get-subtree-options' and
1545 ;; `org-export--get-inbuffer-options'
1547 ;; Also, `org-export--list-bound-variables' collects bound variables
1548 ;; along with their value in order to set them as buffer local
1549 ;; variables later in the process.
1551 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1552 "Collect export options from the current buffer.
1554 Optional argument BACKEND is an export back-end, as returned by
1555 `org-export-create-backend'.
1557 When optional argument SUBTREEP is non-nil, assume the export is
1558 done against the current sub-tree.
1560 Third optional argument EXT-PLIST is a property list with
1561 external parameters overriding Org default settings, but still
1562 inferior to file-local settings."
1563 ;; First install #+BIND variables since these must be set before
1564 ;; global options are read.
1565 (dolist (pair (org-export--list-bound-variables))
1566 (org-set-local (car pair) (nth 1 pair)))
1567 ;; Get and prioritize export options...
1568 (org-combine-plists
1569 ;; ... from global variables...
1570 (org-export--get-global-options backend)
1571 ;; ... from an external property list...
1572 ext-plist
1573 ;; ... from in-buffer settings...
1574 (org-export--get-inbuffer-options backend)
1575 ;; ... and from subtree, when appropriate.
1576 (and subtreep (org-export--get-subtree-options backend))
1577 ;; Eventually add misc. properties.
1578 (list
1579 :back-end
1580 backend
1581 :translate-alist (org-export-get-all-transcoders backend)
1582 :footnote-definition-alist
1583 ;; Footnotes definitions must be collected in the original
1584 ;; buffer, as there's no insurance that they will still be in
1585 ;; the parse tree, due to possible narrowing.
1586 (let (alist)
1587 (org-with-wide-buffer
1588 (goto-char (point-min))
1589 (while (re-search-forward org-footnote-re nil t)
1590 (backward-char)
1591 (let ((fn (save-match-data (org-element-context))))
1592 (case (org-element-type fn)
1593 (footnote-definition
1594 (push
1595 (cons (org-element-property :label fn)
1596 (let ((cbeg (org-element-property :contents-begin fn)))
1597 (when cbeg
1598 (org-element--parse-elements
1599 cbeg (org-element-property :contents-end fn)
1600 nil nil nil nil (list 'org-data nil)))))
1601 alist))
1602 (footnote-reference
1603 (let ((label (org-element-property :label fn))
1604 (cbeg (org-element-property :contents-begin fn)))
1605 (when (and label cbeg
1606 (eq (org-element-property :type fn) 'inline))
1607 (push
1608 (cons label
1609 (org-element-parse-secondary-string
1610 (buffer-substring
1611 cbeg (org-element-property :contents-end fn))
1612 (org-element-restriction 'footnote-reference)))
1613 alist)))))))
1614 alist))
1615 :id-alist
1616 ;; Collect id references.
1617 (let (alist)
1618 (org-with-wide-buffer
1619 (goto-char (point-min))
1620 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1621 (let ((link (org-element-context)))
1622 (when (eq (org-element-type link) 'link)
1623 (let* ((id (org-element-property :path link))
1624 (file (org-id-find-id-file id)))
1625 (when file
1626 (push (cons id (file-relative-name file)) alist)))))))
1627 alist))))
1629 (defun org-export--parse-option-keyword (options &optional backend)
1630 "Parse an OPTIONS line and return values as a plist.
1631 Optional argument BACKEND is an export back-end, as returned by,
1632 e.g., `org-export-create-backend'. It specifies which back-end
1633 specific items to read, if any."
1634 (let* ((all
1635 ;; Priority is given to back-end specific options.
1636 (append (and backend (org-export-get-all-options backend))
1637 org-export-options-alist))
1638 plist)
1639 (dolist (option all)
1640 (let ((property (car option))
1641 (item (nth 2 option)))
1642 (when (and item
1643 (not (plist-member plist property))
1644 (string-match (concat "\\(\\`\\|[ \t]\\)"
1645 (regexp-quote item)
1646 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1647 options))
1648 (setq plist (plist-put plist
1649 property
1650 (car (read-from-string
1651 (match-string 2 options))))))))
1652 plist))
1654 (defun org-export--get-subtree-options (&optional backend)
1655 "Get export options in subtree at point.
1656 Optional argument BACKEND is an export back-end, as returned by,
1657 e.g., `org-export-create-backend'. It specifies back-end used
1658 for export. Return options as a plist."
1659 ;; For each buffer keyword, create a headline property setting the
1660 ;; same property in communication channel. The name for the property
1661 ;; is the keyword with "EXPORT_" appended to it.
1662 (org-with-wide-buffer
1663 (let (prop plist)
1664 ;; Make sure point is at a heading.
1665 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t))
1666 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1667 ;; title (with no todo keyword, priority cookie or tag) as its
1668 ;; fallback value.
1669 (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
1670 (progn (looking-at org-complex-heading-regexp)
1671 (org-match-string-no-properties 4))))
1672 (setq plist
1673 (plist-put
1674 plist :title
1675 (org-element-parse-secondary-string
1676 prop (org-element-restriction 'keyword)))))
1677 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1678 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1679 (setq plist
1680 (nconc plist (org-export--parse-option-keyword prop backend))))
1681 ;; Handle other keywords. TITLE keyword is excluded as it has
1682 ;; been handled already.
1683 (let ((seen '("TITLE")))
1684 (mapc
1685 (lambda (option)
1686 (let ((property (car option))
1687 (keyword (nth 1 option)))
1688 (when (and keyword (not (member keyword seen)))
1689 (let* ((subtree-prop (concat "EXPORT_" keyword))
1690 ;; Export properties are not case-sensitive.
1691 (value (let ((case-fold-search t))
1692 (org-entry-get (point) subtree-prop))))
1693 (push keyword seen)
1694 (when (and value (not (plist-member plist property)))
1695 (setq plist
1696 (plist-put
1697 plist
1698 property
1699 (cond
1700 ;; Parse VALUE if required.
1701 ((member keyword org-element-document-properties)
1702 (org-element-parse-secondary-string
1703 value (org-element-restriction 'keyword)))
1704 ;; If BEHAVIOR is `split' expected value is
1705 ;; a list of strings, not a string.
1706 ((eq (nth 4 option) 'split) (org-split-string value))
1707 (t value)))))))))
1708 ;; Look for both general keywords and back-end specific
1709 ;; options, with priority given to the latter.
1710 (append (and backend (org-export-get-all-options backend))
1711 org-export-options-alist)))
1712 ;; Return value.
1713 plist)))
1715 (defun org-export--get-inbuffer-options (&optional backend)
1716 "Return current buffer export options, as a plist.
1718 Optional argument BACKEND, when non-nil, is an export back-end,
1719 as returned by, e.g., `org-export-create-backend'. It specifies
1720 which back-end specific options should also be read in the
1721 process.
1723 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1724 (let* (plist
1725 get-options ; For byte-compiler.
1726 (case-fold-search t)
1727 (options (append
1728 ;; Priority is given to back-end specific options.
1729 (and backend (org-export-get-all-options backend))
1730 org-export-options-alist))
1731 (regexp (format "^[ \t]*#\\+%s:"
1732 (regexp-opt (nconc (delq nil (mapcar 'cadr options))
1733 org-export-special-keywords))))
1734 (find-properties
1735 (lambda (keyword)
1736 ;; Return all properties associated to KEYWORD.
1737 (let (properties)
1738 (dolist (option options properties)
1739 (when (equal (nth 1 option) keyword)
1740 (pushnew (car option) properties))))))
1741 (get-options
1742 (lambda (&optional files plist)
1743 ;; Recursively read keywords in buffer. FILES is a list
1744 ;; of files read so far. PLIST is the current property
1745 ;; list obtained.
1746 (org-with-wide-buffer
1747 (goto-char (point-min))
1748 (while (re-search-forward regexp nil t)
1749 (let ((element (org-element-at-point)))
1750 (when (eq (org-element-type element) 'keyword)
1751 (let ((key (org-element-property :key element))
1752 (val (org-element-property :value element)))
1753 (cond
1754 ;; Options in `org-export-special-keywords'.
1755 ((equal key "SETUPFILE")
1756 (let ((file (expand-file-name
1757 (org-remove-double-quotes (org-trim val)))))
1758 ;; Avoid circular dependencies.
1759 (unless (member file files)
1760 (with-temp-buffer
1761 (insert (org-file-contents file 'noerror))
1762 (let ((org-inhibit-startup t)) (org-mode))
1763 (setq plist (funcall get-options
1764 (cons file files) plist))))))
1765 ((equal key "OPTIONS")
1766 (setq plist
1767 (org-combine-plists
1768 plist
1769 (org-export--parse-option-keyword val backend))))
1770 ((equal key "FILETAGS")
1771 (setq plist
1772 (org-combine-plists
1773 plist
1774 (list :filetags
1775 (org-uniquify
1776 (append (org-split-string val ":")
1777 (plist-get plist :filetags)))))))
1779 ;; Options in `org-export-options-alist'.
1780 (dolist (property (funcall find-properties key))
1781 (let ((behaviour (nth 4 (assq property options))))
1782 (setq plist
1783 (plist-put
1784 plist property
1785 ;; Handle value depending on specified
1786 ;; BEHAVIOR.
1787 (case behaviour
1788 (space
1789 (if (not (plist-get plist property))
1790 (org-trim val)
1791 (concat (plist-get plist property)
1793 (org-trim val))))
1794 (newline
1795 (org-trim
1796 (concat (plist-get plist property)
1797 "\n"
1798 (org-trim val))))
1799 (split `(,@(plist-get plist property)
1800 ,@(org-split-string val)))
1801 ('t val)
1802 (otherwise
1803 (if (not (plist-member plist property)) val
1804 (plist-get plist property))))))))))))))
1805 ;; Return final value.
1806 plist))))
1807 ;; Read options in the current buffer.
1808 (setq plist (funcall get-options
1809 (and buffer-file-name (list buffer-file-name)) nil))
1810 ;; Parse keywords specified in `org-element-document-properties'
1811 ;; and return PLIST.
1812 (dolist (keyword org-element-document-properties plist)
1813 (dolist (property (funcall find-properties keyword))
1814 (let ((value (plist-get plist property)))
1815 (when (stringp value)
1816 (setq plist
1817 (plist-put plist property
1818 (org-element-parse-secondary-string
1819 value (org-element-restriction 'keyword))))))))))
1821 (defun org-export--get-buffer-attributes ()
1822 "Return properties related to buffer attributes, as a plist."
1823 (list :input-buffer (buffer-name (buffer-base-buffer))
1824 :input-file (buffer-file-name (buffer-base-buffer))))
1826 (defun org-export--get-global-options (&optional backend)
1827 "Return global export options as a plist.
1828 Optional argument BACKEND, if non-nil, is an export back-end, as
1829 returned by, e.g., `org-export-create-backend'. It specifies
1830 which back-end specific export options should also be read in the
1831 process."
1832 (let (plist
1833 ;; Priority is given to back-end specific options.
1834 (all (append (and backend (org-export-get-all-options backend))
1835 org-export-options-alist)))
1836 (dolist (cell all plist)
1837 (let ((prop (car cell)))
1838 (unless (plist-member plist prop)
1839 (setq plist
1840 (plist-put
1841 plist
1842 prop
1843 ;; Evaluate default value provided. If keyword is
1844 ;; a member of `org-element-document-properties',
1845 ;; parse it as a secondary string before storing it.
1846 (let ((value (eval (nth 3 cell))))
1847 (if (and (stringp value)
1848 (member (nth 1 cell)
1849 org-element-document-properties))
1850 (org-element-parse-secondary-string
1851 value (org-element-restriction 'keyword))
1852 value)))))))))
1854 (defun org-export--list-bound-variables ()
1855 "Return variables bound from BIND keywords in current buffer.
1856 Also look for BIND keywords in setup files. The return value is
1857 an alist where associations are (VARIABLE-NAME VALUE)."
1858 (when org-export-allow-bind-keywords
1859 (let* (collect-bind ; For byte-compiler.
1860 (collect-bind
1861 (lambda (files alist)
1862 ;; Return an alist between variable names and their
1863 ;; value. FILES is a list of setup files names read so
1864 ;; far, used to avoid circular dependencies. ALIST is
1865 ;; the alist collected so far.
1866 (let ((case-fold-search t))
1867 (org-with-wide-buffer
1868 (goto-char (point-min))
1869 (while (re-search-forward
1870 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t)
1871 (let ((element (org-element-at-point)))
1872 (when (eq (org-element-type element) 'keyword)
1873 (let ((val (org-element-property :value element)))
1874 (if (equal (org-element-property :key element) "BIND")
1875 (push (read (format "(%s)" val)) alist)
1876 ;; Enter setup file.
1877 (let ((file (expand-file-name
1878 (org-remove-double-quotes val))))
1879 (unless (member file files)
1880 (with-temp-buffer
1881 (let ((org-inhibit-startup t)) (org-mode))
1882 (insert (org-file-contents file 'noerror))
1883 (setq alist
1884 (funcall collect-bind
1885 (cons file files)
1886 alist))))))))))
1887 alist)))))
1888 ;; Return value in appropriate order of appearance.
1889 (nreverse (funcall collect-bind nil nil)))))
1891 ;; defsubst org-export-get-parent must be defined before first use,
1892 ;; was originally defined in the topology section
1894 (defsubst org-export-get-parent (blob)
1895 "Return BLOB parent or nil.
1896 BLOB is the element or object considered."
1897 (org-element-property :parent blob))
1899 ;;;; Tree Properties
1901 ;; Tree properties are information extracted from parse tree. They
1902 ;; are initialized at the beginning of the transcoding process by
1903 ;; `org-export-collect-tree-properties'.
1905 ;; Dedicated functions focus on computing the value of specific tree
1906 ;; properties during initialization. Thus,
1907 ;; `org-export--populate-ignore-list' lists elements and objects that
1908 ;; should be skipped during export, `org-export--get-min-level' gets
1909 ;; the minimal exportable level, used as a basis to compute relative
1910 ;; level for headlines. Eventually
1911 ;; `org-export--collect-headline-numbering' builds an alist between
1912 ;; headlines and their numbering.
1914 (defun org-export-collect-tree-properties (data info)
1915 "Extract tree properties from parse tree.
1917 DATA is the parse tree from which information is retrieved. INFO
1918 is a list holding export options.
1920 Following tree properties are set or updated:
1922 `:exported-data' Hash table used to memoize results from
1923 `org-export-data'.
1925 `:footnote-definition-alist' List of footnotes definitions in
1926 original buffer and current parse tree.
1928 `:headline-offset' Offset between true level of headlines and
1929 local level. An offset of -1 means a headline
1930 of level 2 should be considered as a level
1931 1 headline in the context.
1933 `:headline-numbering' Alist of all headlines as key an the
1934 associated numbering as value.
1936 `:ignore-list' List of elements that should be ignored during
1937 export.
1939 Return updated plist."
1940 ;; Install the parse tree in the communication channel, in order to
1941 ;; use `org-export-get-genealogy' and al.
1942 (setq info (plist-put info :parse-tree data))
1943 ;; Get the list of elements and objects to ignore, and put it into
1944 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1945 ;; been done during parse tree filtering.
1946 (setq info
1947 (plist-put info
1948 :ignore-list
1949 (append (org-export--populate-ignore-list data info)
1950 (plist-get info :ignore-list))))
1951 ;; Compute `:headline-offset' in order to be able to use
1952 ;; `org-export-get-relative-level'.
1953 (setq info
1954 (plist-put info
1955 :headline-offset
1956 (- 1 (org-export--get-min-level data info))))
1957 ;; Footnote definitions in parse tree override those stored in
1958 ;; `:footnote-definition-alist'. This way, any change to
1959 ;; a definition in the parse tree (e.g., through a parse tree
1960 ;; filter) propagates into the alist.
1961 (let ((defs (plist-get info :footnote-definition-alist)))
1962 (org-element-map data '(footnote-definition footnote-reference)
1963 (lambda (fn)
1964 (cond ((eq (org-element-type fn) 'footnote-definition)
1965 (push (cons (org-element-property :label fn)
1966 (append '(org-data nil) (org-element-contents fn)))
1967 defs))
1968 ((eq (org-element-property :type fn) 'inline)
1969 (push (cons (org-element-property :label fn)
1970 (org-element-contents fn))
1971 defs)))))
1972 (setq info (plist-put info :footnote-definition-alist defs)))
1973 ;; Properties order doesn't matter: get the rest of the tree
1974 ;; properties.
1975 (nconc
1976 `(:headline-numbering ,(org-export--collect-headline-numbering data info)
1977 :unnumbered-headline-id ,(org-export--collect-unnumbered-headline-id data info)
1978 :exported-data ,(make-hash-table :test 'eq :size 4001))
1979 info))
1981 (defun org-export--get-min-level (data options)
1982 "Return minimum exportable headline's level in DATA.
1983 DATA is parsed tree as returned by `org-element-parse-buffer'.
1984 OPTIONS is a plist holding export options."
1985 (catch 'exit
1986 (let ((min-level 10000))
1987 (mapc
1988 (lambda (blob)
1989 (when (and (eq (org-element-type blob) 'headline)
1990 (not (org-element-property :footnote-section-p blob))
1991 (not (memq blob (plist-get options :ignore-list))))
1992 (setq min-level (min (org-element-property :level blob) min-level)))
1993 (when (= min-level 1) (throw 'exit 1)))
1994 (org-element-contents data))
1995 ;; If no headline was found, for the sake of consistency, set
1996 ;; minimum level to 1 nonetheless.
1997 (if (= min-level 10000) 1 min-level))))
1999 (defun org-export--collect-headline-numbering (data options)
2000 "Return numbering of all exportable, numbered headlines in a parse tree.
2002 DATA is the parse tree. OPTIONS is the plist holding export
2003 options.
2005 Return an alist whose key is a headline and value is its
2006 associated numbering \(in the shape of a list of numbers\) or nil
2007 for a footnotes section."
2008 (let ((numbering (make-vector org-export-max-depth 0)))
2009 (org-element-map data 'headline
2010 (lambda (headline)
2011 (when (and (org-export-numbered-headline-p headline options)
2012 (not (org-element-property :footnote-section-p headline)))
2013 (let ((relative-level
2014 (1- (org-export-get-relative-level headline options))))
2015 (cons
2016 headline
2017 (loop for n across numbering
2018 for idx from 0 to org-export-max-depth
2019 when (< idx relative-level) collect n
2020 when (= idx relative-level) collect (aset numbering idx (1+ n))
2021 when (> idx relative-level) do (aset numbering idx 0))))))
2022 options)))
2024 (defun org-export--collect-unnumbered-headline-id (data options)
2025 "Return numbering of all exportable, unnumbered headlines.
2026 DATA is the parse tree. OPTIONS is the plist holding export
2027 options. Unnumbered headlines are numbered as a function of
2028 occurrence."
2029 (let ((num 0))
2030 (org-element-map data 'headline
2031 (lambda (headline)
2032 (unless (org-export-numbered-headline-p headline options)
2033 (list headline (incf num)))))))
2035 (defun org-export--populate-ignore-list (data options)
2036 "Return list of elements and objects to ignore during export.
2037 DATA is the parse tree to traverse. OPTIONS is the plist holding
2038 export options."
2039 (let* (walk-data
2040 ;; First find trees containing a select tag, if any.
2041 (selected (org-export--selected-trees data options))
2042 ;; If a select tag is active, also ignore the section before
2043 ;; the first headline, if any.
2044 (ignore (and selected
2045 (let ((first-element (car (org-element-contents data))))
2046 (and (eq (org-element-type first-element) 'section)
2047 first-element))))
2048 (walk-data
2049 (lambda (data)
2050 ;; Collect ignored elements or objects into IGNORE-LIST.
2051 (let ((type (org-element-type data)))
2052 (if (org-export--skip-p data options selected) (push data ignore)
2053 (if (and (eq type 'headline)
2054 (eq (plist-get options :with-archived-trees) 'headline)
2055 (org-element-property :archivedp data))
2056 ;; If headline is archived but tree below has
2057 ;; to be skipped, add it to ignore list.
2058 (dolist (element (org-element-contents data))
2059 (push element ignore))
2060 ;; Move into secondary string, if any.
2061 (let ((sec-prop
2062 (cdr (assq type org-element-secondary-value-alist))))
2063 (when sec-prop
2064 (mapc walk-data (org-element-property sec-prop data))))
2065 ;; Move into recursive objects/elements.
2066 (mapc walk-data (org-element-contents data))))))))
2067 ;; Main call.
2068 (funcall walk-data data)
2069 ;; Return value.
2070 ignore))
2072 (defun org-export--selected-trees (data info)
2073 "Return list of headlines and inlinetasks with a select tag in their tree.
2074 DATA is parsed data as returned by `org-element-parse-buffer'.
2075 INFO is a plist holding export options."
2076 (let* (selected-trees
2077 walk-data ; For byte-compiler.
2078 (walk-data
2079 (function
2080 (lambda (data genealogy)
2081 (let ((type (org-element-type data)))
2082 (cond
2083 ((memq type '(headline inlinetask))
2084 (let ((tags (org-element-property :tags data)))
2085 (if (loop for tag in (plist-get info :select-tags)
2086 thereis (member tag tags))
2087 ;; When a select tag is found, mark full
2088 ;; genealogy and every headline within the tree
2089 ;; as acceptable.
2090 (setq selected-trees
2091 (append
2092 genealogy
2093 (org-element-map data '(headline inlinetask)
2094 'identity)
2095 selected-trees))
2096 ;; If at a headline, continue searching in tree,
2097 ;; recursively.
2098 (when (eq type 'headline)
2099 (mapc (lambda (el)
2100 (funcall walk-data el (cons data genealogy)))
2101 (org-element-contents data))))))
2102 ((or (eq type 'org-data)
2103 (memq type org-element-greater-elements))
2104 (mapc (lambda (el) (funcall walk-data el genealogy))
2105 (org-element-contents data)))))))))
2106 (funcall walk-data data nil)
2107 selected-trees))
2109 (defun org-export--skip-p (blob options selected)
2110 "Non-nil when element or object BLOB should be skipped during export.
2111 OPTIONS is the plist holding export options. SELECTED, when
2112 non-nil, is a list of headlines or inlinetasks belonging to
2113 a tree with a select tag."
2114 (case (org-element-type blob)
2115 (clock (not (plist-get options :with-clocks)))
2116 (drawer
2117 (let ((with-drawers-p (plist-get options :with-drawers)))
2118 (or (not with-drawers-p)
2119 (and (consp with-drawers-p)
2120 ;; If `:with-drawers' value starts with `not', ignore
2121 ;; every drawer whose name belong to that list.
2122 ;; Otherwise, ignore drawers whose name isn't in that
2123 ;; list.
2124 (let ((name (org-element-property :drawer-name blob)))
2125 (if (eq (car with-drawers-p) 'not)
2126 (member-ignore-case name (cdr with-drawers-p))
2127 (not (member-ignore-case name with-drawers-p))))))))
2128 (fixed-width (not (plist-get options :with-fixed-width)))
2129 ((footnote-definition footnote-reference)
2130 (not (plist-get options :with-footnotes)))
2131 ((headline inlinetask)
2132 (let ((with-tasks (plist-get options :with-tasks))
2133 (todo (org-element-property :todo-keyword blob))
2134 (todo-type (org-element-property :todo-type blob))
2135 (archived (plist-get options :with-archived-trees))
2136 (tags (org-element-property :tags blob)))
2138 (and (eq (org-element-type blob) 'inlinetask)
2139 (not (plist-get options :with-inlinetasks)))
2140 ;; Ignore subtrees with an exclude tag.
2141 (loop for k in (plist-get options :exclude-tags)
2142 thereis (member k tags))
2143 ;; When a select tag is present in the buffer, ignore any tree
2144 ;; without it.
2145 (and selected (not (memq blob selected)))
2146 ;; Ignore commented sub-trees.
2147 (org-element-property :commentedp blob)
2148 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
2149 (and (not archived) (org-element-property :archivedp blob))
2150 ;; Ignore tasks, if specified by `:with-tasks' property.
2151 (and todo
2152 (or (not with-tasks)
2153 (and (memq with-tasks '(todo done))
2154 (not (eq todo-type with-tasks)))
2155 (and (consp with-tasks) (not (member todo with-tasks))))))))
2156 ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
2157 (node-property
2158 (let ((properties-set (plist-get options :with-properties)))
2159 (cond ((null properties-set) t)
2160 ((consp properties-set)
2161 (not (member-ignore-case (org-element-property :key blob)
2162 properties-set))))))
2163 (planning (not (plist-get options :with-planning)))
2164 (property-drawer (not (plist-get options :with-properties)))
2165 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
2166 (table (not (plist-get options :with-tables)))
2167 (table-cell
2168 (and (org-export-table-has-special-column-p
2169 (org-export-get-parent-table blob))
2170 (not (org-export-get-previous-element blob options))))
2171 (table-row (org-export-table-row-is-special-p blob options))
2172 (timestamp
2173 ;; `:with-timestamps' only applies to isolated timestamps
2174 ;; objects, i.e. timestamp objects in a paragraph containing only
2175 ;; timestamps and whitespaces.
2176 (when (let ((parent (org-export-get-parent-element blob)))
2177 (and (memq (org-element-type parent) '(paragraph verse-block))
2178 (not (org-element-map parent
2179 (cons 'plain-text
2180 (remq 'timestamp org-element-all-objects))
2181 (lambda (obj)
2182 (or (not (stringp obj)) (org-string-nw-p obj)))
2183 options t))))
2184 (case (plist-get options :with-timestamps)
2185 ('nil t)
2186 (active
2187 (not (memq (org-element-property :type blob) '(active active-range))))
2188 (inactive
2189 (not (memq (org-element-property :type blob)
2190 '(inactive inactive-range)))))))))
2193 ;;; The Transcoder
2195 ;; `org-export-data' reads a parse tree (obtained with, i.e.
2196 ;; `org-element-parse-buffer') and transcodes it into a specified
2197 ;; back-end output. It takes care of filtering out elements or
2198 ;; objects according to export options and organizing the output blank
2199 ;; lines and white space are preserved. The function memoizes its
2200 ;; results, so it is cheap to call it within transcoders.
2202 ;; It is possible to modify locally the back-end used by
2203 ;; `org-export-data' or even use a temporary back-end by using
2204 ;; `org-export-data-with-backend'.
2206 ;; Internally, three functions handle the filtering of objects and
2207 ;; elements during the export. In particular,
2208 ;; `org-export-ignore-element' marks an element or object so future
2209 ;; parse tree traversals skip it and `org-export-expand' transforms
2210 ;; the others back into their original shape.
2212 ;; `org-export-transcoder' is an accessor returning appropriate
2213 ;; translator function for a given element or object.
2215 (defun org-export-transcoder (blob info)
2216 "Return appropriate transcoder for BLOB.
2217 INFO is a plist containing export directives."
2218 (let ((type (org-element-type blob)))
2219 ;; Return contents only for complete parse trees.
2220 (if (eq type 'org-data) (lambda (blob contents info) contents)
2221 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
2222 (and (functionp transcoder) transcoder)))))
2224 (defun org-export-data (data info)
2225 "Convert DATA into current back-end format.
2227 DATA is a parse tree, an element or an object or a secondary
2228 string. INFO is a plist holding export options.
2230 Return a string."
2231 (or (gethash data (plist-get info :exported-data))
2232 (let* ((type (org-element-type data))
2233 (results
2234 (cond
2235 ;; Ignored element/object.
2236 ((memq data (plist-get info :ignore-list)) nil)
2237 ;; Plain text.
2238 ((eq type 'plain-text)
2239 (org-export-filter-apply-functions
2240 (plist-get info :filter-plain-text)
2241 (let ((transcoder (org-export-transcoder data info)))
2242 (if transcoder (funcall transcoder data info) data))
2243 info))
2244 ;; Secondary string.
2245 ((not type)
2246 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
2247 ;; Element/Object without contents or, as a special
2248 ;; case, headline with archive tag and archived trees
2249 ;; restricted to title only.
2250 ((or (not (org-element-contents data))
2251 (and (eq type 'headline)
2252 (eq (plist-get info :with-archived-trees) 'headline)
2253 (org-element-property :archivedp data)))
2254 (let ((transcoder (org-export-transcoder data info)))
2255 (or (and (functionp transcoder)
2256 (funcall transcoder data nil info))
2257 ;; Export snippets never return a nil value so
2258 ;; that white spaces following them are never
2259 ;; ignored.
2260 (and (eq type 'export-snippet) ""))))
2261 ;; Element/Object with contents.
2263 (let ((transcoder (org-export-transcoder data info)))
2264 (when transcoder
2265 (let* ((greaterp (memq type org-element-greater-elements))
2266 (objectp
2267 (and (not greaterp)
2268 (memq type org-element-recursive-objects)))
2269 (contents
2270 (mapconcat
2271 (lambda (element) (org-export-data element info))
2272 (org-element-contents
2273 (if (or greaterp objectp) data
2274 ;; Elements directly containing
2275 ;; objects must have their indentation
2276 ;; normalized first.
2277 (org-element-normalize-contents
2278 data
2279 ;; When normalizing contents of the
2280 ;; first paragraph in an item or
2281 ;; a footnote definition, ignore
2282 ;; first line's indentation: there is
2283 ;; none and it might be misleading.
2284 (when (eq type 'paragraph)
2285 (let ((parent (org-export-get-parent data)))
2286 (and
2287 (eq (car (org-element-contents parent))
2288 data)
2289 (memq (org-element-type parent)
2290 '(footnote-definition item))))))))
2291 "")))
2292 (funcall transcoder data
2293 (if (not greaterp) contents
2294 (org-element-normalize-string contents))
2295 info))))))))
2296 ;; Final result will be memoized before being returned.
2297 (puthash
2298 data
2299 (cond
2300 ((not results) "")
2301 ((memq type '(org-data plain-text nil)) results)
2302 ;; Append the same white space between elements or objects
2303 ;; as in the original buffer, and call appropriate filters.
2305 (let ((results
2306 (org-export-filter-apply-functions
2307 (plist-get info (intern (format ":filter-%s" type)))
2308 (let ((post-blank (or (org-element-property :post-blank data)
2309 0)))
2310 (if (memq type org-element-all-elements)
2311 (concat (org-element-normalize-string results)
2312 (make-string post-blank ?\n))
2313 (concat results (make-string post-blank ?\s))))
2314 info)))
2315 results)))
2316 (plist-get info :exported-data)))))
2318 (defun org-export-data-with-backend (data backend info)
2319 "Convert DATA into BACKEND format.
2321 DATA is an element, an object, a secondary string or a string.
2322 BACKEND is a symbol. INFO is a plist used as a communication
2323 channel.
2325 Unlike to `org-export-with-backend', this function will
2326 recursively convert DATA using BACKEND translation table."
2327 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
2328 (org-export-data
2329 data
2330 ;; Set-up a new communication channel with translations defined in
2331 ;; BACKEND as the translate table and a new hash table for
2332 ;; memoization.
2333 (org-combine-plists
2334 info
2335 (list :back-end backend
2336 :translate-alist (org-export-get-all-transcoders backend)
2337 ;; Size of the hash table is reduced since this function
2338 ;; will probably be used on small trees.
2339 :exported-data (make-hash-table :test 'eq :size 401)))))
2341 (defun org-export-remove-uninterpreted-data (data info)
2342 "Change uninterpreted elements back into Org syntax.
2343 DATA is the parse tree. INFO is a plist containing export
2344 options. Each uninterpreted element or object is changed back
2345 into a string. Contents, if any, are not modified. The parse
2346 tree is modified by side effect and returned by the function."
2347 (org-export--remove-uninterpreted-data-1 data info)
2348 (dolist (prop '(:author :date :title))
2349 (plist-put info
2350 prop
2351 (org-export--remove-uninterpreted-data-1
2352 (plist-get info prop)
2353 info))))
2355 (defun org-export--remove-uninterpreted-data-1 (data info)
2356 "Change uninterpreted elements back into Org syntax.
2357 DATA is a parse tree or a secondary string. INFO is a plist
2358 containing export options. It is modified by side effect and
2359 returned by the function."
2360 (org-element-map data
2361 '(entity bold italic latex-environment latex-fragment strike-through
2362 subscript superscript underline)
2363 #'(lambda (blob)
2364 (let ((new
2365 (case (org-element-type blob)
2366 ;; ... entities...
2367 (entity
2368 (and (not (plist-get info :with-entities))
2369 (list (concat
2370 (org-export-expand blob nil)
2371 (make-string
2372 (or (org-element-property :post-blank blob) 0)
2373 ?\s)))))
2374 ;; ... emphasis...
2375 ((bold italic strike-through underline)
2376 (and (not (plist-get info :with-emphasize))
2377 (let ((marker (case (org-element-type blob)
2378 (bold "*")
2379 (italic "/")
2380 (strike-through "+")
2381 (underline "_"))))
2382 (append
2383 (list marker)
2384 (org-element-contents blob)
2385 (list (concat
2386 marker
2387 (make-string
2388 (or (org-element-property :post-blank blob)
2390 ?\s)))))))
2391 ;; ... LaTeX environments and fragments...
2392 ((latex-environment latex-fragment)
2393 (and (eq (plist-get info :with-latex) 'verbatim)
2394 (list (org-export-expand blob nil))))
2395 ;; ... sub/superscripts...
2396 ((subscript superscript)
2397 (let ((sub/super-p (plist-get info :with-sub-superscript))
2398 (bracketp (org-element-property :use-brackets-p blob)))
2399 (and (or (not sub/super-p)
2400 (and (eq sub/super-p '{}) (not bracketp)))
2401 (append
2402 (list (concat
2403 (if (eq (org-element-type blob) 'subscript)
2405 "^")
2406 (and bracketp "{")))
2407 (org-element-contents blob)
2408 (list (concat
2409 (and bracketp "}")
2410 (and (org-element-property :post-blank blob)
2411 (make-string
2412 (org-element-property :post-blank blob)
2413 ?\s)))))))))))
2414 (when new
2415 ;; Splice NEW at BLOB location in parse tree.
2416 (dolist (e new (org-element-extract-element blob))
2417 (unless (string= e "") (org-element-insert-before e blob))))))
2418 info)
2419 ;; Return modified parse tree.
2420 data)
2422 (defun org-export-expand (blob contents &optional with-affiliated)
2423 "Expand a parsed element or object to its original state.
2425 BLOB is either an element or an object. CONTENTS is its
2426 contents, as a string or nil.
2428 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2429 keywords before output."
2430 (let ((type (org-element-type blob)))
2431 (concat (and with-affiliated (memq type org-element-all-elements)
2432 (org-element--interpret-affiliated-keywords blob))
2433 (funcall (intern (format "org-element-%s-interpreter" type))
2434 blob contents))))
2436 (defun org-export-ignore-element (element info)
2437 "Add ELEMENT to `:ignore-list' in INFO.
2439 Any element in `:ignore-list' will be skipped when using
2440 `org-element-map'. INFO is modified by side effects."
2441 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2445 ;;; The Filter System
2447 ;; Filters allow end-users to tweak easily the transcoded output.
2448 ;; They are the functional counterpart of hooks, as every filter in
2449 ;; a set is applied to the return value of the previous one.
2451 ;; Every set is back-end agnostic. Although, a filter is always
2452 ;; called, in addition to the string it applies to, with the back-end
2453 ;; used as argument, so it's easy for the end-user to add back-end
2454 ;; specific filters in the set. The communication channel, as
2455 ;; a plist, is required as the third argument.
2457 ;; From the developer side, filters sets can be installed in the
2458 ;; process with the help of `org-export-define-backend', which
2459 ;; internally stores filters as an alist. Each association has a key
2460 ;; among the following symbols and a function or a list of functions
2461 ;; as value.
2463 ;; - `:filter-options' applies to the property list containing export
2464 ;; options. Unlike to other filters, functions in this list accept
2465 ;; two arguments instead of three: the property list containing
2466 ;; export options and the back-end. Users can set its value through
2467 ;; `org-export-filter-options-functions' variable.
2469 ;; - `:filter-parse-tree' applies directly to the complete parsed
2470 ;; tree. Users can set it through
2471 ;; `org-export-filter-parse-tree-functions' variable.
2473 ;; - `:filter-body' applies to the body of the output, before template
2474 ;; translator chimes in. Users can set it through
2475 ;; `org-export-filter-body-functions' variable.
2477 ;; - `:filter-final-output' applies to the final transcoded string.
2478 ;; Users can set it with `org-export-filter-final-output-functions'
2479 ;; variable.
2481 ;; - `:filter-plain-text' applies to any string not recognized as Org
2482 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2483 ;; configure it.
2485 ;; - `:filter-TYPE' applies on the string returned after an element or
2486 ;; object of type TYPE has been transcoded. A user can modify
2487 ;; `org-export-filter-TYPE-functions' to install these filters.
2489 ;; All filters sets are applied with
2490 ;; `org-export-filter-apply-functions' function. Filters in a set are
2491 ;; applied in a LIFO fashion. It allows developers to be sure that
2492 ;; their filters will be applied first.
2494 ;; Filters properties are installed in communication channel with
2495 ;; `org-export-install-filters' function.
2497 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2498 ;; `org-export-before-parsing-hook') are run at the beginning of the
2499 ;; export process and just before parsing to allow for heavy structure
2500 ;; modifications.
2503 ;;;; Hooks
2505 (defvar org-export-before-processing-hook nil
2506 "Hook run at the beginning of the export process.
2508 This is run before include keywords and macros are expanded and
2509 Babel code blocks executed, on a copy of the original buffer
2510 being exported. Visibility and narrowing are preserved. Point
2511 is at the beginning of the buffer.
2513 Every function in this hook will be called with one argument: the
2514 back-end currently used, as a symbol.")
2516 (defvar org-export-before-parsing-hook nil
2517 "Hook run before parsing an export buffer.
2519 This is run after include keywords and macros have been expanded
2520 and Babel code blocks executed, on a copy of the original buffer
2521 being exported. Visibility and narrowing are preserved. Point
2522 is at the beginning of the buffer.
2524 Every function in this hook will be called with one argument: the
2525 back-end currently used, as a symbol.")
2528 ;;;; Special Filters
2530 (defvar org-export-filter-options-functions nil
2531 "List of functions applied to the export options.
2532 Each filter is called with two arguments: the export options, as
2533 a plist, and the back-end, as a symbol. It must return
2534 a property list containing export options.")
2536 (defvar org-export-filter-parse-tree-functions nil
2537 "List of functions applied to the parsed tree.
2538 Each filter is called with three arguments: the parse tree, as
2539 returned by `org-element-parse-buffer', the back-end, as
2540 a symbol, and the communication channel, as a plist. It must
2541 return the modified parse tree to transcode.")
2543 (defvar org-export-filter-plain-text-functions nil
2544 "List of functions applied to plain text.
2545 Each filter is called with three arguments: a string which
2546 contains no Org syntax, the back-end, as a symbol, and the
2547 communication channel, as a plist. It must return a string or
2548 nil.")
2550 (defvar org-export-filter-body-functions nil
2551 "List of functions applied to transcoded body.
2552 Each filter is called with three arguments: a string which
2553 contains no Org syntax, the back-end, as a symbol, and the
2554 communication channel, as a plist. It must return a string or
2555 nil.")
2557 (defvar org-export-filter-final-output-functions nil
2558 "List of functions applied to the transcoded string.
2559 Each filter is called with three arguments: the full transcoded
2560 string, the back-end, as a symbol, and the communication channel,
2561 as a plist. It must return a string that will be used as the
2562 final export output.")
2565 ;;;; Elements Filters
2567 (defvar org-export-filter-babel-call-functions nil
2568 "List of functions applied to a transcoded babel-call.
2569 Each filter is called with three arguments: the transcoded data,
2570 as a string, the back-end, as a symbol, and the communication
2571 channel, as a plist. It must return a string or nil.")
2573 (defvar org-export-filter-center-block-functions nil
2574 "List of functions applied to a transcoded center block.
2575 Each filter is called with three arguments: the transcoded data,
2576 as a string, the back-end, as a symbol, and the communication
2577 channel, as a plist. It must return a string or nil.")
2579 (defvar org-export-filter-clock-functions nil
2580 "List of functions applied to a transcoded clock.
2581 Each filter is called with three arguments: the transcoded data,
2582 as a string, the back-end, as a symbol, and the communication
2583 channel, as a plist. It must return a string or nil.")
2585 (defvar org-export-filter-comment-functions nil
2586 "List of functions applied to a transcoded comment.
2587 Each filter is called with three arguments: the transcoded data,
2588 as a string, the back-end, as a symbol, and the communication
2589 channel, as a plist. It must return a string or nil.")
2591 (defvar org-export-filter-comment-block-functions nil
2592 "List of functions applied to a transcoded comment-block.
2593 Each filter is called with three arguments: the transcoded data,
2594 as a string, the back-end, as a symbol, and the communication
2595 channel, as a plist. It must return a string or nil.")
2597 (defvar org-export-filter-diary-sexp-functions nil
2598 "List of functions applied to a transcoded diary-sexp.
2599 Each filter is called with three arguments: the transcoded data,
2600 as a string, the back-end, as a symbol, and the communication
2601 channel, as a plist. It must return a string or nil.")
2603 (defvar org-export-filter-drawer-functions nil
2604 "List of functions applied to a transcoded drawer.
2605 Each filter is called with three arguments: the transcoded data,
2606 as a string, the back-end, as a symbol, and the communication
2607 channel, as a plist. It must return a string or nil.")
2609 (defvar org-export-filter-dynamic-block-functions nil
2610 "List of functions applied to a transcoded dynamic-block.
2611 Each filter is called with three arguments: the transcoded data,
2612 as a string, the back-end, as a symbol, and the communication
2613 channel, as a plist. It must return a string or nil.")
2615 (defvar org-export-filter-example-block-functions nil
2616 "List of functions applied to a transcoded example-block.
2617 Each filter is called with three arguments: the transcoded data,
2618 as a string, the back-end, as a symbol, and the communication
2619 channel, as a plist. It must return a string or nil.")
2621 (defvar org-export-filter-export-block-functions nil
2622 "List of functions applied to a transcoded export-block.
2623 Each filter is called with three arguments: the transcoded data,
2624 as a string, the back-end, as a symbol, and the communication
2625 channel, as a plist. It must return a string or nil.")
2627 (defvar org-export-filter-fixed-width-functions nil
2628 "List of functions applied to a transcoded fixed-width.
2629 Each filter is called with three arguments: the transcoded data,
2630 as a string, the back-end, as a symbol, and the communication
2631 channel, as a plist. It must return a string or nil.")
2633 (defvar org-export-filter-footnote-definition-functions nil
2634 "List of functions applied to a transcoded footnote-definition.
2635 Each filter is called with three arguments: the transcoded data,
2636 as a string, the back-end, as a symbol, and the communication
2637 channel, as a plist. It must return a string or nil.")
2639 (defvar org-export-filter-headline-functions nil
2640 "List of functions applied to a transcoded headline.
2641 Each filter is called with three arguments: the transcoded data,
2642 as a string, the back-end, as a symbol, and the communication
2643 channel, as a plist. It must return a string or nil.")
2645 (defvar org-export-filter-horizontal-rule-functions nil
2646 "List of functions applied to a transcoded horizontal-rule.
2647 Each filter is called with three arguments: the transcoded data,
2648 as a string, the back-end, as a symbol, and the communication
2649 channel, as a plist. It must return a string or nil.")
2651 (defvar org-export-filter-inlinetask-functions nil
2652 "List of functions applied to a transcoded inlinetask.
2653 Each filter is called with three arguments: the transcoded data,
2654 as a string, the back-end, as a symbol, and the communication
2655 channel, as a plist. It must return a string or nil.")
2657 (defvar org-export-filter-item-functions nil
2658 "List of functions applied to a transcoded item.
2659 Each filter is called with three arguments: the transcoded data,
2660 as a string, the back-end, as a symbol, and the communication
2661 channel, as a plist. It must return a string or nil.")
2663 (defvar org-export-filter-keyword-functions nil
2664 "List of functions applied to a transcoded keyword.
2665 Each filter is called with three arguments: the transcoded data,
2666 as a string, the back-end, as a symbol, and the communication
2667 channel, as a plist. It must return a string or nil.")
2669 (defvar org-export-filter-latex-environment-functions nil
2670 "List of functions applied to a transcoded latex-environment.
2671 Each filter is called with three arguments: the transcoded data,
2672 as a string, the back-end, as a symbol, and the communication
2673 channel, as a plist. It must return a string or nil.")
2675 (defvar org-export-filter-node-property-functions nil
2676 "List of functions applied to a transcoded node-property.
2677 Each filter is called with three arguments: the transcoded data,
2678 as a string, the back-end, as a symbol, and the communication
2679 channel, as a plist. It must return a string or nil.")
2681 (defvar org-export-filter-paragraph-functions nil
2682 "List of functions applied to a transcoded paragraph.
2683 Each filter is called with three arguments: the transcoded data,
2684 as a string, the back-end, as a symbol, and the communication
2685 channel, as a plist. It must return a string or nil.")
2687 (defvar org-export-filter-plain-list-functions nil
2688 "List of functions applied to a transcoded plain-list.
2689 Each filter is called with three arguments: the transcoded data,
2690 as a string, the back-end, as a symbol, and the communication
2691 channel, as a plist. It must return a string or nil.")
2693 (defvar org-export-filter-planning-functions nil
2694 "List of functions applied to a transcoded planning.
2695 Each filter is called with three arguments: the transcoded data,
2696 as a string, the back-end, as a symbol, and the communication
2697 channel, as a plist. It must return a string or nil.")
2699 (defvar org-export-filter-property-drawer-functions nil
2700 "List of functions applied to a transcoded property-drawer.
2701 Each filter is called with three arguments: the transcoded data,
2702 as a string, the back-end, as a symbol, and the communication
2703 channel, as a plist. It must return a string or nil.")
2705 (defvar org-export-filter-quote-block-functions nil
2706 "List of functions applied to a transcoded quote block.
2707 Each filter is called with three arguments: the transcoded quote
2708 data, as a string, the back-end, as a symbol, and the
2709 communication channel, as a plist. It must return a string or
2710 nil.")
2712 (defvar org-export-filter-section-functions nil
2713 "List of functions applied to a transcoded section.
2714 Each filter is called with three arguments: the transcoded data,
2715 as a string, the back-end, as a symbol, and the communication
2716 channel, as a plist. It must return a string or nil.")
2718 (defvar org-export-filter-special-block-functions nil
2719 "List of functions applied to a transcoded special block.
2720 Each filter is called with three arguments: the transcoded data,
2721 as a string, the back-end, as a symbol, and the communication
2722 channel, as a plist. It must return a string or nil.")
2724 (defvar org-export-filter-src-block-functions nil
2725 "List of functions applied to a transcoded src-block.
2726 Each filter is called with three arguments: the transcoded data,
2727 as a string, the back-end, as a symbol, and the communication
2728 channel, as a plist. It must return a string or nil.")
2730 (defvar org-export-filter-table-functions nil
2731 "List of functions applied to a transcoded table.
2732 Each filter is called with three arguments: the transcoded data,
2733 as a string, the back-end, as a symbol, and the communication
2734 channel, as a plist. It must return a string or nil.")
2736 (defvar org-export-filter-table-cell-functions nil
2737 "List of functions applied to a transcoded table-cell.
2738 Each filter is called with three arguments: the transcoded data,
2739 as a string, the back-end, as a symbol, and the communication
2740 channel, as a plist. It must return a string or nil.")
2742 (defvar org-export-filter-table-row-functions nil
2743 "List of functions applied to a transcoded table-row.
2744 Each filter is called with three arguments: the transcoded data,
2745 as a string, the back-end, as a symbol, and the communication
2746 channel, as a plist. It must return a string or nil.")
2748 (defvar org-export-filter-verse-block-functions nil
2749 "List of functions applied to a transcoded verse block.
2750 Each filter is called with three arguments: the transcoded data,
2751 as a string, the back-end, as a symbol, and the communication
2752 channel, as a plist. It must return a string or nil.")
2755 ;;;; Objects Filters
2757 (defvar org-export-filter-bold-functions nil
2758 "List of functions applied to transcoded bold text.
2759 Each filter is called with three arguments: the transcoded data,
2760 as a string, the back-end, as a symbol, and the communication
2761 channel, as a plist. It must return a string or nil.")
2763 (defvar org-export-filter-code-functions nil
2764 "List of functions applied to transcoded code text.
2765 Each filter is called with three arguments: the transcoded data,
2766 as a string, the back-end, as a symbol, and the communication
2767 channel, as a plist. It must return a string or nil.")
2769 (defvar org-export-filter-entity-functions nil
2770 "List of functions applied to a transcoded entity.
2771 Each filter is called with three arguments: the transcoded data,
2772 as a string, the back-end, as a symbol, and the communication
2773 channel, as a plist. It must return a string or nil.")
2775 (defvar org-export-filter-export-snippet-functions nil
2776 "List of functions applied to a transcoded export-snippet.
2777 Each filter is called with three arguments: the transcoded data,
2778 as a string, the back-end, as a symbol, and the communication
2779 channel, as a plist. It must return a string or nil.")
2781 (defvar org-export-filter-footnote-reference-functions nil
2782 "List of functions applied to a transcoded footnote-reference.
2783 Each filter is called with three arguments: the transcoded data,
2784 as a string, the back-end, as a symbol, and the communication
2785 channel, as a plist. It must return a string or nil.")
2787 (defvar org-export-filter-inline-babel-call-functions nil
2788 "List of functions applied to a transcoded inline-babel-call.
2789 Each filter is called with three arguments: the transcoded data,
2790 as a string, the back-end, as a symbol, and the communication
2791 channel, as a plist. It must return a string or nil.")
2793 (defvar org-export-filter-inline-src-block-functions nil
2794 "List of functions applied to a transcoded inline-src-block.
2795 Each filter is called with three arguments: the transcoded data,
2796 as a string, the back-end, as a symbol, and the communication
2797 channel, as a plist. It must return a string or nil.")
2799 (defvar org-export-filter-italic-functions nil
2800 "List of functions applied to transcoded italic text.
2801 Each filter is called with three arguments: the transcoded data,
2802 as a string, the back-end, as a symbol, and the communication
2803 channel, as a plist. It must return a string or nil.")
2805 (defvar org-export-filter-latex-fragment-functions nil
2806 "List of functions applied to a transcoded latex-fragment.
2807 Each filter is called with three arguments: the transcoded data,
2808 as a string, the back-end, as a symbol, and the communication
2809 channel, as a plist. It must return a string or nil.")
2811 (defvar org-export-filter-line-break-functions nil
2812 "List of functions applied to a transcoded line-break.
2813 Each filter is called with three arguments: the transcoded data,
2814 as a string, the back-end, as a symbol, and the communication
2815 channel, as a plist. It must return a string or nil.")
2817 (defvar org-export-filter-link-functions nil
2818 "List of functions applied to a transcoded link.
2819 Each filter is called with three arguments: the transcoded data,
2820 as a string, the back-end, as a symbol, and the communication
2821 channel, as a plist. It must return a string or nil.")
2823 (defvar org-export-filter-radio-target-functions nil
2824 "List of functions applied to a transcoded radio-target.
2825 Each filter is called with three arguments: the transcoded data,
2826 as a string, the back-end, as a symbol, and the communication
2827 channel, as a plist. It must return a string or nil.")
2829 (defvar org-export-filter-statistics-cookie-functions nil
2830 "List of functions applied to a transcoded statistics-cookie.
2831 Each filter is called with three arguments: the transcoded data,
2832 as a string, the back-end, as a symbol, and the communication
2833 channel, as a plist. It must return a string or nil.")
2835 (defvar org-export-filter-strike-through-functions nil
2836 "List of functions applied to transcoded strike-through text.
2837 Each filter is called with three arguments: the transcoded data,
2838 as a string, the back-end, as a symbol, and the communication
2839 channel, as a plist. It must return a string or nil.")
2841 (defvar org-export-filter-subscript-functions nil
2842 "List of functions applied to a transcoded subscript.
2843 Each filter is called with three arguments: the transcoded data,
2844 as a string, the back-end, as a symbol, and the communication
2845 channel, as a plist. It must return a string or nil.")
2847 (defvar org-export-filter-superscript-functions nil
2848 "List of functions applied to a transcoded superscript.
2849 Each filter is called with three arguments: the transcoded data,
2850 as a string, the back-end, as a symbol, and the communication
2851 channel, as a plist. It must return a string or nil.")
2853 (defvar org-export-filter-target-functions nil
2854 "List of functions applied to a transcoded target.
2855 Each filter is called with three arguments: the transcoded data,
2856 as a string, the back-end, as a symbol, and the communication
2857 channel, as a plist. It must return a string or nil.")
2859 (defvar org-export-filter-timestamp-functions nil
2860 "List of functions applied to a transcoded timestamp.
2861 Each filter is called with three arguments: the transcoded data,
2862 as a string, the back-end, as a symbol, and the communication
2863 channel, as a plist. It must return a string or nil.")
2865 (defvar org-export-filter-underline-functions nil
2866 "List of functions applied to transcoded underline text.
2867 Each filter is called with three arguments: the transcoded data,
2868 as a string, the back-end, as a symbol, and the communication
2869 channel, as a plist. It must return a string or nil.")
2871 (defvar org-export-filter-verbatim-functions nil
2872 "List of functions applied to transcoded verbatim text.
2873 Each filter is called with three arguments: the transcoded data,
2874 as a string, the back-end, as a symbol, and the communication
2875 channel, as a plist. It must return a string or nil.")
2878 ;;;; Filters Tools
2880 ;; Internal function `org-export-install-filters' installs filters
2881 ;; hard-coded in back-ends (developer filters) and filters from global
2882 ;; variables (user filters) in the communication channel.
2884 ;; Internal function `org-export-filter-apply-functions' takes care
2885 ;; about applying each filter in order to a given data. It ignores
2886 ;; filters returning a nil value but stops whenever a filter returns
2887 ;; an empty string.
2889 (defun org-export-filter-apply-functions (filters value info)
2890 "Call every function in FILTERS.
2892 Functions are called with arguments VALUE, current export
2893 back-end's name and INFO. A function returning a nil value will
2894 be skipped. If it returns the empty string, the process ends and
2895 VALUE is ignored.
2897 Call is done in a LIFO fashion, to be sure that developer
2898 specified filters, if any, are called first."
2899 (catch 'exit
2900 (let* ((backend (plist-get info :back-end))
2901 (backend-name (and backend (org-export-backend-name backend))))
2902 (dolist (filter filters value)
2903 (let ((result (funcall filter value backend-name info)))
2904 (cond ((not result) value)
2905 ((equal value "") (throw 'exit nil))
2906 (t (setq value result))))))))
2908 (defun org-export-install-filters (info)
2909 "Install filters properties in communication channel.
2910 INFO is a plist containing the current communication channel.
2911 Return the updated communication channel."
2912 (let (plist)
2913 ;; Install user-defined filters with `org-export-filters-alist'
2914 ;; and filters already in INFO (through ext-plist mechanism).
2915 (mapc (lambda (p)
2916 (let* ((prop (car p))
2917 (info-value (plist-get info prop))
2918 (default-value (symbol-value (cdr p))))
2919 (setq plist
2920 (plist-put plist prop
2921 ;; Filters in INFO will be called
2922 ;; before those user provided.
2923 (append (if (listp info-value) info-value
2924 (list info-value))
2925 default-value)))))
2926 org-export-filters-alist)
2927 ;; Prepend back-end specific filters to that list.
2928 (mapc (lambda (p)
2929 ;; Single values get consed, lists are appended.
2930 (let ((key (car p)) (value (cdr p)))
2931 (when value
2932 (setq plist
2933 (plist-put
2934 plist key
2935 (if (atom value) (cons value (plist-get plist key))
2936 (append value (plist-get plist key))))))))
2937 (org-export-get-all-filters (plist-get info :back-end)))
2938 ;; Return new communication channel.
2939 (org-combine-plists info plist)))
2943 ;;; Core functions
2945 ;; This is the room for the main function, `org-export-as', along with
2946 ;; its derivative, `org-export-string-as'.
2947 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2948 ;; function should be added to kill ring.
2950 ;; Note that `org-export-as' doesn't really parse the current buffer,
2951 ;; but a copy of it (with the same buffer-local variables and
2952 ;; visibility), where macros and include keywords are expanded and
2953 ;; Babel blocks are executed, if appropriate.
2954 ;; `org-export-with-buffer-copy' macro prepares that copy.
2956 ;; File inclusion is taken care of by
2957 ;; `org-export-expand-include-keyword' and
2958 ;; `org-export--prepare-file-contents'. Structure wise, including
2959 ;; a whole Org file in a buffer often makes little sense. For
2960 ;; example, if the file contains a headline and the include keyword
2961 ;; was within an item, the item should contain the headline. That's
2962 ;; why file inclusion should be done before any structure can be
2963 ;; associated to the file, that is before parsing.
2965 ;; `org-export-insert-default-template' is a command to insert
2966 ;; a default template (or a back-end specific template) at point or in
2967 ;; current subtree.
2969 (defun org-export-copy-buffer ()
2970 "Return a copy of the current buffer.
2971 The copy preserves Org buffer-local variables, visibility and
2972 narrowing."
2973 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2974 (new-buf (generate-new-buffer (buffer-name))))
2975 (with-current-buffer new-buf
2976 (funcall copy-buffer-fun)
2977 (set-buffer-modified-p nil))
2978 new-buf))
2980 (defmacro org-export-with-buffer-copy (&rest body)
2981 "Apply BODY in a copy of the current buffer.
2982 The copy preserves local variables, visibility and contents of
2983 the original buffer. Point is at the beginning of the buffer
2984 when BODY is applied."
2985 (declare (debug t))
2986 (org-with-gensyms (buf-copy)
2987 `(let ((,buf-copy (org-export-copy-buffer)))
2988 (unwind-protect
2989 (with-current-buffer ,buf-copy
2990 (goto-char (point-min))
2991 (progn ,@body))
2992 (and (buffer-live-p ,buf-copy)
2993 ;; Kill copy without confirmation.
2994 (progn (with-current-buffer ,buf-copy
2995 (restore-buffer-modified-p nil))
2996 (kill-buffer ,buf-copy)))))))
2998 (defun org-export--generate-copy-script (buffer)
2999 "Generate a function duplicating BUFFER.
3001 The copy will preserve local variables, visibility, contents and
3002 narrowing of the original buffer. If a region was active in
3003 BUFFER, contents will be narrowed to that region instead.
3005 The resulting function can be evaluated at a later time, from
3006 another buffer, effectively cloning the original buffer there.
3008 The function assumes BUFFER's major mode is `org-mode'."
3009 (with-current-buffer buffer
3010 `(lambda ()
3011 (let ((inhibit-modification-hooks t))
3012 ;; Set major mode. Ignore `org-mode-hook' as it has been run
3013 ;; already in BUFFER.
3014 (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
3015 ;; Copy specific buffer local variables and variables set
3016 ;; through BIND keywords.
3017 ,@(let ((bound-variables (org-export--list-bound-variables))
3018 vars)
3019 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
3020 (when (consp entry)
3021 (let ((var (car entry))
3022 (val (cdr entry)))
3023 (and (not (memq var org-export-ignored-local-variables))
3024 (or (memq var
3025 '(default-directory
3026 buffer-file-name
3027 buffer-file-coding-system))
3028 (assq var bound-variables)
3029 (string-match "^\\(org-\\|orgtbl-\\)"
3030 (symbol-name var)))
3031 ;; Skip unreadable values, as they cannot be
3032 ;; sent to external process.
3033 (or (not val) (ignore-errors (read (format "%S" val))))
3034 (push `(set (make-local-variable (quote ,var))
3035 (quote ,val))
3036 vars))))))
3037 ;; Whole buffer contents.
3038 (insert
3039 ,(org-with-wide-buffer
3040 (buffer-substring-no-properties
3041 (point-min) (point-max))))
3042 ;; Narrowing.
3043 ,(if (org-region-active-p)
3044 `(narrow-to-region ,(region-beginning) ,(region-end))
3045 `(narrow-to-region ,(point-min) ,(point-max)))
3046 ;; Current position of point.
3047 (goto-char ,(point))
3048 ;; Overlays with invisible property.
3049 ,@(let (ov-set)
3050 (mapc
3051 (lambda (ov)
3052 (let ((invis-prop (overlay-get ov 'invisible)))
3053 (when invis-prop
3054 (push `(overlay-put
3055 (make-overlay ,(overlay-start ov)
3056 ,(overlay-end ov))
3057 'invisible (quote ,invis-prop))
3058 ov-set))))
3059 (overlays-in (point-min) (point-max)))
3060 ov-set)))))
3062 ;;;###autoload
3063 (defun org-export-as
3064 (backend &optional subtreep visible-only body-only ext-plist)
3065 "Transcode current Org buffer into BACKEND code.
3067 BACKEND is either an export back-end, as returned by, e.g.,
3068 `org-export-create-backend', or a symbol referring to
3069 a registered back-end.
3071 If narrowing is active in the current buffer, only transcode its
3072 narrowed part.
3074 If a region is active, transcode that region.
3076 When optional argument SUBTREEP is non-nil, transcode the
3077 sub-tree at point, extracting information from the headline
3078 properties first.
3080 When optional argument VISIBLE-ONLY is non-nil, don't export
3081 contents of hidden elements.
3083 When optional argument BODY-ONLY is non-nil, only return body
3084 code, without surrounding template.
3086 Optional argument EXT-PLIST, when provided, is a property list
3087 with external parameters overriding Org default settings, but
3088 still inferior to file-local settings.
3090 Return code as a string."
3091 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
3092 (org-export-barf-if-invalid-backend backend)
3093 (save-excursion
3094 (save-restriction
3095 ;; Narrow buffer to an appropriate region or subtree for
3096 ;; parsing. If parsing subtree, be sure to remove main headline
3097 ;; too.
3098 (cond ((org-region-active-p)
3099 (narrow-to-region (region-beginning) (region-end)))
3100 (subtreep
3101 (org-narrow-to-subtree)
3102 (goto-char (point-min))
3103 (forward-line)
3104 (narrow-to-region (point) (point-max))))
3105 ;; Initialize communication channel with original buffer
3106 ;; attributes, unavailable in its copy.
3107 (let* ((org-export-current-backend (org-export-backend-name backend))
3108 (info (org-combine-plists
3109 (list :export-options
3110 (delq nil
3111 (list (and subtreep 'subtree)
3112 (and visible-only 'visible-only)
3113 (and body-only 'body-only))))
3114 (org-export--get-buffer-attributes)))
3115 tree)
3116 ;; Update communication channel and get parse tree. Buffer
3117 ;; isn't parsed directly. Instead, a temporary copy is
3118 ;; created, where include keywords, macros are expanded and
3119 ;; code blocks are evaluated.
3120 (org-export-with-buffer-copy
3121 ;; Run first hook with current back-end's name as argument.
3122 (run-hook-with-args 'org-export-before-processing-hook
3123 (org-export-backend-name backend))
3124 (org-export-expand-include-keyword)
3125 ;; Update macro templates since #+INCLUDE keywords might have
3126 ;; added some new ones.
3127 (org-macro-initialize-templates)
3128 (org-macro-replace-all org-macro-templates)
3129 (org-export-execute-babel-code)
3130 ;; Update radio targets since keyword inclusion might have
3131 ;; added some more.
3132 (org-update-radio-target-regexp)
3133 ;; Run last hook with current back-end's name as argument.
3134 (goto-char (point-min))
3135 (save-excursion
3136 (run-hook-with-args 'org-export-before-parsing-hook
3137 (org-export-backend-name backend)))
3138 ;; Update communication channel with environment. Also
3139 ;; install user's and developer's filters.
3140 (setq info
3141 (org-export-install-filters
3142 (org-combine-plists
3143 info (org-export-get-environment backend subtreep ext-plist))))
3144 ;; Expand export-specific set of macros: {{{author}}},
3145 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
3146 ;; once regular macros have been expanded, since document
3147 ;; keywords may contain one of them.
3148 (org-macro-replace-all
3149 (list (cons "author"
3150 (org-element-interpret-data (plist-get info :author)))
3151 (cons "date"
3152 (org-element-interpret-data (plist-get info :date)))
3153 ;; EMAIL is not a parsed keyword: store it as-is.
3154 (cons "email" (or (plist-get info :email) ""))
3155 (cons "title"
3156 (org-element-interpret-data (plist-get info :title))))
3157 'finalize)
3158 ;; Parse buffer.
3159 (setq tree (org-element-parse-buffer nil visible-only))
3160 ;; Handle left-over uninterpreted elements or objects in
3161 ;; parse tree and communication channel.
3162 (org-export-remove-uninterpreted-data tree info)
3163 ;; Call options filters and update export options. We do not
3164 ;; use `org-export-filter-apply-functions' here since the
3165 ;; arity of such filters is different.
3166 (let ((backend-name (org-export-backend-name backend)))
3167 (dolist (filter (plist-get info :filter-options))
3168 (let ((result (funcall filter info backend-name)))
3169 (when result (setq info result)))))
3170 ;; Parse buffer, handle uninterpreted elements or objects,
3171 ;; then call parse-tree filters.
3172 (setq tree
3173 (org-export-filter-apply-functions
3174 (plist-get info :filter-parse-tree) tree info))
3175 ;; Now tree is complete, compute its properties and add them
3176 ;; to communication channel.
3177 (setq info
3178 (org-combine-plists
3179 info (org-export-collect-tree-properties tree info)))
3180 ;; Eventually transcode TREE. Wrap the resulting string into
3181 ;; a template.
3182 (let* ((body (org-element-normalize-string
3183 (or (org-export-data tree info) "")))
3184 (inner-template (cdr (assq 'inner-template
3185 (plist-get info :translate-alist))))
3186 (full-body (org-export-filter-apply-functions
3187 (plist-get info :filter-body)
3188 (if (not (functionp inner-template)) body
3189 (funcall inner-template body info))
3190 info))
3191 (template (cdr (assq 'template
3192 (plist-get info :translate-alist)))))
3193 ;; Remove all text properties since they cannot be
3194 ;; retrieved from an external process. Finally call
3195 ;; final-output filter and return result.
3196 (org-no-properties
3197 (org-export-filter-apply-functions
3198 (plist-get info :filter-final-output)
3199 (if (or (not (functionp template)) body-only) full-body
3200 (funcall template full-body info))
3201 info))))))))
3203 ;;;###autoload
3204 (defun org-export-string-as (string backend &optional body-only ext-plist)
3205 "Transcode STRING into BACKEND code.
3207 BACKEND is either an export back-end, as returned by, e.g.,
3208 `org-export-create-backend', or a symbol referring to
3209 a registered back-end.
3211 When optional argument BODY-ONLY is non-nil, only return body
3212 code, without preamble nor postamble.
3214 Optional argument EXT-PLIST, when provided, is a property list
3215 with external parameters overriding Org default settings, but
3216 still inferior to file-local settings.
3218 Return code as a string."
3219 (with-temp-buffer
3220 (insert string)
3221 (let ((org-inhibit-startup t)) (org-mode))
3222 (org-export-as backend nil nil body-only ext-plist)))
3224 ;;;###autoload
3225 (defun org-export-replace-region-by (backend)
3226 "Replace the active region by its export to BACKEND.
3227 BACKEND is either an export back-end, as returned by, e.g.,
3228 `org-export-create-backend', or a symbol referring to
3229 a registered back-end."
3230 (if (not (org-region-active-p))
3231 (user-error "No active region to replace")
3232 (let* ((beg (region-beginning))
3233 (end (region-end))
3234 (str (buffer-substring beg end)) rpl)
3235 (setq rpl (org-export-string-as str backend t))
3236 (delete-region beg end)
3237 (insert rpl))))
3239 ;;;###autoload
3240 (defun org-export-insert-default-template (&optional backend subtreep)
3241 "Insert all export keywords with default values at beginning of line.
3243 BACKEND is a symbol referring to the name of a registered export
3244 back-end, for which specific export options should be added to
3245 the template, or `default' for default template. When it is nil,
3246 the user will be prompted for a category.
3248 If SUBTREEP is non-nil, export configuration will be set up
3249 locally for the subtree through node properties."
3250 (interactive)
3251 (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
3252 (when (and subtreep (org-before-first-heading-p))
3253 (user-error "No subtree to set export options for"))
3254 (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
3255 (backend
3256 (or backend
3257 (intern
3258 (org-completing-read
3259 "Options category: "
3260 (cons "default"
3261 (mapcar #'(lambda (b)
3262 (symbol-name (org-export-backend-name b)))
3263 org-export--registered-backends))
3264 nil t))))
3265 options keywords)
3266 ;; Populate OPTIONS and KEYWORDS.
3267 (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
3268 ((org-export-backend-p backend)
3269 (org-export-backend-options backend))
3270 (t (org-export-backend-options
3271 (org-export-get-backend backend)))))
3272 (let ((keyword (nth 1 entry))
3273 (option (nth 2 entry)))
3274 (cond
3275 (keyword (unless (assoc keyword keywords)
3276 (let ((value
3277 (if (eq (nth 4 entry) 'split)
3278 (mapconcat #'identity (eval (nth 3 entry)) " ")
3279 (eval (nth 3 entry)))))
3280 (push (cons keyword value) keywords))))
3281 (option (unless (assoc option options)
3282 (push (cons option (eval (nth 3 entry))) options))))))
3283 ;; Move to an appropriate location in order to insert options.
3284 (unless subtreep (beginning-of-line))
3285 ;; First (multiple) OPTIONS lines. Never go past fill-column.
3286 (when options
3287 (let ((items
3288 (mapcar
3289 #'(lambda (opt) (format "%s:%S" (car opt) (cdr opt)))
3290 (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
3291 (if subtreep
3292 (org-entry-put
3293 node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
3294 (while items
3295 (insert "#+OPTIONS:")
3296 (let ((width 10))
3297 (while (and items
3298 (< (+ width (length (car items)) 1) fill-column))
3299 (let ((item (pop items)))
3300 (insert " " item)
3301 (incf width (1+ (length item))))))
3302 (insert "\n")))))
3303 ;; Then the rest of keywords, in the order specified in either
3304 ;; `org-export-options-alist' or respective export back-ends.
3305 (dolist (key (nreverse keywords))
3306 (let ((val (cond ((equal (car key) "DATE")
3307 (or (cdr key)
3308 (with-temp-buffer
3309 (org-insert-time-stamp (current-time)))))
3310 ((equal (car key) "TITLE")
3311 (or (let ((visited-file
3312 (buffer-file-name (buffer-base-buffer))))
3313 (and visited-file
3314 (file-name-sans-extension
3315 (file-name-nondirectory visited-file))))
3316 (buffer-name (buffer-base-buffer))))
3317 (t (cdr key)))))
3318 (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
3319 (insert
3320 (format "#+%s:%s\n"
3321 (car key)
3322 (if (org-string-nw-p val) (format " %s" val) ""))))))))
3324 (defun org-export-expand-include-keyword (&optional included dir)
3325 "Expand every include keyword in buffer.
3326 Optional argument INCLUDED is a list of included file names along
3327 with their line restriction, when appropriate. It is used to
3328 avoid infinite recursion. Optional argument DIR is the current
3329 working directory. It is used to properly resolve relative
3330 paths."
3331 (let ((case-fold-search t)
3332 (file-prefix (make-hash-table :test #'equal))
3333 (current-prefix 0))
3334 (goto-char (point-min))
3335 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
3336 (let ((element (save-match-data (org-element-at-point))))
3337 (when (eq (org-element-type element) 'keyword)
3338 (beginning-of-line)
3339 ;; Extract arguments from keyword's value.
3340 (let* ((value (org-element-property :value element))
3341 (ind (org-get-indentation))
3342 location
3343 (file (and (string-match
3344 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
3345 (prog1
3346 (save-match-data
3347 (let ((matched (match-string 1 value)))
3348 (when (string-match "\\(::\\(.*?\\)\\)\"?\\'" matched)
3349 (setq location (match-string 2 matched))
3350 (setq matched
3351 (replace-match "" nil nil matched 1)))
3352 (expand-file-name
3353 (org-remove-double-quotes
3354 matched)
3355 dir)))
3356 (setq value (replace-match "" nil nil value)))))
3357 (only-contents
3358 (and (string-match ":only-contents *\\([^: \r\t\n]\\S-*\\)?" value)
3359 (prog1 (org-not-nil (match-string 1 value))
3360 (setq value (replace-match "" nil nil value)))))
3361 (lines
3362 (and (string-match
3363 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3364 value)
3365 (prog1 (match-string 1 value)
3366 (setq value (replace-match "" nil nil value)))))
3367 (env (cond ((string-match "\\<example\\>" value)
3368 'literal)
3369 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
3370 'literal)))
3371 ;; Minimal level of included file defaults to the child
3372 ;; level of the current headline, if any, or one. It
3373 ;; only applies is the file is meant to be included as
3374 ;; an Org one.
3375 (minlevel
3376 (and (not env)
3377 (if (string-match ":minlevel +\\([0-9]+\\)" value)
3378 (prog1 (string-to-number (match-string 1 value))
3379 (setq value (replace-match "" nil nil value)))
3380 (let ((cur (org-current-level)))
3381 (if cur (1+ (org-reduced-level cur)) 1)))))
3382 (src-args (and (eq env 'literal)
3383 (match-string 1 value)))
3384 (block (and (string-match "\\<\\(\\S-+\\)\\>" value)
3385 (match-string 1 value))))
3386 ;; Remove keyword.
3387 (delete-region (point) (progn (forward-line) (point)))
3388 (cond
3389 ((not file) nil)
3390 ((not (file-readable-p file))
3391 (error "Cannot include file %s" file))
3392 ;; Check if files has already been parsed. Look after
3393 ;; inclusion lines too, as different parts of the same file
3394 ;; can be included too.
3395 ((member (list file lines) included)
3396 (error "Recursive file inclusion: %s" file))
3398 (cond
3399 ((eq env 'literal)
3400 (insert
3401 (let ((ind-str (make-string ind ? ))
3402 (arg-str (if (stringp src-args)
3403 (format " %s" src-args)
3404 ""))
3405 (contents
3406 (org-escape-code-in-string
3407 (org-export--prepare-file-contents file lines))))
3408 (format "%s#+BEGIN_%s%s\n%s%s#+END_%s\n"
3409 ind-str block arg-str contents ind-str block))))
3410 ((stringp block)
3411 (insert
3412 (let ((ind-str (make-string ind ? ))
3413 (contents
3414 (org-export--prepare-file-contents file lines)))
3415 (format "%s#+BEGIN_%s\n%s%s#+END_%s\n"
3416 ind-str block contents ind-str block))))
3418 (insert
3419 (with-temp-buffer
3420 (let ((org-inhibit-startup t)
3421 (lines
3422 (if location
3423 (org-export--inclusion-absolute-lines
3424 file location only-contents lines)
3425 lines)))
3426 (org-mode)
3427 (insert
3428 (org-export--prepare-file-contents
3429 file lines ind minlevel
3430 (or (gethash file file-prefix)
3431 (puthash file (incf current-prefix) file-prefix)))))
3432 (org-export-expand-include-keyword
3433 (cons (list file lines) included)
3434 (file-name-directory file))
3435 (buffer-string)))))))))))))
3437 (defun org-export--inclusion-absolute-lines (file location only-contents lines)
3438 "Resolve absolute lines for an included file with file-link.
3440 FILE is string file-name of the file to include. LOCATION is a
3441 string name within FILE to be included (located via
3442 `org-link-search'). If ONLY-CONTENTS is non-nil only the
3443 contents of the named element will be included, as determined
3444 Org-Element. If LINES is non-nil only those lines are included.
3446 Return a string of lines to be included in the format expected by
3447 `org-export--prepare-file-contents'."
3448 (with-temp-buffer
3449 (insert-file-contents file)
3450 (unless (eq major-mode 'org-mode)
3451 (let ((org-inhibit-startup t)) (org-mode)))
3452 (condition-case err
3453 ;; Enforce consistent search.
3454 (let ((org-link-search-must-match-exact-headline t))
3455 (org-link-search location))
3456 (error
3457 (error (format "%s for %s::%s" (error-message-string err) file location))))
3458 (let* ((element (org-element-at-point))
3459 (contents-begin
3460 (and only-contents (org-element-property :contents-begin element))))
3461 (narrow-to-region
3462 (or contents-begin (org-element-property :begin element))
3463 (org-element-property (if contents-begin :contents-end :end) element))
3464 (when (and only-contents
3465 (memq (org-element-type element) '(headline inlinetask)))
3466 ;; Skip planning line and property-drawer. If a normal drawer
3467 ;; precedes a property-drawer both will be included.
3468 ;; Remaining property-drawers are removed as needed in
3469 ;; `org-export--prepare-file-contents'.
3470 (goto-char (point-min))
3471 (when (org-looking-at-p org-planning-line-re) (forward-line))
3472 (when (looking-at org-property-drawer-re) (goto-char (match-end 0)))
3473 (unless (bolp) (forward-line))
3474 (narrow-to-region (point) (point-max))))
3475 (when lines
3476 (org-skip-whitespace)
3477 (beginning-of-line)
3478 (let* ((lines (split-string lines "-"))
3479 (lbeg (string-to-number (car lines)))
3480 (lend (string-to-number (cadr lines)))
3481 (beg (if (zerop lbeg) (point-min)
3482 (goto-char (point-min))
3483 (forward-line (1- lbeg))
3484 (point)))
3485 (end (if (zerop lend) (point-max)
3486 (goto-char beg)
3487 (forward-line (1- lend))
3488 (point))))
3489 (narrow-to-region beg end)))
3490 (let ((end (point-max)))
3491 (goto-char (point-min))
3492 (widen)
3493 (let ((start-line (line-number-at-pos)))
3494 (format "%d-%d"
3495 start-line
3496 (save-excursion
3497 (+ start-line
3498 (let ((counter 0))
3499 (while (< (point) end) (incf counter) (forward-line))
3500 counter))))))))
3502 (defun org-export--prepare-file-contents (file &optional lines ind minlevel id)
3503 "Prepare the contents of FILE for inclusion and return them as a string.
3505 When optional argument LINES is a string specifying a range of
3506 lines, include only those lines.
3508 Optional argument IND, when non-nil, is an integer specifying the
3509 global indentation of returned contents. Since its purpose is to
3510 allow an included file to stay in the same environment it was
3511 created \(i.e. a list item), it doesn't apply past the first
3512 headline encountered.
3514 Optional argument MINLEVEL, when non-nil, is an integer
3515 specifying the level that any top-level headline in the included
3516 file should have.
3518 Optional argument ID is an integer that will be inserted before
3519 each footnote definition and reference if FILE is an Org file.
3520 This is useful to avoid conflicts when more than one Org file
3521 with footnotes is included in a document."
3522 (with-temp-buffer
3523 (insert-file-contents file)
3524 (when lines
3525 (let* ((lines (split-string lines "-"))
3526 (lbeg (string-to-number (car lines)))
3527 (lend (string-to-number (cadr lines)))
3528 (beg (if (zerop lbeg) (point-min)
3529 (goto-char (point-min))
3530 (forward-line (1- lbeg))
3531 (point)))
3532 (end (if (zerop lend) (point-max)
3533 (goto-char (point-min))
3534 (forward-line (1- lend))
3535 (point))))
3536 (narrow-to-region beg end)))
3537 ;; Remove blank lines at beginning and end of contents. The logic
3538 ;; behind that removal is that blank lines around include keyword
3539 ;; override blank lines in included file.
3540 (goto-char (point-min))
3541 (org-skip-whitespace)
3542 (beginning-of-line)
3543 (delete-region (point-min) (point))
3544 (goto-char (point-max))
3545 (skip-chars-backward " \r\t\n")
3546 (forward-line)
3547 (delete-region (point) (point-max))
3548 ;; Remove property-drawers after drawers.
3549 (when (or ind minlevel)
3550 (unless (eq major-mode 'org-mode)
3551 (let ((org-inhibit-startup t)) (org-mode)))
3552 (goto-char (point-min))
3553 (when (looking-at org-drawer-regexp)
3554 (goto-char (match-end 0))
3555 (search-forward-regexp org-drawer-regexp)
3556 (forward-line 1)
3557 (beginning-of-line))
3558 (when (looking-at org-property-drawer-re)
3559 (delete-region (match-beginning 0) (match-end 0))
3560 (beginning-of-line))
3561 (delete-region (point) (save-excursion (and (org-skip-whitespace) (point)))))
3562 ;; If IND is set, preserve indentation of include keyword until
3563 ;; the first headline encountered.
3564 (when ind
3565 (unless (eq major-mode 'org-mode)
3566 (let ((org-inhibit-startup t)) (org-mode)))
3567 (goto-char (point-min))
3568 (let ((ind-str (make-string ind ? )))
3569 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
3570 ;; Do not move footnote definitions out of column 0.
3571 (unless (and (looking-at org-footnote-definition-re)
3572 (eq (org-element-type (org-element-at-point))
3573 'footnote-definition))
3574 (insert ind-str))
3575 (forward-line))))
3576 ;; When MINLEVEL is specified, compute minimal level for headlines
3577 ;; in the file (CUR-MIN), and remove stars to each headline so
3578 ;; that headlines with minimal level have a level of MINLEVEL.
3579 (when minlevel
3580 (unless (eq major-mode 'org-mode)
3581 (let ((org-inhibit-startup t)) (org-mode)))
3582 (org-with-limited-levels
3583 (let ((levels (org-map-entries
3584 (lambda () (org-reduced-level (org-current-level))))))
3585 (when levels
3586 (let ((offset (- minlevel (apply 'min levels))))
3587 (unless (zerop offset)
3588 (when org-odd-levels-only (setq offset (* offset 2)))
3589 ;; Only change stars, don't bother moving whole
3590 ;; sections.
3591 (org-map-entries
3592 (lambda () (if (< offset 0) (delete-char (abs offset))
3593 (insert (make-string offset ?*)))))))))))
3594 ;; Append ID to all footnote references and definitions, so they
3595 ;; become file specific and cannot collide with footnotes in other
3596 ;; included files.
3597 (when id
3598 (goto-char (point-min))
3599 (while (re-search-forward org-footnote-re nil t)
3600 (let ((reference (org-element-context)))
3601 (when (memq (org-element-type reference)
3602 '(footnote-reference footnote-definition))
3603 (goto-char (org-element-property :begin reference))
3604 (forward-char)
3605 (let ((label (org-element-property :label reference)))
3606 (cond ((not label))
3607 ((org-string-match-p "\\`[0-9]+\\'" label)
3608 (insert (format "fn:%d-" id)))
3609 (t (forward-char 3) (insert (format "%d-" id)))))))))
3610 (org-element-normalize-string (buffer-string))))
3612 (defun org-export-execute-babel-code ()
3613 "Execute every Babel code in the visible part of current buffer."
3614 ;; Get a pristine copy of current buffer so Babel references can be
3615 ;; properly resolved.
3616 (let ((reference (org-export-copy-buffer)))
3617 (unwind-protect (org-babel-exp-process-buffer reference)
3618 (kill-buffer reference))))
3620 (defun org-export--copy-to-kill-ring-p ()
3621 "Return a non-nil value when output should be added to the kill ring.
3622 See also `org-export-copy-to-kill-ring'."
3623 (if (eq org-export-copy-to-kill-ring 'if-interactive)
3624 (not (or executing-kbd-macro noninteractive))
3625 (eq org-export-copy-to-kill-ring t)))
3629 ;;; Tools For Back-Ends
3631 ;; A whole set of tools is available to help build new exporters. Any
3632 ;; function general enough to have its use across many back-ends
3633 ;; should be added here.
3635 ;;;; For Affiliated Keywords
3637 ;; `org-export-read-attribute' reads a property from a given element
3638 ;; as a plist. It can be used to normalize affiliated keywords'
3639 ;; syntax.
3641 ;; Since captions can span over multiple lines and accept dual values,
3642 ;; their internal representation is a bit tricky. Therefore,
3643 ;; `org-export-get-caption' transparently returns a given element's
3644 ;; caption as a secondary string.
3646 (defun org-export-read-attribute (attribute element &optional property)
3647 "Turn ATTRIBUTE property from ELEMENT into a plist.
3649 When optional argument PROPERTY is non-nil, return the value of
3650 that property within attributes.
3652 This function assumes attributes are defined as \":keyword
3653 value\" pairs. It is appropriate for `:attr_html' like
3654 properties.
3656 All values will become strings except the empty string and
3657 \"nil\", which will become nil. Also, values containing only
3658 double quotes will be read as-is, which means that \"\" value
3659 will become the empty string."
3660 (let* ((prepare-value
3661 (lambda (str)
3662 (save-match-data
3663 (cond ((member str '(nil "" "nil")) nil)
3664 ((string-match "^\"\\(\"+\\)?\"$" str)
3665 (or (match-string 1 str) ""))
3666 (t str)))))
3667 (attributes
3668 (let ((value (org-element-property attribute element)))
3669 (when value
3670 (let ((s (mapconcat 'identity value " ")) result)
3671 (while (string-match
3672 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3674 (let ((value (substring s 0 (match-beginning 0))))
3675 (push (funcall prepare-value value) result))
3676 (push (intern (match-string 1 s)) result)
3677 (setq s (substring s (match-end 0))))
3678 ;; Ignore any string before first property with `cdr'.
3679 (cdr (nreverse (cons (funcall prepare-value s) result))))))))
3680 (if property (plist-get attributes property) attributes)))
3682 (defun org-export-get-caption (element &optional shortp)
3683 "Return caption from ELEMENT as a secondary string.
3685 When optional argument SHORTP is non-nil, return short caption,
3686 as a secondary string, instead.
3688 Caption lines are separated by a white space."
3689 (let ((full-caption (org-element-property :caption element)) caption)
3690 (dolist (line full-caption (cdr caption))
3691 (let ((cap (funcall (if shortp 'cdr 'car) line)))
3692 (when cap
3693 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
3696 ;;;; For Derived Back-ends
3698 ;; `org-export-with-backend' is a function allowing to locally use
3699 ;; another back-end to transcode some object or element. In a derived
3700 ;; back-end, it may be used as a fall-back function once all specific
3701 ;; cases have been treated.
3703 (defun org-export-with-backend (backend data &optional contents info)
3704 "Call a transcoder from BACKEND on DATA.
3705 BACKEND is an export back-end, as returned by, e.g.,
3706 `org-export-create-backend', or a symbol referring to
3707 a registered back-end. DATA is an Org element, object, secondary
3708 string or string. CONTENTS, when non-nil, is the transcoded
3709 contents of DATA element, as a string. INFO, when non-nil, is
3710 the communication channel used for export, as a plist."
3711 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
3712 (org-export-barf-if-invalid-backend backend)
3713 (let ((type (org-element-type data)))
3714 (if (memq type '(nil org-data)) (error "No foreign transcoder available")
3715 (let* ((all-transcoders (org-export-get-all-transcoders backend))
3716 (transcoder (cdr (assq type all-transcoders))))
3717 (if (not (functionp transcoder))
3718 (error "No foreign transcoder available")
3719 (funcall
3720 transcoder data contents
3721 (org-combine-plists
3722 info (list :back-end backend
3723 :translate-alist all-transcoders
3724 :exported-data (make-hash-table :test 'eq :size 401)))))))))
3727 ;;;; For Export Snippets
3729 ;; Every export snippet is transmitted to the back-end. Though, the
3730 ;; latter will only retain one type of export-snippet, ignoring
3731 ;; others, based on the former's target back-end. The function
3732 ;; `org-export-snippet-backend' returns that back-end for a given
3733 ;; export-snippet.
3735 (defun org-export-snippet-backend (export-snippet)
3736 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3737 Translation, with `org-export-snippet-translation-alist', is
3738 applied."
3739 (let ((back-end (org-element-property :back-end export-snippet)))
3740 (intern
3741 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3742 back-end))))
3745 ;;;; For Footnotes
3747 ;; `org-export-collect-footnote-definitions' is a tool to list
3748 ;; actually used footnotes definitions in the whole parse tree, or in
3749 ;; a headline, in order to add footnote listings throughout the
3750 ;; transcoded data.
3752 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3753 ;; back-ends, when they need to attach the footnote definition only to
3754 ;; the first occurrence of the corresponding label.
3756 ;; `org-export-get-footnote-definition' and
3757 ;; `org-export-get-footnote-number' provide easier access to
3758 ;; additional information relative to a footnote reference.
3760 (defun org-export-collect-footnote-definitions (data info)
3761 "Return an alist between footnote numbers, labels and definitions.
3763 DATA is the parse tree from which definitions are collected.
3764 INFO is the plist used as a communication channel.
3766 Definitions are sorted by order of references. They either
3767 appear as Org data or as a secondary string for inlined
3768 footnotes. Unreferenced definitions are ignored."
3769 (let* (num-alist
3770 collect-fn ; for byte-compiler.
3771 (collect-fn
3772 (function
3773 (lambda (data)
3774 ;; Collect footnote number, label and definition in DATA.
3775 (org-element-map data 'footnote-reference
3776 (lambda (fn)
3777 (when (org-export-footnote-first-reference-p fn info)
3778 (let ((def (org-export-get-footnote-definition fn info)))
3779 (push
3780 (list (org-export-get-footnote-number fn info)
3781 (org-element-property :label fn)
3782 def)
3783 num-alist)
3784 ;; Also search in definition for nested footnotes.
3785 (when (eq (org-element-property :type fn) 'standard)
3786 (funcall collect-fn def)))))
3787 ;; Don't enter footnote definitions since it will happen
3788 ;; when their first reference is found.
3789 info nil 'footnote-definition)))))
3790 (funcall collect-fn (plist-get info :parse-tree))
3791 (reverse num-alist)))
3793 (defun org-export-footnote-first-reference-p (footnote-reference info)
3794 "Non-nil when a footnote reference is the first one for its label.
3796 FOOTNOTE-REFERENCE is the footnote reference being considered.
3797 INFO is the plist used as a communication channel."
3798 (let ((label (org-element-property :label footnote-reference)))
3799 ;; Anonymous footnotes are always a first reference.
3800 (if (not label) t
3801 ;; Otherwise, return the first footnote with the same LABEL and
3802 ;; test if it is equal to FOOTNOTE-REFERENCE.
3803 (let* (search-refs ; for byte-compiler.
3804 (search-refs
3805 (function
3806 (lambda (data)
3807 (org-element-map data 'footnote-reference
3808 (lambda (fn)
3809 (cond
3810 ((string= (org-element-property :label fn) label)
3811 (throw 'exit fn))
3812 ;; If FN isn't inlined, be sure to traverse its
3813 ;; definition before resuming search. See
3814 ;; comments in `org-export-get-footnote-number'
3815 ;; for more information.
3816 ((eq (org-element-property :type fn) 'standard)
3817 (funcall search-refs
3818 (org-export-get-footnote-definition fn info)))))
3819 ;; Don't enter footnote definitions since it will
3820 ;; happen when their first reference is found.
3821 info 'first-match 'footnote-definition)))))
3822 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3823 footnote-reference)))))
3825 (defun org-export-get-footnote-definition (footnote-reference info)
3826 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3827 INFO is the plist used as a communication channel. If no such
3828 definition can be found, raise an error."
3829 (let ((label (org-element-property :label footnote-reference)))
3830 (or (if label
3831 (cdr (assoc label (plist-get info :footnote-definition-alist)))
3832 (org-element-contents footnote-reference))
3833 (error "Definition not found for footnote %s" label))))
3835 (defun org-export-get-footnote-number (footnote info)
3836 "Return number associated to a footnote.
3838 FOOTNOTE is either a footnote reference or a footnote definition.
3839 INFO is the plist used as a communication channel."
3840 (let* ((label (org-element-property :label footnote))
3841 seen-refs
3842 search-ref ; For byte-compiler.
3843 (search-ref
3844 (function
3845 (lambda (data)
3846 ;; Search footnote references through DATA, filling
3847 ;; SEEN-REFS along the way.
3848 (org-element-map data 'footnote-reference
3849 (lambda (fn)
3850 (let ((fn-lbl (org-element-property :label fn)))
3851 (cond
3852 ;; Anonymous footnote match: return number.
3853 ((and (not fn-lbl) (eq fn footnote))
3854 (throw 'exit (1+ (length seen-refs))))
3855 ;; Labels match: return number.
3856 ((and label (string= label fn-lbl))
3857 (throw 'exit (1+ (length seen-refs))))
3858 ;; Anonymous footnote: it's always a new one.
3859 ;; Also, be sure to return nil from the `cond' so
3860 ;; `first-match' doesn't get us out of the loop.
3861 ((not fn-lbl) (push 'inline seen-refs) nil)
3862 ;; Label not seen so far: add it so SEEN-REFS.
3864 ;; Also search for subsequent references in
3865 ;; footnote definition so numbering follows
3866 ;; reading logic. Note that we don't have to care
3867 ;; about inline definitions, since
3868 ;; `org-element-map' already traverses them at the
3869 ;; right time.
3871 ;; Once again, return nil to stay in the loop.
3872 ((not (member fn-lbl seen-refs))
3873 (push fn-lbl seen-refs)
3874 (funcall search-ref
3875 (org-export-get-footnote-definition fn info))
3876 nil))))
3877 ;; Don't enter footnote definitions since it will
3878 ;; happen when their first reference is found.
3879 info 'first-match 'footnote-definition)))))
3880 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3883 ;;;; For Headlines
3885 ;; `org-export-get-relative-level' is a shortcut to get headline
3886 ;; level, relatively to the lower headline level in the parsed tree.
3888 ;; `org-export-get-headline-number' returns the section number of an
3889 ;; headline, while `org-export-number-to-roman' allows to convert it
3890 ;; to roman numbers. With an optional argument,
3891 ;; `org-export-get-headline-number' returns a number to unnumbered
3892 ;; headlines (used for internal id).
3894 ;; `org-export-get-headline-id' returns the unique internal id of a
3895 ;; headline.
3897 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3898 ;; `org-export-last-sibling-p' are three useful predicates when it
3899 ;; comes to fulfill the `:headline-levels' property.
3901 ;; `org-export-get-tags', `org-export-get-category' and
3902 ;; `org-export-get-node-property' extract useful information from an
3903 ;; headline or a parent headline. They all handle inheritance.
3905 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3906 ;; as a secondary string, suitable for table of contents. It falls
3907 ;; back onto default title.
3909 (defun org-export-get-relative-level (headline info)
3910 "Return HEADLINE relative level within current parsed tree.
3911 INFO is a plist holding contextual information."
3912 (+ (org-element-property :level headline)
3913 (or (plist-get info :headline-offset) 0)))
3915 (defun org-export-low-level-p (headline info)
3916 "Non-nil when HEADLINE is considered as low level.
3918 INFO is a plist used as a communication channel.
3920 A low level headlines has a relative level greater than
3921 `:headline-levels' property value.
3923 Return value is the difference between HEADLINE relative level
3924 and the last level being considered as high enough, or nil."
3925 (let ((limit (plist-get info :headline-levels)))
3926 (when (wholenump limit)
3927 (let ((level (org-export-get-relative-level headline info)))
3928 (and (> level limit) (- level limit))))))
3930 (defun org-export-get-headline-id (headline info)
3931 "Return a unique ID for HEADLINE.
3932 INFO is a plist holding contextual information."
3933 (let ((numbered (org-export-numbered-headline-p headline info)))
3934 (concat
3935 (if numbered "sec-" "unnumbered-")
3936 (mapconcat #'number-to-string
3937 (if numbered
3938 (org-export-get-headline-number headline info)
3939 (cdr (assq headline (plist-get info :unnumbered-headline-id)))) "-"))))
3941 (defun org-export-get-headline-number (headline info)
3942 "Return numbered HEADLINE numbering as a list of numbers.
3943 INFO is a plist holding contextual information."
3944 (and (org-export-numbered-headline-p headline info)
3945 (cdr (assq headline (plist-get info :headline-numbering)))))
3947 (defun org-export-numbered-headline-p (headline info)
3948 "Return a non-nil value if HEADLINE element should be numbered.
3949 INFO is a plist used as a communication channel."
3950 (unless (org-some
3951 (lambda (head) (org-not-nil (org-element-property :UNNUMBERED head)))
3952 (cons headline (org-export-get-genealogy headline)))
3953 (let ((sec-num (plist-get info :section-numbers))
3954 (level (org-export-get-relative-level headline info)))
3955 (if (wholenump sec-num) (<= level sec-num) sec-num))))
3957 (defun org-export-number-to-roman (n)
3958 "Convert integer N into a roman numeral."
3959 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3960 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3961 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3962 ( 1 . "I")))
3963 (res ""))
3964 (if (<= n 0)
3965 (number-to-string n)
3966 (while roman
3967 (if (>= n (caar roman))
3968 (setq n (- n (caar roman))
3969 res (concat res (cdar roman)))
3970 (pop roman)))
3971 res)))
3973 (defun org-export-get-tags (element info &optional tags inherited)
3974 "Return list of tags associated to ELEMENT.
3976 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3977 is a plist used as a communication channel.
3979 Select tags (see `org-export-select-tags') and exclude tags (see
3980 `org-export-exclude-tags') are removed from the list.
3982 When non-nil, optional argument TAGS should be a list of strings.
3983 Any tag belonging to this list will also be removed.
3985 When optional argument INHERITED is non-nil, tags can also be
3986 inherited from parent headlines and FILETAGS keywords."
3987 (org-remove-if
3988 (lambda (tag) (or (member tag (plist-get info :select-tags))
3989 (member tag (plist-get info :exclude-tags))
3990 (member tag tags)))
3991 (if (not inherited) (org-element-property :tags element)
3992 ;; Build complete list of inherited tags.
3993 (let ((current-tag-list (org-element-property :tags element)))
3994 (mapc
3995 (lambda (parent)
3996 (mapc
3997 (lambda (tag)
3998 (when (and (memq (org-element-type parent) '(headline inlinetask))
3999 (not (member tag current-tag-list)))
4000 (push tag current-tag-list)))
4001 (org-element-property :tags parent)))
4002 (org-export-get-genealogy element))
4003 ;; Add FILETAGS keywords and return results.
4004 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
4006 (defun org-export-get-node-property (property blob &optional inherited)
4007 "Return node PROPERTY value for BLOB.
4009 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
4010 element or object.
4012 If optional argument INHERITED is non-nil, the value can be
4013 inherited from a parent headline.
4015 Return value is a string or nil."
4016 (let ((headline (if (eq (org-element-type blob) 'headline) blob
4017 (org-export-get-parent-headline blob))))
4018 (if (not inherited) (org-element-property property blob)
4019 (let ((parent headline) value)
4020 (catch 'found
4021 (while parent
4022 (when (plist-member (nth 1 parent) property)
4023 (throw 'found (org-element-property property parent)))
4024 (setq parent (org-element-property :parent parent))))))))
4026 (defun org-export-get-category (blob info)
4027 "Return category for element or object BLOB.
4029 INFO is a plist used as a communication channel.
4031 CATEGORY is automatically inherited from a parent headline, from
4032 #+CATEGORY: keyword or created out of original file name. If all
4033 fail, the fall-back value is \"???\"."
4034 (or (org-export-get-node-property :CATEGORY blob t)
4035 (org-element-map (plist-get info :parse-tree) 'keyword
4036 (lambda (kwd)
4037 (when (equal (org-element-property :key kwd) "CATEGORY")
4038 (org-element-property :value kwd)))
4039 info 'first-match)
4040 (let ((file (plist-get info :input-file)))
4041 (and file (file-name-sans-extension (file-name-nondirectory file))))
4042 "???"))
4044 (defun org-export-get-alt-title (headline info)
4045 "Return alternative title for HEADLINE, as a secondary string.
4046 INFO is a plist used as a communication channel. If no optional
4047 title is defined, fall-back to the regular title."
4048 (or (org-element-property :alt-title headline)
4049 (org-element-property :title headline)))
4051 (defun org-export-first-sibling-p (blob info)
4052 "Non-nil when BLOB is the first sibling in its parent.
4053 BLOB is an element or an object. If BLOB is a headline, non-nil
4054 means it is the first sibling in the sub-tree. INFO is a plist
4055 used as a communication channel."
4056 (memq (org-element-type (org-export-get-previous-element blob info))
4057 '(nil section)))
4059 (defun org-export-last-sibling-p (blob info)
4060 "Non-nil when BLOB is the last sibling in its parent.
4061 BLOB is an element or an object. INFO is a plist used as
4062 a communication channel."
4063 (not (org-export-get-next-element blob info)))
4066 ;;;; For Keywords
4068 ;; `org-export-get-date' returns a date appropriate for the document
4069 ;; to about to be exported. In particular, it takes care of
4070 ;; `org-export-date-timestamp-format'.
4072 (defun org-export-get-date (info &optional fmt)
4073 "Return date value for the current document.
4075 INFO is a plist used as a communication channel. FMT, when
4076 non-nil, is a time format string that will be applied on the date
4077 if it consists in a single timestamp object. It defaults to
4078 `org-export-date-timestamp-format' when nil.
4080 A proper date can be a secondary string, a string or nil. It is
4081 meant to be translated with `org-export-data' or alike."
4082 (let ((date (plist-get info :date))
4083 (fmt (or fmt org-export-date-timestamp-format)))
4084 (cond ((not date) nil)
4085 ((and fmt
4086 (not (cdr date))
4087 (eq (org-element-type (car date)) 'timestamp))
4088 (org-timestamp-format (car date) fmt))
4089 (t date))))
4092 ;;;; For Links
4094 ;; `org-export-solidify-link-text' turns a string into a safer version
4095 ;; for links, replacing most non-standard characters with hyphens.
4097 ;; `org-export-get-coderef-format' returns an appropriate format
4098 ;; string for coderefs.
4100 ;; `org-export-inline-image-p' returns a non-nil value when the link
4101 ;; provided should be considered as an inline image.
4103 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
4104 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
4105 ;; returns an appropriate unique identifier when found, or nil.
4107 ;; `org-export-resolve-id-link' returns the first headline with
4108 ;; specified id or custom-id in parse tree, the path to the external
4109 ;; file with the id or nil when neither was found.
4111 ;; `org-export-resolve-coderef' associates a reference to a line
4112 ;; number in the element it belongs, or returns the reference itself
4113 ;; when the element isn't numbered.
4115 (defun org-export-solidify-link-text (s)
4116 "Take link text S and make a safe target out of it."
4117 (save-match-data
4118 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
4120 (defun org-export-get-coderef-format (path desc)
4121 "Return format string for code reference link.
4122 PATH is the link path. DESC is its description."
4123 (save-match-data
4124 (cond ((not desc) "%s")
4125 ((string-match (regexp-quote (concat "(" path ")")) desc)
4126 (replace-match "%s" t t desc))
4127 (t desc))))
4129 (defun org-export-inline-image-p (link &optional rules)
4130 "Non-nil if LINK object points to an inline image.
4132 Optional argument is a set of RULES defining inline images. It
4133 is an alist where associations have the following shape:
4135 \(TYPE . REGEXP)
4137 Applying a rule means apply REGEXP against LINK's path when its
4138 type is TYPE. The function will return a non-nil value if any of
4139 the provided rules is non-nil. The default rule is
4140 `org-export-default-inline-image-rule'.
4142 This only applies to links without a description."
4143 (and (not (org-element-contents link))
4144 (let ((case-fold-search t)
4145 (rules (or rules org-export-default-inline-image-rule)))
4146 (catch 'exit
4147 (mapc
4148 (lambda (rule)
4149 (and (string= (org-element-property :type link) (car rule))
4150 (string-match (cdr rule)
4151 (org-element-property :path link))
4152 (throw 'exit t)))
4153 rules)
4154 ;; Return nil if no rule matched.
4155 nil))))
4157 (defun org-export-resolve-coderef (ref info)
4158 "Resolve a code reference REF.
4160 INFO is a plist used as a communication channel.
4162 Return associated line number in source code, or REF itself,
4163 depending on src-block or example element's switches."
4164 (org-element-map (plist-get info :parse-tree) '(example-block src-block)
4165 (lambda (el)
4166 (with-temp-buffer
4167 (insert (org-trim (org-element-property :value el)))
4168 (let* ((label-fmt (regexp-quote
4169 (or (org-element-property :label-fmt el)
4170 org-coderef-label-format)))
4171 (ref-re
4172 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
4173 (replace-regexp-in-string "%s" ref label-fmt nil t))))
4174 ;; Element containing REF is found. Resolve it to either
4175 ;; a label or a line number, as needed.
4176 (when (re-search-backward ref-re nil t)
4177 (cond
4178 ((org-element-property :use-labels el) ref)
4179 ((eq (org-element-property :number-lines el) 'continued)
4180 (+ (org-export-get-loc el info) (line-number-at-pos)))
4181 (t (line-number-at-pos)))))))
4182 info 'first-match))
4184 (defun org-export-resolve-fuzzy-link (link info)
4185 "Return LINK destination.
4187 INFO is a plist holding contextual information.
4189 Return value can be an object, an element, or nil:
4191 - If LINK path matches a target object (i.e. <<path>>) return it.
4193 - If LINK path exactly matches the name affiliated keyword
4194 \(i.e. #+NAME: path) of an element, return that element.
4196 - If LINK path exactly matches any headline name, return that
4197 element. If more than one headline share that name, priority
4198 will be given to the one with the closest common ancestor, if
4199 any, or the first one in the parse tree otherwise.
4201 - Otherwise, return nil.
4203 Assume LINK type is \"fuzzy\". White spaces are not
4204 significant."
4205 (let* ((raw-path (org-element-property :path link))
4206 (match-title-p (eq (aref raw-path 0) ?*))
4207 ;; Split PATH at white spaces so matches are space
4208 ;; insensitive.
4209 (path (org-split-string
4210 (if match-title-p (substring raw-path 1) raw-path)))
4211 ;; Cache for destinations that are not position dependent.
4212 (link-cache
4213 (or (plist-get info :resolve-fuzzy-link-cache)
4214 (plist-get (setq info (plist-put info :resolve-fuzzy-link-cache
4215 (make-hash-table :test 'equal)))
4216 :resolve-fuzzy-link-cache)))
4217 (cached (gethash path link-cache 'not-found)))
4218 (cond
4219 ;; Destination is not position dependent: use cached value.
4220 ((and (not match-title-p) (not (eq cached 'not-found))) cached)
4221 ;; First try to find a matching "<<path>>" unless user specified
4222 ;; he was looking for a headline (path starts with a "*"
4223 ;; character).
4224 ((and (not match-title-p)
4225 (let ((match (org-element-map (plist-get info :parse-tree) 'target
4226 (lambda (blob)
4227 (and (equal (org-split-string
4228 (org-element-property :value blob))
4229 path)
4230 blob))
4231 info 'first-match)))
4232 (and match (puthash path match link-cache)))))
4233 ;; Then try to find an element with a matching "#+NAME: path"
4234 ;; affiliated keyword.
4235 ((and (not match-title-p)
4236 (let ((match (org-element-map (plist-get info :parse-tree)
4237 org-element-all-elements
4238 (lambda (el)
4239 (let ((name (org-element-property :name el)))
4240 (when (and name
4241 (equal (org-split-string name) path))
4242 el)))
4243 info 'first-match)))
4244 (and match (puthash path match link-cache)))))
4245 ;; Last case: link either points to a headline or to nothingness.
4246 ;; Try to find the source, with priority given to headlines with
4247 ;; the closest common ancestor. If such candidate is found,
4248 ;; return it, otherwise return nil.
4250 (let ((find-headline
4251 (function
4252 ;; Return first headline whose `:raw-value' property is
4253 ;; NAME in parse tree DATA, or nil. Statistics cookies
4254 ;; are ignored.
4255 (lambda (name data)
4256 (org-element-map data 'headline
4257 (lambda (headline)
4258 (when (equal (org-split-string
4259 (replace-regexp-in-string
4260 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
4261 (org-element-property :raw-value headline)))
4262 name)
4263 headline))
4264 info 'first-match)))))
4265 ;; Search among headlines sharing an ancestor with link, from
4266 ;; closest to farthest.
4267 (catch 'exit
4268 (mapc
4269 (lambda (parent)
4270 (let ((foundp (funcall find-headline path parent)))
4271 (when foundp (throw 'exit foundp))))
4272 (let ((parent-hl (org-export-get-parent-headline link)))
4273 (if (not parent-hl) (list (plist-get info :parse-tree))
4274 (cons parent-hl (org-export-get-genealogy parent-hl)))))
4275 ;; No destination found: return nil.
4276 (and (not match-title-p) (puthash path nil link-cache))))))))
4278 (defun org-export-resolve-id-link (link info)
4279 "Return headline referenced as LINK destination.
4281 INFO is a plist used as a communication channel.
4283 Return value can be the headline element matched in current parse
4284 tree, a file name or nil. Assume LINK type is either \"id\" or
4285 \"custom-id\"."
4286 (let ((id (org-element-property :path link)))
4287 ;; First check if id is within the current parse tree.
4288 (or (org-element-map (plist-get info :parse-tree) 'headline
4289 (lambda (headline)
4290 (when (or (string= (org-element-property :ID headline) id)
4291 (string= (org-element-property :CUSTOM_ID headline) id))
4292 headline))
4293 info 'first-match)
4294 ;; Otherwise, look for external files.
4295 (cdr (assoc id (plist-get info :id-alist))))))
4297 (defun org-export-resolve-radio-link (link info)
4298 "Return radio-target object referenced as LINK destination.
4300 INFO is a plist used as a communication channel.
4302 Return value can be a radio-target object or nil. Assume LINK
4303 has type \"radio\"."
4304 (let ((path (replace-regexp-in-string
4305 "[ \r\t\n]+" " " (org-element-property :path link))))
4306 (org-element-map (plist-get info :parse-tree) 'radio-target
4307 (lambda (radio)
4308 (and (eq (compare-strings
4309 (replace-regexp-in-string
4310 "[ \r\t\n]+" " " (org-element-property :value radio))
4311 nil nil path nil nil t)
4313 radio))
4314 info 'first-match)))
4317 ;;;; For References
4319 ;; `org-export-get-ordinal' associates a sequence number to any object
4320 ;; or element.
4322 (defun org-export-get-ordinal (element info &optional types predicate)
4323 "Return ordinal number of an element or object.
4325 ELEMENT is the element or object considered. INFO is the plist
4326 used as a communication channel.
4328 Optional argument TYPES, when non-nil, is a list of element or
4329 object types, as symbols, that should also be counted in.
4330 Otherwise, only provided element's type is considered.
4332 Optional argument PREDICATE is a function returning a non-nil
4333 value if the current element or object should be counted in. It
4334 accepts two arguments: the element or object being considered and
4335 the plist used as a communication channel. This allows to count
4336 only a certain type of objects (i.e. inline images).
4338 Return value is a list of numbers if ELEMENT is a headline or an
4339 item. It is nil for keywords. It represents the footnote number
4340 for footnote definitions and footnote references. If ELEMENT is
4341 a target, return the same value as if ELEMENT was the closest
4342 table, item or headline containing the target. In any other
4343 case, return the sequence number of ELEMENT among elements or
4344 objects of the same type."
4345 ;; Ordinal of a target object refer to the ordinal of the closest
4346 ;; table, item, or headline containing the object.
4347 (when (eq (org-element-type element) 'target)
4348 (setq element
4349 (loop for parent in (org-export-get-genealogy element)
4350 when
4351 (memq
4352 (org-element-type parent)
4353 '(footnote-definition footnote-reference headline item
4354 table))
4355 return parent)))
4356 (case (org-element-type element)
4357 ;; Special case 1: A headline returns its number as a list.
4358 (headline (org-export-get-headline-number element info))
4359 ;; Special case 2: An item returns its number as a list.
4360 (item (let ((struct (org-element-property :structure element)))
4361 (org-list-get-item-number
4362 (org-element-property :begin element)
4363 struct
4364 (org-list-prevs-alist struct)
4365 (org-list-parents-alist struct))))
4366 ((footnote-definition footnote-reference)
4367 (org-export-get-footnote-number element info))
4368 (otherwise
4369 (let ((counter 0))
4370 ;; Increment counter until ELEMENT is found again.
4371 (org-element-map (plist-get info :parse-tree)
4372 (or types (org-element-type element))
4373 (lambda (el)
4374 (cond
4375 ((eq element el) (1+ counter))
4376 ((not predicate) (incf counter) nil)
4377 ((funcall predicate el info) (incf counter) nil)))
4378 info 'first-match)))))
4381 ;;;; For Src-Blocks
4383 ;; `org-export-get-loc' counts number of code lines accumulated in
4384 ;; src-block or example-block elements with a "+n" switch until
4385 ;; a given element, excluded. Note: "-n" switches reset that count.
4387 ;; `org-export-unravel-code' extracts source code (along with a code
4388 ;; references alist) from an `element-block' or `src-block' type
4389 ;; element.
4391 ;; `org-export-format-code' applies a formatting function to each line
4392 ;; of code, providing relative line number and code reference when
4393 ;; appropriate. Since it doesn't access the original element from
4394 ;; which the source code is coming, it expects from the code calling
4395 ;; it to know if lines should be numbered and if code references
4396 ;; should appear.
4398 ;; Eventually, `org-export-format-code-default' is a higher-level
4399 ;; function (it makes use of the two previous functions) which handles
4400 ;; line numbering and code references inclusion, and returns source
4401 ;; code in a format suitable for plain text or verbatim output.
4403 (defun org-export-get-loc (element info)
4404 "Return accumulated lines of code up to ELEMENT.
4406 INFO is the plist used as a communication channel.
4408 ELEMENT is excluded from count."
4409 (let ((loc 0))
4410 (org-element-map (plist-get info :parse-tree)
4411 `(src-block example-block ,(org-element-type element))
4412 (lambda (el)
4413 (cond
4414 ;; ELEMENT is reached: Quit the loop.
4415 ((eq el element))
4416 ;; Only count lines from src-block and example-block elements
4417 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4418 ((not (memq (org-element-type el) '(src-block example-block))) nil)
4419 ((let ((linums (org-element-property :number-lines el)))
4420 (when linums
4421 ;; Accumulate locs or reset them.
4422 (let ((lines (org-count-lines
4423 (org-trim (org-element-property :value el)))))
4424 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
4425 ;; Return nil to stay in the loop.
4426 nil)))
4427 info 'first-match)
4428 ;; Return value.
4429 loc))
4431 (defun org-export-unravel-code (element)
4432 "Clean source code and extract references out of it.
4434 ELEMENT has either a `src-block' an `example-block' type.
4436 Return a cons cell whose CAR is the source code, cleaned from any
4437 reference, protective commas and spurious indentation, and CDR is
4438 an alist between relative line number (integer) and name of code
4439 reference on that line (string)."
4440 (let* ((line 0) refs
4441 (value (org-element-property :value element))
4442 ;; Get code and clean it. Remove blank lines at its
4443 ;; beginning and end.
4444 (code (replace-regexp-in-string
4445 "\\`\\([ \t]*\n\\)+" ""
4446 (replace-regexp-in-string
4447 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4448 (if (or org-src-preserve-indentation
4449 (org-element-property :preserve-indent element))
4450 value
4451 (org-element-remove-indentation value)))))
4452 ;; Get format used for references.
4453 (label-fmt (regexp-quote
4454 (or (org-element-property :label-fmt element)
4455 org-coderef-label-format)))
4456 ;; Build a regexp matching a loc with a reference.
4457 (with-ref-re
4458 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4459 (replace-regexp-in-string
4460 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
4461 ;; Return value.
4462 (cons
4463 ;; Code with references removed.
4464 (org-element-normalize-string
4465 (mapconcat
4466 (lambda (loc)
4467 (incf line)
4468 (if (not (string-match with-ref-re loc)) loc
4469 ;; Ref line: remove ref, and signal its position in REFS.
4470 (push (cons line (match-string 3 loc)) refs)
4471 (replace-match "" nil nil loc 1)))
4472 (org-split-string code "\n") "\n"))
4473 ;; Reference alist.
4474 refs)))
4476 (defun org-export-format-code (code fun &optional num-lines ref-alist)
4477 "Format CODE by applying FUN line-wise and return it.
4479 CODE is a string representing the code to format. FUN is
4480 a function. It must accept three arguments: a line of
4481 code (string), the current line number (integer) or nil and the
4482 reference associated to the current line (string) or nil.
4484 Optional argument NUM-LINES can be an integer representing the
4485 number of code lines accumulated until the current code. Line
4486 numbers passed to FUN will take it into account. If it is nil,
4487 FUN's second argument will always be nil. This number can be
4488 obtained with `org-export-get-loc' function.
4490 Optional argument REF-ALIST can be an alist between relative line
4491 number (i.e. ignoring NUM-LINES) and the name of the code
4492 reference on it. If it is nil, FUN's third argument will always
4493 be nil. It can be obtained through the use of
4494 `org-export-unravel-code' function."
4495 (let ((--locs (org-split-string code "\n"))
4496 (--line 0))
4497 (org-element-normalize-string
4498 (mapconcat
4499 (lambda (--loc)
4500 (incf --line)
4501 (let ((--ref (cdr (assq --line ref-alist))))
4502 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
4503 --locs "\n"))))
4505 (defun org-export-format-code-default (element info)
4506 "Return source code from ELEMENT, formatted in a standard way.
4508 ELEMENT is either a `src-block' or `example-block' element. INFO
4509 is a plist used as a communication channel.
4511 This function takes care of line numbering and code references
4512 inclusion. Line numbers, when applicable, appear at the
4513 beginning of the line, separated from the code by two white
4514 spaces. Code references, on the other hand, appear flushed to
4515 the right, separated by six white spaces from the widest line of
4516 code."
4517 ;; Extract code and references.
4518 (let* ((code-info (org-export-unravel-code element))
4519 (code (car code-info))
4520 (code-lines (org-split-string code "\n")))
4521 (if (null code-lines) ""
4522 (let* ((refs (and (org-element-property :retain-labels element)
4523 (cdr code-info)))
4524 ;; Handle line numbering.
4525 (num-start (case (org-element-property :number-lines element)
4526 (continued (org-export-get-loc element info))
4527 (new 0)))
4528 (num-fmt
4529 (and num-start
4530 (format "%%%ds "
4531 (length (number-to-string
4532 (+ (length code-lines) num-start))))))
4533 ;; Prepare references display, if required. Any reference
4534 ;; should start six columns after the widest line of code,
4535 ;; wrapped with parenthesis.
4536 (max-width
4537 (+ (apply 'max (mapcar 'length code-lines))
4538 (if (not num-start) 0 (length (format num-fmt num-start))))))
4539 (org-export-format-code
4540 code
4541 (lambda (loc line-num ref)
4542 (let ((number-str (and num-fmt (format num-fmt line-num))))
4543 (concat
4544 number-str
4546 (and ref
4547 (concat (make-string
4548 (- (+ 6 max-width)
4549 (+ (length loc) (length number-str))) ? )
4550 (format "(%s)" ref))))))
4551 num-start refs)))))
4554 ;;;; For Tables
4556 ;; `org-export-table-has-special-column-p' and and
4557 ;; `org-export-table-row-is-special-p' are predicates used to look for
4558 ;; meta-information about the table structure.
4560 ;; `org-table-has-header-p' tells when the rows before the first rule
4561 ;; should be considered as table's header.
4563 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4564 ;; and `org-export-table-cell-borders' extract information from
4565 ;; a table-cell element.
4567 ;; `org-export-table-dimensions' gives the number on rows and columns
4568 ;; in the table, ignoring horizontal rules and special columns.
4569 ;; `org-export-table-cell-address', given a table-cell object, returns
4570 ;; the absolute address of a cell. On the other hand,
4571 ;; `org-export-get-table-cell-at' does the contrary.
4573 ;; `org-export-table-cell-starts-colgroup-p',
4574 ;; `org-export-table-cell-ends-colgroup-p',
4575 ;; `org-export-table-row-starts-rowgroup-p',
4576 ;; `org-export-table-row-ends-rowgroup-p',
4577 ;; `org-export-table-row-starts-header-p',
4578 ;; `org-export-table-row-ends-header-p' and
4579 ;; `org-export-table-row-in-header-p' indicate position of current row
4580 ;; or cell within the table.
4582 (defun org-export-table-has-special-column-p (table)
4583 "Non-nil when TABLE has a special column.
4584 All special columns will be ignored during export."
4585 ;; The table has a special column when every first cell of every row
4586 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4587 ;; "*" "_" and "^". Though, do not consider a first row containing
4588 ;; only empty cells as special.
4589 (let ((special-column-p 'empty))
4590 (catch 'exit
4591 (mapc
4592 (lambda (row)
4593 (when (eq (org-element-property :type row) 'standard)
4594 (let ((value (org-element-contents
4595 (car (org-element-contents row)))))
4596 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4597 (setq special-column-p 'special))
4598 ((not value))
4599 (t (throw 'exit nil))))))
4600 (org-element-contents table))
4601 (eq special-column-p 'special))))
4603 (defun org-export-table-has-header-p (table info)
4604 "Non-nil when TABLE has a header.
4606 INFO is a plist used as a communication channel.
4608 A table has a header when it contains at least two row groups."
4609 (let ((cache (or (plist-get info :table-header-cache)
4610 (plist-get (setq info
4611 (plist-put info :table-header-cache
4612 (make-hash-table :test 'eq)))
4613 :table-header-cache))))
4614 (or (gethash table cache)
4615 (let ((rowgroup 1) row-flag)
4616 (puthash
4617 table
4618 (org-element-map table 'table-row
4619 (lambda (row)
4620 (cond
4621 ((> rowgroup 1) t)
4622 ((and row-flag (eq (org-element-property :type row) 'rule))
4623 (incf rowgroup) (setq row-flag nil))
4624 ((and (not row-flag) (eq (org-element-property :type row)
4625 'standard))
4626 (setq row-flag t) nil)))
4627 info 'first-match)
4628 cache)))))
4630 (defun org-export-table-row-is-special-p (table-row info)
4631 "Non-nil if TABLE-ROW is considered special.
4633 INFO is a plist used as the communication channel.
4635 All special rows will be ignored during export."
4636 (when (eq (org-element-property :type table-row) 'standard)
4637 (let ((first-cell (org-element-contents
4638 (car (org-element-contents table-row)))))
4639 ;; A row is special either when...
4641 ;; ... it starts with a field only containing "/",
4642 (equal first-cell '("/"))
4643 ;; ... the table contains a special column and the row start
4644 ;; with a marking character among, "^", "_", "$" or "!",
4645 (and (org-export-table-has-special-column-p
4646 (org-export-get-parent table-row))
4647 (member first-cell '(("^") ("_") ("$") ("!"))))
4648 ;; ... it contains only alignment cookies and empty cells.
4649 (let ((special-row-p 'empty))
4650 (catch 'exit
4651 (mapc
4652 (lambda (cell)
4653 (let ((value (org-element-contents cell)))
4654 ;; Since VALUE is a secondary string, the following
4655 ;; checks avoid expanding it with `org-export-data'.
4656 (cond ((not value))
4657 ((and (not (cdr value))
4658 (stringp (car value))
4659 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4660 (car value)))
4661 (setq special-row-p 'cookie))
4662 (t (throw 'exit nil)))))
4663 (org-element-contents table-row))
4664 (eq special-row-p 'cookie)))))))
4666 (defun org-export-table-row-group (table-row info)
4667 "Return TABLE-ROW's group number, as an integer.
4669 INFO is a plist used as the communication channel.
4671 Return value is the group number, as an integer, or nil for
4672 special rows and rows separators. First group is also table's
4673 header."
4674 (let ((cache (or (plist-get info :table-row-group-cache)
4675 (plist-get (setq info
4676 (plist-put info :table-row-group-cache
4677 (make-hash-table :test 'eq)))
4678 :table-row-group-cache))))
4679 (cond ((gethash table-row cache))
4680 ((eq (org-element-property :type table-row) 'rule) nil)
4681 (t (let ((group 0) row-flag)
4682 (org-element-map (org-export-get-parent table-row) 'table-row
4683 (lambda (row)
4684 (if (eq (org-element-property :type row) 'rule)
4685 (setq row-flag nil)
4686 (unless row-flag (incf group) (setq row-flag t)))
4687 (when (eq table-row row) (puthash table-row group cache)))
4688 info 'first-match))))))
4690 (defun org-export-table-cell-width (table-cell info)
4691 "Return TABLE-CELL contents width.
4693 INFO is a plist used as the communication channel.
4695 Return value is the width given by the last width cookie in the
4696 same column as TABLE-CELL, or nil."
4697 (let* ((row (org-export-get-parent table-cell))
4698 (table (org-export-get-parent row))
4699 (cells (org-element-contents row))
4700 (columns (length cells))
4701 (column (- columns (length (memq table-cell cells))))
4702 (cache (or (plist-get info :table-cell-width-cache)
4703 (plist-get (setq info
4704 (plist-put info :table-cell-width-cache
4705 (make-hash-table :test 'eq)))
4706 :table-cell-width-cache)))
4707 (width-vector (or (gethash table cache)
4708 (puthash table (make-vector columns 'empty) cache)))
4709 (value (aref width-vector column)))
4710 (if (not (eq value 'empty)) value
4711 (let (cookie-width)
4712 (dolist (row (org-element-contents table)
4713 (aset width-vector column cookie-width))
4714 (when (org-export-table-row-is-special-p row info)
4715 ;; In a special row, try to find a width cookie at COLUMN.
4716 (let* ((value (org-element-contents
4717 (elt (org-element-contents row) column)))
4718 (cookie (car value)))
4719 ;; The following checks avoid expanding unnecessarily
4720 ;; the cell with `org-export-data'.
4721 (when (and value
4722 (not (cdr value))
4723 (stringp cookie)
4724 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
4725 (match-string 1 cookie))
4726 (setq cookie-width
4727 (string-to-number (match-string 1 cookie)))))))))))
4729 (defun org-export-table-cell-alignment (table-cell info)
4730 "Return TABLE-CELL contents alignment.
4732 INFO is a plist used as the communication channel.
4734 Return alignment as specified by the last alignment cookie in the
4735 same column as TABLE-CELL. If no such cookie is found, a default
4736 alignment value will be deduced from fraction of numbers in the
4737 column (see `org-table-number-fraction' for more information).
4738 Possible values are `left', `right' and `center'."
4739 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4740 (require 'org-table)
4741 (let* ((row (org-export-get-parent table-cell))
4742 (table (org-export-get-parent row))
4743 (cells (org-element-contents row))
4744 (columns (length cells))
4745 (column (- columns (length (memq table-cell cells))))
4746 (cache (or (plist-get info :table-cell-alignment-cache)
4747 (plist-get (setq info
4748 (plist-put info :table-cell-alignment-cache
4749 (make-hash-table :test 'eq)))
4750 :table-cell-alignment-cache)))
4751 (align-vector (or (gethash table cache)
4752 (puthash table (make-vector columns nil) cache))))
4753 (or (aref align-vector column)
4754 (let ((number-cells 0)
4755 (total-cells 0)
4756 cookie-align
4757 previous-cell-number-p)
4758 (dolist (row (org-element-contents (org-export-get-parent row)))
4759 (cond
4760 ;; In a special row, try to find an alignment cookie at
4761 ;; COLUMN.
4762 ((org-export-table-row-is-special-p row info)
4763 (let ((value (org-element-contents
4764 (elt (org-element-contents row) column))))
4765 ;; Since VALUE is a secondary string, the following
4766 ;; checks avoid useless expansion through
4767 ;; `org-export-data'.
4768 (when (and value
4769 (not (cdr value))
4770 (stringp (car value))
4771 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4772 (car value))
4773 (match-string 1 (car value)))
4774 (setq cookie-align (match-string 1 (car value))))))
4775 ;; Ignore table rules.
4776 ((eq (org-element-property :type row) 'rule))
4777 ;; In a standard row, check if cell's contents are
4778 ;; expressing some kind of number. Increase NUMBER-CELLS
4779 ;; accordingly. Though, don't bother if an alignment
4780 ;; cookie has already defined cell's alignment.
4781 ((not cookie-align)
4782 (let ((value (org-export-data
4783 (org-element-contents
4784 (elt (org-element-contents row) column))
4785 info)))
4786 (incf total-cells)
4787 ;; Treat an empty cell as a number if it follows
4788 ;; a number.
4789 (if (not (or (string-match org-table-number-regexp value)
4790 (and (string= value "") previous-cell-number-p)))
4791 (setq previous-cell-number-p nil)
4792 (setq previous-cell-number-p t)
4793 (incf number-cells))))))
4794 ;; Return value. Alignment specified by cookies has
4795 ;; precedence over alignment deduced from cell's contents.
4796 (aset align-vector
4797 column
4798 (cond ((equal cookie-align "l") 'left)
4799 ((equal cookie-align "r") 'right)
4800 ((equal cookie-align "c") 'center)
4801 ((>= (/ (float number-cells) total-cells)
4802 org-table-number-fraction)
4803 'right)
4804 (t 'left)))))))
4806 (defun org-export-table-cell-borders (table-cell info)
4807 "Return TABLE-CELL borders.
4809 INFO is a plist used as a communication channel.
4811 Return value is a list of symbols, or nil. Possible values are:
4812 `top', `bottom', `above', `below', `left' and `right'. Note:
4813 `top' (resp. `bottom') only happen for a cell in the first
4814 row (resp. last row) of the table, ignoring table rules, if any.
4816 Returned borders ignore special rows."
4817 (let* ((row (org-export-get-parent table-cell))
4818 (table (org-export-get-parent-table table-cell))
4819 borders)
4820 ;; Top/above border? TABLE-CELL has a border above when a rule
4821 ;; used to demarcate row groups can be found above. Hence,
4822 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4823 ;; another regular row has to be found above that rule.
4824 (let (rule-flag)
4825 (catch 'exit
4826 (mapc (lambda (row)
4827 (cond ((eq (org-element-property :type row) 'rule)
4828 (setq rule-flag t))
4829 ((not (org-export-table-row-is-special-p row info))
4830 (if rule-flag (throw 'exit (push 'above borders))
4831 (throw 'exit nil)))))
4832 ;; Look at every row before the current one.
4833 (cdr (memq row (reverse (org-element-contents table)))))
4834 ;; No rule above, or rule found starts the table (ignoring any
4835 ;; special row): TABLE-CELL is at the top of the table.
4836 (when rule-flag (push 'above borders))
4837 (push 'top borders)))
4838 ;; Bottom/below border? TABLE-CELL has a border below when next
4839 ;; non-regular row below is a rule.
4840 (let (rule-flag)
4841 (catch 'exit
4842 (mapc (lambda (row)
4843 (cond ((eq (org-element-property :type row) 'rule)
4844 (setq rule-flag t))
4845 ((not (org-export-table-row-is-special-p row info))
4846 (if rule-flag (throw 'exit (push 'below borders))
4847 (throw 'exit nil)))))
4848 ;; Look at every row after the current one.
4849 (cdr (memq row (org-element-contents table))))
4850 ;; No rule below, or rule found ends the table (modulo some
4851 ;; special row): TABLE-CELL is at the bottom of the table.
4852 (when rule-flag (push 'below borders))
4853 (push 'bottom borders)))
4854 ;; Right/left borders? They can only be specified by column
4855 ;; groups. Column groups are defined in a row starting with "/".
4856 ;; Also a column groups row only contains "<", "<>", ">" or blank
4857 ;; cells.
4858 (catch 'exit
4859 (let ((column (let ((cells (org-element-contents row)))
4860 (- (length cells) (length (memq table-cell cells))))))
4861 (mapc
4862 (lambda (row)
4863 (unless (eq (org-element-property :type row) 'rule)
4864 (when (equal (org-element-contents
4865 (car (org-element-contents row)))
4866 '("/"))
4867 (let ((column-groups
4868 (mapcar
4869 (lambda (cell)
4870 (let ((value (org-element-contents cell)))
4871 (when (member value '(("<") ("<>") (">") nil))
4872 (car value))))
4873 (org-element-contents row))))
4874 ;; There's a left border when previous cell, if
4875 ;; any, ends a group, or current one starts one.
4876 (when (or (and (not (zerop column))
4877 (member (elt column-groups (1- column))
4878 '(">" "<>")))
4879 (member (elt column-groups column) '("<" "<>")))
4880 (push 'left borders))
4881 ;; There's a right border when next cell, if any,
4882 ;; starts a group, or current one ends one.
4883 (when (or (and (/= (1+ column) (length column-groups))
4884 (member (elt column-groups (1+ column))
4885 '("<" "<>")))
4886 (member (elt column-groups column) '(">" "<>")))
4887 (push 'right borders))
4888 (throw 'exit nil)))))
4889 ;; Table rows are read in reverse order so last column groups
4890 ;; row has precedence over any previous one.
4891 (reverse (org-element-contents table)))))
4892 ;; Return value.
4893 borders))
4895 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4896 "Non-nil when TABLE-CELL is at the beginning of a column group.
4897 INFO is a plist used as a communication channel."
4898 ;; A cell starts a column group either when it is at the beginning
4899 ;; of a row (or after the special column, if any) or when it has
4900 ;; a left border.
4901 (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
4902 'identity info 'first-match)
4903 table-cell)
4904 (memq 'left (org-export-table-cell-borders table-cell info))))
4906 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4907 "Non-nil when TABLE-CELL is at the end of a column group.
4908 INFO is a plist used as a communication channel."
4909 ;; A cell ends a column group either when it is at the end of a row
4910 ;; or when it has a right border.
4911 (or (eq (car (last (org-element-contents
4912 (org-export-get-parent table-cell))))
4913 table-cell)
4914 (memq 'right (org-export-table-cell-borders table-cell info))))
4916 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4917 "Non-nil when TABLE-ROW is at the beginning of a row group.
4918 INFO is a plist used as a communication channel."
4919 (unless (or (eq (org-element-property :type table-row) 'rule)
4920 (org-export-table-row-is-special-p table-row info))
4921 (let ((borders (org-export-table-cell-borders
4922 (car (org-element-contents table-row)) info)))
4923 (or (memq 'top borders) (memq 'above borders)))))
4925 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4926 "Non-nil when TABLE-ROW is at the end of a row group.
4927 INFO is a plist used as a communication channel."
4928 (unless (or (eq (org-element-property :type table-row) 'rule)
4929 (org-export-table-row-is-special-p table-row info))
4930 (let ((borders (org-export-table-cell-borders
4931 (car (org-element-contents table-row)) info)))
4932 (or (memq 'bottom borders) (memq 'below borders)))))
4934 (defun org-export-table-row-in-header-p (table-row info)
4935 "Non-nil when TABLE-ROW is located within table's header.
4936 INFO is a plist used as a communication channel. Always return
4937 nil for special rows and rows separators."
4938 (and (org-export-table-has-header-p
4939 (org-export-get-parent-table table-row) info)
4940 (eql (org-export-table-row-group table-row info) 1)))
4942 (defun org-export-table-row-starts-header-p (table-row info)
4943 "Non-nil when TABLE-ROW is the first table header's row.
4944 INFO is a plist used as a communication channel."
4945 (and (org-export-table-row-in-header-p table-row info)
4946 (org-export-table-row-starts-rowgroup-p table-row info)))
4948 (defun org-export-table-row-ends-header-p (table-row info)
4949 "Non-nil when TABLE-ROW is the last table header's row.
4950 INFO is a plist used as a communication channel."
4951 (and (org-export-table-row-in-header-p table-row info)
4952 (org-export-table-row-ends-rowgroup-p table-row info)))
4954 (defun org-export-table-row-number (table-row info)
4955 "Return TABLE-ROW number.
4956 INFO is a plist used as a communication channel. Return value is
4957 zero-based and ignores separators. The function returns nil for
4958 special columns and separators."
4959 (when (and (eq (org-element-property :type table-row) 'standard)
4960 (not (org-export-table-row-is-special-p table-row info)))
4961 (let ((number 0))
4962 (org-element-map (org-export-get-parent-table table-row) 'table-row
4963 (lambda (row)
4964 (cond ((eq row table-row) number)
4965 ((eq (org-element-property :type row) 'standard)
4966 (incf number) nil)))
4967 info 'first-match))))
4969 (defun org-export-table-dimensions (table info)
4970 "Return TABLE dimensions.
4972 INFO is a plist used as a communication channel.
4974 Return value is a CONS like (ROWS . COLUMNS) where
4975 ROWS (resp. COLUMNS) is the number of exportable
4976 rows (resp. columns)."
4977 (let (first-row (columns 0) (rows 0))
4978 ;; Set number of rows, and extract first one.
4979 (org-element-map table 'table-row
4980 (lambda (row)
4981 (when (eq (org-element-property :type row) 'standard)
4982 (incf rows)
4983 (unless first-row (setq first-row row)))) info)
4984 ;; Set number of columns.
4985 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4986 ;; Return value.
4987 (cons rows columns)))
4989 (defun org-export-table-cell-address (table-cell info)
4990 "Return address of a regular TABLE-CELL object.
4992 TABLE-CELL is the cell considered. INFO is a plist used as
4993 a communication channel.
4995 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4996 zero-based index. Only exportable cells are considered. The
4997 function returns nil for other cells."
4998 (let* ((table-row (org-export-get-parent table-cell))
4999 (row-number (org-export-table-row-number table-row info)))
5000 (when row-number
5001 (cons row-number
5002 (let ((col-count 0))
5003 (org-element-map table-row 'table-cell
5004 (lambda (cell)
5005 (if (eq cell table-cell) col-count (incf col-count) nil))
5006 info 'first-match))))))
5008 (defun org-export-get-table-cell-at (address table info)
5009 "Return regular table-cell object at ADDRESS in TABLE.
5011 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
5012 zero-based index. TABLE is a table type element. INFO is
5013 a plist used as a communication channel.
5015 If no table-cell, among exportable cells, is found at ADDRESS,
5016 return nil."
5017 (let ((column-pos (cdr address)) (column-count 0))
5018 (org-element-map
5019 ;; Row at (car address) or nil.
5020 (let ((row-pos (car address)) (row-count 0))
5021 (org-element-map table 'table-row
5022 (lambda (row)
5023 (cond ((eq (org-element-property :type row) 'rule) nil)
5024 ((= row-count row-pos) row)
5025 (t (incf row-count) nil)))
5026 info 'first-match))
5027 'table-cell
5028 (lambda (cell)
5029 (if (= column-count column-pos) cell
5030 (incf column-count) nil))
5031 info 'first-match)))
5034 ;;;; For Tables Of Contents
5036 ;; `org-export-collect-headlines' builds a list of all exportable
5037 ;; headline elements, maybe limited to a certain depth. One can then
5038 ;; easily parse it and transcode it.
5040 ;; Building lists of tables, figures or listings is quite similar.
5041 ;; Once the generic function `org-export-collect-elements' is defined,
5042 ;; `org-export-collect-tables', `org-export-collect-figures' and
5043 ;; `org-export-collect-listings' can be derived from it.
5045 (defun org-export-collect-headlines (info &optional n)
5046 "Collect headlines in order to build a table of contents.
5048 INFO is a plist used as a communication channel.
5050 When optional argument N is an integer, it specifies the depth of
5051 the table of contents. Otherwise, it is set to the value of the
5052 last headline level. See `org-export-headline-levels' for more
5053 information.
5055 Return a list of all exportable headlines as parsed elements.
5056 Footnote sections, if any, will be ignored."
5057 (let ((limit (plist-get info :headline-levels)))
5058 (setq n (if (wholenump n) (min n limit) limit))
5059 (org-element-map (plist-get info :parse-tree) 'headline
5060 #'(lambda (headline)
5061 (unless (org-element-property :footnote-section-p headline)
5062 (let ((level (org-export-get-relative-level headline info)))
5063 (and (<= level n) headline))))
5064 info)))
5066 (defun org-export-collect-elements (type info &optional predicate)
5067 "Collect referenceable elements of a determined type.
5069 TYPE can be a symbol or a list of symbols specifying element
5070 types to search. Only elements with a caption are collected.
5072 INFO is a plist used as a communication channel.
5074 When non-nil, optional argument PREDICATE is a function accepting
5075 one argument, an element of type TYPE. It returns a non-nil
5076 value when that element should be collected.
5078 Return a list of all elements found, in order of appearance."
5079 (org-element-map (plist-get info :parse-tree) type
5080 (lambda (element)
5081 (and (org-element-property :caption element)
5082 (or (not predicate) (funcall predicate element))
5083 element))
5084 info))
5086 (defun org-export-collect-tables (info)
5087 "Build a list of tables.
5088 INFO is a plist used as a communication channel.
5090 Return a list of table elements with a caption."
5091 (org-export-collect-elements 'table info))
5093 (defun org-export-collect-figures (info predicate)
5094 "Build a list of figures.
5096 INFO is a plist used as a communication channel. PREDICATE is
5097 a function which accepts one argument: a paragraph element and
5098 whose return value is non-nil when that element should be
5099 collected.
5101 A figure is a paragraph type element, with a caption, verifying
5102 PREDICATE. The latter has to be provided since a \"figure\" is
5103 a vague concept that may depend on back-end.
5105 Return a list of elements recognized as figures."
5106 (org-export-collect-elements 'paragraph info predicate))
5108 (defun org-export-collect-listings (info)
5109 "Build a list of src blocks.
5111 INFO is a plist used as a communication channel.
5113 Return a list of src-block elements with a caption."
5114 (org-export-collect-elements 'src-block info))
5117 ;;;; Smart Quotes
5119 ;; The main function for the smart quotes sub-system is
5120 ;; `org-export-activate-smart-quotes', which replaces every quote in
5121 ;; a given string from the parse tree with its "smart" counterpart.
5123 ;; Dictionary for smart quotes is stored in
5124 ;; `org-export-smart-quotes-alist'.
5126 ;; Internally, regexps matching potential smart quotes (checks at
5127 ;; string boundaries are also necessary) are defined in
5128 ;; `org-export-smart-quotes-regexps'.
5130 (defconst org-export-smart-quotes-alist
5131 '(("da"
5132 ;; one may use: »...«, "...", ›...‹, or '...'.
5133 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
5134 ;; LaTeX quotes require Babel!
5135 (opening-double-quote :utf-8 "»" :html "&raquo;" :latex ">>"
5136 :texinfo "@guillemetright{}")
5137 (closing-double-quote :utf-8 "«" :html "&laquo;" :latex "<<"
5138 :texinfo "@guillemetleft{}")
5139 (opening-single-quote :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}"
5140 :texinfo "@guilsinglright{}")
5141 (closing-single-quote :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}"
5142 :texinfo "@guilsingleft{}")
5143 (apostrophe :utf-8 "’" :html "&rsquo;"))
5144 ("de"
5145 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
5146 :texinfo "@quotedblbase{}")
5147 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
5148 :texinfo "@quotedblleft{}")
5149 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
5150 :texinfo "@quotesinglbase{}")
5151 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
5152 :texinfo "@quoteleft{}")
5153 (apostrophe :utf-8 "’" :html "&rsquo;"))
5154 ("en"
5155 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
5156 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
5157 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5158 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5159 (apostrophe :utf-8 "’" :html "&rsquo;"))
5160 ("es"
5161 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5162 :texinfo "@guillemetleft{}")
5163 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5164 :texinfo "@guillemetright{}")
5165 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
5166 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
5167 (apostrophe :utf-8 "’" :html "&rsquo;"))
5168 ("fr"
5169 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
5170 :texinfo "@guillemetleft{}@tie{}")
5171 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
5172 :texinfo "@tie{}@guillemetright{}")
5173 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
5174 :texinfo "@guillemetleft{}@tie{}")
5175 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
5176 :texinfo "@tie{}@guillemetright{}")
5177 (apostrophe :utf-8 "’" :html "&rsquo;"))
5178 ("no"
5179 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5180 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5181 :texinfo "@guillemetleft{}")
5182 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5183 :texinfo "@guillemetright{}")
5184 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5185 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5186 (apostrophe :utf-8 "’" :html "&rsquo;"))
5187 ("nb"
5188 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5189 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5190 :texinfo "@guillemetleft{}")
5191 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5192 :texinfo "@guillemetright{}")
5193 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5194 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5195 (apostrophe :utf-8 "’" :html "&rsquo;"))
5196 ("nn"
5197 ;; https://nn.wikipedia.org/wiki/Sitatteikn
5198 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
5199 :texinfo "@guillemetleft{}")
5200 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
5201 :texinfo "@guillemetright{}")
5202 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
5203 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
5204 (apostrophe :utf-8 "’" :html "&rsquo;"))
5205 ("ru"
5206 ;; http://ru.wikipedia.org/wiki/%D0%9A%D0%B0%D0%B2%D1%8B%D1%87%D0%BA%D0%B8#.D0.9A.D0.B0.D0.B2.D1.8B.D1.87.D0.BA.D0.B8.2C_.D0.B8.D1.81.D0.BF.D0.BE.D0.BB.D1.8C.D0.B7.D1.83.D0.B5.D0.BC.D1.8B.D0.B5_.D0.B2_.D1.80.D1.83.D1.81.D1.81.D0.BA.D0.BE.D0.BC_.D1.8F.D0.B7.D1.8B.D0.BA.D0.B5
5207 ;; http://www.artlebedev.ru/kovodstvo/sections/104/
5208 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "{}<<"
5209 :texinfo "@guillemetleft{}")
5210 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex ">>{}"
5211 :texinfo "@guillemetright{}")
5212 (opening-single-quote :utf-8 "„" :html "&bdquo;" :latex "\\glqq{}"
5213 :texinfo "@quotedblbase{}")
5214 (closing-single-quote :utf-8 "“" :html "&ldquo;" :latex "\\grqq{}"
5215 :texinfo "@quotedblleft{}")
5216 (apostrophe :utf-8 "’" :html: "&#39;"))
5217 ("sv"
5218 ;; based on https://sv.wikipedia.org/wiki/Citattecken
5219 (opening-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
5220 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
5221 (opening-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
5222 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
5223 (apostrophe :utf-8 "’" :html "&rsquo;"))
5225 "Smart quotes translations.
5227 Alist whose CAR is a language string and CDR is an alist with
5228 quote type as key and a plist associating various encodings to
5229 their translation as value.
5231 A quote type can be any symbol among `opening-double-quote',
5232 `closing-double-quote', `opening-single-quote',
5233 `closing-single-quote' and `apostrophe'.
5235 Valid encodings include `:utf-8', `:html', `:latex' and
5236 `:texinfo'.
5238 If no translation is found, the quote character is left as-is.")
5240 (defconst org-export-smart-quotes-regexps
5241 (list
5242 ;; Possible opening quote at beginning of string.
5243 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\|\\s(\\)"
5244 ;; Possible closing quote at beginning of string.
5245 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
5246 ;; Possible apostrophe at beginning of string.
5247 "\\`\\('\\)\\S-"
5248 ;; Opening single and double quotes.
5249 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
5250 ;; Closing single and double quotes.
5251 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
5252 ;; Apostrophe.
5253 "\\S-\\('\\)\\S-"
5254 ;; Possible opening quote at end of string.
5255 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
5256 ;; Possible closing quote at end of string.
5257 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
5258 ;; Possible apostrophe at end of string.
5259 "\\S-\\('\\)\\'")
5260 "List of regexps matching a quote or an apostrophe.
5261 In every regexp, quote or apostrophe matched is put in group 1.")
5263 (defun org-export-activate-smart-quotes (s encoding info &optional original)
5264 "Replace regular quotes with \"smart\" quotes in string S.
5266 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
5267 `:utf-8'. INFO is a plist used as a communication channel.
5269 The function has to retrieve information about string
5270 surroundings in parse tree. It can only happen with an
5271 unmodified string. Thus, if S has already been through another
5272 process, a non-nil ORIGINAL optional argument will provide that
5273 original string.
5275 Return the new string."
5276 (if (equal s "") ""
5277 (let* ((prev (org-export-get-previous-element (or original s) info))
5278 ;; Try to be flexible when computing number of blanks
5279 ;; before object. The previous object may be a string
5280 ;; introduced by the back-end and not completely parsed.
5281 (pre-blank (and prev
5282 (or (org-element-property :post-blank prev)
5283 ;; A string with missing `:post-blank'
5284 ;; property.
5285 (and (stringp prev)
5286 (string-match " *\\'" prev)
5287 (length (match-string 0 prev)))
5288 ;; Fallback value.
5289 0)))
5290 (next (org-export-get-next-element (or original s) info))
5291 (get-smart-quote
5292 (lambda (q type)
5293 ;; Return smart quote associated to a give quote Q, as
5294 ;; a string. TYPE is a symbol among `open', `close' and
5295 ;; `apostrophe'.
5296 (let ((key (case type
5297 (apostrophe 'apostrophe)
5298 (open (if (equal "'" q) 'opening-single-quote
5299 'opening-double-quote))
5300 (otherwise (if (equal "'" q) 'closing-single-quote
5301 'closing-double-quote)))))
5302 (or (plist-get
5303 (cdr (assq key
5304 (cdr (assoc (plist-get info :language)
5305 org-export-smart-quotes-alist))))
5306 encoding)
5307 q)))))
5308 (if (or (equal "\"" s) (equal "'" s))
5309 ;; Only a quote: no regexp can match. We have to check both
5310 ;; sides and decide what to do.
5311 (cond ((and (not prev) (not next)) s)
5312 ((not prev) (funcall get-smart-quote s 'open))
5313 ((and (not next) (zerop pre-blank))
5314 (funcall get-smart-quote s 'close))
5315 ((not next) s)
5316 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
5317 (t (funcall get-smart-quote 'open)))
5318 ;; 1. Replace quote character at the beginning of S.
5319 (cond
5320 ;; Apostrophe?
5321 ((and prev (zerop pre-blank)
5322 (string-match (nth 2 org-export-smart-quotes-regexps) s))
5323 (setq s (replace-match
5324 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
5325 nil t s 1)))
5326 ;; Closing quote?
5327 ((and prev (zerop pre-blank)
5328 (string-match (nth 1 org-export-smart-quotes-regexps) s))
5329 (setq s (replace-match
5330 (funcall get-smart-quote (match-string 1 s) 'close)
5331 nil t s 1)))
5332 ;; Opening quote?
5333 ((and (or (not prev) (> pre-blank 0))
5334 (string-match (nth 0 org-export-smart-quotes-regexps) s))
5335 (setq s (replace-match
5336 (funcall get-smart-quote (match-string 1 s) 'open)
5337 nil t s 1))))
5338 ;; 2. Replace quotes in the middle of the string.
5339 (setq s (replace-regexp-in-string
5340 ;; Opening quotes.
5341 (nth 3 org-export-smart-quotes-regexps)
5342 (lambda (text)
5343 (funcall get-smart-quote (match-string 1 text) 'open))
5344 s nil t 1))
5345 (setq s (replace-regexp-in-string
5346 ;; Closing quotes.
5347 (nth 4 org-export-smart-quotes-regexps)
5348 (lambda (text)
5349 (funcall get-smart-quote (match-string 1 text) 'close))
5350 s nil t 1))
5351 (setq s (replace-regexp-in-string
5352 ;; Apostrophes.
5353 (nth 5 org-export-smart-quotes-regexps)
5354 (lambda (text)
5355 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
5356 s nil t 1))
5357 ;; 3. Replace quote character at the end of S.
5358 (cond
5359 ;; Apostrophe?
5360 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
5361 (setq s (replace-match
5362 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
5363 nil t s 1)))
5364 ;; Closing quote?
5365 ((and (not next)
5366 (string-match (nth 7 org-export-smart-quotes-regexps) s))
5367 (setq s (replace-match
5368 (funcall get-smart-quote (match-string 1 s) 'close)
5369 nil t s 1)))
5370 ;; Opening quote?
5371 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
5372 (setq s (replace-match
5373 (funcall get-smart-quote (match-string 1 s) 'open)
5374 nil t s 1))))
5375 ;; Return string with smart quotes.
5376 s))))
5378 ;;;; Topology
5380 ;; Here are various functions to retrieve information about the
5381 ;; neighborhood of a given element or object. Neighbors of interest
5382 ;; are direct parent (`org-export-get-parent'), parent headline
5383 ;; (`org-export-get-parent-headline'), first element containing an
5384 ;; object, (`org-export-get-parent-element'), parent table
5385 ;; (`org-export-get-parent-table'), previous element or object
5386 ;; (`org-export-get-previous-element') and next element or object
5387 ;; (`org-export-get-next-element').
5389 ;; `org-export-get-genealogy' returns the full genealogy of a given
5390 ;; element or object, from closest parent to full parse tree.
5392 ;; defsubst org-export-get-parent must be defined before first use
5394 (defun org-export-get-genealogy (blob)
5395 "Return full genealogy relative to a given element or object.
5397 BLOB is the element or object being considered.
5399 Ancestors are returned from closest to farthest, the last one
5400 being the full parse tree."
5401 (let (genealogy (parent blob))
5402 (while (setq parent (org-element-property :parent parent))
5403 (push parent genealogy))
5404 (nreverse genealogy)))
5406 (defun org-export-get-parent-headline (blob)
5407 "Return BLOB parent headline or nil.
5408 BLOB is the element or object being considered."
5409 (let ((parent blob))
5410 (while (and (setq parent (org-element-property :parent parent))
5411 (not (eq (org-element-type parent) 'headline))))
5412 parent))
5414 (defun org-export-get-parent-element (object)
5415 "Return first element containing OBJECT or nil.
5416 OBJECT is the object to consider."
5417 (let ((parent object))
5418 (while (and (setq parent (org-element-property :parent parent))
5419 (memq (org-element-type parent) org-element-all-objects)))
5420 parent))
5422 (defun org-export-get-parent-table (object)
5423 "Return OBJECT parent table or nil.
5424 OBJECT is either a `table-cell' or `table-element' type object."
5425 (let ((parent object))
5426 (while (and (setq parent (org-element-property :parent parent))
5427 (not (eq (org-element-type parent) 'table))))
5428 parent))
5430 (defun org-export-get-previous-element (blob info &optional n)
5431 "Return previous element or object.
5433 BLOB is an element or object. INFO is a plist used as
5434 a communication channel. Return previous exportable element or
5435 object, a string, or nil.
5437 When optional argument N is a positive integer, return a list
5438 containing up to N siblings before BLOB, from farthest to
5439 closest. With any other non-nil value, return a list containing
5440 all of them."
5441 (let* ((secondary (org-element-secondary-p blob))
5442 (parent (org-export-get-parent blob))
5443 (siblings
5444 (if secondary (org-element-property secondary parent)
5445 (org-element-contents parent)))
5446 prev)
5447 (catch 'exit
5448 (dolist (obj (cdr (memq blob (reverse siblings))) prev)
5449 (cond ((memq obj (plist-get info :ignore-list)))
5450 ((null n) (throw 'exit obj))
5451 ((not (wholenump n)) (push obj prev))
5452 ((zerop n) (throw 'exit prev))
5453 (t (decf n) (push obj prev)))))))
5455 (defun org-export-get-next-element (blob info &optional n)
5456 "Return next element or object.
5458 BLOB is an element or object. INFO is a plist used as
5459 a communication channel. Return next exportable element or
5460 object, a string, or nil.
5462 When optional argument N is a positive integer, return a list
5463 containing up to N siblings after BLOB, from closest to farthest.
5464 With any other non-nil value, return a list containing all of
5465 them."
5466 (let* ((secondary (org-element-secondary-p blob))
5467 (parent (org-export-get-parent blob))
5468 (siblings
5469 (cdr (memq blob
5470 (if secondary (org-element-property secondary parent)
5471 (org-element-contents parent)))))
5472 next)
5473 (catch 'exit
5474 (dolist (obj siblings (nreverse next))
5475 (cond ((memq obj (plist-get info :ignore-list)))
5476 ((null n) (throw 'exit obj))
5477 ((not (wholenump n)) (push obj next))
5478 ((zerop n) (throw 'exit (nreverse next)))
5479 (t (decf n) (push obj next)))))))
5482 ;;;; Translation
5484 ;; `org-export-translate' translates a string according to the language
5485 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5486 ;; the dictionary used for the translation.
5488 (defconst org-export-dictionary
5489 '(("%e %n: %c"
5490 ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
5491 ("Author"
5492 ("ca" :default "Autor")
5493 ("cs" :default "Autor")
5494 ("da" :default "Forfatter")
5495 ("de" :default "Autor")
5496 ("eo" :html "A&#365;toro")
5497 ("es" :default "Autor")
5498 ("et" :default "Autor")
5499 ("fi" :html "Tekij&auml;")
5500 ("fr" :default "Auteur")
5501 ("hu" :default "Szerz&otilde;")
5502 ("is" :html "H&ouml;fundur")
5503 ("it" :default "Autore")
5504 ("ja" :default "著者" :html "&#33879;&#32773;")
5505 ("nl" :default "Auteur")
5506 ("no" :default "Forfatter")
5507 ("nb" :default "Forfatter")
5508 ("nn" :default "Forfattar")
5509 ("pl" :default "Autor")
5510 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5511 ("sv" :html "F&ouml;rfattare")
5512 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5513 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
5514 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
5515 ("Continued from previous page"
5516 ("de" :default "Fortsetzung von vorheriger Seite")
5517 ("es" :default "Continúa de la página anterior")
5518 ("fr" :default "Suite de la page précédente")
5519 ("it" :default "Continua da pagina precedente")
5520 ("ja" :default "前ページからの続き")
5521 ("nl" :default "Vervolg van vorige pagina")
5522 ("pt" :default "Continuação da página anterior")
5523 ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077;)"
5524 :utf-8 "(Продолжение)"))
5525 ("Continued on next page"
5526 ("de" :default "Fortsetzung nächste Seite")
5527 ("es" :default "Continúa en la siguiente página")
5528 ("fr" :default "Suite page suivante")
5529 ("it" :default "Continua alla pagina successiva")
5530 ("ja" :default "次ページに続く")
5531 ("nl" :default "Vervolg op volgende pagina")
5532 ("pt" :default "Continua na página seguinte")
5533 ("ru" :html "(&#1055;&#1088;&#1086;&#1076;&#1086;&#1083;&#1078;&#1077;&#1085;&#1080;&#1077; &#1089;&#1083;&#1077;&#1076;&#1091;&#1077;&#1090;)"
5534 :utf-8 "(Продолжение следует)"))
5535 ("Date"
5536 ("ca" :default "Data")
5537 ("cs" :default "Datum")
5538 ("da" :default "Dato")
5539 ("de" :default "Datum")
5540 ("eo" :default "Dato")
5541 ("es" :default "Fecha")
5542 ("et" :html "Kuup&#228;ev" :utf-8 "Kuupäev")
5543 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
5544 ("hu" :html "D&aacute;tum")
5545 ("is" :default "Dagsetning")
5546 ("it" :default "Data")
5547 ("ja" :default "日付" :html "&#26085;&#20184;")
5548 ("nl" :default "Datum")
5549 ("no" :default "Dato")
5550 ("nb" :default "Dato")
5551 ("nn" :default "Dato")
5552 ("pl" :default "Data")
5553 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5554 ("sv" :default "Datum")
5555 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5556 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
5557 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
5558 ("Equation"
5559 ("da" :default "Ligning")
5560 ("de" :default "Gleichung")
5561 ("es" :html "Ecuaci&oacute;n" :default "Ecuación")
5562 ("et" :html "V&#245;rrand" :utf-8 "Võrrand")
5563 ("fr" :ascii "Equation" :default "Équation")
5564 ("ja" :default "方程式")
5565 ("no" :default "Ligning")
5566 ("nb" :default "Ligning")
5567 ("nn" :default "Likning")
5568 ("ru" :html "&#1059;&#1088;&#1072;&#1074;&#1085;&#1077;&#1085;&#1080;&#1077;"
5569 :utf-8 "Уравнение")
5570 ("sv" :default "Ekvation")
5571 ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
5572 ("Figure"
5573 ("da" :default "Figur")
5574 ("de" :default "Abbildung")
5575 ("es" :default "Figura")
5576 ("et" :default "Joonis")
5577 ("ja" :default "図" :html "&#22259;")
5578 ("no" :default "Illustrasjon")
5579 ("nb" :default "Illustrasjon")
5580 ("nn" :default "Illustrasjon")
5581 ("ru" :html "&#1056;&#1080;&#1089;&#1091;&#1085;&#1086;&#1082;" :utf-8 "Рисунок")
5582 ("sv" :default "Illustration")
5583 ("zh-CN" :html "&#22270;" :utf-8 "图"))
5584 ("Figure %d:"
5585 ("da" :default "Figur %d")
5586 ("de" :default "Abbildung %d:")
5587 ("es" :default "Figura %d:")
5588 ("et" :default "Joonis %d:")
5589 ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
5590 ("ja" :default "図%d: " :html "&#22259;%d: ")
5591 ("no" :default "Illustrasjon %d")
5592 ("nb" :default "Illustrasjon %d")
5593 ("nn" :default "Illustrasjon %d")
5594 ("ru" :html "&#1056;&#1080;&#1089;. %d.:" :utf-8 "Рис. %d.:")
5595 ("sv" :default "Illustration %d")
5596 ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
5597 ("Footnotes"
5598 ("ca" :html "Peus de p&agrave;gina")
5599 ("cs" :default "Pozn\xe1mky pod carou")
5600 ("da" :default "Fodnoter")
5601 ("de" :html "Fu&szlig;noten" :default "Fußnoten")
5602 ("eo" :default "Piednotoj")
5603 ("es" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
5604 ("et" :html "Allm&#228;rkused" :utf-8 "Allmärkused")
5605 ("fi" :default "Alaviitteet")
5606 ("fr" :default "Notes de bas de page")
5607 ("hu" :html "L&aacute;bjegyzet")
5608 ("is" :html "Aftanm&aacute;lsgreinar")
5609 ("it" :html "Note a pi&egrave; di pagina")
5610 ("ja" :default "脚注" :html "&#33050;&#27880;")
5611 ("nl" :default "Voetnoten")
5612 ("no" :default "Fotnoter")
5613 ("nb" :default "Fotnoter")
5614 ("nn" :default "Fotnotar")
5615 ("pl" :default "Przypis")
5616 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
5617 ("sv" :default "Fotnoter")
5618 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
5619 :utf-8 "Примітки")
5620 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
5621 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
5622 ("List of Listings"
5623 ("da" :default "Programmer")
5624 ("de" :default "Programmauflistungsverzeichnis")
5625 ("es" :default "Indice de Listados de programas")
5626 ("et" :default "Loendite nimekiri")
5627 ("fr" :default "Liste des programmes")
5628 ("ja" :default "ソースコード目次")
5629 ("no" :default "Dataprogrammer")
5630 ("nb" :default "Dataprogrammer")
5631 ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1088;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1086;&#1082;"
5632 :utf-8 "Список распечаток")
5633 ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
5634 ("List of Tables"
5635 ("da" :default "Tabeller")
5636 ("de" :default "Tabellenverzeichnis")
5637 ("es" :default "Indice de tablas")
5638 ("et" :default "Tabelite nimekiri")
5639 ("fr" :default "Liste des tableaux")
5640 ("ja" :default "表目次")
5641 ("no" :default "Tabeller")
5642 ("nb" :default "Tabeller")
5643 ("nn" :default "Tabeller")
5644 ("ru" :html "&#1057;&#1087;&#1080;&#1089;&#1086;&#1082; &#1090;&#1072;&#1073;&#1083;&#1080;&#1094;"
5645 :utf-8 "Список таблиц")
5646 ("sv" :default "Tabeller")
5647 ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
5648 ("Listing %d:"
5649 ("da" :default "Program %d")
5650 ("de" :default "Programmlisting %d")
5651 ("es" :default "Listado de programa %d")
5652 ("et" :default "Loend %d")
5653 ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
5654 ("ja" :default "ソースコード%d:")
5655 ("no" :default "Dataprogram %d")
5656 ("nb" :default "Dataprogram %d")
5657 ("ru" :html "&#1056;&#1072;&#1089;&#1087;&#1077;&#1095;&#1072;&#1090;&#1082;&#1072; %d.:"
5658 :utf-8 "Распечатка %d.:")
5659 ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
5660 ("References"
5661 ("fr" :ascii "References" :default "Références")
5662 ("de" :default "Quellen"))
5663 ("See section %s"
5664 ("da" :default "jævnfør afsnit %s")
5665 ("de" :default "siehe Abschnitt %s")
5666 ("es" :default "vea seccion %s")
5667 ("et" :html "Vaata peat&#252;kki %s" :utf-8 "Vaata peatükki %s")
5668 ("fr" :default "cf. section %s")
5669 ("ja" :default "セクション %s を参照")
5670 ("ru" :html "&#1057;&#1084;. &#1088;&#1072;&#1079;&#1076;&#1077;&#1083; %s"
5671 :utf-8 "См. раздел %s")
5672 ("zh-CN" :html "&#21442;&#35265;&#31532;%s&#33410;" :utf-8 "参见第%s节"))
5673 ("Table"
5674 ("de" :default "Tabelle")
5675 ("es" :default "Tabla")
5676 ("et" :default "Tabel")
5677 ("fr" :default "Tableau")
5678 ("ja" :default "表" :html "&#34920;")
5679 ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072;" :utf-8 "Таблица")
5680 ("zh-CN" :html "&#34920;" :utf-8 "表"))
5681 ("Table %d:"
5682 ("da" :default "Tabel %d")
5683 ("de" :default "Tabelle %d")
5684 ("es" :default "Tabla %d")
5685 ("et" :default "Tabel %d")
5686 ("fr" :default "Tableau %d :")
5687 ("ja" :default "表%d:" :html "&#34920;%d:")
5688 ("no" :default "Tabell %d")
5689 ("nb" :default "Tabell %d")
5690 ("nn" :default "Tabell %d")
5691 ("ru" :html "&#1058;&#1072;&#1073;&#1083;&#1080;&#1094;&#1072; %d.:"
5692 :utf-8 "Таблица %d.:")
5693 ("sv" :default "Tabell %d")
5694 ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
5695 ("Table of Contents"
5696 ("ca" :html "&Iacute;ndex")
5697 ("cs" :default "Obsah")
5698 ("da" :default "Indhold")
5699 ("de" :default "Inhaltsverzeichnis")
5700 ("eo" :default "Enhavo")
5701 ("es" :html "&Iacute;ndice")
5702 ("et" :default "Sisukord")
5703 ("fi" :html "Sis&auml;llysluettelo")
5704 ("fr" :ascii "Sommaire" :default "Table des matières")
5705 ("hu" :html "Tartalomjegyz&eacute;k")
5706 ("is" :default "Efnisyfirlit")
5707 ("it" :default "Indice")
5708 ("ja" :default "目次" :html "&#30446;&#27425;")
5709 ("nl" :default "Inhoudsopgave")
5710 ("no" :default "Innhold")
5711 ("nb" :default "Innhold")
5712 ("nn" :default "Innhald")
5713 ("pl" :html "Spis tre&#x015b;ci")
5714 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
5715 :utf-8 "Содержание")
5716 ("sv" :html "Inneh&aring;ll")
5717 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
5718 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
5719 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
5720 ("Unknown reference"
5721 ("da" :default "ukendt reference")
5722 ("de" :default "Unbekannter Verweis")
5723 ("es" :default "referencia desconocida")
5724 ("et" :default "Tundmatu viide")
5725 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
5726 ("ja" :default "不明な参照先")
5727 ("ru" :html "&#1053;&#1077;&#1080;&#1079;&#1074;&#1077;&#1089;&#1090;&#1085;&#1072;&#1103; &#1089;&#1089;&#1099;&#1083;&#1082;&#1072;"
5728 :utf-8 "Неизвестная ссылка")
5729 ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
5730 "Dictionary for export engine.
5732 Alist whose CAR is the string to translate and CDR is an alist
5733 whose CAR is the language string and CDR is a plist whose
5734 properties are possible charsets and values translated terms.
5736 It is used as a database for `org-export-translate'. Since this
5737 function returns the string as-is if no translation was found,
5738 the variable only needs to record values different from the
5739 entry.")
5741 (defun org-export-translate (s encoding info)
5742 "Translate string S according to language specification.
5744 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5745 and `:utf-8'. INFO is a plist used as a communication channel.
5747 Translation depends on `:language' property. Return the
5748 translated string. If no translation is found, try to fall back
5749 to `:default' encoding. If it fails, return S."
5750 (let* ((lang (plist-get info :language))
5751 (translations (cdr (assoc lang
5752 (cdr (assoc s org-export-dictionary))))))
5753 (or (plist-get translations encoding)
5754 (plist-get translations :default)
5755 s)))
5759 ;;; Asynchronous Export
5761 ;; `org-export-async-start' is the entry point for asynchronous
5762 ;; export. It recreates current buffer (including visibility,
5763 ;; narrowing and visited file) in an external Emacs process, and
5764 ;; evaluates a command there. It then applies a function on the
5765 ;; returned results in the current process.
5767 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5768 ;; allow to export to a buffer or a file, asynchronously or not.
5770 ;; `org-export-output-file-name' is an auxiliary function meant to be
5771 ;; used with `org-export-to-file'. With a given extension, it tries
5772 ;; to provide a canonical file name to write export output to.
5774 ;; Asynchronously generated results are never displayed directly.
5775 ;; Instead, they are stored in `org-export-stack-contents'. They can
5776 ;; then be retrieved by calling `org-export-stack'.
5778 ;; Export Stack is viewed through a dedicated major mode
5779 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5780 ;;`org-export-stack-delete', `org-export-stack-view' and
5781 ;;`org-export-stack-clear'.
5783 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5784 ;; It should be used whenever `org-export-async-start' is called.
5786 (defmacro org-export-async-start (fun &rest body)
5787 "Call function FUN on the results returned by BODY evaluation.
5789 FUN is an anonymous function of one argument. BODY evaluation
5790 happens in an asynchronous process, from a buffer which is an
5791 exact copy of the current one.
5793 Use `org-export-add-to-stack' in FUN in order to register results
5794 in the stack.
5796 This is a low level function. See also `org-export-to-buffer'
5797 and `org-export-to-file' for more specialized functions."
5798 (declare (indent 1) (debug t))
5799 (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
5800 ;; Write the full sexp evaluating BODY in a copy of the current
5801 ;; buffer to a temporary file, as it may be too long for program
5802 ;; args in `start-process'.
5803 `(with-temp-message "Initializing asynchronous export process"
5804 (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
5805 (,temp-file (make-temp-file "org-export-process"))
5806 (,coding buffer-file-coding-system))
5807 (with-temp-file ,temp-file
5808 (insert
5809 ;; Null characters (from variable values) are inserted
5810 ;; within the file. As a consequence, coding system for
5811 ;; buffer contents will not be recognized properly. So,
5812 ;; we make sure it is the same as the one used to display
5813 ;; the original buffer.
5814 (format ";; -*- coding: %s; -*-\n%S"
5815 ,coding
5816 `(with-temp-buffer
5817 (when org-export-async-debug '(setq debug-on-error t))
5818 ;; Ignore `kill-emacs-hook' and code evaluation
5819 ;; queries from Babel as we need a truly
5820 ;; non-interactive process.
5821 (setq kill-emacs-hook nil
5822 org-babel-confirm-evaluate-answer-no t)
5823 ;; Initialize export framework.
5824 (require 'ox)
5825 ;; Re-create current buffer there.
5826 (funcall ,,copy-fun)
5827 (restore-buffer-modified-p nil)
5828 ;; Sexp to evaluate in the buffer.
5829 (print (progn ,,@body))))))
5830 ;; Start external process.
5831 (let* ((process-connection-type nil)
5832 (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
5833 (,process
5834 (apply
5835 #'start-process
5836 (append
5837 (list "org-export-process"
5838 ,proc-buffer
5839 (expand-file-name invocation-name invocation-directory)
5840 "--batch")
5841 (if org-export-async-init-file
5842 (list "-Q" "-l" org-export-async-init-file)
5843 (list "-l" user-init-file))
5844 (list "-l" ,temp-file)))))
5845 ;; Register running process in stack.
5846 (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
5847 ;; Set-up sentinel in order to catch results.
5848 (let ((handler ,fun))
5849 (set-process-sentinel
5850 ,process
5851 `(lambda (p status)
5852 (let ((proc-buffer (process-buffer p)))
5853 (when (eq (process-status p) 'exit)
5854 (unwind-protect
5855 (if (zerop (process-exit-status p))
5856 (unwind-protect
5857 (let ((results
5858 (with-current-buffer proc-buffer
5859 (goto-char (point-max))
5860 (backward-sexp)
5861 (read (current-buffer)))))
5862 (funcall ,handler results))
5863 (unless org-export-async-debug
5864 (and (get-buffer proc-buffer)
5865 (kill-buffer proc-buffer))))
5866 (org-export-add-to-stack proc-buffer nil p)
5867 (ding)
5868 (message "Process '%s' exited abnormally" p))
5869 (unless org-export-async-debug
5870 (delete-file ,,temp-file)))))))))))))
5872 ;;;###autoload
5873 (defun org-export-to-buffer
5874 (backend buffer
5875 &optional async subtreep visible-only body-only ext-plist
5876 post-process)
5877 "Call `org-export-as' with output to a specified buffer.
5879 BACKEND is either an export back-end, as returned by, e.g.,
5880 `org-export-create-backend', or a symbol referring to
5881 a registered back-end.
5883 BUFFER is the name of the output buffer. If it already exists,
5884 it will be erased first, otherwise, it will be created.
5886 A non-nil optional argument ASYNC means the process should happen
5887 asynchronously. The resulting buffer should then be accessible
5888 through the `org-export-stack' interface. When ASYNC is nil, the
5889 buffer is displayed if `org-export-show-temporary-export-buffer'
5890 is non-nil.
5892 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5893 EXT-PLIST are similar to those used in `org-export-as', which
5894 see.
5896 Optional argument POST-PROCESS is a function which should accept
5897 no argument. It is always called within the current process,
5898 from BUFFER, with point at its beginning. Export back-ends can
5899 use it to set a major mode there, e.g,
5901 \(defun org-latex-export-as-latex
5902 \(&optional async subtreep visible-only body-only ext-plist)
5903 \(interactive)
5904 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5905 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5907 This function returns BUFFER."
5908 (declare (indent 2))
5909 (if async
5910 (org-export-async-start
5911 `(lambda (output)
5912 (with-current-buffer (get-buffer-create ,buffer)
5913 (erase-buffer)
5914 (setq buffer-file-coding-system ',buffer-file-coding-system)
5915 (insert output)
5916 (goto-char (point-min))
5917 (org-export-add-to-stack (current-buffer) ',backend)
5918 (ignore-errors (funcall ,post-process))))
5919 `(org-export-as
5920 ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
5921 (let ((output
5922 (org-export-as backend subtreep visible-only body-only ext-plist))
5923 (buffer (get-buffer-create buffer))
5924 (encoding buffer-file-coding-system))
5925 (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
5926 (org-kill-new output))
5927 (with-current-buffer buffer
5928 (erase-buffer)
5929 (setq buffer-file-coding-system encoding)
5930 (insert output)
5931 (goto-char (point-min))
5932 (and (functionp post-process) (funcall post-process)))
5933 (when org-export-show-temporary-export-buffer
5934 (switch-to-buffer-other-window buffer))
5935 buffer)))
5937 ;;;###autoload
5938 (defun org-export-to-file
5939 (backend file &optional async subtreep visible-only body-only ext-plist
5940 post-process)
5941 "Call `org-export-as' with output to a specified file.
5943 BACKEND is either an export back-end, as returned by, e.g.,
5944 `org-export-create-backend', or a symbol referring to
5945 a registered back-end. FILE is the name of the output file, as
5946 a string.
5948 A non-nil optional argument ASYNC means the process should happen
5949 asynchronously. The resulting buffer will then be accessible
5950 through the `org-export-stack' interface.
5952 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5953 EXT-PLIST are similar to those used in `org-export-as', which
5954 see.
5956 Optional argument POST-PROCESS is called with FILE as its
5957 argument and happens asynchronously when ASYNC is non-nil. It
5958 has to return a file name, or nil. Export back-ends can use this
5959 to send the output file through additional processing, e.g,
5961 \(defun org-latex-export-to-latex
5962 \(&optional async subtreep visible-only body-only ext-plist)
5963 \(interactive)
5964 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5965 \(org-export-to-file 'latex outfile
5966 async subtreep visible-only body-only ext-plist
5967 \(lambda (file) (org-latex-compile file)))
5969 The function returns either a file name returned by POST-PROCESS,
5970 or FILE."
5971 (declare (indent 2))
5972 (if (not (file-writable-p file)) (error "Output file not writable")
5973 (let ((ext-plist (org-combine-plists `(:output-file ,file) ext-plist))
5974 (encoding (or org-export-coding-system buffer-file-coding-system)))
5975 (if async
5976 (org-export-async-start
5977 `(lambda (file)
5978 (org-export-add-to-stack (expand-file-name file) ',backend))
5979 `(let ((output
5980 (org-export-as
5981 ',backend ,subtreep ,visible-only ,body-only
5982 ',ext-plist)))
5983 (with-temp-buffer
5984 (insert output)
5985 (let ((coding-system-for-write ',encoding))
5986 (write-file ,file)))
5987 (or (ignore-errors (funcall ',post-process ,file)) ,file)))
5988 (let ((output (org-export-as
5989 backend subtreep visible-only body-only ext-plist)))
5990 (with-temp-buffer
5991 (insert output)
5992 (let ((coding-system-for-write encoding))
5993 (write-file file)))
5994 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
5995 (org-kill-new output))
5996 ;; Get proper return value.
5997 (or (and (functionp post-process) (funcall post-process file))
5998 file))))))
6000 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
6001 "Return output file's name according to buffer specifications.
6003 EXTENSION is a string representing the output file extension,
6004 with the leading dot.
6006 With a non-nil optional argument SUBTREEP, try to determine
6007 output file's name by looking for \"EXPORT_FILE_NAME\" property
6008 of subtree at point.
6010 When optional argument PUB-DIR is set, use it as the publishing
6011 directory.
6013 When optional argument VISIBLE-ONLY is non-nil, don't export
6014 contents of hidden elements.
6016 Return file name as a string."
6017 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
6018 (base-name
6019 ;; File name may come from EXPORT_FILE_NAME subtree
6020 ;; property, assuming point is at beginning of said
6021 ;; sub-tree.
6022 (file-name-sans-extension
6023 (or (and subtreep
6024 (org-entry-get
6025 (save-excursion
6026 (ignore-errors (org-back-to-heading) (point)))
6027 "EXPORT_FILE_NAME" t))
6028 ;; File name may be extracted from buffer's associated
6029 ;; file, if any.
6030 (and visited-file (file-name-nondirectory visited-file))
6031 ;; Can't determine file name on our own: Ask user.
6032 (let ((read-file-name-function
6033 (and org-completion-use-ido 'ido-read-file-name)))
6034 (read-file-name
6035 "Output file: " pub-dir nil nil nil
6036 (lambda (name)
6037 (string= (file-name-extension name t) extension)))))))
6038 (output-file
6039 ;; Build file name. Enforce EXTENSION over whatever user
6040 ;; may have come up with. PUB-DIR, if defined, always has
6041 ;; precedence over any provided path.
6042 (cond
6043 (pub-dir
6044 (concat (file-name-as-directory pub-dir)
6045 (file-name-nondirectory base-name)
6046 extension))
6047 ((file-name-absolute-p base-name) (concat base-name extension))
6048 (t (concat (file-name-as-directory ".") base-name extension)))))
6049 ;; If writing to OUTPUT-FILE would overwrite original file, append
6050 ;; EXTENSION another time to final name.
6051 (if (and visited-file (org-file-equal-p visited-file output-file))
6052 (concat output-file extension)
6053 output-file)))
6055 (defun org-export-add-to-stack (source backend &optional process)
6056 "Add a new result to export stack if not present already.
6058 SOURCE is a buffer or a file name containing export results.
6059 BACKEND is a symbol representing export back-end used to generate
6062 Entries already pointing to SOURCE and unavailable entries are
6063 removed beforehand. Return the new stack."
6064 (setq org-export-stack-contents
6065 (cons (list source backend (or process (current-time)))
6066 (org-export-stack-remove source))))
6068 (defun org-export-stack ()
6069 "Menu for asynchronous export results and running processes."
6070 (interactive)
6071 (let ((buffer (get-buffer-create "*Org Export Stack*")))
6072 (set-buffer buffer)
6073 (when (zerop (buffer-size)) (org-export-stack-mode))
6074 (org-export-stack-refresh)
6075 (pop-to-buffer buffer))
6076 (message "Type \"q\" to quit, \"?\" for help"))
6078 (defun org-export--stack-source-at-point ()
6079 "Return source from export results at point in stack."
6080 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
6081 (if (not source) (error "Source unavailable, please refresh buffer")
6082 (let ((source-name (if (stringp source) source (buffer-name source))))
6083 (if (save-excursion
6084 (beginning-of-line)
6085 (looking-at (concat ".* +" (regexp-quote source-name) "$")))
6086 source
6087 ;; SOURCE is not consistent with current line. The stack
6088 ;; view is outdated.
6089 (error "Source unavailable; type `g' to update buffer"))))))
6091 (defun org-export-stack-clear ()
6092 "Remove all entries from export stack."
6093 (interactive)
6094 (setq org-export-stack-contents nil))
6096 (defun org-export-stack-refresh (&rest dummy)
6097 "Refresh the asynchronous export stack.
6098 DUMMY is ignored. Unavailable sources are removed from the list.
6099 Return the new stack."
6100 (let ((inhibit-read-only t))
6101 (org-preserve-lc
6102 (erase-buffer)
6103 (insert (concat
6104 (let ((counter 0))
6105 (mapconcat
6106 (lambda (entry)
6107 (let ((proc-p (processp (nth 2 entry))))
6108 (concat
6109 ;; Back-end.
6110 (format " %-12s " (or (nth 1 entry) ""))
6111 ;; Age.
6112 (let ((data (nth 2 entry)))
6113 (if proc-p (format " %6s " (process-status data))
6114 ;; Compute age of the results.
6115 (org-format-seconds
6116 "%4h:%.2m "
6117 (float-time (time-since data)))))
6118 ;; Source.
6119 (format " %s"
6120 (let ((source (car entry)))
6121 (if (stringp source) source
6122 (buffer-name source)))))))
6123 ;; Clear stack from exited processes, dead buffers or
6124 ;; non-existent files.
6125 (setq org-export-stack-contents
6126 (org-remove-if-not
6127 (lambda (el)
6128 (if (processp (nth 2 el))
6129 (buffer-live-p (process-buffer (nth 2 el)))
6130 (let ((source (car el)))
6131 (if (bufferp source) (buffer-live-p source)
6132 (file-exists-p source)))))
6133 org-export-stack-contents)) "\n")))))))
6135 (defun org-export-stack-remove (&optional source)
6136 "Remove export results at point from stack.
6137 If optional argument SOURCE is non-nil, remove it instead."
6138 (interactive)
6139 (let ((source (or source (org-export--stack-source-at-point))))
6140 (setq org-export-stack-contents
6141 (org-remove-if (lambda (el) (equal (car el) source))
6142 org-export-stack-contents))))
6144 (defun org-export-stack-view (&optional in-emacs)
6145 "View export results at point in stack.
6146 With an optional prefix argument IN-EMACS, force viewing files
6147 within Emacs."
6148 (interactive "P")
6149 (let ((source (org-export--stack-source-at-point)))
6150 (cond ((processp source)
6151 (org-switch-to-buffer-other-window (process-buffer source)))
6152 ((bufferp source) (org-switch-to-buffer-other-window source))
6153 (t (org-open-file source in-emacs)))))
6155 (defvar org-export-stack-mode-map
6156 (let ((km (make-sparse-keymap)))
6157 (define-key km " " 'next-line)
6158 (define-key km "n" 'next-line)
6159 (define-key km "\C-n" 'next-line)
6160 (define-key km [down] 'next-line)
6161 (define-key km "p" 'previous-line)
6162 (define-key km "\C-p" 'previous-line)
6163 (define-key km "\C-?" 'previous-line)
6164 (define-key km [up] 'previous-line)
6165 (define-key km "C" 'org-export-stack-clear)
6166 (define-key km "v" 'org-export-stack-view)
6167 (define-key km (kbd "RET") 'org-export-stack-view)
6168 (define-key km "d" 'org-export-stack-remove)
6170 "Keymap for Org Export Stack.")
6172 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
6173 "Mode for displaying asynchronous export stack.
6175 Type \\[org-export-stack] to visualize the asynchronous export
6176 stack.
6178 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
6179 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
6180 stack completely.
6182 Removing entries in an Org Export Stack buffer doesn't affect
6183 files or buffers, only the display.
6185 \\{org-export-stack-mode-map}"
6186 (abbrev-mode 0)
6187 (auto-fill-mode 0)
6188 (setq buffer-read-only t
6189 buffer-undo-list t
6190 truncate-lines t
6191 header-line-format
6192 '(:eval
6193 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
6194 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
6195 (set (make-local-variable 'revert-buffer-function)
6196 'org-export-stack-refresh))
6200 ;;; The Dispatcher
6202 ;; `org-export-dispatch' is the standard interactive way to start an
6203 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
6204 ;; for its interface, which, in turn, delegates response to key
6205 ;; pressed to `org-export--dispatch-action'.
6207 ;;;###autoload
6208 (defun org-export-dispatch (&optional arg)
6209 "Export dispatcher for Org mode.
6211 It provides an access to common export related tasks in a buffer.
6212 Its interface comes in two flavors: standard and expert.
6214 While both share the same set of bindings, only the former
6215 displays the valid keys associations in a dedicated buffer.
6216 Scrolling (resp. line-wise motion) in this buffer is done with
6217 SPC and DEL (resp. C-n and C-p) keys.
6219 Set variable `org-export-dispatch-use-expert-ui' to switch to one
6220 flavor or the other.
6222 When ARG is \\[universal-argument], repeat the last export action, with the same set
6223 of options used back then, on the current buffer.
6225 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
6226 (interactive "P")
6227 (let* ((input
6228 (cond ((equal arg '(16)) '(stack))
6229 ((and arg org-export-dispatch-last-action))
6230 (t (save-window-excursion
6231 (unwind-protect
6232 (progn
6233 ;; Remember where we are
6234 (move-marker org-export-dispatch-last-position
6235 (point)
6236 (org-base-buffer (current-buffer)))
6237 ;; Get and store an export command
6238 (setq org-export-dispatch-last-action
6239 (org-export--dispatch-ui
6240 (list org-export-initial-scope
6241 (and org-export-in-background 'async))
6243 org-export-dispatch-use-expert-ui)))
6244 (and (get-buffer "*Org Export Dispatcher*")
6245 (kill-buffer "*Org Export Dispatcher*")))))))
6246 (action (car input))
6247 (optns (cdr input)))
6248 (unless (memq 'subtree optns)
6249 (move-marker org-export-dispatch-last-position nil))
6250 (case action
6251 ;; First handle special hard-coded actions.
6252 (template (org-export-insert-default-template nil optns))
6253 (stack (org-export-stack))
6254 (publish-current-file
6255 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
6256 (publish-current-project
6257 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
6258 (publish-choose-project
6259 (org-publish (assoc (org-icompleting-read
6260 "Publish project: "
6261 org-publish-project-alist nil t)
6262 org-publish-project-alist)
6263 (memq 'force optns)
6264 (memq 'async optns)))
6265 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
6266 (otherwise
6267 (save-excursion
6268 (when arg
6269 ;; Repeating command, maybe move cursor to restore subtree
6270 ;; context.
6271 (if (eq (marker-buffer org-export-dispatch-last-position)
6272 (org-base-buffer (current-buffer)))
6273 (goto-char org-export-dispatch-last-position)
6274 ;; We are in a different buffer, forget position.
6275 (move-marker org-export-dispatch-last-position nil)))
6276 (funcall action
6277 ;; Return a symbol instead of a list to ease
6278 ;; asynchronous export macro use.
6279 (and (memq 'async optns) t)
6280 (and (memq 'subtree optns) t)
6281 (and (memq 'visible optns) t)
6282 (and (memq 'body optns) t)))))))
6284 (defun org-export--dispatch-ui (options first-key expertp)
6285 "Handle interface for `org-export-dispatch'.
6287 OPTIONS is a list containing current interactive options set for
6288 export. It can contain any of the following symbols:
6289 `body' toggles a body-only export
6290 `subtree' restricts export to current subtree
6291 `visible' restricts export to visible part of buffer.
6292 `force' force publishing files.
6293 `async' use asynchronous export process
6295 FIRST-KEY is the key pressed to select the first level menu. It
6296 is nil when this menu hasn't been selected yet.
6298 EXPERTP, when non-nil, triggers expert UI. In that case, no help
6299 buffer is provided, but indications about currently active
6300 options are given in the prompt. Moreover, \[?] allows to switch
6301 back to standard interface."
6302 (let* ((fontify-key
6303 (lambda (key &optional access-key)
6304 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
6305 ;; non-nil is the required first-level key to activate
6306 ;; KEY. When its value is t, activate KEY independently
6307 ;; on the first key, if any. A nil value means KEY will
6308 ;; only be activated at first level.
6309 (if (or (eq access-key t) (eq access-key first-key))
6310 (org-propertize key 'face 'org-warning)
6311 key)))
6312 (fontify-value
6313 (lambda (value)
6314 ;; Fontify VALUE string.
6315 (org-propertize value 'face 'font-lock-variable-name-face)))
6316 ;; Prepare menu entries by extracting them from registered
6317 ;; back-ends and sorting them by access key and by ordinal,
6318 ;; if any.
6319 (entries
6320 (sort (sort (delq nil
6321 (mapcar 'org-export-backend-menu
6322 org-export--registered-backends))
6323 (lambda (a b)
6324 (let ((key-a (nth 1 a))
6325 (key-b (nth 1 b)))
6326 (cond ((and (numberp key-a) (numberp key-b))
6327 (< key-a key-b))
6328 ((numberp key-b) t)))))
6329 'car-less-than-car))
6330 ;; Compute a list of allowed keys based on the first key
6331 ;; pressed, if any. Some keys
6332 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
6333 ;; available.
6334 (allowed-keys
6335 (nconc (list 2 22 19 6 1)
6336 (if (not first-key) (org-uniquify (mapcar 'car entries))
6337 (let (sub-menu)
6338 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6339 (when (eq (car entry) first-key)
6340 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6341 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6342 ((not first-key) (list ?P)))
6343 (list ?& ?#)
6344 (when expertp (list ??))
6345 (list ?q)))
6346 ;; Build the help menu for standard UI.
6347 (help
6348 (unless expertp
6349 (concat
6350 ;; Options are hard-coded.
6351 (format "[%s] Body only: %s [%s] Visible only: %s
6352 \[%s] Export scope: %s [%s] Force publishing: %s
6353 \[%s] Async export: %s\n\n"
6354 (funcall fontify-key "C-b" t)
6355 (funcall fontify-value
6356 (if (memq 'body options) "On " "Off"))
6357 (funcall fontify-key "C-v" t)
6358 (funcall fontify-value
6359 (if (memq 'visible options) "On " "Off"))
6360 (funcall fontify-key "C-s" t)
6361 (funcall fontify-value
6362 (if (memq 'subtree options) "Subtree" "Buffer "))
6363 (funcall fontify-key "C-f" t)
6364 (funcall fontify-value
6365 (if (memq 'force options) "On " "Off"))
6366 (funcall fontify-key "C-a" t)
6367 (funcall fontify-value
6368 (if (memq 'async options) "On " "Off")))
6369 ;; Display registered back-end entries. When a key
6370 ;; appears for the second time, do not create another
6371 ;; entry, but append its sub-menu to existing menu.
6372 (let (last-key)
6373 (mapconcat
6374 (lambda (entry)
6375 (let ((top-key (car entry)))
6376 (concat
6377 (unless (eq top-key last-key)
6378 (setq last-key top-key)
6379 (format "\n[%s] %s\n"
6380 (funcall fontify-key (char-to-string top-key))
6381 (nth 1 entry)))
6382 (let ((sub-menu (nth 2 entry)))
6383 (unless (functionp sub-menu)
6384 ;; Split sub-menu into two columns.
6385 (let ((index -1))
6386 (concat
6387 (mapconcat
6388 (lambda (sub-entry)
6389 (incf index)
6390 (format
6391 (if (zerop (mod index 2)) " [%s] %-26s"
6392 "[%s] %s\n")
6393 (funcall fontify-key
6394 (char-to-string (car sub-entry))
6395 top-key)
6396 (nth 1 sub-entry)))
6397 sub-menu "")
6398 (when (zerop (mod index 2)) "\n"))))))))
6399 entries ""))
6400 ;; Publishing menu is hard-coded.
6401 (format "\n[%s] Publish
6402 [%s] Current file [%s] Current project
6403 [%s] Choose project [%s] All projects\n\n\n"
6404 (funcall fontify-key "P")
6405 (funcall fontify-key "f" ?P)
6406 (funcall fontify-key "p" ?P)
6407 (funcall fontify-key "x" ?P)
6408 (funcall fontify-key "a" ?P))
6409 (format "[%s] Export stack [%s] Insert template\n"
6410 (funcall fontify-key "&" t)
6411 (funcall fontify-key "#" t))
6412 (format "[%s] %s"
6413 (funcall fontify-key "q" t)
6414 (if first-key "Main menu" "Exit")))))
6415 ;; Build prompts for both standard and expert UI.
6416 (standard-prompt (unless expertp "Export command: "))
6417 (expert-prompt
6418 (when expertp
6419 (format
6420 "Export command (C-%s%s%s%s%s) [%s]: "
6421 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6422 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6423 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6424 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6425 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6426 (mapconcat (lambda (k)
6427 ;; Strip control characters.
6428 (unless (< k 27) (char-to-string k)))
6429 allowed-keys "")))))
6430 ;; With expert UI, just read key with a fancy prompt. In standard
6431 ;; UI, display an intrusive help buffer.
6432 (if expertp
6433 (org-export--dispatch-action
6434 expert-prompt allowed-keys entries options first-key expertp)
6435 ;; At first call, create frame layout in order to display menu.
6436 (unless (get-buffer "*Org Export Dispatcher*")
6437 (delete-other-windows)
6438 (org-switch-to-buffer-other-window
6439 (get-buffer-create "*Org Export Dispatcher*"))
6440 (setq cursor-type nil
6441 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6442 ;; Make sure that invisible cursor will not highlight square
6443 ;; brackets.
6444 (set-syntax-table (copy-syntax-table))
6445 (modify-syntax-entry ?\[ "w"))
6446 ;; At this point, the buffer containing the menu exists and is
6447 ;; visible in the current window. So, refresh it.
6448 (with-current-buffer "*Org Export Dispatcher*"
6449 ;; Refresh help. Maintain display continuity by re-visiting
6450 ;; previous window position.
6451 (let ((pos (window-start)))
6452 (erase-buffer)
6453 (insert help)
6454 (set-window-start nil pos)))
6455 (org-fit-window-to-buffer)
6456 (org-export--dispatch-action
6457 standard-prompt allowed-keys entries options first-key expertp))))
6459 (defun org-export--dispatch-action
6460 (prompt allowed-keys entries options first-key expertp)
6461 "Read a character from command input and act accordingly.
6463 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6464 a list of characters available at a given step in the process.
6465 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6466 EXPERTP are the same as defined in `org-export--dispatch-ui',
6467 which see.
6469 Toggle export options when required. Otherwise, return value is
6470 a list with action as CAR and a list of interactive export
6471 options as CDR."
6472 (let (key)
6473 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6474 ;; C-p, SPC, DEL).
6475 (while (and (setq key (read-char-exclusive prompt))
6476 (not expertp)
6477 (memq key '(14 16 ?\s ?\d)))
6478 (case key
6479 (14 (if (not (pos-visible-in-window-p (point-max)))
6480 (ignore-errors (scroll-up 1))
6481 (message "End of buffer")
6482 (sit-for 1)))
6483 (16 (if (not (pos-visible-in-window-p (point-min)))
6484 (ignore-errors (scroll-down 1))
6485 (message "Beginning of buffer")
6486 (sit-for 1)))
6487 (?\s (if (not (pos-visible-in-window-p (point-max)))
6488 (scroll-up nil)
6489 (message "End of buffer")
6490 (sit-for 1)))
6491 (?\d (if (not (pos-visible-in-window-p (point-min)))
6492 (scroll-down nil)
6493 (message "Beginning of buffer")
6494 (sit-for 1)))))
6495 (cond
6496 ;; Ignore undefined associations.
6497 ((not (memq key allowed-keys))
6498 (ding)
6499 (unless expertp (message "Invalid key") (sit-for 1))
6500 (org-export--dispatch-ui options first-key expertp))
6501 ;; q key at first level aborts export. At second level, cancel
6502 ;; first key instead.
6503 ((eq key ?q) (if (not first-key) (error "Export aborted")
6504 (org-export--dispatch-ui options nil expertp)))
6505 ;; Help key: Switch back to standard interface if expert UI was
6506 ;; active.
6507 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6508 ;; Send request for template insertion along with export scope.
6509 ((eq key ?#) (cons 'template (memq 'subtree options)))
6510 ;; Switch to asynchronous export stack.
6511 ((eq key ?&) '(stack))
6512 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6513 ((memq key '(2 22 19 6 1))
6514 (org-export--dispatch-ui
6515 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6516 (6 'force) (1 'async))))
6517 (if (memq option options) (remq option options)
6518 (cons option options)))
6519 first-key expertp))
6520 ;; Action selected: Send key and options back to
6521 ;; `org-export-dispatch'.
6522 ((or first-key (functionp (nth 2 (assq key entries))))
6523 (cons (cond
6524 ((not first-key) (nth 2 (assq key entries)))
6525 ;; Publishing actions are hard-coded. Send a special
6526 ;; signal to `org-export-dispatch'.
6527 ((eq first-key ?P)
6528 (case key
6529 (?f 'publish-current-file)
6530 (?p 'publish-current-project)
6531 (?x 'publish-choose-project)
6532 (?a 'publish-all)))
6533 ;; Return first action associated to FIRST-KEY + KEY
6534 ;; path. Indeed, derived backends can share the same
6535 ;; FIRST-KEY.
6536 (t (catch 'found
6537 (mapc (lambda (entry)
6538 (let ((match (assq key (nth 2 entry))))
6539 (when match (throw 'found (nth 2 match)))))
6540 (member (assq first-key entries) entries)))))
6541 options))
6542 ;; Otherwise, enter sub-menu.
6543 (t (org-export--dispatch-ui options key expertp)))))
6547 (provide 'ox)
6549 ;; Local variables:
6550 ;; generated-autoload-file: "org-loaddefs.el"
6551 ;; End:
6553 ;;; ox.el ends here