org.el (org-align-tags-here): Fix bug: move to the correct position
[org-mode.git] / lisp / ox.el
blob72a7a056cf4b6f496de113f681775a21ed936c05
1 ;;; ox.el --- Generic Export Engine for Org Mode
3 ;; Copyright (C) 2012, 2013 Free Software Foundation, Inc.
5 ;; Author: Nicolas Goaziou <n.goaziou at gmail dot com>
6 ;; Keywords: outlines, hypermedia, calendar, wp
8 ;; GNU Emacs is free software: you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation, either version 3 of the License, or
11 ;; (at your option) any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21 ;;; Commentary:
23 ;; This library implements a generic export engine for Org, built on
24 ;; its syntactical parser: Org Elements.
26 ;; Besides that parser, the generic exporter is made of three distinct
27 ;; parts:
29 ;; - The communication channel consists in a property list, which is
30 ;; created and updated during the process. Its use is to offer
31 ;; every piece of information, would it be about initial environment
32 ;; or contextual data, all in a single place. The exhaustive list
33 ;; of properties is given in "The Communication Channel" section of
34 ;; this file.
36 ;; - The transcoder walks the parse tree, ignores or treat as plain
37 ;; text elements and objects according to export options, and
38 ;; eventually calls back-end specific functions to do the real
39 ;; transcoding, concatenating their return value along the way.
41 ;; - The filter system is activated at the very beginning and the very
42 ;; end of the export process, and each time an element or an object
43 ;; has been converted. It is the entry point to fine-tune standard
44 ;; output from back-end transcoders. See "The Filter System"
45 ;; section for more information.
47 ;; The core function is `org-export-as'. It returns the transcoded
48 ;; buffer as a string.
50 ;; An export back-end is defined with `org-export-define-backend'.
51 ;; This function can also support specific buffer keywords, OPTION
52 ;; keyword's items and filters. Refer to function's documentation for
53 ;; more information.
55 ;; If the new back-end shares most properties with another one,
56 ;; `org-export-define-derived-backend' can be used to simplify the
57 ;; process.
59 ;; Any back-end can define its own variables. Among them, those
60 ;; customizable should belong to the `org-export-BACKEND' group.
62 ;; Tools for common tasks across back-ends are implemented in the
63 ;; following part of the file.
65 ;; Then, a wrapper macro for asynchronous export,
66 ;; `org-export-async-start', along with tools to display results. are
67 ;; given in the penultimate part.
69 ;; Eventually, a dispatcher (`org-export-dispatch') for standard
70 ;; back-ends is provided in the last one.
72 ;;; Code:
74 (eval-when-compile (require 'cl))
75 (require 'org-element)
76 (require 'org-macro)
77 (require 'ob-exp)
79 (declare-function org-publish "ox-publish" (project &optional force async))
80 (declare-function org-publish-all "ox-publish" (&optional force async))
81 (declare-function
82 org-publish-current-file "ox-publish" (&optional force async))
83 (declare-function org-publish-current-project "ox-publish"
84 (&optional force async))
86 (defvar org-publish-project-alist)
87 (defvar org-table-number-fraction)
88 (defvar org-table-number-regexp)
92 ;;; Internal Variables
94 ;; Among internal variables, the most important is
95 ;; `org-export-options-alist'. This variable define the global export
96 ;; options, shared between every exporter, and how they are acquired.
98 (defconst org-export-max-depth 19
99 "Maximum nesting depth for headlines, counting from 0.")
101 (defconst org-export-options-alist
102 '((:author "AUTHOR" nil user-full-name t)
103 (:creator "CREATOR" nil org-export-creator-string)
104 (:date "DATE" nil nil t)
105 (:description "DESCRIPTION" nil nil newline)
106 (:email "EMAIL" nil user-mail-address t)
107 (:exclude-tags "EXCLUDE_TAGS" nil org-export-exclude-tags split)
108 (:headline-levels nil "H" org-export-headline-levels)
109 (:keywords "KEYWORDS" nil nil space)
110 (:language "LANGUAGE" nil org-export-default-language t)
111 (:preserve-breaks nil "\\n" org-export-preserve-breaks)
112 (:section-numbers nil "num" org-export-with-section-numbers)
113 (:select-tags "SELECT_TAGS" nil org-export-select-tags split)
114 (:time-stamp-file nil "timestamp" org-export-time-stamp-file)
115 (:title "TITLE" nil nil space)
116 (:with-archived-trees nil "arch" org-export-with-archived-trees)
117 (:with-author nil "author" org-export-with-author)
118 (:with-clocks nil "c" org-export-with-clocks)
119 (:with-creator nil "creator" org-export-with-creator)
120 (:with-date nil "date" org-export-with-date)
121 (:with-drawers nil "d" org-export-with-drawers)
122 (:with-email nil "email" org-export-with-email)
123 (:with-emphasize nil "*" org-export-with-emphasize)
124 (:with-entities nil "e" org-export-with-entities)
125 (:with-fixed-width nil ":" org-export-with-fixed-width)
126 (:with-footnotes nil "f" org-export-with-footnotes)
127 (:with-inlinetasks nil "inline" org-export-with-inlinetasks)
128 (:with-latex nil "tex" org-export-with-latex)
129 (:with-planning nil "p" org-export-with-planning)
130 (:with-priority nil "pri" org-export-with-priority)
131 (:with-smart-quotes nil "'" org-export-with-smart-quotes)
132 (:with-special-strings nil "-" org-export-with-special-strings)
133 (:with-statistics-cookies nil "stat" org-export-with-statistics-cookies)
134 (:with-sub-superscript nil "^" org-export-with-sub-superscripts)
135 (:with-toc nil "toc" org-export-with-toc)
136 (:with-tables nil "|" org-export-with-tables)
137 (:with-tags nil "tags" org-export-with-tags)
138 (:with-tasks nil "tasks" org-export-with-tasks)
139 (:with-timestamps nil "<" org-export-with-timestamps)
140 (:with-todo-keywords nil "todo" org-export-with-todo-keywords))
141 "Alist between export properties and ways to set them.
143 The CAR of the alist is the property name, and the CDR is a list
144 like (KEYWORD OPTION DEFAULT BEHAVIOUR) where:
146 KEYWORD is a string representing a buffer keyword, or nil. Each
147 property defined this way can also be set, during subtree
148 export, through a headline property named after the keyword
149 with the \"EXPORT_\" prefix (i.e. DATE keyword and EXPORT_DATE
150 property).
151 OPTION is a string that could be found in an #+OPTIONS: line.
152 DEFAULT is the default value for the property.
153 BEHAVIOUR determines how Org should handle multiple keywords for
154 the same property. It is a symbol among:
155 nil Keep old value and discard the new one.
156 t Replace old value with the new one.
157 `space' Concatenate the values, separating them with a space.
158 `newline' Concatenate the values, separating them with
159 a newline.
160 `split' Split values at white spaces, and cons them to the
161 previous list.
163 Values set through KEYWORD and OPTION have precedence over
164 DEFAULT.
166 All these properties should be back-end agnostic. Back-end
167 specific properties are set through `org-export-define-backend'.
168 Properties redefined there have precedence over these.")
170 (defconst org-export-special-keywords '("FILETAGS" "SETUPFILE" "OPTIONS")
171 "List of in-buffer keywords that require special treatment.
172 These keywords are not directly associated to a property. The
173 way they are handled must be hard-coded into
174 `org-export--get-inbuffer-options' function.")
176 (defconst org-export-filters-alist
177 '((:filter-bold . org-export-filter-bold-functions)
178 (:filter-babel-call . org-export-filter-babel-call-functions)
179 (:filter-center-block . org-export-filter-center-block-functions)
180 (:filter-clock . org-export-filter-clock-functions)
181 (:filter-code . org-export-filter-code-functions)
182 (:filter-comment . org-export-filter-comment-functions)
183 (:filter-comment-block . org-export-filter-comment-block-functions)
184 (:filter-diary-sexp . org-export-filter-diary-sexp-functions)
185 (:filter-drawer . org-export-filter-drawer-functions)
186 (:filter-dynamic-block . org-export-filter-dynamic-block-functions)
187 (:filter-entity . org-export-filter-entity-functions)
188 (:filter-example-block . org-export-filter-example-block-functions)
189 (:filter-export-block . org-export-filter-export-block-functions)
190 (:filter-export-snippet . org-export-filter-export-snippet-functions)
191 (:filter-final-output . org-export-filter-final-output-functions)
192 (:filter-fixed-width . org-export-filter-fixed-width-functions)
193 (:filter-footnote-definition . org-export-filter-footnote-definition-functions)
194 (:filter-footnote-reference . org-export-filter-footnote-reference-functions)
195 (:filter-headline . org-export-filter-headline-functions)
196 (:filter-horizontal-rule . org-export-filter-horizontal-rule-functions)
197 (:filter-inline-babel-call . org-export-filter-inline-babel-call-functions)
198 (:filter-inline-src-block . org-export-filter-inline-src-block-functions)
199 (:filter-inlinetask . org-export-filter-inlinetask-functions)
200 (:filter-italic . org-export-filter-italic-functions)
201 (:filter-item . org-export-filter-item-functions)
202 (:filter-keyword . org-export-filter-keyword-functions)
203 (:filter-latex-environment . org-export-filter-latex-environment-functions)
204 (:filter-latex-fragment . org-export-filter-latex-fragment-functions)
205 (:filter-line-break . org-export-filter-line-break-functions)
206 (:filter-link . org-export-filter-link-functions)
207 (:filter-node-property . org-export-filter-node-property-functions)
208 (:filter-options . org-export-filter-options-functions)
209 (:filter-paragraph . org-export-filter-paragraph-functions)
210 (:filter-parse-tree . org-export-filter-parse-tree-functions)
211 (:filter-plain-list . org-export-filter-plain-list-functions)
212 (:filter-plain-text . org-export-filter-plain-text-functions)
213 (:filter-planning . org-export-filter-planning-functions)
214 (:filter-property-drawer . org-export-filter-property-drawer-functions)
215 (:filter-quote-block . org-export-filter-quote-block-functions)
216 (:filter-quote-section . org-export-filter-quote-section-functions)
217 (:filter-radio-target . org-export-filter-radio-target-functions)
218 (:filter-section . org-export-filter-section-functions)
219 (:filter-special-block . org-export-filter-special-block-functions)
220 (:filter-src-block . org-export-filter-src-block-functions)
221 (:filter-statistics-cookie . org-export-filter-statistics-cookie-functions)
222 (:filter-strike-through . org-export-filter-strike-through-functions)
223 (:filter-subscript . org-export-filter-subscript-functions)
224 (:filter-superscript . org-export-filter-superscript-functions)
225 (:filter-table . org-export-filter-table-functions)
226 (:filter-table-cell . org-export-filter-table-cell-functions)
227 (:filter-table-row . org-export-filter-table-row-functions)
228 (:filter-target . org-export-filter-target-functions)
229 (:filter-timestamp . org-export-filter-timestamp-functions)
230 (:filter-underline . org-export-filter-underline-functions)
231 (:filter-verbatim . org-export-filter-verbatim-functions)
232 (:filter-verse-block . org-export-filter-verse-block-functions))
233 "Alist between filters properties and initial values.
235 The key of each association is a property name accessible through
236 the communication channel. Its value is a configurable global
237 variable defining initial filters.
239 This list is meant to install user specified filters. Back-end
240 developers may install their own filters using
241 `org-export-define-backend'. Filters defined there will always
242 be prepended to the current list, so they always get applied
243 first.")
245 (defconst org-export-default-inline-image-rule
246 `(("file" .
247 ,(format "\\.%s\\'"
248 (regexp-opt
249 '("png" "jpeg" "jpg" "gif" "tiff" "tif" "xbm"
250 "xpm" "pbm" "pgm" "ppm") t))))
251 "Default rule for link matching an inline image.
252 This rule applies to links with no description. By default, it
253 will be considered as an inline image if it targets a local file
254 whose extension is either \"png\", \"jpeg\", \"jpg\", \"gif\",
255 \"tiff\", \"tif\", \"xbm\", \"xpm\", \"pbm\", \"pgm\" or \"ppm\".
256 See `org-export-inline-image-p' for more information about
257 rules.")
259 (defvar org-export-async-debug nil
260 "Non-nil means asynchronous export process should leave data behind.
262 This data is found in the appropriate \"*Org Export Process*\"
263 buffer, and in files prefixed with \"org-export-process\" and
264 located in `temporary-file-directory'.
266 When non-nil, it will also set `debug-on-error' to a non-nil
267 value in the external process.")
269 (defvar org-export-stack-contents nil
270 "Record asynchronously generated export results and processes.
271 This is an alist: its CAR is the source of the
272 result (destination file or buffer for a finished process,
273 original buffer for a running one) and its CDR is a list
274 containing the back-end used, as a symbol, and either a process
275 or the time at which it finished. It is used to build the menu
276 from `org-export-stack'.")
278 (defvar org-export--registered-backends nil
279 "List of backends currently available in the exporter.
280 This variable is set with `org-export-define-backend' and
281 `org-export-define-derived-backend' functions.")
283 (defvar org-export-dispatch-last-action nil
284 "Last command called from the dispatcher.
285 The value should be a list. Its CAR is the action, as a symbol,
286 and its CDR is a list of export options.")
288 (defvar org-export-dispatch-last-position (make-marker)
289 "The position where the last export command was created using the dispatcher.
290 This marker will be used with `C-u C-c C-e' to make sure export repetition
291 uses the same subtree if the previous command was restricted to a subtree.")
293 ;; For compatibility with Org < 8
294 (defvar org-export-current-backend nil
295 "Name, if any, of the back-end used during an export process.
297 Its value is a symbol such as `html', `latex', `ascii', or nil if
298 the back-end is anonymous (see `org-export-create-backend') or if
299 there is no export process in progress.
301 It can be used to teach Babel blocks how to act differently
302 according to the back-end used.")
305 ;;; User-configurable Variables
307 ;; Configuration for the masses.
309 ;; They should never be accessed directly, as their value is to be
310 ;; stored in a property list (cf. `org-export-options-alist').
311 ;; Back-ends will read their value from there instead.
313 (defgroup org-export nil
314 "Options for exporting Org mode files."
315 :tag "Org Export"
316 :group 'org)
318 (defgroup org-export-general nil
319 "General options for export engine."
320 :tag "Org Export General"
321 :group 'org-export)
323 (defcustom org-export-with-archived-trees 'headline
324 "Whether sub-trees with the ARCHIVE tag should be exported.
326 This can have three different values:
327 nil Do not export, pretend this tree is not present.
328 t Do export the entire tree.
329 `headline' Only export the headline, but skip the tree below it.
331 This option can also be set with the OPTIONS keyword,
332 e.g. \"arch:nil\"."
333 :group 'org-export-general
334 :type '(choice
335 (const :tag "Not at all" nil)
336 (const :tag "Headline only" headline)
337 (const :tag "Entirely" t)))
339 (defcustom org-export-with-author t
340 "Non-nil means insert author name into the exported file.
341 This option can also be set with the OPTIONS keyword,
342 e.g. \"author:nil\"."
343 :group 'org-export-general
344 :type 'boolean)
346 (defcustom org-export-with-clocks nil
347 "Non-nil means export CLOCK keywords.
348 This option can also be set with the OPTIONS keyword,
349 e.g. \"c:t\"."
350 :group 'org-export-general
351 :type 'boolean)
353 (defcustom org-export-with-creator 'comment
354 "Non-nil means the postamble should contain a creator sentence.
356 The sentence can be set in `org-export-creator-string' and
357 defaults to \"Generated by Org mode XX in Emacs XXX.\".
359 If the value is `comment' insert it as a comment."
360 :group 'org-export-general
361 :type '(choice
362 (const :tag "No creator sentence" nil)
363 (const :tag "Sentence as a comment" 'comment)
364 (const :tag "Insert the sentence" t)))
366 (defcustom org-export-with-date t
367 "Non-nil means insert date in the exported document.
368 This option can also be set with the OPTIONS keyword,
369 e.g. \"date:nil\"."
370 :group 'org-export-general
371 :type 'boolean)
373 (defcustom org-export-date-timestamp-format nil
374 "Time-stamp format string to use for DATE keyword.
376 The format string, when specified, only applies if date consists
377 in a single time-stamp. Otherwise its value will be ignored.
379 See `format-time-string' for details on how to build this
380 string."
381 :group 'org-export-general
382 :type '(choice
383 (string :tag "Time-stamp format string")
384 (const :tag "No format string" nil)))
386 (defcustom org-export-creator-string
387 (format "Emacs %s (Org mode %s)"
388 emacs-version
389 (if (fboundp 'org-version) (org-version) "unknown version"))
390 "Information about the creator of the document.
391 This option can also be set on with the CREATOR keyword."
392 :group 'org-export-general
393 :type '(string :tag "Creator string"))
395 (defcustom org-export-with-drawers '(not "LOGBOOK")
396 "Non-nil means export contents of standard drawers.
398 When t, all drawers are exported. This may also be a list of
399 drawer names to export. If that list starts with `not', only
400 drawers with such names will be ignored.
402 This variable doesn't apply to properties drawers.
404 This option can also be set with the OPTIONS keyword,
405 e.g. \"d:nil\"."
406 :group 'org-export-general
407 :version "24.4"
408 :package-version '(Org . "8.0")
409 :type '(choice
410 (const :tag "All drawers" t)
411 (const :tag "None" nil)
412 (repeat :tag "Selected drawers"
413 (string :tag "Drawer name"))
414 (list :tag "Ignored drawers"
415 (const :format "" not)
416 (repeat :tag "Specify names of drawers to ignore during export"
417 :inline t
418 (string :tag "Drawer name")))))
420 (defcustom org-export-with-email nil
421 "Non-nil means insert author email into the exported file.
422 This option can also be set with the OPTIONS keyword,
423 e.g. \"email:t\"."
424 :group 'org-export-general
425 :type 'boolean)
427 (defcustom org-export-with-emphasize t
428 "Non-nil means interpret *word*, /word/, _word_ and +word+.
430 If the export target supports emphasizing text, the word will be
431 typeset in bold, italic, with an underline or strike-through,
432 respectively.
434 This option can also be set with the OPTIONS keyword,
435 e.g. \"*:nil\"."
436 :group 'org-export-general
437 :type 'boolean)
439 (defcustom org-export-exclude-tags '("noexport")
440 "Tags that exclude a tree from export.
442 All trees carrying any of these tags will be excluded from
443 export. This is without condition, so even subtrees inside that
444 carry one of the `org-export-select-tags' will be removed.
446 This option can also be set with the EXCLUDE_TAGS keyword."
447 :group 'org-export-general
448 :type '(repeat (string :tag "Tag")))
450 (defcustom org-export-with-fixed-width t
451 "Non-nil means lines starting with \":\" will be in fixed width font.
453 This can be used to have pre-formatted text, fragments of code
454 etc. For example:
455 : ;; Some Lisp examples
456 : (while (defc cnt)
457 : (ding))
458 will be looking just like this in also HTML. See also the QUOTE
459 keyword. Not all export backends support this.
461 This option can also be set with the OPTIONS keyword,
462 e.g. \"::nil\"."
463 :group 'org-export-general
464 :type 'boolean)
466 (defcustom org-export-with-footnotes t
467 "Non-nil means Org footnotes should be exported.
468 This option can also be set with the OPTIONS keyword,
469 e.g. \"f:nil\"."
470 :group 'org-export-general
471 :type 'boolean)
473 (defcustom org-export-with-latex t
474 "Non-nil means process LaTeX environments and fragments.
476 This option can also be set with the OPTIONS line,
477 e.g. \"tex:verbatim\". Allowed values are:
479 nil Ignore math snippets.
480 `verbatim' Keep everything in verbatim.
481 t Allow export of math snippets."
482 :group 'org-export-general
483 :version "24.4"
484 :package-version '(Org . "8.0")
485 :type '(choice
486 (const :tag "Do not process math in any way" nil)
487 (const :tag "Interpret math snippets" t)
488 (const :tag "Leave math verbatim" verbatim)))
490 (defcustom org-export-headline-levels 3
491 "The last level which is still exported as a headline.
493 Inferior levels will usually produce itemize or enumerate lists
494 when exported, but back-end behaviour may differ.
496 This option can also be set with the OPTIONS keyword,
497 e.g. \"H:2\"."
498 :group 'org-export-general
499 :type 'integer)
501 (defcustom org-export-default-language "en"
502 "The default language for export and clocktable translations, as a string.
503 This may have an association in
504 `org-clock-clocktable-language-setup',
505 `org-export-smart-quotes-alist' and `org-export-dictionary'.
506 This option can also be set with the LANGUAGE keyword."
507 :group 'org-export-general
508 :type '(string :tag "Language"))
510 (defcustom org-export-preserve-breaks nil
511 "Non-nil means preserve all line breaks when exporting.
512 This option can also be set with the OPTIONS keyword,
513 e.g. \"\\n:t\"."
514 :group 'org-export-general
515 :type 'boolean)
517 (defcustom org-export-with-entities t
518 "Non-nil means interpret entities when exporting.
520 For example, HTML export converts \\alpha to &alpha; and \\AA to
521 &Aring;.
523 For a list of supported names, see the constant `org-entities'
524 and the user option `org-entities-user'.
526 This option can also be set with the OPTIONS keyword,
527 e.g. \"e:nil\"."
528 :group 'org-export-general
529 :type 'boolean)
531 (defcustom org-export-with-inlinetasks t
532 "Non-nil means inlinetasks should be exported.
533 This option can also be set with the OPTIONS keyword,
534 e.g. \"inline:nil\"."
535 :group 'org-export-general
536 :version "24.4"
537 :package-version '(Org . "8.0")
538 :type 'boolean)
540 (defcustom org-export-with-planning nil
541 "Non-nil means include planning info in export.
543 Planning info is the line containing either SCHEDULED:,
544 DEADLINE:, CLOSED: time-stamps, or a combination of them.
546 This option can also be set with the OPTIONS keyword,
547 e.g. \"p:t\"."
548 :group 'org-export-general
549 :version "24.4"
550 :package-version '(Org . "8.0")
551 :type 'boolean)
553 (defcustom org-export-with-priority nil
554 "Non-nil means include priority cookies in export.
555 This option can also be set with the OPTIONS keyword,
556 e.g. \"pri:t\"."
557 :group 'org-export-general
558 :type 'boolean)
560 (defcustom org-export-with-section-numbers t
561 "Non-nil means add section numbers to headlines when exporting.
563 When set to an integer n, numbering will only happen for
564 headlines whose relative level is higher or equal to n.
566 This option can also be set with the OPTIONS keyword,
567 e.g. \"num:t\"."
568 :group 'org-export-general
569 :type 'boolean)
571 (defcustom org-export-select-tags '("export")
572 "Tags that select a tree for export.
574 If any such tag is found in a buffer, all trees that do not carry
575 one of these tags will be ignored during export. Inside trees
576 that are selected like this, you can still deselect a subtree by
577 tagging it with one of the `org-export-exclude-tags'.
579 This option can also be set with the SELECT_TAGS keyword."
580 :group 'org-export-general
581 :type '(repeat (string :tag "Tag")))
583 (defcustom org-export-with-smart-quotes nil
584 "Non-nil means activate smart quotes during export.
585 This option can also be set with the OPTIONS keyword,
586 e.g., \"':t\".
588 When setting this to non-nil, you need to take care of
589 using the correct Babel package when exporting to LaTeX.
590 E.g., you can load Babel for french like this:
592 #+LATEX_HEADER: \\usepackage[french]{babel}"
593 :group 'org-export-general
594 :version "24.4"
595 :package-version '(Org . "8.0")
596 :type 'boolean)
598 (defcustom org-export-with-special-strings t
599 "Non-nil means interpret \"\\-\", \"--\" and \"---\" for export.
601 When this option is turned on, these strings will be exported as:
603 Org HTML LaTeX UTF-8
604 -----+----------+--------+-------
605 \\- &shy; \\-
606 -- &ndash; -- –
607 --- &mdash; --- —
608 ... &hellip; \\ldots …
610 This option can also be set with the OPTIONS keyword,
611 e.g. \"-:nil\"."
612 :group 'org-export-general
613 :type 'boolean)
615 (defcustom org-export-with-statistics-cookies t
616 "Non-nil means include statistics cookies in export.
617 This option can also be set with the OPTIONS keyword,
618 e.g. \"stat:nil\""
619 :group 'org-export-general
620 :version "24.4"
621 :package-version '(Org . "8.0")
622 :type 'boolean)
624 (defcustom org-export-with-sub-superscripts t
625 "Non-nil means interpret \"_\" and \"^\" for export.
627 When this option is turned on, you can use TeX-like syntax for
628 sub- and superscripts. Several characters after \"_\" or \"^\"
629 will be considered as a single item - so grouping with {} is
630 normally not needed. For example, the following things will be
631 parsed as single sub- or superscripts.
633 10^24 or 10^tau several digits will be considered 1 item.
634 10^-12 or 10^-tau a leading sign with digits or a word
635 x^2-y^3 will be read as x^2 - y^3, because items are
636 terminated by almost any nonword/nondigit char.
637 x_{i^2} or x^(2-i) braces or parenthesis do grouping.
639 Still, ambiguity is possible - so when in doubt use {} to enclose
640 the sub/superscript. If you set this variable to the symbol
641 `{}', the braces are *required* in order to trigger
642 interpretations as sub/superscript. This can be helpful in
643 documents that need \"_\" frequently in plain text.
645 This option can also be set with the OPTIONS keyword,
646 e.g. \"^:nil\"."
647 :group 'org-export-general
648 :type '(choice
649 (const :tag "Interpret them" t)
650 (const :tag "Curly brackets only" {})
651 (const :tag "Do not interpret them" nil)))
653 (defcustom org-export-with-toc t
654 "Non-nil means create a table of contents in exported files.
656 The TOC contains headlines with levels up
657 to`org-export-headline-levels'. When an integer, include levels
658 up to N in the toc, this may then be different from
659 `org-export-headline-levels', but it will not be allowed to be
660 larger than the number of headline levels. When nil, no table of
661 contents is made.
663 This option can also be set with the OPTIONS keyword,
664 e.g. \"toc:nil\" or \"toc:3\"."
665 :group 'org-export-general
666 :type '(choice
667 (const :tag "No Table of Contents" nil)
668 (const :tag "Full Table of Contents" t)
669 (integer :tag "TOC to level")))
671 (defcustom org-export-with-tables t
672 "If non-nil, lines starting with \"|\" define a table.
673 For example:
675 | Name | Address | Birthday |
676 |-------------+----------+-----------|
677 | Arthur Dent | England | 29.2.2100 |
679 This option can also be set with the OPTIONS keyword,
680 e.g. \"|:nil\"."
681 :group 'org-export-general
682 :type 'boolean)
684 (defcustom org-export-with-tags t
685 "If nil, do not export tags, just remove them from headlines.
687 If this is the symbol `not-in-toc', tags will be removed from
688 table of contents entries, but still be shown in the headlines of
689 the document.
691 This option can also be set with the OPTIONS keyword,
692 e.g. \"tags:nil\"."
693 :group 'org-export-general
694 :type '(choice
695 (const :tag "Off" nil)
696 (const :tag "Not in TOC" not-in-toc)
697 (const :tag "On" t)))
699 (defcustom org-export-with-tasks t
700 "Non-nil means include TODO items for export.
702 This may have the following values:
703 t include tasks independent of state.
704 `todo' include only tasks that are not yet done.
705 `done' include only tasks that are already done.
706 nil ignore all tasks.
707 list of keywords include tasks with these keywords.
709 This option can also be set with the OPTIONS keyword,
710 e.g. \"tasks:nil\"."
711 :group 'org-export-general
712 :type '(choice
713 (const :tag "All tasks" t)
714 (const :tag "No tasks" nil)
715 (const :tag "Not-done tasks" todo)
716 (const :tag "Only done tasks" done)
717 (repeat :tag "Specific TODO keywords"
718 (string :tag "Keyword"))))
720 (defcustom org-export-time-stamp-file t
721 "Non-nil means insert a time stamp into the exported file.
722 The time stamp shows when the file was created. This option can
723 also be set with the OPTIONS keyword, e.g. \"timestamp:nil\"."
724 :group 'org-export-general
725 :type 'boolean)
727 (defcustom org-export-with-timestamps t
728 "Non nil means allow timestamps in export.
730 It can be set to any of the following values:
731 t export all timestamps.
732 `active' export active timestamps only.
733 `inactive' export inactive timestamps only.
734 nil do not export timestamps
736 This only applies to timestamps isolated in a paragraph
737 containing only timestamps. Other timestamps are always
738 exported.
740 This option can also be set with the OPTIONS keyword, e.g.
741 \"<:nil\"."
742 :group 'org-export-general
743 :type '(choice
744 (const :tag "All timestamps" t)
745 (const :tag "Only active timestamps" active)
746 (const :tag "Only inactive timestamps" inactive)
747 (const :tag "No timestamp" nil)))
749 (defcustom org-export-with-todo-keywords t
750 "Non-nil means include TODO keywords in export.
751 When nil, remove all these keywords from the export. This option
752 can also be set with the OPTIONS keyword, e.g. \"todo:nil\"."
753 :group 'org-export-general
754 :type 'boolean)
756 (defcustom org-export-allow-bind-keywords nil
757 "Non-nil means BIND keywords can define local variable values.
758 This is a potential security risk, which is why the default value
759 is nil. You can also allow them through local buffer variables."
760 :group 'org-export-general
761 :version "24.4"
762 :package-version '(Org . "8.0")
763 :type 'boolean)
765 (defcustom org-export-snippet-translation-alist nil
766 "Alist between export snippets back-ends and exporter back-ends.
768 This variable allows to provide shortcuts for export snippets.
770 For example, with a value of '\(\(\"h\" . \"html\"\)\), the
771 HTML back-end will recognize the contents of \"@@h:<b>@@\" as
772 HTML code while every other back-end will ignore it."
773 :group 'org-export-general
774 :version "24.4"
775 :package-version '(Org . "8.0")
776 :type '(repeat
777 (cons (string :tag "Shortcut")
778 (string :tag "Back-end"))))
780 (defcustom org-export-coding-system nil
781 "Coding system for the exported file."
782 :group 'org-export-general
783 :version "24.4"
784 :package-version '(Org . "8.0")
785 :type 'coding-system)
787 (defcustom org-export-copy-to-kill-ring 'if-interactive
788 "Should we push exported content to the kill ring?"
789 :group 'org-export-general
790 :version "24.3"
791 :type '(choice
792 (const :tag "Always" t)
793 (const :tag "When export is done interactively" if-interactive)
794 (const :tag "Never" nil)))
796 (defcustom org-export-initial-scope 'buffer
797 "The initial scope when exporting with `org-export-dispatch'.
798 This variable can be either set to `buffer' or `subtree'."
799 :group 'org-export-general
800 :type '(choice
801 (const :tag "Export current buffer" buffer)
802 (const :tag "Export current subtree" subtree)))
804 (defcustom org-export-show-temporary-export-buffer t
805 "Non-nil means show buffer after exporting to temp buffer.
806 When Org exports to a file, the buffer visiting that file is ever
807 shown, but remains buried. However, when exporting to
808 a temporary buffer, that buffer is popped up in a second window.
809 When this variable is nil, the buffer remains buried also in
810 these cases."
811 :group 'org-export-general
812 :type 'boolean)
814 (defcustom org-export-in-background nil
815 "Non-nil means export and publishing commands will run in background.
816 Results from an asynchronous export are never displayed
817 automatically. But you can retrieve them with \\[org-export-stack]."
818 :group 'org-export-general
819 :version "24.4"
820 :package-version '(Org . "8.0")
821 :type 'boolean)
823 (defcustom org-export-async-init-file user-init-file
824 "File used to initialize external export process.
825 Value must be an absolute file name. It defaults to user's
826 initialization file. Though, a specific configuration makes the
827 process faster and the export more portable."
828 :group 'org-export-general
829 :version "24.4"
830 :package-version '(Org . "8.0")
831 :type '(file :must-match t))
833 (defcustom org-export-dispatch-use-expert-ui nil
834 "Non-nil means using a non-intrusive `org-export-dispatch'.
835 In that case, no help buffer is displayed. Though, an indicator
836 for current export scope is added to the prompt (\"b\" when
837 output is restricted to body only, \"s\" when it is restricted to
838 the current subtree, \"v\" when only visible elements are
839 considered for export, \"f\" when publishing functions should be
840 passed the FORCE argument and \"a\" when the export should be
841 asynchronous). Also, \[?] allows to switch back to standard
842 mode."
843 :group 'org-export-general
844 :version "24.4"
845 :package-version '(Org . "8.0")
846 :type 'boolean)
850 ;;; Defining Back-ends
852 ;; An export back-end is a structure with `org-export-backend' type
853 ;; and `name', `parent', `transcoders', `options', `filters', `blocks'
854 ;; and `menu' slots.
856 ;; At the lowest level, a back-end is created with
857 ;; `org-export-create-backend' function.
859 ;; A named back-end can be registered with
860 ;; `org-export-register-backend' function. A registered back-end can
861 ;; later be referred to by its name, with `org-export-get-backend'
862 ;; function. Also, such a back-end can become the parent of a derived
863 ;; back-end from which slot values will be inherited by default.
864 ;; `org-export-derived-backend-p' can check if a given back-end is
865 ;; derived from a list of back-end names.
867 ;; `org-export-get-all-transcoders', `org-export-get-all-options' and
868 ;; `org-export-get-all-filters' return the full alist of transcoders,
869 ;; options and filters, including those inherited from ancestors.
871 ;; At a higher level, `org-export-define-backend' is the standard way
872 ;; to define an export back-end. If the new back-end is similar to
873 ;; a registered back-end, `org-export-define-derived-backend' may be
874 ;; used instead.
876 ;; Eventually `org-export-barf-if-invalid-backend' returns an error
877 ;; when a given back-end hasn't been registered yet.
879 (defstruct (org-export-backend (:constructor org-export-create-backend)
880 (:copier nil))
881 name parent transcoders options filters blocks menu)
883 (defun org-export-get-backend (name)
884 "Return export back-end named after NAME.
885 NAME is a symbol. Return nil if no such back-end is found."
886 (catch 'found
887 (dolist (b org-export--registered-backends)
888 (when (eq (org-export-backend-name b) name)
889 (throw 'found b)))))
891 (defun org-export-register-backend (backend)
892 "Register BACKEND as a known export back-end.
893 BACKEND is a structure with `org-export-backend' type."
894 ;; Refuse to register an unnamed back-end.
895 (unless (org-export-backend-name backend)
896 (error "Cannot register a unnamed export back-end"))
897 ;; Refuse to register a back-end with an unknown parent.
898 (let ((parent (org-export-backend-parent backend)))
899 (when (and parent (not (org-export-get-backend parent)))
900 (error "Cannot use unknown \"%s\" back-end as a parent" parent)))
901 ;; Register dedicated export blocks in the parser.
902 (dolist (name (org-export-backend-blocks backend))
903 (add-to-list 'org-element-block-name-alist
904 (cons name 'org-element-export-block-parser)))
905 ;; If a back-end with the same name as BACKEND is already
906 ;; registered, replace it with BACKEND. Otherwise, simply add
907 ;; BACKEND to the list of registered back-ends.
908 (let ((old (org-export-get-backend (org-export-backend-name backend))))
909 (if old (setcar (memq old org-export--registered-backends) backend)
910 (push backend org-export--registered-backends))))
912 (defun org-export-barf-if-invalid-backend (backend)
913 "Signal an error if BACKEND isn't defined."
914 (unless (org-export-backend-p backend)
915 (error "Unknown \"%s\" back-end: Aborting export" backend)))
917 (defun org-export-derived-backend-p (backend &rest backends)
918 "Non-nil if BACKEND is derived from one of BACKENDS.
919 BACKEND is an export back-end, as returned by, e.g.,
920 `org-export-create-backend', or a symbol referring to
921 a registered back-end. BACKENDS is constituted of symbols."
922 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
923 (when backend
924 (catch 'exit
925 (while (org-export-backend-parent backend)
926 (when (memq (org-export-backend-name backend) backends)
927 (throw 'exit t))
928 (setq backend
929 (org-export-get-backend (org-export-backend-parent backend))))
930 (memq (org-export-backend-name backend) backends))))
932 (defun org-export-get-all-transcoders (backend)
933 "Return full translation table for BACKEND.
935 BACKEND is an export back-end, as return by, e.g,,
936 `org-export-create-backend'. Return value is an alist where
937 keys are element or object types, as symbols, and values are
938 transcoders.
940 Unlike to `org-export-backend-transcoders', this function
941 also returns transcoders inherited from parent back-ends,
942 if any."
943 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
944 (when backend
945 (let ((transcoders (org-export-backend-transcoders backend))
946 parent)
947 (while (setq parent (org-export-backend-parent backend))
948 (setq backend (org-export-get-backend parent))
949 (setq transcoders
950 (append transcoders (org-export-backend-transcoders backend))))
951 transcoders)))
953 (defun org-export-get-all-options (backend)
954 "Return export options for BACKEND.
956 BACKEND is an export back-end, as return by, e.g,,
957 `org-export-create-backend'. See `org-export-options-alist'
958 for the shape of the return value.
960 Unlike to `org-export-backend-options', this function also
961 returns options inherited from parent back-ends, if any."
962 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
963 (when backend
964 (let ((options (org-export-backend-options backend))
965 parent)
966 (while (setq parent (org-export-backend-parent backend))
967 (setq backend (org-export-get-backend parent))
968 (setq options (append options (org-export-backend-options backend))))
969 options)))
971 (defun org-export-get-all-filters (backend)
972 "Return complete list of filters for BACKEND.
974 BACKEND is an export back-end, as return by, e.g,,
975 `org-export-create-backend'. Return value is an alist where
976 keys are symbols and values lists of functions.
978 Unlike to `org-export-backend-filters', this function also
979 returns filters inherited from parent back-ends, if any."
980 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
981 (when backend
982 (let ((filters (org-export-backend-filters backend))
983 parent)
984 (while (setq parent (org-export-backend-parent backend))
985 (setq backend (org-export-get-backend parent))
986 (setq filters (append filters (org-export-backend-filters backend))))
987 filters)))
989 (defun org-export-define-backend (backend transcoders &rest body)
990 "Define a new back-end BACKEND.
992 TRANSCODERS is an alist between object or element types and
993 functions handling them.
995 These functions should return a string without any trailing
996 space, or nil. They must accept three arguments: the object or
997 element itself, its contents or nil when it isn't recursive and
998 the property list used as a communication channel.
1000 Contents, when not nil, are stripped from any global indentation
1001 \(although the relative one is preserved). They also always end
1002 with a single newline character.
1004 If, for a given type, no function is found, that element or
1005 object type will simply be ignored, along with any blank line or
1006 white space at its end. The same will happen if the function
1007 returns the nil value. If that function returns the empty
1008 string, the type will be ignored, but the blank lines or white
1009 spaces will be kept.
1011 In addition to element and object types, one function can be
1012 associated to the `template' (or `inner-template') symbol and
1013 another one to the `plain-text' symbol.
1015 The former returns the final transcoded string, and can be used
1016 to add a preamble and a postamble to document's body. It must
1017 accept two arguments: the transcoded string and the property list
1018 containing export options. A function associated to `template'
1019 will not be applied if export has option \"body-only\".
1020 A function associated to `inner-template' is always applied.
1022 The latter, when defined, is to be called on every text not
1023 recognized as an element or an object. It must accept two
1024 arguments: the text string and the information channel. It is an
1025 appropriate place to protect special chars relative to the
1026 back-end.
1028 BODY can start with pre-defined keyword arguments. The following
1029 keywords are understood:
1031 :export-block
1033 String, or list of strings, representing block names that
1034 will not be parsed. This is used to specify blocks that will
1035 contain raw code specific to the back-end. These blocks
1036 still have to be handled by the relative `export-block' type
1037 translator.
1039 :filters-alist
1041 Alist between filters and function, or list of functions,
1042 specific to the back-end. See `org-export-filters-alist' for
1043 a list of all allowed filters. Filters defined here
1044 shouldn't make a back-end test, as it may prevent back-ends
1045 derived from this one to behave properly.
1047 :menu-entry
1049 Menu entry for the export dispatcher. It should be a list
1050 like:
1052 '(KEY DESCRIPTION-OR-ORDINAL ACTION-OR-MENU)
1054 where :
1056 KEY is a free character selecting the back-end.
1058 DESCRIPTION-OR-ORDINAL is either a string or a number.
1060 If it is a string, is will be used to name the back-end in
1061 its menu entry. If it is a number, the following menu will
1062 be displayed as a sub-menu of the back-end with the same
1063 KEY. Also, the number will be used to determine in which
1064 order such sub-menus will appear (lowest first).
1066 ACTION-OR-MENU is either a function or an alist.
1068 If it is an action, it will be called with four
1069 arguments (booleans): ASYNC, SUBTREEP, VISIBLE-ONLY and
1070 BODY-ONLY. See `org-export-as' for further explanations on
1071 some of them.
1073 If it is an alist, associations should follow the
1074 pattern:
1076 '(KEY DESCRIPTION ACTION)
1078 where KEY, DESCRIPTION and ACTION are described above.
1080 Valid values include:
1082 '(?m \"My Special Back-end\" my-special-export-function)
1086 '(?l \"Export to LaTeX\"
1087 \(?p \"As PDF file\" org-latex-export-to-pdf)
1088 \(?o \"As PDF file and open\"
1089 \(lambda (a s v b)
1090 \(if a (org-latex-export-to-pdf t s v b)
1091 \(org-open-file
1092 \(org-latex-export-to-pdf nil s v b)))))))
1094 or the following, which will be added to the previous
1095 sub-menu,
1097 '(?l 1
1098 \((?B \"As TEX buffer (Beamer)\" org-beamer-export-as-latex)
1099 \(?P \"As PDF file (Beamer)\" org-beamer-export-to-pdf)))
1101 :options-alist
1103 Alist between back-end specific properties introduced in
1104 communication channel and how their value are acquired. See
1105 `org-export-options-alist' for more information about
1106 structure of the values."
1107 (declare (indent 1))
1108 (let (blocks filters menu-entry options contents)
1109 (while (keywordp (car body))
1110 (case (pop body)
1111 (:export-block (let ((names (pop body)))
1112 (setq blocks (if (consp names) (mapcar 'upcase names)
1113 (list (upcase names))))))
1114 (:filters-alist (setq filters (pop body)))
1115 (:menu-entry (setq menu-entry (pop body)))
1116 (:options-alist (setq options (pop body)))
1117 (t (pop body))))
1118 (org-export-register-backend
1119 (org-export-create-backend :name backend
1120 :transcoders transcoders
1121 :options options
1122 :filters filters
1123 :blocks blocks
1124 :menu menu-entry))))
1126 (defun org-export-define-derived-backend (child parent &rest body)
1127 "Create a new back-end as a variant of an existing one.
1129 CHILD is the name of the derived back-end. PARENT is the name of
1130 the parent back-end.
1132 BODY can start with pre-defined keyword arguments. The following
1133 keywords are understood:
1135 :export-block
1137 String, or list of strings, representing block names that
1138 will not be parsed. This is used to specify blocks that will
1139 contain raw code specific to the back-end. These blocks
1140 still have to be handled by the relative `export-block' type
1141 translator.
1143 :filters-alist
1145 Alist of filters that will overwrite or complete filters
1146 defined in PARENT back-end. See `org-export-filters-alist'
1147 for a list of allowed filters.
1149 :menu-entry
1151 Menu entry for the export dispatcher. See
1152 `org-export-define-backend' for more information about the
1153 expected value.
1155 :options-alist
1157 Alist of back-end specific properties that will overwrite or
1158 complete those defined in PARENT back-end. Refer to
1159 `org-export-options-alist' for more information about
1160 structure of the values.
1162 :translate-alist
1164 Alist of element and object types and transcoders that will
1165 overwrite or complete transcode table from PARENT back-end.
1166 Refer to `org-export-define-backend' for detailed information
1167 about transcoders.
1169 As an example, here is how one could define \"my-latex\" back-end
1170 as a variant of `latex' back-end with a custom template function:
1172 \(org-export-define-derived-backend 'my-latex 'latex
1173 :translate-alist '((template . my-latex-template-fun)))
1175 The back-end could then be called with, for example:
1177 \(org-export-to-buffer 'my-latex \"*Test my-latex*\")"
1178 (declare (indent 2))
1179 (let (blocks filters menu-entry options transcoders contents)
1180 (while (keywordp (car body))
1181 (case (pop body)
1182 (:export-block (let ((names (pop body)))
1183 (setq blocks (if (consp names) (mapcar 'upcase names)
1184 (list (upcase names))))))
1185 (:filters-alist (setq filters (pop body)))
1186 (:menu-entry (setq menu-entry (pop body)))
1187 (:options-alist (setq options (pop body)))
1188 (:translate-alist (setq transcoders (pop body)))
1189 (t (pop body))))
1190 (org-export-register-backend
1191 (org-export-create-backend :name child
1192 :parent parent
1193 :transcoders transcoders
1194 :options options
1195 :filters filters
1196 :blocks blocks
1197 :menu menu-entry))))
1201 ;;; The Communication Channel
1203 ;; During export process, every function has access to a number of
1204 ;; properties. They are of two types:
1206 ;; 1. Environment options are collected once at the very beginning of
1207 ;; the process, out of the original buffer and configuration.
1208 ;; Collecting them is handled by `org-export-get-environment'
1209 ;; function.
1211 ;; Most environment options are defined through the
1212 ;; `org-export-options-alist' variable.
1214 ;; 2. Tree properties are extracted directly from the parsed tree,
1215 ;; just before export, by `org-export-collect-tree-properties'.
1217 ;; Here is the full list of properties available during transcode
1218 ;; process, with their category and their value type.
1220 ;; + `:author' :: Author's name.
1221 ;; - category :: option
1222 ;; - type :: string
1224 ;; + `:back-end' :: Current back-end used for transcoding.
1225 ;; - category :: tree
1226 ;; - type :: symbol
1228 ;; + `:creator' :: String to write as creation information.
1229 ;; - category :: option
1230 ;; - type :: string
1232 ;; + `:date' :: String to use as date.
1233 ;; - category :: option
1234 ;; - type :: string
1236 ;; + `:description' :: Description text for the current data.
1237 ;; - category :: option
1238 ;; - type :: string
1240 ;; + `:email' :: Author's email.
1241 ;; - category :: option
1242 ;; - type :: string
1244 ;; + `:exclude-tags' :: Tags for exclusion of subtrees from export
1245 ;; process.
1246 ;; - category :: option
1247 ;; - type :: list of strings
1249 ;; + `:export-options' :: List of export options available for current
1250 ;; process.
1251 ;; - category :: none
1252 ;; - type :: list of symbols, among `subtree', `body-only' and
1253 ;; `visible-only'.
1255 ;; + `:exported-data' :: Hash table used for memoizing
1256 ;; `org-export-data'.
1257 ;; - category :: tree
1258 ;; - type :: hash table
1260 ;; + `:filetags' :: List of global tags for buffer. Used by
1261 ;; `org-export-get-tags' to get tags with inheritance.
1262 ;; - category :: option
1263 ;; - type :: list of strings
1265 ;; + `:footnote-definition-alist' :: Alist between footnote labels and
1266 ;; their definition, as parsed data. Only non-inlined footnotes
1267 ;; are represented in this alist. Also, every definition isn't
1268 ;; guaranteed to be referenced in the parse tree. The purpose of
1269 ;; this property is to preserve definitions from oblivion
1270 ;; (i.e. when the parse tree comes from a part of the original
1271 ;; buffer), it isn't meant for direct use in a back-end. To
1272 ;; retrieve a definition relative to a reference, use
1273 ;; `org-export-get-footnote-definition' instead.
1274 ;; - category :: option
1275 ;; - type :: alist (STRING . LIST)
1277 ;; + `:headline-levels' :: Maximum level being exported as an
1278 ;; headline. Comparison is done with the relative level of
1279 ;; headlines in the parse tree, not necessarily with their
1280 ;; actual level.
1281 ;; - category :: option
1282 ;; - type :: integer
1284 ;; + `:headline-offset' :: Difference between relative and real level
1285 ;; of headlines in the parse tree. For example, a value of -1
1286 ;; means a level 2 headline should be considered as level
1287 ;; 1 (cf. `org-export-get-relative-level').
1288 ;; - category :: tree
1289 ;; - type :: integer
1291 ;; + `:headline-numbering' :: Alist between headlines and their
1292 ;; numbering, as a list of numbers
1293 ;; (cf. `org-export-get-headline-number').
1294 ;; - category :: tree
1295 ;; - type :: alist (INTEGER . LIST)
1297 ;; + `:id-alist' :: Alist between ID strings and destination file's
1298 ;; path, relative to current directory. It is used by
1299 ;; `org-export-resolve-id-link' to resolve ID links targeting an
1300 ;; external file.
1301 ;; - category :: option
1302 ;; - type :: alist (STRING . STRING)
1304 ;; + `:ignore-list' :: List of elements and objects that should be
1305 ;; ignored during export.
1306 ;; - category :: tree
1307 ;; - type :: list of elements and objects
1309 ;; + `:input-file' :: Full path to input file, if any.
1310 ;; - category :: option
1311 ;; - type :: string or nil
1313 ;; + `:keywords' :: List of keywords attached to data.
1314 ;; - category :: option
1315 ;; - type :: string
1317 ;; + `:language' :: Default language used for translations.
1318 ;; - category :: option
1319 ;; - type :: string
1321 ;; + `:parse-tree' :: Whole parse tree, available at any time during
1322 ;; transcoding.
1323 ;; - category :: option
1324 ;; - type :: list (as returned by `org-element-parse-buffer')
1326 ;; + `:preserve-breaks' :: Non-nil means transcoding should preserve
1327 ;; all line breaks.
1328 ;; - category :: option
1329 ;; - type :: symbol (nil, t)
1331 ;; + `:section-numbers' :: Non-nil means transcoding should add
1332 ;; section numbers to headlines.
1333 ;; - category :: option
1334 ;; - type :: symbol (nil, t)
1336 ;; + `:select-tags' :: List of tags enforcing inclusion of sub-trees
1337 ;; in transcoding. When such a tag is present, subtrees without
1338 ;; it are de facto excluded from the process. See
1339 ;; `use-select-tags'.
1340 ;; - category :: option
1341 ;; - type :: list of strings
1343 ;; + `:time-stamp-file' :: Non-nil means transcoding should insert
1344 ;; a time stamp in the output.
1345 ;; - category :: option
1346 ;; - type :: symbol (nil, t)
1348 ;; + `:translate-alist' :: Alist between element and object types and
1349 ;; transcoding functions relative to the current back-end.
1350 ;; Special keys `inner-template', `template' and `plain-text' are
1351 ;; also possible.
1352 ;; - category :: option
1353 ;; - type :: alist (SYMBOL . FUNCTION)
1355 ;; + `:with-archived-trees' :: Non-nil when archived subtrees should
1356 ;; also be transcoded. If it is set to the `headline' symbol,
1357 ;; only the archived headline's name is retained.
1358 ;; - category :: option
1359 ;; - type :: symbol (nil, t, `headline')
1361 ;; + `:with-author' :: Non-nil means author's name should be included
1362 ;; in the output.
1363 ;; - category :: option
1364 ;; - type :: symbol (nil, t)
1366 ;; + `:with-clocks' :: Non-nil means clock keywords should be exported.
1367 ;; - category :: option
1368 ;; - type :: symbol (nil, t)
1370 ;; + `:with-creator' :: Non-nil means a creation sentence should be
1371 ;; inserted at the end of the transcoded string. If the value
1372 ;; is `comment', it should be commented.
1373 ;; - category :: option
1374 ;; - type :: symbol (`comment', nil, t)
1376 ;; + `:with-date' :: Non-nil means output should contain a date.
1377 ;; - category :: option
1378 ;; - type :. symbol (nil, t)
1380 ;; + `:with-drawers' :: Non-nil means drawers should be exported. If
1381 ;; its value is a list of names, only drawers with such names
1382 ;; will be transcoded. If that list starts with `not', drawer
1383 ;; with these names will be skipped.
1384 ;; - category :: option
1385 ;; - type :: symbol (nil, t) or list of strings
1387 ;; + `:with-email' :: Non-nil means output should contain author's
1388 ;; email.
1389 ;; - category :: option
1390 ;; - type :: symbol (nil, t)
1392 ;; + `:with-emphasize' :: Non-nil means emphasized text should be
1393 ;; interpreted.
1394 ;; - category :: option
1395 ;; - type :: symbol (nil, t)
1397 ;; + `:with-fixed-width' :: Non-nil if transcoder should interpret
1398 ;; strings starting with a colon as a fixed-with (verbatim) area.
1399 ;; - category :: option
1400 ;; - type :: symbol (nil, t)
1402 ;; + `:with-footnotes' :: Non-nil if transcoder should interpret
1403 ;; footnotes.
1404 ;; - category :: option
1405 ;; - type :: symbol (nil, t)
1407 ;; + `:with-latex' :: Non-nil means `latex-environment' elements and
1408 ;; `latex-fragment' objects should appear in export output. When
1409 ;; this property is set to `verbatim', they will be left as-is.
1410 ;; - category :: option
1411 ;; - type :: symbol (`verbatim', nil, t)
1413 ;; + `:with-planning' :: Non-nil means transcoding should include
1414 ;; planning info.
1415 ;; - category :: option
1416 ;; - type :: symbol (nil, t)
1418 ;; + `:with-priority' :: Non-nil means transcoding should include
1419 ;; priority cookies.
1420 ;; - category :: option
1421 ;; - type :: symbol (nil, t)
1423 ;; + `:with-smart-quotes' :: Non-nil means activate smart quotes in
1424 ;; plain text.
1425 ;; - category :: option
1426 ;; - type :: symbol (nil, t)
1428 ;; + `:with-special-strings' :: Non-nil means transcoding should
1429 ;; interpret special strings in plain text.
1430 ;; - category :: option
1431 ;; - type :: symbol (nil, t)
1433 ;; + `:with-sub-superscript' :: Non-nil means transcoding should
1434 ;; interpret subscript and superscript. With a value of "{}",
1435 ;; only interpret those using curly brackets.
1436 ;; - category :: option
1437 ;; - type :: symbol (nil, {}, t)
1439 ;; + `:with-tables' :: Non-nil means transcoding should interpret
1440 ;; tables.
1441 ;; - category :: option
1442 ;; - type :: symbol (nil, t)
1444 ;; + `:with-tags' :: Non-nil means transcoding should keep tags in
1445 ;; headlines. A `not-in-toc' value will remove them from the
1446 ;; table of contents, if any, nonetheless.
1447 ;; - category :: option
1448 ;; - type :: symbol (nil, t, `not-in-toc')
1450 ;; + `:with-tasks' :: Non-nil means transcoding should include
1451 ;; headlines with a TODO keyword. A `todo' value will only
1452 ;; include headlines with a todo type keyword while a `done'
1453 ;; value will do the contrary. If a list of strings is provided,
1454 ;; only tasks with keywords belonging to that list will be kept.
1455 ;; - category :: option
1456 ;; - type :: symbol (t, todo, done, nil) or list of strings
1458 ;; + `:with-timestamps' :: Non-nil means transcoding should include
1459 ;; time stamps. Special value `active' (resp. `inactive') ask to
1460 ;; export only active (resp. inactive) timestamps. Otherwise,
1461 ;; completely remove them.
1462 ;; - category :: option
1463 ;; - type :: symbol: (`active', `inactive', t, nil)
1465 ;; + `:with-toc' :: Non-nil means that a table of contents has to be
1466 ;; added to the output. An integer value limits its depth.
1467 ;; - category :: option
1468 ;; - type :: symbol (nil, t or integer)
1470 ;; + `:with-todo-keywords' :: Non-nil means transcoding should
1471 ;; include TODO keywords.
1472 ;; - category :: option
1473 ;; - type :: symbol (nil, t)
1476 ;;;; Environment Options
1478 ;; Environment options encompass all parameters defined outside the
1479 ;; scope of the parsed data. They come from five sources, in
1480 ;; increasing precedence order:
1482 ;; - Global variables,
1483 ;; - Buffer's attributes,
1484 ;; - Options keyword symbols,
1485 ;; - Buffer keywords,
1486 ;; - Subtree properties.
1488 ;; The central internal function with regards to environment options
1489 ;; is `org-export-get-environment'. It updates global variables with
1490 ;; "#+BIND:" keywords, then retrieve and prioritize properties from
1491 ;; the different sources.
1493 ;; The internal functions doing the retrieval are:
1494 ;; `org-export--get-global-options',
1495 ;; `org-export--get-buffer-attributes',
1496 ;; `org-export--parse-option-keyword',
1497 ;; `org-export--get-subtree-options' and
1498 ;; `org-export--get-inbuffer-options'
1500 ;; Also, `org-export--list-bound-variables' collects bound variables
1501 ;; along with their value in order to set them as buffer local
1502 ;; variables later in the process.
1504 (defun org-export-get-environment (&optional backend subtreep ext-plist)
1505 "Collect export options from the current buffer.
1507 Optional argument BACKEND is an export back-end, as returned by
1508 `org-export-create-backend'.
1510 When optional argument SUBTREEP is non-nil, assume the export is
1511 done against the current sub-tree.
1513 Third optional argument EXT-PLIST is a property list with
1514 external parameters overriding Org default settings, but still
1515 inferior to file-local settings."
1516 ;; First install #+BIND variables since these must be set before
1517 ;; global options are read.
1518 (dolist (pair (org-export--list-bound-variables))
1519 (org-set-local (car pair) (nth 1 pair)))
1520 ;; Get and prioritize export options...
1521 (org-combine-plists
1522 ;; ... from global variables...
1523 (org-export--get-global-options backend)
1524 ;; ... from an external property list...
1525 ext-plist
1526 ;; ... from in-buffer settings...
1527 (org-export--get-inbuffer-options backend)
1528 ;; ... and from subtree, when appropriate.
1529 (and subtreep (org-export--get-subtree-options backend))
1530 ;; Eventually add misc. properties.
1531 (list
1532 :back-end
1533 backend
1534 :translate-alist (org-export-get-all-transcoders backend)
1535 :footnote-definition-alist
1536 ;; Footnotes definitions must be collected in the original
1537 ;; buffer, as there's no insurance that they will still be in
1538 ;; the parse tree, due to possible narrowing.
1539 (let (alist)
1540 (org-with-wide-buffer
1541 (goto-char (point-min))
1542 (while (re-search-forward org-footnote-definition-re nil t)
1543 (let ((def (save-match-data (org-element-at-point))))
1544 (when (eq (org-element-type def) 'footnote-definition)
1545 (push
1546 (cons (org-element-property :label def)
1547 (let ((cbeg (org-element-property :contents-begin def)))
1548 (when cbeg
1549 (org-element--parse-elements
1550 cbeg (org-element-property :contents-end def)
1551 nil nil nil nil (list 'org-data nil)))))
1552 alist))))
1553 alist))
1554 :id-alist
1555 ;; Collect id references.
1556 (let (alist)
1557 (org-with-wide-buffer
1558 (goto-char (point-min))
1559 (while (re-search-forward "\\[\\[id:\\S-+?\\]" nil t)
1560 (let ((link (org-element-context)))
1561 (when (eq (org-element-type link) 'link)
1562 (let* ((id (org-element-property :path link))
1563 (file (org-id-find-id-file id)))
1564 (when file
1565 (push (cons id (file-relative-name file)) alist)))))))
1566 alist))))
1568 (defun org-export--parse-option-keyword (options &optional backend)
1569 "Parse an OPTIONS line and return values as a plist.
1570 Optional argument BACKEND is an export back-end, as returned by,
1571 e.g., `org-export-create-backend'. It specifies which back-end
1572 specific items to read, if any."
1573 (let* ((all
1574 ;; Priority is given to back-end specific options.
1575 (append (and backend (org-export-get-all-options backend))
1576 org-export-options-alist))
1577 plist)
1578 (dolist (option all)
1579 (let ((property (car option))
1580 (item (nth 2 option)))
1581 (when (and item
1582 (not (plist-member plist property))
1583 (string-match (concat "\\(\\`\\|[ \t]\\)"
1584 (regexp-quote item)
1585 ":\\(([^)\n]+)\\|[^ \t\n\r;,.]*\\)")
1586 options))
1587 (setq plist (plist-put plist
1588 property
1589 (car (read-from-string
1590 (match-string 2 options))))))))
1591 plist))
1593 (defun org-export--get-subtree-options (&optional backend)
1594 "Get export options in subtree at point.
1595 Optional argument BACKEND is an export back-end, as returned by,
1596 e.g., `org-export-create-backend'. It specifies back-end used
1597 for export. Return options as a plist."
1598 ;; For each buffer keyword, create a headline property setting the
1599 ;; same property in communication channel. The name for the property
1600 ;; is the keyword with "EXPORT_" appended to it.
1601 (org-with-wide-buffer
1602 (let (prop plist)
1603 ;; Make sure point is at a heading.
1604 (if (org-at-heading-p) (org-up-heading-safe) (org-back-to-heading t))
1605 ;; Take care of EXPORT_TITLE. If it isn't defined, use headline's
1606 ;; title as its fallback value.
1607 (when (setq prop (or (org-entry-get (point) "EXPORT_TITLE")
1608 (progn (looking-at org-todo-line-regexp)
1609 (org-match-string-no-properties 3))))
1610 (setq plist
1611 (plist-put
1612 plist :title
1613 (org-element-parse-secondary-string
1614 prop (org-element-restriction 'keyword)))))
1615 ;; EXPORT_OPTIONS are parsed in a non-standard way.
1616 (when (setq prop (org-entry-get (point) "EXPORT_OPTIONS"))
1617 (setq plist
1618 (nconc plist (org-export--parse-option-keyword prop backend))))
1619 ;; Handle other keywords. TITLE keyword is excluded as it has
1620 ;; been handled already.
1621 (let ((seen '("TITLE")))
1622 (mapc
1623 (lambda (option)
1624 (let ((property (car option))
1625 (keyword (nth 1 option)))
1626 (when (and keyword (not (member keyword seen)))
1627 (let* ((subtree-prop (concat "EXPORT_" keyword))
1628 ;; Export properties are not case-sensitive.
1629 (value (let ((case-fold-search t))
1630 (org-entry-get (point) subtree-prop))))
1631 (push keyword seen)
1632 (when (and value (not (plist-member plist property)))
1633 (setq plist
1634 (plist-put
1635 plist
1636 property
1637 (cond
1638 ;; Parse VALUE if required.
1639 ((member keyword org-element-document-properties)
1640 (org-element-parse-secondary-string
1641 value (org-element-restriction 'keyword)))
1642 ;; If BEHAVIOUR is `split' expected value is
1643 ;; a list of strings, not a string.
1644 ((eq (nth 4 option) 'split) (org-split-string value))
1645 (t value)))))))))
1646 ;; Look for both general keywords and back-end specific
1647 ;; options, with priority given to the latter.
1648 (append (and backend (org-export-get-all-options backend))
1649 org-export-options-alist)))
1650 ;; Return value.
1651 plist)))
1653 (defun org-export--get-inbuffer-options (&optional backend)
1654 "Return current buffer export options, as a plist.
1656 Optional argument BACKEND, when non-nil, is an export back-end,
1657 as returned by, e.g., `org-export-create-backend'. It specifies
1658 which back-end specific options should also be read in the
1659 process.
1661 Assume buffer is in Org mode. Narrowing, if any, is ignored."
1662 (let* (plist
1663 get-options ; For byte-compiler.
1664 (case-fold-search t)
1665 (options (append
1666 ;; Priority is given to back-end specific options.
1667 (and backend (org-export-get-all-options backend))
1668 org-export-options-alist))
1669 (regexp (format "^[ \t]*#\\+%s:"
1670 (regexp-opt (nconc (delq nil (mapcar 'cadr options))
1671 org-export-special-keywords))))
1672 (find-properties
1673 (lambda (keyword)
1674 ;; Return all properties associated to KEYWORD.
1675 (let (properties)
1676 (dolist (option options properties)
1677 (when (equal (nth 1 option) keyword)
1678 (pushnew (car option) properties))))))
1679 (get-options
1680 (lambda (&optional files plist)
1681 ;; Recursively read keywords in buffer. FILES is a list
1682 ;; of files read so far. PLIST is the current property
1683 ;; list obtained.
1684 (org-with-wide-buffer
1685 (goto-char (point-min))
1686 (while (re-search-forward regexp nil t)
1687 (let ((element (org-element-at-point)))
1688 (when (eq (org-element-type element) 'keyword)
1689 (let ((key (org-element-property :key element))
1690 (val (org-element-property :value element)))
1691 (cond
1692 ;; Options in `org-export-special-keywords'.
1693 ((equal key "SETUPFILE")
1694 (let ((file (expand-file-name
1695 (org-remove-double-quotes (org-trim val)))))
1696 ;; Avoid circular dependencies.
1697 (unless (member file files)
1698 (with-temp-buffer
1699 (insert (org-file-contents file 'noerror))
1700 (let ((org-inhibit-startup t)) (org-mode))
1701 (setq plist (funcall get-options
1702 (cons file files) plist))))))
1703 ((equal key "OPTIONS")
1704 (setq plist
1705 (org-combine-plists
1706 plist
1707 (org-export--parse-option-keyword val backend))))
1708 ((equal key "FILETAGS")
1709 (setq plist
1710 (org-combine-plists
1711 plist
1712 (list :filetags
1713 (org-uniquify
1714 (append (org-split-string val ":")
1715 (plist-get plist :filetags)))))))
1717 ;; Options in `org-export-options-alist'.
1718 (dolist (property (funcall find-properties key))
1719 (let ((behaviour (nth 4 (assq property options))))
1720 (setq plist
1721 (plist-put
1722 plist property
1723 ;; Handle value depending on specified
1724 ;; BEHAVIOUR.
1725 (case behaviour
1726 (space
1727 (if (not (plist-get plist property))
1728 (org-trim val)
1729 (concat (plist-get plist property)
1731 (org-trim val))))
1732 (newline
1733 (org-trim
1734 (concat (plist-get plist property)
1735 "\n"
1736 (org-trim val))))
1737 (split `(,@(plist-get plist property)
1738 ,@(org-split-string val)))
1739 ('t val)
1740 (otherwise
1741 (if (not (plist-member plist property)) val
1742 (plist-get plist property))))))))))))))
1743 ;; Return final value.
1744 plist))))
1745 ;; Read options in the current buffer.
1746 (setq plist (funcall get-options
1747 (and buffer-file-name (list buffer-file-name)) nil))
1748 ;; Parse keywords specified in `org-element-document-properties'
1749 ;; and return PLIST.
1750 (dolist (keyword org-element-document-properties plist)
1751 (dolist (property (funcall find-properties keyword))
1752 (let ((value (plist-get plist property)))
1753 (when (stringp value)
1754 (setq plist
1755 (plist-put plist property
1756 (org-element-parse-secondary-string
1757 value (org-element-restriction 'keyword))))))))))
1759 (defun org-export--get-buffer-attributes ()
1760 "Return properties related to buffer attributes, as a plist."
1761 ;; Store full path of input file name, or nil. For internal use.
1762 (let ((visited-file (buffer-file-name (buffer-base-buffer))))
1763 (list :input-file visited-file
1764 :title (if (not visited-file) (buffer-name (buffer-base-buffer))
1765 (file-name-sans-extension
1766 (file-name-nondirectory visited-file))))))
1768 (defun org-export--get-global-options (&optional backend)
1769 "Return global export options as a plist.
1770 Optional argument BACKEND, if non-nil, is an export back-end, as
1771 returned by, e.g., `org-export-create-backend'. It specifies
1772 which back-end specific export options should also be read in the
1773 process."
1774 (let (plist
1775 ;; Priority is given to back-end specific options.
1776 (all (append (and backend (org-export-get-all-options backend))
1777 org-export-options-alist)))
1778 (dolist (cell all plist)
1779 (let ((prop (car cell))
1780 (default-value (nth 3 cell)))
1781 (unless (or (not default-value) (plist-member plist prop))
1782 (setq plist
1783 (plist-put
1784 plist
1785 prop
1786 ;; Eval default value provided. If keyword is
1787 ;; a member of `org-element-document-properties',
1788 ;; parse it as a secondary string before storing it.
1789 (let ((value (eval (nth 3 cell))))
1790 (if (not (stringp value)) value
1791 (let ((keyword (nth 1 cell)))
1792 (if (member keyword org-element-document-properties)
1793 (org-element-parse-secondary-string
1794 value (org-element-restriction 'keyword))
1795 value)))))))))))
1797 (defun org-export--list-bound-variables ()
1798 "Return variables bound from BIND keywords in current buffer.
1799 Also look for BIND keywords in setup files. The return value is
1800 an alist where associations are (VARIABLE-NAME VALUE)."
1801 (when org-export-allow-bind-keywords
1802 (let* (collect-bind ; For byte-compiler.
1803 (collect-bind
1804 (lambda (files alist)
1805 ;; Return an alist between variable names and their
1806 ;; value. FILES is a list of setup files names read so
1807 ;; far, used to avoid circular dependencies. ALIST is
1808 ;; the alist collected so far.
1809 (let ((case-fold-search t))
1810 (org-with-wide-buffer
1811 (goto-char (point-min))
1812 (while (re-search-forward
1813 "^[ \t]*#\\+\\(BIND\\|SETUPFILE\\):" nil t)
1814 (let ((element (org-element-at-point)))
1815 (when (eq (org-element-type element) 'keyword)
1816 (let ((val (org-element-property :value element)))
1817 (if (equal (org-element-property :key element) "BIND")
1818 (push (read (format "(%s)" val)) alist)
1819 ;; Enter setup file.
1820 (let ((file (expand-file-name
1821 (org-remove-double-quotes val))))
1822 (unless (member file files)
1823 (with-temp-buffer
1824 (let ((org-inhibit-startup t)) (org-mode))
1825 (insert (org-file-contents file 'noerror))
1826 (setq alist
1827 (funcall collect-bind
1828 (cons file files)
1829 alist))))))))))
1830 alist)))))
1831 ;; Return value in appropriate order of appearance.
1832 (nreverse (funcall collect-bind nil nil)))))
1835 ;;;; Tree Properties
1837 ;; Tree properties are information extracted from parse tree. They
1838 ;; are initialized at the beginning of the transcoding process by
1839 ;; `org-export-collect-tree-properties'.
1841 ;; Dedicated functions focus on computing the value of specific tree
1842 ;; properties during initialization. Thus,
1843 ;; `org-export--populate-ignore-list' lists elements and objects that
1844 ;; should be skipped during export, `org-export--get-min-level' gets
1845 ;; the minimal exportable level, used as a basis to compute relative
1846 ;; level for headlines. Eventually
1847 ;; `org-export--collect-headline-numbering' builds an alist between
1848 ;; headlines and their numbering.
1850 (defun org-export-collect-tree-properties (data info)
1851 "Extract tree properties from parse tree.
1853 DATA is the parse tree from which information is retrieved. INFO
1854 is a list holding export options.
1856 Following tree properties are set or updated:
1858 `:exported-data' Hash table used to memoize results from
1859 `org-export-data'.
1861 `:footnote-definition-alist' List of footnotes definitions in
1862 original buffer and current parse tree.
1864 `:headline-offset' Offset between true level of headlines and
1865 local level. An offset of -1 means a headline
1866 of level 2 should be considered as a level
1867 1 headline in the context.
1869 `:headline-numbering' Alist of all headlines as key an the
1870 associated numbering as value.
1872 `:ignore-list' List of elements that should be ignored during
1873 export.
1875 Return updated plist."
1876 ;; Install the parse tree in the communication channel, in order to
1877 ;; use `org-export-get-genealogy' and al.
1878 (setq info (plist-put info :parse-tree data))
1879 ;; Get the list of elements and objects to ignore, and put it into
1880 ;; `:ignore-list'. Do not overwrite any user ignore that might have
1881 ;; been done during parse tree filtering.
1882 (setq info
1883 (plist-put info
1884 :ignore-list
1885 (append (org-export--populate-ignore-list data info)
1886 (plist-get info :ignore-list))))
1887 ;; Compute `:headline-offset' in order to be able to use
1888 ;; `org-export-get-relative-level'.
1889 (setq info
1890 (plist-put info
1891 :headline-offset
1892 (- 1 (org-export--get-min-level data info))))
1893 ;; Update footnotes definitions list with definitions in parse tree.
1894 ;; This is required since buffer expansion might have modified
1895 ;; boundaries of footnote definitions contained in the parse tree.
1896 ;; This way, definitions in `footnote-definition-alist' are bound to
1897 ;; match those in the parse tree.
1898 (let ((defs (plist-get info :footnote-definition-alist)))
1899 (org-element-map data 'footnote-definition
1900 (lambda (fn)
1901 (push (cons (org-element-property :label fn)
1902 `(org-data nil ,@(org-element-contents fn)))
1903 defs)))
1904 (setq info (plist-put info :footnote-definition-alist defs)))
1905 ;; Properties order doesn't matter: get the rest of the tree
1906 ;; properties.
1907 (nconc
1908 `(:headline-numbering ,(org-export--collect-headline-numbering data info)
1909 :exported-data ,(make-hash-table :test 'eq :size 4001))
1910 info))
1912 (defun org-export--get-min-level (data options)
1913 "Return minimum exportable headline's level in DATA.
1914 DATA is parsed tree as returned by `org-element-parse-buffer'.
1915 OPTIONS is a plist holding export options."
1916 (catch 'exit
1917 (let ((min-level 10000))
1918 (mapc
1919 (lambda (blob)
1920 (when (and (eq (org-element-type blob) 'headline)
1921 (not (org-element-property :footnote-section-p blob))
1922 (not (memq blob (plist-get options :ignore-list))))
1923 (setq min-level (min (org-element-property :level blob) min-level)))
1924 (when (= min-level 1) (throw 'exit 1)))
1925 (org-element-contents data))
1926 ;; If no headline was found, for the sake of consistency, set
1927 ;; minimum level to 1 nonetheless.
1928 (if (= min-level 10000) 1 min-level))))
1930 (defun org-export--collect-headline-numbering (data options)
1931 "Return numbering of all exportable headlines in a parse tree.
1933 DATA is the parse tree. OPTIONS is the plist holding export
1934 options.
1936 Return an alist whose key is a headline and value is its
1937 associated numbering \(in the shape of a list of numbers\) or nil
1938 for a footnotes section."
1939 (let ((numbering (make-vector org-export-max-depth 0)))
1940 (org-element-map data 'headline
1941 (lambda (headline)
1942 (unless (org-element-property :footnote-section-p headline)
1943 (let ((relative-level
1944 (1- (org-export-get-relative-level headline options))))
1945 (cons
1946 headline
1947 (loop for n across numbering
1948 for idx from 0 to org-export-max-depth
1949 when (< idx relative-level) collect n
1950 when (= idx relative-level) collect (aset numbering idx (1+ n))
1951 when (> idx relative-level) do (aset numbering idx 0))))))
1952 options)))
1954 (defun org-export--populate-ignore-list (data options)
1955 "Return list of elements and objects to ignore during export.
1956 DATA is the parse tree to traverse. OPTIONS is the plist holding
1957 export options."
1958 (let* (ignore
1959 walk-data
1960 ;; First find trees containing a select tag, if any.
1961 (selected (org-export--selected-trees data options))
1962 (walk-data
1963 (lambda (data)
1964 ;; Collect ignored elements or objects into IGNORE-LIST.
1965 (let ((type (org-element-type data)))
1966 (if (org-export--skip-p data options selected) (push data ignore)
1967 (if (and (eq type 'headline)
1968 (eq (plist-get options :with-archived-trees) 'headline)
1969 (org-element-property :archivedp data))
1970 ;; If headline is archived but tree below has
1971 ;; to be skipped, add it to ignore list.
1972 (mapc (lambda (e) (push e ignore))
1973 (org-element-contents data))
1974 ;; Move into secondary string, if any.
1975 (let ((sec-prop
1976 (cdr (assq type org-element-secondary-value-alist))))
1977 (when sec-prop
1978 (mapc walk-data (org-element-property sec-prop data))))
1979 ;; Move into recursive objects/elements.
1980 (mapc walk-data (org-element-contents data))))))))
1981 ;; Main call.
1982 (funcall walk-data data)
1983 ;; Return value.
1984 ignore))
1986 (defun org-export--selected-trees (data info)
1987 "Return list of headlines and inlinetasks with a select tag in their tree.
1988 DATA is parsed data as returned by `org-element-parse-buffer'.
1989 INFO is a plist holding export options."
1990 (let* (selected-trees
1991 walk-data ; For byte-compiler.
1992 (walk-data
1993 (function
1994 (lambda (data genealogy)
1995 (let ((type (org-element-type data)))
1996 (cond
1997 ((memq type '(headline inlinetask))
1998 (let ((tags (org-element-property :tags data)))
1999 (if (loop for tag in (plist-get info :select-tags)
2000 thereis (member tag tags))
2001 ;; When a select tag is found, mark full
2002 ;; genealogy and every headline within the tree
2003 ;; as acceptable.
2004 (setq selected-trees
2005 (append
2006 genealogy
2007 (org-element-map data '(headline inlinetask)
2008 'identity)
2009 selected-trees))
2010 ;; If at a headline, continue searching in tree,
2011 ;; recursively.
2012 (when (eq type 'headline)
2013 (mapc (lambda (el)
2014 (funcall walk-data el (cons data genealogy)))
2015 (org-element-contents data))))))
2016 ((or (eq type 'org-data)
2017 (memq type org-element-greater-elements))
2018 (mapc (lambda (el) (funcall walk-data el genealogy))
2019 (org-element-contents data)))))))))
2020 (funcall walk-data data nil)
2021 selected-trees))
2023 (defun org-export--skip-p (blob options selected)
2024 "Non-nil when element or object BLOB should be skipped during export.
2025 OPTIONS is the plist holding export options. SELECTED, when
2026 non-nil, is a list of headlines or inlinetasks belonging to
2027 a tree with a select tag."
2028 (case (org-element-type blob)
2029 (clock (not (plist-get options :with-clocks)))
2030 (drawer
2031 (let ((with-drawers-p (plist-get options :with-drawers)))
2032 (or (not with-drawers-p)
2033 (and (consp with-drawers-p)
2034 ;; If `:with-drawers' value starts with `not', ignore
2035 ;; every drawer whose name belong to that list.
2036 ;; Otherwise, ignore drawers whose name isn't in that
2037 ;; list.
2038 (let ((name (org-element-property :drawer-name blob)))
2039 (if (eq (car with-drawers-p) 'not)
2040 (member-ignore-case name (cdr with-drawers-p))
2041 (not (member-ignore-case name with-drawers-p))))))))
2042 ((footnote-definition footnote-reference)
2043 (not (plist-get options :with-footnotes)))
2044 ((headline inlinetask)
2045 (let ((with-tasks (plist-get options :with-tasks))
2046 (todo (org-element-property :todo-keyword blob))
2047 (todo-type (org-element-property :todo-type blob))
2048 (archived (plist-get options :with-archived-trees))
2049 (tags (org-element-property :tags blob)))
2051 (and (eq (org-element-type blob) 'inlinetask)
2052 (not (plist-get options :with-inlinetasks)))
2053 ;; Ignore subtrees with an exclude tag.
2054 (loop for k in (plist-get options :exclude-tags)
2055 thereis (member k tags))
2056 ;; When a select tag is present in the buffer, ignore any tree
2057 ;; without it.
2058 (and selected (not (memq blob selected)))
2059 ;; Ignore commented sub-trees.
2060 (org-element-property :commentedp blob)
2061 ;; Ignore archived subtrees if `:with-archived-trees' is nil.
2062 (and (not archived) (org-element-property :archivedp blob))
2063 ;; Ignore tasks, if specified by `:with-tasks' property.
2064 (and todo
2065 (or (not with-tasks)
2066 (and (memq with-tasks '(todo done))
2067 (not (eq todo-type with-tasks)))
2068 (and (consp with-tasks) (not (member todo with-tasks))))))))
2069 ((latex-environment latex-fragment) (not (plist-get options :with-latex)))
2070 (planning (not (plist-get options :with-planning)))
2071 (statistics-cookie (not (plist-get options :with-statistics-cookies)))
2072 (table-cell
2073 (and (org-export-table-has-special-column-p
2074 (org-export-get-parent-table blob))
2075 (not (org-export-get-previous-element blob options))))
2076 (table-row (org-export-table-row-is-special-p blob options))
2077 (timestamp
2078 ;; `:with-timestamps' only applies to isolated timestamps
2079 ;; objects, i.e. timestamp objects in a paragraph containing only
2080 ;; timestamps and whitespaces.
2081 (when (let ((parent (org-export-get-parent-element blob)))
2082 (and (memq (org-element-type parent) '(paragraph verse-block))
2083 (not (org-element-map parent
2084 (cons 'plain-text
2085 (remq 'timestamp org-element-all-objects))
2086 (lambda (obj)
2087 (or (not (stringp obj)) (org-string-nw-p obj)))
2088 options t))))
2089 (case (plist-get options :with-timestamps)
2090 ('nil t)
2091 (active
2092 (not (memq (org-element-property :type blob) '(active active-range))))
2093 (inactive
2094 (not (memq (org-element-property :type blob)
2095 '(inactive inactive-range)))))))))
2098 ;;; The Transcoder
2100 ;; `org-export-data' reads a parse tree (obtained with, i.e.
2101 ;; `org-element-parse-buffer') and transcodes it into a specified
2102 ;; back-end output. It takes care of filtering out elements or
2103 ;; objects according to export options and organizing the output blank
2104 ;; lines and white space are preserved. The function memoizes its
2105 ;; results, so it is cheap to call it within transcoders.
2107 ;; It is possible to modify locally the back-end used by
2108 ;; `org-export-data' or even use a temporary back-end by using
2109 ;; `org-export-data-with-backend'.
2111 ;; Internally, three functions handle the filtering of objects and
2112 ;; elements during the export. In particular,
2113 ;; `org-export-ignore-element' marks an element or object so future
2114 ;; parse tree traversals skip it, `org-export--interpret-p' tells which
2115 ;; elements or objects should be seen as real Org syntax and
2116 ;; `org-export-expand' transforms the others back into their original
2117 ;; shape
2119 ;; `org-export-transcoder' is an accessor returning appropriate
2120 ;; translator function for a given element or object.
2122 (defun org-export-transcoder (blob info)
2123 "Return appropriate transcoder for BLOB.
2124 INFO is a plist containing export directives."
2125 (let ((type (org-element-type blob)))
2126 ;; Return contents only for complete parse trees.
2127 (if (eq type 'org-data) (lambda (blob contents info) contents)
2128 (let ((transcoder (cdr (assq type (plist-get info :translate-alist)))))
2129 (and (functionp transcoder) transcoder)))))
2131 (defun org-export-data (data info)
2132 "Convert DATA into current back-end format.
2134 DATA is a parse tree, an element or an object or a secondary
2135 string. INFO is a plist holding export options.
2137 Return transcoded string."
2138 (let ((memo (gethash data (plist-get info :exported-data) 'no-memo)))
2139 (if (not (eq memo 'no-memo)) memo
2140 (let* ((type (org-element-type data))
2141 (results
2142 (cond
2143 ;; Ignored element/object.
2144 ((memq data (plist-get info :ignore-list)) nil)
2145 ;; Plain text.
2146 ((eq type 'plain-text)
2147 (org-export-filter-apply-functions
2148 (plist-get info :filter-plain-text)
2149 (let ((transcoder (org-export-transcoder data info)))
2150 (if transcoder (funcall transcoder data info) data))
2151 info))
2152 ;; Uninterpreted element/object: change it back to Org
2153 ;; syntax and export again resulting raw string.
2154 ((not (org-export--interpret-p data info))
2155 (org-export-data
2156 (org-export-expand
2157 data
2158 (mapconcat (lambda (blob) (org-export-data blob info))
2159 (org-element-contents data)
2160 ""))
2161 info))
2162 ;; Secondary string.
2163 ((not type)
2164 (mapconcat (lambda (obj) (org-export-data obj info)) data ""))
2165 ;; Element/Object without contents or, as a special case,
2166 ;; headline with archive tag and archived trees restricted
2167 ;; to title only.
2168 ((or (not (org-element-contents data))
2169 (and (eq type 'headline)
2170 (eq (plist-get info :with-archived-trees) 'headline)
2171 (org-element-property :archivedp data)))
2172 (let ((transcoder (org-export-transcoder data info)))
2173 (or (and (functionp transcoder)
2174 (funcall transcoder data nil info))
2175 ;; Export snippets never return a nil value so
2176 ;; that white spaces following them are never
2177 ;; ignored.
2178 (and (eq type 'export-snippet) ""))))
2179 ;; Element/Object with contents.
2181 (let ((transcoder (org-export-transcoder data info)))
2182 (when transcoder
2183 (let* ((greaterp (memq type org-element-greater-elements))
2184 (objectp
2185 (and (not greaterp)
2186 (memq type org-element-recursive-objects)))
2187 (contents
2188 (mapconcat
2189 (lambda (element) (org-export-data element info))
2190 (org-element-contents
2191 (if (or greaterp objectp) data
2192 ;; Elements directly containing objects
2193 ;; must have their indentation normalized
2194 ;; first.
2195 (org-element-normalize-contents
2196 data
2197 ;; When normalizing contents of the first
2198 ;; paragraph in an item or a footnote
2199 ;; definition, ignore first line's
2200 ;; indentation: there is none and it
2201 ;; might be misleading.
2202 (when (eq type 'paragraph)
2203 (let ((parent (org-export-get-parent data)))
2204 (and
2205 (eq (car (org-element-contents parent))
2206 data)
2207 (memq (org-element-type parent)
2208 '(footnote-definition item))))))))
2209 "")))
2210 (funcall transcoder data
2211 (if (not greaterp) contents
2212 (org-element-normalize-string contents))
2213 info))))))))
2214 ;; Final result will be memoized before being returned.
2215 (puthash
2216 data
2217 (cond
2218 ((not results) nil)
2219 ((memq type '(org-data plain-text nil)) results)
2220 ;; Append the same white space between elements or objects as in
2221 ;; the original buffer, and call appropriate filters.
2223 (let ((results
2224 (org-export-filter-apply-functions
2225 (plist-get info (intern (format ":filter-%s" type)))
2226 (let ((post-blank (or (org-element-property :post-blank data)
2227 0)))
2228 (if (memq type org-element-all-elements)
2229 (concat (org-element-normalize-string results)
2230 (make-string post-blank ?\n))
2231 (concat results (make-string post-blank ? ))))
2232 info)))
2233 results)))
2234 (plist-get info :exported-data))))))
2236 (defun org-export-data-with-backend (data backend info)
2237 "Convert DATA into BACKEND format.
2239 DATA is an element, an object, a secondary string or a string.
2240 BACKEND is a symbol. INFO is a plist used as a communication
2241 channel.
2243 Unlike to `org-export-with-backend', this function will
2244 recursively convert DATA using BACKEND translation table."
2245 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
2246 (org-export-data
2247 data
2248 ;; Set-up a new communication channel with translations defined in
2249 ;; BACKEND as the translate table and a new hash table for
2250 ;; memoization.
2251 (org-combine-plists
2252 info
2253 (list :back-end backend
2254 :translate-alist (org-export-get-all-transcoders backend)
2255 ;; Size of the hash table is reduced since this function
2256 ;; will probably be used on small trees.
2257 :exported-data (make-hash-table :test 'eq :size 401)))))
2259 (defun org-export--interpret-p (blob info)
2260 "Non-nil if element or object BLOB should be interpreted during export.
2261 If nil, BLOB will appear as raw Org syntax. Check is done
2262 according to export options INFO, stored as a plist."
2263 (case (org-element-type blob)
2264 ;; ... entities...
2265 (entity (plist-get info :with-entities))
2266 ;; ... emphasis...
2267 ((bold italic strike-through underline)
2268 (plist-get info :with-emphasize))
2269 ;; ... fixed-width areas.
2270 (fixed-width (plist-get info :with-fixed-width))
2271 ;; ... LaTeX environments and fragments...
2272 ((latex-environment latex-fragment)
2273 (let ((with-latex-p (plist-get info :with-latex)))
2274 (and with-latex-p (not (eq with-latex-p 'verbatim)))))
2275 ;; ... sub/superscripts...
2276 ((subscript superscript)
2277 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2278 (if (eq sub/super-p '{})
2279 (org-element-property :use-brackets-p blob)
2280 sub/super-p)))
2281 ;; ... tables...
2282 (table (plist-get info :with-tables))
2283 (otherwise t)))
2285 (defun org-export-expand (blob contents &optional with-affiliated)
2286 "Expand a parsed element or object to its original state.
2288 BLOB is either an element or an object. CONTENTS is its
2289 contents, as a string or nil.
2291 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2292 keywords before output."
2293 (let ((type (org-element-type blob)))
2294 (concat (and with-affiliated (memq type org-element-all-elements)
2295 (org-element--interpret-affiliated-keywords blob))
2296 (funcall (intern (format "org-element-%s-interpreter" type))
2297 blob contents))))
2299 (defun org-export-ignore-element (element info)
2300 "Add ELEMENT to `:ignore-list' in INFO.
2302 Any element in `:ignore-list' will be skipped when using
2303 `org-element-map'. INFO is modified by side effects."
2304 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2308 ;;; The Filter System
2310 ;; Filters allow end-users to tweak easily the transcoded output.
2311 ;; They are the functional counterpart of hooks, as every filter in
2312 ;; a set is applied to the return value of the previous one.
2314 ;; Every set is back-end agnostic. Although, a filter is always
2315 ;; called, in addition to the string it applies to, with the back-end
2316 ;; used as argument, so it's easy for the end-user to add back-end
2317 ;; specific filters in the set. The communication channel, as
2318 ;; a plist, is required as the third argument.
2320 ;; From the developer side, filters sets can be installed in the
2321 ;; process with the help of `org-export-define-backend', which
2322 ;; internally stores filters as an alist. Each association has a key
2323 ;; among the following symbols and a function or a list of functions
2324 ;; as value.
2326 ;; - `:filter-options' applies to the property list containing export
2327 ;; options. Unlike to other filters, functions in this list accept
2328 ;; two arguments instead of three: the property list containing
2329 ;; export options and the back-end. Users can set its value through
2330 ;; `org-export-filter-options-functions' variable.
2332 ;; - `:filter-parse-tree' applies directly to the complete parsed
2333 ;; tree. Users can set it through
2334 ;; `org-export-filter-parse-tree-functions' variable.
2336 ;; - `:filter-final-output' applies to the final transcoded string.
2337 ;; Users can set it with `org-export-filter-final-output-functions'
2338 ;; variable
2340 ;; - `:filter-plain-text' applies to any string not recognized as Org
2341 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2342 ;; configure it.
2344 ;; - `:filter-TYPE' applies on the string returned after an element or
2345 ;; object of type TYPE has been transcoded. A user can modify
2346 ;; `org-export-filter-TYPE-functions'
2348 ;; All filters sets are applied with
2349 ;; `org-export-filter-apply-functions' function. Filters in a set are
2350 ;; applied in a LIFO fashion. It allows developers to be sure that
2351 ;; their filters will be applied first.
2353 ;; Filters properties are installed in communication channel with
2354 ;; `org-export-install-filters' function.
2356 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2357 ;; `org-export-before-parsing-hook') are run at the beginning of the
2358 ;; export process and just before parsing to allow for heavy structure
2359 ;; modifications.
2362 ;;;; Hooks
2364 (defvar org-export-before-processing-hook nil
2365 "Hook run at the beginning of the export process.
2367 This is run before include keywords and macros are expanded and
2368 Babel code blocks executed, on a copy of the original buffer
2369 being exported. Visibility and narrowing are preserved. Point
2370 is at the beginning of the buffer.
2372 Every function in this hook will be called with one argument: the
2373 back-end currently used, as a symbol.")
2375 (defvar org-export-before-parsing-hook nil
2376 "Hook run before parsing an export buffer.
2378 This is run after include keywords and macros have been expanded
2379 and Babel code blocks executed, on a copy of the original buffer
2380 being exported. Visibility and narrowing are preserved. Point
2381 is at the beginning of the buffer.
2383 Every function in this hook will be called with one argument: the
2384 back-end currently used, as a symbol.")
2387 ;;;; Special Filters
2389 (defvar org-export-filter-options-functions nil
2390 "List of functions applied to the export options.
2391 Each filter is called with two arguments: the export options, as
2392 a plist, and the back-end, as a symbol. It must return
2393 a property list containing export options.")
2395 (defvar org-export-filter-parse-tree-functions nil
2396 "List of functions applied to the parsed tree.
2397 Each filter is called with three arguments: the parse tree, as
2398 returned by `org-element-parse-buffer', the back-end, as
2399 a symbol, and the communication channel, as a plist. It must
2400 return the modified parse tree to transcode.")
2402 (defvar org-export-filter-plain-text-functions nil
2403 "List of functions applied to plain text.
2404 Each filter is called with three arguments: a string which
2405 contains no Org syntax, the back-end, as a symbol, and the
2406 communication channel, as a plist. It must return a string or
2407 nil.")
2409 (defvar org-export-filter-final-output-functions nil
2410 "List of functions applied to the transcoded string.
2411 Each filter is called with three arguments: the full transcoded
2412 string, the back-end, as a symbol, and the communication channel,
2413 as a plist. It must return a string that will be used as the
2414 final export output.")
2417 ;;;; Elements Filters
2419 (defvar org-export-filter-babel-call-functions nil
2420 "List of functions applied to a transcoded babel-call.
2421 Each filter is called with three arguments: the transcoded data,
2422 as a string, the back-end, as a symbol, and the communication
2423 channel, as a plist. It must return a string or nil.")
2425 (defvar org-export-filter-center-block-functions nil
2426 "List of functions applied to a transcoded center block.
2427 Each filter is called with three arguments: the transcoded data,
2428 as a string, the back-end, as a symbol, and the communication
2429 channel, as a plist. It must return a string or nil.")
2431 (defvar org-export-filter-clock-functions nil
2432 "List of functions applied to a transcoded clock.
2433 Each filter is called with three arguments: the transcoded data,
2434 as a string, the back-end, as a symbol, and the communication
2435 channel, as a plist. It must return a string or nil.")
2437 (defvar org-export-filter-comment-functions nil
2438 "List of functions applied to a transcoded comment.
2439 Each filter is called with three arguments: the transcoded data,
2440 as a string, the back-end, as a symbol, and the communication
2441 channel, as a plist. It must return a string or nil.")
2443 (defvar org-export-filter-comment-block-functions nil
2444 "List of functions applied to a transcoded comment-block.
2445 Each filter is called with three arguments: the transcoded data,
2446 as a string, the back-end, as a symbol, and the communication
2447 channel, as a plist. It must return a string or nil.")
2449 (defvar org-export-filter-diary-sexp-functions nil
2450 "List of functions applied to a transcoded diary-sexp.
2451 Each filter is called with three arguments: the transcoded data,
2452 as a string, the back-end, as a symbol, and the communication
2453 channel, as a plist. It must return a string or nil.")
2455 (defvar org-export-filter-drawer-functions nil
2456 "List of functions applied to a transcoded drawer.
2457 Each filter is called with three arguments: the transcoded data,
2458 as a string, the back-end, as a symbol, and the communication
2459 channel, as a plist. It must return a string or nil.")
2461 (defvar org-export-filter-dynamic-block-functions nil
2462 "List of functions applied to a transcoded dynamic-block.
2463 Each filter is called with three arguments: the transcoded data,
2464 as a string, the back-end, as a symbol, and the communication
2465 channel, as a plist. It must return a string or nil.")
2467 (defvar org-export-filter-example-block-functions nil
2468 "List of functions applied to a transcoded example-block.
2469 Each filter is called with three arguments: the transcoded data,
2470 as a string, the back-end, as a symbol, and the communication
2471 channel, as a plist. It must return a string or nil.")
2473 (defvar org-export-filter-export-block-functions nil
2474 "List of functions applied to a transcoded export-block.
2475 Each filter is called with three arguments: the transcoded data,
2476 as a string, the back-end, as a symbol, and the communication
2477 channel, as a plist. It must return a string or nil.")
2479 (defvar org-export-filter-fixed-width-functions nil
2480 "List of functions applied to a transcoded fixed-width.
2481 Each filter is called with three arguments: the transcoded data,
2482 as a string, the back-end, as a symbol, and the communication
2483 channel, as a plist. It must return a string or nil.")
2485 (defvar org-export-filter-footnote-definition-functions nil
2486 "List of functions applied to a transcoded footnote-definition.
2487 Each filter is called with three arguments: the transcoded data,
2488 as a string, the back-end, as a symbol, and the communication
2489 channel, as a plist. It must return a string or nil.")
2491 (defvar org-export-filter-headline-functions nil
2492 "List of functions applied to a transcoded headline.
2493 Each filter is called with three arguments: the transcoded data,
2494 as a string, the back-end, as a symbol, and the communication
2495 channel, as a plist. It must return a string or nil.")
2497 (defvar org-export-filter-horizontal-rule-functions nil
2498 "List of functions applied to a transcoded horizontal-rule.
2499 Each filter is called with three arguments: the transcoded data,
2500 as a string, the back-end, as a symbol, and the communication
2501 channel, as a plist. It must return a string or nil.")
2503 (defvar org-export-filter-inlinetask-functions nil
2504 "List of functions applied to a transcoded inlinetask.
2505 Each filter is called with three arguments: the transcoded data,
2506 as a string, the back-end, as a symbol, and the communication
2507 channel, as a plist. It must return a string or nil.")
2509 (defvar org-export-filter-item-functions nil
2510 "List of functions applied to a transcoded item.
2511 Each filter is called with three arguments: the transcoded data,
2512 as a string, the back-end, as a symbol, and the communication
2513 channel, as a plist. It must return a string or nil.")
2515 (defvar org-export-filter-keyword-functions nil
2516 "List of functions applied to a transcoded keyword.
2517 Each filter is called with three arguments: the transcoded data,
2518 as a string, the back-end, as a symbol, and the communication
2519 channel, as a plist. It must return a string or nil.")
2521 (defvar org-export-filter-latex-environment-functions nil
2522 "List of functions applied to a transcoded latex-environment.
2523 Each filter is called with three arguments: the transcoded data,
2524 as a string, the back-end, as a symbol, and the communication
2525 channel, as a plist. It must return a string or nil.")
2527 (defvar org-export-filter-node-property-functions nil
2528 "List of functions applied to a transcoded node-property.
2529 Each filter is called with three arguments: the transcoded data,
2530 as a string, the back-end, as a symbol, and the communication
2531 channel, as a plist. It must return a string or nil.")
2533 (defvar org-export-filter-paragraph-functions nil
2534 "List of functions applied to a transcoded paragraph.
2535 Each filter is called with three arguments: the transcoded data,
2536 as a string, the back-end, as a symbol, and the communication
2537 channel, as a plist. It must return a string or nil.")
2539 (defvar org-export-filter-plain-list-functions nil
2540 "List of functions applied to a transcoded plain-list.
2541 Each filter is called with three arguments: the transcoded data,
2542 as a string, the back-end, as a symbol, and the communication
2543 channel, as a plist. It must return a string or nil.")
2545 (defvar org-export-filter-planning-functions nil
2546 "List of functions applied to a transcoded planning.
2547 Each filter is called with three arguments: the transcoded data,
2548 as a string, the back-end, as a symbol, and the communication
2549 channel, as a plist. It must return a string or nil.")
2551 (defvar org-export-filter-property-drawer-functions nil
2552 "List of functions applied to a transcoded property-drawer.
2553 Each filter is called with three arguments: the transcoded data,
2554 as a string, the back-end, as a symbol, and the communication
2555 channel, as a plist. It must return a string or nil.")
2557 (defvar org-export-filter-quote-block-functions nil
2558 "List of functions applied to a transcoded quote block.
2559 Each filter is called with three arguments: the transcoded quote
2560 data, as a string, the back-end, as a symbol, and the
2561 communication channel, as a plist. It must return a string or
2562 nil.")
2564 (defvar org-export-filter-quote-section-functions nil
2565 "List of functions applied to a transcoded quote-section.
2566 Each filter is called with three arguments: the transcoded data,
2567 as a string, the back-end, as a symbol, and the communication
2568 channel, as a plist. It must return a string or nil.")
2570 (defvar org-export-filter-section-functions nil
2571 "List of functions applied to a transcoded section.
2572 Each filter is called with three arguments: the transcoded data,
2573 as a string, the back-end, as a symbol, and the communication
2574 channel, as a plist. It must return a string or nil.")
2576 (defvar org-export-filter-special-block-functions nil
2577 "List of functions applied to a transcoded special block.
2578 Each filter is called with three arguments: the transcoded data,
2579 as a string, the back-end, as a symbol, and the communication
2580 channel, as a plist. It must return a string or nil.")
2582 (defvar org-export-filter-src-block-functions nil
2583 "List of functions applied to a transcoded src-block.
2584 Each filter is called with three arguments: the transcoded data,
2585 as a string, the back-end, as a symbol, and the communication
2586 channel, as a plist. It must return a string or nil.")
2588 (defvar org-export-filter-table-functions nil
2589 "List of functions applied to a transcoded table.
2590 Each filter is called with three arguments: the transcoded data,
2591 as a string, the back-end, as a symbol, and the communication
2592 channel, as a plist. It must return a string or nil.")
2594 (defvar org-export-filter-table-cell-functions nil
2595 "List of functions applied to a transcoded table-cell.
2596 Each filter is called with three arguments: the transcoded data,
2597 as a string, the back-end, as a symbol, and the communication
2598 channel, as a plist. It must return a string or nil.")
2600 (defvar org-export-filter-table-row-functions nil
2601 "List of functions applied to a transcoded table-row.
2602 Each filter is called with three arguments: the transcoded data,
2603 as a string, the back-end, as a symbol, and the communication
2604 channel, as a plist. It must return a string or nil.")
2606 (defvar org-export-filter-verse-block-functions nil
2607 "List of functions applied to a transcoded verse block.
2608 Each filter is called with three arguments: the transcoded data,
2609 as a string, the back-end, as a symbol, and the communication
2610 channel, as a plist. It must return a string or nil.")
2613 ;;;; Objects Filters
2615 (defvar org-export-filter-bold-functions nil
2616 "List of functions applied to transcoded bold text.
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-code-functions nil
2622 "List of functions applied to transcoded code text.
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-entity-functions nil
2628 "List of functions applied to a transcoded entity.
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-export-snippet-functions nil
2634 "List of functions applied to a transcoded export-snippet.
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-footnote-reference-functions nil
2640 "List of functions applied to a transcoded footnote-reference.
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-inline-babel-call-functions nil
2646 "List of functions applied to a transcoded inline-babel-call.
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-inline-src-block-functions nil
2652 "List of functions applied to a transcoded inline-src-block.
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-italic-functions nil
2658 "List of functions applied to transcoded italic text.
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-latex-fragment-functions nil
2664 "List of functions applied to a transcoded latex-fragment.
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-line-break-functions nil
2670 "List of functions applied to a transcoded line-break.
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-link-functions nil
2676 "List of functions applied to a transcoded link.
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-radio-target-functions nil
2682 "List of functions applied to a transcoded radio-target.
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-statistics-cookie-functions nil
2688 "List of functions applied to a transcoded statistics-cookie.
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-strike-through-functions nil
2694 "List of functions applied to transcoded strike-through text.
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-subscript-functions nil
2700 "List of functions applied to a transcoded subscript.
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-superscript-functions nil
2706 "List of functions applied to a transcoded superscript.
2707 Each filter is called with three arguments: the transcoded data,
2708 as a string, the back-end, as a symbol, and the communication
2709 channel, as a plist. It must return a string or nil.")
2711 (defvar org-export-filter-target-functions nil
2712 "List of functions applied to a transcoded target.
2713 Each filter is called with three arguments: the transcoded data,
2714 as a string, the back-end, as a symbol, and the communication
2715 channel, as a plist. It must return a string or nil.")
2717 (defvar org-export-filter-timestamp-functions nil
2718 "List of functions applied to a transcoded timestamp.
2719 Each filter is called with three arguments: the transcoded data,
2720 as a string, the back-end, as a symbol, and the communication
2721 channel, as a plist. It must return a string or nil.")
2723 (defvar org-export-filter-underline-functions nil
2724 "List of functions applied to transcoded underline text.
2725 Each filter is called with three arguments: the transcoded data,
2726 as a string, the back-end, as a symbol, and the communication
2727 channel, as a plist. It must return a string or nil.")
2729 (defvar org-export-filter-verbatim-functions nil
2730 "List of functions applied to transcoded verbatim text.
2731 Each filter is called with three arguments: the transcoded data,
2732 as a string, the back-end, as a symbol, and the communication
2733 channel, as a plist. It must return a string or nil.")
2736 ;;;; Filters Tools
2738 ;; Internal function `org-export-install-filters' installs filters
2739 ;; hard-coded in back-ends (developer filters) and filters from global
2740 ;; variables (user filters) in the communication channel.
2742 ;; Internal function `org-export-filter-apply-functions' takes care
2743 ;; about applying each filter in order to a given data. It ignores
2744 ;; filters returning a nil value but stops whenever a filter returns
2745 ;; an empty string.
2747 (defun org-export-filter-apply-functions (filters value info)
2748 "Call every function in FILTERS.
2750 Functions are called with arguments VALUE, current export
2751 back-end's name and INFO. A function returning a nil value will
2752 be skipped. If it returns the empty string, the process ends and
2753 VALUE is ignored.
2755 Call is done in a LIFO fashion, to be sure that developer
2756 specified filters, if any, are called first."
2757 (catch 'exit
2758 (let* ((backend (plist-get info :back-end))
2759 (backend-name (and backend (org-export-backend-name backend))))
2760 (dolist (filter filters value)
2761 (let ((result (funcall filter value backend-name info)))
2762 (cond ((not result) value)
2763 ((equal value "") (throw 'exit nil))
2764 (t (setq value result))))))))
2766 (defun org-export-install-filters (info)
2767 "Install filters properties in communication channel.
2768 INFO is a plist containing the current communication channel.
2769 Return the updated communication channel."
2770 (let (plist)
2771 ;; Install user-defined filters with `org-export-filters-alist'
2772 ;; and filters already in INFO (through ext-plist mechanism).
2773 (mapc (lambda (p)
2774 (let* ((prop (car p))
2775 (info-value (plist-get info prop))
2776 (default-value (symbol-value (cdr p))))
2777 (setq plist
2778 (plist-put plist prop
2779 ;; Filters in INFO will be called
2780 ;; before those user provided.
2781 (append (if (listp info-value) info-value
2782 (list info-value))
2783 default-value)))))
2784 org-export-filters-alist)
2785 ;; Prepend back-end specific filters to that list.
2786 (mapc (lambda (p)
2787 ;; Single values get consed, lists are appended.
2788 (let ((key (car p)) (value (cdr p)))
2789 (when value
2790 (setq plist
2791 (plist-put
2792 plist key
2793 (if (atom value) (cons value (plist-get plist key))
2794 (append value (plist-get plist key))))))))
2795 (org-export-get-all-filters (plist-get info :back-end)))
2796 ;; Return new communication channel.
2797 (org-combine-plists info plist)))
2801 ;;; Core functions
2803 ;; This is the room for the main function, `org-export-as', along with
2804 ;; its derivative, `org-export-string-as'.
2805 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2806 ;; function should be added to kill ring.
2808 ;; Note that `org-export-as' doesn't really parse the current buffer,
2809 ;; but a copy of it (with the same buffer-local variables and
2810 ;; visibility), where macros and include keywords are expanded and
2811 ;; Babel blocks are executed, if appropriate.
2812 ;; `org-export-with-buffer-copy' macro prepares that copy.
2814 ;; File inclusion is taken care of by
2815 ;; `org-export-expand-include-keyword' and
2816 ;; `org-export--prepare-file-contents'. Structure wise, including
2817 ;; a whole Org file in a buffer often makes little sense. For
2818 ;; example, if the file contains a headline and the include keyword
2819 ;; was within an item, the item should contain the headline. That's
2820 ;; why file inclusion should be done before any structure can be
2821 ;; associated to the file, that is before parsing.
2823 ;; `org-export-insert-default-template' is a command to insert
2824 ;; a default template (or a back-end specific template) at point or in
2825 ;; current subtree.
2827 (defun org-export-copy-buffer ()
2828 "Return a copy of the current buffer.
2829 The copy preserves Org buffer-local variables, visibility and
2830 narrowing."
2831 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2832 (new-buf (generate-new-buffer (buffer-name))))
2833 (with-current-buffer new-buf
2834 (funcall copy-buffer-fun)
2835 (set-buffer-modified-p nil))
2836 new-buf))
2838 (defmacro org-export-with-buffer-copy (&rest body)
2839 "Apply BODY in a copy of the current buffer.
2840 The copy preserves local variables, visibility and contents of
2841 the original buffer. Point is at the beginning of the buffer
2842 when BODY is applied."
2843 (declare (debug t))
2844 (org-with-gensyms (buf-copy)
2845 `(let ((,buf-copy (org-export-copy-buffer)))
2846 (unwind-protect
2847 (with-current-buffer ,buf-copy
2848 (goto-char (point-min))
2849 (progn ,@body))
2850 (and (buffer-live-p ,buf-copy)
2851 ;; Kill copy without confirmation.
2852 (progn (with-current-buffer ,buf-copy
2853 (restore-buffer-modified-p nil))
2854 (kill-buffer ,buf-copy)))))))
2856 (defun org-export--generate-copy-script (buffer)
2857 "Generate a function duplicating BUFFER.
2859 The copy will preserve local variables, visibility, contents and
2860 narrowing of the original buffer. If a region was active in
2861 BUFFER, contents will be narrowed to that region instead.
2863 The resulting function can be evaled at a later time, from
2864 another buffer, effectively cloning the original buffer there.
2866 The function assumes BUFFER's major mode is `org-mode'."
2867 (with-current-buffer buffer
2868 `(lambda ()
2869 (let ((inhibit-modification-hooks t))
2870 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2871 ;; already in BUFFER.
2872 (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
2873 ;; Copy specific buffer local variables and variables set
2874 ;; through BIND keywords.
2875 ,@(let ((bound-variables (org-export--list-bound-variables))
2876 vars)
2877 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
2878 (when (consp entry)
2879 (let ((var (car entry))
2880 (val (cdr entry)))
2881 (and (not (eq var 'org-font-lock-keywords))
2882 (or (memq var
2883 '(default-directory
2884 buffer-file-name
2885 buffer-file-coding-system))
2886 (assq var bound-variables)
2887 (string-match "^\\(org-\\|orgtbl-\\)"
2888 (symbol-name var)))
2889 ;; Skip unreadable values, as they cannot be
2890 ;; sent to external process.
2891 (or (not val) (ignore-errors (read (format "%S" val))))
2892 (push `(set (make-local-variable (quote ,var))
2893 (quote ,val))
2894 vars))))))
2895 ;; Whole buffer contents.
2896 (insert
2897 ,(org-with-wide-buffer
2898 (buffer-substring-no-properties
2899 (point-min) (point-max))))
2900 ;; Narrowing.
2901 ,(if (org-region-active-p)
2902 `(narrow-to-region ,(region-beginning) ,(region-end))
2903 `(narrow-to-region ,(point-min) ,(point-max)))
2904 ;; Current position of point.
2905 (goto-char ,(point))
2906 ;; Overlays with invisible property.
2907 ,@(let (ov-set)
2908 (mapc
2909 (lambda (ov)
2910 (let ((invis-prop (overlay-get ov 'invisible)))
2911 (when invis-prop
2912 (push `(overlay-put
2913 (make-overlay ,(overlay-start ov)
2914 ,(overlay-end ov))
2915 'invisible (quote ,invis-prop))
2916 ov-set))))
2917 (overlays-in (point-min) (point-max)))
2918 ov-set)))))
2920 ;;;###autoload
2921 (defun org-export-as
2922 (backend &optional subtreep visible-only body-only ext-plist)
2923 "Transcode current Org buffer into BACKEND code.
2925 BACKEND is either an export back-end, as returned by, e.g.,
2926 `org-export-create-backend', or a symbol referring to
2927 a registered back-end.
2929 If narrowing is active in the current buffer, only transcode its
2930 narrowed part.
2932 If a region is active, transcode that region.
2934 When optional argument SUBTREEP is non-nil, transcode the
2935 sub-tree at point, extracting information from the headline
2936 properties first.
2938 When optional argument VISIBLE-ONLY is non-nil, don't export
2939 contents of hidden elements.
2941 When optional argument BODY-ONLY is non-nil, only return body
2942 code, without surrounding template.
2944 Optional argument EXT-PLIST, when provided, is a property list
2945 with external parameters overriding Org default settings, but
2946 still inferior to file-local settings.
2948 Return code as a string."
2949 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
2950 (org-export-barf-if-invalid-backend backend)
2951 (save-excursion
2952 (save-restriction
2953 ;; Narrow buffer to an appropriate region or subtree for
2954 ;; parsing. If parsing subtree, be sure to remove main headline
2955 ;; too.
2956 (cond ((org-region-active-p)
2957 (narrow-to-region (region-beginning) (region-end)))
2958 (subtreep
2959 (org-narrow-to-subtree)
2960 (goto-char (point-min))
2961 (forward-line)
2962 (narrow-to-region (point) (point-max))))
2963 ;; Initialize communication channel with original buffer
2964 ;; attributes, unavailable in its copy.
2965 (let* ((org-export-current-backend (org-export-backend-name backend))
2966 (info (org-combine-plists
2967 (list :export-options
2968 (delq nil
2969 (list (and subtreep 'subtree)
2970 (and visible-only 'visible-only)
2971 (and body-only 'body-only))))
2972 (org-export--get-buffer-attributes)))
2973 tree)
2974 ;; Update communication channel and get parse tree. Buffer
2975 ;; isn't parsed directly. Instead, a temporary copy is
2976 ;; created, where include keywords, macros are expanded and
2977 ;; code blocks are evaluated.
2978 (org-export-with-buffer-copy
2979 ;; Run first hook with current back-end's name as argument.
2980 (run-hook-with-args 'org-export-before-processing-hook
2981 (org-export-backend-name backend))
2982 (org-export-expand-include-keyword)
2983 ;; Update macro templates since #+INCLUDE keywords might have
2984 ;; added some new ones.
2985 (org-macro-initialize-templates)
2986 (org-macro-replace-all org-macro-templates)
2987 (org-export-execute-babel-code)
2988 ;; Update radio targets since keyword inclusion might have
2989 ;; added some more.
2990 (org-update-radio-target-regexp)
2991 ;; Run last hook with current back-end's name as argument.
2992 (goto-char (point-min))
2993 (save-excursion
2994 (run-hook-with-args 'org-export-before-parsing-hook
2995 (org-export-backend-name backend)))
2996 ;; Update communication channel with environment. Also
2997 ;; install user's and developer's filters.
2998 (setq info
2999 (org-export-install-filters
3000 (org-combine-plists
3001 info (org-export-get-environment backend subtreep ext-plist))))
3002 ;; Expand export-specific set of macros: {{{author}}},
3003 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
3004 ;; once regular macros have been expanded, since document
3005 ;; keywords may contain one of them.
3006 (org-macro-replace-all
3007 (list (cons "author"
3008 (org-element-interpret-data (plist-get info :author)))
3009 (cons "date"
3010 (org-element-interpret-data (plist-get info :date)))
3011 ;; EMAIL is not a parsed keyword: store it as-is.
3012 (cons "email" (or (plist-get info :email) ""))
3013 (cons "title"
3014 (org-element-interpret-data (plist-get info :title)))))
3015 ;; Call options filters and update export options. We do not
3016 ;; use `org-export-filter-apply-functions' here since the
3017 ;; arity of such filters is different.
3018 (let ((backend-name (org-export-backend-name backend)))
3019 (dolist (filter (plist-get info :filter-options))
3020 (let ((result (funcall filter info backend-name)))
3021 (when result (setq info result)))))
3022 ;; Parse buffer and call parse-tree filter on it.
3023 (setq tree
3024 (org-export-filter-apply-functions
3025 (plist-get info :filter-parse-tree)
3026 (org-element-parse-buffer nil visible-only) info))
3027 ;; Now tree is complete, compute its properties and add them
3028 ;; to communication channel.
3029 (setq info
3030 (org-combine-plists
3031 info (org-export-collect-tree-properties tree info)))
3032 ;; Eventually transcode TREE. Wrap the resulting string into
3033 ;; a template.
3034 (let* ((body (org-element-normalize-string
3035 (or (org-export-data tree info) "")))
3036 (inner-template (cdr (assq 'inner-template
3037 (plist-get info :translate-alist))))
3038 (full-body (if (not (functionp inner-template)) body
3039 (funcall inner-template body info)))
3040 (template (cdr (assq 'template
3041 (plist-get info :translate-alist)))))
3042 ;; Remove all text properties since they cannot be
3043 ;; retrieved from an external process. Finally call
3044 ;; final-output filter and return result.
3045 (org-no-properties
3046 (org-export-filter-apply-functions
3047 (plist-get info :filter-final-output)
3048 (if (or (not (functionp template)) body-only) full-body
3049 (funcall template full-body info))
3050 info))))))))
3052 ;;;###autoload
3053 (defun org-export-string-as (string backend &optional body-only ext-plist)
3054 "Transcode STRING into BACKEND code.
3056 BACKEND is either an export back-end, as returned by, e.g.,
3057 `org-export-create-backend', or a symbol referring to
3058 a registered back-end.
3060 When optional argument BODY-ONLY is non-nil, only return body
3061 code, without preamble nor postamble.
3063 Optional argument EXT-PLIST, when provided, is a property list
3064 with external parameters overriding Org default settings, but
3065 still inferior to file-local settings.
3067 Return code as a string."
3068 (with-temp-buffer
3069 (insert string)
3070 (let ((org-inhibit-startup t)) (org-mode))
3071 (org-export-as backend nil nil body-only ext-plist)))
3073 ;;;###autoload
3074 (defun org-export-replace-region-by (backend)
3075 "Replace the active region by its export to BACKEND.
3076 BACKEND is either an export back-end, as returned by, e.g.,
3077 `org-export-create-backend', or a symbol referring to
3078 a registered back-end."
3079 (if (not (org-region-active-p))
3080 (user-error "No active region to replace")
3081 (let* ((beg (region-beginning))
3082 (end (region-end))
3083 (str (buffer-substring beg end)) rpl)
3084 (setq rpl (org-export-string-as str backend t))
3085 (delete-region beg end)
3086 (insert rpl))))
3088 ;;;###autoload
3089 (defun org-export-insert-default-template (&optional backend subtreep)
3090 "Insert all export keywords with default values at beginning of line.
3092 BACKEND is a symbol referring to the name of a registered export
3093 back-end, for which specific export options should be added to
3094 the template, or `default' for default template. When it is nil,
3095 the user will be prompted for a category.
3097 If SUBTREEP is non-nil, export configuration will be set up
3098 locally for the subtree through node properties."
3099 (interactive)
3100 (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
3101 (when (and subtreep (org-before-first-heading-p))
3102 (user-error "No subtree to set export options for"))
3103 (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
3104 (backend
3105 (or backend
3106 (intern
3107 (org-completing-read
3108 "Options category: "
3109 (cons "default"
3110 (mapcar (lambda (b)
3111 (symbol-name (org-export-backend-name b)))
3112 org-export--registered-backends))))))
3113 options keywords)
3114 ;; Populate OPTIONS and KEYWORDS.
3115 (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
3116 ((org-export-backend-p backend)
3117 (org-export-get-all-options backend))
3118 (t (org-export-get-all-options
3119 (org-export-get-backend backend)))))
3120 (let ((keyword (nth 1 entry))
3121 (option (nth 2 entry)))
3122 (cond
3123 (keyword (unless (assoc keyword keywords)
3124 (let ((value
3125 (if (eq (nth 4 entry) 'split)
3126 (mapconcat 'identity (eval (nth 3 entry)) " ")
3127 (eval (nth 3 entry)))))
3128 (push (cons keyword value) keywords))))
3129 (option (unless (assoc option options)
3130 (push (cons option (eval (nth 3 entry))) options))))))
3131 ;; Move to an appropriate location in order to insert options.
3132 (unless subtreep (beginning-of-line))
3133 ;; First get TITLE, DATE, AUTHOR and EMAIL if they belong to the
3134 ;; list of available keywords.
3135 (when (assoc "TITLE" keywords)
3136 (let ((title
3137 (or (let ((visited-file (buffer-file-name (buffer-base-buffer))))
3138 (and visited-file
3139 (file-name-sans-extension
3140 (file-name-nondirectory visited-file))))
3141 (buffer-name (buffer-base-buffer)))))
3142 (if (not subtreep) (insert (format "#+TITLE: %s\n" title))
3143 (org-entry-put node "EXPORT_TITLE" title))))
3144 (when (assoc "DATE" keywords)
3145 (let ((date (with-temp-buffer (org-insert-time-stamp (current-time)))))
3146 (if (not subtreep) (insert "#+DATE: " date "\n")
3147 (org-entry-put node "EXPORT_DATE" date))))
3148 (when (assoc "AUTHOR" keywords)
3149 (let ((author (cdr (assoc "AUTHOR" keywords))))
3150 (if subtreep (org-entry-put node "EXPORT_AUTHOR" author)
3151 (insert
3152 (format "#+AUTHOR:%s\n"
3153 (if (not (org-string-nw-p author)) ""
3154 (concat " " author)))))))
3155 (when (assoc "EMAIL" keywords)
3156 (let ((email (cdr (assoc "EMAIL" keywords))))
3157 (if subtreep (org-entry-put node "EXPORT_EMAIL" email)
3158 (insert
3159 (format "#+EMAIL:%s\n"
3160 (if (not (org-string-nw-p email)) ""
3161 (concat " " email)))))))
3162 ;; Then (multiple) OPTIONS lines. Never go past fill-column.
3163 (when options
3164 (let ((items
3165 (mapcar
3166 #'(lambda (opt) (format "%s:%S" (car opt) (cdr opt)))
3167 (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
3168 (if subtreep
3169 (org-entry-put
3170 node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
3171 (while items
3172 (insert "#+OPTIONS:")
3173 (let ((width 10))
3174 (while (and items
3175 (< (+ width (length (car items)) 1) fill-column))
3176 (let ((item (pop items)))
3177 (insert " " item)
3178 (incf width (1+ (length item))))))
3179 (insert "\n")))))
3180 ;; And the rest of keywords.
3181 (dolist (key (sort keywords (lambda (k1 k2) (string< (car k1) (car k2)))))
3182 (unless (member (car key) '("TITLE" "DATE" "AUTHOR" "EMAIL"))
3183 (let ((val (cdr key)))
3184 (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
3185 (insert
3186 (format "#+%s:%s\n"
3187 (car key)
3188 (if (org-string-nw-p val) (format " %s" val) "")))))))))
3190 (defun org-export-expand-include-keyword (&optional included dir)
3191 "Expand every include keyword in buffer.
3192 Optional argument INCLUDED is a list of included file names along
3193 with their line restriction, when appropriate. It is used to
3194 avoid infinite recursion. Optional argument DIR is the current
3195 working directory. It is used to properly resolve relative
3196 paths."
3197 (let ((case-fold-search t))
3198 (goto-char (point-min))
3199 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
3200 (let ((element (save-match-data (org-element-at-point))))
3201 (when (eq (org-element-type element) 'keyword)
3202 (beginning-of-line)
3203 ;; Extract arguments from keyword's value.
3204 (let* ((value (org-element-property :value element))
3205 (ind (org-get-indentation))
3206 (file (and (string-match
3207 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
3208 (prog1 (expand-file-name
3209 (org-remove-double-quotes
3210 (match-string 1 value))
3211 dir)
3212 (setq value (replace-match "" nil nil value)))))
3213 (lines
3214 (and (string-match
3215 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3216 value)
3217 (prog1 (match-string 1 value)
3218 (setq value (replace-match "" nil nil value)))))
3219 (env (cond ((string-match "\\<example\\>" value) 'example)
3220 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
3221 (match-string 1 value))))
3222 ;; Minimal level of included file defaults to the child
3223 ;; level of the current headline, if any, or one. It
3224 ;; only applies is the file is meant to be included as
3225 ;; an Org one.
3226 (minlevel
3227 (and (not env)
3228 (if (string-match ":minlevel +\\([0-9]+\\)" value)
3229 (prog1 (string-to-number (match-string 1 value))
3230 (setq value (replace-match "" nil nil value)))
3231 (let ((cur (org-current-level)))
3232 (if cur (1+ (org-reduced-level cur)) 1))))))
3233 ;; Remove keyword.
3234 (delete-region (point) (progn (forward-line) (point)))
3235 (cond
3236 ((not file) nil)
3237 ((not (file-readable-p file))
3238 (error "Cannot include file %s" file))
3239 ;; Check if files has already been parsed. Look after
3240 ;; inclusion lines too, as different parts of the same file
3241 ;; can be included too.
3242 ((member (list file lines) included)
3243 (error "Recursive file inclusion: %s" file))
3245 (cond
3246 ((eq env 'example)
3247 (insert
3248 (let ((ind-str (make-string ind ? ))
3249 (contents
3250 (org-escape-code-in-string
3251 (org-export--prepare-file-contents file lines))))
3252 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
3253 ind-str contents ind-str))))
3254 ((stringp env)
3255 (insert
3256 (let ((ind-str (make-string ind ? ))
3257 (contents
3258 (org-escape-code-in-string
3259 (org-export--prepare-file-contents file lines))))
3260 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
3261 ind-str env contents ind-str))))
3263 (insert
3264 (with-temp-buffer
3265 (let ((org-inhibit-startup t)) (org-mode))
3266 (insert
3267 (org-export--prepare-file-contents file lines ind minlevel))
3268 (org-export-expand-include-keyword
3269 (cons (list file lines) included)
3270 (file-name-directory file))
3271 (buffer-string)))))))))))))
3273 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
3274 "Prepare the contents of FILE for inclusion and return them as a string.
3276 When optional argument LINES is a string specifying a range of
3277 lines, include only those lines.
3279 Optional argument IND, when non-nil, is an integer specifying the
3280 global indentation of returned contents. Since its purpose is to
3281 allow an included file to stay in the same environment it was
3282 created \(i.e. a list item), it doesn't apply past the first
3283 headline encountered.
3285 Optional argument MINLEVEL, when non-nil, is an integer
3286 specifying the level that any top-level headline in the included
3287 file should have."
3288 (with-temp-buffer
3289 (insert-file-contents file)
3290 (when lines
3291 (let* ((lines (split-string lines "-"))
3292 (lbeg (string-to-number (car lines)))
3293 (lend (string-to-number (cadr lines)))
3294 (beg (if (zerop lbeg) (point-min)
3295 (goto-char (point-min))
3296 (forward-line (1- lbeg))
3297 (point)))
3298 (end (if (zerop lend) (point-max)
3299 (goto-char (point-min))
3300 (forward-line (1- lend))
3301 (point))))
3302 (narrow-to-region beg end)))
3303 ;; Remove blank lines at beginning and end of contents. The logic
3304 ;; behind that removal is that blank lines around include keyword
3305 ;; override blank lines in included file.
3306 (goto-char (point-min))
3307 (org-skip-whitespace)
3308 (beginning-of-line)
3309 (delete-region (point-min) (point))
3310 (goto-char (point-max))
3311 (skip-chars-backward " \r\t\n")
3312 (forward-line)
3313 (delete-region (point) (point-max))
3314 ;; If IND is set, preserve indentation of include keyword until
3315 ;; the first headline encountered.
3316 (when ind
3317 (unless (eq major-mode 'org-mode)
3318 (let ((org-inhibit-startup t)) (org-mode)))
3319 (goto-char (point-min))
3320 (let ((ind-str (make-string ind ? )))
3321 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
3322 ;; Do not move footnote definitions out of column 0.
3323 (unless (and (looking-at org-footnote-definition-re)
3324 (eq (org-element-type (org-element-at-point))
3325 'footnote-definition))
3326 (insert ind-str))
3327 (forward-line))))
3328 ;; When MINLEVEL is specified, compute minimal level for headlines
3329 ;; in the file (CUR-MIN), and remove stars to each headline so
3330 ;; that headlines with minimal level have a level of MINLEVEL.
3331 (when minlevel
3332 (unless (eq major-mode 'org-mode)
3333 (let ((org-inhibit-startup t)) (org-mode)))
3334 (org-with-limited-levels
3335 (let ((levels (org-map-entries
3336 (lambda () (org-reduced-level (org-current-level))))))
3337 (when levels
3338 (let ((offset (- minlevel (apply 'min levels))))
3339 (unless (zerop offset)
3340 (when org-odd-levels-only (setq offset (* offset 2)))
3341 ;; Only change stars, don't bother moving whole
3342 ;; sections.
3343 (org-map-entries
3344 (lambda () (if (< offset 0) (delete-char (abs offset))
3345 (insert (make-string offset ?*)))))))))))
3346 (org-element-normalize-string (buffer-string))))
3348 (defun org-export-execute-babel-code ()
3349 "Execute every Babel code in the visible part of current buffer."
3350 ;; Get a pristine copy of current buffer so Babel references can be
3351 ;; properly resolved.
3352 (let ((reference (org-export-copy-buffer)))
3353 (unwind-protect (let ((org-current-export-file reference))
3354 (org-babel-exp-process-buffer))
3355 (kill-buffer reference))))
3357 (defun org-export--copy-to-kill-ring-p ()
3358 "Return a non-nil value when output should be added to the kill ring.
3359 See also `org-export-copy-to-kill-ring'."
3360 (if (eq org-export-copy-to-kill-ring 'if-interactive)
3361 (not (or executing-kbd-macro noninteractive))
3362 (eq org-export-copy-to-kill-ring t)))
3366 ;;; Tools For Back-Ends
3368 ;; A whole set of tools is available to help build new exporters. Any
3369 ;; function general enough to have its use across many back-ends
3370 ;; should be added here.
3372 ;;;; For Affiliated Keywords
3374 ;; `org-export-read-attribute' reads a property from a given element
3375 ;; as a plist. It can be used to normalize affiliated keywords'
3376 ;; syntax.
3378 ;; Since captions can span over multiple lines and accept dual values,
3379 ;; their internal representation is a bit tricky. Therefore,
3380 ;; `org-export-get-caption' transparently returns a given element's
3381 ;; caption as a secondary string.
3383 (defun org-export-read-attribute (attribute element &optional property)
3384 "Turn ATTRIBUTE property from ELEMENT into a plist.
3386 When optional argument PROPERTY is non-nil, return the value of
3387 that property within attributes.
3389 This function assumes attributes are defined as \":keyword
3390 value\" pairs. It is appropriate for `:attr_html' like
3391 properties.
3393 All values will become strings except the empty string and
3394 \"nil\", which will become nil. Also, values containing only
3395 double quotes will be read as-is, which means that \"\" value
3396 will become the empty string."
3397 (let* ((prepare-value
3398 (lambda (str)
3399 (save-match-data
3400 (cond ((member str '(nil "" "nil")) nil)
3401 ((string-match "^\"\\(\"+\\)?\"$" str)
3402 (or (match-string 1 str) ""))
3403 (t str)))))
3404 (attributes
3405 (let ((value (org-element-property attribute element)))
3406 (when value
3407 (let ((s (mapconcat 'identity value " ")) result)
3408 (while (string-match
3409 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3411 (let ((value (substring s 0 (match-beginning 0))))
3412 (push (funcall prepare-value value) result))
3413 (push (intern (match-string 1 s)) result)
3414 (setq s (substring s (match-end 0))))
3415 ;; Ignore any string before first property with `cdr'.
3416 (cdr (nreverse (cons (funcall prepare-value s) result))))))))
3417 (if property (plist-get attributes property) attributes)))
3419 (defun org-export-get-caption (element &optional shortp)
3420 "Return caption from ELEMENT as a secondary string.
3422 When optional argument SHORTP is non-nil, return short caption,
3423 as a secondary string, instead.
3425 Caption lines are separated by a white space."
3426 (let ((full-caption (org-element-property :caption element)) caption)
3427 (dolist (line full-caption (cdr caption))
3428 (let ((cap (funcall (if shortp 'cdr 'car) line)))
3429 (when cap
3430 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
3433 ;;;; For Derived Back-ends
3435 ;; `org-export-with-backend' is a function allowing to locally use
3436 ;; another back-end to transcode some object or element. In a derived
3437 ;; back-end, it may be used as a fall-back function once all specific
3438 ;; cases have been treated.
3440 (defun org-export-with-backend (backend data &optional contents info)
3441 "Call a transcoder from BACKEND on DATA.
3442 BACKEND is an export back-end, as returned by, e.g.,
3443 `org-export-create-backend', or a symbol referring to
3444 a registered back-end. DATA is an Org element, object, secondary
3445 string or string. CONTENTS, when non-nil, is the transcoded
3446 contents of DATA element, as a string. INFO, when non-nil, is
3447 the communication channel used for export, as a plist."
3448 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
3449 (org-export-barf-if-invalid-backend backend)
3450 (let ((type (org-element-type data)))
3451 (if (memq type '(nil org-data)) (error "No foreign transcoder available")
3452 (let* ((all-transcoders (org-export-get-all-transcoders backend))
3453 (transcoder (cdr (assq type all-transcoders))))
3454 (if (not (functionp transcoder))
3455 (error "No foreign transcoder available")
3456 (funcall
3457 transcoder data contents
3458 (org-combine-plists
3459 info (list :back-end backend
3460 :translate-alist all-transcoders
3461 :exported-data (make-hash-table :test 'eq :size 401)))))))))
3464 ;;;; For Export Snippets
3466 ;; Every export snippet is transmitted to the back-end. Though, the
3467 ;; latter will only retain one type of export-snippet, ignoring
3468 ;; others, based on the former's target back-end. The function
3469 ;; `org-export-snippet-backend' returns that back-end for a given
3470 ;; export-snippet.
3472 (defun org-export-snippet-backend (export-snippet)
3473 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3474 Translation, with `org-export-snippet-translation-alist', is
3475 applied."
3476 (let ((back-end (org-element-property :back-end export-snippet)))
3477 (intern
3478 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3479 back-end))))
3482 ;;;; For Footnotes
3484 ;; `org-export-collect-footnote-definitions' is a tool to list
3485 ;; actually used footnotes definitions in the whole parse tree, or in
3486 ;; a headline, in order to add footnote listings throughout the
3487 ;; transcoded data.
3489 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3490 ;; back-ends, when they need to attach the footnote definition only to
3491 ;; the first occurrence of the corresponding label.
3493 ;; `org-export-get-footnote-definition' and
3494 ;; `org-export-get-footnote-number' provide easier access to
3495 ;; additional information relative to a footnote reference.
3497 (defun org-export-collect-footnote-definitions (data info)
3498 "Return an alist between footnote numbers, labels and definitions.
3500 DATA is the parse tree from which definitions are collected.
3501 INFO is the plist used as a communication channel.
3503 Definitions are sorted by order of references. They either
3504 appear as Org data or as a secondary string for inlined
3505 footnotes. Unreferenced definitions are ignored."
3506 (let* (num-alist
3507 collect-fn ; for byte-compiler.
3508 (collect-fn
3509 (function
3510 (lambda (data)
3511 ;; Collect footnote number, label and definition in DATA.
3512 (org-element-map data 'footnote-reference
3513 (lambda (fn)
3514 (when (org-export-footnote-first-reference-p fn info)
3515 (let ((def (org-export-get-footnote-definition fn info)))
3516 (push
3517 (list (org-export-get-footnote-number fn info)
3518 (org-element-property :label fn)
3519 def)
3520 num-alist)
3521 ;; Also search in definition for nested footnotes.
3522 (when (eq (org-element-property :type fn) 'standard)
3523 (funcall collect-fn def)))))
3524 ;; Don't enter footnote definitions since it will happen
3525 ;; when their first reference is found.
3526 info nil 'footnote-definition)))))
3527 (funcall collect-fn (plist-get info :parse-tree))
3528 (reverse num-alist)))
3530 (defun org-export-footnote-first-reference-p (footnote-reference info)
3531 "Non-nil when a footnote reference is the first one for its label.
3533 FOOTNOTE-REFERENCE is the footnote reference being considered.
3534 INFO is the plist used as a communication channel."
3535 (let ((label (org-element-property :label footnote-reference)))
3536 ;; Anonymous footnotes are always a first reference.
3537 (if (not label) t
3538 ;; Otherwise, return the first footnote with the same LABEL and
3539 ;; test if it is equal to FOOTNOTE-REFERENCE.
3540 (let* (search-refs ; for byte-compiler.
3541 (search-refs
3542 (function
3543 (lambda (data)
3544 (org-element-map data 'footnote-reference
3545 (lambda (fn)
3546 (cond
3547 ((string= (org-element-property :label fn) label)
3548 (throw 'exit fn))
3549 ;; If FN isn't inlined, be sure to traverse its
3550 ;; definition before resuming search. See
3551 ;; comments in `org-export-get-footnote-number'
3552 ;; for more information.
3553 ((eq (org-element-property :type fn) 'standard)
3554 (funcall search-refs
3555 (org-export-get-footnote-definition fn info)))))
3556 ;; Don't enter footnote definitions since it will
3557 ;; happen when their first reference is found.
3558 info 'first-match 'footnote-definition)))))
3559 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3560 footnote-reference)))))
3562 (defun org-export-get-footnote-definition (footnote-reference info)
3563 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3564 INFO is the plist used as a communication channel. If no such
3565 definition can be found, return the \"DEFINITION NOT FOUND\"
3566 string."
3567 (let ((label (org-element-property :label footnote-reference)))
3568 (or (org-element-property :inline-definition footnote-reference)
3569 (cdr (assoc label (plist-get info :footnote-definition-alist)))
3570 "DEFINITION NOT FOUND.")))
3572 (defun org-export-get-footnote-number (footnote info)
3573 "Return number associated to a footnote.
3575 FOOTNOTE is either a footnote reference or a footnote definition.
3576 INFO is the plist used as a communication channel."
3577 (let* ((label (org-element-property :label footnote))
3578 seen-refs
3579 search-ref ; For byte-compiler.
3580 (search-ref
3581 (function
3582 (lambda (data)
3583 ;; Search footnote references through DATA, filling
3584 ;; SEEN-REFS along the way.
3585 (org-element-map data 'footnote-reference
3586 (lambda (fn)
3587 (let ((fn-lbl (org-element-property :label fn)))
3588 (cond
3589 ;; Anonymous footnote match: return number.
3590 ((and (not fn-lbl) (eq fn footnote))
3591 (throw 'exit (1+ (length seen-refs))))
3592 ;; Labels match: return number.
3593 ((and label (string= label fn-lbl))
3594 (throw 'exit (1+ (length seen-refs))))
3595 ;; Anonymous footnote: it's always a new one.
3596 ;; Also, be sure to return nil from the `cond' so
3597 ;; `first-match' doesn't get us out of the loop.
3598 ((not fn-lbl) (push 'inline seen-refs) nil)
3599 ;; Label not seen so far: add it so SEEN-REFS.
3601 ;; Also search for subsequent references in
3602 ;; footnote definition so numbering follows
3603 ;; reading logic. Note that we don't have to care
3604 ;; about inline definitions, since
3605 ;; `org-element-map' already traverses them at the
3606 ;; right time.
3608 ;; Once again, return nil to stay in the loop.
3609 ((not (member fn-lbl seen-refs))
3610 (push fn-lbl seen-refs)
3611 (funcall search-ref
3612 (org-export-get-footnote-definition fn info))
3613 nil))))
3614 ;; Don't enter footnote definitions since it will
3615 ;; happen when their first reference is found.
3616 info 'first-match 'footnote-definition)))))
3617 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3620 ;;;; For Headlines
3622 ;; `org-export-get-relative-level' is a shortcut to get headline
3623 ;; level, relatively to the lower headline level in the parsed tree.
3625 ;; `org-export-get-headline-number' returns the section number of an
3626 ;; headline, while `org-export-number-to-roman' allows to convert it
3627 ;; to roman numbers.
3629 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3630 ;; `org-export-last-sibling-p' are three useful predicates when it
3631 ;; comes to fulfill the `:headline-levels' property.
3633 ;; `org-export-get-tags', `org-export-get-category' and
3634 ;; `org-export-get-node-property' extract useful information from an
3635 ;; headline or a parent headline. They all handle inheritance.
3637 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3638 ;; as a secondary string, suitable for table of contents. It falls
3639 ;; back onto default title.
3641 (defun org-export-get-relative-level (headline info)
3642 "Return HEADLINE relative level within current parsed tree.
3643 INFO is a plist holding contextual information."
3644 (+ (org-element-property :level headline)
3645 (or (plist-get info :headline-offset) 0)))
3647 (defun org-export-low-level-p (headline info)
3648 "Non-nil when HEADLINE is considered as low level.
3650 INFO is a plist used as a communication channel.
3652 A low level headlines has a relative level greater than
3653 `:headline-levels' property value.
3655 Return value is the difference between HEADLINE relative level
3656 and the last level being considered as high enough, or nil."
3657 (let ((limit (plist-get info :headline-levels)))
3658 (when (wholenump limit)
3659 (let ((level (org-export-get-relative-level headline info)))
3660 (and (> level limit) (- level limit))))))
3662 (defun org-export-get-headline-number (headline info)
3663 "Return HEADLINE numbering as a list of numbers.
3664 INFO is a plist holding contextual information."
3665 (cdr (assoc headline (plist-get info :headline-numbering))))
3667 (defun org-export-numbered-headline-p (headline info)
3668 "Return a non-nil value if HEADLINE element should be numbered.
3669 INFO is a plist used as a communication channel."
3670 (let ((sec-num (plist-get info :section-numbers))
3671 (level (org-export-get-relative-level headline info)))
3672 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3674 (defun org-export-number-to-roman (n)
3675 "Convert integer N into a roman numeral."
3676 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3677 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3678 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3679 ( 1 . "I")))
3680 (res ""))
3681 (if (<= n 0)
3682 (number-to-string n)
3683 (while roman
3684 (if (>= n (caar roman))
3685 (setq n (- n (caar roman))
3686 res (concat res (cdar roman)))
3687 (pop roman)))
3688 res)))
3690 (defun org-export-get-tags (element info &optional tags inherited)
3691 "Return list of tags associated to ELEMENT.
3693 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3694 is a plist used as a communication channel.
3696 Select tags (see `org-export-select-tags') and exclude tags (see
3697 `org-export-exclude-tags') are removed from the list.
3699 When non-nil, optional argument TAGS should be a list of strings.
3700 Any tag belonging to this list will also be removed.
3702 When optional argument INHERITED is non-nil, tags can also be
3703 inherited from parent headlines and FILETAGS keywords."
3704 (org-remove-if
3705 (lambda (tag) (or (member tag (plist-get info :select-tags))
3706 (member tag (plist-get info :exclude-tags))
3707 (member tag tags)))
3708 (if (not inherited) (org-element-property :tags element)
3709 ;; Build complete list of inherited tags.
3710 (let ((current-tag-list (org-element-property :tags element)))
3711 (mapc
3712 (lambda (parent)
3713 (mapc
3714 (lambda (tag)
3715 (when (and (memq (org-element-type parent) '(headline inlinetask))
3716 (not (member tag current-tag-list)))
3717 (push tag current-tag-list)))
3718 (org-element-property :tags parent)))
3719 (org-export-get-genealogy element))
3720 ;; Add FILETAGS keywords and return results.
3721 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
3723 (defun org-export-get-node-property (property blob &optional inherited)
3724 "Return node PROPERTY value for BLOB.
3726 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3727 element or object.
3729 If optional argument INHERITED is non-nil, the value can be
3730 inherited from a parent headline.
3732 Return value is a string or nil."
3733 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3734 (org-export-get-parent-headline blob))))
3735 (if (not inherited) (org-element-property property blob)
3736 (let ((parent headline) value)
3737 (catch 'found
3738 (while parent
3739 (when (plist-member (nth 1 parent) property)
3740 (throw 'found (org-element-property property parent)))
3741 (setq parent (org-element-property :parent parent))))))))
3743 (defun org-export-get-category (blob info)
3744 "Return category for element or object BLOB.
3746 INFO is a plist used as a communication channel.
3748 CATEGORY is automatically inherited from a parent headline, from
3749 #+CATEGORY: keyword or created out of original file name. If all
3750 fail, the fall-back value is \"???\"."
3751 (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
3752 (org-export-get-parent-headline blob))))
3753 ;; Almost like `org-export-node-property', but we cannot trust
3754 ;; `plist-member' as every headline has a `:CATEGORY'
3755 ;; property, would it be nil or equal to "???" (which has the
3756 ;; same meaning).
3757 (let ((parent headline) value)
3758 (catch 'found
3759 (while parent
3760 (let ((category (org-element-property :CATEGORY parent)))
3761 (and category (not (equal "???" category))
3762 (throw 'found category)))
3763 (setq parent (org-element-property :parent parent))))))
3764 (org-element-map (plist-get info :parse-tree) 'keyword
3765 (lambda (kwd)
3766 (when (equal (org-element-property :key kwd) "CATEGORY")
3767 (org-element-property :value kwd)))
3768 info 'first-match)
3769 (let ((file (plist-get info :input-file)))
3770 (and file (file-name-sans-extension (file-name-nondirectory file))))
3771 "???"))
3773 (defun org-export-get-alt-title (headline info)
3774 "Return alternative title for HEADLINE, as a secondary string.
3775 INFO is a plist used as a communication channel. If no optional
3776 title is defined, fall-back to the regular title."
3777 (or (org-element-property :alt-title headline)
3778 (org-element-property :title headline)))
3780 (defun org-export-first-sibling-p (headline info)
3781 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3782 INFO is a plist used as a communication channel."
3783 (not (eq (org-element-type (org-export-get-previous-element headline info))
3784 'headline)))
3786 (defun org-export-last-sibling-p (headline info)
3787 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3788 INFO is a plist used as a communication channel."
3789 (not (org-export-get-next-element headline info)))
3792 ;;;; For Keywords
3794 ;; `org-export-get-date' returns a date appropriate for the document
3795 ;; to about to be exported. In particular, it takes care of
3796 ;; `org-export-date-timestamp-format'.
3798 (defun org-export-get-date (info &optional fmt)
3799 "Return date value for the current document.
3801 INFO is a plist used as a communication channel. FMT, when
3802 non-nil, is a time format string that will be applied on the date
3803 if it consists in a single timestamp object. It defaults to
3804 `org-export-date-timestamp-format' when nil.
3806 A proper date can be a secondary string, a string or nil. It is
3807 meant to be translated with `org-export-data' or alike."
3808 (let ((date (plist-get info :date))
3809 (fmt (or fmt org-export-date-timestamp-format)))
3810 (cond ((not date) nil)
3811 ((and fmt
3812 (not (cdr date))
3813 (eq (org-element-type (car date)) 'timestamp))
3814 (org-timestamp-format (car date) fmt))
3815 (t date))))
3818 ;;;; For Links
3820 ;; `org-export-solidify-link-text' turns a string into a safer version
3821 ;; for links, replacing most non-standard characters with hyphens.
3823 ;; `org-export-get-coderef-format' returns an appropriate format
3824 ;; string for coderefs.
3826 ;; `org-export-inline-image-p' returns a non-nil value when the link
3827 ;; provided should be considered as an inline image.
3829 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3830 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3831 ;; returns an appropriate unique identifier when found, or nil.
3833 ;; `org-export-resolve-id-link' returns the first headline with
3834 ;; specified id or custom-id in parse tree, the path to the external
3835 ;; file with the id or nil when neither was found.
3837 ;; `org-export-resolve-coderef' associates a reference to a line
3838 ;; number in the element it belongs, or returns the reference itself
3839 ;; when the element isn't numbered.
3841 (defun org-export-solidify-link-text (s)
3842 "Take link text S and make a safe target out of it."
3843 (save-match-data
3844 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
3846 (defun org-export-get-coderef-format (path desc)
3847 "Return format string for code reference link.
3848 PATH is the link path. DESC is its description."
3849 (save-match-data
3850 (cond ((not desc) "%s")
3851 ((string-match (regexp-quote (concat "(" path ")")) desc)
3852 (replace-match "%s" t t desc))
3853 (t desc))))
3855 (defun org-export-inline-image-p (link &optional rules)
3856 "Non-nil if LINK object points to an inline image.
3858 Optional argument is a set of RULES defining inline images. It
3859 is an alist where associations have the following shape:
3861 \(TYPE . REGEXP)
3863 Applying a rule means apply REGEXP against LINK's path when its
3864 type is TYPE. The function will return a non-nil value if any of
3865 the provided rules is non-nil. The default rule is
3866 `org-export-default-inline-image-rule'.
3868 This only applies to links without a description."
3869 (and (not (org-element-contents link))
3870 (let ((case-fold-search t)
3871 (rules (or rules org-export-default-inline-image-rule)))
3872 (catch 'exit
3873 (mapc
3874 (lambda (rule)
3875 (and (string= (org-element-property :type link) (car rule))
3876 (string-match (cdr rule)
3877 (org-element-property :path link))
3878 (throw 'exit t)))
3879 rules)
3880 ;; Return nil if no rule matched.
3881 nil))))
3883 (defun org-export-resolve-coderef (ref info)
3884 "Resolve a code reference REF.
3886 INFO is a plist used as a communication channel.
3888 Return associated line number in source code, or REF itself,
3889 depending on src-block or example element's switches."
3890 (org-element-map (plist-get info :parse-tree) '(example-block src-block)
3891 (lambda (el)
3892 (with-temp-buffer
3893 (insert (org-trim (org-element-property :value el)))
3894 (let* ((label-fmt (regexp-quote
3895 (or (org-element-property :label-fmt el)
3896 org-coderef-label-format)))
3897 (ref-re
3898 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3899 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3900 ;; Element containing REF is found. Resolve it to either
3901 ;; a label or a line number, as needed.
3902 (when (re-search-backward ref-re nil t)
3903 (cond
3904 ((org-element-property :use-labels el) ref)
3905 ((eq (org-element-property :number-lines el) 'continued)
3906 (+ (org-export-get-loc el info) (line-number-at-pos)))
3907 (t (line-number-at-pos)))))))
3908 info 'first-match))
3910 (defun org-export-resolve-fuzzy-link (link info)
3911 "Return LINK destination.
3913 INFO is a plist holding contextual information.
3915 Return value can be an object, an element, or nil:
3917 - If LINK path matches a target object (i.e. <<path>>) return it.
3919 - If LINK path exactly matches the name affiliated keyword
3920 \(i.e. #+NAME: path) of an element, return that element.
3922 - If LINK path exactly matches any headline name, return that
3923 element. If more than one headline share that name, priority
3924 will be given to the one with the closest common ancestor, if
3925 any, or the first one in the parse tree otherwise.
3927 - Otherwise, return nil.
3929 Assume LINK type is \"fuzzy\". White spaces are not
3930 significant."
3931 (let* ((raw-path (org-element-property :path link))
3932 (match-title-p (eq (aref raw-path 0) ?*))
3933 ;; Split PATH at white spaces so matches are space
3934 ;; insensitive.
3935 (path (org-split-string
3936 (if match-title-p (substring raw-path 1) raw-path)))
3937 ;; Cache for destinations that are not position dependent.
3938 (link-cache
3939 (or (plist-get info :resolve-fuzzy-link-cache)
3940 (plist-get (setq info (plist-put info :resolve-fuzzy-link-cache
3941 (make-hash-table :test 'equal)))
3942 :resolve-fuzzy-link-cache)))
3943 (cached (gethash path link-cache 'not-found)))
3944 (cond
3945 ;; Destination is not position dependent: use cached value.
3946 ((and (not match-title-p) (not (eq cached 'not-found))) cached)
3947 ;; First try to find a matching "<<path>>" unless user specified
3948 ;; he was looking for a headline (path starts with a "*"
3949 ;; character).
3950 ((and (not match-title-p)
3951 (let ((match (org-element-map (plist-get info :parse-tree) 'target
3952 (lambda (blob)
3953 (and (equal (org-split-string
3954 (org-element-property :value blob))
3955 path)
3956 blob))
3957 info 'first-match)))
3958 (and match (puthash path match link-cache)))))
3959 ;; Then try to find an element with a matching "#+NAME: path"
3960 ;; affiliated keyword.
3961 ((and (not match-title-p)
3962 (let ((match (org-element-map (plist-get info :parse-tree)
3963 org-element-all-elements
3964 (lambda (el)
3965 (let ((name (org-element-property :name el)))
3966 (when (and name
3967 (equal (org-split-string name) path))
3968 el)))
3969 info 'first-match)))
3970 (and match (puthash path match link-cache)))))
3971 ;; Last case: link either points to a headline or to nothingness.
3972 ;; Try to find the source, with priority given to headlines with
3973 ;; the closest common ancestor. If such candidate is found,
3974 ;; return it, otherwise return nil.
3976 (let ((find-headline
3977 (function
3978 ;; Return first headline whose `:raw-value' property is
3979 ;; NAME in parse tree DATA, or nil. Statistics cookies
3980 ;; are ignored.
3981 (lambda (name data)
3982 (org-element-map data 'headline
3983 (lambda (headline)
3984 (when (equal (org-split-string
3985 (replace-regexp-in-string
3986 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
3987 (org-element-property :raw-value headline)))
3988 name)
3989 headline))
3990 info 'first-match)))))
3991 ;; Search among headlines sharing an ancestor with link, from
3992 ;; closest to farthest.
3993 (catch 'exit
3994 (mapc
3995 (lambda (parent)
3996 (let ((foundp (funcall find-headline path parent)))
3997 (when foundp (throw 'exit foundp))))
3998 (let ((parent-hl (org-export-get-parent-headline link)))
3999 (if (not parent-hl) (list (plist-get info :parse-tree))
4000 (cons parent-hl (org-export-get-genealogy parent-hl)))))
4001 ;; No destination found: return nil.
4002 (and (not match-title-p) (puthash path nil link-cache))))))))
4004 (defun org-export-resolve-id-link (link info)
4005 "Return headline referenced as LINK destination.
4007 INFO is a plist used as a communication channel.
4009 Return value can be the headline element matched in current parse
4010 tree, a file name or nil. Assume LINK type is either \"id\" or
4011 \"custom-id\"."
4012 (let ((id (org-element-property :path link)))
4013 ;; First check if id is within the current parse tree.
4014 (or (org-element-map (plist-get info :parse-tree) 'headline
4015 (lambda (headline)
4016 (when (or (string= (org-element-property :ID headline) id)
4017 (string= (org-element-property :CUSTOM_ID headline) id))
4018 headline))
4019 info 'first-match)
4020 ;; Otherwise, look for external files.
4021 (cdr (assoc id (plist-get info :id-alist))))))
4023 (defun org-export-resolve-radio-link (link info)
4024 "Return radio-target object referenced as LINK destination.
4026 INFO is a plist used as a communication channel.
4028 Return value can be a radio-target object or nil. Assume LINK
4029 has type \"radio\"."
4030 (let ((path (replace-regexp-in-string
4031 "[ \r\t\n]+" " " (org-element-property :path link))))
4032 (org-element-map (plist-get info :parse-tree) 'radio-target
4033 (lambda (radio)
4034 (and (eq (compare-strings
4035 (replace-regexp-in-string
4036 "[ \r\t\n]+" " " (org-element-property :value radio))
4037 nil nil path nil nil t)
4039 radio))
4040 info 'first-match)))
4043 ;;;; For References
4045 ;; `org-export-get-ordinal' associates a sequence number to any object
4046 ;; or element.
4048 (defun org-export-get-ordinal (element info &optional types predicate)
4049 "Return ordinal number of an element or object.
4051 ELEMENT is the element or object considered. INFO is the plist
4052 used as a communication channel.
4054 Optional argument TYPES, when non-nil, is a list of element or
4055 object types, as symbols, that should also be counted in.
4056 Otherwise, only provided element's type is considered.
4058 Optional argument PREDICATE is a function returning a non-nil
4059 value if the current element or object should be counted in. It
4060 accepts two arguments: the element or object being considered and
4061 the plist used as a communication channel. This allows to count
4062 only a certain type of objects (i.e. inline images).
4064 Return value is a list of numbers if ELEMENT is a headline or an
4065 item. It is nil for keywords. It represents the footnote number
4066 for footnote definitions and footnote references. If ELEMENT is
4067 a target, return the same value as if ELEMENT was the closest
4068 table, item or headline containing the target. In any other
4069 case, return the sequence number of ELEMENT among elements or
4070 objects of the same type."
4071 ;; Ordinal of a target object refer to the ordinal of the closest
4072 ;; table, item, or headline containing the object.
4073 (when (eq (org-element-type element) 'target)
4074 (setq element
4075 (loop for parent in (org-export-get-genealogy element)
4076 when
4077 (memq
4078 (org-element-type parent)
4079 '(footnote-definition footnote-reference headline item
4080 table))
4081 return parent)))
4082 (case (org-element-type element)
4083 ;; Special case 1: A headline returns its number as a list.
4084 (headline (org-export-get-headline-number element info))
4085 ;; Special case 2: An item returns its number as a list.
4086 (item (let ((struct (org-element-property :structure element)))
4087 (org-list-get-item-number
4088 (org-element-property :begin element)
4089 struct
4090 (org-list-prevs-alist struct)
4091 (org-list-parents-alist struct))))
4092 ((footnote-definition footnote-reference)
4093 (org-export-get-footnote-number element info))
4094 (otherwise
4095 (let ((counter 0))
4096 ;; Increment counter until ELEMENT is found again.
4097 (org-element-map (plist-get info :parse-tree)
4098 (or types (org-element-type element))
4099 (lambda (el)
4100 (cond
4101 ((eq element el) (1+ counter))
4102 ((not predicate) (incf counter) nil)
4103 ((funcall predicate el info) (incf counter) nil)))
4104 info 'first-match)))))
4107 ;;;; For Src-Blocks
4109 ;; `org-export-get-loc' counts number of code lines accumulated in
4110 ;; src-block or example-block elements with a "+n" switch until
4111 ;; a given element, excluded. Note: "-n" switches reset that count.
4113 ;; `org-export-unravel-code' extracts source code (along with a code
4114 ;; references alist) from an `element-block' or `src-block' type
4115 ;; element.
4117 ;; `org-export-format-code' applies a formatting function to each line
4118 ;; of code, providing relative line number and code reference when
4119 ;; appropriate. Since it doesn't access the original element from
4120 ;; which the source code is coming, it expects from the code calling
4121 ;; it to know if lines should be numbered and if code references
4122 ;; should appear.
4124 ;; Eventually, `org-export-format-code-default' is a higher-level
4125 ;; function (it makes use of the two previous functions) which handles
4126 ;; line numbering and code references inclusion, and returns source
4127 ;; code in a format suitable for plain text or verbatim output.
4129 (defun org-export-get-loc (element info)
4130 "Return accumulated lines of code up to ELEMENT.
4132 INFO is the plist used as a communication channel.
4134 ELEMENT is excluded from count."
4135 (let ((loc 0))
4136 (org-element-map (plist-get info :parse-tree)
4137 `(src-block example-block ,(org-element-type element))
4138 (lambda (el)
4139 (cond
4140 ;; ELEMENT is reached: Quit the loop.
4141 ((eq el element))
4142 ;; Only count lines from src-block and example-block elements
4143 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4144 ((not (memq (org-element-type el) '(src-block example-block))) nil)
4145 ((let ((linums (org-element-property :number-lines el)))
4146 (when linums
4147 ;; Accumulate locs or reset them.
4148 (let ((lines (org-count-lines
4149 (org-trim (org-element-property :value el)))))
4150 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
4151 ;; Return nil to stay in the loop.
4152 nil)))
4153 info 'first-match)
4154 ;; Return value.
4155 loc))
4157 (defun org-export-unravel-code (element)
4158 "Clean source code and extract references out of it.
4160 ELEMENT has either a `src-block' an `example-block' type.
4162 Return a cons cell whose CAR is the source code, cleaned from any
4163 reference and protective comma and CDR is an alist between
4164 relative line number (integer) and name of code reference on that
4165 line (string)."
4166 (let* ((line 0) refs
4167 ;; Get code and clean it. Remove blank lines at its
4168 ;; beginning and end.
4169 (code (replace-regexp-in-string
4170 "\\`\\([ \t]*\n\\)+" ""
4171 (replace-regexp-in-string
4172 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4173 (org-element-property :value element))))
4174 ;; Get format used for references.
4175 (label-fmt (regexp-quote
4176 (or (org-element-property :label-fmt element)
4177 org-coderef-label-format)))
4178 ;; Build a regexp matching a loc with a reference.
4179 (with-ref-re
4180 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4181 (replace-regexp-in-string
4182 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
4183 ;; Return value.
4184 (cons
4185 ;; Code with references removed.
4186 (org-element-normalize-string
4187 (mapconcat
4188 (lambda (loc)
4189 (incf line)
4190 (if (not (string-match with-ref-re loc)) loc
4191 ;; Ref line: remove ref, and signal its position in REFS.
4192 (push (cons line (match-string 3 loc)) refs)
4193 (replace-match "" nil nil loc 1)))
4194 (org-split-string code "\n") "\n"))
4195 ;; Reference alist.
4196 refs)))
4198 (defun org-export-format-code (code fun &optional num-lines ref-alist)
4199 "Format CODE by applying FUN line-wise and return it.
4201 CODE is a string representing the code to format. FUN is
4202 a function. It must accept three arguments: a line of
4203 code (string), the current line number (integer) or nil and the
4204 reference associated to the current line (string) or nil.
4206 Optional argument NUM-LINES can be an integer representing the
4207 number of code lines accumulated until the current code. Line
4208 numbers passed to FUN will take it into account. If it is nil,
4209 FUN's second argument will always be nil. This number can be
4210 obtained with `org-export-get-loc' function.
4212 Optional argument REF-ALIST can be an alist between relative line
4213 number (i.e. ignoring NUM-LINES) and the name of the code
4214 reference on it. If it is nil, FUN's third argument will always
4215 be nil. It can be obtained through the use of
4216 `org-export-unravel-code' function."
4217 (let ((--locs (org-split-string code "\n"))
4218 (--line 0))
4219 (org-element-normalize-string
4220 (mapconcat
4221 (lambda (--loc)
4222 (incf --line)
4223 (let ((--ref (cdr (assq --line ref-alist))))
4224 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
4225 --locs "\n"))))
4227 (defun org-export-format-code-default (element info)
4228 "Return source code from ELEMENT, formatted in a standard way.
4230 ELEMENT is either a `src-block' or `example-block' element. INFO
4231 is a plist used as a communication channel.
4233 This function takes care of line numbering and code references
4234 inclusion. Line numbers, when applicable, appear at the
4235 beginning of the line, separated from the code by two white
4236 spaces. Code references, on the other hand, appear flushed to
4237 the right, separated by six white spaces from the widest line of
4238 code."
4239 ;; Extract code and references.
4240 (let* ((code-info (org-export-unravel-code element))
4241 (code (car code-info))
4242 (code-lines (org-split-string code "\n")))
4243 (if (null code-lines) ""
4244 (let* ((refs (and (org-element-property :retain-labels element)
4245 (cdr code-info)))
4246 ;; Handle line numbering.
4247 (num-start (case (org-element-property :number-lines element)
4248 (continued (org-export-get-loc element info))
4249 (new 0)))
4250 (num-fmt
4251 (and num-start
4252 (format "%%%ds "
4253 (length (number-to-string
4254 (+ (length code-lines) num-start))))))
4255 ;; Prepare references display, if required. Any reference
4256 ;; should start six columns after the widest line of code,
4257 ;; wrapped with parenthesis.
4258 (max-width
4259 (+ (apply 'max (mapcar 'length code-lines))
4260 (if (not num-start) 0 (length (format num-fmt num-start))))))
4261 (org-export-format-code
4262 code
4263 (lambda (loc line-num ref)
4264 (let ((number-str (and num-fmt (format num-fmt line-num))))
4265 (concat
4266 number-str
4268 (and ref
4269 (concat (make-string
4270 (- (+ 6 max-width)
4271 (+ (length loc) (length number-str))) ? )
4272 (format "(%s)" ref))))))
4273 num-start refs)))))
4276 ;;;; For Tables
4278 ;; `org-export-table-has-special-column-p' and and
4279 ;; `org-export-table-row-is-special-p' are predicates used to look for
4280 ;; meta-information about the table structure.
4282 ;; `org-table-has-header-p' tells when the rows before the first rule
4283 ;; should be considered as table's header.
4285 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4286 ;; and `org-export-table-cell-borders' extract information from
4287 ;; a table-cell element.
4289 ;; `org-export-table-dimensions' gives the number on rows and columns
4290 ;; in the table, ignoring horizontal rules and special columns.
4291 ;; `org-export-table-cell-address', given a table-cell object, returns
4292 ;; the absolute address of a cell. On the other hand,
4293 ;; `org-export-get-table-cell-at' does the contrary.
4295 ;; `org-export-table-cell-starts-colgroup-p',
4296 ;; `org-export-table-cell-ends-colgroup-p',
4297 ;; `org-export-table-row-starts-rowgroup-p',
4298 ;; `org-export-table-row-ends-rowgroup-p',
4299 ;; `org-export-table-row-starts-header-p' and
4300 ;; `org-export-table-row-ends-header-p' indicate position of current
4301 ;; row or cell within the table.
4303 (defun org-export-table-has-special-column-p (table)
4304 "Non-nil when TABLE has a special column.
4305 All special columns will be ignored during export."
4306 ;; The table has a special column when every first cell of every row
4307 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4308 ;; "*" "_" and "^". Though, do not consider a first row containing
4309 ;; only empty cells as special.
4310 (let ((special-column-p 'empty))
4311 (catch 'exit
4312 (mapc
4313 (lambda (row)
4314 (when (eq (org-element-property :type row) 'standard)
4315 (let ((value (org-element-contents
4316 (car (org-element-contents row)))))
4317 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4318 (setq special-column-p 'special))
4319 ((not value))
4320 (t (throw 'exit nil))))))
4321 (org-element-contents table))
4322 (eq special-column-p 'special))))
4324 (defun org-export-table-has-header-p (table info)
4325 "Non-nil when TABLE has a header.
4327 INFO is a plist used as a communication channel.
4329 A table has a header when it contains at least two row groups."
4330 (let ((cache (or (plist-get info :table-header-cache)
4331 (plist-get (setq info
4332 (plist-put info :table-header-cache
4333 (make-hash-table :test 'eq)))
4334 :table-header-cache))))
4335 (or (gethash table cache)
4336 (let ((rowgroup 1) row-flag)
4337 (puthash
4338 table
4339 (org-element-map table 'table-row
4340 (lambda (row)
4341 (cond
4342 ((> rowgroup 1) t)
4343 ((and row-flag (eq (org-element-property :type row) 'rule))
4344 (incf rowgroup) (setq row-flag nil))
4345 ((and (not row-flag) (eq (org-element-property :type row)
4346 'standard))
4347 (setq row-flag t) nil)))
4348 info 'first-match)
4349 cache)))))
4351 (defun org-export-table-row-is-special-p (table-row info)
4352 "Non-nil if TABLE-ROW is considered special.
4354 INFO is a plist used as the communication channel.
4356 All special rows will be ignored during export."
4357 (when (eq (org-element-property :type table-row) 'standard)
4358 (let ((first-cell (org-element-contents
4359 (car (org-element-contents table-row)))))
4360 ;; A row is special either when...
4362 ;; ... it starts with a field only containing "/",
4363 (equal first-cell '("/"))
4364 ;; ... the table contains a special column and the row start
4365 ;; with a marking character among, "^", "_", "$" or "!",
4366 (and (org-export-table-has-special-column-p
4367 (org-export-get-parent table-row))
4368 (member first-cell '(("^") ("_") ("$") ("!"))))
4369 ;; ... it contains only alignment cookies and empty cells.
4370 (let ((special-row-p 'empty))
4371 (catch 'exit
4372 (mapc
4373 (lambda (cell)
4374 (let ((value (org-element-contents cell)))
4375 ;; Since VALUE is a secondary string, the following
4376 ;; checks avoid expanding it with `org-export-data'.
4377 (cond ((not value))
4378 ((and (not (cdr value))
4379 (stringp (car value))
4380 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4381 (car value)))
4382 (setq special-row-p 'cookie))
4383 (t (throw 'exit nil)))))
4384 (org-element-contents table-row))
4385 (eq special-row-p 'cookie)))))))
4387 (defun org-export-table-row-group (table-row info)
4388 "Return TABLE-ROW's group number, as an integer.
4390 INFO is a plist used as the communication channel.
4392 Return value is the group number, as an integer, or nil for
4393 special rows and rows separators. First group is also table's
4394 header."
4395 (let ((cache (or (plist-get info :table-row-group-cache)
4396 (plist-get (setq info
4397 (plist-put info :table-row-group-cache
4398 (make-hash-table :test 'eq)))
4399 :table-row-group-cache))))
4400 (cond ((gethash table-row cache))
4401 ((eq (org-element-property :type table-row) 'rule) nil)
4402 (t (let ((group 0) row-flag)
4403 (org-element-map (org-export-get-parent table-row) 'table-row
4404 (lambda (row)
4405 (if (eq (org-element-property :type row) 'rule)
4406 (setq row-flag nil)
4407 (unless row-flag (incf group) (setq row-flag t)))
4408 (when (eq table-row row) (puthash table-row group cache)))
4409 info 'first-match))))))
4411 (defun org-export-table-cell-width (table-cell info)
4412 "Return TABLE-CELL contents width.
4414 INFO is a plist used as the communication channel.
4416 Return value is the width given by the last width cookie in the
4417 same column as TABLE-CELL, or nil."
4418 (let* ((row (org-export-get-parent table-cell))
4419 (table (org-export-get-parent row))
4420 (cells (org-element-contents row))
4421 (columns (length cells))
4422 (column (- columns (length (memq table-cell cells))))
4423 (cache (or (plist-get info :table-cell-width-cache)
4424 (plist-get (setq info
4425 (plist-put info :table-cell-width-cache
4426 (make-hash-table :test 'eq)))
4427 :table-cell-width-cache)))
4428 (width-vector (or (gethash table cache)
4429 (puthash table (make-vector columns 'empty) cache)))
4430 (value (aref width-vector column)))
4431 (if (not (eq value 'empty)) value
4432 (let (cookie-width)
4433 (dolist (row (org-element-contents table)
4434 (aset width-vector column cookie-width))
4435 (when (org-export-table-row-is-special-p row info)
4436 ;; In a special row, try to find a width cookie at COLUMN.
4437 (let* ((value (org-element-contents
4438 (elt (org-element-contents row) column)))
4439 (cookie (car value)))
4440 ;; The following checks avoid expanding unnecessarily
4441 ;; the cell with `org-export-data'.
4442 (when (and value
4443 (not (cdr value))
4444 (stringp cookie)
4445 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
4446 (match-string 1 cookie))
4447 (setq cookie-width
4448 (string-to-number (match-string 1 cookie)))))))))))
4450 (defun org-export-table-cell-alignment (table-cell info)
4451 "Return TABLE-CELL contents alignment.
4453 INFO is a plist used as the communication channel.
4455 Return alignment as specified by the last alignment cookie in the
4456 same column as TABLE-CELL. If no such cookie is found, a default
4457 alignment value will be deduced from fraction of numbers in the
4458 column (see `org-table-number-fraction' for more information).
4459 Possible values are `left', `right' and `center'."
4460 ;; Load `org-table-number-fraction' and `org-table-number-regexp'.
4461 (require 'org-table)
4462 (let* ((row (org-export-get-parent table-cell))
4463 (table (org-export-get-parent row))
4464 (cells (org-element-contents row))
4465 (columns (length cells))
4466 (column (- columns (length (memq table-cell cells))))
4467 (cache (or (plist-get info :table-cell-alignment-cache)
4468 (plist-get (setq info
4469 (plist-put info :table-cell-alignment-cache
4470 (make-hash-table :test 'eq)))
4471 :table-cell-alignment-cache)))
4472 (align-vector (or (gethash table cache)
4473 (puthash table (make-vector columns nil) cache))))
4474 (or (aref align-vector column)
4475 (let ((number-cells 0)
4476 (total-cells 0)
4477 cookie-align
4478 previous-cell-number-p)
4479 (dolist (row (org-element-contents (org-export-get-parent row)))
4480 (cond
4481 ;; In a special row, try to find an alignment cookie at
4482 ;; COLUMN.
4483 ((org-export-table-row-is-special-p row info)
4484 (let ((value (org-element-contents
4485 (elt (org-element-contents row) column))))
4486 ;; Since VALUE is a secondary string, the following
4487 ;; checks avoid useless expansion through
4488 ;; `org-export-data'.
4489 (when (and value
4490 (not (cdr value))
4491 (stringp (car value))
4492 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4493 (car value))
4494 (match-string 1 (car value)))
4495 (setq cookie-align (match-string 1 (car value))))))
4496 ;; Ignore table rules.
4497 ((eq (org-element-property :type row) 'rule))
4498 ;; In a standard row, check if cell's contents are
4499 ;; expressing some kind of number. Increase NUMBER-CELLS
4500 ;; accordingly. Though, don't bother if an alignment
4501 ;; cookie has already defined cell's alignment.
4502 ((not cookie-align)
4503 (let ((value (org-export-data
4504 (org-element-contents
4505 (elt (org-element-contents row) column))
4506 info)))
4507 (incf total-cells)
4508 ;; Treat an empty cell as a number if it follows
4509 ;; a number.
4510 (if (not (or (string-match org-table-number-regexp value)
4511 (and (string= value "") previous-cell-number-p)))
4512 (setq previous-cell-number-p nil)
4513 (setq previous-cell-number-p t)
4514 (incf number-cells))))))
4515 ;; Return value. Alignment specified by cookies has
4516 ;; precedence over alignment deduced from cell's contents.
4517 (aset align-vector
4518 column
4519 (cond ((equal cookie-align "l") 'left)
4520 ((equal cookie-align "r") 'right)
4521 ((equal cookie-align "c") 'center)
4522 ((>= (/ (float number-cells) total-cells)
4523 org-table-number-fraction)
4524 'right)
4525 (t 'left)))))))
4527 (defun org-export-table-cell-borders (table-cell info)
4528 "Return TABLE-CELL borders.
4530 INFO is a plist used as a communication channel.
4532 Return value is a list of symbols, or nil. Possible values are:
4533 `top', `bottom', `above', `below', `left' and `right'. Note:
4534 `top' (resp. `bottom') only happen for a cell in the first
4535 row (resp. last row) of the table, ignoring table rules, if any.
4537 Returned borders ignore special rows."
4538 (let* ((row (org-export-get-parent table-cell))
4539 (table (org-export-get-parent-table table-cell))
4540 borders)
4541 ;; Top/above border? TABLE-CELL has a border above when a rule
4542 ;; used to demarcate row groups can be found above. Hence,
4543 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4544 ;; another regular row has to be found above that rule.
4545 (let (rule-flag)
4546 (catch 'exit
4547 (mapc (lambda (row)
4548 (cond ((eq (org-element-property :type row) 'rule)
4549 (setq rule-flag t))
4550 ((not (org-export-table-row-is-special-p row info))
4551 (if rule-flag (throw 'exit (push 'above borders))
4552 (throw 'exit nil)))))
4553 ;; Look at every row before the current one.
4554 (cdr (memq row (reverse (org-element-contents table)))))
4555 ;; No rule above, or rule found starts the table (ignoring any
4556 ;; special row): TABLE-CELL is at the top of the table.
4557 (when rule-flag (push 'above borders))
4558 (push 'top borders)))
4559 ;; Bottom/below border? TABLE-CELL has a border below when next
4560 ;; non-regular row below is a rule.
4561 (let (rule-flag)
4562 (catch 'exit
4563 (mapc (lambda (row)
4564 (cond ((eq (org-element-property :type row) 'rule)
4565 (setq rule-flag t))
4566 ((not (org-export-table-row-is-special-p row info))
4567 (if rule-flag (throw 'exit (push 'below borders))
4568 (throw 'exit nil)))))
4569 ;; Look at every row after the current one.
4570 (cdr (memq row (org-element-contents table))))
4571 ;; No rule below, or rule found ends the table (modulo some
4572 ;; special row): TABLE-CELL is at the bottom of the table.
4573 (when rule-flag (push 'below borders))
4574 (push 'bottom borders)))
4575 ;; Right/left borders? They can only be specified by column
4576 ;; groups. Column groups are defined in a row starting with "/".
4577 ;; Also a column groups row only contains "<", "<>", ">" or blank
4578 ;; cells.
4579 (catch 'exit
4580 (let ((column (let ((cells (org-element-contents row)))
4581 (- (length cells) (length (memq table-cell cells))))))
4582 (mapc
4583 (lambda (row)
4584 (unless (eq (org-element-property :type row) 'rule)
4585 (when (equal (org-element-contents
4586 (car (org-element-contents row)))
4587 '("/"))
4588 (let ((column-groups
4589 (mapcar
4590 (lambda (cell)
4591 (let ((value (org-element-contents cell)))
4592 (when (member value '(("<") ("<>") (">") nil))
4593 (car value))))
4594 (org-element-contents row))))
4595 ;; There's a left border when previous cell, if
4596 ;; any, ends a group, or current one starts one.
4597 (when (or (and (not (zerop column))
4598 (member (elt column-groups (1- column))
4599 '(">" "<>")))
4600 (member (elt column-groups column) '("<" "<>")))
4601 (push 'left borders))
4602 ;; There's a right border when next cell, if any,
4603 ;; starts a group, or current one ends one.
4604 (when (or (and (/= (1+ column) (length column-groups))
4605 (member (elt column-groups (1+ column))
4606 '("<" "<>")))
4607 (member (elt column-groups column) '(">" "<>")))
4608 (push 'right borders))
4609 (throw 'exit nil)))))
4610 ;; Table rows are read in reverse order so last column groups
4611 ;; row has precedence over any previous one.
4612 (reverse (org-element-contents table)))))
4613 ;; Return value.
4614 borders))
4616 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4617 "Non-nil when TABLE-CELL is at the beginning of a row group.
4618 INFO is a plist used as a communication channel."
4619 ;; A cell starts a column group either when it is at the beginning
4620 ;; of a row (or after the special column, if any) or when it has
4621 ;; a left border.
4622 (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
4623 'identity info 'first-match)
4624 table-cell)
4625 (memq 'left (org-export-table-cell-borders table-cell info))))
4627 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4628 "Non-nil when TABLE-CELL is at the end of a row group.
4629 INFO is a plist used as a communication channel."
4630 ;; A cell ends a column group either when it is at the end of a row
4631 ;; or when it has a right border.
4632 (or (eq (car (last (org-element-contents
4633 (org-export-get-parent table-cell))))
4634 table-cell)
4635 (memq 'right (org-export-table-cell-borders table-cell info))))
4637 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4638 "Non-nil when TABLE-ROW is at the beginning of a column group.
4639 INFO is a plist used as a communication channel."
4640 (unless (or (eq (org-element-property :type table-row) 'rule)
4641 (org-export-table-row-is-special-p table-row info))
4642 (let ((borders (org-export-table-cell-borders
4643 (car (org-element-contents table-row)) info)))
4644 (or (memq 'top borders) (memq 'above borders)))))
4646 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4647 "Non-nil when TABLE-ROW is at the end of a column group.
4648 INFO is a plist used as a communication channel."
4649 (unless (or (eq (org-element-property :type table-row) 'rule)
4650 (org-export-table-row-is-special-p table-row info))
4651 (let ((borders (org-export-table-cell-borders
4652 (car (org-element-contents table-row)) info)))
4653 (or (memq 'bottom borders) (memq 'below borders)))))
4655 (defun org-export-table-row-starts-header-p (table-row info)
4656 "Non-nil when TABLE-ROW is the first table header's row.
4657 INFO is a plist used as a communication channel."
4658 (and (org-export-table-has-header-p
4659 (org-export-get-parent-table table-row) info)
4660 (org-export-table-row-starts-rowgroup-p table-row info)
4661 (= (org-export-table-row-group table-row info) 1)))
4663 (defun org-export-table-row-ends-header-p (table-row info)
4664 "Non-nil when TABLE-ROW is the last table header's row.
4665 INFO is a plist used as a communication channel."
4666 (and (org-export-table-has-header-p
4667 (org-export-get-parent-table table-row) info)
4668 (org-export-table-row-ends-rowgroup-p table-row info)
4669 (= (org-export-table-row-group table-row info) 1)))
4671 (defun org-export-table-row-number (table-row info)
4672 "Return TABLE-ROW number.
4673 INFO is a plist used as a communication channel. Return value is
4674 zero-based and ignores separators. The function returns nil for
4675 special colums and separators."
4676 (when (and (eq (org-element-property :type table-row) 'standard)
4677 (not (org-export-table-row-is-special-p table-row info)))
4678 (let ((number 0))
4679 (org-element-map (org-export-get-parent-table table-row) 'table-row
4680 (lambda (row)
4681 (cond ((eq row table-row) number)
4682 ((eq (org-element-property :type row) 'standard)
4683 (incf number) nil)))
4684 info 'first-match))))
4686 (defun org-export-table-dimensions (table info)
4687 "Return TABLE dimensions.
4689 INFO is a plist used as a communication channel.
4691 Return value is a CONS like (ROWS . COLUMNS) where
4692 ROWS (resp. COLUMNS) is the number of exportable
4693 rows (resp. columns)."
4694 (let (first-row (columns 0) (rows 0))
4695 ;; Set number of rows, and extract first one.
4696 (org-element-map table 'table-row
4697 (lambda (row)
4698 (when (eq (org-element-property :type row) 'standard)
4699 (incf rows)
4700 (unless first-row (setq first-row row)))) info)
4701 ;; Set number of columns.
4702 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4703 ;; Return value.
4704 (cons rows columns)))
4706 (defun org-export-table-cell-address (table-cell info)
4707 "Return address of a regular TABLE-CELL object.
4709 TABLE-CELL is the cell considered. INFO is a plist used as
4710 a communication channel.
4712 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4713 zero-based index. Only exportable cells are considered. The
4714 function returns nil for other cells."
4715 (let* ((table-row (org-export-get-parent table-cell))
4716 (row-number (org-export-table-row-number table-row info)))
4717 (when row-number
4718 (cons row-number
4719 (let ((col-count 0))
4720 (org-element-map table-row 'table-cell
4721 (lambda (cell)
4722 (if (eq cell table-cell) col-count (incf col-count) nil))
4723 info 'first-match))))))
4725 (defun org-export-get-table-cell-at (address table info)
4726 "Return regular table-cell object at ADDRESS in TABLE.
4728 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4729 zero-based index. TABLE is a table type element. INFO is
4730 a plist used as a communication channel.
4732 If no table-cell, among exportable cells, is found at ADDRESS,
4733 return nil."
4734 (let ((column-pos (cdr address)) (column-count 0))
4735 (org-element-map
4736 ;; Row at (car address) or nil.
4737 (let ((row-pos (car address)) (row-count 0))
4738 (org-element-map table 'table-row
4739 (lambda (row)
4740 (cond ((eq (org-element-property :type row) 'rule) nil)
4741 ((= row-count row-pos) row)
4742 (t (incf row-count) nil)))
4743 info 'first-match))
4744 'table-cell
4745 (lambda (cell)
4746 (if (= column-count column-pos) cell
4747 (incf column-count) nil))
4748 info 'first-match)))
4751 ;;;; For Tables Of Contents
4753 ;; `org-export-collect-headlines' builds a list of all exportable
4754 ;; headline elements, maybe limited to a certain depth. One can then
4755 ;; easily parse it and transcode it.
4757 ;; Building lists of tables, figures or listings is quite similar.
4758 ;; Once the generic function `org-export-collect-elements' is defined,
4759 ;; `org-export-collect-tables', `org-export-collect-figures' and
4760 ;; `org-export-collect-listings' can be derived from it.
4762 (defun org-export-collect-headlines (info &optional n)
4763 "Collect headlines in order to build a table of contents.
4765 INFO is a plist used as a communication channel.
4767 When optional argument N is an integer, it specifies the depth of
4768 the table of contents. Otherwise, it is set to the value of the
4769 last headline level. See `org-export-headline-levels' for more
4770 information.
4772 Return a list of all exportable headlines as parsed elements.
4773 Footnote sections, if any, will be ignored."
4774 (let ((limit (plist-get info :headline-levels)))
4775 (setq n (if (wholenump n) (min n limit) limit))
4776 (org-element-map (plist-get info :parse-tree) 'headline
4777 #'(lambda (headline)
4778 (unless (org-element-property :footnote-section-p headline)
4779 (let ((level (org-export-get-relative-level headline info)))
4780 (and (<= level n) headline))))
4781 info)))
4783 (defun org-export-collect-elements (type info &optional predicate)
4784 "Collect referenceable elements of a determined type.
4786 TYPE can be a symbol or a list of symbols specifying element
4787 types to search. Only elements with a caption are collected.
4789 INFO is a plist used as a communication channel.
4791 When non-nil, optional argument PREDICATE is a function accepting
4792 one argument, an element of type TYPE. It returns a non-nil
4793 value when that element should be collected.
4795 Return a list of all elements found, in order of appearance."
4796 (org-element-map (plist-get info :parse-tree) type
4797 (lambda (element)
4798 (and (org-element-property :caption element)
4799 (or (not predicate) (funcall predicate element))
4800 element))
4801 info))
4803 (defun org-export-collect-tables (info)
4804 "Build a list of tables.
4805 INFO is a plist used as a communication channel.
4807 Return a list of table elements with a caption."
4808 (org-export-collect-elements 'table info))
4810 (defun org-export-collect-figures (info predicate)
4811 "Build a list of figures.
4813 INFO is a plist used as a communication channel. PREDICATE is
4814 a function which accepts one argument: a paragraph element and
4815 whose return value is non-nil when that element should be
4816 collected.
4818 A figure is a paragraph type element, with a caption, verifying
4819 PREDICATE. The latter has to be provided since a \"figure\" is
4820 a vague concept that may depend on back-end.
4822 Return a list of elements recognized as figures."
4823 (org-export-collect-elements 'paragraph info predicate))
4825 (defun org-export-collect-listings (info)
4826 "Build a list of src blocks.
4828 INFO is a plist used as a communication channel.
4830 Return a list of src-block elements with a caption."
4831 (org-export-collect-elements 'src-block info))
4834 ;;;; Smart Quotes
4836 ;; The main function for the smart quotes sub-system is
4837 ;; `org-export-activate-smart-quotes', which replaces every quote in
4838 ;; a given string from the parse tree with its "smart" counterpart.
4840 ;; Dictionary for smart quotes is stored in
4841 ;; `org-export-smart-quotes-alist'.
4843 ;; Internally, regexps matching potential smart quotes (checks at
4844 ;; string boundaries are also necessary) are defined in
4845 ;; `org-export-smart-quotes-regexps'.
4847 (defconst org-export-smart-quotes-alist
4848 '(("da"
4849 ;; one may use: »...«, "...", ›...‹, or '...'.
4850 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
4851 ;; LaTeX quotes require Babel!
4852 (opening-double-quote :utf-8 "»" :html "&raquo;" :latex ">>"
4853 :texinfo "@guillemetright{}")
4854 (closing-double-quote :utf-8 "«" :html "&laquo;" :latex "<<"
4855 :texinfo "@guillemetleft{}")
4856 (opening-single-quote :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}"
4857 :texinfo "@guilsinglright{}")
4858 (closing-single-quote :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}"
4859 :texinfo "@guilsingleft{}")
4860 (apostrophe :utf-8 "’" :html "&rsquo;"))
4861 ("de"
4862 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
4863 :texinfo "@quotedblbase{}")
4864 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
4865 :texinfo "@quotedblleft{}")
4866 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
4867 :texinfo "@quotesinglbase{}")
4868 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
4869 :texinfo "@quoteleft{}")
4870 (apostrophe :utf-8 "’" :html "&rsquo;"))
4871 ("en"
4872 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4873 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4874 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4875 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4876 (apostrophe :utf-8 "’" :html "&rsquo;"))
4877 ("es"
4878 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4879 :texinfo "@guillemetleft{}")
4880 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4881 :texinfo "@guillemetright{}")
4882 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4883 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4884 (apostrophe :utf-8 "’" :html "&rsquo;"))
4885 ("fr"
4886 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4887 :texinfo "@guillemetleft{}@tie{}")
4888 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4889 :texinfo "@tie{}@guillemetright{}")
4890 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4891 :texinfo "@guillemetleft{}@tie{}")
4892 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4893 :texinfo "@tie{}@guillemetright{}")
4894 (apostrophe :utf-8 "’" :html "&rsquo;"))
4895 ("no"
4896 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4897 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4898 :texinfo "@guillemetleft{}")
4899 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4900 :texinfo "@guillemetright{}")
4901 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4902 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4903 (apostrophe :utf-8 "’" :html "&rsquo;"))
4904 ("nb"
4905 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4906 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4907 :texinfo "@guillemetleft{}")
4908 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4909 :texinfo "@guillemetright{}")
4910 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4911 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4912 (apostrophe :utf-8 "’" :html "&rsquo;"))
4913 ("nn"
4914 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4915 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4916 :texinfo "@guillemetleft{}")
4917 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4918 :texinfo "@guillemetright{}")
4919 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4920 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4921 (apostrophe :utf-8 "’" :html "&rsquo;"))
4922 ("sv"
4923 ;; based on https://sv.wikipedia.org/wiki/Citattecken
4924 (opening-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
4925 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
4926 (opening-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
4927 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
4928 (apostrophe :utf-8 "’" :html "&rsquo;"))
4930 "Smart quotes translations.
4932 Alist whose CAR is a language string and CDR is an alist with
4933 quote type as key and a plist associating various encodings to
4934 their translation as value.
4936 A quote type can be any symbol among `opening-double-quote',
4937 `closing-double-quote', `opening-single-quote',
4938 `closing-single-quote' and `apostrophe'.
4940 Valid encodings include `:utf-8', `:html', `:latex' and
4941 `:texinfo'.
4943 If no translation is found, the quote character is left as-is.")
4945 (defconst org-export-smart-quotes-regexps
4946 (list
4947 ;; Possible opening quote at beginning of string.
4948 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4949 ;; Possible closing quote at beginning of string.
4950 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4951 ;; Possible apostrophe at beginning of string.
4952 "\\`\\('\\)\\S-"
4953 ;; Opening single and double quotes.
4954 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4955 ;; Closing single and double quotes.
4956 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4957 ;; Apostrophe.
4958 "\\S-\\('\\)\\S-"
4959 ;; Possible opening quote at end of string.
4960 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4961 ;; Possible closing quote at end of string.
4962 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4963 ;; Possible apostrophe at end of string.
4964 "\\S-\\('\\)\\'")
4965 "List of regexps matching a quote or an apostrophe.
4966 In every regexp, quote or apostrophe matched is put in group 1.")
4968 (defun org-export-activate-smart-quotes (s encoding info &optional original)
4969 "Replace regular quotes with \"smart\" quotes in string S.
4971 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
4972 `:utf-8'. INFO is a plist used as a communication channel.
4974 The function has to retrieve information about string
4975 surroundings in parse tree. It can only happen with an
4976 unmodified string. Thus, if S has already been through another
4977 process, a non-nil ORIGINAL optional argument will provide that
4978 original string.
4980 Return the new string."
4981 (if (equal s "") ""
4982 (let* ((prev (org-export-get-previous-element (or original s) info))
4983 ;; Try to be flexible when computing number of blanks
4984 ;; before object. The previous object may be a string
4985 ;; introduced by the back-end and not completely parsed.
4986 (pre-blank (and prev
4987 (or (org-element-property :post-blank prev)
4988 ;; A string with missing `:post-blank'
4989 ;; property.
4990 (and (stringp prev)
4991 (string-match " *\\'" prev)
4992 (length (match-string 0 prev)))
4993 ;; Fallback value.
4994 0)))
4995 (next (org-export-get-next-element (or original s) info))
4996 (get-smart-quote
4997 (lambda (q type)
4998 ;; Return smart quote associated to a give quote Q, as
4999 ;; a string. TYPE is a symbol among `open', `close' and
5000 ;; `apostrophe'.
5001 (let ((key (case type
5002 (apostrophe 'apostrophe)
5003 (open (if (equal "'" q) 'opening-single-quote
5004 'opening-double-quote))
5005 (otherwise (if (equal "'" q) 'closing-single-quote
5006 'closing-double-quote)))))
5007 (or (plist-get
5008 (cdr (assq key
5009 (cdr (assoc (plist-get info :language)
5010 org-export-smart-quotes-alist))))
5011 encoding)
5012 q)))))
5013 (if (or (equal "\"" s) (equal "'" s))
5014 ;; Only a quote: no regexp can match. We have to check both
5015 ;; sides and decide what to do.
5016 (cond ((and (not prev) (not next)) s)
5017 ((not prev) (funcall get-smart-quote s 'open))
5018 ((and (not next) (zerop pre-blank))
5019 (funcall get-smart-quote s 'close))
5020 ((not next) s)
5021 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
5022 (t (funcall get-smart-quote 'open)))
5023 ;; 1. Replace quote character at the beginning of S.
5024 (cond
5025 ;; Apostrophe?
5026 ((and prev (zerop pre-blank)
5027 (string-match (nth 2 org-export-smart-quotes-regexps) s))
5028 (setq s (replace-match
5029 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
5030 nil t s 1)))
5031 ;; Closing quote?
5032 ((and prev (zerop pre-blank)
5033 (string-match (nth 1 org-export-smart-quotes-regexps) s))
5034 (setq s (replace-match
5035 (funcall get-smart-quote (match-string 1 s) 'close)
5036 nil t s 1)))
5037 ;; Opening quote?
5038 ((and (or (not prev) (> pre-blank 0))
5039 (string-match (nth 0 org-export-smart-quotes-regexps) s))
5040 (setq s (replace-match
5041 (funcall get-smart-quote (match-string 1 s) 'open)
5042 nil t s 1))))
5043 ;; 2. Replace quotes in the middle of the string.
5044 (setq s (replace-regexp-in-string
5045 ;; Opening quotes.
5046 (nth 3 org-export-smart-quotes-regexps)
5047 (lambda (text)
5048 (funcall get-smart-quote (match-string 1 text) 'open))
5049 s nil t 1))
5050 (setq s (replace-regexp-in-string
5051 ;; Closing quotes.
5052 (nth 4 org-export-smart-quotes-regexps)
5053 (lambda (text)
5054 (funcall get-smart-quote (match-string 1 text) 'close))
5055 s nil t 1))
5056 (setq s (replace-regexp-in-string
5057 ;; Apostrophes.
5058 (nth 5 org-export-smart-quotes-regexps)
5059 (lambda (text)
5060 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
5061 s nil t 1))
5062 ;; 3. Replace quote character at the end of S.
5063 (cond
5064 ;; Apostrophe?
5065 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
5066 (setq s (replace-match
5067 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
5068 nil t s 1)))
5069 ;; Closing quote?
5070 ((and (not next)
5071 (string-match (nth 7 org-export-smart-quotes-regexps) s))
5072 (setq s (replace-match
5073 (funcall get-smart-quote (match-string 1 s) 'close)
5074 nil t s 1)))
5075 ;; Opening quote?
5076 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
5077 (setq s (replace-match
5078 (funcall get-smart-quote (match-string 1 s) 'open)
5079 nil t s 1))))
5080 ;; Return string with smart quotes.
5081 s))))
5083 ;;;; Topology
5085 ;; Here are various functions to retrieve information about the
5086 ;; neighbourhood of a given element or object. Neighbours of interest
5087 ;; are direct parent (`org-export-get-parent'), parent headline
5088 ;; (`org-export-get-parent-headline'), first element containing an
5089 ;; object, (`org-export-get-parent-element'), parent table
5090 ;; (`org-export-get-parent-table'), previous element or object
5091 ;; (`org-export-get-previous-element') and next element or object
5092 ;; (`org-export-get-next-element').
5094 ;; `org-export-get-genealogy' returns the full genealogy of a given
5095 ;; element or object, from closest parent to full parse tree.
5097 (defsubst org-export-get-parent (blob)
5098 "Return BLOB parent or nil.
5099 BLOB is the element or object considered."
5100 (org-element-property :parent blob))
5102 (defun org-export-get-genealogy (blob)
5103 "Return full genealogy relative to a given element or object.
5105 BLOB is the element or object being considered.
5107 Ancestors are returned from closest to farthest, the last one
5108 being the full parse tree."
5109 (let (genealogy (parent blob))
5110 (while (setq parent (org-element-property :parent parent))
5111 (push parent genealogy))
5112 (nreverse genealogy)))
5114 (defun org-export-get-parent-headline (blob)
5115 "Return BLOB parent headline or nil.
5116 BLOB is the element or object being considered."
5117 (let ((parent blob))
5118 (while (and (setq parent (org-element-property :parent parent))
5119 (not (eq (org-element-type parent) 'headline))))
5120 parent))
5122 (defun org-export-get-parent-element (object)
5123 "Return first element containing OBJECT or nil.
5124 OBJECT is the object to consider."
5125 (let ((parent object))
5126 (while (and (setq parent (org-element-property :parent parent))
5127 (memq (org-element-type parent) org-element-all-objects)))
5128 parent))
5130 (defun org-export-get-parent-table (object)
5131 "Return OBJECT parent table or nil.
5132 OBJECT is either a `table-cell' or `table-element' type object."
5133 (let ((parent object))
5134 (while (and (setq parent (org-element-property :parent parent))
5135 (not (eq (org-element-type parent) 'table))))
5136 parent))
5138 (defun org-export-get-previous-element (blob info &optional n)
5139 "Return previous element or object.
5141 BLOB is an element or object. INFO is a plist used as
5142 a communication channel. Return previous exportable element or
5143 object, a string, or nil.
5145 When optional argument N is a positive integer, return a list
5146 containing up to N siblings before BLOB, from farthest to
5147 closest. With any other non-nil value, return a list containing
5148 all of them."
5149 (let ((siblings
5150 ;; An object can belong to the contents of its parent or
5151 ;; to a secondary string. We check the latter option
5152 ;; first.
5153 (let ((parent (org-export-get-parent blob)))
5154 (or (and (not (memq (org-element-type blob)
5155 org-element-all-elements))
5156 (let ((sec-value
5157 (org-element-property
5158 (cdr (assq (org-element-type parent)
5159 org-element-secondary-value-alist))
5160 parent)))
5161 (and (memq blob sec-value) sec-value)))
5162 (org-element-contents parent))))
5163 prev)
5164 (catch 'exit
5165 (mapc (lambda (obj)
5166 (cond ((memq obj (plist-get info :ignore-list)))
5167 ((null n) (throw 'exit obj))
5168 ((not (wholenump n)) (push obj prev))
5169 ((zerop n) (throw 'exit prev))
5170 (t (decf n) (push obj prev))))
5171 (cdr (memq blob (reverse siblings))))
5172 prev)))
5174 (defun org-export-get-next-element (blob info &optional n)
5175 "Return next element or object.
5177 BLOB is an element or object. INFO is a plist used as
5178 a communication channel. Return next exportable element or
5179 object, a string, or nil.
5181 When optional argument N is a positive integer, return a list
5182 containing up to N siblings after BLOB, from closest to farthest.
5183 With any other non-nil value, return a list containing all of
5184 them."
5185 (let ((siblings
5186 ;; An object can belong to the contents of its parent or to
5187 ;; a secondary string. We check the latter option first.
5188 (let ((parent (org-export-get-parent blob)))
5189 (or (and (not (memq (org-element-type blob)
5190 org-element-all-objects))
5191 (let ((sec-value
5192 (org-element-property
5193 (cdr (assq (org-element-type parent)
5194 org-element-secondary-value-alist))
5195 parent)))
5196 (cdr (memq blob sec-value))))
5197 (cdr (memq blob (org-element-contents parent))))))
5198 next)
5199 (catch 'exit
5200 (mapc (lambda (obj)
5201 (cond ((memq obj (plist-get info :ignore-list)))
5202 ((null n) (throw 'exit obj))
5203 ((not (wholenump n)) (push obj next))
5204 ((zerop n) (throw 'exit (nreverse next)))
5205 (t (decf n) (push obj next))))
5206 siblings)
5207 (nreverse next))))
5210 ;;;; Translation
5212 ;; `org-export-translate' translates a string according to the language
5213 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5214 ;; the dictionary used for the translation.
5216 (defconst org-export-dictionary
5217 '(("%e %n: %c"
5218 ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
5219 ("Author"
5220 ("ca" :default "Autor")
5221 ("cs" :default "Autor")
5222 ("da" :default "Forfatter")
5223 ("de" :default "Autor")
5224 ("eo" :html "A&#365;toro")
5225 ("es" :default "Autor")
5226 ("fi" :html "Tekij&auml;")
5227 ("fr" :default "Auteur")
5228 ("hu" :default "Szerz&otilde;")
5229 ("is" :html "H&ouml;fundur")
5230 ("it" :default "Autore")
5231 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
5232 ("nl" :default "Auteur")
5233 ("no" :default "Forfatter")
5234 ("nb" :default "Forfatter")
5235 ("nn" :default "Forfattar")
5236 ("pl" :default "Autor")
5237 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5238 ("sv" :html "F&ouml;rfattare")
5239 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5240 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
5241 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
5242 ("Date"
5243 ("ca" :default "Data")
5244 ("cs" :default "Datum")
5245 ("da" :default "Dato")
5246 ("de" :default "Datum")
5247 ("eo" :default "Dato")
5248 ("es" :default "Fecha")
5249 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
5250 ("hu" :html "D&aacute;tum")
5251 ("is" :default "Dagsetning")
5252 ("it" :default "Data")
5253 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
5254 ("nl" :default "Datum")
5255 ("no" :default "Dato")
5256 ("nb" :default "Dato")
5257 ("nn" :default "Dato")
5258 ("pl" :default "Data")
5259 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5260 ("sv" :default "Datum")
5261 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5262 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
5263 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
5264 ("Equation"
5265 ("da" :default "Ligning")
5266 ("de" :default "Gleichung")
5267 ("es" :html "Ecuaci&oacute;n" :default "Ecuación")
5268 ("fr" :ascii "Equation" :default "Équation")
5269 ("no" :default "Ligning")
5270 ("nb" :default "Ligning")
5271 ("nn" :default "Likning")
5272 ("sv" :default "Ekvation")
5273 ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
5274 ("Figure"
5275 ("da" :default "Figur")
5276 ("de" :default "Abbildung")
5277 ("es" :default "Figura")
5278 ("ja" :html "&#22259;" :utf-8 "図")
5279 ("no" :default "Illustrasjon")
5280 ("nb" :default "Illustrasjon")
5281 ("nn" :default "Illustrasjon")
5282 ("sv" :default "Illustration")
5283 ("zh-CN" :html "&#22270;" :utf-8 "图"))
5284 ("Figure %d:"
5285 ("da" :default "Figur %d")
5286 ("de" :default "Abbildung %d:")
5287 ("es" :default "Figura %d:")
5288 ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
5289 ("ja" :html "&#22259;%d: " :utf-8 "図%d: ")
5290 ("no" :default "Illustrasjon %d")
5291 ("nb" :default "Illustrasjon %d")
5292 ("nn" :default "Illustrasjon %d")
5293 ("sv" :default "Illustration %d")
5294 ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
5295 ("Footnotes"
5296 ("ca" :html "Peus de p&agrave;gina")
5297 ("cs" :default "Pozn\xe1mky pod carou")
5298 ("da" :default "Fodnoter")
5299 ("de" :html "Fu&szlig;noten" :default "Fußnoten")
5300 ("eo" :default "Piednotoj")
5301 ("es" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
5302 ("fi" :default "Alaviitteet")
5303 ("fr" :default "Notes de bas de page")
5304 ("hu" :html "L&aacute;bjegyzet")
5305 ("is" :html "Aftanm&aacute;lsgreinar")
5306 ("it" :html "Note a pi&egrave; di pagina")
5307 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
5308 ("nl" :default "Voetnoten")
5309 ("no" :default "Fotnoter")
5310 ("nb" :default "Fotnoter")
5311 ("nn" :default "Fotnotar")
5312 ("pl" :default "Przypis")
5313 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
5314 ("sv" :default "Fotnoter")
5315 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
5316 :utf-8 "Примітки")
5317 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
5318 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
5319 ("List of Listings"
5320 ("da" :default "Programmer")
5321 ("de" :default "Programmauflistungsverzeichnis")
5322 ("es" :default "Indice de Listados de programas")
5323 ("fr" :default "Liste des programmes")
5324 ("no" :default "Dataprogrammer")
5325 ("nb" :default "Dataprogrammer")
5326 ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
5327 ("List of Tables"
5328 ("da" :default "Tabeller")
5329 ("de" :default "Tabellenverzeichnis")
5330 ("es" :default "Indice de tablas")
5331 ("fr" :default "Liste des tableaux")
5332 ("no" :default "Tabeller")
5333 ("nb" :default "Tabeller")
5334 ("nn" :default "Tabeller")
5335 ("sv" :default "Tabeller")
5336 ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
5337 ("Listing %d:"
5338 ("da" :default "Program %d")
5339 ("de" :default "Programmlisting %d")
5340 ("es" :default "Listado de programa %d")
5341 ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
5342 ("no" :default "Dataprogram")
5343 ("nb" :default "Dataprogram")
5344 ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
5345 ("See section %s"
5346 ("da" :default "jævnfør afsnit %s")
5347 ("de" :default "siehe Abschnitt %s")
5348 ("es" :default "vea seccion %s")
5349 ("fr" :default "cf. section %s")
5350 ("zh-CN" :html "&#21442;&#35265;&#31532;%d&#33410;" :utf-8 "参见第%s节"))
5351 ("Table"
5352 ("de" :default "Tabelle")
5353 ("es" :default "Tabla")
5354 ("fr" :default "Tableau")
5355 ("ja" :html "&#34920;" :utf-8 "表")
5356 ("zh-CN" :html "&#34920;" :utf-8 "表"))
5357 ("Table %d:"
5358 ("da" :default "Tabel %d")
5359 ("de" :default "Tabelle %d")
5360 ("es" :default "Tabla %d")
5361 ("fr" :default "Tableau %d :")
5362 ("ja" :html "&#34920;%d:" :utf-8 "表%d:")
5363 ("no" :default "Tabell %d")
5364 ("nb" :default "Tabell %d")
5365 ("nn" :default "Tabell %d")
5366 ("sv" :default "Tabell %d")
5367 ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
5368 ("Table of Contents"
5369 ("ca" :html "&Iacute;ndex")
5370 ("cs" :default "Obsah")
5371 ("da" :default "Indhold")
5372 ("de" :default "Inhaltsverzeichnis")
5373 ("eo" :default "Enhavo")
5374 ("es" :html "&Iacute;ndice")
5375 ("fi" :html "Sis&auml;llysluettelo")
5376 ("fr" :ascii "Sommaire" :default "Table des matières")
5377 ("hu" :html "Tartalomjegyz&eacute;k")
5378 ("is" :default "Efnisyfirlit")
5379 ("it" :default "Indice")
5380 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
5381 ("nl" :default "Inhoudsopgave")
5382 ("no" :default "Innhold")
5383 ("nb" :default "Innhold")
5384 ("nn" :default "Innhald")
5385 ("pl" :html "Spis tre&#x015b;ci")
5386 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
5387 :utf-8 "Содержание")
5388 ("sv" :html "Inneh&aring;ll")
5389 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
5390 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
5391 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
5392 ("Unknown reference"
5393 ("da" :default "ukendt reference")
5394 ("de" :default "Unbekannter Verweis")
5395 ("es" :default "referencia desconocida")
5396 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
5397 ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
5398 "Dictionary for export engine.
5400 Alist whose CAR is the string to translate and CDR is an alist
5401 whose CAR is the language string and CDR is a plist whose
5402 properties are possible charsets and values translated terms.
5404 It is used as a database for `org-export-translate'. Since this
5405 function returns the string as-is if no translation was found,
5406 the variable only needs to record values different from the
5407 entry.")
5409 (defun org-export-translate (s encoding info)
5410 "Translate string S according to language specification.
5412 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5413 and `:utf-8'. INFO is a plist used as a communication channel.
5415 Translation depends on `:language' property. Return the
5416 translated string. If no translation is found, try to fall back
5417 to `:default' encoding. If it fails, return S."
5418 (let* ((lang (plist-get info :language))
5419 (translations (cdr (assoc lang
5420 (cdr (assoc s org-export-dictionary))))))
5421 (or (plist-get translations encoding)
5422 (plist-get translations :default)
5423 s)))
5427 ;;; Asynchronous Export
5429 ;; `org-export-async-start' is the entry point for asynchronous
5430 ;; export. It recreates current buffer (including visibility,
5431 ;; narrowing and visited file) in an external Emacs process, and
5432 ;; evaluates a command there. It then applies a function on the
5433 ;; returned results in the current process.
5435 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5436 ;; allow to export to a buffer or a file, asynchronously or not.
5438 ;; `org-export-output-file-name' is an auxiliary function meant to be
5439 ;; used with `org-export-to-file'. With a given extension, it tries
5440 ;; to provide a canonical file name to write export output to.
5442 ;; Asynchronously generated results are never displayed directly.
5443 ;; Instead, they are stored in `org-export-stack-contents'. They can
5444 ;; then be retrieved by calling `org-export-stack'.
5446 ;; Export Stack is viewed through a dedicated major mode
5447 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5448 ;;`org-export-stack-delete', `org-export-stack-view' and
5449 ;;`org-export-stack-clear'.
5451 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5452 ;; It should be used whenever `org-export-async-start' is called.
5454 (defmacro org-export-async-start (fun &rest body)
5455 "Call function FUN on the results returned by BODY evaluation.
5457 BODY evaluation happens in an asynchronous process, from a buffer
5458 which is an exact copy of the current one.
5460 Use `org-export-add-to-stack' in FUN in order to register results
5461 in the stack.
5463 This is a low level function. See also `org-export-to-buffer'
5464 and `org-export-to-file' for more specialized functions."
5465 (declare (indent 1) (debug t))
5466 (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
5467 ;; Write the full sexp evaluating BODY in a copy of the current
5468 ;; buffer to a temporary file, as it may be too long for program
5469 ;; args in `start-process'.
5470 `(with-temp-message "Initializing asynchronous export process"
5471 (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
5472 (,temp-file (make-temp-file "org-export-process"))
5473 (,coding buffer-file-coding-system))
5474 (with-temp-file ,temp-file
5475 (insert
5476 ;; Null characters (from variable values) are inserted
5477 ;; within the file. As a consequence, coding system for
5478 ;; buffer contents will not be recognized properly. So,
5479 ;; we make sure it is the same as the one used to display
5480 ;; the original buffer.
5481 (format ";; -*- coding: %s; -*-\n%S"
5482 ,coding
5483 `(with-temp-buffer
5484 (when org-export-async-debug '(setq debug-on-error t))
5485 ;; Ignore `kill-emacs-hook' and code evaluation
5486 ;; queries from Babel as we need a truly
5487 ;; non-interactive process.
5488 (setq kill-emacs-hook nil
5489 org-babel-confirm-evaluate-answer-no t)
5490 ;; Initialize export framework.
5491 (require 'ox)
5492 ;; Re-create current buffer there.
5493 (funcall ,,copy-fun)
5494 (restore-buffer-modified-p nil)
5495 ;; Sexp to evaluate in the buffer.
5496 (print (progn ,,@body))))))
5497 ;; Start external process.
5498 (let* ((process-connection-type nil)
5499 (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
5500 (,process
5501 (start-process
5502 "org-export-process" ,proc-buffer
5503 (expand-file-name invocation-name invocation-directory)
5504 "-Q" "--batch"
5505 "-l" org-export-async-init-file
5506 "-l" ,temp-file)))
5507 ;; Register running process in stack.
5508 (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
5509 ;; Set-up sentinel in order to catch results.
5510 (let ((handler ,fun))
5511 (set-process-sentinel
5512 ,process
5513 `(lambda (p status)
5514 (let ((proc-buffer (process-buffer p)))
5515 (when (eq (process-status p) 'exit)
5516 (unwind-protect
5517 (if (zerop (process-exit-status p))
5518 (unwind-protect
5519 (let ((results
5520 (with-current-buffer proc-buffer
5521 (goto-char (point-max))
5522 (backward-sexp)
5523 (read (current-buffer)))))
5524 (funcall ,handler results))
5525 (unless org-export-async-debug
5526 (and (get-buffer proc-buffer)
5527 (kill-buffer proc-buffer))))
5528 (org-export-add-to-stack proc-buffer nil p)
5529 (ding)
5530 (message "Process '%s' exited abnormally" p))
5531 (unless org-export-async-debug
5532 (delete-file ,,temp-file)))))))))))))
5534 ;;;###autoload
5535 (defun org-export-to-buffer
5536 (backend buffer
5537 &optional async subtreep visible-only body-only ext-plist
5538 post-process)
5539 "Call `org-export-as' with output to a specified buffer.
5541 BACKEND is either an export back-end, as returned by, e.g.,
5542 `org-export-create-backend', or a symbol referring to
5543 a registered back-end.
5545 BUFFER is the name of the output buffer. If it already exists,
5546 it will be erased first, otherwise, it will be created.
5548 A non-nil optional argument ASYNC means the process should happen
5549 asynchronously. The resulting buffer should then be accessible
5550 through the `org-export-stack' interface. When ASYNC is nil, the
5551 buffer is displayed if `org-export-show-temporary-export-buffer'
5552 is non-nil.
5554 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5555 EXT-PLIST are similar to those used in `org-export-as', which
5556 see.
5558 Optional argument POST-PROCESS is a function which should accept
5559 no argument. It is always called within the current process,
5560 from BUFFER, with point at its beginning. Export back-ends can
5561 use it to set a major mode there, e.g,
5563 \(defun org-latex-export-as-latex
5564 \(&optional async subtreep visible-only body-only ext-plist)
5565 \(interactive)
5566 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5567 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5569 This function returns BUFFER."
5570 (declare (indent 2))
5571 (if async
5572 (org-export-async-start
5573 `(lambda (output)
5574 (with-current-buffer (get-buffer-create ,buffer)
5575 (erase-buffer)
5576 (setq buffer-file-coding-system ',buffer-file-coding-system)
5577 (insert output)
5578 (goto-char (point-min))
5579 (org-export-add-to-stack (current-buffer) ',backend)
5580 (ignore-errors (funcall ,post-process))))
5581 `(org-export-as
5582 ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
5583 (let ((output
5584 (org-export-as backend subtreep visible-only body-only ext-plist))
5585 (buffer (get-buffer-create buffer))
5586 (encoding buffer-file-coding-system))
5587 (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
5588 (org-kill-new output))
5589 (with-current-buffer buffer
5590 (erase-buffer)
5591 (setq buffer-file-coding-system encoding)
5592 (insert output)
5593 (goto-char (point-min))
5594 (and (functionp post-process) (funcall post-process)))
5595 (when org-export-show-temporary-export-buffer
5596 (switch-to-buffer-other-window buffer))
5597 buffer)))
5599 ;;;###autoload
5600 (defun org-export-to-file
5601 (backend file &optional async subtreep visible-only body-only ext-plist
5602 post-process)
5603 "Call `org-export-as' with output to a specified file.
5605 BACKEND is either an export back-end, as returned by, e.g.,
5606 `org-export-create-backend', or a symbol referring to
5607 a registered back-end. FILE is the name of the output file, as
5608 a string.
5610 A non-nil optional argument ASYNC means the process should happen
5611 asynchronously. The resulting buffer file then be accessible
5612 through the `org-export-stack' interface.
5614 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5615 EXT-PLIST are similar to those used in `org-export-as', which
5616 see.
5618 Optional argument POST-PROCESS is called with FILE as its
5619 argument and happens asynchronously when ASYNC is non-nil. It
5620 has to return a file name, or nil. Export back-ends can use this
5621 to send the output file through additional processing, e.g,
5623 \(defun org-latex-export-to-latex
5624 \(&optional async subtreep visible-only body-only ext-plist)
5625 \(interactive)
5626 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5627 \(org-export-to-file 'latex outfile
5628 async subtreep visible-only body-only ext-plist
5629 \(lambda (file) (org-latex-compile file)))
5631 The function returns either a file name returned by POST-PROCESS,
5632 or FILE."
5633 (declare (indent 2))
5634 (if (not (file-writable-p file)) (error "Output file not writable")
5635 (let ((encoding (or org-export-coding-system buffer-file-coding-system)))
5636 (if async
5637 (org-export-async-start
5638 `(lambda (file)
5639 (org-export-add-to-stack (expand-file-name file) ',backend))
5640 `(let ((output
5641 (org-export-as
5642 ',backend ,subtreep ,visible-only ,body-only
5643 ',ext-plist)))
5644 (with-temp-buffer
5645 (insert output)
5646 (let ((coding-system-for-write ',encoding))
5647 (write-file ,file)))
5648 (or (ignore-errors (funcall ',post-process ,file)) ,file)))
5649 (let ((output (org-export-as
5650 backend subtreep visible-only body-only ext-plist)))
5651 (with-temp-buffer
5652 (insert output)
5653 (let ((coding-system-for-write encoding))
5654 (write-file file)))
5655 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
5656 (org-kill-new output))
5657 ;; Get proper return value.
5658 (or (and (functionp post-process) (funcall post-process file))
5659 file))))))
5661 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
5662 "Return output file's name according to buffer specifications.
5664 EXTENSION is a string representing the output file extension,
5665 with the leading dot.
5667 With a non-nil optional argument SUBTREEP, try to determine
5668 output file's name by looking for \"EXPORT_FILE_NAME\" property
5669 of subtree at point.
5671 When optional argument PUB-DIR is set, use it as the publishing
5672 directory.
5674 When optional argument VISIBLE-ONLY is non-nil, don't export
5675 contents of hidden elements.
5677 Return file name as a string."
5678 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5679 (base-name
5680 ;; File name may come from EXPORT_FILE_NAME subtree
5681 ;; property, assuming point is at beginning of said
5682 ;; sub-tree.
5683 (file-name-sans-extension
5684 (or (and subtreep
5685 (org-entry-get
5686 (save-excursion
5687 (ignore-errors (org-back-to-heading) (point)))
5688 "EXPORT_FILE_NAME" t))
5689 ;; File name may be extracted from buffer's associated
5690 ;; file, if any.
5691 (and visited-file (file-name-nondirectory visited-file))
5692 ;; Can't determine file name on our own: Ask user.
5693 (let ((read-file-name-function
5694 (and org-completion-use-ido 'ido-read-file-name)))
5695 (read-file-name
5696 "Output file: " pub-dir nil nil nil
5697 (lambda (name)
5698 (string= (file-name-extension name t) extension)))))))
5699 (output-file
5700 ;; Build file name. Enforce EXTENSION over whatever user
5701 ;; may have come up with. PUB-DIR, if defined, always has
5702 ;; precedence over any provided path.
5703 (cond
5704 (pub-dir
5705 (concat (file-name-as-directory pub-dir)
5706 (file-name-nondirectory base-name)
5707 extension))
5708 ((file-name-absolute-p base-name) (concat base-name extension))
5709 (t (concat (file-name-as-directory ".") base-name extension)))))
5710 ;; If writing to OUTPUT-FILE would overwrite original file, append
5711 ;; EXTENSION another time to final name.
5712 (if (and visited-file (org-file-equal-p visited-file output-file))
5713 (concat output-file extension)
5714 output-file)))
5716 (defun org-export-add-to-stack (source backend &optional process)
5717 "Add a new result to export stack if not present already.
5719 SOURCE is a buffer or a file name containing export results.
5720 BACKEND is a symbol representing export back-end used to generate
5723 Entries already pointing to SOURCE and unavailable entries are
5724 removed beforehand. Return the new stack."
5725 (setq org-export-stack-contents
5726 (cons (list source backend (or process (current-time)))
5727 (org-export-stack-remove source))))
5729 (defun org-export-stack ()
5730 "Menu for asynchronous export results and running processes."
5731 (interactive)
5732 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5733 (set-buffer buffer)
5734 (when (zerop (buffer-size)) (org-export-stack-mode))
5735 (org-export-stack-refresh)
5736 (pop-to-buffer buffer))
5737 (message "Type \"q\" to quit, \"?\" for help"))
5739 (defun org-export--stack-source-at-point ()
5740 "Return source from export results at point in stack."
5741 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
5742 (if (not source) (error "Source unavailable, please refresh buffer")
5743 (let ((source-name (if (stringp source) source (buffer-name source))))
5744 (if (save-excursion
5745 (beginning-of-line)
5746 (looking-at (concat ".* +" (regexp-quote source-name) "$")))
5747 source
5748 ;; SOURCE is not consistent with current line. The stack
5749 ;; view is outdated.
5750 (error "Source unavailable; type `g' to update buffer"))))))
5752 (defun org-export-stack-clear ()
5753 "Remove all entries from export stack."
5754 (interactive)
5755 (setq org-export-stack-contents nil))
5757 (defun org-export-stack-refresh (&rest dummy)
5758 "Refresh the asynchronous export stack.
5759 DUMMY is ignored. Unavailable sources are removed from the list.
5760 Return the new stack."
5761 (let ((inhibit-read-only t))
5762 (org-preserve-lc
5763 (erase-buffer)
5764 (insert (concat
5765 (let ((counter 0))
5766 (mapconcat
5767 (lambda (entry)
5768 (let ((proc-p (processp (nth 2 entry))))
5769 (concat
5770 ;; Back-end.
5771 (format " %-12s " (or (nth 1 entry) ""))
5772 ;; Age.
5773 (let ((data (nth 2 entry)))
5774 (if proc-p (format " %6s " (process-status data))
5775 ;; Compute age of the results.
5776 (org-format-seconds
5777 "%4h:%.2m "
5778 (float-time (time-since data)))))
5779 ;; Source.
5780 (format " %s"
5781 (let ((source (car entry)))
5782 (if (stringp source) source
5783 (buffer-name source)))))))
5784 ;; Clear stack from exited processes, dead buffers or
5785 ;; non-existent files.
5786 (setq org-export-stack-contents
5787 (org-remove-if-not
5788 (lambda (el)
5789 (if (processp (nth 2 el))
5790 (buffer-live-p (process-buffer (nth 2 el)))
5791 (let ((source (car el)))
5792 (if (bufferp source) (buffer-live-p source)
5793 (file-exists-p source)))))
5794 org-export-stack-contents)) "\n")))))))
5796 (defun org-export-stack-remove (&optional source)
5797 "Remove export results at point from stack.
5798 If optional argument SOURCE is non-nil, remove it instead."
5799 (interactive)
5800 (let ((source (or source (org-export--stack-source-at-point))))
5801 (setq org-export-stack-contents
5802 (org-remove-if (lambda (el) (equal (car el) source))
5803 org-export-stack-contents))))
5805 (defun org-export-stack-view (&optional in-emacs)
5806 "View export results at point in stack.
5807 With an optional prefix argument IN-EMACS, force viewing files
5808 within Emacs."
5809 (interactive "P")
5810 (let ((source (org-export--stack-source-at-point)))
5811 (cond ((processp source)
5812 (org-switch-to-buffer-other-window (process-buffer source)))
5813 ((bufferp source) (org-switch-to-buffer-other-window source))
5814 (t (org-open-file source in-emacs)))))
5816 (defvar org-export-stack-mode-map
5817 (let ((km (make-sparse-keymap)))
5818 (define-key km " " 'next-line)
5819 (define-key km "n" 'next-line)
5820 (define-key km "\C-n" 'next-line)
5821 (define-key km [down] 'next-line)
5822 (define-key km "p" 'previous-line)
5823 (define-key km "\C-p" 'previous-line)
5824 (define-key km "\C-?" 'previous-line)
5825 (define-key km [up] 'previous-line)
5826 (define-key km "C" 'org-export-stack-clear)
5827 (define-key km "v" 'org-export-stack-view)
5828 (define-key km (kbd "RET") 'org-export-stack-view)
5829 (define-key km "d" 'org-export-stack-remove)
5831 "Keymap for Org Export Stack.")
5833 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5834 "Mode for displaying asynchronous export stack.
5836 Type \\[org-export-stack] to visualize the asynchronous export
5837 stack.
5839 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
5840 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
5841 stack completely.
5843 Removing entries in an Org Export Stack buffer doesn't affect
5844 files or buffers, only the display.
5846 \\{org-export-stack-mode-map}"
5847 (abbrev-mode 0)
5848 (auto-fill-mode 0)
5849 (setq buffer-read-only t
5850 buffer-undo-list t
5851 truncate-lines t
5852 header-line-format
5853 '(:eval
5854 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
5855 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
5856 (set (make-local-variable 'revert-buffer-function)
5857 'org-export-stack-refresh))
5861 ;;; The Dispatcher
5863 ;; `org-export-dispatch' is the standard interactive way to start an
5864 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
5865 ;; for its interface, which, in turn, delegates response to key
5866 ;; pressed to `org-export--dispatch-action'.
5868 ;;;###autoload
5869 (defun org-export-dispatch (&optional arg)
5870 "Export dispatcher for Org mode.
5872 It provides an access to common export related tasks in a buffer.
5873 Its interface comes in two flavours: standard and expert.
5875 While both share the same set of bindings, only the former
5876 displays the valid keys associations in a dedicated buffer.
5877 Scrolling (resp. line-wise motion) in this buffer is done with
5878 SPC and DEL (resp. C-n and C-p) keys.
5880 Set variable `org-export-dispatch-use-expert-ui' to switch to one
5881 flavour or the other.
5883 When ARG is \\[universal-argument], repeat the last export action, with the same set
5884 of options used back then, on the current buffer.
5886 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
5887 (interactive "P")
5888 (let* ((input
5889 (cond ((equal arg '(16)) '(stack))
5890 ((and arg org-export-dispatch-last-action))
5891 (t (save-window-excursion
5892 (unwind-protect
5893 (progn
5894 ;; Remember where we are
5895 (move-marker org-export-dispatch-last-position
5896 (point)
5897 (org-base-buffer (current-buffer)))
5898 ;; Get and store an export command
5899 (setq org-export-dispatch-last-action
5900 (org-export--dispatch-ui
5901 (list org-export-initial-scope
5902 (and org-export-in-background 'async))
5904 org-export-dispatch-use-expert-ui)))
5905 (and (get-buffer "*Org Export Dispatcher*")
5906 (kill-buffer "*Org Export Dispatcher*")))))))
5907 (action (car input))
5908 (optns (cdr input)))
5909 (unless (memq 'subtree optns)
5910 (move-marker org-export-dispatch-last-position nil))
5911 (case action
5912 ;; First handle special hard-coded actions.
5913 (template (org-export-insert-default-template nil optns))
5914 (stack (org-export-stack))
5915 (publish-current-file
5916 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
5917 (publish-current-project
5918 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
5919 (publish-choose-project
5920 (org-publish (assoc (org-icompleting-read
5921 "Publish project: "
5922 org-publish-project-alist nil t)
5923 org-publish-project-alist)
5924 (memq 'force optns)
5925 (memq 'async optns)))
5926 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
5927 (otherwise
5928 (save-excursion
5929 (when arg
5930 ;; Repeating command, maybe move cursor to restore subtree
5931 ;; context.
5932 (if (eq (marker-buffer org-export-dispatch-last-position)
5933 (org-base-buffer (current-buffer)))
5934 (goto-char org-export-dispatch-last-position)
5935 ;; We are in a different buffer, forget position.
5936 (move-marker org-export-dispatch-last-position nil)))
5937 (funcall action
5938 ;; Return a symbol instead of a list to ease
5939 ;; asynchronous export macro use.
5940 (and (memq 'async optns) t)
5941 (and (memq 'subtree optns) t)
5942 (and (memq 'visible optns) t)
5943 (and (memq 'body optns) t)))))))
5945 (defun org-export--dispatch-ui (options first-key expertp)
5946 "Handle interface for `org-export-dispatch'.
5948 OPTIONS is a list containing current interactive options set for
5949 export. It can contain any of the following symbols:
5950 `body' toggles a body-only export
5951 `subtree' restricts export to current subtree
5952 `visible' restricts export to visible part of buffer.
5953 `force' force publishing files.
5954 `async' use asynchronous export process
5956 FIRST-KEY is the key pressed to select the first level menu. It
5957 is nil when this menu hasn't been selected yet.
5959 EXPERTP, when non-nil, triggers expert UI. In that case, no help
5960 buffer is provided, but indications about currently active
5961 options are given in the prompt. Moreover, \[?] allows to switch
5962 back to standard interface."
5963 (let* ((fontify-key
5964 (lambda (key &optional access-key)
5965 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
5966 ;; non-nil is the required first-level key to activate
5967 ;; KEY. When its value is t, activate KEY independently
5968 ;; on the first key, if any. A nil value means KEY will
5969 ;; only be activated at first level.
5970 (if (or (eq access-key t) (eq access-key first-key))
5971 (org-propertize key 'face 'org-warning)
5972 key)))
5973 (fontify-value
5974 (lambda (value)
5975 ;; Fontify VALUE string.
5976 (org-propertize value 'face 'font-lock-variable-name-face)))
5977 ;; Prepare menu entries by extracting them from registered
5978 ;; back-ends and sorting them by access key and by ordinal,
5979 ;; if any.
5980 (entries
5981 (sort (sort (delq nil
5982 (mapcar 'org-export-backend-menu
5983 org-export--registered-backends))
5984 (lambda (a b)
5985 (let ((key-a (nth 1 a))
5986 (key-b (nth 1 b)))
5987 (cond ((and (numberp key-a) (numberp key-b))
5988 (< key-a key-b))
5989 ((numberp key-b) t)))))
5990 'car-less-than-car))
5991 ;; Compute a list of allowed keys based on the first key
5992 ;; pressed, if any. Some keys
5993 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
5994 ;; available.
5995 (allowed-keys
5996 (nconc (list 2 22 19 6 1)
5997 (if (not first-key) (org-uniquify (mapcar 'car entries))
5998 (let (sub-menu)
5999 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
6000 (when (eq (car entry) first-key)
6001 (setq sub-menu (append (nth 2 entry) sub-menu))))))
6002 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
6003 ((not first-key) (list ?P)))
6004 (list ?& ?#)
6005 (when expertp (list ??))
6006 (list ?q)))
6007 ;; Build the help menu for standard UI.
6008 (help
6009 (unless expertp
6010 (concat
6011 ;; Options are hard-coded.
6012 (format "[%s] Body only: %s [%s] Visible only: %s
6013 \[%s] Export scope: %s [%s] Force publishing: %s
6014 \[%s] Async export: %s\n\n"
6015 (funcall fontify-key "C-b" t)
6016 (funcall fontify-value
6017 (if (memq 'body options) "On " "Off"))
6018 (funcall fontify-key "C-v" t)
6019 (funcall fontify-value
6020 (if (memq 'visible options) "On " "Off"))
6021 (funcall fontify-key "C-s" t)
6022 (funcall fontify-value
6023 (if (memq 'subtree options) "Subtree" "Buffer "))
6024 (funcall fontify-key "C-f" t)
6025 (funcall fontify-value
6026 (if (memq 'force options) "On " "Off"))
6027 (funcall fontify-key "C-a" t)
6028 (funcall fontify-value
6029 (if (memq 'async options) "On " "Off")))
6030 ;; Display registered back-end entries. When a key
6031 ;; appears for the second time, do not create another
6032 ;; entry, but append its sub-menu to existing menu.
6033 (let (last-key)
6034 (mapconcat
6035 (lambda (entry)
6036 (let ((top-key (car entry)))
6037 (concat
6038 (unless (eq top-key last-key)
6039 (setq last-key top-key)
6040 (format "\n[%s] %s\n"
6041 (funcall fontify-key (char-to-string top-key))
6042 (nth 1 entry)))
6043 (let ((sub-menu (nth 2 entry)))
6044 (unless (functionp sub-menu)
6045 ;; Split sub-menu into two columns.
6046 (let ((index -1))
6047 (concat
6048 (mapconcat
6049 (lambda (sub-entry)
6050 (incf index)
6051 (format
6052 (if (zerop (mod index 2)) " [%s] %-26s"
6053 "[%s] %s\n")
6054 (funcall fontify-key
6055 (char-to-string (car sub-entry))
6056 top-key)
6057 (nth 1 sub-entry)))
6058 sub-menu "")
6059 (when (zerop (mod index 2)) "\n"))))))))
6060 entries ""))
6061 ;; Publishing menu is hard-coded.
6062 (format "\n[%s] Publish
6063 [%s] Current file [%s] Current project
6064 [%s] Choose project [%s] All projects\n\n\n"
6065 (funcall fontify-key "P")
6066 (funcall fontify-key "f" ?P)
6067 (funcall fontify-key "p" ?P)
6068 (funcall fontify-key "x" ?P)
6069 (funcall fontify-key "a" ?P))
6070 (format "[%s] Export stack [%s] Insert template\n"
6071 (funcall fontify-key "&" t)
6072 (funcall fontify-key "#" t))
6073 (format "[%s] %s"
6074 (funcall fontify-key "q" t)
6075 (if first-key "Main menu" "Exit")))))
6076 ;; Build prompts for both standard and expert UI.
6077 (standard-prompt (unless expertp "Export command: "))
6078 (expert-prompt
6079 (when expertp
6080 (format
6081 "Export command (C-%s%s%s%s%s) [%s]: "
6082 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6083 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6084 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6085 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6086 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6087 (mapconcat (lambda (k)
6088 ;; Strip control characters.
6089 (unless (< k 27) (char-to-string k)))
6090 allowed-keys "")))))
6091 ;; With expert UI, just read key with a fancy prompt. In standard
6092 ;; UI, display an intrusive help buffer.
6093 (if expertp
6094 (org-export--dispatch-action
6095 expert-prompt allowed-keys entries options first-key expertp)
6096 ;; At first call, create frame layout in order to display menu.
6097 (unless (get-buffer "*Org Export Dispatcher*")
6098 (delete-other-windows)
6099 (org-switch-to-buffer-other-window
6100 (get-buffer-create "*Org Export Dispatcher*"))
6101 (setq cursor-type nil
6102 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6103 ;; Make sure that invisible cursor will not highlight square
6104 ;; brackets.
6105 (set-syntax-table (copy-syntax-table))
6106 (modify-syntax-entry ?\[ "w"))
6107 ;; At this point, the buffer containing the menu exists and is
6108 ;; visible in the current window. So, refresh it.
6109 (with-current-buffer "*Org Export Dispatcher*"
6110 ;; Refresh help. Maintain display continuity by re-visiting
6111 ;; previous window position.
6112 (let ((pos (window-start)))
6113 (erase-buffer)
6114 (insert help)
6115 (set-window-start nil pos)))
6116 (org-fit-window-to-buffer)
6117 (org-export--dispatch-action
6118 standard-prompt allowed-keys entries options first-key expertp))))
6120 (defun org-export--dispatch-action
6121 (prompt allowed-keys entries options first-key expertp)
6122 "Read a character from command input and act accordingly.
6124 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6125 a list of characters available at a given step in the process.
6126 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6127 EXPERTP are the same as defined in `org-export--dispatch-ui',
6128 which see.
6130 Toggle export options when required. Otherwise, return value is
6131 a list with action as CAR and a list of interactive export
6132 options as CDR."
6133 (let (key)
6134 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6135 ;; C-p, SPC, DEL).
6136 (while (and (setq key (read-char-exclusive prompt))
6137 (not expertp)
6138 (memq key '(14 16 ?\s ?\d)))
6139 (case key
6140 (14 (if (not (pos-visible-in-window-p (point-max)))
6141 (ignore-errors (scroll-up 1))
6142 (message "End of buffer")
6143 (sit-for 1)))
6144 (16 (if (not (pos-visible-in-window-p (point-min)))
6145 (ignore-errors (scroll-down 1))
6146 (message "Beginning of buffer")
6147 (sit-for 1)))
6148 (?\s (if (not (pos-visible-in-window-p (point-max)))
6149 (scroll-up nil)
6150 (message "End of buffer")
6151 (sit-for 1)))
6152 (?\d (if (not (pos-visible-in-window-p (point-min)))
6153 (scroll-down nil)
6154 (message "Beginning of buffer")
6155 (sit-for 1)))))
6156 (cond
6157 ;; Ignore undefined associations.
6158 ((not (memq key allowed-keys))
6159 (ding)
6160 (unless expertp (message "Invalid key") (sit-for 1))
6161 (org-export--dispatch-ui options first-key expertp))
6162 ;; q key at first level aborts export. At second level, cancel
6163 ;; first key instead.
6164 ((eq key ?q) (if (not first-key) (error "Export aborted")
6165 (org-export--dispatch-ui options nil expertp)))
6166 ;; Help key: Switch back to standard interface if expert UI was
6167 ;; active.
6168 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6169 ;; Send request for template insertion along with export scope.
6170 ((eq key ?#) (cons 'template (memq 'subtree options)))
6171 ;; Switch to asynchronous export stack.
6172 ((eq key ?&) '(stack))
6173 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6174 ((memq key '(2 22 19 6 1))
6175 (org-export--dispatch-ui
6176 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6177 (6 'force) (1 'async))))
6178 (if (memq option options) (remq option options)
6179 (cons option options)))
6180 first-key expertp))
6181 ;; Action selected: Send key and options back to
6182 ;; `org-export-dispatch'.
6183 ((or first-key (functionp (nth 2 (assq key entries))))
6184 (cons (cond
6185 ((not first-key) (nth 2 (assq key entries)))
6186 ;; Publishing actions are hard-coded. Send a special
6187 ;; signal to `org-export-dispatch'.
6188 ((eq first-key ?P)
6189 (case key
6190 (?f 'publish-current-file)
6191 (?p 'publish-current-project)
6192 (?x 'publish-choose-project)
6193 (?a 'publish-all)))
6194 ;; Return first action associated to FIRST-KEY + KEY
6195 ;; path. Indeed, derived backends can share the same
6196 ;; FIRST-KEY.
6197 (t (catch 'found
6198 (mapc (lambda (entry)
6199 (let ((match (assq key (nth 2 entry))))
6200 (when match (throw 'found (nth 2 match)))))
6201 (member (assq first-key entries) entries)))))
6202 options))
6203 ;; Otherwise, enter sub-menu.
6204 (t (org-export--dispatch-ui options key expertp)))))
6208 (provide 'ox)
6210 ;; Local variables:
6211 ;; generated-autoload-file: "org-loaddefs.el"
6212 ;; End:
6214 ;;; ox.el ends here