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