Merge branch 'maint'
[org-mode.git] / lisp / ox.el
blob9d5b69d8c3e2f0ae1d18be567ab0bda8fdfa21ee
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 :translate-alist (org-export-get-all-transcoders backend)
2254 ;; Size of the hash table is reduced since this function
2255 ;; will probably be used on short trees.
2256 :exported-data (make-hash-table :test 'eq :size 401)))))
2258 (defun org-export--interpret-p (blob info)
2259 "Non-nil if element or object BLOB should be interpreted during export.
2260 If nil, BLOB will appear as raw Org syntax. Check is done
2261 according to export options INFO, stored as a plist."
2262 (case (org-element-type blob)
2263 ;; ... entities...
2264 (entity (plist-get info :with-entities))
2265 ;; ... emphasis...
2266 ((bold italic strike-through underline)
2267 (plist-get info :with-emphasize))
2268 ;; ... fixed-width areas.
2269 (fixed-width (plist-get info :with-fixed-width))
2270 ;; ... LaTeX environments and fragments...
2271 ((latex-environment latex-fragment)
2272 (let ((with-latex-p (plist-get info :with-latex)))
2273 (and with-latex-p (not (eq with-latex-p 'verbatim)))))
2274 ;; ... sub/superscripts...
2275 ((subscript superscript)
2276 (let ((sub/super-p (plist-get info :with-sub-superscript)))
2277 (if (eq sub/super-p '{})
2278 (org-element-property :use-brackets-p blob)
2279 sub/super-p)))
2280 ;; ... tables...
2281 (table (plist-get info :with-tables))
2282 (otherwise t)))
2284 (defun org-export-expand (blob contents &optional with-affiliated)
2285 "Expand a parsed element or object to its original state.
2287 BLOB is either an element or an object. CONTENTS is its
2288 contents, as a string or nil.
2290 When optional argument WITH-AFFILIATED is non-nil, add affiliated
2291 keywords before output."
2292 (let ((type (org-element-type blob)))
2293 (concat (and with-affiliated (memq type org-element-all-elements)
2294 (org-element--interpret-affiliated-keywords blob))
2295 (funcall (intern (format "org-element-%s-interpreter" type))
2296 blob contents))))
2298 (defun org-export-ignore-element (element info)
2299 "Add ELEMENT to `:ignore-list' in INFO.
2301 Any element in `:ignore-list' will be skipped when using
2302 `org-element-map'. INFO is modified by side effects."
2303 (plist-put info :ignore-list (cons element (plist-get info :ignore-list))))
2307 ;;; The Filter System
2309 ;; Filters allow end-users to tweak easily the transcoded output.
2310 ;; They are the functional counterpart of hooks, as every filter in
2311 ;; a set is applied to the return value of the previous one.
2313 ;; Every set is back-end agnostic. Although, a filter is always
2314 ;; called, in addition to the string it applies to, with the back-end
2315 ;; used as argument, so it's easy for the end-user to add back-end
2316 ;; specific filters in the set. The communication channel, as
2317 ;; a plist, is required as the third argument.
2319 ;; From the developer side, filters sets can be installed in the
2320 ;; process with the help of `org-export-define-backend', which
2321 ;; internally stores filters as an alist. Each association has a key
2322 ;; among the following symbols and a function or a list of functions
2323 ;; as value.
2325 ;; - `:filter-options' applies to the property list containing export
2326 ;; options. Unlike to other filters, functions in this list accept
2327 ;; two arguments instead of three: the property list containing
2328 ;; export options and the back-end. Users can set its value through
2329 ;; `org-export-filter-options-functions' variable.
2331 ;; - `:filter-parse-tree' applies directly to the complete parsed
2332 ;; tree. Users can set it through
2333 ;; `org-export-filter-parse-tree-functions' variable.
2335 ;; - `:filter-final-output' applies to the final transcoded string.
2336 ;; Users can set it with `org-export-filter-final-output-functions'
2337 ;; variable
2339 ;; - `:filter-plain-text' applies to any string not recognized as Org
2340 ;; syntax. `org-export-filter-plain-text-functions' allows users to
2341 ;; configure it.
2343 ;; - `:filter-TYPE' applies on the string returned after an element or
2344 ;; object of type TYPE has been transcoded. A user can modify
2345 ;; `org-export-filter-TYPE-functions'
2347 ;; All filters sets are applied with
2348 ;; `org-export-filter-apply-functions' function. Filters in a set are
2349 ;; applied in a LIFO fashion. It allows developers to be sure that
2350 ;; their filters will be applied first.
2352 ;; Filters properties are installed in communication channel with
2353 ;; `org-export-install-filters' function.
2355 ;; Eventually, two hooks (`org-export-before-processing-hook' and
2356 ;; `org-export-before-parsing-hook') are run at the beginning of the
2357 ;; export process and just before parsing to allow for heavy structure
2358 ;; modifications.
2361 ;;;; Hooks
2363 (defvar org-export-before-processing-hook nil
2364 "Hook run at the beginning of the export process.
2366 This is run before include keywords and macros are expanded and
2367 Babel code blocks executed, on a copy of the original buffer
2368 being exported. Visibility and narrowing are preserved. Point
2369 is at the beginning of the buffer.
2371 Every function in this hook will be called with one argument: the
2372 back-end currently used, as a symbol.")
2374 (defvar org-export-before-parsing-hook nil
2375 "Hook run before parsing an export buffer.
2377 This is run after include keywords and macros have been expanded
2378 and Babel code blocks executed, on a copy of the original buffer
2379 being exported. Visibility and narrowing are preserved. Point
2380 is at the beginning of the buffer.
2382 Every function in this hook will be called with one argument: the
2383 back-end currently used, as a symbol.")
2386 ;;;; Special Filters
2388 (defvar org-export-filter-options-functions nil
2389 "List of functions applied to the export options.
2390 Each filter is called with two arguments: the export options, as
2391 a plist, and the back-end, as a symbol. It must return
2392 a property list containing export options.")
2394 (defvar org-export-filter-parse-tree-functions nil
2395 "List of functions applied to the parsed tree.
2396 Each filter is called with three arguments: the parse tree, as
2397 returned by `org-element-parse-buffer', the back-end, as
2398 a symbol, and the communication channel, as a plist. It must
2399 return the modified parse tree to transcode.")
2401 (defvar org-export-filter-plain-text-functions nil
2402 "List of functions applied to plain text.
2403 Each filter is called with three arguments: a string which
2404 contains no Org syntax, the back-end, as a symbol, and the
2405 communication channel, as a plist. It must return a string or
2406 nil.")
2408 (defvar org-export-filter-final-output-functions nil
2409 "List of functions applied to the transcoded string.
2410 Each filter is called with three arguments: the full transcoded
2411 string, the back-end, as a symbol, and the communication channel,
2412 as a plist. It must return a string that will be used as the
2413 final export output.")
2416 ;;;; Elements Filters
2418 (defvar org-export-filter-babel-call-functions nil
2419 "List of functions applied to a transcoded babel-call.
2420 Each filter is called with three arguments: the transcoded data,
2421 as a string, the back-end, as a symbol, and the communication
2422 channel, as a plist. It must return a string or nil.")
2424 (defvar org-export-filter-center-block-functions nil
2425 "List of functions applied to a transcoded center block.
2426 Each filter is called with three arguments: the transcoded data,
2427 as a string, the back-end, as a symbol, and the communication
2428 channel, as a plist. It must return a string or nil.")
2430 (defvar org-export-filter-clock-functions nil
2431 "List of functions applied to a transcoded clock.
2432 Each filter is called with three arguments: the transcoded data,
2433 as a string, the back-end, as a symbol, and the communication
2434 channel, as a plist. It must return a string or nil.")
2436 (defvar org-export-filter-comment-functions nil
2437 "List of functions applied to a transcoded comment.
2438 Each filter is called with three arguments: the transcoded data,
2439 as a string, the back-end, as a symbol, and the communication
2440 channel, as a plist. It must return a string or nil.")
2442 (defvar org-export-filter-comment-block-functions nil
2443 "List of functions applied to a transcoded comment-block.
2444 Each filter is called with three arguments: the transcoded data,
2445 as a string, the back-end, as a symbol, and the communication
2446 channel, as a plist. It must return a string or nil.")
2448 (defvar org-export-filter-diary-sexp-functions nil
2449 "List of functions applied to a transcoded diary-sexp.
2450 Each filter is called with three arguments: the transcoded data,
2451 as a string, the back-end, as a symbol, and the communication
2452 channel, as a plist. It must return a string or nil.")
2454 (defvar org-export-filter-drawer-functions nil
2455 "List of functions applied to a transcoded drawer.
2456 Each filter is called with three arguments: the transcoded data,
2457 as a string, the back-end, as a symbol, and the communication
2458 channel, as a plist. It must return a string or nil.")
2460 (defvar org-export-filter-dynamic-block-functions nil
2461 "List of functions applied to a transcoded dynamic-block.
2462 Each filter is called with three arguments: the transcoded data,
2463 as a string, the back-end, as a symbol, and the communication
2464 channel, as a plist. It must return a string or nil.")
2466 (defvar org-export-filter-example-block-functions nil
2467 "List of functions applied to a transcoded example-block.
2468 Each filter is called with three arguments: the transcoded data,
2469 as a string, the back-end, as a symbol, and the communication
2470 channel, as a plist. It must return a string or nil.")
2472 (defvar org-export-filter-export-block-functions nil
2473 "List of functions applied to a transcoded export-block.
2474 Each filter is called with three arguments: the transcoded data,
2475 as a string, the back-end, as a symbol, and the communication
2476 channel, as a plist. It must return a string or nil.")
2478 (defvar org-export-filter-fixed-width-functions nil
2479 "List of functions applied to a transcoded fixed-width.
2480 Each filter is called with three arguments: the transcoded data,
2481 as a string, the back-end, as a symbol, and the communication
2482 channel, as a plist. It must return a string or nil.")
2484 (defvar org-export-filter-footnote-definition-functions nil
2485 "List of functions applied to a transcoded footnote-definition.
2486 Each filter is called with three arguments: the transcoded data,
2487 as a string, the back-end, as a symbol, and the communication
2488 channel, as a plist. It must return a string or nil.")
2490 (defvar org-export-filter-headline-functions nil
2491 "List of functions applied to a transcoded headline.
2492 Each filter is called with three arguments: the transcoded data,
2493 as a string, the back-end, as a symbol, and the communication
2494 channel, as a plist. It must return a string or nil.")
2496 (defvar org-export-filter-horizontal-rule-functions nil
2497 "List of functions applied to a transcoded horizontal-rule.
2498 Each filter is called with three arguments: the transcoded data,
2499 as a string, the back-end, as a symbol, and the communication
2500 channel, as a plist. It must return a string or nil.")
2502 (defvar org-export-filter-inlinetask-functions nil
2503 "List of functions applied to a transcoded inlinetask.
2504 Each filter is called with three arguments: the transcoded data,
2505 as a string, the back-end, as a symbol, and the communication
2506 channel, as a plist. It must return a string or nil.")
2508 (defvar org-export-filter-item-functions nil
2509 "List of functions applied to a transcoded item.
2510 Each filter is called with three arguments: the transcoded data,
2511 as a string, the back-end, as a symbol, and the communication
2512 channel, as a plist. It must return a string or nil.")
2514 (defvar org-export-filter-keyword-functions nil
2515 "List of functions applied to a transcoded keyword.
2516 Each filter is called with three arguments: the transcoded data,
2517 as a string, the back-end, as a symbol, and the communication
2518 channel, as a plist. It must return a string or nil.")
2520 (defvar org-export-filter-latex-environment-functions nil
2521 "List of functions applied to a transcoded latex-environment.
2522 Each filter is called with three arguments: the transcoded data,
2523 as a string, the back-end, as a symbol, and the communication
2524 channel, as a plist. It must return a string or nil.")
2526 (defvar org-export-filter-node-property-functions nil
2527 "List of functions applied to a transcoded node-property.
2528 Each filter is called with three arguments: the transcoded data,
2529 as a string, the back-end, as a symbol, and the communication
2530 channel, as a plist. It must return a string or nil.")
2532 (defvar org-export-filter-paragraph-functions nil
2533 "List of functions applied to a transcoded paragraph.
2534 Each filter is called with three arguments: the transcoded data,
2535 as a string, the back-end, as a symbol, and the communication
2536 channel, as a plist. It must return a string or nil.")
2538 (defvar org-export-filter-plain-list-functions nil
2539 "List of functions applied to a transcoded plain-list.
2540 Each filter is called with three arguments: the transcoded data,
2541 as a string, the back-end, as a symbol, and the communication
2542 channel, as a plist. It must return a string or nil.")
2544 (defvar org-export-filter-planning-functions nil
2545 "List of functions applied to a transcoded planning.
2546 Each filter is called with three arguments: the transcoded data,
2547 as a string, the back-end, as a symbol, and the communication
2548 channel, as a plist. It must return a string or nil.")
2550 (defvar org-export-filter-property-drawer-functions nil
2551 "List of functions applied to a transcoded property-drawer.
2552 Each filter is called with three arguments: the transcoded data,
2553 as a string, the back-end, as a symbol, and the communication
2554 channel, as a plist. It must return a string or nil.")
2556 (defvar org-export-filter-quote-block-functions nil
2557 "List of functions applied to a transcoded quote block.
2558 Each filter is called with three arguments: the transcoded quote
2559 data, as a string, the back-end, as a symbol, and the
2560 communication channel, as a plist. It must return a string or
2561 nil.")
2563 (defvar org-export-filter-quote-section-functions nil
2564 "List of functions applied to a transcoded quote-section.
2565 Each filter is called with three arguments: the transcoded data,
2566 as a string, the back-end, as a symbol, and the communication
2567 channel, as a plist. It must return a string or nil.")
2569 (defvar org-export-filter-section-functions nil
2570 "List of functions applied to a transcoded section.
2571 Each filter is called with three arguments: the transcoded data,
2572 as a string, the back-end, as a symbol, and the communication
2573 channel, as a plist. It must return a string or nil.")
2575 (defvar org-export-filter-special-block-functions nil
2576 "List of functions applied to a transcoded special block.
2577 Each filter is called with three arguments: the transcoded data,
2578 as a string, the back-end, as a symbol, and the communication
2579 channel, as a plist. It must return a string or nil.")
2581 (defvar org-export-filter-src-block-functions nil
2582 "List of functions applied to a transcoded src-block.
2583 Each filter is called with three arguments: the transcoded data,
2584 as a string, the back-end, as a symbol, and the communication
2585 channel, as a plist. It must return a string or nil.")
2587 (defvar org-export-filter-table-functions nil
2588 "List of functions applied to a transcoded table.
2589 Each filter is called with three arguments: the transcoded data,
2590 as a string, the back-end, as a symbol, and the communication
2591 channel, as a plist. It must return a string or nil.")
2593 (defvar org-export-filter-table-cell-functions nil
2594 "List of functions applied to a transcoded table-cell.
2595 Each filter is called with three arguments: the transcoded data,
2596 as a string, the back-end, as a symbol, and the communication
2597 channel, as a plist. It must return a string or nil.")
2599 (defvar org-export-filter-table-row-functions nil
2600 "List of functions applied to a transcoded table-row.
2601 Each filter is called with three arguments: the transcoded data,
2602 as a string, the back-end, as a symbol, and the communication
2603 channel, as a plist. It must return a string or nil.")
2605 (defvar org-export-filter-verse-block-functions nil
2606 "List of functions applied to a transcoded verse block.
2607 Each filter is called with three arguments: the transcoded data,
2608 as a string, the back-end, as a symbol, and the communication
2609 channel, as a plist. It must return a string or nil.")
2612 ;;;; Objects Filters
2614 (defvar org-export-filter-bold-functions nil
2615 "List of functions applied to transcoded bold text.
2616 Each filter is called with three arguments: the transcoded data,
2617 as a string, the back-end, as a symbol, and the communication
2618 channel, as a plist. It must return a string or nil.")
2620 (defvar org-export-filter-code-functions nil
2621 "List of functions applied to transcoded code text.
2622 Each filter is called with three arguments: the transcoded data,
2623 as a string, the back-end, as a symbol, and the communication
2624 channel, as a plist. It must return a string or nil.")
2626 (defvar org-export-filter-entity-functions nil
2627 "List of functions applied to a transcoded entity.
2628 Each filter is called with three arguments: the transcoded data,
2629 as a string, the back-end, as a symbol, and the communication
2630 channel, as a plist. It must return a string or nil.")
2632 (defvar org-export-filter-export-snippet-functions nil
2633 "List of functions applied to a transcoded export-snippet.
2634 Each filter is called with three arguments: the transcoded data,
2635 as a string, the back-end, as a symbol, and the communication
2636 channel, as a plist. It must return a string or nil.")
2638 (defvar org-export-filter-footnote-reference-functions nil
2639 "List of functions applied to a transcoded footnote-reference.
2640 Each filter is called with three arguments: the transcoded data,
2641 as a string, the back-end, as a symbol, and the communication
2642 channel, as a plist. It must return a string or nil.")
2644 (defvar org-export-filter-inline-babel-call-functions nil
2645 "List of functions applied to a transcoded inline-babel-call.
2646 Each filter is called with three arguments: the transcoded data,
2647 as a string, the back-end, as a symbol, and the communication
2648 channel, as a plist. It must return a string or nil.")
2650 (defvar org-export-filter-inline-src-block-functions nil
2651 "List of functions applied to a transcoded inline-src-block.
2652 Each filter is called with three arguments: the transcoded data,
2653 as a string, the back-end, as a symbol, and the communication
2654 channel, as a plist. It must return a string or nil.")
2656 (defvar org-export-filter-italic-functions nil
2657 "List of functions applied to transcoded italic text.
2658 Each filter is called with three arguments: the transcoded data,
2659 as a string, the back-end, as a symbol, and the communication
2660 channel, as a plist. It must return a string or nil.")
2662 (defvar org-export-filter-latex-fragment-functions nil
2663 "List of functions applied to a transcoded latex-fragment.
2664 Each filter is called with three arguments: the transcoded data,
2665 as a string, the back-end, as a symbol, and the communication
2666 channel, as a plist. It must return a string or nil.")
2668 (defvar org-export-filter-line-break-functions nil
2669 "List of functions applied to a transcoded line-break.
2670 Each filter is called with three arguments: the transcoded data,
2671 as a string, the back-end, as a symbol, and the communication
2672 channel, as a plist. It must return a string or nil.")
2674 (defvar org-export-filter-link-functions nil
2675 "List of functions applied to a transcoded link.
2676 Each filter is called with three arguments: the transcoded data,
2677 as a string, the back-end, as a symbol, and the communication
2678 channel, as a plist. It must return a string or nil.")
2680 (defvar org-export-filter-radio-target-functions nil
2681 "List of functions applied to a transcoded radio-target.
2682 Each filter is called with three arguments: the transcoded data,
2683 as a string, the back-end, as a symbol, and the communication
2684 channel, as a plist. It must return a string or nil.")
2686 (defvar org-export-filter-statistics-cookie-functions nil
2687 "List of functions applied to a transcoded statistics-cookie.
2688 Each filter is called with three arguments: the transcoded data,
2689 as a string, the back-end, as a symbol, and the communication
2690 channel, as a plist. It must return a string or nil.")
2692 (defvar org-export-filter-strike-through-functions nil
2693 "List of functions applied to transcoded strike-through text.
2694 Each filter is called with three arguments: the transcoded data,
2695 as a string, the back-end, as a symbol, and the communication
2696 channel, as a plist. It must return a string or nil.")
2698 (defvar org-export-filter-subscript-functions nil
2699 "List of functions applied to a transcoded subscript.
2700 Each filter is called with three arguments: the transcoded data,
2701 as a string, the back-end, as a symbol, and the communication
2702 channel, as a plist. It must return a string or nil.")
2704 (defvar org-export-filter-superscript-functions nil
2705 "List of functions applied to a transcoded superscript.
2706 Each filter is called with three arguments: the transcoded data,
2707 as a string, the back-end, as a symbol, and the communication
2708 channel, as a plist. It must return a string or nil.")
2710 (defvar org-export-filter-target-functions nil
2711 "List of functions applied to a transcoded target.
2712 Each filter is called with three arguments: the transcoded data,
2713 as a string, the back-end, as a symbol, and the communication
2714 channel, as a plist. It must return a string or nil.")
2716 (defvar org-export-filter-timestamp-functions nil
2717 "List of functions applied to a transcoded timestamp.
2718 Each filter is called with three arguments: the transcoded data,
2719 as a string, the back-end, as a symbol, and the communication
2720 channel, as a plist. It must return a string or nil.")
2722 (defvar org-export-filter-underline-functions nil
2723 "List of functions applied to transcoded underline text.
2724 Each filter is called with three arguments: the transcoded data,
2725 as a string, the back-end, as a symbol, and the communication
2726 channel, as a plist. It must return a string or nil.")
2728 (defvar org-export-filter-verbatim-functions nil
2729 "List of functions applied to transcoded verbatim text.
2730 Each filter is called with three arguments: the transcoded data,
2731 as a string, the back-end, as a symbol, and the communication
2732 channel, as a plist. It must return a string or nil.")
2735 ;;;; Filters Tools
2737 ;; Internal function `org-export-install-filters' installs filters
2738 ;; hard-coded in back-ends (developer filters) and filters from global
2739 ;; variables (user filters) in the communication channel.
2741 ;; Internal function `org-export-filter-apply-functions' takes care
2742 ;; about applying each filter in order to a given data. It ignores
2743 ;; filters returning a nil value but stops whenever a filter returns
2744 ;; an empty string.
2746 (defun org-export-filter-apply-functions (filters value info)
2747 "Call every function in FILTERS.
2749 Functions are called with arguments VALUE, current export
2750 back-end's name and INFO. A function returning a nil value will
2751 be skipped. If it returns the empty string, the process ends and
2752 VALUE is ignored.
2754 Call is done in a LIFO fashion, to be sure that developer
2755 specified filters, if any, are called first."
2756 (catch 'exit
2757 (let ((backend-name (plist-get info :back-end)))
2758 (dolist (filter filters value)
2759 (let ((result (funcall filter value backend-name info)))
2760 (cond ((not result) value)
2761 ((equal value "") (throw 'exit nil))
2762 (t (setq value result))))))))
2764 (defun org-export-install-filters (info)
2765 "Install filters properties in communication channel.
2766 INFO is a plist containing the current communication channel.
2767 Return the updated communication channel."
2768 (let (plist)
2769 ;; Install user-defined filters with `org-export-filters-alist'
2770 ;; and filters already in INFO (through ext-plist mechanism).
2771 (mapc (lambda (p)
2772 (let* ((prop (car p))
2773 (info-value (plist-get info prop))
2774 (default-value (symbol-value (cdr p))))
2775 (setq plist
2776 (plist-put plist prop
2777 ;; Filters in INFO will be called
2778 ;; before those user provided.
2779 (append (if (listp info-value) info-value
2780 (list info-value))
2781 default-value)))))
2782 org-export-filters-alist)
2783 ;; Prepend back-end specific filters to that list.
2784 (mapc (lambda (p)
2785 ;; Single values get consed, lists are appended.
2786 (let ((key (car p)) (value (cdr p)))
2787 (when value
2788 (setq plist
2789 (plist-put
2790 plist key
2791 (if (atom value) (cons value (plist-get plist key))
2792 (append value (plist-get plist key))))))))
2793 (org-export-get-all-filters (plist-get info :back-end)))
2794 ;; Return new communication channel.
2795 (org-combine-plists info plist)))
2799 ;;; Core functions
2801 ;; This is the room for the main function, `org-export-as', along with
2802 ;; its derivative, `org-export-string-as'.
2803 ;; `org-export--copy-to-kill-ring-p' determines if output of these
2804 ;; function should be added to kill ring.
2806 ;; Note that `org-export-as' doesn't really parse the current buffer,
2807 ;; but a copy of it (with the same buffer-local variables and
2808 ;; visibility), where macros and include keywords are expanded and
2809 ;; Babel blocks are executed, if appropriate.
2810 ;; `org-export-with-buffer-copy' macro prepares that copy.
2812 ;; File inclusion is taken care of by
2813 ;; `org-export-expand-include-keyword' and
2814 ;; `org-export--prepare-file-contents'. Structure wise, including
2815 ;; a whole Org file in a buffer often makes little sense. For
2816 ;; example, if the file contains a headline and the include keyword
2817 ;; was within an item, the item should contain the headline. That's
2818 ;; why file inclusion should be done before any structure can be
2819 ;; associated to the file, that is before parsing.
2821 ;; `org-export-insert-default-template' is a command to insert
2822 ;; a default template (or a back-end specific template) at point or in
2823 ;; current subtree.
2825 (defun org-export-copy-buffer ()
2826 "Return a copy of the current buffer.
2827 The copy preserves Org buffer-local variables, visibility and
2828 narrowing."
2829 (let ((copy-buffer-fun (org-export--generate-copy-script (current-buffer)))
2830 (new-buf (generate-new-buffer (buffer-name))))
2831 (with-current-buffer new-buf
2832 (funcall copy-buffer-fun)
2833 (set-buffer-modified-p nil))
2834 new-buf))
2836 (defmacro org-export-with-buffer-copy (&rest body)
2837 "Apply BODY in a copy of the current buffer.
2838 The copy preserves local variables, visibility and contents of
2839 the original buffer. Point is at the beginning of the buffer
2840 when BODY is applied."
2841 (declare (debug t))
2842 (org-with-gensyms (buf-copy)
2843 `(let ((,buf-copy (org-export-copy-buffer)))
2844 (unwind-protect
2845 (with-current-buffer ,buf-copy
2846 (goto-char (point-min))
2847 (progn ,@body))
2848 (and (buffer-live-p ,buf-copy)
2849 ;; Kill copy without confirmation.
2850 (progn (with-current-buffer ,buf-copy
2851 (restore-buffer-modified-p nil))
2852 (kill-buffer ,buf-copy)))))))
2854 (defun org-export--generate-copy-script (buffer)
2855 "Generate a function duplicating BUFFER.
2857 The copy will preserve local variables, visibility, contents and
2858 narrowing of the original buffer. If a region was active in
2859 BUFFER, contents will be narrowed to that region instead.
2861 The resulting function can be evaled at a later time, from
2862 another buffer, effectively cloning the original buffer there.
2864 The function assumes BUFFER's major mode is `org-mode'."
2865 (with-current-buffer buffer
2866 `(lambda ()
2867 (let ((inhibit-modification-hooks t))
2868 ;; Set major mode. Ignore `org-mode-hook' as it has been run
2869 ;; already in BUFFER.
2870 (let ((org-mode-hook nil) (org-inhibit-startup t)) (org-mode))
2871 ;; Copy specific buffer local variables and variables set
2872 ;; through BIND keywords.
2873 ,@(let ((bound-variables (org-export--list-bound-variables))
2874 vars)
2875 (dolist (entry (buffer-local-variables (buffer-base-buffer)) vars)
2876 (when (consp entry)
2877 (let ((var (car entry))
2878 (val (cdr entry)))
2879 (and (not (eq var 'org-font-lock-keywords))
2880 (or (memq var
2881 '(default-directory
2882 buffer-file-name
2883 buffer-file-coding-system))
2884 (assq var bound-variables)
2885 (string-match "^\\(org-\\|orgtbl-\\)"
2886 (symbol-name var)))
2887 ;; Skip unreadable values, as they cannot be
2888 ;; sent to external process.
2889 (or (not val) (ignore-errors (read (format "%S" val))))
2890 (push `(set (make-local-variable (quote ,var))
2891 (quote ,val))
2892 vars))))))
2893 ;; Whole buffer contents.
2894 (insert
2895 ,(org-with-wide-buffer
2896 (buffer-substring-no-properties
2897 (point-min) (point-max))))
2898 ;; Narrowing.
2899 ,(if (org-region-active-p)
2900 `(narrow-to-region ,(region-beginning) ,(region-end))
2901 `(narrow-to-region ,(point-min) ,(point-max)))
2902 ;; Current position of point.
2903 (goto-char ,(point))
2904 ;; Overlays with invisible property.
2905 ,@(let (ov-set)
2906 (mapc
2907 (lambda (ov)
2908 (let ((invis-prop (overlay-get ov 'invisible)))
2909 (when invis-prop
2910 (push `(overlay-put
2911 (make-overlay ,(overlay-start ov)
2912 ,(overlay-end ov))
2913 'invisible (quote ,invis-prop))
2914 ov-set))))
2915 (overlays-in (point-min) (point-max)))
2916 ov-set)))))
2918 ;;;###autoload
2919 (defun org-export-as
2920 (backend &optional subtreep visible-only body-only ext-plist)
2921 "Transcode current Org buffer into BACKEND code.
2923 BACKEND is either an export back-end, as returned by, e.g.,
2924 `org-export-create-backend', or a symbol referring to
2925 a registered back-end.
2927 If narrowing is active in the current buffer, only transcode its
2928 narrowed part.
2930 If a region is active, transcode that region.
2932 When optional argument SUBTREEP is non-nil, transcode the
2933 sub-tree at point, extracting information from the headline
2934 properties first.
2936 When optional argument VISIBLE-ONLY is non-nil, don't export
2937 contents of hidden elements.
2939 When optional argument BODY-ONLY is non-nil, only return body
2940 code, without surrounding template.
2942 Optional argument EXT-PLIST, when provided, is a property list
2943 with external parameters overriding Org default settings, but
2944 still inferior to file-local settings.
2946 Return code as a string."
2947 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
2948 (org-export-barf-if-invalid-backend backend)
2949 (save-excursion
2950 (save-restriction
2951 ;; Narrow buffer to an appropriate region or subtree for
2952 ;; parsing. If parsing subtree, be sure to remove main headline
2953 ;; too.
2954 (cond ((org-region-active-p)
2955 (narrow-to-region (region-beginning) (region-end)))
2956 (subtreep
2957 (org-narrow-to-subtree)
2958 (goto-char (point-min))
2959 (forward-line)
2960 (narrow-to-region (point) (point-max))))
2961 ;; Initialize communication channel with original buffer
2962 ;; attributes, unavailable in its copy.
2963 (let* ((org-export-current-backend (org-export-backend-name backend))
2964 (info (org-combine-plists
2965 (list :export-options
2966 (delq nil
2967 (list (and subtreep 'subtree)
2968 (and visible-only 'visible-only)
2969 (and body-only 'body-only))))
2970 (org-export--get-buffer-attributes)))
2971 tree)
2972 ;; Update communication channel and get parse tree. Buffer
2973 ;; isn't parsed directly. Instead, a temporary copy is
2974 ;; created, where include keywords, macros are expanded and
2975 ;; code blocks are evaluated.
2976 (org-export-with-buffer-copy
2977 ;; Run first hook with current back-end's name as argument.
2978 (run-hook-with-args 'org-export-before-processing-hook
2979 (org-export-backend-name backend))
2980 (org-export-expand-include-keyword)
2981 ;; Update macro templates since #+INCLUDE keywords might have
2982 ;; added some new ones.
2983 (org-macro-initialize-templates)
2984 (org-macro-replace-all org-macro-templates)
2985 (org-export-execute-babel-code)
2986 ;; Update radio targets since keyword inclusion might have
2987 ;; added some more.
2988 (org-update-radio-target-regexp)
2989 ;; Run last hook with current back-end's name as argument.
2990 (goto-char (point-min))
2991 (save-excursion
2992 (run-hook-with-args 'org-export-before-parsing-hook
2993 (org-export-backend-name backend)))
2994 ;; Update communication channel with environment. Also
2995 ;; install user's and developer's filters.
2996 (setq info
2997 (org-export-install-filters
2998 (org-combine-plists
2999 info (org-export-get-environment backend subtreep ext-plist))))
3000 ;; Expand export-specific set of macros: {{{author}}},
3001 ;; {{{date}}}, {{{email}}} and {{{title}}}. It must be done
3002 ;; once regular macros have been expanded, since document
3003 ;; keywords may contain one of them.
3004 (org-macro-replace-all
3005 (list (cons "author"
3006 (org-element-interpret-data (plist-get info :author)))
3007 (cons "date"
3008 (org-element-interpret-data (plist-get info :date)))
3009 ;; EMAIL is not a parsed keyword: store it as-is.
3010 (cons "email" (or (plist-get info :email) ""))
3011 (cons "title"
3012 (org-element-interpret-data (plist-get info :title)))))
3013 ;; Call options filters and update export options. We do not
3014 ;; use `org-export-filter-apply-functions' here since the
3015 ;; arity of such filters is different.
3016 (let ((backend-name (org-export-backend-name backend)))
3017 (dolist (filter (plist-get info :filter-options))
3018 (let ((result (funcall filter info backend-name)))
3019 (when result (setq info result)))))
3020 ;; Parse buffer and call parse-tree filter on it.
3021 (setq tree
3022 (org-export-filter-apply-functions
3023 (plist-get info :filter-parse-tree)
3024 (org-element-parse-buffer nil visible-only) info))
3025 ;; Now tree is complete, compute its properties and add them
3026 ;; to communication channel.
3027 (setq info
3028 (org-combine-plists
3029 info (org-export-collect-tree-properties tree info)))
3030 ;; Eventually transcode TREE. Wrap the resulting string into
3031 ;; a template.
3032 (let* ((body (org-element-normalize-string
3033 (or (org-export-data tree info) "")))
3034 (inner-template (cdr (assq 'inner-template
3035 (plist-get info :translate-alist))))
3036 (full-body (if (not (functionp inner-template)) body
3037 (funcall inner-template body info)))
3038 (template (cdr (assq 'template
3039 (plist-get info :translate-alist)))))
3040 ;; Remove all text properties since they cannot be
3041 ;; retrieved from an external process. Finally call
3042 ;; final-output filter and return result.
3043 (org-no-properties
3044 (org-export-filter-apply-functions
3045 (plist-get info :filter-final-output)
3046 (if (or (not (functionp template)) body-only) full-body
3047 (funcall template full-body info))
3048 info))))))))
3050 ;;;###autoload
3051 (defun org-export-string-as (string backend &optional body-only ext-plist)
3052 "Transcode STRING into BACKEND code.
3054 BACKEND is either an export back-end, as returned by, e.g.,
3055 `org-export-create-backend', or a symbol referring to
3056 a registered back-end.
3058 When optional argument BODY-ONLY is non-nil, only return body
3059 code, without preamble nor postamble.
3061 Optional argument EXT-PLIST, when provided, is a property list
3062 with external parameters overriding Org default settings, but
3063 still inferior to file-local settings.
3065 Return code as a string."
3066 (with-temp-buffer
3067 (insert string)
3068 (let ((org-inhibit-startup t)) (org-mode))
3069 (org-export-as backend nil nil body-only ext-plist)))
3071 ;;;###autoload
3072 (defun org-export-replace-region-by (backend)
3073 "Replace the active region by its export to BACKEND.
3074 BACKEND is either an export back-end, as returned by, e.g.,
3075 `org-export-create-backend', or a symbol referring to
3076 a registered back-end."
3077 (if (not (org-region-active-p))
3078 (user-error "No active region to replace")
3079 (let* ((beg (region-beginning))
3080 (end (region-end))
3081 (str (buffer-substring beg end)) rpl)
3082 (setq rpl (org-export-string-as str backend t))
3083 (delete-region beg end)
3084 (insert rpl))))
3086 ;;;###autoload
3087 (defun org-export-insert-default-template (&optional backend subtreep)
3088 "Insert all export keywords with default values at beginning of line.
3090 BACKEND is a symbol referring to the name of a registered export
3091 back-end, for which specific export options should be added to
3092 the template, or `default' for default template. When it is nil,
3093 the user will be prompted for a category.
3095 If SUBTREEP is non-nil, export configuration will be set up
3096 locally for the subtree through node properties."
3097 (interactive)
3098 (unless (derived-mode-p 'org-mode) (user-error "Not in an Org mode buffer"))
3099 (when (and subtreep (org-before-first-heading-p))
3100 (user-error "No subtree to set export options for"))
3101 (let ((node (and subtreep (save-excursion (org-back-to-heading t) (point))))
3102 (backend
3103 (or backend
3104 (intern
3105 (org-completing-read
3106 "Options category: "
3107 (cons "default"
3108 (mapcar (lambda (b)
3109 (symbol-name (org-export-backend-name b)))
3110 org-export--registered-backends))))))
3111 options keywords)
3112 ;; Populate OPTIONS and KEYWORDS.
3113 (dolist (entry (cond ((eq backend 'default) org-export-options-alist)
3114 ((org-export-backend-p backend)
3115 (org-export-get-all-options backend))
3116 (t (org-export-get-all-options
3117 (org-export-get-backend backend)))))
3118 (let ((keyword (nth 1 entry))
3119 (option (nth 2 entry)))
3120 (cond
3121 (keyword (unless (assoc keyword keywords)
3122 (let ((value
3123 (if (eq (nth 4 entry) 'split)
3124 (mapconcat 'identity (eval (nth 3 entry)) " ")
3125 (eval (nth 3 entry)))))
3126 (push (cons keyword value) keywords))))
3127 (option (unless (assoc option options)
3128 (push (cons option (eval (nth 3 entry))) options))))))
3129 ;; Move to an appropriate location in order to insert options.
3130 (unless subtreep (beginning-of-line))
3131 ;; First get TITLE, DATE, AUTHOR and EMAIL if they belong to the
3132 ;; list of available keywords.
3133 (when (assoc "TITLE" keywords)
3134 (let ((title
3135 (or (let ((visited-file (buffer-file-name (buffer-base-buffer))))
3136 (and visited-file
3137 (file-name-sans-extension
3138 (file-name-nondirectory visited-file))))
3139 (buffer-name (buffer-base-buffer)))))
3140 (if (not subtreep) (insert (format "#+TITLE: %s\n" title))
3141 (org-entry-put node "EXPORT_TITLE" title))))
3142 (when (assoc "DATE" keywords)
3143 (let ((date (with-temp-buffer (org-insert-time-stamp (current-time)))))
3144 (if (not subtreep) (insert "#+DATE: " date "\n")
3145 (org-entry-put node "EXPORT_DATE" date))))
3146 (when (assoc "AUTHOR" keywords)
3147 (let ((author (cdr (assoc "AUTHOR" keywords))))
3148 (if subtreep (org-entry-put node "EXPORT_AUTHOR" author)
3149 (insert
3150 (format "#+AUTHOR:%s\n"
3151 (if (not (org-string-nw-p author)) ""
3152 (concat " " author)))))))
3153 (when (assoc "EMAIL" keywords)
3154 (let ((email (cdr (assoc "EMAIL" keywords))))
3155 (if subtreep (org-entry-put node "EXPORT_EMAIL" email)
3156 (insert
3157 (format "#+EMAIL:%s\n"
3158 (if (not (org-string-nw-p email)) ""
3159 (concat " " email)))))))
3160 ;; Then (multiple) OPTIONS lines. Never go past fill-column.
3161 (when options
3162 (let ((items
3163 (mapcar
3164 (lambda (opt)
3165 (format "%s:%s" (car opt) (format "%s" (cdr opt))))
3166 (sort options (lambda (k1 k2) (string< (car k1) (car k2)))))))
3167 (if subtreep
3168 (org-entry-put
3169 node "EXPORT_OPTIONS" (mapconcat 'identity items " "))
3170 (while items
3171 (insert "#+OPTIONS:")
3172 (let ((width 10))
3173 (while (and items
3174 (< (+ width (length (car items)) 1) fill-column))
3175 (let ((item (pop items)))
3176 (insert " " item)
3177 (incf width (1+ (length item))))))
3178 (insert "\n")))))
3179 ;; And the rest of keywords.
3180 (dolist (key (sort keywords (lambda (k1 k2) (string< (car k1) (car k2)))))
3181 (unless (member (car key) '("TITLE" "DATE" "AUTHOR" "EMAIL"))
3182 (let ((val (cdr key)))
3183 (if subtreep (org-entry-put node (concat "EXPORT_" (car key)) val)
3184 (insert
3185 (format "#+%s:%s\n"
3186 (car key)
3187 (if (org-string-nw-p val) (format " %s" val) "")))))))))
3189 (defun org-export-expand-include-keyword (&optional included dir)
3190 "Expand every include keyword in buffer.
3191 Optional argument INCLUDED is a list of included file names along
3192 with their line restriction, when appropriate. It is used to
3193 avoid infinite recursion. Optional argument DIR is the current
3194 working directory. It is used to properly resolve relative
3195 paths."
3196 (let ((case-fold-search t))
3197 (goto-char (point-min))
3198 (while (re-search-forward "^[ \t]*#\\+INCLUDE:" nil t)
3199 (let ((element (save-match-data (org-element-at-point))))
3200 (when (eq (org-element-type element) 'keyword)
3201 (beginning-of-line)
3202 ;; Extract arguments from keyword's value.
3203 (let* ((value (org-element-property :value element))
3204 (ind (org-get-indentation))
3205 (file (and (string-match
3206 "^\\(\".+?\"\\|\\S-+\\)\\(?:\\s-+\\|$\\)" value)
3207 (prog1 (expand-file-name
3208 (org-remove-double-quotes
3209 (match-string 1 value))
3210 dir)
3211 (setq value (replace-match "" nil nil value)))))
3212 (lines
3213 (and (string-match
3214 ":lines +\"\\(\\(?:[0-9]+\\)?-\\(?:[0-9]+\\)?\\)\""
3215 value)
3216 (prog1 (match-string 1 value)
3217 (setq value (replace-match "" nil nil value)))))
3218 (env (cond ((string-match "\\<example\\>" value) 'example)
3219 ((string-match "\\<src\\(?: +\\(.*\\)\\)?" value)
3220 (match-string 1 value))))
3221 ;; Minimal level of included file defaults to the child
3222 ;; level of the current headline, if any, or one. It
3223 ;; only applies is the file is meant to be included as
3224 ;; an Org one.
3225 (minlevel
3226 (and (not env)
3227 (if (string-match ":minlevel +\\([0-9]+\\)" value)
3228 (prog1 (string-to-number (match-string 1 value))
3229 (setq value (replace-match "" nil nil value)))
3230 (let ((cur (org-current-level)))
3231 (if cur (1+ (org-reduced-level cur)) 1))))))
3232 ;; Remove keyword.
3233 (delete-region (point) (progn (forward-line) (point)))
3234 (cond
3235 ((not file) nil)
3236 ((not (file-readable-p file))
3237 (error "Cannot include file %s" file))
3238 ;; Check if files has already been parsed. Look after
3239 ;; inclusion lines too, as different parts of the same file
3240 ;; can be included too.
3241 ((member (list file lines) included)
3242 (error "Recursive file inclusion: %s" file))
3244 (cond
3245 ((eq env 'example)
3246 (insert
3247 (let ((ind-str (make-string ind ? ))
3248 (contents
3249 (org-escape-code-in-string
3250 (org-export--prepare-file-contents file lines))))
3251 (format "%s#+BEGIN_EXAMPLE\n%s%s#+END_EXAMPLE\n"
3252 ind-str contents ind-str))))
3253 ((stringp env)
3254 (insert
3255 (let ((ind-str (make-string ind ? ))
3256 (contents
3257 (org-escape-code-in-string
3258 (org-export--prepare-file-contents file lines))))
3259 (format "%s#+BEGIN_SRC %s\n%s%s#+END_SRC\n"
3260 ind-str env contents ind-str))))
3262 (insert
3263 (with-temp-buffer
3264 (let ((org-inhibit-startup t)) (org-mode))
3265 (insert
3266 (org-export--prepare-file-contents file lines ind minlevel))
3267 (org-export-expand-include-keyword
3268 (cons (list file lines) included)
3269 (file-name-directory file))
3270 (buffer-string)))))))))))))
3272 (defun org-export--prepare-file-contents (file &optional lines ind minlevel)
3273 "Prepare the contents of FILE for inclusion and return them as a string.
3275 When optional argument LINES is a string specifying a range of
3276 lines, include only those lines.
3278 Optional argument IND, when non-nil, is an integer specifying the
3279 global indentation of returned contents. Since its purpose is to
3280 allow an included file to stay in the same environment it was
3281 created \(i.e. a list item), it doesn't apply past the first
3282 headline encountered.
3284 Optional argument MINLEVEL, when non-nil, is an integer
3285 specifying the level that any top-level headline in the included
3286 file should have."
3287 (with-temp-buffer
3288 (insert-file-contents file)
3289 (when lines
3290 (let* ((lines (split-string lines "-"))
3291 (lbeg (string-to-number (car lines)))
3292 (lend (string-to-number (cadr lines)))
3293 (beg (if (zerop lbeg) (point-min)
3294 (goto-char (point-min))
3295 (forward-line (1- lbeg))
3296 (point)))
3297 (end (if (zerop lend) (point-max)
3298 (goto-char (point-min))
3299 (forward-line (1- lend))
3300 (point))))
3301 (narrow-to-region beg end)))
3302 ;; Remove blank lines at beginning and end of contents. The logic
3303 ;; behind that removal is that blank lines around include keyword
3304 ;; override blank lines in included file.
3305 (goto-char (point-min))
3306 (org-skip-whitespace)
3307 (beginning-of-line)
3308 (delete-region (point-min) (point))
3309 (goto-char (point-max))
3310 (skip-chars-backward " \r\t\n")
3311 (forward-line)
3312 (delete-region (point) (point-max))
3313 ;; If IND is set, preserve indentation of include keyword until
3314 ;; the first headline encountered.
3315 (when ind
3316 (unless (eq major-mode 'org-mode)
3317 (let ((org-inhibit-startup t)) (org-mode)))
3318 (goto-char (point-min))
3319 (let ((ind-str (make-string ind ? )))
3320 (while (not (or (eobp) (looking-at org-outline-regexp-bol)))
3321 ;; Do not move footnote definitions out of column 0.
3322 (unless (and (looking-at org-footnote-definition-re)
3323 (eq (org-element-type (org-element-at-point))
3324 'footnote-definition))
3325 (insert ind-str))
3326 (forward-line))))
3327 ;; When MINLEVEL is specified, compute minimal level for headlines
3328 ;; in the file (CUR-MIN), and remove stars to each headline so
3329 ;; that headlines with minimal level have a level of MINLEVEL.
3330 (when minlevel
3331 (unless (eq major-mode 'org-mode)
3332 (let ((org-inhibit-startup t)) (org-mode)))
3333 (org-with-limited-levels
3334 (let ((levels (org-map-entries
3335 (lambda () (org-reduced-level (org-current-level))))))
3336 (when levels
3337 (let ((offset (- minlevel (apply 'min levels))))
3338 (unless (zerop offset)
3339 (when org-odd-levels-only (setq offset (* offset 2)))
3340 ;; Only change stars, don't bother moving whole
3341 ;; sections.
3342 (org-map-entries
3343 (lambda () (if (< offset 0) (delete-char (abs offset))
3344 (insert (make-string offset ?*)))))))))))
3345 (org-element-normalize-string (buffer-string))))
3347 (defun org-export-execute-babel-code ()
3348 "Execute every Babel code in the visible part of current buffer."
3349 ;; Get a pristine copy of current buffer so Babel references can be
3350 ;; properly resolved.
3351 (let ((reference (org-export-copy-buffer)))
3352 (unwind-protect (let ((org-current-export-file reference))
3353 (org-babel-exp-process-buffer))
3354 (kill-buffer reference))))
3356 (defun org-export--copy-to-kill-ring-p ()
3357 "Return a non-nil value when output should be added to the kill ring.
3358 See also `org-export-copy-to-kill-ring'."
3359 (if (eq org-export-copy-to-kill-ring 'if-interactive)
3360 (not (or executing-kbd-macro noninteractive))
3361 (eq org-export-copy-to-kill-ring t)))
3365 ;;; Tools For Back-Ends
3367 ;; A whole set of tools is available to help build new exporters. Any
3368 ;; function general enough to have its use across many back-ends
3369 ;; should be added here.
3371 ;;;; For Affiliated Keywords
3373 ;; `org-export-read-attribute' reads a property from a given element
3374 ;; as a plist. It can be used to normalize affiliated keywords'
3375 ;; syntax.
3377 ;; Since captions can span over multiple lines and accept dual values,
3378 ;; their internal representation is a bit tricky. Therefore,
3379 ;; `org-export-get-caption' transparently returns a given element's
3380 ;; caption as a secondary string.
3382 (defun org-export-read-attribute (attribute element &optional property)
3383 "Turn ATTRIBUTE property from ELEMENT into a plist.
3385 When optional argument PROPERTY is non-nil, return the value of
3386 that property within attributes.
3388 This function assumes attributes are defined as \":keyword
3389 value\" pairs. It is appropriate for `:attr_html' like
3390 properties.
3392 All values will become strings except the empty string and
3393 \"nil\", which will become nil. Also, values containing only
3394 double quotes will be read as-is, which means that \"\" value
3395 will become the empty string."
3396 (let* ((prepare-value
3397 (lambda (str)
3398 (save-match-data
3399 (cond ((member str '(nil "" "nil")) nil)
3400 ((string-match "^\"\\(\"+\\)?\"$" str)
3401 (or (match-string 1 str) ""))
3402 (t str)))))
3403 (attributes
3404 (let ((value (org-element-property attribute element)))
3405 (when value
3406 (let ((s (mapconcat 'identity value " ")) result)
3407 (while (string-match
3408 "\\(?:^\\|[ \t]+\\)\\(:[-a-zA-Z0-9_]+\\)\\([ \t]+\\|$\\)"
3410 (let ((value (substring s 0 (match-beginning 0))))
3411 (push (funcall prepare-value value) result))
3412 (push (intern (match-string 1 s)) result)
3413 (setq s (substring s (match-end 0))))
3414 ;; Ignore any string before first property with `cdr'.
3415 (cdr (nreverse (cons (funcall prepare-value s) result))))))))
3416 (if property (plist-get attributes property) attributes)))
3418 (defun org-export-get-caption (element &optional shortp)
3419 "Return caption from ELEMENT as a secondary string.
3421 When optional argument SHORTP is non-nil, return short caption,
3422 as a secondary string, instead.
3424 Caption lines are separated by a white space."
3425 (let ((full-caption (org-element-property :caption element)) caption)
3426 (dolist (line full-caption (cdr caption))
3427 (let ((cap (funcall (if shortp 'cdr 'car) line)))
3428 (when cap
3429 (setq caption (nconc (list " ") (copy-sequence cap) caption)))))))
3432 ;;;; For Derived Back-ends
3434 ;; `org-export-with-backend' is a function allowing to locally use
3435 ;; another back-end to transcode some object or element. In a derived
3436 ;; back-end, it may be used as a fall-back function once all specific
3437 ;; cases have been treated.
3439 (defun org-export-with-backend (backend data &optional contents info)
3440 "Call a transcoder from BACKEND on DATA.
3441 BACKEND is an export back-end, as returned by, e.g.,
3442 `org-export-create-backend', or a symbol referring to
3443 a registered back-end. DATA is an Org element, object, secondary
3444 string or string. CONTENTS, when non-nil, is the transcoded
3445 contents of DATA element, as a string. INFO, when non-nil, is
3446 the communication channel used for export, as a plist."
3447 (when (symbolp backend) (setq backend (org-export-get-backend backend)))
3448 (org-export-barf-if-invalid-backend backend)
3449 (let ((type (org-element-type data)))
3450 (if (memq type '(nil org-data)) (error "No foreign transcoder available")
3451 (let ((transcoder
3452 (cdr (assq type (org-export-get-all-transcoders backend)))))
3453 (if (functionp transcoder) (funcall transcoder data contents info)
3454 (error "No foreign transcoder available"))))))
3457 ;;;; For Export Snippets
3459 ;; Every export snippet is transmitted to the back-end. Though, the
3460 ;; latter will only retain one type of export-snippet, ignoring
3461 ;; others, based on the former's target back-end. The function
3462 ;; `org-export-snippet-backend' returns that back-end for a given
3463 ;; export-snippet.
3465 (defun org-export-snippet-backend (export-snippet)
3466 "Return EXPORT-SNIPPET targeted back-end as a symbol.
3467 Translation, with `org-export-snippet-translation-alist', is
3468 applied."
3469 (let ((back-end (org-element-property :back-end export-snippet)))
3470 (intern
3471 (or (cdr (assoc back-end org-export-snippet-translation-alist))
3472 back-end))))
3475 ;;;; For Footnotes
3477 ;; `org-export-collect-footnote-definitions' is a tool to list
3478 ;; actually used footnotes definitions in the whole parse tree, or in
3479 ;; a headline, in order to add footnote listings throughout the
3480 ;; transcoded data.
3482 ;; `org-export-footnote-first-reference-p' is a predicate used by some
3483 ;; back-ends, when they need to attach the footnote definition only to
3484 ;; the first occurrence of the corresponding label.
3486 ;; `org-export-get-footnote-definition' and
3487 ;; `org-export-get-footnote-number' provide easier access to
3488 ;; additional information relative to a footnote reference.
3490 (defun org-export-collect-footnote-definitions (data info)
3491 "Return an alist between footnote numbers, labels and definitions.
3493 DATA is the parse tree from which definitions are collected.
3494 INFO is the plist used as a communication channel.
3496 Definitions are sorted by order of references. They either
3497 appear as Org data or as a secondary string for inlined
3498 footnotes. Unreferenced definitions are ignored."
3499 (let* (num-alist
3500 collect-fn ; for byte-compiler.
3501 (collect-fn
3502 (function
3503 (lambda (data)
3504 ;; Collect footnote number, label and definition in DATA.
3505 (org-element-map data 'footnote-reference
3506 (lambda (fn)
3507 (when (org-export-footnote-first-reference-p fn info)
3508 (let ((def (org-export-get-footnote-definition fn info)))
3509 (push
3510 (list (org-export-get-footnote-number fn info)
3511 (org-element-property :label fn)
3512 def)
3513 num-alist)
3514 ;; Also search in definition for nested footnotes.
3515 (when (eq (org-element-property :type fn) 'standard)
3516 (funcall collect-fn def)))))
3517 ;; Don't enter footnote definitions since it will happen
3518 ;; when their first reference is found.
3519 info nil 'footnote-definition)))))
3520 (funcall collect-fn (plist-get info :parse-tree))
3521 (reverse num-alist)))
3523 (defun org-export-footnote-first-reference-p (footnote-reference info)
3524 "Non-nil when a footnote reference is the first one for its label.
3526 FOOTNOTE-REFERENCE is the footnote reference being considered.
3527 INFO is the plist used as a communication channel."
3528 (let ((label (org-element-property :label footnote-reference)))
3529 ;; Anonymous footnotes are always a first reference.
3530 (if (not label) t
3531 ;; Otherwise, return the first footnote with the same LABEL and
3532 ;; test if it is equal to FOOTNOTE-REFERENCE.
3533 (let* (search-refs ; for byte-compiler.
3534 (search-refs
3535 (function
3536 (lambda (data)
3537 (org-element-map data 'footnote-reference
3538 (lambda (fn)
3539 (cond
3540 ((string= (org-element-property :label fn) label)
3541 (throw 'exit fn))
3542 ;; If FN isn't inlined, be sure to traverse its
3543 ;; definition before resuming search. See
3544 ;; comments in `org-export-get-footnote-number'
3545 ;; for more information.
3546 ((eq (org-element-property :type fn) 'standard)
3547 (funcall search-refs
3548 (org-export-get-footnote-definition fn info)))))
3549 ;; Don't enter footnote definitions since it will
3550 ;; happen when their first reference is found.
3551 info 'first-match 'footnote-definition)))))
3552 (eq (catch 'exit (funcall search-refs (plist-get info :parse-tree)))
3553 footnote-reference)))))
3555 (defun org-export-get-footnote-definition (footnote-reference info)
3556 "Return definition of FOOTNOTE-REFERENCE as parsed data.
3557 INFO is the plist used as a communication channel. If no such
3558 definition can be found, return the \"DEFINITION NOT FOUND\"
3559 string."
3560 (let ((label (org-element-property :label footnote-reference)))
3561 (or (org-element-property :inline-definition footnote-reference)
3562 (cdr (assoc label (plist-get info :footnote-definition-alist)))
3563 "DEFINITION NOT FOUND.")))
3565 (defun org-export-get-footnote-number (footnote info)
3566 "Return number associated to a footnote.
3568 FOOTNOTE is either a footnote reference or a footnote definition.
3569 INFO is the plist used as a communication channel."
3570 (let* ((label (org-element-property :label footnote))
3571 seen-refs
3572 search-ref ; For byte-compiler.
3573 (search-ref
3574 (function
3575 (lambda (data)
3576 ;; Search footnote references through DATA, filling
3577 ;; SEEN-REFS along the way.
3578 (org-element-map data 'footnote-reference
3579 (lambda (fn)
3580 (let ((fn-lbl (org-element-property :label fn)))
3581 (cond
3582 ;; Anonymous footnote match: return number.
3583 ((and (not fn-lbl) (eq fn footnote))
3584 (throw 'exit (1+ (length seen-refs))))
3585 ;; Labels match: return number.
3586 ((and label (string= label fn-lbl))
3587 (throw 'exit (1+ (length seen-refs))))
3588 ;; Anonymous footnote: it's always a new one.
3589 ;; Also, be sure to return nil from the `cond' so
3590 ;; `first-match' doesn't get us out of the loop.
3591 ((not fn-lbl) (push 'inline seen-refs) nil)
3592 ;; Label not seen so far: add it so SEEN-REFS.
3594 ;; Also search for subsequent references in
3595 ;; footnote definition so numbering follows
3596 ;; reading logic. Note that we don't have to care
3597 ;; about inline definitions, since
3598 ;; `org-element-map' already traverses them at the
3599 ;; right time.
3601 ;; Once again, return nil to stay in the loop.
3602 ((not (member fn-lbl seen-refs))
3603 (push fn-lbl seen-refs)
3604 (funcall search-ref
3605 (org-export-get-footnote-definition fn info))
3606 nil))))
3607 ;; Don't enter footnote definitions since it will
3608 ;; happen when their first reference is found.
3609 info 'first-match 'footnote-definition)))))
3610 (catch 'exit (funcall search-ref (plist-get info :parse-tree)))))
3613 ;;;; For Headlines
3615 ;; `org-export-get-relative-level' is a shortcut to get headline
3616 ;; level, relatively to the lower headline level in the parsed tree.
3618 ;; `org-export-get-headline-number' returns the section number of an
3619 ;; headline, while `org-export-number-to-roman' allows to convert it
3620 ;; to roman numbers.
3622 ;; `org-export-low-level-p', `org-export-first-sibling-p' and
3623 ;; `org-export-last-sibling-p' are three useful predicates when it
3624 ;; comes to fulfill the `:headline-levels' property.
3626 ;; `org-export-get-tags', `org-export-get-category' and
3627 ;; `org-export-get-node-property' extract useful information from an
3628 ;; headline or a parent headline. They all handle inheritance.
3630 ;; `org-export-get-alt-title' tries to retrieve an alternative title,
3631 ;; as a secondary string, suitable for table of contents. It falls
3632 ;; back onto default title.
3634 (defun org-export-get-relative-level (headline info)
3635 "Return HEADLINE relative level within current parsed tree.
3636 INFO is a plist holding contextual information."
3637 (+ (org-element-property :level headline)
3638 (or (plist-get info :headline-offset) 0)))
3640 (defun org-export-low-level-p (headline info)
3641 "Non-nil when HEADLINE is considered as low level.
3643 INFO is a plist used as a communication channel.
3645 A low level headlines has a relative level greater than
3646 `:headline-levels' property value.
3648 Return value is the difference between HEADLINE relative level
3649 and the last level being considered as high enough, or nil."
3650 (let ((limit (plist-get info :headline-levels)))
3651 (when (wholenump limit)
3652 (let ((level (org-export-get-relative-level headline info)))
3653 (and (> level limit) (- level limit))))))
3655 (defun org-export-get-headline-number (headline info)
3656 "Return HEADLINE numbering as a list of numbers.
3657 INFO is a plist holding contextual information."
3658 (cdr (assoc headline (plist-get info :headline-numbering))))
3660 (defun org-export-numbered-headline-p (headline info)
3661 "Return a non-nil value if HEADLINE element should be numbered.
3662 INFO is a plist used as a communication channel."
3663 (let ((sec-num (plist-get info :section-numbers))
3664 (level (org-export-get-relative-level headline info)))
3665 (if (wholenump sec-num) (<= level sec-num) sec-num)))
3667 (defun org-export-number-to-roman (n)
3668 "Convert integer N into a roman numeral."
3669 (let ((roman '((1000 . "M") (900 . "CM") (500 . "D") (400 . "CD")
3670 ( 100 . "C") ( 90 . "XC") ( 50 . "L") ( 40 . "XL")
3671 ( 10 . "X") ( 9 . "IX") ( 5 . "V") ( 4 . "IV")
3672 ( 1 . "I")))
3673 (res ""))
3674 (if (<= n 0)
3675 (number-to-string n)
3676 (while roman
3677 (if (>= n (caar roman))
3678 (setq n (- n (caar roman))
3679 res (concat res (cdar roman)))
3680 (pop roman)))
3681 res)))
3683 (defun org-export-get-tags (element info &optional tags inherited)
3684 "Return list of tags associated to ELEMENT.
3686 ELEMENT has either an `headline' or an `inlinetask' type. INFO
3687 is a plist used as a communication channel.
3689 Select tags (see `org-export-select-tags') and exclude tags (see
3690 `org-export-exclude-tags') are removed from the list.
3692 When non-nil, optional argument TAGS should be a list of strings.
3693 Any tag belonging to this list will also be removed.
3695 When optional argument INHERITED is non-nil, tags can also be
3696 inherited from parent headlines and FILETAGS keywords."
3697 (org-remove-if
3698 (lambda (tag) (or (member tag (plist-get info :select-tags))
3699 (member tag (plist-get info :exclude-tags))
3700 (member tag tags)))
3701 (if (not inherited) (org-element-property :tags element)
3702 ;; Build complete list of inherited tags.
3703 (let ((current-tag-list (org-element-property :tags element)))
3704 (mapc
3705 (lambda (parent)
3706 (mapc
3707 (lambda (tag)
3708 (when (and (memq (org-element-type parent) '(headline inlinetask))
3709 (not (member tag current-tag-list)))
3710 (push tag current-tag-list)))
3711 (org-element-property :tags parent)))
3712 (org-export-get-genealogy element))
3713 ;; Add FILETAGS keywords and return results.
3714 (org-uniquify (append (plist-get info :filetags) current-tag-list))))))
3716 (defun org-export-get-node-property (property blob &optional inherited)
3717 "Return node PROPERTY value for BLOB.
3719 PROPERTY is an upcase symbol (i.e. `:COOKIE_DATA'). BLOB is an
3720 element or object.
3722 If optional argument INHERITED is non-nil, the value can be
3723 inherited from a parent headline.
3725 Return value is a string or nil."
3726 (let ((headline (if (eq (org-element-type blob) 'headline) blob
3727 (org-export-get-parent-headline blob))))
3728 (if (not inherited) (org-element-property property blob)
3729 (let ((parent headline) value)
3730 (catch 'found
3731 (while parent
3732 (when (plist-member (nth 1 parent) property)
3733 (throw 'found (org-element-property property parent)))
3734 (setq parent (org-element-property :parent parent))))))))
3736 (defun org-export-get-category (blob info)
3737 "Return category for element or object BLOB.
3739 INFO is a plist used as a communication channel.
3741 CATEGORY is automatically inherited from a parent headline, from
3742 #+CATEGORY: keyword or created out of original file name. If all
3743 fail, the fall-back value is \"???\"."
3744 (or (let ((headline (if (eq (org-element-type blob) 'headline) blob
3745 (org-export-get-parent-headline blob))))
3746 ;; Almost like `org-export-node-property', but we cannot trust
3747 ;; `plist-member' as every headline has a `:CATEGORY'
3748 ;; property, would it be nil or equal to "???" (which has the
3749 ;; same meaning).
3750 (let ((parent headline) value)
3751 (catch 'found
3752 (while parent
3753 (let ((category (org-element-property :CATEGORY parent)))
3754 (and category (not (equal "???" category))
3755 (throw 'found category)))
3756 (setq parent (org-element-property :parent parent))))))
3757 (org-element-map (plist-get info :parse-tree) 'keyword
3758 (lambda (kwd)
3759 (when (equal (org-element-property :key kwd) "CATEGORY")
3760 (org-element-property :value kwd)))
3761 info 'first-match)
3762 (let ((file (plist-get info :input-file)))
3763 (and file (file-name-sans-extension (file-name-nondirectory file))))
3764 "???"))
3766 (defun org-export-get-alt-title (headline info)
3767 "Return alternative title for HEADLINE, as a secondary string.
3768 INFO is a plist used as a communication channel. If no optional
3769 title is defined, fall-back to the regular title."
3770 (or (org-element-property :alt-title headline)
3771 (org-element-property :title headline)))
3773 (defun org-export-first-sibling-p (headline info)
3774 "Non-nil when HEADLINE is the first sibling in its sub-tree.
3775 INFO is a plist used as a communication channel."
3776 (not (eq (org-element-type (org-export-get-previous-element headline info))
3777 'headline)))
3779 (defun org-export-last-sibling-p (headline info)
3780 "Non-nil when HEADLINE is the last sibling in its sub-tree.
3781 INFO is a plist used as a communication channel."
3782 (not (org-export-get-next-element headline info)))
3785 ;;;; For Keywords
3787 ;; `org-export-get-date' returns a date appropriate for the document
3788 ;; to about to be exported. In particular, it takes care of
3789 ;; `org-export-date-timestamp-format'.
3791 (defun org-export-get-date (info &optional fmt)
3792 "Return date value for the current document.
3794 INFO is a plist used as a communication channel. FMT, when
3795 non-nil, is a time format string that will be applied on the date
3796 if it consists in a single timestamp object. It defaults to
3797 `org-export-date-timestamp-format' when nil.
3799 A proper date can be a secondary string, a string or nil. It is
3800 meant to be translated with `org-export-data' or alike."
3801 (let ((date (plist-get info :date))
3802 (fmt (or fmt org-export-date-timestamp-format)))
3803 (cond ((not date) nil)
3804 ((and fmt
3805 (not (cdr date))
3806 (eq (org-element-type (car date)) 'timestamp))
3807 (org-timestamp-format (car date) fmt))
3808 (t date))))
3811 ;;;; For Links
3813 ;; `org-export-solidify-link-text' turns a string into a safer version
3814 ;; for links, replacing most non-standard characters with hyphens.
3816 ;; `org-export-get-coderef-format' returns an appropriate format
3817 ;; string for coderefs.
3819 ;; `org-export-inline-image-p' returns a non-nil value when the link
3820 ;; provided should be considered as an inline image.
3822 ;; `org-export-resolve-fuzzy-link' searches destination of fuzzy links
3823 ;; (i.e. links with "fuzzy" as type) within the parsed tree, and
3824 ;; returns an appropriate unique identifier when found, or nil.
3826 ;; `org-export-resolve-id-link' returns the first headline with
3827 ;; specified id or custom-id in parse tree, the path to the external
3828 ;; file with the id or nil when neither was found.
3830 ;; `org-export-resolve-coderef' associates a reference to a line
3831 ;; number in the element it belongs, or returns the reference itself
3832 ;; when the element isn't numbered.
3834 (defun org-export-solidify-link-text (s)
3835 "Take link text S and make a safe target out of it."
3836 (save-match-data
3837 (mapconcat 'identity (org-split-string s "[^a-zA-Z0-9_.-:]+") "-")))
3839 (defun org-export-get-coderef-format (path desc)
3840 "Return format string for code reference link.
3841 PATH is the link path. DESC is its description."
3842 (save-match-data
3843 (cond ((not desc) "%s")
3844 ((string-match (regexp-quote (concat "(" path ")")) desc)
3845 (replace-match "%s" t t desc))
3846 (t desc))))
3848 (defun org-export-inline-image-p (link &optional rules)
3849 "Non-nil if LINK object points to an inline image.
3851 Optional argument is a set of RULES defining inline images. It
3852 is an alist where associations have the following shape:
3854 \(TYPE . REGEXP)
3856 Applying a rule means apply REGEXP against LINK's path when its
3857 type is TYPE. The function will return a non-nil value if any of
3858 the provided rules is non-nil. The default rule is
3859 `org-export-default-inline-image-rule'.
3861 This only applies to links without a description."
3862 (and (not (org-element-contents link))
3863 (let ((case-fold-search t)
3864 (rules (or rules org-export-default-inline-image-rule)))
3865 (catch 'exit
3866 (mapc
3867 (lambda (rule)
3868 (and (string= (org-element-property :type link) (car rule))
3869 (string-match (cdr rule)
3870 (org-element-property :path link))
3871 (throw 'exit t)))
3872 rules)
3873 ;; Return nil if no rule matched.
3874 nil))))
3876 (defun org-export-resolve-coderef (ref info)
3877 "Resolve a code reference REF.
3879 INFO is a plist used as a communication channel.
3881 Return associated line number in source code, or REF itself,
3882 depending on src-block or example element's switches."
3883 (org-element-map (plist-get info :parse-tree) '(example-block src-block)
3884 (lambda (el)
3885 (with-temp-buffer
3886 (insert (org-trim (org-element-property :value el)))
3887 (let* ((label-fmt (regexp-quote
3888 (or (org-element-property :label-fmt el)
3889 org-coderef-label-format)))
3890 (ref-re
3891 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)\\)[ \t]*$"
3892 (replace-regexp-in-string "%s" ref label-fmt nil t))))
3893 ;; Element containing REF is found. Resolve it to either
3894 ;; a label or a line number, as needed.
3895 (when (re-search-backward ref-re nil t)
3896 (cond
3897 ((org-element-property :use-labels el) ref)
3898 ((eq (org-element-property :number-lines el) 'continued)
3899 (+ (org-export-get-loc el info) (line-number-at-pos)))
3900 (t (line-number-at-pos)))))))
3901 info 'first-match))
3903 (defun org-export-resolve-fuzzy-link (link info)
3904 "Return LINK destination.
3906 INFO is a plist holding contextual information.
3908 Return value can be an object, an element, or nil:
3910 - If LINK path matches a target object (i.e. <<path>>) return it.
3912 - If LINK path exactly matches the name affiliated keyword
3913 \(i.e. #+NAME: path) of an element, return that element.
3915 - If LINK path exactly matches any headline name, return that
3916 element. If more than one headline share that name, priority
3917 will be given to the one with the closest common ancestor, if
3918 any, or the first one in the parse tree otherwise.
3920 - Otherwise, return nil.
3922 Assume LINK type is \"fuzzy\". White spaces are not
3923 significant."
3924 (let* ((raw-path (org-element-property :path link))
3925 (match-title-p (eq (aref raw-path 0) ?*))
3926 ;; Split PATH at white spaces so matches are space
3927 ;; insensitive.
3928 (path (org-split-string
3929 (if match-title-p (substring raw-path 1) raw-path)))
3930 ;; Cache for destinations that are not position dependent.
3931 (link-cache
3932 (or (plist-get info :resolve-fuzzy-link-cache)
3933 (plist-get (setq info (plist-put info :resolve-fuzzy-link-cache
3934 (make-hash-table :test 'equal)))
3935 :resolve-fuzzy-link-cache)))
3936 (cached (gethash path link-cache 'not-found)))
3937 (cond
3938 ;; Destination is not position dependent: use cached value.
3939 ((and (not match-title-p) (not (eq cached 'not-found))) cached)
3940 ;; First try to find a matching "<<path>>" unless user specified
3941 ;; he was looking for a headline (path starts with a "*"
3942 ;; character).
3943 ((and (not match-title-p)
3944 (let ((match (org-element-map (plist-get info :parse-tree) 'target
3945 (lambda (blob)
3946 (and (equal (org-split-string
3947 (org-element-property :value blob))
3948 path)
3949 blob))
3950 info 'first-match)))
3951 (and match (puthash path match link-cache)))))
3952 ;; Then try to find an element with a matching "#+NAME: path"
3953 ;; affiliated keyword.
3954 ((and (not match-title-p)
3955 (let ((match (org-element-map (plist-get info :parse-tree)
3956 org-element-all-elements
3957 (lambda (el)
3958 (let ((name (org-element-property :name el)))
3959 (when (and name
3960 (equal (org-split-string name) path))
3961 el)))
3962 info 'first-match)))
3963 (and match (puthash path match link-cache)))))
3964 ;; Last case: link either points to a headline or to nothingness.
3965 ;; Try to find the source, with priority given to headlines with
3966 ;; the closest common ancestor. If such candidate is found,
3967 ;; return it, otherwise return nil.
3969 (let ((find-headline
3970 (function
3971 ;; Return first headline whose `:raw-value' property is
3972 ;; NAME in parse tree DATA, or nil. Statistics cookies
3973 ;; are ignored.
3974 (lambda (name data)
3975 (org-element-map data 'headline
3976 (lambda (headline)
3977 (when (equal (org-split-string
3978 (replace-regexp-in-string
3979 "\\[[0-9]+%\\]\\|\\[[0-9]+/[0-9]+\\]" ""
3980 (org-element-property :raw-value headline)))
3981 name)
3982 headline))
3983 info 'first-match)))))
3984 ;; Search among headlines sharing an ancestor with link, from
3985 ;; closest to farthest.
3986 (catch 'exit
3987 (mapc
3988 (lambda (parent)
3989 (let ((foundp (funcall find-headline path parent)))
3990 (when foundp (throw 'exit foundp))))
3991 (let ((parent-hl (org-export-get-parent-headline link)))
3992 (if (not parent-hl) (list (plist-get info :parse-tree))
3993 (cons parent-hl (org-export-get-genealogy parent-hl)))))
3994 ;; No destination found: return nil.
3995 (and (not match-title-p) (puthash path nil link-cache))))))))
3997 (defun org-export-resolve-id-link (link info)
3998 "Return headline referenced as LINK destination.
4000 INFO is a plist used as a communication channel.
4002 Return value can be the headline element matched in current parse
4003 tree, a file name or nil. Assume LINK type is either \"id\" or
4004 \"custom-id\"."
4005 (let ((id (org-element-property :path link)))
4006 ;; First check if id is within the current parse tree.
4007 (or (org-element-map (plist-get info :parse-tree) 'headline
4008 (lambda (headline)
4009 (when (or (string= (org-element-property :ID headline) id)
4010 (string= (org-element-property :CUSTOM_ID headline) id))
4011 headline))
4012 info 'first-match)
4013 ;; Otherwise, look for external files.
4014 (cdr (assoc id (plist-get info :id-alist))))))
4016 (defun org-export-resolve-radio-link (link info)
4017 "Return radio-target object referenced as LINK destination.
4019 INFO is a plist used as a communication channel.
4021 Return value can be a radio-target object or nil. Assume LINK
4022 has type \"radio\"."
4023 (let ((path (replace-regexp-in-string
4024 "[ \r\t\n]+" " " (org-element-property :path link))))
4025 (org-element-map (plist-get info :parse-tree) 'radio-target
4026 (lambda (radio)
4027 (and (eq (compare-strings
4028 (replace-regexp-in-string
4029 "[ \r\t\n]+" " " (org-element-property :value radio))
4030 nil nil path nil nil t)
4032 radio))
4033 info 'first-match)))
4036 ;;;; For References
4038 ;; `org-export-get-ordinal' associates a sequence number to any object
4039 ;; or element.
4041 (defun org-export-get-ordinal (element info &optional types predicate)
4042 "Return ordinal number of an element or object.
4044 ELEMENT is the element or object considered. INFO is the plist
4045 used as a communication channel.
4047 Optional argument TYPES, when non-nil, is a list of element or
4048 object types, as symbols, that should also be counted in.
4049 Otherwise, only provided element's type is considered.
4051 Optional argument PREDICATE is a function returning a non-nil
4052 value if the current element or object should be counted in. It
4053 accepts two arguments: the element or object being considered and
4054 the plist used as a communication channel. This allows to count
4055 only a certain type of objects (i.e. inline images).
4057 Return value is a list of numbers if ELEMENT is a headline or an
4058 item. It is nil for keywords. It represents the footnote number
4059 for footnote definitions and footnote references. If ELEMENT is
4060 a target, return the same value as if ELEMENT was the closest
4061 table, item or headline containing the target. In any other
4062 case, return the sequence number of ELEMENT among elements or
4063 objects of the same type."
4064 ;; Ordinal of a target object refer to the ordinal of the closest
4065 ;; table, item, or headline containing the object.
4066 (when (eq (org-element-type element) 'target)
4067 (setq element
4068 (loop for parent in (org-export-get-genealogy element)
4069 when
4070 (memq
4071 (org-element-type parent)
4072 '(footnote-definition footnote-reference headline item
4073 table))
4074 return parent)))
4075 (case (org-element-type element)
4076 ;; Special case 1: A headline returns its number as a list.
4077 (headline (org-export-get-headline-number element info))
4078 ;; Special case 2: An item returns its number as a list.
4079 (item (let ((struct (org-element-property :structure element)))
4080 (org-list-get-item-number
4081 (org-element-property :begin element)
4082 struct
4083 (org-list-prevs-alist struct)
4084 (org-list-parents-alist struct))))
4085 ((footnote-definition footnote-reference)
4086 (org-export-get-footnote-number element info))
4087 (otherwise
4088 (let ((counter 0))
4089 ;; Increment counter until ELEMENT is found again.
4090 (org-element-map (plist-get info :parse-tree)
4091 (or types (org-element-type element))
4092 (lambda (el)
4093 (cond
4094 ((eq element el) (1+ counter))
4095 ((not predicate) (incf counter) nil)
4096 ((funcall predicate el info) (incf counter) nil)))
4097 info 'first-match)))))
4100 ;;;; For Src-Blocks
4102 ;; `org-export-get-loc' counts number of code lines accumulated in
4103 ;; src-block or example-block elements with a "+n" switch until
4104 ;; a given element, excluded. Note: "-n" switches reset that count.
4106 ;; `org-export-unravel-code' extracts source code (along with a code
4107 ;; references alist) from an `element-block' or `src-block' type
4108 ;; element.
4110 ;; `org-export-format-code' applies a formatting function to each line
4111 ;; of code, providing relative line number and code reference when
4112 ;; appropriate. Since it doesn't access the original element from
4113 ;; which the source code is coming, it expects from the code calling
4114 ;; it to know if lines should be numbered and if code references
4115 ;; should appear.
4117 ;; Eventually, `org-export-format-code-default' is a higher-level
4118 ;; function (it makes use of the two previous functions) which handles
4119 ;; line numbering and code references inclusion, and returns source
4120 ;; code in a format suitable for plain text or verbatim output.
4122 (defun org-export-get-loc (element info)
4123 "Return accumulated lines of code up to ELEMENT.
4125 INFO is the plist used as a communication channel.
4127 ELEMENT is excluded from count."
4128 (let ((loc 0))
4129 (org-element-map (plist-get info :parse-tree)
4130 `(src-block example-block ,(org-element-type element))
4131 (lambda (el)
4132 (cond
4133 ;; ELEMENT is reached: Quit the loop.
4134 ((eq el element))
4135 ;; Only count lines from src-block and example-block elements
4136 ;; with a "+n" or "-n" switch. A "-n" switch resets counter.
4137 ((not (memq (org-element-type el) '(src-block example-block))) nil)
4138 ((let ((linums (org-element-property :number-lines el)))
4139 (when linums
4140 ;; Accumulate locs or reset them.
4141 (let ((lines (org-count-lines
4142 (org-trim (org-element-property :value el)))))
4143 (setq loc (if (eq linums 'new) lines (+ loc lines))))))
4144 ;; Return nil to stay in the loop.
4145 nil)))
4146 info 'first-match)
4147 ;; Return value.
4148 loc))
4150 (defun org-export-unravel-code (element)
4151 "Clean source code and extract references out of it.
4153 ELEMENT has either a `src-block' an `example-block' type.
4155 Return a cons cell whose CAR is the source code, cleaned from any
4156 reference and protective comma and CDR is an alist between
4157 relative line number (integer) and name of code reference on that
4158 line (string)."
4159 (let* ((line 0) refs
4160 ;; Get code and clean it. Remove blank lines at its
4161 ;; beginning and end.
4162 (code (replace-regexp-in-string
4163 "\\`\\([ \t]*\n\\)+" ""
4164 (replace-regexp-in-string
4165 "\\([ \t]*\n\\)*[ \t]*\\'" "\n"
4166 (org-element-property :value element))))
4167 ;; Get format used for references.
4168 (label-fmt (regexp-quote
4169 (or (org-element-property :label-fmt element)
4170 org-coderef-label-format)))
4171 ;; Build a regexp matching a loc with a reference.
4172 (with-ref-re
4173 (format "^.*?\\S-.*?\\([ \t]*\\(%s\\)[ \t]*\\)$"
4174 (replace-regexp-in-string
4175 "%s" "\\([-a-zA-Z0-9_ ]+\\)" label-fmt nil t))))
4176 ;; Return value.
4177 (cons
4178 ;; Code with references removed.
4179 (org-element-normalize-string
4180 (mapconcat
4181 (lambda (loc)
4182 (incf line)
4183 (if (not (string-match with-ref-re loc)) loc
4184 ;; Ref line: remove ref, and signal its position in REFS.
4185 (push (cons line (match-string 3 loc)) refs)
4186 (replace-match "" nil nil loc 1)))
4187 (org-split-string code "\n") "\n"))
4188 ;; Reference alist.
4189 refs)))
4191 (defun org-export-format-code (code fun &optional num-lines ref-alist)
4192 "Format CODE by applying FUN line-wise and return it.
4194 CODE is a string representing the code to format. FUN is
4195 a function. It must accept three arguments: a line of
4196 code (string), the current line number (integer) or nil and the
4197 reference associated to the current line (string) or nil.
4199 Optional argument NUM-LINES can be an integer representing the
4200 number of code lines accumulated until the current code. Line
4201 numbers passed to FUN will take it into account. If it is nil,
4202 FUN's second argument will always be nil. This number can be
4203 obtained with `org-export-get-loc' function.
4205 Optional argument REF-ALIST can be an alist between relative line
4206 number (i.e. ignoring NUM-LINES) and the name of the code
4207 reference on it. If it is nil, FUN's third argument will always
4208 be nil. It can be obtained through the use of
4209 `org-export-unravel-code' function."
4210 (let ((--locs (org-split-string code "\n"))
4211 (--line 0))
4212 (org-element-normalize-string
4213 (mapconcat
4214 (lambda (--loc)
4215 (incf --line)
4216 (let ((--ref (cdr (assq --line ref-alist))))
4217 (funcall fun --loc (and num-lines (+ num-lines --line)) --ref)))
4218 --locs "\n"))))
4220 (defun org-export-format-code-default (element info)
4221 "Return source code from ELEMENT, formatted in a standard way.
4223 ELEMENT is either a `src-block' or `example-block' element. INFO
4224 is a plist used as a communication channel.
4226 This function takes care of line numbering and code references
4227 inclusion. Line numbers, when applicable, appear at the
4228 beginning of the line, separated from the code by two white
4229 spaces. Code references, on the other hand, appear flushed to
4230 the right, separated by six white spaces from the widest line of
4231 code."
4232 ;; Extract code and references.
4233 (let* ((code-info (org-export-unravel-code element))
4234 (code (car code-info))
4235 (code-lines (org-split-string code "\n")))
4236 (if (null code-lines) ""
4237 (let* ((refs (and (org-element-property :retain-labels element)
4238 (cdr code-info)))
4239 ;; Handle line numbering.
4240 (num-start (case (org-element-property :number-lines element)
4241 (continued (org-export-get-loc element info))
4242 (new 0)))
4243 (num-fmt
4244 (and num-start
4245 (format "%%%ds "
4246 (length (number-to-string
4247 (+ (length code-lines) num-start))))))
4248 ;; Prepare references display, if required. Any reference
4249 ;; should start six columns after the widest line of code,
4250 ;; wrapped with parenthesis.
4251 (max-width
4252 (+ (apply 'max (mapcar 'length code-lines))
4253 (if (not num-start) 0 (length (format num-fmt num-start))))))
4254 (org-export-format-code
4255 code
4256 (lambda (loc line-num ref)
4257 (let ((number-str (and num-fmt (format num-fmt line-num))))
4258 (concat
4259 number-str
4261 (and ref
4262 (concat (make-string
4263 (- (+ 6 max-width)
4264 (+ (length loc) (length number-str))) ? )
4265 (format "(%s)" ref))))))
4266 num-start refs)))))
4269 ;;;; For Tables
4271 ;; `org-export-table-has-special-column-p' and and
4272 ;; `org-export-table-row-is-special-p' are predicates used to look for
4273 ;; meta-information about the table structure.
4275 ;; `org-table-has-header-p' tells when the rows before the first rule
4276 ;; should be considered as table's header.
4278 ;; `org-export-table-cell-width', `org-export-table-cell-alignment'
4279 ;; and `org-export-table-cell-borders' extract information from
4280 ;; a table-cell element.
4282 ;; `org-export-table-dimensions' gives the number on rows and columns
4283 ;; in the table, ignoring horizontal rules and special columns.
4284 ;; `org-export-table-cell-address', given a table-cell object, returns
4285 ;; the absolute address of a cell. On the other hand,
4286 ;; `org-export-get-table-cell-at' does the contrary.
4288 ;; `org-export-table-cell-starts-colgroup-p',
4289 ;; `org-export-table-cell-ends-colgroup-p',
4290 ;; `org-export-table-row-starts-rowgroup-p',
4291 ;; `org-export-table-row-ends-rowgroup-p',
4292 ;; `org-export-table-row-starts-header-p' and
4293 ;; `org-export-table-row-ends-header-p' indicate position of current
4294 ;; row or cell within the table.
4296 (defun org-export-table-has-special-column-p (table)
4297 "Non-nil when TABLE has a special column.
4298 All special columns will be ignored during export."
4299 ;; The table has a special column when every first cell of every row
4300 ;; has an empty value or contains a symbol among "/", "#", "!", "$",
4301 ;; "*" "_" and "^". Though, do not consider a first row containing
4302 ;; only empty cells as special.
4303 (let ((special-column-p 'empty))
4304 (catch 'exit
4305 (mapc
4306 (lambda (row)
4307 (when (eq (org-element-property :type row) 'standard)
4308 (let ((value (org-element-contents
4309 (car (org-element-contents row)))))
4310 (cond ((member value '(("/") ("#") ("!") ("$") ("*") ("_") ("^")))
4311 (setq special-column-p 'special))
4312 ((not value))
4313 (t (throw 'exit nil))))))
4314 (org-element-contents table))
4315 (eq special-column-p 'special))))
4317 (defun org-export-table-has-header-p (table info)
4318 "Non-nil when TABLE has a header.
4320 INFO is a plist used as a communication channel.
4322 A table has a header when it contains at least two row groups."
4323 (let ((cache (or (plist-get info :table-header-cache)
4324 (plist-get (setq info
4325 (plist-put info :table-header-cache
4326 (make-hash-table :test 'eq)))
4327 :table-header-cache))))
4328 (or (gethash table cache)
4329 (let ((rowgroup 1) row-flag)
4330 (puthash
4331 table
4332 (org-element-map table 'table-row
4333 (lambda (row)
4334 (cond
4335 ((> rowgroup 1) t)
4336 ((and row-flag (eq (org-element-property :type row) 'rule))
4337 (incf rowgroup) (setq row-flag nil))
4338 ((and (not row-flag) (eq (org-element-property :type row)
4339 'standard))
4340 (setq row-flag t) nil)))
4341 info 'first-match)
4342 cache)))))
4344 (defun org-export-table-row-is-special-p (table-row info)
4345 "Non-nil if TABLE-ROW is considered special.
4347 INFO is a plist used as the communication channel.
4349 All special rows will be ignored during export."
4350 (when (eq (org-element-property :type table-row) 'standard)
4351 (let ((first-cell (org-element-contents
4352 (car (org-element-contents table-row)))))
4353 ;; A row is special either when...
4355 ;; ... it starts with a field only containing "/",
4356 (equal first-cell '("/"))
4357 ;; ... the table contains a special column and the row start
4358 ;; with a marking character among, "^", "_", "$" or "!",
4359 (and (org-export-table-has-special-column-p
4360 (org-export-get-parent table-row))
4361 (member first-cell '(("^") ("_") ("$") ("!"))))
4362 ;; ... it contains only alignment cookies and empty cells.
4363 (let ((special-row-p 'empty))
4364 (catch 'exit
4365 (mapc
4366 (lambda (cell)
4367 (let ((value (org-element-contents cell)))
4368 ;; Since VALUE is a secondary string, the following
4369 ;; checks avoid expanding it with `org-export-data'.
4370 (cond ((not value))
4371 ((and (not (cdr value))
4372 (stringp (car value))
4373 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'"
4374 (car value)))
4375 (setq special-row-p 'cookie))
4376 (t (throw 'exit nil)))))
4377 (org-element-contents table-row))
4378 (eq special-row-p 'cookie)))))))
4380 (defun org-export-table-row-group (table-row info)
4381 "Return TABLE-ROW's group number, as an integer.
4383 INFO is a plist used as the communication channel.
4385 Return value is the group number, as an integer, or nil for
4386 special rows and rows separators. First group is also table's
4387 header."
4388 (let ((cache (or (plist-get info :table-row-group-cache)
4389 (plist-get (setq info
4390 (plist-put info :table-row-group-cache
4391 (make-hash-table :test 'eq)))
4392 :table-row-group-cache))))
4393 (cond ((gethash table-row cache))
4394 ((eq (org-element-property :type table-row) 'rule) nil)
4395 (t (let ((group 0) row-flag)
4396 (org-element-map (org-export-get-parent table-row) 'table-row
4397 (lambda (row)
4398 (if (eq (org-element-property :type row) 'rule)
4399 (setq row-flag nil)
4400 (unless row-flag (incf group) (setq row-flag t)))
4401 (when (eq table-row row) (puthash table-row group cache)))
4402 info 'first-match))))))
4404 (defun org-export-table-cell-width (table-cell info)
4405 "Return TABLE-CELL contents width.
4407 INFO is a plist used as the communication channel.
4409 Return value is the width given by the last width cookie in the
4410 same column as TABLE-CELL, or nil."
4411 (let* ((row (org-export-get-parent table-cell))
4412 (table (org-export-get-parent row))
4413 (column (let ((cells (org-element-contents row)))
4414 (- (length cells) (length (memq table-cell cells)))))
4415 (cache (or (plist-get info :table-cell-width-cache)
4416 (plist-get (setq info
4417 (plist-put info :table-cell-width-cache
4418 (make-hash-table :test 'equal)))
4419 :table-cell-width-cache)))
4420 (key (cons table column))
4421 (value (gethash key cache 'no-result)))
4422 (if (not (eq value 'no-result)) value
4423 (let (cookie-width)
4424 (dolist (row (org-element-contents table)
4425 (puthash key cookie-width cache))
4426 (when (org-export-table-row-is-special-p row info)
4427 ;; In a special row, try to find a width cookie at COLUMN.
4428 (let* ((value (org-element-contents
4429 (elt (org-element-contents row) column)))
4430 (cookie (car value)))
4431 ;; The following checks avoid expanding unnecessarily
4432 ;; the cell with `org-export-data'.
4433 (when (and value
4434 (not (cdr value))
4435 (stringp cookie)
4436 (string-match "\\`<[lrc]?\\([0-9]+\\)?>\\'" cookie)
4437 (match-string 1 cookie))
4438 (setq cookie-width
4439 (string-to-number (match-string 1 cookie)))))))))))
4441 (defun org-export-table-cell-alignment (table-cell info)
4442 "Return TABLE-CELL contents alignment.
4444 INFO is a plist used as the communication channel.
4446 Return alignment as specified by the last alignment cookie in the
4447 same column as TABLE-CELL. If no such cookie is found, a default
4448 alignment value will be deduced from fraction of numbers in the
4449 column (see `org-table-number-fraction' for more information).
4450 Possible values are `left', `right' and `center'."
4451 (let* ((row (org-export-get-parent table-cell))
4452 (table (org-export-get-parent row))
4453 (column (let ((cells (org-element-contents row)))
4454 (- (length cells) (length (memq table-cell cells)))))
4455 (cache (or (plist-get info :table-cell-alignment-cache)
4456 (plist-get (setq info
4457 (plist-put info :table-cell-alignment-cache
4458 (make-hash-table :test 'equal)))
4459 :table-cell-alignment-cache))))
4460 (or (gethash (cons table column) cache)
4461 (let ((number-cells 0)
4462 (total-cells 0)
4463 cookie-align
4464 previous-cell-number-p)
4465 (dolist (row (org-element-contents (org-export-get-parent row)))
4466 (cond
4467 ;; In a special row, try to find an alignment cookie at
4468 ;; COLUMN.
4469 ((org-export-table-row-is-special-p row info)
4470 (let ((value (org-element-contents
4471 (elt (org-element-contents row) column))))
4472 ;; Since VALUE is a secondary string, the following
4473 ;; checks avoid useless expansion through
4474 ;; `org-export-data'.
4475 (when (and value
4476 (not (cdr value))
4477 (stringp (car value))
4478 (string-match "\\`<\\([lrc]\\)?\\([0-9]+\\)?>\\'"
4479 (car value))
4480 (match-string 1 (car value)))
4481 (setq cookie-align (match-string 1 (car value))))))
4482 ;; Ignore table rules.
4483 ((eq (org-element-property :type row) 'rule))
4484 ;; In a standard row, check if cell's contents are
4485 ;; expressing some kind of number. Increase NUMBER-CELLS
4486 ;; accordingly. Though, don't bother if an alignment
4487 ;; cookie has already defined cell's alignment.
4488 ((not cookie-align)
4489 (let ((value (org-export-data
4490 (org-element-contents
4491 (elt (org-element-contents row) column))
4492 info)))
4493 (incf total-cells)
4494 ;; Treat an empty cell as a number if it follows
4495 ;; a number.
4496 (if (not (or (string-match org-table-number-regexp value)
4497 (and (string= value "") previous-cell-number-p)))
4498 (setq previous-cell-number-p nil)
4499 (setq previous-cell-number-p t)
4500 (incf number-cells))))))
4501 ;; Return value. Alignment specified by cookies has
4502 ;; precedence over alignment deduced from cell's contents.
4503 (puthash (cons table column)
4504 (cond ((equal cookie-align "l") 'left)
4505 ((equal cookie-align "r") 'right)
4506 ((equal cookie-align "c") 'center)
4507 ((>= (/ (float number-cells) total-cells)
4508 org-table-number-fraction)
4509 'right)
4510 (t 'left))
4511 cache)))))
4513 (defun org-export-table-cell-borders (table-cell info)
4514 "Return TABLE-CELL borders.
4516 INFO is a plist used as a communication channel.
4518 Return value is a list of symbols, or nil. Possible values are:
4519 `top', `bottom', `above', `below', `left' and `right'. Note:
4520 `top' (resp. `bottom') only happen for a cell in the first
4521 row (resp. last row) of the table, ignoring table rules, if any.
4523 Returned borders ignore special rows."
4524 (let* ((row (org-export-get-parent table-cell))
4525 (table (org-export-get-parent-table table-cell))
4526 borders)
4527 ;; Top/above border? TABLE-CELL has a border above when a rule
4528 ;; used to demarcate row groups can be found above. Hence,
4529 ;; finding a rule isn't sufficient to push `above' in BORDERS:
4530 ;; another regular row has to be found above that rule.
4531 (let (rule-flag)
4532 (catch 'exit
4533 (mapc (lambda (row)
4534 (cond ((eq (org-element-property :type row) 'rule)
4535 (setq rule-flag t))
4536 ((not (org-export-table-row-is-special-p row info))
4537 (if rule-flag (throw 'exit (push 'above borders))
4538 (throw 'exit nil)))))
4539 ;; Look at every row before the current one.
4540 (cdr (memq row (reverse (org-element-contents table)))))
4541 ;; No rule above, or rule found starts the table (ignoring any
4542 ;; special row): TABLE-CELL is at the top of the table.
4543 (when rule-flag (push 'above borders))
4544 (push 'top borders)))
4545 ;; Bottom/below border? TABLE-CELL has a border below when next
4546 ;; non-regular row below is a rule.
4547 (let (rule-flag)
4548 (catch 'exit
4549 (mapc (lambda (row)
4550 (cond ((eq (org-element-property :type row) 'rule)
4551 (setq rule-flag t))
4552 ((not (org-export-table-row-is-special-p row info))
4553 (if rule-flag (throw 'exit (push 'below borders))
4554 (throw 'exit nil)))))
4555 ;; Look at every row after the current one.
4556 (cdr (memq row (org-element-contents table))))
4557 ;; No rule below, or rule found ends the table (modulo some
4558 ;; special row): TABLE-CELL is at the bottom of the table.
4559 (when rule-flag (push 'below borders))
4560 (push 'bottom borders)))
4561 ;; Right/left borders? They can only be specified by column
4562 ;; groups. Column groups are defined in a row starting with "/".
4563 ;; Also a column groups row only contains "<", "<>", ">" or blank
4564 ;; cells.
4565 (catch 'exit
4566 (let ((column (let ((cells (org-element-contents row)))
4567 (- (length cells) (length (memq table-cell cells))))))
4568 (mapc
4569 (lambda (row)
4570 (unless (eq (org-element-property :type row) 'rule)
4571 (when (equal (org-element-contents
4572 (car (org-element-contents row)))
4573 '("/"))
4574 (let ((column-groups
4575 (mapcar
4576 (lambda (cell)
4577 (let ((value (org-element-contents cell)))
4578 (when (member value '(("<") ("<>") (">") nil))
4579 (car value))))
4580 (org-element-contents row))))
4581 ;; There's a left border when previous cell, if
4582 ;; any, ends a group, or current one starts one.
4583 (when (or (and (not (zerop column))
4584 (member (elt column-groups (1- column))
4585 '(">" "<>")))
4586 (member (elt column-groups column) '("<" "<>")))
4587 (push 'left borders))
4588 ;; There's a right border when next cell, if any,
4589 ;; starts a group, or current one ends one.
4590 (when (or (and (/= (1+ column) (length column-groups))
4591 (member (elt column-groups (1+ column))
4592 '("<" "<>")))
4593 (member (elt column-groups column) '(">" "<>")))
4594 (push 'right borders))
4595 (throw 'exit nil)))))
4596 ;; Table rows are read in reverse order so last column groups
4597 ;; row has precedence over any previous one.
4598 (reverse (org-element-contents table)))))
4599 ;; Return value.
4600 borders))
4602 (defun org-export-table-cell-starts-colgroup-p (table-cell info)
4603 "Non-nil when TABLE-CELL is at the beginning of a row group.
4604 INFO is a plist used as a communication channel."
4605 ;; A cell starts a column group either when it is at the beginning
4606 ;; of a row (or after the special column, if any) or when it has
4607 ;; a left border.
4608 (or (eq (org-element-map (org-export-get-parent table-cell) 'table-cell
4609 'identity info 'first-match)
4610 table-cell)
4611 (memq 'left (org-export-table-cell-borders table-cell info))))
4613 (defun org-export-table-cell-ends-colgroup-p (table-cell info)
4614 "Non-nil when TABLE-CELL is at the end of a row group.
4615 INFO is a plist used as a communication channel."
4616 ;; A cell ends a column group either when it is at the end of a row
4617 ;; or when it has a right border.
4618 (or (eq (car (last (org-element-contents
4619 (org-export-get-parent table-cell))))
4620 table-cell)
4621 (memq 'right (org-export-table-cell-borders table-cell info))))
4623 (defun org-export-table-row-starts-rowgroup-p (table-row info)
4624 "Non-nil when TABLE-ROW is at the beginning of a column group.
4625 INFO is a plist used as a communication channel."
4626 (unless (or (eq (org-element-property :type table-row) 'rule)
4627 (org-export-table-row-is-special-p table-row info))
4628 (let ((borders (org-export-table-cell-borders
4629 (car (org-element-contents table-row)) info)))
4630 (or (memq 'top borders) (memq 'above borders)))))
4632 (defun org-export-table-row-ends-rowgroup-p (table-row info)
4633 "Non-nil when TABLE-ROW is at the end of a column group.
4634 INFO is a plist used as a communication channel."
4635 (unless (or (eq (org-element-property :type table-row) 'rule)
4636 (org-export-table-row-is-special-p table-row info))
4637 (let ((borders (org-export-table-cell-borders
4638 (car (org-element-contents table-row)) info)))
4639 (or (memq 'bottom borders) (memq 'below borders)))))
4641 (defun org-export-table-row-starts-header-p (table-row info)
4642 "Non-nil when TABLE-ROW is the first table header's row.
4643 INFO is a plist used as a communication channel."
4644 (and (org-export-table-has-header-p
4645 (org-export-get-parent-table table-row) info)
4646 (org-export-table-row-starts-rowgroup-p table-row info)
4647 (= (org-export-table-row-group table-row info) 1)))
4649 (defun org-export-table-row-ends-header-p (table-row info)
4650 "Non-nil when TABLE-ROW is the last table header's row.
4651 INFO is a plist used as a communication channel."
4652 (and (org-export-table-has-header-p
4653 (org-export-get-parent-table table-row) info)
4654 (org-export-table-row-ends-rowgroup-p table-row info)
4655 (= (org-export-table-row-group table-row info) 1)))
4657 (defun org-export-table-row-number (table-row info)
4658 "Return TABLE-ROW number.
4659 INFO is a plist used as a communication channel. Return value is
4660 zero-based and ignores separators. The function returns nil for
4661 special colums and separators."
4662 (when (and (eq (org-element-property :type table-row) 'standard)
4663 (not (org-export-table-row-is-special-p table-row info)))
4664 (let ((number 0))
4665 (org-element-map (org-export-get-parent-table table-row) 'table-row
4666 (lambda (row)
4667 (cond ((eq row table-row) number)
4668 ((eq (org-element-property :type row) 'standard)
4669 (incf number) nil)))
4670 info 'first-match))))
4672 (defun org-export-table-dimensions (table info)
4673 "Return TABLE dimensions.
4675 INFO is a plist used as a communication channel.
4677 Return value is a CONS like (ROWS . COLUMNS) where
4678 ROWS (resp. COLUMNS) is the number of exportable
4679 rows (resp. columns)."
4680 (let (first-row (columns 0) (rows 0))
4681 ;; Set number of rows, and extract first one.
4682 (org-element-map table 'table-row
4683 (lambda (row)
4684 (when (eq (org-element-property :type row) 'standard)
4685 (incf rows)
4686 (unless first-row (setq first-row row)))) info)
4687 ;; Set number of columns.
4688 (org-element-map first-row 'table-cell (lambda (cell) (incf columns)) info)
4689 ;; Return value.
4690 (cons rows columns)))
4692 (defun org-export-table-cell-address (table-cell info)
4693 "Return address of a regular TABLE-CELL object.
4695 TABLE-CELL is the cell considered. INFO is a plist used as
4696 a communication channel.
4698 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4699 zero-based index. Only exportable cells are considered. The
4700 function returns nil for other cells."
4701 (let* ((table-row (org-export-get-parent table-cell))
4702 (row-number (org-export-table-row-number table-row info)))
4703 (when row-number
4704 (cons row-number
4705 (let ((col-count 0))
4706 (org-element-map table-row 'table-cell
4707 (lambda (cell)
4708 (if (eq cell table-cell) col-count (incf col-count) nil))
4709 info 'first-match))))))
4711 (defun org-export-get-table-cell-at (address table info)
4712 "Return regular table-cell object at ADDRESS in TABLE.
4714 Address is a CONS cell (ROW . COLUMN), where ROW and COLUMN are
4715 zero-based index. TABLE is a table type element. INFO is
4716 a plist used as a communication channel.
4718 If no table-cell, among exportable cells, is found at ADDRESS,
4719 return nil."
4720 (let ((column-pos (cdr address)) (column-count 0))
4721 (org-element-map
4722 ;; Row at (car address) or nil.
4723 (let ((row-pos (car address)) (row-count 0))
4724 (org-element-map table 'table-row
4725 (lambda (row)
4726 (cond ((eq (org-element-property :type row) 'rule) nil)
4727 ((= row-count row-pos) row)
4728 (t (incf row-count) nil)))
4729 info 'first-match))
4730 'table-cell
4731 (lambda (cell)
4732 (if (= column-count column-pos) cell
4733 (incf column-count) nil))
4734 info 'first-match)))
4737 ;;;; For Tables Of Contents
4739 ;; `org-export-collect-headlines' builds a list of all exportable
4740 ;; headline elements, maybe limited to a certain depth. One can then
4741 ;; easily parse it and transcode it.
4743 ;; Building lists of tables, figures or listings is quite similar.
4744 ;; Once the generic function `org-export-collect-elements' is defined,
4745 ;; `org-export-collect-tables', `org-export-collect-figures' and
4746 ;; `org-export-collect-listings' can be derived from it.
4748 (defun org-export-collect-headlines (info &optional n)
4749 "Collect headlines in order to build a table of contents.
4751 INFO is a plist used as a communication channel.
4753 When optional argument N is an integer, it specifies the depth of
4754 the table of contents. Otherwise, it is set to the value of the
4755 last headline level. See `org-export-headline-levels' for more
4756 information.
4758 Return a list of all exportable headlines as parsed elements.
4759 Footnote sections, if any, will be ignored."
4760 (unless (wholenump n) (setq n (plist-get info :headline-levels)))
4761 (org-element-map (plist-get info :parse-tree) 'headline
4762 (lambda (headline)
4763 (unless (org-element-property :footnote-section-p headline)
4764 ;; Strip contents from HEADLINE.
4765 (let ((relative-level (org-export-get-relative-level headline info)))
4766 (unless (> relative-level n) headline))))
4767 info))
4769 (defun org-export-collect-elements (type info &optional predicate)
4770 "Collect referenceable elements of a determined type.
4772 TYPE can be a symbol or a list of symbols specifying element
4773 types to search. Only elements with a caption are collected.
4775 INFO is a plist used as a communication channel.
4777 When non-nil, optional argument PREDICATE is a function accepting
4778 one argument, an element of type TYPE. It returns a non-nil
4779 value when that element should be collected.
4781 Return a list of all elements found, in order of appearance."
4782 (org-element-map (plist-get info :parse-tree) type
4783 (lambda (element)
4784 (and (org-element-property :caption element)
4785 (or (not predicate) (funcall predicate element))
4786 element))
4787 info))
4789 (defun org-export-collect-tables (info)
4790 "Build a list of tables.
4791 INFO is a plist used as a communication channel.
4793 Return a list of table elements with a caption."
4794 (org-export-collect-elements 'table info))
4796 (defun org-export-collect-figures (info predicate)
4797 "Build a list of figures.
4799 INFO is a plist used as a communication channel. PREDICATE is
4800 a function which accepts one argument: a paragraph element and
4801 whose return value is non-nil when that element should be
4802 collected.
4804 A figure is a paragraph type element, with a caption, verifying
4805 PREDICATE. The latter has to be provided since a \"figure\" is
4806 a vague concept that may depend on back-end.
4808 Return a list of elements recognized as figures."
4809 (org-export-collect-elements 'paragraph info predicate))
4811 (defun org-export-collect-listings (info)
4812 "Build a list of src blocks.
4814 INFO is a plist used as a communication channel.
4816 Return a list of src-block elements with a caption."
4817 (org-export-collect-elements 'src-block info))
4820 ;;;; Smart Quotes
4822 ;; The main function for the smart quotes sub-system is
4823 ;; `org-export-activate-smart-quotes', which replaces every quote in
4824 ;; a given string from the parse tree with its "smart" counterpart.
4826 ;; Dictionary for smart quotes is stored in
4827 ;; `org-export-smart-quotes-alist'.
4829 ;; Internally, regexps matching potential smart quotes (checks at
4830 ;; string boundaries are also necessary) are defined in
4831 ;; `org-export-smart-quotes-regexps'.
4833 (defconst org-export-smart-quotes-alist
4834 '(("da"
4835 ;; one may use: »...«, "...", ›...‹, or '...'.
4836 ;; http://sproget.dk/raad-og-regler/retskrivningsregler/retskrivningsregler/a7-40-60/a7-58-anforselstegn/
4837 ;; LaTeX quotes require Babel!
4838 (opening-double-quote :utf-8 "»" :html "&raquo;" :latex ">>"
4839 :texinfo "@guillemetright{}")
4840 (closing-double-quote :utf-8 "«" :html "&laquo;" :latex "<<"
4841 :texinfo "@guillemetleft{}")
4842 (opening-single-quote :utf-8 "›" :html "&rsaquo;" :latex "\\frq{}"
4843 :texinfo "@guilsinglright{}")
4844 (closing-single-quote :utf-8 "‹" :html "&lsaquo;" :latex "\\flq{}"
4845 :texinfo "@guilsingleft{}")
4846 (apostrophe :utf-8 "’" :html "&rsquo;"))
4847 ("de"
4848 (opening-double-quote :utf-8 "„" :html "&bdquo;" :latex "\"`"
4849 :texinfo "@quotedblbase{}")
4850 (closing-double-quote :utf-8 "“" :html "&ldquo;" :latex "\"'"
4851 :texinfo "@quotedblleft{}")
4852 (opening-single-quote :utf-8 "‚" :html "&sbquo;" :latex "\\glq{}"
4853 :texinfo "@quotesinglbase{}")
4854 (closing-single-quote :utf-8 "‘" :html "&lsquo;" :latex "\\grq{}"
4855 :texinfo "@quoteleft{}")
4856 (apostrophe :utf-8 "’" :html "&rsquo;"))
4857 ("en"
4858 (opening-double-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4859 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4860 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4861 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4862 (apostrophe :utf-8 "’" :html "&rsquo;"))
4863 ("es"
4864 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4865 :texinfo "@guillemetleft{}")
4866 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4867 :texinfo "@guillemetright{}")
4868 (opening-single-quote :utf-8 "“" :html "&ldquo;" :latex "``" :texinfo "``")
4869 (closing-single-quote :utf-8 "”" :html "&rdquo;" :latex "''" :texinfo "''")
4870 (apostrophe :utf-8 "’" :html "&rsquo;"))
4871 ("fr"
4872 (opening-double-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4873 :texinfo "@guillemetleft{}@tie{}")
4874 (closing-double-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4875 :texinfo "@tie{}@guillemetright{}")
4876 (opening-single-quote :utf-8 "« " :html "&laquo;&nbsp;" :latex "\\og "
4877 :texinfo "@guillemetleft{}@tie{}")
4878 (closing-single-quote :utf-8 " »" :html "&nbsp;&raquo;" :latex "\\fg{}"
4879 :texinfo "@tie{}@guillemetright{}")
4880 (apostrophe :utf-8 "’" :html "&rsquo;"))
4881 ("no"
4882 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4883 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4884 :texinfo "@guillemetleft{}")
4885 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4886 :texinfo "@guillemetright{}")
4887 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4888 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4889 (apostrophe :utf-8 "’" :html "&rsquo;"))
4890 ("nb"
4891 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4892 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4893 :texinfo "@guillemetleft{}")
4894 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4895 :texinfo "@guillemetright{}")
4896 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4897 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4898 (apostrophe :utf-8 "’" :html "&rsquo;"))
4899 ("nn"
4900 ;; https://nn.wikipedia.org/wiki/Sitatteikn
4901 (opening-double-quote :utf-8 "«" :html "&laquo;" :latex "\\guillemotleft{}"
4902 :texinfo "@guillemetleft{}")
4903 (closing-double-quote :utf-8 "»" :html "&raquo;" :latex "\\guillemotright{}"
4904 :texinfo "@guillemetright{}")
4905 (opening-single-quote :utf-8 "‘" :html "&lsquo;" :latex "`" :texinfo "`")
4906 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "'" :texinfo "'")
4907 (apostrophe :utf-8 "’" :html "&rsquo;"))
4908 ("sv"
4909 ;; based on https://sv.wikipedia.org/wiki/Citattecken
4910 (opening-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
4911 (closing-double-quote :utf-8 "”" :html "&rdquo;" :latex "’’" :texinfo "’’")
4912 (opening-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "`")
4913 (closing-single-quote :utf-8 "’" :html "&rsquo;" :latex "’" :texinfo "'")
4914 (apostrophe :utf-8 "’" :html "&rsquo;"))
4916 "Smart quotes translations.
4918 Alist whose CAR is a language string and CDR is an alist with
4919 quote type as key and a plist associating various encodings to
4920 their translation as value.
4922 A quote type can be any symbol among `opening-double-quote',
4923 `closing-double-quote', `opening-single-quote',
4924 `closing-single-quote' and `apostrophe'.
4926 Valid encodings include `:utf-8', `:html', `:latex' and
4927 `:texinfo'.
4929 If no translation is found, the quote character is left as-is.")
4931 (defconst org-export-smart-quotes-regexps
4932 (list
4933 ;; Possible opening quote at beginning of string.
4934 "\\`\\([\"']\\)\\(\\w\\|\\s.\\|\\s_\\)"
4935 ;; Possible closing quote at beginning of string.
4936 "\\`\\([\"']\\)\\(\\s-\\|\\s)\\|\\s.\\)"
4937 ;; Possible apostrophe at beginning of string.
4938 "\\`\\('\\)\\S-"
4939 ;; Opening single and double quotes.
4940 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\(?:\\w\\|\\s.\\|\\s_\\)"
4941 ;; Closing single and double quotes.
4942 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\(?:\\s-\\|\\s)\\|\\s.\\)"
4943 ;; Apostrophe.
4944 "\\S-\\('\\)\\S-"
4945 ;; Possible opening quote at end of string.
4946 "\\(?:\\s-\\|\\s(\\)\\([\"']\\)\\'"
4947 ;; Possible closing quote at end of string.
4948 "\\(?:\\w\\|\\s.\\|\\s_\\)\\([\"']\\)\\'"
4949 ;; Possible apostrophe at end of string.
4950 "\\S-\\('\\)\\'")
4951 "List of regexps matching a quote or an apostrophe.
4952 In every regexp, quote or apostrophe matched is put in group 1.")
4954 (defun org-export-activate-smart-quotes (s encoding info &optional original)
4955 "Replace regular quotes with \"smart\" quotes in string S.
4957 ENCODING is a symbol among `:html', `:latex', `:texinfo' and
4958 `:utf-8'. INFO is a plist used as a communication channel.
4960 The function has to retrieve information about string
4961 surroundings in parse tree. It can only happen with an
4962 unmodified string. Thus, if S has already been through another
4963 process, a non-nil ORIGINAL optional argument will provide that
4964 original string.
4966 Return the new string."
4967 (if (equal s "") ""
4968 (let* ((prev (org-export-get-previous-element (or original s) info))
4969 ;; Try to be flexible when computing number of blanks
4970 ;; before object. The previous object may be a string
4971 ;; introduced by the back-end and not completely parsed.
4972 (pre-blank (and prev
4973 (or (org-element-property :post-blank prev)
4974 ;; A string with missing `:post-blank'
4975 ;; property.
4976 (and (stringp prev)
4977 (string-match " *\\'" prev)
4978 (length (match-string 0 prev)))
4979 ;; Fallback value.
4980 0)))
4981 (next (org-export-get-next-element (or original s) info))
4982 (get-smart-quote
4983 (lambda (q type)
4984 ;; Return smart quote associated to a give quote Q, as
4985 ;; a string. TYPE is a symbol among `open', `close' and
4986 ;; `apostrophe'.
4987 (let ((key (case type
4988 (apostrophe 'apostrophe)
4989 (open (if (equal "'" q) 'opening-single-quote
4990 'opening-double-quote))
4991 (otherwise (if (equal "'" q) 'closing-single-quote
4992 'closing-double-quote)))))
4993 (or (plist-get
4994 (cdr (assq key
4995 (cdr (assoc (plist-get info :language)
4996 org-export-smart-quotes-alist))))
4997 encoding)
4998 q)))))
4999 (if (or (equal "\"" s) (equal "'" s))
5000 ;; Only a quote: no regexp can match. We have to check both
5001 ;; sides and decide what to do.
5002 (cond ((and (not prev) (not next)) s)
5003 ((not prev) (funcall get-smart-quote s 'open))
5004 ((and (not next) (zerop pre-blank))
5005 (funcall get-smart-quote s 'close))
5006 ((not next) s)
5007 ((zerop pre-blank) (funcall get-smart-quote s 'apostrophe))
5008 (t (funcall get-smart-quote 'open)))
5009 ;; 1. Replace quote character at the beginning of S.
5010 (cond
5011 ;; Apostrophe?
5012 ((and prev (zerop pre-blank)
5013 (string-match (nth 2 org-export-smart-quotes-regexps) s))
5014 (setq s (replace-match
5015 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
5016 nil t s 1)))
5017 ;; Closing quote?
5018 ((and prev (zerop pre-blank)
5019 (string-match (nth 1 org-export-smart-quotes-regexps) s))
5020 (setq s (replace-match
5021 (funcall get-smart-quote (match-string 1 s) 'close)
5022 nil t s 1)))
5023 ;; Opening quote?
5024 ((and (or (not prev) (> pre-blank 0))
5025 (string-match (nth 0 org-export-smart-quotes-regexps) s))
5026 (setq s (replace-match
5027 (funcall get-smart-quote (match-string 1 s) 'open)
5028 nil t s 1))))
5029 ;; 2. Replace quotes in the middle of the string.
5030 (setq s (replace-regexp-in-string
5031 ;; Opening quotes.
5032 (nth 3 org-export-smart-quotes-regexps)
5033 (lambda (text)
5034 (funcall get-smart-quote (match-string 1 text) 'open))
5035 s nil t 1))
5036 (setq s (replace-regexp-in-string
5037 ;; Closing quotes.
5038 (nth 4 org-export-smart-quotes-regexps)
5039 (lambda (text)
5040 (funcall get-smart-quote (match-string 1 text) 'close))
5041 s nil t 1))
5042 (setq s (replace-regexp-in-string
5043 ;; Apostrophes.
5044 (nth 5 org-export-smart-quotes-regexps)
5045 (lambda (text)
5046 (funcall get-smart-quote (match-string 1 text) 'apostrophe))
5047 s nil t 1))
5048 ;; 3. Replace quote character at the end of S.
5049 (cond
5050 ;; Apostrophe?
5051 ((and next (string-match (nth 8 org-export-smart-quotes-regexps) s))
5052 (setq s (replace-match
5053 (funcall get-smart-quote (match-string 1 s) 'apostrophe)
5054 nil t s 1)))
5055 ;; Closing quote?
5056 ((and (not next)
5057 (string-match (nth 7 org-export-smart-quotes-regexps) s))
5058 (setq s (replace-match
5059 (funcall get-smart-quote (match-string 1 s) 'close)
5060 nil t s 1)))
5061 ;; Opening quote?
5062 ((and next (string-match (nth 6 org-export-smart-quotes-regexps) s))
5063 (setq s (replace-match
5064 (funcall get-smart-quote (match-string 1 s) 'open)
5065 nil t s 1))))
5066 ;; Return string with smart quotes.
5067 s))))
5069 ;;;; Topology
5071 ;; Here are various functions to retrieve information about the
5072 ;; neighbourhood of a given element or object. Neighbours of interest
5073 ;; are direct parent (`org-export-get-parent'), parent headline
5074 ;; (`org-export-get-parent-headline'), first element containing an
5075 ;; object, (`org-export-get-parent-element'), parent table
5076 ;; (`org-export-get-parent-table'), previous element or object
5077 ;; (`org-export-get-previous-element') and next element or object
5078 ;; (`org-export-get-next-element').
5080 ;; `org-export-get-genealogy' returns the full genealogy of a given
5081 ;; element or object, from closest parent to full parse tree.
5083 (defsubst org-export-get-parent (blob)
5084 "Return BLOB parent or nil.
5085 BLOB is the element or object considered."
5086 (org-element-property :parent blob))
5088 (defun org-export-get-genealogy (blob)
5089 "Return full genealogy relative to a given element or object.
5091 BLOB is the element or object being considered.
5093 Ancestors are returned from closest to farthest, the last one
5094 being the full parse tree."
5095 (let (genealogy (parent blob))
5096 (while (setq parent (org-element-property :parent parent))
5097 (push parent genealogy))
5098 (nreverse genealogy)))
5100 (defun org-export-get-parent-headline (blob)
5101 "Return BLOB parent headline or nil.
5102 BLOB is the element or object being considered."
5103 (let ((parent blob))
5104 (while (and (setq parent (org-element-property :parent parent))
5105 (not (eq (org-element-type parent) 'headline))))
5106 parent))
5108 (defun org-export-get-parent-element (object)
5109 "Return first element containing OBJECT or nil.
5110 OBJECT is the object to consider."
5111 (let ((parent object))
5112 (while (and (setq parent (org-element-property :parent parent))
5113 (memq (org-element-type parent) org-element-all-objects)))
5114 parent))
5116 (defun org-export-get-parent-table (object)
5117 "Return OBJECT parent table or nil.
5118 OBJECT is either a `table-cell' or `table-element' type object."
5119 (let ((parent object))
5120 (while (and (setq parent (org-element-property :parent parent))
5121 (not (eq (org-element-type parent) 'table))))
5122 parent))
5124 (defun org-export-get-previous-element (blob info &optional n)
5125 "Return previous element or object.
5127 BLOB is an element or object. INFO is a plist used as
5128 a communication channel. Return previous exportable element or
5129 object, a string, or nil.
5131 When optional argument N is a positive integer, return a list
5132 containing up to N siblings before BLOB, from farthest to
5133 closest. With any other non-nil value, return a list containing
5134 all of them."
5135 (let ((siblings
5136 ;; An object can belong to the contents of its parent or
5137 ;; to a secondary string. We check the latter option
5138 ;; first.
5139 (let ((parent (org-export-get-parent blob)))
5140 (or (and (not (memq (org-element-type blob)
5141 org-element-all-elements))
5142 (let ((sec-value
5143 (org-element-property
5144 (cdr (assq (org-element-type parent)
5145 org-element-secondary-value-alist))
5146 parent)))
5147 (and (memq blob sec-value) sec-value)))
5148 (org-element-contents parent))))
5149 prev)
5150 (catch 'exit
5151 (mapc (lambda (obj)
5152 (cond ((memq obj (plist-get info :ignore-list)))
5153 ((null n) (throw 'exit obj))
5154 ((not (wholenump n)) (push obj prev))
5155 ((zerop n) (throw 'exit prev))
5156 (t (decf n) (push obj prev))))
5157 (cdr (memq blob (reverse siblings))))
5158 prev)))
5160 (defun org-export-get-next-element (blob info &optional n)
5161 "Return next element or object.
5163 BLOB is an element or object. INFO is a plist used as
5164 a communication channel. Return next exportable element or
5165 object, a string, or nil.
5167 When optional argument N is a positive integer, return a list
5168 containing up to N siblings after BLOB, from closest to farthest.
5169 With any other non-nil value, return a list containing all of
5170 them."
5171 (let ((siblings
5172 ;; An object can belong to the contents of its parent or to
5173 ;; a secondary string. We check the latter option first.
5174 (let ((parent (org-export-get-parent blob)))
5175 (or (and (not (memq (org-element-type blob)
5176 org-element-all-objects))
5177 (let ((sec-value
5178 (org-element-property
5179 (cdr (assq (org-element-type parent)
5180 org-element-secondary-value-alist))
5181 parent)))
5182 (cdr (memq blob sec-value))))
5183 (cdr (memq blob (org-element-contents parent))))))
5184 next)
5185 (catch 'exit
5186 (mapc (lambda (obj)
5187 (cond ((memq obj (plist-get info :ignore-list)))
5188 ((null n) (throw 'exit obj))
5189 ((not (wholenump n)) (push obj next))
5190 ((zerop n) (throw 'exit (nreverse next)))
5191 (t (decf n) (push obj next))))
5192 siblings)
5193 (nreverse next))))
5196 ;;;; Translation
5198 ;; `org-export-translate' translates a string according to the language
5199 ;; specified by the LANGUAGE keyword. `org-export-dictionary' contains
5200 ;; the dictionary used for the translation.
5202 (defconst org-export-dictionary
5203 '(("%e %n: %c"
5204 ("fr" :default "%e %n : %c" :html "%e&nbsp;%n&nbsp;: %c"))
5205 ("Author"
5206 ("ca" :default "Autor")
5207 ("cs" :default "Autor")
5208 ("da" :default "Forfatter")
5209 ("de" :default "Autor")
5210 ("eo" :html "A&#365;toro")
5211 ("es" :default "Autor")
5212 ("fi" :html "Tekij&auml;")
5213 ("fr" :default "Auteur")
5214 ("hu" :default "Szerz&otilde;")
5215 ("is" :html "H&ouml;fundur")
5216 ("it" :default "Autore")
5217 ("ja" :html "&#33879;&#32773;" :utf-8 "著者")
5218 ("nl" :default "Auteur")
5219 ("no" :default "Forfatter")
5220 ("nb" :default "Forfatter")
5221 ("nn" :default "Forfattar")
5222 ("pl" :default "Autor")
5223 ("ru" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5224 ("sv" :html "F&ouml;rfattare")
5225 ("uk" :html "&#1040;&#1074;&#1090;&#1086;&#1088;" :utf-8 "Автор")
5226 ("zh-CN" :html "&#20316;&#32773;" :utf-8 "作者")
5227 ("zh-TW" :html "&#20316;&#32773;" :utf-8 "作者"))
5228 ("Date"
5229 ("ca" :default "Data")
5230 ("cs" :default "Datum")
5231 ("da" :default "Dato")
5232 ("de" :default "Datum")
5233 ("eo" :default "Dato")
5234 ("es" :default "Fecha")
5235 ("fi" :html "P&auml;iv&auml;m&auml;&auml;r&auml;")
5236 ("hu" :html "D&aacute;tum")
5237 ("is" :default "Dagsetning")
5238 ("it" :default "Data")
5239 ("ja" :html "&#26085;&#20184;" :utf-8 "日付")
5240 ("nl" :default "Datum")
5241 ("no" :default "Dato")
5242 ("nb" :default "Dato")
5243 ("nn" :default "Dato")
5244 ("pl" :default "Data")
5245 ("ru" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5246 ("sv" :default "Datum")
5247 ("uk" :html "&#1044;&#1072;&#1090;&#1072;" :utf-8 "Дата")
5248 ("zh-CN" :html "&#26085;&#26399;" :utf-8 "日期")
5249 ("zh-TW" :html "&#26085;&#26399;" :utf-8 "日期"))
5250 ("Equation"
5251 ("da" :default "Ligning")
5252 ("de" :default "Gleichung")
5253 ("es" :html "Ecuaci&oacute;n" :default "Ecuación")
5254 ("fr" :ascii "Equation" :default "Équation")
5255 ("no" :default "Ligning")
5256 ("nb" :default "Ligning")
5257 ("nn" :default "Likning")
5258 ("sv" :default "Ekvation")
5259 ("zh-CN" :html "&#26041;&#31243;" :utf-8 "方程"))
5260 ("Figure"
5261 ("da" :default "Figur")
5262 ("de" :default "Abbildung")
5263 ("es" :default "Figura")
5264 ("ja" :html "&#22259;" :utf-8 "図")
5265 ("no" :default "Illustrasjon")
5266 ("nb" :default "Illustrasjon")
5267 ("nn" :default "Illustrasjon")
5268 ("sv" :default "Illustration")
5269 ("zh-CN" :html "&#22270;" :utf-8 "图"))
5270 ("Figure %d:"
5271 ("da" :default "Figur %d")
5272 ("de" :default "Abbildung %d:")
5273 ("es" :default "Figura %d:")
5274 ("fr" :default "Figure %d :" :html "Figure&nbsp;%d&nbsp;:")
5275 ("ja" :html "&#22259;%d: " :utf-8 "図%d: ")
5276 ("no" :default "Illustrasjon %d")
5277 ("nb" :default "Illustrasjon %d")
5278 ("nn" :default "Illustrasjon %d")
5279 ("sv" :default "Illustration %d")
5280 ("zh-CN" :html "&#22270;%d&nbsp;" :utf-8 "图%d "))
5281 ("Footnotes"
5282 ("ca" :html "Peus de p&agrave;gina")
5283 ("cs" :default "Pozn\xe1mky pod carou")
5284 ("da" :default "Fodnoter")
5285 ("de" :html "Fu&szlig;noten" :default "Fußnoten")
5286 ("eo" :default "Piednotoj")
5287 ("es" :html "Nota al pie de p&aacute;gina" :default "Nota al pie de página")
5288 ("fi" :default "Alaviitteet")
5289 ("fr" :default "Notes de bas de page")
5290 ("hu" :html "L&aacute;bjegyzet")
5291 ("is" :html "Aftanm&aacute;lsgreinar")
5292 ("it" :html "Note a pi&egrave; di pagina")
5293 ("ja" :html "&#33050;&#27880;" :utf-8 "脚注")
5294 ("nl" :default "Voetnoten")
5295 ("no" :default "Fotnoter")
5296 ("nb" :default "Fotnoter")
5297 ("nn" :default "Fotnotar")
5298 ("pl" :default "Przypis")
5299 ("ru" :html "&#1057;&#1085;&#1086;&#1089;&#1082;&#1080;" :utf-8 "Сноски")
5300 ("sv" :default "Fotnoter")
5301 ("uk" :html "&#1055;&#1088;&#1080;&#1084;&#1110;&#1090;&#1082;&#1080;"
5302 :utf-8 "Примітки")
5303 ("zh-CN" :html "&#33050;&#27880;" :utf-8 "脚注")
5304 ("zh-TW" :html "&#33139;&#35387;" :utf-8 "腳註"))
5305 ("List of Listings"
5306 ("da" :default "Programmer")
5307 ("de" :default "Programmauflistungsverzeichnis")
5308 ("es" :default "Indice de Listados de programas")
5309 ("fr" :default "Liste des programmes")
5310 ("no" :default "Dataprogrammer")
5311 ("nb" :default "Dataprogrammer")
5312 ("zh-CN" :html "&#20195;&#30721;&#30446;&#24405;" :utf-8 "代码目录"))
5313 ("List of Tables"
5314 ("da" :default "Tabeller")
5315 ("de" :default "Tabellenverzeichnis")
5316 ("es" :default "Indice de tablas")
5317 ("fr" :default "Liste des tableaux")
5318 ("no" :default "Tabeller")
5319 ("nb" :default "Tabeller")
5320 ("nn" :default "Tabeller")
5321 ("sv" :default "Tabeller")
5322 ("zh-CN" :html "&#34920;&#26684;&#30446;&#24405;" :utf-8 "表格目录"))
5323 ("Listing %d:"
5324 ("da" :default "Program %d")
5325 ("de" :default "Programmlisting %d")
5326 ("es" :default "Listado de programa %d")
5327 ("fr" :default "Programme %d :" :html "Programme&nbsp;%d&nbsp;:")
5328 ("no" :default "Dataprogram")
5329 ("nb" :default "Dataprogram")
5330 ("zh-CN" :html "&#20195;&#30721;%d&nbsp;" :utf-8 "代码%d "))
5331 ("See section %s"
5332 ("da" :default "jævnfør afsnit %s")
5333 ("de" :default "siehe Abschnitt %s")
5334 ("es" :default "vea seccion %s")
5335 ("fr" :default "cf. section %s")
5336 ("zh-CN" :html "&#21442;&#35265;&#31532;%d&#33410;" :utf-8 "参见第%s节"))
5337 ("Table"
5338 ("de" :default "Tabelle")
5339 ("es" :default "Tabla")
5340 ("fr" :default "Tableau")
5341 ("ja" :html "&#34920;" :utf-8 "表")
5342 ("zh-CN" :html "&#34920;" :utf-8 "表"))
5343 ("Table %d:"
5344 ("da" :default "Tabel %d")
5345 ("de" :default "Tabelle %d")
5346 ("es" :default "Tabla %d")
5347 ("fr" :default "Tableau %d :")
5348 ("ja" :html "&#34920;%d:" :utf-8 "表%d:")
5349 ("no" :default "Tabell %d")
5350 ("nb" :default "Tabell %d")
5351 ("nn" :default "Tabell %d")
5352 ("sv" :default "Tabell %d")
5353 ("zh-CN" :html "&#34920;%d&nbsp;" :utf-8 "表%d "))
5354 ("Table of Contents"
5355 ("ca" :html "&Iacute;ndex")
5356 ("cs" :default "Obsah")
5357 ("da" :default "Indhold")
5358 ("de" :default "Inhaltsverzeichnis")
5359 ("eo" :default "Enhavo")
5360 ("es" :html "&Iacute;ndice")
5361 ("fi" :html "Sis&auml;llysluettelo")
5362 ("fr" :ascii "Sommaire" :default "Table des matières")
5363 ("hu" :html "Tartalomjegyz&eacute;k")
5364 ("is" :default "Efnisyfirlit")
5365 ("it" :default "Indice")
5366 ("ja" :html "&#30446;&#27425;" :utf-8 "目次")
5367 ("nl" :default "Inhoudsopgave")
5368 ("no" :default "Innhold")
5369 ("nb" :default "Innhold")
5370 ("nn" :default "Innhald")
5371 ("pl" :html "Spis tre&#x015b;ci")
5372 ("ru" :html "&#1057;&#1086;&#1076;&#1077;&#1088;&#1078;&#1072;&#1085;&#1080;&#1077;"
5373 :utf-8 "Содержание")
5374 ("sv" :html "Inneh&aring;ll")
5375 ("uk" :html "&#1047;&#1084;&#1110;&#1089;&#1090;" :utf-8 "Зміст")
5376 ("zh-CN" :html "&#30446;&#24405;" :utf-8 "目录")
5377 ("zh-TW" :html "&#30446;&#37636;" :utf-8 "目錄"))
5378 ("Unknown reference"
5379 ("da" :default "ukendt reference")
5380 ("de" :default "Unbekannter Verweis")
5381 ("es" :default "referencia desconocida")
5382 ("fr" :ascii "Destination inconnue" :default "Référence inconnue")
5383 ("zh-CN" :html "&#26410;&#30693;&#24341;&#29992;" :utf-8 "未知引用")))
5384 "Dictionary for export engine.
5386 Alist whose CAR is the string to translate and CDR is an alist
5387 whose CAR is the language string and CDR is a plist whose
5388 properties are possible charsets and values translated terms.
5390 It is used as a database for `org-export-translate'. Since this
5391 function returns the string as-is if no translation was found,
5392 the variable only needs to record values different from the
5393 entry.")
5395 (defun org-export-translate (s encoding info)
5396 "Translate string S according to language specification.
5398 ENCODING is a symbol among `:ascii', `:html', `:latex', `:latin1'
5399 and `:utf-8'. INFO is a plist used as a communication channel.
5401 Translation depends on `:language' property. Return the
5402 translated string. If no translation is found, try to fall back
5403 to `:default' encoding. If it fails, return S."
5404 (let* ((lang (plist-get info :language))
5405 (translations (cdr (assoc lang
5406 (cdr (assoc s org-export-dictionary))))))
5407 (or (plist-get translations encoding)
5408 (plist-get translations :default)
5409 s)))
5413 ;;; Asynchronous Export
5415 ;; `org-export-async-start' is the entry point for asynchronous
5416 ;; export. It recreates current buffer (including visibility,
5417 ;; narrowing and visited file) in an external Emacs process, and
5418 ;; evaluates a command there. It then applies a function on the
5419 ;; returned results in the current process.
5421 ;; At a higher level, `org-export-to-buffer' and `org-export-to-file'
5422 ;; allow to export to a buffer or a file, asynchronously or not.
5424 ;; `org-export-output-file-name' is an auxiliary function meant to be
5425 ;; used with `org-export-to-file'. With a given extension, it tries
5426 ;; to provide a canonical file name to write export output to.
5428 ;; Asynchronously generated results are never displayed directly.
5429 ;; Instead, they are stored in `org-export-stack-contents'. They can
5430 ;; then be retrieved by calling `org-export-stack'.
5432 ;; Export Stack is viewed through a dedicated major mode
5433 ;;`org-export-stack-mode' and tools: `org-export-stack-refresh',
5434 ;;`org-export-stack-delete', `org-export-stack-view' and
5435 ;;`org-export-stack-clear'.
5437 ;; For back-ends, `org-export-add-to-stack' add a new source to stack.
5438 ;; It should be used whenever `org-export-async-start' is called.
5440 (defmacro org-export-async-start (fun &rest body)
5441 "Call function FUN on the results returned by BODY evaluation.
5443 BODY evaluation happens in an asynchronous process, from a buffer
5444 which is an exact copy of the current one.
5446 Use `org-export-add-to-stack' in FUN in order to register results
5447 in the stack.
5449 This is a low level function. See also `org-export-to-buffer'
5450 and `org-export-to-file' for more specialized functions."
5451 (declare (indent 1) (debug t))
5452 (org-with-gensyms (process temp-file copy-fun proc-buffer coding)
5453 ;; Write the full sexp evaluating BODY in a copy of the current
5454 ;; buffer to a temporary file, as it may be too long for program
5455 ;; args in `start-process'.
5456 `(with-temp-message "Initializing asynchronous export process"
5457 (let ((,copy-fun (org-export--generate-copy-script (current-buffer)))
5458 (,temp-file (make-temp-file "org-export-process"))
5459 (,coding buffer-file-coding-system))
5460 (with-temp-file ,temp-file
5461 (insert
5462 ;; Null characters (from variable values) are inserted
5463 ;; within the file. As a consequence, coding system for
5464 ;; buffer contents will not be recognized properly. So,
5465 ;; we make sure it is the same as the one used to display
5466 ;; the original buffer.
5467 (format ";; -*- coding: %s; -*-\n%S"
5468 ,coding
5469 `(with-temp-buffer
5470 (when org-export-async-debug '(setq debug-on-error t))
5471 ;; Ignore `kill-emacs-hook' and code evaluation
5472 ;; queries from Babel as we need a truly
5473 ;; non-interactive process.
5474 (setq kill-emacs-hook nil
5475 org-babel-confirm-evaluate-answer-no t)
5476 ;; Initialize export framework.
5477 (require 'ox)
5478 ;; Re-create current buffer there.
5479 (funcall ,,copy-fun)
5480 (restore-buffer-modified-p nil)
5481 ;; Sexp to evaluate in the buffer.
5482 (print (progn ,,@body))))))
5483 ;; Start external process.
5484 (let* ((process-connection-type nil)
5485 (,proc-buffer (generate-new-buffer-name "*Org Export Process*"))
5486 (,process
5487 (start-process
5488 "org-export-process" ,proc-buffer
5489 (expand-file-name invocation-name invocation-directory)
5490 "-Q" "--batch"
5491 "-l" org-export-async-init-file
5492 "-l" ,temp-file)))
5493 ;; Register running process in stack.
5494 (org-export-add-to-stack (get-buffer ,proc-buffer) nil ,process)
5495 ;; Set-up sentinel in order to catch results.
5496 (let ((handler ,fun))
5497 (set-process-sentinel
5498 ,process
5499 `(lambda (p status)
5500 (let ((proc-buffer (process-buffer p)))
5501 (when (eq (process-status p) 'exit)
5502 (unwind-protect
5503 (if (zerop (process-exit-status p))
5504 (unwind-protect
5505 (let ((results
5506 (with-current-buffer proc-buffer
5507 (goto-char (point-max))
5508 (backward-sexp)
5509 (read (current-buffer)))))
5510 (funcall ,handler results))
5511 (unless org-export-async-debug
5512 (and (get-buffer proc-buffer)
5513 (kill-buffer proc-buffer))))
5514 (org-export-add-to-stack proc-buffer nil p)
5515 (ding)
5516 (message "Process '%s' exited abnormally" p))
5517 (unless org-export-async-debug
5518 (delete-file ,,temp-file)))))))))))))
5520 ;;;###autoload
5521 (defun org-export-to-buffer
5522 (backend buffer
5523 &optional async subtreep visible-only body-only ext-plist
5524 post-process)
5525 "Call `org-export-as' with output to a specified buffer.
5527 BACKEND is either an export back-end, as returned by, e.g.,
5528 `org-export-create-backend', or a symbol referring to
5529 a registered back-end.
5531 BUFFER is the name of the output buffer. If it already exists,
5532 it will be erased first, otherwise, it will be created.
5534 A non-nil optional argument ASYNC means the process should happen
5535 asynchronously. The resulting buffer should then be accessible
5536 through the `org-export-stack' interface. When ASYNC is nil, the
5537 buffer is displayed if `org-export-show-temporary-export-buffer'
5538 is non-nil.
5540 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5541 EXT-PLIST are similar to those used in `org-export-as', which
5542 see.
5544 Optional argument POST-PROCESS is a function which should accept
5545 no argument. It is called within the current process, from
5546 BUFFER, with point at its beginning. Export back-ends can use it
5547 to set a major mode there, e.g,
5549 \(defun org-latex-export-as-latex
5550 \(&optional async subtreep visible-only body-only ext-plist)
5551 \(interactive)
5552 \(org-export-to-buffer 'latex \"*Org LATEX Export*\"
5553 async subtreep visible-only body-only ext-plist (lambda () (LaTeX-mode))))
5555 This function returns BUFFER."
5556 (declare (indent 2))
5557 (if async
5558 (org-export-async-start
5559 `(lambda (output)
5560 (with-current-buffer (get-buffer-create ,buffer)
5561 (erase-buffer)
5562 (setq buffer-file-coding-system ',buffer-file-coding-system)
5563 (insert output)
5564 (goto-char (point-min))
5565 (org-export-add-to-stack (current-buffer) ',backend)
5566 (ignore-errors (funcall ,post-process))))
5567 `(org-export-as
5568 ',backend ,subtreep ,visible-only ,body-only ',ext-plist))
5569 (let ((output
5570 (org-export-as backend subtreep visible-only body-only ext-plist))
5571 (buffer (get-buffer-create buffer))
5572 (encoding buffer-file-coding-system))
5573 (when (and (org-string-nw-p output) (org-export--copy-to-kill-ring-p))
5574 (org-kill-new output))
5575 (with-current-buffer buffer
5576 (erase-buffer)
5577 (setq buffer-file-coding-system encoding)
5578 (insert output)
5579 (goto-char (point-min))
5580 (and (functionp post-process) (funcall post-process)))
5581 (when org-export-show-temporary-export-buffer
5582 (switch-to-buffer-other-window buffer))
5583 buffer)))
5585 ;;;###autoload
5586 (defun org-export-to-file
5587 (backend file &optional async subtreep visible-only body-only ext-plist
5588 post-process)
5589 "Call `org-export-as' with output to a specified file.
5591 BACKEND is either an export back-end, as returned by, e.g.,
5592 `org-export-create-backend', or a symbol referring to
5593 a registered back-end. FILE is the name of the output file, as
5594 a string.
5596 A non-nil optional argument ASYNC means the process should happen
5597 asynchronously. The resulting buffer file then be accessible
5598 through the `org-export-stack' interface.
5600 Optional arguments SUBTREEP, VISIBLE-ONLY, BODY-ONLY and
5601 EXT-PLIST are similar to those used in `org-export-as', which
5602 see.
5604 Optional argument POST-PROCESS is called with FILE as its
5605 argument, in the asynchronous process. It has to return a file
5606 name, or nil. Export back-ends can use this to send the output
5607 file through additional processing, e.g,
5609 \(defun org-latex-export-to-latex
5610 \(&optional async subtreep visible-only body-only ext-plist)
5611 \(interactive)
5612 \(let ((outfile (org-export-output-file-name \".tex\" subtreep)))
5613 \(org-export-to-file 'latex outfile
5614 async subtreep visible-only body-only ext-plist
5615 \(lambda (file) (org-latex-compile file)))
5617 The function returns either a file name returned by POST-PROCESS,
5618 or FILE."
5619 (declare (indent 2))
5620 (if (not (file-writable-p file)) (error "Output file not writable")
5621 (let ((encoding (or org-export-coding-system buffer-file-coding-system)))
5622 (if async
5623 (org-export-async-start
5624 `(lambda (file)
5625 (org-export-add-to-stack (expand-file-name file) ',backend))
5626 `(let ((output
5627 (org-export-as
5628 ',backend ,subtreep ,visible-only ,body-only
5629 ',ext-plist)))
5630 (with-temp-buffer
5631 (insert output)
5632 (let ((coding-system-for-write ',encoding))
5633 (write-file ,file)))
5634 (or (ignore-errors (funcall ',post-process ,file)) ,file)))
5635 (let ((output (org-export-as
5636 backend subtreep visible-only body-only ext-plist)))
5637 (with-temp-buffer
5638 (insert output)
5639 (let ((coding-system-for-write encoding))
5640 (write-file file)))
5641 (when (and (org-export--copy-to-kill-ring-p) (org-string-nw-p output))
5642 (org-kill-new output))
5643 ;; Get proper return value.
5644 (or (and (functionp post-process) (funcall post-process file))
5645 file))))))
5647 (defun org-export-output-file-name (extension &optional subtreep pub-dir)
5648 "Return output file's name according to buffer specifications.
5650 EXTENSION is a string representing the output file extension,
5651 with the leading dot.
5653 With a non-nil optional argument SUBTREEP, try to determine
5654 output file's name by looking for \"EXPORT_FILE_NAME\" property
5655 of subtree at point.
5657 When optional argument PUB-DIR is set, use it as the publishing
5658 directory.
5660 When optional argument VISIBLE-ONLY is non-nil, don't export
5661 contents of hidden elements.
5663 Return file name as a string."
5664 (let* ((visited-file (buffer-file-name (buffer-base-buffer)))
5665 (base-name
5666 ;; File name may come from EXPORT_FILE_NAME subtree
5667 ;; property, assuming point is at beginning of said
5668 ;; sub-tree.
5669 (file-name-sans-extension
5670 (or (and subtreep
5671 (org-entry-get
5672 (save-excursion
5673 (ignore-errors (org-back-to-heading) (point)))
5674 "EXPORT_FILE_NAME" t))
5675 ;; File name may be extracted from buffer's associated
5676 ;; file, if any.
5677 (and visited-file (file-name-nondirectory visited-file))
5678 ;; Can't determine file name on our own: Ask user.
5679 (let ((read-file-name-function
5680 (and org-completion-use-ido 'ido-read-file-name)))
5681 (read-file-name
5682 "Output file: " pub-dir nil nil nil
5683 (lambda (name)
5684 (string= (file-name-extension name t) extension)))))))
5685 (output-file
5686 ;; Build file name. Enforce EXTENSION over whatever user
5687 ;; may have come up with. PUB-DIR, if defined, always has
5688 ;; precedence over any provided path.
5689 (cond
5690 (pub-dir
5691 (concat (file-name-as-directory pub-dir)
5692 (file-name-nondirectory base-name)
5693 extension))
5694 ((file-name-absolute-p base-name) (concat base-name extension))
5695 (t (concat (file-name-as-directory ".") base-name extension)))))
5696 ;; If writing to OUTPUT-FILE would overwrite original file, append
5697 ;; EXTENSION another time to final name.
5698 (if (and visited-file (org-file-equal-p visited-file output-file))
5699 (concat output-file extension)
5700 output-file)))
5702 (defun org-export-add-to-stack (source backend &optional process)
5703 "Add a new result to export stack if not present already.
5705 SOURCE is a buffer or a file name containing export results.
5706 BACKEND is a symbol representing export back-end used to generate
5709 Entries already pointing to SOURCE and unavailable entries are
5710 removed beforehand. Return the new stack."
5711 (setq org-export-stack-contents
5712 (cons (list source backend (or process (current-time)))
5713 (org-export-stack-remove source))))
5715 (defun org-export-stack ()
5716 "Menu for asynchronous export results and running processes."
5717 (interactive)
5718 (let ((buffer (get-buffer-create "*Org Export Stack*")))
5719 (set-buffer buffer)
5720 (when (zerop (buffer-size)) (org-export-stack-mode))
5721 (org-export-stack-refresh)
5722 (pop-to-buffer buffer))
5723 (message "Type \"q\" to quit, \"?\" for help"))
5725 (defun org-export--stack-source-at-point ()
5726 "Return source from export results at point in stack."
5727 (let ((source (car (nth (1- (org-current-line)) org-export-stack-contents))))
5728 (if (not source) (error "Source unavailable, please refresh buffer")
5729 (let ((source-name (if (stringp source) source (buffer-name source))))
5730 (if (save-excursion
5731 (beginning-of-line)
5732 (looking-at (concat ".* +" (regexp-quote source-name) "$")))
5733 source
5734 ;; SOURCE is not consistent with current line. The stack
5735 ;; view is outdated.
5736 (error "Source unavailable; type `g' to update buffer"))))))
5738 (defun org-export-stack-clear ()
5739 "Remove all entries from export stack."
5740 (interactive)
5741 (setq org-export-stack-contents nil))
5743 (defun org-export-stack-refresh (&rest dummy)
5744 "Refresh the asynchronous export stack.
5745 DUMMY is ignored. Unavailable sources are removed from the list.
5746 Return the new stack."
5747 (let ((inhibit-read-only t))
5748 (org-preserve-lc
5749 (erase-buffer)
5750 (insert (concat
5751 (let ((counter 0))
5752 (mapconcat
5753 (lambda (entry)
5754 (let ((proc-p (processp (nth 2 entry))))
5755 (concat
5756 ;; Back-end.
5757 (format " %-12s " (or (nth 1 entry) ""))
5758 ;; Age.
5759 (let ((data (nth 2 entry)))
5760 (if proc-p (format " %6s " (process-status data))
5761 ;; Compute age of the results.
5762 (org-format-seconds
5763 "%4h:%.2m "
5764 (float-time (time-since data)))))
5765 ;; Source.
5766 (format " %s"
5767 (let ((source (car entry)))
5768 (if (stringp source) source
5769 (buffer-name source)))))))
5770 ;; Clear stack from exited processes, dead buffers or
5771 ;; non-existent files.
5772 (setq org-export-stack-contents
5773 (org-remove-if-not
5774 (lambda (el)
5775 (if (processp (nth 2 el))
5776 (buffer-live-p (process-buffer (nth 2 el)))
5777 (let ((source (car el)))
5778 (if (bufferp source) (buffer-live-p source)
5779 (file-exists-p source)))))
5780 org-export-stack-contents)) "\n")))))))
5782 (defun org-export-stack-remove (&optional source)
5783 "Remove export results at point from stack.
5784 If optional argument SOURCE is non-nil, remove it instead."
5785 (interactive)
5786 (let ((source (or source (org-export--stack-source-at-point))))
5787 (setq org-export-stack-contents
5788 (org-remove-if (lambda (el) (equal (car el) source))
5789 org-export-stack-contents))))
5791 (defun org-export-stack-view (&optional in-emacs)
5792 "View export results at point in stack.
5793 With an optional prefix argument IN-EMACS, force viewing files
5794 within Emacs."
5795 (interactive "P")
5796 (let ((source (org-export--stack-source-at-point)))
5797 (cond ((processp source)
5798 (org-switch-to-buffer-other-window (process-buffer source)))
5799 ((bufferp source) (org-switch-to-buffer-other-window source))
5800 (t (org-open-file source in-emacs)))))
5802 (defvar org-export-stack-mode-map
5803 (let ((km (make-sparse-keymap)))
5804 (define-key km " " 'next-line)
5805 (define-key km "n" 'next-line)
5806 (define-key km "\C-n" 'next-line)
5807 (define-key km [down] 'next-line)
5808 (define-key km "p" 'previous-line)
5809 (define-key km "\C-p" 'previous-line)
5810 (define-key km "\C-?" 'previous-line)
5811 (define-key km [up] 'previous-line)
5812 (define-key km "C" 'org-export-stack-clear)
5813 (define-key km "v" 'org-export-stack-view)
5814 (define-key km (kbd "RET") 'org-export-stack-view)
5815 (define-key km "d" 'org-export-stack-remove)
5817 "Keymap for Org Export Stack.")
5819 (define-derived-mode org-export-stack-mode special-mode "Org-Stack"
5820 "Mode for displaying asynchronous export stack.
5822 Type \\[org-export-stack] to visualize the asynchronous export
5823 stack.
5825 In an Org Export Stack buffer, use \\<org-export-stack-mode-map>\\[org-export-stack-view] to view export output
5826 on current line, \\[org-export-stack-remove] to remove it from the stack and \\[org-export-stack-clear] to clear
5827 stack completely.
5829 Removing entries in an Org Export Stack buffer doesn't affect
5830 files or buffers, only the display.
5832 \\{org-export-stack-mode-map}"
5833 (abbrev-mode 0)
5834 (auto-fill-mode 0)
5835 (setq buffer-read-only t
5836 buffer-undo-list t
5837 truncate-lines t
5838 header-line-format
5839 '(:eval
5840 (format " %-12s | %6s | %s" "Back-End" "Age" "Source")))
5841 (org-add-hook 'post-command-hook 'org-export-stack-refresh nil t)
5842 (set (make-local-variable 'revert-buffer-function)
5843 'org-export-stack-refresh))
5847 ;;; The Dispatcher
5849 ;; `org-export-dispatch' is the standard interactive way to start an
5850 ;; export process. It uses `org-export--dispatch-ui' as a subroutine
5851 ;; for its interface, which, in turn, delegates response to key
5852 ;; pressed to `org-export--dispatch-action'.
5854 ;;;###autoload
5855 (defun org-export-dispatch (&optional arg)
5856 "Export dispatcher for Org mode.
5858 It provides an access to common export related tasks in a buffer.
5859 Its interface comes in two flavours: standard and expert.
5861 While both share the same set of bindings, only the former
5862 displays the valid keys associations in a dedicated buffer.
5863 Scrolling (resp. line-wise motion) in this buffer is done with
5864 SPC and DEL (resp. C-n and C-p) keys.
5866 Set variable `org-export-dispatch-use-expert-ui' to switch to one
5867 flavour or the other.
5869 When ARG is \\[universal-argument], repeat the last export action, with the same set
5870 of options used back then, on the current buffer.
5872 When ARG is \\[universal-argument] \\[universal-argument], display the asynchronous export stack."
5873 (interactive "P")
5874 (let* ((input
5875 (cond ((equal arg '(16)) '(stack))
5876 ((and arg org-export-dispatch-last-action))
5877 (t (save-window-excursion
5878 (unwind-protect
5879 (progn
5880 ;; Remember where we are
5881 (move-marker org-export-dispatch-last-position
5882 (point)
5883 (org-base-buffer (current-buffer)))
5884 ;; Get and store an export command
5885 (setq org-export-dispatch-last-action
5886 (org-export--dispatch-ui
5887 (list org-export-initial-scope
5888 (and org-export-in-background 'async))
5890 org-export-dispatch-use-expert-ui)))
5891 (and (get-buffer "*Org Export Dispatcher*")
5892 (kill-buffer "*Org Export Dispatcher*")))))))
5893 (action (car input))
5894 (optns (cdr input)))
5895 (unless (memq 'subtree optns)
5896 (move-marker org-export-dispatch-last-position nil))
5897 (case action
5898 ;; First handle special hard-coded actions.
5899 (template (org-export-insert-default-template nil optns))
5900 (stack (org-export-stack))
5901 (publish-current-file
5902 (org-publish-current-file (memq 'force optns) (memq 'async optns)))
5903 (publish-current-project
5904 (org-publish-current-project (memq 'force optns) (memq 'async optns)))
5905 (publish-choose-project
5906 (org-publish (assoc (org-icompleting-read
5907 "Publish project: "
5908 org-publish-project-alist nil t)
5909 org-publish-project-alist)
5910 (memq 'force optns)
5911 (memq 'async optns)))
5912 (publish-all (org-publish-all (memq 'force optns) (memq 'async optns)))
5913 (otherwise
5914 (save-excursion
5915 (when arg
5916 ;; Repeating command, maybe move cursor to restore subtree
5917 ;; context.
5918 (if (eq (marker-buffer org-export-dispatch-last-position)
5919 (org-base-buffer (current-buffer)))
5920 (goto-char org-export-dispatch-last-position)
5921 ;; We are in a different buffer, forget position.
5922 (move-marker org-export-dispatch-last-position nil)))
5923 (funcall action
5924 ;; Return a symbol instead of a list to ease
5925 ;; asynchronous export macro use.
5926 (and (memq 'async optns) t)
5927 (and (memq 'subtree optns) t)
5928 (and (memq 'visible optns) t)
5929 (and (memq 'body optns) t)))))))
5931 (defun org-export--dispatch-ui (options first-key expertp)
5932 "Handle interface for `org-export-dispatch'.
5934 OPTIONS is a list containing current interactive options set for
5935 export. It can contain any of the following symbols:
5936 `body' toggles a body-only export
5937 `subtree' restricts export to current subtree
5938 `visible' restricts export to visible part of buffer.
5939 `force' force publishing files.
5940 `async' use asynchronous export process
5942 FIRST-KEY is the key pressed to select the first level menu. It
5943 is nil when this menu hasn't been selected yet.
5945 EXPERTP, when non-nil, triggers expert UI. In that case, no help
5946 buffer is provided, but indications about currently active
5947 options are given in the prompt. Moreover, \[?] allows to switch
5948 back to standard interface."
5949 (let* ((fontify-key
5950 (lambda (key &optional access-key)
5951 ;; Fontify KEY string. Optional argument ACCESS-KEY, when
5952 ;; non-nil is the required first-level key to activate
5953 ;; KEY. When its value is t, activate KEY independently
5954 ;; on the first key, if any. A nil value means KEY will
5955 ;; only be activated at first level.
5956 (if (or (eq access-key t) (eq access-key first-key))
5957 (org-propertize key 'face 'org-warning)
5958 key)))
5959 (fontify-value
5960 (lambda (value)
5961 ;; Fontify VALUE string.
5962 (org-propertize value 'face 'font-lock-variable-name-face)))
5963 ;; Prepare menu entries by extracting them from registered
5964 ;; back-ends and sorting them by access key and by ordinal,
5965 ;; if any.
5966 (entries
5967 (sort (sort (delq nil
5968 (mapcar 'org-export-backend-menu
5969 org-export--registered-backends))
5970 (lambda (a b)
5971 (let ((key-a (nth 1 a))
5972 (key-b (nth 1 b)))
5973 (cond ((and (numberp key-a) (numberp key-b))
5974 (< key-a key-b))
5975 ((numberp key-b) t)))))
5976 'car-less-than-car))
5977 ;; Compute a list of allowed keys based on the first key
5978 ;; pressed, if any. Some keys
5979 ;; (?^B, ?^V, ?^S, ?^F, ?^A, ?&, ?# and ?q) are always
5980 ;; available.
5981 (allowed-keys
5982 (nconc (list 2 22 19 6 1)
5983 (if (not first-key) (org-uniquify (mapcar 'car entries))
5984 (let (sub-menu)
5985 (dolist (entry entries (sort (mapcar 'car sub-menu) '<))
5986 (when (eq (car entry) first-key)
5987 (setq sub-menu (append (nth 2 entry) sub-menu))))))
5988 (cond ((eq first-key ?P) (list ?f ?p ?x ?a))
5989 ((not first-key) (list ?P)))
5990 (list ?& ?#)
5991 (when expertp (list ??))
5992 (list ?q)))
5993 ;; Build the help menu for standard UI.
5994 (help
5995 (unless expertp
5996 (concat
5997 ;; Options are hard-coded.
5998 (format "[%s] Body only: %s [%s] Visible only: %s
5999 \[%s] Export scope: %s [%s] Force publishing: %s
6000 \[%s] Async export: %s\n\n"
6001 (funcall fontify-key "C-b" t)
6002 (funcall fontify-value
6003 (if (memq 'body options) "On " "Off"))
6004 (funcall fontify-key "C-v" t)
6005 (funcall fontify-value
6006 (if (memq 'visible options) "On " "Off"))
6007 (funcall fontify-key "C-s" t)
6008 (funcall fontify-value
6009 (if (memq 'subtree options) "Subtree" "Buffer "))
6010 (funcall fontify-key "C-f" t)
6011 (funcall fontify-value
6012 (if (memq 'force options) "On " "Off"))
6013 (funcall fontify-key "C-a" t)
6014 (funcall fontify-value
6015 (if (memq 'async options) "On " "Off")))
6016 ;; Display registered back-end entries. When a key
6017 ;; appears for the second time, do not create another
6018 ;; entry, but append its sub-menu to existing menu.
6019 (let (last-key)
6020 (mapconcat
6021 (lambda (entry)
6022 (let ((top-key (car entry)))
6023 (concat
6024 (unless (eq top-key last-key)
6025 (setq last-key top-key)
6026 (format "\n[%s] %s\n"
6027 (funcall fontify-key (char-to-string top-key))
6028 (nth 1 entry)))
6029 (let ((sub-menu (nth 2 entry)))
6030 (unless (functionp sub-menu)
6031 ;; Split sub-menu into two columns.
6032 (let ((index -1))
6033 (concat
6034 (mapconcat
6035 (lambda (sub-entry)
6036 (incf index)
6037 (format
6038 (if (zerop (mod index 2)) " [%s] %-26s"
6039 "[%s] %s\n")
6040 (funcall fontify-key
6041 (char-to-string (car sub-entry))
6042 top-key)
6043 (nth 1 sub-entry)))
6044 sub-menu "")
6045 (when (zerop (mod index 2)) "\n"))))))))
6046 entries ""))
6047 ;; Publishing menu is hard-coded.
6048 (format "\n[%s] Publish
6049 [%s] Current file [%s] Current project
6050 [%s] Choose project [%s] All projects\n\n\n"
6051 (funcall fontify-key "P")
6052 (funcall fontify-key "f" ?P)
6053 (funcall fontify-key "p" ?P)
6054 (funcall fontify-key "x" ?P)
6055 (funcall fontify-key "a" ?P))
6056 (format "[%s] Export stack [%s] Insert template\n"
6057 (funcall fontify-key "&" t)
6058 (funcall fontify-key "#" t))
6059 (format "[%s] %s"
6060 (funcall fontify-key "q" t)
6061 (if first-key "Main menu" "Exit")))))
6062 ;; Build prompts for both standard and expert UI.
6063 (standard-prompt (unless expertp "Export command: "))
6064 (expert-prompt
6065 (when expertp
6066 (format
6067 "Export command (C-%s%s%s%s%s) [%s]: "
6068 (if (memq 'body options) (funcall fontify-key "b" t) "b")
6069 (if (memq 'visible options) (funcall fontify-key "v" t) "v")
6070 (if (memq 'subtree options) (funcall fontify-key "s" t) "s")
6071 (if (memq 'force options) (funcall fontify-key "f" t) "f")
6072 (if (memq 'async options) (funcall fontify-key "a" t) "a")
6073 (mapconcat (lambda (k)
6074 ;; Strip control characters.
6075 (unless (< k 27) (char-to-string k)))
6076 allowed-keys "")))))
6077 ;; With expert UI, just read key with a fancy prompt. In standard
6078 ;; UI, display an intrusive help buffer.
6079 (if expertp
6080 (org-export--dispatch-action
6081 expert-prompt allowed-keys entries options first-key expertp)
6082 ;; At first call, create frame layout in order to display menu.
6083 (unless (get-buffer "*Org Export Dispatcher*")
6084 (delete-other-windows)
6085 (org-switch-to-buffer-other-window
6086 (get-buffer-create "*Org Export Dispatcher*"))
6087 (setq cursor-type nil
6088 header-line-format "Use SPC, DEL, C-n or C-p to navigate.")
6089 ;; Make sure that invisible cursor will not highlight square
6090 ;; brackets.
6091 (set-syntax-table (copy-syntax-table))
6092 (modify-syntax-entry ?\[ "w"))
6093 ;; At this point, the buffer containing the menu exists and is
6094 ;; visible in the current window. So, refresh it.
6095 (with-current-buffer "*Org Export Dispatcher*"
6096 ;; Refresh help. Maintain display continuity by re-visiting
6097 ;; previous window position.
6098 (let ((pos (window-start)))
6099 (erase-buffer)
6100 (insert help)
6101 (set-window-start nil pos)))
6102 (org-fit-window-to-buffer)
6103 (org-export--dispatch-action
6104 standard-prompt allowed-keys entries options first-key expertp))))
6106 (defun org-export--dispatch-action
6107 (prompt allowed-keys entries options first-key expertp)
6108 "Read a character from command input and act accordingly.
6110 PROMPT is the displayed prompt, as a string. ALLOWED-KEYS is
6111 a list of characters available at a given step in the process.
6112 ENTRIES is a list of menu entries. OPTIONS, FIRST-KEY and
6113 EXPERTP are the same as defined in `org-export--dispatch-ui',
6114 which see.
6116 Toggle export options when required. Otherwise, return value is
6117 a list with action as CAR and a list of interactive export
6118 options as CDR."
6119 (let (key)
6120 ;; Scrolling: when in non-expert mode, act on motion keys (C-n,
6121 ;; C-p, SPC, DEL).
6122 (while (and (setq key (read-char-exclusive prompt))
6123 (not expertp)
6124 (memq key '(14 16 ?\s ?\d)))
6125 (case key
6126 (14 (if (not (pos-visible-in-window-p (point-max)))
6127 (ignore-errors (scroll-up 1))
6128 (message "End of buffer")
6129 (sit-for 1)))
6130 (16 (if (not (pos-visible-in-window-p (point-min)))
6131 (ignore-errors (scroll-down 1))
6132 (message "Beginning of buffer")
6133 (sit-for 1)))
6134 (?\s (if (not (pos-visible-in-window-p (point-max)))
6135 (scroll-up nil)
6136 (message "End of buffer")
6137 (sit-for 1)))
6138 (?\d (if (not (pos-visible-in-window-p (point-min)))
6139 (scroll-down nil)
6140 (message "Beginning of buffer")
6141 (sit-for 1)))))
6142 (cond
6143 ;; Ignore undefined associations.
6144 ((not (memq key allowed-keys))
6145 (ding)
6146 (unless expertp (message "Invalid key") (sit-for 1))
6147 (org-export--dispatch-ui options first-key expertp))
6148 ;; q key at first level aborts export. At second level, cancel
6149 ;; first key instead.
6150 ((eq key ?q) (if (not first-key) (error "Export aborted")
6151 (org-export--dispatch-ui options nil expertp)))
6152 ;; Help key: Switch back to standard interface if expert UI was
6153 ;; active.
6154 ((eq key ??) (org-export--dispatch-ui options first-key nil))
6155 ;; Send request for template insertion along with export scope.
6156 ((eq key ?#) (cons 'template (memq 'subtree options)))
6157 ;; Switch to asynchronous export stack.
6158 ((eq key ?&) '(stack))
6159 ;; Toggle options: C-b (2) C-v (22) C-s (19) C-f (6) C-a (1).
6160 ((memq key '(2 22 19 6 1))
6161 (org-export--dispatch-ui
6162 (let ((option (case key (2 'body) (22 'visible) (19 'subtree)
6163 (6 'force) (1 'async))))
6164 (if (memq option options) (remq option options)
6165 (cons option options)))
6166 first-key expertp))
6167 ;; Action selected: Send key and options back to
6168 ;; `org-export-dispatch'.
6169 ((or first-key (functionp (nth 2 (assq key entries))))
6170 (cons (cond
6171 ((not first-key) (nth 2 (assq key entries)))
6172 ;; Publishing actions are hard-coded. Send a special
6173 ;; signal to `org-export-dispatch'.
6174 ((eq first-key ?P)
6175 (case key
6176 (?f 'publish-current-file)
6177 (?p 'publish-current-project)
6178 (?x 'publish-choose-project)
6179 (?a 'publish-all)))
6180 ;; Return first action associated to FIRST-KEY + KEY
6181 ;; path. Indeed, derived backends can share the same
6182 ;; FIRST-KEY.
6183 (t (catch 'found
6184 (mapc (lambda (entry)
6185 (let ((match (assq key (nth 2 entry))))
6186 (when match (throw 'found (nth 2 match)))))
6187 (member (assq first-key entries) entries)))))
6188 options))
6189 ;; Otherwise, enter sub-menu.
6190 (t (org-export--dispatch-ui options key expertp)))))
6194 (provide 'ox)
6196 ;; Local variables:
6197 ;; generated-autoload-file: "org-loaddefs.el"
6198 ;; End:
6200 ;;; ox.el ends here